blob: d11bbe0da355e6b3417dd77f3ddf04fd7faa4050 [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
Vijay Subramanianc8753d52014-10-02 10:00:43 -0700268 fclones->skb2.fclone = SKB_FCLONE_FREE;
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);
544 break;
545
546 case SKB_FCLONE_ORIG:
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700547 fclones = container_of(skb, struct sk_buff_fclones, skb1);
548 if (atomic_dec_and_test(&fclones->fclone_ref))
549 kmem_cache_free(skbuff_fclone_cache, fclones);
David S. Millerd179cd12005-08-17 14:57:30 -0700550 break;
551
552 case SKB_FCLONE_CLONE:
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700553 fclones = container_of(skb, struct sk_buff_fclones, skb2);
David S. Millerd179cd12005-08-17 14:57:30 -0700554
Eric Dumazetce1a4ea2014-10-01 15:27:15 -0700555 /* Warning : We must perform the atomic_dec_and_test() before
Vijay Subramanianc8753d52014-10-02 10:00:43 -0700556 * setting skb->fclone back to SKB_FCLONE_FREE, otherwise
Eric Dumazetce1a4ea2014-10-01 15:27:15 -0700557 * skb_clone() could set clone_ref to 2 before our decrement.
558 * Anyway, if we are going to free the structure, no need to
559 * rewrite skb->fclone.
David S. Millerd179cd12005-08-17 14:57:30 -0700560 */
Eric Dumazetce1a4ea2014-10-01 15:27:15 -0700561 if (atomic_dec_and_test(&fclones->fclone_ref)) {
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700562 kmem_cache_free(skbuff_fclone_cache, fclones);
Eric Dumazetce1a4ea2014-10-01 15:27:15 -0700563 } else {
564 /* The clone portion is available for
565 * fast-cloning again.
566 */
Vijay Subramanianc8753d52014-10-02 10:00:43 -0700567 skb->fclone = SKB_FCLONE_FREE;
Eric Dumazetce1a4ea2014-10-01 15:27:15 -0700568 }
David S. Millerd179cd12005-08-17 14:57:30 -0700569 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700570 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571}
572
Lennert Buytenhek04a4bb52008-10-01 02:33:12 -0700573static void skb_release_head_state(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574{
Eric Dumazetadf30902009-06-02 05:19:30 +0000575 skb_dst_drop(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576#ifdef CONFIG_XFRM
577 secpath_put(skb->sp);
578#endif
Stephen Hemminger9c2b3322005-04-19 22:39:42 -0700579 if (skb->destructor) {
580 WARN_ON(in_irq());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 skb->destructor(skb);
582 }
Igor Maravića3bf7ae2011-12-12 02:58:22 +0000583#if IS_ENABLED(CONFIG_NF_CONNTRACK)
Yasuyuki Kozakai5f79e0f2007-03-23 11:17:07 -0700584 nf_conntrack_put(skb->nfct);
KOVACS Krisztian2fc72c72011-01-12 20:25:08 +0100585#endif
Pablo Neira Ayuso1109a902014-10-01 11:19:17 +0200586#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 nf_bridge_put(skb->nf_bridge);
588#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589/* XXX: IS this still necessary? - JHS */
590#ifdef CONFIG_NET_SCHED
591 skb->tc_index = 0;
592#ifdef CONFIG_NET_CLS_ACT
593 skb->tc_verd = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594#endif
595#endif
Lennert Buytenhek04a4bb52008-10-01 02:33:12 -0700596}
597
598/* Free everything but the sk_buff shell. */
599static void skb_release_all(struct sk_buff *skb)
600{
601 skb_release_head_state(skb);
Pablo Neira5e71d9d2013-06-03 09:28:43 +0000602 if (likely(skb->head))
Patrick McHardy0ebd0ac2013-04-17 06:46:58 +0000603 skb_release_data(skb);
Herbert Xu2d4baff2007-11-26 23:11:19 +0800604}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
Herbert Xu2d4baff2007-11-26 23:11:19 +0800606/**
607 * __kfree_skb - private function
608 * @skb: buffer
609 *
610 * Free an sk_buff. Release anything attached to the buffer.
611 * Clean the state. This is an internal helper function. Users should
612 * always call kfree_skb
613 */
614
615void __kfree_skb(struct sk_buff *skb)
616{
617 skb_release_all(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 kfree_skbmem(skb);
619}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800620EXPORT_SYMBOL(__kfree_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
622/**
Jörn Engel231d06a2006-03-20 21:28:35 -0800623 * kfree_skb - free an sk_buff
624 * @skb: buffer to free
625 *
626 * Drop a reference to the buffer and free it if the usage count has
627 * hit zero.
628 */
629void kfree_skb(struct sk_buff *skb)
630{
631 if (unlikely(!skb))
632 return;
633 if (likely(atomic_read(&skb->users) == 1))
634 smp_rmb();
635 else if (likely(!atomic_dec_and_test(&skb->users)))
636 return;
Neil Hormanead2ceb2009-03-11 09:49:55 +0000637 trace_kfree_skb(skb, __builtin_return_address(0));
Jörn Engel231d06a2006-03-20 21:28:35 -0800638 __kfree_skb(skb);
639}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800640EXPORT_SYMBOL(kfree_skb);
Jörn Engel231d06a2006-03-20 21:28:35 -0800641
Eric Dumazetbd8a7032013-06-24 06:26:00 -0700642void kfree_skb_list(struct sk_buff *segs)
643{
644 while (segs) {
645 struct sk_buff *next = segs->next;
646
647 kfree_skb(segs);
648 segs = next;
649 }
650}
651EXPORT_SYMBOL(kfree_skb_list);
652
Stephen Hemmingerd1a203e2008-11-01 21:01:09 -0700653/**
Michael S. Tsirkin25121172012-11-01 09:16:28 +0000654 * skb_tx_error - report an sk_buff xmit error
655 * @skb: buffer that triggered an error
656 *
657 * Report xmit error if a device callback is tracking this skb.
658 * skb must be freed afterwards.
659 */
660void skb_tx_error(struct sk_buff *skb)
661{
662 if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) {
663 struct ubuf_info *uarg;
664
665 uarg = skb_shinfo(skb)->destructor_arg;
666 if (uarg->callback)
667 uarg->callback(uarg, false);
668 skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
669 }
670}
671EXPORT_SYMBOL(skb_tx_error);
672
673/**
Neil Hormanead2ceb2009-03-11 09:49:55 +0000674 * consume_skb - free an skbuff
675 * @skb: buffer to free
676 *
677 * Drop a ref to the buffer and free it if the usage count has hit zero
678 * Functions identically to kfree_skb, but kfree_skb assumes that the frame
679 * is being dropped after a failure and notes that
680 */
681void consume_skb(struct sk_buff *skb)
682{
683 if (unlikely(!skb))
684 return;
685 if (likely(atomic_read(&skb->users) == 1))
686 smp_rmb();
687 else if (likely(!atomic_dec_and_test(&skb->users)))
688 return;
Koki Sanagi07dc22e2010-08-23 18:46:12 +0900689 trace_consume_skb(skb);
Neil Hormanead2ceb2009-03-11 09:49:55 +0000690 __kfree_skb(skb);
691}
692EXPORT_SYMBOL(consume_skb);
693
Eric Dumazetb1937222014-09-28 22:18:47 -0700694/* Make sure a field is enclosed inside headers_start/headers_end section */
695#define CHECK_SKB_FIELD(field) \
696 BUILD_BUG_ON(offsetof(struct sk_buff, field) < \
697 offsetof(struct sk_buff, headers_start)); \
698 BUILD_BUG_ON(offsetof(struct sk_buff, field) > \
699 offsetof(struct sk_buff, headers_end)); \
700
Herbert Xudec18812007-10-14 00:37:30 -0700701static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
702{
703 new->tstamp = old->tstamp;
Eric Dumazetb1937222014-09-28 22:18:47 -0700704 /* We do not copy old->sk */
Herbert Xudec18812007-10-14 00:37:30 -0700705 new->dev = old->dev;
Eric Dumazetb1937222014-09-28 22:18:47 -0700706 memcpy(new->cb, old->cb, sizeof(old->cb));
Eric Dumazet7fee2262010-05-11 23:19:48 +0000707 skb_dst_copy(new, old);
Alexey Dobriyandef8b4f2008-10-28 13:24:06 -0700708#ifdef CONFIG_XFRM
Herbert Xudec18812007-10-14 00:37:30 -0700709 new->sp = secpath_get(old->sp);
710#endif
Eric Dumazetb1937222014-09-28 22:18:47 -0700711 __nf_copy(new, old, false);
Patrick McHardy6aa895b02008-07-14 22:49:06 -0700712
Eric Dumazetb1937222014-09-28 22:18:47 -0700713 /* Note : this field could be in headers_start/headers_end section
714 * It is not yet because we do not want to have a 16 bit hole
715 */
716 new->queue_mapping = old->queue_mapping;
Eliezer Tamir06021292013-06-10 11:39:50 +0300717
Eric Dumazetb1937222014-09-28 22:18:47 -0700718 memcpy(&new->headers_start, &old->headers_start,
719 offsetof(struct sk_buff, headers_end) -
720 offsetof(struct sk_buff, headers_start));
721 CHECK_SKB_FIELD(protocol);
722 CHECK_SKB_FIELD(csum);
723 CHECK_SKB_FIELD(hash);
724 CHECK_SKB_FIELD(priority);
725 CHECK_SKB_FIELD(skb_iif);
726 CHECK_SKB_FIELD(vlan_proto);
727 CHECK_SKB_FIELD(vlan_tci);
728 CHECK_SKB_FIELD(transport_header);
729 CHECK_SKB_FIELD(network_header);
730 CHECK_SKB_FIELD(mac_header);
731 CHECK_SKB_FIELD(inner_protocol);
732 CHECK_SKB_FIELD(inner_transport_header);
733 CHECK_SKB_FIELD(inner_network_header);
734 CHECK_SKB_FIELD(inner_mac_header);
735 CHECK_SKB_FIELD(mark);
736#ifdef CONFIG_NETWORK_SECMARK
737 CHECK_SKB_FIELD(secmark);
738#endif
Cong Wange0d10952013-08-01 11:10:25 +0800739#ifdef CONFIG_NET_RX_BUSY_POLL
Eric Dumazetb1937222014-09-28 22:18:47 -0700740 CHECK_SKB_FIELD(napi_id);
Eliezer Tamir06021292013-06-10 11:39:50 +0300741#endif
Eric Dumazetb1937222014-09-28 22:18:47 -0700742#ifdef CONFIG_NET_SCHED
743 CHECK_SKB_FIELD(tc_index);
744#ifdef CONFIG_NET_CLS_ACT
745 CHECK_SKB_FIELD(tc_verd);
746#endif
747#endif
748
Herbert Xudec18812007-10-14 00:37:30 -0700749}
750
Herbert Xu82c49a32009-05-22 22:11:37 +0000751/*
752 * You should not add any new code to this function. Add it to
753 * __copy_skb_header above instead.
754 */
Herbert Xue0053ec2007-10-14 00:37:52 -0700755static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757#define C(x) n->x = skb->x
758
759 n->next = n->prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 n->sk = NULL;
Herbert Xudec18812007-10-14 00:37:30 -0700761 __copy_skb_header(n, skb);
762
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 C(len);
764 C(data_len);
Alexey Dobriyan3e6b3b22007-03-16 15:00:46 -0700765 C(mac_len);
Patrick McHardy334a8132007-06-25 04:35:20 -0700766 n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
Paul Moore02f1c892008-01-07 21:56:41 -0800767 n->cloned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 n->nohdr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 n->destructor = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 C(tail);
771 C(end);
Paul Moore02f1c892008-01-07 21:56:41 -0800772 C(head);
Eric Dumazetd3836f22012-04-27 00:33:38 +0000773 C(head_frag);
Paul Moore02f1c892008-01-07 21:56:41 -0800774 C(data);
775 C(truesize);
776 atomic_set(&n->users, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
778 atomic_inc(&(skb_shinfo(skb)->dataref));
779 skb->cloned = 1;
780
781 return n;
Herbert Xue0053ec2007-10-14 00:37:52 -0700782#undef C
783}
784
785/**
786 * skb_morph - morph one skb into another
787 * @dst: the skb to receive the contents
788 * @src: the skb to supply the contents
789 *
790 * This is identical to skb_clone except that the target skb is
791 * supplied by the user.
792 *
793 * The target skb is returned upon exit.
794 */
795struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
796{
Herbert Xu2d4baff2007-11-26 23:11:19 +0800797 skb_release_all(dst);
Herbert Xue0053ec2007-10-14 00:37:52 -0700798 return __skb_clone(dst, src);
799}
800EXPORT_SYMBOL_GPL(skb_morph);
801
Ben Hutchings2c530402012-07-10 10:55:09 +0000802/**
803 * skb_copy_ubufs - copy userspace skb frags buffers to kernel
Michael S. Tsirkin48c83012011-08-31 08:03:29 +0000804 * @skb: the skb to modify
805 * @gfp_mask: allocation priority
806 *
807 * This must be called on SKBTX_DEV_ZEROCOPY skb.
808 * It will copy all frags into kernel and drop the reference
809 * to userspace pages.
810 *
811 * If this function is called from an interrupt gfp_mask() must be
812 * %GFP_ATOMIC.
813 *
814 * Returns 0 on success or a negative error code on failure
815 * to allocate kernel memory to copy to.
816 */
817int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask)
Shirley Maa6686f22011-07-06 12:22:12 +0000818{
819 int i;
820 int num_frags = skb_shinfo(skb)->nr_frags;
821 struct page *page, *head = NULL;
822 struct ubuf_info *uarg = skb_shinfo(skb)->destructor_arg;
823
824 for (i = 0; i < num_frags; i++) {
825 u8 *vaddr;
826 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
827
Krishna Kumar02756ed2012-07-17 02:05:29 +0000828 page = alloc_page(gfp_mask);
Shirley Maa6686f22011-07-06 12:22:12 +0000829 if (!page) {
830 while (head) {
Sunghan Suh40dadff2013-07-12 16:17:23 +0900831 struct page *next = (struct page *)page_private(head);
Shirley Maa6686f22011-07-06 12:22:12 +0000832 put_page(head);
833 head = next;
834 }
835 return -ENOMEM;
836 }
Eric Dumazet51c56b02012-04-05 11:35:15 +0200837 vaddr = kmap_atomic(skb_frag_page(f));
Shirley Maa6686f22011-07-06 12:22:12 +0000838 memcpy(page_address(page),
Eric Dumazet9e903e02011-10-18 21:00:24 +0000839 vaddr + f->page_offset, skb_frag_size(f));
Eric Dumazet51c56b02012-04-05 11:35:15 +0200840 kunmap_atomic(vaddr);
Sunghan Suh40dadff2013-07-12 16:17:23 +0900841 set_page_private(page, (unsigned long)head);
Shirley Maa6686f22011-07-06 12:22:12 +0000842 head = page;
843 }
844
845 /* skb frags release userspace buffers */
Krishna Kumar02756ed2012-07-17 02:05:29 +0000846 for (i = 0; i < num_frags; i++)
Ian Campbella8605c62011-10-19 23:01:49 +0000847 skb_frag_unref(skb, i);
Shirley Maa6686f22011-07-06 12:22:12 +0000848
Michael S. Tsirkine19d6762012-11-01 09:16:22 +0000849 uarg->callback(uarg, false);
Shirley Maa6686f22011-07-06 12:22:12 +0000850
851 /* skb frags point to kernel buffers */
Krishna Kumar02756ed2012-07-17 02:05:29 +0000852 for (i = num_frags - 1; i >= 0; i--) {
853 __skb_fill_page_desc(skb, i, head, 0,
854 skb_shinfo(skb)->frags[i].size);
Sunghan Suh40dadff2013-07-12 16:17:23 +0900855 head = (struct page *)page_private(head);
Shirley Maa6686f22011-07-06 12:22:12 +0000856 }
Michael S. Tsirkin48c83012011-08-31 08:03:29 +0000857
858 skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
Shirley Maa6686f22011-07-06 12:22:12 +0000859 return 0;
860}
Michael S. Tsirkindcc0fb72012-07-20 09:23:20 +0000861EXPORT_SYMBOL_GPL(skb_copy_ubufs);
Shirley Maa6686f22011-07-06 12:22:12 +0000862
Herbert Xue0053ec2007-10-14 00:37:52 -0700863/**
864 * skb_clone - duplicate an sk_buff
865 * @skb: buffer to clone
866 * @gfp_mask: allocation priority
867 *
868 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
869 * copies share the same packet data but not structure. The new
870 * buffer has a reference count of 1. If the allocation fails the
871 * function returns %NULL otherwise the new buffer is returned.
872 *
873 * If this function is called from an interrupt gfp_mask() must be
874 * %GFP_ATOMIC.
875 */
876
877struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
878{
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700879 struct sk_buff_fclones *fclones = container_of(skb,
880 struct sk_buff_fclones,
881 skb1);
882 struct sk_buff *n = &fclones->skb2;
Herbert Xue0053ec2007-10-14 00:37:52 -0700883
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +0000884 if (skb_orphan_frags(skb, gfp_mask))
885 return NULL;
Shirley Maa6686f22011-07-06 12:22:12 +0000886
Herbert Xue0053ec2007-10-14 00:37:52 -0700887 if (skb->fclone == SKB_FCLONE_ORIG &&
Vijay Subramanianc8753d52014-10-02 10:00:43 -0700888 n->fclone == SKB_FCLONE_FREE) {
Herbert Xue0053ec2007-10-14 00:37:52 -0700889 n->fclone = SKB_FCLONE_CLONE;
Eric Dumazetce1a4ea2014-10-01 15:27:15 -0700890 /* As our fastclone was free, clone_ref must be 1 at this point.
891 * We could use atomic_inc() here, but it is faster
892 * to set the final value.
893 */
894 atomic_set(&fclones->fclone_ref, 2);
Herbert Xue0053ec2007-10-14 00:37:52 -0700895 } else {
Mel Gormanc93bdd02012-07-31 16:44:19 -0700896 if (skb_pfmemalloc(skb))
897 gfp_mask |= __GFP_MEMALLOC;
898
Herbert Xue0053ec2007-10-14 00:37:52 -0700899 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
900 if (!n)
901 return NULL;
Vegard Nossumfe55f6d2008-08-30 12:16:35 +0200902
903 kmemcheck_annotate_bitfield(n, flags1);
Herbert Xue0053ec2007-10-14 00:37:52 -0700904 n->fclone = SKB_FCLONE_UNAVAILABLE;
905 }
906
907 return __skb_clone(n, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800909EXPORT_SYMBOL(skb_clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
Pravin B Shelarf5b17292013-03-07 13:21:40 +0000911static void skb_headers_offset_update(struct sk_buff *skb, int off)
912{
Eric Dumazet030737b2013-10-19 11:42:54 -0700913 /* Only adjust this if it actually is csum_start rather than csum */
914 if (skb->ip_summed == CHECKSUM_PARTIAL)
915 skb->csum_start += off;
Pravin B Shelarf5b17292013-03-07 13:21:40 +0000916 /* {transport,network,mac}_header and tail are relative to skb->head */
917 skb->transport_header += off;
918 skb->network_header += off;
919 if (skb_mac_header_was_set(skb))
920 skb->mac_header += off;
921 skb->inner_transport_header += off;
922 skb->inner_network_header += off;
Pravin B Shelaraefbd2b2013-03-07 13:21:46 +0000923 skb->inner_mac_header += off;
Pravin B Shelarf5b17292013-03-07 13:21:40 +0000924}
925
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
927{
Herbert Xudec18812007-10-14 00:37:30 -0700928 __copy_skb_header(new, old);
929
Herbert Xu79671682006-06-22 02:40:14 -0700930 skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
931 skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
932 skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933}
934
Mel Gormanc93bdd02012-07-31 16:44:19 -0700935static inline int skb_alloc_rx_flag(const struct sk_buff *skb)
936{
937 if (skb_pfmemalloc(skb))
938 return SKB_ALLOC_RX;
939 return 0;
940}
941
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942/**
943 * skb_copy - create private copy of an sk_buff
944 * @skb: buffer to copy
945 * @gfp_mask: allocation priority
946 *
947 * Make a copy of both an &sk_buff and its data. This is used when the
948 * caller wishes to modify the data and needs a private copy of the
949 * data to alter. Returns %NULL on failure or the pointer to the buffer
950 * on success. The returned buffer has a reference count of 1.
951 *
952 * As by-product this function converts non-linear &sk_buff to linear
953 * one, so that &sk_buff becomes completely private and caller is allowed
954 * to modify all the data of returned buffer. This means that this
955 * function is not recommended for use in circumstances when only
956 * header is going to be modified. Use pskb_copy() instead.
957 */
958
Al Virodd0fc662005-10-07 07:46:04 +0100959struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960{
Eric Dumazet6602ceb2010-09-01 05:25:10 +0000961 int headerlen = skb_headroom(skb);
Alexander Duyckec47ea82012-05-04 14:26:56 +0000962 unsigned int size = skb_end_offset(skb) + skb->data_len;
Mel Gormanc93bdd02012-07-31 16:44:19 -0700963 struct sk_buff *n = __alloc_skb(size, gfp_mask,
964 skb_alloc_rx_flag(skb), NUMA_NO_NODE);
Eric Dumazet6602ceb2010-09-01 05:25:10 +0000965
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 if (!n)
967 return NULL;
968
969 /* Set the data pointer */
970 skb_reserve(n, headerlen);
971 /* Set the tail pointer and length */
972 skb_put(n, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
974 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
975 BUG();
976
977 copy_skb_header(n, skb);
978 return n;
979}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800980EXPORT_SYMBOL(skb_copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
982/**
Octavian Purdilabad93e92014-06-12 01:36:26 +0300983 * __pskb_copy_fclone - create copy of an sk_buff with private head.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 * @skb: buffer to copy
Eric Dumazet117632e2011-12-03 21:39:53 +0000985 * @headroom: headroom of new skb
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 * @gfp_mask: allocation priority
Octavian Purdilabad93e92014-06-12 01:36:26 +0300987 * @fclone: if true allocate the copy of the skb from the fclone
988 * cache instead of the head cache; it is recommended to set this
989 * to true for the cases where the copy will likely be cloned
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 *
991 * Make a copy of both an &sk_buff and part of its data, located
992 * in header. Fragmented data remain shared. This is used when
993 * the caller wishes to modify only header of &sk_buff and needs
994 * private copy of the header to alter. Returns %NULL on failure
995 * or the pointer to the buffer on success.
996 * The returned buffer has a reference count of 1.
997 */
998
Octavian Purdilabad93e92014-06-12 01:36:26 +0300999struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom,
1000 gfp_t gfp_mask, bool fclone)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001{
Eric Dumazet117632e2011-12-03 21:39:53 +00001002 unsigned int size = skb_headlen(skb) + headroom;
Octavian Purdilabad93e92014-06-12 01:36:26 +03001003 int flags = skb_alloc_rx_flag(skb) | (fclone ? SKB_ALLOC_FCLONE : 0);
1004 struct sk_buff *n = __alloc_skb(size, gfp_mask, flags, NUMA_NO_NODE);
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001005
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 if (!n)
1007 goto out;
1008
1009 /* Set the data pointer */
Eric Dumazet117632e2011-12-03 21:39:53 +00001010 skb_reserve(n, headroom);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 /* Set the tail pointer and length */
1012 skb_put(n, skb_headlen(skb));
1013 /* Copy the bytes */
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001014 skb_copy_from_linear_data(skb, n->data, n->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
Herbert Xu25f484a2006-11-07 14:57:15 -08001016 n->truesize += skb->data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 n->data_len = skb->data_len;
1018 n->len = skb->len;
1019
1020 if (skb_shinfo(skb)->nr_frags) {
1021 int i;
1022
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +00001023 if (skb_orphan_frags(skb, gfp_mask)) {
1024 kfree_skb(n);
1025 n = NULL;
1026 goto out;
Shirley Maa6686f22011-07-06 12:22:12 +00001027 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1029 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
Ian Campbellea2ab692011-08-22 23:44:58 +00001030 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 }
1032 skb_shinfo(n)->nr_frags = i;
1033 }
1034
David S. Miller21dc3302010-08-23 00:13:46 -07001035 if (skb_has_frag_list(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
1037 skb_clone_fraglist(n);
1038 }
1039
1040 copy_skb_header(n, skb);
1041out:
1042 return n;
1043}
Octavian Purdilabad93e92014-06-12 01:36:26 +03001044EXPORT_SYMBOL(__pskb_copy_fclone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045
1046/**
1047 * pskb_expand_head - reallocate header of &sk_buff
1048 * @skb: buffer to reallocate
1049 * @nhead: room to add at head
1050 * @ntail: room to add at tail
1051 * @gfp_mask: allocation priority
1052 *
Mathias Krausebc323832013-11-07 14:18:26 +01001053 * Expands (or creates identical copy, if @nhead and @ntail are zero)
1054 * header of @skb. &sk_buff itself is not changed. &sk_buff MUST have
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 * reference count of 1. Returns zero in the case of success or error,
1056 * if expansion failed. In the last case, &sk_buff is not changed.
1057 *
1058 * All the pointers pointing into skb header may change and must be
1059 * reloaded after call to this function.
1060 */
1061
Victor Fusco86a76ca2005-07-08 14:57:47 -07001062int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
Al Virodd0fc662005-10-07 07:46:04 +01001063 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064{
1065 int i;
1066 u8 *data;
Alexander Duyckec47ea82012-05-04 14:26:56 +00001067 int size = nhead + skb_end_offset(skb) + ntail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 long off;
1069
Herbert Xu4edd87a2008-10-01 07:09:38 -07001070 BUG_ON(nhead < 0);
1071
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 if (skb_shared(skb))
1073 BUG();
1074
1075 size = SKB_DATA_ALIGN(size);
1076
Mel Gormanc93bdd02012-07-31 16:44:19 -07001077 if (skb_pfmemalloc(skb))
1078 gfp_mask |= __GFP_MEMALLOC;
1079 data = kmalloc_reserve(size + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
1080 gfp_mask, NUMA_NO_NODE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 if (!data)
1082 goto nodata;
Eric Dumazet87151b82012-04-10 20:08:39 +00001083 size = SKB_WITH_OVERHEAD(ksize(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
1085 /* Copy only real data... and, alas, header. This should be
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001086 * optimized for the cases when header is void.
1087 */
1088 memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head);
1089
1090 memcpy((struct skb_shared_info *)(data + size),
1091 skb_shinfo(skb),
Eric Dumazetfed66382010-07-22 19:09:08 +00001092 offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
Alexander Duyck3e245912012-05-04 14:26:51 +00001094 /*
1095 * if shinfo is shared we must drop the old head gracefully, but if it
1096 * is not we can just drop the old head and let the existing refcount
1097 * be since all we did is relocate the values
1098 */
1099 if (skb_cloned(skb)) {
Shirley Maa6686f22011-07-06 12:22:12 +00001100 /* copy this zero copy skb frags */
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +00001101 if (skb_orphan_frags(skb, gfp_mask))
1102 goto nofrags;
Eric Dumazet1fd63042010-09-02 23:09:32 +00001103 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
Ian Campbellea2ab692011-08-22 23:44:58 +00001104 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105
Eric Dumazet1fd63042010-09-02 23:09:32 +00001106 if (skb_has_frag_list(skb))
1107 skb_clone_fraglist(skb);
1108
1109 skb_release_data(skb);
Alexander Duyck3e245912012-05-04 14:26:51 +00001110 } else {
1111 skb_free_head(skb);
Eric Dumazet1fd63042010-09-02 23:09:32 +00001112 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 off = (data + nhead) - skb->head;
1114
1115 skb->head = data;
Eric Dumazetd3836f22012-04-27 00:33:38 +00001116 skb->head_frag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 skb->data += off;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001118#ifdef NET_SKBUFF_DATA_USES_OFFSET
1119 skb->end = size;
Patrick McHardy56eb8882007-04-09 11:45:04 -07001120 off = nhead;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001121#else
1122 skb->end = skb->head + size;
Patrick McHardy56eb8882007-04-09 11:45:04 -07001123#endif
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001124 skb->tail += off;
Peter Pan(潘卫平)b41abb42013-06-06 21:27:21 +08001125 skb_headers_offset_update(skb, nhead);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 skb->cloned = 0;
Patrick McHardy334a8132007-06-25 04:35:20 -07001127 skb->hdr_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 skb->nohdr = 0;
1129 atomic_set(&skb_shinfo(skb)->dataref, 1);
1130 return 0;
1131
Shirley Maa6686f22011-07-06 12:22:12 +00001132nofrags:
1133 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134nodata:
1135 return -ENOMEM;
1136}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001137EXPORT_SYMBOL(pskb_expand_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
1139/* Make private copy of skb with writable head and some headroom */
1140
1141struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
1142{
1143 struct sk_buff *skb2;
1144 int delta = headroom - skb_headroom(skb);
1145
1146 if (delta <= 0)
1147 skb2 = pskb_copy(skb, GFP_ATOMIC);
1148 else {
1149 skb2 = skb_clone(skb, GFP_ATOMIC);
1150 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
1151 GFP_ATOMIC)) {
1152 kfree_skb(skb2);
1153 skb2 = NULL;
1154 }
1155 }
1156 return skb2;
1157}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001158EXPORT_SYMBOL(skb_realloc_headroom);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
1160/**
1161 * skb_copy_expand - copy and expand sk_buff
1162 * @skb: buffer to copy
1163 * @newheadroom: new free bytes at head
1164 * @newtailroom: new free bytes at tail
1165 * @gfp_mask: allocation priority
1166 *
1167 * Make a copy of both an &sk_buff and its data and while doing so
1168 * allocate additional space.
1169 *
1170 * This is used when the caller wishes to modify the data and needs a
1171 * private copy of the data to alter as well as more space for new fields.
1172 * Returns %NULL on failure or the pointer to the buffer
1173 * on success. The returned buffer has a reference count of 1.
1174 *
1175 * You must pass %GFP_ATOMIC as the allocation priority if this function
1176 * is called from an interrupt.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 */
1178struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
Victor Fusco86a76ca2005-07-08 14:57:47 -07001179 int newheadroom, int newtailroom,
Al Virodd0fc662005-10-07 07:46:04 +01001180 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181{
1182 /*
1183 * Allocate the copy buffer
1184 */
Mel Gormanc93bdd02012-07-31 16:44:19 -07001185 struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom,
1186 gfp_mask, skb_alloc_rx_flag(skb),
1187 NUMA_NO_NODE);
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001188 int oldheadroom = skb_headroom(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 int head_copy_len, head_copy_off;
1190
1191 if (!n)
1192 return NULL;
1193
1194 skb_reserve(n, newheadroom);
1195
1196 /* Set the tail pointer and length */
1197 skb_put(n, skb->len);
1198
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001199 head_copy_len = oldheadroom;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 head_copy_off = 0;
1201 if (newheadroom <= head_copy_len)
1202 head_copy_len = newheadroom;
1203 else
1204 head_copy_off = newheadroom - head_copy_len;
1205
1206 /* Copy the linear header and data. */
1207 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
1208 skb->len + head_copy_len))
1209 BUG();
1210
1211 copy_skb_header(n, skb);
1212
Eric Dumazet030737b2013-10-19 11:42:54 -07001213 skb_headers_offset_update(n, newheadroom - oldheadroom);
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001214
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 return n;
1216}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001217EXPORT_SYMBOL(skb_copy_expand);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218
1219/**
1220 * skb_pad - zero pad the tail of an skb
1221 * @skb: buffer to pad
1222 * @pad: space to pad
1223 *
1224 * Ensure that a buffer is followed by a padding area that is zero
1225 * filled. Used by network drivers which may DMA or transfer data
1226 * beyond the buffer end onto the wire.
1227 *
Herbert Xu5b057c62006-06-23 02:06:41 -07001228 * May return error in out of memory cases. The skb is freed on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001230
Herbert Xu5b057c62006-06-23 02:06:41 -07001231int skb_pad(struct sk_buff *skb, int pad)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232{
Herbert Xu5b057c62006-06-23 02:06:41 -07001233 int err;
1234 int ntail;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001235
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 /* If the skbuff is non linear tailroom is always zero.. */
Herbert Xu5b057c62006-06-23 02:06:41 -07001237 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 memset(skb->data+skb->len, 0, pad);
Herbert Xu5b057c62006-06-23 02:06:41 -07001239 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 }
Herbert Xu5b057c62006-06-23 02:06:41 -07001241
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001242 ntail = skb->data_len + pad - (skb->end - skb->tail);
Herbert Xu5b057c62006-06-23 02:06:41 -07001243 if (likely(skb_cloned(skb) || ntail > 0)) {
1244 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
1245 if (unlikely(err))
1246 goto free_skb;
1247 }
1248
1249 /* FIXME: The use of this function with non-linear skb's really needs
1250 * to be audited.
1251 */
1252 err = skb_linearize(skb);
1253 if (unlikely(err))
1254 goto free_skb;
1255
1256 memset(skb->data + skb->len, 0, pad);
1257 return 0;
1258
1259free_skb:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 kfree_skb(skb);
Herbert Xu5b057c62006-06-23 02:06:41 -07001261 return err;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001262}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001263EXPORT_SYMBOL(skb_pad);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001264
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -07001265/**
Mathias Krause0c7ddf32013-11-07 14:18:24 +01001266 * pskb_put - add data to the tail of a potentially fragmented buffer
1267 * @skb: start of the buffer to use
1268 * @tail: tail fragment of the buffer to use
1269 * @len: amount of data to add
1270 *
1271 * This function extends the used data area of the potentially
1272 * fragmented buffer. @tail must be the last fragment of @skb -- or
1273 * @skb itself. If this would exceed the total buffer size the kernel
1274 * will panic. A pointer to the first byte of the extra data is
1275 * returned.
1276 */
1277
1278unsigned char *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len)
1279{
1280 if (tail != skb) {
1281 skb->data_len += len;
1282 skb->len += len;
1283 }
1284 return skb_put(tail, len);
1285}
1286EXPORT_SYMBOL_GPL(pskb_put);
1287
1288/**
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -07001289 * skb_put - add data to a buffer
1290 * @skb: buffer to use
1291 * @len: amount of data to add
1292 *
1293 * This function extends the used data area of the buffer. If this would
1294 * exceed the total buffer size the kernel will panic. A pointer to the
1295 * first byte of the extra data is returned.
1296 */
1297unsigned char *skb_put(struct sk_buff *skb, unsigned int len)
1298{
1299 unsigned char *tmp = skb_tail_pointer(skb);
1300 SKB_LINEAR_ASSERT(skb);
1301 skb->tail += len;
1302 skb->len += len;
1303 if (unlikely(skb->tail > skb->end))
1304 skb_over_panic(skb, len, __builtin_return_address(0));
1305 return tmp;
1306}
1307EXPORT_SYMBOL(skb_put);
1308
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001309/**
Ilpo Järvinenc2aa2702008-03-27 17:52:40 -07001310 * skb_push - add data to the start of a buffer
1311 * @skb: buffer to use
1312 * @len: amount of data to add
1313 *
1314 * This function extends the used data area of the buffer at the buffer
1315 * start. If this would exceed the total buffer headroom the kernel will
1316 * panic. A pointer to the first byte of the extra data is returned.
1317 */
1318unsigned char *skb_push(struct sk_buff *skb, unsigned int len)
1319{
1320 skb->data -= len;
1321 skb->len += len;
1322 if (unlikely(skb->data<skb->head))
1323 skb_under_panic(skb, len, __builtin_return_address(0));
1324 return skb->data;
1325}
1326EXPORT_SYMBOL(skb_push);
1327
1328/**
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001329 * skb_pull - remove data from the start of a buffer
1330 * @skb: buffer to use
1331 * @len: amount of data to remove
1332 *
1333 * This function removes data from the start of a buffer, returning
1334 * the memory to the headroom. A pointer to the next data in the buffer
1335 * is returned. Once the data has been pulled future pushes will overwrite
1336 * the old data.
1337 */
1338unsigned char *skb_pull(struct sk_buff *skb, unsigned int len)
1339{
David S. Miller47d29642010-05-02 02:21:44 -07001340 return skb_pull_inline(skb, len);
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001341}
1342EXPORT_SYMBOL(skb_pull);
1343
Ilpo Järvinen419ae742008-03-27 17:54:01 -07001344/**
1345 * skb_trim - remove end from a buffer
1346 * @skb: buffer to alter
1347 * @len: new length
1348 *
1349 * Cut the length of a buffer down by removing data from the tail. If
1350 * the buffer is already under the length specified it is not modified.
1351 * The skb must be linear.
1352 */
1353void skb_trim(struct sk_buff *skb, unsigned int len)
1354{
1355 if (skb->len > len)
1356 __skb_trim(skb, len);
1357}
1358EXPORT_SYMBOL(skb_trim);
1359
Herbert Xu3cc0e872006-06-09 16:13:38 -07001360/* Trims skb to length len. It can change skb pointers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 */
1362
Herbert Xu3cc0e872006-06-09 16:13:38 -07001363int ___pskb_trim(struct sk_buff *skb, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364{
Herbert Xu27b437c2006-07-13 19:26:39 -07001365 struct sk_buff **fragp;
1366 struct sk_buff *frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 int offset = skb_headlen(skb);
1368 int nfrags = skb_shinfo(skb)->nr_frags;
1369 int i;
Herbert Xu27b437c2006-07-13 19:26:39 -07001370 int err;
1371
1372 if (skb_cloned(skb) &&
1373 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
1374 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001376 i = 0;
1377 if (offset >= len)
1378 goto drop_pages;
1379
1380 for (; i < nfrags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001381 int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]);
Herbert Xu27b437c2006-07-13 19:26:39 -07001382
1383 if (end < len) {
1384 offset = end;
1385 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 }
Herbert Xu27b437c2006-07-13 19:26:39 -07001387
Eric Dumazet9e903e02011-10-18 21:00:24 +00001388 skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset);
Herbert Xu27b437c2006-07-13 19:26:39 -07001389
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001390drop_pages:
Herbert Xu27b437c2006-07-13 19:26:39 -07001391 skb_shinfo(skb)->nr_frags = i;
1392
1393 for (; i < nfrags; i++)
Ian Campbellea2ab692011-08-22 23:44:58 +00001394 skb_frag_unref(skb, i);
Herbert Xu27b437c2006-07-13 19:26:39 -07001395
David S. Miller21dc3302010-08-23 00:13:46 -07001396 if (skb_has_frag_list(skb))
Herbert Xu27b437c2006-07-13 19:26:39 -07001397 skb_drop_fraglist(skb);
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001398 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 }
1400
Herbert Xu27b437c2006-07-13 19:26:39 -07001401 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
1402 fragp = &frag->next) {
1403 int end = offset + frag->len;
1404
1405 if (skb_shared(frag)) {
1406 struct sk_buff *nfrag;
1407
1408 nfrag = skb_clone(frag, GFP_ATOMIC);
1409 if (unlikely(!nfrag))
1410 return -ENOMEM;
1411
1412 nfrag->next = frag->next;
Eric Dumazet85bb2a62012-04-19 02:24:53 +00001413 consume_skb(frag);
Herbert Xu27b437c2006-07-13 19:26:39 -07001414 frag = nfrag;
1415 *fragp = frag;
1416 }
1417
1418 if (end < len) {
1419 offset = end;
1420 continue;
1421 }
1422
1423 if (end > len &&
1424 unlikely((err = pskb_trim(frag, len - offset))))
1425 return err;
1426
1427 if (frag->next)
1428 skb_drop_list(&frag->next);
1429 break;
1430 }
1431
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001432done:
Herbert Xu27b437c2006-07-13 19:26:39 -07001433 if (len > skb_headlen(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 skb->data_len -= skb->len - len;
1435 skb->len = len;
1436 } else {
Herbert Xu27b437c2006-07-13 19:26:39 -07001437 skb->len = len;
1438 skb->data_len = 0;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001439 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 }
1441
1442 return 0;
1443}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001444EXPORT_SYMBOL(___pskb_trim);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
1446/**
1447 * __pskb_pull_tail - advance tail of skb header
1448 * @skb: buffer to reallocate
1449 * @delta: number of bytes to advance tail
1450 *
1451 * The function makes a sense only on a fragmented &sk_buff,
1452 * it expands header moving its tail forward and copying necessary
1453 * data from fragmented part.
1454 *
1455 * &sk_buff MUST have reference count of 1.
1456 *
1457 * Returns %NULL (and &sk_buff does not change) if pull failed
1458 * or value of new tail of skb in the case of success.
1459 *
1460 * All the pointers pointing into skb header may change and must be
1461 * reloaded after call to this function.
1462 */
1463
1464/* Moves tail of skb head forward, copying data from fragmented part,
1465 * when it is necessary.
1466 * 1. It may fail due to malloc failure.
1467 * 2. It may change skb pointers.
1468 *
1469 * It is pretty complicated. Luckily, it is called only in exceptional cases.
1470 */
1471unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
1472{
1473 /* If skb has not enough free space at tail, get new one
1474 * plus 128 bytes for future expansions. If we have enough
1475 * room at tail, reallocate without expansion only if skb is cloned.
1476 */
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001477 int i, k, eat = (skb->tail + delta) - skb->end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478
1479 if (eat > 0 || skb_cloned(skb)) {
1480 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
1481 GFP_ATOMIC))
1482 return NULL;
1483 }
1484
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001485 if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 BUG();
1487
1488 /* Optimization: no fragments, no reasons to preestimate
1489 * size of pulled pages. Superb.
1490 */
David S. Miller21dc3302010-08-23 00:13:46 -07001491 if (!skb_has_frag_list(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 goto pull_pages;
1493
1494 /* Estimate size of pulled pages. */
1495 eat = delta;
1496 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001497 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1498
1499 if (size >= eat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 goto pull_pages;
Eric Dumazet9e903e02011-10-18 21:00:24 +00001501 eat -= size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 }
1503
1504 /* If we need update frag list, we are in troubles.
1505 * Certainly, it possible to add an offset to skb data,
1506 * but taking into account that pulling is expected to
1507 * be very rare operation, it is worth to fight against
1508 * further bloating skb head and crucify ourselves here instead.
1509 * Pure masohism, indeed. 8)8)
1510 */
1511 if (eat) {
1512 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1513 struct sk_buff *clone = NULL;
1514 struct sk_buff *insp = NULL;
1515
1516 do {
Kris Katterjohn09a62662006-01-08 22:24:28 -08001517 BUG_ON(!list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518
1519 if (list->len <= eat) {
1520 /* Eaten as whole. */
1521 eat -= list->len;
1522 list = list->next;
1523 insp = list;
1524 } else {
1525 /* Eaten partially. */
1526
1527 if (skb_shared(list)) {
1528 /* Sucks! We need to fork list. :-( */
1529 clone = skb_clone(list, GFP_ATOMIC);
1530 if (!clone)
1531 return NULL;
1532 insp = list->next;
1533 list = clone;
1534 } else {
1535 /* This may be pulled without
1536 * problems. */
1537 insp = list;
1538 }
1539 if (!pskb_pull(list, eat)) {
Wei Yongjunf3fbbe02009-02-25 00:37:32 +00001540 kfree_skb(clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 return NULL;
1542 }
1543 break;
1544 }
1545 } while (eat);
1546
1547 /* Free pulled out fragments. */
1548 while ((list = skb_shinfo(skb)->frag_list) != insp) {
1549 skb_shinfo(skb)->frag_list = list->next;
1550 kfree_skb(list);
1551 }
1552 /* And insert new clone at head. */
1553 if (clone) {
1554 clone->next = list;
1555 skb_shinfo(skb)->frag_list = clone;
1556 }
1557 }
1558 /* Success! Now we may commit changes to skb data. */
1559
1560pull_pages:
1561 eat = delta;
1562 k = 0;
1563 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001564 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1565
1566 if (size <= eat) {
Ian Campbellea2ab692011-08-22 23:44:58 +00001567 skb_frag_unref(skb, i);
Eric Dumazet9e903e02011-10-18 21:00:24 +00001568 eat -= size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 } else {
1570 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
1571 if (eat) {
1572 skb_shinfo(skb)->frags[k].page_offset += eat;
Eric Dumazet9e903e02011-10-18 21:00:24 +00001573 skb_frag_size_sub(&skb_shinfo(skb)->frags[k], eat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 eat = 0;
1575 }
1576 k++;
1577 }
1578 }
1579 skb_shinfo(skb)->nr_frags = k;
1580
1581 skb->tail += delta;
1582 skb->data_len -= delta;
1583
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001584 return skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001586EXPORT_SYMBOL(__pskb_pull_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587
Eric Dumazet22019b12011-07-29 18:37:31 +00001588/**
1589 * skb_copy_bits - copy bits from skb to kernel buffer
1590 * @skb: source skb
1591 * @offset: offset in source
1592 * @to: destination buffer
1593 * @len: number of bytes to copy
1594 *
1595 * Copy the specified number of bytes from the source skb to the
1596 * destination buffer.
1597 *
1598 * CAUTION ! :
1599 * If its prototype is ever changed,
1600 * check arch/{*}/net/{*}.S files,
1601 * since it is called from BPF assembly code.
1602 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
1604{
David S. Miller1a028e52007-04-27 15:21:23 -07001605 int start = skb_headlen(skb);
David S. Millerfbb398a2009-06-09 00:18:59 -07001606 struct sk_buff *frag_iter;
1607 int i, copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608
1609 if (offset > (int)skb->len - len)
1610 goto fault;
1611
1612 /* Copy header. */
David S. Miller1a028e52007-04-27 15:21:23 -07001613 if ((copy = start - offset) > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 if (copy > len)
1615 copy = len;
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001616 skb_copy_from_linear_data_offset(skb, offset, to, copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 if ((len -= copy) == 0)
1618 return 0;
1619 offset += copy;
1620 to += copy;
1621 }
1622
1623 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07001624 int end;
Eric Dumazet51c56b02012-04-05 11:35:15 +02001625 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001627 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07001628
Eric Dumazet51c56b02012-04-05 11:35:15 +02001629 end = start + skb_frag_size(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 if ((copy = end - offset) > 0) {
1631 u8 *vaddr;
1632
1633 if (copy > len)
1634 copy = len;
1635
Eric Dumazet51c56b02012-04-05 11:35:15 +02001636 vaddr = kmap_atomic(skb_frag_page(f));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 memcpy(to,
Eric Dumazet51c56b02012-04-05 11:35:15 +02001638 vaddr + f->page_offset + offset - start,
1639 copy);
1640 kunmap_atomic(vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641
1642 if ((len -= copy) == 0)
1643 return 0;
1644 offset += copy;
1645 to += copy;
1646 }
David S. Miller1a028e52007-04-27 15:21:23 -07001647 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 }
1649
David S. Millerfbb398a2009-06-09 00:18:59 -07001650 skb_walk_frags(skb, frag_iter) {
1651 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652
David S. Millerfbb398a2009-06-09 00:18:59 -07001653 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654
David S. Millerfbb398a2009-06-09 00:18:59 -07001655 end = start + frag_iter->len;
1656 if ((copy = end - offset) > 0) {
1657 if (copy > len)
1658 copy = len;
1659 if (skb_copy_bits(frag_iter, offset - start, to, copy))
1660 goto fault;
1661 if ((len -= copy) == 0)
1662 return 0;
1663 offset += copy;
1664 to += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 }
David S. Millerfbb398a2009-06-09 00:18:59 -07001666 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 }
Shirley Maa6686f22011-07-06 12:22:12 +00001668
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 if (!len)
1670 return 0;
1671
1672fault:
1673 return -EFAULT;
1674}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001675EXPORT_SYMBOL(skb_copy_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676
Jens Axboe9c55e012007-11-06 23:30:13 -08001677/*
1678 * Callback from splice_to_pipe(), if we need to release some pages
1679 * at the end of the spd in case we error'ed out in filling the pipe.
1680 */
1681static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
1682{
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001683 put_page(spd->pages[i]);
1684}
Jens Axboe9c55e012007-11-06 23:30:13 -08001685
David S. Millera108d5f2012-04-23 23:06:11 -04001686static struct page *linear_to_page(struct page *page, unsigned int *len,
1687 unsigned int *offset,
Eric Dumazet18aafc62013-01-11 14:46:37 +00001688 struct sock *sk)
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001689{
Eric Dumazet5640f762012-09-23 23:04:42 +00001690 struct page_frag *pfrag = sk_page_frag(sk);
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001691
Eric Dumazet5640f762012-09-23 23:04:42 +00001692 if (!sk_page_frag_refill(sk, pfrag))
1693 return NULL;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001694
Eric Dumazet5640f762012-09-23 23:04:42 +00001695 *len = min_t(unsigned int, *len, pfrag->size - pfrag->offset);
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001696
Eric Dumazet5640f762012-09-23 23:04:42 +00001697 memcpy(page_address(pfrag->page) + pfrag->offset,
1698 page_address(page) + *offset, *len);
1699 *offset = pfrag->offset;
1700 pfrag->offset += *len;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001701
Eric Dumazet5640f762012-09-23 23:04:42 +00001702 return pfrag->page;
Jens Axboe9c55e012007-11-06 23:30:13 -08001703}
1704
Eric Dumazet41c73a02012-04-22 12:26:16 +00001705static bool spd_can_coalesce(const struct splice_pipe_desc *spd,
1706 struct page *page,
1707 unsigned int offset)
1708{
1709 return spd->nr_pages &&
1710 spd->pages[spd->nr_pages - 1] == page &&
1711 (spd->partial[spd->nr_pages - 1].offset +
1712 spd->partial[spd->nr_pages - 1].len == offset);
1713}
1714
Jens Axboe9c55e012007-11-06 23:30:13 -08001715/*
1716 * Fill page/offset/length into spd, if it can hold more pages.
1717 */
David S. Millera108d5f2012-04-23 23:06:11 -04001718static bool spd_fill_page(struct splice_pipe_desc *spd,
1719 struct pipe_inode_info *pipe, struct page *page,
1720 unsigned int *len, unsigned int offset,
Eric Dumazet18aafc62013-01-11 14:46:37 +00001721 bool linear,
David S. Millera108d5f2012-04-23 23:06:11 -04001722 struct sock *sk)
Jens Axboe9c55e012007-11-06 23:30:13 -08001723{
Eric Dumazet41c73a02012-04-22 12:26:16 +00001724 if (unlikely(spd->nr_pages == MAX_SKB_FRAGS))
David S. Millera108d5f2012-04-23 23:06:11 -04001725 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08001726
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001727 if (linear) {
Eric Dumazet18aafc62013-01-11 14:46:37 +00001728 page = linear_to_page(page, len, &offset, sk);
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001729 if (!page)
David S. Millera108d5f2012-04-23 23:06:11 -04001730 return true;
Eric Dumazet41c73a02012-04-22 12:26:16 +00001731 }
1732 if (spd_can_coalesce(spd, page, offset)) {
1733 spd->partial[spd->nr_pages - 1].len += *len;
David S. Millera108d5f2012-04-23 23:06:11 -04001734 return false;
Eric Dumazet41c73a02012-04-22 12:26:16 +00001735 }
1736 get_page(page);
Jens Axboe9c55e012007-11-06 23:30:13 -08001737 spd->pages[spd->nr_pages] = page;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001738 spd->partial[spd->nr_pages].len = *len;
Jens Axboe9c55e012007-11-06 23:30:13 -08001739 spd->partial[spd->nr_pages].offset = offset;
Jens Axboe9c55e012007-11-06 23:30:13 -08001740 spd->nr_pages++;
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001741
David S. Millera108d5f2012-04-23 23:06:11 -04001742 return false;
Jens Axboe9c55e012007-11-06 23:30:13 -08001743}
1744
David S. Millera108d5f2012-04-23 23:06:11 -04001745static bool __splice_segment(struct page *page, unsigned int poff,
1746 unsigned int plen, unsigned int *off,
Eric Dumazet18aafc62013-01-11 14:46:37 +00001747 unsigned int *len,
Eric Dumazetd7ccf7c2012-04-23 23:35:04 -04001748 struct splice_pipe_desc *spd, bool linear,
David S. Millera108d5f2012-04-23 23:06:11 -04001749 struct sock *sk,
1750 struct pipe_inode_info *pipe)
Octavian Purdila2870c432008-07-15 00:49:11 -07001751{
1752 if (!*len)
David S. Millera108d5f2012-04-23 23:06:11 -04001753 return true;
Octavian Purdila2870c432008-07-15 00:49:11 -07001754
1755 /* skip this segment if already processed */
1756 if (*off >= plen) {
1757 *off -= plen;
David S. Millera108d5f2012-04-23 23:06:11 -04001758 return false;
Octavian Purdiladb43a282008-06-27 17:27:21 -07001759 }
Jens Axboe9c55e012007-11-06 23:30:13 -08001760
Octavian Purdila2870c432008-07-15 00:49:11 -07001761 /* ignore any bits we already processed */
Eric Dumazet9ca1b222013-01-05 21:31:18 +00001762 poff += *off;
1763 plen -= *off;
1764 *off = 0;
Octavian Purdila2870c432008-07-15 00:49:11 -07001765
Eric Dumazet18aafc62013-01-11 14:46:37 +00001766 do {
1767 unsigned int flen = min(*len, plen);
Octavian Purdila2870c432008-07-15 00:49:11 -07001768
Eric Dumazet18aafc62013-01-11 14:46:37 +00001769 if (spd_fill_page(spd, pipe, page, &flen, poff,
1770 linear, sk))
1771 return true;
1772 poff += flen;
1773 plen -= flen;
1774 *len -= flen;
1775 } while (*len && plen);
Octavian Purdila2870c432008-07-15 00:49:11 -07001776
David S. Millera108d5f2012-04-23 23:06:11 -04001777 return false;
Octavian Purdila2870c432008-07-15 00:49:11 -07001778}
1779
1780/*
David S. Millera108d5f2012-04-23 23:06:11 -04001781 * Map linear and fragment data from the skb to spd. It reports true if the
Octavian Purdila2870c432008-07-15 00:49:11 -07001782 * pipe is full or if we already spliced the requested length.
1783 */
David S. Millera108d5f2012-04-23 23:06:11 -04001784static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe,
1785 unsigned int *offset, unsigned int *len,
1786 struct splice_pipe_desc *spd, struct sock *sk)
Octavian Purdila2870c432008-07-15 00:49:11 -07001787{
1788 int seg;
1789
Eric Dumazet1d0c0b32012-04-27 02:10:03 +00001790 /* map the linear part :
Alexander Duyck2996d312012-05-02 18:18:42 +00001791 * If skb->head_frag is set, this 'linear' part is backed by a
1792 * fragment, and if the head is not shared with any clones then
1793 * we can avoid a copy since we own the head portion of this page.
Jens Axboe9c55e012007-11-06 23:30:13 -08001794 */
Octavian Purdila2870c432008-07-15 00:49:11 -07001795 if (__splice_segment(virt_to_page(skb->data),
1796 (unsigned long) skb->data & (PAGE_SIZE - 1),
1797 skb_headlen(skb),
Eric Dumazet18aafc62013-01-11 14:46:37 +00001798 offset, len, spd,
Alexander Duyck3a7c1ee42012-05-03 01:09:42 +00001799 skb_head_is_locked(skb),
Eric Dumazet1d0c0b32012-04-27 02:10:03 +00001800 sk, pipe))
David S. Millera108d5f2012-04-23 23:06:11 -04001801 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08001802
1803 /*
1804 * then map the fragments
1805 */
Jens Axboe9c55e012007-11-06 23:30:13 -08001806 for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
1807 const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
1808
Ian Campbellea2ab692011-08-22 23:44:58 +00001809 if (__splice_segment(skb_frag_page(f),
Eric Dumazet9e903e02011-10-18 21:00:24 +00001810 f->page_offset, skb_frag_size(f),
Eric Dumazet18aafc62013-01-11 14:46:37 +00001811 offset, len, spd, false, sk, pipe))
David S. Millera108d5f2012-04-23 23:06:11 -04001812 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08001813 }
1814
David S. Millera108d5f2012-04-23 23:06:11 -04001815 return false;
Jens Axboe9c55e012007-11-06 23:30:13 -08001816}
1817
1818/*
1819 * Map data from the skb to a pipe. Should handle both the linear part,
1820 * the fragments, and the frag list. It does NOT handle frag lists within
1821 * the frag list, if such a thing exists. We'd probably need to recurse to
1822 * handle that cleanly.
1823 */
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001824int skb_splice_bits(struct sk_buff *skb, unsigned int offset,
Jens Axboe9c55e012007-11-06 23:30:13 -08001825 struct pipe_inode_info *pipe, unsigned int tlen,
1826 unsigned int flags)
1827{
Eric Dumazet41c73a02012-04-22 12:26:16 +00001828 struct partial_page partial[MAX_SKB_FRAGS];
1829 struct page *pages[MAX_SKB_FRAGS];
Jens Axboe9c55e012007-11-06 23:30:13 -08001830 struct splice_pipe_desc spd = {
1831 .pages = pages,
1832 .partial = partial,
Eric Dumazet047fe362012-06-12 15:24:40 +02001833 .nr_pages_max = MAX_SKB_FRAGS,
Jens Axboe9c55e012007-11-06 23:30:13 -08001834 .flags = flags,
Miklos Szeredi28a625c2014-01-22 19:36:57 +01001835 .ops = &nosteal_pipe_buf_ops,
Jens Axboe9c55e012007-11-06 23:30:13 -08001836 .spd_release = sock_spd_release,
1837 };
David S. Millerfbb398a2009-06-09 00:18:59 -07001838 struct sk_buff *frag_iter;
Jarek Poplawski7a67e562009-04-30 05:41:19 -07001839 struct sock *sk = skb->sk;
Jens Axboe35f3d142010-05-20 10:43:18 +02001840 int ret = 0;
1841
Jens Axboe9c55e012007-11-06 23:30:13 -08001842 /*
1843 * __skb_splice_bits() only fails if the output has no room left,
1844 * so no point in going over the frag_list for the error case.
1845 */
Jens Axboe35f3d142010-05-20 10:43:18 +02001846 if (__skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk))
Jens Axboe9c55e012007-11-06 23:30:13 -08001847 goto done;
1848 else if (!tlen)
1849 goto done;
1850
1851 /*
1852 * now see if we have a frag_list to map
1853 */
David S. Millerfbb398a2009-06-09 00:18:59 -07001854 skb_walk_frags(skb, frag_iter) {
1855 if (!tlen)
1856 break;
Jens Axboe35f3d142010-05-20 10:43:18 +02001857 if (__skb_splice_bits(frag_iter, pipe, &offset, &tlen, &spd, sk))
David S. Millerfbb398a2009-06-09 00:18:59 -07001858 break;
Jens Axboe9c55e012007-11-06 23:30:13 -08001859 }
1860
1861done:
Jens Axboe9c55e012007-11-06 23:30:13 -08001862 if (spd.nr_pages) {
Jens Axboe9c55e012007-11-06 23:30:13 -08001863 /*
1864 * Drop the socket lock, otherwise we have reverse
1865 * locking dependencies between sk_lock and i_mutex
1866 * here as compared to sendfile(). We enter here
1867 * with the socket lock held, and splice_to_pipe() will
1868 * grab the pipe inode lock. For sendfile() emulation,
1869 * we call into ->sendpage() with the i_mutex lock held
1870 * and networking will grab the socket lock.
1871 */
Octavian Purdila293ad602008-06-04 15:45:58 -07001872 release_sock(sk);
Jens Axboe9c55e012007-11-06 23:30:13 -08001873 ret = splice_to_pipe(pipe, &spd);
Octavian Purdila293ad602008-06-04 15:45:58 -07001874 lock_sock(sk);
Jens Axboe9c55e012007-11-06 23:30:13 -08001875 }
1876
Jens Axboe35f3d142010-05-20 10:43:18 +02001877 return ret;
Jens Axboe9c55e012007-11-06 23:30:13 -08001878}
1879
Herbert Xu357b40a2005-04-19 22:30:14 -07001880/**
1881 * skb_store_bits - store bits from kernel buffer to skb
1882 * @skb: destination buffer
1883 * @offset: offset in destination
1884 * @from: source buffer
1885 * @len: number of bytes to copy
1886 *
1887 * Copy the specified number of bytes from the source buffer to the
1888 * destination skb. This function handles all the messy bits of
1889 * traversing fragment lists and such.
1890 */
1891
Stephen Hemminger0c6fcc82007-04-20 16:40:01 -07001892int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
Herbert Xu357b40a2005-04-19 22:30:14 -07001893{
David S. Miller1a028e52007-04-27 15:21:23 -07001894 int start = skb_headlen(skb);
David S. Millerfbb398a2009-06-09 00:18:59 -07001895 struct sk_buff *frag_iter;
1896 int i, copy;
Herbert Xu357b40a2005-04-19 22:30:14 -07001897
1898 if (offset > (int)skb->len - len)
1899 goto fault;
1900
David S. Miller1a028e52007-04-27 15:21:23 -07001901 if ((copy = start - offset) > 0) {
Herbert Xu357b40a2005-04-19 22:30:14 -07001902 if (copy > len)
1903 copy = len;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001904 skb_copy_to_linear_data_offset(skb, offset, from, copy);
Herbert Xu357b40a2005-04-19 22:30:14 -07001905 if ((len -= copy) == 0)
1906 return 0;
1907 offset += copy;
1908 from += copy;
1909 }
1910
1911 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1912 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
David S. Miller1a028e52007-04-27 15:21:23 -07001913 int end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001914
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001915 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07001916
Eric Dumazet9e903e02011-10-18 21:00:24 +00001917 end = start + skb_frag_size(frag);
Herbert Xu357b40a2005-04-19 22:30:14 -07001918 if ((copy = end - offset) > 0) {
1919 u8 *vaddr;
1920
1921 if (copy > len)
1922 copy = len;
1923
Eric Dumazet51c56b02012-04-05 11:35:15 +02001924 vaddr = kmap_atomic(skb_frag_page(frag));
David S. Miller1a028e52007-04-27 15:21:23 -07001925 memcpy(vaddr + frag->page_offset + offset - start,
1926 from, copy);
Eric Dumazet51c56b02012-04-05 11:35:15 +02001927 kunmap_atomic(vaddr);
Herbert Xu357b40a2005-04-19 22:30:14 -07001928
1929 if ((len -= copy) == 0)
1930 return 0;
1931 offset += copy;
1932 from += copy;
1933 }
David S. Miller1a028e52007-04-27 15:21:23 -07001934 start = end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001935 }
1936
David S. Millerfbb398a2009-06-09 00:18:59 -07001937 skb_walk_frags(skb, frag_iter) {
1938 int end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001939
David S. Millerfbb398a2009-06-09 00:18:59 -07001940 WARN_ON(start > offset + len);
Herbert Xu357b40a2005-04-19 22:30:14 -07001941
David S. Millerfbb398a2009-06-09 00:18:59 -07001942 end = start + frag_iter->len;
1943 if ((copy = end - offset) > 0) {
1944 if (copy > len)
1945 copy = len;
1946 if (skb_store_bits(frag_iter, offset - start,
1947 from, copy))
1948 goto fault;
1949 if ((len -= copy) == 0)
1950 return 0;
1951 offset += copy;
1952 from += copy;
Herbert Xu357b40a2005-04-19 22:30:14 -07001953 }
David S. Millerfbb398a2009-06-09 00:18:59 -07001954 start = end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001955 }
1956 if (!len)
1957 return 0;
1958
1959fault:
1960 return -EFAULT;
1961}
Herbert Xu357b40a2005-04-19 22:30:14 -07001962EXPORT_SYMBOL(skb_store_bits);
1963
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964/* Checksum skb data. */
Daniel Borkmann2817a332013-10-30 11:50:51 +01001965__wsum __skb_checksum(const struct sk_buff *skb, int offset, int len,
1966 __wsum csum, const struct skb_checksum_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967{
David S. Miller1a028e52007-04-27 15:21:23 -07001968 int start = skb_headlen(skb);
1969 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07001970 struct sk_buff *frag_iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 int pos = 0;
1972
1973 /* Checksum header. */
1974 if (copy > 0) {
1975 if (copy > len)
1976 copy = len;
Daniel Borkmann2817a332013-10-30 11:50:51 +01001977 csum = ops->update(skb->data + offset, copy, csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 if ((len -= copy) == 0)
1979 return csum;
1980 offset += copy;
1981 pos = copy;
1982 }
1983
1984 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07001985 int end;
Eric Dumazet51c56b02012-04-05 11:35:15 +02001986 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001988 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07001989
Eric Dumazet51c56b02012-04-05 11:35:15 +02001990 end = start + skb_frag_size(frag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 if ((copy = end - offset) > 0) {
Al Viro44bb9362006-11-14 21:36:14 -08001992 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 u8 *vaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994
1995 if (copy > len)
1996 copy = len;
Eric Dumazet51c56b02012-04-05 11:35:15 +02001997 vaddr = kmap_atomic(skb_frag_page(frag));
Daniel Borkmann2817a332013-10-30 11:50:51 +01001998 csum2 = ops->update(vaddr + frag->page_offset +
1999 offset - start, copy, 0);
Eric Dumazet51c56b02012-04-05 11:35:15 +02002000 kunmap_atomic(vaddr);
Daniel Borkmann2817a332013-10-30 11:50:51 +01002001 csum = ops->combine(csum, csum2, pos, copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 if (!(len -= copy))
2003 return csum;
2004 offset += copy;
2005 pos += copy;
2006 }
David S. Miller1a028e52007-04-27 15:21:23 -07002007 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 }
2009
David S. Millerfbb398a2009-06-09 00:18:59 -07002010 skb_walk_frags(skb, frag_iter) {
2011 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012
David S. Millerfbb398a2009-06-09 00:18:59 -07002013 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014
David S. Millerfbb398a2009-06-09 00:18:59 -07002015 end = start + frag_iter->len;
2016 if ((copy = end - offset) > 0) {
2017 __wsum csum2;
2018 if (copy > len)
2019 copy = len;
Daniel Borkmann2817a332013-10-30 11:50:51 +01002020 csum2 = __skb_checksum(frag_iter, offset - start,
2021 copy, 0, ops);
2022 csum = ops->combine(csum, csum2, pos, copy);
David S. Millerfbb398a2009-06-09 00:18:59 -07002023 if ((len -= copy) == 0)
2024 return csum;
2025 offset += copy;
2026 pos += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002028 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08002030 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031
2032 return csum;
2033}
Daniel Borkmann2817a332013-10-30 11:50:51 +01002034EXPORT_SYMBOL(__skb_checksum);
2035
2036__wsum skb_checksum(const struct sk_buff *skb, int offset,
2037 int len, __wsum csum)
2038{
2039 const struct skb_checksum_ops ops = {
Daniel Borkmanncea80ea2013-11-04 17:10:25 +01002040 .update = csum_partial_ext,
Daniel Borkmann2817a332013-10-30 11:50:51 +01002041 .combine = csum_block_add_ext,
2042 };
2043
2044 return __skb_checksum(skb, offset, len, csum, &ops);
2045}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002046EXPORT_SYMBOL(skb_checksum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047
2048/* Both of above in one bottle. */
2049
Al Viro81d77662006-11-14 21:37:33 -08002050__wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
2051 u8 *to, int len, __wsum csum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052{
David S. Miller1a028e52007-04-27 15:21:23 -07002053 int start = skb_headlen(skb);
2054 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07002055 struct sk_buff *frag_iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 int pos = 0;
2057
2058 /* Copy header. */
2059 if (copy > 0) {
2060 if (copy > len)
2061 copy = len;
2062 csum = csum_partial_copy_nocheck(skb->data + offset, to,
2063 copy, csum);
2064 if ((len -= copy) == 0)
2065 return csum;
2066 offset += copy;
2067 to += copy;
2068 pos = copy;
2069 }
2070
2071 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07002072 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002074 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07002075
Eric Dumazet9e903e02011-10-18 21:00:24 +00002076 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 if ((copy = end - offset) > 0) {
Al Viro50842052006-11-14 21:36:34 -08002078 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 u8 *vaddr;
2080 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2081
2082 if (copy > len)
2083 copy = len;
Eric Dumazet51c56b02012-04-05 11:35:15 +02002084 vaddr = kmap_atomic(skb_frag_page(frag));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 csum2 = csum_partial_copy_nocheck(vaddr +
David S. Miller1a028e52007-04-27 15:21:23 -07002086 frag->page_offset +
2087 offset - start, to,
2088 copy, 0);
Eric Dumazet51c56b02012-04-05 11:35:15 +02002089 kunmap_atomic(vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 csum = csum_block_add(csum, csum2, pos);
2091 if (!(len -= copy))
2092 return csum;
2093 offset += copy;
2094 to += copy;
2095 pos += copy;
2096 }
David S. Miller1a028e52007-04-27 15:21:23 -07002097 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 }
2099
David S. Millerfbb398a2009-06-09 00:18:59 -07002100 skb_walk_frags(skb, frag_iter) {
2101 __wsum csum2;
2102 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103
David S. Millerfbb398a2009-06-09 00:18:59 -07002104 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105
David S. Millerfbb398a2009-06-09 00:18:59 -07002106 end = start + frag_iter->len;
2107 if ((copy = end - offset) > 0) {
2108 if (copy > len)
2109 copy = len;
2110 csum2 = skb_copy_and_csum_bits(frag_iter,
2111 offset - start,
2112 to, copy, 0);
2113 csum = csum_block_add(csum, csum2, pos);
2114 if ((len -= copy) == 0)
2115 return csum;
2116 offset += copy;
2117 to += copy;
2118 pos += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002120 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08002122 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 return csum;
2124}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002125EXPORT_SYMBOL(skb_copy_and_csum_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126
Thomas Grafaf2806f2013-12-13 15:22:17 +01002127 /**
2128 * skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy()
2129 * @from: source buffer
2130 *
2131 * Calculates the amount of linear headroom needed in the 'to' skb passed
2132 * into skb_zerocopy().
2133 */
2134unsigned int
2135skb_zerocopy_headlen(const struct sk_buff *from)
2136{
2137 unsigned int hlen = 0;
2138
2139 if (!from->head_frag ||
2140 skb_headlen(from) < L1_CACHE_BYTES ||
2141 skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
2142 hlen = skb_headlen(from);
2143
2144 if (skb_has_frag_list(from))
2145 hlen = from->len;
2146
2147 return hlen;
2148}
2149EXPORT_SYMBOL_GPL(skb_zerocopy_headlen);
2150
2151/**
2152 * skb_zerocopy - Zero copy skb to skb
2153 * @to: destination buffer
Masanari Iida7fceb4d2014-01-29 01:05:28 +09002154 * @from: source buffer
Thomas Grafaf2806f2013-12-13 15:22:17 +01002155 * @len: number of bytes to copy from source buffer
2156 * @hlen: size of linear headroom in destination buffer
2157 *
2158 * Copies up to `len` bytes from `from` to `to` by creating references
2159 * to the frags in the source buffer.
2160 *
2161 * The `hlen` as calculated by skb_zerocopy_headlen() specifies the
2162 * headroom in the `to` buffer.
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002163 *
2164 * Return value:
2165 * 0: everything is OK
2166 * -ENOMEM: couldn't orphan frags of @from due to lack of memory
2167 * -EFAULT: skb_copy_bits() found some problem with skb geometry
Thomas Grafaf2806f2013-12-13 15:22:17 +01002168 */
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002169int
2170skb_zerocopy(struct sk_buff *to, struct sk_buff *from, int len, int hlen)
Thomas Grafaf2806f2013-12-13 15:22:17 +01002171{
2172 int i, j = 0;
2173 int plen = 0; /* length of skb->head fragment */
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002174 int ret;
Thomas Grafaf2806f2013-12-13 15:22:17 +01002175 struct page *page;
2176 unsigned int offset;
2177
2178 BUG_ON(!from->head_frag && !hlen);
2179
2180 /* dont bother with small payloads */
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002181 if (len <= skb_tailroom(to))
2182 return skb_copy_bits(from, 0, skb_put(to, len), len);
Thomas Grafaf2806f2013-12-13 15:22:17 +01002183
2184 if (hlen) {
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002185 ret = skb_copy_bits(from, 0, skb_put(to, hlen), hlen);
2186 if (unlikely(ret))
2187 return ret;
Thomas Grafaf2806f2013-12-13 15:22:17 +01002188 len -= hlen;
2189 } else {
2190 plen = min_t(int, skb_headlen(from), len);
2191 if (plen) {
2192 page = virt_to_head_page(from->head);
2193 offset = from->data - (unsigned char *)page_address(page);
2194 __skb_fill_page_desc(to, 0, page, offset, plen);
2195 get_page(page);
2196 j = 1;
2197 len -= plen;
2198 }
2199 }
2200
2201 to->truesize += len + plen;
2202 to->len += len + plen;
2203 to->data_len += len + plen;
2204
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002205 if (unlikely(skb_orphan_frags(from, GFP_ATOMIC))) {
2206 skb_tx_error(from);
2207 return -ENOMEM;
2208 }
2209
Thomas Grafaf2806f2013-12-13 15:22:17 +01002210 for (i = 0; i < skb_shinfo(from)->nr_frags; i++) {
2211 if (!len)
2212 break;
2213 skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i];
2214 skb_shinfo(to)->frags[j].size = min_t(int, skb_shinfo(to)->frags[j].size, len);
2215 len -= skb_shinfo(to)->frags[j].size;
2216 skb_frag_ref(to, j);
2217 j++;
2218 }
2219 skb_shinfo(to)->nr_frags = j;
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002220
2221 return 0;
Thomas Grafaf2806f2013-12-13 15:22:17 +01002222}
2223EXPORT_SYMBOL_GPL(skb_zerocopy);
2224
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
2226{
Al Virod3bc23e2006-11-14 21:24:49 -08002227 __wsum csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 long csstart;
2229
Patrick McHardy84fa7932006-08-29 16:44:56 -07002230 if (skb->ip_summed == CHECKSUM_PARTIAL)
Michał Mirosław55508d62010-12-14 15:24:08 +00002231 csstart = skb_checksum_start_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 else
2233 csstart = skb_headlen(skb);
2234
Kris Katterjohn09a62662006-01-08 22:24:28 -08002235 BUG_ON(csstart > skb_headlen(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03002237 skb_copy_from_linear_data(skb, to, csstart);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238
2239 csum = 0;
2240 if (csstart != skb->len)
2241 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
2242 skb->len - csstart, 0);
2243
Patrick McHardy84fa7932006-08-29 16:44:56 -07002244 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Al Viroff1dcad2006-11-20 18:07:29 -08002245 long csstuff = csstart + skb->csum_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246
Al Virod3bc23e2006-11-14 21:24:49 -08002247 *((__sum16 *)(to + csstuff)) = csum_fold(csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 }
2249}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002250EXPORT_SYMBOL(skb_copy_and_csum_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251
2252/**
2253 * skb_dequeue - remove from the head of the queue
2254 * @list: list to dequeue from
2255 *
2256 * Remove the head of the list. The list lock is taken so the function
2257 * may be used safely with other locking list functions. The head item is
2258 * returned or %NULL if the list is empty.
2259 */
2260
2261struct sk_buff *skb_dequeue(struct sk_buff_head *list)
2262{
2263 unsigned long flags;
2264 struct sk_buff *result;
2265
2266 spin_lock_irqsave(&list->lock, flags);
2267 result = __skb_dequeue(list);
2268 spin_unlock_irqrestore(&list->lock, flags);
2269 return result;
2270}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002271EXPORT_SYMBOL(skb_dequeue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272
2273/**
2274 * skb_dequeue_tail - remove from the tail of the queue
2275 * @list: list to dequeue from
2276 *
2277 * Remove the tail of the list. The list lock is taken so the function
2278 * may be used safely with other locking list functions. The tail item is
2279 * returned or %NULL if the list is empty.
2280 */
2281struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
2282{
2283 unsigned long flags;
2284 struct sk_buff *result;
2285
2286 spin_lock_irqsave(&list->lock, flags);
2287 result = __skb_dequeue_tail(list);
2288 spin_unlock_irqrestore(&list->lock, flags);
2289 return result;
2290}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002291EXPORT_SYMBOL(skb_dequeue_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292
2293/**
2294 * skb_queue_purge - empty a list
2295 * @list: list to empty
2296 *
2297 * Delete all buffers on an &sk_buff list. Each buffer is removed from
2298 * the list and one reference dropped. This function takes the list
2299 * lock and is atomic with respect to other list locking functions.
2300 */
2301void skb_queue_purge(struct sk_buff_head *list)
2302{
2303 struct sk_buff *skb;
2304 while ((skb = skb_dequeue(list)) != NULL)
2305 kfree_skb(skb);
2306}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002307EXPORT_SYMBOL(skb_queue_purge);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308
2309/**
2310 * skb_queue_head - queue a buffer at the list head
2311 * @list: list to use
2312 * @newsk: buffer to queue
2313 *
2314 * Queue a buffer at the start of the list. This function takes the
2315 * list lock and can be used safely with other locking &sk_buff functions
2316 * safely.
2317 *
2318 * A buffer cannot be placed on two lists at the same time.
2319 */
2320void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
2321{
2322 unsigned long flags;
2323
2324 spin_lock_irqsave(&list->lock, flags);
2325 __skb_queue_head(list, newsk);
2326 spin_unlock_irqrestore(&list->lock, flags);
2327}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002328EXPORT_SYMBOL(skb_queue_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329
2330/**
2331 * skb_queue_tail - queue a buffer at the list tail
2332 * @list: list to use
2333 * @newsk: buffer to queue
2334 *
2335 * Queue a buffer at the tail of the list. This function takes the
2336 * list lock and can be used safely with other locking &sk_buff functions
2337 * safely.
2338 *
2339 * A buffer cannot be placed on two lists at the same time.
2340 */
2341void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
2342{
2343 unsigned long flags;
2344
2345 spin_lock_irqsave(&list->lock, flags);
2346 __skb_queue_tail(list, newsk);
2347 spin_unlock_irqrestore(&list->lock, flags);
2348}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002349EXPORT_SYMBOL(skb_queue_tail);
David S. Miller8728b832005-08-09 19:25:21 -07002350
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351/**
2352 * skb_unlink - remove a buffer from a list
2353 * @skb: buffer to remove
David S. Miller8728b832005-08-09 19:25:21 -07002354 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 *
David S. Miller8728b832005-08-09 19:25:21 -07002356 * Remove a packet from a list. The list locks are taken and this
2357 * function is atomic with respect to other list locked calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 *
David S. Miller8728b832005-08-09 19:25:21 -07002359 * You must know what list the SKB is on.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 */
David S. Miller8728b832005-08-09 19:25:21 -07002361void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362{
David S. Miller8728b832005-08-09 19:25:21 -07002363 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364
David S. Miller8728b832005-08-09 19:25:21 -07002365 spin_lock_irqsave(&list->lock, flags);
2366 __skb_unlink(skb, list);
2367 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002369EXPORT_SYMBOL(skb_unlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371/**
2372 * skb_append - append a buffer
2373 * @old: buffer to insert after
2374 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07002375 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 *
2377 * Place a packet after a given packet in a list. The list locks are taken
2378 * and this function is atomic with respect to other list locked calls.
2379 * A buffer cannot be placed on two lists at the same time.
2380 */
David S. Miller8728b832005-08-09 19:25:21 -07002381void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382{
2383 unsigned long flags;
2384
David S. Miller8728b832005-08-09 19:25:21 -07002385 spin_lock_irqsave(&list->lock, flags);
Gerrit Renker7de6c032008-04-14 00:05:09 -07002386 __skb_queue_after(list, old, newsk);
David S. Miller8728b832005-08-09 19:25:21 -07002387 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002389EXPORT_SYMBOL(skb_append);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390
2391/**
2392 * skb_insert - insert a buffer
2393 * @old: buffer to insert before
2394 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07002395 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 *
David S. Miller8728b832005-08-09 19:25:21 -07002397 * Place a packet before a given packet in a list. The list locks are
2398 * taken and this function is atomic with respect to other list locked
2399 * calls.
2400 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 * A buffer cannot be placed on two lists at the same time.
2402 */
David S. Miller8728b832005-08-09 19:25:21 -07002403void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404{
2405 unsigned long flags;
2406
David S. Miller8728b832005-08-09 19:25:21 -07002407 spin_lock_irqsave(&list->lock, flags);
2408 __skb_insert(newsk, old->prev, old, list);
2409 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002411EXPORT_SYMBOL(skb_insert);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413static inline void skb_split_inside_header(struct sk_buff *skb,
2414 struct sk_buff* skb1,
2415 const u32 len, const int pos)
2416{
2417 int i;
2418
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03002419 skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
2420 pos - len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 /* And move data appendix as is. */
2422 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
2423 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
2424
2425 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
2426 skb_shinfo(skb)->nr_frags = 0;
2427 skb1->data_len = skb->data_len;
2428 skb1->len += skb1->data_len;
2429 skb->data_len = 0;
2430 skb->len = len;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002431 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432}
2433
2434static inline void skb_split_no_header(struct sk_buff *skb,
2435 struct sk_buff* skb1,
2436 const u32 len, int pos)
2437{
2438 int i, k = 0;
2439 const int nfrags = skb_shinfo(skb)->nr_frags;
2440
2441 skb_shinfo(skb)->nr_frags = 0;
2442 skb1->len = skb1->data_len = skb->len - len;
2443 skb->len = len;
2444 skb->data_len = len - pos;
2445
2446 for (i = 0; i < nfrags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00002447 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448
2449 if (pos + size > len) {
2450 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
2451
2452 if (pos < len) {
2453 /* Split frag.
2454 * We have two variants in this case:
2455 * 1. Move all the frag to the second
2456 * part, if it is possible. F.e.
2457 * this approach is mandatory for TUX,
2458 * where splitting is expensive.
2459 * 2. Split is accurately. We make this.
2460 */
Ian Campbellea2ab692011-08-22 23:44:58 +00002461 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002463 skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos);
2464 skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 skb_shinfo(skb)->nr_frags++;
2466 }
2467 k++;
2468 } else
2469 skb_shinfo(skb)->nr_frags++;
2470 pos += size;
2471 }
2472 skb_shinfo(skb1)->nr_frags = k;
2473}
2474
2475/**
2476 * skb_split - Split fragmented skb to two parts at length len.
2477 * @skb: the buffer to split
2478 * @skb1: the buffer to receive the second part
2479 * @len: new length for skb
2480 */
2481void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
2482{
2483 int pos = skb_headlen(skb);
2484
Amerigo Wang68534c62013-02-19 22:51:30 +00002485 skb_shinfo(skb1)->tx_flags = skb_shinfo(skb)->tx_flags & SKBTX_SHARED_FRAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 if (len < pos) /* Split line is inside header. */
2487 skb_split_inside_header(skb, skb1, len, pos);
2488 else /* Second chunk has no header, nothing to copy. */
2489 skb_split_no_header(skb, skb1, len, pos);
2490}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002491EXPORT_SYMBOL(skb_split);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492
Ilpo Järvinen9f782db2008-11-25 13:57:01 -08002493/* Shifting from/to a cloned skb is a no-go.
2494 *
2495 * Caller cannot keep skb_shinfo related pointers past calling here!
2496 */
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002497static int skb_prepare_for_shift(struct sk_buff *skb)
2498{
Ilpo Järvinen0ace2852008-11-24 21:30:21 -08002499 return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002500}
2501
2502/**
2503 * skb_shift - Shifts paged data partially from skb to another
2504 * @tgt: buffer into which tail data gets added
2505 * @skb: buffer from which the paged data comes from
2506 * @shiftlen: shift up to this many bytes
2507 *
2508 * Attempts to shift up to shiftlen worth of bytes, which may be less than
Feng King20e994a2011-11-21 01:47:11 +00002509 * the length of the skb, from skb to tgt. Returns number bytes shifted.
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002510 * It's up to caller to free skb if everything was shifted.
2511 *
2512 * If @tgt runs out of frags, the whole operation is aborted.
2513 *
2514 * Skb cannot include anything else but paged data while tgt is allowed
2515 * to have non-paged data as well.
2516 *
2517 * TODO: full sized shift could be optimized but that would need
2518 * specialized skb free'er to handle frags without up-to-date nr_frags.
2519 */
2520int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
2521{
2522 int from, to, merge, todo;
2523 struct skb_frag_struct *fragfrom, *fragto;
2524
2525 BUG_ON(shiftlen > skb->len);
2526 BUG_ON(skb_headlen(skb)); /* Would corrupt stream */
2527
2528 todo = shiftlen;
2529 from = 0;
2530 to = skb_shinfo(tgt)->nr_frags;
2531 fragfrom = &skb_shinfo(skb)->frags[from];
2532
2533 /* Actual merge is delayed until the point when we know we can
2534 * commit all, so that we don't have to undo partial changes
2535 */
2536 if (!to ||
Ian Campbellea2ab692011-08-22 23:44:58 +00002537 !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom),
2538 fragfrom->page_offset)) {
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002539 merge = -1;
2540 } else {
2541 merge = to - 1;
2542
Eric Dumazet9e903e02011-10-18 21:00:24 +00002543 todo -= skb_frag_size(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002544 if (todo < 0) {
2545 if (skb_prepare_for_shift(skb) ||
2546 skb_prepare_for_shift(tgt))
2547 return 0;
2548
Ilpo Järvinen9f782db2008-11-25 13:57:01 -08002549 /* All previous frag pointers might be stale! */
2550 fragfrom = &skb_shinfo(skb)->frags[from];
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002551 fragto = &skb_shinfo(tgt)->frags[merge];
2552
Eric Dumazet9e903e02011-10-18 21:00:24 +00002553 skb_frag_size_add(fragto, shiftlen);
2554 skb_frag_size_sub(fragfrom, shiftlen);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002555 fragfrom->page_offset += shiftlen;
2556
2557 goto onlymerged;
2558 }
2559
2560 from++;
2561 }
2562
2563 /* Skip full, not-fitting skb to avoid expensive operations */
2564 if ((shiftlen == skb->len) &&
2565 (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to))
2566 return 0;
2567
2568 if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt))
2569 return 0;
2570
2571 while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) {
2572 if (to == MAX_SKB_FRAGS)
2573 return 0;
2574
2575 fragfrom = &skb_shinfo(skb)->frags[from];
2576 fragto = &skb_shinfo(tgt)->frags[to];
2577
Eric Dumazet9e903e02011-10-18 21:00:24 +00002578 if (todo >= skb_frag_size(fragfrom)) {
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002579 *fragto = *fragfrom;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002580 todo -= skb_frag_size(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002581 from++;
2582 to++;
2583
2584 } else {
Ian Campbellea2ab692011-08-22 23:44:58 +00002585 __skb_frag_ref(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002586 fragto->page = fragfrom->page;
2587 fragto->page_offset = fragfrom->page_offset;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002588 skb_frag_size_set(fragto, todo);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002589
2590 fragfrom->page_offset += todo;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002591 skb_frag_size_sub(fragfrom, todo);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002592 todo = 0;
2593
2594 to++;
2595 break;
2596 }
2597 }
2598
2599 /* Ready to "commit" this state change to tgt */
2600 skb_shinfo(tgt)->nr_frags = to;
2601
2602 if (merge >= 0) {
2603 fragfrom = &skb_shinfo(skb)->frags[0];
2604 fragto = &skb_shinfo(tgt)->frags[merge];
2605
Eric Dumazet9e903e02011-10-18 21:00:24 +00002606 skb_frag_size_add(fragto, skb_frag_size(fragfrom));
Ian Campbellea2ab692011-08-22 23:44:58 +00002607 __skb_frag_unref(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002608 }
2609
2610 /* Reposition in the original skb */
2611 to = 0;
2612 while (from < skb_shinfo(skb)->nr_frags)
2613 skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++];
2614 skb_shinfo(skb)->nr_frags = to;
2615
2616 BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags);
2617
2618onlymerged:
2619 /* Most likely the tgt won't ever need its checksum anymore, skb on
2620 * the other hand might need it if it needs to be resent
2621 */
2622 tgt->ip_summed = CHECKSUM_PARTIAL;
2623 skb->ip_summed = CHECKSUM_PARTIAL;
2624
2625 /* Yak, is it really working this way? Some helper please? */
2626 skb->len -= shiftlen;
2627 skb->data_len -= shiftlen;
2628 skb->truesize -= shiftlen;
2629 tgt->len += shiftlen;
2630 tgt->data_len += shiftlen;
2631 tgt->truesize += shiftlen;
2632
2633 return shiftlen;
2634}
2635
Thomas Graf677e90e2005-06-23 20:59:51 -07002636/**
2637 * skb_prepare_seq_read - Prepare a sequential read of skb data
2638 * @skb: the buffer to read
2639 * @from: lower offset of data to be read
2640 * @to: upper offset of data to be read
2641 * @st: state variable
2642 *
2643 * Initializes the specified state variable. Must be called before
2644 * invoking skb_seq_read() for the first time.
2645 */
2646void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
2647 unsigned int to, struct skb_seq_state *st)
2648{
2649 st->lower_offset = from;
2650 st->upper_offset = to;
2651 st->root_skb = st->cur_skb = skb;
2652 st->frag_idx = st->stepped_offset = 0;
2653 st->frag_data = NULL;
2654}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002655EXPORT_SYMBOL(skb_prepare_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07002656
2657/**
2658 * skb_seq_read - Sequentially read skb data
2659 * @consumed: number of bytes consumed by the caller so far
2660 * @data: destination pointer for data to be returned
2661 * @st: state variable
2662 *
Mathias Krausebc323832013-11-07 14:18:26 +01002663 * Reads a block of skb data at @consumed relative to the
Thomas Graf677e90e2005-06-23 20:59:51 -07002664 * lower offset specified to skb_prepare_seq_read(). Assigns
Mathias Krausebc323832013-11-07 14:18:26 +01002665 * the head of the data block to @data and returns the length
Thomas Graf677e90e2005-06-23 20:59:51 -07002666 * of the block or 0 if the end of the skb data or the upper
2667 * offset has been reached.
2668 *
2669 * The caller is not required to consume all of the data
Mathias Krausebc323832013-11-07 14:18:26 +01002670 * returned, i.e. @consumed is typically set to the number
Thomas Graf677e90e2005-06-23 20:59:51 -07002671 * of bytes already consumed and the next call to
2672 * skb_seq_read() will return the remaining part of the block.
2673 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002674 * Note 1: The size of each block of data returned can be arbitrary,
Masanari Iidae793c0f2014-09-04 23:44:36 +09002675 * this limitation is the cost for zerocopy sequential
Thomas Graf677e90e2005-06-23 20:59:51 -07002676 * reads of potentially non linear data.
2677 *
Randy Dunlapbc2cda12008-02-13 15:03:25 -08002678 * Note 2: Fragment lists within fragments are not implemented
Thomas Graf677e90e2005-06-23 20:59:51 -07002679 * at the moment, state->root_skb could be replaced with
2680 * a stack for this purpose.
2681 */
2682unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
2683 struct skb_seq_state *st)
2684{
2685 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
2686 skb_frag_t *frag;
2687
Wedson Almeida Filhoaeb193e2013-06-23 23:33:48 -07002688 if (unlikely(abs_offset >= st->upper_offset)) {
2689 if (st->frag_data) {
2690 kunmap_atomic(st->frag_data);
2691 st->frag_data = NULL;
2692 }
Thomas Graf677e90e2005-06-23 20:59:51 -07002693 return 0;
Wedson Almeida Filhoaeb193e2013-06-23 23:33:48 -07002694 }
Thomas Graf677e90e2005-06-23 20:59:51 -07002695
2696next_skb:
Herbert Xu95e3b242009-01-29 16:07:52 -08002697 block_limit = skb_headlen(st->cur_skb) + st->stepped_offset;
Thomas Graf677e90e2005-06-23 20:59:51 -07002698
Thomas Chenault995b3372009-05-18 21:43:27 -07002699 if (abs_offset < block_limit && !st->frag_data) {
Herbert Xu95e3b242009-01-29 16:07:52 -08002700 *data = st->cur_skb->data + (abs_offset - st->stepped_offset);
Thomas Graf677e90e2005-06-23 20:59:51 -07002701 return block_limit - abs_offset;
2702 }
2703
2704 if (st->frag_idx == 0 && !st->frag_data)
2705 st->stepped_offset += skb_headlen(st->cur_skb);
2706
2707 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
2708 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
Eric Dumazet9e903e02011-10-18 21:00:24 +00002709 block_limit = skb_frag_size(frag) + st->stepped_offset;
Thomas Graf677e90e2005-06-23 20:59:51 -07002710
2711 if (abs_offset < block_limit) {
2712 if (!st->frag_data)
Eric Dumazet51c56b02012-04-05 11:35:15 +02002713 st->frag_data = kmap_atomic(skb_frag_page(frag));
Thomas Graf677e90e2005-06-23 20:59:51 -07002714
2715 *data = (u8 *) st->frag_data + frag->page_offset +
2716 (abs_offset - st->stepped_offset);
2717
2718 return block_limit - abs_offset;
2719 }
2720
2721 if (st->frag_data) {
Eric Dumazet51c56b02012-04-05 11:35:15 +02002722 kunmap_atomic(st->frag_data);
Thomas Graf677e90e2005-06-23 20:59:51 -07002723 st->frag_data = NULL;
2724 }
2725
2726 st->frag_idx++;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002727 st->stepped_offset += skb_frag_size(frag);
Thomas Graf677e90e2005-06-23 20:59:51 -07002728 }
2729
Olaf Kirch5b5a60d2007-06-23 23:11:52 -07002730 if (st->frag_data) {
Eric Dumazet51c56b02012-04-05 11:35:15 +02002731 kunmap_atomic(st->frag_data);
Olaf Kirch5b5a60d2007-06-23 23:11:52 -07002732 st->frag_data = NULL;
2733 }
2734
David S. Miller21dc3302010-08-23 00:13:46 -07002735 if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) {
Shyam Iyer71b33462009-01-29 16:12:42 -08002736 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
Thomas Graf677e90e2005-06-23 20:59:51 -07002737 st->frag_idx = 0;
2738 goto next_skb;
Shyam Iyer71b33462009-01-29 16:12:42 -08002739 } else if (st->cur_skb->next) {
2740 st->cur_skb = st->cur_skb->next;
Herbert Xu95e3b242009-01-29 16:07:52 -08002741 st->frag_idx = 0;
Thomas Graf677e90e2005-06-23 20:59:51 -07002742 goto next_skb;
2743 }
2744
2745 return 0;
2746}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002747EXPORT_SYMBOL(skb_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07002748
2749/**
2750 * skb_abort_seq_read - Abort a sequential read of skb data
2751 * @st: state variable
2752 *
2753 * Must be called if skb_seq_read() was not called until it
2754 * returned 0.
2755 */
2756void skb_abort_seq_read(struct skb_seq_state *st)
2757{
2758 if (st->frag_data)
Eric Dumazet51c56b02012-04-05 11:35:15 +02002759 kunmap_atomic(st->frag_data);
Thomas Graf677e90e2005-06-23 20:59:51 -07002760}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002761EXPORT_SYMBOL(skb_abort_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07002762
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002763#define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
2764
2765static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
2766 struct ts_config *conf,
2767 struct ts_state *state)
2768{
2769 return skb_seq_read(offset, text, TS_SKB_CB(state));
2770}
2771
2772static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
2773{
2774 skb_abort_seq_read(TS_SKB_CB(state));
2775}
2776
2777/**
2778 * skb_find_text - Find a text pattern in skb data
2779 * @skb: the buffer to look in
2780 * @from: search offset
2781 * @to: search limit
2782 * @config: textsearch configuration
2783 * @state: uninitialized textsearch state variable
2784 *
2785 * Finds a pattern in the skb data according to the specified
2786 * textsearch configuration. Use textsearch_next() to retrieve
2787 * subsequent occurrences of the pattern. Returns the offset
2788 * to the first occurrence or UINT_MAX if no match was found.
2789 */
2790unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
2791 unsigned int to, struct ts_config *config,
2792 struct ts_state *state)
2793{
Phil Oesterf72b9482006-06-26 00:00:57 -07002794 unsigned int ret;
2795
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002796 config->get_next_block = skb_ts_get_next_block;
2797 config->finish = skb_ts_finish;
2798
2799 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state));
2800
Phil Oesterf72b9482006-06-26 00:00:57 -07002801 ret = textsearch_find(config, state);
2802 return (ret <= to - from ? ret : UINT_MAX);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002803}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002804EXPORT_SYMBOL(skb_find_text);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002805
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002806/**
Ben Hutchings2c530402012-07-10 10:55:09 +00002807 * skb_append_datato_frags - append the user data to a skb
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002808 * @sk: sock structure
Masanari Iidae793c0f2014-09-04 23:44:36 +09002809 * @skb: skb structure to be appended with user data.
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002810 * @getfrag: call back function to be used for getting the user data
2811 * @from: pointer to user message iov
2812 * @length: length of the iov message
2813 *
2814 * Description: This procedure append the user data in the fragment part
2815 * of the skb if any page alloc fails user this procedure returns -ENOMEM
2816 */
2817int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
Martin Waitzdab96302005-12-05 13:40:12 -08002818 int (*getfrag)(void *from, char *to, int offset,
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002819 int len, int odd, struct sk_buff *skb),
2820 void *from, int length)
2821{
Eric Dumazetb2111722012-12-28 06:06:37 +00002822 int frg_cnt = skb_shinfo(skb)->nr_frags;
2823 int copy;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002824 int offset = 0;
2825 int ret;
Eric Dumazetb2111722012-12-28 06:06:37 +00002826 struct page_frag *pfrag = &current->task_frag;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002827
2828 do {
2829 /* Return error if we don't have space for new frag */
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002830 if (frg_cnt >= MAX_SKB_FRAGS)
Eric Dumazetb2111722012-12-28 06:06:37 +00002831 return -EMSGSIZE;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002832
Eric Dumazetb2111722012-12-28 06:06:37 +00002833 if (!sk_page_frag_refill(sk, pfrag))
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002834 return -ENOMEM;
2835
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002836 /* copy the user data to page */
Eric Dumazetb2111722012-12-28 06:06:37 +00002837 copy = min_t(int, length, pfrag->size - pfrag->offset);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002838
Eric Dumazetb2111722012-12-28 06:06:37 +00002839 ret = getfrag(from, page_address(pfrag->page) + pfrag->offset,
2840 offset, copy, 0, skb);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002841 if (ret < 0)
2842 return -EFAULT;
2843
2844 /* copy was successful so update the size parameters */
Eric Dumazetb2111722012-12-28 06:06:37 +00002845 skb_fill_page_desc(skb, frg_cnt, pfrag->page, pfrag->offset,
2846 copy);
2847 frg_cnt++;
2848 pfrag->offset += copy;
2849 get_page(pfrag->page);
2850
2851 skb->truesize += copy;
2852 atomic_add(copy, &sk->sk_wmem_alloc);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002853 skb->len += copy;
2854 skb->data_len += copy;
2855 offset += copy;
2856 length -= copy;
2857
2858 } while (length > 0);
2859
2860 return 0;
2861}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002862EXPORT_SYMBOL(skb_append_datato_frags);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002863
Herbert Xucbb042f2006-03-20 22:43:56 -08002864/**
2865 * skb_pull_rcsum - pull skb and update receive checksum
2866 * @skb: buffer to update
Herbert Xucbb042f2006-03-20 22:43:56 -08002867 * @len: length of data pulled
2868 *
2869 * This function performs an skb_pull on the packet and updates
Urs Thuermannfee54fa2008-02-12 22:03:25 -08002870 * the CHECKSUM_COMPLETE checksum. It should be used on
Patrick McHardy84fa7932006-08-29 16:44:56 -07002871 * receive path processing instead of skb_pull unless you know
2872 * that the checksum difference is zero (e.g., a valid IP header)
2873 * or you are setting ip_summed to CHECKSUM_NONE.
Herbert Xucbb042f2006-03-20 22:43:56 -08002874 */
2875unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
2876{
2877 BUG_ON(len > skb->len);
2878 skb->len -= len;
2879 BUG_ON(skb->len < skb->data_len);
2880 skb_postpull_rcsum(skb, skb->data, len);
2881 return skb->data += len;
2882}
Arnaldo Carvalho de Melof94691a2006-03-20 22:47:55 -08002883EXPORT_SYMBOL_GPL(skb_pull_rcsum);
2884
Herbert Xuf4c50d92006-06-22 03:02:40 -07002885/**
2886 * skb_segment - Perform protocol segmentation on skb.
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002887 * @head_skb: buffer to segment
Herbert Xu576a30e2006-06-27 13:22:38 -07002888 * @features: features for the output path (see dev->features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07002889 *
2890 * This function performs segmentation on the given skb. It returns
Ben Hutchings4c821d72008-04-13 21:52:48 -07002891 * a pointer to the first in a list of new skbs for the segments.
2892 * In case of error it returns ERR_PTR(err).
Herbert Xuf4c50d92006-06-22 03:02:40 -07002893 */
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002894struct sk_buff *skb_segment(struct sk_buff *head_skb,
2895 netdev_features_t features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07002896{
2897 struct sk_buff *segs = NULL;
2898 struct sk_buff *tail = NULL;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02002899 struct sk_buff *list_skb = skb_shinfo(head_skb)->frag_list;
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002900 skb_frag_t *frag = skb_shinfo(head_skb)->frags;
2901 unsigned int mss = skb_shinfo(head_skb)->gso_size;
2902 unsigned int doffset = head_skb->data - skb_mac_header(head_skb);
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02002903 struct sk_buff *frag_skb = head_skb;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002904 unsigned int offset = doffset;
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002905 unsigned int tnl_hlen = skb_tnl_header_len(head_skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002906 unsigned int headroom;
2907 unsigned int len;
Pravin B Shelarec5f0612013-03-07 09:28:01 +00002908 __be16 proto;
2909 bool csum;
Michał Mirosław04ed3e72011-01-24 15:32:47 -08002910 int sg = !!(features & NETIF_F_SG);
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002911 int nfrags = skb_shinfo(head_skb)->nr_frags;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002912 int err = -ENOMEM;
2913 int i = 0;
2914 int pos;
Vlad Yasevich53d64712014-03-27 17:26:18 -04002915 int dummy;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002916
Wei-Chun Chao5882a072014-06-08 23:48:54 -07002917 __skb_push(head_skb, doffset);
Vlad Yasevich53d64712014-03-27 17:26:18 -04002918 proto = skb_network_protocol(head_skb, &dummy);
Pravin B Shelarec5f0612013-03-07 09:28:01 +00002919 if (unlikely(!proto))
2920 return ERR_PTR(-EINVAL);
2921
Tom Herbert7e2b10c2014-06-04 17:20:02 -07002922 csum = !head_skb->encap_hdr_csum &&
2923 !!can_checksum_protocol(features, proto);
2924
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002925 headroom = skb_headroom(head_skb);
2926 pos = skb_headlen(head_skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002927
2928 do {
2929 struct sk_buff *nskb;
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02002930 skb_frag_t *nskb_frag;
Herbert Xuc8884ed2006-10-29 15:59:41 -08002931 int hsize;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002932 int size;
2933
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002934 len = head_skb->len - offset;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002935 if (len > mss)
2936 len = mss;
2937
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002938 hsize = skb_headlen(head_skb) - offset;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002939 if (hsize < 0)
2940 hsize = 0;
Herbert Xuc8884ed2006-10-29 15:59:41 -08002941 if (hsize > len || !sg)
2942 hsize = len;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002943
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02002944 if (!hsize && i >= nfrags && skb_headlen(list_skb) &&
2945 (skb_headlen(list_skb) == len || sg)) {
2946 BUG_ON(skb_headlen(list_skb) > len);
Herbert Xu89319d382008-12-15 23:26:06 -08002947
Herbert Xu9d8506c2013-11-21 11:10:04 -08002948 i = 0;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02002949 nfrags = skb_shinfo(list_skb)->nr_frags;
2950 frag = skb_shinfo(list_skb)->frags;
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02002951 frag_skb = list_skb;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02002952 pos += skb_headlen(list_skb);
Herbert Xu9d8506c2013-11-21 11:10:04 -08002953
2954 while (pos < offset + len) {
2955 BUG_ON(i >= nfrags);
2956
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02002957 size = skb_frag_size(frag);
Herbert Xu9d8506c2013-11-21 11:10:04 -08002958 if (pos + size > offset + len)
2959 break;
2960
2961 i++;
2962 pos += size;
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02002963 frag++;
Herbert Xu9d8506c2013-11-21 11:10:04 -08002964 }
2965
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02002966 nskb = skb_clone(list_skb, GFP_ATOMIC);
2967 list_skb = list_skb->next;
Herbert Xu89319d382008-12-15 23:26:06 -08002968
2969 if (unlikely(!nskb))
2970 goto err;
2971
Herbert Xu9d8506c2013-11-21 11:10:04 -08002972 if (unlikely(pskb_trim(nskb, len))) {
2973 kfree_skb(nskb);
2974 goto err;
2975 }
2976
Alexander Duyckec47ea82012-05-04 14:26:56 +00002977 hsize = skb_end_offset(nskb);
Herbert Xu89319d382008-12-15 23:26:06 -08002978 if (skb_cow_head(nskb, doffset + headroom)) {
2979 kfree_skb(nskb);
2980 goto err;
2981 }
2982
Alexander Duyckec47ea82012-05-04 14:26:56 +00002983 nskb->truesize += skb_end_offset(nskb) - hsize;
Herbert Xu89319d382008-12-15 23:26:06 -08002984 skb_release_head_state(nskb);
2985 __skb_push(nskb, doffset);
2986 } else {
Mel Gormanc93bdd02012-07-31 16:44:19 -07002987 nskb = __alloc_skb(hsize + doffset + headroom,
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002988 GFP_ATOMIC, skb_alloc_rx_flag(head_skb),
Mel Gormanc93bdd02012-07-31 16:44:19 -07002989 NUMA_NO_NODE);
Herbert Xu89319d382008-12-15 23:26:06 -08002990
2991 if (unlikely(!nskb))
2992 goto err;
2993
2994 skb_reserve(nskb, headroom);
2995 __skb_put(nskb, doffset);
2996 }
Herbert Xuf4c50d92006-06-22 03:02:40 -07002997
2998 if (segs)
2999 tail->next = nskb;
3000 else
3001 segs = nskb;
3002 tail = nskb;
3003
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003004 __copy_skb_header(nskb, head_skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003005
Eric Dumazet030737b2013-10-19 11:42:54 -07003006 skb_headers_offset_update(nskb, skb_headroom(nskb) - headroom);
Vlad Yasevichfcdfe3a2014-07-31 10:33:06 -04003007 skb_reset_mac_len(nskb);
Pravin B Shelar68c33162013-02-14 14:02:41 +00003008
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003009 skb_copy_from_linear_data_offset(head_skb, -tnl_hlen,
Pravin B Shelar68c33162013-02-14 14:02:41 +00003010 nskb->data - tnl_hlen,
3011 doffset + tnl_hlen);
Herbert Xu89319d382008-12-15 23:26:06 -08003012
Herbert Xu9d8506c2013-11-21 11:10:04 -08003013 if (nskb->len == len + doffset)
Simon Horman1cdbcb72013-05-19 15:46:49 +00003014 goto perform_csum_check;
Herbert Xu89319d382008-12-15 23:26:06 -08003015
Tom Herberte585f232014-11-04 09:06:54 -08003016 if (!sg && !nskb->remcsum_offload) {
Herbert Xu6f85a122008-08-15 14:55:02 -07003017 nskb->ip_summed = CHECKSUM_NONE;
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003018 nskb->csum = skb_copy_and_csum_bits(head_skb, offset,
Herbert Xuf4c50d92006-06-22 03:02:40 -07003019 skb_put(nskb, len),
3020 len, 0);
Tom Herbert7e2b10c2014-06-04 17:20:02 -07003021 SKB_GSO_CB(nskb)->csum_start =
Tom Herbertde843722014-06-25 12:51:01 -07003022 skb_headroom(nskb) + doffset;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003023 continue;
3024 }
3025
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003026 nskb_frag = skb_shinfo(nskb)->frags;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003027
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003028 skb_copy_from_linear_data_offset(head_skb, offset,
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03003029 skb_put(nskb, hsize), hsize);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003030
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003031 skb_shinfo(nskb)->tx_flags = skb_shinfo(head_skb)->tx_flags &
3032 SKBTX_SHARED_FRAG;
Eric Dumazetcef401d2013-01-25 20:34:37 +00003033
Herbert Xu9d8506c2013-11-21 11:10:04 -08003034 while (pos < offset + len) {
3035 if (i >= nfrags) {
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003036 BUG_ON(skb_headlen(list_skb));
Herbert Xu9d8506c2013-11-21 11:10:04 -08003037
3038 i = 0;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003039 nfrags = skb_shinfo(list_skb)->nr_frags;
3040 frag = skb_shinfo(list_skb)->frags;
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02003041 frag_skb = list_skb;
Herbert Xu9d8506c2013-11-21 11:10:04 -08003042
3043 BUG_ON(!nfrags);
3044
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003045 list_skb = list_skb->next;
Herbert Xu9d8506c2013-11-21 11:10:04 -08003046 }
3047
3048 if (unlikely(skb_shinfo(nskb)->nr_frags >=
3049 MAX_SKB_FRAGS)) {
3050 net_warn_ratelimited(
3051 "skb_segment: too many frags: %u %u\n",
3052 pos, mss);
3053 goto err;
3054 }
3055
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02003056 if (unlikely(skb_orphan_frags(frag_skb, GFP_ATOMIC)))
3057 goto err;
3058
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02003059 *nskb_frag = *frag;
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003060 __skb_frag_ref(nskb_frag);
3061 size = skb_frag_size(nskb_frag);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003062
3063 if (pos < offset) {
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003064 nskb_frag->page_offset += offset - pos;
3065 skb_frag_size_sub(nskb_frag, offset - pos);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003066 }
3067
Herbert Xu89319d382008-12-15 23:26:06 -08003068 skb_shinfo(nskb)->nr_frags++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003069
3070 if (pos + size <= offset + len) {
3071 i++;
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02003072 frag++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003073 pos += size;
3074 } else {
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003075 skb_frag_size_sub(nskb_frag, pos + size - (offset + len));
Herbert Xu89319d382008-12-15 23:26:06 -08003076 goto skip_fraglist;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003077 }
3078
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003079 nskb_frag++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003080 }
3081
Herbert Xu89319d382008-12-15 23:26:06 -08003082skip_fraglist:
Herbert Xuf4c50d92006-06-22 03:02:40 -07003083 nskb->data_len = len - hsize;
3084 nskb->len += nskb->data_len;
3085 nskb->truesize += nskb->data_len;
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003086
Simon Horman1cdbcb72013-05-19 15:46:49 +00003087perform_csum_check:
Tom Herberte585f232014-11-04 09:06:54 -08003088 if (!csum && !nskb->remcsum_offload) {
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003089 nskb->csum = skb_checksum(nskb, doffset,
3090 nskb->len - doffset, 0);
3091 nskb->ip_summed = CHECKSUM_NONE;
Tom Herbert7e2b10c2014-06-04 17:20:02 -07003092 SKB_GSO_CB(nskb)->csum_start =
3093 skb_headroom(nskb) + doffset;
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003094 }
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003095 } while ((offset += len) < head_skb->len);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003096
Eric Dumazetbec3cfd2014-10-03 20:59:19 -07003097 /* Some callers want to get the end of the list.
3098 * Put it in segs->prev to avoid walking the list.
3099 * (see validate_xmit_skb_list() for example)
3100 */
3101 segs->prev = tail;
Toshiaki Makita432c8562014-10-27 10:30:51 -07003102
3103 /* Following permits correct backpressure, for protocols
3104 * using skb_set_owner_w().
3105 * Idea is to tranfert ownership from head_skb to last segment.
3106 */
3107 if (head_skb->destructor == sock_wfree) {
3108 swap(tail->truesize, head_skb->truesize);
3109 swap(tail->destructor, head_skb->destructor);
3110 swap(tail->sk, head_skb->sk);
3111 }
Herbert Xuf4c50d92006-06-22 03:02:40 -07003112 return segs;
3113
3114err:
Eric Dumazet289dccb2013-12-20 14:29:08 -08003115 kfree_skb_list(segs);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003116 return ERR_PTR(err);
3117}
Herbert Xuf4c50d92006-06-22 03:02:40 -07003118EXPORT_SYMBOL_GPL(skb_segment);
3119
Herbert Xu71d93b32008-12-15 23:42:33 -08003120int skb_gro_receive(struct sk_buff **head, struct sk_buff *skb)
3121{
Eric Dumazet8a291112013-10-08 09:02:23 -07003122 struct skb_shared_info *pinfo, *skbinfo = skb_shinfo(skb);
Herbert Xu67147ba2009-05-26 18:50:22 +00003123 unsigned int offset = skb_gro_offset(skb);
3124 unsigned int headlen = skb_headlen(skb);
Eric Dumazet8a291112013-10-08 09:02:23 -07003125 struct sk_buff *nskb, *lp, *p = *head;
3126 unsigned int len = skb_gro_len(skb);
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003127 unsigned int delta_truesize;
Eric Dumazet8a291112013-10-08 09:02:23 -07003128 unsigned int headroom;
Herbert Xu71d93b32008-12-15 23:42:33 -08003129
Eric Dumazet8a291112013-10-08 09:02:23 -07003130 if (unlikely(p->len + len >= 65536))
Herbert Xu71d93b32008-12-15 23:42:33 -08003131 return -E2BIG;
3132
Eric Dumazet29e98242014-05-16 11:34:37 -07003133 lp = NAPI_GRO_CB(p)->last;
Eric Dumazet8a291112013-10-08 09:02:23 -07003134 pinfo = skb_shinfo(lp);
3135
3136 if (headlen <= offset) {
Herbert Xu42da6992009-05-26 18:50:19 +00003137 skb_frag_t *frag;
Herbert Xu66e92fc2009-05-26 18:50:32 +00003138 skb_frag_t *frag2;
Herbert Xu9aaa1562009-05-26 18:50:33 +00003139 int i = skbinfo->nr_frags;
3140 int nr_frags = pinfo->nr_frags + i;
Herbert Xu42da6992009-05-26 18:50:19 +00003141
Herbert Xu66e92fc2009-05-26 18:50:32 +00003142 if (nr_frags > MAX_SKB_FRAGS)
Eric Dumazet8a291112013-10-08 09:02:23 -07003143 goto merge;
Herbert Xu81705ad2009-01-29 14:19:51 +00003144
Eric Dumazet8a291112013-10-08 09:02:23 -07003145 offset -= headlen;
Herbert Xu9aaa1562009-05-26 18:50:33 +00003146 pinfo->nr_frags = nr_frags;
3147 skbinfo->nr_frags = 0;
Herbert Xuf5572062009-01-14 20:40:03 -08003148
Herbert Xu9aaa1562009-05-26 18:50:33 +00003149 frag = pinfo->frags + nr_frags;
3150 frag2 = skbinfo->frags + i;
Herbert Xu66e92fc2009-05-26 18:50:32 +00003151 do {
3152 *--frag = *--frag2;
3153 } while (--i);
3154
3155 frag->page_offset += offset;
Eric Dumazet9e903e02011-10-18 21:00:24 +00003156 skb_frag_size_sub(frag, offset);
Herbert Xu66e92fc2009-05-26 18:50:32 +00003157
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003158 /* all fragments truesize : remove (head size + sk_buff) */
Alexander Duyckec47ea82012-05-04 14:26:56 +00003159 delta_truesize = skb->truesize -
3160 SKB_TRUESIZE(skb_end_offset(skb));
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003161
Herbert Xuf5572062009-01-14 20:40:03 -08003162 skb->truesize -= skb->data_len;
3163 skb->len -= skb->data_len;
3164 skb->data_len = 0;
3165
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003166 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE;
Herbert Xu5d38a072009-01-04 16:13:40 -08003167 goto done;
Eric Dumazetd7e88832012-04-30 08:10:34 +00003168 } else if (skb->head_frag) {
3169 int nr_frags = pinfo->nr_frags;
3170 skb_frag_t *frag = pinfo->frags + nr_frags;
3171 struct page *page = virt_to_head_page(skb->head);
3172 unsigned int first_size = headlen - offset;
3173 unsigned int first_offset;
3174
3175 if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS)
Eric Dumazet8a291112013-10-08 09:02:23 -07003176 goto merge;
Eric Dumazetd7e88832012-04-30 08:10:34 +00003177
3178 first_offset = skb->data -
3179 (unsigned char *)page_address(page) +
3180 offset;
3181
3182 pinfo->nr_frags = nr_frags + 1 + skbinfo->nr_frags;
3183
3184 frag->page.p = page;
3185 frag->page_offset = first_offset;
3186 skb_frag_size_set(frag, first_size);
3187
3188 memcpy(frag + 1, skbinfo->frags, sizeof(*frag) * skbinfo->nr_frags);
3189 /* We dont need to clear skbinfo->nr_frags here */
3190
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003191 delta_truesize = skb->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
Eric Dumazetd7e88832012-04-30 08:10:34 +00003192 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD;
3193 goto done;
Eric Dumazet8a291112013-10-08 09:02:23 -07003194 }
Eric Dumazet73d3fe62014-09-29 10:34:29 -07003195 /* switch back to head shinfo */
3196 pinfo = skb_shinfo(p);
3197
Eric Dumazet8a291112013-10-08 09:02:23 -07003198 if (pinfo->frag_list)
3199 goto merge;
3200 if (skb_gro_len(p) != pinfo->gso_size)
Herbert Xu69c0cab2009-11-17 05:18:18 -08003201 return -E2BIG;
Herbert Xu71d93b32008-12-15 23:42:33 -08003202
3203 headroom = skb_headroom(p);
Eric Dumazet3d3be432010-09-01 00:50:51 +00003204 nskb = alloc_skb(headroom + skb_gro_offset(p), GFP_ATOMIC);
Herbert Xu71d93b32008-12-15 23:42:33 -08003205 if (unlikely(!nskb))
3206 return -ENOMEM;
3207
3208 __copy_skb_header(nskb, p);
3209 nskb->mac_len = p->mac_len;
3210
3211 skb_reserve(nskb, headroom);
Herbert Xu86911732009-01-29 14:19:50 +00003212 __skb_put(nskb, skb_gro_offset(p));
Herbert Xu71d93b32008-12-15 23:42:33 -08003213
Herbert Xu86911732009-01-29 14:19:50 +00003214 skb_set_mac_header(nskb, skb_mac_header(p) - p->data);
Herbert Xu71d93b32008-12-15 23:42:33 -08003215 skb_set_network_header(nskb, skb_network_offset(p));
3216 skb_set_transport_header(nskb, skb_transport_offset(p));
3217
Herbert Xu86911732009-01-29 14:19:50 +00003218 __skb_pull(p, skb_gro_offset(p));
3219 memcpy(skb_mac_header(nskb), skb_mac_header(p),
3220 p->data - skb_mac_header(p));
Herbert Xu71d93b32008-12-15 23:42:33 -08003221
Herbert Xu71d93b32008-12-15 23:42:33 -08003222 skb_shinfo(nskb)->frag_list = p;
Herbert Xu9aaa1562009-05-26 18:50:33 +00003223 skb_shinfo(nskb)->gso_size = pinfo->gso_size;
Herbert Xu622e0ca2010-05-20 23:07:56 -07003224 pinfo->gso_size = 0;
Eric Dumazetf4a775d2014-09-22 16:29:32 -07003225 __skb_header_release(p);
Eric Dumazetc3c7c252012-12-06 13:54:59 +00003226 NAPI_GRO_CB(nskb)->last = p;
Herbert Xu71d93b32008-12-15 23:42:33 -08003227
3228 nskb->data_len += p->len;
Eric Dumazetde8261c2012-02-13 04:09:20 +00003229 nskb->truesize += p->truesize;
Herbert Xu71d93b32008-12-15 23:42:33 -08003230 nskb->len += p->len;
3231
3232 *head = nskb;
3233 nskb->next = p->next;
3234 p->next = NULL;
3235
3236 p = nskb;
3237
3238merge:
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003239 delta_truesize = skb->truesize;
Herbert Xu67147ba2009-05-26 18:50:22 +00003240 if (offset > headlen) {
Michal Schmidtd1dc7ab2011-01-24 12:08:48 +00003241 unsigned int eat = offset - headlen;
3242
3243 skbinfo->frags[0].page_offset += eat;
Eric Dumazet9e903e02011-10-18 21:00:24 +00003244 skb_frag_size_sub(&skbinfo->frags[0], eat);
Michal Schmidtd1dc7ab2011-01-24 12:08:48 +00003245 skb->data_len -= eat;
3246 skb->len -= eat;
Herbert Xu67147ba2009-05-26 18:50:22 +00003247 offset = headlen;
Herbert Xu56035022009-02-05 21:26:52 -08003248 }
3249
Herbert Xu67147ba2009-05-26 18:50:22 +00003250 __skb_pull(skb, offset);
Herbert Xu56035022009-02-05 21:26:52 -08003251
Eric Dumazet29e98242014-05-16 11:34:37 -07003252 if (NAPI_GRO_CB(p)->last == p)
Eric Dumazet8a291112013-10-08 09:02:23 -07003253 skb_shinfo(p)->frag_list = skb;
3254 else
3255 NAPI_GRO_CB(p)->last->next = skb;
Eric Dumazetc3c7c252012-12-06 13:54:59 +00003256 NAPI_GRO_CB(p)->last = skb;
Eric Dumazetf4a775d2014-09-22 16:29:32 -07003257 __skb_header_release(skb);
Eric Dumazet8a291112013-10-08 09:02:23 -07003258 lp = p;
Herbert Xu71d93b32008-12-15 23:42:33 -08003259
Herbert Xu5d38a072009-01-04 16:13:40 -08003260done:
3261 NAPI_GRO_CB(p)->count++;
Herbert Xu37fe4732009-01-17 19:48:13 +00003262 p->data_len += len;
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003263 p->truesize += delta_truesize;
Herbert Xu37fe4732009-01-17 19:48:13 +00003264 p->len += len;
Eric Dumazet8a291112013-10-08 09:02:23 -07003265 if (lp != p) {
3266 lp->data_len += len;
3267 lp->truesize += delta_truesize;
3268 lp->len += len;
3269 }
Herbert Xu71d93b32008-12-15 23:42:33 -08003270 NAPI_GRO_CB(skb)->same_flow = 1;
3271 return 0;
3272}
Herbert Xu71d93b32008-12-15 23:42:33 -08003273
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274void __init skb_init(void)
3275{
3276 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
3277 sizeof(struct sk_buff),
3278 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07003279 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09003280 NULL);
David S. Millerd179cd12005-08-17 14:57:30 -07003281 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
Eric Dumazetd0bf4a92014-09-29 13:29:15 -07003282 sizeof(struct sk_buff_fclones),
David S. Millerd179cd12005-08-17 14:57:30 -07003283 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07003284 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09003285 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286}
3287
David Howells716ea3a2007-04-02 20:19:53 -07003288/**
3289 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
3290 * @skb: Socket buffer containing the buffers to be mapped
3291 * @sg: The scatter-gather list to map into
3292 * @offset: The offset into the buffer's contents to start mapping
3293 * @len: Length of buffer space to be mapped
3294 *
3295 * Fill the specified scatter-gather list with mappings/pointers into a
3296 * region of the buffer space attached to a socket buffer.
3297 */
David S. Miller51c739d2007-10-30 21:29:29 -07003298static int
3299__skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
David Howells716ea3a2007-04-02 20:19:53 -07003300{
David S. Miller1a028e52007-04-27 15:21:23 -07003301 int start = skb_headlen(skb);
3302 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07003303 struct sk_buff *frag_iter;
David Howells716ea3a2007-04-02 20:19:53 -07003304 int elt = 0;
3305
3306 if (copy > 0) {
3307 if (copy > len)
3308 copy = len;
Jens Axboe642f1492007-10-24 11:20:47 +02003309 sg_set_buf(sg, skb->data + offset, copy);
David Howells716ea3a2007-04-02 20:19:53 -07003310 elt++;
3311 if ((len -= copy) == 0)
3312 return elt;
3313 offset += copy;
3314 }
3315
3316 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07003317 int end;
David Howells716ea3a2007-04-02 20:19:53 -07003318
Ilpo Järvinen547b7922008-07-25 21:43:18 -07003319 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07003320
Eric Dumazet9e903e02011-10-18 21:00:24 +00003321 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
David Howells716ea3a2007-04-02 20:19:53 -07003322 if ((copy = end - offset) > 0) {
3323 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
3324
3325 if (copy > len)
3326 copy = len;
Ian Campbellea2ab692011-08-22 23:44:58 +00003327 sg_set_page(&sg[elt], skb_frag_page(frag), copy,
Jens Axboe642f1492007-10-24 11:20:47 +02003328 frag->page_offset+offset-start);
David Howells716ea3a2007-04-02 20:19:53 -07003329 elt++;
3330 if (!(len -= copy))
3331 return elt;
3332 offset += copy;
3333 }
David S. Miller1a028e52007-04-27 15:21:23 -07003334 start = end;
David Howells716ea3a2007-04-02 20:19:53 -07003335 }
3336
David S. Millerfbb398a2009-06-09 00:18:59 -07003337 skb_walk_frags(skb, frag_iter) {
3338 int end;
David Howells716ea3a2007-04-02 20:19:53 -07003339
David S. Millerfbb398a2009-06-09 00:18:59 -07003340 WARN_ON(start > offset + len);
David Howells716ea3a2007-04-02 20:19:53 -07003341
David S. Millerfbb398a2009-06-09 00:18:59 -07003342 end = start + frag_iter->len;
3343 if ((copy = end - offset) > 0) {
3344 if (copy > len)
3345 copy = len;
3346 elt += __skb_to_sgvec(frag_iter, sg+elt, offset - start,
3347 copy);
3348 if ((len -= copy) == 0)
3349 return elt;
3350 offset += copy;
David Howells716ea3a2007-04-02 20:19:53 -07003351 }
David S. Millerfbb398a2009-06-09 00:18:59 -07003352 start = end;
David Howells716ea3a2007-04-02 20:19:53 -07003353 }
3354 BUG_ON(len);
3355 return elt;
3356}
3357
Fan Du25a91d82014-01-18 09:54:23 +08003358/* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given
3359 * sglist without mark the sg which contain last skb data as the end.
3360 * So the caller can mannipulate sg list as will when padding new data after
3361 * the first call without calling sg_unmark_end to expend sg list.
3362 *
3363 * Scenario to use skb_to_sgvec_nomark:
3364 * 1. sg_init_table
3365 * 2. skb_to_sgvec_nomark(payload1)
3366 * 3. skb_to_sgvec_nomark(payload2)
3367 *
3368 * This is equivalent to:
3369 * 1. sg_init_table
3370 * 2. skb_to_sgvec(payload1)
3371 * 3. sg_unmark_end
3372 * 4. skb_to_sgvec(payload2)
3373 *
3374 * When mapping mutilple payload conditionally, skb_to_sgvec_nomark
3375 * is more preferable.
3376 */
3377int skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg,
3378 int offset, int len)
3379{
3380 return __skb_to_sgvec(skb, sg, offset, len);
3381}
3382EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark);
3383
David S. Miller51c739d2007-10-30 21:29:29 -07003384int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
3385{
3386 int nsg = __skb_to_sgvec(skb, sg, offset, len);
3387
Jens Axboec46f2332007-10-31 12:06:37 +01003388 sg_mark_end(&sg[nsg - 1]);
David S. Miller51c739d2007-10-30 21:29:29 -07003389
3390 return nsg;
3391}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003392EXPORT_SYMBOL_GPL(skb_to_sgvec);
David S. Miller51c739d2007-10-30 21:29:29 -07003393
David Howells716ea3a2007-04-02 20:19:53 -07003394/**
3395 * skb_cow_data - Check that a socket buffer's data buffers are writable
3396 * @skb: The socket buffer to check.
3397 * @tailbits: Amount of trailing space to be added
3398 * @trailer: Returned pointer to the skb where the @tailbits space begins
3399 *
3400 * Make sure that the data buffers attached to a socket buffer are
3401 * writable. If they are not, private copies are made of the data buffers
3402 * and the socket buffer is set to use these instead.
3403 *
3404 * If @tailbits is given, make sure that there is space to write @tailbits
3405 * bytes of data beyond current end of socket buffer. @trailer will be
3406 * set to point to the skb in which this space begins.
3407 *
3408 * The number of scatterlist elements required to completely map the
3409 * COW'd and extended socket buffer will be returned.
3410 */
3411int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
3412{
3413 int copyflag;
3414 int elt;
3415 struct sk_buff *skb1, **skb_p;
3416
3417 /* If skb is cloned or its head is paged, reallocate
3418 * head pulling out all the pages (pages are considered not writable
3419 * at the moment even if they are anonymous).
3420 */
3421 if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
3422 __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
3423 return -ENOMEM;
3424
3425 /* Easy case. Most of packets will go this way. */
David S. Miller21dc3302010-08-23 00:13:46 -07003426 if (!skb_has_frag_list(skb)) {
David Howells716ea3a2007-04-02 20:19:53 -07003427 /* A little of trouble, not enough of space for trailer.
3428 * This should not happen, when stack is tuned to generate
3429 * good frames. OK, on miss we reallocate and reserve even more
3430 * space, 128 bytes is fair. */
3431
3432 if (skb_tailroom(skb) < tailbits &&
3433 pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
3434 return -ENOMEM;
3435
3436 /* Voila! */
3437 *trailer = skb;
3438 return 1;
3439 }
3440
3441 /* Misery. We are in troubles, going to mincer fragments... */
3442
3443 elt = 1;
3444 skb_p = &skb_shinfo(skb)->frag_list;
3445 copyflag = 0;
3446
3447 while ((skb1 = *skb_p) != NULL) {
3448 int ntail = 0;
3449
3450 /* The fragment is partially pulled by someone,
3451 * this can happen on input. Copy it and everything
3452 * after it. */
3453
3454 if (skb_shared(skb1))
3455 copyflag = 1;
3456
3457 /* If the skb is the last, worry about trailer. */
3458
3459 if (skb1->next == NULL && tailbits) {
3460 if (skb_shinfo(skb1)->nr_frags ||
David S. Miller21dc3302010-08-23 00:13:46 -07003461 skb_has_frag_list(skb1) ||
David Howells716ea3a2007-04-02 20:19:53 -07003462 skb_tailroom(skb1) < tailbits)
3463 ntail = tailbits + 128;
3464 }
3465
3466 if (copyflag ||
3467 skb_cloned(skb1) ||
3468 ntail ||
3469 skb_shinfo(skb1)->nr_frags ||
David S. Miller21dc3302010-08-23 00:13:46 -07003470 skb_has_frag_list(skb1)) {
David Howells716ea3a2007-04-02 20:19:53 -07003471 struct sk_buff *skb2;
3472
3473 /* Fuck, we are miserable poor guys... */
3474 if (ntail == 0)
3475 skb2 = skb_copy(skb1, GFP_ATOMIC);
3476 else
3477 skb2 = skb_copy_expand(skb1,
3478 skb_headroom(skb1),
3479 ntail,
3480 GFP_ATOMIC);
3481 if (unlikely(skb2 == NULL))
3482 return -ENOMEM;
3483
3484 if (skb1->sk)
3485 skb_set_owner_w(skb2, skb1->sk);
3486
3487 /* Looking around. Are we still alive?
3488 * OK, link new skb, drop old one */
3489
3490 skb2->next = skb1->next;
3491 *skb_p = skb2;
3492 kfree_skb(skb1);
3493 skb1 = skb2;
3494 }
3495 elt++;
3496 *trailer = skb1;
3497 skb_p = &skb1->next;
3498 }
3499
3500 return elt;
3501}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003502EXPORT_SYMBOL_GPL(skb_cow_data);
David Howells716ea3a2007-04-02 20:19:53 -07003503
Eric Dumazetb1faf562010-05-31 23:44:05 -07003504static void sock_rmem_free(struct sk_buff *skb)
3505{
3506 struct sock *sk = skb->sk;
3507
3508 atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
3509}
3510
3511/*
3512 * Note: We dont mem charge error packets (no sk_forward_alloc changes)
3513 */
3514int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb)
3515{
3516 if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
Eric Dumazet95c96172012-04-15 05:58:06 +00003517 (unsigned int)sk->sk_rcvbuf)
Eric Dumazetb1faf562010-05-31 23:44:05 -07003518 return -ENOMEM;
3519
3520 skb_orphan(skb);
3521 skb->sk = sk;
3522 skb->destructor = sock_rmem_free;
3523 atomic_add(skb->truesize, &sk->sk_rmem_alloc);
3524
Eric Dumazetabb57ea2011-05-18 02:21:31 -04003525 /* before exiting rcu section, make sure dst is refcounted */
3526 skb_dst_force(skb);
3527
Eric Dumazetb1faf562010-05-31 23:44:05 -07003528 skb_queue_tail(&sk->sk_error_queue, skb);
3529 if (!sock_flag(sk, SOCK_DEAD))
David S. Miller676d2362014-04-11 16:15:36 -04003530 sk->sk_data_ready(sk);
Eric Dumazetb1faf562010-05-31 23:44:05 -07003531 return 0;
3532}
3533EXPORT_SYMBOL(sock_queue_err_skb);
3534
Willem de Bruijn364a9e92014-08-31 21:30:27 -04003535struct sk_buff *sock_dequeue_err_skb(struct sock *sk)
3536{
3537 struct sk_buff_head *q = &sk->sk_error_queue;
3538 struct sk_buff *skb, *skb_next;
3539 int err = 0;
3540
3541 spin_lock_bh(&q->lock);
3542 skb = __skb_dequeue(q);
3543 if (skb && (skb_next = skb_peek(q)))
3544 err = SKB_EXT_ERR(skb_next)->ee.ee_errno;
3545 spin_unlock_bh(&q->lock);
3546
3547 sk->sk_err = err;
3548 if (err)
3549 sk->sk_error_report(sk);
3550
3551 return skb;
3552}
3553EXPORT_SYMBOL(sock_dequeue_err_skb);
3554
Alexander Duyckcab41c42014-09-10 18:05:26 -04003555/**
3556 * skb_clone_sk - create clone of skb, and take reference to socket
3557 * @skb: the skb to clone
3558 *
3559 * This function creates a clone of a buffer that holds a reference on
3560 * sk_refcnt. Buffers created via this function are meant to be
3561 * returned using sock_queue_err_skb, or free via kfree_skb.
3562 *
3563 * When passing buffers allocated with this function to sock_queue_err_skb
3564 * it is necessary to wrap the call with sock_hold/sock_put in order to
3565 * prevent the socket from being released prior to being enqueued on
3566 * the sk_error_queue.
3567 */
Alexander Duyck62bccb82014-09-04 13:31:35 -04003568struct sk_buff *skb_clone_sk(struct sk_buff *skb)
3569{
3570 struct sock *sk = skb->sk;
3571 struct sk_buff *clone;
3572
3573 if (!sk || !atomic_inc_not_zero(&sk->sk_refcnt))
3574 return NULL;
3575
3576 clone = skb_clone(skb, GFP_ATOMIC);
3577 if (!clone) {
3578 sock_put(sk);
3579 return NULL;
3580 }
3581
3582 clone->sk = sk;
3583 clone->destructor = sock_efree;
3584
3585 return clone;
3586}
3587EXPORT_SYMBOL(skb_clone_sk);
3588
Alexander Duyck37846ef2014-09-04 13:31:10 -04003589static void __skb_complete_tx_timestamp(struct sk_buff *skb,
3590 struct sock *sk,
3591 int tstype)
Patrick Ohlyac45f602009-02-12 05:03:37 +00003592{
Patrick Ohlyac45f602009-02-12 05:03:37 +00003593 struct sock_exterr_skb *serr;
Patrick Ohlyac45f602009-02-12 05:03:37 +00003594 int err;
3595
Patrick Ohlyac45f602009-02-12 05:03:37 +00003596 serr = SKB_EXT_ERR(skb);
3597 memset(serr, 0, sizeof(*serr));
3598 serr->ee.ee_errno = ENOMSG;
3599 serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
Willem de Bruijne7fd2882014-08-04 22:11:48 -04003600 serr->ee.ee_info = tstype;
Willem de Bruijn4ed2d762014-08-04 22:11:49 -04003601 if (sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) {
Willem de Bruijn09c2d252014-08-04 22:11:47 -04003602 serr->ee.ee_data = skb_shinfo(skb)->tskey;
Willem de Bruijn4ed2d762014-08-04 22:11:49 -04003603 if (sk->sk_protocol == IPPROTO_TCP)
3604 serr->ee.ee_data -= sk->sk_tskey;
3605 }
Eric Dumazet29030372010-05-29 00:20:48 -07003606
Patrick Ohlyac45f602009-02-12 05:03:37 +00003607 err = sock_queue_err_skb(sk, skb);
Eric Dumazet29030372010-05-29 00:20:48 -07003608
Patrick Ohlyac45f602009-02-12 05:03:37 +00003609 if (err)
3610 kfree_skb(skb);
3611}
Alexander Duyck37846ef2014-09-04 13:31:10 -04003612
3613void skb_complete_tx_timestamp(struct sk_buff *skb,
3614 struct skb_shared_hwtstamps *hwtstamps)
3615{
3616 struct sock *sk = skb->sk;
3617
Alexander Duyck62bccb82014-09-04 13:31:35 -04003618 /* take a reference to prevent skb_orphan() from freeing the socket */
3619 sock_hold(sk);
Alexander Duyck37846ef2014-09-04 13:31:10 -04003620
Alexander Duyck62bccb82014-09-04 13:31:35 -04003621 *skb_hwtstamps(skb) = *hwtstamps;
3622 __skb_complete_tx_timestamp(skb, sk, SCM_TSTAMP_SND);
Alexander Duyck37846ef2014-09-04 13:31:10 -04003623
3624 sock_put(sk);
3625}
3626EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp);
3627
3628void __skb_tstamp_tx(struct sk_buff *orig_skb,
3629 struct skb_shared_hwtstamps *hwtstamps,
3630 struct sock *sk, int tstype)
3631{
3632 struct sk_buff *skb;
3633
3634 if (!sk)
3635 return;
3636
3637 if (hwtstamps)
3638 *skb_hwtstamps(orig_skb) = *hwtstamps;
3639 else
3640 orig_skb->tstamp = ktime_get_real();
3641
3642 skb = skb_clone(orig_skb, GFP_ATOMIC);
3643 if (!skb)
3644 return;
3645
3646 __skb_complete_tx_timestamp(skb, sk, tstype);
3647}
Willem de Bruijne7fd2882014-08-04 22:11:48 -04003648EXPORT_SYMBOL_GPL(__skb_tstamp_tx);
3649
3650void skb_tstamp_tx(struct sk_buff *orig_skb,
3651 struct skb_shared_hwtstamps *hwtstamps)
3652{
3653 return __skb_tstamp_tx(orig_skb, hwtstamps, orig_skb->sk,
3654 SCM_TSTAMP_SND);
3655}
Patrick Ohlyac45f602009-02-12 05:03:37 +00003656EXPORT_SYMBOL_GPL(skb_tstamp_tx);
3657
Johannes Berg6e3e9392011-11-09 10:15:42 +01003658void skb_complete_wifi_ack(struct sk_buff *skb, bool acked)
3659{
3660 struct sock *sk = skb->sk;
3661 struct sock_exterr_skb *serr;
3662 int err;
3663
3664 skb->wifi_acked_valid = 1;
3665 skb->wifi_acked = acked;
3666
3667 serr = SKB_EXT_ERR(skb);
3668 memset(serr, 0, sizeof(*serr));
3669 serr->ee.ee_errno = ENOMSG;
3670 serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS;
3671
Alexander Duyckbf7fa552014-09-10 18:05:42 -04003672 /* take a reference to prevent skb_orphan() from freeing the socket */
3673 sock_hold(sk);
3674
Johannes Berg6e3e9392011-11-09 10:15:42 +01003675 err = sock_queue_err_skb(sk, skb);
3676 if (err)
3677 kfree_skb(skb);
Alexander Duyckbf7fa552014-09-10 18:05:42 -04003678
3679 sock_put(sk);
Johannes Berg6e3e9392011-11-09 10:15:42 +01003680}
3681EXPORT_SYMBOL_GPL(skb_complete_wifi_ack);
3682
Patrick Ohlyac45f602009-02-12 05:03:37 +00003683
Rusty Russellf35d9d82008-02-04 23:49:54 -05003684/**
3685 * skb_partial_csum_set - set up and verify partial csum values for packet
3686 * @skb: the skb to set
3687 * @start: the number of bytes after skb->data to start checksumming.
3688 * @off: the offset from start to place the checksum.
3689 *
3690 * For untrusted partially-checksummed packets, we need to make sure the values
3691 * for skb->csum_start and skb->csum_offset are valid so we don't oops.
3692 *
3693 * This function checks and sets those values and skb->ip_summed: if this
3694 * returns false you should drop the packet.
3695 */
3696bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
3697{
Herbert Xu5ff8dda2009-06-04 01:22:01 +00003698 if (unlikely(start > skb_headlen(skb)) ||
3699 unlikely((int)start + off > skb_headlen(skb) - 2)) {
Joe Perchese87cc472012-05-13 21:56:26 +00003700 net_warn_ratelimited("bad partial csum: csum=%u/%u len=%u\n",
3701 start, off, skb_headlen(skb));
Rusty Russellf35d9d82008-02-04 23:49:54 -05003702 return false;
3703 }
3704 skb->ip_summed = CHECKSUM_PARTIAL;
3705 skb->csum_start = skb_headroom(skb) + start;
3706 skb->csum_offset = off;
Jason Wange5d5dec2013-03-26 23:11:20 +00003707 skb_set_transport_header(skb, start);
Rusty Russellf35d9d82008-02-04 23:49:54 -05003708 return true;
3709}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003710EXPORT_SYMBOL_GPL(skb_partial_csum_set);
Rusty Russellf35d9d82008-02-04 23:49:54 -05003711
Paul Durranted1f50c2014-01-09 10:02:46 +00003712static int skb_maybe_pull_tail(struct sk_buff *skb, unsigned int len,
3713 unsigned int max)
3714{
3715 if (skb_headlen(skb) >= len)
3716 return 0;
3717
3718 /* If we need to pullup then pullup to the max, so we
3719 * won't need to do it again.
3720 */
3721 if (max > skb->len)
3722 max = skb->len;
3723
3724 if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL)
3725 return -ENOMEM;
3726
3727 if (skb_headlen(skb) < len)
3728 return -EPROTO;
3729
3730 return 0;
3731}
3732
Jan Beulichf9708b42014-03-11 13:56:05 +00003733#define MAX_TCP_HDR_LEN (15 * 4)
3734
3735static __sum16 *skb_checksum_setup_ip(struct sk_buff *skb,
3736 typeof(IPPROTO_IP) proto,
3737 unsigned int off)
3738{
3739 switch (proto) {
3740 int err;
3741
3742 case IPPROTO_TCP:
3743 err = skb_maybe_pull_tail(skb, off + sizeof(struct tcphdr),
3744 off + MAX_TCP_HDR_LEN);
3745 if (!err && !skb_partial_csum_set(skb, off,
3746 offsetof(struct tcphdr,
3747 check)))
3748 err = -EPROTO;
3749 return err ? ERR_PTR(err) : &tcp_hdr(skb)->check;
3750
3751 case IPPROTO_UDP:
3752 err = skb_maybe_pull_tail(skb, off + sizeof(struct udphdr),
3753 off + sizeof(struct udphdr));
3754 if (!err && !skb_partial_csum_set(skb, off,
3755 offsetof(struct udphdr,
3756 check)))
3757 err = -EPROTO;
3758 return err ? ERR_PTR(err) : &udp_hdr(skb)->check;
3759 }
3760
3761 return ERR_PTR(-EPROTO);
3762}
3763
Paul Durranted1f50c2014-01-09 10:02:46 +00003764/* This value should be large enough to cover a tagged ethernet header plus
3765 * maximally sized IP and TCP or UDP headers.
3766 */
3767#define MAX_IP_HDR_LEN 128
3768
Jan Beulichf9708b42014-03-11 13:56:05 +00003769static int skb_checksum_setup_ipv4(struct sk_buff *skb, bool recalculate)
Paul Durranted1f50c2014-01-09 10:02:46 +00003770{
3771 unsigned int off;
3772 bool fragment;
Jan Beulichf9708b42014-03-11 13:56:05 +00003773 __sum16 *csum;
Paul Durranted1f50c2014-01-09 10:02:46 +00003774 int err;
3775
3776 fragment = false;
3777
3778 err = skb_maybe_pull_tail(skb,
3779 sizeof(struct iphdr),
3780 MAX_IP_HDR_LEN);
3781 if (err < 0)
3782 goto out;
3783
3784 if (ip_hdr(skb)->frag_off & htons(IP_OFFSET | IP_MF))
3785 fragment = true;
3786
3787 off = ip_hdrlen(skb);
3788
3789 err = -EPROTO;
3790
3791 if (fragment)
3792 goto out;
3793
Jan Beulichf9708b42014-03-11 13:56:05 +00003794 csum = skb_checksum_setup_ip(skb, ip_hdr(skb)->protocol, off);
3795 if (IS_ERR(csum))
3796 return PTR_ERR(csum);
Paul Durranted1f50c2014-01-09 10:02:46 +00003797
Jan Beulichf9708b42014-03-11 13:56:05 +00003798 if (recalculate)
3799 *csum = ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
3800 ip_hdr(skb)->daddr,
3801 skb->len - off,
3802 ip_hdr(skb)->protocol, 0);
Paul Durranted1f50c2014-01-09 10:02:46 +00003803 err = 0;
3804
3805out:
3806 return err;
3807}
3808
3809/* This value should be large enough to cover a tagged ethernet header plus
3810 * an IPv6 header, all options, and a maximal TCP or UDP header.
3811 */
3812#define MAX_IPV6_HDR_LEN 256
3813
3814#define OPT_HDR(type, skb, off) \
3815 (type *)(skb_network_header(skb) + (off))
3816
3817static int skb_checksum_setup_ipv6(struct sk_buff *skb, bool recalculate)
3818{
3819 int err;
3820 u8 nexthdr;
3821 unsigned int off;
3822 unsigned int len;
3823 bool fragment;
3824 bool done;
Jan Beulichf9708b42014-03-11 13:56:05 +00003825 __sum16 *csum;
Paul Durranted1f50c2014-01-09 10:02:46 +00003826
3827 fragment = false;
3828 done = false;
3829
3830 off = sizeof(struct ipv6hdr);
3831
3832 err = skb_maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN);
3833 if (err < 0)
3834 goto out;
3835
3836 nexthdr = ipv6_hdr(skb)->nexthdr;
3837
3838 len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len);
3839 while (off <= len && !done) {
3840 switch (nexthdr) {
3841 case IPPROTO_DSTOPTS:
3842 case IPPROTO_HOPOPTS:
3843 case IPPROTO_ROUTING: {
3844 struct ipv6_opt_hdr *hp;
3845
3846 err = skb_maybe_pull_tail(skb,
3847 off +
3848 sizeof(struct ipv6_opt_hdr),
3849 MAX_IPV6_HDR_LEN);
3850 if (err < 0)
3851 goto out;
3852
3853 hp = OPT_HDR(struct ipv6_opt_hdr, skb, off);
3854 nexthdr = hp->nexthdr;
3855 off += ipv6_optlen(hp);
3856 break;
3857 }
3858 case IPPROTO_AH: {
3859 struct ip_auth_hdr *hp;
3860
3861 err = skb_maybe_pull_tail(skb,
3862 off +
3863 sizeof(struct ip_auth_hdr),
3864 MAX_IPV6_HDR_LEN);
3865 if (err < 0)
3866 goto out;
3867
3868 hp = OPT_HDR(struct ip_auth_hdr, skb, off);
3869 nexthdr = hp->nexthdr;
3870 off += ipv6_authlen(hp);
3871 break;
3872 }
3873 case IPPROTO_FRAGMENT: {
3874 struct frag_hdr *hp;
3875
3876 err = skb_maybe_pull_tail(skb,
3877 off +
3878 sizeof(struct frag_hdr),
3879 MAX_IPV6_HDR_LEN);
3880 if (err < 0)
3881 goto out;
3882
3883 hp = OPT_HDR(struct frag_hdr, skb, off);
3884
3885 if (hp->frag_off & htons(IP6_OFFSET | IP6_MF))
3886 fragment = true;
3887
3888 nexthdr = hp->nexthdr;
3889 off += sizeof(struct frag_hdr);
3890 break;
3891 }
3892 default:
3893 done = true;
3894 break;
3895 }
3896 }
3897
3898 err = -EPROTO;
3899
3900 if (!done || fragment)
3901 goto out;
3902
Jan Beulichf9708b42014-03-11 13:56:05 +00003903 csum = skb_checksum_setup_ip(skb, nexthdr, off);
3904 if (IS_ERR(csum))
3905 return PTR_ERR(csum);
Paul Durranted1f50c2014-01-09 10:02:46 +00003906
Jan Beulichf9708b42014-03-11 13:56:05 +00003907 if (recalculate)
3908 *csum = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
3909 &ipv6_hdr(skb)->daddr,
3910 skb->len - off, nexthdr, 0);
Paul Durranted1f50c2014-01-09 10:02:46 +00003911 err = 0;
3912
3913out:
3914 return err;
3915}
3916
3917/**
3918 * skb_checksum_setup - set up partial checksum offset
3919 * @skb: the skb to set up
3920 * @recalculate: if true the pseudo-header checksum will be recalculated
3921 */
3922int skb_checksum_setup(struct sk_buff *skb, bool recalculate)
3923{
3924 int err;
3925
3926 switch (skb->protocol) {
3927 case htons(ETH_P_IP):
Jan Beulichf9708b42014-03-11 13:56:05 +00003928 err = skb_checksum_setup_ipv4(skb, recalculate);
Paul Durranted1f50c2014-01-09 10:02:46 +00003929 break;
3930
3931 case htons(ETH_P_IPV6):
3932 err = skb_checksum_setup_ipv6(skb, recalculate);
3933 break;
3934
3935 default:
3936 err = -EPROTO;
3937 break;
3938 }
3939
3940 return err;
3941}
3942EXPORT_SYMBOL(skb_checksum_setup);
3943
Ben Hutchings4497b072008-06-19 16:22:28 -07003944void __skb_warn_lro_forwarding(const struct sk_buff *skb)
3945{
Joe Perchese87cc472012-05-13 21:56:26 +00003946 net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n",
3947 skb->dev->name);
Ben Hutchings4497b072008-06-19 16:22:28 -07003948}
Ben Hutchings4497b072008-06-19 16:22:28 -07003949EXPORT_SYMBOL(__skb_warn_lro_forwarding);
Eric Dumazetbad43ca2012-05-19 03:02:02 +00003950
3951void kfree_skb_partial(struct sk_buff *skb, bool head_stolen)
3952{
Eric Dumazet3d861f62012-10-22 09:03:40 +00003953 if (head_stolen) {
3954 skb_release_head_state(skb);
Eric Dumazetbad43ca2012-05-19 03:02:02 +00003955 kmem_cache_free(skbuff_head_cache, skb);
Eric Dumazet3d861f62012-10-22 09:03:40 +00003956 } else {
Eric Dumazetbad43ca2012-05-19 03:02:02 +00003957 __kfree_skb(skb);
Eric Dumazet3d861f62012-10-22 09:03:40 +00003958 }
Eric Dumazetbad43ca2012-05-19 03:02:02 +00003959}
3960EXPORT_SYMBOL(kfree_skb_partial);
3961
3962/**
3963 * skb_try_coalesce - try to merge skb to prior one
3964 * @to: prior buffer
3965 * @from: buffer to add
3966 * @fragstolen: pointer to boolean
Randy Dunlapc6c4b972012-06-08 14:01:44 +00003967 * @delta_truesize: how much more was allocated than was requested
Eric Dumazetbad43ca2012-05-19 03:02:02 +00003968 */
3969bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
3970 bool *fragstolen, int *delta_truesize)
3971{
3972 int i, delta, len = from->len;
3973
3974 *fragstolen = false;
3975
3976 if (skb_cloned(to))
3977 return false;
3978
3979 if (len <= skb_tailroom(to)) {
Eric Dumazete93a0432014-09-15 04:19:52 -07003980 if (len)
3981 BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len));
Eric Dumazetbad43ca2012-05-19 03:02:02 +00003982 *delta_truesize = 0;
3983 return true;
3984 }
3985
3986 if (skb_has_frag_list(to) || skb_has_frag_list(from))
3987 return false;
3988
3989 if (skb_headlen(from) != 0) {
3990 struct page *page;
3991 unsigned int offset;
3992
3993 if (skb_shinfo(to)->nr_frags +
3994 skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
3995 return false;
3996
3997 if (skb_head_is_locked(from))
3998 return false;
3999
4000 delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
4001
4002 page = virt_to_head_page(from->head);
4003 offset = from->data - (unsigned char *)page_address(page);
4004
4005 skb_fill_page_desc(to, skb_shinfo(to)->nr_frags,
4006 page, offset, skb_headlen(from));
4007 *fragstolen = true;
4008 } else {
4009 if (skb_shinfo(to)->nr_frags +
4010 skb_shinfo(from)->nr_frags > MAX_SKB_FRAGS)
4011 return false;
4012
Weiping Panf4b549a2012-09-28 20:15:30 +00004013 delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from));
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004014 }
4015
4016 WARN_ON_ONCE(delta < len);
4017
4018 memcpy(skb_shinfo(to)->frags + skb_shinfo(to)->nr_frags,
4019 skb_shinfo(from)->frags,
4020 skb_shinfo(from)->nr_frags * sizeof(skb_frag_t));
4021 skb_shinfo(to)->nr_frags += skb_shinfo(from)->nr_frags;
4022
4023 if (!skb_cloned(from))
4024 skb_shinfo(from)->nr_frags = 0;
4025
Li RongQing8ea853f2012-09-18 16:53:21 +00004026 /* if the skb is not cloned this does nothing
4027 * since we set nr_frags to 0.
4028 */
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004029 for (i = 0; i < skb_shinfo(from)->nr_frags; i++)
4030 skb_frag_ref(from, i);
4031
4032 to->truesize += delta;
4033 to->len += len;
4034 to->data_len += len;
4035
4036 *delta_truesize = delta;
4037 return true;
4038}
4039EXPORT_SYMBOL(skb_try_coalesce);
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004040
4041/**
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004042 * skb_scrub_packet - scrub an skb
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004043 *
4044 * @skb: buffer to clean
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004045 * @xnet: packet is crossing netns
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004046 *
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004047 * skb_scrub_packet can be used after encapsulating or decapsulting a packet
4048 * into/from a tunnel. Some information have to be cleared during these
4049 * operations.
4050 * skb_scrub_packet can also be used to clean a skb before injecting it in
4051 * another namespace (@xnet == true). We have to clear all information in the
4052 * skb that could impact namespace isolation.
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004053 */
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004054void skb_scrub_packet(struct sk_buff *skb, bool xnet)
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004055{
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004056 if (xnet)
4057 skb_orphan(skb);
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004058 skb->tstamp.tv64 = 0;
4059 skb->pkt_type = PACKET_HOST;
4060 skb->skb_iif = 0;
WANG Cong60ff7462014-05-04 16:39:18 -07004061 skb->ignore_df = 0;
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004062 skb_dst_drop(skb);
4063 skb->mark = 0;
4064 secpath_reset(skb);
4065 nf_reset(skb);
4066 nf_reset_trace(skb);
4067}
4068EXPORT_SYMBOL_GPL(skb_scrub_packet);
Florian Westphalde960aa2014-01-26 10:58:16 +01004069
4070/**
4071 * skb_gso_transport_seglen - Return length of individual segments of a gso packet
4072 *
4073 * @skb: GSO skb
4074 *
4075 * skb_gso_transport_seglen is used to determine the real size of the
4076 * individual segments, including Layer4 headers (TCP/UDP).
4077 *
4078 * The MAC/L2 or network (IP, IPv6) headers are not accounted for.
4079 */
4080unsigned int skb_gso_transport_seglen(const struct sk_buff *skb)
4081{
4082 const struct skb_shared_info *shinfo = skb_shinfo(skb);
Florian Westphalf993bc22014-10-20 13:49:18 +02004083 unsigned int thlen = 0;
Florian Westphalde960aa2014-01-26 10:58:16 +01004084
Florian Westphalf993bc22014-10-20 13:49:18 +02004085 if (skb->encapsulation) {
4086 thlen = skb_inner_transport_header(skb) -
4087 skb_transport_header(skb);
Florian Westphal6d39d582014-04-09 10:28:50 +02004088
Florian Westphalf993bc22014-10-20 13:49:18 +02004089 if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)))
4090 thlen += inner_tcp_hdrlen(skb);
4091 } else if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) {
4092 thlen = tcp_hdrlen(skb);
4093 }
Florian Westphal6d39d582014-04-09 10:28:50 +02004094 /* UFO sets gso_size to the size of the fragmentation
4095 * payload, i.e. the size of the L4 (UDP) header is already
4096 * accounted for.
4097 */
Florian Westphalf993bc22014-10-20 13:49:18 +02004098 return thlen + shinfo->gso_size;
Florian Westphalde960aa2014-01-26 10:58:16 +01004099}
4100EXPORT_SYMBOL_GPL(skb_gso_transport_seglen);
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04004101
4102static struct sk_buff *skb_reorder_vlan_header(struct sk_buff *skb)
4103{
4104 if (skb_cow(skb, skb_headroom(skb)) < 0) {
4105 kfree_skb(skb);
4106 return NULL;
4107 }
4108
4109 memmove(skb->data - ETH_HLEN, skb->data - VLAN_ETH_HLEN, 2 * ETH_ALEN);
4110 skb->mac_header += VLAN_HLEN;
4111 return skb;
4112}
4113
4114struct sk_buff *skb_vlan_untag(struct sk_buff *skb)
4115{
4116 struct vlan_hdr *vhdr;
4117 u16 vlan_tci;
4118
4119 if (unlikely(vlan_tx_tag_present(skb))) {
4120 /* vlan_tci is already set-up so leave this for another time */
4121 return skb;
4122 }
4123
4124 skb = skb_share_check(skb, GFP_ATOMIC);
4125 if (unlikely(!skb))
4126 goto err_free;
4127
4128 if (unlikely(!pskb_may_pull(skb, VLAN_HLEN)))
4129 goto err_free;
4130
4131 vhdr = (struct vlan_hdr *)skb->data;
4132 vlan_tci = ntohs(vhdr->h_vlan_TCI);
4133 __vlan_hwaccel_put_tag(skb, skb->protocol, vlan_tci);
4134
4135 skb_pull_rcsum(skb, VLAN_HLEN);
4136 vlan_set_encap_proto(skb, vhdr);
4137
4138 skb = skb_reorder_vlan_header(skb);
4139 if (unlikely(!skb))
4140 goto err_free;
4141
4142 skb_reset_network_header(skb);
4143 skb_reset_transport_header(skb);
4144 skb_reset_mac_len(skb);
4145
4146 return skb;
4147
4148err_free:
4149 kfree_skb(skb);
4150 return NULL;
4151}
4152EXPORT_SYMBOL(skb_vlan_untag);
Eric Dumazet2e4e4412014-09-17 04:49:49 -07004153
Jiri Pirkoe2195122014-11-19 14:05:01 +01004154int skb_ensure_writable(struct sk_buff *skb, int write_len)
4155{
4156 if (!pskb_may_pull(skb, write_len))
4157 return -ENOMEM;
4158
4159 if (!skb_cloned(skb) || skb_clone_writable(skb, write_len))
4160 return 0;
4161
4162 return pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
4163}
4164EXPORT_SYMBOL(skb_ensure_writable);
4165
Eric Dumazet2e4e4412014-09-17 04:49:49 -07004166/**
4167 * alloc_skb_with_frags - allocate skb with page frags
4168 *
Masanari Iidade3f0d02014-10-09 12:58:08 +09004169 * @header_len: size of linear part
4170 * @data_len: needed length in frags
4171 * @max_page_order: max page order desired.
4172 * @errcode: pointer to error code if any
4173 * @gfp_mask: allocation mask
Eric Dumazet2e4e4412014-09-17 04:49:49 -07004174 *
4175 * This can be used to allocate a paged skb, given a maximal order for frags.
4176 */
4177struct sk_buff *alloc_skb_with_frags(unsigned long header_len,
4178 unsigned long data_len,
4179 int max_page_order,
4180 int *errcode,
4181 gfp_t gfp_mask)
4182{
4183 int npages = (data_len + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
4184 unsigned long chunk;
4185 struct sk_buff *skb;
4186 struct page *page;
4187 gfp_t gfp_head;
4188 int i;
4189
4190 *errcode = -EMSGSIZE;
4191 /* Note this test could be relaxed, if we succeed to allocate
4192 * high order pages...
4193 */
4194 if (npages > MAX_SKB_FRAGS)
4195 return NULL;
4196
4197 gfp_head = gfp_mask;
4198 if (gfp_head & __GFP_WAIT)
4199 gfp_head |= __GFP_REPEAT;
4200
4201 *errcode = -ENOBUFS;
4202 skb = alloc_skb(header_len, gfp_head);
4203 if (!skb)
4204 return NULL;
4205
4206 skb->truesize += npages << PAGE_SHIFT;
4207
4208 for (i = 0; npages > 0; i++) {
4209 int order = max_page_order;
4210
4211 while (order) {
4212 if (npages >= 1 << order) {
4213 page = alloc_pages(gfp_mask |
4214 __GFP_COMP |
4215 __GFP_NOWARN |
4216 __GFP_NORETRY,
4217 order);
4218 if (page)
4219 goto fill_page;
4220 /* Do not retry other high order allocations */
4221 order = 1;
4222 max_page_order = 0;
4223 }
4224 order--;
4225 }
4226 page = alloc_page(gfp_mask);
4227 if (!page)
4228 goto failure;
4229fill_page:
4230 chunk = min_t(unsigned long, data_len,
4231 PAGE_SIZE << order);
4232 skb_fill_page_desc(skb, i, page, 0, chunk);
4233 data_len -= chunk;
4234 npages -= 1 << order;
4235 }
4236 return skb;
4237
4238failure:
4239 kfree_skb(skb);
4240 return NULL;
4241}
4242EXPORT_SYMBOL(alloc_skb_with_frags);