blob: b64187b8777386cd98a84b82aa6316a41e96c94c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Routines having to do with the 'struct sk_buff' memory handlers.
3 *
Alan Cox113aa832008-10-13 19:01:08 -07004 * Authors: Alan Cox <alan@lxorguk.ukuu.org.uk>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Florian La Roche <rzsfl@rz.uni-sb.de>
6 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * Fixes:
8 * Alan Cox : Fixed the worst of the load
9 * balancer bugs.
10 * Dave Platt : Interrupt stacking fix.
11 * Richard Kooijman : Timestamp fixes.
12 * Alan Cox : Changed buffer format.
13 * Alan Cox : destructor hook for AF_UNIX etc.
14 * Linus Torvalds : Better skb_clone.
15 * Alan Cox : Added skb_copy.
16 * Alan Cox : Added all the changed routines Linus
17 * only put in the headers
18 * Ray VanTassle : Fixed --skb->lock in free
19 * Alan Cox : skb_copy copy arp field
20 * Andi Kleen : slabified it.
21 * Robert Olsson : Removed skb_head_pool
22 *
23 * NOTE:
24 * The __skb_ routines should be called with interrupts
25 * disabled, or you better be *real* sure that the operation is atomic
26 * with respect to whatever list is being frobbed (e.g. via lock_sock()
27 * or via disabling bottom half handlers, etc).
28 *
29 * This program is free software; you can redistribute it and/or
30 * modify it under the terms of the GNU General Public License
31 * as published by the Free Software Foundation; either version
32 * 2 of the License, or (at your option) any later version.
33 */
34
35/*
36 * The functions in this file will not compile correctly with gcc 2.4.x
37 */
38
Joe Perchese005d192012-05-16 19:58:40 +000039#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/module.h>
42#include <linux/types.h>
43#include <linux/kernel.h>
Vegard Nossumfe55f6d2008-08-30 12:16:35 +020044#include <linux/kmemcheck.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/mm.h>
46#include <linux/interrupt.h>
47#include <linux/in.h>
48#include <linux/inet.h>
49#include <linux/slab.h>
Florian Westphalde960aa2014-01-26 10:58:16 +010050#include <linux/tcp.h>
51#include <linux/udp.h>
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
74#include <asm/uaccess.h>
Steven Rostedtad8d75f2009-04-14 19:39:12 -040075#include <trace/events/skb.h>
Eric Dumazet51c56b02012-04-05 11:35:15 +020076#include <linux/highmem.h>
Willem de Bruijnb245be12015-01-30 13:29:32 -050077#include <linux/capability.h>
78#include <linux/user_namespace.h>
Al Viroa1f8e7f72006-10-19 16:08:53 -040079
Eric Dumazetd7e88832012-04-30 08:10:34 +000080struct kmem_cache *skbuff_head_cache __read_mostly;
Christoph Lametere18b8902006-12-06 20:33:20 -080081static struct kmem_cache *skbuff_fclone_cache __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Linus Torvalds1da177e2005-04-16 15:20:36 -070083/**
Jean Sacrenf05de732013-02-11 13:30:38 +000084 * skb_panic - private function for out-of-line support
85 * @skb: buffer
86 * @sz: size
87 * @addr: address
James Hogan99d58512013-02-13 11:20:27 +000088 * @msg: skb_over_panic or skb_under_panic
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 *
Jean Sacrenf05de732013-02-11 13:30:38 +000090 * Out-of-line support for skb_put() and skb_push().
91 * Called via the wrapper skb_over_panic() or skb_under_panic().
92 * Keep out of line to prevent kernel bloat.
93 * __builtin_return_address is not used because it is not always reliable.
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 */
Jean Sacrenf05de732013-02-11 13:30:38 +000095static void skb_panic(struct sk_buff *skb, unsigned int sz, void *addr,
James Hogan99d58512013-02-13 11:20:27 +000096 const char msg[])
Linus Torvalds1da177e2005-04-16 15:20:36 -070097{
Joe Perchese005d192012-05-16 19:58:40 +000098 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 +000099 msg, addr, skb->len, sz, skb->head, skb->data,
Joe Perchese005d192012-05-16 19:58:40 +0000100 (unsigned long)skb->tail, (unsigned long)skb->end,
101 skb->dev ? skb->dev->name : "<NULL>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 BUG();
103}
104
Jean Sacrenf05de732013-02-11 13:30:38 +0000105static void skb_over_panic(struct sk_buff *skb, unsigned int sz, void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106{
Jean Sacrenf05de732013-02-11 13:30:38 +0000107 skb_panic(skb, sz, addr, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108}
109
Jean Sacrenf05de732013-02-11 13:30:38 +0000110static void skb_under_panic(struct sk_buff *skb, unsigned int sz, void *addr)
111{
112 skb_panic(skb, sz, addr, __func__);
113}
Mel Gormanc93bdd02012-07-31 16:44:19 -0700114
115/*
116 * kmalloc_reserve is a wrapper around kmalloc_node_track_caller that tells
117 * the caller if emergency pfmemalloc reserves are being used. If it is and
118 * the socket is later found to be SOCK_MEMALLOC then PFMEMALLOC reserves
119 * may be used. Otherwise, the packet data may be discarded until enough
120 * memory is free
121 */
122#define kmalloc_reserve(size, gfp, node, pfmemalloc) \
123 __kmalloc_reserve(size, gfp, node, _RET_IP_, pfmemalloc)
stephen hemminger61c5e882012-12-28 18:24:28 +0000124
125static void *__kmalloc_reserve(size_t size, gfp_t flags, int node,
126 unsigned long ip, bool *pfmemalloc)
Mel Gormanc93bdd02012-07-31 16:44:19 -0700127{
128 void *obj;
129 bool ret_pfmemalloc = false;
130
131 /*
132 * Try a regular allocation, when that fails and we're not entitled
133 * to the reserves, fail.
134 */
135 obj = kmalloc_node_track_caller(size,
136 flags | __GFP_NOMEMALLOC | __GFP_NOWARN,
137 node);
138 if (obj || !(gfp_pfmemalloc_allowed(flags)))
139 goto out;
140
141 /* Try again but now we are using pfmemalloc reserves */
142 ret_pfmemalloc = true;
143 obj = kmalloc_node_track_caller(size, flags, node);
144
145out:
146 if (pfmemalloc)
147 *pfmemalloc = ret_pfmemalloc;
148
149 return obj;
150}
151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152/* Allocate a new skbuff. We do this ourselves so we can fill in a few
153 * 'private' fields and also do memory statistics to find all the
154 * [BEEP] leaks.
155 *
156 */
157
Patrick McHardy0ebd0ac2013-04-17 06:46:58 +0000158struct sk_buff *__alloc_skb_head(gfp_t gfp_mask, int node)
159{
160 struct sk_buff *skb;
161
162 /* Get the HEAD */
163 skb = kmem_cache_alloc_node(skbuff_head_cache,
164 gfp_mask & ~__GFP_DMA, node);
165 if (!skb)
166 goto out;
167
168 /*
169 * Only clear those fields we need to clear, not those that we will
170 * actually initialise below. Hence, don't put any more fields after
171 * the tail pointer in struct sk_buff!
172 */
173 memset(skb, 0, offsetof(struct sk_buff, tail));
Pablo Neira5e71d9d2013-06-03 09:28:43 +0000174 skb->head = NULL;
Patrick McHardy0ebd0ac2013-04-17 06:46:58 +0000175 skb->truesize = sizeof(struct sk_buff);
176 atomic_set(&skb->users, 1);
177
Cong Wang35d04612013-05-29 15:16:05 +0800178 skb->mac_header = (typeof(skb->mac_header))~0U;
Patrick McHardy0ebd0ac2013-04-17 06:46:58 +0000179out:
180 return skb;
181}
182
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183/**
David S. Millerd179cd12005-08-17 14:57:30 -0700184 * __alloc_skb - allocate a network buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 * @size: size to allocate
186 * @gfp_mask: allocation mask
Mel Gormanc93bdd02012-07-31 16:44:19 -0700187 * @flags: If SKB_ALLOC_FCLONE is set, allocate from fclone cache
188 * instead of head cache and allocate a cloned (child) skb.
189 * If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for
190 * allocations in case the data is required for writeback
Christoph Hellwigb30973f2006-12-06 20:32:36 -0800191 * @node: numa node to allocate memory on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 *
193 * Allocate a new &sk_buff. The returned buffer has no headroom and a
Ben Hutchings94b60422012-06-06 15:23:37 +0000194 * tail room of at least size bytes. The object has a reference count
195 * of one. The return is the buffer. On a failure the return is %NULL.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 *
197 * Buffers may only be allocated from interrupts using a @gfp_mask of
198 * %GFP_ATOMIC.
199 */
Al Virodd0fc662005-10-07 07:46:04 +0100200struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
Mel Gormanc93bdd02012-07-31 16:44:19 -0700201 int flags, int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
Christoph Lametere18b8902006-12-06 20:33:20 -0800203 struct kmem_cache *cache;
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800204 struct skb_shared_info *shinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 struct sk_buff *skb;
206 u8 *data;
Mel Gormanc93bdd02012-07-31 16:44:19 -0700207 bool pfmemalloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Mel Gormanc93bdd02012-07-31 16:44:19 -0700209 cache = (flags & SKB_ALLOC_FCLONE)
210 ? skbuff_fclone_cache : skbuff_head_cache;
211
212 if (sk_memalloc_socks() && (flags & SKB_ALLOC_RX))
213 gfp_mask |= __GFP_MEMALLOC;
Herbert Xu8798b3f2006-01-23 16:32:45 -0800214
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 /* Get the HEAD */
Christoph Hellwigb30973f2006-12-06 20:32:36 -0800216 skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 if (!skb)
218 goto out;
Eric Dumazetec7d2f22010-05-05 01:07:37 -0700219 prefetchw(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
Eric Dumazet87fb4b72011-10-13 07:28:54 +0000221 /* We do our best to align skb_shared_info on a separate cache
222 * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives
223 * aligned memory blocks, unless SLUB/SLAB debug is enabled.
224 * Both skb->head and skb_shared_info are cache line aligned.
225 */
Tony Lindgrenbc417e32011-11-02 13:40:28 +0000226 size = SKB_DATA_ALIGN(size);
Eric Dumazet87fb4b72011-10-13 07:28:54 +0000227 size += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Mel Gormanc93bdd02012-07-31 16:44:19 -0700228 data = kmalloc_reserve(size, gfp_mask, node, &pfmemalloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 if (!data)
230 goto nodata;
Eric Dumazet87fb4b72011-10-13 07:28:54 +0000231 /* kmalloc(size) might give us more room than requested.
232 * Put skb_shared_info exactly at the end of allocated zone,
233 * to allow max possible filling before reallocation.
234 */
235 size = SKB_WITH_OVERHEAD(ksize(data));
Eric Dumazetec7d2f22010-05-05 01:07:37 -0700236 prefetchw(data + size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
Arnaldo Carvalho de Meloca0605a2007-03-19 10:48:59 -0300238 /*
Johannes Bergc8005782008-05-03 20:56:42 -0700239 * Only clear those fields we need to clear, not those that we will
240 * actually initialise below. Hence, don't put any more fields after
241 * the tail pointer in struct sk_buff!
Arnaldo Carvalho de Meloca0605a2007-03-19 10:48:59 -0300242 */
243 memset(skb, 0, offsetof(struct sk_buff, tail));
Eric Dumazet87fb4b72011-10-13 07:28:54 +0000244 /* Account for allocated memory : skb + skb->head */
245 skb->truesize = SKB_TRUESIZE(size);
Mel Gormanc93bdd02012-07-31 16:44:19 -0700246 skb->pfmemalloc = pfmemalloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 atomic_set(&skb->users, 1);
248 skb->head = data;
249 skb->data = data;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700250 skb_reset_tail_pointer(skb);
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700251 skb->end = skb->tail + size;
Cong Wang35d04612013-05-29 15:16:05 +0800252 skb->mac_header = (typeof(skb->mac_header))~0U;
253 skb->transport_header = (typeof(skb->transport_header))~0U;
Stephen Hemminger19633e12009-06-17 05:23:27 +0000254
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800255 /* make sure we initialize shinfo sequentially */
256 shinfo = skb_shinfo(skb);
Eric Dumazetec7d2f22010-05-05 01:07:37 -0700257 memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800258 atomic_set(&shinfo->dataref, 1);
Eric Dumazetc2aa3662011-01-25 23:18:38 +0000259 kmemcheck_annotate_variable(shinfo->destructor_arg);
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800260
Mel Gormanc93bdd02012-07-31 16:44:19 -0700261 if (flags & SKB_ALLOC_FCLONE) {
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700262 struct sk_buff_fclones *fclones;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700264 fclones = container_of(skb, struct sk_buff_fclones, skb1);
265
266 kmemcheck_annotate_bitfield(&fclones->skb2, flags1);
David S. Millerd179cd12005-08-17 14:57:30 -0700267 skb->fclone = SKB_FCLONE_ORIG;
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700268 atomic_set(&fclones->fclone_ref, 1);
David S. Millerd179cd12005-08-17 14:57:30 -0700269
Eric Dumazet6ffe75e2014-12-03 17:04:39 -0800270 fclones->skb2.fclone = SKB_FCLONE_CLONE;
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700271 fclones->skb2.pfmemalloc = pfmemalloc;
David S. Millerd179cd12005-08-17 14:57:30 -0700272 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273out:
274 return skb;
275nodata:
Herbert Xu8798b3f2006-01-23 16:32:45 -0800276 kmem_cache_free(cache, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 skb = NULL;
278 goto out;
279}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800280EXPORT_SYMBOL(__alloc_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
282/**
Eric Dumazet2ea2f622015-04-24 16:05:01 -0700283 * __build_skb - build a network buffer
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000284 * @data: data buffer provided by caller
Eric Dumazet2ea2f622015-04-24 16:05:01 -0700285 * @frag_size: size of data, or 0 if head was kmalloced
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000286 *
287 * Allocate a new &sk_buff. Caller provides space holding head and
Florian Fainellideceb4c2013-07-23 20:22:39 +0100288 * skb_shared_info. @data must have been allocated by kmalloc() only if
Eric Dumazet2ea2f622015-04-24 16:05:01 -0700289 * @frag_size is 0, otherwise data should come from the page allocator
290 * or vmalloc()
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000291 * The return is the new skb buffer.
292 * On a failure the return is %NULL, and @data is not freed.
293 * Notes :
294 * Before IO, driver allocates only data buffer where NIC put incoming frame
295 * Driver should add room at head (NET_SKB_PAD) and
296 * MUST add room at tail (SKB_DATA_ALIGN(skb_shared_info))
297 * After IO, driver calls build_skb(), to allocate sk_buff and populate it
298 * before giving packet to stack.
299 * RX rings only contains data buffers, not full skbs.
300 */
Eric Dumazet2ea2f622015-04-24 16:05:01 -0700301struct sk_buff *__build_skb(void *data, unsigned int frag_size)
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000302{
303 struct skb_shared_info *shinfo;
304 struct sk_buff *skb;
Eric Dumazetd3836f22012-04-27 00:33:38 +0000305 unsigned int size = frag_size ? : ksize(data);
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000306
307 skb = kmem_cache_alloc(skbuff_head_cache, GFP_ATOMIC);
308 if (!skb)
309 return NULL;
310
Eric Dumazetd3836f22012-04-27 00:33:38 +0000311 size -= SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000312
313 memset(skb, 0, offsetof(struct sk_buff, tail));
314 skb->truesize = SKB_TRUESIZE(size);
315 atomic_set(&skb->users, 1);
316 skb->head = data;
317 skb->data = data;
318 skb_reset_tail_pointer(skb);
319 skb->end = skb->tail + size;
Cong Wang35d04612013-05-29 15:16:05 +0800320 skb->mac_header = (typeof(skb->mac_header))~0U;
321 skb->transport_header = (typeof(skb->transport_header))~0U;
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000322
323 /* make sure we initialize shinfo sequentially */
324 shinfo = skb_shinfo(skb);
325 memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
326 atomic_set(&shinfo->dataref, 1);
327 kmemcheck_annotate_variable(shinfo->destructor_arg);
328
329 return skb;
330}
Eric Dumazet2ea2f622015-04-24 16:05:01 -0700331
332/* build_skb() is wrapper over __build_skb(), that specifically
333 * takes care of skb->head and skb->pfmemalloc
334 * This means that if @frag_size is not zero, then @data must be backed
335 * by a page fragment, not kmalloc() or vmalloc()
336 */
337struct sk_buff *build_skb(void *data, unsigned int frag_size)
338{
339 struct sk_buff *skb = __build_skb(data, frag_size);
340
341 if (skb && frag_size) {
342 skb->head_frag = 1;
Michal Hocko2f064f32015-08-21 14:11:51 -0700343 if (page_is_pfmemalloc(virt_to_head_page(data)))
Eric Dumazet2ea2f622015-04-24 16:05:01 -0700344 skb->pfmemalloc = 1;
345 }
346 return skb;
347}
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000348EXPORT_SYMBOL(build_skb);
349
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100350#define NAPI_SKB_CACHE_SIZE 64
351
352struct napi_alloc_cache {
353 struct page_frag_cache page;
354 size_t skb_count;
355 void *skb_cache[NAPI_SKB_CACHE_SIZE];
356};
357
Alexander Duyckb63ae8c2015-05-06 21:11:57 -0700358static DEFINE_PER_CPU(struct page_frag_cache, netdev_alloc_cache);
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100359static DEFINE_PER_CPU(struct napi_alloc_cache, napi_alloc_cache);
Alexander Duyckffde7322014-12-09 19:40:42 -0800360
361static void *__netdev_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
362{
Alexander Duyckb63ae8c2015-05-06 21:11:57 -0700363 struct page_frag_cache *nc;
Alexander Duyckffde7322014-12-09 19:40:42 -0800364 unsigned long flags;
365 void *data;
366
367 local_irq_save(flags);
Alexander Duyck94519802015-05-06 21:11:40 -0700368 nc = this_cpu_ptr(&netdev_alloc_cache);
369 data = __alloc_page_frag(nc, fragsz, gfp_mask);
Eric Dumazet6f532612012-05-18 05:12:12 +0000370 local_irq_restore(flags);
371 return data;
372}
Mel Gormanc93bdd02012-07-31 16:44:19 -0700373
374/**
375 * netdev_alloc_frag - allocate a page fragment
376 * @fragsz: fragment size
377 *
378 * Allocates a frag from a page for receive buffer.
379 * Uses GFP_ATOMIC allocations.
380 */
381void *netdev_alloc_frag(unsigned int fragsz)
382{
383 return __netdev_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD);
384}
Eric Dumazet6f532612012-05-18 05:12:12 +0000385EXPORT_SYMBOL(netdev_alloc_frag);
386
Alexander Duyckffde7322014-12-09 19:40:42 -0800387static void *__napi_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
388{
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100389 struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
Alexander Duyck94519802015-05-06 21:11:40 -0700390
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100391 return __alloc_page_frag(&nc->page, fragsz, gfp_mask);
Alexander Duyckffde7322014-12-09 19:40:42 -0800392}
393
394void *napi_alloc_frag(unsigned int fragsz)
395{
396 return __napi_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD);
397}
398EXPORT_SYMBOL(napi_alloc_frag);
399
Eric Dumazet6f532612012-05-18 05:12:12 +0000400/**
Alexander Duyckfd11a832014-12-09 19:40:49 -0800401 * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
402 * @dev: network device to receive on
Masanari Iidad7499162015-08-24 22:56:54 +0900403 * @len: length to allocate
Alexander Duyckfd11a832014-12-09 19:40:49 -0800404 * @gfp_mask: get_free_pages mask, passed to alloc_skb
405 *
406 * Allocate a new &sk_buff and assign it a usage count of one. The
407 * buffer has NET_SKB_PAD headroom built in. Users should allocate
408 * the headroom they think they need without accounting for the
409 * built in space. The built in space is used for optimisations.
410 *
411 * %NULL is returned if there is no free memory.
412 */
Alexander Duyck94519802015-05-06 21:11:40 -0700413struct sk_buff *__netdev_alloc_skb(struct net_device *dev, unsigned int len,
414 gfp_t gfp_mask)
Alexander Duyckfd11a832014-12-09 19:40:49 -0800415{
Alexander Duyckb63ae8c2015-05-06 21:11:57 -0700416 struct page_frag_cache *nc;
Alexander Duyck94519802015-05-06 21:11:40 -0700417 unsigned long flags;
Alexander Duyckfd11a832014-12-09 19:40:49 -0800418 struct sk_buff *skb;
Alexander Duyck94519802015-05-06 21:11:40 -0700419 bool pfmemalloc;
420 void *data;
Alexander Duyckfd11a832014-12-09 19:40:49 -0800421
Alexander Duyck94519802015-05-06 21:11:40 -0700422 len += NET_SKB_PAD;
Alexander Duyckfd11a832014-12-09 19:40:49 -0800423
Alexander Duyck94519802015-05-06 21:11:40 -0700424 if ((len > SKB_WITH_OVERHEAD(PAGE_SIZE)) ||
Mel Gormand0164ad2015-11-06 16:28:21 -0800425 (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) {
Alexander Duycka080e7b2015-05-13 13:34:13 -0700426 skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE);
427 if (!skb)
428 goto skb_fail;
429 goto skb_success;
430 }
Alexander Duyck94519802015-05-06 21:11:40 -0700431
432 len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
433 len = SKB_DATA_ALIGN(len);
434
435 if (sk_memalloc_socks())
436 gfp_mask |= __GFP_MEMALLOC;
437
438 local_irq_save(flags);
439
440 nc = this_cpu_ptr(&netdev_alloc_cache);
441 data = __alloc_page_frag(nc, len, gfp_mask);
442 pfmemalloc = nc->pfmemalloc;
443
444 local_irq_restore(flags);
445
446 if (unlikely(!data))
447 return NULL;
448
449 skb = __build_skb(data, len);
450 if (unlikely(!skb)) {
Alexander Duyck181edb22015-05-06 21:12:03 -0700451 skb_free_frag(data);
Alexander Duyck94519802015-05-06 21:11:40 -0700452 return NULL;
Christoph Hellwig7b2e4972006-08-07 16:09:04 -0700453 }
Alexander Duyckfd11a832014-12-09 19:40:49 -0800454
Alexander Duyck94519802015-05-06 21:11:40 -0700455 /* use OR instead of assignment to avoid clearing of bits in mask */
456 if (pfmemalloc)
457 skb->pfmemalloc = 1;
458 skb->head_frag = 1;
459
Alexander Duycka080e7b2015-05-13 13:34:13 -0700460skb_success:
Alexander Duyck94519802015-05-06 21:11:40 -0700461 skb_reserve(skb, NET_SKB_PAD);
462 skb->dev = dev;
463
Alexander Duycka080e7b2015-05-13 13:34:13 -0700464skb_fail:
Christoph Hellwig8af27452006-07-31 22:35:23 -0700465 return skb;
466}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800467EXPORT_SYMBOL(__netdev_alloc_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
Alexander Duyckfd11a832014-12-09 19:40:49 -0800469/**
470 * __napi_alloc_skb - allocate skbuff for rx in a specific NAPI instance
471 * @napi: napi instance this buffer was allocated for
Masanari Iidad7499162015-08-24 22:56:54 +0900472 * @len: length to allocate
Alexander Duyckfd11a832014-12-09 19:40:49 -0800473 * @gfp_mask: get_free_pages mask, passed to alloc_skb and alloc_pages
474 *
475 * Allocate a new sk_buff for use in NAPI receive. This buffer will
476 * attempt to allocate the head from a special reserved region used
477 * only for NAPI Rx allocation. By doing this we can save several
478 * CPU cycles by avoiding having to disable and re-enable IRQs.
479 *
480 * %NULL is returned if there is no free memory.
481 */
Alexander Duyck94519802015-05-06 21:11:40 -0700482struct sk_buff *__napi_alloc_skb(struct napi_struct *napi, unsigned int len,
483 gfp_t gfp_mask)
Alexander Duyckfd11a832014-12-09 19:40:49 -0800484{
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100485 struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
Alexander Duyckfd11a832014-12-09 19:40:49 -0800486 struct sk_buff *skb;
Alexander Duyck94519802015-05-06 21:11:40 -0700487 void *data;
Alexander Duyckfd11a832014-12-09 19:40:49 -0800488
Alexander Duyck94519802015-05-06 21:11:40 -0700489 len += NET_SKB_PAD + NET_IP_ALIGN;
Alexander Duyckfd11a832014-12-09 19:40:49 -0800490
Alexander Duyck94519802015-05-06 21:11:40 -0700491 if ((len > SKB_WITH_OVERHEAD(PAGE_SIZE)) ||
Mel Gormand0164ad2015-11-06 16:28:21 -0800492 (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) {
Alexander Duycka080e7b2015-05-13 13:34:13 -0700493 skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE);
494 if (!skb)
495 goto skb_fail;
496 goto skb_success;
497 }
Alexander Duyck94519802015-05-06 21:11:40 -0700498
499 len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
500 len = SKB_DATA_ALIGN(len);
501
502 if (sk_memalloc_socks())
503 gfp_mask |= __GFP_MEMALLOC;
504
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100505 data = __alloc_page_frag(&nc->page, len, gfp_mask);
Alexander Duyck94519802015-05-06 21:11:40 -0700506 if (unlikely(!data))
507 return NULL;
508
509 skb = __build_skb(data, len);
510 if (unlikely(!skb)) {
Alexander Duyck181edb22015-05-06 21:12:03 -0700511 skb_free_frag(data);
Alexander Duyck94519802015-05-06 21:11:40 -0700512 return NULL;
Alexander Duyckfd11a832014-12-09 19:40:49 -0800513 }
514
Alexander Duyck94519802015-05-06 21:11:40 -0700515 /* use OR instead of assignment to avoid clearing of bits in mask */
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100516 if (nc->page.pfmemalloc)
Alexander Duyck94519802015-05-06 21:11:40 -0700517 skb->pfmemalloc = 1;
518 skb->head_frag = 1;
519
Alexander Duycka080e7b2015-05-13 13:34:13 -0700520skb_success:
Alexander Duyck94519802015-05-06 21:11:40 -0700521 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
522 skb->dev = napi->dev;
523
Alexander Duycka080e7b2015-05-13 13:34:13 -0700524skb_fail:
Alexander Duyckfd11a832014-12-09 19:40:49 -0800525 return skb;
526}
527EXPORT_SYMBOL(__napi_alloc_skb);
528
Peter Zijlstra654bed12008-10-07 14:22:33 -0700529void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off,
Eric Dumazet50269e12012-03-23 23:59:33 +0000530 int size, unsigned int truesize)
Peter Zijlstra654bed12008-10-07 14:22:33 -0700531{
532 skb_fill_page_desc(skb, i, page, off, size);
533 skb->len += size;
534 skb->data_len += size;
Eric Dumazet50269e12012-03-23 23:59:33 +0000535 skb->truesize += truesize;
Peter Zijlstra654bed12008-10-07 14:22:33 -0700536}
537EXPORT_SYMBOL(skb_add_rx_frag);
538
Jason Wangf8e617e2013-11-01 14:07:47 +0800539void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size,
540 unsigned int truesize)
541{
542 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
543
544 skb_frag_size_add(frag, size);
545 skb->len += size;
546 skb->data_len += size;
547 skb->truesize += truesize;
548}
549EXPORT_SYMBOL(skb_coalesce_rx_frag);
550
Herbert Xu27b437c2006-07-13 19:26:39 -0700551static void skb_drop_list(struct sk_buff **listp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552{
Eric Dumazetbd8a7032013-06-24 06:26:00 -0700553 kfree_skb_list(*listp);
Herbert Xu27b437c2006-07-13 19:26:39 -0700554 *listp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555}
556
Herbert Xu27b437c2006-07-13 19:26:39 -0700557static inline void skb_drop_fraglist(struct sk_buff *skb)
558{
559 skb_drop_list(&skb_shinfo(skb)->frag_list);
560}
561
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562static void skb_clone_fraglist(struct sk_buff *skb)
563{
564 struct sk_buff *list;
565
David S. Millerfbb398a2009-06-09 00:18:59 -0700566 skb_walk_frags(skb, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 skb_get(list);
568}
569
Eric Dumazetd3836f22012-04-27 00:33:38 +0000570static void skb_free_head(struct sk_buff *skb)
571{
Alexander Duyck181edb22015-05-06 21:12:03 -0700572 unsigned char *head = skb->head;
573
Eric Dumazetd3836f22012-04-27 00:33:38 +0000574 if (skb->head_frag)
Alexander Duyck181edb22015-05-06 21:12:03 -0700575 skb_free_frag(head);
Eric Dumazetd3836f22012-04-27 00:33:38 +0000576 else
Alexander Duyck181edb22015-05-06 21:12:03 -0700577 kfree(head);
Eric Dumazetd3836f22012-04-27 00:33:38 +0000578}
579
Adrian Bunk5bba1712006-06-29 13:02:35 -0700580static void skb_release_data(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
Eric Dumazetff04a772014-09-23 18:39:30 -0700582 struct skb_shared_info *shinfo = skb_shinfo(skb);
583 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
Eric Dumazetff04a772014-09-23 18:39:30 -0700585 if (skb->cloned &&
586 atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
587 &shinfo->dataref))
588 return;
Shirley Maa6686f22011-07-06 12:22:12 +0000589
Eric Dumazetff04a772014-09-23 18:39:30 -0700590 for (i = 0; i < shinfo->nr_frags; i++)
591 __skb_frag_unref(&shinfo->frags[i]);
Shirley Maa6686f22011-07-06 12:22:12 +0000592
Eric Dumazetff04a772014-09-23 18:39:30 -0700593 /*
594 * If skb buf is from userspace, we need to notify the caller
595 * the lower device DMA has done;
596 */
597 if (shinfo->tx_flags & SKBTX_DEV_ZEROCOPY) {
598 struct ubuf_info *uarg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
Eric Dumazetff04a772014-09-23 18:39:30 -0700600 uarg = shinfo->destructor_arg;
601 if (uarg->callback)
602 uarg->callback(uarg, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 }
Eric Dumazetff04a772014-09-23 18:39:30 -0700604
605 if (shinfo->frag_list)
606 kfree_skb_list(shinfo->frag_list);
607
608 skb_free_head(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609}
610
611/*
612 * Free an skbuff by memory without cleaning the state.
613 */
Herbert Xu2d4baff2007-11-26 23:11:19 +0800614static void kfree_skbmem(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615{
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700616 struct sk_buff_fclones *fclones;
David S. Millerd179cd12005-08-17 14:57:30 -0700617
David S. Millerd179cd12005-08-17 14:57:30 -0700618 switch (skb->fclone) {
619 case SKB_FCLONE_UNAVAILABLE:
620 kmem_cache_free(skbuff_head_cache, skb);
Eric Dumazet6ffe75e2014-12-03 17:04:39 -0800621 return;
David S. Millerd179cd12005-08-17 14:57:30 -0700622
623 case SKB_FCLONE_ORIG:
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700624 fclones = container_of(skb, struct sk_buff_fclones, skb1);
Eric Dumazet6ffe75e2014-12-03 17:04:39 -0800625
626 /* We usually free the clone (TX completion) before original skb
627 * This test would have no chance to be true for the clone,
628 * while here, branch prediction will be good.
629 */
630 if (atomic_read(&fclones->fclone_ref) == 1)
631 goto fastpath;
David S. Millerd179cd12005-08-17 14:57:30 -0700632 break;
633
Eric Dumazet6ffe75e2014-12-03 17:04:39 -0800634 default: /* SKB_FCLONE_CLONE */
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700635 fclones = container_of(skb, struct sk_buff_fclones, skb2);
David S. Millerd179cd12005-08-17 14:57:30 -0700636 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700637 }
Eric Dumazet6ffe75e2014-12-03 17:04:39 -0800638 if (!atomic_dec_and_test(&fclones->fclone_ref))
639 return;
640fastpath:
641 kmem_cache_free(skbuff_fclone_cache, fclones);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642}
643
Lennert Buytenhek04a4bb52008-10-01 02:33:12 -0700644static void skb_release_head_state(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645{
Eric Dumazetadf30902009-06-02 05:19:30 +0000646 skb_dst_drop(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647#ifdef CONFIG_XFRM
648 secpath_put(skb->sp);
649#endif
Stephen Hemminger9c2b3322005-04-19 22:39:42 -0700650 if (skb->destructor) {
651 WARN_ON(in_irq());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 skb->destructor(skb);
653 }
Igor Maravića3bf7ae2011-12-12 02:58:22 +0000654#if IS_ENABLED(CONFIG_NF_CONNTRACK)
Yasuyuki Kozakai5f79e0f2007-03-23 11:17:07 -0700655 nf_conntrack_put(skb->nfct);
KOVACS Krisztian2fc72c72011-01-12 20:25:08 +0100656#endif
Pablo Neira Ayuso1109a902014-10-01 11:19:17 +0200657#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 nf_bridge_put(skb->nf_bridge);
659#endif
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);
Pablo Neira5e71d9d2013-06-03 09:28:43 +0000666 if (likely(skb->head))
Patrick McHardy0ebd0ac2013-04-17 06:46:58 +0000667 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{
695 if (unlikely(!skb))
696 return;
697 if (likely(atomic_read(&skb->users) == 1))
698 smp_rmb();
699 else if (likely(!atomic_dec_and_test(&skb->users)))
700 return;
Neil Hormanead2ceb2009-03-11 09:49:55 +0000701 trace_kfree_skb(skb, __builtin_return_address(0));
Jörn Engel231d06a2006-03-20 21:28:35 -0800702 __kfree_skb(skb);
703}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800704EXPORT_SYMBOL(kfree_skb);
Jörn Engel231d06a2006-03-20 21:28:35 -0800705
Eric Dumazetbd8a7032013-06-24 06:26:00 -0700706void kfree_skb_list(struct sk_buff *segs)
707{
708 while (segs) {
709 struct sk_buff *next = segs->next;
710
711 kfree_skb(segs);
712 segs = next;
713 }
714}
715EXPORT_SYMBOL(kfree_skb_list);
716
Stephen Hemmingerd1a203e2008-11-01 21:01:09 -0700717/**
Michael S. Tsirkin25121172012-11-01 09:16:28 +0000718 * skb_tx_error - report an sk_buff xmit error
719 * @skb: buffer that triggered an error
720 *
721 * Report xmit error if a device callback is tracking this skb.
722 * skb must be freed afterwards.
723 */
724void skb_tx_error(struct sk_buff *skb)
725{
726 if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) {
727 struct ubuf_info *uarg;
728
729 uarg = skb_shinfo(skb)->destructor_arg;
730 if (uarg->callback)
731 uarg->callback(uarg, false);
732 skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
733 }
734}
735EXPORT_SYMBOL(skb_tx_error);
736
737/**
Neil Hormanead2ceb2009-03-11 09:49:55 +0000738 * consume_skb - free an skbuff
739 * @skb: buffer to free
740 *
741 * Drop a ref to the buffer and free it if the usage count has hit zero
742 * Functions identically to kfree_skb, but kfree_skb assumes that the frame
743 * is being dropped after a failure and notes that
744 */
745void consume_skb(struct sk_buff *skb)
746{
747 if (unlikely(!skb))
748 return;
749 if (likely(atomic_read(&skb->users) == 1))
750 smp_rmb();
751 else if (likely(!atomic_dec_and_test(&skb->users)))
752 return;
Koki Sanagi07dc22e2010-08-23 18:46:12 +0900753 trace_consume_skb(skb);
Neil Hormanead2ceb2009-03-11 09:49:55 +0000754 __kfree_skb(skb);
755}
756EXPORT_SYMBOL(consume_skb);
757
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100758void __kfree_skb_flush(void)
759{
760 struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
761
762 /* flush skb_cache if containing objects */
763 if (nc->skb_count) {
764 kmem_cache_free_bulk(skbuff_head_cache, nc->skb_count,
765 nc->skb_cache);
766 nc->skb_count = 0;
767 }
768}
769
770static void __kfree_skb_defer(struct sk_buff *skb)
771{
772 struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
773
774 /* drop skb->head and call any destructors for packet */
775 skb_release_all(skb);
776
777 /* record skb to CPU local list */
778 nc->skb_cache[nc->skb_count++] = skb;
779
780#ifdef CONFIG_SLUB
781 /* SLUB writes into objects when freeing */
782 prefetchw(skb);
783#endif
784
785 /* flush skb_cache if it is filled */
786 if (unlikely(nc->skb_count == NAPI_SKB_CACHE_SIZE)) {
787 kmem_cache_free_bulk(skbuff_head_cache, NAPI_SKB_CACHE_SIZE,
788 nc->skb_cache);
789 nc->skb_count = 0;
790 }
791}
792
793void napi_consume_skb(struct sk_buff *skb, int budget)
794{
795 if (unlikely(!skb))
796 return;
797
798 /* if budget is 0 assume netpoll w/ IRQs disabled */
799 if (unlikely(!budget)) {
800 dev_consume_skb_irq(skb);
801 return;
802 }
803
804 if (likely(atomic_read(&skb->users) == 1))
805 smp_rmb();
806 else if (likely(!atomic_dec_and_test(&skb->users)))
807 return;
808 /* if reaching here SKB is ready to free */
809 trace_consume_skb(skb);
810
811 /* if SKB is a clone, don't handle this case */
812 if (unlikely(skb->fclone != SKB_FCLONE_UNAVAILABLE)) {
813 __kfree_skb(skb);
814 return;
815 }
816
817 __kfree_skb_defer(skb);
818}
819EXPORT_SYMBOL(napi_consume_skb);
820
Eric Dumazetb1937222014-09-28 22:18:47 -0700821/* Make sure a field is enclosed inside headers_start/headers_end section */
822#define CHECK_SKB_FIELD(field) \
823 BUILD_BUG_ON(offsetof(struct sk_buff, field) < \
824 offsetof(struct sk_buff, headers_start)); \
825 BUILD_BUG_ON(offsetof(struct sk_buff, field) > \
826 offsetof(struct sk_buff, headers_end)); \
827
Herbert Xudec18812007-10-14 00:37:30 -0700828static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
829{
830 new->tstamp = old->tstamp;
Eric Dumazetb1937222014-09-28 22:18:47 -0700831 /* We do not copy old->sk */
Herbert Xudec18812007-10-14 00:37:30 -0700832 new->dev = old->dev;
Eric Dumazetb1937222014-09-28 22:18:47 -0700833 memcpy(new->cb, old->cb, sizeof(old->cb));
Eric Dumazet7fee2262010-05-11 23:19:48 +0000834 skb_dst_copy(new, old);
Alexey Dobriyandef8b4f2008-10-28 13:24:06 -0700835#ifdef CONFIG_XFRM
Herbert Xudec18812007-10-14 00:37:30 -0700836 new->sp = secpath_get(old->sp);
837#endif
Eric Dumazetb1937222014-09-28 22:18:47 -0700838 __nf_copy(new, old, false);
Patrick McHardy6aa895b02008-07-14 22:49:06 -0700839
Eric Dumazetb1937222014-09-28 22:18:47 -0700840 /* Note : this field could be in headers_start/headers_end section
841 * It is not yet because we do not want to have a 16 bit hole
842 */
843 new->queue_mapping = old->queue_mapping;
Eliezer Tamir06021292013-06-10 11:39:50 +0300844
Eric Dumazetb1937222014-09-28 22:18:47 -0700845 memcpy(&new->headers_start, &old->headers_start,
846 offsetof(struct sk_buff, headers_end) -
847 offsetof(struct sk_buff, headers_start));
848 CHECK_SKB_FIELD(protocol);
849 CHECK_SKB_FIELD(csum);
850 CHECK_SKB_FIELD(hash);
851 CHECK_SKB_FIELD(priority);
852 CHECK_SKB_FIELD(skb_iif);
853 CHECK_SKB_FIELD(vlan_proto);
854 CHECK_SKB_FIELD(vlan_tci);
855 CHECK_SKB_FIELD(transport_header);
856 CHECK_SKB_FIELD(network_header);
857 CHECK_SKB_FIELD(mac_header);
858 CHECK_SKB_FIELD(inner_protocol);
859 CHECK_SKB_FIELD(inner_transport_header);
860 CHECK_SKB_FIELD(inner_network_header);
861 CHECK_SKB_FIELD(inner_mac_header);
862 CHECK_SKB_FIELD(mark);
863#ifdef CONFIG_NETWORK_SECMARK
864 CHECK_SKB_FIELD(secmark);
865#endif
Cong Wange0d10952013-08-01 11:10:25 +0800866#ifdef CONFIG_NET_RX_BUSY_POLL
Eric Dumazetb1937222014-09-28 22:18:47 -0700867 CHECK_SKB_FIELD(napi_id);
Eliezer Tamir06021292013-06-10 11:39:50 +0300868#endif
Eric Dumazet2bd82482015-02-03 23:48:24 -0800869#ifdef CONFIG_XPS
870 CHECK_SKB_FIELD(sender_cpu);
871#endif
Eric Dumazetb1937222014-09-28 22:18:47 -0700872#ifdef CONFIG_NET_SCHED
873 CHECK_SKB_FIELD(tc_index);
874#ifdef CONFIG_NET_CLS_ACT
875 CHECK_SKB_FIELD(tc_verd);
876#endif
877#endif
878
Herbert Xudec18812007-10-14 00:37:30 -0700879}
880
Herbert Xu82c49a32009-05-22 22:11:37 +0000881/*
882 * You should not add any new code to this function. Add it to
883 * __copy_skb_header above instead.
884 */
Herbert Xue0053ec2007-10-14 00:37:52 -0700885static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887#define C(x) n->x = skb->x
888
889 n->next = n->prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 n->sk = NULL;
Herbert Xudec18812007-10-14 00:37:30 -0700891 __copy_skb_header(n, skb);
892
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 C(len);
894 C(data_len);
Alexey Dobriyan3e6b3b22007-03-16 15:00:46 -0700895 C(mac_len);
Patrick McHardy334a8132007-06-25 04:35:20 -0700896 n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
Paul Moore02f1c892008-01-07 21:56:41 -0800897 n->cloned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 n->nohdr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 n->destructor = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 C(tail);
901 C(end);
Paul Moore02f1c892008-01-07 21:56:41 -0800902 C(head);
Eric Dumazetd3836f22012-04-27 00:33:38 +0000903 C(head_frag);
Paul Moore02f1c892008-01-07 21:56:41 -0800904 C(data);
905 C(truesize);
906 atomic_set(&n->users, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
908 atomic_inc(&(skb_shinfo(skb)->dataref));
909 skb->cloned = 1;
910
911 return n;
Herbert Xue0053ec2007-10-14 00:37:52 -0700912#undef C
913}
914
915/**
916 * skb_morph - morph one skb into another
917 * @dst: the skb to receive the contents
918 * @src: the skb to supply the contents
919 *
920 * This is identical to skb_clone except that the target skb is
921 * supplied by the user.
922 *
923 * The target skb is returned upon exit.
924 */
925struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
926{
Herbert Xu2d4baff2007-11-26 23:11:19 +0800927 skb_release_all(dst);
Herbert Xue0053ec2007-10-14 00:37:52 -0700928 return __skb_clone(dst, src);
929}
930EXPORT_SYMBOL_GPL(skb_morph);
931
Ben Hutchings2c530402012-07-10 10:55:09 +0000932/**
933 * skb_copy_ubufs - copy userspace skb frags buffers to kernel
Michael S. Tsirkin48c83012011-08-31 08:03:29 +0000934 * @skb: the skb to modify
935 * @gfp_mask: allocation priority
936 *
937 * This must be called on SKBTX_DEV_ZEROCOPY skb.
938 * It will copy all frags into kernel and drop the reference
939 * to userspace pages.
940 *
941 * If this function is called from an interrupt gfp_mask() must be
942 * %GFP_ATOMIC.
943 *
944 * Returns 0 on success or a negative error code on failure
945 * to allocate kernel memory to copy to.
946 */
947int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask)
Shirley Maa6686f22011-07-06 12:22:12 +0000948{
949 int i;
950 int num_frags = skb_shinfo(skb)->nr_frags;
951 struct page *page, *head = NULL;
952 struct ubuf_info *uarg = skb_shinfo(skb)->destructor_arg;
953
954 for (i = 0; i < num_frags; i++) {
955 u8 *vaddr;
956 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
957
Krishna Kumar02756ed2012-07-17 02:05:29 +0000958 page = alloc_page(gfp_mask);
Shirley Maa6686f22011-07-06 12:22:12 +0000959 if (!page) {
960 while (head) {
Sunghan Suh40dadff2013-07-12 16:17:23 +0900961 struct page *next = (struct page *)page_private(head);
Shirley Maa6686f22011-07-06 12:22:12 +0000962 put_page(head);
963 head = next;
964 }
965 return -ENOMEM;
966 }
Eric Dumazet51c56b02012-04-05 11:35:15 +0200967 vaddr = kmap_atomic(skb_frag_page(f));
Shirley Maa6686f22011-07-06 12:22:12 +0000968 memcpy(page_address(page),
Eric Dumazet9e903e02011-10-18 21:00:24 +0000969 vaddr + f->page_offset, skb_frag_size(f));
Eric Dumazet51c56b02012-04-05 11:35:15 +0200970 kunmap_atomic(vaddr);
Sunghan Suh40dadff2013-07-12 16:17:23 +0900971 set_page_private(page, (unsigned long)head);
Shirley Maa6686f22011-07-06 12:22:12 +0000972 head = page;
973 }
974
975 /* skb frags release userspace buffers */
Krishna Kumar02756ed2012-07-17 02:05:29 +0000976 for (i = 0; i < num_frags; i++)
Ian Campbella8605c62011-10-19 23:01:49 +0000977 skb_frag_unref(skb, i);
Shirley Maa6686f22011-07-06 12:22:12 +0000978
Michael S. Tsirkine19d6762012-11-01 09:16:22 +0000979 uarg->callback(uarg, false);
Shirley Maa6686f22011-07-06 12:22:12 +0000980
981 /* skb frags point to kernel buffers */
Krishna Kumar02756ed2012-07-17 02:05:29 +0000982 for (i = num_frags - 1; i >= 0; i--) {
983 __skb_fill_page_desc(skb, i, head, 0,
984 skb_shinfo(skb)->frags[i].size);
Sunghan Suh40dadff2013-07-12 16:17:23 +0900985 head = (struct page *)page_private(head);
Shirley Maa6686f22011-07-06 12:22:12 +0000986 }
Michael S. Tsirkin48c83012011-08-31 08:03:29 +0000987
988 skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
Shirley Maa6686f22011-07-06 12:22:12 +0000989 return 0;
990}
Michael S. Tsirkindcc0fb72012-07-20 09:23:20 +0000991EXPORT_SYMBOL_GPL(skb_copy_ubufs);
Shirley Maa6686f22011-07-06 12:22:12 +0000992
Herbert Xue0053ec2007-10-14 00:37:52 -0700993/**
994 * skb_clone - duplicate an sk_buff
995 * @skb: buffer to clone
996 * @gfp_mask: allocation priority
997 *
998 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
999 * copies share the same packet data but not structure. The new
1000 * buffer has a reference count of 1. If the allocation fails the
1001 * function returns %NULL otherwise the new buffer is returned.
1002 *
1003 * If this function is called from an interrupt gfp_mask() must be
1004 * %GFP_ATOMIC.
1005 */
1006
1007struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
1008{
Eric Dumazetd0bf4a92014-09-29 13:29:15 -07001009 struct sk_buff_fclones *fclones = container_of(skb,
1010 struct sk_buff_fclones,
1011 skb1);
Eric Dumazet6ffe75e2014-12-03 17:04:39 -08001012 struct sk_buff *n;
Herbert Xue0053ec2007-10-14 00:37:52 -07001013
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +00001014 if (skb_orphan_frags(skb, gfp_mask))
1015 return NULL;
Shirley Maa6686f22011-07-06 12:22:12 +00001016
Herbert Xue0053ec2007-10-14 00:37:52 -07001017 if (skb->fclone == SKB_FCLONE_ORIG &&
Eric Dumazet6ffe75e2014-12-03 17:04:39 -08001018 atomic_read(&fclones->fclone_ref) == 1) {
1019 n = &fclones->skb2;
1020 atomic_set(&fclones->fclone_ref, 2);
Herbert Xue0053ec2007-10-14 00:37:52 -07001021 } else {
Mel Gormanc93bdd02012-07-31 16:44:19 -07001022 if (skb_pfmemalloc(skb))
1023 gfp_mask |= __GFP_MEMALLOC;
1024
Herbert Xue0053ec2007-10-14 00:37:52 -07001025 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
1026 if (!n)
1027 return NULL;
Vegard Nossumfe55f6d2008-08-30 12:16:35 +02001028
1029 kmemcheck_annotate_bitfield(n, flags1);
Herbert Xue0053ec2007-10-14 00:37:52 -07001030 n->fclone = SKB_FCLONE_UNAVAILABLE;
1031 }
1032
1033 return __skb_clone(n, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001035EXPORT_SYMBOL(skb_clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
Pravin B Shelarf5b17292013-03-07 13:21:40 +00001037static void skb_headers_offset_update(struct sk_buff *skb, int off)
1038{
Eric Dumazet030737b2013-10-19 11:42:54 -07001039 /* Only adjust this if it actually is csum_start rather than csum */
1040 if (skb->ip_summed == CHECKSUM_PARTIAL)
1041 skb->csum_start += off;
Pravin B Shelarf5b17292013-03-07 13:21:40 +00001042 /* {transport,network,mac}_header and tail are relative to skb->head */
1043 skb->transport_header += off;
1044 skb->network_header += off;
1045 if (skb_mac_header_was_set(skb))
1046 skb->mac_header += off;
1047 skb->inner_transport_header += off;
1048 skb->inner_network_header += off;
Pravin B Shelaraefbd2b2013-03-07 13:21:46 +00001049 skb->inner_mac_header += off;
Pravin B Shelarf5b17292013-03-07 13:21:40 +00001050}
1051
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
1053{
Herbert Xudec18812007-10-14 00:37:30 -07001054 __copy_skb_header(new, old);
1055
Herbert Xu79671682006-06-22 02:40:14 -07001056 skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
1057 skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
1058 skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059}
1060
Mel Gormanc93bdd02012-07-31 16:44:19 -07001061static inline int skb_alloc_rx_flag(const struct sk_buff *skb)
1062{
1063 if (skb_pfmemalloc(skb))
1064 return SKB_ALLOC_RX;
1065 return 0;
1066}
1067
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068/**
1069 * skb_copy - create private copy of an sk_buff
1070 * @skb: buffer to copy
1071 * @gfp_mask: allocation priority
1072 *
1073 * Make a copy of both an &sk_buff and its data. This is used when the
1074 * caller wishes to modify the data and needs a private copy of the
1075 * data to alter. Returns %NULL on failure or the pointer to the buffer
1076 * on success. The returned buffer has a reference count of 1.
1077 *
1078 * As by-product this function converts non-linear &sk_buff to linear
1079 * one, so that &sk_buff becomes completely private and caller is allowed
1080 * to modify all the data of returned buffer. This means that this
1081 * function is not recommended for use in circumstances when only
1082 * header is going to be modified. Use pskb_copy() instead.
1083 */
1084
Al Virodd0fc662005-10-07 07:46:04 +01001085struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086{
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001087 int headerlen = skb_headroom(skb);
Alexander Duyckec47ea82012-05-04 14:26:56 +00001088 unsigned int size = skb_end_offset(skb) + skb->data_len;
Mel Gormanc93bdd02012-07-31 16:44:19 -07001089 struct sk_buff *n = __alloc_skb(size, gfp_mask,
1090 skb_alloc_rx_flag(skb), NUMA_NO_NODE);
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001091
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 if (!n)
1093 return NULL;
1094
1095 /* Set the data pointer */
1096 skb_reserve(n, headerlen);
1097 /* Set the tail pointer and length */
1098 skb_put(n, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
1100 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
1101 BUG();
1102
1103 copy_skb_header(n, skb);
1104 return n;
1105}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001106EXPORT_SYMBOL(skb_copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
1108/**
Octavian Purdilabad93e92014-06-12 01:36:26 +03001109 * __pskb_copy_fclone - create copy of an sk_buff with private head.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 * @skb: buffer to copy
Eric Dumazet117632e2011-12-03 21:39:53 +00001111 * @headroom: headroom of new skb
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 * @gfp_mask: allocation priority
Octavian Purdilabad93e92014-06-12 01:36:26 +03001113 * @fclone: if true allocate the copy of the skb from the fclone
1114 * cache instead of the head cache; it is recommended to set this
1115 * to true for the cases where the copy will likely be cloned
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 *
1117 * Make a copy of both an &sk_buff and part of its data, located
1118 * in header. Fragmented data remain shared. This is used when
1119 * the caller wishes to modify only header of &sk_buff and needs
1120 * private copy of the header to alter. Returns %NULL on failure
1121 * or the pointer to the buffer on success.
1122 * The returned buffer has a reference count of 1.
1123 */
1124
Octavian Purdilabad93e92014-06-12 01:36:26 +03001125struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom,
1126 gfp_t gfp_mask, bool fclone)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127{
Eric Dumazet117632e2011-12-03 21:39:53 +00001128 unsigned int size = skb_headlen(skb) + headroom;
Octavian Purdilabad93e92014-06-12 01:36:26 +03001129 int flags = skb_alloc_rx_flag(skb) | (fclone ? SKB_ALLOC_FCLONE : 0);
1130 struct sk_buff *n = __alloc_skb(size, gfp_mask, flags, NUMA_NO_NODE);
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001131
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 if (!n)
1133 goto out;
1134
1135 /* Set the data pointer */
Eric Dumazet117632e2011-12-03 21:39:53 +00001136 skb_reserve(n, headroom);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 /* Set the tail pointer and length */
1138 skb_put(n, skb_headlen(skb));
1139 /* Copy the bytes */
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001140 skb_copy_from_linear_data(skb, n->data, n->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
Herbert Xu25f484a2006-11-07 14:57:15 -08001142 n->truesize += skb->data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 n->data_len = skb->data_len;
1144 n->len = skb->len;
1145
1146 if (skb_shinfo(skb)->nr_frags) {
1147 int i;
1148
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +00001149 if (skb_orphan_frags(skb, gfp_mask)) {
1150 kfree_skb(n);
1151 n = NULL;
1152 goto out;
Shirley Maa6686f22011-07-06 12:22:12 +00001153 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1155 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
Ian Campbellea2ab692011-08-22 23:44:58 +00001156 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 }
1158 skb_shinfo(n)->nr_frags = i;
1159 }
1160
David S. Miller21dc3302010-08-23 00:13:46 -07001161 if (skb_has_frag_list(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
1163 skb_clone_fraglist(n);
1164 }
1165
1166 copy_skb_header(n, skb);
1167out:
1168 return n;
1169}
Octavian Purdilabad93e92014-06-12 01:36:26 +03001170EXPORT_SYMBOL(__pskb_copy_fclone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
1172/**
1173 * pskb_expand_head - reallocate header of &sk_buff
1174 * @skb: buffer to reallocate
1175 * @nhead: room to add at head
1176 * @ntail: room to add at tail
1177 * @gfp_mask: allocation priority
1178 *
Mathias Krausebc323832013-11-07 14:18:26 +01001179 * Expands (or creates identical copy, if @nhead and @ntail are zero)
1180 * header of @skb. &sk_buff itself is not changed. &sk_buff MUST have
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 * reference count of 1. Returns zero in the case of success or error,
1182 * if expansion failed. In the last case, &sk_buff is not changed.
1183 *
1184 * All the pointers pointing into skb header may change and must be
1185 * reloaded after call to this function.
1186 */
1187
Victor Fusco86a76ca2005-07-08 14:57:47 -07001188int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
Al Virodd0fc662005-10-07 07:46:04 +01001189 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190{
1191 int i;
1192 u8 *data;
Alexander Duyckec47ea82012-05-04 14:26:56 +00001193 int size = nhead + skb_end_offset(skb) + ntail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 long off;
1195
Herbert Xu4edd87a2008-10-01 07:09:38 -07001196 BUG_ON(nhead < 0);
1197
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 if (skb_shared(skb))
1199 BUG();
1200
1201 size = SKB_DATA_ALIGN(size);
1202
Mel Gormanc93bdd02012-07-31 16:44:19 -07001203 if (skb_pfmemalloc(skb))
1204 gfp_mask |= __GFP_MEMALLOC;
1205 data = kmalloc_reserve(size + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
1206 gfp_mask, NUMA_NO_NODE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 if (!data)
1208 goto nodata;
Eric Dumazet87151b82012-04-10 20:08:39 +00001209 size = SKB_WITH_OVERHEAD(ksize(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210
1211 /* Copy only real data... and, alas, header. This should be
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001212 * optimized for the cases when header is void.
1213 */
1214 memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head);
1215
1216 memcpy((struct skb_shared_info *)(data + size),
1217 skb_shinfo(skb),
Eric Dumazetfed66382010-07-22 19:09:08 +00001218 offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
Alexander Duyck3e245912012-05-04 14:26:51 +00001220 /*
1221 * if shinfo is shared we must drop the old head gracefully, but if it
1222 * is not we can just drop the old head and let the existing refcount
1223 * be since all we did is relocate the values
1224 */
1225 if (skb_cloned(skb)) {
Shirley Maa6686f22011-07-06 12:22:12 +00001226 /* copy this zero copy skb frags */
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +00001227 if (skb_orphan_frags(skb, gfp_mask))
1228 goto nofrags;
Eric Dumazet1fd63042010-09-02 23:09:32 +00001229 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
Ian Campbellea2ab692011-08-22 23:44:58 +00001230 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
Eric Dumazet1fd63042010-09-02 23:09:32 +00001232 if (skb_has_frag_list(skb))
1233 skb_clone_fraglist(skb);
1234
1235 skb_release_data(skb);
Alexander Duyck3e245912012-05-04 14:26:51 +00001236 } else {
1237 skb_free_head(skb);
Eric Dumazet1fd63042010-09-02 23:09:32 +00001238 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 off = (data + nhead) - skb->head;
1240
1241 skb->head = data;
Eric Dumazetd3836f22012-04-27 00:33:38 +00001242 skb->head_frag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 skb->data += off;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001244#ifdef NET_SKBUFF_DATA_USES_OFFSET
1245 skb->end = size;
Patrick McHardy56eb8882007-04-09 11:45:04 -07001246 off = nhead;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001247#else
1248 skb->end = skb->head + size;
Patrick McHardy56eb8882007-04-09 11:45:04 -07001249#endif
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001250 skb->tail += off;
Peter Pan(潘卫平)b41abb42013-06-06 21:27:21 +08001251 skb_headers_offset_update(skb, nhead);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 skb->cloned = 0;
Patrick McHardy334a8132007-06-25 04:35:20 -07001253 skb->hdr_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 skb->nohdr = 0;
1255 atomic_set(&skb_shinfo(skb)->dataref, 1);
1256 return 0;
1257
Shirley Maa6686f22011-07-06 12:22:12 +00001258nofrags:
1259 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260nodata:
1261 return -ENOMEM;
1262}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001263EXPORT_SYMBOL(pskb_expand_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
1265/* Make private copy of skb with writable head and some headroom */
1266
1267struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
1268{
1269 struct sk_buff *skb2;
1270 int delta = headroom - skb_headroom(skb);
1271
1272 if (delta <= 0)
1273 skb2 = pskb_copy(skb, GFP_ATOMIC);
1274 else {
1275 skb2 = skb_clone(skb, GFP_ATOMIC);
1276 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
1277 GFP_ATOMIC)) {
1278 kfree_skb(skb2);
1279 skb2 = NULL;
1280 }
1281 }
1282 return skb2;
1283}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001284EXPORT_SYMBOL(skb_realloc_headroom);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
1286/**
1287 * skb_copy_expand - copy and expand sk_buff
1288 * @skb: buffer to copy
1289 * @newheadroom: new free bytes at head
1290 * @newtailroom: new free bytes at tail
1291 * @gfp_mask: allocation priority
1292 *
1293 * Make a copy of both an &sk_buff and its data and while doing so
1294 * allocate additional space.
1295 *
1296 * This is used when the caller wishes to modify the data and needs a
1297 * private copy of the data to alter as well as more space for new fields.
1298 * Returns %NULL on failure or the pointer to the buffer
1299 * on success. The returned buffer has a reference count of 1.
1300 *
1301 * You must pass %GFP_ATOMIC as the allocation priority if this function
1302 * is called from an interrupt.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 */
1304struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
Victor Fusco86a76ca2005-07-08 14:57:47 -07001305 int newheadroom, int newtailroom,
Al Virodd0fc662005-10-07 07:46:04 +01001306 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307{
1308 /*
1309 * Allocate the copy buffer
1310 */
Mel Gormanc93bdd02012-07-31 16:44:19 -07001311 struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom,
1312 gfp_mask, skb_alloc_rx_flag(skb),
1313 NUMA_NO_NODE);
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001314 int oldheadroom = skb_headroom(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 int head_copy_len, head_copy_off;
1316
1317 if (!n)
1318 return NULL;
1319
1320 skb_reserve(n, newheadroom);
1321
1322 /* Set the tail pointer and length */
1323 skb_put(n, skb->len);
1324
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001325 head_copy_len = oldheadroom;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 head_copy_off = 0;
1327 if (newheadroom <= head_copy_len)
1328 head_copy_len = newheadroom;
1329 else
1330 head_copy_off = newheadroom - head_copy_len;
1331
1332 /* Copy the linear header and data. */
1333 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
1334 skb->len + head_copy_len))
1335 BUG();
1336
1337 copy_skb_header(n, skb);
1338
Eric Dumazet030737b2013-10-19 11:42:54 -07001339 skb_headers_offset_update(n, newheadroom - oldheadroom);
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001340
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 return n;
1342}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001343EXPORT_SYMBOL(skb_copy_expand);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
1345/**
1346 * skb_pad - zero pad the tail of an skb
1347 * @skb: buffer to pad
1348 * @pad: space to pad
1349 *
1350 * Ensure that a buffer is followed by a padding area that is zero
1351 * filled. Used by network drivers which may DMA or transfer data
1352 * beyond the buffer end onto the wire.
1353 *
Herbert Xu5b057c62006-06-23 02:06:41 -07001354 * May return error in out of memory cases. The skb is freed on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001356
Herbert Xu5b057c62006-06-23 02:06:41 -07001357int skb_pad(struct sk_buff *skb, int pad)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358{
Herbert Xu5b057c62006-06-23 02:06:41 -07001359 int err;
1360 int ntail;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001361
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 /* If the skbuff is non linear tailroom is always zero.. */
Herbert Xu5b057c62006-06-23 02:06:41 -07001363 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 memset(skb->data+skb->len, 0, pad);
Herbert Xu5b057c62006-06-23 02:06:41 -07001365 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 }
Herbert Xu5b057c62006-06-23 02:06:41 -07001367
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001368 ntail = skb->data_len + pad - (skb->end - skb->tail);
Herbert Xu5b057c62006-06-23 02:06:41 -07001369 if (likely(skb_cloned(skb) || ntail > 0)) {
1370 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
1371 if (unlikely(err))
1372 goto free_skb;
1373 }
1374
1375 /* FIXME: The use of this function with non-linear skb's really needs
1376 * to be audited.
1377 */
1378 err = skb_linearize(skb);
1379 if (unlikely(err))
1380 goto free_skb;
1381
1382 memset(skb->data + skb->len, 0, pad);
1383 return 0;
1384
1385free_skb:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 kfree_skb(skb);
Herbert Xu5b057c62006-06-23 02:06:41 -07001387 return err;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001388}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001389EXPORT_SYMBOL(skb_pad);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001390
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -07001391/**
Mathias Krause0c7ddf32013-11-07 14:18:24 +01001392 * pskb_put - add data to the tail of a potentially fragmented buffer
1393 * @skb: start of the buffer to use
1394 * @tail: tail fragment of the buffer to use
1395 * @len: amount of data to add
1396 *
1397 * This function extends the used data area of the potentially
1398 * fragmented buffer. @tail must be the last fragment of @skb -- or
1399 * @skb itself. If this would exceed the total buffer size the kernel
1400 * will panic. A pointer to the first byte of the extra data is
1401 * returned.
1402 */
1403
1404unsigned char *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len)
1405{
1406 if (tail != skb) {
1407 skb->data_len += len;
1408 skb->len += len;
1409 }
1410 return skb_put(tail, len);
1411}
1412EXPORT_SYMBOL_GPL(pskb_put);
1413
1414/**
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -07001415 * skb_put - add data to a buffer
1416 * @skb: buffer to use
1417 * @len: amount of data to add
1418 *
1419 * This function extends the used data area of the buffer. If this would
1420 * exceed the total buffer size the kernel will panic. A pointer to the
1421 * first byte of the extra data is returned.
1422 */
1423unsigned char *skb_put(struct sk_buff *skb, unsigned int len)
1424{
1425 unsigned char *tmp = skb_tail_pointer(skb);
1426 SKB_LINEAR_ASSERT(skb);
1427 skb->tail += len;
1428 skb->len += len;
1429 if (unlikely(skb->tail > skb->end))
1430 skb_over_panic(skb, len, __builtin_return_address(0));
1431 return tmp;
1432}
1433EXPORT_SYMBOL(skb_put);
1434
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001435/**
Ilpo Järvinenc2aa2702008-03-27 17:52:40 -07001436 * skb_push - add data to the start of a buffer
1437 * @skb: buffer to use
1438 * @len: amount of data to add
1439 *
1440 * This function extends the used data area of the buffer at the buffer
1441 * start. If this would exceed the total buffer headroom the kernel will
1442 * panic. A pointer to the first byte of the extra data is returned.
1443 */
1444unsigned char *skb_push(struct sk_buff *skb, unsigned int len)
1445{
1446 skb->data -= len;
1447 skb->len += len;
1448 if (unlikely(skb->data<skb->head))
1449 skb_under_panic(skb, len, __builtin_return_address(0));
1450 return skb->data;
1451}
1452EXPORT_SYMBOL(skb_push);
1453
1454/**
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001455 * skb_pull - remove data from the start of a buffer
1456 * @skb: buffer to use
1457 * @len: amount of data to remove
1458 *
1459 * This function removes data from the start of a buffer, returning
1460 * the memory to the headroom. A pointer to the next data in the buffer
1461 * is returned. Once the data has been pulled future pushes will overwrite
1462 * the old data.
1463 */
1464unsigned char *skb_pull(struct sk_buff *skb, unsigned int len)
1465{
David S. Miller47d29642010-05-02 02:21:44 -07001466 return skb_pull_inline(skb, len);
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001467}
1468EXPORT_SYMBOL(skb_pull);
1469
Ilpo Järvinen419ae742008-03-27 17:54:01 -07001470/**
1471 * skb_trim - remove end from a buffer
1472 * @skb: buffer to alter
1473 * @len: new length
1474 *
1475 * Cut the length of a buffer down by removing data from the tail. If
1476 * the buffer is already under the length specified it is not modified.
1477 * The skb must be linear.
1478 */
1479void skb_trim(struct sk_buff *skb, unsigned int len)
1480{
1481 if (skb->len > len)
1482 __skb_trim(skb, len);
1483}
1484EXPORT_SYMBOL(skb_trim);
1485
Herbert Xu3cc0e872006-06-09 16:13:38 -07001486/* Trims skb to length len. It can change skb pointers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 */
1488
Herbert Xu3cc0e872006-06-09 16:13:38 -07001489int ___pskb_trim(struct sk_buff *skb, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490{
Herbert Xu27b437c2006-07-13 19:26:39 -07001491 struct sk_buff **fragp;
1492 struct sk_buff *frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 int offset = skb_headlen(skb);
1494 int nfrags = skb_shinfo(skb)->nr_frags;
1495 int i;
Herbert Xu27b437c2006-07-13 19:26:39 -07001496 int err;
1497
1498 if (skb_cloned(skb) &&
1499 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
1500 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001502 i = 0;
1503 if (offset >= len)
1504 goto drop_pages;
1505
1506 for (; i < nfrags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001507 int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]);
Herbert Xu27b437c2006-07-13 19:26:39 -07001508
1509 if (end < len) {
1510 offset = end;
1511 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 }
Herbert Xu27b437c2006-07-13 19:26:39 -07001513
Eric Dumazet9e903e02011-10-18 21:00:24 +00001514 skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset);
Herbert Xu27b437c2006-07-13 19:26:39 -07001515
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001516drop_pages:
Herbert Xu27b437c2006-07-13 19:26:39 -07001517 skb_shinfo(skb)->nr_frags = i;
1518
1519 for (; i < nfrags; i++)
Ian Campbellea2ab692011-08-22 23:44:58 +00001520 skb_frag_unref(skb, i);
Herbert Xu27b437c2006-07-13 19:26:39 -07001521
David S. Miller21dc3302010-08-23 00:13:46 -07001522 if (skb_has_frag_list(skb))
Herbert Xu27b437c2006-07-13 19:26:39 -07001523 skb_drop_fraglist(skb);
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001524 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 }
1526
Herbert Xu27b437c2006-07-13 19:26:39 -07001527 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
1528 fragp = &frag->next) {
1529 int end = offset + frag->len;
1530
1531 if (skb_shared(frag)) {
1532 struct sk_buff *nfrag;
1533
1534 nfrag = skb_clone(frag, GFP_ATOMIC);
1535 if (unlikely(!nfrag))
1536 return -ENOMEM;
1537
1538 nfrag->next = frag->next;
Eric Dumazet85bb2a62012-04-19 02:24:53 +00001539 consume_skb(frag);
Herbert Xu27b437c2006-07-13 19:26:39 -07001540 frag = nfrag;
1541 *fragp = frag;
1542 }
1543
1544 if (end < len) {
1545 offset = end;
1546 continue;
1547 }
1548
1549 if (end > len &&
1550 unlikely((err = pskb_trim(frag, len - offset))))
1551 return err;
1552
1553 if (frag->next)
1554 skb_drop_list(&frag->next);
1555 break;
1556 }
1557
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001558done:
Herbert Xu27b437c2006-07-13 19:26:39 -07001559 if (len > skb_headlen(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 skb->data_len -= skb->len - len;
1561 skb->len = len;
1562 } else {
Herbert Xu27b437c2006-07-13 19:26:39 -07001563 skb->len = len;
1564 skb->data_len = 0;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001565 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 }
1567
1568 return 0;
1569}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001570EXPORT_SYMBOL(___pskb_trim);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571
1572/**
1573 * __pskb_pull_tail - advance tail of skb header
1574 * @skb: buffer to reallocate
1575 * @delta: number of bytes to advance tail
1576 *
1577 * The function makes a sense only on a fragmented &sk_buff,
1578 * it expands header moving its tail forward and copying necessary
1579 * data from fragmented part.
1580 *
1581 * &sk_buff MUST have reference count of 1.
1582 *
1583 * Returns %NULL (and &sk_buff does not change) if pull failed
1584 * or value of new tail of skb in the case of success.
1585 *
1586 * All the pointers pointing into skb header may change and must be
1587 * reloaded after call to this function.
1588 */
1589
1590/* Moves tail of skb head forward, copying data from fragmented part,
1591 * when it is necessary.
1592 * 1. It may fail due to malloc failure.
1593 * 2. It may change skb pointers.
1594 *
1595 * It is pretty complicated. Luckily, it is called only in exceptional cases.
1596 */
1597unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
1598{
1599 /* If skb has not enough free space at tail, get new one
1600 * plus 128 bytes for future expansions. If we have enough
1601 * room at tail, reallocate without expansion only if skb is cloned.
1602 */
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001603 int i, k, eat = (skb->tail + delta) - skb->end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604
1605 if (eat > 0 || skb_cloned(skb)) {
1606 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
1607 GFP_ATOMIC))
1608 return NULL;
1609 }
1610
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001611 if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 BUG();
1613
1614 /* Optimization: no fragments, no reasons to preestimate
1615 * size of pulled pages. Superb.
1616 */
David S. Miller21dc3302010-08-23 00:13:46 -07001617 if (!skb_has_frag_list(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 goto pull_pages;
1619
1620 /* Estimate size of pulled pages. */
1621 eat = delta;
1622 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001623 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1624
1625 if (size >= eat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 goto pull_pages;
Eric Dumazet9e903e02011-10-18 21:00:24 +00001627 eat -= size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 }
1629
1630 /* If we need update frag list, we are in troubles.
1631 * Certainly, it possible to add an offset to skb data,
1632 * but taking into account that pulling is expected to
1633 * be very rare operation, it is worth to fight against
1634 * further bloating skb head and crucify ourselves here instead.
1635 * Pure masohism, indeed. 8)8)
1636 */
1637 if (eat) {
1638 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1639 struct sk_buff *clone = NULL;
1640 struct sk_buff *insp = NULL;
1641
1642 do {
Kris Katterjohn09a62662006-01-08 22:24:28 -08001643 BUG_ON(!list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644
1645 if (list->len <= eat) {
1646 /* Eaten as whole. */
1647 eat -= list->len;
1648 list = list->next;
1649 insp = list;
1650 } else {
1651 /* Eaten partially. */
1652
1653 if (skb_shared(list)) {
1654 /* Sucks! We need to fork list. :-( */
1655 clone = skb_clone(list, GFP_ATOMIC);
1656 if (!clone)
1657 return NULL;
1658 insp = list->next;
1659 list = clone;
1660 } else {
1661 /* This may be pulled without
1662 * problems. */
1663 insp = list;
1664 }
1665 if (!pskb_pull(list, eat)) {
Wei Yongjunf3fbbe02009-02-25 00:37:32 +00001666 kfree_skb(clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 return NULL;
1668 }
1669 break;
1670 }
1671 } while (eat);
1672
1673 /* Free pulled out fragments. */
1674 while ((list = skb_shinfo(skb)->frag_list) != insp) {
1675 skb_shinfo(skb)->frag_list = list->next;
1676 kfree_skb(list);
1677 }
1678 /* And insert new clone at head. */
1679 if (clone) {
1680 clone->next = list;
1681 skb_shinfo(skb)->frag_list = clone;
1682 }
1683 }
1684 /* Success! Now we may commit changes to skb data. */
1685
1686pull_pages:
1687 eat = delta;
1688 k = 0;
1689 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001690 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1691
1692 if (size <= eat) {
Ian Campbellea2ab692011-08-22 23:44:58 +00001693 skb_frag_unref(skb, i);
Eric Dumazet9e903e02011-10-18 21:00:24 +00001694 eat -= size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 } else {
1696 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
1697 if (eat) {
1698 skb_shinfo(skb)->frags[k].page_offset += eat;
Eric Dumazet9e903e02011-10-18 21:00:24 +00001699 skb_frag_size_sub(&skb_shinfo(skb)->frags[k], eat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 eat = 0;
1701 }
1702 k++;
1703 }
1704 }
1705 skb_shinfo(skb)->nr_frags = k;
1706
1707 skb->tail += delta;
1708 skb->data_len -= delta;
1709
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001710 return skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001712EXPORT_SYMBOL(__pskb_pull_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713
Eric Dumazet22019b12011-07-29 18:37:31 +00001714/**
1715 * skb_copy_bits - copy bits from skb to kernel buffer
1716 * @skb: source skb
1717 * @offset: offset in source
1718 * @to: destination buffer
1719 * @len: number of bytes to copy
1720 *
1721 * Copy the specified number of bytes from the source skb to the
1722 * destination buffer.
1723 *
1724 * CAUTION ! :
1725 * If its prototype is ever changed,
1726 * check arch/{*}/net/{*}.S files,
1727 * since it is called from BPF assembly code.
1728 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
1730{
David S. Miller1a028e52007-04-27 15:21:23 -07001731 int start = skb_headlen(skb);
David S. Millerfbb398a2009-06-09 00:18:59 -07001732 struct sk_buff *frag_iter;
1733 int i, copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734
1735 if (offset > (int)skb->len - len)
1736 goto fault;
1737
1738 /* Copy header. */
David S. Miller1a028e52007-04-27 15:21:23 -07001739 if ((copy = start - offset) > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 if (copy > len)
1741 copy = len;
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001742 skb_copy_from_linear_data_offset(skb, offset, to, copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 if ((len -= copy) == 0)
1744 return 0;
1745 offset += copy;
1746 to += copy;
1747 }
1748
1749 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07001750 int end;
Eric Dumazet51c56b02012-04-05 11:35:15 +02001751 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001753 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07001754
Eric Dumazet51c56b02012-04-05 11:35:15 +02001755 end = start + skb_frag_size(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 if ((copy = end - offset) > 0) {
1757 u8 *vaddr;
1758
1759 if (copy > len)
1760 copy = len;
1761
Eric Dumazet51c56b02012-04-05 11:35:15 +02001762 vaddr = kmap_atomic(skb_frag_page(f));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 memcpy(to,
Eric Dumazet51c56b02012-04-05 11:35:15 +02001764 vaddr + f->page_offset + offset - start,
1765 copy);
1766 kunmap_atomic(vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767
1768 if ((len -= copy) == 0)
1769 return 0;
1770 offset += copy;
1771 to += copy;
1772 }
David S. Miller1a028e52007-04-27 15:21:23 -07001773 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 }
1775
David S. Millerfbb398a2009-06-09 00:18:59 -07001776 skb_walk_frags(skb, frag_iter) {
1777 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778
David S. Millerfbb398a2009-06-09 00:18:59 -07001779 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780
David S. Millerfbb398a2009-06-09 00:18:59 -07001781 end = start + frag_iter->len;
1782 if ((copy = end - offset) > 0) {
1783 if (copy > len)
1784 copy = len;
1785 if (skb_copy_bits(frag_iter, offset - start, to, copy))
1786 goto fault;
1787 if ((len -= copy) == 0)
1788 return 0;
1789 offset += copy;
1790 to += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 }
David S. Millerfbb398a2009-06-09 00:18:59 -07001792 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 }
Shirley Maa6686f22011-07-06 12:22:12 +00001794
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 if (!len)
1796 return 0;
1797
1798fault:
1799 return -EFAULT;
1800}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001801EXPORT_SYMBOL(skb_copy_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802
Jens Axboe9c55e012007-11-06 23:30:13 -08001803/*
1804 * Callback from splice_to_pipe(), if we need to release some pages
1805 * at the end of the spd in case we error'ed out in filling the pipe.
1806 */
1807static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
1808{
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001809 put_page(spd->pages[i]);
1810}
Jens Axboe9c55e012007-11-06 23:30:13 -08001811
David S. Millera108d5f2012-04-23 23:06:11 -04001812static struct page *linear_to_page(struct page *page, unsigned int *len,
1813 unsigned int *offset,
Eric Dumazet18aafc62013-01-11 14:46:37 +00001814 struct sock *sk)
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001815{
Eric Dumazet5640f762012-09-23 23:04:42 +00001816 struct page_frag *pfrag = sk_page_frag(sk);
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001817
Eric Dumazet5640f762012-09-23 23:04:42 +00001818 if (!sk_page_frag_refill(sk, pfrag))
1819 return NULL;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001820
Eric Dumazet5640f762012-09-23 23:04:42 +00001821 *len = min_t(unsigned int, *len, pfrag->size - pfrag->offset);
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001822
Eric Dumazet5640f762012-09-23 23:04:42 +00001823 memcpy(page_address(pfrag->page) + pfrag->offset,
1824 page_address(page) + *offset, *len);
1825 *offset = pfrag->offset;
1826 pfrag->offset += *len;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001827
Eric Dumazet5640f762012-09-23 23:04:42 +00001828 return pfrag->page;
Jens Axboe9c55e012007-11-06 23:30:13 -08001829}
1830
Eric Dumazet41c73a02012-04-22 12:26:16 +00001831static bool spd_can_coalesce(const struct splice_pipe_desc *spd,
1832 struct page *page,
1833 unsigned int offset)
1834{
1835 return spd->nr_pages &&
1836 spd->pages[spd->nr_pages - 1] == page &&
1837 (spd->partial[spd->nr_pages - 1].offset +
1838 spd->partial[spd->nr_pages - 1].len == offset);
1839}
1840
Jens Axboe9c55e012007-11-06 23:30:13 -08001841/*
1842 * Fill page/offset/length into spd, if it can hold more pages.
1843 */
David S. Millera108d5f2012-04-23 23:06:11 -04001844static bool spd_fill_page(struct splice_pipe_desc *spd,
1845 struct pipe_inode_info *pipe, struct page *page,
1846 unsigned int *len, unsigned int offset,
Eric Dumazet18aafc62013-01-11 14:46:37 +00001847 bool linear,
David S. Millera108d5f2012-04-23 23:06:11 -04001848 struct sock *sk)
Jens Axboe9c55e012007-11-06 23:30:13 -08001849{
Eric Dumazet41c73a02012-04-22 12:26:16 +00001850 if (unlikely(spd->nr_pages == MAX_SKB_FRAGS))
David S. Millera108d5f2012-04-23 23:06:11 -04001851 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08001852
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001853 if (linear) {
Eric Dumazet18aafc62013-01-11 14:46:37 +00001854 page = linear_to_page(page, len, &offset, sk);
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001855 if (!page)
David S. Millera108d5f2012-04-23 23:06:11 -04001856 return true;
Eric Dumazet41c73a02012-04-22 12:26:16 +00001857 }
1858 if (spd_can_coalesce(spd, page, offset)) {
1859 spd->partial[spd->nr_pages - 1].len += *len;
David S. Millera108d5f2012-04-23 23:06:11 -04001860 return false;
Eric Dumazet41c73a02012-04-22 12:26:16 +00001861 }
1862 get_page(page);
Jens Axboe9c55e012007-11-06 23:30:13 -08001863 spd->pages[spd->nr_pages] = page;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001864 spd->partial[spd->nr_pages].len = *len;
Jens Axboe9c55e012007-11-06 23:30:13 -08001865 spd->partial[spd->nr_pages].offset = offset;
Jens Axboe9c55e012007-11-06 23:30:13 -08001866 spd->nr_pages++;
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001867
David S. Millera108d5f2012-04-23 23:06:11 -04001868 return false;
Jens Axboe9c55e012007-11-06 23:30:13 -08001869}
1870
David S. Millera108d5f2012-04-23 23:06:11 -04001871static bool __splice_segment(struct page *page, unsigned int poff,
1872 unsigned int plen, unsigned int *off,
Eric Dumazet18aafc62013-01-11 14:46:37 +00001873 unsigned int *len,
Eric Dumazetd7ccf7c2012-04-23 23:35:04 -04001874 struct splice_pipe_desc *spd, bool linear,
David S. Millera108d5f2012-04-23 23:06:11 -04001875 struct sock *sk,
1876 struct pipe_inode_info *pipe)
Octavian Purdila2870c432008-07-15 00:49:11 -07001877{
1878 if (!*len)
David S. Millera108d5f2012-04-23 23:06:11 -04001879 return true;
Octavian Purdila2870c432008-07-15 00:49:11 -07001880
1881 /* skip this segment if already processed */
1882 if (*off >= plen) {
1883 *off -= plen;
David S. Millera108d5f2012-04-23 23:06:11 -04001884 return false;
Octavian Purdiladb43a282008-06-27 17:27:21 -07001885 }
Jens Axboe9c55e012007-11-06 23:30:13 -08001886
Octavian Purdila2870c432008-07-15 00:49:11 -07001887 /* ignore any bits we already processed */
Eric Dumazet9ca1b222013-01-05 21:31:18 +00001888 poff += *off;
1889 plen -= *off;
1890 *off = 0;
Octavian Purdila2870c432008-07-15 00:49:11 -07001891
Eric Dumazet18aafc62013-01-11 14:46:37 +00001892 do {
1893 unsigned int flen = min(*len, plen);
Octavian Purdila2870c432008-07-15 00:49:11 -07001894
Eric Dumazet18aafc62013-01-11 14:46:37 +00001895 if (spd_fill_page(spd, pipe, page, &flen, poff,
1896 linear, sk))
1897 return true;
1898 poff += flen;
1899 plen -= flen;
1900 *len -= flen;
1901 } while (*len && plen);
Octavian Purdila2870c432008-07-15 00:49:11 -07001902
David S. Millera108d5f2012-04-23 23:06:11 -04001903 return false;
Octavian Purdila2870c432008-07-15 00:49:11 -07001904}
1905
1906/*
David S. Millera108d5f2012-04-23 23:06:11 -04001907 * Map linear and fragment data from the skb to spd. It reports true if the
Octavian Purdila2870c432008-07-15 00:49:11 -07001908 * pipe is full or if we already spliced the requested length.
1909 */
David S. Millera108d5f2012-04-23 23:06:11 -04001910static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe,
1911 unsigned int *offset, unsigned int *len,
1912 struct splice_pipe_desc *spd, struct sock *sk)
Octavian Purdila2870c432008-07-15 00:49:11 -07001913{
1914 int seg;
1915
Eric Dumazet1d0c0b32012-04-27 02:10:03 +00001916 /* map the linear part :
Alexander Duyck2996d312012-05-02 18:18:42 +00001917 * If skb->head_frag is set, this 'linear' part is backed by a
1918 * fragment, and if the head is not shared with any clones then
1919 * we can avoid a copy since we own the head portion of this page.
Jens Axboe9c55e012007-11-06 23:30:13 -08001920 */
Octavian Purdila2870c432008-07-15 00:49:11 -07001921 if (__splice_segment(virt_to_page(skb->data),
1922 (unsigned long) skb->data & (PAGE_SIZE - 1),
1923 skb_headlen(skb),
Eric Dumazet18aafc62013-01-11 14:46:37 +00001924 offset, len, spd,
Alexander Duyck3a7c1ee42012-05-03 01:09:42 +00001925 skb_head_is_locked(skb),
Eric Dumazet1d0c0b32012-04-27 02:10:03 +00001926 sk, pipe))
David S. Millera108d5f2012-04-23 23:06:11 -04001927 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08001928
1929 /*
1930 * then map the fragments
1931 */
Jens Axboe9c55e012007-11-06 23:30:13 -08001932 for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
1933 const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
1934
Ian Campbellea2ab692011-08-22 23:44:58 +00001935 if (__splice_segment(skb_frag_page(f),
Eric Dumazet9e903e02011-10-18 21:00:24 +00001936 f->page_offset, skb_frag_size(f),
Eric Dumazet18aafc62013-01-11 14:46:37 +00001937 offset, len, spd, false, sk, pipe))
David S. Millera108d5f2012-04-23 23:06:11 -04001938 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08001939 }
1940
David S. Millera108d5f2012-04-23 23:06:11 -04001941 return false;
Jens Axboe9c55e012007-11-06 23:30:13 -08001942}
1943
Hannes Frederic Sowaa60e3cc2015-05-21 17:00:00 +02001944ssize_t skb_socket_splice(struct sock *sk,
1945 struct pipe_inode_info *pipe,
1946 struct splice_pipe_desc *spd)
1947{
1948 int ret;
1949
1950 /* Drop the socket lock, otherwise we have reverse
1951 * locking dependencies between sk_lock and i_mutex
1952 * here as compared to sendfile(). We enter here
1953 * with the socket lock held, and splice_to_pipe() will
1954 * grab the pipe inode lock. For sendfile() emulation,
1955 * we call into ->sendpage() with the i_mutex lock held
1956 * and networking will grab the socket lock.
1957 */
1958 release_sock(sk);
1959 ret = splice_to_pipe(pipe, spd);
1960 lock_sock(sk);
1961
1962 return ret;
1963}
1964
Jens Axboe9c55e012007-11-06 23:30:13 -08001965/*
1966 * Map data from the skb to a pipe. Should handle both the linear part,
1967 * the fragments, and the frag list. It does NOT handle frag lists within
1968 * the frag list, if such a thing exists. We'd probably need to recurse to
1969 * handle that cleanly.
1970 */
Hannes Frederic Sowaa60e3cc2015-05-21 17:00:00 +02001971int skb_splice_bits(struct sk_buff *skb, struct sock *sk, unsigned int offset,
Jens Axboe9c55e012007-11-06 23:30:13 -08001972 struct pipe_inode_info *pipe, unsigned int tlen,
Hannes Frederic Sowaa60e3cc2015-05-21 17:00:00 +02001973 unsigned int flags,
1974 ssize_t (*splice_cb)(struct sock *,
1975 struct pipe_inode_info *,
1976 struct splice_pipe_desc *))
Jens Axboe9c55e012007-11-06 23:30:13 -08001977{
Eric Dumazet41c73a02012-04-22 12:26:16 +00001978 struct partial_page partial[MAX_SKB_FRAGS];
1979 struct page *pages[MAX_SKB_FRAGS];
Jens Axboe9c55e012007-11-06 23:30:13 -08001980 struct splice_pipe_desc spd = {
1981 .pages = pages,
1982 .partial = partial,
Eric Dumazet047fe362012-06-12 15:24:40 +02001983 .nr_pages_max = MAX_SKB_FRAGS,
Jens Axboe9c55e012007-11-06 23:30:13 -08001984 .flags = flags,
Miklos Szeredi28a625c2014-01-22 19:36:57 +01001985 .ops = &nosteal_pipe_buf_ops,
Jens Axboe9c55e012007-11-06 23:30:13 -08001986 .spd_release = sock_spd_release,
1987 };
David S. Millerfbb398a2009-06-09 00:18:59 -07001988 struct sk_buff *frag_iter;
Jens Axboe35f3d142010-05-20 10:43:18 +02001989 int ret = 0;
1990
Jens Axboe9c55e012007-11-06 23:30:13 -08001991 /*
1992 * __skb_splice_bits() only fails if the output has no room left,
1993 * so no point in going over the frag_list for the error case.
1994 */
Jens Axboe35f3d142010-05-20 10:43:18 +02001995 if (__skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk))
Jens Axboe9c55e012007-11-06 23:30:13 -08001996 goto done;
1997 else if (!tlen)
1998 goto done;
1999
2000 /*
2001 * now see if we have a frag_list to map
2002 */
David S. Millerfbb398a2009-06-09 00:18:59 -07002003 skb_walk_frags(skb, frag_iter) {
2004 if (!tlen)
2005 break;
Jens Axboe35f3d142010-05-20 10:43:18 +02002006 if (__skb_splice_bits(frag_iter, pipe, &offset, &tlen, &spd, sk))
David S. Millerfbb398a2009-06-09 00:18:59 -07002007 break;
Jens Axboe9c55e012007-11-06 23:30:13 -08002008 }
2009
2010done:
Hannes Frederic Sowaa60e3cc2015-05-21 17:00:00 +02002011 if (spd.nr_pages)
2012 ret = splice_cb(sk, pipe, &spd);
Jens Axboe9c55e012007-11-06 23:30:13 -08002013
Jens Axboe35f3d142010-05-20 10:43:18 +02002014 return ret;
Jens Axboe9c55e012007-11-06 23:30:13 -08002015}
Hannes Frederic Sowa2b514572015-05-21 17:00:01 +02002016EXPORT_SYMBOL_GPL(skb_splice_bits);
Jens Axboe9c55e012007-11-06 23:30:13 -08002017
Herbert Xu357b40a2005-04-19 22:30:14 -07002018/**
2019 * skb_store_bits - store bits from kernel buffer to skb
2020 * @skb: destination buffer
2021 * @offset: offset in destination
2022 * @from: source buffer
2023 * @len: number of bytes to copy
2024 *
2025 * Copy the specified number of bytes from the source buffer to the
2026 * destination skb. This function handles all the messy bits of
2027 * traversing fragment lists and such.
2028 */
2029
Stephen Hemminger0c6fcc82007-04-20 16:40:01 -07002030int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
Herbert Xu357b40a2005-04-19 22:30:14 -07002031{
David S. Miller1a028e52007-04-27 15:21:23 -07002032 int start = skb_headlen(skb);
David S. Millerfbb398a2009-06-09 00:18:59 -07002033 struct sk_buff *frag_iter;
2034 int i, copy;
Herbert Xu357b40a2005-04-19 22:30:14 -07002035
2036 if (offset > (int)skb->len - len)
2037 goto fault;
2038
David S. Miller1a028e52007-04-27 15:21:23 -07002039 if ((copy = start - offset) > 0) {
Herbert Xu357b40a2005-04-19 22:30:14 -07002040 if (copy > len)
2041 copy = len;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03002042 skb_copy_to_linear_data_offset(skb, offset, from, copy);
Herbert Xu357b40a2005-04-19 22:30:14 -07002043 if ((len -= copy) == 0)
2044 return 0;
2045 offset += copy;
2046 from += copy;
2047 }
2048
2049 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2050 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
David S. Miller1a028e52007-04-27 15:21:23 -07002051 int end;
Herbert Xu357b40a2005-04-19 22:30:14 -07002052
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002053 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07002054
Eric Dumazet9e903e02011-10-18 21:00:24 +00002055 end = start + skb_frag_size(frag);
Herbert Xu357b40a2005-04-19 22:30:14 -07002056 if ((copy = end - offset) > 0) {
2057 u8 *vaddr;
2058
2059 if (copy > len)
2060 copy = len;
2061
Eric Dumazet51c56b02012-04-05 11:35:15 +02002062 vaddr = kmap_atomic(skb_frag_page(frag));
David S. Miller1a028e52007-04-27 15:21:23 -07002063 memcpy(vaddr + frag->page_offset + offset - start,
2064 from, copy);
Eric Dumazet51c56b02012-04-05 11:35:15 +02002065 kunmap_atomic(vaddr);
Herbert Xu357b40a2005-04-19 22:30:14 -07002066
2067 if ((len -= copy) == 0)
2068 return 0;
2069 offset += copy;
2070 from += copy;
2071 }
David S. Miller1a028e52007-04-27 15:21:23 -07002072 start = end;
Herbert Xu357b40a2005-04-19 22:30:14 -07002073 }
2074
David S. Millerfbb398a2009-06-09 00:18:59 -07002075 skb_walk_frags(skb, frag_iter) {
2076 int end;
Herbert Xu357b40a2005-04-19 22:30:14 -07002077
David S. Millerfbb398a2009-06-09 00:18:59 -07002078 WARN_ON(start > offset + len);
Herbert Xu357b40a2005-04-19 22:30:14 -07002079
David S. Millerfbb398a2009-06-09 00:18:59 -07002080 end = start + frag_iter->len;
2081 if ((copy = end - offset) > 0) {
2082 if (copy > len)
2083 copy = len;
2084 if (skb_store_bits(frag_iter, offset - start,
2085 from, copy))
2086 goto fault;
2087 if ((len -= copy) == 0)
2088 return 0;
2089 offset += copy;
2090 from += copy;
Herbert Xu357b40a2005-04-19 22:30:14 -07002091 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002092 start = end;
Herbert Xu357b40a2005-04-19 22:30:14 -07002093 }
2094 if (!len)
2095 return 0;
2096
2097fault:
2098 return -EFAULT;
2099}
Herbert Xu357b40a2005-04-19 22:30:14 -07002100EXPORT_SYMBOL(skb_store_bits);
2101
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102/* Checksum skb data. */
Daniel Borkmann2817a332013-10-30 11:50:51 +01002103__wsum __skb_checksum(const struct sk_buff *skb, int offset, int len,
2104 __wsum csum, const struct skb_checksum_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105{
David S. Miller1a028e52007-04-27 15:21:23 -07002106 int start = skb_headlen(skb);
2107 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07002108 struct sk_buff *frag_iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 int pos = 0;
2110
2111 /* Checksum header. */
2112 if (copy > 0) {
2113 if (copy > len)
2114 copy = len;
Daniel Borkmann2817a332013-10-30 11:50:51 +01002115 csum = ops->update(skb->data + offset, copy, csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 if ((len -= copy) == 0)
2117 return csum;
2118 offset += copy;
2119 pos = copy;
2120 }
2121
2122 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07002123 int end;
Eric Dumazet51c56b02012-04-05 11:35:15 +02002124 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002126 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07002127
Eric Dumazet51c56b02012-04-05 11:35:15 +02002128 end = start + skb_frag_size(frag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 if ((copy = end - offset) > 0) {
Al Viro44bb9362006-11-14 21:36:14 -08002130 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 u8 *vaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132
2133 if (copy > len)
2134 copy = len;
Eric Dumazet51c56b02012-04-05 11:35:15 +02002135 vaddr = kmap_atomic(skb_frag_page(frag));
Daniel Borkmann2817a332013-10-30 11:50:51 +01002136 csum2 = ops->update(vaddr + frag->page_offset +
2137 offset - start, copy, 0);
Eric Dumazet51c56b02012-04-05 11:35:15 +02002138 kunmap_atomic(vaddr);
Daniel Borkmann2817a332013-10-30 11:50:51 +01002139 csum = ops->combine(csum, csum2, pos, copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 if (!(len -= copy))
2141 return csum;
2142 offset += copy;
2143 pos += copy;
2144 }
David S. Miller1a028e52007-04-27 15:21:23 -07002145 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 }
2147
David S. Millerfbb398a2009-06-09 00:18:59 -07002148 skb_walk_frags(skb, frag_iter) {
2149 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150
David S. Millerfbb398a2009-06-09 00:18:59 -07002151 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152
David S. Millerfbb398a2009-06-09 00:18:59 -07002153 end = start + frag_iter->len;
2154 if ((copy = end - offset) > 0) {
2155 __wsum csum2;
2156 if (copy > len)
2157 copy = len;
Daniel Borkmann2817a332013-10-30 11:50:51 +01002158 csum2 = __skb_checksum(frag_iter, offset - start,
2159 copy, 0, ops);
2160 csum = ops->combine(csum, csum2, pos, copy);
David S. Millerfbb398a2009-06-09 00:18:59 -07002161 if ((len -= copy) == 0)
2162 return csum;
2163 offset += copy;
2164 pos += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002166 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08002168 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169
2170 return csum;
2171}
Daniel Borkmann2817a332013-10-30 11:50:51 +01002172EXPORT_SYMBOL(__skb_checksum);
2173
2174__wsum skb_checksum(const struct sk_buff *skb, int offset,
2175 int len, __wsum csum)
2176{
2177 const struct skb_checksum_ops ops = {
Daniel Borkmanncea80ea2013-11-04 17:10:25 +01002178 .update = csum_partial_ext,
Daniel Borkmann2817a332013-10-30 11:50:51 +01002179 .combine = csum_block_add_ext,
2180 };
2181
2182 return __skb_checksum(skb, offset, len, csum, &ops);
2183}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002184EXPORT_SYMBOL(skb_checksum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185
2186/* Both of above in one bottle. */
2187
Al Viro81d77662006-11-14 21:37:33 -08002188__wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
2189 u8 *to, int len, __wsum csum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190{
David S. Miller1a028e52007-04-27 15:21:23 -07002191 int start = skb_headlen(skb);
2192 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07002193 struct sk_buff *frag_iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 int pos = 0;
2195
2196 /* Copy header. */
2197 if (copy > 0) {
2198 if (copy > len)
2199 copy = len;
2200 csum = csum_partial_copy_nocheck(skb->data + offset, to,
2201 copy, csum);
2202 if ((len -= copy) == 0)
2203 return csum;
2204 offset += copy;
2205 to += copy;
2206 pos = copy;
2207 }
2208
2209 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07002210 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002212 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07002213
Eric Dumazet9e903e02011-10-18 21:00:24 +00002214 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 if ((copy = end - offset) > 0) {
Al Viro50842052006-11-14 21:36:34 -08002216 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 u8 *vaddr;
2218 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2219
2220 if (copy > len)
2221 copy = len;
Eric Dumazet51c56b02012-04-05 11:35:15 +02002222 vaddr = kmap_atomic(skb_frag_page(frag));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 csum2 = csum_partial_copy_nocheck(vaddr +
David S. Miller1a028e52007-04-27 15:21:23 -07002224 frag->page_offset +
2225 offset - start, to,
2226 copy, 0);
Eric Dumazet51c56b02012-04-05 11:35:15 +02002227 kunmap_atomic(vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 csum = csum_block_add(csum, csum2, pos);
2229 if (!(len -= copy))
2230 return csum;
2231 offset += copy;
2232 to += copy;
2233 pos += copy;
2234 }
David S. Miller1a028e52007-04-27 15:21:23 -07002235 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 }
2237
David S. Millerfbb398a2009-06-09 00:18:59 -07002238 skb_walk_frags(skb, frag_iter) {
2239 __wsum csum2;
2240 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241
David S. Millerfbb398a2009-06-09 00:18:59 -07002242 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243
David S. Millerfbb398a2009-06-09 00:18:59 -07002244 end = start + frag_iter->len;
2245 if ((copy = end - offset) > 0) {
2246 if (copy > len)
2247 copy = len;
2248 csum2 = skb_copy_and_csum_bits(frag_iter,
2249 offset - start,
2250 to, copy, 0);
2251 csum = csum_block_add(csum, csum2, pos);
2252 if ((len -= copy) == 0)
2253 return csum;
2254 offset += copy;
2255 to += copy;
2256 pos += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002258 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08002260 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 return csum;
2262}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002263EXPORT_SYMBOL(skb_copy_and_csum_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264
Thomas Grafaf2806f2013-12-13 15:22:17 +01002265 /**
2266 * skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy()
2267 * @from: source buffer
2268 *
2269 * Calculates the amount of linear headroom needed in the 'to' skb passed
2270 * into skb_zerocopy().
2271 */
2272unsigned int
2273skb_zerocopy_headlen(const struct sk_buff *from)
2274{
2275 unsigned int hlen = 0;
2276
2277 if (!from->head_frag ||
2278 skb_headlen(from) < L1_CACHE_BYTES ||
2279 skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
2280 hlen = skb_headlen(from);
2281
2282 if (skb_has_frag_list(from))
2283 hlen = from->len;
2284
2285 return hlen;
2286}
2287EXPORT_SYMBOL_GPL(skb_zerocopy_headlen);
2288
2289/**
2290 * skb_zerocopy - Zero copy skb to skb
2291 * @to: destination buffer
Masanari Iida7fceb4d2014-01-29 01:05:28 +09002292 * @from: source buffer
Thomas Grafaf2806f2013-12-13 15:22:17 +01002293 * @len: number of bytes to copy from source buffer
2294 * @hlen: size of linear headroom in destination buffer
2295 *
2296 * Copies up to `len` bytes from `from` to `to` by creating references
2297 * to the frags in the source buffer.
2298 *
2299 * The `hlen` as calculated by skb_zerocopy_headlen() specifies the
2300 * headroom in the `to` buffer.
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002301 *
2302 * Return value:
2303 * 0: everything is OK
2304 * -ENOMEM: couldn't orphan frags of @from due to lack of memory
2305 * -EFAULT: skb_copy_bits() found some problem with skb geometry
Thomas Grafaf2806f2013-12-13 15:22:17 +01002306 */
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002307int
2308skb_zerocopy(struct sk_buff *to, struct sk_buff *from, int len, int hlen)
Thomas Grafaf2806f2013-12-13 15:22:17 +01002309{
2310 int i, j = 0;
2311 int plen = 0; /* length of skb->head fragment */
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002312 int ret;
Thomas Grafaf2806f2013-12-13 15:22:17 +01002313 struct page *page;
2314 unsigned int offset;
2315
2316 BUG_ON(!from->head_frag && !hlen);
2317
2318 /* dont bother with small payloads */
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002319 if (len <= skb_tailroom(to))
2320 return skb_copy_bits(from, 0, skb_put(to, len), len);
Thomas Grafaf2806f2013-12-13 15:22:17 +01002321
2322 if (hlen) {
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002323 ret = skb_copy_bits(from, 0, skb_put(to, hlen), hlen);
2324 if (unlikely(ret))
2325 return ret;
Thomas Grafaf2806f2013-12-13 15:22:17 +01002326 len -= hlen;
2327 } else {
2328 plen = min_t(int, skb_headlen(from), len);
2329 if (plen) {
2330 page = virt_to_head_page(from->head);
2331 offset = from->data - (unsigned char *)page_address(page);
2332 __skb_fill_page_desc(to, 0, page, offset, plen);
2333 get_page(page);
2334 j = 1;
2335 len -= plen;
2336 }
2337 }
2338
2339 to->truesize += len + plen;
2340 to->len += len + plen;
2341 to->data_len += len + plen;
2342
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002343 if (unlikely(skb_orphan_frags(from, GFP_ATOMIC))) {
2344 skb_tx_error(from);
2345 return -ENOMEM;
2346 }
2347
Thomas Grafaf2806f2013-12-13 15:22:17 +01002348 for (i = 0; i < skb_shinfo(from)->nr_frags; i++) {
2349 if (!len)
2350 break;
2351 skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i];
2352 skb_shinfo(to)->frags[j].size = min_t(int, skb_shinfo(to)->frags[j].size, len);
2353 len -= skb_shinfo(to)->frags[j].size;
2354 skb_frag_ref(to, j);
2355 j++;
2356 }
2357 skb_shinfo(to)->nr_frags = j;
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002358
2359 return 0;
Thomas Grafaf2806f2013-12-13 15:22:17 +01002360}
2361EXPORT_SYMBOL_GPL(skb_zerocopy);
2362
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
2364{
Al Virod3bc23e2006-11-14 21:24:49 -08002365 __wsum csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 long csstart;
2367
Patrick McHardy84fa7932006-08-29 16:44:56 -07002368 if (skb->ip_summed == CHECKSUM_PARTIAL)
Michał Mirosław55508d62010-12-14 15:24:08 +00002369 csstart = skb_checksum_start_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 else
2371 csstart = skb_headlen(skb);
2372
Kris Katterjohn09a62662006-01-08 22:24:28 -08002373 BUG_ON(csstart > skb_headlen(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03002375 skb_copy_from_linear_data(skb, to, csstart);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376
2377 csum = 0;
2378 if (csstart != skb->len)
2379 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
2380 skb->len - csstart, 0);
2381
Patrick McHardy84fa7932006-08-29 16:44:56 -07002382 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Al Viroff1dcad2006-11-20 18:07:29 -08002383 long csstuff = csstart + skb->csum_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384
Al Virod3bc23e2006-11-14 21:24:49 -08002385 *((__sum16 *)(to + csstuff)) = csum_fold(csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 }
2387}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002388EXPORT_SYMBOL(skb_copy_and_csum_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389
2390/**
2391 * skb_dequeue - remove from the head of the queue
2392 * @list: list to dequeue from
2393 *
2394 * Remove the head of the list. The list lock is taken so the function
2395 * may be used safely with other locking list functions. The head item is
2396 * returned or %NULL if the list is empty.
2397 */
2398
2399struct sk_buff *skb_dequeue(struct sk_buff_head *list)
2400{
2401 unsigned long flags;
2402 struct sk_buff *result;
2403
2404 spin_lock_irqsave(&list->lock, flags);
2405 result = __skb_dequeue(list);
2406 spin_unlock_irqrestore(&list->lock, flags);
2407 return result;
2408}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002409EXPORT_SYMBOL(skb_dequeue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410
2411/**
2412 * skb_dequeue_tail - remove from the tail of the queue
2413 * @list: list to dequeue from
2414 *
2415 * Remove the tail of the list. The list lock is taken so the function
2416 * may be used safely with other locking list functions. The tail item is
2417 * returned or %NULL if the list is empty.
2418 */
2419struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
2420{
2421 unsigned long flags;
2422 struct sk_buff *result;
2423
2424 spin_lock_irqsave(&list->lock, flags);
2425 result = __skb_dequeue_tail(list);
2426 spin_unlock_irqrestore(&list->lock, flags);
2427 return result;
2428}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002429EXPORT_SYMBOL(skb_dequeue_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430
2431/**
2432 * skb_queue_purge - empty a list
2433 * @list: list to empty
2434 *
2435 * Delete all buffers on an &sk_buff list. Each buffer is removed from
2436 * the list and one reference dropped. This function takes the list
2437 * lock and is atomic with respect to other list locking functions.
2438 */
2439void skb_queue_purge(struct sk_buff_head *list)
2440{
2441 struct sk_buff *skb;
2442 while ((skb = skb_dequeue(list)) != NULL)
2443 kfree_skb(skb);
2444}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002445EXPORT_SYMBOL(skb_queue_purge);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446
2447/**
2448 * skb_queue_head - queue a buffer at the list head
2449 * @list: list to use
2450 * @newsk: buffer to queue
2451 *
2452 * Queue a buffer at the start of the list. This function takes the
2453 * list lock and can be used safely with other locking &sk_buff functions
2454 * safely.
2455 *
2456 * A buffer cannot be placed on two lists at the same time.
2457 */
2458void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
2459{
2460 unsigned long flags;
2461
2462 spin_lock_irqsave(&list->lock, flags);
2463 __skb_queue_head(list, newsk);
2464 spin_unlock_irqrestore(&list->lock, flags);
2465}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002466EXPORT_SYMBOL(skb_queue_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467
2468/**
2469 * skb_queue_tail - queue a buffer at the list tail
2470 * @list: list to use
2471 * @newsk: buffer to queue
2472 *
2473 * Queue a buffer at the tail of the list. This function takes the
2474 * list lock and can be used safely with other locking &sk_buff functions
2475 * safely.
2476 *
2477 * A buffer cannot be placed on two lists at the same time.
2478 */
2479void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
2480{
2481 unsigned long flags;
2482
2483 spin_lock_irqsave(&list->lock, flags);
2484 __skb_queue_tail(list, newsk);
2485 spin_unlock_irqrestore(&list->lock, flags);
2486}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002487EXPORT_SYMBOL(skb_queue_tail);
David S. Miller8728b832005-08-09 19:25:21 -07002488
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489/**
2490 * skb_unlink - remove a buffer from a list
2491 * @skb: buffer to remove
David S. Miller8728b832005-08-09 19:25:21 -07002492 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 *
David S. Miller8728b832005-08-09 19:25:21 -07002494 * Remove a packet from a list. The list locks are taken and this
2495 * function is atomic with respect to other list locked calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 *
David S. Miller8728b832005-08-09 19:25:21 -07002497 * You must know what list the SKB is on.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 */
David S. Miller8728b832005-08-09 19:25:21 -07002499void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500{
David S. Miller8728b832005-08-09 19:25:21 -07002501 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502
David S. Miller8728b832005-08-09 19:25:21 -07002503 spin_lock_irqsave(&list->lock, flags);
2504 __skb_unlink(skb, list);
2505 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002507EXPORT_SYMBOL(skb_unlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509/**
2510 * skb_append - append a buffer
2511 * @old: buffer to insert after
2512 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07002513 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 *
2515 * Place a packet after a given packet in a list. The list locks are taken
2516 * and this function is atomic with respect to other list locked calls.
2517 * A buffer cannot be placed on two lists at the same time.
2518 */
David S. Miller8728b832005-08-09 19:25:21 -07002519void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520{
2521 unsigned long flags;
2522
David S. Miller8728b832005-08-09 19:25:21 -07002523 spin_lock_irqsave(&list->lock, flags);
Gerrit Renker7de6c032008-04-14 00:05:09 -07002524 __skb_queue_after(list, old, newsk);
David S. Miller8728b832005-08-09 19:25:21 -07002525 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002527EXPORT_SYMBOL(skb_append);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528
2529/**
2530 * skb_insert - insert a buffer
2531 * @old: buffer to insert before
2532 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07002533 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 *
David S. Miller8728b832005-08-09 19:25:21 -07002535 * Place a packet before a given packet in a list. The list locks are
2536 * taken and this function is atomic with respect to other list locked
2537 * calls.
2538 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 * A buffer cannot be placed on two lists at the same time.
2540 */
David S. Miller8728b832005-08-09 19:25:21 -07002541void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542{
2543 unsigned long flags;
2544
David S. Miller8728b832005-08-09 19:25:21 -07002545 spin_lock_irqsave(&list->lock, flags);
2546 __skb_insert(newsk, old->prev, old, list);
2547 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002549EXPORT_SYMBOL(skb_insert);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551static inline void skb_split_inside_header(struct sk_buff *skb,
2552 struct sk_buff* skb1,
2553 const u32 len, const int pos)
2554{
2555 int i;
2556
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03002557 skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
2558 pos - len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 /* And move data appendix as is. */
2560 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
2561 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
2562
2563 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
2564 skb_shinfo(skb)->nr_frags = 0;
2565 skb1->data_len = skb->data_len;
2566 skb1->len += skb1->data_len;
2567 skb->data_len = 0;
2568 skb->len = len;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002569 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570}
2571
2572static inline void skb_split_no_header(struct sk_buff *skb,
2573 struct sk_buff* skb1,
2574 const u32 len, int pos)
2575{
2576 int i, k = 0;
2577 const int nfrags = skb_shinfo(skb)->nr_frags;
2578
2579 skb_shinfo(skb)->nr_frags = 0;
2580 skb1->len = skb1->data_len = skb->len - len;
2581 skb->len = len;
2582 skb->data_len = len - pos;
2583
2584 for (i = 0; i < nfrags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00002585 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586
2587 if (pos + size > len) {
2588 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
2589
2590 if (pos < len) {
2591 /* Split frag.
2592 * We have two variants in this case:
2593 * 1. Move all the frag to the second
2594 * part, if it is possible. F.e.
2595 * this approach is mandatory for TUX,
2596 * where splitting is expensive.
2597 * 2. Split is accurately. We make this.
2598 */
Ian Campbellea2ab692011-08-22 23:44:58 +00002599 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002601 skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos);
2602 skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 skb_shinfo(skb)->nr_frags++;
2604 }
2605 k++;
2606 } else
2607 skb_shinfo(skb)->nr_frags++;
2608 pos += size;
2609 }
2610 skb_shinfo(skb1)->nr_frags = k;
2611}
2612
2613/**
2614 * skb_split - Split fragmented skb to two parts at length len.
2615 * @skb: the buffer to split
2616 * @skb1: the buffer to receive the second part
2617 * @len: new length for skb
2618 */
2619void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
2620{
2621 int pos = skb_headlen(skb);
2622
Amerigo Wang68534c62013-02-19 22:51:30 +00002623 skb_shinfo(skb1)->tx_flags = skb_shinfo(skb)->tx_flags & SKBTX_SHARED_FRAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 if (len < pos) /* Split line is inside header. */
2625 skb_split_inside_header(skb, skb1, len, pos);
2626 else /* Second chunk has no header, nothing to copy. */
2627 skb_split_no_header(skb, skb1, len, pos);
2628}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002629EXPORT_SYMBOL(skb_split);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630
Ilpo Järvinen9f782db2008-11-25 13:57:01 -08002631/* Shifting from/to a cloned skb is a no-go.
2632 *
2633 * Caller cannot keep skb_shinfo related pointers past calling here!
2634 */
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002635static int skb_prepare_for_shift(struct sk_buff *skb)
2636{
Ilpo Järvinen0ace2852008-11-24 21:30:21 -08002637 return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002638}
2639
2640/**
2641 * skb_shift - Shifts paged data partially from skb to another
2642 * @tgt: buffer into which tail data gets added
2643 * @skb: buffer from which the paged data comes from
2644 * @shiftlen: shift up to this many bytes
2645 *
2646 * Attempts to shift up to shiftlen worth of bytes, which may be less than
Feng King20e994a2011-11-21 01:47:11 +00002647 * the length of the skb, from skb to tgt. Returns number bytes shifted.
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002648 * It's up to caller to free skb if everything was shifted.
2649 *
2650 * If @tgt runs out of frags, the whole operation is aborted.
2651 *
2652 * Skb cannot include anything else but paged data while tgt is allowed
2653 * to have non-paged data as well.
2654 *
2655 * TODO: full sized shift could be optimized but that would need
2656 * specialized skb free'er to handle frags without up-to-date nr_frags.
2657 */
2658int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
2659{
2660 int from, to, merge, todo;
2661 struct skb_frag_struct *fragfrom, *fragto;
2662
2663 BUG_ON(shiftlen > skb->len);
2664 BUG_ON(skb_headlen(skb)); /* Would corrupt stream */
2665
2666 todo = shiftlen;
2667 from = 0;
2668 to = skb_shinfo(tgt)->nr_frags;
2669 fragfrom = &skb_shinfo(skb)->frags[from];
2670
2671 /* Actual merge is delayed until the point when we know we can
2672 * commit all, so that we don't have to undo partial changes
2673 */
2674 if (!to ||
Ian Campbellea2ab692011-08-22 23:44:58 +00002675 !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom),
2676 fragfrom->page_offset)) {
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002677 merge = -1;
2678 } else {
2679 merge = to - 1;
2680
Eric Dumazet9e903e02011-10-18 21:00:24 +00002681 todo -= skb_frag_size(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002682 if (todo < 0) {
2683 if (skb_prepare_for_shift(skb) ||
2684 skb_prepare_for_shift(tgt))
2685 return 0;
2686
Ilpo Järvinen9f782db2008-11-25 13:57:01 -08002687 /* All previous frag pointers might be stale! */
2688 fragfrom = &skb_shinfo(skb)->frags[from];
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002689 fragto = &skb_shinfo(tgt)->frags[merge];
2690
Eric Dumazet9e903e02011-10-18 21:00:24 +00002691 skb_frag_size_add(fragto, shiftlen);
2692 skb_frag_size_sub(fragfrom, shiftlen);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002693 fragfrom->page_offset += shiftlen;
2694
2695 goto onlymerged;
2696 }
2697
2698 from++;
2699 }
2700
2701 /* Skip full, not-fitting skb to avoid expensive operations */
2702 if ((shiftlen == skb->len) &&
2703 (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to))
2704 return 0;
2705
2706 if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt))
2707 return 0;
2708
2709 while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) {
2710 if (to == MAX_SKB_FRAGS)
2711 return 0;
2712
2713 fragfrom = &skb_shinfo(skb)->frags[from];
2714 fragto = &skb_shinfo(tgt)->frags[to];
2715
Eric Dumazet9e903e02011-10-18 21:00:24 +00002716 if (todo >= skb_frag_size(fragfrom)) {
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002717 *fragto = *fragfrom;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002718 todo -= skb_frag_size(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002719 from++;
2720 to++;
2721
2722 } else {
Ian Campbellea2ab692011-08-22 23:44:58 +00002723 __skb_frag_ref(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002724 fragto->page = fragfrom->page;
2725 fragto->page_offset = fragfrom->page_offset;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002726 skb_frag_size_set(fragto, todo);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002727
2728 fragfrom->page_offset += todo;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002729 skb_frag_size_sub(fragfrom, todo);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002730 todo = 0;
2731
2732 to++;
2733 break;
2734 }
2735 }
2736
2737 /* Ready to "commit" this state change to tgt */
2738 skb_shinfo(tgt)->nr_frags = to;
2739
2740 if (merge >= 0) {
2741 fragfrom = &skb_shinfo(skb)->frags[0];
2742 fragto = &skb_shinfo(tgt)->frags[merge];
2743
Eric Dumazet9e903e02011-10-18 21:00:24 +00002744 skb_frag_size_add(fragto, skb_frag_size(fragfrom));
Ian Campbellea2ab692011-08-22 23:44:58 +00002745 __skb_frag_unref(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002746 }
2747
2748 /* Reposition in the original skb */
2749 to = 0;
2750 while (from < skb_shinfo(skb)->nr_frags)
2751 skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++];
2752 skb_shinfo(skb)->nr_frags = to;
2753
2754 BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags);
2755
2756onlymerged:
2757 /* Most likely the tgt won't ever need its checksum anymore, skb on
2758 * the other hand might need it if it needs to be resent
2759 */
2760 tgt->ip_summed = CHECKSUM_PARTIAL;
2761 skb->ip_summed = CHECKSUM_PARTIAL;
2762
2763 /* Yak, is it really working this way? Some helper please? */
2764 skb->len -= shiftlen;
2765 skb->data_len -= shiftlen;
2766 skb->truesize -= shiftlen;
2767 tgt->len += shiftlen;
2768 tgt->data_len += shiftlen;
2769 tgt->truesize += shiftlen;
2770
2771 return shiftlen;
2772}
2773
Thomas Graf677e90e2005-06-23 20:59:51 -07002774/**
2775 * skb_prepare_seq_read - Prepare a sequential read of skb data
2776 * @skb: the buffer to read
2777 * @from: lower offset of data to be read
2778 * @to: upper offset of data to be read
2779 * @st: state variable
2780 *
2781 * Initializes the specified state variable. Must be called before
2782 * invoking skb_seq_read() for the first time.
2783 */
2784void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
2785 unsigned int to, struct skb_seq_state *st)
2786{
2787 st->lower_offset = from;
2788 st->upper_offset = to;
2789 st->root_skb = st->cur_skb = skb;
2790 st->frag_idx = st->stepped_offset = 0;
2791 st->frag_data = NULL;
2792}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002793EXPORT_SYMBOL(skb_prepare_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07002794
2795/**
2796 * skb_seq_read - Sequentially read skb data
2797 * @consumed: number of bytes consumed by the caller so far
2798 * @data: destination pointer for data to be returned
2799 * @st: state variable
2800 *
Mathias Krausebc323832013-11-07 14:18:26 +01002801 * Reads a block of skb data at @consumed relative to the
Thomas Graf677e90e2005-06-23 20:59:51 -07002802 * lower offset specified to skb_prepare_seq_read(). Assigns
Mathias Krausebc323832013-11-07 14:18:26 +01002803 * the head of the data block to @data and returns the length
Thomas Graf677e90e2005-06-23 20:59:51 -07002804 * of the block or 0 if the end of the skb data or the upper
2805 * offset has been reached.
2806 *
2807 * The caller is not required to consume all of the data
Mathias Krausebc323832013-11-07 14:18:26 +01002808 * returned, i.e. @consumed is typically set to the number
Thomas Graf677e90e2005-06-23 20:59:51 -07002809 * of bytes already consumed and the next call to
2810 * skb_seq_read() will return the remaining part of the block.
2811 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002812 * Note 1: The size of each block of data returned can be arbitrary,
Masanari Iidae793c0f2014-09-04 23:44:36 +09002813 * this limitation is the cost for zerocopy sequential
Thomas Graf677e90e2005-06-23 20:59:51 -07002814 * reads of potentially non linear data.
2815 *
Randy Dunlapbc2cda12008-02-13 15:03:25 -08002816 * Note 2: Fragment lists within fragments are not implemented
Thomas Graf677e90e2005-06-23 20:59:51 -07002817 * at the moment, state->root_skb could be replaced with
2818 * a stack for this purpose.
2819 */
2820unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
2821 struct skb_seq_state *st)
2822{
2823 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
2824 skb_frag_t *frag;
2825
Wedson Almeida Filhoaeb193e2013-06-23 23:33:48 -07002826 if (unlikely(abs_offset >= st->upper_offset)) {
2827 if (st->frag_data) {
2828 kunmap_atomic(st->frag_data);
2829 st->frag_data = NULL;
2830 }
Thomas Graf677e90e2005-06-23 20:59:51 -07002831 return 0;
Wedson Almeida Filhoaeb193e2013-06-23 23:33:48 -07002832 }
Thomas Graf677e90e2005-06-23 20:59:51 -07002833
2834next_skb:
Herbert Xu95e3b242009-01-29 16:07:52 -08002835 block_limit = skb_headlen(st->cur_skb) + st->stepped_offset;
Thomas Graf677e90e2005-06-23 20:59:51 -07002836
Thomas Chenault995b3372009-05-18 21:43:27 -07002837 if (abs_offset < block_limit && !st->frag_data) {
Herbert Xu95e3b242009-01-29 16:07:52 -08002838 *data = st->cur_skb->data + (abs_offset - st->stepped_offset);
Thomas Graf677e90e2005-06-23 20:59:51 -07002839 return block_limit - abs_offset;
2840 }
2841
2842 if (st->frag_idx == 0 && !st->frag_data)
2843 st->stepped_offset += skb_headlen(st->cur_skb);
2844
2845 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
2846 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
Eric Dumazet9e903e02011-10-18 21:00:24 +00002847 block_limit = skb_frag_size(frag) + st->stepped_offset;
Thomas Graf677e90e2005-06-23 20:59:51 -07002848
2849 if (abs_offset < block_limit) {
2850 if (!st->frag_data)
Eric Dumazet51c56b02012-04-05 11:35:15 +02002851 st->frag_data = kmap_atomic(skb_frag_page(frag));
Thomas Graf677e90e2005-06-23 20:59:51 -07002852
2853 *data = (u8 *) st->frag_data + frag->page_offset +
2854 (abs_offset - st->stepped_offset);
2855
2856 return block_limit - abs_offset;
2857 }
2858
2859 if (st->frag_data) {
Eric Dumazet51c56b02012-04-05 11:35:15 +02002860 kunmap_atomic(st->frag_data);
Thomas Graf677e90e2005-06-23 20:59:51 -07002861 st->frag_data = NULL;
2862 }
2863
2864 st->frag_idx++;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002865 st->stepped_offset += skb_frag_size(frag);
Thomas Graf677e90e2005-06-23 20:59:51 -07002866 }
2867
Olaf Kirch5b5a60d2007-06-23 23:11:52 -07002868 if (st->frag_data) {
Eric Dumazet51c56b02012-04-05 11:35:15 +02002869 kunmap_atomic(st->frag_data);
Olaf Kirch5b5a60d2007-06-23 23:11:52 -07002870 st->frag_data = NULL;
2871 }
2872
David S. Miller21dc3302010-08-23 00:13:46 -07002873 if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) {
Shyam Iyer71b33462009-01-29 16:12:42 -08002874 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
Thomas Graf677e90e2005-06-23 20:59:51 -07002875 st->frag_idx = 0;
2876 goto next_skb;
Shyam Iyer71b33462009-01-29 16:12:42 -08002877 } else if (st->cur_skb->next) {
2878 st->cur_skb = st->cur_skb->next;
Herbert Xu95e3b242009-01-29 16:07:52 -08002879 st->frag_idx = 0;
Thomas Graf677e90e2005-06-23 20:59:51 -07002880 goto next_skb;
2881 }
2882
2883 return 0;
2884}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002885EXPORT_SYMBOL(skb_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07002886
2887/**
2888 * skb_abort_seq_read - Abort a sequential read of skb data
2889 * @st: state variable
2890 *
2891 * Must be called if skb_seq_read() was not called until it
2892 * returned 0.
2893 */
2894void skb_abort_seq_read(struct skb_seq_state *st)
2895{
2896 if (st->frag_data)
Eric Dumazet51c56b02012-04-05 11:35:15 +02002897 kunmap_atomic(st->frag_data);
Thomas Graf677e90e2005-06-23 20:59:51 -07002898}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002899EXPORT_SYMBOL(skb_abort_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07002900
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002901#define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
2902
2903static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
2904 struct ts_config *conf,
2905 struct ts_state *state)
2906{
2907 return skb_seq_read(offset, text, TS_SKB_CB(state));
2908}
2909
2910static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
2911{
2912 skb_abort_seq_read(TS_SKB_CB(state));
2913}
2914
2915/**
2916 * skb_find_text - Find a text pattern in skb data
2917 * @skb: the buffer to look in
2918 * @from: search offset
2919 * @to: search limit
2920 * @config: textsearch configuration
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002921 *
2922 * Finds a pattern in the skb data according to the specified
2923 * textsearch configuration. Use textsearch_next() to retrieve
2924 * subsequent occurrences of the pattern. Returns the offset
2925 * to the first occurrence or UINT_MAX if no match was found.
2926 */
2927unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
Bojan Prtvar059a2442015-02-22 11:46:35 +01002928 unsigned int to, struct ts_config *config)
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002929{
Bojan Prtvar059a2442015-02-22 11:46:35 +01002930 struct ts_state state;
Phil Oesterf72b9482006-06-26 00:00:57 -07002931 unsigned int ret;
2932
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002933 config->get_next_block = skb_ts_get_next_block;
2934 config->finish = skb_ts_finish;
2935
Bojan Prtvar059a2442015-02-22 11:46:35 +01002936 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(&state));
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002937
Bojan Prtvar059a2442015-02-22 11:46:35 +01002938 ret = textsearch_find(config, &state);
Phil Oesterf72b9482006-06-26 00:00:57 -07002939 return (ret <= to - from ? ret : UINT_MAX);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002940}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002941EXPORT_SYMBOL(skb_find_text);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002942
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002943/**
Ben Hutchings2c530402012-07-10 10:55:09 +00002944 * skb_append_datato_frags - append the user data to a skb
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002945 * @sk: sock structure
Masanari Iidae793c0f2014-09-04 23:44:36 +09002946 * @skb: skb structure to be appended with user data.
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002947 * @getfrag: call back function to be used for getting the user data
2948 * @from: pointer to user message iov
2949 * @length: length of the iov message
2950 *
2951 * Description: This procedure append the user data in the fragment part
2952 * of the skb if any page alloc fails user this procedure returns -ENOMEM
2953 */
2954int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
Martin Waitzdab96302005-12-05 13:40:12 -08002955 int (*getfrag)(void *from, char *to, int offset,
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002956 int len, int odd, struct sk_buff *skb),
2957 void *from, int length)
2958{
Eric Dumazetb2111722012-12-28 06:06:37 +00002959 int frg_cnt = skb_shinfo(skb)->nr_frags;
2960 int copy;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002961 int offset = 0;
2962 int ret;
Eric Dumazetb2111722012-12-28 06:06:37 +00002963 struct page_frag *pfrag = &current->task_frag;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002964
2965 do {
2966 /* Return error if we don't have space for new frag */
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002967 if (frg_cnt >= MAX_SKB_FRAGS)
Eric Dumazetb2111722012-12-28 06:06:37 +00002968 return -EMSGSIZE;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002969
Eric Dumazetb2111722012-12-28 06:06:37 +00002970 if (!sk_page_frag_refill(sk, pfrag))
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002971 return -ENOMEM;
2972
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002973 /* copy the user data to page */
Eric Dumazetb2111722012-12-28 06:06:37 +00002974 copy = min_t(int, length, pfrag->size - pfrag->offset);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002975
Eric Dumazetb2111722012-12-28 06:06:37 +00002976 ret = getfrag(from, page_address(pfrag->page) + pfrag->offset,
2977 offset, copy, 0, skb);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002978 if (ret < 0)
2979 return -EFAULT;
2980
2981 /* copy was successful so update the size parameters */
Eric Dumazetb2111722012-12-28 06:06:37 +00002982 skb_fill_page_desc(skb, frg_cnt, pfrag->page, pfrag->offset,
2983 copy);
2984 frg_cnt++;
2985 pfrag->offset += copy;
2986 get_page(pfrag->page);
2987
2988 skb->truesize += copy;
2989 atomic_add(copy, &sk->sk_wmem_alloc);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002990 skb->len += copy;
2991 skb->data_len += copy;
2992 offset += copy;
2993 length -= copy;
2994
2995 } while (length > 0);
2996
2997 return 0;
2998}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002999EXPORT_SYMBOL(skb_append_datato_frags);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07003000
Hannes Frederic Sowabe12a1f2015-05-21 16:59:58 +02003001int skb_append_pagefrags(struct sk_buff *skb, struct page *page,
3002 int offset, size_t size)
3003{
3004 int i = skb_shinfo(skb)->nr_frags;
3005
3006 if (skb_can_coalesce(skb, i, page, offset)) {
3007 skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], size);
3008 } else if (i < MAX_SKB_FRAGS) {
3009 get_page(page);
3010 skb_fill_page_desc(skb, i, page, offset, size);
3011 } else {
3012 return -EMSGSIZE;
3013 }
3014
3015 return 0;
3016}
3017EXPORT_SYMBOL_GPL(skb_append_pagefrags);
3018
Herbert Xucbb042f2006-03-20 22:43:56 -08003019/**
3020 * skb_pull_rcsum - pull skb and update receive checksum
3021 * @skb: buffer to update
Herbert Xucbb042f2006-03-20 22:43:56 -08003022 * @len: length of data pulled
3023 *
3024 * This function performs an skb_pull on the packet and updates
Urs Thuermannfee54fa2008-02-12 22:03:25 -08003025 * the CHECKSUM_COMPLETE checksum. It should be used on
Patrick McHardy84fa7932006-08-29 16:44:56 -07003026 * receive path processing instead of skb_pull unless you know
3027 * that the checksum difference is zero (e.g., a valid IP header)
3028 * or you are setting ip_summed to CHECKSUM_NONE.
Herbert Xucbb042f2006-03-20 22:43:56 -08003029 */
3030unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
3031{
Pravin B Shelar31b33df2015-09-28 17:24:25 -07003032 unsigned char *data = skb->data;
3033
Herbert Xucbb042f2006-03-20 22:43:56 -08003034 BUG_ON(len > skb->len);
Pravin B Shelar31b33df2015-09-28 17:24:25 -07003035 __skb_pull(skb, len);
3036 skb_postpull_rcsum(skb, data, len);
3037 return skb->data;
Herbert Xucbb042f2006-03-20 22:43:56 -08003038}
Arnaldo Carvalho de Melof94691a2006-03-20 22:47:55 -08003039EXPORT_SYMBOL_GPL(skb_pull_rcsum);
3040
Herbert Xuf4c50d92006-06-22 03:02:40 -07003041/**
3042 * skb_segment - Perform protocol segmentation on skb.
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003043 * @head_skb: buffer to segment
Herbert Xu576a30e2006-06-27 13:22:38 -07003044 * @features: features for the output path (see dev->features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07003045 *
3046 * This function performs segmentation on the given skb. It returns
Ben Hutchings4c821d72008-04-13 21:52:48 -07003047 * a pointer to the first in a list of new skbs for the segments.
3048 * In case of error it returns ERR_PTR(err).
Herbert Xuf4c50d92006-06-22 03:02:40 -07003049 */
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003050struct sk_buff *skb_segment(struct sk_buff *head_skb,
3051 netdev_features_t features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07003052{
3053 struct sk_buff *segs = NULL;
3054 struct sk_buff *tail = NULL;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003055 struct sk_buff *list_skb = skb_shinfo(head_skb)->frag_list;
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003056 skb_frag_t *frag = skb_shinfo(head_skb)->frags;
3057 unsigned int mss = skb_shinfo(head_skb)->gso_size;
3058 unsigned int doffset = head_skb->data - skb_mac_header(head_skb);
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02003059 struct sk_buff *frag_skb = head_skb;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003060 unsigned int offset = doffset;
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003061 unsigned int tnl_hlen = skb_tnl_header_len(head_skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003062 unsigned int headroom;
3063 unsigned int len;
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003064 __be16 proto;
3065 bool csum;
Michał Mirosław04ed3e72011-01-24 15:32:47 -08003066 int sg = !!(features & NETIF_F_SG);
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003067 int nfrags = skb_shinfo(head_skb)->nr_frags;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003068 int err = -ENOMEM;
3069 int i = 0;
3070 int pos;
Vlad Yasevich53d64712014-03-27 17:26:18 -04003071 int dummy;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003072
Wei-Chun Chao5882a072014-06-08 23:48:54 -07003073 __skb_push(head_skb, doffset);
Vlad Yasevich53d64712014-03-27 17:26:18 -04003074 proto = skb_network_protocol(head_skb, &dummy);
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003075 if (unlikely(!proto))
3076 return ERR_PTR(-EINVAL);
3077
Alexander Duyckf245d072016-02-05 15:28:26 -08003078 csum = !!can_checksum_protocol(features, proto);
Tom Herbert7e2b10c2014-06-04 17:20:02 -07003079
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003080 headroom = skb_headroom(head_skb);
3081 pos = skb_headlen(head_skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003082
3083 do {
3084 struct sk_buff *nskb;
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003085 skb_frag_t *nskb_frag;
Herbert Xuc8884ed2006-10-29 15:59:41 -08003086 int hsize;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003087 int size;
3088
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003089 len = head_skb->len - offset;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003090 if (len > mss)
3091 len = mss;
3092
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003093 hsize = skb_headlen(head_skb) - offset;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003094 if (hsize < 0)
3095 hsize = 0;
Herbert Xuc8884ed2006-10-29 15:59:41 -08003096 if (hsize > len || !sg)
3097 hsize = len;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003098
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003099 if (!hsize && i >= nfrags && skb_headlen(list_skb) &&
3100 (skb_headlen(list_skb) == len || sg)) {
3101 BUG_ON(skb_headlen(list_skb) > len);
Herbert Xu89319d382008-12-15 23:26:06 -08003102
Herbert Xu9d8506c2013-11-21 11:10:04 -08003103 i = 0;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003104 nfrags = skb_shinfo(list_skb)->nr_frags;
3105 frag = skb_shinfo(list_skb)->frags;
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02003106 frag_skb = list_skb;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003107 pos += skb_headlen(list_skb);
Herbert Xu9d8506c2013-11-21 11:10:04 -08003108
3109 while (pos < offset + len) {
3110 BUG_ON(i >= nfrags);
3111
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02003112 size = skb_frag_size(frag);
Herbert Xu9d8506c2013-11-21 11:10:04 -08003113 if (pos + size > offset + len)
3114 break;
3115
3116 i++;
3117 pos += size;
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02003118 frag++;
Herbert Xu9d8506c2013-11-21 11:10:04 -08003119 }
3120
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003121 nskb = skb_clone(list_skb, GFP_ATOMIC);
3122 list_skb = list_skb->next;
Herbert Xu89319d382008-12-15 23:26:06 -08003123
3124 if (unlikely(!nskb))
3125 goto err;
3126
Herbert Xu9d8506c2013-11-21 11:10:04 -08003127 if (unlikely(pskb_trim(nskb, len))) {
3128 kfree_skb(nskb);
3129 goto err;
3130 }
3131
Alexander Duyckec47ea82012-05-04 14:26:56 +00003132 hsize = skb_end_offset(nskb);
Herbert Xu89319d382008-12-15 23:26:06 -08003133 if (skb_cow_head(nskb, doffset + headroom)) {
3134 kfree_skb(nskb);
3135 goto err;
3136 }
3137
Alexander Duyckec47ea82012-05-04 14:26:56 +00003138 nskb->truesize += skb_end_offset(nskb) - hsize;
Herbert Xu89319d382008-12-15 23:26:06 -08003139 skb_release_head_state(nskb);
3140 __skb_push(nskb, doffset);
3141 } else {
Mel Gormanc93bdd02012-07-31 16:44:19 -07003142 nskb = __alloc_skb(hsize + doffset + headroom,
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003143 GFP_ATOMIC, skb_alloc_rx_flag(head_skb),
Mel Gormanc93bdd02012-07-31 16:44:19 -07003144 NUMA_NO_NODE);
Herbert Xu89319d382008-12-15 23:26:06 -08003145
3146 if (unlikely(!nskb))
3147 goto err;
3148
3149 skb_reserve(nskb, headroom);
3150 __skb_put(nskb, doffset);
3151 }
Herbert Xuf4c50d92006-06-22 03:02:40 -07003152
3153 if (segs)
3154 tail->next = nskb;
3155 else
3156 segs = nskb;
3157 tail = nskb;
3158
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003159 __copy_skb_header(nskb, head_skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003160
Eric Dumazet030737b2013-10-19 11:42:54 -07003161 skb_headers_offset_update(nskb, skb_headroom(nskb) - headroom);
Vlad Yasevichfcdfe3a2014-07-31 10:33:06 -04003162 skb_reset_mac_len(nskb);
Pravin B Shelar68c33162013-02-14 14:02:41 +00003163
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003164 skb_copy_from_linear_data_offset(head_skb, -tnl_hlen,
Pravin B Shelar68c33162013-02-14 14:02:41 +00003165 nskb->data - tnl_hlen,
3166 doffset + tnl_hlen);
Herbert Xu89319d382008-12-15 23:26:06 -08003167
Herbert Xu9d8506c2013-11-21 11:10:04 -08003168 if (nskb->len == len + doffset)
Simon Horman1cdbcb72013-05-19 15:46:49 +00003169 goto perform_csum_check;
Herbert Xu89319d382008-12-15 23:26:06 -08003170
Alexander Duyck7fbeffe2016-02-05 15:27:43 -08003171 if (!sg) {
3172 if (!nskb->remcsum_offload)
3173 nskb->ip_summed = CHECKSUM_NONE;
Alexander Duyck76443452016-02-05 15:27:37 -08003174 SKB_GSO_CB(nskb)->csum =
3175 skb_copy_and_csum_bits(head_skb, offset,
3176 skb_put(nskb, len),
3177 len, 0);
Tom Herbert7e2b10c2014-06-04 17:20:02 -07003178 SKB_GSO_CB(nskb)->csum_start =
Alexander Duyck76443452016-02-05 15:27:37 -08003179 skb_headroom(nskb) + doffset;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003180 continue;
3181 }
3182
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003183 nskb_frag = skb_shinfo(nskb)->frags;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003184
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003185 skb_copy_from_linear_data_offset(head_skb, offset,
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03003186 skb_put(nskb, hsize), hsize);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003187
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003188 skb_shinfo(nskb)->tx_flags = skb_shinfo(head_skb)->tx_flags &
3189 SKBTX_SHARED_FRAG;
Eric Dumazetcef401d2013-01-25 20:34:37 +00003190
Herbert Xu9d8506c2013-11-21 11:10:04 -08003191 while (pos < offset + len) {
3192 if (i >= nfrags) {
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003193 BUG_ON(skb_headlen(list_skb));
Herbert Xu9d8506c2013-11-21 11:10:04 -08003194
3195 i = 0;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003196 nfrags = skb_shinfo(list_skb)->nr_frags;
3197 frag = skb_shinfo(list_skb)->frags;
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02003198 frag_skb = list_skb;
Herbert Xu9d8506c2013-11-21 11:10:04 -08003199
3200 BUG_ON(!nfrags);
3201
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003202 list_skb = list_skb->next;
Herbert Xu9d8506c2013-11-21 11:10:04 -08003203 }
3204
3205 if (unlikely(skb_shinfo(nskb)->nr_frags >=
3206 MAX_SKB_FRAGS)) {
3207 net_warn_ratelimited(
3208 "skb_segment: too many frags: %u %u\n",
3209 pos, mss);
3210 goto err;
3211 }
3212
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02003213 if (unlikely(skb_orphan_frags(frag_skb, GFP_ATOMIC)))
3214 goto err;
3215
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02003216 *nskb_frag = *frag;
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003217 __skb_frag_ref(nskb_frag);
3218 size = skb_frag_size(nskb_frag);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003219
3220 if (pos < offset) {
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003221 nskb_frag->page_offset += offset - pos;
3222 skb_frag_size_sub(nskb_frag, offset - pos);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003223 }
3224
Herbert Xu89319d382008-12-15 23:26:06 -08003225 skb_shinfo(nskb)->nr_frags++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003226
3227 if (pos + size <= offset + len) {
3228 i++;
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02003229 frag++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003230 pos += size;
3231 } else {
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003232 skb_frag_size_sub(nskb_frag, pos + size - (offset + len));
Herbert Xu89319d382008-12-15 23:26:06 -08003233 goto skip_fraglist;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003234 }
3235
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003236 nskb_frag++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003237 }
3238
Herbert Xu89319d382008-12-15 23:26:06 -08003239skip_fraglist:
Herbert Xuf4c50d92006-06-22 03:02:40 -07003240 nskb->data_len = len - hsize;
3241 nskb->len += nskb->data_len;
3242 nskb->truesize += nskb->data_len;
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003243
Simon Horman1cdbcb72013-05-19 15:46:49 +00003244perform_csum_check:
Alexander Duyck7fbeffe2016-02-05 15:27:43 -08003245 if (!csum) {
Alexander Duyckddff00d2016-02-05 15:27:55 -08003246 if (skb_has_shared_frag(nskb)) {
3247 err = __skb_linearize(nskb);
3248 if (err)
3249 goto err;
3250 }
Alexander Duyck7fbeffe2016-02-05 15:27:43 -08003251 if (!nskb->remcsum_offload)
3252 nskb->ip_summed = CHECKSUM_NONE;
Alexander Duyck76443452016-02-05 15:27:37 -08003253 SKB_GSO_CB(nskb)->csum =
3254 skb_checksum(nskb, doffset,
3255 nskb->len - doffset, 0);
Tom Herbert7e2b10c2014-06-04 17:20:02 -07003256 SKB_GSO_CB(nskb)->csum_start =
Alexander Duyck76443452016-02-05 15:27:37 -08003257 skb_headroom(nskb) + doffset;
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003258 }
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003259 } while ((offset += len) < head_skb->len);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003260
Eric Dumazetbec3cfd2014-10-03 20:59:19 -07003261 /* Some callers want to get the end of the list.
3262 * Put it in segs->prev to avoid walking the list.
3263 * (see validate_xmit_skb_list() for example)
3264 */
3265 segs->prev = tail;
Toshiaki Makita432c8562014-10-27 10:30:51 -07003266
3267 /* Following permits correct backpressure, for protocols
3268 * using skb_set_owner_w().
3269 * Idea is to tranfert ownership from head_skb to last segment.
3270 */
3271 if (head_skb->destructor == sock_wfree) {
3272 swap(tail->truesize, head_skb->truesize);
3273 swap(tail->destructor, head_skb->destructor);
3274 swap(tail->sk, head_skb->sk);
3275 }
Herbert Xuf4c50d92006-06-22 03:02:40 -07003276 return segs;
3277
3278err:
Eric Dumazet289dccb2013-12-20 14:29:08 -08003279 kfree_skb_list(segs);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003280 return ERR_PTR(err);
3281}
Herbert Xuf4c50d92006-06-22 03:02:40 -07003282EXPORT_SYMBOL_GPL(skb_segment);
3283
Herbert Xu71d93b32008-12-15 23:42:33 -08003284int skb_gro_receive(struct sk_buff **head, struct sk_buff *skb)
3285{
Eric Dumazet8a291112013-10-08 09:02:23 -07003286 struct skb_shared_info *pinfo, *skbinfo = skb_shinfo(skb);
Herbert Xu67147ba2009-05-26 18:50:22 +00003287 unsigned int offset = skb_gro_offset(skb);
3288 unsigned int headlen = skb_headlen(skb);
Eric Dumazet8a291112013-10-08 09:02:23 -07003289 unsigned int len = skb_gro_len(skb);
Eric Dumazet58025e42015-03-05 13:47:48 -08003290 struct sk_buff *lp, *p = *head;
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003291 unsigned int delta_truesize;
Herbert Xu71d93b32008-12-15 23:42:33 -08003292
Eric Dumazet8a291112013-10-08 09:02:23 -07003293 if (unlikely(p->len + len >= 65536))
Herbert Xu71d93b32008-12-15 23:42:33 -08003294 return -E2BIG;
3295
Eric Dumazet29e98242014-05-16 11:34:37 -07003296 lp = NAPI_GRO_CB(p)->last;
Eric Dumazet8a291112013-10-08 09:02:23 -07003297 pinfo = skb_shinfo(lp);
3298
3299 if (headlen <= offset) {
Herbert Xu42da6992009-05-26 18:50:19 +00003300 skb_frag_t *frag;
Herbert Xu66e92fc2009-05-26 18:50:32 +00003301 skb_frag_t *frag2;
Herbert Xu9aaa1562009-05-26 18:50:33 +00003302 int i = skbinfo->nr_frags;
3303 int nr_frags = pinfo->nr_frags + i;
Herbert Xu42da6992009-05-26 18:50:19 +00003304
Herbert Xu66e92fc2009-05-26 18:50:32 +00003305 if (nr_frags > MAX_SKB_FRAGS)
Eric Dumazet8a291112013-10-08 09:02:23 -07003306 goto merge;
Herbert Xu81705ad2009-01-29 14:19:51 +00003307
Eric Dumazet8a291112013-10-08 09:02:23 -07003308 offset -= headlen;
Herbert Xu9aaa1562009-05-26 18:50:33 +00003309 pinfo->nr_frags = nr_frags;
3310 skbinfo->nr_frags = 0;
Herbert Xuf5572062009-01-14 20:40:03 -08003311
Herbert Xu9aaa1562009-05-26 18:50:33 +00003312 frag = pinfo->frags + nr_frags;
3313 frag2 = skbinfo->frags + i;
Herbert Xu66e92fc2009-05-26 18:50:32 +00003314 do {
3315 *--frag = *--frag2;
3316 } while (--i);
3317
3318 frag->page_offset += offset;
Eric Dumazet9e903e02011-10-18 21:00:24 +00003319 skb_frag_size_sub(frag, offset);
Herbert Xu66e92fc2009-05-26 18:50:32 +00003320
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003321 /* all fragments truesize : remove (head size + sk_buff) */
Alexander Duyckec47ea82012-05-04 14:26:56 +00003322 delta_truesize = skb->truesize -
3323 SKB_TRUESIZE(skb_end_offset(skb));
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003324
Herbert Xuf5572062009-01-14 20:40:03 -08003325 skb->truesize -= skb->data_len;
3326 skb->len -= skb->data_len;
3327 skb->data_len = 0;
3328
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003329 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE;
Herbert Xu5d38a072009-01-04 16:13:40 -08003330 goto done;
Eric Dumazetd7e88832012-04-30 08:10:34 +00003331 } else if (skb->head_frag) {
3332 int nr_frags = pinfo->nr_frags;
3333 skb_frag_t *frag = pinfo->frags + nr_frags;
3334 struct page *page = virt_to_head_page(skb->head);
3335 unsigned int first_size = headlen - offset;
3336 unsigned int first_offset;
3337
3338 if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS)
Eric Dumazet8a291112013-10-08 09:02:23 -07003339 goto merge;
Eric Dumazetd7e88832012-04-30 08:10:34 +00003340
3341 first_offset = skb->data -
3342 (unsigned char *)page_address(page) +
3343 offset;
3344
3345 pinfo->nr_frags = nr_frags + 1 + skbinfo->nr_frags;
3346
3347 frag->page.p = page;
3348 frag->page_offset = first_offset;
3349 skb_frag_size_set(frag, first_size);
3350
3351 memcpy(frag + 1, skbinfo->frags, sizeof(*frag) * skbinfo->nr_frags);
3352 /* We dont need to clear skbinfo->nr_frags here */
3353
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003354 delta_truesize = skb->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
Eric Dumazetd7e88832012-04-30 08:10:34 +00003355 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD;
3356 goto done;
Eric Dumazet8a291112013-10-08 09:02:23 -07003357 }
Herbert Xu71d93b32008-12-15 23:42:33 -08003358
3359merge:
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003360 delta_truesize = skb->truesize;
Herbert Xu67147ba2009-05-26 18:50:22 +00003361 if (offset > headlen) {
Michal Schmidtd1dc7ab2011-01-24 12:08:48 +00003362 unsigned int eat = offset - headlen;
3363
3364 skbinfo->frags[0].page_offset += eat;
Eric Dumazet9e903e02011-10-18 21:00:24 +00003365 skb_frag_size_sub(&skbinfo->frags[0], eat);
Michal Schmidtd1dc7ab2011-01-24 12:08:48 +00003366 skb->data_len -= eat;
3367 skb->len -= eat;
Herbert Xu67147ba2009-05-26 18:50:22 +00003368 offset = headlen;
Herbert Xu56035022009-02-05 21:26:52 -08003369 }
3370
Herbert Xu67147ba2009-05-26 18:50:22 +00003371 __skb_pull(skb, offset);
Herbert Xu56035022009-02-05 21:26:52 -08003372
Eric Dumazet29e98242014-05-16 11:34:37 -07003373 if (NAPI_GRO_CB(p)->last == p)
Eric Dumazet8a291112013-10-08 09:02:23 -07003374 skb_shinfo(p)->frag_list = skb;
3375 else
3376 NAPI_GRO_CB(p)->last->next = skb;
Eric Dumazetc3c7c252012-12-06 13:54:59 +00003377 NAPI_GRO_CB(p)->last = skb;
Eric Dumazetf4a775d2014-09-22 16:29:32 -07003378 __skb_header_release(skb);
Eric Dumazet8a291112013-10-08 09:02:23 -07003379 lp = p;
Herbert Xu71d93b32008-12-15 23:42:33 -08003380
Herbert Xu5d38a072009-01-04 16:13:40 -08003381done:
3382 NAPI_GRO_CB(p)->count++;
Herbert Xu37fe4732009-01-17 19:48:13 +00003383 p->data_len += len;
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003384 p->truesize += delta_truesize;
Herbert Xu37fe4732009-01-17 19:48:13 +00003385 p->len += len;
Eric Dumazet8a291112013-10-08 09:02:23 -07003386 if (lp != p) {
3387 lp->data_len += len;
3388 lp->truesize += delta_truesize;
3389 lp->len += len;
3390 }
Herbert Xu71d93b32008-12-15 23:42:33 -08003391 NAPI_GRO_CB(skb)->same_flow = 1;
3392 return 0;
3393}
Herbert Xu71d93b32008-12-15 23:42:33 -08003394
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395void __init skb_init(void)
3396{
3397 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
3398 sizeof(struct sk_buff),
3399 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07003400 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09003401 NULL);
David S. Millerd179cd12005-08-17 14:57:30 -07003402 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
Eric Dumazetd0bf4a92014-09-29 13:29:15 -07003403 sizeof(struct sk_buff_fclones),
David S. Millerd179cd12005-08-17 14:57:30 -07003404 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07003405 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09003406 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407}
3408
David Howells716ea3a2007-04-02 20:19:53 -07003409/**
3410 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
3411 * @skb: Socket buffer containing the buffers to be mapped
3412 * @sg: The scatter-gather list to map into
3413 * @offset: The offset into the buffer's contents to start mapping
3414 * @len: Length of buffer space to be mapped
3415 *
3416 * Fill the specified scatter-gather list with mappings/pointers into a
3417 * region of the buffer space attached to a socket buffer.
3418 */
David S. Miller51c739d2007-10-30 21:29:29 -07003419static int
3420__skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
David Howells716ea3a2007-04-02 20:19:53 -07003421{
David S. Miller1a028e52007-04-27 15:21:23 -07003422 int start = skb_headlen(skb);
3423 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07003424 struct sk_buff *frag_iter;
David Howells716ea3a2007-04-02 20:19:53 -07003425 int elt = 0;
3426
3427 if (copy > 0) {
3428 if (copy > len)
3429 copy = len;
Jens Axboe642f1492007-10-24 11:20:47 +02003430 sg_set_buf(sg, skb->data + offset, copy);
David Howells716ea3a2007-04-02 20:19:53 -07003431 elt++;
3432 if ((len -= copy) == 0)
3433 return elt;
3434 offset += copy;
3435 }
3436
3437 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07003438 int end;
David Howells716ea3a2007-04-02 20:19:53 -07003439
Ilpo Järvinen547b7922008-07-25 21:43:18 -07003440 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07003441
Eric Dumazet9e903e02011-10-18 21:00:24 +00003442 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
David Howells716ea3a2007-04-02 20:19:53 -07003443 if ((copy = end - offset) > 0) {
3444 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
3445
3446 if (copy > len)
3447 copy = len;
Ian Campbellea2ab692011-08-22 23:44:58 +00003448 sg_set_page(&sg[elt], skb_frag_page(frag), copy,
Jens Axboe642f1492007-10-24 11:20:47 +02003449 frag->page_offset+offset-start);
David Howells716ea3a2007-04-02 20:19:53 -07003450 elt++;
3451 if (!(len -= copy))
3452 return elt;
3453 offset += copy;
3454 }
David S. Miller1a028e52007-04-27 15:21:23 -07003455 start = end;
David Howells716ea3a2007-04-02 20:19:53 -07003456 }
3457
David S. Millerfbb398a2009-06-09 00:18:59 -07003458 skb_walk_frags(skb, frag_iter) {
3459 int end;
David Howells716ea3a2007-04-02 20:19:53 -07003460
David S. Millerfbb398a2009-06-09 00:18:59 -07003461 WARN_ON(start > offset + len);
David Howells716ea3a2007-04-02 20:19:53 -07003462
David S. Millerfbb398a2009-06-09 00:18:59 -07003463 end = start + frag_iter->len;
3464 if ((copy = end - offset) > 0) {
3465 if (copy > len)
3466 copy = len;
3467 elt += __skb_to_sgvec(frag_iter, sg+elt, offset - start,
3468 copy);
3469 if ((len -= copy) == 0)
3470 return elt;
3471 offset += copy;
David Howells716ea3a2007-04-02 20:19:53 -07003472 }
David S. Millerfbb398a2009-06-09 00:18:59 -07003473 start = end;
David Howells716ea3a2007-04-02 20:19:53 -07003474 }
3475 BUG_ON(len);
3476 return elt;
3477}
3478
Fan Du25a91d82014-01-18 09:54:23 +08003479/* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given
3480 * sglist without mark the sg which contain last skb data as the end.
3481 * So the caller can mannipulate sg list as will when padding new data after
3482 * the first call without calling sg_unmark_end to expend sg list.
3483 *
3484 * Scenario to use skb_to_sgvec_nomark:
3485 * 1. sg_init_table
3486 * 2. skb_to_sgvec_nomark(payload1)
3487 * 3. skb_to_sgvec_nomark(payload2)
3488 *
3489 * This is equivalent to:
3490 * 1. sg_init_table
3491 * 2. skb_to_sgvec(payload1)
3492 * 3. sg_unmark_end
3493 * 4. skb_to_sgvec(payload2)
3494 *
3495 * When mapping mutilple payload conditionally, skb_to_sgvec_nomark
3496 * is more preferable.
3497 */
3498int skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg,
3499 int offset, int len)
3500{
3501 return __skb_to_sgvec(skb, sg, offset, len);
3502}
3503EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark);
3504
David S. Miller51c739d2007-10-30 21:29:29 -07003505int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
3506{
3507 int nsg = __skb_to_sgvec(skb, sg, offset, len);
3508
Jens Axboec46f2332007-10-31 12:06:37 +01003509 sg_mark_end(&sg[nsg - 1]);
David S. Miller51c739d2007-10-30 21:29:29 -07003510
3511 return nsg;
3512}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003513EXPORT_SYMBOL_GPL(skb_to_sgvec);
David S. Miller51c739d2007-10-30 21:29:29 -07003514
David Howells716ea3a2007-04-02 20:19:53 -07003515/**
3516 * skb_cow_data - Check that a socket buffer's data buffers are writable
3517 * @skb: The socket buffer to check.
3518 * @tailbits: Amount of trailing space to be added
3519 * @trailer: Returned pointer to the skb where the @tailbits space begins
3520 *
3521 * Make sure that the data buffers attached to a socket buffer are
3522 * writable. If they are not, private copies are made of the data buffers
3523 * and the socket buffer is set to use these instead.
3524 *
3525 * If @tailbits is given, make sure that there is space to write @tailbits
3526 * bytes of data beyond current end of socket buffer. @trailer will be
3527 * set to point to the skb in which this space begins.
3528 *
3529 * The number of scatterlist elements required to completely map the
3530 * COW'd and extended socket buffer will be returned.
3531 */
3532int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
3533{
3534 int copyflag;
3535 int elt;
3536 struct sk_buff *skb1, **skb_p;
3537
3538 /* If skb is cloned or its head is paged, reallocate
3539 * head pulling out all the pages (pages are considered not writable
3540 * at the moment even if they are anonymous).
3541 */
3542 if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
3543 __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
3544 return -ENOMEM;
3545
3546 /* Easy case. Most of packets will go this way. */
David S. Miller21dc3302010-08-23 00:13:46 -07003547 if (!skb_has_frag_list(skb)) {
David Howells716ea3a2007-04-02 20:19:53 -07003548 /* A little of trouble, not enough of space for trailer.
3549 * This should not happen, when stack is tuned to generate
3550 * good frames. OK, on miss we reallocate and reserve even more
3551 * space, 128 bytes is fair. */
3552
3553 if (skb_tailroom(skb) < tailbits &&
3554 pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
3555 return -ENOMEM;
3556
3557 /* Voila! */
3558 *trailer = skb;
3559 return 1;
3560 }
3561
3562 /* Misery. We are in troubles, going to mincer fragments... */
3563
3564 elt = 1;
3565 skb_p = &skb_shinfo(skb)->frag_list;
3566 copyflag = 0;
3567
3568 while ((skb1 = *skb_p) != NULL) {
3569 int ntail = 0;
3570
3571 /* The fragment is partially pulled by someone,
3572 * this can happen on input. Copy it and everything
3573 * after it. */
3574
3575 if (skb_shared(skb1))
3576 copyflag = 1;
3577
3578 /* If the skb is the last, worry about trailer. */
3579
3580 if (skb1->next == NULL && tailbits) {
3581 if (skb_shinfo(skb1)->nr_frags ||
David S. Miller21dc3302010-08-23 00:13:46 -07003582 skb_has_frag_list(skb1) ||
David Howells716ea3a2007-04-02 20:19:53 -07003583 skb_tailroom(skb1) < tailbits)
3584 ntail = tailbits + 128;
3585 }
3586
3587 if (copyflag ||
3588 skb_cloned(skb1) ||
3589 ntail ||
3590 skb_shinfo(skb1)->nr_frags ||
David S. Miller21dc3302010-08-23 00:13:46 -07003591 skb_has_frag_list(skb1)) {
David Howells716ea3a2007-04-02 20:19:53 -07003592 struct sk_buff *skb2;
3593
3594 /* Fuck, we are miserable poor guys... */
3595 if (ntail == 0)
3596 skb2 = skb_copy(skb1, GFP_ATOMIC);
3597 else
3598 skb2 = skb_copy_expand(skb1,
3599 skb_headroom(skb1),
3600 ntail,
3601 GFP_ATOMIC);
3602 if (unlikely(skb2 == NULL))
3603 return -ENOMEM;
3604
3605 if (skb1->sk)
3606 skb_set_owner_w(skb2, skb1->sk);
3607
3608 /* Looking around. Are we still alive?
3609 * OK, link new skb, drop old one */
3610
3611 skb2->next = skb1->next;
3612 *skb_p = skb2;
3613 kfree_skb(skb1);
3614 skb1 = skb2;
3615 }
3616 elt++;
3617 *trailer = skb1;
3618 skb_p = &skb1->next;
3619 }
3620
3621 return elt;
3622}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003623EXPORT_SYMBOL_GPL(skb_cow_data);
David Howells716ea3a2007-04-02 20:19:53 -07003624
Eric Dumazetb1faf562010-05-31 23:44:05 -07003625static void sock_rmem_free(struct sk_buff *skb)
3626{
3627 struct sock *sk = skb->sk;
3628
3629 atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
3630}
3631
3632/*
3633 * Note: We dont mem charge error packets (no sk_forward_alloc changes)
3634 */
3635int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb)
3636{
3637 if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
Eric Dumazet95c96172012-04-15 05:58:06 +00003638 (unsigned int)sk->sk_rcvbuf)
Eric Dumazetb1faf562010-05-31 23:44:05 -07003639 return -ENOMEM;
3640
3641 skb_orphan(skb);
3642 skb->sk = sk;
3643 skb->destructor = sock_rmem_free;
3644 atomic_add(skb->truesize, &sk->sk_rmem_alloc);
3645
Eric Dumazetabb57ea2011-05-18 02:21:31 -04003646 /* before exiting rcu section, make sure dst is refcounted */
3647 skb_dst_force(skb);
3648
Eric Dumazetb1faf562010-05-31 23:44:05 -07003649 skb_queue_tail(&sk->sk_error_queue, skb);
3650 if (!sock_flag(sk, SOCK_DEAD))
David S. Miller676d2362014-04-11 16:15:36 -04003651 sk->sk_data_ready(sk);
Eric Dumazetb1faf562010-05-31 23:44:05 -07003652 return 0;
3653}
3654EXPORT_SYMBOL(sock_queue_err_skb);
3655
Willem de Bruijn364a9e92014-08-31 21:30:27 -04003656struct sk_buff *sock_dequeue_err_skb(struct sock *sk)
3657{
3658 struct sk_buff_head *q = &sk->sk_error_queue;
3659 struct sk_buff *skb, *skb_next;
Eric Dumazet997d5c32015-02-18 05:47:55 -08003660 unsigned long flags;
Willem de Bruijn364a9e92014-08-31 21:30:27 -04003661 int err = 0;
3662
Eric Dumazet997d5c32015-02-18 05:47:55 -08003663 spin_lock_irqsave(&q->lock, flags);
Willem de Bruijn364a9e92014-08-31 21:30:27 -04003664 skb = __skb_dequeue(q);
3665 if (skb && (skb_next = skb_peek(q)))
3666 err = SKB_EXT_ERR(skb_next)->ee.ee_errno;
Eric Dumazet997d5c32015-02-18 05:47:55 -08003667 spin_unlock_irqrestore(&q->lock, flags);
Willem de Bruijn364a9e92014-08-31 21:30:27 -04003668
3669 sk->sk_err = err;
3670 if (err)
3671 sk->sk_error_report(sk);
3672
3673 return skb;
3674}
3675EXPORT_SYMBOL(sock_dequeue_err_skb);
3676
Alexander Duyckcab41c42014-09-10 18:05:26 -04003677/**
3678 * skb_clone_sk - create clone of skb, and take reference to socket
3679 * @skb: the skb to clone
3680 *
3681 * This function creates a clone of a buffer that holds a reference on
3682 * sk_refcnt. Buffers created via this function are meant to be
3683 * returned using sock_queue_err_skb, or free via kfree_skb.
3684 *
3685 * When passing buffers allocated with this function to sock_queue_err_skb
3686 * it is necessary to wrap the call with sock_hold/sock_put in order to
3687 * prevent the socket from being released prior to being enqueued on
3688 * the sk_error_queue.
3689 */
Alexander Duyck62bccb82014-09-04 13:31:35 -04003690struct sk_buff *skb_clone_sk(struct sk_buff *skb)
3691{
3692 struct sock *sk = skb->sk;
3693 struct sk_buff *clone;
3694
3695 if (!sk || !atomic_inc_not_zero(&sk->sk_refcnt))
3696 return NULL;
3697
3698 clone = skb_clone(skb, GFP_ATOMIC);
3699 if (!clone) {
3700 sock_put(sk);
3701 return NULL;
3702 }
3703
3704 clone->sk = sk;
3705 clone->destructor = sock_efree;
3706
3707 return clone;
3708}
3709EXPORT_SYMBOL(skb_clone_sk);
3710
Alexander Duyck37846ef2014-09-04 13:31:10 -04003711static void __skb_complete_tx_timestamp(struct sk_buff *skb,
3712 struct sock *sk,
3713 int tstype)
Patrick Ohlyac45f602009-02-12 05:03:37 +00003714{
Patrick Ohlyac45f602009-02-12 05:03:37 +00003715 struct sock_exterr_skb *serr;
Patrick Ohlyac45f602009-02-12 05:03:37 +00003716 int err;
3717
Patrick Ohlyac45f602009-02-12 05:03:37 +00003718 serr = SKB_EXT_ERR(skb);
3719 memset(serr, 0, sizeof(*serr));
3720 serr->ee.ee_errno = ENOMSG;
3721 serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
Willem de Bruijne7fd2882014-08-04 22:11:48 -04003722 serr->ee.ee_info = tstype;
Willem de Bruijn4ed2d762014-08-04 22:11:49 -04003723 if (sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) {
Willem de Bruijn09c2d252014-08-04 22:11:47 -04003724 serr->ee.ee_data = skb_shinfo(skb)->tskey;
WANG Congac5cc972015-12-16 23:39:04 -08003725 if (sk->sk_protocol == IPPROTO_TCP &&
3726 sk->sk_type == SOCK_STREAM)
Willem de Bruijn4ed2d762014-08-04 22:11:49 -04003727 serr->ee.ee_data -= sk->sk_tskey;
3728 }
Eric Dumazet29030372010-05-29 00:20:48 -07003729
Patrick Ohlyac45f602009-02-12 05:03:37 +00003730 err = sock_queue_err_skb(sk, skb);
Eric Dumazet29030372010-05-29 00:20:48 -07003731
Patrick Ohlyac45f602009-02-12 05:03:37 +00003732 if (err)
3733 kfree_skb(skb);
3734}
Alexander Duyck37846ef2014-09-04 13:31:10 -04003735
Willem de Bruijnb245be12015-01-30 13:29:32 -05003736static bool skb_may_tx_timestamp(struct sock *sk, bool tsonly)
3737{
3738 bool ret;
3739
3740 if (likely(sysctl_tstamp_allow_data || tsonly))
3741 return true;
3742
3743 read_lock_bh(&sk->sk_callback_lock);
3744 ret = sk->sk_socket && sk->sk_socket->file &&
3745 file_ns_capable(sk->sk_socket->file, &init_user_ns, CAP_NET_RAW);
3746 read_unlock_bh(&sk->sk_callback_lock);
3747 return ret;
3748}
3749
Alexander Duyck37846ef2014-09-04 13:31:10 -04003750void skb_complete_tx_timestamp(struct sk_buff *skb,
3751 struct skb_shared_hwtstamps *hwtstamps)
3752{
3753 struct sock *sk = skb->sk;
3754
Willem de Bruijnb245be12015-01-30 13:29:32 -05003755 if (!skb_may_tx_timestamp(sk, false))
3756 return;
3757
Alexander Duyck62bccb82014-09-04 13:31:35 -04003758 /* take a reference to prevent skb_orphan() from freeing the socket */
3759 sock_hold(sk);
Alexander Duyck37846ef2014-09-04 13:31:10 -04003760
Alexander Duyck62bccb82014-09-04 13:31:35 -04003761 *skb_hwtstamps(skb) = *hwtstamps;
3762 __skb_complete_tx_timestamp(skb, sk, SCM_TSTAMP_SND);
Alexander Duyck37846ef2014-09-04 13:31:10 -04003763
3764 sock_put(sk);
3765}
3766EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp);
3767
3768void __skb_tstamp_tx(struct sk_buff *orig_skb,
3769 struct skb_shared_hwtstamps *hwtstamps,
3770 struct sock *sk, int tstype)
3771{
3772 struct sk_buff *skb;
Willem de Bruijn3a8dd972015-03-11 15:43:55 -04003773 bool tsonly;
Alexander Duyck37846ef2014-09-04 13:31:10 -04003774
Willem de Bruijn3a8dd972015-03-11 15:43:55 -04003775 if (!sk)
3776 return;
3777
3778 tsonly = sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TSONLY;
3779 if (!skb_may_tx_timestamp(sk, tsonly))
Alexander Duyck37846ef2014-09-04 13:31:10 -04003780 return;
3781
Willem de Bruijn49ca0d82015-01-30 13:29:31 -05003782 if (tsonly)
3783 skb = alloc_skb(0, GFP_ATOMIC);
Alexander Duyck37846ef2014-09-04 13:31:10 -04003784 else
Willem de Bruijn49ca0d82015-01-30 13:29:31 -05003785 skb = skb_clone(orig_skb, GFP_ATOMIC);
Alexander Duyck37846ef2014-09-04 13:31:10 -04003786 if (!skb)
3787 return;
3788
Willem de Bruijn49ca0d82015-01-30 13:29:31 -05003789 if (tsonly) {
3790 skb_shinfo(skb)->tx_flags = skb_shinfo(orig_skb)->tx_flags;
3791 skb_shinfo(skb)->tskey = skb_shinfo(orig_skb)->tskey;
3792 }
3793
3794 if (hwtstamps)
3795 *skb_hwtstamps(skb) = *hwtstamps;
3796 else
3797 skb->tstamp = ktime_get_real();
3798
Alexander Duyck37846ef2014-09-04 13:31:10 -04003799 __skb_complete_tx_timestamp(skb, sk, tstype);
3800}
Willem de Bruijne7fd2882014-08-04 22:11:48 -04003801EXPORT_SYMBOL_GPL(__skb_tstamp_tx);
3802
3803void skb_tstamp_tx(struct sk_buff *orig_skb,
3804 struct skb_shared_hwtstamps *hwtstamps)
3805{
3806 return __skb_tstamp_tx(orig_skb, hwtstamps, orig_skb->sk,
3807 SCM_TSTAMP_SND);
3808}
Patrick Ohlyac45f602009-02-12 05:03:37 +00003809EXPORT_SYMBOL_GPL(skb_tstamp_tx);
3810
Johannes Berg6e3e9392011-11-09 10:15:42 +01003811void skb_complete_wifi_ack(struct sk_buff *skb, bool acked)
3812{
3813 struct sock *sk = skb->sk;
3814 struct sock_exterr_skb *serr;
3815 int err;
3816
3817 skb->wifi_acked_valid = 1;
3818 skb->wifi_acked = acked;
3819
3820 serr = SKB_EXT_ERR(skb);
3821 memset(serr, 0, sizeof(*serr));
3822 serr->ee.ee_errno = ENOMSG;
3823 serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS;
3824
Alexander Duyckbf7fa552014-09-10 18:05:42 -04003825 /* take a reference to prevent skb_orphan() from freeing the socket */
3826 sock_hold(sk);
3827
Johannes Berg6e3e9392011-11-09 10:15:42 +01003828 err = sock_queue_err_skb(sk, skb);
3829 if (err)
3830 kfree_skb(skb);
Alexander Duyckbf7fa552014-09-10 18:05:42 -04003831
3832 sock_put(sk);
Johannes Berg6e3e9392011-11-09 10:15:42 +01003833}
3834EXPORT_SYMBOL_GPL(skb_complete_wifi_ack);
3835
Rusty Russellf35d9d82008-02-04 23:49:54 -05003836/**
3837 * skb_partial_csum_set - set up and verify partial csum values for packet
3838 * @skb: the skb to set
3839 * @start: the number of bytes after skb->data to start checksumming.
3840 * @off: the offset from start to place the checksum.
3841 *
3842 * For untrusted partially-checksummed packets, we need to make sure the values
3843 * for skb->csum_start and skb->csum_offset are valid so we don't oops.
3844 *
3845 * This function checks and sets those values and skb->ip_summed: if this
3846 * returns false you should drop the packet.
3847 */
3848bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
3849{
Herbert Xu5ff8dda2009-06-04 01:22:01 +00003850 if (unlikely(start > skb_headlen(skb)) ||
3851 unlikely((int)start + off > skb_headlen(skb) - 2)) {
Joe Perchese87cc472012-05-13 21:56:26 +00003852 net_warn_ratelimited("bad partial csum: csum=%u/%u len=%u\n",
3853 start, off, skb_headlen(skb));
Rusty Russellf35d9d82008-02-04 23:49:54 -05003854 return false;
3855 }
3856 skb->ip_summed = CHECKSUM_PARTIAL;
3857 skb->csum_start = skb_headroom(skb) + start;
3858 skb->csum_offset = off;
Jason Wange5d5dec2013-03-26 23:11:20 +00003859 skb_set_transport_header(skb, start);
Rusty Russellf35d9d82008-02-04 23:49:54 -05003860 return true;
3861}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003862EXPORT_SYMBOL_GPL(skb_partial_csum_set);
Rusty Russellf35d9d82008-02-04 23:49:54 -05003863
Paul Durranted1f50c2014-01-09 10:02:46 +00003864static int skb_maybe_pull_tail(struct sk_buff *skb, unsigned int len,
3865 unsigned int max)
3866{
3867 if (skb_headlen(skb) >= len)
3868 return 0;
3869
3870 /* If we need to pullup then pullup to the max, so we
3871 * won't need to do it again.
3872 */
3873 if (max > skb->len)
3874 max = skb->len;
3875
3876 if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL)
3877 return -ENOMEM;
3878
3879 if (skb_headlen(skb) < len)
3880 return -EPROTO;
3881
3882 return 0;
3883}
3884
Jan Beulichf9708b42014-03-11 13:56:05 +00003885#define MAX_TCP_HDR_LEN (15 * 4)
3886
3887static __sum16 *skb_checksum_setup_ip(struct sk_buff *skb,
3888 typeof(IPPROTO_IP) proto,
3889 unsigned int off)
3890{
3891 switch (proto) {
3892 int err;
3893
3894 case IPPROTO_TCP:
3895 err = skb_maybe_pull_tail(skb, off + sizeof(struct tcphdr),
3896 off + MAX_TCP_HDR_LEN);
3897 if (!err && !skb_partial_csum_set(skb, off,
3898 offsetof(struct tcphdr,
3899 check)))
3900 err = -EPROTO;
3901 return err ? ERR_PTR(err) : &tcp_hdr(skb)->check;
3902
3903 case IPPROTO_UDP:
3904 err = skb_maybe_pull_tail(skb, off + sizeof(struct udphdr),
3905 off + sizeof(struct udphdr));
3906 if (!err && !skb_partial_csum_set(skb, off,
3907 offsetof(struct udphdr,
3908 check)))
3909 err = -EPROTO;
3910 return err ? ERR_PTR(err) : &udp_hdr(skb)->check;
3911 }
3912
3913 return ERR_PTR(-EPROTO);
3914}
3915
Paul Durranted1f50c2014-01-09 10:02:46 +00003916/* This value should be large enough to cover a tagged ethernet header plus
3917 * maximally sized IP and TCP or UDP headers.
3918 */
3919#define MAX_IP_HDR_LEN 128
3920
Jan Beulichf9708b42014-03-11 13:56:05 +00003921static int skb_checksum_setup_ipv4(struct sk_buff *skb, bool recalculate)
Paul Durranted1f50c2014-01-09 10:02:46 +00003922{
3923 unsigned int off;
3924 bool fragment;
Jan Beulichf9708b42014-03-11 13:56:05 +00003925 __sum16 *csum;
Paul Durranted1f50c2014-01-09 10:02:46 +00003926 int err;
3927
3928 fragment = false;
3929
3930 err = skb_maybe_pull_tail(skb,
3931 sizeof(struct iphdr),
3932 MAX_IP_HDR_LEN);
3933 if (err < 0)
3934 goto out;
3935
3936 if (ip_hdr(skb)->frag_off & htons(IP_OFFSET | IP_MF))
3937 fragment = true;
3938
3939 off = ip_hdrlen(skb);
3940
3941 err = -EPROTO;
3942
3943 if (fragment)
3944 goto out;
3945
Jan Beulichf9708b42014-03-11 13:56:05 +00003946 csum = skb_checksum_setup_ip(skb, ip_hdr(skb)->protocol, off);
3947 if (IS_ERR(csum))
3948 return PTR_ERR(csum);
Paul Durranted1f50c2014-01-09 10:02:46 +00003949
Jan Beulichf9708b42014-03-11 13:56:05 +00003950 if (recalculate)
3951 *csum = ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
3952 ip_hdr(skb)->daddr,
3953 skb->len - off,
3954 ip_hdr(skb)->protocol, 0);
Paul Durranted1f50c2014-01-09 10:02:46 +00003955 err = 0;
3956
3957out:
3958 return err;
3959}
3960
3961/* This value should be large enough to cover a tagged ethernet header plus
3962 * an IPv6 header, all options, and a maximal TCP or UDP header.
3963 */
3964#define MAX_IPV6_HDR_LEN 256
3965
3966#define OPT_HDR(type, skb, off) \
3967 (type *)(skb_network_header(skb) + (off))
3968
3969static int skb_checksum_setup_ipv6(struct sk_buff *skb, bool recalculate)
3970{
3971 int err;
3972 u8 nexthdr;
3973 unsigned int off;
3974 unsigned int len;
3975 bool fragment;
3976 bool done;
Jan Beulichf9708b42014-03-11 13:56:05 +00003977 __sum16 *csum;
Paul Durranted1f50c2014-01-09 10:02:46 +00003978
3979 fragment = false;
3980 done = false;
3981
3982 off = sizeof(struct ipv6hdr);
3983
3984 err = skb_maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN);
3985 if (err < 0)
3986 goto out;
3987
3988 nexthdr = ipv6_hdr(skb)->nexthdr;
3989
3990 len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len);
3991 while (off <= len && !done) {
3992 switch (nexthdr) {
3993 case IPPROTO_DSTOPTS:
3994 case IPPROTO_HOPOPTS:
3995 case IPPROTO_ROUTING: {
3996 struct ipv6_opt_hdr *hp;
3997
3998 err = skb_maybe_pull_tail(skb,
3999 off +
4000 sizeof(struct ipv6_opt_hdr),
4001 MAX_IPV6_HDR_LEN);
4002 if (err < 0)
4003 goto out;
4004
4005 hp = OPT_HDR(struct ipv6_opt_hdr, skb, off);
4006 nexthdr = hp->nexthdr;
4007 off += ipv6_optlen(hp);
4008 break;
4009 }
4010 case IPPROTO_AH: {
4011 struct ip_auth_hdr *hp;
4012
4013 err = skb_maybe_pull_tail(skb,
4014 off +
4015 sizeof(struct ip_auth_hdr),
4016 MAX_IPV6_HDR_LEN);
4017 if (err < 0)
4018 goto out;
4019
4020 hp = OPT_HDR(struct ip_auth_hdr, skb, off);
4021 nexthdr = hp->nexthdr;
4022 off += ipv6_authlen(hp);
4023 break;
4024 }
4025 case IPPROTO_FRAGMENT: {
4026 struct frag_hdr *hp;
4027
4028 err = skb_maybe_pull_tail(skb,
4029 off +
4030 sizeof(struct frag_hdr),
4031 MAX_IPV6_HDR_LEN);
4032 if (err < 0)
4033 goto out;
4034
4035 hp = OPT_HDR(struct frag_hdr, skb, off);
4036
4037 if (hp->frag_off & htons(IP6_OFFSET | IP6_MF))
4038 fragment = true;
4039
4040 nexthdr = hp->nexthdr;
4041 off += sizeof(struct frag_hdr);
4042 break;
4043 }
4044 default:
4045 done = true;
4046 break;
4047 }
4048 }
4049
4050 err = -EPROTO;
4051
4052 if (!done || fragment)
4053 goto out;
4054
Jan Beulichf9708b42014-03-11 13:56:05 +00004055 csum = skb_checksum_setup_ip(skb, nexthdr, off);
4056 if (IS_ERR(csum))
4057 return PTR_ERR(csum);
Paul Durranted1f50c2014-01-09 10:02:46 +00004058
Jan Beulichf9708b42014-03-11 13:56:05 +00004059 if (recalculate)
4060 *csum = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
4061 &ipv6_hdr(skb)->daddr,
4062 skb->len - off, nexthdr, 0);
Paul Durranted1f50c2014-01-09 10:02:46 +00004063 err = 0;
4064
4065out:
4066 return err;
4067}
4068
4069/**
4070 * skb_checksum_setup - set up partial checksum offset
4071 * @skb: the skb to set up
4072 * @recalculate: if true the pseudo-header checksum will be recalculated
4073 */
4074int skb_checksum_setup(struct sk_buff *skb, bool recalculate)
4075{
4076 int err;
4077
4078 switch (skb->protocol) {
4079 case htons(ETH_P_IP):
Jan Beulichf9708b42014-03-11 13:56:05 +00004080 err = skb_checksum_setup_ipv4(skb, recalculate);
Paul Durranted1f50c2014-01-09 10:02:46 +00004081 break;
4082
4083 case htons(ETH_P_IPV6):
4084 err = skb_checksum_setup_ipv6(skb, recalculate);
4085 break;
4086
4087 default:
4088 err = -EPROTO;
4089 break;
4090 }
4091
4092 return err;
4093}
4094EXPORT_SYMBOL(skb_checksum_setup);
4095
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004096/**
4097 * skb_checksum_maybe_trim - maybe trims the given skb
4098 * @skb: the skb to check
4099 * @transport_len: the data length beyond the network header
4100 *
4101 * Checks whether the given skb has data beyond the given transport length.
4102 * If so, returns a cloned skb trimmed to this transport length.
4103 * Otherwise returns the provided skb. Returns NULL in error cases
4104 * (e.g. transport_len exceeds skb length or out-of-memory).
4105 *
Linus Lüssinga5169932015-08-13 05:54:07 +02004106 * Caller needs to set the skb transport header and free any returned skb if it
4107 * differs from the provided skb.
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004108 */
4109static struct sk_buff *skb_checksum_maybe_trim(struct sk_buff *skb,
4110 unsigned int transport_len)
4111{
4112 struct sk_buff *skb_chk;
4113 unsigned int len = skb_transport_offset(skb) + transport_len;
4114 int ret;
4115
Linus Lüssinga5169932015-08-13 05:54:07 +02004116 if (skb->len < len)
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004117 return NULL;
Linus Lüssinga5169932015-08-13 05:54:07 +02004118 else if (skb->len == len)
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004119 return skb;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004120
4121 skb_chk = skb_clone(skb, GFP_ATOMIC);
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004122 if (!skb_chk)
4123 return NULL;
4124
4125 ret = pskb_trim_rcsum(skb_chk, len);
4126 if (ret) {
4127 kfree_skb(skb_chk);
4128 return NULL;
4129 }
4130
4131 return skb_chk;
4132}
4133
4134/**
4135 * skb_checksum_trimmed - validate checksum of an skb
4136 * @skb: the skb to check
4137 * @transport_len: the data length beyond the network header
4138 * @skb_chkf: checksum function to use
4139 *
4140 * Applies the given checksum function skb_chkf to the provided skb.
4141 * Returns a checked and maybe trimmed skb. Returns NULL on error.
4142 *
4143 * If the skb has data beyond the given transport length, then a
4144 * trimmed & cloned skb is checked and returned.
4145 *
Linus Lüssinga5169932015-08-13 05:54:07 +02004146 * Caller needs to set the skb transport header and free any returned skb if it
4147 * differs from the provided skb.
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004148 */
4149struct sk_buff *skb_checksum_trimmed(struct sk_buff *skb,
4150 unsigned int transport_len,
4151 __sum16(*skb_chkf)(struct sk_buff *skb))
4152{
4153 struct sk_buff *skb_chk;
4154 unsigned int offset = skb_transport_offset(skb);
Linus Lüssingfcba67c2015-05-05 00:19:35 +02004155 __sum16 ret;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004156
4157 skb_chk = skb_checksum_maybe_trim(skb, transport_len);
4158 if (!skb_chk)
Linus Lüssinga5169932015-08-13 05:54:07 +02004159 goto err;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004160
Linus Lüssinga5169932015-08-13 05:54:07 +02004161 if (!pskb_may_pull(skb_chk, offset))
4162 goto err;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004163
4164 __skb_pull(skb_chk, offset);
4165 ret = skb_chkf(skb_chk);
4166 __skb_push(skb_chk, offset);
4167
Linus Lüssinga5169932015-08-13 05:54:07 +02004168 if (ret)
4169 goto err;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004170
4171 return skb_chk;
Linus Lüssinga5169932015-08-13 05:54:07 +02004172
4173err:
4174 if (skb_chk && skb_chk != skb)
4175 kfree_skb(skb_chk);
4176
4177 return NULL;
4178
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004179}
4180EXPORT_SYMBOL(skb_checksum_trimmed);
4181
Ben Hutchings4497b072008-06-19 16:22:28 -07004182void __skb_warn_lro_forwarding(const struct sk_buff *skb)
4183{
Joe Perchese87cc472012-05-13 21:56:26 +00004184 net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n",
4185 skb->dev->name);
Ben Hutchings4497b072008-06-19 16:22:28 -07004186}
Ben Hutchings4497b072008-06-19 16:22:28 -07004187EXPORT_SYMBOL(__skb_warn_lro_forwarding);
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004188
4189void kfree_skb_partial(struct sk_buff *skb, bool head_stolen)
4190{
Eric Dumazet3d861f62012-10-22 09:03:40 +00004191 if (head_stolen) {
4192 skb_release_head_state(skb);
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004193 kmem_cache_free(skbuff_head_cache, skb);
Eric Dumazet3d861f62012-10-22 09:03:40 +00004194 } else {
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004195 __kfree_skb(skb);
Eric Dumazet3d861f62012-10-22 09:03:40 +00004196 }
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004197}
4198EXPORT_SYMBOL(kfree_skb_partial);
4199
4200/**
4201 * skb_try_coalesce - try to merge skb to prior one
4202 * @to: prior buffer
4203 * @from: buffer to add
4204 * @fragstolen: pointer to boolean
Randy Dunlapc6c4b972012-06-08 14:01:44 +00004205 * @delta_truesize: how much more was allocated than was requested
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004206 */
4207bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
4208 bool *fragstolen, int *delta_truesize)
4209{
4210 int i, delta, len = from->len;
4211
4212 *fragstolen = false;
4213
4214 if (skb_cloned(to))
4215 return false;
4216
4217 if (len <= skb_tailroom(to)) {
Eric Dumazete93a0432014-09-15 04:19:52 -07004218 if (len)
4219 BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len));
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004220 *delta_truesize = 0;
4221 return true;
4222 }
4223
4224 if (skb_has_frag_list(to) || skb_has_frag_list(from))
4225 return false;
4226
4227 if (skb_headlen(from) != 0) {
4228 struct page *page;
4229 unsigned int offset;
4230
4231 if (skb_shinfo(to)->nr_frags +
4232 skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
4233 return false;
4234
4235 if (skb_head_is_locked(from))
4236 return false;
4237
4238 delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
4239
4240 page = virt_to_head_page(from->head);
4241 offset = from->data - (unsigned char *)page_address(page);
4242
4243 skb_fill_page_desc(to, skb_shinfo(to)->nr_frags,
4244 page, offset, skb_headlen(from));
4245 *fragstolen = true;
4246 } else {
4247 if (skb_shinfo(to)->nr_frags +
4248 skb_shinfo(from)->nr_frags > MAX_SKB_FRAGS)
4249 return false;
4250
Weiping Panf4b549a2012-09-28 20:15:30 +00004251 delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from));
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004252 }
4253
4254 WARN_ON_ONCE(delta < len);
4255
4256 memcpy(skb_shinfo(to)->frags + skb_shinfo(to)->nr_frags,
4257 skb_shinfo(from)->frags,
4258 skb_shinfo(from)->nr_frags * sizeof(skb_frag_t));
4259 skb_shinfo(to)->nr_frags += skb_shinfo(from)->nr_frags;
4260
4261 if (!skb_cloned(from))
4262 skb_shinfo(from)->nr_frags = 0;
4263
Li RongQing8ea853f2012-09-18 16:53:21 +00004264 /* if the skb is not cloned this does nothing
4265 * since we set nr_frags to 0.
4266 */
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004267 for (i = 0; i < skb_shinfo(from)->nr_frags; i++)
4268 skb_frag_ref(from, i);
4269
4270 to->truesize += delta;
4271 to->len += len;
4272 to->data_len += len;
4273
4274 *delta_truesize = delta;
4275 return true;
4276}
4277EXPORT_SYMBOL(skb_try_coalesce);
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004278
4279/**
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004280 * skb_scrub_packet - scrub an skb
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004281 *
4282 * @skb: buffer to clean
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004283 * @xnet: packet is crossing netns
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004284 *
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004285 * skb_scrub_packet can be used after encapsulating or decapsulting a packet
4286 * into/from a tunnel. Some information have to be cleared during these
4287 * operations.
4288 * skb_scrub_packet can also be used to clean a skb before injecting it in
4289 * another namespace (@xnet == true). We have to clear all information in the
4290 * skb that could impact namespace isolation.
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004291 */
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004292void skb_scrub_packet(struct sk_buff *skb, bool xnet)
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004293{
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004294 skb->tstamp.tv64 = 0;
4295 skb->pkt_type = PACKET_HOST;
4296 skb->skb_iif = 0;
WANG Cong60ff7462014-05-04 16:39:18 -07004297 skb->ignore_df = 0;
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004298 skb_dst_drop(skb);
Eric Dumazetc29390c2015-03-11 18:42:02 -07004299 skb_sender_cpu_clear(skb);
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004300 secpath_reset(skb);
4301 nf_reset(skb);
4302 nf_reset_trace(skb);
Herbert Xu213dd742015-04-16 09:03:27 +08004303
4304 if (!xnet)
4305 return;
4306
4307 skb_orphan(skb);
4308 skb->mark = 0;
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004309}
4310EXPORT_SYMBOL_GPL(skb_scrub_packet);
Florian Westphalde960aa2014-01-26 10:58:16 +01004311
4312/**
4313 * skb_gso_transport_seglen - Return length of individual segments of a gso packet
4314 *
4315 * @skb: GSO skb
4316 *
4317 * skb_gso_transport_seglen is used to determine the real size of the
4318 * individual segments, including Layer4 headers (TCP/UDP).
4319 *
4320 * The MAC/L2 or network (IP, IPv6) headers are not accounted for.
4321 */
4322unsigned int skb_gso_transport_seglen(const struct sk_buff *skb)
4323{
4324 const struct skb_shared_info *shinfo = skb_shinfo(skb);
Florian Westphalf993bc22014-10-20 13:49:18 +02004325 unsigned int thlen = 0;
Florian Westphalde960aa2014-01-26 10:58:16 +01004326
Florian Westphalf993bc22014-10-20 13:49:18 +02004327 if (skb->encapsulation) {
4328 thlen = skb_inner_transport_header(skb) -
4329 skb_transport_header(skb);
Florian Westphal6d39d582014-04-09 10:28:50 +02004330
Florian Westphalf993bc22014-10-20 13:49:18 +02004331 if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)))
4332 thlen += inner_tcp_hdrlen(skb);
4333 } else if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) {
4334 thlen = tcp_hdrlen(skb);
4335 }
Florian Westphal6d39d582014-04-09 10:28:50 +02004336 /* UFO sets gso_size to the size of the fragmentation
4337 * payload, i.e. the size of the L4 (UDP) header is already
4338 * accounted for.
4339 */
Florian Westphalf993bc22014-10-20 13:49:18 +02004340 return thlen + shinfo->gso_size;
Florian Westphalde960aa2014-01-26 10:58:16 +01004341}
4342EXPORT_SYMBOL_GPL(skb_gso_transport_seglen);
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04004343
4344static struct sk_buff *skb_reorder_vlan_header(struct sk_buff *skb)
4345{
4346 if (skb_cow(skb, skb_headroom(skb)) < 0) {
4347 kfree_skb(skb);
4348 return NULL;
4349 }
4350
Vlad Yasevichf6548612015-12-14 17:44:10 -05004351 memmove(skb->data - ETH_HLEN, skb->data - skb->mac_len - VLAN_HLEN,
Vlad Yasevicha6e18ff2015-11-16 15:43:44 -05004352 2 * ETH_ALEN);
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04004353 skb->mac_header += VLAN_HLEN;
4354 return skb;
4355}
4356
4357struct sk_buff *skb_vlan_untag(struct sk_buff *skb)
4358{
4359 struct vlan_hdr *vhdr;
4360 u16 vlan_tci;
4361
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01004362 if (unlikely(skb_vlan_tag_present(skb))) {
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04004363 /* vlan_tci is already set-up so leave this for another time */
4364 return skb;
4365 }
4366
4367 skb = skb_share_check(skb, GFP_ATOMIC);
4368 if (unlikely(!skb))
4369 goto err_free;
4370
4371 if (unlikely(!pskb_may_pull(skb, VLAN_HLEN)))
4372 goto err_free;
4373
4374 vhdr = (struct vlan_hdr *)skb->data;
4375 vlan_tci = ntohs(vhdr->h_vlan_TCI);
4376 __vlan_hwaccel_put_tag(skb, skb->protocol, vlan_tci);
4377
4378 skb_pull_rcsum(skb, VLAN_HLEN);
4379 vlan_set_encap_proto(skb, vhdr);
4380
4381 skb = skb_reorder_vlan_header(skb);
4382 if (unlikely(!skb))
4383 goto err_free;
4384
4385 skb_reset_network_header(skb);
4386 skb_reset_transport_header(skb);
4387 skb_reset_mac_len(skb);
4388
4389 return skb;
4390
4391err_free:
4392 kfree_skb(skb);
4393 return NULL;
4394}
4395EXPORT_SYMBOL(skb_vlan_untag);
Eric Dumazet2e4e4412014-09-17 04:49:49 -07004396
Jiri Pirkoe2195122014-11-19 14:05:01 +01004397int skb_ensure_writable(struct sk_buff *skb, int write_len)
4398{
4399 if (!pskb_may_pull(skb, write_len))
4400 return -ENOMEM;
4401
4402 if (!skb_cloned(skb) || skb_clone_writable(skb, write_len))
4403 return 0;
4404
4405 return pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
4406}
4407EXPORT_SYMBOL(skb_ensure_writable);
4408
Jiri Pirko93515d52014-11-19 14:05:02 +01004409/* remove VLAN header from packet and update csum accordingly. */
4410static int __skb_vlan_pop(struct sk_buff *skb, u16 *vlan_tci)
4411{
4412 struct vlan_hdr *vhdr;
4413 unsigned int offset = skb->data - skb_mac_header(skb);
4414 int err;
4415
4416 __skb_push(skb, offset);
4417 err = skb_ensure_writable(skb, VLAN_ETH_HLEN);
4418 if (unlikely(err))
4419 goto pull;
4420
4421 skb_postpull_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
4422
4423 vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN);
4424 *vlan_tci = ntohs(vhdr->h_vlan_TCI);
4425
4426 memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN);
4427 __skb_pull(skb, VLAN_HLEN);
4428
4429 vlan_set_encap_proto(skb, vhdr);
4430 skb->mac_header += VLAN_HLEN;
4431
4432 if (skb_network_offset(skb) < ETH_HLEN)
4433 skb_set_network_header(skb, ETH_HLEN);
4434
4435 skb_reset_mac_len(skb);
4436pull:
4437 __skb_pull(skb, offset);
4438
4439 return err;
4440}
4441
4442int skb_vlan_pop(struct sk_buff *skb)
4443{
4444 u16 vlan_tci;
4445 __be16 vlan_proto;
4446 int err;
4447
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01004448 if (likely(skb_vlan_tag_present(skb))) {
Jiri Pirko93515d52014-11-19 14:05:02 +01004449 skb->vlan_tci = 0;
4450 } else {
4451 if (unlikely((skb->protocol != htons(ETH_P_8021Q) &&
4452 skb->protocol != htons(ETH_P_8021AD)) ||
4453 skb->len < VLAN_ETH_HLEN))
4454 return 0;
4455
4456 err = __skb_vlan_pop(skb, &vlan_tci);
4457 if (err)
4458 return err;
4459 }
4460 /* move next vlan tag to hw accel tag */
4461 if (likely((skb->protocol != htons(ETH_P_8021Q) &&
4462 skb->protocol != htons(ETH_P_8021AD)) ||
4463 skb->len < VLAN_ETH_HLEN))
4464 return 0;
4465
4466 vlan_proto = skb->protocol;
4467 err = __skb_vlan_pop(skb, &vlan_tci);
4468 if (unlikely(err))
4469 return err;
4470
4471 __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
4472 return 0;
4473}
4474EXPORT_SYMBOL(skb_vlan_pop);
4475
4476int skb_vlan_push(struct sk_buff *skb, __be16 vlan_proto, u16 vlan_tci)
4477{
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01004478 if (skb_vlan_tag_present(skb)) {
Jiri Pirko93515d52014-11-19 14:05:02 +01004479 unsigned int offset = skb->data - skb_mac_header(skb);
4480 int err;
4481
4482 /* __vlan_insert_tag expect skb->data pointing to mac header.
4483 * So change skb->data before calling it and change back to
4484 * original position later
4485 */
4486 __skb_push(skb, offset);
4487 err = __vlan_insert_tag(skb, skb->vlan_proto,
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01004488 skb_vlan_tag_get(skb));
Jiri Pirko93515d52014-11-19 14:05:02 +01004489 if (err)
4490 return err;
4491 skb->protocol = skb->vlan_proto;
4492 skb->mac_len += VLAN_HLEN;
4493 __skb_pull(skb, offset);
4494
4495 if (skb->ip_summed == CHECKSUM_COMPLETE)
4496 skb->csum = csum_add(skb->csum, csum_partial(skb->data
4497 + (2 * ETH_ALEN), VLAN_HLEN, 0));
4498 }
4499 __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
4500 return 0;
4501}
4502EXPORT_SYMBOL(skb_vlan_push);
4503
Eric Dumazet2e4e4412014-09-17 04:49:49 -07004504/**
4505 * alloc_skb_with_frags - allocate skb with page frags
4506 *
Masanari Iidade3f0d02014-10-09 12:58:08 +09004507 * @header_len: size of linear part
4508 * @data_len: needed length in frags
4509 * @max_page_order: max page order desired.
4510 * @errcode: pointer to error code if any
4511 * @gfp_mask: allocation mask
Eric Dumazet2e4e4412014-09-17 04:49:49 -07004512 *
4513 * This can be used to allocate a paged skb, given a maximal order for frags.
4514 */
4515struct sk_buff *alloc_skb_with_frags(unsigned long header_len,
4516 unsigned long data_len,
4517 int max_page_order,
4518 int *errcode,
4519 gfp_t gfp_mask)
4520{
4521 int npages = (data_len + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
4522 unsigned long chunk;
4523 struct sk_buff *skb;
4524 struct page *page;
4525 gfp_t gfp_head;
4526 int i;
4527
4528 *errcode = -EMSGSIZE;
4529 /* Note this test could be relaxed, if we succeed to allocate
4530 * high order pages...
4531 */
4532 if (npages > MAX_SKB_FRAGS)
4533 return NULL;
4534
4535 gfp_head = gfp_mask;
Mel Gormand0164ad2015-11-06 16:28:21 -08004536 if (gfp_head & __GFP_DIRECT_RECLAIM)
Eric Dumazet2e4e4412014-09-17 04:49:49 -07004537 gfp_head |= __GFP_REPEAT;
4538
4539 *errcode = -ENOBUFS;
4540 skb = alloc_skb(header_len, gfp_head);
4541 if (!skb)
4542 return NULL;
4543
4544 skb->truesize += npages << PAGE_SHIFT;
4545
4546 for (i = 0; npages > 0; i++) {
4547 int order = max_page_order;
4548
4549 while (order) {
4550 if (npages >= 1 << order) {
Mel Gormand0164ad2015-11-06 16:28:21 -08004551 page = alloc_pages((gfp_mask & ~__GFP_DIRECT_RECLAIM) |
Eric Dumazet2e4e4412014-09-17 04:49:49 -07004552 __GFP_COMP |
4553 __GFP_NOWARN |
4554 __GFP_NORETRY,
4555 order);
4556 if (page)
4557 goto fill_page;
4558 /* Do not retry other high order allocations */
4559 order = 1;
4560 max_page_order = 0;
4561 }
4562 order--;
4563 }
4564 page = alloc_page(gfp_mask);
4565 if (!page)
4566 goto failure;
4567fill_page:
4568 chunk = min_t(unsigned long, data_len,
4569 PAGE_SIZE << order);
4570 skb_fill_page_desc(skb, i, page, 0, chunk);
4571 data_len -= chunk;
4572 npages -= 1 << order;
4573 }
4574 return skb;
4575
4576failure:
4577 kfree_skb(skb);
4578 return NULL;
4579}
4580EXPORT_SYMBOL(alloc_skb_with_frags);