blob: 7a338fb55cc4360461032c5d9f639ef39678f6ec [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>
Al Viroa1f8e7f72006-10-19 16:08:53 -040077
Eric Dumazetd7e88832012-04-30 08:10:34 +000078struct kmem_cache *skbuff_head_cache __read_mostly;
Christoph Lametere18b8902006-12-06 20:33:20 -080079static struct kmem_cache *skbuff_fclone_cache __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Linus Torvalds1da177e2005-04-16 15:20:36 -070081/**
Jean Sacrenf05de732013-02-11 13:30:38 +000082 * skb_panic - private function for out-of-line support
83 * @skb: buffer
84 * @sz: size
85 * @addr: address
James Hogan99d58512013-02-13 11:20:27 +000086 * @msg: skb_over_panic or skb_under_panic
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 *
Jean Sacrenf05de732013-02-11 13:30:38 +000088 * Out-of-line support for skb_put() and skb_push().
89 * Called via the wrapper skb_over_panic() or skb_under_panic().
90 * Keep out of line to prevent kernel bloat.
91 * __builtin_return_address is not used because it is not always reliable.
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 */
Jean Sacrenf05de732013-02-11 13:30:38 +000093static void skb_panic(struct sk_buff *skb, unsigned int sz, void *addr,
James Hogan99d58512013-02-13 11:20:27 +000094 const char msg[])
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
Joe Perchese005d192012-05-16 19:58:40 +000096 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 +000097 msg, addr, skb->len, sz, skb->head, skb->data,
Joe Perchese005d192012-05-16 19:58:40 +000098 (unsigned long)skb->tail, (unsigned long)skb->end,
99 skb->dev ? skb->dev->name : "<NULL>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 BUG();
101}
102
Jean Sacrenf05de732013-02-11 13:30:38 +0000103static void skb_over_panic(struct sk_buff *skb, unsigned int sz, void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104{
Jean Sacrenf05de732013-02-11 13:30:38 +0000105 skb_panic(skb, sz, addr, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106}
107
Jean Sacrenf05de732013-02-11 13:30:38 +0000108static void skb_under_panic(struct sk_buff *skb, unsigned int sz, void *addr)
109{
110 skb_panic(skb, sz, addr, __func__);
111}
Mel Gormanc93bdd02012-07-31 16:44:19 -0700112
113/*
114 * kmalloc_reserve is a wrapper around kmalloc_node_track_caller that tells
115 * the caller if emergency pfmemalloc reserves are being used. If it is and
116 * the socket is later found to be SOCK_MEMALLOC then PFMEMALLOC reserves
117 * may be used. Otherwise, the packet data may be discarded until enough
118 * memory is free
119 */
120#define kmalloc_reserve(size, gfp, node, pfmemalloc) \
121 __kmalloc_reserve(size, gfp, node, _RET_IP_, pfmemalloc)
stephen hemminger61c5e882012-12-28 18:24:28 +0000122
123static void *__kmalloc_reserve(size_t size, gfp_t flags, int node,
124 unsigned long ip, bool *pfmemalloc)
Mel Gormanc93bdd02012-07-31 16:44:19 -0700125{
126 void *obj;
127 bool ret_pfmemalloc = false;
128
129 /*
130 * Try a regular allocation, when that fails and we're not entitled
131 * to the reserves, fail.
132 */
133 obj = kmalloc_node_track_caller(size,
134 flags | __GFP_NOMEMALLOC | __GFP_NOWARN,
135 node);
136 if (obj || !(gfp_pfmemalloc_allowed(flags)))
137 goto out;
138
139 /* Try again but now we are using pfmemalloc reserves */
140 ret_pfmemalloc = true;
141 obj = kmalloc_node_track_caller(size, flags, node);
142
143out:
144 if (pfmemalloc)
145 *pfmemalloc = ret_pfmemalloc;
146
147 return obj;
148}
149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150/* Allocate a new skbuff. We do this ourselves so we can fill in a few
151 * 'private' fields and also do memory statistics to find all the
152 * [BEEP] leaks.
153 *
154 */
155
Patrick McHardy0ebd0ac2013-04-17 06:46:58 +0000156struct sk_buff *__alloc_skb_head(gfp_t gfp_mask, int node)
157{
158 struct sk_buff *skb;
159
160 /* Get the HEAD */
161 skb = kmem_cache_alloc_node(skbuff_head_cache,
162 gfp_mask & ~__GFP_DMA, node);
163 if (!skb)
164 goto out;
165
166 /*
167 * Only clear those fields we need to clear, not those that we will
168 * actually initialise below. Hence, don't put any more fields after
169 * the tail pointer in struct sk_buff!
170 */
171 memset(skb, 0, offsetof(struct sk_buff, tail));
Pablo Neira5e71d9d2013-06-03 09:28:43 +0000172 skb->head = NULL;
Patrick McHardy0ebd0ac2013-04-17 06:46:58 +0000173 skb->truesize = sizeof(struct sk_buff);
174 atomic_set(&skb->users, 1);
175
Cong Wang35d04612013-05-29 15:16:05 +0800176 skb->mac_header = (typeof(skb->mac_header))~0U;
Patrick McHardy0ebd0ac2013-04-17 06:46:58 +0000177out:
178 return skb;
179}
180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181/**
David S. Millerd179cd12005-08-17 14:57:30 -0700182 * __alloc_skb - allocate a network buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 * @size: size to allocate
184 * @gfp_mask: allocation mask
Mel Gormanc93bdd02012-07-31 16:44:19 -0700185 * @flags: If SKB_ALLOC_FCLONE is set, allocate from fclone cache
186 * instead of head cache and allocate a cloned (child) skb.
187 * If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for
188 * allocations in case the data is required for writeback
Christoph Hellwigb30973f2006-12-06 20:32:36 -0800189 * @node: numa node to allocate memory on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 *
191 * Allocate a new &sk_buff. The returned buffer has no headroom and a
Ben Hutchings94b60422012-06-06 15:23:37 +0000192 * tail room of at least size bytes. The object has a reference count
193 * of one. The return is the buffer. On a failure the return is %NULL.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 *
195 * Buffers may only be allocated from interrupts using a @gfp_mask of
196 * %GFP_ATOMIC.
197 */
Al Virodd0fc662005-10-07 07:46:04 +0100198struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
Mel Gormanc93bdd02012-07-31 16:44:19 -0700199 int flags, int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
Christoph Lametere18b8902006-12-06 20:33:20 -0800201 struct kmem_cache *cache;
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800202 struct skb_shared_info *shinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 struct sk_buff *skb;
204 u8 *data;
Mel Gormanc93bdd02012-07-31 16:44:19 -0700205 bool pfmemalloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
Mel Gormanc93bdd02012-07-31 16:44:19 -0700207 cache = (flags & SKB_ALLOC_FCLONE)
208 ? skbuff_fclone_cache : skbuff_head_cache;
209
210 if (sk_memalloc_socks() && (flags & SKB_ALLOC_RX))
211 gfp_mask |= __GFP_MEMALLOC;
Herbert Xu8798b3f2006-01-23 16:32:45 -0800212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 /* Get the HEAD */
Christoph Hellwigb30973f2006-12-06 20:32:36 -0800214 skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 if (!skb)
216 goto out;
Eric Dumazetec7d2f22010-05-05 01:07:37 -0700217 prefetchw(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
Eric Dumazet87fb4b72011-10-13 07:28:54 +0000219 /* We do our best to align skb_shared_info on a separate cache
220 * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives
221 * aligned memory blocks, unless SLUB/SLAB debug is enabled.
222 * Both skb->head and skb_shared_info are cache line aligned.
223 */
Tony Lindgrenbc417e32011-11-02 13:40:28 +0000224 size = SKB_DATA_ALIGN(size);
Eric Dumazet87fb4b72011-10-13 07:28:54 +0000225 size += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Mel Gormanc93bdd02012-07-31 16:44:19 -0700226 data = kmalloc_reserve(size, gfp_mask, node, &pfmemalloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 if (!data)
228 goto nodata;
Eric Dumazet87fb4b72011-10-13 07:28:54 +0000229 /* kmalloc(size) might give us more room than requested.
230 * Put skb_shared_info exactly at the end of allocated zone,
231 * to allow max possible filling before reallocation.
232 */
233 size = SKB_WITH_OVERHEAD(ksize(data));
Eric Dumazetec7d2f22010-05-05 01:07:37 -0700234 prefetchw(data + size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Arnaldo Carvalho de Meloca0605a2007-03-19 10:48:59 -0300236 /*
Johannes Bergc8005782008-05-03 20:56:42 -0700237 * Only clear those fields we need to clear, not those that we will
238 * actually initialise below. Hence, don't put any more fields after
239 * the tail pointer in struct sk_buff!
Arnaldo Carvalho de Meloca0605a2007-03-19 10:48:59 -0300240 */
241 memset(skb, 0, offsetof(struct sk_buff, tail));
Eric Dumazet87fb4b72011-10-13 07:28:54 +0000242 /* Account for allocated memory : skb + skb->head */
243 skb->truesize = SKB_TRUESIZE(size);
Mel Gormanc93bdd02012-07-31 16:44:19 -0700244 skb->pfmemalloc = pfmemalloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 atomic_set(&skb->users, 1);
246 skb->head = data;
247 skb->data = data;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700248 skb_reset_tail_pointer(skb);
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700249 skb->end = skb->tail + size;
Cong Wang35d04612013-05-29 15:16:05 +0800250 skb->mac_header = (typeof(skb->mac_header))~0U;
251 skb->transport_header = (typeof(skb->transport_header))~0U;
Stephen Hemminger19633e12009-06-17 05:23:27 +0000252
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800253 /* make sure we initialize shinfo sequentially */
254 shinfo = skb_shinfo(skb);
Eric Dumazetec7d2f22010-05-05 01:07:37 -0700255 memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800256 atomic_set(&shinfo->dataref, 1);
Eric Dumazetc2aa3662011-01-25 23:18:38 +0000257 kmemcheck_annotate_variable(shinfo->destructor_arg);
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800258
Mel Gormanc93bdd02012-07-31 16:44:19 -0700259 if (flags & SKB_ALLOC_FCLONE) {
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700260 struct sk_buff_fclones *fclones;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700262 fclones = container_of(skb, struct sk_buff_fclones, skb1);
263
264 kmemcheck_annotate_bitfield(&fclones->skb2, flags1);
David S. Millerd179cd12005-08-17 14:57:30 -0700265 skb->fclone = SKB_FCLONE_ORIG;
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700266 atomic_set(&fclones->fclone_ref, 1);
David S. Millerd179cd12005-08-17 14:57:30 -0700267
Eric Dumazet6ffe75e2014-12-03 17:04:39 -0800268 fclones->skb2.fclone = SKB_FCLONE_CLONE;
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700269 fclones->skb2.pfmemalloc = pfmemalloc;
David S. Millerd179cd12005-08-17 14:57:30 -0700270 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271out:
272 return skb;
273nodata:
Herbert Xu8798b3f2006-01-23 16:32:45 -0800274 kmem_cache_free(cache, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 skb = NULL;
276 goto out;
277}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800278EXPORT_SYMBOL(__alloc_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
280/**
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000281 * build_skb - build a network buffer
282 * @data: data buffer provided by caller
Eric Dumazetd3836f22012-04-27 00:33:38 +0000283 * @frag_size: size of fragment, or 0 if head was kmalloced
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000284 *
285 * Allocate a new &sk_buff. Caller provides space holding head and
Florian Fainellideceb4c2013-07-23 20:22:39 +0100286 * skb_shared_info. @data must have been allocated by kmalloc() only if
287 * @frag_size is 0, otherwise data should come from the page allocator.
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000288 * The return is the new skb buffer.
289 * On a failure the return is %NULL, and @data is not freed.
290 * Notes :
291 * Before IO, driver allocates only data buffer where NIC put incoming frame
292 * Driver should add room at head (NET_SKB_PAD) and
293 * MUST add room at tail (SKB_DATA_ALIGN(skb_shared_info))
294 * After IO, driver calls build_skb(), to allocate sk_buff and populate it
295 * before giving packet to stack.
296 * RX rings only contains data buffers, not full skbs.
297 */
Eric Dumazetd3836f22012-04-27 00:33:38 +0000298struct sk_buff *build_skb(void *data, unsigned int frag_size)
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000299{
300 struct skb_shared_info *shinfo;
301 struct sk_buff *skb;
Eric Dumazetd3836f22012-04-27 00:33:38 +0000302 unsigned int size = frag_size ? : ksize(data);
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000303
304 skb = kmem_cache_alloc(skbuff_head_cache, GFP_ATOMIC);
305 if (!skb)
306 return NULL;
307
Eric Dumazetd3836f22012-04-27 00:33:38 +0000308 size -= SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000309
310 memset(skb, 0, offsetof(struct sk_buff, tail));
311 skb->truesize = SKB_TRUESIZE(size);
Eric Dumazetd3836f22012-04-27 00:33:38 +0000312 skb->head_frag = frag_size != 0;
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000313 atomic_set(&skb->users, 1);
314 skb->head = data;
315 skb->data = data;
316 skb_reset_tail_pointer(skb);
317 skb->end = skb->tail + size;
Cong Wang35d04612013-05-29 15:16:05 +0800318 skb->mac_header = (typeof(skb->mac_header))~0U;
319 skb->transport_header = (typeof(skb->transport_header))~0U;
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000320
321 /* make sure we initialize shinfo sequentially */
322 shinfo = skb_shinfo(skb);
323 memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
324 atomic_set(&shinfo->dataref, 1);
325 kmemcheck_annotate_variable(shinfo->destructor_arg);
326
327 return skb;
328}
329EXPORT_SYMBOL(build_skb);
330
Eric Dumazeta1c7fff2012-05-17 07:34:16 +0000331struct netdev_alloc_cache {
Eric Dumazet69b08f62012-09-26 06:46:57 +0000332 struct page_frag frag;
333 /* we maintain a pagecount bias, so that we dont dirty cache line
334 * containing page->_count every time we allocate a fragment.
335 */
336 unsigned int pagecnt_bias;
Eric Dumazeta1c7fff2012-05-17 07:34:16 +0000337};
338static DEFINE_PER_CPU(struct netdev_alloc_cache, netdev_alloc_cache);
339
Mel Gormanc93bdd02012-07-31 16:44:19 -0700340static void *__netdev_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
Eric Dumazet6f532612012-05-18 05:12:12 +0000341{
342 struct netdev_alloc_cache *nc;
343 void *data = NULL;
Eric Dumazet69b08f62012-09-26 06:46:57 +0000344 int order;
Eric Dumazet6f532612012-05-18 05:12:12 +0000345 unsigned long flags;
346
347 local_irq_save(flags);
Christoph Lameter903ceff2014-08-17 12:30:35 -0500348 nc = this_cpu_ptr(&netdev_alloc_cache);
Eric Dumazet69b08f62012-09-26 06:46:57 +0000349 if (unlikely(!nc->frag.page)) {
Eric Dumazet6f532612012-05-18 05:12:12 +0000350refill:
Eric Dumazet69b08f62012-09-26 06:46:57 +0000351 for (order = NETDEV_FRAG_PAGE_MAX_ORDER; ;) {
352 gfp_t gfp = gfp_mask;
353
354 if (order)
355 gfp |= __GFP_COMP | __GFP_NOWARN;
356 nc->frag.page = alloc_pages(gfp, order);
357 if (likely(nc->frag.page))
358 break;
359 if (--order < 0)
360 goto end;
361 }
362 nc->frag.size = PAGE_SIZE << order;
Eric Dumazet4c450582014-10-10 04:48:18 -0700363 /* Even if we own the page, we do not use atomic_set().
364 * This would break get_page_unless_zero() users.
365 */
366 atomic_add(NETDEV_PAGECNT_MAX_BIAS - 1,
367 &nc->frag.page->_count);
Eric Dumazet69b08f62012-09-26 06:46:57 +0000368 nc->pagecnt_bias = NETDEV_PAGECNT_MAX_BIAS;
369 nc->frag.offset = 0;
Eric Dumazet6f532612012-05-18 05:12:12 +0000370 }
Alexander Duyck540eb7b2012-07-12 14:23:50 +0000371
Eric Dumazet69b08f62012-09-26 06:46:57 +0000372 if (nc->frag.offset + fragsz > nc->frag.size) {
Eric Dumazet4c450582014-10-10 04:48:18 -0700373 if (atomic_read(&nc->frag.page->_count) != nc->pagecnt_bias) {
374 if (!atomic_sub_and_test(nc->pagecnt_bias,
375 &nc->frag.page->_count))
376 goto refill;
377 /* OK, page count is 0, we can safely set it */
378 atomic_set(&nc->frag.page->_count,
379 NETDEV_PAGECNT_MAX_BIAS);
380 } else {
381 atomic_add(NETDEV_PAGECNT_MAX_BIAS - nc->pagecnt_bias,
382 &nc->frag.page->_count);
383 }
384 nc->pagecnt_bias = NETDEV_PAGECNT_MAX_BIAS;
385 nc->frag.offset = 0;
Eric Dumazet6f532612012-05-18 05:12:12 +0000386 }
Alexander Duyck540eb7b2012-07-12 14:23:50 +0000387
Eric Dumazet69b08f62012-09-26 06:46:57 +0000388 data = page_address(nc->frag.page) + nc->frag.offset;
389 nc->frag.offset += fragsz;
Alexander Duyck540eb7b2012-07-12 14:23:50 +0000390 nc->pagecnt_bias--;
391end:
Eric Dumazet6f532612012-05-18 05:12:12 +0000392 local_irq_restore(flags);
393 return data;
394}
Mel Gormanc93bdd02012-07-31 16:44:19 -0700395
396/**
397 * netdev_alloc_frag - allocate a page fragment
398 * @fragsz: fragment size
399 *
400 * Allocates a frag from a page for receive buffer.
401 * Uses GFP_ATOMIC allocations.
402 */
403void *netdev_alloc_frag(unsigned int fragsz)
404{
405 return __netdev_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD);
406}
Eric Dumazet6f532612012-05-18 05:12:12 +0000407EXPORT_SYMBOL(netdev_alloc_frag);
408
409/**
Christoph Hellwig8af27452006-07-31 22:35:23 -0700410 * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
411 * @dev: network device to receive on
412 * @length: length to allocate
413 * @gfp_mask: get_free_pages mask, passed to alloc_skb
414 *
415 * Allocate a new &sk_buff and assign it a usage count of one. The
416 * buffer has unspecified headroom built in. Users should allocate
417 * the headroom they think they need without accounting for the
418 * built in space. The built in space is used for optimisations.
419 *
420 * %NULL is returned if there is no free memory.
421 */
422struct sk_buff *__netdev_alloc_skb(struct net_device *dev,
Eric Dumazet6f532612012-05-18 05:12:12 +0000423 unsigned int length, gfp_t gfp_mask)
Christoph Hellwig8af27452006-07-31 22:35:23 -0700424{
Eric Dumazet6f532612012-05-18 05:12:12 +0000425 struct sk_buff *skb = NULL;
Eric Dumazeta1c7fff2012-05-17 07:34:16 +0000426 unsigned int fragsz = SKB_DATA_ALIGN(length + NET_SKB_PAD) +
427 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Christoph Hellwig8af27452006-07-31 22:35:23 -0700428
Eric Dumazet310e1582012-07-16 13:15:52 +0200429 if (fragsz <= PAGE_SIZE && !(gfp_mask & (__GFP_WAIT | GFP_DMA))) {
Mel Gormanc93bdd02012-07-31 16:44:19 -0700430 void *data;
431
432 if (sk_memalloc_socks())
433 gfp_mask |= __GFP_MEMALLOC;
434
435 data = __netdev_alloc_frag(fragsz, gfp_mask);
Eric Dumazeta1c7fff2012-05-17 07:34:16 +0000436
Eric Dumazet6f532612012-05-18 05:12:12 +0000437 if (likely(data)) {
438 skb = build_skb(data, fragsz);
439 if (unlikely(!skb))
440 put_page(virt_to_head_page(data));
Eric Dumazeta1c7fff2012-05-17 07:34:16 +0000441 }
Eric Dumazeta1c7fff2012-05-17 07:34:16 +0000442 } else {
Mel Gormanc93bdd02012-07-31 16:44:19 -0700443 skb = __alloc_skb(length + NET_SKB_PAD, gfp_mask,
444 SKB_ALLOC_RX, NUMA_NO_NODE);
Eric Dumazeta1c7fff2012-05-17 07:34:16 +0000445 }
Christoph Hellwig7b2e4972006-08-07 16:09:04 -0700446 if (likely(skb)) {
Christoph Hellwig8af27452006-07-31 22:35:23 -0700447 skb_reserve(skb, NET_SKB_PAD);
Christoph Hellwig7b2e4972006-08-07 16:09:04 -0700448 skb->dev = dev;
449 }
Christoph Hellwig8af27452006-07-31 22:35:23 -0700450 return skb;
451}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800452EXPORT_SYMBOL(__netdev_alloc_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
Peter Zijlstra654bed12008-10-07 14:22:33 -0700454void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off,
Eric Dumazet50269e12012-03-23 23:59:33 +0000455 int size, unsigned int truesize)
Peter Zijlstra654bed12008-10-07 14:22:33 -0700456{
457 skb_fill_page_desc(skb, i, page, off, size);
458 skb->len += size;
459 skb->data_len += size;
Eric Dumazet50269e12012-03-23 23:59:33 +0000460 skb->truesize += truesize;
Peter Zijlstra654bed12008-10-07 14:22:33 -0700461}
462EXPORT_SYMBOL(skb_add_rx_frag);
463
Jason Wangf8e617e2013-11-01 14:07:47 +0800464void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size,
465 unsigned int truesize)
466{
467 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
468
469 skb_frag_size_add(frag, size);
470 skb->len += size;
471 skb->data_len += size;
472 skb->truesize += truesize;
473}
474EXPORT_SYMBOL(skb_coalesce_rx_frag);
475
Herbert Xu27b437c2006-07-13 19:26:39 -0700476static void skb_drop_list(struct sk_buff **listp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477{
Eric Dumazetbd8a7032013-06-24 06:26:00 -0700478 kfree_skb_list(*listp);
Herbert Xu27b437c2006-07-13 19:26:39 -0700479 *listp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480}
481
Herbert Xu27b437c2006-07-13 19:26:39 -0700482static inline void skb_drop_fraglist(struct sk_buff *skb)
483{
484 skb_drop_list(&skb_shinfo(skb)->frag_list);
485}
486
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487static void skb_clone_fraglist(struct sk_buff *skb)
488{
489 struct sk_buff *list;
490
David S. Millerfbb398a2009-06-09 00:18:59 -0700491 skb_walk_frags(skb, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 skb_get(list);
493}
494
Eric Dumazetd3836f22012-04-27 00:33:38 +0000495static void skb_free_head(struct sk_buff *skb)
496{
497 if (skb->head_frag)
498 put_page(virt_to_head_page(skb->head));
499 else
500 kfree(skb->head);
501}
502
Adrian Bunk5bba1712006-06-29 13:02:35 -0700503static void skb_release_data(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504{
Eric Dumazetff04a772014-09-23 18:39:30 -0700505 struct skb_shared_info *shinfo = skb_shinfo(skb);
506 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
Eric Dumazetff04a772014-09-23 18:39:30 -0700508 if (skb->cloned &&
509 atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
510 &shinfo->dataref))
511 return;
Shirley Maa6686f22011-07-06 12:22:12 +0000512
Eric Dumazetff04a772014-09-23 18:39:30 -0700513 for (i = 0; i < shinfo->nr_frags; i++)
514 __skb_frag_unref(&shinfo->frags[i]);
Shirley Maa6686f22011-07-06 12:22:12 +0000515
Eric Dumazetff04a772014-09-23 18:39:30 -0700516 /*
517 * If skb buf is from userspace, we need to notify the caller
518 * the lower device DMA has done;
519 */
520 if (shinfo->tx_flags & SKBTX_DEV_ZEROCOPY) {
521 struct ubuf_info *uarg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
Eric Dumazetff04a772014-09-23 18:39:30 -0700523 uarg = shinfo->destructor_arg;
524 if (uarg->callback)
525 uarg->callback(uarg, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 }
Eric Dumazetff04a772014-09-23 18:39:30 -0700527
528 if (shinfo->frag_list)
529 kfree_skb_list(shinfo->frag_list);
530
531 skb_free_head(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532}
533
534/*
535 * Free an skbuff by memory without cleaning the state.
536 */
Herbert Xu2d4baff2007-11-26 23:11:19 +0800537static void kfree_skbmem(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538{
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700539 struct sk_buff_fclones *fclones;
David S. Millerd179cd12005-08-17 14:57:30 -0700540
David S. Millerd179cd12005-08-17 14:57:30 -0700541 switch (skb->fclone) {
542 case SKB_FCLONE_UNAVAILABLE:
543 kmem_cache_free(skbuff_head_cache, skb);
Eric Dumazet6ffe75e2014-12-03 17:04:39 -0800544 return;
David S. Millerd179cd12005-08-17 14:57:30 -0700545
546 case SKB_FCLONE_ORIG:
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700547 fclones = container_of(skb, struct sk_buff_fclones, skb1);
Eric Dumazet6ffe75e2014-12-03 17:04:39 -0800548
549 /* We usually free the clone (TX completion) before original skb
550 * This test would have no chance to be true for the clone,
551 * while here, branch prediction will be good.
552 */
553 if (atomic_read(&fclones->fclone_ref) == 1)
554 goto fastpath;
David S. Millerd179cd12005-08-17 14:57:30 -0700555 break;
556
Eric Dumazet6ffe75e2014-12-03 17:04:39 -0800557 default: /* SKB_FCLONE_CLONE */
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700558 fclones = container_of(skb, struct sk_buff_fclones, skb2);
David S. Millerd179cd12005-08-17 14:57:30 -0700559 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700560 }
Eric Dumazet6ffe75e2014-12-03 17:04:39 -0800561 if (!atomic_dec_and_test(&fclones->fclone_ref))
562 return;
563fastpath:
564 kmem_cache_free(skbuff_fclone_cache, fclones);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565}
566
Lennert Buytenhek04a4bb52008-10-01 02:33:12 -0700567static void skb_release_head_state(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568{
Eric Dumazetadf30902009-06-02 05:19:30 +0000569 skb_dst_drop(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570#ifdef CONFIG_XFRM
571 secpath_put(skb->sp);
572#endif
Stephen Hemminger9c2b3322005-04-19 22:39:42 -0700573 if (skb->destructor) {
574 WARN_ON(in_irq());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 skb->destructor(skb);
576 }
Igor Maravića3bf7ae2011-12-12 02:58:22 +0000577#if IS_ENABLED(CONFIG_NF_CONNTRACK)
Yasuyuki Kozakai5f79e0f2007-03-23 11:17:07 -0700578 nf_conntrack_put(skb->nfct);
KOVACS Krisztian2fc72c72011-01-12 20:25:08 +0100579#endif
Pablo Neira Ayuso1109a902014-10-01 11:19:17 +0200580#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 nf_bridge_put(skb->nf_bridge);
582#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583/* XXX: IS this still necessary? - JHS */
584#ifdef CONFIG_NET_SCHED
585 skb->tc_index = 0;
586#ifdef CONFIG_NET_CLS_ACT
587 skb->tc_verd = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588#endif
589#endif
Lennert Buytenhek04a4bb52008-10-01 02:33:12 -0700590}
591
592/* Free everything but the sk_buff shell. */
593static void skb_release_all(struct sk_buff *skb)
594{
595 skb_release_head_state(skb);
Pablo Neira5e71d9d2013-06-03 09:28:43 +0000596 if (likely(skb->head))
Patrick McHardy0ebd0ac2013-04-17 06:46:58 +0000597 skb_release_data(skb);
Herbert Xu2d4baff2007-11-26 23:11:19 +0800598}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
Herbert Xu2d4baff2007-11-26 23:11:19 +0800600/**
601 * __kfree_skb - private function
602 * @skb: buffer
603 *
604 * Free an sk_buff. Release anything attached to the buffer.
605 * Clean the state. This is an internal helper function. Users should
606 * always call kfree_skb
607 */
608
609void __kfree_skb(struct sk_buff *skb)
610{
611 skb_release_all(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 kfree_skbmem(skb);
613}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800614EXPORT_SYMBOL(__kfree_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
616/**
Jörn Engel231d06a2006-03-20 21:28:35 -0800617 * kfree_skb - free an sk_buff
618 * @skb: buffer to free
619 *
620 * Drop a reference to the buffer and free it if the usage count has
621 * hit zero.
622 */
623void kfree_skb(struct sk_buff *skb)
624{
625 if (unlikely(!skb))
626 return;
627 if (likely(atomic_read(&skb->users) == 1))
628 smp_rmb();
629 else if (likely(!atomic_dec_and_test(&skb->users)))
630 return;
Neil Hormanead2ceb2009-03-11 09:49:55 +0000631 trace_kfree_skb(skb, __builtin_return_address(0));
Jörn Engel231d06a2006-03-20 21:28:35 -0800632 __kfree_skb(skb);
633}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800634EXPORT_SYMBOL(kfree_skb);
Jörn Engel231d06a2006-03-20 21:28:35 -0800635
Eric Dumazetbd8a7032013-06-24 06:26:00 -0700636void kfree_skb_list(struct sk_buff *segs)
637{
638 while (segs) {
639 struct sk_buff *next = segs->next;
640
641 kfree_skb(segs);
642 segs = next;
643 }
644}
645EXPORT_SYMBOL(kfree_skb_list);
646
Stephen Hemmingerd1a203e2008-11-01 21:01:09 -0700647/**
Michael S. Tsirkin25121172012-11-01 09:16:28 +0000648 * skb_tx_error - report an sk_buff xmit error
649 * @skb: buffer that triggered an error
650 *
651 * Report xmit error if a device callback is tracking this skb.
652 * skb must be freed afterwards.
653 */
654void skb_tx_error(struct sk_buff *skb)
655{
656 if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) {
657 struct ubuf_info *uarg;
658
659 uarg = skb_shinfo(skb)->destructor_arg;
660 if (uarg->callback)
661 uarg->callback(uarg, false);
662 skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
663 }
664}
665EXPORT_SYMBOL(skb_tx_error);
666
667/**
Neil Hormanead2ceb2009-03-11 09:49:55 +0000668 * consume_skb - free an skbuff
669 * @skb: buffer to free
670 *
671 * Drop a ref to the buffer and free it if the usage count has hit zero
672 * Functions identically to kfree_skb, but kfree_skb assumes that the frame
673 * is being dropped after a failure and notes that
674 */
675void consume_skb(struct sk_buff *skb)
676{
677 if (unlikely(!skb))
678 return;
679 if (likely(atomic_read(&skb->users) == 1))
680 smp_rmb();
681 else if (likely(!atomic_dec_and_test(&skb->users)))
682 return;
Koki Sanagi07dc22e2010-08-23 18:46:12 +0900683 trace_consume_skb(skb);
Neil Hormanead2ceb2009-03-11 09:49:55 +0000684 __kfree_skb(skb);
685}
686EXPORT_SYMBOL(consume_skb);
687
Eric Dumazetb1937222014-09-28 22:18:47 -0700688/* Make sure a field is enclosed inside headers_start/headers_end section */
689#define CHECK_SKB_FIELD(field) \
690 BUILD_BUG_ON(offsetof(struct sk_buff, field) < \
691 offsetof(struct sk_buff, headers_start)); \
692 BUILD_BUG_ON(offsetof(struct sk_buff, field) > \
693 offsetof(struct sk_buff, headers_end)); \
694
Herbert Xudec18812007-10-14 00:37:30 -0700695static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
696{
697 new->tstamp = old->tstamp;
Eric Dumazetb1937222014-09-28 22:18:47 -0700698 /* We do not copy old->sk */
Herbert Xudec18812007-10-14 00:37:30 -0700699 new->dev = old->dev;
Eric Dumazetb1937222014-09-28 22:18:47 -0700700 memcpy(new->cb, old->cb, sizeof(old->cb));
Eric Dumazet7fee2262010-05-11 23:19:48 +0000701 skb_dst_copy(new, old);
Alexey Dobriyandef8b4f2008-10-28 13:24:06 -0700702#ifdef CONFIG_XFRM
Herbert Xudec18812007-10-14 00:37:30 -0700703 new->sp = secpath_get(old->sp);
704#endif
Eric Dumazetb1937222014-09-28 22:18:47 -0700705 __nf_copy(new, old, false);
Patrick McHardy6aa895b2008-07-14 22:49:06 -0700706
Eric Dumazetb1937222014-09-28 22:18:47 -0700707 /* Note : this field could be in headers_start/headers_end section
708 * It is not yet because we do not want to have a 16 bit hole
709 */
710 new->queue_mapping = old->queue_mapping;
Eliezer Tamir06021292013-06-10 11:39:50 +0300711
Eric Dumazetb1937222014-09-28 22:18:47 -0700712 memcpy(&new->headers_start, &old->headers_start,
713 offsetof(struct sk_buff, headers_end) -
714 offsetof(struct sk_buff, headers_start));
715 CHECK_SKB_FIELD(protocol);
716 CHECK_SKB_FIELD(csum);
717 CHECK_SKB_FIELD(hash);
718 CHECK_SKB_FIELD(priority);
719 CHECK_SKB_FIELD(skb_iif);
720 CHECK_SKB_FIELD(vlan_proto);
721 CHECK_SKB_FIELD(vlan_tci);
722 CHECK_SKB_FIELD(transport_header);
723 CHECK_SKB_FIELD(network_header);
724 CHECK_SKB_FIELD(mac_header);
725 CHECK_SKB_FIELD(inner_protocol);
726 CHECK_SKB_FIELD(inner_transport_header);
727 CHECK_SKB_FIELD(inner_network_header);
728 CHECK_SKB_FIELD(inner_mac_header);
729 CHECK_SKB_FIELD(mark);
730#ifdef CONFIG_NETWORK_SECMARK
731 CHECK_SKB_FIELD(secmark);
732#endif
Cong Wange0d10952013-08-01 11:10:25 +0800733#ifdef CONFIG_NET_RX_BUSY_POLL
Eric Dumazetb1937222014-09-28 22:18:47 -0700734 CHECK_SKB_FIELD(napi_id);
Eliezer Tamir06021292013-06-10 11:39:50 +0300735#endif
Eric Dumazetb1937222014-09-28 22:18:47 -0700736#ifdef CONFIG_NET_SCHED
737 CHECK_SKB_FIELD(tc_index);
738#ifdef CONFIG_NET_CLS_ACT
739 CHECK_SKB_FIELD(tc_verd);
740#endif
741#endif
742
Herbert Xudec18812007-10-14 00:37:30 -0700743}
744
Herbert Xu82c49a32009-05-22 22:11:37 +0000745/*
746 * You should not add any new code to this function. Add it to
747 * __copy_skb_header above instead.
748 */
Herbert Xue0053ec2007-10-14 00:37:52 -0700749static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751#define C(x) n->x = skb->x
752
753 n->next = n->prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 n->sk = NULL;
Herbert Xudec18812007-10-14 00:37:30 -0700755 __copy_skb_header(n, skb);
756
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 C(len);
758 C(data_len);
Alexey Dobriyan3e6b3b22007-03-16 15:00:46 -0700759 C(mac_len);
Patrick McHardy334a8132007-06-25 04:35:20 -0700760 n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
Paul Moore02f1c892008-01-07 21:56:41 -0800761 n->cloned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 n->nohdr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 n->destructor = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 C(tail);
765 C(end);
Paul Moore02f1c892008-01-07 21:56:41 -0800766 C(head);
Eric Dumazetd3836f22012-04-27 00:33:38 +0000767 C(head_frag);
Paul Moore02f1c892008-01-07 21:56:41 -0800768 C(data);
769 C(truesize);
770 atomic_set(&n->users, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
772 atomic_inc(&(skb_shinfo(skb)->dataref));
773 skb->cloned = 1;
774
775 return n;
Herbert Xue0053ec2007-10-14 00:37:52 -0700776#undef C
777}
778
779/**
780 * skb_morph - morph one skb into another
781 * @dst: the skb to receive the contents
782 * @src: the skb to supply the contents
783 *
784 * This is identical to skb_clone except that the target skb is
785 * supplied by the user.
786 *
787 * The target skb is returned upon exit.
788 */
789struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
790{
Herbert Xu2d4baff2007-11-26 23:11:19 +0800791 skb_release_all(dst);
Herbert Xue0053ec2007-10-14 00:37:52 -0700792 return __skb_clone(dst, src);
793}
794EXPORT_SYMBOL_GPL(skb_morph);
795
Ben Hutchings2c530402012-07-10 10:55:09 +0000796/**
797 * skb_copy_ubufs - copy userspace skb frags buffers to kernel
Michael S. Tsirkin48c83012011-08-31 08:03:29 +0000798 * @skb: the skb to modify
799 * @gfp_mask: allocation priority
800 *
801 * This must be called on SKBTX_DEV_ZEROCOPY skb.
802 * It will copy all frags into kernel and drop the reference
803 * to userspace pages.
804 *
805 * If this function is called from an interrupt gfp_mask() must be
806 * %GFP_ATOMIC.
807 *
808 * Returns 0 on success or a negative error code on failure
809 * to allocate kernel memory to copy to.
810 */
811int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask)
Shirley Maa6686f22011-07-06 12:22:12 +0000812{
813 int i;
814 int num_frags = skb_shinfo(skb)->nr_frags;
815 struct page *page, *head = NULL;
816 struct ubuf_info *uarg = skb_shinfo(skb)->destructor_arg;
817
818 for (i = 0; i < num_frags; i++) {
819 u8 *vaddr;
820 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
821
Krishna Kumar02756ed2012-07-17 02:05:29 +0000822 page = alloc_page(gfp_mask);
Shirley Maa6686f22011-07-06 12:22:12 +0000823 if (!page) {
824 while (head) {
Sunghan Suh40dadff2013-07-12 16:17:23 +0900825 struct page *next = (struct page *)page_private(head);
Shirley Maa6686f22011-07-06 12:22:12 +0000826 put_page(head);
827 head = next;
828 }
829 return -ENOMEM;
830 }
Eric Dumazet51c56b02012-04-05 11:35:15 +0200831 vaddr = kmap_atomic(skb_frag_page(f));
Shirley Maa6686f22011-07-06 12:22:12 +0000832 memcpy(page_address(page),
Eric Dumazet9e903e02011-10-18 21:00:24 +0000833 vaddr + f->page_offset, skb_frag_size(f));
Eric Dumazet51c56b02012-04-05 11:35:15 +0200834 kunmap_atomic(vaddr);
Sunghan Suh40dadff2013-07-12 16:17:23 +0900835 set_page_private(page, (unsigned long)head);
Shirley Maa6686f22011-07-06 12:22:12 +0000836 head = page;
837 }
838
839 /* skb frags release userspace buffers */
Krishna Kumar02756ed2012-07-17 02:05:29 +0000840 for (i = 0; i < num_frags; i++)
Ian Campbella8605c62011-10-19 23:01:49 +0000841 skb_frag_unref(skb, i);
Shirley Maa6686f22011-07-06 12:22:12 +0000842
Michael S. Tsirkine19d6762012-11-01 09:16:22 +0000843 uarg->callback(uarg, false);
Shirley Maa6686f22011-07-06 12:22:12 +0000844
845 /* skb frags point to kernel buffers */
Krishna Kumar02756ed2012-07-17 02:05:29 +0000846 for (i = num_frags - 1; i >= 0; i--) {
847 __skb_fill_page_desc(skb, i, head, 0,
848 skb_shinfo(skb)->frags[i].size);
Sunghan Suh40dadff2013-07-12 16:17:23 +0900849 head = (struct page *)page_private(head);
Shirley Maa6686f22011-07-06 12:22:12 +0000850 }
Michael S. Tsirkin48c83012011-08-31 08:03:29 +0000851
852 skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
Shirley Maa6686f22011-07-06 12:22:12 +0000853 return 0;
854}
Michael S. Tsirkindcc0fb72012-07-20 09:23:20 +0000855EXPORT_SYMBOL_GPL(skb_copy_ubufs);
Shirley Maa6686f22011-07-06 12:22:12 +0000856
Herbert Xue0053ec2007-10-14 00:37:52 -0700857/**
858 * skb_clone - duplicate an sk_buff
859 * @skb: buffer to clone
860 * @gfp_mask: allocation priority
861 *
862 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
863 * copies share the same packet data but not structure. The new
864 * buffer has a reference count of 1. If the allocation fails the
865 * function returns %NULL otherwise the new buffer is returned.
866 *
867 * If this function is called from an interrupt gfp_mask() must be
868 * %GFP_ATOMIC.
869 */
870
871struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
872{
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700873 struct sk_buff_fclones *fclones = container_of(skb,
874 struct sk_buff_fclones,
875 skb1);
Eric Dumazet6ffe75e2014-12-03 17:04:39 -0800876 struct sk_buff *n;
Herbert Xue0053ec2007-10-14 00:37:52 -0700877
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +0000878 if (skb_orphan_frags(skb, gfp_mask))
879 return NULL;
Shirley Maa6686f22011-07-06 12:22:12 +0000880
Herbert Xue0053ec2007-10-14 00:37:52 -0700881 if (skb->fclone == SKB_FCLONE_ORIG &&
Eric Dumazet6ffe75e2014-12-03 17:04:39 -0800882 atomic_read(&fclones->fclone_ref) == 1) {
883 n = &fclones->skb2;
884 atomic_set(&fclones->fclone_ref, 2);
Herbert Xue0053ec2007-10-14 00:37:52 -0700885 } else {
Mel Gormanc93bdd02012-07-31 16:44:19 -0700886 if (skb_pfmemalloc(skb))
887 gfp_mask |= __GFP_MEMALLOC;
888
Herbert Xue0053ec2007-10-14 00:37:52 -0700889 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
890 if (!n)
891 return NULL;
Vegard Nossumfe55f6d2008-08-30 12:16:35 +0200892
893 kmemcheck_annotate_bitfield(n, flags1);
Herbert Xue0053ec2007-10-14 00:37:52 -0700894 n->fclone = SKB_FCLONE_UNAVAILABLE;
895 }
896
897 return __skb_clone(n, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800899EXPORT_SYMBOL(skb_clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
Pravin B Shelarf5b17292013-03-07 13:21:40 +0000901static void skb_headers_offset_update(struct sk_buff *skb, int off)
902{
Eric Dumazet030737b2013-10-19 11:42:54 -0700903 /* Only adjust this if it actually is csum_start rather than csum */
904 if (skb->ip_summed == CHECKSUM_PARTIAL)
905 skb->csum_start += off;
Pravin B Shelarf5b17292013-03-07 13:21:40 +0000906 /* {transport,network,mac}_header and tail are relative to skb->head */
907 skb->transport_header += off;
908 skb->network_header += off;
909 if (skb_mac_header_was_set(skb))
910 skb->mac_header += off;
911 skb->inner_transport_header += off;
912 skb->inner_network_header += off;
Pravin B Shelaraefbd2b2013-03-07 13:21:46 +0000913 skb->inner_mac_header += off;
Pravin B Shelarf5b17292013-03-07 13:21:40 +0000914}
915
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
917{
Herbert Xudec18812007-10-14 00:37:30 -0700918 __copy_skb_header(new, old);
919
Herbert Xu79671682006-06-22 02:40:14 -0700920 skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
921 skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
922 skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923}
924
Mel Gormanc93bdd02012-07-31 16:44:19 -0700925static inline int skb_alloc_rx_flag(const struct sk_buff *skb)
926{
927 if (skb_pfmemalloc(skb))
928 return SKB_ALLOC_RX;
929 return 0;
930}
931
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932/**
933 * skb_copy - create private copy of an sk_buff
934 * @skb: buffer to copy
935 * @gfp_mask: allocation priority
936 *
937 * Make a copy of both an &sk_buff and its data. This is used when the
938 * caller wishes to modify the data and needs a private copy of the
939 * data to alter. Returns %NULL on failure or the pointer to the buffer
940 * on success. The returned buffer has a reference count of 1.
941 *
942 * As by-product this function converts non-linear &sk_buff to linear
943 * one, so that &sk_buff becomes completely private and caller is allowed
944 * to modify all the data of returned buffer. This means that this
945 * function is not recommended for use in circumstances when only
946 * header is going to be modified. Use pskb_copy() instead.
947 */
948
Al Virodd0fc662005-10-07 07:46:04 +0100949struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950{
Eric Dumazet6602ceb2010-09-01 05:25:10 +0000951 int headerlen = skb_headroom(skb);
Alexander Duyckec47ea82012-05-04 14:26:56 +0000952 unsigned int size = skb_end_offset(skb) + skb->data_len;
Mel Gormanc93bdd02012-07-31 16:44:19 -0700953 struct sk_buff *n = __alloc_skb(size, gfp_mask,
954 skb_alloc_rx_flag(skb), NUMA_NO_NODE);
Eric Dumazet6602ceb2010-09-01 05:25:10 +0000955
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 if (!n)
957 return NULL;
958
959 /* Set the data pointer */
960 skb_reserve(n, headerlen);
961 /* Set the tail pointer and length */
962 skb_put(n, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
964 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
965 BUG();
966
967 copy_skb_header(n, skb);
968 return n;
969}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800970EXPORT_SYMBOL(skb_copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
972/**
Octavian Purdilabad93e92014-06-12 01:36:26 +0300973 * __pskb_copy_fclone - create copy of an sk_buff with private head.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 * @skb: buffer to copy
Eric Dumazet117632e2011-12-03 21:39:53 +0000975 * @headroom: headroom of new skb
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 * @gfp_mask: allocation priority
Octavian Purdilabad93e92014-06-12 01:36:26 +0300977 * @fclone: if true allocate the copy of the skb from the fclone
978 * cache instead of the head cache; it is recommended to set this
979 * to true for the cases where the copy will likely be cloned
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 *
981 * Make a copy of both an &sk_buff and part of its data, located
982 * in header. Fragmented data remain shared. This is used when
983 * the caller wishes to modify only header of &sk_buff and needs
984 * private copy of the header to alter. Returns %NULL on failure
985 * or the pointer to the buffer on success.
986 * The returned buffer has a reference count of 1.
987 */
988
Octavian Purdilabad93e92014-06-12 01:36:26 +0300989struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom,
990 gfp_t gfp_mask, bool fclone)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991{
Eric Dumazet117632e2011-12-03 21:39:53 +0000992 unsigned int size = skb_headlen(skb) + headroom;
Octavian Purdilabad93e92014-06-12 01:36:26 +0300993 int flags = skb_alloc_rx_flag(skb) | (fclone ? SKB_ALLOC_FCLONE : 0);
994 struct sk_buff *n = __alloc_skb(size, gfp_mask, flags, NUMA_NO_NODE);
Eric Dumazet6602ceb2010-09-01 05:25:10 +0000995
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 if (!n)
997 goto out;
998
999 /* Set the data pointer */
Eric Dumazet117632e2011-12-03 21:39:53 +00001000 skb_reserve(n, headroom);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 /* Set the tail pointer and length */
1002 skb_put(n, skb_headlen(skb));
1003 /* Copy the bytes */
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001004 skb_copy_from_linear_data(skb, n->data, n->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
Herbert Xu25f484a2006-11-07 14:57:15 -08001006 n->truesize += skb->data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 n->data_len = skb->data_len;
1008 n->len = skb->len;
1009
1010 if (skb_shinfo(skb)->nr_frags) {
1011 int i;
1012
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +00001013 if (skb_orphan_frags(skb, gfp_mask)) {
1014 kfree_skb(n);
1015 n = NULL;
1016 goto out;
Shirley Maa6686f22011-07-06 12:22:12 +00001017 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1019 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
Ian Campbellea2ab692011-08-22 23:44:58 +00001020 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 }
1022 skb_shinfo(n)->nr_frags = i;
1023 }
1024
David S. Miller21dc3302010-08-23 00:13:46 -07001025 if (skb_has_frag_list(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
1027 skb_clone_fraglist(n);
1028 }
1029
1030 copy_skb_header(n, skb);
1031out:
1032 return n;
1033}
Octavian Purdilabad93e92014-06-12 01:36:26 +03001034EXPORT_SYMBOL(__pskb_copy_fclone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
1036/**
1037 * pskb_expand_head - reallocate header of &sk_buff
1038 * @skb: buffer to reallocate
1039 * @nhead: room to add at head
1040 * @ntail: room to add at tail
1041 * @gfp_mask: allocation priority
1042 *
Mathias Krausebc323832013-11-07 14:18:26 +01001043 * Expands (or creates identical copy, if @nhead and @ntail are zero)
1044 * header of @skb. &sk_buff itself is not changed. &sk_buff MUST have
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 * reference count of 1. Returns zero in the case of success or error,
1046 * if expansion failed. In the last case, &sk_buff is not changed.
1047 *
1048 * All the pointers pointing into skb header may change and must be
1049 * reloaded after call to this function.
1050 */
1051
Victor Fusco86a76ca2005-07-08 14:57:47 -07001052int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
Al Virodd0fc662005-10-07 07:46:04 +01001053 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054{
1055 int i;
1056 u8 *data;
Alexander Duyckec47ea82012-05-04 14:26:56 +00001057 int size = nhead + skb_end_offset(skb) + ntail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 long off;
1059
Herbert Xu4edd87a2008-10-01 07:09:38 -07001060 BUG_ON(nhead < 0);
1061
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 if (skb_shared(skb))
1063 BUG();
1064
1065 size = SKB_DATA_ALIGN(size);
1066
Mel Gormanc93bdd02012-07-31 16:44:19 -07001067 if (skb_pfmemalloc(skb))
1068 gfp_mask |= __GFP_MEMALLOC;
1069 data = kmalloc_reserve(size + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
1070 gfp_mask, NUMA_NO_NODE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 if (!data)
1072 goto nodata;
Eric Dumazet87151b82012-04-10 20:08:39 +00001073 size = SKB_WITH_OVERHEAD(ksize(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074
1075 /* Copy only real data... and, alas, header. This should be
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001076 * optimized for the cases when header is void.
1077 */
1078 memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head);
1079
1080 memcpy((struct skb_shared_info *)(data + size),
1081 skb_shinfo(skb),
Eric Dumazetfed66382010-07-22 19:09:08 +00001082 offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083
Alexander Duyck3e245912012-05-04 14:26:51 +00001084 /*
1085 * if shinfo is shared we must drop the old head gracefully, but if it
1086 * is not we can just drop the old head and let the existing refcount
1087 * be since all we did is relocate the values
1088 */
1089 if (skb_cloned(skb)) {
Shirley Maa6686f22011-07-06 12:22:12 +00001090 /* copy this zero copy skb frags */
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +00001091 if (skb_orphan_frags(skb, gfp_mask))
1092 goto nofrags;
Eric Dumazet1fd63042010-09-02 23:09:32 +00001093 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
Ian Campbellea2ab692011-08-22 23:44:58 +00001094 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
Eric Dumazet1fd63042010-09-02 23:09:32 +00001096 if (skb_has_frag_list(skb))
1097 skb_clone_fraglist(skb);
1098
1099 skb_release_data(skb);
Alexander Duyck3e245912012-05-04 14:26:51 +00001100 } else {
1101 skb_free_head(skb);
Eric Dumazet1fd63042010-09-02 23:09:32 +00001102 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 off = (data + nhead) - skb->head;
1104
1105 skb->head = data;
Eric Dumazetd3836f22012-04-27 00:33:38 +00001106 skb->head_frag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 skb->data += off;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001108#ifdef NET_SKBUFF_DATA_USES_OFFSET
1109 skb->end = size;
Patrick McHardy56eb8882007-04-09 11:45:04 -07001110 off = nhead;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001111#else
1112 skb->end = skb->head + size;
Patrick McHardy56eb8882007-04-09 11:45:04 -07001113#endif
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001114 skb->tail += off;
Peter Pan(潘卫平)b41abb42013-06-06 21:27:21 +08001115 skb_headers_offset_update(skb, nhead);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 skb->cloned = 0;
Patrick McHardy334a8132007-06-25 04:35:20 -07001117 skb->hdr_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 skb->nohdr = 0;
1119 atomic_set(&skb_shinfo(skb)->dataref, 1);
1120 return 0;
1121
Shirley Maa6686f22011-07-06 12:22:12 +00001122nofrags:
1123 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124nodata:
1125 return -ENOMEM;
1126}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001127EXPORT_SYMBOL(pskb_expand_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
1129/* Make private copy of skb with writable head and some headroom */
1130
1131struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
1132{
1133 struct sk_buff *skb2;
1134 int delta = headroom - skb_headroom(skb);
1135
1136 if (delta <= 0)
1137 skb2 = pskb_copy(skb, GFP_ATOMIC);
1138 else {
1139 skb2 = skb_clone(skb, GFP_ATOMIC);
1140 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
1141 GFP_ATOMIC)) {
1142 kfree_skb(skb2);
1143 skb2 = NULL;
1144 }
1145 }
1146 return skb2;
1147}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001148EXPORT_SYMBOL(skb_realloc_headroom);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
1150/**
1151 * skb_copy_expand - copy and expand sk_buff
1152 * @skb: buffer to copy
1153 * @newheadroom: new free bytes at head
1154 * @newtailroom: new free bytes at tail
1155 * @gfp_mask: allocation priority
1156 *
1157 * Make a copy of both an &sk_buff and its data and while doing so
1158 * allocate additional space.
1159 *
1160 * This is used when the caller wishes to modify the data and needs a
1161 * private copy of the data to alter as well as more space for new fields.
1162 * Returns %NULL on failure or the pointer to the buffer
1163 * on success. The returned buffer has a reference count of 1.
1164 *
1165 * You must pass %GFP_ATOMIC as the allocation priority if this function
1166 * is called from an interrupt.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 */
1168struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
Victor Fusco86a76ca2005-07-08 14:57:47 -07001169 int newheadroom, int newtailroom,
Al Virodd0fc662005-10-07 07:46:04 +01001170 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171{
1172 /*
1173 * Allocate the copy buffer
1174 */
Mel Gormanc93bdd02012-07-31 16:44:19 -07001175 struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom,
1176 gfp_mask, skb_alloc_rx_flag(skb),
1177 NUMA_NO_NODE);
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001178 int oldheadroom = skb_headroom(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 int head_copy_len, head_copy_off;
1180
1181 if (!n)
1182 return NULL;
1183
1184 skb_reserve(n, newheadroom);
1185
1186 /* Set the tail pointer and length */
1187 skb_put(n, skb->len);
1188
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001189 head_copy_len = oldheadroom;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 head_copy_off = 0;
1191 if (newheadroom <= head_copy_len)
1192 head_copy_len = newheadroom;
1193 else
1194 head_copy_off = newheadroom - head_copy_len;
1195
1196 /* Copy the linear header and data. */
1197 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
1198 skb->len + head_copy_len))
1199 BUG();
1200
1201 copy_skb_header(n, skb);
1202
Eric Dumazet030737b2013-10-19 11:42:54 -07001203 skb_headers_offset_update(n, newheadroom - oldheadroom);
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001204
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 return n;
1206}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001207EXPORT_SYMBOL(skb_copy_expand);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
1209/**
1210 * skb_pad - zero pad the tail of an skb
1211 * @skb: buffer to pad
1212 * @pad: space to pad
1213 *
1214 * Ensure that a buffer is followed by a padding area that is zero
1215 * filled. Used by network drivers which may DMA or transfer data
1216 * beyond the buffer end onto the wire.
1217 *
Herbert Xu5b057c62006-06-23 02:06:41 -07001218 * May return error in out of memory cases. The skb is freed on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001220
Herbert Xu5b057c62006-06-23 02:06:41 -07001221int skb_pad(struct sk_buff *skb, int pad)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222{
Herbert Xu5b057c62006-06-23 02:06:41 -07001223 int err;
1224 int ntail;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001225
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 /* If the skbuff is non linear tailroom is always zero.. */
Herbert Xu5b057c62006-06-23 02:06:41 -07001227 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 memset(skb->data+skb->len, 0, pad);
Herbert Xu5b057c62006-06-23 02:06:41 -07001229 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 }
Herbert Xu5b057c62006-06-23 02:06:41 -07001231
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001232 ntail = skb->data_len + pad - (skb->end - skb->tail);
Herbert Xu5b057c62006-06-23 02:06:41 -07001233 if (likely(skb_cloned(skb) || ntail > 0)) {
1234 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
1235 if (unlikely(err))
1236 goto free_skb;
1237 }
1238
1239 /* FIXME: The use of this function with non-linear skb's really needs
1240 * to be audited.
1241 */
1242 err = skb_linearize(skb);
1243 if (unlikely(err))
1244 goto free_skb;
1245
1246 memset(skb->data + skb->len, 0, pad);
1247 return 0;
1248
1249free_skb:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 kfree_skb(skb);
Herbert Xu5b057c62006-06-23 02:06:41 -07001251 return err;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001252}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001253EXPORT_SYMBOL(skb_pad);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001254
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -07001255/**
Mathias Krause0c7ddf32013-11-07 14:18:24 +01001256 * pskb_put - add data to the tail of a potentially fragmented buffer
1257 * @skb: start of the buffer to use
1258 * @tail: tail fragment of the buffer to use
1259 * @len: amount of data to add
1260 *
1261 * This function extends the used data area of the potentially
1262 * fragmented buffer. @tail must be the last fragment of @skb -- or
1263 * @skb itself. If this would exceed the total buffer size the kernel
1264 * will panic. A pointer to the first byte of the extra data is
1265 * returned.
1266 */
1267
1268unsigned char *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len)
1269{
1270 if (tail != skb) {
1271 skb->data_len += len;
1272 skb->len += len;
1273 }
1274 return skb_put(tail, len);
1275}
1276EXPORT_SYMBOL_GPL(pskb_put);
1277
1278/**
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -07001279 * skb_put - add data to a buffer
1280 * @skb: buffer to use
1281 * @len: amount of data to add
1282 *
1283 * This function extends the used data area of the buffer. If this would
1284 * exceed the total buffer size the kernel will panic. A pointer to the
1285 * first byte of the extra data is returned.
1286 */
1287unsigned char *skb_put(struct sk_buff *skb, unsigned int len)
1288{
1289 unsigned char *tmp = skb_tail_pointer(skb);
1290 SKB_LINEAR_ASSERT(skb);
1291 skb->tail += len;
1292 skb->len += len;
1293 if (unlikely(skb->tail > skb->end))
1294 skb_over_panic(skb, len, __builtin_return_address(0));
1295 return tmp;
1296}
1297EXPORT_SYMBOL(skb_put);
1298
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001299/**
Ilpo Järvinenc2aa2702008-03-27 17:52:40 -07001300 * skb_push - add data to the start of a buffer
1301 * @skb: buffer to use
1302 * @len: amount of data to add
1303 *
1304 * This function extends the used data area of the buffer at the buffer
1305 * start. If this would exceed the total buffer headroom the kernel will
1306 * panic. A pointer to the first byte of the extra data is returned.
1307 */
1308unsigned char *skb_push(struct sk_buff *skb, unsigned int len)
1309{
1310 skb->data -= len;
1311 skb->len += len;
1312 if (unlikely(skb->data<skb->head))
1313 skb_under_panic(skb, len, __builtin_return_address(0));
1314 return skb->data;
1315}
1316EXPORT_SYMBOL(skb_push);
1317
1318/**
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001319 * skb_pull - remove data from the start of a buffer
1320 * @skb: buffer to use
1321 * @len: amount of data to remove
1322 *
1323 * This function removes data from the start of a buffer, returning
1324 * the memory to the headroom. A pointer to the next data in the buffer
1325 * is returned. Once the data has been pulled future pushes will overwrite
1326 * the old data.
1327 */
1328unsigned char *skb_pull(struct sk_buff *skb, unsigned int len)
1329{
David S. Miller47d29642010-05-02 02:21:44 -07001330 return skb_pull_inline(skb, len);
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001331}
1332EXPORT_SYMBOL(skb_pull);
1333
Ilpo Järvinen419ae742008-03-27 17:54:01 -07001334/**
1335 * skb_trim - remove end from a buffer
1336 * @skb: buffer to alter
1337 * @len: new length
1338 *
1339 * Cut the length of a buffer down by removing data from the tail. If
1340 * the buffer is already under the length specified it is not modified.
1341 * The skb must be linear.
1342 */
1343void skb_trim(struct sk_buff *skb, unsigned int len)
1344{
1345 if (skb->len > len)
1346 __skb_trim(skb, len);
1347}
1348EXPORT_SYMBOL(skb_trim);
1349
Herbert Xu3cc0e872006-06-09 16:13:38 -07001350/* Trims skb to length len. It can change skb pointers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 */
1352
Herbert Xu3cc0e872006-06-09 16:13:38 -07001353int ___pskb_trim(struct sk_buff *skb, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354{
Herbert Xu27b437c2006-07-13 19:26:39 -07001355 struct sk_buff **fragp;
1356 struct sk_buff *frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 int offset = skb_headlen(skb);
1358 int nfrags = skb_shinfo(skb)->nr_frags;
1359 int i;
Herbert Xu27b437c2006-07-13 19:26:39 -07001360 int err;
1361
1362 if (skb_cloned(skb) &&
1363 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
1364 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001366 i = 0;
1367 if (offset >= len)
1368 goto drop_pages;
1369
1370 for (; i < nfrags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001371 int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]);
Herbert Xu27b437c2006-07-13 19:26:39 -07001372
1373 if (end < len) {
1374 offset = end;
1375 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 }
Herbert Xu27b437c2006-07-13 19:26:39 -07001377
Eric Dumazet9e903e02011-10-18 21:00:24 +00001378 skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset);
Herbert Xu27b437c2006-07-13 19:26:39 -07001379
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001380drop_pages:
Herbert Xu27b437c2006-07-13 19:26:39 -07001381 skb_shinfo(skb)->nr_frags = i;
1382
1383 for (; i < nfrags; i++)
Ian Campbellea2ab692011-08-22 23:44:58 +00001384 skb_frag_unref(skb, i);
Herbert Xu27b437c2006-07-13 19:26:39 -07001385
David S. Miller21dc3302010-08-23 00:13:46 -07001386 if (skb_has_frag_list(skb))
Herbert Xu27b437c2006-07-13 19:26:39 -07001387 skb_drop_fraglist(skb);
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001388 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 }
1390
Herbert Xu27b437c2006-07-13 19:26:39 -07001391 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
1392 fragp = &frag->next) {
1393 int end = offset + frag->len;
1394
1395 if (skb_shared(frag)) {
1396 struct sk_buff *nfrag;
1397
1398 nfrag = skb_clone(frag, GFP_ATOMIC);
1399 if (unlikely(!nfrag))
1400 return -ENOMEM;
1401
1402 nfrag->next = frag->next;
Eric Dumazet85bb2a62012-04-19 02:24:53 +00001403 consume_skb(frag);
Herbert Xu27b437c2006-07-13 19:26:39 -07001404 frag = nfrag;
1405 *fragp = frag;
1406 }
1407
1408 if (end < len) {
1409 offset = end;
1410 continue;
1411 }
1412
1413 if (end > len &&
1414 unlikely((err = pskb_trim(frag, len - offset))))
1415 return err;
1416
1417 if (frag->next)
1418 skb_drop_list(&frag->next);
1419 break;
1420 }
1421
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001422done:
Herbert Xu27b437c2006-07-13 19:26:39 -07001423 if (len > skb_headlen(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 skb->data_len -= skb->len - len;
1425 skb->len = len;
1426 } else {
Herbert Xu27b437c2006-07-13 19:26:39 -07001427 skb->len = len;
1428 skb->data_len = 0;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001429 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 }
1431
1432 return 0;
1433}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001434EXPORT_SYMBOL(___pskb_trim);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435
1436/**
1437 * __pskb_pull_tail - advance tail of skb header
1438 * @skb: buffer to reallocate
1439 * @delta: number of bytes to advance tail
1440 *
1441 * The function makes a sense only on a fragmented &sk_buff,
1442 * it expands header moving its tail forward and copying necessary
1443 * data from fragmented part.
1444 *
1445 * &sk_buff MUST have reference count of 1.
1446 *
1447 * Returns %NULL (and &sk_buff does not change) if pull failed
1448 * or value of new tail of skb in the case of success.
1449 *
1450 * All the pointers pointing into skb header may change and must be
1451 * reloaded after call to this function.
1452 */
1453
1454/* Moves tail of skb head forward, copying data from fragmented part,
1455 * when it is necessary.
1456 * 1. It may fail due to malloc failure.
1457 * 2. It may change skb pointers.
1458 *
1459 * It is pretty complicated. Luckily, it is called only in exceptional cases.
1460 */
1461unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
1462{
1463 /* If skb has not enough free space at tail, get new one
1464 * plus 128 bytes for future expansions. If we have enough
1465 * room at tail, reallocate without expansion only if skb is cloned.
1466 */
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001467 int i, k, eat = (skb->tail + delta) - skb->end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468
1469 if (eat > 0 || skb_cloned(skb)) {
1470 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
1471 GFP_ATOMIC))
1472 return NULL;
1473 }
1474
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001475 if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 BUG();
1477
1478 /* Optimization: no fragments, no reasons to preestimate
1479 * size of pulled pages. Superb.
1480 */
David S. Miller21dc3302010-08-23 00:13:46 -07001481 if (!skb_has_frag_list(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 goto pull_pages;
1483
1484 /* Estimate size of pulled pages. */
1485 eat = delta;
1486 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001487 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1488
1489 if (size >= eat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 goto pull_pages;
Eric Dumazet9e903e02011-10-18 21:00:24 +00001491 eat -= size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 }
1493
1494 /* If we need update frag list, we are in troubles.
1495 * Certainly, it possible to add an offset to skb data,
1496 * but taking into account that pulling is expected to
1497 * be very rare operation, it is worth to fight against
1498 * further bloating skb head and crucify ourselves here instead.
1499 * Pure masohism, indeed. 8)8)
1500 */
1501 if (eat) {
1502 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1503 struct sk_buff *clone = NULL;
1504 struct sk_buff *insp = NULL;
1505
1506 do {
Kris Katterjohn09a62662006-01-08 22:24:28 -08001507 BUG_ON(!list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508
1509 if (list->len <= eat) {
1510 /* Eaten as whole. */
1511 eat -= list->len;
1512 list = list->next;
1513 insp = list;
1514 } else {
1515 /* Eaten partially. */
1516
1517 if (skb_shared(list)) {
1518 /* Sucks! We need to fork list. :-( */
1519 clone = skb_clone(list, GFP_ATOMIC);
1520 if (!clone)
1521 return NULL;
1522 insp = list->next;
1523 list = clone;
1524 } else {
1525 /* This may be pulled without
1526 * problems. */
1527 insp = list;
1528 }
1529 if (!pskb_pull(list, eat)) {
Wei Yongjunf3fbbe02009-02-25 00:37:32 +00001530 kfree_skb(clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 return NULL;
1532 }
1533 break;
1534 }
1535 } while (eat);
1536
1537 /* Free pulled out fragments. */
1538 while ((list = skb_shinfo(skb)->frag_list) != insp) {
1539 skb_shinfo(skb)->frag_list = list->next;
1540 kfree_skb(list);
1541 }
1542 /* And insert new clone at head. */
1543 if (clone) {
1544 clone->next = list;
1545 skb_shinfo(skb)->frag_list = clone;
1546 }
1547 }
1548 /* Success! Now we may commit changes to skb data. */
1549
1550pull_pages:
1551 eat = delta;
1552 k = 0;
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) {
Ian Campbellea2ab692011-08-22 23:44:58 +00001557 skb_frag_unref(skb, i);
Eric Dumazet9e903e02011-10-18 21:00:24 +00001558 eat -= size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 } else {
1560 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
1561 if (eat) {
1562 skb_shinfo(skb)->frags[k].page_offset += eat;
Eric Dumazet9e903e02011-10-18 21:00:24 +00001563 skb_frag_size_sub(&skb_shinfo(skb)->frags[k], eat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 eat = 0;
1565 }
1566 k++;
1567 }
1568 }
1569 skb_shinfo(skb)->nr_frags = k;
1570
1571 skb->tail += delta;
1572 skb->data_len -= delta;
1573
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001574 return skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001576EXPORT_SYMBOL(__pskb_pull_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577
Eric Dumazet22019b12011-07-29 18:37:31 +00001578/**
1579 * skb_copy_bits - copy bits from skb to kernel buffer
1580 * @skb: source skb
1581 * @offset: offset in source
1582 * @to: destination buffer
1583 * @len: number of bytes to copy
1584 *
1585 * Copy the specified number of bytes from the source skb to the
1586 * destination buffer.
1587 *
1588 * CAUTION ! :
1589 * If its prototype is ever changed,
1590 * check arch/{*}/net/{*}.S files,
1591 * since it is called from BPF assembly code.
1592 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
1594{
David S. Miller1a028e52007-04-27 15:21:23 -07001595 int start = skb_headlen(skb);
David S. Millerfbb398a2009-06-09 00:18:59 -07001596 struct sk_buff *frag_iter;
1597 int i, copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598
1599 if (offset > (int)skb->len - len)
1600 goto fault;
1601
1602 /* Copy header. */
David S. Miller1a028e52007-04-27 15:21:23 -07001603 if ((copy = start - offset) > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 if (copy > len)
1605 copy = len;
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001606 skb_copy_from_linear_data_offset(skb, offset, to, copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 if ((len -= copy) == 0)
1608 return 0;
1609 offset += copy;
1610 to += copy;
1611 }
1612
1613 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07001614 int end;
Eric Dumazet51c56b02012-04-05 11:35:15 +02001615 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001617 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07001618
Eric Dumazet51c56b02012-04-05 11:35:15 +02001619 end = start + skb_frag_size(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 if ((copy = end - offset) > 0) {
1621 u8 *vaddr;
1622
1623 if (copy > len)
1624 copy = len;
1625
Eric Dumazet51c56b02012-04-05 11:35:15 +02001626 vaddr = kmap_atomic(skb_frag_page(f));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 memcpy(to,
Eric Dumazet51c56b02012-04-05 11:35:15 +02001628 vaddr + f->page_offset + offset - start,
1629 copy);
1630 kunmap_atomic(vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631
1632 if ((len -= copy) == 0)
1633 return 0;
1634 offset += copy;
1635 to += copy;
1636 }
David S. Miller1a028e52007-04-27 15:21:23 -07001637 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 }
1639
David S. Millerfbb398a2009-06-09 00:18:59 -07001640 skb_walk_frags(skb, frag_iter) {
1641 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642
David S. Millerfbb398a2009-06-09 00:18:59 -07001643 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644
David S. Millerfbb398a2009-06-09 00:18:59 -07001645 end = start + frag_iter->len;
1646 if ((copy = end - offset) > 0) {
1647 if (copy > len)
1648 copy = len;
1649 if (skb_copy_bits(frag_iter, offset - start, to, copy))
1650 goto fault;
1651 if ((len -= copy) == 0)
1652 return 0;
1653 offset += copy;
1654 to += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 }
David S. Millerfbb398a2009-06-09 00:18:59 -07001656 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 }
Shirley Maa6686f22011-07-06 12:22:12 +00001658
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 if (!len)
1660 return 0;
1661
1662fault:
1663 return -EFAULT;
1664}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001665EXPORT_SYMBOL(skb_copy_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666
Jens Axboe9c55e012007-11-06 23:30:13 -08001667/*
1668 * Callback from splice_to_pipe(), if we need to release some pages
1669 * at the end of the spd in case we error'ed out in filling the pipe.
1670 */
1671static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
1672{
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001673 put_page(spd->pages[i]);
1674}
Jens Axboe9c55e012007-11-06 23:30:13 -08001675
David S. Millera108d5f2012-04-23 23:06:11 -04001676static struct page *linear_to_page(struct page *page, unsigned int *len,
1677 unsigned int *offset,
Eric Dumazet18aafc62013-01-11 14:46:37 +00001678 struct sock *sk)
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001679{
Eric Dumazet5640f762012-09-23 23:04:42 +00001680 struct page_frag *pfrag = sk_page_frag(sk);
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001681
Eric Dumazet5640f762012-09-23 23:04:42 +00001682 if (!sk_page_frag_refill(sk, pfrag))
1683 return NULL;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001684
Eric Dumazet5640f762012-09-23 23:04:42 +00001685 *len = min_t(unsigned int, *len, pfrag->size - pfrag->offset);
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001686
Eric Dumazet5640f762012-09-23 23:04:42 +00001687 memcpy(page_address(pfrag->page) + pfrag->offset,
1688 page_address(page) + *offset, *len);
1689 *offset = pfrag->offset;
1690 pfrag->offset += *len;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001691
Eric Dumazet5640f762012-09-23 23:04:42 +00001692 return pfrag->page;
Jens Axboe9c55e012007-11-06 23:30:13 -08001693}
1694
Eric Dumazet41c73a02012-04-22 12:26:16 +00001695static bool spd_can_coalesce(const struct splice_pipe_desc *spd,
1696 struct page *page,
1697 unsigned int offset)
1698{
1699 return spd->nr_pages &&
1700 spd->pages[spd->nr_pages - 1] == page &&
1701 (spd->partial[spd->nr_pages - 1].offset +
1702 spd->partial[spd->nr_pages - 1].len == offset);
1703}
1704
Jens Axboe9c55e012007-11-06 23:30:13 -08001705/*
1706 * Fill page/offset/length into spd, if it can hold more pages.
1707 */
David S. Millera108d5f2012-04-23 23:06:11 -04001708static bool spd_fill_page(struct splice_pipe_desc *spd,
1709 struct pipe_inode_info *pipe, struct page *page,
1710 unsigned int *len, unsigned int offset,
Eric Dumazet18aafc62013-01-11 14:46:37 +00001711 bool linear,
David S. Millera108d5f2012-04-23 23:06:11 -04001712 struct sock *sk)
Jens Axboe9c55e012007-11-06 23:30:13 -08001713{
Eric Dumazet41c73a02012-04-22 12:26:16 +00001714 if (unlikely(spd->nr_pages == MAX_SKB_FRAGS))
David S. Millera108d5f2012-04-23 23:06:11 -04001715 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08001716
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001717 if (linear) {
Eric Dumazet18aafc62013-01-11 14:46:37 +00001718 page = linear_to_page(page, len, &offset, sk);
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001719 if (!page)
David S. Millera108d5f2012-04-23 23:06:11 -04001720 return true;
Eric Dumazet41c73a02012-04-22 12:26:16 +00001721 }
1722 if (spd_can_coalesce(spd, page, offset)) {
1723 spd->partial[spd->nr_pages - 1].len += *len;
David S. Millera108d5f2012-04-23 23:06:11 -04001724 return false;
Eric Dumazet41c73a02012-04-22 12:26:16 +00001725 }
1726 get_page(page);
Jens Axboe9c55e012007-11-06 23:30:13 -08001727 spd->pages[spd->nr_pages] = page;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001728 spd->partial[spd->nr_pages].len = *len;
Jens Axboe9c55e012007-11-06 23:30:13 -08001729 spd->partial[spd->nr_pages].offset = offset;
Jens Axboe9c55e012007-11-06 23:30:13 -08001730 spd->nr_pages++;
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001731
David S. Millera108d5f2012-04-23 23:06:11 -04001732 return false;
Jens Axboe9c55e012007-11-06 23:30:13 -08001733}
1734
David S. Millera108d5f2012-04-23 23:06:11 -04001735static bool __splice_segment(struct page *page, unsigned int poff,
1736 unsigned int plen, unsigned int *off,
Eric Dumazet18aafc62013-01-11 14:46:37 +00001737 unsigned int *len,
Eric Dumazetd7ccf7c2012-04-23 23:35:04 -04001738 struct splice_pipe_desc *spd, bool linear,
David S. Millera108d5f2012-04-23 23:06:11 -04001739 struct sock *sk,
1740 struct pipe_inode_info *pipe)
Octavian Purdila2870c432008-07-15 00:49:11 -07001741{
1742 if (!*len)
David S. Millera108d5f2012-04-23 23:06:11 -04001743 return true;
Octavian Purdila2870c432008-07-15 00:49:11 -07001744
1745 /* skip this segment if already processed */
1746 if (*off >= plen) {
1747 *off -= plen;
David S. Millera108d5f2012-04-23 23:06:11 -04001748 return false;
Octavian Purdiladb43a282008-06-27 17:27:21 -07001749 }
Jens Axboe9c55e012007-11-06 23:30:13 -08001750
Octavian Purdila2870c432008-07-15 00:49:11 -07001751 /* ignore any bits we already processed */
Eric Dumazet9ca1b222013-01-05 21:31:18 +00001752 poff += *off;
1753 plen -= *off;
1754 *off = 0;
Octavian Purdila2870c432008-07-15 00:49:11 -07001755
Eric Dumazet18aafc62013-01-11 14:46:37 +00001756 do {
1757 unsigned int flen = min(*len, plen);
Octavian Purdila2870c432008-07-15 00:49:11 -07001758
Eric Dumazet18aafc62013-01-11 14:46:37 +00001759 if (spd_fill_page(spd, pipe, page, &flen, poff,
1760 linear, sk))
1761 return true;
1762 poff += flen;
1763 plen -= flen;
1764 *len -= flen;
1765 } while (*len && plen);
Octavian Purdila2870c432008-07-15 00:49:11 -07001766
David S. Millera108d5f2012-04-23 23:06:11 -04001767 return false;
Octavian Purdila2870c432008-07-15 00:49:11 -07001768}
1769
1770/*
David S. Millera108d5f2012-04-23 23:06:11 -04001771 * Map linear and fragment data from the skb to spd. It reports true if the
Octavian Purdila2870c432008-07-15 00:49:11 -07001772 * pipe is full or if we already spliced the requested length.
1773 */
David S. Millera108d5f2012-04-23 23:06:11 -04001774static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe,
1775 unsigned int *offset, unsigned int *len,
1776 struct splice_pipe_desc *spd, struct sock *sk)
Octavian Purdila2870c432008-07-15 00:49:11 -07001777{
1778 int seg;
1779
Eric Dumazet1d0c0b32012-04-27 02:10:03 +00001780 /* map the linear part :
Alexander Duyck2996d312012-05-02 18:18:42 +00001781 * If skb->head_frag is set, this 'linear' part is backed by a
1782 * fragment, and if the head is not shared with any clones then
1783 * we can avoid a copy since we own the head portion of this page.
Jens Axboe9c55e012007-11-06 23:30:13 -08001784 */
Octavian Purdila2870c432008-07-15 00:49:11 -07001785 if (__splice_segment(virt_to_page(skb->data),
1786 (unsigned long) skb->data & (PAGE_SIZE - 1),
1787 skb_headlen(skb),
Eric Dumazet18aafc62013-01-11 14:46:37 +00001788 offset, len, spd,
Alexander Duyck3a7c1ee42012-05-03 01:09:42 +00001789 skb_head_is_locked(skb),
Eric Dumazet1d0c0b32012-04-27 02:10:03 +00001790 sk, pipe))
David S. Millera108d5f2012-04-23 23:06:11 -04001791 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08001792
1793 /*
1794 * then map the fragments
1795 */
Jens Axboe9c55e012007-11-06 23:30:13 -08001796 for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
1797 const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
1798
Ian Campbellea2ab692011-08-22 23:44:58 +00001799 if (__splice_segment(skb_frag_page(f),
Eric Dumazet9e903e02011-10-18 21:00:24 +00001800 f->page_offset, skb_frag_size(f),
Eric Dumazet18aafc62013-01-11 14:46:37 +00001801 offset, len, spd, false, sk, pipe))
David S. Millera108d5f2012-04-23 23:06:11 -04001802 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08001803 }
1804
David S. Millera108d5f2012-04-23 23:06:11 -04001805 return false;
Jens Axboe9c55e012007-11-06 23:30:13 -08001806}
1807
1808/*
1809 * Map data from the skb to a pipe. Should handle both the linear part,
1810 * the fragments, and the frag list. It does NOT handle frag lists within
1811 * the frag list, if such a thing exists. We'd probably need to recurse to
1812 * handle that cleanly.
1813 */
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001814int skb_splice_bits(struct sk_buff *skb, unsigned int offset,
Jens Axboe9c55e012007-11-06 23:30:13 -08001815 struct pipe_inode_info *pipe, unsigned int tlen,
1816 unsigned int flags)
1817{
Eric Dumazet41c73a02012-04-22 12:26:16 +00001818 struct partial_page partial[MAX_SKB_FRAGS];
1819 struct page *pages[MAX_SKB_FRAGS];
Jens Axboe9c55e012007-11-06 23:30:13 -08001820 struct splice_pipe_desc spd = {
1821 .pages = pages,
1822 .partial = partial,
Eric Dumazet047fe362012-06-12 15:24:40 +02001823 .nr_pages_max = MAX_SKB_FRAGS,
Jens Axboe9c55e012007-11-06 23:30:13 -08001824 .flags = flags,
Miklos Szeredi28a625c2014-01-22 19:36:57 +01001825 .ops = &nosteal_pipe_buf_ops,
Jens Axboe9c55e012007-11-06 23:30:13 -08001826 .spd_release = sock_spd_release,
1827 };
David S. Millerfbb398a2009-06-09 00:18:59 -07001828 struct sk_buff *frag_iter;
Jarek Poplawski7a67e562009-04-30 05:41:19 -07001829 struct sock *sk = skb->sk;
Jens Axboe35f3d142010-05-20 10:43:18 +02001830 int ret = 0;
1831
Jens Axboe9c55e012007-11-06 23:30:13 -08001832 /*
1833 * __skb_splice_bits() only fails if the output has no room left,
1834 * so no point in going over the frag_list for the error case.
1835 */
Jens Axboe35f3d142010-05-20 10:43:18 +02001836 if (__skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk))
Jens Axboe9c55e012007-11-06 23:30:13 -08001837 goto done;
1838 else if (!tlen)
1839 goto done;
1840
1841 /*
1842 * now see if we have a frag_list to map
1843 */
David S. Millerfbb398a2009-06-09 00:18:59 -07001844 skb_walk_frags(skb, frag_iter) {
1845 if (!tlen)
1846 break;
Jens Axboe35f3d142010-05-20 10:43:18 +02001847 if (__skb_splice_bits(frag_iter, pipe, &offset, &tlen, &spd, sk))
David S. Millerfbb398a2009-06-09 00:18:59 -07001848 break;
Jens Axboe9c55e012007-11-06 23:30:13 -08001849 }
1850
1851done:
Jens Axboe9c55e012007-11-06 23:30:13 -08001852 if (spd.nr_pages) {
Jens Axboe9c55e012007-11-06 23:30:13 -08001853 /*
1854 * Drop the socket lock, otherwise we have reverse
1855 * locking dependencies between sk_lock and i_mutex
1856 * here as compared to sendfile(). We enter here
1857 * with the socket lock held, and splice_to_pipe() will
1858 * grab the pipe inode lock. For sendfile() emulation,
1859 * we call into ->sendpage() with the i_mutex lock held
1860 * and networking will grab the socket lock.
1861 */
Octavian Purdila293ad602008-06-04 15:45:58 -07001862 release_sock(sk);
Jens Axboe9c55e012007-11-06 23:30:13 -08001863 ret = splice_to_pipe(pipe, &spd);
Octavian Purdila293ad602008-06-04 15:45:58 -07001864 lock_sock(sk);
Jens Axboe9c55e012007-11-06 23:30:13 -08001865 }
1866
Jens Axboe35f3d142010-05-20 10:43:18 +02001867 return ret;
Jens Axboe9c55e012007-11-06 23:30:13 -08001868}
1869
Herbert Xu357b40a2005-04-19 22:30:14 -07001870/**
1871 * skb_store_bits - store bits from kernel buffer to skb
1872 * @skb: destination buffer
1873 * @offset: offset in destination
1874 * @from: source buffer
1875 * @len: number of bytes to copy
1876 *
1877 * Copy the specified number of bytes from the source buffer to the
1878 * destination skb. This function handles all the messy bits of
1879 * traversing fragment lists and such.
1880 */
1881
Stephen Hemminger0c6fcc82007-04-20 16:40:01 -07001882int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
Herbert Xu357b40a2005-04-19 22:30:14 -07001883{
David S. Miller1a028e52007-04-27 15:21:23 -07001884 int start = skb_headlen(skb);
David S. Millerfbb398a2009-06-09 00:18:59 -07001885 struct sk_buff *frag_iter;
1886 int i, copy;
Herbert Xu357b40a2005-04-19 22:30:14 -07001887
1888 if (offset > (int)skb->len - len)
1889 goto fault;
1890
David S. Miller1a028e52007-04-27 15:21:23 -07001891 if ((copy = start - offset) > 0) {
Herbert Xu357b40a2005-04-19 22:30:14 -07001892 if (copy > len)
1893 copy = len;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001894 skb_copy_to_linear_data_offset(skb, offset, from, copy);
Herbert Xu357b40a2005-04-19 22:30:14 -07001895 if ((len -= copy) == 0)
1896 return 0;
1897 offset += copy;
1898 from += copy;
1899 }
1900
1901 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1902 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
David S. Miller1a028e52007-04-27 15:21:23 -07001903 int end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001904
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001905 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07001906
Eric Dumazet9e903e02011-10-18 21:00:24 +00001907 end = start + skb_frag_size(frag);
Herbert Xu357b40a2005-04-19 22:30:14 -07001908 if ((copy = end - offset) > 0) {
1909 u8 *vaddr;
1910
1911 if (copy > len)
1912 copy = len;
1913
Eric Dumazet51c56b02012-04-05 11:35:15 +02001914 vaddr = kmap_atomic(skb_frag_page(frag));
David S. Miller1a028e52007-04-27 15:21:23 -07001915 memcpy(vaddr + frag->page_offset + offset - start,
1916 from, copy);
Eric Dumazet51c56b02012-04-05 11:35:15 +02001917 kunmap_atomic(vaddr);
Herbert Xu357b40a2005-04-19 22:30:14 -07001918
1919 if ((len -= copy) == 0)
1920 return 0;
1921 offset += copy;
1922 from += copy;
1923 }
David S. Miller1a028e52007-04-27 15:21:23 -07001924 start = end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001925 }
1926
David S. Millerfbb398a2009-06-09 00:18:59 -07001927 skb_walk_frags(skb, frag_iter) {
1928 int end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001929
David S. Millerfbb398a2009-06-09 00:18:59 -07001930 WARN_ON(start > offset + len);
Herbert Xu357b40a2005-04-19 22:30:14 -07001931
David S. Millerfbb398a2009-06-09 00:18:59 -07001932 end = start + frag_iter->len;
1933 if ((copy = end - offset) > 0) {
1934 if (copy > len)
1935 copy = len;
1936 if (skb_store_bits(frag_iter, offset - start,
1937 from, copy))
1938 goto fault;
1939 if ((len -= copy) == 0)
1940 return 0;
1941 offset += copy;
1942 from += copy;
Herbert Xu357b40a2005-04-19 22:30:14 -07001943 }
David S. Millerfbb398a2009-06-09 00:18:59 -07001944 start = end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001945 }
1946 if (!len)
1947 return 0;
1948
1949fault:
1950 return -EFAULT;
1951}
Herbert Xu357b40a2005-04-19 22:30:14 -07001952EXPORT_SYMBOL(skb_store_bits);
1953
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954/* Checksum skb data. */
Daniel Borkmann2817a332013-10-30 11:50:51 +01001955__wsum __skb_checksum(const struct sk_buff *skb, int offset, int len,
1956 __wsum csum, const struct skb_checksum_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957{
David S. Miller1a028e52007-04-27 15:21:23 -07001958 int start = skb_headlen(skb);
1959 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07001960 struct sk_buff *frag_iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 int pos = 0;
1962
1963 /* Checksum header. */
1964 if (copy > 0) {
1965 if (copy > len)
1966 copy = len;
Daniel Borkmann2817a332013-10-30 11:50:51 +01001967 csum = ops->update(skb->data + offset, copy, csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 if ((len -= copy) == 0)
1969 return csum;
1970 offset += copy;
1971 pos = copy;
1972 }
1973
1974 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07001975 int end;
Eric Dumazet51c56b02012-04-05 11:35:15 +02001976 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001978 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07001979
Eric Dumazet51c56b02012-04-05 11:35:15 +02001980 end = start + skb_frag_size(frag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 if ((copy = end - offset) > 0) {
Al Viro44bb9362006-11-14 21:36:14 -08001982 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 u8 *vaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984
1985 if (copy > len)
1986 copy = len;
Eric Dumazet51c56b02012-04-05 11:35:15 +02001987 vaddr = kmap_atomic(skb_frag_page(frag));
Daniel Borkmann2817a332013-10-30 11:50:51 +01001988 csum2 = ops->update(vaddr + frag->page_offset +
1989 offset - start, copy, 0);
Eric Dumazet51c56b02012-04-05 11:35:15 +02001990 kunmap_atomic(vaddr);
Daniel Borkmann2817a332013-10-30 11:50:51 +01001991 csum = ops->combine(csum, csum2, pos, copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 if (!(len -= copy))
1993 return csum;
1994 offset += copy;
1995 pos += copy;
1996 }
David S. Miller1a028e52007-04-27 15:21:23 -07001997 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 }
1999
David S. Millerfbb398a2009-06-09 00:18:59 -07002000 skb_walk_frags(skb, frag_iter) {
2001 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002
David S. Millerfbb398a2009-06-09 00:18:59 -07002003 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004
David S. Millerfbb398a2009-06-09 00:18:59 -07002005 end = start + frag_iter->len;
2006 if ((copy = end - offset) > 0) {
2007 __wsum csum2;
2008 if (copy > len)
2009 copy = len;
Daniel Borkmann2817a332013-10-30 11:50:51 +01002010 csum2 = __skb_checksum(frag_iter, offset - start,
2011 copy, 0, ops);
2012 csum = ops->combine(csum, csum2, pos, copy);
David S. Millerfbb398a2009-06-09 00:18:59 -07002013 if ((len -= copy) == 0)
2014 return csum;
2015 offset += copy;
2016 pos += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002018 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08002020 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021
2022 return csum;
2023}
Daniel Borkmann2817a332013-10-30 11:50:51 +01002024EXPORT_SYMBOL(__skb_checksum);
2025
2026__wsum skb_checksum(const struct sk_buff *skb, int offset,
2027 int len, __wsum csum)
2028{
2029 const struct skb_checksum_ops ops = {
Daniel Borkmanncea80ea2013-11-04 17:10:25 +01002030 .update = csum_partial_ext,
Daniel Borkmann2817a332013-10-30 11:50:51 +01002031 .combine = csum_block_add_ext,
2032 };
2033
2034 return __skb_checksum(skb, offset, len, csum, &ops);
2035}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002036EXPORT_SYMBOL(skb_checksum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037
2038/* Both of above in one bottle. */
2039
Al Viro81d77662006-11-14 21:37:33 -08002040__wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
2041 u8 *to, int len, __wsum csum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042{
David S. Miller1a028e52007-04-27 15:21:23 -07002043 int start = skb_headlen(skb);
2044 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07002045 struct sk_buff *frag_iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 int pos = 0;
2047
2048 /* Copy header. */
2049 if (copy > 0) {
2050 if (copy > len)
2051 copy = len;
2052 csum = csum_partial_copy_nocheck(skb->data + offset, to,
2053 copy, csum);
2054 if ((len -= copy) == 0)
2055 return csum;
2056 offset += copy;
2057 to += copy;
2058 pos = copy;
2059 }
2060
2061 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07002062 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002064 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07002065
Eric Dumazet9e903e02011-10-18 21:00:24 +00002066 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 if ((copy = end - offset) > 0) {
Al Viro50842052006-11-14 21:36:34 -08002068 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 u8 *vaddr;
2070 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2071
2072 if (copy > len)
2073 copy = len;
Eric Dumazet51c56b02012-04-05 11:35:15 +02002074 vaddr = kmap_atomic(skb_frag_page(frag));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 csum2 = csum_partial_copy_nocheck(vaddr +
David S. Miller1a028e52007-04-27 15:21:23 -07002076 frag->page_offset +
2077 offset - start, to,
2078 copy, 0);
Eric Dumazet51c56b02012-04-05 11:35:15 +02002079 kunmap_atomic(vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 csum = csum_block_add(csum, csum2, pos);
2081 if (!(len -= copy))
2082 return csum;
2083 offset += copy;
2084 to += copy;
2085 pos += copy;
2086 }
David S. Miller1a028e52007-04-27 15:21:23 -07002087 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 }
2089
David S. Millerfbb398a2009-06-09 00:18:59 -07002090 skb_walk_frags(skb, frag_iter) {
2091 __wsum csum2;
2092 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093
David S. Millerfbb398a2009-06-09 00:18:59 -07002094 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095
David S. Millerfbb398a2009-06-09 00:18:59 -07002096 end = start + frag_iter->len;
2097 if ((copy = end - offset) > 0) {
2098 if (copy > len)
2099 copy = len;
2100 csum2 = skb_copy_and_csum_bits(frag_iter,
2101 offset - start,
2102 to, copy, 0);
2103 csum = csum_block_add(csum, csum2, pos);
2104 if ((len -= copy) == 0)
2105 return csum;
2106 offset += copy;
2107 to += copy;
2108 pos += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002110 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08002112 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 return csum;
2114}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002115EXPORT_SYMBOL(skb_copy_and_csum_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116
Thomas Grafaf2806f2013-12-13 15:22:17 +01002117 /**
2118 * skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy()
2119 * @from: source buffer
2120 *
2121 * Calculates the amount of linear headroom needed in the 'to' skb passed
2122 * into skb_zerocopy().
2123 */
2124unsigned int
2125skb_zerocopy_headlen(const struct sk_buff *from)
2126{
2127 unsigned int hlen = 0;
2128
2129 if (!from->head_frag ||
2130 skb_headlen(from) < L1_CACHE_BYTES ||
2131 skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
2132 hlen = skb_headlen(from);
2133
2134 if (skb_has_frag_list(from))
2135 hlen = from->len;
2136
2137 return hlen;
2138}
2139EXPORT_SYMBOL_GPL(skb_zerocopy_headlen);
2140
2141/**
2142 * skb_zerocopy - Zero copy skb to skb
2143 * @to: destination buffer
Masanari Iida7fceb4d2014-01-29 01:05:28 +09002144 * @from: source buffer
Thomas Grafaf2806f2013-12-13 15:22:17 +01002145 * @len: number of bytes to copy from source buffer
2146 * @hlen: size of linear headroom in destination buffer
2147 *
2148 * Copies up to `len` bytes from `from` to `to` by creating references
2149 * to the frags in the source buffer.
2150 *
2151 * The `hlen` as calculated by skb_zerocopy_headlen() specifies the
2152 * headroom in the `to` buffer.
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002153 *
2154 * Return value:
2155 * 0: everything is OK
2156 * -ENOMEM: couldn't orphan frags of @from due to lack of memory
2157 * -EFAULT: skb_copy_bits() found some problem with skb geometry
Thomas Grafaf2806f2013-12-13 15:22:17 +01002158 */
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002159int
2160skb_zerocopy(struct sk_buff *to, struct sk_buff *from, int len, int hlen)
Thomas Grafaf2806f2013-12-13 15:22:17 +01002161{
2162 int i, j = 0;
2163 int plen = 0; /* length of skb->head fragment */
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002164 int ret;
Thomas Grafaf2806f2013-12-13 15:22:17 +01002165 struct page *page;
2166 unsigned int offset;
2167
2168 BUG_ON(!from->head_frag && !hlen);
2169
2170 /* dont bother with small payloads */
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002171 if (len <= skb_tailroom(to))
2172 return skb_copy_bits(from, 0, skb_put(to, len), len);
Thomas Grafaf2806f2013-12-13 15:22:17 +01002173
2174 if (hlen) {
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002175 ret = skb_copy_bits(from, 0, skb_put(to, hlen), hlen);
2176 if (unlikely(ret))
2177 return ret;
Thomas Grafaf2806f2013-12-13 15:22:17 +01002178 len -= hlen;
2179 } else {
2180 plen = min_t(int, skb_headlen(from), len);
2181 if (plen) {
2182 page = virt_to_head_page(from->head);
2183 offset = from->data - (unsigned char *)page_address(page);
2184 __skb_fill_page_desc(to, 0, page, offset, plen);
2185 get_page(page);
2186 j = 1;
2187 len -= plen;
2188 }
2189 }
2190
2191 to->truesize += len + plen;
2192 to->len += len + plen;
2193 to->data_len += len + plen;
2194
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002195 if (unlikely(skb_orphan_frags(from, GFP_ATOMIC))) {
2196 skb_tx_error(from);
2197 return -ENOMEM;
2198 }
2199
Thomas Grafaf2806f2013-12-13 15:22:17 +01002200 for (i = 0; i < skb_shinfo(from)->nr_frags; i++) {
2201 if (!len)
2202 break;
2203 skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i];
2204 skb_shinfo(to)->frags[j].size = min_t(int, skb_shinfo(to)->frags[j].size, len);
2205 len -= skb_shinfo(to)->frags[j].size;
2206 skb_frag_ref(to, j);
2207 j++;
2208 }
2209 skb_shinfo(to)->nr_frags = j;
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002210
2211 return 0;
Thomas Grafaf2806f2013-12-13 15:22:17 +01002212}
2213EXPORT_SYMBOL_GPL(skb_zerocopy);
2214
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
2216{
Al Virod3bc23e2006-11-14 21:24:49 -08002217 __wsum csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 long csstart;
2219
Patrick McHardy84fa7932006-08-29 16:44:56 -07002220 if (skb->ip_summed == CHECKSUM_PARTIAL)
Michał Mirosław55508d62010-12-14 15:24:08 +00002221 csstart = skb_checksum_start_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 else
2223 csstart = skb_headlen(skb);
2224
Kris Katterjohn09a62662006-01-08 22:24:28 -08002225 BUG_ON(csstart > skb_headlen(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03002227 skb_copy_from_linear_data(skb, to, csstart);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228
2229 csum = 0;
2230 if (csstart != skb->len)
2231 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
2232 skb->len - csstart, 0);
2233
Patrick McHardy84fa7932006-08-29 16:44:56 -07002234 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Al Viroff1dcad2006-11-20 18:07:29 -08002235 long csstuff = csstart + skb->csum_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236
Al Virod3bc23e2006-11-14 21:24:49 -08002237 *((__sum16 *)(to + csstuff)) = csum_fold(csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 }
2239}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002240EXPORT_SYMBOL(skb_copy_and_csum_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241
2242/**
2243 * skb_dequeue - remove from the head of the queue
2244 * @list: list to dequeue from
2245 *
2246 * Remove the head of the list. The list lock is taken so the function
2247 * may be used safely with other locking list functions. The head item is
2248 * returned or %NULL if the list is empty.
2249 */
2250
2251struct sk_buff *skb_dequeue(struct sk_buff_head *list)
2252{
2253 unsigned long flags;
2254 struct sk_buff *result;
2255
2256 spin_lock_irqsave(&list->lock, flags);
2257 result = __skb_dequeue(list);
2258 spin_unlock_irqrestore(&list->lock, flags);
2259 return result;
2260}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002261EXPORT_SYMBOL(skb_dequeue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262
2263/**
2264 * skb_dequeue_tail - remove from the tail of the queue
2265 * @list: list to dequeue from
2266 *
2267 * Remove the tail of the list. The list lock is taken so the function
2268 * may be used safely with other locking list functions. The tail item is
2269 * returned or %NULL if the list is empty.
2270 */
2271struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
2272{
2273 unsigned long flags;
2274 struct sk_buff *result;
2275
2276 spin_lock_irqsave(&list->lock, flags);
2277 result = __skb_dequeue_tail(list);
2278 spin_unlock_irqrestore(&list->lock, flags);
2279 return result;
2280}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002281EXPORT_SYMBOL(skb_dequeue_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282
2283/**
2284 * skb_queue_purge - empty a list
2285 * @list: list to empty
2286 *
2287 * Delete all buffers on an &sk_buff list. Each buffer is removed from
2288 * the list and one reference dropped. This function takes the list
2289 * lock and is atomic with respect to other list locking functions.
2290 */
2291void skb_queue_purge(struct sk_buff_head *list)
2292{
2293 struct sk_buff *skb;
2294 while ((skb = skb_dequeue(list)) != NULL)
2295 kfree_skb(skb);
2296}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002297EXPORT_SYMBOL(skb_queue_purge);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298
2299/**
2300 * skb_queue_head - queue a buffer at the list head
2301 * @list: list to use
2302 * @newsk: buffer to queue
2303 *
2304 * Queue a buffer at the start of the list. This function takes the
2305 * list lock and can be used safely with other locking &sk_buff functions
2306 * safely.
2307 *
2308 * A buffer cannot be placed on two lists at the same time.
2309 */
2310void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
2311{
2312 unsigned long flags;
2313
2314 spin_lock_irqsave(&list->lock, flags);
2315 __skb_queue_head(list, newsk);
2316 spin_unlock_irqrestore(&list->lock, flags);
2317}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002318EXPORT_SYMBOL(skb_queue_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319
2320/**
2321 * skb_queue_tail - queue a buffer at the list tail
2322 * @list: list to use
2323 * @newsk: buffer to queue
2324 *
2325 * Queue a buffer at the tail of the list. This function takes the
2326 * list lock and can be used safely with other locking &sk_buff functions
2327 * safely.
2328 *
2329 * A buffer cannot be placed on two lists at the same time.
2330 */
2331void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
2332{
2333 unsigned long flags;
2334
2335 spin_lock_irqsave(&list->lock, flags);
2336 __skb_queue_tail(list, newsk);
2337 spin_unlock_irqrestore(&list->lock, flags);
2338}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002339EXPORT_SYMBOL(skb_queue_tail);
David S. Miller8728b832005-08-09 19:25:21 -07002340
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341/**
2342 * skb_unlink - remove a buffer from a list
2343 * @skb: buffer to remove
David S. Miller8728b832005-08-09 19:25:21 -07002344 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 *
David S. Miller8728b832005-08-09 19:25:21 -07002346 * Remove a packet from a list. The list locks are taken and this
2347 * function is atomic with respect to other list locked calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 *
David S. Miller8728b832005-08-09 19:25:21 -07002349 * You must know what list the SKB is on.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 */
David S. Miller8728b832005-08-09 19:25:21 -07002351void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352{
David S. Miller8728b832005-08-09 19:25:21 -07002353 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354
David S. Miller8728b832005-08-09 19:25:21 -07002355 spin_lock_irqsave(&list->lock, flags);
2356 __skb_unlink(skb, list);
2357 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002359EXPORT_SYMBOL(skb_unlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361/**
2362 * skb_append - append a buffer
2363 * @old: buffer to insert after
2364 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07002365 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 *
2367 * Place a packet after a given packet in a list. The list locks are taken
2368 * and this function is atomic with respect to other list locked calls.
2369 * A buffer cannot be placed on two lists at the same time.
2370 */
David S. Miller8728b832005-08-09 19:25:21 -07002371void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372{
2373 unsigned long flags;
2374
David S. Miller8728b832005-08-09 19:25:21 -07002375 spin_lock_irqsave(&list->lock, flags);
Gerrit Renker7de6c032008-04-14 00:05:09 -07002376 __skb_queue_after(list, old, newsk);
David S. Miller8728b832005-08-09 19:25:21 -07002377 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002379EXPORT_SYMBOL(skb_append);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380
2381/**
2382 * skb_insert - insert a buffer
2383 * @old: buffer to insert before
2384 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07002385 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 *
David S. Miller8728b832005-08-09 19:25:21 -07002387 * Place a packet before a given packet in a list. The list locks are
2388 * taken and this function is atomic with respect to other list locked
2389 * calls.
2390 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 * A buffer cannot be placed on two lists at the same time.
2392 */
David S. Miller8728b832005-08-09 19:25:21 -07002393void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394{
2395 unsigned long flags;
2396
David S. Miller8728b832005-08-09 19:25:21 -07002397 spin_lock_irqsave(&list->lock, flags);
2398 __skb_insert(newsk, old->prev, old, list);
2399 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002401EXPORT_SYMBOL(skb_insert);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403static inline void skb_split_inside_header(struct sk_buff *skb,
2404 struct sk_buff* skb1,
2405 const u32 len, const int pos)
2406{
2407 int i;
2408
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03002409 skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
2410 pos - len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 /* And move data appendix as is. */
2412 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
2413 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
2414
2415 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
2416 skb_shinfo(skb)->nr_frags = 0;
2417 skb1->data_len = skb->data_len;
2418 skb1->len += skb1->data_len;
2419 skb->data_len = 0;
2420 skb->len = len;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002421 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422}
2423
2424static inline void skb_split_no_header(struct sk_buff *skb,
2425 struct sk_buff* skb1,
2426 const u32 len, int pos)
2427{
2428 int i, k = 0;
2429 const int nfrags = skb_shinfo(skb)->nr_frags;
2430
2431 skb_shinfo(skb)->nr_frags = 0;
2432 skb1->len = skb1->data_len = skb->len - len;
2433 skb->len = len;
2434 skb->data_len = len - pos;
2435
2436 for (i = 0; i < nfrags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00002437 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438
2439 if (pos + size > len) {
2440 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
2441
2442 if (pos < len) {
2443 /* Split frag.
2444 * We have two variants in this case:
2445 * 1. Move all the frag to the second
2446 * part, if it is possible. F.e.
2447 * this approach is mandatory for TUX,
2448 * where splitting is expensive.
2449 * 2. Split is accurately. We make this.
2450 */
Ian Campbellea2ab692011-08-22 23:44:58 +00002451 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002453 skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos);
2454 skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 skb_shinfo(skb)->nr_frags++;
2456 }
2457 k++;
2458 } else
2459 skb_shinfo(skb)->nr_frags++;
2460 pos += size;
2461 }
2462 skb_shinfo(skb1)->nr_frags = k;
2463}
2464
2465/**
2466 * skb_split - Split fragmented skb to two parts at length len.
2467 * @skb: the buffer to split
2468 * @skb1: the buffer to receive the second part
2469 * @len: new length for skb
2470 */
2471void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
2472{
2473 int pos = skb_headlen(skb);
2474
Amerigo Wang68534c62013-02-19 22:51:30 +00002475 skb_shinfo(skb1)->tx_flags = skb_shinfo(skb)->tx_flags & SKBTX_SHARED_FRAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 if (len < pos) /* Split line is inside header. */
2477 skb_split_inside_header(skb, skb1, len, pos);
2478 else /* Second chunk has no header, nothing to copy. */
2479 skb_split_no_header(skb, skb1, len, pos);
2480}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002481EXPORT_SYMBOL(skb_split);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482
Ilpo Järvinen9f782db2008-11-25 13:57:01 -08002483/* Shifting from/to a cloned skb is a no-go.
2484 *
2485 * Caller cannot keep skb_shinfo related pointers past calling here!
2486 */
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002487static int skb_prepare_for_shift(struct sk_buff *skb)
2488{
Ilpo Järvinen0ace2852008-11-24 21:30:21 -08002489 return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002490}
2491
2492/**
2493 * skb_shift - Shifts paged data partially from skb to another
2494 * @tgt: buffer into which tail data gets added
2495 * @skb: buffer from which the paged data comes from
2496 * @shiftlen: shift up to this many bytes
2497 *
2498 * Attempts to shift up to shiftlen worth of bytes, which may be less than
Feng King20e994a2011-11-21 01:47:11 +00002499 * the length of the skb, from skb to tgt. Returns number bytes shifted.
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002500 * It's up to caller to free skb if everything was shifted.
2501 *
2502 * If @tgt runs out of frags, the whole operation is aborted.
2503 *
2504 * Skb cannot include anything else but paged data while tgt is allowed
2505 * to have non-paged data as well.
2506 *
2507 * TODO: full sized shift could be optimized but that would need
2508 * specialized skb free'er to handle frags without up-to-date nr_frags.
2509 */
2510int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
2511{
2512 int from, to, merge, todo;
2513 struct skb_frag_struct *fragfrom, *fragto;
2514
2515 BUG_ON(shiftlen > skb->len);
2516 BUG_ON(skb_headlen(skb)); /* Would corrupt stream */
2517
2518 todo = shiftlen;
2519 from = 0;
2520 to = skb_shinfo(tgt)->nr_frags;
2521 fragfrom = &skb_shinfo(skb)->frags[from];
2522
2523 /* Actual merge is delayed until the point when we know we can
2524 * commit all, so that we don't have to undo partial changes
2525 */
2526 if (!to ||
Ian Campbellea2ab692011-08-22 23:44:58 +00002527 !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom),
2528 fragfrom->page_offset)) {
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002529 merge = -1;
2530 } else {
2531 merge = to - 1;
2532
Eric Dumazet9e903e02011-10-18 21:00:24 +00002533 todo -= skb_frag_size(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002534 if (todo < 0) {
2535 if (skb_prepare_for_shift(skb) ||
2536 skb_prepare_for_shift(tgt))
2537 return 0;
2538
Ilpo Järvinen9f782db2008-11-25 13:57:01 -08002539 /* All previous frag pointers might be stale! */
2540 fragfrom = &skb_shinfo(skb)->frags[from];
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002541 fragto = &skb_shinfo(tgt)->frags[merge];
2542
Eric Dumazet9e903e02011-10-18 21:00:24 +00002543 skb_frag_size_add(fragto, shiftlen);
2544 skb_frag_size_sub(fragfrom, shiftlen);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002545 fragfrom->page_offset += shiftlen;
2546
2547 goto onlymerged;
2548 }
2549
2550 from++;
2551 }
2552
2553 /* Skip full, not-fitting skb to avoid expensive operations */
2554 if ((shiftlen == skb->len) &&
2555 (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to))
2556 return 0;
2557
2558 if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt))
2559 return 0;
2560
2561 while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) {
2562 if (to == MAX_SKB_FRAGS)
2563 return 0;
2564
2565 fragfrom = &skb_shinfo(skb)->frags[from];
2566 fragto = &skb_shinfo(tgt)->frags[to];
2567
Eric Dumazet9e903e02011-10-18 21:00:24 +00002568 if (todo >= skb_frag_size(fragfrom)) {
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002569 *fragto = *fragfrom;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002570 todo -= skb_frag_size(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002571 from++;
2572 to++;
2573
2574 } else {
Ian Campbellea2ab692011-08-22 23:44:58 +00002575 __skb_frag_ref(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002576 fragto->page = fragfrom->page;
2577 fragto->page_offset = fragfrom->page_offset;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002578 skb_frag_size_set(fragto, todo);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002579
2580 fragfrom->page_offset += todo;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002581 skb_frag_size_sub(fragfrom, todo);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002582 todo = 0;
2583
2584 to++;
2585 break;
2586 }
2587 }
2588
2589 /* Ready to "commit" this state change to tgt */
2590 skb_shinfo(tgt)->nr_frags = to;
2591
2592 if (merge >= 0) {
2593 fragfrom = &skb_shinfo(skb)->frags[0];
2594 fragto = &skb_shinfo(tgt)->frags[merge];
2595
Eric Dumazet9e903e02011-10-18 21:00:24 +00002596 skb_frag_size_add(fragto, skb_frag_size(fragfrom));
Ian Campbellea2ab692011-08-22 23:44:58 +00002597 __skb_frag_unref(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002598 }
2599
2600 /* Reposition in the original skb */
2601 to = 0;
2602 while (from < skb_shinfo(skb)->nr_frags)
2603 skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++];
2604 skb_shinfo(skb)->nr_frags = to;
2605
2606 BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags);
2607
2608onlymerged:
2609 /* Most likely the tgt won't ever need its checksum anymore, skb on
2610 * the other hand might need it if it needs to be resent
2611 */
2612 tgt->ip_summed = CHECKSUM_PARTIAL;
2613 skb->ip_summed = CHECKSUM_PARTIAL;
2614
2615 /* Yak, is it really working this way? Some helper please? */
2616 skb->len -= shiftlen;
2617 skb->data_len -= shiftlen;
2618 skb->truesize -= shiftlen;
2619 tgt->len += shiftlen;
2620 tgt->data_len += shiftlen;
2621 tgt->truesize += shiftlen;
2622
2623 return shiftlen;
2624}
2625
Thomas Graf677e90e2005-06-23 20:59:51 -07002626/**
2627 * skb_prepare_seq_read - Prepare a sequential read of skb data
2628 * @skb: the buffer to read
2629 * @from: lower offset of data to be read
2630 * @to: upper offset of data to be read
2631 * @st: state variable
2632 *
2633 * Initializes the specified state variable. Must be called before
2634 * invoking skb_seq_read() for the first time.
2635 */
2636void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
2637 unsigned int to, struct skb_seq_state *st)
2638{
2639 st->lower_offset = from;
2640 st->upper_offset = to;
2641 st->root_skb = st->cur_skb = skb;
2642 st->frag_idx = st->stepped_offset = 0;
2643 st->frag_data = NULL;
2644}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002645EXPORT_SYMBOL(skb_prepare_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07002646
2647/**
2648 * skb_seq_read - Sequentially read skb data
2649 * @consumed: number of bytes consumed by the caller so far
2650 * @data: destination pointer for data to be returned
2651 * @st: state variable
2652 *
Mathias Krausebc323832013-11-07 14:18:26 +01002653 * Reads a block of skb data at @consumed relative to the
Thomas Graf677e90e2005-06-23 20:59:51 -07002654 * lower offset specified to skb_prepare_seq_read(). Assigns
Mathias Krausebc323832013-11-07 14:18:26 +01002655 * the head of the data block to @data and returns the length
Thomas Graf677e90e2005-06-23 20:59:51 -07002656 * of the block or 0 if the end of the skb data or the upper
2657 * offset has been reached.
2658 *
2659 * The caller is not required to consume all of the data
Mathias Krausebc323832013-11-07 14:18:26 +01002660 * returned, i.e. @consumed is typically set to the number
Thomas Graf677e90e2005-06-23 20:59:51 -07002661 * of bytes already consumed and the next call to
2662 * skb_seq_read() will return the remaining part of the block.
2663 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002664 * Note 1: The size of each block of data returned can be arbitrary,
Masanari Iidae793c0f2014-09-04 23:44:36 +09002665 * this limitation is the cost for zerocopy sequential
Thomas Graf677e90e2005-06-23 20:59:51 -07002666 * reads of potentially non linear data.
2667 *
Randy Dunlapbc2cda12008-02-13 15:03:25 -08002668 * Note 2: Fragment lists within fragments are not implemented
Thomas Graf677e90e2005-06-23 20:59:51 -07002669 * at the moment, state->root_skb could be replaced with
2670 * a stack for this purpose.
2671 */
2672unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
2673 struct skb_seq_state *st)
2674{
2675 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
2676 skb_frag_t *frag;
2677
Wedson Almeida Filhoaeb193e2013-06-23 23:33:48 -07002678 if (unlikely(abs_offset >= st->upper_offset)) {
2679 if (st->frag_data) {
2680 kunmap_atomic(st->frag_data);
2681 st->frag_data = NULL;
2682 }
Thomas Graf677e90e2005-06-23 20:59:51 -07002683 return 0;
Wedson Almeida Filhoaeb193e2013-06-23 23:33:48 -07002684 }
Thomas Graf677e90e2005-06-23 20:59:51 -07002685
2686next_skb:
Herbert Xu95e3b242009-01-29 16:07:52 -08002687 block_limit = skb_headlen(st->cur_skb) + st->stepped_offset;
Thomas Graf677e90e2005-06-23 20:59:51 -07002688
Thomas Chenault995b3372009-05-18 21:43:27 -07002689 if (abs_offset < block_limit && !st->frag_data) {
Herbert Xu95e3b242009-01-29 16:07:52 -08002690 *data = st->cur_skb->data + (abs_offset - st->stepped_offset);
Thomas Graf677e90e2005-06-23 20:59:51 -07002691 return block_limit - abs_offset;
2692 }
2693
2694 if (st->frag_idx == 0 && !st->frag_data)
2695 st->stepped_offset += skb_headlen(st->cur_skb);
2696
2697 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
2698 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
Eric Dumazet9e903e02011-10-18 21:00:24 +00002699 block_limit = skb_frag_size(frag) + st->stepped_offset;
Thomas Graf677e90e2005-06-23 20:59:51 -07002700
2701 if (abs_offset < block_limit) {
2702 if (!st->frag_data)
Eric Dumazet51c56b02012-04-05 11:35:15 +02002703 st->frag_data = kmap_atomic(skb_frag_page(frag));
Thomas Graf677e90e2005-06-23 20:59:51 -07002704
2705 *data = (u8 *) st->frag_data + frag->page_offset +
2706 (abs_offset - st->stepped_offset);
2707
2708 return block_limit - abs_offset;
2709 }
2710
2711 if (st->frag_data) {
Eric Dumazet51c56b02012-04-05 11:35:15 +02002712 kunmap_atomic(st->frag_data);
Thomas Graf677e90e2005-06-23 20:59:51 -07002713 st->frag_data = NULL;
2714 }
2715
2716 st->frag_idx++;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002717 st->stepped_offset += skb_frag_size(frag);
Thomas Graf677e90e2005-06-23 20:59:51 -07002718 }
2719
Olaf Kirch5b5a60d2007-06-23 23:11:52 -07002720 if (st->frag_data) {
Eric Dumazet51c56b02012-04-05 11:35:15 +02002721 kunmap_atomic(st->frag_data);
Olaf Kirch5b5a60d2007-06-23 23:11:52 -07002722 st->frag_data = NULL;
2723 }
2724
David S. Miller21dc3302010-08-23 00:13:46 -07002725 if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) {
Shyam Iyer71b33462009-01-29 16:12:42 -08002726 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
Thomas Graf677e90e2005-06-23 20:59:51 -07002727 st->frag_idx = 0;
2728 goto next_skb;
Shyam Iyer71b33462009-01-29 16:12:42 -08002729 } else if (st->cur_skb->next) {
2730 st->cur_skb = st->cur_skb->next;
Herbert Xu95e3b242009-01-29 16:07:52 -08002731 st->frag_idx = 0;
Thomas Graf677e90e2005-06-23 20:59:51 -07002732 goto next_skb;
2733 }
2734
2735 return 0;
2736}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002737EXPORT_SYMBOL(skb_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07002738
2739/**
2740 * skb_abort_seq_read - Abort a sequential read of skb data
2741 * @st: state variable
2742 *
2743 * Must be called if skb_seq_read() was not called until it
2744 * returned 0.
2745 */
2746void skb_abort_seq_read(struct skb_seq_state *st)
2747{
2748 if (st->frag_data)
Eric Dumazet51c56b02012-04-05 11:35:15 +02002749 kunmap_atomic(st->frag_data);
Thomas Graf677e90e2005-06-23 20:59:51 -07002750}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002751EXPORT_SYMBOL(skb_abort_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07002752
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002753#define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
2754
2755static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
2756 struct ts_config *conf,
2757 struct ts_state *state)
2758{
2759 return skb_seq_read(offset, text, TS_SKB_CB(state));
2760}
2761
2762static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
2763{
2764 skb_abort_seq_read(TS_SKB_CB(state));
2765}
2766
2767/**
2768 * skb_find_text - Find a text pattern in skb data
2769 * @skb: the buffer to look in
2770 * @from: search offset
2771 * @to: search limit
2772 * @config: textsearch configuration
2773 * @state: uninitialized textsearch state variable
2774 *
2775 * Finds a pattern in the skb data according to the specified
2776 * textsearch configuration. Use textsearch_next() to retrieve
2777 * subsequent occurrences of the pattern. Returns the offset
2778 * to the first occurrence or UINT_MAX if no match was found.
2779 */
2780unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
2781 unsigned int to, struct ts_config *config,
2782 struct ts_state *state)
2783{
Phil Oesterf72b9482006-06-26 00:00:57 -07002784 unsigned int ret;
2785
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002786 config->get_next_block = skb_ts_get_next_block;
2787 config->finish = skb_ts_finish;
2788
2789 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state));
2790
Phil Oesterf72b9482006-06-26 00:00:57 -07002791 ret = textsearch_find(config, state);
2792 return (ret <= to - from ? ret : UINT_MAX);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002793}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002794EXPORT_SYMBOL(skb_find_text);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002795
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002796/**
Ben Hutchings2c530402012-07-10 10:55:09 +00002797 * skb_append_datato_frags - append the user data to a skb
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002798 * @sk: sock structure
Masanari Iidae793c0f2014-09-04 23:44:36 +09002799 * @skb: skb structure to be appended with user data.
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002800 * @getfrag: call back function to be used for getting the user data
2801 * @from: pointer to user message iov
2802 * @length: length of the iov message
2803 *
2804 * Description: This procedure append the user data in the fragment part
2805 * of the skb if any page alloc fails user this procedure returns -ENOMEM
2806 */
2807int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
Martin Waitzdab96302005-12-05 13:40:12 -08002808 int (*getfrag)(void *from, char *to, int offset,
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002809 int len, int odd, struct sk_buff *skb),
2810 void *from, int length)
2811{
Eric Dumazetb2111722012-12-28 06:06:37 +00002812 int frg_cnt = skb_shinfo(skb)->nr_frags;
2813 int copy;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002814 int offset = 0;
2815 int ret;
Eric Dumazetb2111722012-12-28 06:06:37 +00002816 struct page_frag *pfrag = &current->task_frag;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002817
2818 do {
2819 /* Return error if we don't have space for new frag */
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002820 if (frg_cnt >= MAX_SKB_FRAGS)
Eric Dumazetb2111722012-12-28 06:06:37 +00002821 return -EMSGSIZE;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002822
Eric Dumazetb2111722012-12-28 06:06:37 +00002823 if (!sk_page_frag_refill(sk, pfrag))
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002824 return -ENOMEM;
2825
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002826 /* copy the user data to page */
Eric Dumazetb2111722012-12-28 06:06:37 +00002827 copy = min_t(int, length, pfrag->size - pfrag->offset);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002828
Eric Dumazetb2111722012-12-28 06:06:37 +00002829 ret = getfrag(from, page_address(pfrag->page) + pfrag->offset,
2830 offset, copy, 0, skb);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002831 if (ret < 0)
2832 return -EFAULT;
2833
2834 /* copy was successful so update the size parameters */
Eric Dumazetb2111722012-12-28 06:06:37 +00002835 skb_fill_page_desc(skb, frg_cnt, pfrag->page, pfrag->offset,
2836 copy);
2837 frg_cnt++;
2838 pfrag->offset += copy;
2839 get_page(pfrag->page);
2840
2841 skb->truesize += copy;
2842 atomic_add(copy, &sk->sk_wmem_alloc);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002843 skb->len += copy;
2844 skb->data_len += copy;
2845 offset += copy;
2846 length -= copy;
2847
2848 } while (length > 0);
2849
2850 return 0;
2851}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002852EXPORT_SYMBOL(skb_append_datato_frags);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002853
Herbert Xucbb042f2006-03-20 22:43:56 -08002854/**
2855 * skb_pull_rcsum - pull skb and update receive checksum
2856 * @skb: buffer to update
Herbert Xucbb042f2006-03-20 22:43:56 -08002857 * @len: length of data pulled
2858 *
2859 * This function performs an skb_pull on the packet and updates
Urs Thuermannfee54fa2008-02-12 22:03:25 -08002860 * the CHECKSUM_COMPLETE checksum. It should be used on
Patrick McHardy84fa7932006-08-29 16:44:56 -07002861 * receive path processing instead of skb_pull unless you know
2862 * that the checksum difference is zero (e.g., a valid IP header)
2863 * or you are setting ip_summed to CHECKSUM_NONE.
Herbert Xucbb042f2006-03-20 22:43:56 -08002864 */
2865unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
2866{
2867 BUG_ON(len > skb->len);
2868 skb->len -= len;
2869 BUG_ON(skb->len < skb->data_len);
2870 skb_postpull_rcsum(skb, skb->data, len);
2871 return skb->data += len;
2872}
Arnaldo Carvalho de Melof94691a2006-03-20 22:47:55 -08002873EXPORT_SYMBOL_GPL(skb_pull_rcsum);
2874
Herbert Xuf4c50d92006-06-22 03:02:40 -07002875/**
2876 * skb_segment - Perform protocol segmentation on skb.
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002877 * @head_skb: buffer to segment
Herbert Xu576a30e2006-06-27 13:22:38 -07002878 * @features: features for the output path (see dev->features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07002879 *
2880 * This function performs segmentation on the given skb. It returns
Ben Hutchings4c821d72008-04-13 21:52:48 -07002881 * a pointer to the first in a list of new skbs for the segments.
2882 * In case of error it returns ERR_PTR(err).
Herbert Xuf4c50d92006-06-22 03:02:40 -07002883 */
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002884struct sk_buff *skb_segment(struct sk_buff *head_skb,
2885 netdev_features_t features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07002886{
2887 struct sk_buff *segs = NULL;
2888 struct sk_buff *tail = NULL;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02002889 struct sk_buff *list_skb = skb_shinfo(head_skb)->frag_list;
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002890 skb_frag_t *frag = skb_shinfo(head_skb)->frags;
2891 unsigned int mss = skb_shinfo(head_skb)->gso_size;
2892 unsigned int doffset = head_skb->data - skb_mac_header(head_skb);
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02002893 struct sk_buff *frag_skb = head_skb;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002894 unsigned int offset = doffset;
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002895 unsigned int tnl_hlen = skb_tnl_header_len(head_skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002896 unsigned int headroom;
2897 unsigned int len;
Pravin B Shelarec5f0612013-03-07 09:28:01 +00002898 __be16 proto;
2899 bool csum;
Michał Mirosław04ed3e72011-01-24 15:32:47 -08002900 int sg = !!(features & NETIF_F_SG);
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002901 int nfrags = skb_shinfo(head_skb)->nr_frags;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002902 int err = -ENOMEM;
2903 int i = 0;
2904 int pos;
Vlad Yasevich53d64712014-03-27 17:26:18 -04002905 int dummy;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002906
Wei-Chun Chao5882a072014-06-08 23:48:54 -07002907 __skb_push(head_skb, doffset);
Vlad Yasevich53d64712014-03-27 17:26:18 -04002908 proto = skb_network_protocol(head_skb, &dummy);
Pravin B Shelarec5f0612013-03-07 09:28:01 +00002909 if (unlikely(!proto))
2910 return ERR_PTR(-EINVAL);
2911
Tom Herbert7e2b10c2014-06-04 17:20:02 -07002912 csum = !head_skb->encap_hdr_csum &&
2913 !!can_checksum_protocol(features, proto);
2914
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002915 headroom = skb_headroom(head_skb);
2916 pos = skb_headlen(head_skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002917
2918 do {
2919 struct sk_buff *nskb;
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02002920 skb_frag_t *nskb_frag;
Herbert Xuc8884ed2006-10-29 15:59:41 -08002921 int hsize;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002922 int size;
2923
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002924 len = head_skb->len - offset;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002925 if (len > mss)
2926 len = mss;
2927
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002928 hsize = skb_headlen(head_skb) - offset;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002929 if (hsize < 0)
2930 hsize = 0;
Herbert Xuc8884ed2006-10-29 15:59:41 -08002931 if (hsize > len || !sg)
2932 hsize = len;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002933
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02002934 if (!hsize && i >= nfrags && skb_headlen(list_skb) &&
2935 (skb_headlen(list_skb) == len || sg)) {
2936 BUG_ON(skb_headlen(list_skb) > len);
Herbert Xu89319d382008-12-15 23:26:06 -08002937
Herbert Xu9d8506c2013-11-21 11:10:04 -08002938 i = 0;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02002939 nfrags = skb_shinfo(list_skb)->nr_frags;
2940 frag = skb_shinfo(list_skb)->frags;
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02002941 frag_skb = list_skb;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02002942 pos += skb_headlen(list_skb);
Herbert Xu9d8506c2013-11-21 11:10:04 -08002943
2944 while (pos < offset + len) {
2945 BUG_ON(i >= nfrags);
2946
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02002947 size = skb_frag_size(frag);
Herbert Xu9d8506c2013-11-21 11:10:04 -08002948 if (pos + size > offset + len)
2949 break;
2950
2951 i++;
2952 pos += size;
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02002953 frag++;
Herbert Xu9d8506c2013-11-21 11:10:04 -08002954 }
2955
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02002956 nskb = skb_clone(list_skb, GFP_ATOMIC);
2957 list_skb = list_skb->next;
Herbert Xu89319d382008-12-15 23:26:06 -08002958
2959 if (unlikely(!nskb))
2960 goto err;
2961
Herbert Xu9d8506c2013-11-21 11:10:04 -08002962 if (unlikely(pskb_trim(nskb, len))) {
2963 kfree_skb(nskb);
2964 goto err;
2965 }
2966
Alexander Duyckec47ea82012-05-04 14:26:56 +00002967 hsize = skb_end_offset(nskb);
Herbert Xu89319d382008-12-15 23:26:06 -08002968 if (skb_cow_head(nskb, doffset + headroom)) {
2969 kfree_skb(nskb);
2970 goto err;
2971 }
2972
Alexander Duyckec47ea82012-05-04 14:26:56 +00002973 nskb->truesize += skb_end_offset(nskb) - hsize;
Herbert Xu89319d382008-12-15 23:26:06 -08002974 skb_release_head_state(nskb);
2975 __skb_push(nskb, doffset);
2976 } else {
Mel Gormanc93bdd02012-07-31 16:44:19 -07002977 nskb = __alloc_skb(hsize + doffset + headroom,
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002978 GFP_ATOMIC, skb_alloc_rx_flag(head_skb),
Mel Gormanc93bdd02012-07-31 16:44:19 -07002979 NUMA_NO_NODE);
Herbert Xu89319d382008-12-15 23:26:06 -08002980
2981 if (unlikely(!nskb))
2982 goto err;
2983
2984 skb_reserve(nskb, headroom);
2985 __skb_put(nskb, doffset);
2986 }
Herbert Xuf4c50d92006-06-22 03:02:40 -07002987
2988 if (segs)
2989 tail->next = nskb;
2990 else
2991 segs = nskb;
2992 tail = nskb;
2993
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002994 __copy_skb_header(nskb, head_skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002995
Eric Dumazet030737b2013-10-19 11:42:54 -07002996 skb_headers_offset_update(nskb, skb_headroom(nskb) - headroom);
Vlad Yasevichfcdfe3a2014-07-31 10:33:06 -04002997 skb_reset_mac_len(nskb);
Pravin B Shelar68c33162013-02-14 14:02:41 +00002998
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002999 skb_copy_from_linear_data_offset(head_skb, -tnl_hlen,
Pravin B Shelar68c33162013-02-14 14:02:41 +00003000 nskb->data - tnl_hlen,
3001 doffset + tnl_hlen);
Herbert Xu89319d382008-12-15 23:26:06 -08003002
Herbert Xu9d8506c2013-11-21 11:10:04 -08003003 if (nskb->len == len + doffset)
Simon Horman1cdbcb72013-05-19 15:46:49 +00003004 goto perform_csum_check;
Herbert Xu89319d382008-12-15 23:26:06 -08003005
Tom Herberte585f232014-11-04 09:06:54 -08003006 if (!sg && !nskb->remcsum_offload) {
Herbert Xu6f85a122008-08-15 14:55:02 -07003007 nskb->ip_summed = CHECKSUM_NONE;
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003008 nskb->csum = skb_copy_and_csum_bits(head_skb, offset,
Herbert Xuf4c50d92006-06-22 03:02:40 -07003009 skb_put(nskb, len),
3010 len, 0);
Tom Herbert7e2b10c2014-06-04 17:20:02 -07003011 SKB_GSO_CB(nskb)->csum_start =
Tom Herbertde843722014-06-25 12:51:01 -07003012 skb_headroom(nskb) + doffset;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003013 continue;
3014 }
3015
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003016 nskb_frag = skb_shinfo(nskb)->frags;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003017
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003018 skb_copy_from_linear_data_offset(head_skb, offset,
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03003019 skb_put(nskb, hsize), hsize);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003020
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003021 skb_shinfo(nskb)->tx_flags = skb_shinfo(head_skb)->tx_flags &
3022 SKBTX_SHARED_FRAG;
Eric Dumazetcef401d2013-01-25 20:34:37 +00003023
Herbert Xu9d8506c2013-11-21 11:10:04 -08003024 while (pos < offset + len) {
3025 if (i >= nfrags) {
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003026 BUG_ON(skb_headlen(list_skb));
Herbert Xu9d8506c2013-11-21 11:10:04 -08003027
3028 i = 0;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003029 nfrags = skb_shinfo(list_skb)->nr_frags;
3030 frag = skb_shinfo(list_skb)->frags;
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02003031 frag_skb = list_skb;
Herbert Xu9d8506c2013-11-21 11:10:04 -08003032
3033 BUG_ON(!nfrags);
3034
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003035 list_skb = list_skb->next;
Herbert Xu9d8506c2013-11-21 11:10:04 -08003036 }
3037
3038 if (unlikely(skb_shinfo(nskb)->nr_frags >=
3039 MAX_SKB_FRAGS)) {
3040 net_warn_ratelimited(
3041 "skb_segment: too many frags: %u %u\n",
3042 pos, mss);
3043 goto err;
3044 }
3045
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02003046 if (unlikely(skb_orphan_frags(frag_skb, GFP_ATOMIC)))
3047 goto err;
3048
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02003049 *nskb_frag = *frag;
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003050 __skb_frag_ref(nskb_frag);
3051 size = skb_frag_size(nskb_frag);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003052
3053 if (pos < offset) {
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003054 nskb_frag->page_offset += offset - pos;
3055 skb_frag_size_sub(nskb_frag, offset - pos);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003056 }
3057
Herbert Xu89319d382008-12-15 23:26:06 -08003058 skb_shinfo(nskb)->nr_frags++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003059
3060 if (pos + size <= offset + len) {
3061 i++;
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02003062 frag++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003063 pos += size;
3064 } else {
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003065 skb_frag_size_sub(nskb_frag, pos + size - (offset + len));
Herbert Xu89319d382008-12-15 23:26:06 -08003066 goto skip_fraglist;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003067 }
3068
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003069 nskb_frag++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003070 }
3071
Herbert Xu89319d382008-12-15 23:26:06 -08003072skip_fraglist:
Herbert Xuf4c50d92006-06-22 03:02:40 -07003073 nskb->data_len = len - hsize;
3074 nskb->len += nskb->data_len;
3075 nskb->truesize += nskb->data_len;
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003076
Simon Horman1cdbcb72013-05-19 15:46:49 +00003077perform_csum_check:
Tom Herberte585f232014-11-04 09:06:54 -08003078 if (!csum && !nskb->remcsum_offload) {
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003079 nskb->csum = skb_checksum(nskb, doffset,
3080 nskb->len - doffset, 0);
3081 nskb->ip_summed = CHECKSUM_NONE;
Tom Herbert7e2b10c2014-06-04 17:20:02 -07003082 SKB_GSO_CB(nskb)->csum_start =
3083 skb_headroom(nskb) + doffset;
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003084 }
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003085 } while ((offset += len) < head_skb->len);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003086
Eric Dumazetbec3cfd2014-10-03 20:59:19 -07003087 /* Some callers want to get the end of the list.
3088 * Put it in segs->prev to avoid walking the list.
3089 * (see validate_xmit_skb_list() for example)
3090 */
3091 segs->prev = tail;
Toshiaki Makita432c8562014-10-27 10:30:51 -07003092
3093 /* Following permits correct backpressure, for protocols
3094 * using skb_set_owner_w().
3095 * Idea is to tranfert ownership from head_skb to last segment.
3096 */
3097 if (head_skb->destructor == sock_wfree) {
3098 swap(tail->truesize, head_skb->truesize);
3099 swap(tail->destructor, head_skb->destructor);
3100 swap(tail->sk, head_skb->sk);
3101 }
Herbert Xuf4c50d92006-06-22 03:02:40 -07003102 return segs;
3103
3104err:
Eric Dumazet289dccb2013-12-20 14:29:08 -08003105 kfree_skb_list(segs);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003106 return ERR_PTR(err);
3107}
Herbert Xuf4c50d92006-06-22 03:02:40 -07003108EXPORT_SYMBOL_GPL(skb_segment);
3109
Herbert Xu71d93b32008-12-15 23:42:33 -08003110int skb_gro_receive(struct sk_buff **head, struct sk_buff *skb)
3111{
Eric Dumazet8a291112013-10-08 09:02:23 -07003112 struct skb_shared_info *pinfo, *skbinfo = skb_shinfo(skb);
Herbert Xu67147ba2009-05-26 18:50:22 +00003113 unsigned int offset = skb_gro_offset(skb);
3114 unsigned int headlen = skb_headlen(skb);
Eric Dumazet8a291112013-10-08 09:02:23 -07003115 struct sk_buff *nskb, *lp, *p = *head;
3116 unsigned int len = skb_gro_len(skb);
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003117 unsigned int delta_truesize;
Eric Dumazet8a291112013-10-08 09:02:23 -07003118 unsigned int headroom;
Herbert Xu71d93b32008-12-15 23:42:33 -08003119
Eric Dumazet8a291112013-10-08 09:02:23 -07003120 if (unlikely(p->len + len >= 65536))
Herbert Xu71d93b32008-12-15 23:42:33 -08003121 return -E2BIG;
3122
Eric Dumazet29e98242014-05-16 11:34:37 -07003123 lp = NAPI_GRO_CB(p)->last;
Eric Dumazet8a291112013-10-08 09:02:23 -07003124 pinfo = skb_shinfo(lp);
3125
3126 if (headlen <= offset) {
Herbert Xu42da6992009-05-26 18:50:19 +00003127 skb_frag_t *frag;
Herbert Xu66e92fc2009-05-26 18:50:32 +00003128 skb_frag_t *frag2;
Herbert Xu9aaa1562009-05-26 18:50:33 +00003129 int i = skbinfo->nr_frags;
3130 int nr_frags = pinfo->nr_frags + i;
Herbert Xu42da6992009-05-26 18:50:19 +00003131
Herbert Xu66e92fc2009-05-26 18:50:32 +00003132 if (nr_frags > MAX_SKB_FRAGS)
Eric Dumazet8a291112013-10-08 09:02:23 -07003133 goto merge;
Herbert Xu81705ad2009-01-29 14:19:51 +00003134
Eric Dumazet8a291112013-10-08 09:02:23 -07003135 offset -= headlen;
Herbert Xu9aaa1562009-05-26 18:50:33 +00003136 pinfo->nr_frags = nr_frags;
3137 skbinfo->nr_frags = 0;
Herbert Xuf5572062009-01-14 20:40:03 -08003138
Herbert Xu9aaa1562009-05-26 18:50:33 +00003139 frag = pinfo->frags + nr_frags;
3140 frag2 = skbinfo->frags + i;
Herbert Xu66e92fc2009-05-26 18:50:32 +00003141 do {
3142 *--frag = *--frag2;
3143 } while (--i);
3144
3145 frag->page_offset += offset;
Eric Dumazet9e903e02011-10-18 21:00:24 +00003146 skb_frag_size_sub(frag, offset);
Herbert Xu66e92fc2009-05-26 18:50:32 +00003147
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003148 /* all fragments truesize : remove (head size + sk_buff) */
Alexander Duyckec47ea82012-05-04 14:26:56 +00003149 delta_truesize = skb->truesize -
3150 SKB_TRUESIZE(skb_end_offset(skb));
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003151
Herbert Xuf5572062009-01-14 20:40:03 -08003152 skb->truesize -= skb->data_len;
3153 skb->len -= skb->data_len;
3154 skb->data_len = 0;
3155
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003156 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE;
Herbert Xu5d38a072009-01-04 16:13:40 -08003157 goto done;
Eric Dumazetd7e88832012-04-30 08:10:34 +00003158 } else if (skb->head_frag) {
3159 int nr_frags = pinfo->nr_frags;
3160 skb_frag_t *frag = pinfo->frags + nr_frags;
3161 struct page *page = virt_to_head_page(skb->head);
3162 unsigned int first_size = headlen - offset;
3163 unsigned int first_offset;
3164
3165 if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS)
Eric Dumazet8a291112013-10-08 09:02:23 -07003166 goto merge;
Eric Dumazetd7e88832012-04-30 08:10:34 +00003167
3168 first_offset = skb->data -
3169 (unsigned char *)page_address(page) +
3170 offset;
3171
3172 pinfo->nr_frags = nr_frags + 1 + skbinfo->nr_frags;
3173
3174 frag->page.p = page;
3175 frag->page_offset = first_offset;
3176 skb_frag_size_set(frag, first_size);
3177
3178 memcpy(frag + 1, skbinfo->frags, sizeof(*frag) * skbinfo->nr_frags);
3179 /* We dont need to clear skbinfo->nr_frags here */
3180
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003181 delta_truesize = skb->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
Eric Dumazetd7e88832012-04-30 08:10:34 +00003182 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD;
3183 goto done;
Eric Dumazet8a291112013-10-08 09:02:23 -07003184 }
Eric Dumazet73d3fe62014-09-29 10:34:29 -07003185 /* switch back to head shinfo */
3186 pinfo = skb_shinfo(p);
3187
Eric Dumazet8a291112013-10-08 09:02:23 -07003188 if (pinfo->frag_list)
3189 goto merge;
3190 if (skb_gro_len(p) != pinfo->gso_size)
Herbert Xu69c0cab2009-11-17 05:18:18 -08003191 return -E2BIG;
Herbert Xu71d93b32008-12-15 23:42:33 -08003192
3193 headroom = skb_headroom(p);
Eric Dumazet3d3be432010-09-01 00:50:51 +00003194 nskb = alloc_skb(headroom + skb_gro_offset(p), GFP_ATOMIC);
Herbert Xu71d93b32008-12-15 23:42:33 -08003195 if (unlikely(!nskb))
3196 return -ENOMEM;
3197
3198 __copy_skb_header(nskb, p);
3199 nskb->mac_len = p->mac_len;
3200
3201 skb_reserve(nskb, headroom);
Herbert Xu86911732009-01-29 14:19:50 +00003202 __skb_put(nskb, skb_gro_offset(p));
Herbert Xu71d93b32008-12-15 23:42:33 -08003203
Herbert Xu86911732009-01-29 14:19:50 +00003204 skb_set_mac_header(nskb, skb_mac_header(p) - p->data);
Herbert Xu71d93b32008-12-15 23:42:33 -08003205 skb_set_network_header(nskb, skb_network_offset(p));
3206 skb_set_transport_header(nskb, skb_transport_offset(p));
3207
Herbert Xu86911732009-01-29 14:19:50 +00003208 __skb_pull(p, skb_gro_offset(p));
3209 memcpy(skb_mac_header(nskb), skb_mac_header(p),
3210 p->data - skb_mac_header(p));
Herbert Xu71d93b32008-12-15 23:42:33 -08003211
Herbert Xu71d93b32008-12-15 23:42:33 -08003212 skb_shinfo(nskb)->frag_list = p;
Herbert Xu9aaa1562009-05-26 18:50:33 +00003213 skb_shinfo(nskb)->gso_size = pinfo->gso_size;
Herbert Xu622e0ca2010-05-20 23:07:56 -07003214 pinfo->gso_size = 0;
Eric Dumazetf4a775d2014-09-22 16:29:32 -07003215 __skb_header_release(p);
Eric Dumazetc3c7c252012-12-06 13:54:59 +00003216 NAPI_GRO_CB(nskb)->last = p;
Herbert Xu71d93b32008-12-15 23:42:33 -08003217
3218 nskb->data_len += p->len;
Eric Dumazetde8261c2012-02-13 04:09:20 +00003219 nskb->truesize += p->truesize;
Herbert Xu71d93b32008-12-15 23:42:33 -08003220 nskb->len += p->len;
3221
3222 *head = nskb;
3223 nskb->next = p->next;
3224 p->next = NULL;
3225
3226 p = nskb;
3227
3228merge:
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003229 delta_truesize = skb->truesize;
Herbert Xu67147ba2009-05-26 18:50:22 +00003230 if (offset > headlen) {
Michal Schmidtd1dc7ab2011-01-24 12:08:48 +00003231 unsigned int eat = offset - headlen;
3232
3233 skbinfo->frags[0].page_offset += eat;
Eric Dumazet9e903e02011-10-18 21:00:24 +00003234 skb_frag_size_sub(&skbinfo->frags[0], eat);
Michal Schmidtd1dc7ab2011-01-24 12:08:48 +00003235 skb->data_len -= eat;
3236 skb->len -= eat;
Herbert Xu67147ba2009-05-26 18:50:22 +00003237 offset = headlen;
Herbert Xu56035022009-02-05 21:26:52 -08003238 }
3239
Herbert Xu67147ba2009-05-26 18:50:22 +00003240 __skb_pull(skb, offset);
Herbert Xu56035022009-02-05 21:26:52 -08003241
Eric Dumazet29e98242014-05-16 11:34:37 -07003242 if (NAPI_GRO_CB(p)->last == p)
Eric Dumazet8a291112013-10-08 09:02:23 -07003243 skb_shinfo(p)->frag_list = skb;
3244 else
3245 NAPI_GRO_CB(p)->last->next = skb;
Eric Dumazetc3c7c252012-12-06 13:54:59 +00003246 NAPI_GRO_CB(p)->last = skb;
Eric Dumazetf4a775d2014-09-22 16:29:32 -07003247 __skb_header_release(skb);
Eric Dumazet8a291112013-10-08 09:02:23 -07003248 lp = p;
Herbert Xu71d93b32008-12-15 23:42:33 -08003249
Herbert Xu5d38a072009-01-04 16:13:40 -08003250done:
3251 NAPI_GRO_CB(p)->count++;
Herbert Xu37fe4732009-01-17 19:48:13 +00003252 p->data_len += len;
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003253 p->truesize += delta_truesize;
Herbert Xu37fe4732009-01-17 19:48:13 +00003254 p->len += len;
Eric Dumazet8a291112013-10-08 09:02:23 -07003255 if (lp != p) {
3256 lp->data_len += len;
3257 lp->truesize += delta_truesize;
3258 lp->len += len;
3259 }
Herbert Xu71d93b32008-12-15 23:42:33 -08003260 NAPI_GRO_CB(skb)->same_flow = 1;
3261 return 0;
3262}
Herbert Xu71d93b32008-12-15 23:42:33 -08003263
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264void __init skb_init(void)
3265{
3266 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
3267 sizeof(struct sk_buff),
3268 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07003269 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09003270 NULL);
David S. Millerd179cd12005-08-17 14:57:30 -07003271 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
Eric Dumazetd0bf4a92014-09-29 13:29:15 -07003272 sizeof(struct sk_buff_fclones),
David S. Millerd179cd12005-08-17 14:57:30 -07003273 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07003274 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09003275 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276}
3277
David Howells716ea3a2007-04-02 20:19:53 -07003278/**
3279 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
3280 * @skb: Socket buffer containing the buffers to be mapped
3281 * @sg: The scatter-gather list to map into
3282 * @offset: The offset into the buffer's contents to start mapping
3283 * @len: Length of buffer space to be mapped
3284 *
3285 * Fill the specified scatter-gather list with mappings/pointers into a
3286 * region of the buffer space attached to a socket buffer.
3287 */
David S. Miller51c739d2007-10-30 21:29:29 -07003288static int
3289__skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
David Howells716ea3a2007-04-02 20:19:53 -07003290{
David S. Miller1a028e52007-04-27 15:21:23 -07003291 int start = skb_headlen(skb);
3292 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07003293 struct sk_buff *frag_iter;
David Howells716ea3a2007-04-02 20:19:53 -07003294 int elt = 0;
3295
3296 if (copy > 0) {
3297 if (copy > len)
3298 copy = len;
Jens Axboe642f1492007-10-24 11:20:47 +02003299 sg_set_buf(sg, skb->data + offset, copy);
David Howells716ea3a2007-04-02 20:19:53 -07003300 elt++;
3301 if ((len -= copy) == 0)
3302 return elt;
3303 offset += copy;
3304 }
3305
3306 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07003307 int end;
David Howells716ea3a2007-04-02 20:19:53 -07003308
Ilpo Järvinen547b7922008-07-25 21:43:18 -07003309 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07003310
Eric Dumazet9e903e02011-10-18 21:00:24 +00003311 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
David Howells716ea3a2007-04-02 20:19:53 -07003312 if ((copy = end - offset) > 0) {
3313 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
3314
3315 if (copy > len)
3316 copy = len;
Ian Campbellea2ab692011-08-22 23:44:58 +00003317 sg_set_page(&sg[elt], skb_frag_page(frag), copy,
Jens Axboe642f1492007-10-24 11:20:47 +02003318 frag->page_offset+offset-start);
David Howells716ea3a2007-04-02 20:19:53 -07003319 elt++;
3320 if (!(len -= copy))
3321 return elt;
3322 offset += copy;
3323 }
David S. Miller1a028e52007-04-27 15:21:23 -07003324 start = end;
David Howells716ea3a2007-04-02 20:19:53 -07003325 }
3326
David S. Millerfbb398a2009-06-09 00:18:59 -07003327 skb_walk_frags(skb, frag_iter) {
3328 int end;
David Howells716ea3a2007-04-02 20:19:53 -07003329
David S. Millerfbb398a2009-06-09 00:18:59 -07003330 WARN_ON(start > offset + len);
David Howells716ea3a2007-04-02 20:19:53 -07003331
David S. Millerfbb398a2009-06-09 00:18:59 -07003332 end = start + frag_iter->len;
3333 if ((copy = end - offset) > 0) {
3334 if (copy > len)
3335 copy = len;
3336 elt += __skb_to_sgvec(frag_iter, sg+elt, offset - start,
3337 copy);
3338 if ((len -= copy) == 0)
3339 return elt;
3340 offset += copy;
David Howells716ea3a2007-04-02 20:19:53 -07003341 }
David S. Millerfbb398a2009-06-09 00:18:59 -07003342 start = end;
David Howells716ea3a2007-04-02 20:19:53 -07003343 }
3344 BUG_ON(len);
3345 return elt;
3346}
3347
Fan Du25a91d82014-01-18 09:54:23 +08003348/* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given
3349 * sglist without mark the sg which contain last skb data as the end.
3350 * So the caller can mannipulate sg list as will when padding new data after
3351 * the first call without calling sg_unmark_end to expend sg list.
3352 *
3353 * Scenario to use skb_to_sgvec_nomark:
3354 * 1. sg_init_table
3355 * 2. skb_to_sgvec_nomark(payload1)
3356 * 3. skb_to_sgvec_nomark(payload2)
3357 *
3358 * This is equivalent to:
3359 * 1. sg_init_table
3360 * 2. skb_to_sgvec(payload1)
3361 * 3. sg_unmark_end
3362 * 4. skb_to_sgvec(payload2)
3363 *
3364 * When mapping mutilple payload conditionally, skb_to_sgvec_nomark
3365 * is more preferable.
3366 */
3367int skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg,
3368 int offset, int len)
3369{
3370 return __skb_to_sgvec(skb, sg, offset, len);
3371}
3372EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark);
3373
David S. Miller51c739d2007-10-30 21:29:29 -07003374int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
3375{
3376 int nsg = __skb_to_sgvec(skb, sg, offset, len);
3377
Jens Axboec46f2332007-10-31 12:06:37 +01003378 sg_mark_end(&sg[nsg - 1]);
David S. Miller51c739d2007-10-30 21:29:29 -07003379
3380 return nsg;
3381}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003382EXPORT_SYMBOL_GPL(skb_to_sgvec);
David S. Miller51c739d2007-10-30 21:29:29 -07003383
David Howells716ea3a2007-04-02 20:19:53 -07003384/**
3385 * skb_cow_data - Check that a socket buffer's data buffers are writable
3386 * @skb: The socket buffer to check.
3387 * @tailbits: Amount of trailing space to be added
3388 * @trailer: Returned pointer to the skb where the @tailbits space begins
3389 *
3390 * Make sure that the data buffers attached to a socket buffer are
3391 * writable. If they are not, private copies are made of the data buffers
3392 * and the socket buffer is set to use these instead.
3393 *
3394 * If @tailbits is given, make sure that there is space to write @tailbits
3395 * bytes of data beyond current end of socket buffer. @trailer will be
3396 * set to point to the skb in which this space begins.
3397 *
3398 * The number of scatterlist elements required to completely map the
3399 * COW'd and extended socket buffer will be returned.
3400 */
3401int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
3402{
3403 int copyflag;
3404 int elt;
3405 struct sk_buff *skb1, **skb_p;
3406
3407 /* If skb is cloned or its head is paged, reallocate
3408 * head pulling out all the pages (pages are considered not writable
3409 * at the moment even if they are anonymous).
3410 */
3411 if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
3412 __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
3413 return -ENOMEM;
3414
3415 /* Easy case. Most of packets will go this way. */
David S. Miller21dc3302010-08-23 00:13:46 -07003416 if (!skb_has_frag_list(skb)) {
David Howells716ea3a2007-04-02 20:19:53 -07003417 /* A little of trouble, not enough of space for trailer.
3418 * This should not happen, when stack is tuned to generate
3419 * good frames. OK, on miss we reallocate and reserve even more
3420 * space, 128 bytes is fair. */
3421
3422 if (skb_tailroom(skb) < tailbits &&
3423 pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
3424 return -ENOMEM;
3425
3426 /* Voila! */
3427 *trailer = skb;
3428 return 1;
3429 }
3430
3431 /* Misery. We are in troubles, going to mincer fragments... */
3432
3433 elt = 1;
3434 skb_p = &skb_shinfo(skb)->frag_list;
3435 copyflag = 0;
3436
3437 while ((skb1 = *skb_p) != NULL) {
3438 int ntail = 0;
3439
3440 /* The fragment is partially pulled by someone,
3441 * this can happen on input. Copy it and everything
3442 * after it. */
3443
3444 if (skb_shared(skb1))
3445 copyflag = 1;
3446
3447 /* If the skb is the last, worry about trailer. */
3448
3449 if (skb1->next == NULL && tailbits) {
3450 if (skb_shinfo(skb1)->nr_frags ||
David S. Miller21dc3302010-08-23 00:13:46 -07003451 skb_has_frag_list(skb1) ||
David Howells716ea3a2007-04-02 20:19:53 -07003452 skb_tailroom(skb1) < tailbits)
3453 ntail = tailbits + 128;
3454 }
3455
3456 if (copyflag ||
3457 skb_cloned(skb1) ||
3458 ntail ||
3459 skb_shinfo(skb1)->nr_frags ||
David S. Miller21dc3302010-08-23 00:13:46 -07003460 skb_has_frag_list(skb1)) {
David Howells716ea3a2007-04-02 20:19:53 -07003461 struct sk_buff *skb2;
3462
3463 /* Fuck, we are miserable poor guys... */
3464 if (ntail == 0)
3465 skb2 = skb_copy(skb1, GFP_ATOMIC);
3466 else
3467 skb2 = skb_copy_expand(skb1,
3468 skb_headroom(skb1),
3469 ntail,
3470 GFP_ATOMIC);
3471 if (unlikely(skb2 == NULL))
3472 return -ENOMEM;
3473
3474 if (skb1->sk)
3475 skb_set_owner_w(skb2, skb1->sk);
3476
3477 /* Looking around. Are we still alive?
3478 * OK, link new skb, drop old one */
3479
3480 skb2->next = skb1->next;
3481 *skb_p = skb2;
3482 kfree_skb(skb1);
3483 skb1 = skb2;
3484 }
3485 elt++;
3486 *trailer = skb1;
3487 skb_p = &skb1->next;
3488 }
3489
3490 return elt;
3491}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003492EXPORT_SYMBOL_GPL(skb_cow_data);
David Howells716ea3a2007-04-02 20:19:53 -07003493
Eric Dumazetb1faf562010-05-31 23:44:05 -07003494static void sock_rmem_free(struct sk_buff *skb)
3495{
3496 struct sock *sk = skb->sk;
3497
3498 atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
3499}
3500
3501/*
3502 * Note: We dont mem charge error packets (no sk_forward_alloc changes)
3503 */
3504int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb)
3505{
3506 if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
Eric Dumazet95c96172012-04-15 05:58:06 +00003507 (unsigned int)sk->sk_rcvbuf)
Eric Dumazetb1faf562010-05-31 23:44:05 -07003508 return -ENOMEM;
3509
3510 skb_orphan(skb);
3511 skb->sk = sk;
3512 skb->destructor = sock_rmem_free;
3513 atomic_add(skb->truesize, &sk->sk_rmem_alloc);
3514
Eric Dumazetabb57ea2011-05-18 02:21:31 -04003515 /* before exiting rcu section, make sure dst is refcounted */
3516 skb_dst_force(skb);
3517
Eric Dumazetb1faf562010-05-31 23:44:05 -07003518 skb_queue_tail(&sk->sk_error_queue, skb);
3519 if (!sock_flag(sk, SOCK_DEAD))
David S. Miller676d2362014-04-11 16:15:36 -04003520 sk->sk_data_ready(sk);
Eric Dumazetb1faf562010-05-31 23:44:05 -07003521 return 0;
3522}
3523EXPORT_SYMBOL(sock_queue_err_skb);
3524
Willem de Bruijn364a9e92014-08-31 21:30:27 -04003525struct sk_buff *sock_dequeue_err_skb(struct sock *sk)
3526{
3527 struct sk_buff_head *q = &sk->sk_error_queue;
3528 struct sk_buff *skb, *skb_next;
3529 int err = 0;
3530
3531 spin_lock_bh(&q->lock);
3532 skb = __skb_dequeue(q);
3533 if (skb && (skb_next = skb_peek(q)))
3534 err = SKB_EXT_ERR(skb_next)->ee.ee_errno;
3535 spin_unlock_bh(&q->lock);
3536
3537 sk->sk_err = err;
3538 if (err)
3539 sk->sk_error_report(sk);
3540
3541 return skb;
3542}
3543EXPORT_SYMBOL(sock_dequeue_err_skb);
3544
Alexander Duyckcab41c42014-09-10 18:05:26 -04003545/**
3546 * skb_clone_sk - create clone of skb, and take reference to socket
3547 * @skb: the skb to clone
3548 *
3549 * This function creates a clone of a buffer that holds a reference on
3550 * sk_refcnt. Buffers created via this function are meant to be
3551 * returned using sock_queue_err_skb, or free via kfree_skb.
3552 *
3553 * When passing buffers allocated with this function to sock_queue_err_skb
3554 * it is necessary to wrap the call with sock_hold/sock_put in order to
3555 * prevent the socket from being released prior to being enqueued on
3556 * the sk_error_queue.
3557 */
Alexander Duyck62bccb82014-09-04 13:31:35 -04003558struct sk_buff *skb_clone_sk(struct sk_buff *skb)
3559{
3560 struct sock *sk = skb->sk;
3561 struct sk_buff *clone;
3562
3563 if (!sk || !atomic_inc_not_zero(&sk->sk_refcnt))
3564 return NULL;
3565
3566 clone = skb_clone(skb, GFP_ATOMIC);
3567 if (!clone) {
3568 sock_put(sk);
3569 return NULL;
3570 }
3571
3572 clone->sk = sk;
3573 clone->destructor = sock_efree;
3574
3575 return clone;
3576}
3577EXPORT_SYMBOL(skb_clone_sk);
3578
Alexander Duyck37846ef2014-09-04 13:31:10 -04003579static void __skb_complete_tx_timestamp(struct sk_buff *skb,
3580 struct sock *sk,
3581 int tstype)
Patrick Ohlyac45f602009-02-12 05:03:37 +00003582{
Patrick Ohlyac45f602009-02-12 05:03:37 +00003583 struct sock_exterr_skb *serr;
Patrick Ohlyac45f602009-02-12 05:03:37 +00003584 int err;
3585
Patrick Ohlyac45f602009-02-12 05:03:37 +00003586 serr = SKB_EXT_ERR(skb);
3587 memset(serr, 0, sizeof(*serr));
3588 serr->ee.ee_errno = ENOMSG;
3589 serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
Willem de Bruijne7fd2882014-08-04 22:11:48 -04003590 serr->ee.ee_info = tstype;
Willem de Bruijn4ed2d762014-08-04 22:11:49 -04003591 if (sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) {
Willem de Bruijn09c2d252014-08-04 22:11:47 -04003592 serr->ee.ee_data = skb_shinfo(skb)->tskey;
Willem de Bruijn4ed2d762014-08-04 22:11:49 -04003593 if (sk->sk_protocol == IPPROTO_TCP)
3594 serr->ee.ee_data -= sk->sk_tskey;
3595 }
Eric Dumazet29030372010-05-29 00:20:48 -07003596
Patrick Ohlyac45f602009-02-12 05:03:37 +00003597 err = sock_queue_err_skb(sk, skb);
Eric Dumazet29030372010-05-29 00:20:48 -07003598
Patrick Ohlyac45f602009-02-12 05:03:37 +00003599 if (err)
3600 kfree_skb(skb);
3601}
Alexander Duyck37846ef2014-09-04 13:31:10 -04003602
3603void skb_complete_tx_timestamp(struct sk_buff *skb,
3604 struct skb_shared_hwtstamps *hwtstamps)
3605{
3606 struct sock *sk = skb->sk;
3607
Alexander Duyck62bccb82014-09-04 13:31:35 -04003608 /* take a reference to prevent skb_orphan() from freeing the socket */
3609 sock_hold(sk);
Alexander Duyck37846ef2014-09-04 13:31:10 -04003610
Alexander Duyck62bccb82014-09-04 13:31:35 -04003611 *skb_hwtstamps(skb) = *hwtstamps;
3612 __skb_complete_tx_timestamp(skb, sk, SCM_TSTAMP_SND);
Alexander Duyck37846ef2014-09-04 13:31:10 -04003613
3614 sock_put(sk);
3615}
3616EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp);
3617
3618void __skb_tstamp_tx(struct sk_buff *orig_skb,
3619 struct skb_shared_hwtstamps *hwtstamps,
3620 struct sock *sk, int tstype)
3621{
3622 struct sk_buff *skb;
3623
3624 if (!sk)
3625 return;
3626
3627 if (hwtstamps)
3628 *skb_hwtstamps(orig_skb) = *hwtstamps;
3629 else
3630 orig_skb->tstamp = ktime_get_real();
3631
3632 skb = skb_clone(orig_skb, GFP_ATOMIC);
3633 if (!skb)
3634 return;
3635
3636 __skb_complete_tx_timestamp(skb, sk, tstype);
3637}
Willem de Bruijne7fd2882014-08-04 22:11:48 -04003638EXPORT_SYMBOL_GPL(__skb_tstamp_tx);
3639
3640void skb_tstamp_tx(struct sk_buff *orig_skb,
3641 struct skb_shared_hwtstamps *hwtstamps)
3642{
3643 return __skb_tstamp_tx(orig_skb, hwtstamps, orig_skb->sk,
3644 SCM_TSTAMP_SND);
3645}
Patrick Ohlyac45f602009-02-12 05:03:37 +00003646EXPORT_SYMBOL_GPL(skb_tstamp_tx);
3647
Johannes Berg6e3e9392011-11-09 10:15:42 +01003648void skb_complete_wifi_ack(struct sk_buff *skb, bool acked)
3649{
3650 struct sock *sk = skb->sk;
3651 struct sock_exterr_skb *serr;
3652 int err;
3653
3654 skb->wifi_acked_valid = 1;
3655 skb->wifi_acked = acked;
3656
3657 serr = SKB_EXT_ERR(skb);
3658 memset(serr, 0, sizeof(*serr));
3659 serr->ee.ee_errno = ENOMSG;
3660 serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS;
3661
Alexander Duyckbf7fa552014-09-10 18:05:42 -04003662 /* take a reference to prevent skb_orphan() from freeing the socket */
3663 sock_hold(sk);
3664
Johannes Berg6e3e9392011-11-09 10:15:42 +01003665 err = sock_queue_err_skb(sk, skb);
3666 if (err)
3667 kfree_skb(skb);
Alexander Duyckbf7fa552014-09-10 18:05:42 -04003668
3669 sock_put(sk);
Johannes Berg6e3e9392011-11-09 10:15:42 +01003670}
3671EXPORT_SYMBOL_GPL(skb_complete_wifi_ack);
3672
Patrick Ohlyac45f602009-02-12 05:03:37 +00003673
Rusty Russellf35d9d82008-02-04 23:49:54 -05003674/**
3675 * skb_partial_csum_set - set up and verify partial csum values for packet
3676 * @skb: the skb to set
3677 * @start: the number of bytes after skb->data to start checksumming.
3678 * @off: the offset from start to place the checksum.
3679 *
3680 * For untrusted partially-checksummed packets, we need to make sure the values
3681 * for skb->csum_start and skb->csum_offset are valid so we don't oops.
3682 *
3683 * This function checks and sets those values and skb->ip_summed: if this
3684 * returns false you should drop the packet.
3685 */
3686bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
3687{
Herbert Xu5ff8dda2009-06-04 01:22:01 +00003688 if (unlikely(start > skb_headlen(skb)) ||
3689 unlikely((int)start + off > skb_headlen(skb) - 2)) {
Joe Perchese87cc472012-05-13 21:56:26 +00003690 net_warn_ratelimited("bad partial csum: csum=%u/%u len=%u\n",
3691 start, off, skb_headlen(skb));
Rusty Russellf35d9d82008-02-04 23:49:54 -05003692 return false;
3693 }
3694 skb->ip_summed = CHECKSUM_PARTIAL;
3695 skb->csum_start = skb_headroom(skb) + start;
3696 skb->csum_offset = off;
Jason Wange5d5dec2013-03-26 23:11:20 +00003697 skb_set_transport_header(skb, start);
Rusty Russellf35d9d82008-02-04 23:49:54 -05003698 return true;
3699}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003700EXPORT_SYMBOL_GPL(skb_partial_csum_set);
Rusty Russellf35d9d82008-02-04 23:49:54 -05003701
Paul Durranted1f50c2014-01-09 10:02:46 +00003702static int skb_maybe_pull_tail(struct sk_buff *skb, unsigned int len,
3703 unsigned int max)
3704{
3705 if (skb_headlen(skb) >= len)
3706 return 0;
3707
3708 /* If we need to pullup then pullup to the max, so we
3709 * won't need to do it again.
3710 */
3711 if (max > skb->len)
3712 max = skb->len;
3713
3714 if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL)
3715 return -ENOMEM;
3716
3717 if (skb_headlen(skb) < len)
3718 return -EPROTO;
3719
3720 return 0;
3721}
3722
Jan Beulichf9708b42014-03-11 13:56:05 +00003723#define MAX_TCP_HDR_LEN (15 * 4)
3724
3725static __sum16 *skb_checksum_setup_ip(struct sk_buff *skb,
3726 typeof(IPPROTO_IP) proto,
3727 unsigned int off)
3728{
3729 switch (proto) {
3730 int err;
3731
3732 case IPPROTO_TCP:
3733 err = skb_maybe_pull_tail(skb, off + sizeof(struct tcphdr),
3734 off + MAX_TCP_HDR_LEN);
3735 if (!err && !skb_partial_csum_set(skb, off,
3736 offsetof(struct tcphdr,
3737 check)))
3738 err = -EPROTO;
3739 return err ? ERR_PTR(err) : &tcp_hdr(skb)->check;
3740
3741 case IPPROTO_UDP:
3742 err = skb_maybe_pull_tail(skb, off + sizeof(struct udphdr),
3743 off + sizeof(struct udphdr));
3744 if (!err && !skb_partial_csum_set(skb, off,
3745 offsetof(struct udphdr,
3746 check)))
3747 err = -EPROTO;
3748 return err ? ERR_PTR(err) : &udp_hdr(skb)->check;
3749 }
3750
3751 return ERR_PTR(-EPROTO);
3752}
3753
Paul Durranted1f50c2014-01-09 10:02:46 +00003754/* This value should be large enough to cover a tagged ethernet header plus
3755 * maximally sized IP and TCP or UDP headers.
3756 */
3757#define MAX_IP_HDR_LEN 128
3758
Jan Beulichf9708b42014-03-11 13:56:05 +00003759static int skb_checksum_setup_ipv4(struct sk_buff *skb, bool recalculate)
Paul Durranted1f50c2014-01-09 10:02:46 +00003760{
3761 unsigned int off;
3762 bool fragment;
Jan Beulichf9708b42014-03-11 13:56:05 +00003763 __sum16 *csum;
Paul Durranted1f50c2014-01-09 10:02:46 +00003764 int err;
3765
3766 fragment = false;
3767
3768 err = skb_maybe_pull_tail(skb,
3769 sizeof(struct iphdr),
3770 MAX_IP_HDR_LEN);
3771 if (err < 0)
3772 goto out;
3773
3774 if (ip_hdr(skb)->frag_off & htons(IP_OFFSET | IP_MF))
3775 fragment = true;
3776
3777 off = ip_hdrlen(skb);
3778
3779 err = -EPROTO;
3780
3781 if (fragment)
3782 goto out;
3783
Jan Beulichf9708b42014-03-11 13:56:05 +00003784 csum = skb_checksum_setup_ip(skb, ip_hdr(skb)->protocol, off);
3785 if (IS_ERR(csum))
3786 return PTR_ERR(csum);
Paul Durranted1f50c2014-01-09 10:02:46 +00003787
Jan Beulichf9708b42014-03-11 13:56:05 +00003788 if (recalculate)
3789 *csum = ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
3790 ip_hdr(skb)->daddr,
3791 skb->len - off,
3792 ip_hdr(skb)->protocol, 0);
Paul Durranted1f50c2014-01-09 10:02:46 +00003793 err = 0;
3794
3795out:
3796 return err;
3797}
3798
3799/* This value should be large enough to cover a tagged ethernet header plus
3800 * an IPv6 header, all options, and a maximal TCP or UDP header.
3801 */
3802#define MAX_IPV6_HDR_LEN 256
3803
3804#define OPT_HDR(type, skb, off) \
3805 (type *)(skb_network_header(skb) + (off))
3806
3807static int skb_checksum_setup_ipv6(struct sk_buff *skb, bool recalculate)
3808{
3809 int err;
3810 u8 nexthdr;
3811 unsigned int off;
3812 unsigned int len;
3813 bool fragment;
3814 bool done;
Jan Beulichf9708b42014-03-11 13:56:05 +00003815 __sum16 *csum;
Paul Durranted1f50c2014-01-09 10:02:46 +00003816
3817 fragment = false;
3818 done = false;
3819
3820 off = sizeof(struct ipv6hdr);
3821
3822 err = skb_maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN);
3823 if (err < 0)
3824 goto out;
3825
3826 nexthdr = ipv6_hdr(skb)->nexthdr;
3827
3828 len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len);
3829 while (off <= len && !done) {
3830 switch (nexthdr) {
3831 case IPPROTO_DSTOPTS:
3832 case IPPROTO_HOPOPTS:
3833 case IPPROTO_ROUTING: {
3834 struct ipv6_opt_hdr *hp;
3835
3836 err = skb_maybe_pull_tail(skb,
3837 off +
3838 sizeof(struct ipv6_opt_hdr),
3839 MAX_IPV6_HDR_LEN);
3840 if (err < 0)
3841 goto out;
3842
3843 hp = OPT_HDR(struct ipv6_opt_hdr, skb, off);
3844 nexthdr = hp->nexthdr;
3845 off += ipv6_optlen(hp);
3846 break;
3847 }
3848 case IPPROTO_AH: {
3849 struct ip_auth_hdr *hp;
3850
3851 err = skb_maybe_pull_tail(skb,
3852 off +
3853 sizeof(struct ip_auth_hdr),
3854 MAX_IPV6_HDR_LEN);
3855 if (err < 0)
3856 goto out;
3857
3858 hp = OPT_HDR(struct ip_auth_hdr, skb, off);
3859 nexthdr = hp->nexthdr;
3860 off += ipv6_authlen(hp);
3861 break;
3862 }
3863 case IPPROTO_FRAGMENT: {
3864 struct frag_hdr *hp;
3865
3866 err = skb_maybe_pull_tail(skb,
3867 off +
3868 sizeof(struct frag_hdr),
3869 MAX_IPV6_HDR_LEN);
3870 if (err < 0)
3871 goto out;
3872
3873 hp = OPT_HDR(struct frag_hdr, skb, off);
3874
3875 if (hp->frag_off & htons(IP6_OFFSET | IP6_MF))
3876 fragment = true;
3877
3878 nexthdr = hp->nexthdr;
3879 off += sizeof(struct frag_hdr);
3880 break;
3881 }
3882 default:
3883 done = true;
3884 break;
3885 }
3886 }
3887
3888 err = -EPROTO;
3889
3890 if (!done || fragment)
3891 goto out;
3892
Jan Beulichf9708b42014-03-11 13:56:05 +00003893 csum = skb_checksum_setup_ip(skb, nexthdr, off);
3894 if (IS_ERR(csum))
3895 return PTR_ERR(csum);
Paul Durranted1f50c2014-01-09 10:02:46 +00003896
Jan Beulichf9708b42014-03-11 13:56:05 +00003897 if (recalculate)
3898 *csum = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
3899 &ipv6_hdr(skb)->daddr,
3900 skb->len - off, nexthdr, 0);
Paul Durranted1f50c2014-01-09 10:02:46 +00003901 err = 0;
3902
3903out:
3904 return err;
3905}
3906
3907/**
3908 * skb_checksum_setup - set up partial checksum offset
3909 * @skb: the skb to set up
3910 * @recalculate: if true the pseudo-header checksum will be recalculated
3911 */
3912int skb_checksum_setup(struct sk_buff *skb, bool recalculate)
3913{
3914 int err;
3915
3916 switch (skb->protocol) {
3917 case htons(ETH_P_IP):
Jan Beulichf9708b42014-03-11 13:56:05 +00003918 err = skb_checksum_setup_ipv4(skb, recalculate);
Paul Durranted1f50c2014-01-09 10:02:46 +00003919 break;
3920
3921 case htons(ETH_P_IPV6):
3922 err = skb_checksum_setup_ipv6(skb, recalculate);
3923 break;
3924
3925 default:
3926 err = -EPROTO;
3927 break;
3928 }
3929
3930 return err;
3931}
3932EXPORT_SYMBOL(skb_checksum_setup);
3933
Ben Hutchings4497b072008-06-19 16:22:28 -07003934void __skb_warn_lro_forwarding(const struct sk_buff *skb)
3935{
Joe Perchese87cc472012-05-13 21:56:26 +00003936 net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n",
3937 skb->dev->name);
Ben Hutchings4497b072008-06-19 16:22:28 -07003938}
Ben Hutchings4497b072008-06-19 16:22:28 -07003939EXPORT_SYMBOL(__skb_warn_lro_forwarding);
Eric Dumazetbad43ca2012-05-19 03:02:02 +00003940
3941void kfree_skb_partial(struct sk_buff *skb, bool head_stolen)
3942{
Eric Dumazet3d861f62012-10-22 09:03:40 +00003943 if (head_stolen) {
3944 skb_release_head_state(skb);
Eric Dumazetbad43ca2012-05-19 03:02:02 +00003945 kmem_cache_free(skbuff_head_cache, skb);
Eric Dumazet3d861f62012-10-22 09:03:40 +00003946 } else {
Eric Dumazetbad43ca2012-05-19 03:02:02 +00003947 __kfree_skb(skb);
Eric Dumazet3d861f62012-10-22 09:03:40 +00003948 }
Eric Dumazetbad43ca2012-05-19 03:02:02 +00003949}
3950EXPORT_SYMBOL(kfree_skb_partial);
3951
3952/**
3953 * skb_try_coalesce - try to merge skb to prior one
3954 * @to: prior buffer
3955 * @from: buffer to add
3956 * @fragstolen: pointer to boolean
Randy Dunlapc6c4b972012-06-08 14:01:44 +00003957 * @delta_truesize: how much more was allocated than was requested
Eric Dumazetbad43ca2012-05-19 03:02:02 +00003958 */
3959bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
3960 bool *fragstolen, int *delta_truesize)
3961{
3962 int i, delta, len = from->len;
3963
3964 *fragstolen = false;
3965
3966 if (skb_cloned(to))
3967 return false;
3968
3969 if (len <= skb_tailroom(to)) {
Eric Dumazete93a0432014-09-15 04:19:52 -07003970 if (len)
3971 BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len));
Eric Dumazetbad43ca2012-05-19 03:02:02 +00003972 *delta_truesize = 0;
3973 return true;
3974 }
3975
3976 if (skb_has_frag_list(to) || skb_has_frag_list(from))
3977 return false;
3978
3979 if (skb_headlen(from) != 0) {
3980 struct page *page;
3981 unsigned int offset;
3982
3983 if (skb_shinfo(to)->nr_frags +
3984 skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
3985 return false;
3986
3987 if (skb_head_is_locked(from))
3988 return false;
3989
3990 delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
3991
3992 page = virt_to_head_page(from->head);
3993 offset = from->data - (unsigned char *)page_address(page);
3994
3995 skb_fill_page_desc(to, skb_shinfo(to)->nr_frags,
3996 page, offset, skb_headlen(from));
3997 *fragstolen = true;
3998 } else {
3999 if (skb_shinfo(to)->nr_frags +
4000 skb_shinfo(from)->nr_frags > MAX_SKB_FRAGS)
4001 return false;
4002
Weiping Panf4b549a2012-09-28 20:15:30 +00004003 delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from));
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004004 }
4005
4006 WARN_ON_ONCE(delta < len);
4007
4008 memcpy(skb_shinfo(to)->frags + skb_shinfo(to)->nr_frags,
4009 skb_shinfo(from)->frags,
4010 skb_shinfo(from)->nr_frags * sizeof(skb_frag_t));
4011 skb_shinfo(to)->nr_frags += skb_shinfo(from)->nr_frags;
4012
4013 if (!skb_cloned(from))
4014 skb_shinfo(from)->nr_frags = 0;
4015
Li RongQing8ea853f2012-09-18 16:53:21 +00004016 /* if the skb is not cloned this does nothing
4017 * since we set nr_frags to 0.
4018 */
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004019 for (i = 0; i < skb_shinfo(from)->nr_frags; i++)
4020 skb_frag_ref(from, i);
4021
4022 to->truesize += delta;
4023 to->len += len;
4024 to->data_len += len;
4025
4026 *delta_truesize = delta;
4027 return true;
4028}
4029EXPORT_SYMBOL(skb_try_coalesce);
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004030
4031/**
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004032 * skb_scrub_packet - scrub an skb
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004033 *
4034 * @skb: buffer to clean
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004035 * @xnet: packet is crossing netns
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004036 *
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004037 * skb_scrub_packet can be used after encapsulating or decapsulting a packet
4038 * into/from a tunnel. Some information have to be cleared during these
4039 * operations.
4040 * skb_scrub_packet can also be used to clean a skb before injecting it in
4041 * another namespace (@xnet == true). We have to clear all information in the
4042 * skb that could impact namespace isolation.
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004043 */
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004044void skb_scrub_packet(struct sk_buff *skb, bool xnet)
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004045{
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004046 if (xnet)
4047 skb_orphan(skb);
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004048 skb->tstamp.tv64 = 0;
4049 skb->pkt_type = PACKET_HOST;
4050 skb->skb_iif = 0;
WANG Cong60ff7462014-05-04 16:39:18 -07004051 skb->ignore_df = 0;
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004052 skb_dst_drop(skb);
4053 skb->mark = 0;
4054 secpath_reset(skb);
4055 nf_reset(skb);
4056 nf_reset_trace(skb);
4057}
4058EXPORT_SYMBOL_GPL(skb_scrub_packet);
Florian Westphalde960aa2014-01-26 10:58:16 +01004059
4060/**
4061 * skb_gso_transport_seglen - Return length of individual segments of a gso packet
4062 *
4063 * @skb: GSO skb
4064 *
4065 * skb_gso_transport_seglen is used to determine the real size of the
4066 * individual segments, including Layer4 headers (TCP/UDP).
4067 *
4068 * The MAC/L2 or network (IP, IPv6) headers are not accounted for.
4069 */
4070unsigned int skb_gso_transport_seglen(const struct sk_buff *skb)
4071{
4072 const struct skb_shared_info *shinfo = skb_shinfo(skb);
Florian Westphalf993bc22014-10-20 13:49:18 +02004073 unsigned int thlen = 0;
Florian Westphalde960aa2014-01-26 10:58:16 +01004074
Florian Westphalf993bc22014-10-20 13:49:18 +02004075 if (skb->encapsulation) {
4076 thlen = skb_inner_transport_header(skb) -
4077 skb_transport_header(skb);
Florian Westphal6d39d582014-04-09 10:28:50 +02004078
Florian Westphalf993bc22014-10-20 13:49:18 +02004079 if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)))
4080 thlen += inner_tcp_hdrlen(skb);
4081 } else if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) {
4082 thlen = tcp_hdrlen(skb);
4083 }
Florian Westphal6d39d582014-04-09 10:28:50 +02004084 /* UFO sets gso_size to the size of the fragmentation
4085 * payload, i.e. the size of the L4 (UDP) header is already
4086 * accounted for.
4087 */
Florian Westphalf993bc22014-10-20 13:49:18 +02004088 return thlen + shinfo->gso_size;
Florian Westphalde960aa2014-01-26 10:58:16 +01004089}
4090EXPORT_SYMBOL_GPL(skb_gso_transport_seglen);
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04004091
4092static struct sk_buff *skb_reorder_vlan_header(struct sk_buff *skb)
4093{
4094 if (skb_cow(skb, skb_headroom(skb)) < 0) {
4095 kfree_skb(skb);
4096 return NULL;
4097 }
4098
4099 memmove(skb->data - ETH_HLEN, skb->data - VLAN_ETH_HLEN, 2 * ETH_ALEN);
4100 skb->mac_header += VLAN_HLEN;
4101 return skb;
4102}
4103
4104struct sk_buff *skb_vlan_untag(struct sk_buff *skb)
4105{
4106 struct vlan_hdr *vhdr;
4107 u16 vlan_tci;
4108
4109 if (unlikely(vlan_tx_tag_present(skb))) {
4110 /* vlan_tci is already set-up so leave this for another time */
4111 return skb;
4112 }
4113
4114 skb = skb_share_check(skb, GFP_ATOMIC);
4115 if (unlikely(!skb))
4116 goto err_free;
4117
4118 if (unlikely(!pskb_may_pull(skb, VLAN_HLEN)))
4119 goto err_free;
4120
4121 vhdr = (struct vlan_hdr *)skb->data;
4122 vlan_tci = ntohs(vhdr->h_vlan_TCI);
4123 __vlan_hwaccel_put_tag(skb, skb->protocol, vlan_tci);
4124
4125 skb_pull_rcsum(skb, VLAN_HLEN);
4126 vlan_set_encap_proto(skb, vhdr);
4127
4128 skb = skb_reorder_vlan_header(skb);
4129 if (unlikely(!skb))
4130 goto err_free;
4131
4132 skb_reset_network_header(skb);
4133 skb_reset_transport_header(skb);
4134 skb_reset_mac_len(skb);
4135
4136 return skb;
4137
4138err_free:
4139 kfree_skb(skb);
4140 return NULL;
4141}
4142EXPORT_SYMBOL(skb_vlan_untag);
Eric Dumazet2e4e4412014-09-17 04:49:49 -07004143
Jiri Pirkoe2195122014-11-19 14:05:01 +01004144int skb_ensure_writable(struct sk_buff *skb, int write_len)
4145{
4146 if (!pskb_may_pull(skb, write_len))
4147 return -ENOMEM;
4148
4149 if (!skb_cloned(skb) || skb_clone_writable(skb, write_len))
4150 return 0;
4151
4152 return pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
4153}
4154EXPORT_SYMBOL(skb_ensure_writable);
4155
Jiri Pirko93515d52014-11-19 14:05:02 +01004156/* remove VLAN header from packet and update csum accordingly. */
4157static int __skb_vlan_pop(struct sk_buff *skb, u16 *vlan_tci)
4158{
4159 struct vlan_hdr *vhdr;
4160 unsigned int offset = skb->data - skb_mac_header(skb);
4161 int err;
4162
4163 __skb_push(skb, offset);
4164 err = skb_ensure_writable(skb, VLAN_ETH_HLEN);
4165 if (unlikely(err))
4166 goto pull;
4167
4168 skb_postpull_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
4169
4170 vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN);
4171 *vlan_tci = ntohs(vhdr->h_vlan_TCI);
4172
4173 memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN);
4174 __skb_pull(skb, VLAN_HLEN);
4175
4176 vlan_set_encap_proto(skb, vhdr);
4177 skb->mac_header += VLAN_HLEN;
4178
4179 if (skb_network_offset(skb) < ETH_HLEN)
4180 skb_set_network_header(skb, ETH_HLEN);
4181
4182 skb_reset_mac_len(skb);
4183pull:
4184 __skb_pull(skb, offset);
4185
4186 return err;
4187}
4188
4189int skb_vlan_pop(struct sk_buff *skb)
4190{
4191 u16 vlan_tci;
4192 __be16 vlan_proto;
4193 int err;
4194
4195 if (likely(vlan_tx_tag_present(skb))) {
4196 skb->vlan_tci = 0;
4197 } else {
4198 if (unlikely((skb->protocol != htons(ETH_P_8021Q) &&
4199 skb->protocol != htons(ETH_P_8021AD)) ||
4200 skb->len < VLAN_ETH_HLEN))
4201 return 0;
4202
4203 err = __skb_vlan_pop(skb, &vlan_tci);
4204 if (err)
4205 return err;
4206 }
4207 /* move next vlan tag to hw accel tag */
4208 if (likely((skb->protocol != htons(ETH_P_8021Q) &&
4209 skb->protocol != htons(ETH_P_8021AD)) ||
4210 skb->len < VLAN_ETH_HLEN))
4211 return 0;
4212
4213 vlan_proto = skb->protocol;
4214 err = __skb_vlan_pop(skb, &vlan_tci);
4215 if (unlikely(err))
4216 return err;
4217
4218 __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
4219 return 0;
4220}
4221EXPORT_SYMBOL(skb_vlan_pop);
4222
4223int skb_vlan_push(struct sk_buff *skb, __be16 vlan_proto, u16 vlan_tci)
4224{
4225 if (vlan_tx_tag_present(skb)) {
4226 unsigned int offset = skb->data - skb_mac_header(skb);
4227 int err;
4228
4229 /* __vlan_insert_tag expect skb->data pointing to mac header.
4230 * So change skb->data before calling it and change back to
4231 * original position later
4232 */
4233 __skb_push(skb, offset);
4234 err = __vlan_insert_tag(skb, skb->vlan_proto,
4235 vlan_tx_tag_get(skb));
4236 if (err)
4237 return err;
4238 skb->protocol = skb->vlan_proto;
4239 skb->mac_len += VLAN_HLEN;
4240 __skb_pull(skb, offset);
4241
4242 if (skb->ip_summed == CHECKSUM_COMPLETE)
4243 skb->csum = csum_add(skb->csum, csum_partial(skb->data
4244 + (2 * ETH_ALEN), VLAN_HLEN, 0));
4245 }
4246 __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
4247 return 0;
4248}
4249EXPORT_SYMBOL(skb_vlan_push);
4250
Eric Dumazet2e4e4412014-09-17 04:49:49 -07004251/**
4252 * alloc_skb_with_frags - allocate skb with page frags
4253 *
Masanari Iidade3f0d02014-10-09 12:58:08 +09004254 * @header_len: size of linear part
4255 * @data_len: needed length in frags
4256 * @max_page_order: max page order desired.
4257 * @errcode: pointer to error code if any
4258 * @gfp_mask: allocation mask
Eric Dumazet2e4e4412014-09-17 04:49:49 -07004259 *
4260 * This can be used to allocate a paged skb, given a maximal order for frags.
4261 */
4262struct sk_buff *alloc_skb_with_frags(unsigned long header_len,
4263 unsigned long data_len,
4264 int max_page_order,
4265 int *errcode,
4266 gfp_t gfp_mask)
4267{
4268 int npages = (data_len + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
4269 unsigned long chunk;
4270 struct sk_buff *skb;
4271 struct page *page;
4272 gfp_t gfp_head;
4273 int i;
4274
4275 *errcode = -EMSGSIZE;
4276 /* Note this test could be relaxed, if we succeed to allocate
4277 * high order pages...
4278 */
4279 if (npages > MAX_SKB_FRAGS)
4280 return NULL;
4281
4282 gfp_head = gfp_mask;
4283 if (gfp_head & __GFP_WAIT)
4284 gfp_head |= __GFP_REPEAT;
4285
4286 *errcode = -ENOBUFS;
4287 skb = alloc_skb(header_len, gfp_head);
4288 if (!skb)
4289 return NULL;
4290
4291 skb->truesize += npages << PAGE_SHIFT;
4292
4293 for (i = 0; npages > 0; i++) {
4294 int order = max_page_order;
4295
4296 while (order) {
4297 if (npages >= 1 << order) {
4298 page = alloc_pages(gfp_mask |
4299 __GFP_COMP |
4300 __GFP_NOWARN |
4301 __GFP_NORETRY,
4302 order);
4303 if (page)
4304 goto fill_page;
4305 /* Do not retry other high order allocations */
4306 order = 1;
4307 max_page_order = 0;
4308 }
4309 order--;
4310 }
4311 page = alloc_page(gfp_mask);
4312 if (!page)
4313 goto failure;
4314fill_page:
4315 chunk = min_t(unsigned long, data_len,
4316 PAGE_SIZE << order);
4317 skb_fill_page_desc(skb, i, page, 0, chunk);
4318 data_len -= chunk;
4319 npages -= 1 << order;
4320 }
4321 return skb;
4322
4323failure:
4324 kfree_skb(skb);
4325 return NULL;
4326}
4327EXPORT_SYMBOL(alloc_skb_with_frags);