blob: 8616d1147c938808da752734b6cecea260b149b9 [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;
Hans Westgaard Ry5f74f822016-02-03 09:26:57 +010082int sysctl_max_skb_frags __read_mostly = MAX_SKB_FRAGS;
83EXPORT_SYMBOL(sysctl_max_skb_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Linus Torvalds1da177e2005-04-16 15:20:36 -070085/**
Jean Sacrenf05de732013-02-11 13:30:38 +000086 * skb_panic - private function for out-of-line support
87 * @skb: buffer
88 * @sz: size
89 * @addr: address
James Hogan99d58512013-02-13 11:20:27 +000090 * @msg: skb_over_panic or skb_under_panic
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 *
Jean Sacrenf05de732013-02-11 13:30:38 +000092 * Out-of-line support for skb_put() and skb_push().
93 * Called via the wrapper skb_over_panic() or skb_under_panic().
94 * Keep out of line to prevent kernel bloat.
95 * __builtin_return_address is not used because it is not always reliable.
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 */
Jean Sacrenf05de732013-02-11 13:30:38 +000097static void skb_panic(struct sk_buff *skb, unsigned int sz, void *addr,
James Hogan99d58512013-02-13 11:20:27 +000098 const char msg[])
Linus Torvalds1da177e2005-04-16 15:20:36 -070099{
Joe Perchese005d192012-05-16 19:58:40 +0000100 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 +0000101 msg, addr, skb->len, sz, skb->head, skb->data,
Joe Perchese005d192012-05-16 19:58:40 +0000102 (unsigned long)skb->tail, (unsigned long)skb->end,
103 skb->dev ? skb->dev->name : "<NULL>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 BUG();
105}
106
Jean Sacrenf05de732013-02-11 13:30:38 +0000107static void skb_over_panic(struct sk_buff *skb, unsigned int sz, void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108{
Jean Sacrenf05de732013-02-11 13:30:38 +0000109 skb_panic(skb, sz, addr, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110}
111
Jean Sacrenf05de732013-02-11 13:30:38 +0000112static void skb_under_panic(struct sk_buff *skb, unsigned int sz, void *addr)
113{
114 skb_panic(skb, sz, addr, __func__);
115}
Mel Gormanc93bdd02012-07-31 16:44:19 -0700116
117/*
118 * kmalloc_reserve is a wrapper around kmalloc_node_track_caller that tells
119 * the caller if emergency pfmemalloc reserves are being used. If it is and
120 * the socket is later found to be SOCK_MEMALLOC then PFMEMALLOC reserves
121 * may be used. Otherwise, the packet data may be discarded until enough
122 * memory is free
123 */
124#define kmalloc_reserve(size, gfp, node, pfmemalloc) \
125 __kmalloc_reserve(size, gfp, node, _RET_IP_, pfmemalloc)
stephen hemminger61c5e882012-12-28 18:24:28 +0000126
127static void *__kmalloc_reserve(size_t size, gfp_t flags, int node,
128 unsigned long ip, bool *pfmemalloc)
Mel Gormanc93bdd02012-07-31 16:44:19 -0700129{
130 void *obj;
131 bool ret_pfmemalloc = false;
132
133 /*
134 * Try a regular allocation, when that fails and we're not entitled
135 * to the reserves, fail.
136 */
137 obj = kmalloc_node_track_caller(size,
138 flags | __GFP_NOMEMALLOC | __GFP_NOWARN,
139 node);
140 if (obj || !(gfp_pfmemalloc_allowed(flags)))
141 goto out;
142
143 /* Try again but now we are using pfmemalloc reserves */
144 ret_pfmemalloc = true;
145 obj = kmalloc_node_track_caller(size, flags, node);
146
147out:
148 if (pfmemalloc)
149 *pfmemalloc = ret_pfmemalloc;
150
151 return obj;
152}
153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154/* Allocate a new skbuff. We do this ourselves so we can fill in a few
155 * 'private' fields and also do memory statistics to find all the
156 * [BEEP] leaks.
157 *
158 */
159
Patrick McHardy0ebd0ac2013-04-17 06:46:58 +0000160struct sk_buff *__alloc_skb_head(gfp_t gfp_mask, int node)
161{
162 struct sk_buff *skb;
163
164 /* Get the HEAD */
165 skb = kmem_cache_alloc_node(skbuff_head_cache,
166 gfp_mask & ~__GFP_DMA, node);
167 if (!skb)
168 goto out;
169
170 /*
171 * Only clear those fields we need to clear, not those that we will
172 * actually initialise below. Hence, don't put any more fields after
173 * the tail pointer in struct sk_buff!
174 */
175 memset(skb, 0, offsetof(struct sk_buff, tail));
Pablo Neira5e71d9d2013-06-03 09:28:43 +0000176 skb->head = NULL;
Patrick McHardy0ebd0ac2013-04-17 06:46:58 +0000177 skb->truesize = sizeof(struct sk_buff);
178 atomic_set(&skb->users, 1);
179
Cong Wang35d04612013-05-29 15:16:05 +0800180 skb->mac_header = (typeof(skb->mac_header))~0U;
Patrick McHardy0ebd0ac2013-04-17 06:46:58 +0000181out:
182 return skb;
183}
184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185/**
David S. Millerd179cd12005-08-17 14:57:30 -0700186 * __alloc_skb - allocate a network buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 * @size: size to allocate
188 * @gfp_mask: allocation mask
Mel Gormanc93bdd02012-07-31 16:44:19 -0700189 * @flags: If SKB_ALLOC_FCLONE is set, allocate from fclone cache
190 * instead of head cache and allocate a cloned (child) skb.
191 * If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for
192 * allocations in case the data is required for writeback
Christoph Hellwigb30973f2006-12-06 20:32:36 -0800193 * @node: numa node to allocate memory on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 *
195 * Allocate a new &sk_buff. The returned buffer has no headroom and a
Ben Hutchings94b60422012-06-06 15:23:37 +0000196 * tail room of at least size bytes. The object has a reference count
197 * of one. The return is the buffer. On a failure the return is %NULL.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 *
199 * Buffers may only be allocated from interrupts using a @gfp_mask of
200 * %GFP_ATOMIC.
201 */
Al Virodd0fc662005-10-07 07:46:04 +0100202struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
Mel Gormanc93bdd02012-07-31 16:44:19 -0700203 int flags, int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
Christoph Lametere18b8902006-12-06 20:33:20 -0800205 struct kmem_cache *cache;
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800206 struct skb_shared_info *shinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 struct sk_buff *skb;
208 u8 *data;
Mel Gormanc93bdd02012-07-31 16:44:19 -0700209 bool pfmemalloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Mel Gormanc93bdd02012-07-31 16:44:19 -0700211 cache = (flags & SKB_ALLOC_FCLONE)
212 ? skbuff_fclone_cache : skbuff_head_cache;
213
214 if (sk_memalloc_socks() && (flags & SKB_ALLOC_RX))
215 gfp_mask |= __GFP_MEMALLOC;
Herbert Xu8798b3f2006-01-23 16:32:45 -0800216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 /* Get the HEAD */
Christoph Hellwigb30973f2006-12-06 20:32:36 -0800218 skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 if (!skb)
220 goto out;
Eric Dumazetec7d2f22010-05-05 01:07:37 -0700221 prefetchw(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
Eric Dumazet87fb4b72011-10-13 07:28:54 +0000223 /* We do our best to align skb_shared_info on a separate cache
224 * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives
225 * aligned memory blocks, unless SLUB/SLAB debug is enabled.
226 * Both skb->head and skb_shared_info are cache line aligned.
227 */
Tony Lindgrenbc417e32011-11-02 13:40:28 +0000228 size = SKB_DATA_ALIGN(size);
Eric Dumazet87fb4b72011-10-13 07:28:54 +0000229 size += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Mel Gormanc93bdd02012-07-31 16:44:19 -0700230 data = kmalloc_reserve(size, gfp_mask, node, &pfmemalloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 if (!data)
232 goto nodata;
Eric Dumazet87fb4b72011-10-13 07:28:54 +0000233 /* kmalloc(size) might give us more room than requested.
234 * Put skb_shared_info exactly at the end of allocated zone,
235 * to allow max possible filling before reallocation.
236 */
237 size = SKB_WITH_OVERHEAD(ksize(data));
Eric Dumazetec7d2f22010-05-05 01:07:37 -0700238 prefetchw(data + size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Arnaldo Carvalho de Meloca0605a2007-03-19 10:48:59 -0300240 /*
Johannes Bergc8005782008-05-03 20:56:42 -0700241 * Only clear those fields we need to clear, not those that we will
242 * actually initialise below. Hence, don't put any more fields after
243 * the tail pointer in struct sk_buff!
Arnaldo Carvalho de Meloca0605a2007-03-19 10:48:59 -0300244 */
245 memset(skb, 0, offsetof(struct sk_buff, tail));
Eric Dumazet87fb4b72011-10-13 07:28:54 +0000246 /* Account for allocated memory : skb + skb->head */
247 skb->truesize = SKB_TRUESIZE(size);
Mel Gormanc93bdd02012-07-31 16:44:19 -0700248 skb->pfmemalloc = pfmemalloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 atomic_set(&skb->users, 1);
250 skb->head = data;
251 skb->data = data;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700252 skb_reset_tail_pointer(skb);
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700253 skb->end = skb->tail + size;
Cong Wang35d04612013-05-29 15:16:05 +0800254 skb->mac_header = (typeof(skb->mac_header))~0U;
255 skb->transport_header = (typeof(skb->transport_header))~0U;
Stephen Hemminger19633e12009-06-17 05:23:27 +0000256
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800257 /* make sure we initialize shinfo sequentially */
258 shinfo = skb_shinfo(skb);
Eric Dumazetec7d2f22010-05-05 01:07:37 -0700259 memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800260 atomic_set(&shinfo->dataref, 1);
Eric Dumazetc2aa3662011-01-25 23:18:38 +0000261 kmemcheck_annotate_variable(shinfo->destructor_arg);
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800262
Mel Gormanc93bdd02012-07-31 16:44:19 -0700263 if (flags & SKB_ALLOC_FCLONE) {
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700264 struct sk_buff_fclones *fclones;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700266 fclones = container_of(skb, struct sk_buff_fclones, skb1);
267
268 kmemcheck_annotate_bitfield(&fclones->skb2, flags1);
David S. Millerd179cd12005-08-17 14:57:30 -0700269 skb->fclone = SKB_FCLONE_ORIG;
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700270 atomic_set(&fclones->fclone_ref, 1);
David S. Millerd179cd12005-08-17 14:57:30 -0700271
Eric Dumazet6ffe75e2014-12-03 17:04:39 -0800272 fclones->skb2.fclone = SKB_FCLONE_CLONE;
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700273 fclones->skb2.pfmemalloc = pfmemalloc;
David S. Millerd179cd12005-08-17 14:57:30 -0700274 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275out:
276 return skb;
277nodata:
Herbert Xu8798b3f2006-01-23 16:32:45 -0800278 kmem_cache_free(cache, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 skb = NULL;
280 goto out;
281}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800282EXPORT_SYMBOL(__alloc_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
284/**
Eric Dumazet2ea2f622015-04-24 16:05:01 -0700285 * __build_skb - build a network buffer
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000286 * @data: data buffer provided by caller
Eric Dumazet2ea2f622015-04-24 16:05:01 -0700287 * @frag_size: size of data, or 0 if head was kmalloced
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000288 *
289 * Allocate a new &sk_buff. Caller provides space holding head and
Florian Fainellideceb4c2013-07-23 20:22:39 +0100290 * skb_shared_info. @data must have been allocated by kmalloc() only if
Eric Dumazet2ea2f622015-04-24 16:05:01 -0700291 * @frag_size is 0, otherwise data should come from the page allocator
292 * or vmalloc()
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000293 * The return is the new skb buffer.
294 * On a failure the return is %NULL, and @data is not freed.
295 * Notes :
296 * Before IO, driver allocates only data buffer where NIC put incoming frame
297 * Driver should add room at head (NET_SKB_PAD) and
298 * MUST add room at tail (SKB_DATA_ALIGN(skb_shared_info))
299 * After IO, driver calls build_skb(), to allocate sk_buff and populate it
300 * before giving packet to stack.
301 * RX rings only contains data buffers, not full skbs.
302 */
Eric Dumazet2ea2f622015-04-24 16:05:01 -0700303struct sk_buff *__build_skb(void *data, unsigned int frag_size)
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000304{
305 struct skb_shared_info *shinfo;
306 struct sk_buff *skb;
Eric Dumazetd3836f22012-04-27 00:33:38 +0000307 unsigned int size = frag_size ? : ksize(data);
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000308
309 skb = kmem_cache_alloc(skbuff_head_cache, GFP_ATOMIC);
310 if (!skb)
311 return NULL;
312
Eric Dumazetd3836f22012-04-27 00:33:38 +0000313 size -= SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000314
315 memset(skb, 0, offsetof(struct sk_buff, tail));
316 skb->truesize = SKB_TRUESIZE(size);
317 atomic_set(&skb->users, 1);
318 skb->head = data;
319 skb->data = data;
320 skb_reset_tail_pointer(skb);
321 skb->end = skb->tail + size;
Cong Wang35d04612013-05-29 15:16:05 +0800322 skb->mac_header = (typeof(skb->mac_header))~0U;
323 skb->transport_header = (typeof(skb->transport_header))~0U;
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000324
325 /* make sure we initialize shinfo sequentially */
326 shinfo = skb_shinfo(skb);
327 memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
328 atomic_set(&shinfo->dataref, 1);
329 kmemcheck_annotate_variable(shinfo->destructor_arg);
330
331 return skb;
332}
Eric Dumazet2ea2f622015-04-24 16:05:01 -0700333
334/* build_skb() is wrapper over __build_skb(), that specifically
335 * takes care of skb->head and skb->pfmemalloc
336 * This means that if @frag_size is not zero, then @data must be backed
337 * by a page fragment, not kmalloc() or vmalloc()
338 */
339struct sk_buff *build_skb(void *data, unsigned int frag_size)
340{
341 struct sk_buff *skb = __build_skb(data, frag_size);
342
343 if (skb && frag_size) {
344 skb->head_frag = 1;
Michal Hocko2f064f32015-08-21 14:11:51 -0700345 if (page_is_pfmemalloc(virt_to_head_page(data)))
Eric Dumazet2ea2f622015-04-24 16:05:01 -0700346 skb->pfmemalloc = 1;
347 }
348 return skb;
349}
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000350EXPORT_SYMBOL(build_skb);
351
Alexander Duyckb63ae8c2015-05-06 21:11:57 -0700352static DEFINE_PER_CPU(struct page_frag_cache, netdev_alloc_cache);
353static DEFINE_PER_CPU(struct page_frag_cache, napi_alloc_cache);
Alexander Duyckffde7322014-12-09 19:40:42 -0800354
355static void *__netdev_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
356{
Alexander Duyckb63ae8c2015-05-06 21:11:57 -0700357 struct page_frag_cache *nc;
Alexander Duyckffde7322014-12-09 19:40:42 -0800358 unsigned long flags;
359 void *data;
360
361 local_irq_save(flags);
Alexander Duyck94519802015-05-06 21:11:40 -0700362 nc = this_cpu_ptr(&netdev_alloc_cache);
363 data = __alloc_page_frag(nc, fragsz, gfp_mask);
Eric Dumazet6f532612012-05-18 05:12:12 +0000364 local_irq_restore(flags);
365 return data;
366}
Mel Gormanc93bdd02012-07-31 16:44:19 -0700367
368/**
369 * netdev_alloc_frag - allocate a page fragment
370 * @fragsz: fragment size
371 *
372 * Allocates a frag from a page for receive buffer.
373 * Uses GFP_ATOMIC allocations.
374 */
375void *netdev_alloc_frag(unsigned int fragsz)
376{
377 return __netdev_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD);
378}
Eric Dumazet6f532612012-05-18 05:12:12 +0000379EXPORT_SYMBOL(netdev_alloc_frag);
380
Alexander Duyckffde7322014-12-09 19:40:42 -0800381static void *__napi_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
382{
Alexander Duyckb63ae8c2015-05-06 21:11:57 -0700383 struct page_frag_cache *nc = this_cpu_ptr(&napi_alloc_cache);
Alexander Duyck94519802015-05-06 21:11:40 -0700384
385 return __alloc_page_frag(nc, fragsz, gfp_mask);
Alexander Duyckffde7322014-12-09 19:40:42 -0800386}
387
388void *napi_alloc_frag(unsigned int fragsz)
389{
390 return __napi_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD);
391}
392EXPORT_SYMBOL(napi_alloc_frag);
393
Eric Dumazet6f532612012-05-18 05:12:12 +0000394/**
Alexander Duyckfd11a832014-12-09 19:40:49 -0800395 * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
396 * @dev: network device to receive on
Masanari Iidad7499162015-08-24 22:56:54 +0900397 * @len: length to allocate
Alexander Duyckfd11a832014-12-09 19:40:49 -0800398 * @gfp_mask: get_free_pages mask, passed to alloc_skb
399 *
400 * Allocate a new &sk_buff and assign it a usage count of one. The
401 * buffer has NET_SKB_PAD headroom built in. Users should allocate
402 * the headroom they think they need without accounting for the
403 * built in space. The built in space is used for optimisations.
404 *
405 * %NULL is returned if there is no free memory.
406 */
Alexander Duyck94519802015-05-06 21:11:40 -0700407struct sk_buff *__netdev_alloc_skb(struct net_device *dev, unsigned int len,
408 gfp_t gfp_mask)
Alexander Duyckfd11a832014-12-09 19:40:49 -0800409{
Alexander Duyckb63ae8c2015-05-06 21:11:57 -0700410 struct page_frag_cache *nc;
Alexander Duyck94519802015-05-06 21:11:40 -0700411 unsigned long flags;
Alexander Duyckfd11a832014-12-09 19:40:49 -0800412 struct sk_buff *skb;
Alexander Duyck94519802015-05-06 21:11:40 -0700413 bool pfmemalloc;
414 void *data;
Alexander Duyckfd11a832014-12-09 19:40:49 -0800415
Alexander Duyck94519802015-05-06 21:11:40 -0700416 len += NET_SKB_PAD;
Alexander Duyckfd11a832014-12-09 19:40:49 -0800417
Alexander Duyck94519802015-05-06 21:11:40 -0700418 if ((len > SKB_WITH_OVERHEAD(PAGE_SIZE)) ||
Mel Gormand0164ad2015-11-06 16:28:21 -0800419 (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) {
Alexander Duycka080e7b2015-05-13 13:34:13 -0700420 skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE);
421 if (!skb)
422 goto skb_fail;
423 goto skb_success;
424 }
Alexander Duyck94519802015-05-06 21:11:40 -0700425
426 len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
427 len = SKB_DATA_ALIGN(len);
428
429 if (sk_memalloc_socks())
430 gfp_mask |= __GFP_MEMALLOC;
431
432 local_irq_save(flags);
433
434 nc = this_cpu_ptr(&netdev_alloc_cache);
435 data = __alloc_page_frag(nc, len, gfp_mask);
436 pfmemalloc = nc->pfmemalloc;
437
438 local_irq_restore(flags);
439
440 if (unlikely(!data))
441 return NULL;
442
443 skb = __build_skb(data, len);
444 if (unlikely(!skb)) {
Alexander Duyck181edb22015-05-06 21:12:03 -0700445 skb_free_frag(data);
Alexander Duyck94519802015-05-06 21:11:40 -0700446 return NULL;
Christoph Hellwig7b2e4972006-08-07 16:09:04 -0700447 }
Alexander Duyckfd11a832014-12-09 19:40:49 -0800448
Alexander Duyck94519802015-05-06 21:11:40 -0700449 /* use OR instead of assignment to avoid clearing of bits in mask */
450 if (pfmemalloc)
451 skb->pfmemalloc = 1;
452 skb->head_frag = 1;
453
Alexander Duycka080e7b2015-05-13 13:34:13 -0700454skb_success:
Alexander Duyck94519802015-05-06 21:11:40 -0700455 skb_reserve(skb, NET_SKB_PAD);
456 skb->dev = dev;
457
Alexander Duycka080e7b2015-05-13 13:34:13 -0700458skb_fail:
Christoph Hellwig8af27452006-07-31 22:35:23 -0700459 return skb;
460}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800461EXPORT_SYMBOL(__netdev_alloc_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
Alexander Duyckfd11a832014-12-09 19:40:49 -0800463/**
464 * __napi_alloc_skb - allocate skbuff for rx in a specific NAPI instance
465 * @napi: napi instance this buffer was allocated for
Masanari Iidad7499162015-08-24 22:56:54 +0900466 * @len: length to allocate
Alexander Duyckfd11a832014-12-09 19:40:49 -0800467 * @gfp_mask: get_free_pages mask, passed to alloc_skb and alloc_pages
468 *
469 * Allocate a new sk_buff for use in NAPI receive. This buffer will
470 * attempt to allocate the head from a special reserved region used
471 * only for NAPI Rx allocation. By doing this we can save several
472 * CPU cycles by avoiding having to disable and re-enable IRQs.
473 *
474 * %NULL is returned if there is no free memory.
475 */
Alexander Duyck94519802015-05-06 21:11:40 -0700476struct sk_buff *__napi_alloc_skb(struct napi_struct *napi, unsigned int len,
477 gfp_t gfp_mask)
Alexander Duyckfd11a832014-12-09 19:40:49 -0800478{
Alexander Duyckb63ae8c2015-05-06 21:11:57 -0700479 struct page_frag_cache *nc = this_cpu_ptr(&napi_alloc_cache);
Alexander Duyckfd11a832014-12-09 19:40:49 -0800480 struct sk_buff *skb;
Alexander Duyck94519802015-05-06 21:11:40 -0700481 void *data;
Alexander Duyckfd11a832014-12-09 19:40:49 -0800482
Alexander Duyck94519802015-05-06 21:11:40 -0700483 len += NET_SKB_PAD + NET_IP_ALIGN;
Alexander Duyckfd11a832014-12-09 19:40:49 -0800484
Alexander Duyck94519802015-05-06 21:11:40 -0700485 if ((len > SKB_WITH_OVERHEAD(PAGE_SIZE)) ||
Mel Gormand0164ad2015-11-06 16:28:21 -0800486 (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) {
Alexander Duycka080e7b2015-05-13 13:34:13 -0700487 skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE);
488 if (!skb)
489 goto skb_fail;
490 goto skb_success;
491 }
Alexander Duyck94519802015-05-06 21:11:40 -0700492
493 len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
494 len = SKB_DATA_ALIGN(len);
495
496 if (sk_memalloc_socks())
497 gfp_mask |= __GFP_MEMALLOC;
498
499 data = __alloc_page_frag(nc, len, gfp_mask);
500 if (unlikely(!data))
501 return NULL;
502
503 skb = __build_skb(data, len);
504 if (unlikely(!skb)) {
Alexander Duyck181edb22015-05-06 21:12:03 -0700505 skb_free_frag(data);
Alexander Duyck94519802015-05-06 21:11:40 -0700506 return NULL;
Alexander Duyckfd11a832014-12-09 19:40:49 -0800507 }
508
Alexander Duyck94519802015-05-06 21:11:40 -0700509 /* use OR instead of assignment to avoid clearing of bits in mask */
510 if (nc->pfmemalloc)
511 skb->pfmemalloc = 1;
512 skb->head_frag = 1;
513
Alexander Duycka080e7b2015-05-13 13:34:13 -0700514skb_success:
Alexander Duyck94519802015-05-06 21:11:40 -0700515 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
516 skb->dev = napi->dev;
517
Alexander Duycka080e7b2015-05-13 13:34:13 -0700518skb_fail:
Alexander Duyckfd11a832014-12-09 19:40:49 -0800519 return skb;
520}
521EXPORT_SYMBOL(__napi_alloc_skb);
522
Peter Zijlstra654bed12008-10-07 14:22:33 -0700523void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off,
Eric Dumazet50269e12012-03-23 23:59:33 +0000524 int size, unsigned int truesize)
Peter Zijlstra654bed12008-10-07 14:22:33 -0700525{
526 skb_fill_page_desc(skb, i, page, off, size);
527 skb->len += size;
528 skb->data_len += size;
Eric Dumazet50269e12012-03-23 23:59:33 +0000529 skb->truesize += truesize;
Peter Zijlstra654bed12008-10-07 14:22:33 -0700530}
531EXPORT_SYMBOL(skb_add_rx_frag);
532
Jason Wangf8e617e2013-11-01 14:07:47 +0800533void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size,
534 unsigned int truesize)
535{
536 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
537
538 skb_frag_size_add(frag, size);
539 skb->len += size;
540 skb->data_len += size;
541 skb->truesize += truesize;
542}
543EXPORT_SYMBOL(skb_coalesce_rx_frag);
544
Herbert Xu27b437c2006-07-13 19:26:39 -0700545static void skb_drop_list(struct sk_buff **listp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546{
Eric Dumazetbd8a7032013-06-24 06:26:00 -0700547 kfree_skb_list(*listp);
Herbert Xu27b437c2006-07-13 19:26:39 -0700548 *listp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549}
550
Herbert Xu27b437c2006-07-13 19:26:39 -0700551static inline void skb_drop_fraglist(struct sk_buff *skb)
552{
553 skb_drop_list(&skb_shinfo(skb)->frag_list);
554}
555
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556static void skb_clone_fraglist(struct sk_buff *skb)
557{
558 struct sk_buff *list;
559
David S. Millerfbb398a2009-06-09 00:18:59 -0700560 skb_walk_frags(skb, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 skb_get(list);
562}
563
Eric Dumazetd3836f22012-04-27 00:33:38 +0000564static void skb_free_head(struct sk_buff *skb)
565{
Alexander Duyck181edb22015-05-06 21:12:03 -0700566 unsigned char *head = skb->head;
567
Eric Dumazetd3836f22012-04-27 00:33:38 +0000568 if (skb->head_frag)
Alexander Duyck181edb22015-05-06 21:12:03 -0700569 skb_free_frag(head);
Eric Dumazetd3836f22012-04-27 00:33:38 +0000570 else
Alexander Duyck181edb22015-05-06 21:12:03 -0700571 kfree(head);
Eric Dumazetd3836f22012-04-27 00:33:38 +0000572}
573
Adrian Bunk5bba1712006-06-29 13:02:35 -0700574static void skb_release_data(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575{
Eric Dumazetff04a772014-09-23 18:39:30 -0700576 struct skb_shared_info *shinfo = skb_shinfo(skb);
577 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
Eric Dumazetff04a772014-09-23 18:39:30 -0700579 if (skb->cloned &&
580 atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
581 &shinfo->dataref))
582 return;
Shirley Maa6686f22011-07-06 12:22:12 +0000583
Eric Dumazetff04a772014-09-23 18:39:30 -0700584 for (i = 0; i < shinfo->nr_frags; i++)
585 __skb_frag_unref(&shinfo->frags[i]);
Shirley Maa6686f22011-07-06 12:22:12 +0000586
Eric Dumazetff04a772014-09-23 18:39:30 -0700587 /*
588 * If skb buf is from userspace, we need to notify the caller
589 * the lower device DMA has done;
590 */
591 if (shinfo->tx_flags & SKBTX_DEV_ZEROCOPY) {
592 struct ubuf_info *uarg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
Eric Dumazetff04a772014-09-23 18:39:30 -0700594 uarg = shinfo->destructor_arg;
595 if (uarg->callback)
596 uarg->callback(uarg, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 }
Eric Dumazetff04a772014-09-23 18:39:30 -0700598
599 if (shinfo->frag_list)
600 kfree_skb_list(shinfo->frag_list);
601
602 skb_free_head(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603}
604
605/*
606 * Free an skbuff by memory without cleaning the state.
607 */
Herbert Xu2d4baff2007-11-26 23:11:19 +0800608static void kfree_skbmem(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609{
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700610 struct sk_buff_fclones *fclones;
David S. Millerd179cd12005-08-17 14:57:30 -0700611
David S. Millerd179cd12005-08-17 14:57:30 -0700612 switch (skb->fclone) {
613 case SKB_FCLONE_UNAVAILABLE:
614 kmem_cache_free(skbuff_head_cache, skb);
Eric Dumazet6ffe75e2014-12-03 17:04:39 -0800615 return;
David S. Millerd179cd12005-08-17 14:57:30 -0700616
617 case SKB_FCLONE_ORIG:
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700618 fclones = container_of(skb, struct sk_buff_fclones, skb1);
Eric Dumazet6ffe75e2014-12-03 17:04:39 -0800619
620 /* We usually free the clone (TX completion) before original skb
621 * This test would have no chance to be true for the clone,
622 * while here, branch prediction will be good.
623 */
624 if (atomic_read(&fclones->fclone_ref) == 1)
625 goto fastpath;
David S. Millerd179cd12005-08-17 14:57:30 -0700626 break;
627
Eric Dumazet6ffe75e2014-12-03 17:04:39 -0800628 default: /* SKB_FCLONE_CLONE */
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700629 fclones = container_of(skb, struct sk_buff_fclones, skb2);
David S. Millerd179cd12005-08-17 14:57:30 -0700630 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700631 }
Eric Dumazet6ffe75e2014-12-03 17:04:39 -0800632 if (!atomic_dec_and_test(&fclones->fclone_ref))
633 return;
634fastpath:
635 kmem_cache_free(skbuff_fclone_cache, fclones);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636}
637
Lennert Buytenhek04a4bb52008-10-01 02:33:12 -0700638static void skb_release_head_state(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639{
Eric Dumazetadf30902009-06-02 05:19:30 +0000640 skb_dst_drop(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641#ifdef CONFIG_XFRM
642 secpath_put(skb->sp);
643#endif
Stephen Hemminger9c2b3322005-04-19 22:39:42 -0700644 if (skb->destructor) {
645 WARN_ON(in_irq());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 skb->destructor(skb);
647 }
Igor Maravića3bf7ae2011-12-12 02:58:22 +0000648#if IS_ENABLED(CONFIG_NF_CONNTRACK)
Yasuyuki Kozakai5f79e0f2007-03-23 11:17:07 -0700649 nf_conntrack_put(skb->nfct);
KOVACS Krisztian2fc72c72011-01-12 20:25:08 +0100650#endif
Pablo Neira Ayuso1109a902014-10-01 11:19:17 +0200651#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 nf_bridge_put(skb->nf_bridge);
653#endif
Lennert Buytenhek04a4bb52008-10-01 02:33:12 -0700654}
655
656/* Free everything but the sk_buff shell. */
657static void skb_release_all(struct sk_buff *skb)
658{
659 skb_release_head_state(skb);
Pablo Neira5e71d9d2013-06-03 09:28:43 +0000660 if (likely(skb->head))
Patrick McHardy0ebd0ac2013-04-17 06:46:58 +0000661 skb_release_data(skb);
Herbert Xu2d4baff2007-11-26 23:11:19 +0800662}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
Herbert Xu2d4baff2007-11-26 23:11:19 +0800664/**
665 * __kfree_skb - private function
666 * @skb: buffer
667 *
668 * Free an sk_buff. Release anything attached to the buffer.
669 * Clean the state. This is an internal helper function. Users should
670 * always call kfree_skb
671 */
672
673void __kfree_skb(struct sk_buff *skb)
674{
675 skb_release_all(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 kfree_skbmem(skb);
677}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800678EXPORT_SYMBOL(__kfree_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
680/**
Jörn Engel231d06a2006-03-20 21:28:35 -0800681 * kfree_skb - free an sk_buff
682 * @skb: buffer to free
683 *
684 * Drop a reference to the buffer and free it if the usage count has
685 * hit zero.
686 */
687void kfree_skb(struct sk_buff *skb)
688{
689 if (unlikely(!skb))
690 return;
691 if (likely(atomic_read(&skb->users) == 1))
692 smp_rmb();
693 else if (likely(!atomic_dec_and_test(&skb->users)))
694 return;
Neil Hormanead2ceb2009-03-11 09:49:55 +0000695 trace_kfree_skb(skb, __builtin_return_address(0));
Jörn Engel231d06a2006-03-20 21:28:35 -0800696 __kfree_skb(skb);
697}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800698EXPORT_SYMBOL(kfree_skb);
Jörn Engel231d06a2006-03-20 21:28:35 -0800699
Eric Dumazetbd8a7032013-06-24 06:26:00 -0700700void kfree_skb_list(struct sk_buff *segs)
701{
702 while (segs) {
703 struct sk_buff *next = segs->next;
704
705 kfree_skb(segs);
706 segs = next;
707 }
708}
709EXPORT_SYMBOL(kfree_skb_list);
710
Stephen Hemmingerd1a203e2008-11-01 21:01:09 -0700711/**
Michael S. Tsirkin25121172012-11-01 09:16:28 +0000712 * skb_tx_error - report an sk_buff xmit error
713 * @skb: buffer that triggered an error
714 *
715 * Report xmit error if a device callback is tracking this skb.
716 * skb must be freed afterwards.
717 */
718void skb_tx_error(struct sk_buff *skb)
719{
720 if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) {
721 struct ubuf_info *uarg;
722
723 uarg = skb_shinfo(skb)->destructor_arg;
724 if (uarg->callback)
725 uarg->callback(uarg, false);
726 skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
727 }
728}
729EXPORT_SYMBOL(skb_tx_error);
730
731/**
Neil Hormanead2ceb2009-03-11 09:49:55 +0000732 * consume_skb - free an skbuff
733 * @skb: buffer to free
734 *
735 * Drop a ref to the buffer and free it if the usage count has hit zero
736 * Functions identically to kfree_skb, but kfree_skb assumes that the frame
737 * is being dropped after a failure and notes that
738 */
739void consume_skb(struct sk_buff *skb)
740{
741 if (unlikely(!skb))
742 return;
743 if (likely(atomic_read(&skb->users) == 1))
744 smp_rmb();
745 else if (likely(!atomic_dec_and_test(&skb->users)))
746 return;
Koki Sanagi07dc22e2010-08-23 18:46:12 +0900747 trace_consume_skb(skb);
Neil Hormanead2ceb2009-03-11 09:49:55 +0000748 __kfree_skb(skb);
749}
750EXPORT_SYMBOL(consume_skb);
751
Eric Dumazetb1937222014-09-28 22:18:47 -0700752/* Make sure a field is enclosed inside headers_start/headers_end section */
753#define CHECK_SKB_FIELD(field) \
754 BUILD_BUG_ON(offsetof(struct sk_buff, field) < \
755 offsetof(struct sk_buff, headers_start)); \
756 BUILD_BUG_ON(offsetof(struct sk_buff, field) > \
757 offsetof(struct sk_buff, headers_end)); \
758
Herbert Xudec18812007-10-14 00:37:30 -0700759static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
760{
761 new->tstamp = old->tstamp;
Eric Dumazetb1937222014-09-28 22:18:47 -0700762 /* We do not copy old->sk */
Herbert Xudec18812007-10-14 00:37:30 -0700763 new->dev = old->dev;
Eric Dumazetb1937222014-09-28 22:18:47 -0700764 memcpy(new->cb, old->cb, sizeof(old->cb));
Eric Dumazet7fee2262010-05-11 23:19:48 +0000765 skb_dst_copy(new, old);
Alexey Dobriyandef8b4f2008-10-28 13:24:06 -0700766#ifdef CONFIG_XFRM
Herbert Xudec18812007-10-14 00:37:30 -0700767 new->sp = secpath_get(old->sp);
768#endif
Eric Dumazetb1937222014-09-28 22:18:47 -0700769 __nf_copy(new, old, false);
Patrick McHardy6aa895b2008-07-14 22:49:06 -0700770
Eric Dumazetb1937222014-09-28 22:18:47 -0700771 /* Note : this field could be in headers_start/headers_end section
772 * It is not yet because we do not want to have a 16 bit hole
773 */
774 new->queue_mapping = old->queue_mapping;
Eliezer Tamir06021292013-06-10 11:39:50 +0300775
Eric Dumazetb1937222014-09-28 22:18:47 -0700776 memcpy(&new->headers_start, &old->headers_start,
777 offsetof(struct sk_buff, headers_end) -
778 offsetof(struct sk_buff, headers_start));
779 CHECK_SKB_FIELD(protocol);
780 CHECK_SKB_FIELD(csum);
781 CHECK_SKB_FIELD(hash);
782 CHECK_SKB_FIELD(priority);
783 CHECK_SKB_FIELD(skb_iif);
784 CHECK_SKB_FIELD(vlan_proto);
785 CHECK_SKB_FIELD(vlan_tci);
786 CHECK_SKB_FIELD(transport_header);
787 CHECK_SKB_FIELD(network_header);
788 CHECK_SKB_FIELD(mac_header);
789 CHECK_SKB_FIELD(inner_protocol);
790 CHECK_SKB_FIELD(inner_transport_header);
791 CHECK_SKB_FIELD(inner_network_header);
792 CHECK_SKB_FIELD(inner_mac_header);
793 CHECK_SKB_FIELD(mark);
794#ifdef CONFIG_NETWORK_SECMARK
795 CHECK_SKB_FIELD(secmark);
796#endif
Cong Wange0d10952013-08-01 11:10:25 +0800797#ifdef CONFIG_NET_RX_BUSY_POLL
Eric Dumazetb1937222014-09-28 22:18:47 -0700798 CHECK_SKB_FIELD(napi_id);
Eliezer Tamir06021292013-06-10 11:39:50 +0300799#endif
Eric Dumazet2bd82482015-02-03 23:48:24 -0800800#ifdef CONFIG_XPS
801 CHECK_SKB_FIELD(sender_cpu);
802#endif
Eric Dumazetb1937222014-09-28 22:18:47 -0700803#ifdef CONFIG_NET_SCHED
804 CHECK_SKB_FIELD(tc_index);
805#ifdef CONFIG_NET_CLS_ACT
806 CHECK_SKB_FIELD(tc_verd);
807#endif
808#endif
809
Herbert Xudec18812007-10-14 00:37:30 -0700810}
811
Herbert Xu82c49a32009-05-22 22:11:37 +0000812/*
813 * You should not add any new code to this function. Add it to
814 * __copy_skb_header above instead.
815 */
Herbert Xue0053ec2007-10-14 00:37:52 -0700816static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818#define C(x) n->x = skb->x
819
820 n->next = n->prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 n->sk = NULL;
Herbert Xudec18812007-10-14 00:37:30 -0700822 __copy_skb_header(n, skb);
823
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 C(len);
825 C(data_len);
Alexey Dobriyan3e6b3b22007-03-16 15:00:46 -0700826 C(mac_len);
Patrick McHardy334a8132007-06-25 04:35:20 -0700827 n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
Paul Moore02f1c892008-01-07 21:56:41 -0800828 n->cloned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 n->nohdr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 n->destructor = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 C(tail);
832 C(end);
Paul Moore02f1c892008-01-07 21:56:41 -0800833 C(head);
Eric Dumazetd3836f22012-04-27 00:33:38 +0000834 C(head_frag);
Paul Moore02f1c892008-01-07 21:56:41 -0800835 C(data);
836 C(truesize);
837 atomic_set(&n->users, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
839 atomic_inc(&(skb_shinfo(skb)->dataref));
840 skb->cloned = 1;
841
842 return n;
Herbert Xue0053ec2007-10-14 00:37:52 -0700843#undef C
844}
845
846/**
847 * skb_morph - morph one skb into another
848 * @dst: the skb to receive the contents
849 * @src: the skb to supply the contents
850 *
851 * This is identical to skb_clone except that the target skb is
852 * supplied by the user.
853 *
854 * The target skb is returned upon exit.
855 */
856struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
857{
Herbert Xu2d4baff2007-11-26 23:11:19 +0800858 skb_release_all(dst);
Herbert Xue0053ec2007-10-14 00:37:52 -0700859 return __skb_clone(dst, src);
860}
861EXPORT_SYMBOL_GPL(skb_morph);
862
Ben Hutchings2c530402012-07-10 10:55:09 +0000863/**
864 * skb_copy_ubufs - copy userspace skb frags buffers to kernel
Michael S. Tsirkin48c83012011-08-31 08:03:29 +0000865 * @skb: the skb to modify
866 * @gfp_mask: allocation priority
867 *
868 * This must be called on SKBTX_DEV_ZEROCOPY skb.
869 * It will copy all frags into kernel and drop the reference
870 * to userspace pages.
871 *
872 * If this function is called from an interrupt gfp_mask() must be
873 * %GFP_ATOMIC.
874 *
875 * Returns 0 on success or a negative error code on failure
876 * to allocate kernel memory to copy to.
877 */
878int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask)
Shirley Maa6686f22011-07-06 12:22:12 +0000879{
880 int i;
881 int num_frags = skb_shinfo(skb)->nr_frags;
882 struct page *page, *head = NULL;
883 struct ubuf_info *uarg = skb_shinfo(skb)->destructor_arg;
884
885 for (i = 0; i < num_frags; i++) {
886 u8 *vaddr;
887 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
888
Krishna Kumar02756ed2012-07-17 02:05:29 +0000889 page = alloc_page(gfp_mask);
Shirley Maa6686f22011-07-06 12:22:12 +0000890 if (!page) {
891 while (head) {
Sunghan Suh40dadff2013-07-12 16:17:23 +0900892 struct page *next = (struct page *)page_private(head);
Shirley Maa6686f22011-07-06 12:22:12 +0000893 put_page(head);
894 head = next;
895 }
896 return -ENOMEM;
897 }
Eric Dumazet51c56b02012-04-05 11:35:15 +0200898 vaddr = kmap_atomic(skb_frag_page(f));
Shirley Maa6686f22011-07-06 12:22:12 +0000899 memcpy(page_address(page),
Eric Dumazet9e903e02011-10-18 21:00:24 +0000900 vaddr + f->page_offset, skb_frag_size(f));
Eric Dumazet51c56b02012-04-05 11:35:15 +0200901 kunmap_atomic(vaddr);
Sunghan Suh40dadff2013-07-12 16:17:23 +0900902 set_page_private(page, (unsigned long)head);
Shirley Maa6686f22011-07-06 12:22:12 +0000903 head = page;
904 }
905
906 /* skb frags release userspace buffers */
Krishna Kumar02756ed2012-07-17 02:05:29 +0000907 for (i = 0; i < num_frags; i++)
Ian Campbella8605c62011-10-19 23:01:49 +0000908 skb_frag_unref(skb, i);
Shirley Maa6686f22011-07-06 12:22:12 +0000909
Michael S. Tsirkine19d6762012-11-01 09:16:22 +0000910 uarg->callback(uarg, false);
Shirley Maa6686f22011-07-06 12:22:12 +0000911
912 /* skb frags point to kernel buffers */
Krishna Kumar02756ed2012-07-17 02:05:29 +0000913 for (i = num_frags - 1; i >= 0; i--) {
914 __skb_fill_page_desc(skb, i, head, 0,
915 skb_shinfo(skb)->frags[i].size);
Sunghan Suh40dadff2013-07-12 16:17:23 +0900916 head = (struct page *)page_private(head);
Shirley Maa6686f22011-07-06 12:22:12 +0000917 }
Michael S. Tsirkin48c83012011-08-31 08:03:29 +0000918
919 skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
Shirley Maa6686f22011-07-06 12:22:12 +0000920 return 0;
921}
Michael S. Tsirkindcc0fb72012-07-20 09:23:20 +0000922EXPORT_SYMBOL_GPL(skb_copy_ubufs);
Shirley Maa6686f22011-07-06 12:22:12 +0000923
Herbert Xue0053ec2007-10-14 00:37:52 -0700924/**
925 * skb_clone - duplicate an sk_buff
926 * @skb: buffer to clone
927 * @gfp_mask: allocation priority
928 *
929 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
930 * copies share the same packet data but not structure. The new
931 * buffer has a reference count of 1. If the allocation fails the
932 * function returns %NULL otherwise the new buffer is returned.
933 *
934 * If this function is called from an interrupt gfp_mask() must be
935 * %GFP_ATOMIC.
936 */
937
938struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
939{
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700940 struct sk_buff_fclones *fclones = container_of(skb,
941 struct sk_buff_fclones,
942 skb1);
Eric Dumazet6ffe75e2014-12-03 17:04:39 -0800943 struct sk_buff *n;
Herbert Xue0053ec2007-10-14 00:37:52 -0700944
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +0000945 if (skb_orphan_frags(skb, gfp_mask))
946 return NULL;
Shirley Maa6686f22011-07-06 12:22:12 +0000947
Herbert Xue0053ec2007-10-14 00:37:52 -0700948 if (skb->fclone == SKB_FCLONE_ORIG &&
Eric Dumazet6ffe75e2014-12-03 17:04:39 -0800949 atomic_read(&fclones->fclone_ref) == 1) {
950 n = &fclones->skb2;
951 atomic_set(&fclones->fclone_ref, 2);
Herbert Xue0053ec2007-10-14 00:37:52 -0700952 } else {
Mel Gormanc93bdd02012-07-31 16:44:19 -0700953 if (skb_pfmemalloc(skb))
954 gfp_mask |= __GFP_MEMALLOC;
955
Herbert Xue0053ec2007-10-14 00:37:52 -0700956 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
957 if (!n)
958 return NULL;
Vegard Nossumfe55f6d2008-08-30 12:16:35 +0200959
960 kmemcheck_annotate_bitfield(n, flags1);
Herbert Xue0053ec2007-10-14 00:37:52 -0700961 n->fclone = SKB_FCLONE_UNAVAILABLE;
962 }
963
964 return __skb_clone(n, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800966EXPORT_SYMBOL(skb_clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
Pravin B Shelarf5b17292013-03-07 13:21:40 +0000968static void skb_headers_offset_update(struct sk_buff *skb, int off)
969{
Eric Dumazet030737b2013-10-19 11:42:54 -0700970 /* Only adjust this if it actually is csum_start rather than csum */
971 if (skb->ip_summed == CHECKSUM_PARTIAL)
972 skb->csum_start += off;
Pravin B Shelarf5b17292013-03-07 13:21:40 +0000973 /* {transport,network,mac}_header and tail are relative to skb->head */
974 skb->transport_header += off;
975 skb->network_header += off;
976 if (skb_mac_header_was_set(skb))
977 skb->mac_header += off;
978 skb->inner_transport_header += off;
979 skb->inner_network_header += off;
Pravin B Shelaraefbd2b2013-03-07 13:21:46 +0000980 skb->inner_mac_header += off;
Pravin B Shelarf5b17292013-03-07 13:21:40 +0000981}
982
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
984{
Herbert Xudec18812007-10-14 00:37:30 -0700985 __copy_skb_header(new, old);
986
Herbert Xu79671682006-06-22 02:40:14 -0700987 skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
988 skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
989 skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990}
991
Mel Gormanc93bdd02012-07-31 16:44:19 -0700992static inline int skb_alloc_rx_flag(const struct sk_buff *skb)
993{
994 if (skb_pfmemalloc(skb))
995 return SKB_ALLOC_RX;
996 return 0;
997}
998
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999/**
1000 * skb_copy - create private copy of an sk_buff
1001 * @skb: buffer to copy
1002 * @gfp_mask: allocation priority
1003 *
1004 * Make a copy of both an &sk_buff and its data. This is used when the
1005 * caller wishes to modify the data and needs a private copy of the
1006 * data to alter. Returns %NULL on failure or the pointer to the buffer
1007 * on success. The returned buffer has a reference count of 1.
1008 *
1009 * As by-product this function converts non-linear &sk_buff to linear
1010 * one, so that &sk_buff becomes completely private and caller is allowed
1011 * to modify all the data of returned buffer. This means that this
1012 * function is not recommended for use in circumstances when only
1013 * header is going to be modified. Use pskb_copy() instead.
1014 */
1015
Al Virodd0fc662005-10-07 07:46:04 +01001016struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017{
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001018 int headerlen = skb_headroom(skb);
Alexander Duyckec47ea82012-05-04 14:26:56 +00001019 unsigned int size = skb_end_offset(skb) + skb->data_len;
Mel Gormanc93bdd02012-07-31 16:44:19 -07001020 struct sk_buff *n = __alloc_skb(size, gfp_mask,
1021 skb_alloc_rx_flag(skb), NUMA_NO_NODE);
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001022
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 if (!n)
1024 return NULL;
1025
1026 /* Set the data pointer */
1027 skb_reserve(n, headerlen);
1028 /* Set the tail pointer and length */
1029 skb_put(n, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
1031 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
1032 BUG();
1033
1034 copy_skb_header(n, skb);
1035 return n;
1036}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001037EXPORT_SYMBOL(skb_copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
1039/**
Octavian Purdilabad93e92014-06-12 01:36:26 +03001040 * __pskb_copy_fclone - create copy of an sk_buff with private head.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 * @skb: buffer to copy
Eric Dumazet117632e2011-12-03 21:39:53 +00001042 * @headroom: headroom of new skb
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 * @gfp_mask: allocation priority
Octavian Purdilabad93e92014-06-12 01:36:26 +03001044 * @fclone: if true allocate the copy of the skb from the fclone
1045 * cache instead of the head cache; it is recommended to set this
1046 * to true for the cases where the copy will likely be cloned
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 *
1048 * Make a copy of both an &sk_buff and part of its data, located
1049 * in header. Fragmented data remain shared. This is used when
1050 * the caller wishes to modify only header of &sk_buff and needs
1051 * private copy of the header to alter. Returns %NULL on failure
1052 * or the pointer to the buffer on success.
1053 * The returned buffer has a reference count of 1.
1054 */
1055
Octavian Purdilabad93e92014-06-12 01:36:26 +03001056struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom,
1057 gfp_t gfp_mask, bool fclone)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058{
Eric Dumazet117632e2011-12-03 21:39:53 +00001059 unsigned int size = skb_headlen(skb) + headroom;
Octavian Purdilabad93e92014-06-12 01:36:26 +03001060 int flags = skb_alloc_rx_flag(skb) | (fclone ? SKB_ALLOC_FCLONE : 0);
1061 struct sk_buff *n = __alloc_skb(size, gfp_mask, flags, NUMA_NO_NODE);
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001062
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 if (!n)
1064 goto out;
1065
1066 /* Set the data pointer */
Eric Dumazet117632e2011-12-03 21:39:53 +00001067 skb_reserve(n, headroom);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 /* Set the tail pointer and length */
1069 skb_put(n, skb_headlen(skb));
1070 /* Copy the bytes */
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001071 skb_copy_from_linear_data(skb, n->data, n->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
Herbert Xu25f484a2006-11-07 14:57:15 -08001073 n->truesize += skb->data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 n->data_len = skb->data_len;
1075 n->len = skb->len;
1076
1077 if (skb_shinfo(skb)->nr_frags) {
1078 int i;
1079
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +00001080 if (skb_orphan_frags(skb, gfp_mask)) {
1081 kfree_skb(n);
1082 n = NULL;
1083 goto out;
Shirley Maa6686f22011-07-06 12:22:12 +00001084 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1086 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
Ian Campbellea2ab692011-08-22 23:44:58 +00001087 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 }
1089 skb_shinfo(n)->nr_frags = i;
1090 }
1091
David S. Miller21dc3302010-08-23 00:13:46 -07001092 if (skb_has_frag_list(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
1094 skb_clone_fraglist(n);
1095 }
1096
1097 copy_skb_header(n, skb);
1098out:
1099 return n;
1100}
Octavian Purdilabad93e92014-06-12 01:36:26 +03001101EXPORT_SYMBOL(__pskb_copy_fclone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
1103/**
1104 * pskb_expand_head - reallocate header of &sk_buff
1105 * @skb: buffer to reallocate
1106 * @nhead: room to add at head
1107 * @ntail: room to add at tail
1108 * @gfp_mask: allocation priority
1109 *
Mathias Krausebc323832013-11-07 14:18:26 +01001110 * Expands (or creates identical copy, if @nhead and @ntail are zero)
1111 * header of @skb. &sk_buff itself is not changed. &sk_buff MUST have
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 * reference count of 1. Returns zero in the case of success or error,
1113 * if expansion failed. In the last case, &sk_buff is not changed.
1114 *
1115 * All the pointers pointing into skb header may change and must be
1116 * reloaded after call to this function.
1117 */
1118
Victor Fusco86a76ca2005-07-08 14:57:47 -07001119int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
Al Virodd0fc662005-10-07 07:46:04 +01001120 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121{
1122 int i;
1123 u8 *data;
Alexander Duyckec47ea82012-05-04 14:26:56 +00001124 int size = nhead + skb_end_offset(skb) + ntail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 long off;
1126
Herbert Xu4edd87a2008-10-01 07:09:38 -07001127 BUG_ON(nhead < 0);
1128
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 if (skb_shared(skb))
1130 BUG();
1131
1132 size = SKB_DATA_ALIGN(size);
1133
Mel Gormanc93bdd02012-07-31 16:44:19 -07001134 if (skb_pfmemalloc(skb))
1135 gfp_mask |= __GFP_MEMALLOC;
1136 data = kmalloc_reserve(size + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
1137 gfp_mask, NUMA_NO_NODE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 if (!data)
1139 goto nodata;
Eric Dumazet87151b82012-04-10 20:08:39 +00001140 size = SKB_WITH_OVERHEAD(ksize(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
1142 /* Copy only real data... and, alas, header. This should be
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001143 * optimized for the cases when header is void.
1144 */
1145 memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head);
1146
1147 memcpy((struct skb_shared_info *)(data + size),
1148 skb_shinfo(skb),
Eric Dumazetfed66382010-07-22 19:09:08 +00001149 offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
Alexander Duyck3e245912012-05-04 14:26:51 +00001151 /*
1152 * if shinfo is shared we must drop the old head gracefully, but if it
1153 * is not we can just drop the old head and let the existing refcount
1154 * be since all we did is relocate the values
1155 */
1156 if (skb_cloned(skb)) {
Shirley Maa6686f22011-07-06 12:22:12 +00001157 /* copy this zero copy skb frags */
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +00001158 if (skb_orphan_frags(skb, gfp_mask))
1159 goto nofrags;
Eric Dumazet1fd63042010-09-02 23:09:32 +00001160 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
Ian Campbellea2ab692011-08-22 23:44:58 +00001161 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
Eric Dumazet1fd63042010-09-02 23:09:32 +00001163 if (skb_has_frag_list(skb))
1164 skb_clone_fraglist(skb);
1165
1166 skb_release_data(skb);
Alexander Duyck3e245912012-05-04 14:26:51 +00001167 } else {
1168 skb_free_head(skb);
Eric Dumazet1fd63042010-09-02 23:09:32 +00001169 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 off = (data + nhead) - skb->head;
1171
1172 skb->head = data;
Eric Dumazetd3836f22012-04-27 00:33:38 +00001173 skb->head_frag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 skb->data += off;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001175#ifdef NET_SKBUFF_DATA_USES_OFFSET
1176 skb->end = size;
Patrick McHardy56eb8882007-04-09 11:45:04 -07001177 off = nhead;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001178#else
1179 skb->end = skb->head + size;
Patrick McHardy56eb8882007-04-09 11:45:04 -07001180#endif
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001181 skb->tail += off;
Peter Pan(潘卫平)b41abb42013-06-06 21:27:21 +08001182 skb_headers_offset_update(skb, nhead);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 skb->cloned = 0;
Patrick McHardy334a8132007-06-25 04:35:20 -07001184 skb->hdr_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 skb->nohdr = 0;
1186 atomic_set(&skb_shinfo(skb)->dataref, 1);
1187 return 0;
1188
Shirley Maa6686f22011-07-06 12:22:12 +00001189nofrags:
1190 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191nodata:
1192 return -ENOMEM;
1193}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001194EXPORT_SYMBOL(pskb_expand_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195
1196/* Make private copy of skb with writable head and some headroom */
1197
1198struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
1199{
1200 struct sk_buff *skb2;
1201 int delta = headroom - skb_headroom(skb);
1202
1203 if (delta <= 0)
1204 skb2 = pskb_copy(skb, GFP_ATOMIC);
1205 else {
1206 skb2 = skb_clone(skb, GFP_ATOMIC);
1207 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
1208 GFP_ATOMIC)) {
1209 kfree_skb(skb2);
1210 skb2 = NULL;
1211 }
1212 }
1213 return skb2;
1214}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001215EXPORT_SYMBOL(skb_realloc_headroom);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
1217/**
1218 * skb_copy_expand - copy and expand sk_buff
1219 * @skb: buffer to copy
1220 * @newheadroom: new free bytes at head
1221 * @newtailroom: new free bytes at tail
1222 * @gfp_mask: allocation priority
1223 *
1224 * Make a copy of both an &sk_buff and its data and while doing so
1225 * allocate additional space.
1226 *
1227 * This is used when the caller wishes to modify the data and needs a
1228 * private copy of the data to alter as well as more space for new fields.
1229 * Returns %NULL on failure or the pointer to the buffer
1230 * on success. The returned buffer has a reference count of 1.
1231 *
1232 * You must pass %GFP_ATOMIC as the allocation priority if this function
1233 * is called from an interrupt.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 */
1235struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
Victor Fusco86a76ca2005-07-08 14:57:47 -07001236 int newheadroom, int newtailroom,
Al Virodd0fc662005-10-07 07:46:04 +01001237 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238{
1239 /*
1240 * Allocate the copy buffer
1241 */
Mel Gormanc93bdd02012-07-31 16:44:19 -07001242 struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom,
1243 gfp_mask, skb_alloc_rx_flag(skb),
1244 NUMA_NO_NODE);
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001245 int oldheadroom = skb_headroom(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 int head_copy_len, head_copy_off;
1247
1248 if (!n)
1249 return NULL;
1250
1251 skb_reserve(n, newheadroom);
1252
1253 /* Set the tail pointer and length */
1254 skb_put(n, skb->len);
1255
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001256 head_copy_len = oldheadroom;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 head_copy_off = 0;
1258 if (newheadroom <= head_copy_len)
1259 head_copy_len = newheadroom;
1260 else
1261 head_copy_off = newheadroom - head_copy_len;
1262
1263 /* Copy the linear header and data. */
1264 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
1265 skb->len + head_copy_len))
1266 BUG();
1267
1268 copy_skb_header(n, skb);
1269
Eric Dumazet030737b2013-10-19 11:42:54 -07001270 skb_headers_offset_update(n, newheadroom - oldheadroom);
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001271
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 return n;
1273}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001274EXPORT_SYMBOL(skb_copy_expand);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
1276/**
1277 * skb_pad - zero pad the tail of an skb
1278 * @skb: buffer to pad
1279 * @pad: space to pad
1280 *
1281 * Ensure that a buffer is followed by a padding area that is zero
1282 * filled. Used by network drivers which may DMA or transfer data
1283 * beyond the buffer end onto the wire.
1284 *
Herbert Xu5b057c62006-06-23 02:06:41 -07001285 * May return error in out of memory cases. The skb is freed on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001287
Herbert Xu5b057c62006-06-23 02:06:41 -07001288int skb_pad(struct sk_buff *skb, int pad)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289{
Herbert Xu5b057c62006-06-23 02:06:41 -07001290 int err;
1291 int ntail;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001292
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 /* If the skbuff is non linear tailroom is always zero.. */
Herbert Xu5b057c62006-06-23 02:06:41 -07001294 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 memset(skb->data+skb->len, 0, pad);
Herbert Xu5b057c62006-06-23 02:06:41 -07001296 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 }
Herbert Xu5b057c62006-06-23 02:06:41 -07001298
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001299 ntail = skb->data_len + pad - (skb->end - skb->tail);
Herbert Xu5b057c62006-06-23 02:06:41 -07001300 if (likely(skb_cloned(skb) || ntail > 0)) {
1301 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
1302 if (unlikely(err))
1303 goto free_skb;
1304 }
1305
1306 /* FIXME: The use of this function with non-linear skb's really needs
1307 * to be audited.
1308 */
1309 err = skb_linearize(skb);
1310 if (unlikely(err))
1311 goto free_skb;
1312
1313 memset(skb->data + skb->len, 0, pad);
1314 return 0;
1315
1316free_skb:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 kfree_skb(skb);
Herbert Xu5b057c62006-06-23 02:06:41 -07001318 return err;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001319}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001320EXPORT_SYMBOL(skb_pad);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001321
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -07001322/**
Mathias Krause0c7ddf32013-11-07 14:18:24 +01001323 * pskb_put - add data to the tail of a potentially fragmented buffer
1324 * @skb: start of the buffer to use
1325 * @tail: tail fragment of the buffer to use
1326 * @len: amount of data to add
1327 *
1328 * This function extends the used data area of the potentially
1329 * fragmented buffer. @tail must be the last fragment of @skb -- or
1330 * @skb itself. If this would exceed the total buffer size the kernel
1331 * will panic. A pointer to the first byte of the extra data is
1332 * returned.
1333 */
1334
1335unsigned char *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len)
1336{
1337 if (tail != skb) {
1338 skb->data_len += len;
1339 skb->len += len;
1340 }
1341 return skb_put(tail, len);
1342}
1343EXPORT_SYMBOL_GPL(pskb_put);
1344
1345/**
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -07001346 * skb_put - add data to a buffer
1347 * @skb: buffer to use
1348 * @len: amount of data to add
1349 *
1350 * This function extends the used data area of the buffer. If this would
1351 * exceed the total buffer size the kernel will panic. A pointer to the
1352 * first byte of the extra data is returned.
1353 */
1354unsigned char *skb_put(struct sk_buff *skb, unsigned int len)
1355{
1356 unsigned char *tmp = skb_tail_pointer(skb);
1357 SKB_LINEAR_ASSERT(skb);
1358 skb->tail += len;
1359 skb->len += len;
1360 if (unlikely(skb->tail > skb->end))
1361 skb_over_panic(skb, len, __builtin_return_address(0));
1362 return tmp;
1363}
1364EXPORT_SYMBOL(skb_put);
1365
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001366/**
Ilpo Järvinenc2aa2702008-03-27 17:52:40 -07001367 * skb_push - add data to the start of a buffer
1368 * @skb: buffer to use
1369 * @len: amount of data to add
1370 *
1371 * This function extends the used data area of the buffer at the buffer
1372 * start. If this would exceed the total buffer headroom the kernel will
1373 * panic. A pointer to the first byte of the extra data is returned.
1374 */
1375unsigned char *skb_push(struct sk_buff *skb, unsigned int len)
1376{
1377 skb->data -= len;
1378 skb->len += len;
1379 if (unlikely(skb->data<skb->head))
1380 skb_under_panic(skb, len, __builtin_return_address(0));
1381 return skb->data;
1382}
1383EXPORT_SYMBOL(skb_push);
1384
1385/**
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001386 * skb_pull - remove data from the start of a buffer
1387 * @skb: buffer to use
1388 * @len: amount of data to remove
1389 *
1390 * This function removes data from the start of a buffer, returning
1391 * the memory to the headroom. A pointer to the next data in the buffer
1392 * is returned. Once the data has been pulled future pushes will overwrite
1393 * the old data.
1394 */
1395unsigned char *skb_pull(struct sk_buff *skb, unsigned int len)
1396{
David S. Miller47d29642010-05-02 02:21:44 -07001397 return skb_pull_inline(skb, len);
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001398}
1399EXPORT_SYMBOL(skb_pull);
1400
Ilpo Järvinen419ae742008-03-27 17:54:01 -07001401/**
1402 * skb_trim - remove end from a buffer
1403 * @skb: buffer to alter
1404 * @len: new length
1405 *
1406 * Cut the length of a buffer down by removing data from the tail. If
1407 * the buffer is already under the length specified it is not modified.
1408 * The skb must be linear.
1409 */
1410void skb_trim(struct sk_buff *skb, unsigned int len)
1411{
1412 if (skb->len > len)
1413 __skb_trim(skb, len);
1414}
1415EXPORT_SYMBOL(skb_trim);
1416
Herbert Xu3cc0e872006-06-09 16:13:38 -07001417/* Trims skb to length len. It can change skb pointers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 */
1419
Herbert Xu3cc0e872006-06-09 16:13:38 -07001420int ___pskb_trim(struct sk_buff *skb, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421{
Herbert Xu27b437c2006-07-13 19:26:39 -07001422 struct sk_buff **fragp;
1423 struct sk_buff *frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 int offset = skb_headlen(skb);
1425 int nfrags = skb_shinfo(skb)->nr_frags;
1426 int i;
Herbert Xu27b437c2006-07-13 19:26:39 -07001427 int err;
1428
1429 if (skb_cloned(skb) &&
1430 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
1431 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001433 i = 0;
1434 if (offset >= len)
1435 goto drop_pages;
1436
1437 for (; i < nfrags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001438 int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]);
Herbert Xu27b437c2006-07-13 19:26:39 -07001439
1440 if (end < len) {
1441 offset = end;
1442 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 }
Herbert Xu27b437c2006-07-13 19:26:39 -07001444
Eric Dumazet9e903e02011-10-18 21:00:24 +00001445 skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset);
Herbert Xu27b437c2006-07-13 19:26:39 -07001446
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001447drop_pages:
Herbert Xu27b437c2006-07-13 19:26:39 -07001448 skb_shinfo(skb)->nr_frags = i;
1449
1450 for (; i < nfrags; i++)
Ian Campbellea2ab692011-08-22 23:44:58 +00001451 skb_frag_unref(skb, i);
Herbert Xu27b437c2006-07-13 19:26:39 -07001452
David S. Miller21dc3302010-08-23 00:13:46 -07001453 if (skb_has_frag_list(skb))
Herbert Xu27b437c2006-07-13 19:26:39 -07001454 skb_drop_fraglist(skb);
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001455 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 }
1457
Herbert Xu27b437c2006-07-13 19:26:39 -07001458 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
1459 fragp = &frag->next) {
1460 int end = offset + frag->len;
1461
1462 if (skb_shared(frag)) {
1463 struct sk_buff *nfrag;
1464
1465 nfrag = skb_clone(frag, GFP_ATOMIC);
1466 if (unlikely(!nfrag))
1467 return -ENOMEM;
1468
1469 nfrag->next = frag->next;
Eric Dumazet85bb2a62012-04-19 02:24:53 +00001470 consume_skb(frag);
Herbert Xu27b437c2006-07-13 19:26:39 -07001471 frag = nfrag;
1472 *fragp = frag;
1473 }
1474
1475 if (end < len) {
1476 offset = end;
1477 continue;
1478 }
1479
1480 if (end > len &&
1481 unlikely((err = pskb_trim(frag, len - offset))))
1482 return err;
1483
1484 if (frag->next)
1485 skb_drop_list(&frag->next);
1486 break;
1487 }
1488
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001489done:
Herbert Xu27b437c2006-07-13 19:26:39 -07001490 if (len > skb_headlen(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 skb->data_len -= skb->len - len;
1492 skb->len = len;
1493 } else {
Herbert Xu27b437c2006-07-13 19:26:39 -07001494 skb->len = len;
1495 skb->data_len = 0;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001496 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 }
1498
1499 return 0;
1500}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001501EXPORT_SYMBOL(___pskb_trim);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502
1503/**
1504 * __pskb_pull_tail - advance tail of skb header
1505 * @skb: buffer to reallocate
1506 * @delta: number of bytes to advance tail
1507 *
1508 * The function makes a sense only on a fragmented &sk_buff,
1509 * it expands header moving its tail forward and copying necessary
1510 * data from fragmented part.
1511 *
1512 * &sk_buff MUST have reference count of 1.
1513 *
1514 * Returns %NULL (and &sk_buff does not change) if pull failed
1515 * or value of new tail of skb in the case of success.
1516 *
1517 * All the pointers pointing into skb header may change and must be
1518 * reloaded after call to this function.
1519 */
1520
1521/* Moves tail of skb head forward, copying data from fragmented part,
1522 * when it is necessary.
1523 * 1. It may fail due to malloc failure.
1524 * 2. It may change skb pointers.
1525 *
1526 * It is pretty complicated. Luckily, it is called only in exceptional cases.
1527 */
1528unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
1529{
1530 /* If skb has not enough free space at tail, get new one
1531 * plus 128 bytes for future expansions. If we have enough
1532 * room at tail, reallocate without expansion only if skb is cloned.
1533 */
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001534 int i, k, eat = (skb->tail + delta) - skb->end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535
1536 if (eat > 0 || skb_cloned(skb)) {
1537 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
1538 GFP_ATOMIC))
1539 return NULL;
1540 }
1541
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001542 if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 BUG();
1544
1545 /* Optimization: no fragments, no reasons to preestimate
1546 * size of pulled pages. Superb.
1547 */
David S. Miller21dc3302010-08-23 00:13:46 -07001548 if (!skb_has_frag_list(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 goto pull_pages;
1550
1551 /* Estimate size of pulled pages. */
1552 eat = delta;
1553 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001554 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1555
1556 if (size >= eat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 goto pull_pages;
Eric Dumazet9e903e02011-10-18 21:00:24 +00001558 eat -= size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 }
1560
1561 /* If we need update frag list, we are in troubles.
1562 * Certainly, it possible to add an offset to skb data,
1563 * but taking into account that pulling is expected to
1564 * be very rare operation, it is worth to fight against
1565 * further bloating skb head and crucify ourselves here instead.
1566 * Pure masohism, indeed. 8)8)
1567 */
1568 if (eat) {
1569 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1570 struct sk_buff *clone = NULL;
1571 struct sk_buff *insp = NULL;
1572
1573 do {
Kris Katterjohn09a62662006-01-08 22:24:28 -08001574 BUG_ON(!list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575
1576 if (list->len <= eat) {
1577 /* Eaten as whole. */
1578 eat -= list->len;
1579 list = list->next;
1580 insp = list;
1581 } else {
1582 /* Eaten partially. */
1583
1584 if (skb_shared(list)) {
1585 /* Sucks! We need to fork list. :-( */
1586 clone = skb_clone(list, GFP_ATOMIC);
1587 if (!clone)
1588 return NULL;
1589 insp = list->next;
1590 list = clone;
1591 } else {
1592 /* This may be pulled without
1593 * problems. */
1594 insp = list;
1595 }
1596 if (!pskb_pull(list, eat)) {
Wei Yongjunf3fbbe02009-02-25 00:37:32 +00001597 kfree_skb(clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 return NULL;
1599 }
1600 break;
1601 }
1602 } while (eat);
1603
1604 /* Free pulled out fragments. */
1605 while ((list = skb_shinfo(skb)->frag_list) != insp) {
1606 skb_shinfo(skb)->frag_list = list->next;
1607 kfree_skb(list);
1608 }
1609 /* And insert new clone at head. */
1610 if (clone) {
1611 clone->next = list;
1612 skb_shinfo(skb)->frag_list = clone;
1613 }
1614 }
1615 /* Success! Now we may commit changes to skb data. */
1616
1617pull_pages:
1618 eat = delta;
1619 k = 0;
1620 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001621 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1622
1623 if (size <= eat) {
Ian Campbellea2ab692011-08-22 23:44:58 +00001624 skb_frag_unref(skb, i);
Eric Dumazet9e903e02011-10-18 21:00:24 +00001625 eat -= size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 } else {
1627 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
1628 if (eat) {
1629 skb_shinfo(skb)->frags[k].page_offset += eat;
Eric Dumazet9e903e02011-10-18 21:00:24 +00001630 skb_frag_size_sub(&skb_shinfo(skb)->frags[k], eat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 eat = 0;
1632 }
1633 k++;
1634 }
1635 }
1636 skb_shinfo(skb)->nr_frags = k;
1637
1638 skb->tail += delta;
1639 skb->data_len -= delta;
1640
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001641 return skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001643EXPORT_SYMBOL(__pskb_pull_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644
Eric Dumazet22019b12011-07-29 18:37:31 +00001645/**
1646 * skb_copy_bits - copy bits from skb to kernel buffer
1647 * @skb: source skb
1648 * @offset: offset in source
1649 * @to: destination buffer
1650 * @len: number of bytes to copy
1651 *
1652 * Copy the specified number of bytes from the source skb to the
1653 * destination buffer.
1654 *
1655 * CAUTION ! :
1656 * If its prototype is ever changed,
1657 * check arch/{*}/net/{*}.S files,
1658 * since it is called from BPF assembly code.
1659 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
1661{
David S. Miller1a028e52007-04-27 15:21:23 -07001662 int start = skb_headlen(skb);
David S. Millerfbb398a2009-06-09 00:18:59 -07001663 struct sk_buff *frag_iter;
1664 int i, copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665
1666 if (offset > (int)skb->len - len)
1667 goto fault;
1668
1669 /* Copy header. */
David S. Miller1a028e52007-04-27 15:21:23 -07001670 if ((copy = start - offset) > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 if (copy > len)
1672 copy = len;
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001673 skb_copy_from_linear_data_offset(skb, offset, to, copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 if ((len -= copy) == 0)
1675 return 0;
1676 offset += copy;
1677 to += copy;
1678 }
1679
1680 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07001681 int end;
Eric Dumazet51c56b02012-04-05 11:35:15 +02001682 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001684 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07001685
Eric Dumazet51c56b02012-04-05 11:35:15 +02001686 end = start + skb_frag_size(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 if ((copy = end - offset) > 0) {
1688 u8 *vaddr;
1689
1690 if (copy > len)
1691 copy = len;
1692
Eric Dumazet51c56b02012-04-05 11:35:15 +02001693 vaddr = kmap_atomic(skb_frag_page(f));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 memcpy(to,
Eric Dumazet51c56b02012-04-05 11:35:15 +02001695 vaddr + f->page_offset + offset - start,
1696 copy);
1697 kunmap_atomic(vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698
1699 if ((len -= copy) == 0)
1700 return 0;
1701 offset += copy;
1702 to += copy;
1703 }
David S. Miller1a028e52007-04-27 15:21:23 -07001704 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 }
1706
David S. Millerfbb398a2009-06-09 00:18:59 -07001707 skb_walk_frags(skb, frag_iter) {
1708 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709
David S. Millerfbb398a2009-06-09 00:18:59 -07001710 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711
David S. Millerfbb398a2009-06-09 00:18:59 -07001712 end = start + frag_iter->len;
1713 if ((copy = end - offset) > 0) {
1714 if (copy > len)
1715 copy = len;
1716 if (skb_copy_bits(frag_iter, offset - start, to, copy))
1717 goto fault;
1718 if ((len -= copy) == 0)
1719 return 0;
1720 offset += copy;
1721 to += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 }
David S. Millerfbb398a2009-06-09 00:18:59 -07001723 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 }
Shirley Maa6686f22011-07-06 12:22:12 +00001725
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 if (!len)
1727 return 0;
1728
1729fault:
1730 return -EFAULT;
1731}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001732EXPORT_SYMBOL(skb_copy_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733
Jens Axboe9c55e012007-11-06 23:30:13 -08001734/*
1735 * Callback from splice_to_pipe(), if we need to release some pages
1736 * at the end of the spd in case we error'ed out in filling the pipe.
1737 */
1738static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
1739{
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001740 put_page(spd->pages[i]);
1741}
Jens Axboe9c55e012007-11-06 23:30:13 -08001742
David S. Millera108d5f2012-04-23 23:06:11 -04001743static struct page *linear_to_page(struct page *page, unsigned int *len,
1744 unsigned int *offset,
Eric Dumazet18aafc62013-01-11 14:46:37 +00001745 struct sock *sk)
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001746{
Eric Dumazet5640f762012-09-23 23:04:42 +00001747 struct page_frag *pfrag = sk_page_frag(sk);
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001748
Eric Dumazet5640f762012-09-23 23:04:42 +00001749 if (!sk_page_frag_refill(sk, pfrag))
1750 return NULL;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001751
Eric Dumazet5640f762012-09-23 23:04:42 +00001752 *len = min_t(unsigned int, *len, pfrag->size - pfrag->offset);
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001753
Eric Dumazet5640f762012-09-23 23:04:42 +00001754 memcpy(page_address(pfrag->page) + pfrag->offset,
1755 page_address(page) + *offset, *len);
1756 *offset = pfrag->offset;
1757 pfrag->offset += *len;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001758
Eric Dumazet5640f762012-09-23 23:04:42 +00001759 return pfrag->page;
Jens Axboe9c55e012007-11-06 23:30:13 -08001760}
1761
Eric Dumazet41c73a02012-04-22 12:26:16 +00001762static bool spd_can_coalesce(const struct splice_pipe_desc *spd,
1763 struct page *page,
1764 unsigned int offset)
1765{
1766 return spd->nr_pages &&
1767 spd->pages[spd->nr_pages - 1] == page &&
1768 (spd->partial[spd->nr_pages - 1].offset +
1769 spd->partial[spd->nr_pages - 1].len == offset);
1770}
1771
Jens Axboe9c55e012007-11-06 23:30:13 -08001772/*
1773 * Fill page/offset/length into spd, if it can hold more pages.
1774 */
David S. Millera108d5f2012-04-23 23:06:11 -04001775static bool spd_fill_page(struct splice_pipe_desc *spd,
1776 struct pipe_inode_info *pipe, struct page *page,
1777 unsigned int *len, unsigned int offset,
Eric Dumazet18aafc62013-01-11 14:46:37 +00001778 bool linear,
David S. Millera108d5f2012-04-23 23:06:11 -04001779 struct sock *sk)
Jens Axboe9c55e012007-11-06 23:30:13 -08001780{
Eric Dumazet41c73a02012-04-22 12:26:16 +00001781 if (unlikely(spd->nr_pages == MAX_SKB_FRAGS))
David S. Millera108d5f2012-04-23 23:06:11 -04001782 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08001783
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001784 if (linear) {
Eric Dumazet18aafc62013-01-11 14:46:37 +00001785 page = linear_to_page(page, len, &offset, sk);
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001786 if (!page)
David S. Millera108d5f2012-04-23 23:06:11 -04001787 return true;
Eric Dumazet41c73a02012-04-22 12:26:16 +00001788 }
1789 if (spd_can_coalesce(spd, page, offset)) {
1790 spd->partial[spd->nr_pages - 1].len += *len;
David S. Millera108d5f2012-04-23 23:06:11 -04001791 return false;
Eric Dumazet41c73a02012-04-22 12:26:16 +00001792 }
1793 get_page(page);
Jens Axboe9c55e012007-11-06 23:30:13 -08001794 spd->pages[spd->nr_pages] = page;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001795 spd->partial[spd->nr_pages].len = *len;
Jens Axboe9c55e012007-11-06 23:30:13 -08001796 spd->partial[spd->nr_pages].offset = offset;
Jens Axboe9c55e012007-11-06 23:30:13 -08001797 spd->nr_pages++;
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001798
David S. Millera108d5f2012-04-23 23:06:11 -04001799 return false;
Jens Axboe9c55e012007-11-06 23:30:13 -08001800}
1801
David S. Millera108d5f2012-04-23 23:06:11 -04001802static bool __splice_segment(struct page *page, unsigned int poff,
1803 unsigned int plen, unsigned int *off,
Eric Dumazet18aafc62013-01-11 14:46:37 +00001804 unsigned int *len,
Eric Dumazetd7ccf7c2012-04-23 23:35:04 -04001805 struct splice_pipe_desc *spd, bool linear,
David S. Millera108d5f2012-04-23 23:06:11 -04001806 struct sock *sk,
1807 struct pipe_inode_info *pipe)
Octavian Purdila2870c432008-07-15 00:49:11 -07001808{
1809 if (!*len)
David S. Millera108d5f2012-04-23 23:06:11 -04001810 return true;
Octavian Purdila2870c432008-07-15 00:49:11 -07001811
1812 /* skip this segment if already processed */
1813 if (*off >= plen) {
1814 *off -= plen;
David S. Millera108d5f2012-04-23 23:06:11 -04001815 return false;
Octavian Purdiladb43a282008-06-27 17:27:21 -07001816 }
Jens Axboe9c55e012007-11-06 23:30:13 -08001817
Octavian Purdila2870c432008-07-15 00:49:11 -07001818 /* ignore any bits we already processed */
Eric Dumazet9ca1b222013-01-05 21:31:18 +00001819 poff += *off;
1820 plen -= *off;
1821 *off = 0;
Octavian Purdila2870c432008-07-15 00:49:11 -07001822
Eric Dumazet18aafc62013-01-11 14:46:37 +00001823 do {
1824 unsigned int flen = min(*len, plen);
Octavian Purdila2870c432008-07-15 00:49:11 -07001825
Eric Dumazet18aafc62013-01-11 14:46:37 +00001826 if (spd_fill_page(spd, pipe, page, &flen, poff,
1827 linear, sk))
1828 return true;
1829 poff += flen;
1830 plen -= flen;
1831 *len -= flen;
1832 } while (*len && plen);
Octavian Purdila2870c432008-07-15 00:49:11 -07001833
David S. Millera108d5f2012-04-23 23:06:11 -04001834 return false;
Octavian Purdila2870c432008-07-15 00:49:11 -07001835}
1836
1837/*
David S. Millera108d5f2012-04-23 23:06:11 -04001838 * Map linear and fragment data from the skb to spd. It reports true if the
Octavian Purdila2870c432008-07-15 00:49:11 -07001839 * pipe is full or if we already spliced the requested length.
1840 */
David S. Millera108d5f2012-04-23 23:06:11 -04001841static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe,
1842 unsigned int *offset, unsigned int *len,
1843 struct splice_pipe_desc *spd, struct sock *sk)
Octavian Purdila2870c432008-07-15 00:49:11 -07001844{
1845 int seg;
1846
Eric Dumazet1d0c0b32012-04-27 02:10:03 +00001847 /* map the linear part :
Alexander Duyck2996d312012-05-02 18:18:42 +00001848 * If skb->head_frag is set, this 'linear' part is backed by a
1849 * fragment, and if the head is not shared with any clones then
1850 * we can avoid a copy since we own the head portion of this page.
Jens Axboe9c55e012007-11-06 23:30:13 -08001851 */
Octavian Purdila2870c432008-07-15 00:49:11 -07001852 if (__splice_segment(virt_to_page(skb->data),
1853 (unsigned long) skb->data & (PAGE_SIZE - 1),
1854 skb_headlen(skb),
Eric Dumazet18aafc62013-01-11 14:46:37 +00001855 offset, len, spd,
Alexander Duyck3a7c1ee42012-05-03 01:09:42 +00001856 skb_head_is_locked(skb),
Eric Dumazet1d0c0b32012-04-27 02:10:03 +00001857 sk, pipe))
David S. Millera108d5f2012-04-23 23:06:11 -04001858 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08001859
1860 /*
1861 * then map the fragments
1862 */
Jens Axboe9c55e012007-11-06 23:30:13 -08001863 for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
1864 const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
1865
Ian Campbellea2ab692011-08-22 23:44:58 +00001866 if (__splice_segment(skb_frag_page(f),
Eric Dumazet9e903e02011-10-18 21:00:24 +00001867 f->page_offset, skb_frag_size(f),
Eric Dumazet18aafc62013-01-11 14:46:37 +00001868 offset, len, spd, false, sk, pipe))
David S. Millera108d5f2012-04-23 23:06:11 -04001869 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08001870 }
1871
David S. Millera108d5f2012-04-23 23:06:11 -04001872 return false;
Jens Axboe9c55e012007-11-06 23:30:13 -08001873}
1874
Hannes Frederic Sowaa60e3cc2015-05-21 17:00:00 +02001875ssize_t skb_socket_splice(struct sock *sk,
1876 struct pipe_inode_info *pipe,
1877 struct splice_pipe_desc *spd)
1878{
1879 int ret;
1880
1881 /* Drop the socket lock, otherwise we have reverse
1882 * locking dependencies between sk_lock and i_mutex
1883 * here as compared to sendfile(). We enter here
1884 * with the socket lock held, and splice_to_pipe() will
1885 * grab the pipe inode lock. For sendfile() emulation,
1886 * we call into ->sendpage() with the i_mutex lock held
1887 * and networking will grab the socket lock.
1888 */
1889 release_sock(sk);
1890 ret = splice_to_pipe(pipe, spd);
1891 lock_sock(sk);
1892
1893 return ret;
1894}
1895
Jens Axboe9c55e012007-11-06 23:30:13 -08001896/*
1897 * Map data from the skb to a pipe. Should handle both the linear part,
1898 * the fragments, and the frag list. It does NOT handle frag lists within
1899 * the frag list, if such a thing exists. We'd probably need to recurse to
1900 * handle that cleanly.
1901 */
Hannes Frederic Sowaa60e3cc2015-05-21 17:00:00 +02001902int skb_splice_bits(struct sk_buff *skb, struct sock *sk, unsigned int offset,
Jens Axboe9c55e012007-11-06 23:30:13 -08001903 struct pipe_inode_info *pipe, unsigned int tlen,
Hannes Frederic Sowaa60e3cc2015-05-21 17:00:00 +02001904 unsigned int flags,
1905 ssize_t (*splice_cb)(struct sock *,
1906 struct pipe_inode_info *,
1907 struct splice_pipe_desc *))
Jens Axboe9c55e012007-11-06 23:30:13 -08001908{
Eric Dumazet41c73a02012-04-22 12:26:16 +00001909 struct partial_page partial[MAX_SKB_FRAGS];
1910 struct page *pages[MAX_SKB_FRAGS];
Jens Axboe9c55e012007-11-06 23:30:13 -08001911 struct splice_pipe_desc spd = {
1912 .pages = pages,
1913 .partial = partial,
Eric Dumazet047fe362012-06-12 15:24:40 +02001914 .nr_pages_max = MAX_SKB_FRAGS,
Jens Axboe9c55e012007-11-06 23:30:13 -08001915 .flags = flags,
Miklos Szeredi28a625c2014-01-22 19:36:57 +01001916 .ops = &nosteal_pipe_buf_ops,
Jens Axboe9c55e012007-11-06 23:30:13 -08001917 .spd_release = sock_spd_release,
1918 };
David S. Millerfbb398a2009-06-09 00:18:59 -07001919 struct sk_buff *frag_iter;
Jens Axboe35f3d142010-05-20 10:43:18 +02001920 int ret = 0;
1921
Jens Axboe9c55e012007-11-06 23:30:13 -08001922 /*
1923 * __skb_splice_bits() only fails if the output has no room left,
1924 * so no point in going over the frag_list for the error case.
1925 */
Jens Axboe35f3d142010-05-20 10:43:18 +02001926 if (__skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk))
Jens Axboe9c55e012007-11-06 23:30:13 -08001927 goto done;
1928 else if (!tlen)
1929 goto done;
1930
1931 /*
1932 * now see if we have a frag_list to map
1933 */
David S. Millerfbb398a2009-06-09 00:18:59 -07001934 skb_walk_frags(skb, frag_iter) {
1935 if (!tlen)
1936 break;
Jens Axboe35f3d142010-05-20 10:43:18 +02001937 if (__skb_splice_bits(frag_iter, pipe, &offset, &tlen, &spd, sk))
David S. Millerfbb398a2009-06-09 00:18:59 -07001938 break;
Jens Axboe9c55e012007-11-06 23:30:13 -08001939 }
1940
1941done:
Hannes Frederic Sowaa60e3cc2015-05-21 17:00:00 +02001942 if (spd.nr_pages)
1943 ret = splice_cb(sk, pipe, &spd);
Jens Axboe9c55e012007-11-06 23:30:13 -08001944
Jens Axboe35f3d142010-05-20 10:43:18 +02001945 return ret;
Jens Axboe9c55e012007-11-06 23:30:13 -08001946}
Hannes Frederic Sowa2b514572015-05-21 17:00:01 +02001947EXPORT_SYMBOL_GPL(skb_splice_bits);
Jens Axboe9c55e012007-11-06 23:30:13 -08001948
Herbert Xu357b40a2005-04-19 22:30:14 -07001949/**
1950 * skb_store_bits - store bits from kernel buffer to skb
1951 * @skb: destination buffer
1952 * @offset: offset in destination
1953 * @from: source buffer
1954 * @len: number of bytes to copy
1955 *
1956 * Copy the specified number of bytes from the source buffer to the
1957 * destination skb. This function handles all the messy bits of
1958 * traversing fragment lists and such.
1959 */
1960
Stephen Hemminger0c6fcc82007-04-20 16:40:01 -07001961int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
Herbert Xu357b40a2005-04-19 22:30:14 -07001962{
David S. Miller1a028e52007-04-27 15:21:23 -07001963 int start = skb_headlen(skb);
David S. Millerfbb398a2009-06-09 00:18:59 -07001964 struct sk_buff *frag_iter;
1965 int i, copy;
Herbert Xu357b40a2005-04-19 22:30:14 -07001966
1967 if (offset > (int)skb->len - len)
1968 goto fault;
1969
David S. Miller1a028e52007-04-27 15:21:23 -07001970 if ((copy = start - offset) > 0) {
Herbert Xu357b40a2005-04-19 22:30:14 -07001971 if (copy > len)
1972 copy = len;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001973 skb_copy_to_linear_data_offset(skb, offset, from, copy);
Herbert Xu357b40a2005-04-19 22:30:14 -07001974 if ((len -= copy) == 0)
1975 return 0;
1976 offset += copy;
1977 from += copy;
1978 }
1979
1980 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1981 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
David S. Miller1a028e52007-04-27 15:21:23 -07001982 int end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001983
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001984 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07001985
Eric Dumazet9e903e02011-10-18 21:00:24 +00001986 end = start + skb_frag_size(frag);
Herbert Xu357b40a2005-04-19 22:30:14 -07001987 if ((copy = end - offset) > 0) {
1988 u8 *vaddr;
1989
1990 if (copy > len)
1991 copy = len;
1992
Eric Dumazet51c56b02012-04-05 11:35:15 +02001993 vaddr = kmap_atomic(skb_frag_page(frag));
David S. Miller1a028e52007-04-27 15:21:23 -07001994 memcpy(vaddr + frag->page_offset + offset - start,
1995 from, copy);
Eric Dumazet51c56b02012-04-05 11:35:15 +02001996 kunmap_atomic(vaddr);
Herbert Xu357b40a2005-04-19 22:30:14 -07001997
1998 if ((len -= copy) == 0)
1999 return 0;
2000 offset += copy;
2001 from += copy;
2002 }
David S. Miller1a028e52007-04-27 15:21:23 -07002003 start = end;
Herbert Xu357b40a2005-04-19 22:30:14 -07002004 }
2005
David S. Millerfbb398a2009-06-09 00:18:59 -07002006 skb_walk_frags(skb, frag_iter) {
2007 int end;
Herbert Xu357b40a2005-04-19 22:30:14 -07002008
David S. Millerfbb398a2009-06-09 00:18:59 -07002009 WARN_ON(start > offset + len);
Herbert Xu357b40a2005-04-19 22:30:14 -07002010
David S. Millerfbb398a2009-06-09 00:18:59 -07002011 end = start + frag_iter->len;
2012 if ((copy = end - offset) > 0) {
2013 if (copy > len)
2014 copy = len;
2015 if (skb_store_bits(frag_iter, offset - start,
2016 from, copy))
2017 goto fault;
2018 if ((len -= copy) == 0)
2019 return 0;
2020 offset += copy;
2021 from += copy;
Herbert Xu357b40a2005-04-19 22:30:14 -07002022 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002023 start = end;
Herbert Xu357b40a2005-04-19 22:30:14 -07002024 }
2025 if (!len)
2026 return 0;
2027
2028fault:
2029 return -EFAULT;
2030}
Herbert Xu357b40a2005-04-19 22:30:14 -07002031EXPORT_SYMBOL(skb_store_bits);
2032
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033/* Checksum skb data. */
Daniel Borkmann2817a332013-10-30 11:50:51 +01002034__wsum __skb_checksum(const struct sk_buff *skb, int offset, int len,
2035 __wsum csum, const struct skb_checksum_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036{
David S. Miller1a028e52007-04-27 15:21:23 -07002037 int start = skb_headlen(skb);
2038 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07002039 struct sk_buff *frag_iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 int pos = 0;
2041
2042 /* Checksum header. */
2043 if (copy > 0) {
2044 if (copy > len)
2045 copy = len;
Daniel Borkmann2817a332013-10-30 11:50:51 +01002046 csum = ops->update(skb->data + offset, copy, csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 if ((len -= copy) == 0)
2048 return csum;
2049 offset += copy;
2050 pos = copy;
2051 }
2052
2053 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07002054 int end;
Eric Dumazet51c56b02012-04-05 11:35:15 +02002055 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002057 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07002058
Eric Dumazet51c56b02012-04-05 11:35:15 +02002059 end = start + skb_frag_size(frag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 if ((copy = end - offset) > 0) {
Al Viro44bb9362006-11-14 21:36:14 -08002061 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 u8 *vaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063
2064 if (copy > len)
2065 copy = len;
Eric Dumazet51c56b02012-04-05 11:35:15 +02002066 vaddr = kmap_atomic(skb_frag_page(frag));
Daniel Borkmann2817a332013-10-30 11:50:51 +01002067 csum2 = ops->update(vaddr + frag->page_offset +
2068 offset - start, copy, 0);
Eric Dumazet51c56b02012-04-05 11:35:15 +02002069 kunmap_atomic(vaddr);
Daniel Borkmann2817a332013-10-30 11:50:51 +01002070 csum = ops->combine(csum, csum2, pos, copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 if (!(len -= copy))
2072 return csum;
2073 offset += copy;
2074 pos += copy;
2075 }
David S. Miller1a028e52007-04-27 15:21:23 -07002076 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 }
2078
David S. Millerfbb398a2009-06-09 00:18:59 -07002079 skb_walk_frags(skb, frag_iter) {
2080 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081
David S. Millerfbb398a2009-06-09 00:18:59 -07002082 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083
David S. Millerfbb398a2009-06-09 00:18:59 -07002084 end = start + frag_iter->len;
2085 if ((copy = end - offset) > 0) {
2086 __wsum csum2;
2087 if (copy > len)
2088 copy = len;
Daniel Borkmann2817a332013-10-30 11:50:51 +01002089 csum2 = __skb_checksum(frag_iter, offset - start,
2090 copy, 0, ops);
2091 csum = ops->combine(csum, csum2, pos, copy);
David S. Millerfbb398a2009-06-09 00:18:59 -07002092 if ((len -= copy) == 0)
2093 return csum;
2094 offset += copy;
2095 pos += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002097 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08002099 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100
2101 return csum;
2102}
Daniel Borkmann2817a332013-10-30 11:50:51 +01002103EXPORT_SYMBOL(__skb_checksum);
2104
2105__wsum skb_checksum(const struct sk_buff *skb, int offset,
2106 int len, __wsum csum)
2107{
2108 const struct skb_checksum_ops ops = {
Daniel Borkmanncea80ea82013-11-04 17:10:25 +01002109 .update = csum_partial_ext,
Daniel Borkmann2817a332013-10-30 11:50:51 +01002110 .combine = csum_block_add_ext,
2111 };
2112
2113 return __skb_checksum(skb, offset, len, csum, &ops);
2114}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002115EXPORT_SYMBOL(skb_checksum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116
2117/* Both of above in one bottle. */
2118
Al Viro81d77662006-11-14 21:37:33 -08002119__wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
2120 u8 *to, int len, __wsum csum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121{
David S. Miller1a028e52007-04-27 15:21:23 -07002122 int start = skb_headlen(skb);
2123 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07002124 struct sk_buff *frag_iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 int pos = 0;
2126
2127 /* Copy header. */
2128 if (copy > 0) {
2129 if (copy > len)
2130 copy = len;
2131 csum = csum_partial_copy_nocheck(skb->data + offset, to,
2132 copy, csum);
2133 if ((len -= copy) == 0)
2134 return csum;
2135 offset += copy;
2136 to += copy;
2137 pos = copy;
2138 }
2139
2140 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07002141 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002143 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07002144
Eric Dumazet9e903e02011-10-18 21:00:24 +00002145 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 if ((copy = end - offset) > 0) {
Al Viro50842052006-11-14 21:36:34 -08002147 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 u8 *vaddr;
2149 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2150
2151 if (copy > len)
2152 copy = len;
Eric Dumazet51c56b02012-04-05 11:35:15 +02002153 vaddr = kmap_atomic(skb_frag_page(frag));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 csum2 = csum_partial_copy_nocheck(vaddr +
David S. Miller1a028e52007-04-27 15:21:23 -07002155 frag->page_offset +
2156 offset - start, to,
2157 copy, 0);
Eric Dumazet51c56b02012-04-05 11:35:15 +02002158 kunmap_atomic(vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 csum = csum_block_add(csum, csum2, pos);
2160 if (!(len -= copy))
2161 return csum;
2162 offset += copy;
2163 to += copy;
2164 pos += copy;
2165 }
David S. Miller1a028e52007-04-27 15:21:23 -07002166 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 }
2168
David S. Millerfbb398a2009-06-09 00:18:59 -07002169 skb_walk_frags(skb, frag_iter) {
2170 __wsum csum2;
2171 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172
David S. Millerfbb398a2009-06-09 00:18:59 -07002173 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174
David S. Millerfbb398a2009-06-09 00:18:59 -07002175 end = start + frag_iter->len;
2176 if ((copy = end - offset) > 0) {
2177 if (copy > len)
2178 copy = len;
2179 csum2 = skb_copy_and_csum_bits(frag_iter,
2180 offset - start,
2181 to, copy, 0);
2182 csum = csum_block_add(csum, csum2, pos);
2183 if ((len -= copy) == 0)
2184 return csum;
2185 offset += copy;
2186 to += copy;
2187 pos += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002189 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08002191 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 return csum;
2193}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002194EXPORT_SYMBOL(skb_copy_and_csum_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195
Thomas Grafaf2806f2013-12-13 15:22:17 +01002196 /**
2197 * skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy()
2198 * @from: source buffer
2199 *
2200 * Calculates the amount of linear headroom needed in the 'to' skb passed
2201 * into skb_zerocopy().
2202 */
2203unsigned int
2204skb_zerocopy_headlen(const struct sk_buff *from)
2205{
2206 unsigned int hlen = 0;
2207
2208 if (!from->head_frag ||
2209 skb_headlen(from) < L1_CACHE_BYTES ||
2210 skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
2211 hlen = skb_headlen(from);
2212
2213 if (skb_has_frag_list(from))
2214 hlen = from->len;
2215
2216 return hlen;
2217}
2218EXPORT_SYMBOL_GPL(skb_zerocopy_headlen);
2219
2220/**
2221 * skb_zerocopy - Zero copy skb to skb
2222 * @to: destination buffer
Masanari Iida7fceb4d2014-01-29 01:05:28 +09002223 * @from: source buffer
Thomas Grafaf2806f2013-12-13 15:22:17 +01002224 * @len: number of bytes to copy from source buffer
2225 * @hlen: size of linear headroom in destination buffer
2226 *
2227 * Copies up to `len` bytes from `from` to `to` by creating references
2228 * to the frags in the source buffer.
2229 *
2230 * The `hlen` as calculated by skb_zerocopy_headlen() specifies the
2231 * headroom in the `to` buffer.
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002232 *
2233 * Return value:
2234 * 0: everything is OK
2235 * -ENOMEM: couldn't orphan frags of @from due to lack of memory
2236 * -EFAULT: skb_copy_bits() found some problem with skb geometry
Thomas Grafaf2806f2013-12-13 15:22:17 +01002237 */
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002238int
2239skb_zerocopy(struct sk_buff *to, struct sk_buff *from, int len, int hlen)
Thomas Grafaf2806f2013-12-13 15:22:17 +01002240{
2241 int i, j = 0;
2242 int plen = 0; /* length of skb->head fragment */
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002243 int ret;
Thomas Grafaf2806f2013-12-13 15:22:17 +01002244 struct page *page;
2245 unsigned int offset;
2246
2247 BUG_ON(!from->head_frag && !hlen);
2248
2249 /* dont bother with small payloads */
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002250 if (len <= skb_tailroom(to))
2251 return skb_copy_bits(from, 0, skb_put(to, len), len);
Thomas Grafaf2806f2013-12-13 15:22:17 +01002252
2253 if (hlen) {
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002254 ret = skb_copy_bits(from, 0, skb_put(to, hlen), hlen);
2255 if (unlikely(ret))
2256 return ret;
Thomas Grafaf2806f2013-12-13 15:22:17 +01002257 len -= hlen;
2258 } else {
2259 plen = min_t(int, skb_headlen(from), len);
2260 if (plen) {
2261 page = virt_to_head_page(from->head);
2262 offset = from->data - (unsigned char *)page_address(page);
2263 __skb_fill_page_desc(to, 0, page, offset, plen);
2264 get_page(page);
2265 j = 1;
2266 len -= plen;
2267 }
2268 }
2269
2270 to->truesize += len + plen;
2271 to->len += len + plen;
2272 to->data_len += len + plen;
2273
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002274 if (unlikely(skb_orphan_frags(from, GFP_ATOMIC))) {
2275 skb_tx_error(from);
2276 return -ENOMEM;
2277 }
2278
Thomas Grafaf2806f2013-12-13 15:22:17 +01002279 for (i = 0; i < skb_shinfo(from)->nr_frags; i++) {
2280 if (!len)
2281 break;
2282 skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i];
2283 skb_shinfo(to)->frags[j].size = min_t(int, skb_shinfo(to)->frags[j].size, len);
2284 len -= skb_shinfo(to)->frags[j].size;
2285 skb_frag_ref(to, j);
2286 j++;
2287 }
2288 skb_shinfo(to)->nr_frags = j;
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002289
2290 return 0;
Thomas Grafaf2806f2013-12-13 15:22:17 +01002291}
2292EXPORT_SYMBOL_GPL(skb_zerocopy);
2293
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
2295{
Al Virod3bc23e2006-11-14 21:24:49 -08002296 __wsum csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 long csstart;
2298
Patrick McHardy84fa7932006-08-29 16:44:56 -07002299 if (skb->ip_summed == CHECKSUM_PARTIAL)
Michał Mirosław55508d62010-12-14 15:24:08 +00002300 csstart = skb_checksum_start_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 else
2302 csstart = skb_headlen(skb);
2303
Kris Katterjohn09a62662006-01-08 22:24:28 -08002304 BUG_ON(csstart > skb_headlen(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03002306 skb_copy_from_linear_data(skb, to, csstart);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307
2308 csum = 0;
2309 if (csstart != skb->len)
2310 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
2311 skb->len - csstart, 0);
2312
Patrick McHardy84fa7932006-08-29 16:44:56 -07002313 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Al Viroff1dcad2006-11-20 18:07:29 -08002314 long csstuff = csstart + skb->csum_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315
Al Virod3bc23e2006-11-14 21:24:49 -08002316 *((__sum16 *)(to + csstuff)) = csum_fold(csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 }
2318}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002319EXPORT_SYMBOL(skb_copy_and_csum_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320
2321/**
2322 * skb_dequeue - remove from the head of the queue
2323 * @list: list to dequeue from
2324 *
2325 * Remove the head of the list. The list lock is taken so the function
2326 * may be used safely with other locking list functions. The head item is
2327 * returned or %NULL if the list is empty.
2328 */
2329
2330struct sk_buff *skb_dequeue(struct sk_buff_head *list)
2331{
2332 unsigned long flags;
2333 struct sk_buff *result;
2334
2335 spin_lock_irqsave(&list->lock, flags);
2336 result = __skb_dequeue(list);
2337 spin_unlock_irqrestore(&list->lock, flags);
2338 return result;
2339}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002340EXPORT_SYMBOL(skb_dequeue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341
2342/**
2343 * skb_dequeue_tail - remove from the tail of the queue
2344 * @list: list to dequeue from
2345 *
2346 * Remove the tail of the list. The list lock is taken so the function
2347 * may be used safely with other locking list functions. The tail item is
2348 * returned or %NULL if the list is empty.
2349 */
2350struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
2351{
2352 unsigned long flags;
2353 struct sk_buff *result;
2354
2355 spin_lock_irqsave(&list->lock, flags);
2356 result = __skb_dequeue_tail(list);
2357 spin_unlock_irqrestore(&list->lock, flags);
2358 return result;
2359}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002360EXPORT_SYMBOL(skb_dequeue_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361
2362/**
2363 * skb_queue_purge - empty a list
2364 * @list: list to empty
2365 *
2366 * Delete all buffers on an &sk_buff list. Each buffer is removed from
2367 * the list and one reference dropped. This function takes the list
2368 * lock and is atomic with respect to other list locking functions.
2369 */
2370void skb_queue_purge(struct sk_buff_head *list)
2371{
2372 struct sk_buff *skb;
2373 while ((skb = skb_dequeue(list)) != NULL)
2374 kfree_skb(skb);
2375}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002376EXPORT_SYMBOL(skb_queue_purge);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377
2378/**
2379 * skb_queue_head - queue a buffer at the list head
2380 * @list: list to use
2381 * @newsk: buffer to queue
2382 *
2383 * Queue a buffer at the start of the list. This function takes the
2384 * list lock and can be used safely with other locking &sk_buff functions
2385 * safely.
2386 *
2387 * A buffer cannot be placed on two lists at the same time.
2388 */
2389void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
2390{
2391 unsigned long flags;
2392
2393 spin_lock_irqsave(&list->lock, flags);
2394 __skb_queue_head(list, newsk);
2395 spin_unlock_irqrestore(&list->lock, flags);
2396}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002397EXPORT_SYMBOL(skb_queue_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398
2399/**
2400 * skb_queue_tail - queue a buffer at the list tail
2401 * @list: list to use
2402 * @newsk: buffer to queue
2403 *
2404 * Queue a buffer at the tail of the list. This function takes the
2405 * list lock and can be used safely with other locking &sk_buff functions
2406 * safely.
2407 *
2408 * A buffer cannot be placed on two lists at the same time.
2409 */
2410void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
2411{
2412 unsigned long flags;
2413
2414 spin_lock_irqsave(&list->lock, flags);
2415 __skb_queue_tail(list, newsk);
2416 spin_unlock_irqrestore(&list->lock, flags);
2417}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002418EXPORT_SYMBOL(skb_queue_tail);
David S. Miller8728b832005-08-09 19:25:21 -07002419
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420/**
2421 * skb_unlink - remove a buffer from a list
2422 * @skb: buffer to remove
David S. Miller8728b832005-08-09 19:25:21 -07002423 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 *
David S. Miller8728b832005-08-09 19:25:21 -07002425 * Remove a packet from a list. The list locks are taken and this
2426 * function is atomic with respect to other list locked calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 *
David S. Miller8728b832005-08-09 19:25:21 -07002428 * You must know what list the SKB is on.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 */
David S. Miller8728b832005-08-09 19:25:21 -07002430void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431{
David S. Miller8728b832005-08-09 19:25:21 -07002432 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433
David S. Miller8728b832005-08-09 19:25:21 -07002434 spin_lock_irqsave(&list->lock, flags);
2435 __skb_unlink(skb, list);
2436 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002438EXPORT_SYMBOL(skb_unlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440/**
2441 * skb_append - append a buffer
2442 * @old: buffer to insert after
2443 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07002444 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 *
2446 * Place a packet after a given packet in a list. The list locks are taken
2447 * and this function is atomic with respect to other list locked calls.
2448 * A buffer cannot be placed on two lists at the same time.
2449 */
David S. Miller8728b832005-08-09 19:25:21 -07002450void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451{
2452 unsigned long flags;
2453
David S. Miller8728b832005-08-09 19:25:21 -07002454 spin_lock_irqsave(&list->lock, flags);
Gerrit Renker7de6c032008-04-14 00:05:09 -07002455 __skb_queue_after(list, old, newsk);
David S. Miller8728b832005-08-09 19:25:21 -07002456 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002458EXPORT_SYMBOL(skb_append);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459
2460/**
2461 * skb_insert - insert a buffer
2462 * @old: buffer to insert before
2463 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07002464 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 *
David S. Miller8728b832005-08-09 19:25:21 -07002466 * Place a packet before a given packet in a list. The list locks are
2467 * taken and this function is atomic with respect to other list locked
2468 * calls.
2469 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 * A buffer cannot be placed on two lists at the same time.
2471 */
David S. Miller8728b832005-08-09 19:25:21 -07002472void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473{
2474 unsigned long flags;
2475
David S. Miller8728b832005-08-09 19:25:21 -07002476 spin_lock_irqsave(&list->lock, flags);
2477 __skb_insert(newsk, old->prev, old, list);
2478 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002480EXPORT_SYMBOL(skb_insert);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482static inline void skb_split_inside_header(struct sk_buff *skb,
2483 struct sk_buff* skb1,
2484 const u32 len, const int pos)
2485{
2486 int i;
2487
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03002488 skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
2489 pos - len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 /* And move data appendix as is. */
2491 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
2492 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
2493
2494 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
2495 skb_shinfo(skb)->nr_frags = 0;
2496 skb1->data_len = skb->data_len;
2497 skb1->len += skb1->data_len;
2498 skb->data_len = 0;
2499 skb->len = len;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002500 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501}
2502
2503static inline void skb_split_no_header(struct sk_buff *skb,
2504 struct sk_buff* skb1,
2505 const u32 len, int pos)
2506{
2507 int i, k = 0;
2508 const int nfrags = skb_shinfo(skb)->nr_frags;
2509
2510 skb_shinfo(skb)->nr_frags = 0;
2511 skb1->len = skb1->data_len = skb->len - len;
2512 skb->len = len;
2513 skb->data_len = len - pos;
2514
2515 for (i = 0; i < nfrags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00002516 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517
2518 if (pos + size > len) {
2519 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
2520
2521 if (pos < len) {
2522 /* Split frag.
2523 * We have two variants in this case:
2524 * 1. Move all the frag to the second
2525 * part, if it is possible. F.e.
2526 * this approach is mandatory for TUX,
2527 * where splitting is expensive.
2528 * 2. Split is accurately. We make this.
2529 */
Ian Campbellea2ab692011-08-22 23:44:58 +00002530 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002532 skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos);
2533 skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 skb_shinfo(skb)->nr_frags++;
2535 }
2536 k++;
2537 } else
2538 skb_shinfo(skb)->nr_frags++;
2539 pos += size;
2540 }
2541 skb_shinfo(skb1)->nr_frags = k;
2542}
2543
2544/**
2545 * skb_split - Split fragmented skb to two parts at length len.
2546 * @skb: the buffer to split
2547 * @skb1: the buffer to receive the second part
2548 * @len: new length for skb
2549 */
2550void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
2551{
2552 int pos = skb_headlen(skb);
2553
Amerigo Wang68534c62013-02-19 22:51:30 +00002554 skb_shinfo(skb1)->tx_flags = skb_shinfo(skb)->tx_flags & SKBTX_SHARED_FRAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 if (len < pos) /* Split line is inside header. */
2556 skb_split_inside_header(skb, skb1, len, pos);
2557 else /* Second chunk has no header, nothing to copy. */
2558 skb_split_no_header(skb, skb1, len, pos);
2559}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002560EXPORT_SYMBOL(skb_split);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561
Ilpo Järvinen9f782db2008-11-25 13:57:01 -08002562/* Shifting from/to a cloned skb is a no-go.
2563 *
2564 * Caller cannot keep skb_shinfo related pointers past calling here!
2565 */
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002566static int skb_prepare_for_shift(struct sk_buff *skb)
2567{
Ilpo Järvinen0ace2852008-11-24 21:30:21 -08002568 return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002569}
2570
2571/**
2572 * skb_shift - Shifts paged data partially from skb to another
2573 * @tgt: buffer into which tail data gets added
2574 * @skb: buffer from which the paged data comes from
2575 * @shiftlen: shift up to this many bytes
2576 *
2577 * Attempts to shift up to shiftlen worth of bytes, which may be less than
Feng King20e994a2011-11-21 01:47:11 +00002578 * the length of the skb, from skb to tgt. Returns number bytes shifted.
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002579 * It's up to caller to free skb if everything was shifted.
2580 *
2581 * If @tgt runs out of frags, the whole operation is aborted.
2582 *
2583 * Skb cannot include anything else but paged data while tgt is allowed
2584 * to have non-paged data as well.
2585 *
2586 * TODO: full sized shift could be optimized but that would need
2587 * specialized skb free'er to handle frags without up-to-date nr_frags.
2588 */
2589int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
2590{
2591 int from, to, merge, todo;
2592 struct skb_frag_struct *fragfrom, *fragto;
2593
2594 BUG_ON(shiftlen > skb->len);
2595 BUG_ON(skb_headlen(skb)); /* Would corrupt stream */
2596
2597 todo = shiftlen;
2598 from = 0;
2599 to = skb_shinfo(tgt)->nr_frags;
2600 fragfrom = &skb_shinfo(skb)->frags[from];
2601
2602 /* Actual merge is delayed until the point when we know we can
2603 * commit all, so that we don't have to undo partial changes
2604 */
2605 if (!to ||
Ian Campbellea2ab692011-08-22 23:44:58 +00002606 !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom),
2607 fragfrom->page_offset)) {
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002608 merge = -1;
2609 } else {
2610 merge = to - 1;
2611
Eric Dumazet9e903e02011-10-18 21:00:24 +00002612 todo -= skb_frag_size(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002613 if (todo < 0) {
2614 if (skb_prepare_for_shift(skb) ||
2615 skb_prepare_for_shift(tgt))
2616 return 0;
2617
Ilpo Järvinen9f782db2008-11-25 13:57:01 -08002618 /* All previous frag pointers might be stale! */
2619 fragfrom = &skb_shinfo(skb)->frags[from];
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002620 fragto = &skb_shinfo(tgt)->frags[merge];
2621
Eric Dumazet9e903e02011-10-18 21:00:24 +00002622 skb_frag_size_add(fragto, shiftlen);
2623 skb_frag_size_sub(fragfrom, shiftlen);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002624 fragfrom->page_offset += shiftlen;
2625
2626 goto onlymerged;
2627 }
2628
2629 from++;
2630 }
2631
2632 /* Skip full, not-fitting skb to avoid expensive operations */
2633 if ((shiftlen == skb->len) &&
2634 (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to))
2635 return 0;
2636
2637 if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt))
2638 return 0;
2639
2640 while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) {
2641 if (to == MAX_SKB_FRAGS)
2642 return 0;
2643
2644 fragfrom = &skb_shinfo(skb)->frags[from];
2645 fragto = &skb_shinfo(tgt)->frags[to];
2646
Eric Dumazet9e903e02011-10-18 21:00:24 +00002647 if (todo >= skb_frag_size(fragfrom)) {
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002648 *fragto = *fragfrom;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002649 todo -= skb_frag_size(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002650 from++;
2651 to++;
2652
2653 } else {
Ian Campbellea2ab692011-08-22 23:44:58 +00002654 __skb_frag_ref(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002655 fragto->page = fragfrom->page;
2656 fragto->page_offset = fragfrom->page_offset;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002657 skb_frag_size_set(fragto, todo);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002658
2659 fragfrom->page_offset += todo;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002660 skb_frag_size_sub(fragfrom, todo);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002661 todo = 0;
2662
2663 to++;
2664 break;
2665 }
2666 }
2667
2668 /* Ready to "commit" this state change to tgt */
2669 skb_shinfo(tgt)->nr_frags = to;
2670
2671 if (merge >= 0) {
2672 fragfrom = &skb_shinfo(skb)->frags[0];
2673 fragto = &skb_shinfo(tgt)->frags[merge];
2674
Eric Dumazet9e903e02011-10-18 21:00:24 +00002675 skb_frag_size_add(fragto, skb_frag_size(fragfrom));
Ian Campbellea2ab692011-08-22 23:44:58 +00002676 __skb_frag_unref(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002677 }
2678
2679 /* Reposition in the original skb */
2680 to = 0;
2681 while (from < skb_shinfo(skb)->nr_frags)
2682 skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++];
2683 skb_shinfo(skb)->nr_frags = to;
2684
2685 BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags);
2686
2687onlymerged:
2688 /* Most likely the tgt won't ever need its checksum anymore, skb on
2689 * the other hand might need it if it needs to be resent
2690 */
2691 tgt->ip_summed = CHECKSUM_PARTIAL;
2692 skb->ip_summed = CHECKSUM_PARTIAL;
2693
2694 /* Yak, is it really working this way? Some helper please? */
2695 skb->len -= shiftlen;
2696 skb->data_len -= shiftlen;
2697 skb->truesize -= shiftlen;
2698 tgt->len += shiftlen;
2699 tgt->data_len += shiftlen;
2700 tgt->truesize += shiftlen;
2701
2702 return shiftlen;
2703}
2704
Thomas Graf677e90e2005-06-23 20:59:51 -07002705/**
2706 * skb_prepare_seq_read - Prepare a sequential read of skb data
2707 * @skb: the buffer to read
2708 * @from: lower offset of data to be read
2709 * @to: upper offset of data to be read
2710 * @st: state variable
2711 *
2712 * Initializes the specified state variable. Must be called before
2713 * invoking skb_seq_read() for the first time.
2714 */
2715void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
2716 unsigned int to, struct skb_seq_state *st)
2717{
2718 st->lower_offset = from;
2719 st->upper_offset = to;
2720 st->root_skb = st->cur_skb = skb;
2721 st->frag_idx = st->stepped_offset = 0;
2722 st->frag_data = NULL;
2723}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002724EXPORT_SYMBOL(skb_prepare_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07002725
2726/**
2727 * skb_seq_read - Sequentially read skb data
2728 * @consumed: number of bytes consumed by the caller so far
2729 * @data: destination pointer for data to be returned
2730 * @st: state variable
2731 *
Mathias Krausebc323832013-11-07 14:18:26 +01002732 * Reads a block of skb data at @consumed relative to the
Thomas Graf677e90e2005-06-23 20:59:51 -07002733 * lower offset specified to skb_prepare_seq_read(). Assigns
Mathias Krausebc323832013-11-07 14:18:26 +01002734 * the head of the data block to @data and returns the length
Thomas Graf677e90e2005-06-23 20:59:51 -07002735 * of the block or 0 if the end of the skb data or the upper
2736 * offset has been reached.
2737 *
2738 * The caller is not required to consume all of the data
Mathias Krausebc323832013-11-07 14:18:26 +01002739 * returned, i.e. @consumed is typically set to the number
Thomas Graf677e90e2005-06-23 20:59:51 -07002740 * of bytes already consumed and the next call to
2741 * skb_seq_read() will return the remaining part of the block.
2742 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002743 * Note 1: The size of each block of data returned can be arbitrary,
Masanari Iidae793c0f2014-09-04 23:44:36 +09002744 * this limitation is the cost for zerocopy sequential
Thomas Graf677e90e2005-06-23 20:59:51 -07002745 * reads of potentially non linear data.
2746 *
Randy Dunlapbc2cda12008-02-13 15:03:25 -08002747 * Note 2: Fragment lists within fragments are not implemented
Thomas Graf677e90e2005-06-23 20:59:51 -07002748 * at the moment, state->root_skb could be replaced with
2749 * a stack for this purpose.
2750 */
2751unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
2752 struct skb_seq_state *st)
2753{
2754 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
2755 skb_frag_t *frag;
2756
Wedson Almeida Filhoaeb193e2013-06-23 23:33:48 -07002757 if (unlikely(abs_offset >= st->upper_offset)) {
2758 if (st->frag_data) {
2759 kunmap_atomic(st->frag_data);
2760 st->frag_data = NULL;
2761 }
Thomas Graf677e90e2005-06-23 20:59:51 -07002762 return 0;
Wedson Almeida Filhoaeb193e2013-06-23 23:33:48 -07002763 }
Thomas Graf677e90e2005-06-23 20:59:51 -07002764
2765next_skb:
Herbert Xu95e3b242009-01-29 16:07:52 -08002766 block_limit = skb_headlen(st->cur_skb) + st->stepped_offset;
Thomas Graf677e90e2005-06-23 20:59:51 -07002767
Thomas Chenault995b3372009-05-18 21:43:27 -07002768 if (abs_offset < block_limit && !st->frag_data) {
Herbert Xu95e3b242009-01-29 16:07:52 -08002769 *data = st->cur_skb->data + (abs_offset - st->stepped_offset);
Thomas Graf677e90e2005-06-23 20:59:51 -07002770 return block_limit - abs_offset;
2771 }
2772
2773 if (st->frag_idx == 0 && !st->frag_data)
2774 st->stepped_offset += skb_headlen(st->cur_skb);
2775
2776 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
2777 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
Eric Dumazet9e903e02011-10-18 21:00:24 +00002778 block_limit = skb_frag_size(frag) + st->stepped_offset;
Thomas Graf677e90e2005-06-23 20:59:51 -07002779
2780 if (abs_offset < block_limit) {
2781 if (!st->frag_data)
Eric Dumazet51c56b02012-04-05 11:35:15 +02002782 st->frag_data = kmap_atomic(skb_frag_page(frag));
Thomas Graf677e90e2005-06-23 20:59:51 -07002783
2784 *data = (u8 *) st->frag_data + frag->page_offset +
2785 (abs_offset - st->stepped_offset);
2786
2787 return block_limit - abs_offset;
2788 }
2789
2790 if (st->frag_data) {
Eric Dumazet51c56b02012-04-05 11:35:15 +02002791 kunmap_atomic(st->frag_data);
Thomas Graf677e90e2005-06-23 20:59:51 -07002792 st->frag_data = NULL;
2793 }
2794
2795 st->frag_idx++;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002796 st->stepped_offset += skb_frag_size(frag);
Thomas Graf677e90e2005-06-23 20:59:51 -07002797 }
2798
Olaf Kirch5b5a60d2007-06-23 23:11:52 -07002799 if (st->frag_data) {
Eric Dumazet51c56b02012-04-05 11:35:15 +02002800 kunmap_atomic(st->frag_data);
Olaf Kirch5b5a60d2007-06-23 23:11:52 -07002801 st->frag_data = NULL;
2802 }
2803
David S. Miller21dc3302010-08-23 00:13:46 -07002804 if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) {
Shyam Iyer71b33462009-01-29 16:12:42 -08002805 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
Thomas Graf677e90e2005-06-23 20:59:51 -07002806 st->frag_idx = 0;
2807 goto next_skb;
Shyam Iyer71b33462009-01-29 16:12:42 -08002808 } else if (st->cur_skb->next) {
2809 st->cur_skb = st->cur_skb->next;
Herbert Xu95e3b242009-01-29 16:07:52 -08002810 st->frag_idx = 0;
Thomas Graf677e90e2005-06-23 20:59:51 -07002811 goto next_skb;
2812 }
2813
2814 return 0;
2815}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002816EXPORT_SYMBOL(skb_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07002817
2818/**
2819 * skb_abort_seq_read - Abort a sequential read of skb data
2820 * @st: state variable
2821 *
2822 * Must be called if skb_seq_read() was not called until it
2823 * returned 0.
2824 */
2825void skb_abort_seq_read(struct skb_seq_state *st)
2826{
2827 if (st->frag_data)
Eric Dumazet51c56b02012-04-05 11:35:15 +02002828 kunmap_atomic(st->frag_data);
Thomas Graf677e90e2005-06-23 20:59:51 -07002829}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002830EXPORT_SYMBOL(skb_abort_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07002831
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002832#define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
2833
2834static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
2835 struct ts_config *conf,
2836 struct ts_state *state)
2837{
2838 return skb_seq_read(offset, text, TS_SKB_CB(state));
2839}
2840
2841static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
2842{
2843 skb_abort_seq_read(TS_SKB_CB(state));
2844}
2845
2846/**
2847 * skb_find_text - Find a text pattern in skb data
2848 * @skb: the buffer to look in
2849 * @from: search offset
2850 * @to: search limit
2851 * @config: textsearch configuration
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002852 *
2853 * Finds a pattern in the skb data according to the specified
2854 * textsearch configuration. Use textsearch_next() to retrieve
2855 * subsequent occurrences of the pattern. Returns the offset
2856 * to the first occurrence or UINT_MAX if no match was found.
2857 */
2858unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
Bojan Prtvar059a2442015-02-22 11:46:35 +01002859 unsigned int to, struct ts_config *config)
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002860{
Bojan Prtvar059a2442015-02-22 11:46:35 +01002861 struct ts_state state;
Phil Oesterf72b9482006-06-26 00:00:57 -07002862 unsigned int ret;
2863
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002864 config->get_next_block = skb_ts_get_next_block;
2865 config->finish = skb_ts_finish;
2866
Bojan Prtvar059a2442015-02-22 11:46:35 +01002867 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(&state));
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002868
Bojan Prtvar059a2442015-02-22 11:46:35 +01002869 ret = textsearch_find(config, &state);
Phil Oesterf72b9482006-06-26 00:00:57 -07002870 return (ret <= to - from ? ret : UINT_MAX);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002871}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002872EXPORT_SYMBOL(skb_find_text);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002873
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002874/**
Ben Hutchings2c530402012-07-10 10:55:09 +00002875 * skb_append_datato_frags - append the user data to a skb
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002876 * @sk: sock structure
Masanari Iidae793c0f2014-09-04 23:44:36 +09002877 * @skb: skb structure to be appended with user data.
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002878 * @getfrag: call back function to be used for getting the user data
2879 * @from: pointer to user message iov
2880 * @length: length of the iov message
2881 *
2882 * Description: This procedure append the user data in the fragment part
2883 * of the skb if any page alloc fails user this procedure returns -ENOMEM
2884 */
2885int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
Martin Waitzdab96302005-12-05 13:40:12 -08002886 int (*getfrag)(void *from, char *to, int offset,
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002887 int len, int odd, struct sk_buff *skb),
2888 void *from, int length)
2889{
Eric Dumazetb2111722012-12-28 06:06:37 +00002890 int frg_cnt = skb_shinfo(skb)->nr_frags;
2891 int copy;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002892 int offset = 0;
2893 int ret;
Eric Dumazetb2111722012-12-28 06:06:37 +00002894 struct page_frag *pfrag = &current->task_frag;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002895
2896 do {
2897 /* Return error if we don't have space for new frag */
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002898 if (frg_cnt >= MAX_SKB_FRAGS)
Eric Dumazetb2111722012-12-28 06:06:37 +00002899 return -EMSGSIZE;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002900
Eric Dumazetb2111722012-12-28 06:06:37 +00002901 if (!sk_page_frag_refill(sk, pfrag))
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002902 return -ENOMEM;
2903
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002904 /* copy the user data to page */
Eric Dumazetb2111722012-12-28 06:06:37 +00002905 copy = min_t(int, length, pfrag->size - pfrag->offset);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002906
Eric Dumazetb2111722012-12-28 06:06:37 +00002907 ret = getfrag(from, page_address(pfrag->page) + pfrag->offset,
2908 offset, copy, 0, skb);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002909 if (ret < 0)
2910 return -EFAULT;
2911
2912 /* copy was successful so update the size parameters */
Eric Dumazetb2111722012-12-28 06:06:37 +00002913 skb_fill_page_desc(skb, frg_cnt, pfrag->page, pfrag->offset,
2914 copy);
2915 frg_cnt++;
2916 pfrag->offset += copy;
2917 get_page(pfrag->page);
2918
2919 skb->truesize += copy;
2920 atomic_add(copy, &sk->sk_wmem_alloc);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002921 skb->len += copy;
2922 skb->data_len += copy;
2923 offset += copy;
2924 length -= copy;
2925
2926 } while (length > 0);
2927
2928 return 0;
2929}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002930EXPORT_SYMBOL(skb_append_datato_frags);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002931
Hannes Frederic Sowabe12a1f2015-05-21 16:59:58 +02002932int skb_append_pagefrags(struct sk_buff *skb, struct page *page,
2933 int offset, size_t size)
2934{
2935 int i = skb_shinfo(skb)->nr_frags;
2936
2937 if (skb_can_coalesce(skb, i, page, offset)) {
2938 skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], size);
2939 } else if (i < MAX_SKB_FRAGS) {
2940 get_page(page);
2941 skb_fill_page_desc(skb, i, page, offset, size);
2942 } else {
2943 return -EMSGSIZE;
2944 }
2945
2946 return 0;
2947}
2948EXPORT_SYMBOL_GPL(skb_append_pagefrags);
2949
Herbert Xucbb042f92006-03-20 22:43:56 -08002950/**
Linus Lüssing9b368812016-02-24 04:21:42 +01002951 * skb_push_rcsum - push skb and update receive checksum
2952 * @skb: buffer to update
2953 * @len: length of data pulled
2954 *
2955 * This function performs an skb_push on the packet and updates
2956 * the CHECKSUM_COMPLETE checksum. It should be used on
2957 * receive path processing instead of skb_push unless you know
2958 * that the checksum difference is zero (e.g., a valid IP header)
2959 * or you are setting ip_summed to CHECKSUM_NONE.
2960 */
2961static unsigned char *skb_push_rcsum(struct sk_buff *skb, unsigned len)
2962{
2963 skb_push(skb, len);
2964 skb_postpush_rcsum(skb, skb->data, len);
2965 return skb->data;
2966}
2967
2968/**
Herbert Xucbb042f92006-03-20 22:43:56 -08002969 * skb_pull_rcsum - pull skb and update receive checksum
2970 * @skb: buffer to update
Herbert Xucbb042f92006-03-20 22:43:56 -08002971 * @len: length of data pulled
2972 *
2973 * This function performs an skb_pull on the packet and updates
Urs Thuermannfee54fa2008-02-12 22:03:25 -08002974 * the CHECKSUM_COMPLETE checksum. It should be used on
Patrick McHardy84fa7932006-08-29 16:44:56 -07002975 * receive path processing instead of skb_pull unless you know
2976 * that the checksum difference is zero (e.g., a valid IP header)
2977 * or you are setting ip_summed to CHECKSUM_NONE.
Herbert Xucbb042f92006-03-20 22:43:56 -08002978 */
2979unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
2980{
Pravin B Shelar31b33df2015-09-28 17:24:25 -07002981 unsigned char *data = skb->data;
2982
Herbert Xucbb042f92006-03-20 22:43:56 -08002983 BUG_ON(len > skb->len);
Pravin B Shelar31b33df2015-09-28 17:24:25 -07002984 __skb_pull(skb, len);
2985 skb_postpull_rcsum(skb, data, len);
2986 return skb->data;
Herbert Xucbb042f92006-03-20 22:43:56 -08002987}
Arnaldo Carvalho de Melof94691a2006-03-20 22:47:55 -08002988EXPORT_SYMBOL_GPL(skb_pull_rcsum);
2989
Herbert Xuf4c50d92006-06-22 03:02:40 -07002990/**
2991 * skb_segment - Perform protocol segmentation on skb.
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002992 * @head_skb: buffer to segment
Herbert Xu576a30e2006-06-27 13:22:38 -07002993 * @features: features for the output path (see dev->features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07002994 *
2995 * This function performs segmentation on the given skb. It returns
Ben Hutchings4c821d72008-04-13 21:52:48 -07002996 * a pointer to the first in a list of new skbs for the segments.
2997 * In case of error it returns ERR_PTR(err).
Herbert Xuf4c50d92006-06-22 03:02:40 -07002998 */
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002999struct sk_buff *skb_segment(struct sk_buff *head_skb,
3000 netdev_features_t features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07003001{
3002 struct sk_buff *segs = NULL;
3003 struct sk_buff *tail = NULL;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003004 struct sk_buff *list_skb = skb_shinfo(head_skb)->frag_list;
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003005 skb_frag_t *frag = skb_shinfo(head_skb)->frags;
3006 unsigned int mss = skb_shinfo(head_skb)->gso_size;
3007 unsigned int doffset = head_skb->data - skb_mac_header(head_skb);
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02003008 struct sk_buff *frag_skb = head_skb;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003009 unsigned int offset = doffset;
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003010 unsigned int tnl_hlen = skb_tnl_header_len(head_skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003011 unsigned int headroom;
3012 unsigned int len;
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003013 __be16 proto;
3014 bool csum;
Michał Mirosław04ed3e72011-01-24 15:32:47 -08003015 int sg = !!(features & NETIF_F_SG);
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003016 int nfrags = skb_shinfo(head_skb)->nr_frags;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003017 int err = -ENOMEM;
3018 int i = 0;
3019 int pos;
Vlad Yasevich53d64712014-03-27 17:26:18 -04003020 int dummy;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003021
Wei-Chun Chao5882a072014-06-08 23:48:54 -07003022 __skb_push(head_skb, doffset);
Vlad Yasevich53d64712014-03-27 17:26:18 -04003023 proto = skb_network_protocol(head_skb, &dummy);
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003024 if (unlikely(!proto))
3025 return ERR_PTR(-EINVAL);
3026
Tom Herbert7e2b10c2014-06-04 17:20:02 -07003027 csum = !head_skb->encap_hdr_csum &&
3028 !!can_checksum_protocol(features, proto);
3029
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003030 headroom = skb_headroom(head_skb);
3031 pos = skb_headlen(head_skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003032
3033 do {
3034 struct sk_buff *nskb;
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003035 skb_frag_t *nskb_frag;
Herbert Xuc8884ed2006-10-29 15:59:41 -08003036 int hsize;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003037 int size;
3038
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003039 len = head_skb->len - offset;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003040 if (len > mss)
3041 len = mss;
3042
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003043 hsize = skb_headlen(head_skb) - offset;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003044 if (hsize < 0)
3045 hsize = 0;
Herbert Xuc8884ed2006-10-29 15:59:41 -08003046 if (hsize > len || !sg)
3047 hsize = len;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003048
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003049 if (!hsize && i >= nfrags && skb_headlen(list_skb) &&
3050 (skb_headlen(list_skb) == len || sg)) {
3051 BUG_ON(skb_headlen(list_skb) > len);
Herbert Xu89319d382008-12-15 23:26:06 -08003052
Herbert Xu9d8506c2013-11-21 11:10:04 -08003053 i = 0;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003054 nfrags = skb_shinfo(list_skb)->nr_frags;
3055 frag = skb_shinfo(list_skb)->frags;
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02003056 frag_skb = list_skb;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003057 pos += skb_headlen(list_skb);
Herbert Xu9d8506c2013-11-21 11:10:04 -08003058
3059 while (pos < offset + len) {
3060 BUG_ON(i >= nfrags);
3061
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02003062 size = skb_frag_size(frag);
Herbert Xu9d8506c2013-11-21 11:10:04 -08003063 if (pos + size > offset + len)
3064 break;
3065
3066 i++;
3067 pos += size;
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02003068 frag++;
Herbert Xu9d8506c2013-11-21 11:10:04 -08003069 }
3070
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003071 nskb = skb_clone(list_skb, GFP_ATOMIC);
3072 list_skb = list_skb->next;
Herbert Xu89319d382008-12-15 23:26:06 -08003073
3074 if (unlikely(!nskb))
3075 goto err;
3076
Herbert Xu9d8506c2013-11-21 11:10:04 -08003077 if (unlikely(pskb_trim(nskb, len))) {
3078 kfree_skb(nskb);
3079 goto err;
3080 }
3081
Alexander Duyckec47ea82012-05-04 14:26:56 +00003082 hsize = skb_end_offset(nskb);
Herbert Xu89319d382008-12-15 23:26:06 -08003083 if (skb_cow_head(nskb, doffset + headroom)) {
3084 kfree_skb(nskb);
3085 goto err;
3086 }
3087
Alexander Duyckec47ea82012-05-04 14:26:56 +00003088 nskb->truesize += skb_end_offset(nskb) - hsize;
Herbert Xu89319d382008-12-15 23:26:06 -08003089 skb_release_head_state(nskb);
3090 __skb_push(nskb, doffset);
3091 } else {
Mel Gormanc93bdd02012-07-31 16:44:19 -07003092 nskb = __alloc_skb(hsize + doffset + headroom,
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003093 GFP_ATOMIC, skb_alloc_rx_flag(head_skb),
Mel Gormanc93bdd02012-07-31 16:44:19 -07003094 NUMA_NO_NODE);
Herbert Xu89319d382008-12-15 23:26:06 -08003095
3096 if (unlikely(!nskb))
3097 goto err;
3098
3099 skb_reserve(nskb, headroom);
3100 __skb_put(nskb, doffset);
3101 }
Herbert Xuf4c50d92006-06-22 03:02:40 -07003102
3103 if (segs)
3104 tail->next = nskb;
3105 else
3106 segs = nskb;
3107 tail = nskb;
3108
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003109 __copy_skb_header(nskb, head_skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003110
Eric Dumazet030737b2013-10-19 11:42:54 -07003111 skb_headers_offset_update(nskb, skb_headroom(nskb) - headroom);
Vlad Yasevichfcdfe3a2014-07-31 10:33:06 -04003112 skb_reset_mac_len(nskb);
Pravin B Shelar68c33162013-02-14 14:02:41 +00003113
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003114 skb_copy_from_linear_data_offset(head_skb, -tnl_hlen,
Pravin B Shelar68c33162013-02-14 14:02:41 +00003115 nskb->data - tnl_hlen,
3116 doffset + tnl_hlen);
Herbert Xu89319d382008-12-15 23:26:06 -08003117
Herbert Xu9d8506c2013-11-21 11:10:04 -08003118 if (nskb->len == len + doffset)
Simon Horman1cdbcb72013-05-19 15:46:49 +00003119 goto perform_csum_check;
Herbert Xu89319d382008-12-15 23:26:06 -08003120
Tom Herberte585f232014-11-04 09:06:54 -08003121 if (!sg && !nskb->remcsum_offload) {
Herbert Xu6f85a122008-08-15 14:55:02 -07003122 nskb->ip_summed = CHECKSUM_NONE;
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003123 nskb->csum = skb_copy_and_csum_bits(head_skb, offset,
Herbert Xuf4c50d92006-06-22 03:02:40 -07003124 skb_put(nskb, len),
3125 len, 0);
Tom Herbert7e2b10c2014-06-04 17:20:02 -07003126 SKB_GSO_CB(nskb)->csum_start =
Tom Herbertde843722014-06-25 12:51:01 -07003127 skb_headroom(nskb) + doffset;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003128 continue;
3129 }
3130
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003131 nskb_frag = skb_shinfo(nskb)->frags;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003132
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003133 skb_copy_from_linear_data_offset(head_skb, offset,
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03003134 skb_put(nskb, hsize), hsize);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003135
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003136 skb_shinfo(nskb)->tx_flags = skb_shinfo(head_skb)->tx_flags &
3137 SKBTX_SHARED_FRAG;
Eric Dumazetcef401d2013-01-25 20:34:37 +00003138
Herbert Xu9d8506c2013-11-21 11:10:04 -08003139 while (pos < offset + len) {
3140 if (i >= nfrags) {
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003141 BUG_ON(skb_headlen(list_skb));
Herbert Xu9d8506c2013-11-21 11:10:04 -08003142
3143 i = 0;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003144 nfrags = skb_shinfo(list_skb)->nr_frags;
3145 frag = skb_shinfo(list_skb)->frags;
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02003146 frag_skb = list_skb;
Herbert Xu9d8506c2013-11-21 11:10:04 -08003147
3148 BUG_ON(!nfrags);
3149
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003150 list_skb = list_skb->next;
Herbert Xu9d8506c2013-11-21 11:10:04 -08003151 }
3152
3153 if (unlikely(skb_shinfo(nskb)->nr_frags >=
3154 MAX_SKB_FRAGS)) {
3155 net_warn_ratelimited(
3156 "skb_segment: too many frags: %u %u\n",
3157 pos, mss);
3158 goto err;
3159 }
3160
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02003161 if (unlikely(skb_orphan_frags(frag_skb, GFP_ATOMIC)))
3162 goto err;
3163
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02003164 *nskb_frag = *frag;
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003165 __skb_frag_ref(nskb_frag);
3166 size = skb_frag_size(nskb_frag);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003167
3168 if (pos < offset) {
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003169 nskb_frag->page_offset += offset - pos;
3170 skb_frag_size_sub(nskb_frag, offset - pos);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003171 }
3172
Herbert Xu89319d382008-12-15 23:26:06 -08003173 skb_shinfo(nskb)->nr_frags++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003174
3175 if (pos + size <= offset + len) {
3176 i++;
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02003177 frag++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003178 pos += size;
3179 } else {
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003180 skb_frag_size_sub(nskb_frag, pos + size - (offset + len));
Herbert Xu89319d382008-12-15 23:26:06 -08003181 goto skip_fraglist;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003182 }
3183
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003184 nskb_frag++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003185 }
3186
Herbert Xu89319d382008-12-15 23:26:06 -08003187skip_fraglist:
Herbert Xuf4c50d92006-06-22 03:02:40 -07003188 nskb->data_len = len - hsize;
3189 nskb->len += nskb->data_len;
3190 nskb->truesize += nskb->data_len;
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003191
Simon Horman1cdbcb72013-05-19 15:46:49 +00003192perform_csum_check:
Tom Herberte585f232014-11-04 09:06:54 -08003193 if (!csum && !nskb->remcsum_offload) {
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003194 nskb->csum = skb_checksum(nskb, doffset,
3195 nskb->len - doffset, 0);
3196 nskb->ip_summed = CHECKSUM_NONE;
Tom Herbert7e2b10c2014-06-04 17:20:02 -07003197 SKB_GSO_CB(nskb)->csum_start =
3198 skb_headroom(nskb) + doffset;
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003199 }
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003200 } while ((offset += len) < head_skb->len);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003201
Eric Dumazetbec3cfd2014-10-03 20:59:19 -07003202 /* Some callers want to get the end of the list.
3203 * Put it in segs->prev to avoid walking the list.
3204 * (see validate_xmit_skb_list() for example)
3205 */
3206 segs->prev = tail;
Toshiaki Makita432c8562014-10-27 10:30:51 -07003207
3208 /* Following permits correct backpressure, for protocols
3209 * using skb_set_owner_w().
3210 * Idea is to tranfert ownership from head_skb to last segment.
3211 */
3212 if (head_skb->destructor == sock_wfree) {
3213 swap(tail->truesize, head_skb->truesize);
3214 swap(tail->destructor, head_skb->destructor);
3215 swap(tail->sk, head_skb->sk);
3216 }
Herbert Xuf4c50d92006-06-22 03:02:40 -07003217 return segs;
3218
3219err:
Eric Dumazet289dccb2013-12-20 14:29:08 -08003220 kfree_skb_list(segs);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003221 return ERR_PTR(err);
3222}
Herbert Xuf4c50d92006-06-22 03:02:40 -07003223EXPORT_SYMBOL_GPL(skb_segment);
3224
Herbert Xu71d93b32008-12-15 23:42:33 -08003225int skb_gro_receive(struct sk_buff **head, struct sk_buff *skb)
3226{
Eric Dumazet8a291112013-10-08 09:02:23 -07003227 struct skb_shared_info *pinfo, *skbinfo = skb_shinfo(skb);
Herbert Xu67147ba2009-05-26 18:50:22 +00003228 unsigned int offset = skb_gro_offset(skb);
3229 unsigned int headlen = skb_headlen(skb);
Eric Dumazet8a291112013-10-08 09:02:23 -07003230 unsigned int len = skb_gro_len(skb);
Eric Dumazet58025e42015-03-05 13:47:48 -08003231 struct sk_buff *lp, *p = *head;
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003232 unsigned int delta_truesize;
Herbert Xu71d93b32008-12-15 23:42:33 -08003233
Eric Dumazet8a291112013-10-08 09:02:23 -07003234 if (unlikely(p->len + len >= 65536))
Herbert Xu71d93b32008-12-15 23:42:33 -08003235 return -E2BIG;
3236
Eric Dumazet29e98242014-05-16 11:34:37 -07003237 lp = NAPI_GRO_CB(p)->last;
Eric Dumazet8a291112013-10-08 09:02:23 -07003238 pinfo = skb_shinfo(lp);
3239
3240 if (headlen <= offset) {
Herbert Xu42da6992009-05-26 18:50:19 +00003241 skb_frag_t *frag;
Herbert Xu66e92fc2009-05-26 18:50:32 +00003242 skb_frag_t *frag2;
Herbert Xu9aaa1562009-05-26 18:50:33 +00003243 int i = skbinfo->nr_frags;
3244 int nr_frags = pinfo->nr_frags + i;
Herbert Xu42da6992009-05-26 18:50:19 +00003245
Herbert Xu66e92fc2009-05-26 18:50:32 +00003246 if (nr_frags > MAX_SKB_FRAGS)
Eric Dumazet8a291112013-10-08 09:02:23 -07003247 goto merge;
Herbert Xu81705ad2009-01-29 14:19:51 +00003248
Eric Dumazet8a291112013-10-08 09:02:23 -07003249 offset -= headlen;
Herbert Xu9aaa1562009-05-26 18:50:33 +00003250 pinfo->nr_frags = nr_frags;
3251 skbinfo->nr_frags = 0;
Herbert Xuf5572062009-01-14 20:40:03 -08003252
Herbert Xu9aaa1562009-05-26 18:50:33 +00003253 frag = pinfo->frags + nr_frags;
3254 frag2 = skbinfo->frags + i;
Herbert Xu66e92fc2009-05-26 18:50:32 +00003255 do {
3256 *--frag = *--frag2;
3257 } while (--i);
3258
3259 frag->page_offset += offset;
Eric Dumazet9e903e02011-10-18 21:00:24 +00003260 skb_frag_size_sub(frag, offset);
Herbert Xu66e92fc2009-05-26 18:50:32 +00003261
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003262 /* all fragments truesize : remove (head size + sk_buff) */
Alexander Duyckec47ea82012-05-04 14:26:56 +00003263 delta_truesize = skb->truesize -
3264 SKB_TRUESIZE(skb_end_offset(skb));
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003265
Herbert Xuf5572062009-01-14 20:40:03 -08003266 skb->truesize -= skb->data_len;
3267 skb->len -= skb->data_len;
3268 skb->data_len = 0;
3269
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003270 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE;
Herbert Xu5d38a072009-01-04 16:13:40 -08003271 goto done;
Eric Dumazetd7e88832012-04-30 08:10:34 +00003272 } else if (skb->head_frag) {
3273 int nr_frags = pinfo->nr_frags;
3274 skb_frag_t *frag = pinfo->frags + nr_frags;
3275 struct page *page = virt_to_head_page(skb->head);
3276 unsigned int first_size = headlen - offset;
3277 unsigned int first_offset;
3278
3279 if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS)
Eric Dumazet8a291112013-10-08 09:02:23 -07003280 goto merge;
Eric Dumazetd7e88832012-04-30 08:10:34 +00003281
3282 first_offset = skb->data -
3283 (unsigned char *)page_address(page) +
3284 offset;
3285
3286 pinfo->nr_frags = nr_frags + 1 + skbinfo->nr_frags;
3287
3288 frag->page.p = page;
3289 frag->page_offset = first_offset;
3290 skb_frag_size_set(frag, first_size);
3291
3292 memcpy(frag + 1, skbinfo->frags, sizeof(*frag) * skbinfo->nr_frags);
3293 /* We dont need to clear skbinfo->nr_frags here */
3294
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003295 delta_truesize = skb->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
Eric Dumazetd7e88832012-04-30 08:10:34 +00003296 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD;
3297 goto done;
Eric Dumazet8a291112013-10-08 09:02:23 -07003298 }
Herbert Xu71d93b32008-12-15 23:42:33 -08003299
3300merge:
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003301 delta_truesize = skb->truesize;
Herbert Xu67147ba2009-05-26 18:50:22 +00003302 if (offset > headlen) {
Michal Schmidtd1dc7ab2011-01-24 12:08:48 +00003303 unsigned int eat = offset - headlen;
3304
3305 skbinfo->frags[0].page_offset += eat;
Eric Dumazet9e903e02011-10-18 21:00:24 +00003306 skb_frag_size_sub(&skbinfo->frags[0], eat);
Michal Schmidtd1dc7ab2011-01-24 12:08:48 +00003307 skb->data_len -= eat;
3308 skb->len -= eat;
Herbert Xu67147ba2009-05-26 18:50:22 +00003309 offset = headlen;
Herbert Xu56035022009-02-05 21:26:52 -08003310 }
3311
Herbert Xu67147ba2009-05-26 18:50:22 +00003312 __skb_pull(skb, offset);
Herbert Xu56035022009-02-05 21:26:52 -08003313
Eric Dumazet29e98242014-05-16 11:34:37 -07003314 if (NAPI_GRO_CB(p)->last == p)
Eric Dumazet8a291112013-10-08 09:02:23 -07003315 skb_shinfo(p)->frag_list = skb;
3316 else
3317 NAPI_GRO_CB(p)->last->next = skb;
Eric Dumazetc3c7c252012-12-06 13:54:59 +00003318 NAPI_GRO_CB(p)->last = skb;
Eric Dumazetf4a775d2014-09-22 16:29:32 -07003319 __skb_header_release(skb);
Eric Dumazet8a291112013-10-08 09:02:23 -07003320 lp = p;
Herbert Xu71d93b32008-12-15 23:42:33 -08003321
Herbert Xu5d38a072009-01-04 16:13:40 -08003322done:
3323 NAPI_GRO_CB(p)->count++;
Herbert Xu37fe4732009-01-17 19:48:13 +00003324 p->data_len += len;
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003325 p->truesize += delta_truesize;
Herbert Xu37fe4732009-01-17 19:48:13 +00003326 p->len += len;
Eric Dumazet8a291112013-10-08 09:02:23 -07003327 if (lp != p) {
3328 lp->data_len += len;
3329 lp->truesize += delta_truesize;
3330 lp->len += len;
3331 }
Herbert Xu71d93b32008-12-15 23:42:33 -08003332 NAPI_GRO_CB(skb)->same_flow = 1;
3333 return 0;
3334}
Herbert Xu71d93b32008-12-15 23:42:33 -08003335
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336void __init skb_init(void)
3337{
3338 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
3339 sizeof(struct sk_buff),
3340 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07003341 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09003342 NULL);
David S. Millerd179cd12005-08-17 14:57:30 -07003343 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
Eric Dumazetd0bf4a92014-09-29 13:29:15 -07003344 sizeof(struct sk_buff_fclones),
David S. Millerd179cd12005-08-17 14:57:30 -07003345 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07003346 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09003347 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348}
3349
David Howells716ea3a2007-04-02 20:19:53 -07003350/**
3351 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
3352 * @skb: Socket buffer containing the buffers to be mapped
3353 * @sg: The scatter-gather list to map into
3354 * @offset: The offset into the buffer's contents to start mapping
3355 * @len: Length of buffer space to be mapped
3356 *
3357 * Fill the specified scatter-gather list with mappings/pointers into a
3358 * region of the buffer space attached to a socket buffer.
3359 */
David S. Miller51c739d2007-10-30 21:29:29 -07003360static int
3361__skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
David Howells716ea3a2007-04-02 20:19:53 -07003362{
David S. Miller1a028e52007-04-27 15:21:23 -07003363 int start = skb_headlen(skb);
3364 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07003365 struct sk_buff *frag_iter;
David Howells716ea3a2007-04-02 20:19:53 -07003366 int elt = 0;
3367
3368 if (copy > 0) {
3369 if (copy > len)
3370 copy = len;
Jens Axboe642f1492007-10-24 11:20:47 +02003371 sg_set_buf(sg, skb->data + offset, copy);
David Howells716ea3a2007-04-02 20:19:53 -07003372 elt++;
3373 if ((len -= copy) == 0)
3374 return elt;
3375 offset += copy;
3376 }
3377
3378 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07003379 int end;
David Howells716ea3a2007-04-02 20:19:53 -07003380
Ilpo Järvinen547b7922008-07-25 21:43:18 -07003381 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07003382
Eric Dumazet9e903e02011-10-18 21:00:24 +00003383 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
David Howells716ea3a2007-04-02 20:19:53 -07003384 if ((copy = end - offset) > 0) {
3385 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
3386
3387 if (copy > len)
3388 copy = len;
Ian Campbellea2ab692011-08-22 23:44:58 +00003389 sg_set_page(&sg[elt], skb_frag_page(frag), copy,
Jens Axboe642f1492007-10-24 11:20:47 +02003390 frag->page_offset+offset-start);
David Howells716ea3a2007-04-02 20:19:53 -07003391 elt++;
3392 if (!(len -= copy))
3393 return elt;
3394 offset += copy;
3395 }
David S. Miller1a028e52007-04-27 15:21:23 -07003396 start = end;
David Howells716ea3a2007-04-02 20:19:53 -07003397 }
3398
David S. Millerfbb398a2009-06-09 00:18:59 -07003399 skb_walk_frags(skb, frag_iter) {
3400 int end;
David Howells716ea3a2007-04-02 20:19:53 -07003401
David S. Millerfbb398a2009-06-09 00:18:59 -07003402 WARN_ON(start > offset + len);
David Howells716ea3a2007-04-02 20:19:53 -07003403
David S. Millerfbb398a2009-06-09 00:18:59 -07003404 end = start + frag_iter->len;
3405 if ((copy = end - offset) > 0) {
3406 if (copy > len)
3407 copy = len;
3408 elt += __skb_to_sgvec(frag_iter, sg+elt, offset - start,
3409 copy);
3410 if ((len -= copy) == 0)
3411 return elt;
3412 offset += copy;
David Howells716ea3a2007-04-02 20:19:53 -07003413 }
David S. Millerfbb398a2009-06-09 00:18:59 -07003414 start = end;
David Howells716ea3a2007-04-02 20:19:53 -07003415 }
3416 BUG_ON(len);
3417 return elt;
3418}
3419
Fan Du25a91d82014-01-18 09:54:23 +08003420/* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given
3421 * sglist without mark the sg which contain last skb data as the end.
3422 * So the caller can mannipulate sg list as will when padding new data after
3423 * the first call without calling sg_unmark_end to expend sg list.
3424 *
3425 * Scenario to use skb_to_sgvec_nomark:
3426 * 1. sg_init_table
3427 * 2. skb_to_sgvec_nomark(payload1)
3428 * 3. skb_to_sgvec_nomark(payload2)
3429 *
3430 * This is equivalent to:
3431 * 1. sg_init_table
3432 * 2. skb_to_sgvec(payload1)
3433 * 3. sg_unmark_end
3434 * 4. skb_to_sgvec(payload2)
3435 *
3436 * When mapping mutilple payload conditionally, skb_to_sgvec_nomark
3437 * is more preferable.
3438 */
3439int skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg,
3440 int offset, int len)
3441{
3442 return __skb_to_sgvec(skb, sg, offset, len);
3443}
3444EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark);
3445
David S. Miller51c739d2007-10-30 21:29:29 -07003446int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
3447{
3448 int nsg = __skb_to_sgvec(skb, sg, offset, len);
3449
Jens Axboec46f2332007-10-31 12:06:37 +01003450 sg_mark_end(&sg[nsg - 1]);
David S. Miller51c739d2007-10-30 21:29:29 -07003451
3452 return nsg;
3453}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003454EXPORT_SYMBOL_GPL(skb_to_sgvec);
David S. Miller51c739d2007-10-30 21:29:29 -07003455
David Howells716ea3a2007-04-02 20:19:53 -07003456/**
3457 * skb_cow_data - Check that a socket buffer's data buffers are writable
3458 * @skb: The socket buffer to check.
3459 * @tailbits: Amount of trailing space to be added
3460 * @trailer: Returned pointer to the skb where the @tailbits space begins
3461 *
3462 * Make sure that the data buffers attached to a socket buffer are
3463 * writable. If they are not, private copies are made of the data buffers
3464 * and the socket buffer is set to use these instead.
3465 *
3466 * If @tailbits is given, make sure that there is space to write @tailbits
3467 * bytes of data beyond current end of socket buffer. @trailer will be
3468 * set to point to the skb in which this space begins.
3469 *
3470 * The number of scatterlist elements required to completely map the
3471 * COW'd and extended socket buffer will be returned.
3472 */
3473int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
3474{
3475 int copyflag;
3476 int elt;
3477 struct sk_buff *skb1, **skb_p;
3478
3479 /* If skb is cloned or its head is paged, reallocate
3480 * head pulling out all the pages (pages are considered not writable
3481 * at the moment even if they are anonymous).
3482 */
3483 if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
3484 __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
3485 return -ENOMEM;
3486
3487 /* Easy case. Most of packets will go this way. */
David S. Miller21dc3302010-08-23 00:13:46 -07003488 if (!skb_has_frag_list(skb)) {
David Howells716ea3a2007-04-02 20:19:53 -07003489 /* A little of trouble, not enough of space for trailer.
3490 * This should not happen, when stack is tuned to generate
3491 * good frames. OK, on miss we reallocate and reserve even more
3492 * space, 128 bytes is fair. */
3493
3494 if (skb_tailroom(skb) < tailbits &&
3495 pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
3496 return -ENOMEM;
3497
3498 /* Voila! */
3499 *trailer = skb;
3500 return 1;
3501 }
3502
3503 /* Misery. We are in troubles, going to mincer fragments... */
3504
3505 elt = 1;
3506 skb_p = &skb_shinfo(skb)->frag_list;
3507 copyflag = 0;
3508
3509 while ((skb1 = *skb_p) != NULL) {
3510 int ntail = 0;
3511
3512 /* The fragment is partially pulled by someone,
3513 * this can happen on input. Copy it and everything
3514 * after it. */
3515
3516 if (skb_shared(skb1))
3517 copyflag = 1;
3518
3519 /* If the skb is the last, worry about trailer. */
3520
3521 if (skb1->next == NULL && tailbits) {
3522 if (skb_shinfo(skb1)->nr_frags ||
David S. Miller21dc3302010-08-23 00:13:46 -07003523 skb_has_frag_list(skb1) ||
David Howells716ea3a2007-04-02 20:19:53 -07003524 skb_tailroom(skb1) < tailbits)
3525 ntail = tailbits + 128;
3526 }
3527
3528 if (copyflag ||
3529 skb_cloned(skb1) ||
3530 ntail ||
3531 skb_shinfo(skb1)->nr_frags ||
David S. Miller21dc3302010-08-23 00:13:46 -07003532 skb_has_frag_list(skb1)) {
David Howells716ea3a2007-04-02 20:19:53 -07003533 struct sk_buff *skb2;
3534
3535 /* Fuck, we are miserable poor guys... */
3536 if (ntail == 0)
3537 skb2 = skb_copy(skb1, GFP_ATOMIC);
3538 else
3539 skb2 = skb_copy_expand(skb1,
3540 skb_headroom(skb1),
3541 ntail,
3542 GFP_ATOMIC);
3543 if (unlikely(skb2 == NULL))
3544 return -ENOMEM;
3545
3546 if (skb1->sk)
3547 skb_set_owner_w(skb2, skb1->sk);
3548
3549 /* Looking around. Are we still alive?
3550 * OK, link new skb, drop old one */
3551
3552 skb2->next = skb1->next;
3553 *skb_p = skb2;
3554 kfree_skb(skb1);
3555 skb1 = skb2;
3556 }
3557 elt++;
3558 *trailer = skb1;
3559 skb_p = &skb1->next;
3560 }
3561
3562 return elt;
3563}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003564EXPORT_SYMBOL_GPL(skb_cow_data);
David Howells716ea3a2007-04-02 20:19:53 -07003565
Eric Dumazetb1faf562010-05-31 23:44:05 -07003566static void sock_rmem_free(struct sk_buff *skb)
3567{
3568 struct sock *sk = skb->sk;
3569
3570 atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
3571}
3572
3573/*
3574 * Note: We dont mem charge error packets (no sk_forward_alloc changes)
3575 */
3576int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb)
3577{
3578 if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
Eric Dumazet95c96172012-04-15 05:58:06 +00003579 (unsigned int)sk->sk_rcvbuf)
Eric Dumazetb1faf562010-05-31 23:44:05 -07003580 return -ENOMEM;
3581
3582 skb_orphan(skb);
3583 skb->sk = sk;
3584 skb->destructor = sock_rmem_free;
3585 atomic_add(skb->truesize, &sk->sk_rmem_alloc);
3586
Eric Dumazetabb57ea2011-05-18 02:21:31 -04003587 /* before exiting rcu section, make sure dst is refcounted */
3588 skb_dst_force(skb);
3589
Eric Dumazetb1faf562010-05-31 23:44:05 -07003590 skb_queue_tail(&sk->sk_error_queue, skb);
3591 if (!sock_flag(sk, SOCK_DEAD))
David S. Miller676d2362014-04-11 16:15:36 -04003592 sk->sk_data_ready(sk);
Eric Dumazetb1faf562010-05-31 23:44:05 -07003593 return 0;
3594}
3595EXPORT_SYMBOL(sock_queue_err_skb);
3596
Willem de Bruijn364a9e92014-08-31 21:30:27 -04003597struct sk_buff *sock_dequeue_err_skb(struct sock *sk)
3598{
3599 struct sk_buff_head *q = &sk->sk_error_queue;
3600 struct sk_buff *skb, *skb_next;
Eric Dumazet997d5c32015-02-18 05:47:55 -08003601 unsigned long flags;
Willem de Bruijn364a9e92014-08-31 21:30:27 -04003602 int err = 0;
3603
Eric Dumazet997d5c32015-02-18 05:47:55 -08003604 spin_lock_irqsave(&q->lock, flags);
Willem de Bruijn364a9e92014-08-31 21:30:27 -04003605 skb = __skb_dequeue(q);
3606 if (skb && (skb_next = skb_peek(q)))
3607 err = SKB_EXT_ERR(skb_next)->ee.ee_errno;
Eric Dumazet997d5c32015-02-18 05:47:55 -08003608 spin_unlock_irqrestore(&q->lock, flags);
Willem de Bruijn364a9e92014-08-31 21:30:27 -04003609
3610 sk->sk_err = err;
3611 if (err)
3612 sk->sk_error_report(sk);
3613
3614 return skb;
3615}
3616EXPORT_SYMBOL(sock_dequeue_err_skb);
3617
Alexander Duyckcab41c42014-09-10 18:05:26 -04003618/**
3619 * skb_clone_sk - create clone of skb, and take reference to socket
3620 * @skb: the skb to clone
3621 *
3622 * This function creates a clone of a buffer that holds a reference on
3623 * sk_refcnt. Buffers created via this function are meant to be
3624 * returned using sock_queue_err_skb, or free via kfree_skb.
3625 *
3626 * When passing buffers allocated with this function to sock_queue_err_skb
3627 * it is necessary to wrap the call with sock_hold/sock_put in order to
3628 * prevent the socket from being released prior to being enqueued on
3629 * the sk_error_queue.
3630 */
Alexander Duyck62bccb82014-09-04 13:31:35 -04003631struct sk_buff *skb_clone_sk(struct sk_buff *skb)
3632{
3633 struct sock *sk = skb->sk;
3634 struct sk_buff *clone;
3635
3636 if (!sk || !atomic_inc_not_zero(&sk->sk_refcnt))
3637 return NULL;
3638
3639 clone = skb_clone(skb, GFP_ATOMIC);
3640 if (!clone) {
3641 sock_put(sk);
3642 return NULL;
3643 }
3644
3645 clone->sk = sk;
3646 clone->destructor = sock_efree;
3647
3648 return clone;
3649}
3650EXPORT_SYMBOL(skb_clone_sk);
3651
Alexander Duyck37846ef2014-09-04 13:31:10 -04003652static void __skb_complete_tx_timestamp(struct sk_buff *skb,
3653 struct sock *sk,
3654 int tstype)
Patrick Ohlyac45f602009-02-12 05:03:37 +00003655{
Patrick Ohlyac45f602009-02-12 05:03:37 +00003656 struct sock_exterr_skb *serr;
Patrick Ohlyac45f602009-02-12 05:03:37 +00003657 int err;
3658
Patrick Ohlyac45f602009-02-12 05:03:37 +00003659 serr = SKB_EXT_ERR(skb);
3660 memset(serr, 0, sizeof(*serr));
3661 serr->ee.ee_errno = ENOMSG;
3662 serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
Willem de Bruijne7fd2882014-08-04 22:11:48 -04003663 serr->ee.ee_info = tstype;
Willem de Bruijn4ed2d762014-08-04 22:11:49 -04003664 if (sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) {
Willem de Bruijn09c2d252014-08-04 22:11:47 -04003665 serr->ee.ee_data = skb_shinfo(skb)->tskey;
WANG Congac5cc972015-12-16 23:39:04 -08003666 if (sk->sk_protocol == IPPROTO_TCP &&
3667 sk->sk_type == SOCK_STREAM)
Willem de Bruijn4ed2d762014-08-04 22:11:49 -04003668 serr->ee.ee_data -= sk->sk_tskey;
3669 }
Eric Dumazet29030372010-05-29 00:20:48 -07003670
Patrick Ohlyac45f602009-02-12 05:03:37 +00003671 err = sock_queue_err_skb(sk, skb);
Eric Dumazet29030372010-05-29 00:20:48 -07003672
Patrick Ohlyac45f602009-02-12 05:03:37 +00003673 if (err)
3674 kfree_skb(skb);
3675}
Alexander Duyck37846ef2014-09-04 13:31:10 -04003676
Willem de Bruijnb245be12015-01-30 13:29:32 -05003677static bool skb_may_tx_timestamp(struct sock *sk, bool tsonly)
3678{
3679 bool ret;
3680
3681 if (likely(sysctl_tstamp_allow_data || tsonly))
3682 return true;
3683
3684 read_lock_bh(&sk->sk_callback_lock);
3685 ret = sk->sk_socket && sk->sk_socket->file &&
3686 file_ns_capable(sk->sk_socket->file, &init_user_ns, CAP_NET_RAW);
3687 read_unlock_bh(&sk->sk_callback_lock);
3688 return ret;
3689}
3690
Alexander Duyck37846ef2014-09-04 13:31:10 -04003691void skb_complete_tx_timestamp(struct sk_buff *skb,
3692 struct skb_shared_hwtstamps *hwtstamps)
3693{
3694 struct sock *sk = skb->sk;
3695
Willem de Bruijnb245be12015-01-30 13:29:32 -05003696 if (!skb_may_tx_timestamp(sk, false))
3697 return;
3698
Alexander Duyck62bccb82014-09-04 13:31:35 -04003699 /* take a reference to prevent skb_orphan() from freeing the socket */
3700 sock_hold(sk);
Alexander Duyck37846ef2014-09-04 13:31:10 -04003701
Alexander Duyck62bccb82014-09-04 13:31:35 -04003702 *skb_hwtstamps(skb) = *hwtstamps;
3703 __skb_complete_tx_timestamp(skb, sk, SCM_TSTAMP_SND);
Alexander Duyck37846ef2014-09-04 13:31:10 -04003704
3705 sock_put(sk);
3706}
3707EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp);
3708
3709void __skb_tstamp_tx(struct sk_buff *orig_skb,
3710 struct skb_shared_hwtstamps *hwtstamps,
3711 struct sock *sk, int tstype)
3712{
3713 struct sk_buff *skb;
Willem de Bruijn3a8dd972015-03-11 15:43:55 -04003714 bool tsonly;
Alexander Duyck37846ef2014-09-04 13:31:10 -04003715
Willem de Bruijn3a8dd972015-03-11 15:43:55 -04003716 if (!sk)
3717 return;
3718
3719 tsonly = sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TSONLY;
3720 if (!skb_may_tx_timestamp(sk, tsonly))
Alexander Duyck37846ef2014-09-04 13:31:10 -04003721 return;
3722
Willem de Bruijn49ca0d82015-01-30 13:29:31 -05003723 if (tsonly)
3724 skb = alloc_skb(0, GFP_ATOMIC);
Alexander Duyck37846ef2014-09-04 13:31:10 -04003725 else
Willem de Bruijn49ca0d82015-01-30 13:29:31 -05003726 skb = skb_clone(orig_skb, GFP_ATOMIC);
Alexander Duyck37846ef2014-09-04 13:31:10 -04003727 if (!skb)
3728 return;
3729
Willem de Bruijn49ca0d82015-01-30 13:29:31 -05003730 if (tsonly) {
3731 skb_shinfo(skb)->tx_flags = skb_shinfo(orig_skb)->tx_flags;
3732 skb_shinfo(skb)->tskey = skb_shinfo(orig_skb)->tskey;
3733 }
3734
3735 if (hwtstamps)
3736 *skb_hwtstamps(skb) = *hwtstamps;
3737 else
3738 skb->tstamp = ktime_get_real();
3739
Alexander Duyck37846ef2014-09-04 13:31:10 -04003740 __skb_complete_tx_timestamp(skb, sk, tstype);
3741}
Willem de Bruijne7fd2882014-08-04 22:11:48 -04003742EXPORT_SYMBOL_GPL(__skb_tstamp_tx);
3743
3744void skb_tstamp_tx(struct sk_buff *orig_skb,
3745 struct skb_shared_hwtstamps *hwtstamps)
3746{
3747 return __skb_tstamp_tx(orig_skb, hwtstamps, orig_skb->sk,
3748 SCM_TSTAMP_SND);
3749}
Patrick Ohlyac45f602009-02-12 05:03:37 +00003750EXPORT_SYMBOL_GPL(skb_tstamp_tx);
3751
Johannes Berg6e3e9392011-11-09 10:15:42 +01003752void skb_complete_wifi_ack(struct sk_buff *skb, bool acked)
3753{
3754 struct sock *sk = skb->sk;
3755 struct sock_exterr_skb *serr;
3756 int err;
3757
3758 skb->wifi_acked_valid = 1;
3759 skb->wifi_acked = acked;
3760
3761 serr = SKB_EXT_ERR(skb);
3762 memset(serr, 0, sizeof(*serr));
3763 serr->ee.ee_errno = ENOMSG;
3764 serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS;
3765
Alexander Duyckbf7fa552014-09-10 18:05:42 -04003766 /* take a reference to prevent skb_orphan() from freeing the socket */
3767 sock_hold(sk);
3768
Johannes Berg6e3e9392011-11-09 10:15:42 +01003769 err = sock_queue_err_skb(sk, skb);
3770 if (err)
3771 kfree_skb(skb);
Alexander Duyckbf7fa552014-09-10 18:05:42 -04003772
3773 sock_put(sk);
Johannes Berg6e3e9392011-11-09 10:15:42 +01003774}
3775EXPORT_SYMBOL_GPL(skb_complete_wifi_ack);
3776
Rusty Russellf35d9d82008-02-04 23:49:54 -05003777/**
3778 * skb_partial_csum_set - set up and verify partial csum values for packet
3779 * @skb: the skb to set
3780 * @start: the number of bytes after skb->data to start checksumming.
3781 * @off: the offset from start to place the checksum.
3782 *
3783 * For untrusted partially-checksummed packets, we need to make sure the values
3784 * for skb->csum_start and skb->csum_offset are valid so we don't oops.
3785 *
3786 * This function checks and sets those values and skb->ip_summed: if this
3787 * returns false you should drop the packet.
3788 */
3789bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
3790{
Herbert Xu5ff8dda2009-06-04 01:22:01 +00003791 if (unlikely(start > skb_headlen(skb)) ||
3792 unlikely((int)start + off > skb_headlen(skb) - 2)) {
Joe Perchese87cc472012-05-13 21:56:26 +00003793 net_warn_ratelimited("bad partial csum: csum=%u/%u len=%u\n",
3794 start, off, skb_headlen(skb));
Rusty Russellf35d9d82008-02-04 23:49:54 -05003795 return false;
3796 }
3797 skb->ip_summed = CHECKSUM_PARTIAL;
3798 skb->csum_start = skb_headroom(skb) + start;
3799 skb->csum_offset = off;
Jason Wange5d5dec2013-03-26 23:11:20 +00003800 skb_set_transport_header(skb, start);
Rusty Russellf35d9d82008-02-04 23:49:54 -05003801 return true;
3802}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003803EXPORT_SYMBOL_GPL(skb_partial_csum_set);
Rusty Russellf35d9d82008-02-04 23:49:54 -05003804
Paul Durranted1f50c2014-01-09 10:02:46 +00003805static int skb_maybe_pull_tail(struct sk_buff *skb, unsigned int len,
3806 unsigned int max)
3807{
3808 if (skb_headlen(skb) >= len)
3809 return 0;
3810
3811 /* If we need to pullup then pullup to the max, so we
3812 * won't need to do it again.
3813 */
3814 if (max > skb->len)
3815 max = skb->len;
3816
3817 if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL)
3818 return -ENOMEM;
3819
3820 if (skb_headlen(skb) < len)
3821 return -EPROTO;
3822
3823 return 0;
3824}
3825
Jan Beulichf9708b42014-03-11 13:56:05 +00003826#define MAX_TCP_HDR_LEN (15 * 4)
3827
3828static __sum16 *skb_checksum_setup_ip(struct sk_buff *skb,
3829 typeof(IPPROTO_IP) proto,
3830 unsigned int off)
3831{
3832 switch (proto) {
3833 int err;
3834
3835 case IPPROTO_TCP:
3836 err = skb_maybe_pull_tail(skb, off + sizeof(struct tcphdr),
3837 off + MAX_TCP_HDR_LEN);
3838 if (!err && !skb_partial_csum_set(skb, off,
3839 offsetof(struct tcphdr,
3840 check)))
3841 err = -EPROTO;
3842 return err ? ERR_PTR(err) : &tcp_hdr(skb)->check;
3843
3844 case IPPROTO_UDP:
3845 err = skb_maybe_pull_tail(skb, off + sizeof(struct udphdr),
3846 off + sizeof(struct udphdr));
3847 if (!err && !skb_partial_csum_set(skb, off,
3848 offsetof(struct udphdr,
3849 check)))
3850 err = -EPROTO;
3851 return err ? ERR_PTR(err) : &udp_hdr(skb)->check;
3852 }
3853
3854 return ERR_PTR(-EPROTO);
3855}
3856
Paul Durranted1f50c2014-01-09 10:02:46 +00003857/* This value should be large enough to cover a tagged ethernet header plus
3858 * maximally sized IP and TCP or UDP headers.
3859 */
3860#define MAX_IP_HDR_LEN 128
3861
Jan Beulichf9708b42014-03-11 13:56:05 +00003862static int skb_checksum_setup_ipv4(struct sk_buff *skb, bool recalculate)
Paul Durranted1f50c2014-01-09 10:02:46 +00003863{
3864 unsigned int off;
3865 bool fragment;
Jan Beulichf9708b42014-03-11 13:56:05 +00003866 __sum16 *csum;
Paul Durranted1f50c2014-01-09 10:02:46 +00003867 int err;
3868
3869 fragment = false;
3870
3871 err = skb_maybe_pull_tail(skb,
3872 sizeof(struct iphdr),
3873 MAX_IP_HDR_LEN);
3874 if (err < 0)
3875 goto out;
3876
3877 if (ip_hdr(skb)->frag_off & htons(IP_OFFSET | IP_MF))
3878 fragment = true;
3879
3880 off = ip_hdrlen(skb);
3881
3882 err = -EPROTO;
3883
3884 if (fragment)
3885 goto out;
3886
Jan Beulichf9708b42014-03-11 13:56:05 +00003887 csum = skb_checksum_setup_ip(skb, ip_hdr(skb)->protocol, off);
3888 if (IS_ERR(csum))
3889 return PTR_ERR(csum);
Paul Durranted1f50c2014-01-09 10:02:46 +00003890
Jan Beulichf9708b42014-03-11 13:56:05 +00003891 if (recalculate)
3892 *csum = ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
3893 ip_hdr(skb)->daddr,
3894 skb->len - off,
3895 ip_hdr(skb)->protocol, 0);
Paul Durranted1f50c2014-01-09 10:02:46 +00003896 err = 0;
3897
3898out:
3899 return err;
3900}
3901
3902/* This value should be large enough to cover a tagged ethernet header plus
3903 * an IPv6 header, all options, and a maximal TCP or UDP header.
3904 */
3905#define MAX_IPV6_HDR_LEN 256
3906
3907#define OPT_HDR(type, skb, off) \
3908 (type *)(skb_network_header(skb) + (off))
3909
3910static int skb_checksum_setup_ipv6(struct sk_buff *skb, bool recalculate)
3911{
3912 int err;
3913 u8 nexthdr;
3914 unsigned int off;
3915 unsigned int len;
3916 bool fragment;
3917 bool done;
Jan Beulichf9708b42014-03-11 13:56:05 +00003918 __sum16 *csum;
Paul Durranted1f50c2014-01-09 10:02:46 +00003919
3920 fragment = false;
3921 done = false;
3922
3923 off = sizeof(struct ipv6hdr);
3924
3925 err = skb_maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN);
3926 if (err < 0)
3927 goto out;
3928
3929 nexthdr = ipv6_hdr(skb)->nexthdr;
3930
3931 len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len);
3932 while (off <= len && !done) {
3933 switch (nexthdr) {
3934 case IPPROTO_DSTOPTS:
3935 case IPPROTO_HOPOPTS:
3936 case IPPROTO_ROUTING: {
3937 struct ipv6_opt_hdr *hp;
3938
3939 err = skb_maybe_pull_tail(skb,
3940 off +
3941 sizeof(struct ipv6_opt_hdr),
3942 MAX_IPV6_HDR_LEN);
3943 if (err < 0)
3944 goto out;
3945
3946 hp = OPT_HDR(struct ipv6_opt_hdr, skb, off);
3947 nexthdr = hp->nexthdr;
3948 off += ipv6_optlen(hp);
3949 break;
3950 }
3951 case IPPROTO_AH: {
3952 struct ip_auth_hdr *hp;
3953
3954 err = skb_maybe_pull_tail(skb,
3955 off +
3956 sizeof(struct ip_auth_hdr),
3957 MAX_IPV6_HDR_LEN);
3958 if (err < 0)
3959 goto out;
3960
3961 hp = OPT_HDR(struct ip_auth_hdr, skb, off);
3962 nexthdr = hp->nexthdr;
3963 off += ipv6_authlen(hp);
3964 break;
3965 }
3966 case IPPROTO_FRAGMENT: {
3967 struct frag_hdr *hp;
3968
3969 err = skb_maybe_pull_tail(skb,
3970 off +
3971 sizeof(struct frag_hdr),
3972 MAX_IPV6_HDR_LEN);
3973 if (err < 0)
3974 goto out;
3975
3976 hp = OPT_HDR(struct frag_hdr, skb, off);
3977
3978 if (hp->frag_off & htons(IP6_OFFSET | IP6_MF))
3979 fragment = true;
3980
3981 nexthdr = hp->nexthdr;
3982 off += sizeof(struct frag_hdr);
3983 break;
3984 }
3985 default:
3986 done = true;
3987 break;
3988 }
3989 }
3990
3991 err = -EPROTO;
3992
3993 if (!done || fragment)
3994 goto out;
3995
Jan Beulichf9708b42014-03-11 13:56:05 +00003996 csum = skb_checksum_setup_ip(skb, nexthdr, off);
3997 if (IS_ERR(csum))
3998 return PTR_ERR(csum);
Paul Durranted1f50c2014-01-09 10:02:46 +00003999
Jan Beulichf9708b42014-03-11 13:56:05 +00004000 if (recalculate)
4001 *csum = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
4002 &ipv6_hdr(skb)->daddr,
4003 skb->len - off, nexthdr, 0);
Paul Durranted1f50c2014-01-09 10:02:46 +00004004 err = 0;
4005
4006out:
4007 return err;
4008}
4009
4010/**
4011 * skb_checksum_setup - set up partial checksum offset
4012 * @skb: the skb to set up
4013 * @recalculate: if true the pseudo-header checksum will be recalculated
4014 */
4015int skb_checksum_setup(struct sk_buff *skb, bool recalculate)
4016{
4017 int err;
4018
4019 switch (skb->protocol) {
4020 case htons(ETH_P_IP):
Jan Beulichf9708b42014-03-11 13:56:05 +00004021 err = skb_checksum_setup_ipv4(skb, recalculate);
Paul Durranted1f50c2014-01-09 10:02:46 +00004022 break;
4023
4024 case htons(ETH_P_IPV6):
4025 err = skb_checksum_setup_ipv6(skb, recalculate);
4026 break;
4027
4028 default:
4029 err = -EPROTO;
4030 break;
4031 }
4032
4033 return err;
4034}
4035EXPORT_SYMBOL(skb_checksum_setup);
4036
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004037/**
4038 * skb_checksum_maybe_trim - maybe trims the given skb
4039 * @skb: the skb to check
4040 * @transport_len: the data length beyond the network header
4041 *
4042 * Checks whether the given skb has data beyond the given transport length.
4043 * If so, returns a cloned skb trimmed to this transport length.
4044 * Otherwise returns the provided skb. Returns NULL in error cases
4045 * (e.g. transport_len exceeds skb length or out-of-memory).
4046 *
Linus Lüssinga5169932015-08-13 05:54:07 +02004047 * Caller needs to set the skb transport header and free any returned skb if it
4048 * differs from the provided skb.
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004049 */
4050static struct sk_buff *skb_checksum_maybe_trim(struct sk_buff *skb,
4051 unsigned int transport_len)
4052{
4053 struct sk_buff *skb_chk;
4054 unsigned int len = skb_transport_offset(skb) + transport_len;
4055 int ret;
4056
Linus Lüssinga5169932015-08-13 05:54:07 +02004057 if (skb->len < len)
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004058 return NULL;
Linus Lüssinga5169932015-08-13 05:54:07 +02004059 else if (skb->len == len)
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004060 return skb;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004061
4062 skb_chk = skb_clone(skb, GFP_ATOMIC);
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004063 if (!skb_chk)
4064 return NULL;
4065
4066 ret = pskb_trim_rcsum(skb_chk, len);
4067 if (ret) {
4068 kfree_skb(skb_chk);
4069 return NULL;
4070 }
4071
4072 return skb_chk;
4073}
4074
4075/**
4076 * skb_checksum_trimmed - validate checksum of an skb
4077 * @skb: the skb to check
4078 * @transport_len: the data length beyond the network header
4079 * @skb_chkf: checksum function to use
4080 *
4081 * Applies the given checksum function skb_chkf to the provided skb.
4082 * Returns a checked and maybe trimmed skb. Returns NULL on error.
4083 *
4084 * If the skb has data beyond the given transport length, then a
4085 * trimmed & cloned skb is checked and returned.
4086 *
Linus Lüssinga5169932015-08-13 05:54:07 +02004087 * Caller needs to set the skb transport header and free any returned skb if it
4088 * differs from the provided skb.
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004089 */
4090struct sk_buff *skb_checksum_trimmed(struct sk_buff *skb,
4091 unsigned int transport_len,
4092 __sum16(*skb_chkf)(struct sk_buff *skb))
4093{
4094 struct sk_buff *skb_chk;
4095 unsigned int offset = skb_transport_offset(skb);
Linus Lüssingfcba67c2015-05-05 00:19:35 +02004096 __sum16 ret;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004097
4098 skb_chk = skb_checksum_maybe_trim(skb, transport_len);
4099 if (!skb_chk)
Linus Lüssinga5169932015-08-13 05:54:07 +02004100 goto err;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004101
Linus Lüssinga5169932015-08-13 05:54:07 +02004102 if (!pskb_may_pull(skb_chk, offset))
4103 goto err;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004104
Linus Lüssing9b368812016-02-24 04:21:42 +01004105 skb_pull_rcsum(skb_chk, offset);
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004106 ret = skb_chkf(skb_chk);
Linus Lüssing9b368812016-02-24 04:21:42 +01004107 skb_push_rcsum(skb_chk, offset);
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004108
Linus Lüssinga5169932015-08-13 05:54:07 +02004109 if (ret)
4110 goto err;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004111
4112 return skb_chk;
Linus Lüssinga5169932015-08-13 05:54:07 +02004113
4114err:
4115 if (skb_chk && skb_chk != skb)
4116 kfree_skb(skb_chk);
4117
4118 return NULL;
4119
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004120}
4121EXPORT_SYMBOL(skb_checksum_trimmed);
4122
Ben Hutchings4497b072008-06-19 16:22:28 -07004123void __skb_warn_lro_forwarding(const struct sk_buff *skb)
4124{
Joe Perchese87cc472012-05-13 21:56:26 +00004125 net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n",
4126 skb->dev->name);
Ben Hutchings4497b072008-06-19 16:22:28 -07004127}
Ben Hutchings4497b072008-06-19 16:22:28 -07004128EXPORT_SYMBOL(__skb_warn_lro_forwarding);
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004129
4130void kfree_skb_partial(struct sk_buff *skb, bool head_stolen)
4131{
Eric Dumazet3d861f62012-10-22 09:03:40 +00004132 if (head_stolen) {
4133 skb_release_head_state(skb);
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004134 kmem_cache_free(skbuff_head_cache, skb);
Eric Dumazet3d861f62012-10-22 09:03:40 +00004135 } else {
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004136 __kfree_skb(skb);
Eric Dumazet3d861f62012-10-22 09:03:40 +00004137 }
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004138}
4139EXPORT_SYMBOL(kfree_skb_partial);
4140
4141/**
4142 * skb_try_coalesce - try to merge skb to prior one
4143 * @to: prior buffer
4144 * @from: buffer to add
4145 * @fragstolen: pointer to boolean
Randy Dunlapc6c4b972012-06-08 14:01:44 +00004146 * @delta_truesize: how much more was allocated than was requested
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004147 */
4148bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
4149 bool *fragstolen, int *delta_truesize)
4150{
4151 int i, delta, len = from->len;
4152
4153 *fragstolen = false;
4154
4155 if (skb_cloned(to))
4156 return false;
4157
4158 if (len <= skb_tailroom(to)) {
Eric Dumazete93a0432014-09-15 04:19:52 -07004159 if (len)
4160 BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len));
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004161 *delta_truesize = 0;
4162 return true;
4163 }
4164
4165 if (skb_has_frag_list(to) || skb_has_frag_list(from))
4166 return false;
4167
4168 if (skb_headlen(from) != 0) {
4169 struct page *page;
4170 unsigned int offset;
4171
4172 if (skb_shinfo(to)->nr_frags +
4173 skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
4174 return false;
4175
4176 if (skb_head_is_locked(from))
4177 return false;
4178
4179 delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
4180
4181 page = virt_to_head_page(from->head);
4182 offset = from->data - (unsigned char *)page_address(page);
4183
4184 skb_fill_page_desc(to, skb_shinfo(to)->nr_frags,
4185 page, offset, skb_headlen(from));
4186 *fragstolen = true;
4187 } else {
4188 if (skb_shinfo(to)->nr_frags +
4189 skb_shinfo(from)->nr_frags > MAX_SKB_FRAGS)
4190 return false;
4191
Weiping Panf4b549a2012-09-28 20:15:30 +00004192 delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from));
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004193 }
4194
4195 WARN_ON_ONCE(delta < len);
4196
4197 memcpy(skb_shinfo(to)->frags + skb_shinfo(to)->nr_frags,
4198 skb_shinfo(from)->frags,
4199 skb_shinfo(from)->nr_frags * sizeof(skb_frag_t));
4200 skb_shinfo(to)->nr_frags += skb_shinfo(from)->nr_frags;
4201
4202 if (!skb_cloned(from))
4203 skb_shinfo(from)->nr_frags = 0;
4204
Li RongQing8ea853f2012-09-18 16:53:21 +00004205 /* if the skb is not cloned this does nothing
4206 * since we set nr_frags to 0.
4207 */
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004208 for (i = 0; i < skb_shinfo(from)->nr_frags; i++)
4209 skb_frag_ref(from, i);
4210
4211 to->truesize += delta;
4212 to->len += len;
4213 to->data_len += len;
4214
4215 *delta_truesize = delta;
4216 return true;
4217}
4218EXPORT_SYMBOL(skb_try_coalesce);
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004219
4220/**
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004221 * skb_scrub_packet - scrub an skb
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004222 *
4223 * @skb: buffer to clean
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004224 * @xnet: packet is crossing netns
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004225 *
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004226 * skb_scrub_packet can be used after encapsulating or decapsulting a packet
4227 * into/from a tunnel. Some information have to be cleared during these
4228 * operations.
4229 * skb_scrub_packet can also be used to clean a skb before injecting it in
4230 * another namespace (@xnet == true). We have to clear all information in the
4231 * skb that could impact namespace isolation.
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004232 */
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004233void skb_scrub_packet(struct sk_buff *skb, bool xnet)
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004234{
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004235 skb->tstamp.tv64 = 0;
4236 skb->pkt_type = PACKET_HOST;
4237 skb->skb_iif = 0;
WANG Cong60ff7462014-05-04 16:39:18 -07004238 skb->ignore_df = 0;
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004239 skb_dst_drop(skb);
Eric Dumazetc29390c2015-03-11 18:42:02 -07004240 skb_sender_cpu_clear(skb);
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004241 secpath_reset(skb);
4242 nf_reset(skb);
4243 nf_reset_trace(skb);
Herbert Xu213dd742015-04-16 09:03:27 +08004244
4245 if (!xnet)
4246 return;
4247
4248 skb_orphan(skb);
4249 skb->mark = 0;
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004250}
4251EXPORT_SYMBOL_GPL(skb_scrub_packet);
Florian Westphalde960aa2014-01-26 10:58:16 +01004252
4253/**
4254 * skb_gso_transport_seglen - Return length of individual segments of a gso packet
4255 *
4256 * @skb: GSO skb
4257 *
4258 * skb_gso_transport_seglen is used to determine the real size of the
4259 * individual segments, including Layer4 headers (TCP/UDP).
4260 *
4261 * The MAC/L2 or network (IP, IPv6) headers are not accounted for.
4262 */
4263unsigned int skb_gso_transport_seglen(const struct sk_buff *skb)
4264{
4265 const struct skb_shared_info *shinfo = skb_shinfo(skb);
Florian Westphalf993bc22014-10-20 13:49:18 +02004266 unsigned int thlen = 0;
Florian Westphalde960aa2014-01-26 10:58:16 +01004267
Florian Westphalf993bc22014-10-20 13:49:18 +02004268 if (skb->encapsulation) {
4269 thlen = skb_inner_transport_header(skb) -
4270 skb_transport_header(skb);
Florian Westphal6d39d582014-04-09 10:28:50 +02004271
Florian Westphalf993bc22014-10-20 13:49:18 +02004272 if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)))
4273 thlen += inner_tcp_hdrlen(skb);
4274 } else if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) {
4275 thlen = tcp_hdrlen(skb);
4276 }
Florian Westphal6d39d582014-04-09 10:28:50 +02004277 /* UFO sets gso_size to the size of the fragmentation
4278 * payload, i.e. the size of the L4 (UDP) header is already
4279 * accounted for.
4280 */
Florian Westphalf993bc22014-10-20 13:49:18 +02004281 return thlen + shinfo->gso_size;
Florian Westphalde960aa2014-01-26 10:58:16 +01004282}
4283EXPORT_SYMBOL_GPL(skb_gso_transport_seglen);
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04004284
4285static struct sk_buff *skb_reorder_vlan_header(struct sk_buff *skb)
4286{
4287 if (skb_cow(skb, skb_headroom(skb)) < 0) {
4288 kfree_skb(skb);
4289 return NULL;
4290 }
4291
Vlad Yasevichf6548612015-12-14 17:44:10 -05004292 memmove(skb->data - ETH_HLEN, skb->data - skb->mac_len - VLAN_HLEN,
Vlad Yasevicha6e18ff2015-11-16 15:43:44 -05004293 2 * ETH_ALEN);
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04004294 skb->mac_header += VLAN_HLEN;
4295 return skb;
4296}
4297
4298struct sk_buff *skb_vlan_untag(struct sk_buff *skb)
4299{
4300 struct vlan_hdr *vhdr;
4301 u16 vlan_tci;
4302
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01004303 if (unlikely(skb_vlan_tag_present(skb))) {
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04004304 /* vlan_tci is already set-up so leave this for another time */
4305 return skb;
4306 }
4307
4308 skb = skb_share_check(skb, GFP_ATOMIC);
4309 if (unlikely(!skb))
4310 goto err_free;
4311
4312 if (unlikely(!pskb_may_pull(skb, VLAN_HLEN)))
4313 goto err_free;
4314
4315 vhdr = (struct vlan_hdr *)skb->data;
4316 vlan_tci = ntohs(vhdr->h_vlan_TCI);
4317 __vlan_hwaccel_put_tag(skb, skb->protocol, vlan_tci);
4318
4319 skb_pull_rcsum(skb, VLAN_HLEN);
4320 vlan_set_encap_proto(skb, vhdr);
4321
4322 skb = skb_reorder_vlan_header(skb);
4323 if (unlikely(!skb))
4324 goto err_free;
4325
4326 skb_reset_network_header(skb);
4327 skb_reset_transport_header(skb);
4328 skb_reset_mac_len(skb);
4329
4330 return skb;
4331
4332err_free:
4333 kfree_skb(skb);
4334 return NULL;
4335}
4336EXPORT_SYMBOL(skb_vlan_untag);
Eric Dumazet2e4e4412014-09-17 04:49:49 -07004337
Jiri Pirkoe2195122014-11-19 14:05:01 +01004338int skb_ensure_writable(struct sk_buff *skb, int write_len)
4339{
4340 if (!pskb_may_pull(skb, write_len))
4341 return -ENOMEM;
4342
4343 if (!skb_cloned(skb) || skb_clone_writable(skb, write_len))
4344 return 0;
4345
4346 return pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
4347}
4348EXPORT_SYMBOL(skb_ensure_writable);
4349
Jiri Pirko93515d52014-11-19 14:05:02 +01004350/* remove VLAN header from packet and update csum accordingly. */
4351static int __skb_vlan_pop(struct sk_buff *skb, u16 *vlan_tci)
4352{
4353 struct vlan_hdr *vhdr;
4354 unsigned int offset = skb->data - skb_mac_header(skb);
4355 int err;
4356
4357 __skb_push(skb, offset);
4358 err = skb_ensure_writable(skb, VLAN_ETH_HLEN);
4359 if (unlikely(err))
4360 goto pull;
4361
4362 skb_postpull_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
4363
4364 vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN);
4365 *vlan_tci = ntohs(vhdr->h_vlan_TCI);
4366
4367 memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN);
4368 __skb_pull(skb, VLAN_HLEN);
4369
4370 vlan_set_encap_proto(skb, vhdr);
4371 skb->mac_header += VLAN_HLEN;
4372
4373 if (skb_network_offset(skb) < ETH_HLEN)
4374 skb_set_network_header(skb, ETH_HLEN);
4375
4376 skb_reset_mac_len(skb);
4377pull:
4378 __skb_pull(skb, offset);
4379
4380 return err;
4381}
4382
4383int skb_vlan_pop(struct sk_buff *skb)
4384{
4385 u16 vlan_tci;
4386 __be16 vlan_proto;
4387 int err;
4388
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01004389 if (likely(skb_vlan_tag_present(skb))) {
Jiri Pirko93515d52014-11-19 14:05:02 +01004390 skb->vlan_tci = 0;
4391 } else {
4392 if (unlikely((skb->protocol != htons(ETH_P_8021Q) &&
4393 skb->protocol != htons(ETH_P_8021AD)) ||
4394 skb->len < VLAN_ETH_HLEN))
4395 return 0;
4396
4397 err = __skb_vlan_pop(skb, &vlan_tci);
4398 if (err)
4399 return err;
4400 }
4401 /* move next vlan tag to hw accel tag */
4402 if (likely((skb->protocol != htons(ETH_P_8021Q) &&
4403 skb->protocol != htons(ETH_P_8021AD)) ||
4404 skb->len < VLAN_ETH_HLEN))
4405 return 0;
4406
4407 vlan_proto = skb->protocol;
4408 err = __skb_vlan_pop(skb, &vlan_tci);
4409 if (unlikely(err))
4410 return err;
4411
4412 __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
4413 return 0;
4414}
4415EXPORT_SYMBOL(skb_vlan_pop);
4416
4417int skb_vlan_push(struct sk_buff *skb, __be16 vlan_proto, u16 vlan_tci)
4418{
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01004419 if (skb_vlan_tag_present(skb)) {
Jiri Pirko93515d52014-11-19 14:05:02 +01004420 unsigned int offset = skb->data - skb_mac_header(skb);
4421 int err;
4422
4423 /* __vlan_insert_tag expect skb->data pointing to mac header.
4424 * So change skb->data before calling it and change back to
4425 * original position later
4426 */
4427 __skb_push(skb, offset);
4428 err = __vlan_insert_tag(skb, skb->vlan_proto,
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01004429 skb_vlan_tag_get(skb));
Jiri Pirko93515d52014-11-19 14:05:02 +01004430 if (err)
4431 return err;
4432 skb->protocol = skb->vlan_proto;
4433 skb->mac_len += VLAN_HLEN;
4434 __skb_pull(skb, offset);
4435
4436 if (skb->ip_summed == CHECKSUM_COMPLETE)
4437 skb->csum = csum_add(skb->csum, csum_partial(skb->data
4438 + (2 * ETH_ALEN), VLAN_HLEN, 0));
4439 }
4440 __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
4441 return 0;
4442}
4443EXPORT_SYMBOL(skb_vlan_push);
4444
Eric Dumazet2e4e4412014-09-17 04:49:49 -07004445/**
4446 * alloc_skb_with_frags - allocate skb with page frags
4447 *
Masanari Iidade3f0d02014-10-09 12:58:08 +09004448 * @header_len: size of linear part
4449 * @data_len: needed length in frags
4450 * @max_page_order: max page order desired.
4451 * @errcode: pointer to error code if any
4452 * @gfp_mask: allocation mask
Eric Dumazet2e4e4412014-09-17 04:49:49 -07004453 *
4454 * This can be used to allocate a paged skb, given a maximal order for frags.
4455 */
4456struct sk_buff *alloc_skb_with_frags(unsigned long header_len,
4457 unsigned long data_len,
4458 int max_page_order,
4459 int *errcode,
4460 gfp_t gfp_mask)
4461{
4462 int npages = (data_len + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
4463 unsigned long chunk;
4464 struct sk_buff *skb;
4465 struct page *page;
4466 gfp_t gfp_head;
4467 int i;
4468
4469 *errcode = -EMSGSIZE;
4470 /* Note this test could be relaxed, if we succeed to allocate
4471 * high order pages...
4472 */
4473 if (npages > MAX_SKB_FRAGS)
4474 return NULL;
4475
4476 gfp_head = gfp_mask;
Mel Gormand0164ad2015-11-06 16:28:21 -08004477 if (gfp_head & __GFP_DIRECT_RECLAIM)
Eric Dumazet2e4e4412014-09-17 04:49:49 -07004478 gfp_head |= __GFP_REPEAT;
4479
4480 *errcode = -ENOBUFS;
4481 skb = alloc_skb(header_len, gfp_head);
4482 if (!skb)
4483 return NULL;
4484
4485 skb->truesize += npages << PAGE_SHIFT;
4486
4487 for (i = 0; npages > 0; i++) {
4488 int order = max_page_order;
4489
4490 while (order) {
4491 if (npages >= 1 << order) {
Mel Gormand0164ad2015-11-06 16:28:21 -08004492 page = alloc_pages((gfp_mask & ~__GFP_DIRECT_RECLAIM) |
Eric Dumazet2e4e4412014-09-17 04:49:49 -07004493 __GFP_COMP |
4494 __GFP_NOWARN |
4495 __GFP_NORETRY,
4496 order);
4497 if (page)
4498 goto fill_page;
4499 /* Do not retry other high order allocations */
4500 order = 1;
4501 max_page_order = 0;
4502 }
4503 order--;
4504 }
4505 page = alloc_page(gfp_mask);
4506 if (!page)
4507 goto failure;
4508fill_page:
4509 chunk = min_t(unsigned long, data_len,
4510 PAGE_SIZE << order);
4511 skb_fill_page_desc(skb, i, page, 0, chunk);
4512 data_len -= chunk;
4513 npages -= 1 << order;
4514 }
4515 return skb;
4516
4517failure:
4518 kfree_skb(skb);
4519 return NULL;
4520}
4521EXPORT_SYMBOL(alloc_skb_with_frags);