blob: 7de3d679f3e0aab3c699fadae95d509c006bf835 [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) {
David S. Millerd179cd12005-08-17 14:57:30 -0700260 struct sk_buff *child = skb + 1;
261 atomic_t *fclone_ref = (atomic_t *) (child + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
Vegard Nossumfe55f6d2008-08-30 12:16:35 +0200263 kmemcheck_annotate_bitfield(child, flags1);
David S. Millerd179cd12005-08-17 14:57:30 -0700264 skb->fclone = SKB_FCLONE_ORIG;
265 atomic_set(fclone_ref, 1);
266
267 child->fclone = SKB_FCLONE_UNAVAILABLE;
Mel Gormanc93bdd02012-07-31 16:44:19 -0700268 child->pfmemalloc = pfmemalloc;
David S. Millerd179cd12005-08-17 14:57:30 -0700269 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270out:
271 return skb;
272nodata:
Herbert Xu8798b3f2006-01-23 16:32:45 -0800273 kmem_cache_free(cache, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 skb = NULL;
275 goto out;
276}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800277EXPORT_SYMBOL(__alloc_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
279/**
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000280 * build_skb - build a network buffer
281 * @data: data buffer provided by caller
Eric Dumazetd3836f22012-04-27 00:33:38 +0000282 * @frag_size: size of fragment, or 0 if head was kmalloced
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000283 *
284 * Allocate a new &sk_buff. Caller provides space holding head and
Florian Fainellideceb4c2013-07-23 20:22:39 +0100285 * skb_shared_info. @data must have been allocated by kmalloc() only if
286 * @frag_size is 0, otherwise data should come from the page allocator.
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000287 * The return is the new skb buffer.
288 * On a failure the return is %NULL, and @data is not freed.
289 * Notes :
290 * Before IO, driver allocates only data buffer where NIC put incoming frame
291 * Driver should add room at head (NET_SKB_PAD) and
292 * MUST add room at tail (SKB_DATA_ALIGN(skb_shared_info))
293 * After IO, driver calls build_skb(), to allocate sk_buff and populate it
294 * before giving packet to stack.
295 * RX rings only contains data buffers, not full skbs.
296 */
Eric Dumazetd3836f22012-04-27 00:33:38 +0000297struct sk_buff *build_skb(void *data, unsigned int frag_size)
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000298{
299 struct skb_shared_info *shinfo;
300 struct sk_buff *skb;
Eric Dumazetd3836f22012-04-27 00:33:38 +0000301 unsigned int size = frag_size ? : ksize(data);
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000302
303 skb = kmem_cache_alloc(skbuff_head_cache, GFP_ATOMIC);
304 if (!skb)
305 return NULL;
306
Eric Dumazetd3836f22012-04-27 00:33:38 +0000307 size -= SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000308
309 memset(skb, 0, offsetof(struct sk_buff, tail));
310 skb->truesize = SKB_TRUESIZE(size);
Eric Dumazetd3836f22012-04-27 00:33:38 +0000311 skb->head_frag = frag_size != 0;
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000312 atomic_set(&skb->users, 1);
313 skb->head = data;
314 skb->data = data;
315 skb_reset_tail_pointer(skb);
316 skb->end = skb->tail + size;
Cong Wang35d04612013-05-29 15:16:05 +0800317 skb->mac_header = (typeof(skb->mac_header))~0U;
318 skb->transport_header = (typeof(skb->transport_header))~0U;
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000319
320 /* make sure we initialize shinfo sequentially */
321 shinfo = skb_shinfo(skb);
322 memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
323 atomic_set(&shinfo->dataref, 1);
324 kmemcheck_annotate_variable(shinfo->destructor_arg);
325
326 return skb;
327}
328EXPORT_SYMBOL(build_skb);
329
Eric Dumazeta1c7fff2012-05-17 07:34:16 +0000330struct netdev_alloc_cache {
Eric Dumazet69b08f62012-09-26 06:46:57 +0000331 struct page_frag frag;
332 /* we maintain a pagecount bias, so that we dont dirty cache line
333 * containing page->_count every time we allocate a fragment.
334 */
335 unsigned int pagecnt_bias;
Eric Dumazeta1c7fff2012-05-17 07:34:16 +0000336};
337static DEFINE_PER_CPU(struct netdev_alloc_cache, netdev_alloc_cache);
338
Mel Gormanc93bdd02012-07-31 16:44:19 -0700339static void *__netdev_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
Eric Dumazet6f532612012-05-18 05:12:12 +0000340{
341 struct netdev_alloc_cache *nc;
342 void *data = NULL;
Eric Dumazet69b08f62012-09-26 06:46:57 +0000343 int order;
Eric Dumazet6f532612012-05-18 05:12:12 +0000344 unsigned long flags;
345
346 local_irq_save(flags);
347 nc = &__get_cpu_var(netdev_alloc_cache);
Eric Dumazet69b08f62012-09-26 06:46:57 +0000348 if (unlikely(!nc->frag.page)) {
Eric Dumazet6f532612012-05-18 05:12:12 +0000349refill:
Eric Dumazet69b08f62012-09-26 06:46:57 +0000350 for (order = NETDEV_FRAG_PAGE_MAX_ORDER; ;) {
351 gfp_t gfp = gfp_mask;
352
353 if (order)
354 gfp |= __GFP_COMP | __GFP_NOWARN;
355 nc->frag.page = alloc_pages(gfp, order);
356 if (likely(nc->frag.page))
357 break;
358 if (--order < 0)
359 goto end;
360 }
361 nc->frag.size = PAGE_SIZE << order;
Alexander Duyck540eb7b2012-07-12 14:23:50 +0000362recycle:
Eric Dumazet69b08f62012-09-26 06:46:57 +0000363 atomic_set(&nc->frag.page->_count, NETDEV_PAGECNT_MAX_BIAS);
364 nc->pagecnt_bias = NETDEV_PAGECNT_MAX_BIAS;
365 nc->frag.offset = 0;
Eric Dumazet6f532612012-05-18 05:12:12 +0000366 }
Alexander Duyck540eb7b2012-07-12 14:23:50 +0000367
Eric Dumazet69b08f62012-09-26 06:46:57 +0000368 if (nc->frag.offset + fragsz > nc->frag.size) {
Alexander Duyck540eb7b2012-07-12 14:23:50 +0000369 /* avoid unnecessary locked operations if possible */
Eric Dumazet69b08f62012-09-26 06:46:57 +0000370 if ((atomic_read(&nc->frag.page->_count) == nc->pagecnt_bias) ||
371 atomic_sub_and_test(nc->pagecnt_bias, &nc->frag.page->_count))
Alexander Duyck540eb7b2012-07-12 14:23:50 +0000372 goto recycle;
373 goto refill;
Eric Dumazet6f532612012-05-18 05:12:12 +0000374 }
Alexander Duyck540eb7b2012-07-12 14:23:50 +0000375
Eric Dumazet69b08f62012-09-26 06:46:57 +0000376 data = page_address(nc->frag.page) + nc->frag.offset;
377 nc->frag.offset += fragsz;
Alexander Duyck540eb7b2012-07-12 14:23:50 +0000378 nc->pagecnt_bias--;
379end:
Eric Dumazet6f532612012-05-18 05:12:12 +0000380 local_irq_restore(flags);
381 return data;
382}
Mel Gormanc93bdd02012-07-31 16:44:19 -0700383
384/**
385 * netdev_alloc_frag - allocate a page fragment
386 * @fragsz: fragment size
387 *
388 * Allocates a frag from a page for receive buffer.
389 * Uses GFP_ATOMIC allocations.
390 */
391void *netdev_alloc_frag(unsigned int fragsz)
392{
393 return __netdev_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD);
394}
Eric Dumazet6f532612012-05-18 05:12:12 +0000395EXPORT_SYMBOL(netdev_alloc_frag);
396
397/**
Christoph Hellwig8af27452006-07-31 22:35:23 -0700398 * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
399 * @dev: network device to receive on
400 * @length: length to allocate
401 * @gfp_mask: get_free_pages mask, passed to alloc_skb
402 *
403 * Allocate a new &sk_buff and assign it a usage count of one. The
404 * buffer has unspecified headroom built in. Users should allocate
405 * the headroom they think they need without accounting for the
406 * built in space. The built in space is used for optimisations.
407 *
408 * %NULL is returned if there is no free memory.
409 */
410struct sk_buff *__netdev_alloc_skb(struct net_device *dev,
Eric Dumazet6f532612012-05-18 05:12:12 +0000411 unsigned int length, gfp_t gfp_mask)
Christoph Hellwig8af27452006-07-31 22:35:23 -0700412{
Eric Dumazet6f532612012-05-18 05:12:12 +0000413 struct sk_buff *skb = NULL;
Eric Dumazeta1c7fff2012-05-17 07:34:16 +0000414 unsigned int fragsz = SKB_DATA_ALIGN(length + NET_SKB_PAD) +
415 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Christoph Hellwig8af27452006-07-31 22:35:23 -0700416
Eric Dumazet310e1582012-07-16 13:15:52 +0200417 if (fragsz <= PAGE_SIZE && !(gfp_mask & (__GFP_WAIT | GFP_DMA))) {
Mel Gormanc93bdd02012-07-31 16:44:19 -0700418 void *data;
419
420 if (sk_memalloc_socks())
421 gfp_mask |= __GFP_MEMALLOC;
422
423 data = __netdev_alloc_frag(fragsz, gfp_mask);
Eric Dumazeta1c7fff2012-05-17 07:34:16 +0000424
Eric Dumazet6f532612012-05-18 05:12:12 +0000425 if (likely(data)) {
426 skb = build_skb(data, fragsz);
427 if (unlikely(!skb))
428 put_page(virt_to_head_page(data));
Eric Dumazeta1c7fff2012-05-17 07:34:16 +0000429 }
Eric Dumazeta1c7fff2012-05-17 07:34:16 +0000430 } else {
Mel Gormanc93bdd02012-07-31 16:44:19 -0700431 skb = __alloc_skb(length + NET_SKB_PAD, gfp_mask,
432 SKB_ALLOC_RX, NUMA_NO_NODE);
Eric Dumazeta1c7fff2012-05-17 07:34:16 +0000433 }
Christoph Hellwig7b2e4972006-08-07 16:09:04 -0700434 if (likely(skb)) {
Christoph Hellwig8af27452006-07-31 22:35:23 -0700435 skb_reserve(skb, NET_SKB_PAD);
Christoph Hellwig7b2e4972006-08-07 16:09:04 -0700436 skb->dev = dev;
437 }
Christoph Hellwig8af27452006-07-31 22:35:23 -0700438 return skb;
439}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800440EXPORT_SYMBOL(__netdev_alloc_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
Peter Zijlstra654bed12008-10-07 14:22:33 -0700442void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off,
Eric Dumazet50269e12012-03-23 23:59:33 +0000443 int size, unsigned int truesize)
Peter Zijlstra654bed12008-10-07 14:22:33 -0700444{
445 skb_fill_page_desc(skb, i, page, off, size);
446 skb->len += size;
447 skb->data_len += size;
Eric Dumazet50269e12012-03-23 23:59:33 +0000448 skb->truesize += truesize;
Peter Zijlstra654bed12008-10-07 14:22:33 -0700449}
450EXPORT_SYMBOL(skb_add_rx_frag);
451
Jason Wangf8e617e2013-11-01 14:07:47 +0800452void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size,
453 unsigned int truesize)
454{
455 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
456
457 skb_frag_size_add(frag, size);
458 skb->len += size;
459 skb->data_len += size;
460 skb->truesize += truesize;
461}
462EXPORT_SYMBOL(skb_coalesce_rx_frag);
463
Herbert Xu27b437c2006-07-13 19:26:39 -0700464static void skb_drop_list(struct sk_buff **listp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465{
Eric Dumazetbd8a7032013-06-24 06:26:00 -0700466 kfree_skb_list(*listp);
Herbert Xu27b437c2006-07-13 19:26:39 -0700467 *listp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468}
469
Herbert Xu27b437c2006-07-13 19:26:39 -0700470static inline void skb_drop_fraglist(struct sk_buff *skb)
471{
472 skb_drop_list(&skb_shinfo(skb)->frag_list);
473}
474
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475static void skb_clone_fraglist(struct sk_buff *skb)
476{
477 struct sk_buff *list;
478
David S. Millerfbb398a2009-06-09 00:18:59 -0700479 skb_walk_frags(skb, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 skb_get(list);
481}
482
Eric Dumazetd3836f22012-04-27 00:33:38 +0000483static void skb_free_head(struct sk_buff *skb)
484{
485 if (skb->head_frag)
486 put_page(virt_to_head_page(skb->head));
487 else
488 kfree(skb->head);
489}
490
Adrian Bunk5bba1712006-06-29 13:02:35 -0700491static void skb_release_data(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
Eric Dumazetff04a772014-09-23 18:39:30 -0700493 struct skb_shared_info *shinfo = skb_shinfo(skb);
494 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Eric Dumazetff04a772014-09-23 18:39:30 -0700496 if (skb->cloned &&
497 atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
498 &shinfo->dataref))
499 return;
Shirley Maa6686f22011-07-06 12:22:12 +0000500
Eric Dumazetff04a772014-09-23 18:39:30 -0700501 for (i = 0; i < shinfo->nr_frags; i++)
502 __skb_frag_unref(&shinfo->frags[i]);
Shirley Maa6686f22011-07-06 12:22:12 +0000503
Eric Dumazetff04a772014-09-23 18:39:30 -0700504 /*
505 * If skb buf is from userspace, we need to notify the caller
506 * the lower device DMA has done;
507 */
508 if (shinfo->tx_flags & SKBTX_DEV_ZEROCOPY) {
509 struct ubuf_info *uarg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
Eric Dumazetff04a772014-09-23 18:39:30 -0700511 uarg = shinfo->destructor_arg;
512 if (uarg->callback)
513 uarg->callback(uarg, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 }
Eric Dumazetff04a772014-09-23 18:39:30 -0700515
516 if (shinfo->frag_list)
517 kfree_skb_list(shinfo->frag_list);
518
519 skb_free_head(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520}
521
522/*
523 * Free an skbuff by memory without cleaning the state.
524 */
Herbert Xu2d4baff2007-11-26 23:11:19 +0800525static void kfree_skbmem(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526{
David S. Millerd179cd12005-08-17 14:57:30 -0700527 struct sk_buff *other;
528 atomic_t *fclone_ref;
529
David S. Millerd179cd12005-08-17 14:57:30 -0700530 switch (skb->fclone) {
531 case SKB_FCLONE_UNAVAILABLE:
532 kmem_cache_free(skbuff_head_cache, skb);
533 break;
534
535 case SKB_FCLONE_ORIG:
536 fclone_ref = (atomic_t *) (skb + 2);
537 if (atomic_dec_and_test(fclone_ref))
538 kmem_cache_free(skbuff_fclone_cache, skb);
539 break;
540
541 case SKB_FCLONE_CLONE:
542 fclone_ref = (atomic_t *) (skb + 1);
543 other = skb - 1;
544
545 /* The clone portion is available for
546 * fast-cloning again.
547 */
548 skb->fclone = SKB_FCLONE_UNAVAILABLE;
549
550 if (atomic_dec_and_test(fclone_ref))
551 kmem_cache_free(skbuff_fclone_cache, other);
552 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700553 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554}
555
Lennert Buytenhek04a4bb52008-10-01 02:33:12 -0700556static void skb_release_head_state(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{
Eric Dumazetadf30902009-06-02 05:19:30 +0000558 skb_dst_drop(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559#ifdef CONFIG_XFRM
560 secpath_put(skb->sp);
561#endif
Stephen Hemminger9c2b3322005-04-19 22:39:42 -0700562 if (skb->destructor) {
563 WARN_ON(in_irq());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 skb->destructor(skb);
565 }
Igor Maravića3bf7ae2011-12-12 02:58:22 +0000566#if IS_ENABLED(CONFIG_NF_CONNTRACK)
Yasuyuki Kozakai5f79e0f2007-03-23 11:17:07 -0700567 nf_conntrack_put(skb->nfct);
KOVACS Krisztian2fc72c72011-01-12 20:25:08 +0100568#endif
Pablo Neira Ayuso1109a902014-10-01 11:19:17 +0200569#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 nf_bridge_put(skb->nf_bridge);
571#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572/* XXX: IS this still necessary? - JHS */
573#ifdef CONFIG_NET_SCHED
574 skb->tc_index = 0;
575#ifdef CONFIG_NET_CLS_ACT
576 skb->tc_verd = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577#endif
578#endif
Lennert Buytenhek04a4bb52008-10-01 02:33:12 -0700579}
580
581/* Free everything but the sk_buff shell. */
582static void skb_release_all(struct sk_buff *skb)
583{
584 skb_release_head_state(skb);
Pablo Neira5e71d9d2013-06-03 09:28:43 +0000585 if (likely(skb->head))
Patrick McHardy0ebd0ac2013-04-17 06:46:58 +0000586 skb_release_data(skb);
Herbert Xu2d4baff2007-11-26 23:11:19 +0800587}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
Herbert Xu2d4baff2007-11-26 23:11:19 +0800589/**
590 * __kfree_skb - private function
591 * @skb: buffer
592 *
593 * Free an sk_buff. Release anything attached to the buffer.
594 * Clean the state. This is an internal helper function. Users should
595 * always call kfree_skb
596 */
597
598void __kfree_skb(struct sk_buff *skb)
599{
600 skb_release_all(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 kfree_skbmem(skb);
602}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800603EXPORT_SYMBOL(__kfree_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
605/**
Jörn Engel231d06a2006-03-20 21:28:35 -0800606 * kfree_skb - free an sk_buff
607 * @skb: buffer to free
608 *
609 * Drop a reference to the buffer and free it if the usage count has
610 * hit zero.
611 */
612void kfree_skb(struct sk_buff *skb)
613{
614 if (unlikely(!skb))
615 return;
616 if (likely(atomic_read(&skb->users) == 1))
617 smp_rmb();
618 else if (likely(!atomic_dec_and_test(&skb->users)))
619 return;
Neil Hormanead2ceb2009-03-11 09:49:55 +0000620 trace_kfree_skb(skb, __builtin_return_address(0));
Jörn Engel231d06a2006-03-20 21:28:35 -0800621 __kfree_skb(skb);
622}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800623EXPORT_SYMBOL(kfree_skb);
Jörn Engel231d06a2006-03-20 21:28:35 -0800624
Eric Dumazetbd8a7032013-06-24 06:26:00 -0700625void kfree_skb_list(struct sk_buff *segs)
626{
627 while (segs) {
628 struct sk_buff *next = segs->next;
629
630 kfree_skb(segs);
631 segs = next;
632 }
633}
634EXPORT_SYMBOL(kfree_skb_list);
635
Stephen Hemmingerd1a203e2008-11-01 21:01:09 -0700636/**
Michael S. Tsirkin25121172012-11-01 09:16:28 +0000637 * skb_tx_error - report an sk_buff xmit error
638 * @skb: buffer that triggered an error
639 *
640 * Report xmit error if a device callback is tracking this skb.
641 * skb must be freed afterwards.
642 */
643void skb_tx_error(struct sk_buff *skb)
644{
645 if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) {
646 struct ubuf_info *uarg;
647
648 uarg = skb_shinfo(skb)->destructor_arg;
649 if (uarg->callback)
650 uarg->callback(uarg, false);
651 skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
652 }
653}
654EXPORT_SYMBOL(skb_tx_error);
655
656/**
Neil Hormanead2ceb2009-03-11 09:49:55 +0000657 * consume_skb - free an skbuff
658 * @skb: buffer to free
659 *
660 * Drop a ref to the buffer and free it if the usage count has hit zero
661 * Functions identically to kfree_skb, but kfree_skb assumes that the frame
662 * is being dropped after a failure and notes that
663 */
664void consume_skb(struct sk_buff *skb)
665{
666 if (unlikely(!skb))
667 return;
668 if (likely(atomic_read(&skb->users) == 1))
669 smp_rmb();
670 else if (likely(!atomic_dec_and_test(&skb->users)))
671 return;
Koki Sanagi07dc22e2010-08-23 18:46:12 +0900672 trace_consume_skb(skb);
Neil Hormanead2ceb2009-03-11 09:49:55 +0000673 __kfree_skb(skb);
674}
675EXPORT_SYMBOL(consume_skb);
676
Eric Dumazetb1937222014-09-28 22:18:47 -0700677/* Make sure a field is enclosed inside headers_start/headers_end section */
678#define CHECK_SKB_FIELD(field) \
679 BUILD_BUG_ON(offsetof(struct sk_buff, field) < \
680 offsetof(struct sk_buff, headers_start)); \
681 BUILD_BUG_ON(offsetof(struct sk_buff, field) > \
682 offsetof(struct sk_buff, headers_end)); \
683
Herbert Xudec18812007-10-14 00:37:30 -0700684static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
685{
686 new->tstamp = old->tstamp;
Eric Dumazetb1937222014-09-28 22:18:47 -0700687 /* We do not copy old->sk */
Herbert Xudec18812007-10-14 00:37:30 -0700688 new->dev = old->dev;
Eric Dumazetb1937222014-09-28 22:18:47 -0700689 memcpy(new->cb, old->cb, sizeof(old->cb));
Eric Dumazet7fee2262010-05-11 23:19:48 +0000690 skb_dst_copy(new, old);
Alexey Dobriyandef8b4f2008-10-28 13:24:06 -0700691#ifdef CONFIG_XFRM
Herbert Xudec18812007-10-14 00:37:30 -0700692 new->sp = secpath_get(old->sp);
693#endif
Eric Dumazetb1937222014-09-28 22:18:47 -0700694 __nf_copy(new, old, false);
Patrick McHardy6aa895b2008-07-14 22:49:06 -0700695
Eric Dumazetb1937222014-09-28 22:18:47 -0700696 /* Note : this field could be in headers_start/headers_end section
697 * It is not yet because we do not want to have a 16 bit hole
698 */
699 new->queue_mapping = old->queue_mapping;
Eliezer Tamir06021292013-06-10 11:39:50 +0300700
Eric Dumazetb1937222014-09-28 22:18:47 -0700701 memcpy(&new->headers_start, &old->headers_start,
702 offsetof(struct sk_buff, headers_end) -
703 offsetof(struct sk_buff, headers_start));
704 CHECK_SKB_FIELD(protocol);
705 CHECK_SKB_FIELD(csum);
706 CHECK_SKB_FIELD(hash);
707 CHECK_SKB_FIELD(priority);
708 CHECK_SKB_FIELD(skb_iif);
709 CHECK_SKB_FIELD(vlan_proto);
710 CHECK_SKB_FIELD(vlan_tci);
711 CHECK_SKB_FIELD(transport_header);
712 CHECK_SKB_FIELD(network_header);
713 CHECK_SKB_FIELD(mac_header);
714 CHECK_SKB_FIELD(inner_protocol);
715 CHECK_SKB_FIELD(inner_transport_header);
716 CHECK_SKB_FIELD(inner_network_header);
717 CHECK_SKB_FIELD(inner_mac_header);
718 CHECK_SKB_FIELD(mark);
719#ifdef CONFIG_NETWORK_SECMARK
720 CHECK_SKB_FIELD(secmark);
721#endif
Cong Wange0d10952013-08-01 11:10:25 +0800722#ifdef CONFIG_NET_RX_BUSY_POLL
Eric Dumazetb1937222014-09-28 22:18:47 -0700723 CHECK_SKB_FIELD(napi_id);
Eliezer Tamir06021292013-06-10 11:39:50 +0300724#endif
Eric Dumazetb1937222014-09-28 22:18:47 -0700725#ifdef CONFIG_NET_SCHED
726 CHECK_SKB_FIELD(tc_index);
727#ifdef CONFIG_NET_CLS_ACT
728 CHECK_SKB_FIELD(tc_verd);
729#endif
730#endif
731
Herbert Xudec18812007-10-14 00:37:30 -0700732}
733
Herbert Xu82c49a32009-05-22 22:11:37 +0000734/*
735 * You should not add any new code to this function. Add it to
736 * __copy_skb_header above instead.
737 */
Herbert Xue0053ec2007-10-14 00:37:52 -0700738static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740#define C(x) n->x = skb->x
741
742 n->next = n->prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 n->sk = NULL;
Herbert Xudec18812007-10-14 00:37:30 -0700744 __copy_skb_header(n, skb);
745
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 C(len);
747 C(data_len);
Alexey Dobriyan3e6b3b22007-03-16 15:00:46 -0700748 C(mac_len);
Patrick McHardy334a8132007-06-25 04:35:20 -0700749 n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
Paul Moore02f1c892008-01-07 21:56:41 -0800750 n->cloned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 n->nohdr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 n->destructor = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 C(tail);
754 C(end);
Paul Moore02f1c892008-01-07 21:56:41 -0800755 C(head);
Eric Dumazetd3836f22012-04-27 00:33:38 +0000756 C(head_frag);
Paul Moore02f1c892008-01-07 21:56:41 -0800757 C(data);
758 C(truesize);
759 atomic_set(&n->users, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
761 atomic_inc(&(skb_shinfo(skb)->dataref));
762 skb->cloned = 1;
763
764 return n;
Herbert Xue0053ec2007-10-14 00:37:52 -0700765#undef C
766}
767
768/**
769 * skb_morph - morph one skb into another
770 * @dst: the skb to receive the contents
771 * @src: the skb to supply the contents
772 *
773 * This is identical to skb_clone except that the target skb is
774 * supplied by the user.
775 *
776 * The target skb is returned upon exit.
777 */
778struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
779{
Herbert Xu2d4baff2007-11-26 23:11:19 +0800780 skb_release_all(dst);
Herbert Xue0053ec2007-10-14 00:37:52 -0700781 return __skb_clone(dst, src);
782}
783EXPORT_SYMBOL_GPL(skb_morph);
784
Ben Hutchings2c530402012-07-10 10:55:09 +0000785/**
786 * skb_copy_ubufs - copy userspace skb frags buffers to kernel
Michael S. Tsirkin48c83012011-08-31 08:03:29 +0000787 * @skb: the skb to modify
788 * @gfp_mask: allocation priority
789 *
790 * This must be called on SKBTX_DEV_ZEROCOPY skb.
791 * It will copy all frags into kernel and drop the reference
792 * to userspace pages.
793 *
794 * If this function is called from an interrupt gfp_mask() must be
795 * %GFP_ATOMIC.
796 *
797 * Returns 0 on success or a negative error code on failure
798 * to allocate kernel memory to copy to.
799 */
800int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask)
Shirley Maa6686f22011-07-06 12:22:12 +0000801{
802 int i;
803 int num_frags = skb_shinfo(skb)->nr_frags;
804 struct page *page, *head = NULL;
805 struct ubuf_info *uarg = skb_shinfo(skb)->destructor_arg;
806
807 for (i = 0; i < num_frags; i++) {
808 u8 *vaddr;
809 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
810
Krishna Kumar02756ed2012-07-17 02:05:29 +0000811 page = alloc_page(gfp_mask);
Shirley Maa6686f22011-07-06 12:22:12 +0000812 if (!page) {
813 while (head) {
Sunghan Suh40dadff2013-07-12 16:17:23 +0900814 struct page *next = (struct page *)page_private(head);
Shirley Maa6686f22011-07-06 12:22:12 +0000815 put_page(head);
816 head = next;
817 }
818 return -ENOMEM;
819 }
Eric Dumazet51c56b02012-04-05 11:35:15 +0200820 vaddr = kmap_atomic(skb_frag_page(f));
Shirley Maa6686f22011-07-06 12:22:12 +0000821 memcpy(page_address(page),
Eric Dumazet9e903e02011-10-18 21:00:24 +0000822 vaddr + f->page_offset, skb_frag_size(f));
Eric Dumazet51c56b02012-04-05 11:35:15 +0200823 kunmap_atomic(vaddr);
Sunghan Suh40dadff2013-07-12 16:17:23 +0900824 set_page_private(page, (unsigned long)head);
Shirley Maa6686f22011-07-06 12:22:12 +0000825 head = page;
826 }
827
828 /* skb frags release userspace buffers */
Krishna Kumar02756ed2012-07-17 02:05:29 +0000829 for (i = 0; i < num_frags; i++)
Ian Campbella8605c62011-10-19 23:01:49 +0000830 skb_frag_unref(skb, i);
Shirley Maa6686f22011-07-06 12:22:12 +0000831
Michael S. Tsirkine19d6762012-11-01 09:16:22 +0000832 uarg->callback(uarg, false);
Shirley Maa6686f22011-07-06 12:22:12 +0000833
834 /* skb frags point to kernel buffers */
Krishna Kumar02756ed2012-07-17 02:05:29 +0000835 for (i = num_frags - 1; i >= 0; i--) {
836 __skb_fill_page_desc(skb, i, head, 0,
837 skb_shinfo(skb)->frags[i].size);
Sunghan Suh40dadff2013-07-12 16:17:23 +0900838 head = (struct page *)page_private(head);
Shirley Maa6686f22011-07-06 12:22:12 +0000839 }
Michael S. Tsirkin48c83012011-08-31 08:03:29 +0000840
841 skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
Shirley Maa6686f22011-07-06 12:22:12 +0000842 return 0;
843}
Michael S. Tsirkindcc0fb72012-07-20 09:23:20 +0000844EXPORT_SYMBOL_GPL(skb_copy_ubufs);
Shirley Maa6686f22011-07-06 12:22:12 +0000845
Herbert Xue0053ec2007-10-14 00:37:52 -0700846/**
847 * skb_clone - duplicate an sk_buff
848 * @skb: buffer to clone
849 * @gfp_mask: allocation priority
850 *
851 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
852 * copies share the same packet data but not structure. The new
853 * buffer has a reference count of 1. If the allocation fails the
854 * function returns %NULL otherwise the new buffer is returned.
855 *
856 * If this function is called from an interrupt gfp_mask() must be
857 * %GFP_ATOMIC.
858 */
859
860struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
861{
862 struct sk_buff *n;
863
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +0000864 if (skb_orphan_frags(skb, gfp_mask))
865 return NULL;
Shirley Maa6686f22011-07-06 12:22:12 +0000866
Herbert Xue0053ec2007-10-14 00:37:52 -0700867 n = skb + 1;
868 if (skb->fclone == SKB_FCLONE_ORIG &&
869 n->fclone == SKB_FCLONE_UNAVAILABLE) {
870 atomic_t *fclone_ref = (atomic_t *) (n + 1);
871 n->fclone = SKB_FCLONE_CLONE;
872 atomic_inc(fclone_ref);
873 } else {
Mel Gormanc93bdd02012-07-31 16:44:19 -0700874 if (skb_pfmemalloc(skb))
875 gfp_mask |= __GFP_MEMALLOC;
876
Herbert Xue0053ec2007-10-14 00:37:52 -0700877 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
878 if (!n)
879 return NULL;
Vegard Nossumfe55f6d2008-08-30 12:16:35 +0200880
881 kmemcheck_annotate_bitfield(n, flags1);
Herbert Xue0053ec2007-10-14 00:37:52 -0700882 n->fclone = SKB_FCLONE_UNAVAILABLE;
883 }
884
885 return __skb_clone(n, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800887EXPORT_SYMBOL(skb_clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
Pravin B Shelarf5b17292013-03-07 13:21:40 +0000889static void skb_headers_offset_update(struct sk_buff *skb, int off)
890{
Eric Dumazet030737b2013-10-19 11:42:54 -0700891 /* Only adjust this if it actually is csum_start rather than csum */
892 if (skb->ip_summed == CHECKSUM_PARTIAL)
893 skb->csum_start += off;
Pravin B Shelarf5b17292013-03-07 13:21:40 +0000894 /* {transport,network,mac}_header and tail are relative to skb->head */
895 skb->transport_header += off;
896 skb->network_header += off;
897 if (skb_mac_header_was_set(skb))
898 skb->mac_header += off;
899 skb->inner_transport_header += off;
900 skb->inner_network_header += off;
Pravin B Shelaraefbd2b2013-03-07 13:21:46 +0000901 skb->inner_mac_header += off;
Pravin B Shelarf5b17292013-03-07 13:21:40 +0000902}
903
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
905{
Herbert Xudec18812007-10-14 00:37:30 -0700906 __copy_skb_header(new, old);
907
Herbert Xu79671682006-06-22 02:40:14 -0700908 skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
909 skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
910 skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911}
912
Mel Gormanc93bdd02012-07-31 16:44:19 -0700913static inline int skb_alloc_rx_flag(const struct sk_buff *skb)
914{
915 if (skb_pfmemalloc(skb))
916 return SKB_ALLOC_RX;
917 return 0;
918}
919
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920/**
921 * skb_copy - create private copy of an sk_buff
922 * @skb: buffer to copy
923 * @gfp_mask: allocation priority
924 *
925 * Make a copy of both an &sk_buff and its data. This is used when the
926 * caller wishes to modify the data and needs a private copy of the
927 * data to alter. Returns %NULL on failure or the pointer to the buffer
928 * on success. The returned buffer has a reference count of 1.
929 *
930 * As by-product this function converts non-linear &sk_buff to linear
931 * one, so that &sk_buff becomes completely private and caller is allowed
932 * to modify all the data of returned buffer. This means that this
933 * function is not recommended for use in circumstances when only
934 * header is going to be modified. Use pskb_copy() instead.
935 */
936
Al Virodd0fc662005-10-07 07:46:04 +0100937struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938{
Eric Dumazet6602ceb2010-09-01 05:25:10 +0000939 int headerlen = skb_headroom(skb);
Alexander Duyckec47ea82012-05-04 14:26:56 +0000940 unsigned int size = skb_end_offset(skb) + skb->data_len;
Mel Gormanc93bdd02012-07-31 16:44:19 -0700941 struct sk_buff *n = __alloc_skb(size, gfp_mask,
942 skb_alloc_rx_flag(skb), NUMA_NO_NODE);
Eric Dumazet6602ceb2010-09-01 05:25:10 +0000943
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 if (!n)
945 return NULL;
946
947 /* Set the data pointer */
948 skb_reserve(n, headerlen);
949 /* Set the tail pointer and length */
950 skb_put(n, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
952 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
953 BUG();
954
955 copy_skb_header(n, skb);
956 return n;
957}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800958EXPORT_SYMBOL(skb_copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
960/**
Octavian Purdilabad93e92014-06-12 01:36:26 +0300961 * __pskb_copy_fclone - create copy of an sk_buff with private head.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 * @skb: buffer to copy
Eric Dumazet117632e2011-12-03 21:39:53 +0000963 * @headroom: headroom of new skb
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 * @gfp_mask: allocation priority
Octavian Purdilabad93e92014-06-12 01:36:26 +0300965 * @fclone: if true allocate the copy of the skb from the fclone
966 * cache instead of the head cache; it is recommended to set this
967 * to true for the cases where the copy will likely be cloned
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 *
969 * Make a copy of both an &sk_buff and part of its data, located
970 * in header. Fragmented data remain shared. This is used when
971 * the caller wishes to modify only header of &sk_buff and needs
972 * private copy of the header to alter. Returns %NULL on failure
973 * or the pointer to the buffer on success.
974 * The returned buffer has a reference count of 1.
975 */
976
Octavian Purdilabad93e92014-06-12 01:36:26 +0300977struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom,
978 gfp_t gfp_mask, bool fclone)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979{
Eric Dumazet117632e2011-12-03 21:39:53 +0000980 unsigned int size = skb_headlen(skb) + headroom;
Octavian Purdilabad93e92014-06-12 01:36:26 +0300981 int flags = skb_alloc_rx_flag(skb) | (fclone ? SKB_ALLOC_FCLONE : 0);
982 struct sk_buff *n = __alloc_skb(size, gfp_mask, flags, NUMA_NO_NODE);
Eric Dumazet6602ceb2010-09-01 05:25:10 +0000983
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 if (!n)
985 goto out;
986
987 /* Set the data pointer */
Eric Dumazet117632e2011-12-03 21:39:53 +0000988 skb_reserve(n, headroom);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 /* Set the tail pointer and length */
990 skb_put(n, skb_headlen(skb));
991 /* Copy the bytes */
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -0300992 skb_copy_from_linear_data(skb, n->data, n->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
Herbert Xu25f484a2006-11-07 14:57:15 -0800994 n->truesize += skb->data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 n->data_len = skb->data_len;
996 n->len = skb->len;
997
998 if (skb_shinfo(skb)->nr_frags) {
999 int i;
1000
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +00001001 if (skb_orphan_frags(skb, gfp_mask)) {
1002 kfree_skb(n);
1003 n = NULL;
1004 goto out;
Shirley Maa6686f22011-07-06 12:22:12 +00001005 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1007 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
Ian Campbellea2ab692011-08-22 23:44:58 +00001008 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 }
1010 skb_shinfo(n)->nr_frags = i;
1011 }
1012
David S. Miller21dc3302010-08-23 00:13:46 -07001013 if (skb_has_frag_list(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
1015 skb_clone_fraglist(n);
1016 }
1017
1018 copy_skb_header(n, skb);
1019out:
1020 return n;
1021}
Octavian Purdilabad93e92014-06-12 01:36:26 +03001022EXPORT_SYMBOL(__pskb_copy_fclone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
1024/**
1025 * pskb_expand_head - reallocate header of &sk_buff
1026 * @skb: buffer to reallocate
1027 * @nhead: room to add at head
1028 * @ntail: room to add at tail
1029 * @gfp_mask: allocation priority
1030 *
Mathias Krausebc323832013-11-07 14:18:26 +01001031 * Expands (or creates identical copy, if @nhead and @ntail are zero)
1032 * header of @skb. &sk_buff itself is not changed. &sk_buff MUST have
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 * reference count of 1. Returns zero in the case of success or error,
1034 * if expansion failed. In the last case, &sk_buff is not changed.
1035 *
1036 * All the pointers pointing into skb header may change and must be
1037 * reloaded after call to this function.
1038 */
1039
Victor Fusco86a76ca2005-07-08 14:57:47 -07001040int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
Al Virodd0fc662005-10-07 07:46:04 +01001041 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042{
1043 int i;
1044 u8 *data;
Alexander Duyckec47ea82012-05-04 14:26:56 +00001045 int size = nhead + skb_end_offset(skb) + ntail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 long off;
1047
Herbert Xu4edd87a2008-10-01 07:09:38 -07001048 BUG_ON(nhead < 0);
1049
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 if (skb_shared(skb))
1051 BUG();
1052
1053 size = SKB_DATA_ALIGN(size);
1054
Mel Gormanc93bdd02012-07-31 16:44:19 -07001055 if (skb_pfmemalloc(skb))
1056 gfp_mask |= __GFP_MEMALLOC;
1057 data = kmalloc_reserve(size + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
1058 gfp_mask, NUMA_NO_NODE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 if (!data)
1060 goto nodata;
Eric Dumazet87151b82012-04-10 20:08:39 +00001061 size = SKB_WITH_OVERHEAD(ksize(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
1063 /* Copy only real data... and, alas, header. This should be
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001064 * optimized for the cases when header is void.
1065 */
1066 memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head);
1067
1068 memcpy((struct skb_shared_info *)(data + size),
1069 skb_shinfo(skb),
Eric Dumazetfed66382010-07-22 19:09:08 +00001070 offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
Alexander Duyck3e245912012-05-04 14:26:51 +00001072 /*
1073 * if shinfo is shared we must drop the old head gracefully, but if it
1074 * is not we can just drop the old head and let the existing refcount
1075 * be since all we did is relocate the values
1076 */
1077 if (skb_cloned(skb)) {
Shirley Maa6686f22011-07-06 12:22:12 +00001078 /* copy this zero copy skb frags */
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +00001079 if (skb_orphan_frags(skb, gfp_mask))
1080 goto nofrags;
Eric Dumazet1fd63042010-09-02 23:09:32 +00001081 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
Ian Campbellea2ab692011-08-22 23:44:58 +00001082 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083
Eric Dumazet1fd63042010-09-02 23:09:32 +00001084 if (skb_has_frag_list(skb))
1085 skb_clone_fraglist(skb);
1086
1087 skb_release_data(skb);
Alexander Duyck3e245912012-05-04 14:26:51 +00001088 } else {
1089 skb_free_head(skb);
Eric Dumazet1fd63042010-09-02 23:09:32 +00001090 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 off = (data + nhead) - skb->head;
1092
1093 skb->head = data;
Eric Dumazetd3836f22012-04-27 00:33:38 +00001094 skb->head_frag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 skb->data += off;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001096#ifdef NET_SKBUFF_DATA_USES_OFFSET
1097 skb->end = size;
Patrick McHardy56eb8882007-04-09 11:45:04 -07001098 off = nhead;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001099#else
1100 skb->end = skb->head + size;
Patrick McHardy56eb8882007-04-09 11:45:04 -07001101#endif
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001102 skb->tail += off;
Peter Pan(潘卫平)b41abb42013-06-06 21:27:21 +08001103 skb_headers_offset_update(skb, nhead);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 skb->cloned = 0;
Patrick McHardy334a8132007-06-25 04:35:20 -07001105 skb->hdr_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 skb->nohdr = 0;
1107 atomic_set(&skb_shinfo(skb)->dataref, 1);
1108 return 0;
1109
Shirley Maa6686f22011-07-06 12:22:12 +00001110nofrags:
1111 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112nodata:
1113 return -ENOMEM;
1114}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001115EXPORT_SYMBOL(pskb_expand_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
1117/* Make private copy of skb with writable head and some headroom */
1118
1119struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
1120{
1121 struct sk_buff *skb2;
1122 int delta = headroom - skb_headroom(skb);
1123
1124 if (delta <= 0)
1125 skb2 = pskb_copy(skb, GFP_ATOMIC);
1126 else {
1127 skb2 = skb_clone(skb, GFP_ATOMIC);
1128 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
1129 GFP_ATOMIC)) {
1130 kfree_skb(skb2);
1131 skb2 = NULL;
1132 }
1133 }
1134 return skb2;
1135}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001136EXPORT_SYMBOL(skb_realloc_headroom);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
1138/**
1139 * skb_copy_expand - copy and expand sk_buff
1140 * @skb: buffer to copy
1141 * @newheadroom: new free bytes at head
1142 * @newtailroom: new free bytes at tail
1143 * @gfp_mask: allocation priority
1144 *
1145 * Make a copy of both an &sk_buff and its data and while doing so
1146 * allocate additional space.
1147 *
1148 * This is used when the caller wishes to modify the data and needs a
1149 * private copy of the data to alter as well as more space for new fields.
1150 * Returns %NULL on failure or the pointer to the buffer
1151 * on success. The returned buffer has a reference count of 1.
1152 *
1153 * You must pass %GFP_ATOMIC as the allocation priority if this function
1154 * is called from an interrupt.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 */
1156struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
Victor Fusco86a76ca2005-07-08 14:57:47 -07001157 int newheadroom, int newtailroom,
Al Virodd0fc662005-10-07 07:46:04 +01001158 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159{
1160 /*
1161 * Allocate the copy buffer
1162 */
Mel Gormanc93bdd02012-07-31 16:44:19 -07001163 struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom,
1164 gfp_mask, skb_alloc_rx_flag(skb),
1165 NUMA_NO_NODE);
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001166 int oldheadroom = skb_headroom(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 int head_copy_len, head_copy_off;
1168
1169 if (!n)
1170 return NULL;
1171
1172 skb_reserve(n, newheadroom);
1173
1174 /* Set the tail pointer and length */
1175 skb_put(n, skb->len);
1176
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001177 head_copy_len = oldheadroom;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 head_copy_off = 0;
1179 if (newheadroom <= head_copy_len)
1180 head_copy_len = newheadroom;
1181 else
1182 head_copy_off = newheadroom - head_copy_len;
1183
1184 /* Copy the linear header and data. */
1185 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
1186 skb->len + head_copy_len))
1187 BUG();
1188
1189 copy_skb_header(n, skb);
1190
Eric Dumazet030737b2013-10-19 11:42:54 -07001191 skb_headers_offset_update(n, newheadroom - oldheadroom);
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001192
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 return n;
1194}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001195EXPORT_SYMBOL(skb_copy_expand);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
1197/**
1198 * skb_pad - zero pad the tail of an skb
1199 * @skb: buffer to pad
1200 * @pad: space to pad
1201 *
1202 * Ensure that a buffer is followed by a padding area that is zero
1203 * filled. Used by network drivers which may DMA or transfer data
1204 * beyond the buffer end onto the wire.
1205 *
Herbert Xu5b057c62006-06-23 02:06:41 -07001206 * May return error in out of memory cases. The skb is freed on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001208
Herbert Xu5b057c62006-06-23 02:06:41 -07001209int skb_pad(struct sk_buff *skb, int pad)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210{
Herbert Xu5b057c62006-06-23 02:06:41 -07001211 int err;
1212 int ntail;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001213
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 /* If the skbuff is non linear tailroom is always zero.. */
Herbert Xu5b057c62006-06-23 02:06:41 -07001215 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 memset(skb->data+skb->len, 0, pad);
Herbert Xu5b057c62006-06-23 02:06:41 -07001217 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 }
Herbert Xu5b057c62006-06-23 02:06:41 -07001219
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001220 ntail = skb->data_len + pad - (skb->end - skb->tail);
Herbert Xu5b057c62006-06-23 02:06:41 -07001221 if (likely(skb_cloned(skb) || ntail > 0)) {
1222 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
1223 if (unlikely(err))
1224 goto free_skb;
1225 }
1226
1227 /* FIXME: The use of this function with non-linear skb's really needs
1228 * to be audited.
1229 */
1230 err = skb_linearize(skb);
1231 if (unlikely(err))
1232 goto free_skb;
1233
1234 memset(skb->data + skb->len, 0, pad);
1235 return 0;
1236
1237free_skb:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 kfree_skb(skb);
Herbert Xu5b057c62006-06-23 02:06:41 -07001239 return err;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001240}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001241EXPORT_SYMBOL(skb_pad);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001242
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -07001243/**
Mathias Krause0c7ddf32013-11-07 14:18:24 +01001244 * pskb_put - add data to the tail of a potentially fragmented buffer
1245 * @skb: start of the buffer to use
1246 * @tail: tail fragment of the buffer to use
1247 * @len: amount of data to add
1248 *
1249 * This function extends the used data area of the potentially
1250 * fragmented buffer. @tail must be the last fragment of @skb -- or
1251 * @skb itself. If this would exceed the total buffer size the kernel
1252 * will panic. A pointer to the first byte of the extra data is
1253 * returned.
1254 */
1255
1256unsigned char *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len)
1257{
1258 if (tail != skb) {
1259 skb->data_len += len;
1260 skb->len += len;
1261 }
1262 return skb_put(tail, len);
1263}
1264EXPORT_SYMBOL_GPL(pskb_put);
1265
1266/**
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -07001267 * skb_put - add data to a buffer
1268 * @skb: buffer to use
1269 * @len: amount of data to add
1270 *
1271 * This function extends the used data area of the buffer. If this would
1272 * exceed the total buffer size the kernel will panic. A pointer to the
1273 * first byte of the extra data is returned.
1274 */
1275unsigned char *skb_put(struct sk_buff *skb, unsigned int len)
1276{
1277 unsigned char *tmp = skb_tail_pointer(skb);
1278 SKB_LINEAR_ASSERT(skb);
1279 skb->tail += len;
1280 skb->len += len;
1281 if (unlikely(skb->tail > skb->end))
1282 skb_over_panic(skb, len, __builtin_return_address(0));
1283 return tmp;
1284}
1285EXPORT_SYMBOL(skb_put);
1286
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001287/**
Ilpo Järvinenc2aa2702008-03-27 17:52:40 -07001288 * skb_push - add data to the start of a buffer
1289 * @skb: buffer to use
1290 * @len: amount of data to add
1291 *
1292 * This function extends the used data area of the buffer at the buffer
1293 * start. If this would exceed the total buffer headroom the kernel will
1294 * panic. A pointer to the first byte of the extra data is returned.
1295 */
1296unsigned char *skb_push(struct sk_buff *skb, unsigned int len)
1297{
1298 skb->data -= len;
1299 skb->len += len;
1300 if (unlikely(skb->data<skb->head))
1301 skb_under_panic(skb, len, __builtin_return_address(0));
1302 return skb->data;
1303}
1304EXPORT_SYMBOL(skb_push);
1305
1306/**
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001307 * skb_pull - remove data from the start of a buffer
1308 * @skb: buffer to use
1309 * @len: amount of data to remove
1310 *
1311 * This function removes data from the start of a buffer, returning
1312 * the memory to the headroom. A pointer to the next data in the buffer
1313 * is returned. Once the data has been pulled future pushes will overwrite
1314 * the old data.
1315 */
1316unsigned char *skb_pull(struct sk_buff *skb, unsigned int len)
1317{
David S. Miller47d29642010-05-02 02:21:44 -07001318 return skb_pull_inline(skb, len);
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001319}
1320EXPORT_SYMBOL(skb_pull);
1321
Ilpo Järvinen419ae742008-03-27 17:54:01 -07001322/**
1323 * skb_trim - remove end from a buffer
1324 * @skb: buffer to alter
1325 * @len: new length
1326 *
1327 * Cut the length of a buffer down by removing data from the tail. If
1328 * the buffer is already under the length specified it is not modified.
1329 * The skb must be linear.
1330 */
1331void skb_trim(struct sk_buff *skb, unsigned int len)
1332{
1333 if (skb->len > len)
1334 __skb_trim(skb, len);
1335}
1336EXPORT_SYMBOL(skb_trim);
1337
Herbert Xu3cc0e872006-06-09 16:13:38 -07001338/* Trims skb to length len. It can change skb pointers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 */
1340
Herbert Xu3cc0e872006-06-09 16:13:38 -07001341int ___pskb_trim(struct sk_buff *skb, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342{
Herbert Xu27b437c2006-07-13 19:26:39 -07001343 struct sk_buff **fragp;
1344 struct sk_buff *frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 int offset = skb_headlen(skb);
1346 int nfrags = skb_shinfo(skb)->nr_frags;
1347 int i;
Herbert Xu27b437c2006-07-13 19:26:39 -07001348 int err;
1349
1350 if (skb_cloned(skb) &&
1351 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
1352 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001354 i = 0;
1355 if (offset >= len)
1356 goto drop_pages;
1357
1358 for (; i < nfrags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001359 int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]);
Herbert Xu27b437c2006-07-13 19:26:39 -07001360
1361 if (end < len) {
1362 offset = end;
1363 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 }
Herbert Xu27b437c2006-07-13 19:26:39 -07001365
Eric Dumazet9e903e02011-10-18 21:00:24 +00001366 skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset);
Herbert Xu27b437c2006-07-13 19:26:39 -07001367
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001368drop_pages:
Herbert Xu27b437c2006-07-13 19:26:39 -07001369 skb_shinfo(skb)->nr_frags = i;
1370
1371 for (; i < nfrags; i++)
Ian Campbellea2ab692011-08-22 23:44:58 +00001372 skb_frag_unref(skb, i);
Herbert Xu27b437c2006-07-13 19:26:39 -07001373
David S. Miller21dc3302010-08-23 00:13:46 -07001374 if (skb_has_frag_list(skb))
Herbert Xu27b437c2006-07-13 19:26:39 -07001375 skb_drop_fraglist(skb);
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001376 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 }
1378
Herbert Xu27b437c2006-07-13 19:26:39 -07001379 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
1380 fragp = &frag->next) {
1381 int end = offset + frag->len;
1382
1383 if (skb_shared(frag)) {
1384 struct sk_buff *nfrag;
1385
1386 nfrag = skb_clone(frag, GFP_ATOMIC);
1387 if (unlikely(!nfrag))
1388 return -ENOMEM;
1389
1390 nfrag->next = frag->next;
Eric Dumazet85bb2a62012-04-19 02:24:53 +00001391 consume_skb(frag);
Herbert Xu27b437c2006-07-13 19:26:39 -07001392 frag = nfrag;
1393 *fragp = frag;
1394 }
1395
1396 if (end < len) {
1397 offset = end;
1398 continue;
1399 }
1400
1401 if (end > len &&
1402 unlikely((err = pskb_trim(frag, len - offset))))
1403 return err;
1404
1405 if (frag->next)
1406 skb_drop_list(&frag->next);
1407 break;
1408 }
1409
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001410done:
Herbert Xu27b437c2006-07-13 19:26:39 -07001411 if (len > skb_headlen(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 skb->data_len -= skb->len - len;
1413 skb->len = len;
1414 } else {
Herbert Xu27b437c2006-07-13 19:26:39 -07001415 skb->len = len;
1416 skb->data_len = 0;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001417 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 }
1419
1420 return 0;
1421}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001422EXPORT_SYMBOL(___pskb_trim);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423
1424/**
1425 * __pskb_pull_tail - advance tail of skb header
1426 * @skb: buffer to reallocate
1427 * @delta: number of bytes to advance tail
1428 *
1429 * The function makes a sense only on a fragmented &sk_buff,
1430 * it expands header moving its tail forward and copying necessary
1431 * data from fragmented part.
1432 *
1433 * &sk_buff MUST have reference count of 1.
1434 *
1435 * Returns %NULL (and &sk_buff does not change) if pull failed
1436 * or value of new tail of skb in the case of success.
1437 *
1438 * All the pointers pointing into skb header may change and must be
1439 * reloaded after call to this function.
1440 */
1441
1442/* Moves tail of skb head forward, copying data from fragmented part,
1443 * when it is necessary.
1444 * 1. It may fail due to malloc failure.
1445 * 2. It may change skb pointers.
1446 *
1447 * It is pretty complicated. Luckily, it is called only in exceptional cases.
1448 */
1449unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
1450{
1451 /* If skb has not enough free space at tail, get new one
1452 * plus 128 bytes for future expansions. If we have enough
1453 * room at tail, reallocate without expansion only if skb is cloned.
1454 */
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001455 int i, k, eat = (skb->tail + delta) - skb->end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
1457 if (eat > 0 || skb_cloned(skb)) {
1458 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
1459 GFP_ATOMIC))
1460 return NULL;
1461 }
1462
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001463 if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 BUG();
1465
1466 /* Optimization: no fragments, no reasons to preestimate
1467 * size of pulled pages. Superb.
1468 */
David S. Miller21dc3302010-08-23 00:13:46 -07001469 if (!skb_has_frag_list(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 goto pull_pages;
1471
1472 /* Estimate size of pulled pages. */
1473 eat = delta;
1474 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001475 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1476
1477 if (size >= eat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 goto pull_pages;
Eric Dumazet9e903e02011-10-18 21:00:24 +00001479 eat -= size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 }
1481
1482 /* If we need update frag list, we are in troubles.
1483 * Certainly, it possible to add an offset to skb data,
1484 * but taking into account that pulling is expected to
1485 * be very rare operation, it is worth to fight against
1486 * further bloating skb head and crucify ourselves here instead.
1487 * Pure masohism, indeed. 8)8)
1488 */
1489 if (eat) {
1490 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1491 struct sk_buff *clone = NULL;
1492 struct sk_buff *insp = NULL;
1493
1494 do {
Kris Katterjohn09a62662006-01-08 22:24:28 -08001495 BUG_ON(!list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496
1497 if (list->len <= eat) {
1498 /* Eaten as whole. */
1499 eat -= list->len;
1500 list = list->next;
1501 insp = list;
1502 } else {
1503 /* Eaten partially. */
1504
1505 if (skb_shared(list)) {
1506 /* Sucks! We need to fork list. :-( */
1507 clone = skb_clone(list, GFP_ATOMIC);
1508 if (!clone)
1509 return NULL;
1510 insp = list->next;
1511 list = clone;
1512 } else {
1513 /* This may be pulled without
1514 * problems. */
1515 insp = list;
1516 }
1517 if (!pskb_pull(list, eat)) {
Wei Yongjunf3fbbe02009-02-25 00:37:32 +00001518 kfree_skb(clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 return NULL;
1520 }
1521 break;
1522 }
1523 } while (eat);
1524
1525 /* Free pulled out fragments. */
1526 while ((list = skb_shinfo(skb)->frag_list) != insp) {
1527 skb_shinfo(skb)->frag_list = list->next;
1528 kfree_skb(list);
1529 }
1530 /* And insert new clone at head. */
1531 if (clone) {
1532 clone->next = list;
1533 skb_shinfo(skb)->frag_list = clone;
1534 }
1535 }
1536 /* Success! Now we may commit changes to skb data. */
1537
1538pull_pages:
1539 eat = delta;
1540 k = 0;
1541 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001542 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1543
1544 if (size <= eat) {
Ian Campbellea2ab692011-08-22 23:44:58 +00001545 skb_frag_unref(skb, i);
Eric Dumazet9e903e02011-10-18 21:00:24 +00001546 eat -= size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 } else {
1548 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
1549 if (eat) {
1550 skb_shinfo(skb)->frags[k].page_offset += eat;
Eric Dumazet9e903e02011-10-18 21:00:24 +00001551 skb_frag_size_sub(&skb_shinfo(skb)->frags[k], eat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 eat = 0;
1553 }
1554 k++;
1555 }
1556 }
1557 skb_shinfo(skb)->nr_frags = k;
1558
1559 skb->tail += delta;
1560 skb->data_len -= delta;
1561
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001562 return skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001564EXPORT_SYMBOL(__pskb_pull_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565
Eric Dumazet22019b12011-07-29 18:37:31 +00001566/**
1567 * skb_copy_bits - copy bits from skb to kernel buffer
1568 * @skb: source skb
1569 * @offset: offset in source
1570 * @to: destination buffer
1571 * @len: number of bytes to copy
1572 *
1573 * Copy the specified number of bytes from the source skb to the
1574 * destination buffer.
1575 *
1576 * CAUTION ! :
1577 * If its prototype is ever changed,
1578 * check arch/{*}/net/{*}.S files,
1579 * since it is called from BPF assembly code.
1580 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
1582{
David S. Miller1a028e52007-04-27 15:21:23 -07001583 int start = skb_headlen(skb);
David S. Millerfbb398a2009-06-09 00:18:59 -07001584 struct sk_buff *frag_iter;
1585 int i, copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586
1587 if (offset > (int)skb->len - len)
1588 goto fault;
1589
1590 /* Copy header. */
David S. Miller1a028e52007-04-27 15:21:23 -07001591 if ((copy = start - offset) > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 if (copy > len)
1593 copy = len;
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001594 skb_copy_from_linear_data_offset(skb, offset, to, copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 if ((len -= copy) == 0)
1596 return 0;
1597 offset += copy;
1598 to += copy;
1599 }
1600
1601 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07001602 int end;
Eric Dumazet51c56b02012-04-05 11:35:15 +02001603 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001605 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07001606
Eric Dumazet51c56b02012-04-05 11:35:15 +02001607 end = start + skb_frag_size(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 if ((copy = end - offset) > 0) {
1609 u8 *vaddr;
1610
1611 if (copy > len)
1612 copy = len;
1613
Eric Dumazet51c56b02012-04-05 11:35:15 +02001614 vaddr = kmap_atomic(skb_frag_page(f));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 memcpy(to,
Eric Dumazet51c56b02012-04-05 11:35:15 +02001616 vaddr + f->page_offset + offset - start,
1617 copy);
1618 kunmap_atomic(vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619
1620 if ((len -= copy) == 0)
1621 return 0;
1622 offset += copy;
1623 to += copy;
1624 }
David S. Miller1a028e52007-04-27 15:21:23 -07001625 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 }
1627
David S. Millerfbb398a2009-06-09 00:18:59 -07001628 skb_walk_frags(skb, frag_iter) {
1629 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630
David S. Millerfbb398a2009-06-09 00:18:59 -07001631 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632
David S. Millerfbb398a2009-06-09 00:18:59 -07001633 end = start + frag_iter->len;
1634 if ((copy = end - offset) > 0) {
1635 if (copy > len)
1636 copy = len;
1637 if (skb_copy_bits(frag_iter, offset - start, to, copy))
1638 goto fault;
1639 if ((len -= copy) == 0)
1640 return 0;
1641 offset += copy;
1642 to += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 }
David S. Millerfbb398a2009-06-09 00:18:59 -07001644 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 }
Shirley Maa6686f22011-07-06 12:22:12 +00001646
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 if (!len)
1648 return 0;
1649
1650fault:
1651 return -EFAULT;
1652}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001653EXPORT_SYMBOL(skb_copy_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654
Jens Axboe9c55e012007-11-06 23:30:13 -08001655/*
1656 * Callback from splice_to_pipe(), if we need to release some pages
1657 * at the end of the spd in case we error'ed out in filling the pipe.
1658 */
1659static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
1660{
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001661 put_page(spd->pages[i]);
1662}
Jens Axboe9c55e012007-11-06 23:30:13 -08001663
David S. Millera108d5f2012-04-23 23:06:11 -04001664static struct page *linear_to_page(struct page *page, unsigned int *len,
1665 unsigned int *offset,
Eric Dumazet18aafc62013-01-11 14:46:37 +00001666 struct sock *sk)
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001667{
Eric Dumazet5640f762012-09-23 23:04:42 +00001668 struct page_frag *pfrag = sk_page_frag(sk);
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001669
Eric Dumazet5640f762012-09-23 23:04:42 +00001670 if (!sk_page_frag_refill(sk, pfrag))
1671 return NULL;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001672
Eric Dumazet5640f762012-09-23 23:04:42 +00001673 *len = min_t(unsigned int, *len, pfrag->size - pfrag->offset);
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001674
Eric Dumazet5640f762012-09-23 23:04:42 +00001675 memcpy(page_address(pfrag->page) + pfrag->offset,
1676 page_address(page) + *offset, *len);
1677 *offset = pfrag->offset;
1678 pfrag->offset += *len;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001679
Eric Dumazet5640f762012-09-23 23:04:42 +00001680 return pfrag->page;
Jens Axboe9c55e012007-11-06 23:30:13 -08001681}
1682
Eric Dumazet41c73a02012-04-22 12:26:16 +00001683static bool spd_can_coalesce(const struct splice_pipe_desc *spd,
1684 struct page *page,
1685 unsigned int offset)
1686{
1687 return spd->nr_pages &&
1688 spd->pages[spd->nr_pages - 1] == page &&
1689 (spd->partial[spd->nr_pages - 1].offset +
1690 spd->partial[spd->nr_pages - 1].len == offset);
1691}
1692
Jens Axboe9c55e012007-11-06 23:30:13 -08001693/*
1694 * Fill page/offset/length into spd, if it can hold more pages.
1695 */
David S. Millera108d5f2012-04-23 23:06:11 -04001696static bool spd_fill_page(struct splice_pipe_desc *spd,
1697 struct pipe_inode_info *pipe, struct page *page,
1698 unsigned int *len, unsigned int offset,
Eric Dumazet18aafc62013-01-11 14:46:37 +00001699 bool linear,
David S. Millera108d5f2012-04-23 23:06:11 -04001700 struct sock *sk)
Jens Axboe9c55e012007-11-06 23:30:13 -08001701{
Eric Dumazet41c73a02012-04-22 12:26:16 +00001702 if (unlikely(spd->nr_pages == MAX_SKB_FRAGS))
David S. Millera108d5f2012-04-23 23:06:11 -04001703 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08001704
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001705 if (linear) {
Eric Dumazet18aafc62013-01-11 14:46:37 +00001706 page = linear_to_page(page, len, &offset, sk);
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001707 if (!page)
David S. Millera108d5f2012-04-23 23:06:11 -04001708 return true;
Eric Dumazet41c73a02012-04-22 12:26:16 +00001709 }
1710 if (spd_can_coalesce(spd, page, offset)) {
1711 spd->partial[spd->nr_pages - 1].len += *len;
David S. Millera108d5f2012-04-23 23:06:11 -04001712 return false;
Eric Dumazet41c73a02012-04-22 12:26:16 +00001713 }
1714 get_page(page);
Jens Axboe9c55e012007-11-06 23:30:13 -08001715 spd->pages[spd->nr_pages] = page;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001716 spd->partial[spd->nr_pages].len = *len;
Jens Axboe9c55e012007-11-06 23:30:13 -08001717 spd->partial[spd->nr_pages].offset = offset;
Jens Axboe9c55e012007-11-06 23:30:13 -08001718 spd->nr_pages++;
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001719
David S. Millera108d5f2012-04-23 23:06:11 -04001720 return false;
Jens Axboe9c55e012007-11-06 23:30:13 -08001721}
1722
David S. Millera108d5f2012-04-23 23:06:11 -04001723static bool __splice_segment(struct page *page, unsigned int poff,
1724 unsigned int plen, unsigned int *off,
Eric Dumazet18aafc62013-01-11 14:46:37 +00001725 unsigned int *len,
Eric Dumazetd7ccf7c2012-04-23 23:35:04 -04001726 struct splice_pipe_desc *spd, bool linear,
David S. Millera108d5f2012-04-23 23:06:11 -04001727 struct sock *sk,
1728 struct pipe_inode_info *pipe)
Octavian Purdila2870c432008-07-15 00:49:11 -07001729{
1730 if (!*len)
David S. Millera108d5f2012-04-23 23:06:11 -04001731 return true;
Octavian Purdila2870c432008-07-15 00:49:11 -07001732
1733 /* skip this segment if already processed */
1734 if (*off >= plen) {
1735 *off -= plen;
David S. Millera108d5f2012-04-23 23:06:11 -04001736 return false;
Octavian Purdiladb43a282008-06-27 17:27:21 -07001737 }
Jens Axboe9c55e012007-11-06 23:30:13 -08001738
Octavian Purdila2870c432008-07-15 00:49:11 -07001739 /* ignore any bits we already processed */
Eric Dumazet9ca1b222013-01-05 21:31:18 +00001740 poff += *off;
1741 plen -= *off;
1742 *off = 0;
Octavian Purdila2870c432008-07-15 00:49:11 -07001743
Eric Dumazet18aafc62013-01-11 14:46:37 +00001744 do {
1745 unsigned int flen = min(*len, plen);
Octavian Purdila2870c432008-07-15 00:49:11 -07001746
Eric Dumazet18aafc62013-01-11 14:46:37 +00001747 if (spd_fill_page(spd, pipe, page, &flen, poff,
1748 linear, sk))
1749 return true;
1750 poff += flen;
1751 plen -= flen;
1752 *len -= flen;
1753 } while (*len && plen);
Octavian Purdila2870c432008-07-15 00:49:11 -07001754
David S. Millera108d5f2012-04-23 23:06:11 -04001755 return false;
Octavian Purdila2870c432008-07-15 00:49:11 -07001756}
1757
1758/*
David S. Millera108d5f2012-04-23 23:06:11 -04001759 * Map linear and fragment data from the skb to spd. It reports true if the
Octavian Purdila2870c432008-07-15 00:49:11 -07001760 * pipe is full or if we already spliced the requested length.
1761 */
David S. Millera108d5f2012-04-23 23:06:11 -04001762static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe,
1763 unsigned int *offset, unsigned int *len,
1764 struct splice_pipe_desc *spd, struct sock *sk)
Octavian Purdila2870c432008-07-15 00:49:11 -07001765{
1766 int seg;
1767
Eric Dumazet1d0c0b32012-04-27 02:10:03 +00001768 /* map the linear part :
Alexander Duyck2996d312012-05-02 18:18:42 +00001769 * If skb->head_frag is set, this 'linear' part is backed by a
1770 * fragment, and if the head is not shared with any clones then
1771 * we can avoid a copy since we own the head portion of this page.
Jens Axboe9c55e012007-11-06 23:30:13 -08001772 */
Octavian Purdila2870c432008-07-15 00:49:11 -07001773 if (__splice_segment(virt_to_page(skb->data),
1774 (unsigned long) skb->data & (PAGE_SIZE - 1),
1775 skb_headlen(skb),
Eric Dumazet18aafc62013-01-11 14:46:37 +00001776 offset, len, spd,
Alexander Duyck3a7c1ee42012-05-03 01:09:42 +00001777 skb_head_is_locked(skb),
Eric Dumazet1d0c0b32012-04-27 02:10:03 +00001778 sk, pipe))
David S. Millera108d5f2012-04-23 23:06:11 -04001779 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08001780
1781 /*
1782 * then map the fragments
1783 */
Jens Axboe9c55e012007-11-06 23:30:13 -08001784 for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
1785 const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
1786
Ian Campbellea2ab692011-08-22 23:44:58 +00001787 if (__splice_segment(skb_frag_page(f),
Eric Dumazet9e903e02011-10-18 21:00:24 +00001788 f->page_offset, skb_frag_size(f),
Eric Dumazet18aafc62013-01-11 14:46:37 +00001789 offset, len, spd, false, sk, pipe))
David S. Millera108d5f2012-04-23 23:06:11 -04001790 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08001791 }
1792
David S. Millera108d5f2012-04-23 23:06:11 -04001793 return false;
Jens Axboe9c55e012007-11-06 23:30:13 -08001794}
1795
1796/*
1797 * Map data from the skb to a pipe. Should handle both the linear part,
1798 * the fragments, and the frag list. It does NOT handle frag lists within
1799 * the frag list, if such a thing exists. We'd probably need to recurse to
1800 * handle that cleanly.
1801 */
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001802int skb_splice_bits(struct sk_buff *skb, unsigned int offset,
Jens Axboe9c55e012007-11-06 23:30:13 -08001803 struct pipe_inode_info *pipe, unsigned int tlen,
1804 unsigned int flags)
1805{
Eric Dumazet41c73a02012-04-22 12:26:16 +00001806 struct partial_page partial[MAX_SKB_FRAGS];
1807 struct page *pages[MAX_SKB_FRAGS];
Jens Axboe9c55e012007-11-06 23:30:13 -08001808 struct splice_pipe_desc spd = {
1809 .pages = pages,
1810 .partial = partial,
Eric Dumazet047fe362012-06-12 15:24:40 +02001811 .nr_pages_max = MAX_SKB_FRAGS,
Jens Axboe9c55e012007-11-06 23:30:13 -08001812 .flags = flags,
Miklos Szeredi28a625c2014-01-22 19:36:57 +01001813 .ops = &nosteal_pipe_buf_ops,
Jens Axboe9c55e012007-11-06 23:30:13 -08001814 .spd_release = sock_spd_release,
1815 };
David S. Millerfbb398a2009-06-09 00:18:59 -07001816 struct sk_buff *frag_iter;
Jarek Poplawski7a67e562009-04-30 05:41:19 -07001817 struct sock *sk = skb->sk;
Jens Axboe35f3d142010-05-20 10:43:18 +02001818 int ret = 0;
1819
Jens Axboe9c55e012007-11-06 23:30:13 -08001820 /*
1821 * __skb_splice_bits() only fails if the output has no room left,
1822 * so no point in going over the frag_list for the error case.
1823 */
Jens Axboe35f3d142010-05-20 10:43:18 +02001824 if (__skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk))
Jens Axboe9c55e012007-11-06 23:30:13 -08001825 goto done;
1826 else if (!tlen)
1827 goto done;
1828
1829 /*
1830 * now see if we have a frag_list to map
1831 */
David S. Millerfbb398a2009-06-09 00:18:59 -07001832 skb_walk_frags(skb, frag_iter) {
1833 if (!tlen)
1834 break;
Jens Axboe35f3d142010-05-20 10:43:18 +02001835 if (__skb_splice_bits(frag_iter, pipe, &offset, &tlen, &spd, sk))
David S. Millerfbb398a2009-06-09 00:18:59 -07001836 break;
Jens Axboe9c55e012007-11-06 23:30:13 -08001837 }
1838
1839done:
Jens Axboe9c55e012007-11-06 23:30:13 -08001840 if (spd.nr_pages) {
Jens Axboe9c55e012007-11-06 23:30:13 -08001841 /*
1842 * Drop the socket lock, otherwise we have reverse
1843 * locking dependencies between sk_lock and i_mutex
1844 * here as compared to sendfile(). We enter here
1845 * with the socket lock held, and splice_to_pipe() will
1846 * grab the pipe inode lock. For sendfile() emulation,
1847 * we call into ->sendpage() with the i_mutex lock held
1848 * and networking will grab the socket lock.
1849 */
Octavian Purdila293ad602008-06-04 15:45:58 -07001850 release_sock(sk);
Jens Axboe9c55e012007-11-06 23:30:13 -08001851 ret = splice_to_pipe(pipe, &spd);
Octavian Purdila293ad602008-06-04 15:45:58 -07001852 lock_sock(sk);
Jens Axboe9c55e012007-11-06 23:30:13 -08001853 }
1854
Jens Axboe35f3d142010-05-20 10:43:18 +02001855 return ret;
Jens Axboe9c55e012007-11-06 23:30:13 -08001856}
1857
Herbert Xu357b40a2005-04-19 22:30:14 -07001858/**
1859 * skb_store_bits - store bits from kernel buffer to skb
1860 * @skb: destination buffer
1861 * @offset: offset in destination
1862 * @from: source buffer
1863 * @len: number of bytes to copy
1864 *
1865 * Copy the specified number of bytes from the source buffer to the
1866 * destination skb. This function handles all the messy bits of
1867 * traversing fragment lists and such.
1868 */
1869
Stephen Hemminger0c6fcc82007-04-20 16:40:01 -07001870int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
Herbert Xu357b40a2005-04-19 22:30:14 -07001871{
David S. Miller1a028e52007-04-27 15:21:23 -07001872 int start = skb_headlen(skb);
David S. Millerfbb398a2009-06-09 00:18:59 -07001873 struct sk_buff *frag_iter;
1874 int i, copy;
Herbert Xu357b40a2005-04-19 22:30:14 -07001875
1876 if (offset > (int)skb->len - len)
1877 goto fault;
1878
David S. Miller1a028e52007-04-27 15:21:23 -07001879 if ((copy = start - offset) > 0) {
Herbert Xu357b40a2005-04-19 22:30:14 -07001880 if (copy > len)
1881 copy = len;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001882 skb_copy_to_linear_data_offset(skb, offset, from, copy);
Herbert Xu357b40a2005-04-19 22:30:14 -07001883 if ((len -= copy) == 0)
1884 return 0;
1885 offset += copy;
1886 from += copy;
1887 }
1888
1889 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1890 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
David S. Miller1a028e52007-04-27 15:21:23 -07001891 int end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001892
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001893 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07001894
Eric Dumazet9e903e02011-10-18 21:00:24 +00001895 end = start + skb_frag_size(frag);
Herbert Xu357b40a2005-04-19 22:30:14 -07001896 if ((copy = end - offset) > 0) {
1897 u8 *vaddr;
1898
1899 if (copy > len)
1900 copy = len;
1901
Eric Dumazet51c56b02012-04-05 11:35:15 +02001902 vaddr = kmap_atomic(skb_frag_page(frag));
David S. Miller1a028e52007-04-27 15:21:23 -07001903 memcpy(vaddr + frag->page_offset + offset - start,
1904 from, copy);
Eric Dumazet51c56b02012-04-05 11:35:15 +02001905 kunmap_atomic(vaddr);
Herbert Xu357b40a2005-04-19 22:30:14 -07001906
1907 if ((len -= copy) == 0)
1908 return 0;
1909 offset += copy;
1910 from += copy;
1911 }
David S. Miller1a028e52007-04-27 15:21:23 -07001912 start = end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001913 }
1914
David S. Millerfbb398a2009-06-09 00:18:59 -07001915 skb_walk_frags(skb, frag_iter) {
1916 int end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001917
David S. Millerfbb398a2009-06-09 00:18:59 -07001918 WARN_ON(start > offset + len);
Herbert Xu357b40a2005-04-19 22:30:14 -07001919
David S. Millerfbb398a2009-06-09 00:18:59 -07001920 end = start + frag_iter->len;
1921 if ((copy = end - offset) > 0) {
1922 if (copy > len)
1923 copy = len;
1924 if (skb_store_bits(frag_iter, offset - start,
1925 from, copy))
1926 goto fault;
1927 if ((len -= copy) == 0)
1928 return 0;
1929 offset += copy;
1930 from += copy;
Herbert Xu357b40a2005-04-19 22:30:14 -07001931 }
David S. Millerfbb398a2009-06-09 00:18:59 -07001932 start = end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001933 }
1934 if (!len)
1935 return 0;
1936
1937fault:
1938 return -EFAULT;
1939}
Herbert Xu357b40a2005-04-19 22:30:14 -07001940EXPORT_SYMBOL(skb_store_bits);
1941
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942/* Checksum skb data. */
Daniel Borkmann2817a332013-10-30 11:50:51 +01001943__wsum __skb_checksum(const struct sk_buff *skb, int offset, int len,
1944 __wsum csum, const struct skb_checksum_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945{
David S. Miller1a028e52007-04-27 15:21:23 -07001946 int start = skb_headlen(skb);
1947 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07001948 struct sk_buff *frag_iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 int pos = 0;
1950
1951 /* Checksum header. */
1952 if (copy > 0) {
1953 if (copy > len)
1954 copy = len;
Daniel Borkmann2817a332013-10-30 11:50:51 +01001955 csum = ops->update(skb->data + offset, copy, csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 if ((len -= copy) == 0)
1957 return csum;
1958 offset += copy;
1959 pos = copy;
1960 }
1961
1962 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07001963 int end;
Eric Dumazet51c56b02012-04-05 11:35:15 +02001964 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001966 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07001967
Eric Dumazet51c56b02012-04-05 11:35:15 +02001968 end = start + skb_frag_size(frag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 if ((copy = end - offset) > 0) {
Al Viro44bb9362006-11-14 21:36:14 -08001970 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 u8 *vaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972
1973 if (copy > len)
1974 copy = len;
Eric Dumazet51c56b02012-04-05 11:35:15 +02001975 vaddr = kmap_atomic(skb_frag_page(frag));
Daniel Borkmann2817a332013-10-30 11:50:51 +01001976 csum2 = ops->update(vaddr + frag->page_offset +
1977 offset - start, copy, 0);
Eric Dumazet51c56b02012-04-05 11:35:15 +02001978 kunmap_atomic(vaddr);
Daniel Borkmann2817a332013-10-30 11:50:51 +01001979 csum = ops->combine(csum, csum2, pos, copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 if (!(len -= copy))
1981 return csum;
1982 offset += copy;
1983 pos += copy;
1984 }
David S. Miller1a028e52007-04-27 15:21:23 -07001985 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 }
1987
David S. Millerfbb398a2009-06-09 00:18:59 -07001988 skb_walk_frags(skb, frag_iter) {
1989 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990
David S. Millerfbb398a2009-06-09 00:18:59 -07001991 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992
David S. Millerfbb398a2009-06-09 00:18:59 -07001993 end = start + frag_iter->len;
1994 if ((copy = end - offset) > 0) {
1995 __wsum csum2;
1996 if (copy > len)
1997 copy = len;
Daniel Borkmann2817a332013-10-30 11:50:51 +01001998 csum2 = __skb_checksum(frag_iter, offset - start,
1999 copy, 0, ops);
2000 csum = ops->combine(csum, csum2, pos, copy);
David S. Millerfbb398a2009-06-09 00:18:59 -07002001 if ((len -= copy) == 0)
2002 return csum;
2003 offset += copy;
2004 pos += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002006 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08002008 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009
2010 return csum;
2011}
Daniel Borkmann2817a332013-10-30 11:50:51 +01002012EXPORT_SYMBOL(__skb_checksum);
2013
2014__wsum skb_checksum(const struct sk_buff *skb, int offset,
2015 int len, __wsum csum)
2016{
2017 const struct skb_checksum_ops ops = {
Daniel Borkmanncea80ea82013-11-04 17:10:25 +01002018 .update = csum_partial_ext,
Daniel Borkmann2817a332013-10-30 11:50:51 +01002019 .combine = csum_block_add_ext,
2020 };
2021
2022 return __skb_checksum(skb, offset, len, csum, &ops);
2023}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002024EXPORT_SYMBOL(skb_checksum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025
2026/* Both of above in one bottle. */
2027
Al Viro81d77662006-11-14 21:37:33 -08002028__wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
2029 u8 *to, int len, __wsum csum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030{
David S. Miller1a028e52007-04-27 15:21:23 -07002031 int start = skb_headlen(skb);
2032 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07002033 struct sk_buff *frag_iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 int pos = 0;
2035
2036 /* Copy header. */
2037 if (copy > 0) {
2038 if (copy > len)
2039 copy = len;
2040 csum = csum_partial_copy_nocheck(skb->data + offset, to,
2041 copy, csum);
2042 if ((len -= copy) == 0)
2043 return csum;
2044 offset += copy;
2045 to += copy;
2046 pos = copy;
2047 }
2048
2049 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07002050 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002052 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07002053
Eric Dumazet9e903e02011-10-18 21:00:24 +00002054 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 if ((copy = end - offset) > 0) {
Al Viro50842052006-11-14 21:36:34 -08002056 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 u8 *vaddr;
2058 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2059
2060 if (copy > len)
2061 copy = len;
Eric Dumazet51c56b02012-04-05 11:35:15 +02002062 vaddr = kmap_atomic(skb_frag_page(frag));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 csum2 = csum_partial_copy_nocheck(vaddr +
David S. Miller1a028e52007-04-27 15:21:23 -07002064 frag->page_offset +
2065 offset - start, to,
2066 copy, 0);
Eric Dumazet51c56b02012-04-05 11:35:15 +02002067 kunmap_atomic(vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 csum = csum_block_add(csum, csum2, pos);
2069 if (!(len -= copy))
2070 return csum;
2071 offset += copy;
2072 to += copy;
2073 pos += copy;
2074 }
David S. Miller1a028e52007-04-27 15:21:23 -07002075 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 }
2077
David S. Millerfbb398a2009-06-09 00:18:59 -07002078 skb_walk_frags(skb, frag_iter) {
2079 __wsum csum2;
2080 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081
David S. Millerfbb398a2009-06-09 00:18:59 -07002082 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083
David S. Millerfbb398a2009-06-09 00:18:59 -07002084 end = start + frag_iter->len;
2085 if ((copy = end - offset) > 0) {
2086 if (copy > len)
2087 copy = len;
2088 csum2 = skb_copy_and_csum_bits(frag_iter,
2089 offset - start,
2090 to, copy, 0);
2091 csum = csum_block_add(csum, csum2, pos);
2092 if ((len -= copy) == 0)
2093 return csum;
2094 offset += copy;
2095 to += copy;
2096 pos += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002098 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08002100 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 return csum;
2102}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002103EXPORT_SYMBOL(skb_copy_and_csum_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104
Thomas Grafaf2806f2013-12-13 15:22:17 +01002105 /**
2106 * skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy()
2107 * @from: source buffer
2108 *
2109 * Calculates the amount of linear headroom needed in the 'to' skb passed
2110 * into skb_zerocopy().
2111 */
2112unsigned int
2113skb_zerocopy_headlen(const struct sk_buff *from)
2114{
2115 unsigned int hlen = 0;
2116
2117 if (!from->head_frag ||
2118 skb_headlen(from) < L1_CACHE_BYTES ||
2119 skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
2120 hlen = skb_headlen(from);
2121
2122 if (skb_has_frag_list(from))
2123 hlen = from->len;
2124
2125 return hlen;
2126}
2127EXPORT_SYMBOL_GPL(skb_zerocopy_headlen);
2128
2129/**
2130 * skb_zerocopy - Zero copy skb to skb
2131 * @to: destination buffer
Masanari Iida7fceb4d2014-01-29 01:05:28 +09002132 * @from: source buffer
Thomas Grafaf2806f2013-12-13 15:22:17 +01002133 * @len: number of bytes to copy from source buffer
2134 * @hlen: size of linear headroom in destination buffer
2135 *
2136 * Copies up to `len` bytes from `from` to `to` by creating references
2137 * to the frags in the source buffer.
2138 *
2139 * The `hlen` as calculated by skb_zerocopy_headlen() specifies the
2140 * headroom in the `to` buffer.
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002141 *
2142 * Return value:
2143 * 0: everything is OK
2144 * -ENOMEM: couldn't orphan frags of @from due to lack of memory
2145 * -EFAULT: skb_copy_bits() found some problem with skb geometry
Thomas Grafaf2806f2013-12-13 15:22:17 +01002146 */
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002147int
2148skb_zerocopy(struct sk_buff *to, struct sk_buff *from, int len, int hlen)
Thomas Grafaf2806f2013-12-13 15:22:17 +01002149{
2150 int i, j = 0;
2151 int plen = 0; /* length of skb->head fragment */
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002152 int ret;
Thomas Grafaf2806f2013-12-13 15:22:17 +01002153 struct page *page;
2154 unsigned int offset;
2155
2156 BUG_ON(!from->head_frag && !hlen);
2157
2158 /* dont bother with small payloads */
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002159 if (len <= skb_tailroom(to))
2160 return skb_copy_bits(from, 0, skb_put(to, len), len);
Thomas Grafaf2806f2013-12-13 15:22:17 +01002161
2162 if (hlen) {
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002163 ret = skb_copy_bits(from, 0, skb_put(to, hlen), hlen);
2164 if (unlikely(ret))
2165 return ret;
Thomas Grafaf2806f2013-12-13 15:22:17 +01002166 len -= hlen;
2167 } else {
2168 plen = min_t(int, skb_headlen(from), len);
2169 if (plen) {
2170 page = virt_to_head_page(from->head);
2171 offset = from->data - (unsigned char *)page_address(page);
2172 __skb_fill_page_desc(to, 0, page, offset, plen);
2173 get_page(page);
2174 j = 1;
2175 len -= plen;
2176 }
2177 }
2178
2179 to->truesize += len + plen;
2180 to->len += len + plen;
2181 to->data_len += len + plen;
2182
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002183 if (unlikely(skb_orphan_frags(from, GFP_ATOMIC))) {
2184 skb_tx_error(from);
2185 return -ENOMEM;
2186 }
2187
Thomas Grafaf2806f2013-12-13 15:22:17 +01002188 for (i = 0; i < skb_shinfo(from)->nr_frags; i++) {
2189 if (!len)
2190 break;
2191 skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i];
2192 skb_shinfo(to)->frags[j].size = min_t(int, skb_shinfo(to)->frags[j].size, len);
2193 len -= skb_shinfo(to)->frags[j].size;
2194 skb_frag_ref(to, j);
2195 j++;
2196 }
2197 skb_shinfo(to)->nr_frags = j;
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002198
2199 return 0;
Thomas Grafaf2806f2013-12-13 15:22:17 +01002200}
2201EXPORT_SYMBOL_GPL(skb_zerocopy);
2202
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
2204{
Al Virod3bc23e2006-11-14 21:24:49 -08002205 __wsum csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 long csstart;
2207
Patrick McHardy84fa7932006-08-29 16:44:56 -07002208 if (skb->ip_summed == CHECKSUM_PARTIAL)
Michał Mirosław55508d62010-12-14 15:24:08 +00002209 csstart = skb_checksum_start_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 else
2211 csstart = skb_headlen(skb);
2212
Kris Katterjohn09a62662006-01-08 22:24:28 -08002213 BUG_ON(csstart > skb_headlen(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03002215 skb_copy_from_linear_data(skb, to, csstart);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216
2217 csum = 0;
2218 if (csstart != skb->len)
2219 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
2220 skb->len - csstart, 0);
2221
Patrick McHardy84fa7932006-08-29 16:44:56 -07002222 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Al Viroff1dcad2006-11-20 18:07:29 -08002223 long csstuff = csstart + skb->csum_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224
Al Virod3bc23e2006-11-14 21:24:49 -08002225 *((__sum16 *)(to + csstuff)) = csum_fold(csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 }
2227}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002228EXPORT_SYMBOL(skb_copy_and_csum_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229
2230/**
2231 * skb_dequeue - remove from the head of the queue
2232 * @list: list to dequeue from
2233 *
2234 * Remove the head of the list. The list lock is taken so the function
2235 * may be used safely with other locking list functions. The head item is
2236 * returned or %NULL if the list is empty.
2237 */
2238
2239struct sk_buff *skb_dequeue(struct sk_buff_head *list)
2240{
2241 unsigned long flags;
2242 struct sk_buff *result;
2243
2244 spin_lock_irqsave(&list->lock, flags);
2245 result = __skb_dequeue(list);
2246 spin_unlock_irqrestore(&list->lock, flags);
2247 return result;
2248}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002249EXPORT_SYMBOL(skb_dequeue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250
2251/**
2252 * skb_dequeue_tail - remove from the tail of the queue
2253 * @list: list to dequeue from
2254 *
2255 * Remove the tail of the list. The list lock is taken so the function
2256 * may be used safely with other locking list functions. The tail item is
2257 * returned or %NULL if the list is empty.
2258 */
2259struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
2260{
2261 unsigned long flags;
2262 struct sk_buff *result;
2263
2264 spin_lock_irqsave(&list->lock, flags);
2265 result = __skb_dequeue_tail(list);
2266 spin_unlock_irqrestore(&list->lock, flags);
2267 return result;
2268}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002269EXPORT_SYMBOL(skb_dequeue_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270
2271/**
2272 * skb_queue_purge - empty a list
2273 * @list: list to empty
2274 *
2275 * Delete all buffers on an &sk_buff list. Each buffer is removed from
2276 * the list and one reference dropped. This function takes the list
2277 * lock and is atomic with respect to other list locking functions.
2278 */
2279void skb_queue_purge(struct sk_buff_head *list)
2280{
2281 struct sk_buff *skb;
2282 while ((skb = skb_dequeue(list)) != NULL)
2283 kfree_skb(skb);
2284}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002285EXPORT_SYMBOL(skb_queue_purge);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286
2287/**
2288 * skb_queue_head - queue a buffer at the list head
2289 * @list: list to use
2290 * @newsk: buffer to queue
2291 *
2292 * Queue a buffer at the start of the list. This function takes the
2293 * list lock and can be used safely with other locking &sk_buff functions
2294 * safely.
2295 *
2296 * A buffer cannot be placed on two lists at the same time.
2297 */
2298void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
2299{
2300 unsigned long flags;
2301
2302 spin_lock_irqsave(&list->lock, flags);
2303 __skb_queue_head(list, newsk);
2304 spin_unlock_irqrestore(&list->lock, flags);
2305}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002306EXPORT_SYMBOL(skb_queue_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307
2308/**
2309 * skb_queue_tail - queue a buffer at the list tail
2310 * @list: list to use
2311 * @newsk: buffer to queue
2312 *
2313 * Queue a buffer at the tail of the list. This function takes the
2314 * list lock and can be used safely with other locking &sk_buff functions
2315 * safely.
2316 *
2317 * A buffer cannot be placed on two lists at the same time.
2318 */
2319void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
2320{
2321 unsigned long flags;
2322
2323 spin_lock_irqsave(&list->lock, flags);
2324 __skb_queue_tail(list, newsk);
2325 spin_unlock_irqrestore(&list->lock, flags);
2326}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002327EXPORT_SYMBOL(skb_queue_tail);
David S. Miller8728b832005-08-09 19:25:21 -07002328
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329/**
2330 * skb_unlink - remove a buffer from a list
2331 * @skb: buffer to remove
David S. Miller8728b832005-08-09 19:25:21 -07002332 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 *
David S. Miller8728b832005-08-09 19:25:21 -07002334 * Remove a packet from a list. The list locks are taken and this
2335 * function is atomic with respect to other list locked calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 *
David S. Miller8728b832005-08-09 19:25:21 -07002337 * You must know what list the SKB is on.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 */
David S. Miller8728b832005-08-09 19:25:21 -07002339void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340{
David S. Miller8728b832005-08-09 19:25:21 -07002341 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342
David S. Miller8728b832005-08-09 19:25:21 -07002343 spin_lock_irqsave(&list->lock, flags);
2344 __skb_unlink(skb, list);
2345 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002347EXPORT_SYMBOL(skb_unlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349/**
2350 * skb_append - append a buffer
2351 * @old: buffer to insert after
2352 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07002353 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 *
2355 * Place a packet after a given packet in a list. The list locks are taken
2356 * and this function is atomic with respect to other list locked calls.
2357 * A buffer cannot be placed on two lists at the same time.
2358 */
David S. Miller8728b832005-08-09 19:25:21 -07002359void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360{
2361 unsigned long flags;
2362
David S. Miller8728b832005-08-09 19:25:21 -07002363 spin_lock_irqsave(&list->lock, flags);
Gerrit Renker7de6c032008-04-14 00:05:09 -07002364 __skb_queue_after(list, old, newsk);
David S. Miller8728b832005-08-09 19:25:21 -07002365 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002367EXPORT_SYMBOL(skb_append);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368
2369/**
2370 * skb_insert - insert a buffer
2371 * @old: buffer to insert before
2372 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07002373 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 *
David S. Miller8728b832005-08-09 19:25:21 -07002375 * Place a packet before a given packet in a list. The list locks are
2376 * taken and this function is atomic with respect to other list locked
2377 * calls.
2378 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 * A buffer cannot be placed on two lists at the same time.
2380 */
David S. Miller8728b832005-08-09 19:25:21 -07002381void skb_insert(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);
2386 __skb_insert(newsk, old->prev, old, list);
2387 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_insert);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391static inline void skb_split_inside_header(struct sk_buff *skb,
2392 struct sk_buff* skb1,
2393 const u32 len, const int pos)
2394{
2395 int i;
2396
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03002397 skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
2398 pos - len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 /* And move data appendix as is. */
2400 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
2401 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
2402
2403 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
2404 skb_shinfo(skb)->nr_frags = 0;
2405 skb1->data_len = skb->data_len;
2406 skb1->len += skb1->data_len;
2407 skb->data_len = 0;
2408 skb->len = len;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002409 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410}
2411
2412static inline void skb_split_no_header(struct sk_buff *skb,
2413 struct sk_buff* skb1,
2414 const u32 len, int pos)
2415{
2416 int i, k = 0;
2417 const int nfrags = skb_shinfo(skb)->nr_frags;
2418
2419 skb_shinfo(skb)->nr_frags = 0;
2420 skb1->len = skb1->data_len = skb->len - len;
2421 skb->len = len;
2422 skb->data_len = len - pos;
2423
2424 for (i = 0; i < nfrags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00002425 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426
2427 if (pos + size > len) {
2428 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
2429
2430 if (pos < len) {
2431 /* Split frag.
2432 * We have two variants in this case:
2433 * 1. Move all the frag to the second
2434 * part, if it is possible. F.e.
2435 * this approach is mandatory for TUX,
2436 * where splitting is expensive.
2437 * 2. Split is accurately. We make this.
2438 */
Ian Campbellea2ab692011-08-22 23:44:58 +00002439 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002441 skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos);
2442 skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 skb_shinfo(skb)->nr_frags++;
2444 }
2445 k++;
2446 } else
2447 skb_shinfo(skb)->nr_frags++;
2448 pos += size;
2449 }
2450 skb_shinfo(skb1)->nr_frags = k;
2451}
2452
2453/**
2454 * skb_split - Split fragmented skb to two parts at length len.
2455 * @skb: the buffer to split
2456 * @skb1: the buffer to receive the second part
2457 * @len: new length for skb
2458 */
2459void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
2460{
2461 int pos = skb_headlen(skb);
2462
Amerigo Wang68534c62013-02-19 22:51:30 +00002463 skb_shinfo(skb1)->tx_flags = skb_shinfo(skb)->tx_flags & SKBTX_SHARED_FRAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 if (len < pos) /* Split line is inside header. */
2465 skb_split_inside_header(skb, skb1, len, pos);
2466 else /* Second chunk has no header, nothing to copy. */
2467 skb_split_no_header(skb, skb1, len, pos);
2468}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002469EXPORT_SYMBOL(skb_split);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470
Ilpo Järvinen9f782db2008-11-25 13:57:01 -08002471/* Shifting from/to a cloned skb is a no-go.
2472 *
2473 * Caller cannot keep skb_shinfo related pointers past calling here!
2474 */
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002475static int skb_prepare_for_shift(struct sk_buff *skb)
2476{
Ilpo Järvinen0ace2852008-11-24 21:30:21 -08002477 return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002478}
2479
2480/**
2481 * skb_shift - Shifts paged data partially from skb to another
2482 * @tgt: buffer into which tail data gets added
2483 * @skb: buffer from which the paged data comes from
2484 * @shiftlen: shift up to this many bytes
2485 *
2486 * Attempts to shift up to shiftlen worth of bytes, which may be less than
Feng King20e994a2011-11-21 01:47:11 +00002487 * the length of the skb, from skb to tgt. Returns number bytes shifted.
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002488 * It's up to caller to free skb if everything was shifted.
2489 *
2490 * If @tgt runs out of frags, the whole operation is aborted.
2491 *
2492 * Skb cannot include anything else but paged data while tgt is allowed
2493 * to have non-paged data as well.
2494 *
2495 * TODO: full sized shift could be optimized but that would need
2496 * specialized skb free'er to handle frags without up-to-date nr_frags.
2497 */
2498int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
2499{
2500 int from, to, merge, todo;
2501 struct skb_frag_struct *fragfrom, *fragto;
2502
2503 BUG_ON(shiftlen > skb->len);
2504 BUG_ON(skb_headlen(skb)); /* Would corrupt stream */
2505
2506 todo = shiftlen;
2507 from = 0;
2508 to = skb_shinfo(tgt)->nr_frags;
2509 fragfrom = &skb_shinfo(skb)->frags[from];
2510
2511 /* Actual merge is delayed until the point when we know we can
2512 * commit all, so that we don't have to undo partial changes
2513 */
2514 if (!to ||
Ian Campbellea2ab692011-08-22 23:44:58 +00002515 !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom),
2516 fragfrom->page_offset)) {
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002517 merge = -1;
2518 } else {
2519 merge = to - 1;
2520
Eric Dumazet9e903e02011-10-18 21:00:24 +00002521 todo -= skb_frag_size(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002522 if (todo < 0) {
2523 if (skb_prepare_for_shift(skb) ||
2524 skb_prepare_for_shift(tgt))
2525 return 0;
2526
Ilpo Järvinen9f782db2008-11-25 13:57:01 -08002527 /* All previous frag pointers might be stale! */
2528 fragfrom = &skb_shinfo(skb)->frags[from];
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002529 fragto = &skb_shinfo(tgt)->frags[merge];
2530
Eric Dumazet9e903e02011-10-18 21:00:24 +00002531 skb_frag_size_add(fragto, shiftlen);
2532 skb_frag_size_sub(fragfrom, shiftlen);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002533 fragfrom->page_offset += shiftlen;
2534
2535 goto onlymerged;
2536 }
2537
2538 from++;
2539 }
2540
2541 /* Skip full, not-fitting skb to avoid expensive operations */
2542 if ((shiftlen == skb->len) &&
2543 (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to))
2544 return 0;
2545
2546 if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt))
2547 return 0;
2548
2549 while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) {
2550 if (to == MAX_SKB_FRAGS)
2551 return 0;
2552
2553 fragfrom = &skb_shinfo(skb)->frags[from];
2554 fragto = &skb_shinfo(tgt)->frags[to];
2555
Eric Dumazet9e903e02011-10-18 21:00:24 +00002556 if (todo >= skb_frag_size(fragfrom)) {
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002557 *fragto = *fragfrom;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002558 todo -= skb_frag_size(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002559 from++;
2560 to++;
2561
2562 } else {
Ian Campbellea2ab692011-08-22 23:44:58 +00002563 __skb_frag_ref(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002564 fragto->page = fragfrom->page;
2565 fragto->page_offset = fragfrom->page_offset;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002566 skb_frag_size_set(fragto, todo);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002567
2568 fragfrom->page_offset += todo;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002569 skb_frag_size_sub(fragfrom, todo);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002570 todo = 0;
2571
2572 to++;
2573 break;
2574 }
2575 }
2576
2577 /* Ready to "commit" this state change to tgt */
2578 skb_shinfo(tgt)->nr_frags = to;
2579
2580 if (merge >= 0) {
2581 fragfrom = &skb_shinfo(skb)->frags[0];
2582 fragto = &skb_shinfo(tgt)->frags[merge];
2583
Eric Dumazet9e903e02011-10-18 21:00:24 +00002584 skb_frag_size_add(fragto, skb_frag_size(fragfrom));
Ian Campbellea2ab692011-08-22 23:44:58 +00002585 __skb_frag_unref(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002586 }
2587
2588 /* Reposition in the original skb */
2589 to = 0;
2590 while (from < skb_shinfo(skb)->nr_frags)
2591 skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++];
2592 skb_shinfo(skb)->nr_frags = to;
2593
2594 BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags);
2595
2596onlymerged:
2597 /* Most likely the tgt won't ever need its checksum anymore, skb on
2598 * the other hand might need it if it needs to be resent
2599 */
2600 tgt->ip_summed = CHECKSUM_PARTIAL;
2601 skb->ip_summed = CHECKSUM_PARTIAL;
2602
2603 /* Yak, is it really working this way? Some helper please? */
2604 skb->len -= shiftlen;
2605 skb->data_len -= shiftlen;
2606 skb->truesize -= shiftlen;
2607 tgt->len += shiftlen;
2608 tgt->data_len += shiftlen;
2609 tgt->truesize += shiftlen;
2610
2611 return shiftlen;
2612}
2613
Thomas Graf677e90e2005-06-23 20:59:51 -07002614/**
2615 * skb_prepare_seq_read - Prepare a sequential read of skb data
2616 * @skb: the buffer to read
2617 * @from: lower offset of data to be read
2618 * @to: upper offset of data to be read
2619 * @st: state variable
2620 *
2621 * Initializes the specified state variable. Must be called before
2622 * invoking skb_seq_read() for the first time.
2623 */
2624void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
2625 unsigned int to, struct skb_seq_state *st)
2626{
2627 st->lower_offset = from;
2628 st->upper_offset = to;
2629 st->root_skb = st->cur_skb = skb;
2630 st->frag_idx = st->stepped_offset = 0;
2631 st->frag_data = NULL;
2632}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002633EXPORT_SYMBOL(skb_prepare_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07002634
2635/**
2636 * skb_seq_read - Sequentially read skb data
2637 * @consumed: number of bytes consumed by the caller so far
2638 * @data: destination pointer for data to be returned
2639 * @st: state variable
2640 *
Mathias Krausebc323832013-11-07 14:18:26 +01002641 * Reads a block of skb data at @consumed relative to the
Thomas Graf677e90e2005-06-23 20:59:51 -07002642 * lower offset specified to skb_prepare_seq_read(). Assigns
Mathias Krausebc323832013-11-07 14:18:26 +01002643 * the head of the data block to @data and returns the length
Thomas Graf677e90e2005-06-23 20:59:51 -07002644 * of the block or 0 if the end of the skb data or the upper
2645 * offset has been reached.
2646 *
2647 * The caller is not required to consume all of the data
Mathias Krausebc323832013-11-07 14:18:26 +01002648 * returned, i.e. @consumed is typically set to the number
Thomas Graf677e90e2005-06-23 20:59:51 -07002649 * of bytes already consumed and the next call to
2650 * skb_seq_read() will return the remaining part of the block.
2651 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002652 * Note 1: The size of each block of data returned can be arbitrary,
Masanari Iidae793c0f2014-09-04 23:44:36 +09002653 * this limitation is the cost for zerocopy sequential
Thomas Graf677e90e2005-06-23 20:59:51 -07002654 * reads of potentially non linear data.
2655 *
Randy Dunlapbc2cda12008-02-13 15:03:25 -08002656 * Note 2: Fragment lists within fragments are not implemented
Thomas Graf677e90e2005-06-23 20:59:51 -07002657 * at the moment, state->root_skb could be replaced with
2658 * a stack for this purpose.
2659 */
2660unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
2661 struct skb_seq_state *st)
2662{
2663 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
2664 skb_frag_t *frag;
2665
Wedson Almeida Filhoaeb193e2013-06-23 23:33:48 -07002666 if (unlikely(abs_offset >= st->upper_offset)) {
2667 if (st->frag_data) {
2668 kunmap_atomic(st->frag_data);
2669 st->frag_data = NULL;
2670 }
Thomas Graf677e90e2005-06-23 20:59:51 -07002671 return 0;
Wedson Almeida Filhoaeb193e2013-06-23 23:33:48 -07002672 }
Thomas Graf677e90e2005-06-23 20:59:51 -07002673
2674next_skb:
Herbert Xu95e3b242009-01-29 16:07:52 -08002675 block_limit = skb_headlen(st->cur_skb) + st->stepped_offset;
Thomas Graf677e90e2005-06-23 20:59:51 -07002676
Thomas Chenault995b3372009-05-18 21:43:27 -07002677 if (abs_offset < block_limit && !st->frag_data) {
Herbert Xu95e3b242009-01-29 16:07:52 -08002678 *data = st->cur_skb->data + (abs_offset - st->stepped_offset);
Thomas Graf677e90e2005-06-23 20:59:51 -07002679 return block_limit - abs_offset;
2680 }
2681
2682 if (st->frag_idx == 0 && !st->frag_data)
2683 st->stepped_offset += skb_headlen(st->cur_skb);
2684
2685 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
2686 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
Eric Dumazet9e903e02011-10-18 21:00:24 +00002687 block_limit = skb_frag_size(frag) + st->stepped_offset;
Thomas Graf677e90e2005-06-23 20:59:51 -07002688
2689 if (abs_offset < block_limit) {
2690 if (!st->frag_data)
Eric Dumazet51c56b02012-04-05 11:35:15 +02002691 st->frag_data = kmap_atomic(skb_frag_page(frag));
Thomas Graf677e90e2005-06-23 20:59:51 -07002692
2693 *data = (u8 *) st->frag_data + frag->page_offset +
2694 (abs_offset - st->stepped_offset);
2695
2696 return block_limit - abs_offset;
2697 }
2698
2699 if (st->frag_data) {
Eric Dumazet51c56b02012-04-05 11:35:15 +02002700 kunmap_atomic(st->frag_data);
Thomas Graf677e90e2005-06-23 20:59:51 -07002701 st->frag_data = NULL;
2702 }
2703
2704 st->frag_idx++;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002705 st->stepped_offset += skb_frag_size(frag);
Thomas Graf677e90e2005-06-23 20:59:51 -07002706 }
2707
Olaf Kirch5b5a60d2007-06-23 23:11:52 -07002708 if (st->frag_data) {
Eric Dumazet51c56b02012-04-05 11:35:15 +02002709 kunmap_atomic(st->frag_data);
Olaf Kirch5b5a60d2007-06-23 23:11:52 -07002710 st->frag_data = NULL;
2711 }
2712
David S. Miller21dc3302010-08-23 00:13:46 -07002713 if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) {
Shyam Iyer71b33462009-01-29 16:12:42 -08002714 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
Thomas Graf677e90e2005-06-23 20:59:51 -07002715 st->frag_idx = 0;
2716 goto next_skb;
Shyam Iyer71b33462009-01-29 16:12:42 -08002717 } else if (st->cur_skb->next) {
2718 st->cur_skb = st->cur_skb->next;
Herbert Xu95e3b242009-01-29 16:07:52 -08002719 st->frag_idx = 0;
Thomas Graf677e90e2005-06-23 20:59:51 -07002720 goto next_skb;
2721 }
2722
2723 return 0;
2724}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002725EXPORT_SYMBOL(skb_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07002726
2727/**
2728 * skb_abort_seq_read - Abort a sequential read of skb data
2729 * @st: state variable
2730 *
2731 * Must be called if skb_seq_read() was not called until it
2732 * returned 0.
2733 */
2734void skb_abort_seq_read(struct skb_seq_state *st)
2735{
2736 if (st->frag_data)
Eric Dumazet51c56b02012-04-05 11:35:15 +02002737 kunmap_atomic(st->frag_data);
Thomas Graf677e90e2005-06-23 20:59:51 -07002738}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002739EXPORT_SYMBOL(skb_abort_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07002740
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002741#define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
2742
2743static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
2744 struct ts_config *conf,
2745 struct ts_state *state)
2746{
2747 return skb_seq_read(offset, text, TS_SKB_CB(state));
2748}
2749
2750static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
2751{
2752 skb_abort_seq_read(TS_SKB_CB(state));
2753}
2754
2755/**
2756 * skb_find_text - Find a text pattern in skb data
2757 * @skb: the buffer to look in
2758 * @from: search offset
2759 * @to: search limit
2760 * @config: textsearch configuration
2761 * @state: uninitialized textsearch state variable
2762 *
2763 * Finds a pattern in the skb data according to the specified
2764 * textsearch configuration. Use textsearch_next() to retrieve
2765 * subsequent occurrences of the pattern. Returns the offset
2766 * to the first occurrence or UINT_MAX if no match was found.
2767 */
2768unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
2769 unsigned int to, struct ts_config *config,
2770 struct ts_state *state)
2771{
Phil Oesterf72b9482006-06-26 00:00:57 -07002772 unsigned int ret;
2773
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002774 config->get_next_block = skb_ts_get_next_block;
2775 config->finish = skb_ts_finish;
2776
2777 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state));
2778
Phil Oesterf72b9482006-06-26 00:00:57 -07002779 ret = textsearch_find(config, state);
2780 return (ret <= to - from ? ret : UINT_MAX);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002781}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002782EXPORT_SYMBOL(skb_find_text);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002783
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002784/**
Ben Hutchings2c530402012-07-10 10:55:09 +00002785 * skb_append_datato_frags - append the user data to a skb
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002786 * @sk: sock structure
Masanari Iidae793c0f2014-09-04 23:44:36 +09002787 * @skb: skb structure to be appended with user data.
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002788 * @getfrag: call back function to be used for getting the user data
2789 * @from: pointer to user message iov
2790 * @length: length of the iov message
2791 *
2792 * Description: This procedure append the user data in the fragment part
2793 * of the skb if any page alloc fails user this procedure returns -ENOMEM
2794 */
2795int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
Martin Waitzdab96302005-12-05 13:40:12 -08002796 int (*getfrag)(void *from, char *to, int offset,
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002797 int len, int odd, struct sk_buff *skb),
2798 void *from, int length)
2799{
Eric Dumazetb2111722012-12-28 06:06:37 +00002800 int frg_cnt = skb_shinfo(skb)->nr_frags;
2801 int copy;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002802 int offset = 0;
2803 int ret;
Eric Dumazetb2111722012-12-28 06:06:37 +00002804 struct page_frag *pfrag = &current->task_frag;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002805
2806 do {
2807 /* Return error if we don't have space for new frag */
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002808 if (frg_cnt >= MAX_SKB_FRAGS)
Eric Dumazetb2111722012-12-28 06:06:37 +00002809 return -EMSGSIZE;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002810
Eric Dumazetb2111722012-12-28 06:06:37 +00002811 if (!sk_page_frag_refill(sk, pfrag))
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002812 return -ENOMEM;
2813
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002814 /* copy the user data to page */
Eric Dumazetb2111722012-12-28 06:06:37 +00002815 copy = min_t(int, length, pfrag->size - pfrag->offset);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002816
Eric Dumazetb2111722012-12-28 06:06:37 +00002817 ret = getfrag(from, page_address(pfrag->page) + pfrag->offset,
2818 offset, copy, 0, skb);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002819 if (ret < 0)
2820 return -EFAULT;
2821
2822 /* copy was successful so update the size parameters */
Eric Dumazetb2111722012-12-28 06:06:37 +00002823 skb_fill_page_desc(skb, frg_cnt, pfrag->page, pfrag->offset,
2824 copy);
2825 frg_cnt++;
2826 pfrag->offset += copy;
2827 get_page(pfrag->page);
2828
2829 skb->truesize += copy;
2830 atomic_add(copy, &sk->sk_wmem_alloc);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002831 skb->len += copy;
2832 skb->data_len += copy;
2833 offset += copy;
2834 length -= copy;
2835
2836 } while (length > 0);
2837
2838 return 0;
2839}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002840EXPORT_SYMBOL(skb_append_datato_frags);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002841
Herbert Xucbb042f92006-03-20 22:43:56 -08002842/**
2843 * skb_pull_rcsum - pull skb and update receive checksum
2844 * @skb: buffer to update
Herbert Xucbb042f92006-03-20 22:43:56 -08002845 * @len: length of data pulled
2846 *
2847 * This function performs an skb_pull on the packet and updates
Urs Thuermannfee54fa2008-02-12 22:03:25 -08002848 * the CHECKSUM_COMPLETE checksum. It should be used on
Patrick McHardy84fa7932006-08-29 16:44:56 -07002849 * receive path processing instead of skb_pull unless you know
2850 * that the checksum difference is zero (e.g., a valid IP header)
2851 * or you are setting ip_summed to CHECKSUM_NONE.
Herbert Xucbb042f92006-03-20 22:43:56 -08002852 */
2853unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
2854{
2855 BUG_ON(len > skb->len);
2856 skb->len -= len;
2857 BUG_ON(skb->len < skb->data_len);
2858 skb_postpull_rcsum(skb, skb->data, len);
2859 return skb->data += len;
2860}
Arnaldo Carvalho de Melof94691a2006-03-20 22:47:55 -08002861EXPORT_SYMBOL_GPL(skb_pull_rcsum);
2862
Herbert Xuf4c50d92006-06-22 03:02:40 -07002863/**
2864 * skb_segment - Perform protocol segmentation on skb.
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002865 * @head_skb: buffer to segment
Herbert Xu576a30e2006-06-27 13:22:38 -07002866 * @features: features for the output path (see dev->features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07002867 *
2868 * This function performs segmentation on the given skb. It returns
Ben Hutchings4c821d72008-04-13 21:52:48 -07002869 * a pointer to the first in a list of new skbs for the segments.
2870 * In case of error it returns ERR_PTR(err).
Herbert Xuf4c50d92006-06-22 03:02:40 -07002871 */
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002872struct sk_buff *skb_segment(struct sk_buff *head_skb,
2873 netdev_features_t features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07002874{
2875 struct sk_buff *segs = NULL;
2876 struct sk_buff *tail = NULL;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02002877 struct sk_buff *list_skb = skb_shinfo(head_skb)->frag_list;
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002878 skb_frag_t *frag = skb_shinfo(head_skb)->frags;
2879 unsigned int mss = skb_shinfo(head_skb)->gso_size;
2880 unsigned int doffset = head_skb->data - skb_mac_header(head_skb);
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02002881 struct sk_buff *frag_skb = head_skb;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002882 unsigned int offset = doffset;
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002883 unsigned int tnl_hlen = skb_tnl_header_len(head_skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002884 unsigned int headroom;
2885 unsigned int len;
Pravin B Shelarec5f0612013-03-07 09:28:01 +00002886 __be16 proto;
2887 bool csum;
Michał Mirosław04ed3e72011-01-24 15:32:47 -08002888 int sg = !!(features & NETIF_F_SG);
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002889 int nfrags = skb_shinfo(head_skb)->nr_frags;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002890 int err = -ENOMEM;
2891 int i = 0;
2892 int pos;
Vlad Yasevich53d64712014-03-27 17:26:18 -04002893 int dummy;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002894
Wei-Chun Chao5882a072014-06-08 23:48:54 -07002895 __skb_push(head_skb, doffset);
Vlad Yasevich53d64712014-03-27 17:26:18 -04002896 proto = skb_network_protocol(head_skb, &dummy);
Pravin B Shelarec5f0612013-03-07 09:28:01 +00002897 if (unlikely(!proto))
2898 return ERR_PTR(-EINVAL);
2899
Tom Herbert7e2b10c2014-06-04 17:20:02 -07002900 csum = !head_skb->encap_hdr_csum &&
2901 !!can_checksum_protocol(features, proto);
2902
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002903 headroom = skb_headroom(head_skb);
2904 pos = skb_headlen(head_skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002905
2906 do {
2907 struct sk_buff *nskb;
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02002908 skb_frag_t *nskb_frag;
Herbert Xuc8884ed2006-10-29 15:59:41 -08002909 int hsize;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002910 int size;
2911
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002912 len = head_skb->len - offset;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002913 if (len > mss)
2914 len = mss;
2915
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002916 hsize = skb_headlen(head_skb) - offset;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002917 if (hsize < 0)
2918 hsize = 0;
Herbert Xuc8884ed2006-10-29 15:59:41 -08002919 if (hsize > len || !sg)
2920 hsize = len;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002921
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02002922 if (!hsize && i >= nfrags && skb_headlen(list_skb) &&
2923 (skb_headlen(list_skb) == len || sg)) {
2924 BUG_ON(skb_headlen(list_skb) > len);
Herbert Xu89319d382008-12-15 23:26:06 -08002925
Herbert Xu9d8506c2013-11-21 11:10:04 -08002926 i = 0;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02002927 nfrags = skb_shinfo(list_skb)->nr_frags;
2928 frag = skb_shinfo(list_skb)->frags;
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02002929 frag_skb = list_skb;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02002930 pos += skb_headlen(list_skb);
Herbert Xu9d8506c2013-11-21 11:10:04 -08002931
2932 while (pos < offset + len) {
2933 BUG_ON(i >= nfrags);
2934
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02002935 size = skb_frag_size(frag);
Herbert Xu9d8506c2013-11-21 11:10:04 -08002936 if (pos + size > offset + len)
2937 break;
2938
2939 i++;
2940 pos += size;
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02002941 frag++;
Herbert Xu9d8506c2013-11-21 11:10:04 -08002942 }
2943
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02002944 nskb = skb_clone(list_skb, GFP_ATOMIC);
2945 list_skb = list_skb->next;
Herbert Xu89319d382008-12-15 23:26:06 -08002946
2947 if (unlikely(!nskb))
2948 goto err;
2949
Herbert Xu9d8506c2013-11-21 11:10:04 -08002950 if (unlikely(pskb_trim(nskb, len))) {
2951 kfree_skb(nskb);
2952 goto err;
2953 }
2954
Alexander Duyckec47ea82012-05-04 14:26:56 +00002955 hsize = skb_end_offset(nskb);
Herbert Xu89319d382008-12-15 23:26:06 -08002956 if (skb_cow_head(nskb, doffset + headroom)) {
2957 kfree_skb(nskb);
2958 goto err;
2959 }
2960
Alexander Duyckec47ea82012-05-04 14:26:56 +00002961 nskb->truesize += skb_end_offset(nskb) - hsize;
Herbert Xu89319d382008-12-15 23:26:06 -08002962 skb_release_head_state(nskb);
2963 __skb_push(nskb, doffset);
2964 } else {
Mel Gormanc93bdd02012-07-31 16:44:19 -07002965 nskb = __alloc_skb(hsize + doffset + headroom,
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002966 GFP_ATOMIC, skb_alloc_rx_flag(head_skb),
Mel Gormanc93bdd02012-07-31 16:44:19 -07002967 NUMA_NO_NODE);
Herbert Xu89319d382008-12-15 23:26:06 -08002968
2969 if (unlikely(!nskb))
2970 goto err;
2971
2972 skb_reserve(nskb, headroom);
2973 __skb_put(nskb, doffset);
2974 }
Herbert Xuf4c50d92006-06-22 03:02:40 -07002975
2976 if (segs)
2977 tail->next = nskb;
2978 else
2979 segs = nskb;
2980 tail = nskb;
2981
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002982 __copy_skb_header(nskb, head_skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002983
Eric Dumazet030737b2013-10-19 11:42:54 -07002984 skb_headers_offset_update(nskb, skb_headroom(nskb) - headroom);
Vlad Yasevichfcdfe3a2014-07-31 10:33:06 -04002985 skb_reset_mac_len(nskb);
Pravin B Shelar68c33162013-02-14 14:02:41 +00002986
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002987 skb_copy_from_linear_data_offset(head_skb, -tnl_hlen,
Pravin B Shelar68c33162013-02-14 14:02:41 +00002988 nskb->data - tnl_hlen,
2989 doffset + tnl_hlen);
Herbert Xu89319d382008-12-15 23:26:06 -08002990
Herbert Xu9d8506c2013-11-21 11:10:04 -08002991 if (nskb->len == len + doffset)
Simon Horman1cdbcb72013-05-19 15:46:49 +00002992 goto perform_csum_check;
Herbert Xu89319d382008-12-15 23:26:06 -08002993
Herbert Xuf4c50d92006-06-22 03:02:40 -07002994 if (!sg) {
Herbert Xu6f85a122008-08-15 14:55:02 -07002995 nskb->ip_summed = CHECKSUM_NONE;
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002996 nskb->csum = skb_copy_and_csum_bits(head_skb, offset,
Herbert Xuf4c50d92006-06-22 03:02:40 -07002997 skb_put(nskb, len),
2998 len, 0);
Tom Herbert7e2b10c2014-06-04 17:20:02 -07002999 SKB_GSO_CB(nskb)->csum_start =
Tom Herbertde843722014-06-25 12:51:01 -07003000 skb_headroom(nskb) + doffset;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003001 continue;
3002 }
3003
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003004 nskb_frag = skb_shinfo(nskb)->frags;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003005
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003006 skb_copy_from_linear_data_offset(head_skb, offset,
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03003007 skb_put(nskb, hsize), hsize);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003008
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003009 skb_shinfo(nskb)->tx_flags = skb_shinfo(head_skb)->tx_flags &
3010 SKBTX_SHARED_FRAG;
Eric Dumazetcef401d2013-01-25 20:34:37 +00003011
Herbert Xu9d8506c2013-11-21 11:10:04 -08003012 while (pos < offset + len) {
3013 if (i >= nfrags) {
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003014 BUG_ON(skb_headlen(list_skb));
Herbert Xu9d8506c2013-11-21 11:10:04 -08003015
3016 i = 0;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003017 nfrags = skb_shinfo(list_skb)->nr_frags;
3018 frag = skb_shinfo(list_skb)->frags;
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02003019 frag_skb = list_skb;
Herbert Xu9d8506c2013-11-21 11:10:04 -08003020
3021 BUG_ON(!nfrags);
3022
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003023 list_skb = list_skb->next;
Herbert Xu9d8506c2013-11-21 11:10:04 -08003024 }
3025
3026 if (unlikely(skb_shinfo(nskb)->nr_frags >=
3027 MAX_SKB_FRAGS)) {
3028 net_warn_ratelimited(
3029 "skb_segment: too many frags: %u %u\n",
3030 pos, mss);
3031 goto err;
3032 }
3033
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02003034 if (unlikely(skb_orphan_frags(frag_skb, GFP_ATOMIC)))
3035 goto err;
3036
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02003037 *nskb_frag = *frag;
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003038 __skb_frag_ref(nskb_frag);
3039 size = skb_frag_size(nskb_frag);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003040
3041 if (pos < offset) {
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003042 nskb_frag->page_offset += offset - pos;
3043 skb_frag_size_sub(nskb_frag, offset - pos);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003044 }
3045
Herbert Xu89319d382008-12-15 23:26:06 -08003046 skb_shinfo(nskb)->nr_frags++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003047
3048 if (pos + size <= offset + len) {
3049 i++;
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02003050 frag++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003051 pos += size;
3052 } else {
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003053 skb_frag_size_sub(nskb_frag, pos + size - (offset + len));
Herbert Xu89319d382008-12-15 23:26:06 -08003054 goto skip_fraglist;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003055 }
3056
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003057 nskb_frag++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003058 }
3059
Herbert Xu89319d382008-12-15 23:26:06 -08003060skip_fraglist:
Herbert Xuf4c50d92006-06-22 03:02:40 -07003061 nskb->data_len = len - hsize;
3062 nskb->len += nskb->data_len;
3063 nskb->truesize += nskb->data_len;
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003064
Simon Horman1cdbcb72013-05-19 15:46:49 +00003065perform_csum_check:
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003066 if (!csum) {
3067 nskb->csum = skb_checksum(nskb, doffset,
3068 nskb->len - doffset, 0);
3069 nskb->ip_summed = CHECKSUM_NONE;
Tom Herbert7e2b10c2014-06-04 17:20:02 -07003070 SKB_GSO_CB(nskb)->csum_start =
3071 skb_headroom(nskb) + doffset;
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003072 }
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003073 } while ((offset += len) < head_skb->len);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003074
3075 return segs;
3076
3077err:
Eric Dumazet289dccb2013-12-20 14:29:08 -08003078 kfree_skb_list(segs);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003079 return ERR_PTR(err);
3080}
Herbert Xuf4c50d92006-06-22 03:02:40 -07003081EXPORT_SYMBOL_GPL(skb_segment);
3082
Herbert Xu71d93b32008-12-15 23:42:33 -08003083int skb_gro_receive(struct sk_buff **head, struct sk_buff *skb)
3084{
Eric Dumazet8a291112013-10-08 09:02:23 -07003085 struct skb_shared_info *pinfo, *skbinfo = skb_shinfo(skb);
Herbert Xu67147ba2009-05-26 18:50:22 +00003086 unsigned int offset = skb_gro_offset(skb);
3087 unsigned int headlen = skb_headlen(skb);
Eric Dumazet8a291112013-10-08 09:02:23 -07003088 struct sk_buff *nskb, *lp, *p = *head;
3089 unsigned int len = skb_gro_len(skb);
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003090 unsigned int delta_truesize;
Eric Dumazet8a291112013-10-08 09:02:23 -07003091 unsigned int headroom;
Herbert Xu71d93b32008-12-15 23:42:33 -08003092
Eric Dumazet8a291112013-10-08 09:02:23 -07003093 if (unlikely(p->len + len >= 65536))
Herbert Xu71d93b32008-12-15 23:42:33 -08003094 return -E2BIG;
3095
Eric Dumazet29e98242014-05-16 11:34:37 -07003096 lp = NAPI_GRO_CB(p)->last;
Eric Dumazet8a291112013-10-08 09:02:23 -07003097 pinfo = skb_shinfo(lp);
3098
3099 if (headlen <= offset) {
Herbert Xu42da6992009-05-26 18:50:19 +00003100 skb_frag_t *frag;
Herbert Xu66e92fc2009-05-26 18:50:32 +00003101 skb_frag_t *frag2;
Herbert Xu9aaa1562009-05-26 18:50:33 +00003102 int i = skbinfo->nr_frags;
3103 int nr_frags = pinfo->nr_frags + i;
Herbert Xu42da6992009-05-26 18:50:19 +00003104
Herbert Xu66e92fc2009-05-26 18:50:32 +00003105 if (nr_frags > MAX_SKB_FRAGS)
Eric Dumazet8a291112013-10-08 09:02:23 -07003106 goto merge;
Herbert Xu81705ad2009-01-29 14:19:51 +00003107
Eric Dumazet8a291112013-10-08 09:02:23 -07003108 offset -= headlen;
Herbert Xu9aaa1562009-05-26 18:50:33 +00003109 pinfo->nr_frags = nr_frags;
3110 skbinfo->nr_frags = 0;
Herbert Xuf5572062009-01-14 20:40:03 -08003111
Herbert Xu9aaa1562009-05-26 18:50:33 +00003112 frag = pinfo->frags + nr_frags;
3113 frag2 = skbinfo->frags + i;
Herbert Xu66e92fc2009-05-26 18:50:32 +00003114 do {
3115 *--frag = *--frag2;
3116 } while (--i);
3117
3118 frag->page_offset += offset;
Eric Dumazet9e903e02011-10-18 21:00:24 +00003119 skb_frag_size_sub(frag, offset);
Herbert Xu66e92fc2009-05-26 18:50:32 +00003120
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003121 /* all fragments truesize : remove (head size + sk_buff) */
Alexander Duyckec47ea82012-05-04 14:26:56 +00003122 delta_truesize = skb->truesize -
3123 SKB_TRUESIZE(skb_end_offset(skb));
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003124
Herbert Xuf5572062009-01-14 20:40:03 -08003125 skb->truesize -= skb->data_len;
3126 skb->len -= skb->data_len;
3127 skb->data_len = 0;
3128
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003129 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE;
Herbert Xu5d38a072009-01-04 16:13:40 -08003130 goto done;
Eric Dumazetd7e88832012-04-30 08:10:34 +00003131 } else if (skb->head_frag) {
3132 int nr_frags = pinfo->nr_frags;
3133 skb_frag_t *frag = pinfo->frags + nr_frags;
3134 struct page *page = virt_to_head_page(skb->head);
3135 unsigned int first_size = headlen - offset;
3136 unsigned int first_offset;
3137
3138 if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS)
Eric Dumazet8a291112013-10-08 09:02:23 -07003139 goto merge;
Eric Dumazetd7e88832012-04-30 08:10:34 +00003140
3141 first_offset = skb->data -
3142 (unsigned char *)page_address(page) +
3143 offset;
3144
3145 pinfo->nr_frags = nr_frags + 1 + skbinfo->nr_frags;
3146
3147 frag->page.p = page;
3148 frag->page_offset = first_offset;
3149 skb_frag_size_set(frag, first_size);
3150
3151 memcpy(frag + 1, skbinfo->frags, sizeof(*frag) * skbinfo->nr_frags);
3152 /* We dont need to clear skbinfo->nr_frags here */
3153
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003154 delta_truesize = skb->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
Eric Dumazetd7e88832012-04-30 08:10:34 +00003155 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD;
3156 goto done;
Eric Dumazet8a291112013-10-08 09:02:23 -07003157 }
3158 if (pinfo->frag_list)
3159 goto merge;
3160 if (skb_gro_len(p) != pinfo->gso_size)
Herbert Xu69c0cab2009-11-17 05:18:18 -08003161 return -E2BIG;
Herbert Xu71d93b32008-12-15 23:42:33 -08003162
3163 headroom = skb_headroom(p);
Eric Dumazet3d3be432010-09-01 00:50:51 +00003164 nskb = alloc_skb(headroom + skb_gro_offset(p), GFP_ATOMIC);
Herbert Xu71d93b32008-12-15 23:42:33 -08003165 if (unlikely(!nskb))
3166 return -ENOMEM;
3167
3168 __copy_skb_header(nskb, p);
3169 nskb->mac_len = p->mac_len;
3170
3171 skb_reserve(nskb, headroom);
Herbert Xu86911732009-01-29 14:19:50 +00003172 __skb_put(nskb, skb_gro_offset(p));
Herbert Xu71d93b32008-12-15 23:42:33 -08003173
Herbert Xu86911732009-01-29 14:19:50 +00003174 skb_set_mac_header(nskb, skb_mac_header(p) - p->data);
Herbert Xu71d93b32008-12-15 23:42:33 -08003175 skb_set_network_header(nskb, skb_network_offset(p));
3176 skb_set_transport_header(nskb, skb_transport_offset(p));
3177
Herbert Xu86911732009-01-29 14:19:50 +00003178 __skb_pull(p, skb_gro_offset(p));
3179 memcpy(skb_mac_header(nskb), skb_mac_header(p),
3180 p->data - skb_mac_header(p));
Herbert Xu71d93b32008-12-15 23:42:33 -08003181
Herbert Xu71d93b32008-12-15 23:42:33 -08003182 skb_shinfo(nskb)->frag_list = p;
Herbert Xu9aaa1562009-05-26 18:50:33 +00003183 skb_shinfo(nskb)->gso_size = pinfo->gso_size;
Herbert Xu622e0ca2010-05-20 23:07:56 -07003184 pinfo->gso_size = 0;
Eric Dumazetf4a775d2014-09-22 16:29:32 -07003185 __skb_header_release(p);
Eric Dumazetc3c7c252012-12-06 13:54:59 +00003186 NAPI_GRO_CB(nskb)->last = p;
Herbert Xu71d93b32008-12-15 23:42:33 -08003187
3188 nskb->data_len += p->len;
Eric Dumazetde8261c2012-02-13 04:09:20 +00003189 nskb->truesize += p->truesize;
Herbert Xu71d93b32008-12-15 23:42:33 -08003190 nskb->len += p->len;
3191
3192 *head = nskb;
3193 nskb->next = p->next;
3194 p->next = NULL;
3195
3196 p = nskb;
3197
3198merge:
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003199 delta_truesize = skb->truesize;
Herbert Xu67147ba2009-05-26 18:50:22 +00003200 if (offset > headlen) {
Michal Schmidtd1dc7ab2011-01-24 12:08:48 +00003201 unsigned int eat = offset - headlen;
3202
3203 skbinfo->frags[0].page_offset += eat;
Eric Dumazet9e903e02011-10-18 21:00:24 +00003204 skb_frag_size_sub(&skbinfo->frags[0], eat);
Michal Schmidtd1dc7ab2011-01-24 12:08:48 +00003205 skb->data_len -= eat;
3206 skb->len -= eat;
Herbert Xu67147ba2009-05-26 18:50:22 +00003207 offset = headlen;
Herbert Xu56035022009-02-05 21:26:52 -08003208 }
3209
Herbert Xu67147ba2009-05-26 18:50:22 +00003210 __skb_pull(skb, offset);
Herbert Xu56035022009-02-05 21:26:52 -08003211
Eric Dumazet29e98242014-05-16 11:34:37 -07003212 if (NAPI_GRO_CB(p)->last == p)
Eric Dumazet8a291112013-10-08 09:02:23 -07003213 skb_shinfo(p)->frag_list = skb;
3214 else
3215 NAPI_GRO_CB(p)->last->next = skb;
Eric Dumazetc3c7c252012-12-06 13:54:59 +00003216 NAPI_GRO_CB(p)->last = skb;
Eric Dumazetf4a775d2014-09-22 16:29:32 -07003217 __skb_header_release(skb);
Eric Dumazet8a291112013-10-08 09:02:23 -07003218 lp = p;
Herbert Xu71d93b32008-12-15 23:42:33 -08003219
Herbert Xu5d38a072009-01-04 16:13:40 -08003220done:
3221 NAPI_GRO_CB(p)->count++;
Herbert Xu37fe4732009-01-17 19:48:13 +00003222 p->data_len += len;
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003223 p->truesize += delta_truesize;
Herbert Xu37fe4732009-01-17 19:48:13 +00003224 p->len += len;
Eric Dumazet8a291112013-10-08 09:02:23 -07003225 if (lp != p) {
3226 lp->data_len += len;
3227 lp->truesize += delta_truesize;
3228 lp->len += len;
3229 }
Herbert Xu71d93b32008-12-15 23:42:33 -08003230 NAPI_GRO_CB(skb)->same_flow = 1;
3231 return 0;
3232}
3233EXPORT_SYMBOL_GPL(skb_gro_receive);
3234
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235void __init skb_init(void)
3236{
3237 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
3238 sizeof(struct sk_buff),
3239 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07003240 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09003241 NULL);
David S. Millerd179cd12005-08-17 14:57:30 -07003242 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
3243 (2*sizeof(struct sk_buff)) +
3244 sizeof(atomic_t),
3245 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07003246 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09003247 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248}
3249
David Howells716ea3a2007-04-02 20:19:53 -07003250/**
3251 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
3252 * @skb: Socket buffer containing the buffers to be mapped
3253 * @sg: The scatter-gather list to map into
3254 * @offset: The offset into the buffer's contents to start mapping
3255 * @len: Length of buffer space to be mapped
3256 *
3257 * Fill the specified scatter-gather list with mappings/pointers into a
3258 * region of the buffer space attached to a socket buffer.
3259 */
David S. Miller51c739d2007-10-30 21:29:29 -07003260static int
3261__skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
David Howells716ea3a2007-04-02 20:19:53 -07003262{
David S. Miller1a028e52007-04-27 15:21:23 -07003263 int start = skb_headlen(skb);
3264 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07003265 struct sk_buff *frag_iter;
David Howells716ea3a2007-04-02 20:19:53 -07003266 int elt = 0;
3267
3268 if (copy > 0) {
3269 if (copy > len)
3270 copy = len;
Jens Axboe642f1492007-10-24 11:20:47 +02003271 sg_set_buf(sg, skb->data + offset, copy);
David Howells716ea3a2007-04-02 20:19:53 -07003272 elt++;
3273 if ((len -= copy) == 0)
3274 return elt;
3275 offset += copy;
3276 }
3277
3278 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07003279 int end;
David Howells716ea3a2007-04-02 20:19:53 -07003280
Ilpo Järvinen547b7922008-07-25 21:43:18 -07003281 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07003282
Eric Dumazet9e903e02011-10-18 21:00:24 +00003283 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
David Howells716ea3a2007-04-02 20:19:53 -07003284 if ((copy = end - offset) > 0) {
3285 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
3286
3287 if (copy > len)
3288 copy = len;
Ian Campbellea2ab692011-08-22 23:44:58 +00003289 sg_set_page(&sg[elt], skb_frag_page(frag), copy,
Jens Axboe642f1492007-10-24 11:20:47 +02003290 frag->page_offset+offset-start);
David Howells716ea3a2007-04-02 20:19:53 -07003291 elt++;
3292 if (!(len -= copy))
3293 return elt;
3294 offset += copy;
3295 }
David S. Miller1a028e52007-04-27 15:21:23 -07003296 start = end;
David Howells716ea3a2007-04-02 20:19:53 -07003297 }
3298
David S. Millerfbb398a2009-06-09 00:18:59 -07003299 skb_walk_frags(skb, frag_iter) {
3300 int end;
David Howells716ea3a2007-04-02 20:19:53 -07003301
David S. Millerfbb398a2009-06-09 00:18:59 -07003302 WARN_ON(start > offset + len);
David Howells716ea3a2007-04-02 20:19:53 -07003303
David S. Millerfbb398a2009-06-09 00:18:59 -07003304 end = start + frag_iter->len;
3305 if ((copy = end - offset) > 0) {
3306 if (copy > len)
3307 copy = len;
3308 elt += __skb_to_sgvec(frag_iter, sg+elt, offset - start,
3309 copy);
3310 if ((len -= copy) == 0)
3311 return elt;
3312 offset += copy;
David Howells716ea3a2007-04-02 20:19:53 -07003313 }
David S. Millerfbb398a2009-06-09 00:18:59 -07003314 start = end;
David Howells716ea3a2007-04-02 20:19:53 -07003315 }
3316 BUG_ON(len);
3317 return elt;
3318}
3319
Fan Du25a91d82014-01-18 09:54:23 +08003320/* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given
3321 * sglist without mark the sg which contain last skb data as the end.
3322 * So the caller can mannipulate sg list as will when padding new data after
3323 * the first call without calling sg_unmark_end to expend sg list.
3324 *
3325 * Scenario to use skb_to_sgvec_nomark:
3326 * 1. sg_init_table
3327 * 2. skb_to_sgvec_nomark(payload1)
3328 * 3. skb_to_sgvec_nomark(payload2)
3329 *
3330 * This is equivalent to:
3331 * 1. sg_init_table
3332 * 2. skb_to_sgvec(payload1)
3333 * 3. sg_unmark_end
3334 * 4. skb_to_sgvec(payload2)
3335 *
3336 * When mapping mutilple payload conditionally, skb_to_sgvec_nomark
3337 * is more preferable.
3338 */
3339int skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg,
3340 int offset, int len)
3341{
3342 return __skb_to_sgvec(skb, sg, offset, len);
3343}
3344EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark);
3345
David S. Miller51c739d2007-10-30 21:29:29 -07003346int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
3347{
3348 int nsg = __skb_to_sgvec(skb, sg, offset, len);
3349
Jens Axboec46f2332007-10-31 12:06:37 +01003350 sg_mark_end(&sg[nsg - 1]);
David S. Miller51c739d2007-10-30 21:29:29 -07003351
3352 return nsg;
3353}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003354EXPORT_SYMBOL_GPL(skb_to_sgvec);
David S. Miller51c739d2007-10-30 21:29:29 -07003355
David Howells716ea3a2007-04-02 20:19:53 -07003356/**
3357 * skb_cow_data - Check that a socket buffer's data buffers are writable
3358 * @skb: The socket buffer to check.
3359 * @tailbits: Amount of trailing space to be added
3360 * @trailer: Returned pointer to the skb where the @tailbits space begins
3361 *
3362 * Make sure that the data buffers attached to a socket buffer are
3363 * writable. If they are not, private copies are made of the data buffers
3364 * and the socket buffer is set to use these instead.
3365 *
3366 * If @tailbits is given, make sure that there is space to write @tailbits
3367 * bytes of data beyond current end of socket buffer. @trailer will be
3368 * set to point to the skb in which this space begins.
3369 *
3370 * The number of scatterlist elements required to completely map the
3371 * COW'd and extended socket buffer will be returned.
3372 */
3373int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
3374{
3375 int copyflag;
3376 int elt;
3377 struct sk_buff *skb1, **skb_p;
3378
3379 /* If skb is cloned or its head is paged, reallocate
3380 * head pulling out all the pages (pages are considered not writable
3381 * at the moment even if they are anonymous).
3382 */
3383 if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
3384 __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
3385 return -ENOMEM;
3386
3387 /* Easy case. Most of packets will go this way. */
David S. Miller21dc3302010-08-23 00:13:46 -07003388 if (!skb_has_frag_list(skb)) {
David Howells716ea3a2007-04-02 20:19:53 -07003389 /* A little of trouble, not enough of space for trailer.
3390 * This should not happen, when stack is tuned to generate
3391 * good frames. OK, on miss we reallocate and reserve even more
3392 * space, 128 bytes is fair. */
3393
3394 if (skb_tailroom(skb) < tailbits &&
3395 pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
3396 return -ENOMEM;
3397
3398 /* Voila! */
3399 *trailer = skb;
3400 return 1;
3401 }
3402
3403 /* Misery. We are in troubles, going to mincer fragments... */
3404
3405 elt = 1;
3406 skb_p = &skb_shinfo(skb)->frag_list;
3407 copyflag = 0;
3408
3409 while ((skb1 = *skb_p) != NULL) {
3410 int ntail = 0;
3411
3412 /* The fragment is partially pulled by someone,
3413 * this can happen on input. Copy it and everything
3414 * after it. */
3415
3416 if (skb_shared(skb1))
3417 copyflag = 1;
3418
3419 /* If the skb is the last, worry about trailer. */
3420
3421 if (skb1->next == NULL && tailbits) {
3422 if (skb_shinfo(skb1)->nr_frags ||
David S. Miller21dc3302010-08-23 00:13:46 -07003423 skb_has_frag_list(skb1) ||
David Howells716ea3a2007-04-02 20:19:53 -07003424 skb_tailroom(skb1) < tailbits)
3425 ntail = tailbits + 128;
3426 }
3427
3428 if (copyflag ||
3429 skb_cloned(skb1) ||
3430 ntail ||
3431 skb_shinfo(skb1)->nr_frags ||
David S. Miller21dc3302010-08-23 00:13:46 -07003432 skb_has_frag_list(skb1)) {
David Howells716ea3a2007-04-02 20:19:53 -07003433 struct sk_buff *skb2;
3434
3435 /* Fuck, we are miserable poor guys... */
3436 if (ntail == 0)
3437 skb2 = skb_copy(skb1, GFP_ATOMIC);
3438 else
3439 skb2 = skb_copy_expand(skb1,
3440 skb_headroom(skb1),
3441 ntail,
3442 GFP_ATOMIC);
3443 if (unlikely(skb2 == NULL))
3444 return -ENOMEM;
3445
3446 if (skb1->sk)
3447 skb_set_owner_w(skb2, skb1->sk);
3448
3449 /* Looking around. Are we still alive?
3450 * OK, link new skb, drop old one */
3451
3452 skb2->next = skb1->next;
3453 *skb_p = skb2;
3454 kfree_skb(skb1);
3455 skb1 = skb2;
3456 }
3457 elt++;
3458 *trailer = skb1;
3459 skb_p = &skb1->next;
3460 }
3461
3462 return elt;
3463}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003464EXPORT_SYMBOL_GPL(skb_cow_data);
David Howells716ea3a2007-04-02 20:19:53 -07003465
Eric Dumazetb1faf562010-05-31 23:44:05 -07003466static void sock_rmem_free(struct sk_buff *skb)
3467{
3468 struct sock *sk = skb->sk;
3469
3470 atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
3471}
3472
3473/*
3474 * Note: We dont mem charge error packets (no sk_forward_alloc changes)
3475 */
3476int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb)
3477{
3478 if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
Eric Dumazet95c96172012-04-15 05:58:06 +00003479 (unsigned int)sk->sk_rcvbuf)
Eric Dumazetb1faf562010-05-31 23:44:05 -07003480 return -ENOMEM;
3481
3482 skb_orphan(skb);
3483 skb->sk = sk;
3484 skb->destructor = sock_rmem_free;
3485 atomic_add(skb->truesize, &sk->sk_rmem_alloc);
3486
Eric Dumazetabb57ea2011-05-18 02:21:31 -04003487 /* before exiting rcu section, make sure dst is refcounted */
3488 skb_dst_force(skb);
3489
Eric Dumazetb1faf562010-05-31 23:44:05 -07003490 skb_queue_tail(&sk->sk_error_queue, skb);
3491 if (!sock_flag(sk, SOCK_DEAD))
David S. Miller676d2362014-04-11 16:15:36 -04003492 sk->sk_data_ready(sk);
Eric Dumazetb1faf562010-05-31 23:44:05 -07003493 return 0;
3494}
3495EXPORT_SYMBOL(sock_queue_err_skb);
3496
Willem de Bruijn364a9e92014-08-31 21:30:27 -04003497struct sk_buff *sock_dequeue_err_skb(struct sock *sk)
3498{
3499 struct sk_buff_head *q = &sk->sk_error_queue;
3500 struct sk_buff *skb, *skb_next;
3501 int err = 0;
3502
3503 spin_lock_bh(&q->lock);
3504 skb = __skb_dequeue(q);
3505 if (skb && (skb_next = skb_peek(q)))
3506 err = SKB_EXT_ERR(skb_next)->ee.ee_errno;
3507 spin_unlock_bh(&q->lock);
3508
3509 sk->sk_err = err;
3510 if (err)
3511 sk->sk_error_report(sk);
3512
3513 return skb;
3514}
3515EXPORT_SYMBOL(sock_dequeue_err_skb);
3516
Alexander Duyckcab41c42014-09-10 18:05:26 -04003517/**
3518 * skb_clone_sk - create clone of skb, and take reference to socket
3519 * @skb: the skb to clone
3520 *
3521 * This function creates a clone of a buffer that holds a reference on
3522 * sk_refcnt. Buffers created via this function are meant to be
3523 * returned using sock_queue_err_skb, or free via kfree_skb.
3524 *
3525 * When passing buffers allocated with this function to sock_queue_err_skb
3526 * it is necessary to wrap the call with sock_hold/sock_put in order to
3527 * prevent the socket from being released prior to being enqueued on
3528 * the sk_error_queue.
3529 */
Alexander Duyck62bccb82014-09-04 13:31:35 -04003530struct sk_buff *skb_clone_sk(struct sk_buff *skb)
3531{
3532 struct sock *sk = skb->sk;
3533 struct sk_buff *clone;
3534
3535 if (!sk || !atomic_inc_not_zero(&sk->sk_refcnt))
3536 return NULL;
3537
3538 clone = skb_clone(skb, GFP_ATOMIC);
3539 if (!clone) {
3540 sock_put(sk);
3541 return NULL;
3542 }
3543
3544 clone->sk = sk;
3545 clone->destructor = sock_efree;
3546
3547 return clone;
3548}
3549EXPORT_SYMBOL(skb_clone_sk);
3550
Alexander Duyck37846ef2014-09-04 13:31:10 -04003551static void __skb_complete_tx_timestamp(struct sk_buff *skb,
3552 struct sock *sk,
3553 int tstype)
Patrick Ohlyac45f602009-02-12 05:03:37 +00003554{
Patrick Ohlyac45f602009-02-12 05:03:37 +00003555 struct sock_exterr_skb *serr;
Patrick Ohlyac45f602009-02-12 05:03:37 +00003556 int err;
3557
Patrick Ohlyac45f602009-02-12 05:03:37 +00003558 serr = SKB_EXT_ERR(skb);
3559 memset(serr, 0, sizeof(*serr));
3560 serr->ee.ee_errno = ENOMSG;
3561 serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
Willem de Bruijne7fd2882014-08-04 22:11:48 -04003562 serr->ee.ee_info = tstype;
Willem de Bruijn4ed2d762014-08-04 22:11:49 -04003563 if (sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) {
Willem de Bruijn09c2d252014-08-04 22:11:47 -04003564 serr->ee.ee_data = skb_shinfo(skb)->tskey;
Willem de Bruijn4ed2d762014-08-04 22:11:49 -04003565 if (sk->sk_protocol == IPPROTO_TCP)
3566 serr->ee.ee_data -= sk->sk_tskey;
3567 }
Eric Dumazet29030372010-05-29 00:20:48 -07003568
Patrick Ohlyac45f602009-02-12 05:03:37 +00003569 err = sock_queue_err_skb(sk, skb);
Eric Dumazet29030372010-05-29 00:20:48 -07003570
Patrick Ohlyac45f602009-02-12 05:03:37 +00003571 if (err)
3572 kfree_skb(skb);
3573}
Alexander Duyck37846ef2014-09-04 13:31:10 -04003574
3575void skb_complete_tx_timestamp(struct sk_buff *skb,
3576 struct skb_shared_hwtstamps *hwtstamps)
3577{
3578 struct sock *sk = skb->sk;
3579
Alexander Duyck62bccb82014-09-04 13:31:35 -04003580 /* take a reference to prevent skb_orphan() from freeing the socket */
3581 sock_hold(sk);
Alexander Duyck37846ef2014-09-04 13:31:10 -04003582
Alexander Duyck62bccb82014-09-04 13:31:35 -04003583 *skb_hwtstamps(skb) = *hwtstamps;
3584 __skb_complete_tx_timestamp(skb, sk, SCM_TSTAMP_SND);
Alexander Duyck37846ef2014-09-04 13:31:10 -04003585
3586 sock_put(sk);
3587}
3588EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp);
3589
3590void __skb_tstamp_tx(struct sk_buff *orig_skb,
3591 struct skb_shared_hwtstamps *hwtstamps,
3592 struct sock *sk, int tstype)
3593{
3594 struct sk_buff *skb;
3595
3596 if (!sk)
3597 return;
3598
3599 if (hwtstamps)
3600 *skb_hwtstamps(orig_skb) = *hwtstamps;
3601 else
3602 orig_skb->tstamp = ktime_get_real();
3603
3604 skb = skb_clone(orig_skb, GFP_ATOMIC);
3605 if (!skb)
3606 return;
3607
3608 __skb_complete_tx_timestamp(skb, sk, tstype);
3609}
Willem de Bruijne7fd2882014-08-04 22:11:48 -04003610EXPORT_SYMBOL_GPL(__skb_tstamp_tx);
3611
3612void skb_tstamp_tx(struct sk_buff *orig_skb,
3613 struct skb_shared_hwtstamps *hwtstamps)
3614{
3615 return __skb_tstamp_tx(orig_skb, hwtstamps, orig_skb->sk,
3616 SCM_TSTAMP_SND);
3617}
Patrick Ohlyac45f602009-02-12 05:03:37 +00003618EXPORT_SYMBOL_GPL(skb_tstamp_tx);
3619
Johannes Berg6e3e9392011-11-09 10:15:42 +01003620void skb_complete_wifi_ack(struct sk_buff *skb, bool acked)
3621{
3622 struct sock *sk = skb->sk;
3623 struct sock_exterr_skb *serr;
3624 int err;
3625
3626 skb->wifi_acked_valid = 1;
3627 skb->wifi_acked = acked;
3628
3629 serr = SKB_EXT_ERR(skb);
3630 memset(serr, 0, sizeof(*serr));
3631 serr->ee.ee_errno = ENOMSG;
3632 serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS;
3633
Alexander Duyckbf7fa552014-09-10 18:05:42 -04003634 /* take a reference to prevent skb_orphan() from freeing the socket */
3635 sock_hold(sk);
3636
Johannes Berg6e3e9392011-11-09 10:15:42 +01003637 err = sock_queue_err_skb(sk, skb);
3638 if (err)
3639 kfree_skb(skb);
Alexander Duyckbf7fa552014-09-10 18:05:42 -04003640
3641 sock_put(sk);
Johannes Berg6e3e9392011-11-09 10:15:42 +01003642}
3643EXPORT_SYMBOL_GPL(skb_complete_wifi_ack);
3644
Patrick Ohlyac45f602009-02-12 05:03:37 +00003645
Rusty Russellf35d9d82008-02-04 23:49:54 -05003646/**
3647 * skb_partial_csum_set - set up and verify partial csum values for packet
3648 * @skb: the skb to set
3649 * @start: the number of bytes after skb->data to start checksumming.
3650 * @off: the offset from start to place the checksum.
3651 *
3652 * For untrusted partially-checksummed packets, we need to make sure the values
3653 * for skb->csum_start and skb->csum_offset are valid so we don't oops.
3654 *
3655 * This function checks and sets those values and skb->ip_summed: if this
3656 * returns false you should drop the packet.
3657 */
3658bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
3659{
Herbert Xu5ff8dda2009-06-04 01:22:01 +00003660 if (unlikely(start > skb_headlen(skb)) ||
3661 unlikely((int)start + off > skb_headlen(skb) - 2)) {
Joe Perchese87cc472012-05-13 21:56:26 +00003662 net_warn_ratelimited("bad partial csum: csum=%u/%u len=%u\n",
3663 start, off, skb_headlen(skb));
Rusty Russellf35d9d82008-02-04 23:49:54 -05003664 return false;
3665 }
3666 skb->ip_summed = CHECKSUM_PARTIAL;
3667 skb->csum_start = skb_headroom(skb) + start;
3668 skb->csum_offset = off;
Jason Wange5d5dec2013-03-26 23:11:20 +00003669 skb_set_transport_header(skb, start);
Rusty Russellf35d9d82008-02-04 23:49:54 -05003670 return true;
3671}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003672EXPORT_SYMBOL_GPL(skb_partial_csum_set);
Rusty Russellf35d9d82008-02-04 23:49:54 -05003673
Paul Durranted1f50c2014-01-09 10:02:46 +00003674static int skb_maybe_pull_tail(struct sk_buff *skb, unsigned int len,
3675 unsigned int max)
3676{
3677 if (skb_headlen(skb) >= len)
3678 return 0;
3679
3680 /* If we need to pullup then pullup to the max, so we
3681 * won't need to do it again.
3682 */
3683 if (max > skb->len)
3684 max = skb->len;
3685
3686 if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL)
3687 return -ENOMEM;
3688
3689 if (skb_headlen(skb) < len)
3690 return -EPROTO;
3691
3692 return 0;
3693}
3694
Jan Beulichf9708b42014-03-11 13:56:05 +00003695#define MAX_TCP_HDR_LEN (15 * 4)
3696
3697static __sum16 *skb_checksum_setup_ip(struct sk_buff *skb,
3698 typeof(IPPROTO_IP) proto,
3699 unsigned int off)
3700{
3701 switch (proto) {
3702 int err;
3703
3704 case IPPROTO_TCP:
3705 err = skb_maybe_pull_tail(skb, off + sizeof(struct tcphdr),
3706 off + MAX_TCP_HDR_LEN);
3707 if (!err && !skb_partial_csum_set(skb, off,
3708 offsetof(struct tcphdr,
3709 check)))
3710 err = -EPROTO;
3711 return err ? ERR_PTR(err) : &tcp_hdr(skb)->check;
3712
3713 case IPPROTO_UDP:
3714 err = skb_maybe_pull_tail(skb, off + sizeof(struct udphdr),
3715 off + sizeof(struct udphdr));
3716 if (!err && !skb_partial_csum_set(skb, off,
3717 offsetof(struct udphdr,
3718 check)))
3719 err = -EPROTO;
3720 return err ? ERR_PTR(err) : &udp_hdr(skb)->check;
3721 }
3722
3723 return ERR_PTR(-EPROTO);
3724}
3725
Paul Durranted1f50c2014-01-09 10:02:46 +00003726/* This value should be large enough to cover a tagged ethernet header plus
3727 * maximally sized IP and TCP or UDP headers.
3728 */
3729#define MAX_IP_HDR_LEN 128
3730
Jan Beulichf9708b42014-03-11 13:56:05 +00003731static int skb_checksum_setup_ipv4(struct sk_buff *skb, bool recalculate)
Paul Durranted1f50c2014-01-09 10:02:46 +00003732{
3733 unsigned int off;
3734 bool fragment;
Jan Beulichf9708b42014-03-11 13:56:05 +00003735 __sum16 *csum;
Paul Durranted1f50c2014-01-09 10:02:46 +00003736 int err;
3737
3738 fragment = false;
3739
3740 err = skb_maybe_pull_tail(skb,
3741 sizeof(struct iphdr),
3742 MAX_IP_HDR_LEN);
3743 if (err < 0)
3744 goto out;
3745
3746 if (ip_hdr(skb)->frag_off & htons(IP_OFFSET | IP_MF))
3747 fragment = true;
3748
3749 off = ip_hdrlen(skb);
3750
3751 err = -EPROTO;
3752
3753 if (fragment)
3754 goto out;
3755
Jan Beulichf9708b42014-03-11 13:56:05 +00003756 csum = skb_checksum_setup_ip(skb, ip_hdr(skb)->protocol, off);
3757 if (IS_ERR(csum))
3758 return PTR_ERR(csum);
Paul Durranted1f50c2014-01-09 10:02:46 +00003759
Jan Beulichf9708b42014-03-11 13:56:05 +00003760 if (recalculate)
3761 *csum = ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
3762 ip_hdr(skb)->daddr,
3763 skb->len - off,
3764 ip_hdr(skb)->protocol, 0);
Paul Durranted1f50c2014-01-09 10:02:46 +00003765 err = 0;
3766
3767out:
3768 return err;
3769}
3770
3771/* This value should be large enough to cover a tagged ethernet header plus
3772 * an IPv6 header, all options, and a maximal TCP or UDP header.
3773 */
3774#define MAX_IPV6_HDR_LEN 256
3775
3776#define OPT_HDR(type, skb, off) \
3777 (type *)(skb_network_header(skb) + (off))
3778
3779static int skb_checksum_setup_ipv6(struct sk_buff *skb, bool recalculate)
3780{
3781 int err;
3782 u8 nexthdr;
3783 unsigned int off;
3784 unsigned int len;
3785 bool fragment;
3786 bool done;
Jan Beulichf9708b42014-03-11 13:56:05 +00003787 __sum16 *csum;
Paul Durranted1f50c2014-01-09 10:02:46 +00003788
3789 fragment = false;
3790 done = false;
3791
3792 off = sizeof(struct ipv6hdr);
3793
3794 err = skb_maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN);
3795 if (err < 0)
3796 goto out;
3797
3798 nexthdr = ipv6_hdr(skb)->nexthdr;
3799
3800 len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len);
3801 while (off <= len && !done) {
3802 switch (nexthdr) {
3803 case IPPROTO_DSTOPTS:
3804 case IPPROTO_HOPOPTS:
3805 case IPPROTO_ROUTING: {
3806 struct ipv6_opt_hdr *hp;
3807
3808 err = skb_maybe_pull_tail(skb,
3809 off +
3810 sizeof(struct ipv6_opt_hdr),
3811 MAX_IPV6_HDR_LEN);
3812 if (err < 0)
3813 goto out;
3814
3815 hp = OPT_HDR(struct ipv6_opt_hdr, skb, off);
3816 nexthdr = hp->nexthdr;
3817 off += ipv6_optlen(hp);
3818 break;
3819 }
3820 case IPPROTO_AH: {
3821 struct ip_auth_hdr *hp;
3822
3823 err = skb_maybe_pull_tail(skb,
3824 off +
3825 sizeof(struct ip_auth_hdr),
3826 MAX_IPV6_HDR_LEN);
3827 if (err < 0)
3828 goto out;
3829
3830 hp = OPT_HDR(struct ip_auth_hdr, skb, off);
3831 nexthdr = hp->nexthdr;
3832 off += ipv6_authlen(hp);
3833 break;
3834 }
3835 case IPPROTO_FRAGMENT: {
3836 struct frag_hdr *hp;
3837
3838 err = skb_maybe_pull_tail(skb,
3839 off +
3840 sizeof(struct frag_hdr),
3841 MAX_IPV6_HDR_LEN);
3842 if (err < 0)
3843 goto out;
3844
3845 hp = OPT_HDR(struct frag_hdr, skb, off);
3846
3847 if (hp->frag_off & htons(IP6_OFFSET | IP6_MF))
3848 fragment = true;
3849
3850 nexthdr = hp->nexthdr;
3851 off += sizeof(struct frag_hdr);
3852 break;
3853 }
3854 default:
3855 done = true;
3856 break;
3857 }
3858 }
3859
3860 err = -EPROTO;
3861
3862 if (!done || fragment)
3863 goto out;
3864
Jan Beulichf9708b42014-03-11 13:56:05 +00003865 csum = skb_checksum_setup_ip(skb, nexthdr, off);
3866 if (IS_ERR(csum))
3867 return PTR_ERR(csum);
Paul Durranted1f50c2014-01-09 10:02:46 +00003868
Jan Beulichf9708b42014-03-11 13:56:05 +00003869 if (recalculate)
3870 *csum = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
3871 &ipv6_hdr(skb)->daddr,
3872 skb->len - off, nexthdr, 0);
Paul Durranted1f50c2014-01-09 10:02:46 +00003873 err = 0;
3874
3875out:
3876 return err;
3877}
3878
3879/**
3880 * skb_checksum_setup - set up partial checksum offset
3881 * @skb: the skb to set up
3882 * @recalculate: if true the pseudo-header checksum will be recalculated
3883 */
3884int skb_checksum_setup(struct sk_buff *skb, bool recalculate)
3885{
3886 int err;
3887
3888 switch (skb->protocol) {
3889 case htons(ETH_P_IP):
Jan Beulichf9708b42014-03-11 13:56:05 +00003890 err = skb_checksum_setup_ipv4(skb, recalculate);
Paul Durranted1f50c2014-01-09 10:02:46 +00003891 break;
3892
3893 case htons(ETH_P_IPV6):
3894 err = skb_checksum_setup_ipv6(skb, recalculate);
3895 break;
3896
3897 default:
3898 err = -EPROTO;
3899 break;
3900 }
3901
3902 return err;
3903}
3904EXPORT_SYMBOL(skb_checksum_setup);
3905
Ben Hutchings4497b072008-06-19 16:22:28 -07003906void __skb_warn_lro_forwarding(const struct sk_buff *skb)
3907{
Joe Perchese87cc472012-05-13 21:56:26 +00003908 net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n",
3909 skb->dev->name);
Ben Hutchings4497b072008-06-19 16:22:28 -07003910}
Ben Hutchings4497b072008-06-19 16:22:28 -07003911EXPORT_SYMBOL(__skb_warn_lro_forwarding);
Eric Dumazetbad43ca2012-05-19 03:02:02 +00003912
3913void kfree_skb_partial(struct sk_buff *skb, bool head_stolen)
3914{
Eric Dumazet3d861f62012-10-22 09:03:40 +00003915 if (head_stolen) {
3916 skb_release_head_state(skb);
Eric Dumazetbad43ca2012-05-19 03:02:02 +00003917 kmem_cache_free(skbuff_head_cache, skb);
Eric Dumazet3d861f62012-10-22 09:03:40 +00003918 } else {
Eric Dumazetbad43ca2012-05-19 03:02:02 +00003919 __kfree_skb(skb);
Eric Dumazet3d861f62012-10-22 09:03:40 +00003920 }
Eric Dumazetbad43ca2012-05-19 03:02:02 +00003921}
3922EXPORT_SYMBOL(kfree_skb_partial);
3923
3924/**
3925 * skb_try_coalesce - try to merge skb to prior one
3926 * @to: prior buffer
3927 * @from: buffer to add
3928 * @fragstolen: pointer to boolean
Randy Dunlapc6c4b972012-06-08 14:01:44 +00003929 * @delta_truesize: how much more was allocated than was requested
Eric Dumazetbad43ca2012-05-19 03:02:02 +00003930 */
3931bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
3932 bool *fragstolen, int *delta_truesize)
3933{
3934 int i, delta, len = from->len;
3935
3936 *fragstolen = false;
3937
3938 if (skb_cloned(to))
3939 return false;
3940
3941 if (len <= skb_tailroom(to)) {
Eric Dumazete93a0432014-09-15 04:19:52 -07003942 if (len)
3943 BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len));
Eric Dumazetbad43ca2012-05-19 03:02:02 +00003944 *delta_truesize = 0;
3945 return true;
3946 }
3947
3948 if (skb_has_frag_list(to) || skb_has_frag_list(from))
3949 return false;
3950
3951 if (skb_headlen(from) != 0) {
3952 struct page *page;
3953 unsigned int offset;
3954
3955 if (skb_shinfo(to)->nr_frags +
3956 skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
3957 return false;
3958
3959 if (skb_head_is_locked(from))
3960 return false;
3961
3962 delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
3963
3964 page = virt_to_head_page(from->head);
3965 offset = from->data - (unsigned char *)page_address(page);
3966
3967 skb_fill_page_desc(to, skb_shinfo(to)->nr_frags,
3968 page, offset, skb_headlen(from));
3969 *fragstolen = true;
3970 } else {
3971 if (skb_shinfo(to)->nr_frags +
3972 skb_shinfo(from)->nr_frags > MAX_SKB_FRAGS)
3973 return false;
3974
Weiping Panf4b549a2012-09-28 20:15:30 +00003975 delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from));
Eric Dumazetbad43ca2012-05-19 03:02:02 +00003976 }
3977
3978 WARN_ON_ONCE(delta < len);
3979
3980 memcpy(skb_shinfo(to)->frags + skb_shinfo(to)->nr_frags,
3981 skb_shinfo(from)->frags,
3982 skb_shinfo(from)->nr_frags * sizeof(skb_frag_t));
3983 skb_shinfo(to)->nr_frags += skb_shinfo(from)->nr_frags;
3984
3985 if (!skb_cloned(from))
3986 skb_shinfo(from)->nr_frags = 0;
3987
Li RongQing8ea853f2012-09-18 16:53:21 +00003988 /* if the skb is not cloned this does nothing
3989 * since we set nr_frags to 0.
3990 */
Eric Dumazetbad43ca2012-05-19 03:02:02 +00003991 for (i = 0; i < skb_shinfo(from)->nr_frags; i++)
3992 skb_frag_ref(from, i);
3993
3994 to->truesize += delta;
3995 to->len += len;
3996 to->data_len += len;
3997
3998 *delta_truesize = delta;
3999 return true;
4000}
4001EXPORT_SYMBOL(skb_try_coalesce);
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004002
4003/**
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004004 * skb_scrub_packet - scrub an skb
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004005 *
4006 * @skb: buffer to clean
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004007 * @xnet: packet is crossing netns
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004008 *
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004009 * skb_scrub_packet can be used after encapsulating or decapsulting a packet
4010 * into/from a tunnel. Some information have to be cleared during these
4011 * operations.
4012 * skb_scrub_packet can also be used to clean a skb before injecting it in
4013 * another namespace (@xnet == true). We have to clear all information in the
4014 * skb that could impact namespace isolation.
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004015 */
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004016void skb_scrub_packet(struct sk_buff *skb, bool xnet)
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004017{
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004018 if (xnet)
4019 skb_orphan(skb);
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004020 skb->tstamp.tv64 = 0;
4021 skb->pkt_type = PACKET_HOST;
4022 skb->skb_iif = 0;
WANG Cong60ff7462014-05-04 16:39:18 -07004023 skb->ignore_df = 0;
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004024 skb_dst_drop(skb);
4025 skb->mark = 0;
4026 secpath_reset(skb);
4027 nf_reset(skb);
4028 nf_reset_trace(skb);
4029}
4030EXPORT_SYMBOL_GPL(skb_scrub_packet);
Florian Westphalde960aa2014-01-26 10:58:16 +01004031
4032/**
4033 * skb_gso_transport_seglen - Return length of individual segments of a gso packet
4034 *
4035 * @skb: GSO skb
4036 *
4037 * skb_gso_transport_seglen is used to determine the real size of the
4038 * individual segments, including Layer4 headers (TCP/UDP).
4039 *
4040 * The MAC/L2 or network (IP, IPv6) headers are not accounted for.
4041 */
4042unsigned int skb_gso_transport_seglen(const struct sk_buff *skb)
4043{
4044 const struct skb_shared_info *shinfo = skb_shinfo(skb);
Florian Westphalde960aa2014-01-26 10:58:16 +01004045
4046 if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)))
Florian Westphal6d39d582014-04-09 10:28:50 +02004047 return tcp_hdrlen(skb) + shinfo->gso_size;
4048
4049 /* UFO sets gso_size to the size of the fragmentation
4050 * payload, i.e. the size of the L4 (UDP) header is already
4051 * accounted for.
4052 */
4053 return shinfo->gso_size;
Florian Westphalde960aa2014-01-26 10:58:16 +01004054}
4055EXPORT_SYMBOL_GPL(skb_gso_transport_seglen);
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04004056
4057static struct sk_buff *skb_reorder_vlan_header(struct sk_buff *skb)
4058{
4059 if (skb_cow(skb, skb_headroom(skb)) < 0) {
4060 kfree_skb(skb);
4061 return NULL;
4062 }
4063
4064 memmove(skb->data - ETH_HLEN, skb->data - VLAN_ETH_HLEN, 2 * ETH_ALEN);
4065 skb->mac_header += VLAN_HLEN;
4066 return skb;
4067}
4068
4069struct sk_buff *skb_vlan_untag(struct sk_buff *skb)
4070{
4071 struct vlan_hdr *vhdr;
4072 u16 vlan_tci;
4073
4074 if (unlikely(vlan_tx_tag_present(skb))) {
4075 /* vlan_tci is already set-up so leave this for another time */
4076 return skb;
4077 }
4078
4079 skb = skb_share_check(skb, GFP_ATOMIC);
4080 if (unlikely(!skb))
4081 goto err_free;
4082
4083 if (unlikely(!pskb_may_pull(skb, VLAN_HLEN)))
4084 goto err_free;
4085
4086 vhdr = (struct vlan_hdr *)skb->data;
4087 vlan_tci = ntohs(vhdr->h_vlan_TCI);
4088 __vlan_hwaccel_put_tag(skb, skb->protocol, vlan_tci);
4089
4090 skb_pull_rcsum(skb, VLAN_HLEN);
4091 vlan_set_encap_proto(skb, vhdr);
4092
4093 skb = skb_reorder_vlan_header(skb);
4094 if (unlikely(!skb))
4095 goto err_free;
4096
4097 skb_reset_network_header(skb);
4098 skb_reset_transport_header(skb);
4099 skb_reset_mac_len(skb);
4100
4101 return skb;
4102
4103err_free:
4104 kfree_skb(skb);
4105 return NULL;
4106}
4107EXPORT_SYMBOL(skb_vlan_untag);
Eric Dumazet2e4e4412014-09-17 04:49:49 -07004108
4109/**
4110 * alloc_skb_with_frags - allocate skb with page frags
4111 *
4112 * header_len: size of linear part
4113 * data_len: needed length in frags
4114 * max_page_order: max page order desired.
4115 * errcode: pointer to error code if any
4116 * gfp_mask: allocation mask
4117 *
4118 * This can be used to allocate a paged skb, given a maximal order for frags.
4119 */
4120struct sk_buff *alloc_skb_with_frags(unsigned long header_len,
4121 unsigned long data_len,
4122 int max_page_order,
4123 int *errcode,
4124 gfp_t gfp_mask)
4125{
4126 int npages = (data_len + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
4127 unsigned long chunk;
4128 struct sk_buff *skb;
4129 struct page *page;
4130 gfp_t gfp_head;
4131 int i;
4132
4133 *errcode = -EMSGSIZE;
4134 /* Note this test could be relaxed, if we succeed to allocate
4135 * high order pages...
4136 */
4137 if (npages > MAX_SKB_FRAGS)
4138 return NULL;
4139
4140 gfp_head = gfp_mask;
4141 if (gfp_head & __GFP_WAIT)
4142 gfp_head |= __GFP_REPEAT;
4143
4144 *errcode = -ENOBUFS;
4145 skb = alloc_skb(header_len, gfp_head);
4146 if (!skb)
4147 return NULL;
4148
4149 skb->truesize += npages << PAGE_SHIFT;
4150
4151 for (i = 0; npages > 0; i++) {
4152 int order = max_page_order;
4153
4154 while (order) {
4155 if (npages >= 1 << order) {
4156 page = alloc_pages(gfp_mask |
4157 __GFP_COMP |
4158 __GFP_NOWARN |
4159 __GFP_NORETRY,
4160 order);
4161 if (page)
4162 goto fill_page;
4163 /* Do not retry other high order allocations */
4164 order = 1;
4165 max_page_order = 0;
4166 }
4167 order--;
4168 }
4169 page = alloc_page(gfp_mask);
4170 if (!page)
4171 goto failure;
4172fill_page:
4173 chunk = min_t(unsigned long, data_len,
4174 PAGE_SIZE << order);
4175 skb_fill_page_desc(skb, i, page, 0, chunk);
4176 data_len -= chunk;
4177 npages -= 1 << order;
4178 }
4179 return skb;
4180
4181failure:
4182 kfree_skb(skb);
4183 return NULL;
4184}
4185EXPORT_SYMBOL(alloc_skb_with_frags);