blob: 32e31c2996315770149b71b1e17837311d672a1e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Routines having to do with the 'struct sk_buff' memory handlers.
3 *
Alan Cox113aa832008-10-13 19:01:08 -07004 * Authors: Alan Cox <alan@lxorguk.ukuu.org.uk>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Florian La Roche <rzsfl@rz.uni-sb.de>
6 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * Fixes:
8 * Alan Cox : Fixed the worst of the load
9 * balancer bugs.
10 * Dave Platt : Interrupt stacking fix.
11 * Richard Kooijman : Timestamp fixes.
12 * Alan Cox : Changed buffer format.
13 * Alan Cox : destructor hook for AF_UNIX etc.
14 * Linus Torvalds : Better skb_clone.
15 * Alan Cox : Added skb_copy.
16 * Alan Cox : Added all the changed routines Linus
17 * only put in the headers
18 * Ray VanTassle : Fixed --skb->lock in free
19 * Alan Cox : skb_copy copy arp field
20 * Andi Kleen : slabified it.
21 * Robert Olsson : Removed skb_head_pool
22 *
23 * NOTE:
24 * The __skb_ routines should be called with interrupts
25 * disabled, or you better be *real* sure that the operation is atomic
26 * with respect to whatever list is being frobbed (e.g. via lock_sock()
27 * or via disabling bottom half handlers, etc).
28 *
29 * This program is free software; you can redistribute it and/or
30 * modify it under the terms of the GNU General Public License
31 * as published by the Free Software Foundation; either version
32 * 2 of the License, or (at your option) any later version.
33 */
34
35/*
36 * The functions in this file will not compile correctly with gcc 2.4.x
37 */
38
Joe Perchese005d192012-05-16 19:58:40 +000039#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/module.h>
42#include <linux/types.h>
43#include <linux/kernel.h>
Vegard Nossumfe55f6d2008-08-30 12:16:35 +020044#include <linux/kmemcheck.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/mm.h>
46#include <linux/interrupt.h>
47#include <linux/in.h>
48#include <linux/inet.h>
49#include <linux/slab.h>
Florian Westphalde960aa2014-01-26 10:58:16 +010050#include <linux/tcp.h>
51#include <linux/udp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <linux/netdevice.h>
53#ifdef CONFIG_NET_CLS_ACT
54#include <net/pkt_sched.h>
55#endif
56#include <linux/string.h>
57#include <linux/skbuff.h>
Jens Axboe9c55e012007-11-06 23:30:13 -080058#include <linux/splice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#include <linux/cache.h>
60#include <linux/rtnetlink.h>
61#include <linux/init.h>
David Howells716ea3a2007-04-02 20:19:53 -070062#include <linux/scatterlist.h>
Patrick Ohlyac45f602009-02-12 05:03:37 +000063#include <linux/errqueue.h>
Linus Torvalds268bb0c2011-05-20 12:50:29 -070064#include <linux/prefetch.h>
Vlad Yasevich0d5501c2014-08-08 14:42:13 -040065#include <linux/if_vlan.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67#include <net/protocol.h>
68#include <net/dst.h>
69#include <net/sock.h>
70#include <net/checksum.h>
Paul Durranted1f50c2014-01-09 10:02:46 +000071#include <net/ip6_checksum.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#include <net/xfrm.h>
73
74#include <asm/uaccess.h>
Steven Rostedtad8d75f2009-04-14 19:39:12 -040075#include <trace/events/skb.h>
Eric Dumazet51c56b02012-04-05 11:35:15 +020076#include <linux/highmem.h>
Al Viroa1f8e7f72006-10-19 16:08:53 -040077
Eric Dumazetd7e88832012-04-30 08:10:34 +000078struct kmem_cache *skbuff_head_cache __read_mostly;
Christoph Lametere18b8902006-12-06 20:33:20 -080079static struct kmem_cache *skbuff_fclone_cache __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Linus Torvalds1da177e2005-04-16 15:20:36 -070081/**
Jean Sacrenf05de732013-02-11 13:30:38 +000082 * skb_panic - private function for out-of-line support
83 * @skb: buffer
84 * @sz: size
85 * @addr: address
James Hogan99d58512013-02-13 11:20:27 +000086 * @msg: skb_over_panic or skb_under_panic
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 *
Jean Sacrenf05de732013-02-11 13:30:38 +000088 * Out-of-line support for skb_put() and skb_push().
89 * Called via the wrapper skb_over_panic() or skb_under_panic().
90 * Keep out of line to prevent kernel bloat.
91 * __builtin_return_address is not used because it is not always reliable.
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 */
Jean Sacrenf05de732013-02-11 13:30:38 +000093static void skb_panic(struct sk_buff *skb, unsigned int sz, void *addr,
James Hogan99d58512013-02-13 11:20:27 +000094 const char msg[])
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
Joe Perchese005d192012-05-16 19:58:40 +000096 pr_emerg("%s: text:%p len:%d put:%d head:%p data:%p tail:%#lx end:%#lx dev:%s\n",
James Hogan99d58512013-02-13 11:20:27 +000097 msg, addr, skb->len, sz, skb->head, skb->data,
Joe Perchese005d192012-05-16 19:58:40 +000098 (unsigned long)skb->tail, (unsigned long)skb->end,
99 skb->dev ? skb->dev->name : "<NULL>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 BUG();
101}
102
Jean Sacrenf05de732013-02-11 13:30:38 +0000103static void skb_over_panic(struct sk_buff *skb, unsigned int sz, void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104{
Jean Sacrenf05de732013-02-11 13:30:38 +0000105 skb_panic(skb, sz, addr, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106}
107
Jean Sacrenf05de732013-02-11 13:30:38 +0000108static void skb_under_panic(struct sk_buff *skb, unsigned int sz, void *addr)
109{
110 skb_panic(skb, sz, addr, __func__);
111}
Mel Gormanc93bdd02012-07-31 16:44:19 -0700112
113/*
114 * kmalloc_reserve is a wrapper around kmalloc_node_track_caller that tells
115 * the caller if emergency pfmemalloc reserves are being used. If it is and
116 * the socket is later found to be SOCK_MEMALLOC then PFMEMALLOC reserves
117 * may be used. Otherwise, the packet data may be discarded until enough
118 * memory is free
119 */
120#define kmalloc_reserve(size, gfp, node, pfmemalloc) \
121 __kmalloc_reserve(size, gfp, node, _RET_IP_, pfmemalloc)
stephen hemminger61c5e882012-12-28 18:24:28 +0000122
123static void *__kmalloc_reserve(size_t size, gfp_t flags, int node,
124 unsigned long ip, bool *pfmemalloc)
Mel Gormanc93bdd02012-07-31 16:44:19 -0700125{
126 void *obj;
127 bool ret_pfmemalloc = false;
128
129 /*
130 * Try a regular allocation, when that fails and we're not entitled
131 * to the reserves, fail.
132 */
133 obj = kmalloc_node_track_caller(size,
134 flags | __GFP_NOMEMALLOC | __GFP_NOWARN,
135 node);
136 if (obj || !(gfp_pfmemalloc_allowed(flags)))
137 goto out;
138
139 /* Try again but now we are using pfmemalloc reserves */
140 ret_pfmemalloc = true;
141 obj = kmalloc_node_track_caller(size, flags, node);
142
143out:
144 if (pfmemalloc)
145 *pfmemalloc = ret_pfmemalloc;
146
147 return obj;
148}
149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150/* Allocate a new skbuff. We do this ourselves so we can fill in a few
151 * 'private' fields and also do memory statistics to find all the
152 * [BEEP] leaks.
153 *
154 */
155
Patrick McHardy0ebd0ac2013-04-17 06:46:58 +0000156struct sk_buff *__alloc_skb_head(gfp_t gfp_mask, int node)
157{
158 struct sk_buff *skb;
159
160 /* Get the HEAD */
161 skb = kmem_cache_alloc_node(skbuff_head_cache,
162 gfp_mask & ~__GFP_DMA, node);
163 if (!skb)
164 goto out;
165
166 /*
167 * Only clear those fields we need to clear, not those that we will
168 * actually initialise below. Hence, don't put any more fields after
169 * the tail pointer in struct sk_buff!
170 */
171 memset(skb, 0, offsetof(struct sk_buff, tail));
Pablo Neira5e71d9d2013-06-03 09:28:43 +0000172 skb->head = NULL;
Patrick McHardy0ebd0ac2013-04-17 06:46:58 +0000173 skb->truesize = sizeof(struct sk_buff);
174 atomic_set(&skb->users, 1);
175
Cong Wang35d04612013-05-29 15:16:05 +0800176 skb->mac_header = (typeof(skb->mac_header))~0U;
Patrick McHardy0ebd0ac2013-04-17 06:46:58 +0000177out:
178 return skb;
179}
180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181/**
David S. Millerd179cd12005-08-17 14:57:30 -0700182 * __alloc_skb - allocate a network buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 * @size: size to allocate
184 * @gfp_mask: allocation mask
Mel Gormanc93bdd02012-07-31 16:44:19 -0700185 * @flags: If SKB_ALLOC_FCLONE is set, allocate from fclone cache
186 * instead of head cache and allocate a cloned (child) skb.
187 * If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for
188 * allocations in case the data is required for writeback
Christoph Hellwigb30973f2006-12-06 20:32:36 -0800189 * @node: numa node to allocate memory on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 *
191 * Allocate a new &sk_buff. The returned buffer has no headroom and a
Ben Hutchings94b60422012-06-06 15:23:37 +0000192 * tail room of at least size bytes. The object has a reference count
193 * of one. The return is the buffer. On a failure the return is %NULL.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 *
195 * Buffers may only be allocated from interrupts using a @gfp_mask of
196 * %GFP_ATOMIC.
197 */
Al Virodd0fc662005-10-07 07:46:04 +0100198struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
Mel Gormanc93bdd02012-07-31 16:44:19 -0700199 int flags, int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
Christoph Lametere18b8902006-12-06 20:33:20 -0800201 struct kmem_cache *cache;
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800202 struct skb_shared_info *shinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 struct sk_buff *skb;
204 u8 *data;
Mel Gormanc93bdd02012-07-31 16:44:19 -0700205 bool pfmemalloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
Mel Gormanc93bdd02012-07-31 16:44:19 -0700207 cache = (flags & SKB_ALLOC_FCLONE)
208 ? skbuff_fclone_cache : skbuff_head_cache;
209
210 if (sk_memalloc_socks() && (flags & SKB_ALLOC_RX))
211 gfp_mask |= __GFP_MEMALLOC;
Herbert Xu8798b3f2006-01-23 16:32:45 -0800212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 /* Get the HEAD */
Christoph Hellwigb30973f2006-12-06 20:32:36 -0800214 skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 if (!skb)
216 goto out;
Eric Dumazetec7d2f22010-05-05 01:07:37 -0700217 prefetchw(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
Eric Dumazet87fb4b72011-10-13 07:28:54 +0000219 /* We do our best to align skb_shared_info on a separate cache
220 * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives
221 * aligned memory blocks, unless SLUB/SLAB debug is enabled.
222 * Both skb->head and skb_shared_info are cache line aligned.
223 */
Tony Lindgrenbc417e32011-11-02 13:40:28 +0000224 size = SKB_DATA_ALIGN(size);
Eric Dumazet87fb4b72011-10-13 07:28:54 +0000225 size += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Mel Gormanc93bdd02012-07-31 16:44:19 -0700226 data = kmalloc_reserve(size, gfp_mask, node, &pfmemalloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 if (!data)
228 goto nodata;
Eric Dumazet87fb4b72011-10-13 07:28:54 +0000229 /* kmalloc(size) might give us more room than requested.
230 * Put skb_shared_info exactly at the end of allocated zone,
231 * to allow max possible filling before reallocation.
232 */
233 size = SKB_WITH_OVERHEAD(ksize(data));
Eric Dumazetec7d2f22010-05-05 01:07:37 -0700234 prefetchw(data + size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Arnaldo Carvalho de Meloca0605a2007-03-19 10:48:59 -0300236 /*
Johannes Bergc8005782008-05-03 20:56:42 -0700237 * Only clear those fields we need to clear, not those that we will
238 * actually initialise below. Hence, don't put any more fields after
239 * the tail pointer in struct sk_buff!
Arnaldo Carvalho de Meloca0605a2007-03-19 10:48:59 -0300240 */
241 memset(skb, 0, offsetof(struct sk_buff, tail));
Eric Dumazet87fb4b72011-10-13 07:28:54 +0000242 /* Account for allocated memory : skb + skb->head */
243 skb->truesize = SKB_TRUESIZE(size);
Mel Gormanc93bdd02012-07-31 16:44:19 -0700244 skb->pfmemalloc = pfmemalloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 atomic_set(&skb->users, 1);
246 skb->head = data;
247 skb->data = data;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700248 skb_reset_tail_pointer(skb);
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700249 skb->end = skb->tail + size;
Cong Wang35d04612013-05-29 15:16:05 +0800250 skb->mac_header = (typeof(skb->mac_header))~0U;
251 skb->transport_header = (typeof(skb->transport_header))~0U;
Stephen Hemminger19633e12009-06-17 05:23:27 +0000252
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800253 /* make sure we initialize shinfo sequentially */
254 shinfo = skb_shinfo(skb);
Eric Dumazetec7d2f22010-05-05 01:07:37 -0700255 memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800256 atomic_set(&shinfo->dataref, 1);
Eric Dumazetc2aa3662011-01-25 23:18:38 +0000257 kmemcheck_annotate_variable(shinfo->destructor_arg);
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800258
Mel Gormanc93bdd02012-07-31 16:44:19 -0700259 if (flags & SKB_ALLOC_FCLONE) {
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700260 struct sk_buff_fclones *fclones;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700262 fclones = container_of(skb, struct sk_buff_fclones, skb1);
263
264 kmemcheck_annotate_bitfield(&fclones->skb2, flags1);
David S. Millerd179cd12005-08-17 14:57:30 -0700265 skb->fclone = SKB_FCLONE_ORIG;
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700266 atomic_set(&fclones->fclone_ref, 1);
David S. Millerd179cd12005-08-17 14:57:30 -0700267
Vijay Subramanianc8753d52014-10-02 10:00:43 -0700268 fclones->skb2.fclone = SKB_FCLONE_FREE;
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700269 fclones->skb2.pfmemalloc = pfmemalloc;
David S. Millerd179cd12005-08-17 14:57:30 -0700270 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271out:
272 return skb;
273nodata:
Herbert Xu8798b3f2006-01-23 16:32:45 -0800274 kmem_cache_free(cache, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 skb = NULL;
276 goto out;
277}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800278EXPORT_SYMBOL(__alloc_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
280/**
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000281 * build_skb - build a network buffer
282 * @data: data buffer provided by caller
Eric Dumazetd3836f22012-04-27 00:33:38 +0000283 * @frag_size: size of fragment, or 0 if head was kmalloced
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000284 *
285 * Allocate a new &sk_buff. Caller provides space holding head and
Florian Fainellideceb4c2013-07-23 20:22:39 +0100286 * skb_shared_info. @data must have been allocated by kmalloc() only if
287 * @frag_size is 0, otherwise data should come from the page allocator.
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000288 * The return is the new skb buffer.
289 * On a failure the return is %NULL, and @data is not freed.
290 * Notes :
291 * Before IO, driver allocates only data buffer where NIC put incoming frame
292 * Driver should add room at head (NET_SKB_PAD) and
293 * MUST add room at tail (SKB_DATA_ALIGN(skb_shared_info))
294 * After IO, driver calls build_skb(), to allocate sk_buff and populate it
295 * before giving packet to stack.
296 * RX rings only contains data buffers, not full skbs.
297 */
Eric Dumazetd3836f22012-04-27 00:33:38 +0000298struct sk_buff *build_skb(void *data, unsigned int frag_size)
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000299{
300 struct skb_shared_info *shinfo;
301 struct sk_buff *skb;
Eric Dumazetd3836f22012-04-27 00:33:38 +0000302 unsigned int size = frag_size ? : ksize(data);
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000303
304 skb = kmem_cache_alloc(skbuff_head_cache, GFP_ATOMIC);
305 if (!skb)
306 return NULL;
307
Eric Dumazetd3836f22012-04-27 00:33:38 +0000308 size -= SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000309
310 memset(skb, 0, offsetof(struct sk_buff, tail));
311 skb->truesize = SKB_TRUESIZE(size);
Eric Dumazetd3836f22012-04-27 00:33:38 +0000312 skb->head_frag = frag_size != 0;
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000313 atomic_set(&skb->users, 1);
314 skb->head = data;
315 skb->data = data;
316 skb_reset_tail_pointer(skb);
317 skb->end = skb->tail + size;
Cong Wang35d04612013-05-29 15:16:05 +0800318 skb->mac_header = (typeof(skb->mac_header))~0U;
319 skb->transport_header = (typeof(skb->transport_header))~0U;
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000320
321 /* make sure we initialize shinfo sequentially */
322 shinfo = skb_shinfo(skb);
323 memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
324 atomic_set(&shinfo->dataref, 1);
325 kmemcheck_annotate_variable(shinfo->destructor_arg);
326
327 return skb;
328}
329EXPORT_SYMBOL(build_skb);
330
Eric Dumazeta1c7fff2012-05-17 07:34:16 +0000331struct netdev_alloc_cache {
Eric Dumazet69b08f62012-09-26 06:46:57 +0000332 struct page_frag frag;
333 /* we maintain a pagecount bias, so that we dont dirty cache line
334 * containing page->_count every time we allocate a fragment.
335 */
336 unsigned int pagecnt_bias;
Eric Dumazeta1c7fff2012-05-17 07:34:16 +0000337};
338static DEFINE_PER_CPU(struct netdev_alloc_cache, netdev_alloc_cache);
339
Mel Gormanc93bdd02012-07-31 16:44:19 -0700340static void *__netdev_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
Eric Dumazet6f532612012-05-18 05:12:12 +0000341{
342 struct netdev_alloc_cache *nc;
343 void *data = NULL;
Eric Dumazet69b08f62012-09-26 06:46:57 +0000344 int order;
Eric Dumazet6f532612012-05-18 05:12:12 +0000345 unsigned long flags;
346
347 local_irq_save(flags);
Christoph Lameter903ceff2014-08-17 12:30:35 -0500348 nc = this_cpu_ptr(&netdev_alloc_cache);
Eric Dumazet69b08f62012-09-26 06:46:57 +0000349 if (unlikely(!nc->frag.page)) {
Eric Dumazet6f532612012-05-18 05:12:12 +0000350refill:
Eric Dumazet69b08f62012-09-26 06:46:57 +0000351 for (order = NETDEV_FRAG_PAGE_MAX_ORDER; ;) {
352 gfp_t gfp = gfp_mask;
353
354 if (order)
355 gfp |= __GFP_COMP | __GFP_NOWARN;
356 nc->frag.page = alloc_pages(gfp, order);
357 if (likely(nc->frag.page))
358 break;
359 if (--order < 0)
360 goto end;
361 }
362 nc->frag.size = PAGE_SIZE << order;
Eric Dumazet4c450582014-10-10 04:48:18 -0700363 /* Even if we own the page, we do not use atomic_set().
364 * This would break get_page_unless_zero() users.
365 */
366 atomic_add(NETDEV_PAGECNT_MAX_BIAS - 1,
367 &nc->frag.page->_count);
Eric Dumazet69b08f62012-09-26 06:46:57 +0000368 nc->pagecnt_bias = NETDEV_PAGECNT_MAX_BIAS;
369 nc->frag.offset = 0;
Eric Dumazet6f532612012-05-18 05:12:12 +0000370 }
Alexander Duyck540eb7b2012-07-12 14:23:50 +0000371
Eric Dumazet69b08f62012-09-26 06:46:57 +0000372 if (nc->frag.offset + fragsz > nc->frag.size) {
Eric Dumazet4c450582014-10-10 04:48:18 -0700373 if (atomic_read(&nc->frag.page->_count) != nc->pagecnt_bias) {
374 if (!atomic_sub_and_test(nc->pagecnt_bias,
375 &nc->frag.page->_count))
376 goto refill;
377 /* OK, page count is 0, we can safely set it */
378 atomic_set(&nc->frag.page->_count,
379 NETDEV_PAGECNT_MAX_BIAS);
380 } else {
381 atomic_add(NETDEV_PAGECNT_MAX_BIAS - nc->pagecnt_bias,
382 &nc->frag.page->_count);
383 }
384 nc->pagecnt_bias = NETDEV_PAGECNT_MAX_BIAS;
385 nc->frag.offset = 0;
Eric Dumazet6f532612012-05-18 05:12:12 +0000386 }
Alexander Duyck540eb7b2012-07-12 14:23:50 +0000387
Eric Dumazet69b08f62012-09-26 06:46:57 +0000388 data = page_address(nc->frag.page) + nc->frag.offset;
389 nc->frag.offset += fragsz;
Alexander Duyck540eb7b2012-07-12 14:23:50 +0000390 nc->pagecnt_bias--;
391end:
Eric Dumazet6f532612012-05-18 05:12:12 +0000392 local_irq_restore(flags);
393 return data;
394}
Mel Gormanc93bdd02012-07-31 16:44:19 -0700395
396/**
397 * netdev_alloc_frag - allocate a page fragment
398 * @fragsz: fragment size
399 *
400 * Allocates a frag from a page for receive buffer.
401 * Uses GFP_ATOMIC allocations.
402 */
403void *netdev_alloc_frag(unsigned int fragsz)
404{
405 return __netdev_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD);
406}
Eric Dumazet6f532612012-05-18 05:12:12 +0000407EXPORT_SYMBOL(netdev_alloc_frag);
408
409/**
Christoph Hellwig8af27452006-07-31 22:35:23 -0700410 * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
411 * @dev: network device to receive on
412 * @length: length to allocate
413 * @gfp_mask: get_free_pages mask, passed to alloc_skb
414 *
415 * Allocate a new &sk_buff and assign it a usage count of one. The
416 * buffer has unspecified headroom built in. Users should allocate
417 * the headroom they think they need without accounting for the
418 * built in space. The built in space is used for optimisations.
419 *
420 * %NULL is returned if there is no free memory.
421 */
422struct sk_buff *__netdev_alloc_skb(struct net_device *dev,
Eric Dumazet6f532612012-05-18 05:12:12 +0000423 unsigned int length, gfp_t gfp_mask)
Christoph Hellwig8af27452006-07-31 22:35:23 -0700424{
Eric Dumazet6f532612012-05-18 05:12:12 +0000425 struct sk_buff *skb = NULL;
Eric Dumazeta1c7fff2012-05-17 07:34:16 +0000426 unsigned int fragsz = SKB_DATA_ALIGN(length + NET_SKB_PAD) +
427 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Christoph Hellwig8af27452006-07-31 22:35:23 -0700428
Eric Dumazet310e1582012-07-16 13:15:52 +0200429 if (fragsz <= PAGE_SIZE && !(gfp_mask & (__GFP_WAIT | GFP_DMA))) {
Mel Gormanc93bdd02012-07-31 16:44:19 -0700430 void *data;
431
432 if (sk_memalloc_socks())
433 gfp_mask |= __GFP_MEMALLOC;
434
435 data = __netdev_alloc_frag(fragsz, gfp_mask);
Eric Dumazeta1c7fff2012-05-17 07:34:16 +0000436
Eric Dumazet6f532612012-05-18 05:12:12 +0000437 if (likely(data)) {
438 skb = build_skb(data, fragsz);
439 if (unlikely(!skb))
440 put_page(virt_to_head_page(data));
Eric Dumazeta1c7fff2012-05-17 07:34:16 +0000441 }
Eric Dumazeta1c7fff2012-05-17 07:34:16 +0000442 } else {
Mel Gormanc93bdd02012-07-31 16:44:19 -0700443 skb = __alloc_skb(length + NET_SKB_PAD, gfp_mask,
444 SKB_ALLOC_RX, NUMA_NO_NODE);
Eric Dumazeta1c7fff2012-05-17 07:34:16 +0000445 }
Christoph Hellwig7b2e4972006-08-07 16:09:04 -0700446 if (likely(skb)) {
Christoph Hellwig8af27452006-07-31 22:35:23 -0700447 skb_reserve(skb, NET_SKB_PAD);
Christoph Hellwig7b2e4972006-08-07 16:09:04 -0700448 skb->dev = dev;
449 }
Christoph Hellwig8af27452006-07-31 22:35:23 -0700450 return skb;
451}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800452EXPORT_SYMBOL(__netdev_alloc_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
Peter Zijlstra654bed12008-10-07 14:22:33 -0700454void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off,
Eric Dumazet50269e12012-03-23 23:59:33 +0000455 int size, unsigned int truesize)
Peter Zijlstra654bed12008-10-07 14:22:33 -0700456{
457 skb_fill_page_desc(skb, i, page, off, size);
458 skb->len += size;
459 skb->data_len += size;
Eric Dumazet50269e12012-03-23 23:59:33 +0000460 skb->truesize += truesize;
Peter Zijlstra654bed12008-10-07 14:22:33 -0700461}
462EXPORT_SYMBOL(skb_add_rx_frag);
463
Jason Wangf8e617e2013-11-01 14:07:47 +0800464void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size,
465 unsigned int truesize)
466{
467 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
468
469 skb_frag_size_add(frag, size);
470 skb->len += size;
471 skb->data_len += size;
472 skb->truesize += truesize;
473}
474EXPORT_SYMBOL(skb_coalesce_rx_frag);
475
Herbert Xu27b437c2006-07-13 19:26:39 -0700476static void skb_drop_list(struct sk_buff **listp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477{
Eric Dumazetbd8a7032013-06-24 06:26:00 -0700478 kfree_skb_list(*listp);
Herbert Xu27b437c2006-07-13 19:26:39 -0700479 *listp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480}
481
Herbert Xu27b437c2006-07-13 19:26:39 -0700482static inline void skb_drop_fraglist(struct sk_buff *skb)
483{
484 skb_drop_list(&skb_shinfo(skb)->frag_list);
485}
486
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487static void skb_clone_fraglist(struct sk_buff *skb)
488{
489 struct sk_buff *list;
490
David S. Millerfbb398a2009-06-09 00:18:59 -0700491 skb_walk_frags(skb, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 skb_get(list);
493}
494
Eric Dumazetd3836f22012-04-27 00:33:38 +0000495static void skb_free_head(struct sk_buff *skb)
496{
497 if (skb->head_frag)
498 put_page(virt_to_head_page(skb->head));
499 else
500 kfree(skb->head);
501}
502
Adrian Bunk5bba1712006-06-29 13:02:35 -0700503static void skb_release_data(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504{
Eric Dumazetff04a772014-09-23 18:39:30 -0700505 struct skb_shared_info *shinfo = skb_shinfo(skb);
506 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
Eric Dumazetff04a772014-09-23 18:39:30 -0700508 if (skb->cloned &&
509 atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
510 &shinfo->dataref))
511 return;
Shirley Maa6686f22011-07-06 12:22:12 +0000512
Eric Dumazetff04a772014-09-23 18:39:30 -0700513 for (i = 0; i < shinfo->nr_frags; i++)
514 __skb_frag_unref(&shinfo->frags[i]);
Shirley Maa6686f22011-07-06 12:22:12 +0000515
Eric Dumazetff04a772014-09-23 18:39:30 -0700516 /*
517 * If skb buf is from userspace, we need to notify the caller
518 * the lower device DMA has done;
519 */
520 if (shinfo->tx_flags & SKBTX_DEV_ZEROCOPY) {
521 struct ubuf_info *uarg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
Eric Dumazetff04a772014-09-23 18:39:30 -0700523 uarg = shinfo->destructor_arg;
524 if (uarg->callback)
525 uarg->callback(uarg, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 }
Eric Dumazetff04a772014-09-23 18:39:30 -0700527
528 if (shinfo->frag_list)
529 kfree_skb_list(shinfo->frag_list);
530
531 skb_free_head(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532}
533
534/*
535 * Free an skbuff by memory without cleaning the state.
536 */
Herbert Xu2d4baff2007-11-26 23:11:19 +0800537static void kfree_skbmem(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538{
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700539 struct sk_buff_fclones *fclones;
David S. Millerd179cd12005-08-17 14:57:30 -0700540
David S. Millerd179cd12005-08-17 14:57:30 -0700541 switch (skb->fclone) {
542 case SKB_FCLONE_UNAVAILABLE:
543 kmem_cache_free(skbuff_head_cache, skb);
544 break;
545
546 case SKB_FCLONE_ORIG:
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700547 fclones = container_of(skb, struct sk_buff_fclones, skb1);
548 if (atomic_dec_and_test(&fclones->fclone_ref))
549 kmem_cache_free(skbuff_fclone_cache, fclones);
David S. Millerd179cd12005-08-17 14:57:30 -0700550 break;
551
552 case SKB_FCLONE_CLONE:
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700553 fclones = container_of(skb, struct sk_buff_fclones, skb2);
David S. Millerd179cd12005-08-17 14:57:30 -0700554
Eric Dumazete7820e32014-11-21 11:47:16 -0800555 /* The clone portion is available for
556 * fast-cloning again.
David S. Millerd179cd12005-08-17 14:57:30 -0700557 */
Eric Dumazete7820e32014-11-21 11:47:16 -0800558 skb->fclone = SKB_FCLONE_FREE;
559
560 if (atomic_dec_and_test(&fclones->fclone_ref))
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700561 kmem_cache_free(skbuff_fclone_cache, fclones);
David S. Millerd179cd12005-08-17 14:57:30 -0700562 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700563 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564}
565
Lennert Buytenhek04a4bb52008-10-01 02:33:12 -0700566static void skb_release_head_state(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567{
Eric Dumazetadf30902009-06-02 05:19:30 +0000568 skb_dst_drop(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569#ifdef CONFIG_XFRM
570 secpath_put(skb->sp);
571#endif
Stephen Hemminger9c2b3322005-04-19 22:39:42 -0700572 if (skb->destructor) {
573 WARN_ON(in_irq());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 skb->destructor(skb);
575 }
Igor Maravića3bf7ae2011-12-12 02:58:22 +0000576#if IS_ENABLED(CONFIG_NF_CONNTRACK)
Yasuyuki Kozakai5f79e0f2007-03-23 11:17:07 -0700577 nf_conntrack_put(skb->nfct);
KOVACS Krisztian2fc72c72011-01-12 20:25:08 +0100578#endif
Pablo Neira Ayuso1109a902014-10-01 11:19:17 +0200579#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 nf_bridge_put(skb->nf_bridge);
581#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582/* XXX: IS this still necessary? - JHS */
583#ifdef CONFIG_NET_SCHED
584 skb->tc_index = 0;
585#ifdef CONFIG_NET_CLS_ACT
586 skb->tc_verd = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587#endif
588#endif
Lennert Buytenhek04a4bb52008-10-01 02:33:12 -0700589}
590
591/* Free everything but the sk_buff shell. */
592static void skb_release_all(struct sk_buff *skb)
593{
594 skb_release_head_state(skb);
Pablo Neira5e71d9d2013-06-03 09:28:43 +0000595 if (likely(skb->head))
Patrick McHardy0ebd0ac2013-04-17 06:46:58 +0000596 skb_release_data(skb);
Herbert Xu2d4baff2007-11-26 23:11:19 +0800597}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
Herbert Xu2d4baff2007-11-26 23:11:19 +0800599/**
600 * __kfree_skb - private function
601 * @skb: buffer
602 *
603 * Free an sk_buff. Release anything attached to the buffer.
604 * Clean the state. This is an internal helper function. Users should
605 * always call kfree_skb
606 */
607
608void __kfree_skb(struct sk_buff *skb)
609{
610 skb_release_all(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 kfree_skbmem(skb);
612}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800613EXPORT_SYMBOL(__kfree_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
615/**
Jörn Engel231d06a2006-03-20 21:28:35 -0800616 * kfree_skb - free an sk_buff
617 * @skb: buffer to free
618 *
619 * Drop a reference to the buffer and free it if the usage count has
620 * hit zero.
621 */
622void kfree_skb(struct sk_buff *skb)
623{
624 if (unlikely(!skb))
625 return;
626 if (likely(atomic_read(&skb->users) == 1))
627 smp_rmb();
628 else if (likely(!atomic_dec_and_test(&skb->users)))
629 return;
Neil Hormanead2ceb2009-03-11 09:49:55 +0000630 trace_kfree_skb(skb, __builtin_return_address(0));
Jörn Engel231d06a2006-03-20 21:28:35 -0800631 __kfree_skb(skb);
632}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800633EXPORT_SYMBOL(kfree_skb);
Jörn Engel231d06a2006-03-20 21:28:35 -0800634
Eric Dumazetbd8a7032013-06-24 06:26:00 -0700635void kfree_skb_list(struct sk_buff *segs)
636{
637 while (segs) {
638 struct sk_buff *next = segs->next;
639
640 kfree_skb(segs);
641 segs = next;
642 }
643}
644EXPORT_SYMBOL(kfree_skb_list);
645
Stephen Hemmingerd1a203e2008-11-01 21:01:09 -0700646/**
Michael S. Tsirkin25121172012-11-01 09:16:28 +0000647 * skb_tx_error - report an sk_buff xmit error
648 * @skb: buffer that triggered an error
649 *
650 * Report xmit error if a device callback is tracking this skb.
651 * skb must be freed afterwards.
652 */
653void skb_tx_error(struct sk_buff *skb)
654{
655 if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) {
656 struct ubuf_info *uarg;
657
658 uarg = skb_shinfo(skb)->destructor_arg;
659 if (uarg->callback)
660 uarg->callback(uarg, false);
661 skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
662 }
663}
664EXPORT_SYMBOL(skb_tx_error);
665
666/**
Neil Hormanead2ceb2009-03-11 09:49:55 +0000667 * consume_skb - free an skbuff
668 * @skb: buffer to free
669 *
670 * Drop a ref to the buffer and free it if the usage count has hit zero
671 * Functions identically to kfree_skb, but kfree_skb assumes that the frame
672 * is being dropped after a failure and notes that
673 */
674void consume_skb(struct sk_buff *skb)
675{
676 if (unlikely(!skb))
677 return;
678 if (likely(atomic_read(&skb->users) == 1))
679 smp_rmb();
680 else if (likely(!atomic_dec_and_test(&skb->users)))
681 return;
Koki Sanagi07dc22e2010-08-23 18:46:12 +0900682 trace_consume_skb(skb);
Neil Hormanead2ceb2009-03-11 09:49:55 +0000683 __kfree_skb(skb);
684}
685EXPORT_SYMBOL(consume_skb);
686
Eric Dumazetb1937222014-09-28 22:18:47 -0700687/* Make sure a field is enclosed inside headers_start/headers_end section */
688#define CHECK_SKB_FIELD(field) \
689 BUILD_BUG_ON(offsetof(struct sk_buff, field) < \
690 offsetof(struct sk_buff, headers_start)); \
691 BUILD_BUG_ON(offsetof(struct sk_buff, field) > \
692 offsetof(struct sk_buff, headers_end)); \
693
Herbert Xudec18812007-10-14 00:37:30 -0700694static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
695{
696 new->tstamp = old->tstamp;
Eric Dumazetb1937222014-09-28 22:18:47 -0700697 /* We do not copy old->sk */
Herbert Xudec18812007-10-14 00:37:30 -0700698 new->dev = old->dev;
Eric Dumazetb1937222014-09-28 22:18:47 -0700699 memcpy(new->cb, old->cb, sizeof(old->cb));
Eric Dumazet7fee2262010-05-11 23:19:48 +0000700 skb_dst_copy(new, old);
Alexey Dobriyandef8b4f2008-10-28 13:24:06 -0700701#ifdef CONFIG_XFRM
Herbert Xudec18812007-10-14 00:37:30 -0700702 new->sp = secpath_get(old->sp);
703#endif
Eric Dumazetb1937222014-09-28 22:18:47 -0700704 __nf_copy(new, old, false);
Patrick McHardy6aa895b02008-07-14 22:49:06 -0700705
Eric Dumazetb1937222014-09-28 22:18:47 -0700706 /* Note : this field could be in headers_start/headers_end section
707 * It is not yet because we do not want to have a 16 bit hole
708 */
709 new->queue_mapping = old->queue_mapping;
Eliezer Tamir06021292013-06-10 11:39:50 +0300710
Eric Dumazetb1937222014-09-28 22:18:47 -0700711 memcpy(&new->headers_start, &old->headers_start,
712 offsetof(struct sk_buff, headers_end) -
713 offsetof(struct sk_buff, headers_start));
714 CHECK_SKB_FIELD(protocol);
715 CHECK_SKB_FIELD(csum);
716 CHECK_SKB_FIELD(hash);
717 CHECK_SKB_FIELD(priority);
718 CHECK_SKB_FIELD(skb_iif);
719 CHECK_SKB_FIELD(vlan_proto);
720 CHECK_SKB_FIELD(vlan_tci);
721 CHECK_SKB_FIELD(transport_header);
722 CHECK_SKB_FIELD(network_header);
723 CHECK_SKB_FIELD(mac_header);
724 CHECK_SKB_FIELD(inner_protocol);
725 CHECK_SKB_FIELD(inner_transport_header);
726 CHECK_SKB_FIELD(inner_network_header);
727 CHECK_SKB_FIELD(inner_mac_header);
728 CHECK_SKB_FIELD(mark);
729#ifdef CONFIG_NETWORK_SECMARK
730 CHECK_SKB_FIELD(secmark);
731#endif
Cong Wange0d10952013-08-01 11:10:25 +0800732#ifdef CONFIG_NET_RX_BUSY_POLL
Eric Dumazetb1937222014-09-28 22:18:47 -0700733 CHECK_SKB_FIELD(napi_id);
Eliezer Tamir06021292013-06-10 11:39:50 +0300734#endif
Eric Dumazetb1937222014-09-28 22:18:47 -0700735#ifdef CONFIG_NET_SCHED
736 CHECK_SKB_FIELD(tc_index);
737#ifdef CONFIG_NET_CLS_ACT
738 CHECK_SKB_FIELD(tc_verd);
739#endif
740#endif
741
Herbert Xudec18812007-10-14 00:37:30 -0700742}
743
Herbert Xu82c49a32009-05-22 22:11:37 +0000744/*
745 * You should not add any new code to this function. Add it to
746 * __copy_skb_header above instead.
747 */
Herbert Xue0053ec2007-10-14 00:37:52 -0700748static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750#define C(x) n->x = skb->x
751
752 n->next = n->prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 n->sk = NULL;
Herbert Xudec18812007-10-14 00:37:30 -0700754 __copy_skb_header(n, skb);
755
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 C(len);
757 C(data_len);
Alexey Dobriyan3e6b3b22007-03-16 15:00:46 -0700758 C(mac_len);
Patrick McHardy334a8132007-06-25 04:35:20 -0700759 n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
Paul Moore02f1c892008-01-07 21:56:41 -0800760 n->cloned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 n->nohdr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 n->destructor = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 C(tail);
764 C(end);
Paul Moore02f1c892008-01-07 21:56:41 -0800765 C(head);
Eric Dumazetd3836f22012-04-27 00:33:38 +0000766 C(head_frag);
Paul Moore02f1c892008-01-07 21:56:41 -0800767 C(data);
768 C(truesize);
769 atomic_set(&n->users, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
771 atomic_inc(&(skb_shinfo(skb)->dataref));
772 skb->cloned = 1;
773
774 return n;
Herbert Xue0053ec2007-10-14 00:37:52 -0700775#undef C
776}
777
778/**
779 * skb_morph - morph one skb into another
780 * @dst: the skb to receive the contents
781 * @src: the skb to supply the contents
782 *
783 * This is identical to skb_clone except that the target skb is
784 * supplied by the user.
785 *
786 * The target skb is returned upon exit.
787 */
788struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
789{
Herbert Xu2d4baff2007-11-26 23:11:19 +0800790 skb_release_all(dst);
Herbert Xue0053ec2007-10-14 00:37:52 -0700791 return __skb_clone(dst, src);
792}
793EXPORT_SYMBOL_GPL(skb_morph);
794
Ben Hutchings2c530402012-07-10 10:55:09 +0000795/**
796 * skb_copy_ubufs - copy userspace skb frags buffers to kernel
Michael S. Tsirkin48c83012011-08-31 08:03:29 +0000797 * @skb: the skb to modify
798 * @gfp_mask: allocation priority
799 *
800 * This must be called on SKBTX_DEV_ZEROCOPY skb.
801 * It will copy all frags into kernel and drop the reference
802 * to userspace pages.
803 *
804 * If this function is called from an interrupt gfp_mask() must be
805 * %GFP_ATOMIC.
806 *
807 * Returns 0 on success or a negative error code on failure
808 * to allocate kernel memory to copy to.
809 */
810int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask)
Shirley Maa6686f22011-07-06 12:22:12 +0000811{
812 int i;
813 int num_frags = skb_shinfo(skb)->nr_frags;
814 struct page *page, *head = NULL;
815 struct ubuf_info *uarg = skb_shinfo(skb)->destructor_arg;
816
817 for (i = 0; i < num_frags; i++) {
818 u8 *vaddr;
819 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
820
Krishna Kumar02756ed2012-07-17 02:05:29 +0000821 page = alloc_page(gfp_mask);
Shirley Maa6686f22011-07-06 12:22:12 +0000822 if (!page) {
823 while (head) {
Sunghan Suh40dadff2013-07-12 16:17:23 +0900824 struct page *next = (struct page *)page_private(head);
Shirley Maa6686f22011-07-06 12:22:12 +0000825 put_page(head);
826 head = next;
827 }
828 return -ENOMEM;
829 }
Eric Dumazet51c56b02012-04-05 11:35:15 +0200830 vaddr = kmap_atomic(skb_frag_page(f));
Shirley Maa6686f22011-07-06 12:22:12 +0000831 memcpy(page_address(page),
Eric Dumazet9e903e02011-10-18 21:00:24 +0000832 vaddr + f->page_offset, skb_frag_size(f));
Eric Dumazet51c56b02012-04-05 11:35:15 +0200833 kunmap_atomic(vaddr);
Sunghan Suh40dadff2013-07-12 16:17:23 +0900834 set_page_private(page, (unsigned long)head);
Shirley Maa6686f22011-07-06 12:22:12 +0000835 head = page;
836 }
837
838 /* skb frags release userspace buffers */
Krishna Kumar02756ed2012-07-17 02:05:29 +0000839 for (i = 0; i < num_frags; i++)
Ian Campbella8605c62011-10-19 23:01:49 +0000840 skb_frag_unref(skb, i);
Shirley Maa6686f22011-07-06 12:22:12 +0000841
Michael S. Tsirkine19d6762012-11-01 09:16:22 +0000842 uarg->callback(uarg, false);
Shirley Maa6686f22011-07-06 12:22:12 +0000843
844 /* skb frags point to kernel buffers */
Krishna Kumar02756ed2012-07-17 02:05:29 +0000845 for (i = num_frags - 1; i >= 0; i--) {
846 __skb_fill_page_desc(skb, i, head, 0,
847 skb_shinfo(skb)->frags[i].size);
Sunghan Suh40dadff2013-07-12 16:17:23 +0900848 head = (struct page *)page_private(head);
Shirley Maa6686f22011-07-06 12:22:12 +0000849 }
Michael S. Tsirkin48c83012011-08-31 08:03:29 +0000850
851 skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
Shirley Maa6686f22011-07-06 12:22:12 +0000852 return 0;
853}
Michael S. Tsirkindcc0fb72012-07-20 09:23:20 +0000854EXPORT_SYMBOL_GPL(skb_copy_ubufs);
Shirley Maa6686f22011-07-06 12:22:12 +0000855
Herbert Xue0053ec2007-10-14 00:37:52 -0700856/**
857 * skb_clone - duplicate an sk_buff
858 * @skb: buffer to clone
859 * @gfp_mask: allocation priority
860 *
861 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
862 * copies share the same packet data but not structure. The new
863 * buffer has a reference count of 1. If the allocation fails the
864 * function returns %NULL otherwise the new buffer is returned.
865 *
866 * If this function is called from an interrupt gfp_mask() must be
867 * %GFP_ATOMIC.
868 */
869
870struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
871{
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700872 struct sk_buff_fclones *fclones = container_of(skb,
873 struct sk_buff_fclones,
874 skb1);
875 struct sk_buff *n = &fclones->skb2;
Herbert Xue0053ec2007-10-14 00:37:52 -0700876
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +0000877 if (skb_orphan_frags(skb, gfp_mask))
878 return NULL;
Shirley Maa6686f22011-07-06 12:22:12 +0000879
Herbert Xue0053ec2007-10-14 00:37:52 -0700880 if (skb->fclone == SKB_FCLONE_ORIG &&
Vijay Subramanianc8753d52014-10-02 10:00:43 -0700881 n->fclone == SKB_FCLONE_FREE) {
Herbert Xue0053ec2007-10-14 00:37:52 -0700882 n->fclone = SKB_FCLONE_CLONE;
Eric Dumazete7820e32014-11-21 11:47:16 -0800883 atomic_inc(&fclones->fclone_ref);
Herbert Xue0053ec2007-10-14 00:37:52 -0700884 } else {
Mel Gormanc93bdd02012-07-31 16:44:19 -0700885 if (skb_pfmemalloc(skb))
886 gfp_mask |= __GFP_MEMALLOC;
887
Herbert Xue0053ec2007-10-14 00:37:52 -0700888 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
889 if (!n)
890 return NULL;
Vegard Nossumfe55f6d2008-08-30 12:16:35 +0200891
892 kmemcheck_annotate_bitfield(n, flags1);
Herbert Xue0053ec2007-10-14 00:37:52 -0700893 n->fclone = SKB_FCLONE_UNAVAILABLE;
894 }
895
896 return __skb_clone(n, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800898EXPORT_SYMBOL(skb_clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
Pravin B Shelarf5b17292013-03-07 13:21:40 +0000900static void skb_headers_offset_update(struct sk_buff *skb, int off)
901{
Eric Dumazet030737b2013-10-19 11:42:54 -0700902 /* Only adjust this if it actually is csum_start rather than csum */
903 if (skb->ip_summed == CHECKSUM_PARTIAL)
904 skb->csum_start += off;
Pravin B Shelarf5b17292013-03-07 13:21:40 +0000905 /* {transport,network,mac}_header and tail are relative to skb->head */
906 skb->transport_header += off;
907 skb->network_header += off;
908 if (skb_mac_header_was_set(skb))
909 skb->mac_header += off;
910 skb->inner_transport_header += off;
911 skb->inner_network_header += off;
Pravin B Shelaraefbd2b2013-03-07 13:21:46 +0000912 skb->inner_mac_header += off;
Pravin B Shelarf5b17292013-03-07 13:21:40 +0000913}
914
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
916{
Herbert Xudec18812007-10-14 00:37:30 -0700917 __copy_skb_header(new, old);
918
Herbert Xu79671682006-06-22 02:40:14 -0700919 skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
920 skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
921 skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922}
923
Mel Gormanc93bdd02012-07-31 16:44:19 -0700924static inline int skb_alloc_rx_flag(const struct sk_buff *skb)
925{
926 if (skb_pfmemalloc(skb))
927 return SKB_ALLOC_RX;
928 return 0;
929}
930
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931/**
932 * skb_copy - create private copy of an sk_buff
933 * @skb: buffer to copy
934 * @gfp_mask: allocation priority
935 *
936 * Make a copy of both an &sk_buff and its data. This is used when the
937 * caller wishes to modify the data and needs a private copy of the
938 * data to alter. Returns %NULL on failure or the pointer to the buffer
939 * on success. The returned buffer has a reference count of 1.
940 *
941 * As by-product this function converts non-linear &sk_buff to linear
942 * one, so that &sk_buff becomes completely private and caller is allowed
943 * to modify all the data of returned buffer. This means that this
944 * function is not recommended for use in circumstances when only
945 * header is going to be modified. Use pskb_copy() instead.
946 */
947
Al Virodd0fc662005-10-07 07:46:04 +0100948struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949{
Eric Dumazet6602ceb2010-09-01 05:25:10 +0000950 int headerlen = skb_headroom(skb);
Alexander Duyckec47ea82012-05-04 14:26:56 +0000951 unsigned int size = skb_end_offset(skb) + skb->data_len;
Mel Gormanc93bdd02012-07-31 16:44:19 -0700952 struct sk_buff *n = __alloc_skb(size, gfp_mask,
953 skb_alloc_rx_flag(skb), NUMA_NO_NODE);
Eric Dumazet6602ceb2010-09-01 05:25:10 +0000954
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 if (!n)
956 return NULL;
957
958 /* Set the data pointer */
959 skb_reserve(n, headerlen);
960 /* Set the tail pointer and length */
961 skb_put(n, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
963 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
964 BUG();
965
966 copy_skb_header(n, skb);
967 return n;
968}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800969EXPORT_SYMBOL(skb_copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
971/**
Octavian Purdilabad93e92014-06-12 01:36:26 +0300972 * __pskb_copy_fclone - create copy of an sk_buff with private head.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 * @skb: buffer to copy
Eric Dumazet117632e2011-12-03 21:39:53 +0000974 * @headroom: headroom of new skb
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 * @gfp_mask: allocation priority
Octavian Purdilabad93e92014-06-12 01:36:26 +0300976 * @fclone: if true allocate the copy of the skb from the fclone
977 * cache instead of the head cache; it is recommended to set this
978 * to true for the cases where the copy will likely be cloned
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 *
980 * Make a copy of both an &sk_buff and part of its data, located
981 * in header. Fragmented data remain shared. This is used when
982 * the caller wishes to modify only header of &sk_buff and needs
983 * private copy of the header to alter. Returns %NULL on failure
984 * or the pointer to the buffer on success.
985 * The returned buffer has a reference count of 1.
986 */
987
Octavian Purdilabad93e92014-06-12 01:36:26 +0300988struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom,
989 gfp_t gfp_mask, bool fclone)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990{
Eric Dumazet117632e2011-12-03 21:39:53 +0000991 unsigned int size = skb_headlen(skb) + headroom;
Octavian Purdilabad93e92014-06-12 01:36:26 +0300992 int flags = skb_alloc_rx_flag(skb) | (fclone ? SKB_ALLOC_FCLONE : 0);
993 struct sk_buff *n = __alloc_skb(size, gfp_mask, flags, NUMA_NO_NODE);
Eric Dumazet6602ceb2010-09-01 05:25:10 +0000994
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 if (!n)
996 goto out;
997
998 /* Set the data pointer */
Eric Dumazet117632e2011-12-03 21:39:53 +0000999 skb_reserve(n, headroom);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 /* Set the tail pointer and length */
1001 skb_put(n, skb_headlen(skb));
1002 /* Copy the bytes */
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001003 skb_copy_from_linear_data(skb, n->data, n->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
Herbert Xu25f484a2006-11-07 14:57:15 -08001005 n->truesize += skb->data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 n->data_len = skb->data_len;
1007 n->len = skb->len;
1008
1009 if (skb_shinfo(skb)->nr_frags) {
1010 int i;
1011
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +00001012 if (skb_orphan_frags(skb, gfp_mask)) {
1013 kfree_skb(n);
1014 n = NULL;
1015 goto out;
Shirley Maa6686f22011-07-06 12:22:12 +00001016 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1018 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
Ian Campbellea2ab692011-08-22 23:44:58 +00001019 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 }
1021 skb_shinfo(n)->nr_frags = i;
1022 }
1023
David S. Miller21dc3302010-08-23 00:13:46 -07001024 if (skb_has_frag_list(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
1026 skb_clone_fraglist(n);
1027 }
1028
1029 copy_skb_header(n, skb);
1030out:
1031 return n;
1032}
Octavian Purdilabad93e92014-06-12 01:36:26 +03001033EXPORT_SYMBOL(__pskb_copy_fclone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
1035/**
1036 * pskb_expand_head - reallocate header of &sk_buff
1037 * @skb: buffer to reallocate
1038 * @nhead: room to add at head
1039 * @ntail: room to add at tail
1040 * @gfp_mask: allocation priority
1041 *
Mathias Krausebc323832013-11-07 14:18:26 +01001042 * Expands (or creates identical copy, if @nhead and @ntail are zero)
1043 * header of @skb. &sk_buff itself is not changed. &sk_buff MUST have
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 * reference count of 1. Returns zero in the case of success or error,
1045 * if expansion failed. In the last case, &sk_buff is not changed.
1046 *
1047 * All the pointers pointing into skb header may change and must be
1048 * reloaded after call to this function.
1049 */
1050
Victor Fusco86a76ca2005-07-08 14:57:47 -07001051int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
Al Virodd0fc662005-10-07 07:46:04 +01001052 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053{
1054 int i;
1055 u8 *data;
Alexander Duyckec47ea82012-05-04 14:26:56 +00001056 int size = nhead + skb_end_offset(skb) + ntail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 long off;
1058
Herbert Xu4edd87a2008-10-01 07:09:38 -07001059 BUG_ON(nhead < 0);
1060
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 if (skb_shared(skb))
1062 BUG();
1063
1064 size = SKB_DATA_ALIGN(size);
1065
Mel Gormanc93bdd02012-07-31 16:44:19 -07001066 if (skb_pfmemalloc(skb))
1067 gfp_mask |= __GFP_MEMALLOC;
1068 data = kmalloc_reserve(size + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
1069 gfp_mask, NUMA_NO_NODE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 if (!data)
1071 goto nodata;
Eric Dumazet87151b82012-04-10 20:08:39 +00001072 size = SKB_WITH_OVERHEAD(ksize(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
1074 /* Copy only real data... and, alas, header. This should be
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001075 * optimized for the cases when header is void.
1076 */
1077 memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head);
1078
1079 memcpy((struct skb_shared_info *)(data + size),
1080 skb_shinfo(skb),
Eric Dumazetfed66382010-07-22 19:09:08 +00001081 offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
Alexander Duyck3e245912012-05-04 14:26:51 +00001083 /*
1084 * if shinfo is shared we must drop the old head gracefully, but if it
1085 * is not we can just drop the old head and let the existing refcount
1086 * be since all we did is relocate the values
1087 */
1088 if (skb_cloned(skb)) {
Shirley Maa6686f22011-07-06 12:22:12 +00001089 /* copy this zero copy skb frags */
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +00001090 if (skb_orphan_frags(skb, gfp_mask))
1091 goto nofrags;
Eric Dumazet1fd63042010-09-02 23:09:32 +00001092 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
Ian Campbellea2ab692011-08-22 23:44:58 +00001093 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
Eric Dumazet1fd63042010-09-02 23:09:32 +00001095 if (skb_has_frag_list(skb))
1096 skb_clone_fraglist(skb);
1097
1098 skb_release_data(skb);
Alexander Duyck3e245912012-05-04 14:26:51 +00001099 } else {
1100 skb_free_head(skb);
Eric Dumazet1fd63042010-09-02 23:09:32 +00001101 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 off = (data + nhead) - skb->head;
1103
1104 skb->head = data;
Eric Dumazetd3836f22012-04-27 00:33:38 +00001105 skb->head_frag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 skb->data += off;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001107#ifdef NET_SKBUFF_DATA_USES_OFFSET
1108 skb->end = size;
Patrick McHardy56eb8882007-04-09 11:45:04 -07001109 off = nhead;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001110#else
1111 skb->end = skb->head + size;
Patrick McHardy56eb8882007-04-09 11:45:04 -07001112#endif
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001113 skb->tail += off;
Peter Pan(潘卫平)b41abb42013-06-06 21:27:21 +08001114 skb_headers_offset_update(skb, nhead);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 skb->cloned = 0;
Patrick McHardy334a8132007-06-25 04:35:20 -07001116 skb->hdr_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 skb->nohdr = 0;
1118 atomic_set(&skb_shinfo(skb)->dataref, 1);
1119 return 0;
1120
Shirley Maa6686f22011-07-06 12:22:12 +00001121nofrags:
1122 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123nodata:
1124 return -ENOMEM;
1125}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001126EXPORT_SYMBOL(pskb_expand_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
1128/* Make private copy of skb with writable head and some headroom */
1129
1130struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
1131{
1132 struct sk_buff *skb2;
1133 int delta = headroom - skb_headroom(skb);
1134
1135 if (delta <= 0)
1136 skb2 = pskb_copy(skb, GFP_ATOMIC);
1137 else {
1138 skb2 = skb_clone(skb, GFP_ATOMIC);
1139 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
1140 GFP_ATOMIC)) {
1141 kfree_skb(skb2);
1142 skb2 = NULL;
1143 }
1144 }
1145 return skb2;
1146}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001147EXPORT_SYMBOL(skb_realloc_headroom);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
1149/**
1150 * skb_copy_expand - copy and expand sk_buff
1151 * @skb: buffer to copy
1152 * @newheadroom: new free bytes at head
1153 * @newtailroom: new free bytes at tail
1154 * @gfp_mask: allocation priority
1155 *
1156 * Make a copy of both an &sk_buff and its data and while doing so
1157 * allocate additional space.
1158 *
1159 * This is used when the caller wishes to modify the data and needs a
1160 * private copy of the data to alter as well as more space for new fields.
1161 * Returns %NULL on failure or the pointer to the buffer
1162 * on success. The returned buffer has a reference count of 1.
1163 *
1164 * You must pass %GFP_ATOMIC as the allocation priority if this function
1165 * is called from an interrupt.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 */
1167struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
Victor Fusco86a76ca2005-07-08 14:57:47 -07001168 int newheadroom, int newtailroom,
Al Virodd0fc662005-10-07 07:46:04 +01001169 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170{
1171 /*
1172 * Allocate the copy buffer
1173 */
Mel Gormanc93bdd02012-07-31 16:44:19 -07001174 struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom,
1175 gfp_mask, skb_alloc_rx_flag(skb),
1176 NUMA_NO_NODE);
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001177 int oldheadroom = skb_headroom(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 int head_copy_len, head_copy_off;
1179
1180 if (!n)
1181 return NULL;
1182
1183 skb_reserve(n, newheadroom);
1184
1185 /* Set the tail pointer and length */
1186 skb_put(n, skb->len);
1187
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001188 head_copy_len = oldheadroom;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 head_copy_off = 0;
1190 if (newheadroom <= head_copy_len)
1191 head_copy_len = newheadroom;
1192 else
1193 head_copy_off = newheadroom - head_copy_len;
1194
1195 /* Copy the linear header and data. */
1196 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
1197 skb->len + head_copy_len))
1198 BUG();
1199
1200 copy_skb_header(n, skb);
1201
Eric Dumazet030737b2013-10-19 11:42:54 -07001202 skb_headers_offset_update(n, newheadroom - oldheadroom);
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001203
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 return n;
1205}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001206EXPORT_SYMBOL(skb_copy_expand);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
1208/**
1209 * skb_pad - zero pad the tail of an skb
1210 * @skb: buffer to pad
1211 * @pad: space to pad
1212 *
1213 * Ensure that a buffer is followed by a padding area that is zero
1214 * filled. Used by network drivers which may DMA or transfer data
1215 * beyond the buffer end onto the wire.
1216 *
Herbert Xu5b057c62006-06-23 02:06:41 -07001217 * May return error in out of memory cases. The skb is freed on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001219
Herbert Xu5b057c62006-06-23 02:06:41 -07001220int skb_pad(struct sk_buff *skb, int pad)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221{
Herbert Xu5b057c62006-06-23 02:06:41 -07001222 int err;
1223 int ntail;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001224
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 /* If the skbuff is non linear tailroom is always zero.. */
Herbert Xu5b057c62006-06-23 02:06:41 -07001226 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 memset(skb->data+skb->len, 0, pad);
Herbert Xu5b057c62006-06-23 02:06:41 -07001228 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 }
Herbert Xu5b057c62006-06-23 02:06:41 -07001230
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001231 ntail = skb->data_len + pad - (skb->end - skb->tail);
Herbert Xu5b057c62006-06-23 02:06:41 -07001232 if (likely(skb_cloned(skb) || ntail > 0)) {
1233 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
1234 if (unlikely(err))
1235 goto free_skb;
1236 }
1237
1238 /* FIXME: The use of this function with non-linear skb's really needs
1239 * to be audited.
1240 */
1241 err = skb_linearize(skb);
1242 if (unlikely(err))
1243 goto free_skb;
1244
1245 memset(skb->data + skb->len, 0, pad);
1246 return 0;
1247
1248free_skb:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 kfree_skb(skb);
Herbert Xu5b057c62006-06-23 02:06:41 -07001250 return err;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001251}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001252EXPORT_SYMBOL(skb_pad);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001253
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -07001254/**
Mathias Krause0c7ddf32013-11-07 14:18:24 +01001255 * pskb_put - add data to the tail of a potentially fragmented buffer
1256 * @skb: start of the buffer to use
1257 * @tail: tail fragment of the buffer to use
1258 * @len: amount of data to add
1259 *
1260 * This function extends the used data area of the potentially
1261 * fragmented buffer. @tail must be the last fragment of @skb -- or
1262 * @skb itself. If this would exceed the total buffer size the kernel
1263 * will panic. A pointer to the first byte of the extra data is
1264 * returned.
1265 */
1266
1267unsigned char *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len)
1268{
1269 if (tail != skb) {
1270 skb->data_len += len;
1271 skb->len += len;
1272 }
1273 return skb_put(tail, len);
1274}
1275EXPORT_SYMBOL_GPL(pskb_put);
1276
1277/**
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -07001278 * skb_put - add data to a buffer
1279 * @skb: buffer to use
1280 * @len: amount of data to add
1281 *
1282 * This function extends the used data area of the buffer. If this would
1283 * exceed the total buffer size the kernel will panic. A pointer to the
1284 * first byte of the extra data is returned.
1285 */
1286unsigned char *skb_put(struct sk_buff *skb, unsigned int len)
1287{
1288 unsigned char *tmp = skb_tail_pointer(skb);
1289 SKB_LINEAR_ASSERT(skb);
1290 skb->tail += len;
1291 skb->len += len;
1292 if (unlikely(skb->tail > skb->end))
1293 skb_over_panic(skb, len, __builtin_return_address(0));
1294 return tmp;
1295}
1296EXPORT_SYMBOL(skb_put);
1297
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001298/**
Ilpo Järvinenc2aa2702008-03-27 17:52:40 -07001299 * skb_push - add data to the start of a buffer
1300 * @skb: buffer to use
1301 * @len: amount of data to add
1302 *
1303 * This function extends the used data area of the buffer at the buffer
1304 * start. If this would exceed the total buffer headroom the kernel will
1305 * panic. A pointer to the first byte of the extra data is returned.
1306 */
1307unsigned char *skb_push(struct sk_buff *skb, unsigned int len)
1308{
1309 skb->data -= len;
1310 skb->len += len;
1311 if (unlikely(skb->data<skb->head))
1312 skb_under_panic(skb, len, __builtin_return_address(0));
1313 return skb->data;
1314}
1315EXPORT_SYMBOL(skb_push);
1316
1317/**
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001318 * skb_pull - remove data from the start of a buffer
1319 * @skb: buffer to use
1320 * @len: amount of data to remove
1321 *
1322 * This function removes data from the start of a buffer, returning
1323 * the memory to the headroom. A pointer to the next data in the buffer
1324 * is returned. Once the data has been pulled future pushes will overwrite
1325 * the old data.
1326 */
1327unsigned char *skb_pull(struct sk_buff *skb, unsigned int len)
1328{
David S. Miller47d29642010-05-02 02:21:44 -07001329 return skb_pull_inline(skb, len);
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001330}
1331EXPORT_SYMBOL(skb_pull);
1332
Ilpo Järvinen419ae742008-03-27 17:54:01 -07001333/**
1334 * skb_trim - remove end from a buffer
1335 * @skb: buffer to alter
1336 * @len: new length
1337 *
1338 * Cut the length of a buffer down by removing data from the tail. If
1339 * the buffer is already under the length specified it is not modified.
1340 * The skb must be linear.
1341 */
1342void skb_trim(struct sk_buff *skb, unsigned int len)
1343{
1344 if (skb->len > len)
1345 __skb_trim(skb, len);
1346}
1347EXPORT_SYMBOL(skb_trim);
1348
Herbert Xu3cc0e872006-06-09 16:13:38 -07001349/* Trims skb to length len. It can change skb pointers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 */
1351
Herbert Xu3cc0e872006-06-09 16:13:38 -07001352int ___pskb_trim(struct sk_buff *skb, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353{
Herbert Xu27b437c2006-07-13 19:26:39 -07001354 struct sk_buff **fragp;
1355 struct sk_buff *frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 int offset = skb_headlen(skb);
1357 int nfrags = skb_shinfo(skb)->nr_frags;
1358 int i;
Herbert Xu27b437c2006-07-13 19:26:39 -07001359 int err;
1360
1361 if (skb_cloned(skb) &&
1362 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
1363 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001365 i = 0;
1366 if (offset >= len)
1367 goto drop_pages;
1368
1369 for (; i < nfrags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001370 int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]);
Herbert Xu27b437c2006-07-13 19:26:39 -07001371
1372 if (end < len) {
1373 offset = end;
1374 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 }
Herbert Xu27b437c2006-07-13 19:26:39 -07001376
Eric Dumazet9e903e02011-10-18 21:00:24 +00001377 skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset);
Herbert Xu27b437c2006-07-13 19:26:39 -07001378
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001379drop_pages:
Herbert Xu27b437c2006-07-13 19:26:39 -07001380 skb_shinfo(skb)->nr_frags = i;
1381
1382 for (; i < nfrags; i++)
Ian Campbellea2ab692011-08-22 23:44:58 +00001383 skb_frag_unref(skb, i);
Herbert Xu27b437c2006-07-13 19:26:39 -07001384
David S. Miller21dc3302010-08-23 00:13:46 -07001385 if (skb_has_frag_list(skb))
Herbert Xu27b437c2006-07-13 19:26:39 -07001386 skb_drop_fraglist(skb);
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001387 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 }
1389
Herbert Xu27b437c2006-07-13 19:26:39 -07001390 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
1391 fragp = &frag->next) {
1392 int end = offset + frag->len;
1393
1394 if (skb_shared(frag)) {
1395 struct sk_buff *nfrag;
1396
1397 nfrag = skb_clone(frag, GFP_ATOMIC);
1398 if (unlikely(!nfrag))
1399 return -ENOMEM;
1400
1401 nfrag->next = frag->next;
Eric Dumazet85bb2a62012-04-19 02:24:53 +00001402 consume_skb(frag);
Herbert Xu27b437c2006-07-13 19:26:39 -07001403 frag = nfrag;
1404 *fragp = frag;
1405 }
1406
1407 if (end < len) {
1408 offset = end;
1409 continue;
1410 }
1411
1412 if (end > len &&
1413 unlikely((err = pskb_trim(frag, len - offset))))
1414 return err;
1415
1416 if (frag->next)
1417 skb_drop_list(&frag->next);
1418 break;
1419 }
1420
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001421done:
Herbert Xu27b437c2006-07-13 19:26:39 -07001422 if (len > skb_headlen(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 skb->data_len -= skb->len - len;
1424 skb->len = len;
1425 } else {
Herbert Xu27b437c2006-07-13 19:26:39 -07001426 skb->len = len;
1427 skb->data_len = 0;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001428 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 }
1430
1431 return 0;
1432}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001433EXPORT_SYMBOL(___pskb_trim);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434
1435/**
1436 * __pskb_pull_tail - advance tail of skb header
1437 * @skb: buffer to reallocate
1438 * @delta: number of bytes to advance tail
1439 *
1440 * The function makes a sense only on a fragmented &sk_buff,
1441 * it expands header moving its tail forward and copying necessary
1442 * data from fragmented part.
1443 *
1444 * &sk_buff MUST have reference count of 1.
1445 *
1446 * Returns %NULL (and &sk_buff does not change) if pull failed
1447 * or value of new tail of skb in the case of success.
1448 *
1449 * All the pointers pointing into skb header may change and must be
1450 * reloaded after call to this function.
1451 */
1452
1453/* Moves tail of skb head forward, copying data from fragmented part,
1454 * when it is necessary.
1455 * 1. It may fail due to malloc failure.
1456 * 2. It may change skb pointers.
1457 *
1458 * It is pretty complicated. Luckily, it is called only in exceptional cases.
1459 */
1460unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
1461{
1462 /* If skb has not enough free space at tail, get new one
1463 * plus 128 bytes for future expansions. If we have enough
1464 * room at tail, reallocate without expansion only if skb is cloned.
1465 */
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001466 int i, k, eat = (skb->tail + delta) - skb->end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467
1468 if (eat > 0 || skb_cloned(skb)) {
1469 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
1470 GFP_ATOMIC))
1471 return NULL;
1472 }
1473
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001474 if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 BUG();
1476
1477 /* Optimization: no fragments, no reasons to preestimate
1478 * size of pulled pages. Superb.
1479 */
David S. Miller21dc3302010-08-23 00:13:46 -07001480 if (!skb_has_frag_list(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 goto pull_pages;
1482
1483 /* Estimate size of pulled pages. */
1484 eat = delta;
1485 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001486 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1487
1488 if (size >= eat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 goto pull_pages;
Eric Dumazet9e903e02011-10-18 21:00:24 +00001490 eat -= size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 }
1492
1493 /* If we need update frag list, we are in troubles.
1494 * Certainly, it possible to add an offset to skb data,
1495 * but taking into account that pulling is expected to
1496 * be very rare operation, it is worth to fight against
1497 * further bloating skb head and crucify ourselves here instead.
1498 * Pure masohism, indeed. 8)8)
1499 */
1500 if (eat) {
1501 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1502 struct sk_buff *clone = NULL;
1503 struct sk_buff *insp = NULL;
1504
1505 do {
Kris Katterjohn09a62662006-01-08 22:24:28 -08001506 BUG_ON(!list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
1508 if (list->len <= eat) {
1509 /* Eaten as whole. */
1510 eat -= list->len;
1511 list = list->next;
1512 insp = list;
1513 } else {
1514 /* Eaten partially. */
1515
1516 if (skb_shared(list)) {
1517 /* Sucks! We need to fork list. :-( */
1518 clone = skb_clone(list, GFP_ATOMIC);
1519 if (!clone)
1520 return NULL;
1521 insp = list->next;
1522 list = clone;
1523 } else {
1524 /* This may be pulled without
1525 * problems. */
1526 insp = list;
1527 }
1528 if (!pskb_pull(list, eat)) {
Wei Yongjunf3fbbe02009-02-25 00:37:32 +00001529 kfree_skb(clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 return NULL;
1531 }
1532 break;
1533 }
1534 } while (eat);
1535
1536 /* Free pulled out fragments. */
1537 while ((list = skb_shinfo(skb)->frag_list) != insp) {
1538 skb_shinfo(skb)->frag_list = list->next;
1539 kfree_skb(list);
1540 }
1541 /* And insert new clone at head. */
1542 if (clone) {
1543 clone->next = list;
1544 skb_shinfo(skb)->frag_list = clone;
1545 }
1546 }
1547 /* Success! Now we may commit changes to skb data. */
1548
1549pull_pages:
1550 eat = delta;
1551 k = 0;
1552 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001553 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1554
1555 if (size <= eat) {
Ian Campbellea2ab692011-08-22 23:44:58 +00001556 skb_frag_unref(skb, i);
Eric Dumazet9e903e02011-10-18 21:00:24 +00001557 eat -= size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 } else {
1559 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
1560 if (eat) {
1561 skb_shinfo(skb)->frags[k].page_offset += eat;
Eric Dumazet9e903e02011-10-18 21:00:24 +00001562 skb_frag_size_sub(&skb_shinfo(skb)->frags[k], eat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 eat = 0;
1564 }
1565 k++;
1566 }
1567 }
1568 skb_shinfo(skb)->nr_frags = k;
1569
1570 skb->tail += delta;
1571 skb->data_len -= delta;
1572
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001573 return skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001575EXPORT_SYMBOL(__pskb_pull_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576
Eric Dumazet22019b12011-07-29 18:37:31 +00001577/**
1578 * skb_copy_bits - copy bits from skb to kernel buffer
1579 * @skb: source skb
1580 * @offset: offset in source
1581 * @to: destination buffer
1582 * @len: number of bytes to copy
1583 *
1584 * Copy the specified number of bytes from the source skb to the
1585 * destination buffer.
1586 *
1587 * CAUTION ! :
1588 * If its prototype is ever changed,
1589 * check arch/{*}/net/{*}.S files,
1590 * since it is called from BPF assembly code.
1591 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
1593{
David S. Miller1a028e52007-04-27 15:21:23 -07001594 int start = skb_headlen(skb);
David S. Millerfbb398a2009-06-09 00:18:59 -07001595 struct sk_buff *frag_iter;
1596 int i, copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597
1598 if (offset > (int)skb->len - len)
1599 goto fault;
1600
1601 /* Copy header. */
David S. Miller1a028e52007-04-27 15:21:23 -07001602 if ((copy = start - offset) > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 if (copy > len)
1604 copy = len;
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001605 skb_copy_from_linear_data_offset(skb, offset, to, copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 if ((len -= copy) == 0)
1607 return 0;
1608 offset += copy;
1609 to += copy;
1610 }
1611
1612 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07001613 int end;
Eric Dumazet51c56b02012-04-05 11:35:15 +02001614 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001616 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07001617
Eric Dumazet51c56b02012-04-05 11:35:15 +02001618 end = start + skb_frag_size(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 if ((copy = end - offset) > 0) {
1620 u8 *vaddr;
1621
1622 if (copy > len)
1623 copy = len;
1624
Eric Dumazet51c56b02012-04-05 11:35:15 +02001625 vaddr = kmap_atomic(skb_frag_page(f));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 memcpy(to,
Eric Dumazet51c56b02012-04-05 11:35:15 +02001627 vaddr + f->page_offset + offset - start,
1628 copy);
1629 kunmap_atomic(vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630
1631 if ((len -= copy) == 0)
1632 return 0;
1633 offset += copy;
1634 to += copy;
1635 }
David S. Miller1a028e52007-04-27 15:21:23 -07001636 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 }
1638
David S. Millerfbb398a2009-06-09 00:18:59 -07001639 skb_walk_frags(skb, frag_iter) {
1640 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641
David S. Millerfbb398a2009-06-09 00:18:59 -07001642 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643
David S. Millerfbb398a2009-06-09 00:18:59 -07001644 end = start + frag_iter->len;
1645 if ((copy = end - offset) > 0) {
1646 if (copy > len)
1647 copy = len;
1648 if (skb_copy_bits(frag_iter, offset - start, to, copy))
1649 goto fault;
1650 if ((len -= copy) == 0)
1651 return 0;
1652 offset += copy;
1653 to += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 }
David S. Millerfbb398a2009-06-09 00:18:59 -07001655 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 }
Shirley Maa6686f22011-07-06 12:22:12 +00001657
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 if (!len)
1659 return 0;
1660
1661fault:
1662 return -EFAULT;
1663}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001664EXPORT_SYMBOL(skb_copy_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665
Jens Axboe9c55e012007-11-06 23:30:13 -08001666/*
1667 * Callback from splice_to_pipe(), if we need to release some pages
1668 * at the end of the spd in case we error'ed out in filling the pipe.
1669 */
1670static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
1671{
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001672 put_page(spd->pages[i]);
1673}
Jens Axboe9c55e012007-11-06 23:30:13 -08001674
David S. Millera108d5f2012-04-23 23:06:11 -04001675static struct page *linear_to_page(struct page *page, unsigned int *len,
1676 unsigned int *offset,
Eric Dumazet18aafc62013-01-11 14:46:37 +00001677 struct sock *sk)
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001678{
Eric Dumazet5640f762012-09-23 23:04:42 +00001679 struct page_frag *pfrag = sk_page_frag(sk);
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001680
Eric Dumazet5640f762012-09-23 23:04:42 +00001681 if (!sk_page_frag_refill(sk, pfrag))
1682 return NULL;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001683
Eric Dumazet5640f762012-09-23 23:04:42 +00001684 *len = min_t(unsigned int, *len, pfrag->size - pfrag->offset);
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001685
Eric Dumazet5640f762012-09-23 23:04:42 +00001686 memcpy(page_address(pfrag->page) + pfrag->offset,
1687 page_address(page) + *offset, *len);
1688 *offset = pfrag->offset;
1689 pfrag->offset += *len;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001690
Eric Dumazet5640f762012-09-23 23:04:42 +00001691 return pfrag->page;
Jens Axboe9c55e012007-11-06 23:30:13 -08001692}
1693
Eric Dumazet41c73a02012-04-22 12:26:16 +00001694static bool spd_can_coalesce(const struct splice_pipe_desc *spd,
1695 struct page *page,
1696 unsigned int offset)
1697{
1698 return spd->nr_pages &&
1699 spd->pages[spd->nr_pages - 1] == page &&
1700 (spd->partial[spd->nr_pages - 1].offset +
1701 spd->partial[spd->nr_pages - 1].len == offset);
1702}
1703
Jens Axboe9c55e012007-11-06 23:30:13 -08001704/*
1705 * Fill page/offset/length into spd, if it can hold more pages.
1706 */
David S. Millera108d5f2012-04-23 23:06:11 -04001707static bool spd_fill_page(struct splice_pipe_desc *spd,
1708 struct pipe_inode_info *pipe, struct page *page,
1709 unsigned int *len, unsigned int offset,
Eric Dumazet18aafc62013-01-11 14:46:37 +00001710 bool linear,
David S. Millera108d5f2012-04-23 23:06:11 -04001711 struct sock *sk)
Jens Axboe9c55e012007-11-06 23:30:13 -08001712{
Eric Dumazet41c73a02012-04-22 12:26:16 +00001713 if (unlikely(spd->nr_pages == MAX_SKB_FRAGS))
David S. Millera108d5f2012-04-23 23:06:11 -04001714 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08001715
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001716 if (linear) {
Eric Dumazet18aafc62013-01-11 14:46:37 +00001717 page = linear_to_page(page, len, &offset, sk);
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001718 if (!page)
David S. Millera108d5f2012-04-23 23:06:11 -04001719 return true;
Eric Dumazet41c73a02012-04-22 12:26:16 +00001720 }
1721 if (spd_can_coalesce(spd, page, offset)) {
1722 spd->partial[spd->nr_pages - 1].len += *len;
David S. Millera108d5f2012-04-23 23:06:11 -04001723 return false;
Eric Dumazet41c73a02012-04-22 12:26:16 +00001724 }
1725 get_page(page);
Jens Axboe9c55e012007-11-06 23:30:13 -08001726 spd->pages[spd->nr_pages] = page;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001727 spd->partial[spd->nr_pages].len = *len;
Jens Axboe9c55e012007-11-06 23:30:13 -08001728 spd->partial[spd->nr_pages].offset = offset;
Jens Axboe9c55e012007-11-06 23:30:13 -08001729 spd->nr_pages++;
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001730
David S. Millera108d5f2012-04-23 23:06:11 -04001731 return false;
Jens Axboe9c55e012007-11-06 23:30:13 -08001732}
1733
David S. Millera108d5f2012-04-23 23:06:11 -04001734static bool __splice_segment(struct page *page, unsigned int poff,
1735 unsigned int plen, unsigned int *off,
Eric Dumazet18aafc62013-01-11 14:46:37 +00001736 unsigned int *len,
Eric Dumazetd7ccf7c2012-04-23 23:35:04 -04001737 struct splice_pipe_desc *spd, bool linear,
David S. Millera108d5f2012-04-23 23:06:11 -04001738 struct sock *sk,
1739 struct pipe_inode_info *pipe)
Octavian Purdila2870c432008-07-15 00:49:11 -07001740{
1741 if (!*len)
David S. Millera108d5f2012-04-23 23:06:11 -04001742 return true;
Octavian Purdila2870c432008-07-15 00:49:11 -07001743
1744 /* skip this segment if already processed */
1745 if (*off >= plen) {
1746 *off -= plen;
David S. Millera108d5f2012-04-23 23:06:11 -04001747 return false;
Octavian Purdiladb43a282008-06-27 17:27:21 -07001748 }
Jens Axboe9c55e012007-11-06 23:30:13 -08001749
Octavian Purdila2870c432008-07-15 00:49:11 -07001750 /* ignore any bits we already processed */
Eric Dumazet9ca1b222013-01-05 21:31:18 +00001751 poff += *off;
1752 plen -= *off;
1753 *off = 0;
Octavian Purdila2870c432008-07-15 00:49:11 -07001754
Eric Dumazet18aafc62013-01-11 14:46:37 +00001755 do {
1756 unsigned int flen = min(*len, plen);
Octavian Purdila2870c432008-07-15 00:49:11 -07001757
Eric Dumazet18aafc62013-01-11 14:46:37 +00001758 if (spd_fill_page(spd, pipe, page, &flen, poff,
1759 linear, sk))
1760 return true;
1761 poff += flen;
1762 plen -= flen;
1763 *len -= flen;
1764 } while (*len && plen);
Octavian Purdila2870c432008-07-15 00:49:11 -07001765
David S. Millera108d5f2012-04-23 23:06:11 -04001766 return false;
Octavian Purdila2870c432008-07-15 00:49:11 -07001767}
1768
1769/*
David S. Millera108d5f2012-04-23 23:06:11 -04001770 * Map linear and fragment data from the skb to spd. It reports true if the
Octavian Purdila2870c432008-07-15 00:49:11 -07001771 * pipe is full or if we already spliced the requested length.
1772 */
David S. Millera108d5f2012-04-23 23:06:11 -04001773static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe,
1774 unsigned int *offset, unsigned int *len,
1775 struct splice_pipe_desc *spd, struct sock *sk)
Octavian Purdila2870c432008-07-15 00:49:11 -07001776{
1777 int seg;
1778
Eric Dumazet1d0c0b32012-04-27 02:10:03 +00001779 /* map the linear part :
Alexander Duyck2996d312012-05-02 18:18:42 +00001780 * If skb->head_frag is set, this 'linear' part is backed by a
1781 * fragment, and if the head is not shared with any clones then
1782 * we can avoid a copy since we own the head portion of this page.
Jens Axboe9c55e012007-11-06 23:30:13 -08001783 */
Octavian Purdila2870c432008-07-15 00:49:11 -07001784 if (__splice_segment(virt_to_page(skb->data),
1785 (unsigned long) skb->data & (PAGE_SIZE - 1),
1786 skb_headlen(skb),
Eric Dumazet18aafc62013-01-11 14:46:37 +00001787 offset, len, spd,
Alexander Duyck3a7c1ee42012-05-03 01:09:42 +00001788 skb_head_is_locked(skb),
Eric Dumazet1d0c0b32012-04-27 02:10:03 +00001789 sk, pipe))
David S. Millera108d5f2012-04-23 23:06:11 -04001790 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08001791
1792 /*
1793 * then map the fragments
1794 */
Jens Axboe9c55e012007-11-06 23:30:13 -08001795 for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
1796 const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
1797
Ian Campbellea2ab692011-08-22 23:44:58 +00001798 if (__splice_segment(skb_frag_page(f),
Eric Dumazet9e903e02011-10-18 21:00:24 +00001799 f->page_offset, skb_frag_size(f),
Eric Dumazet18aafc62013-01-11 14:46:37 +00001800 offset, len, spd, false, sk, pipe))
David S. Millera108d5f2012-04-23 23:06:11 -04001801 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08001802 }
1803
David S. Millera108d5f2012-04-23 23:06:11 -04001804 return false;
Jens Axboe9c55e012007-11-06 23:30:13 -08001805}
1806
1807/*
1808 * Map data from the skb to a pipe. Should handle both the linear part,
1809 * the fragments, and the frag list. It does NOT handle frag lists within
1810 * the frag list, if such a thing exists. We'd probably need to recurse to
1811 * handle that cleanly.
1812 */
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001813int skb_splice_bits(struct sk_buff *skb, unsigned int offset,
Jens Axboe9c55e012007-11-06 23:30:13 -08001814 struct pipe_inode_info *pipe, unsigned int tlen,
1815 unsigned int flags)
1816{
Eric Dumazet41c73a02012-04-22 12:26:16 +00001817 struct partial_page partial[MAX_SKB_FRAGS];
1818 struct page *pages[MAX_SKB_FRAGS];
Jens Axboe9c55e012007-11-06 23:30:13 -08001819 struct splice_pipe_desc spd = {
1820 .pages = pages,
1821 .partial = partial,
Eric Dumazet047fe362012-06-12 15:24:40 +02001822 .nr_pages_max = MAX_SKB_FRAGS,
Jens Axboe9c55e012007-11-06 23:30:13 -08001823 .flags = flags,
Miklos Szeredi28a625c2014-01-22 19:36:57 +01001824 .ops = &nosteal_pipe_buf_ops,
Jens Axboe9c55e012007-11-06 23:30:13 -08001825 .spd_release = sock_spd_release,
1826 };
David S. Millerfbb398a2009-06-09 00:18:59 -07001827 struct sk_buff *frag_iter;
Jarek Poplawski7a67e562009-04-30 05:41:19 -07001828 struct sock *sk = skb->sk;
Jens Axboe35f3d142010-05-20 10:43:18 +02001829 int ret = 0;
1830
Jens Axboe9c55e012007-11-06 23:30:13 -08001831 /*
1832 * __skb_splice_bits() only fails if the output has no room left,
1833 * so no point in going over the frag_list for the error case.
1834 */
Jens Axboe35f3d142010-05-20 10:43:18 +02001835 if (__skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk))
Jens Axboe9c55e012007-11-06 23:30:13 -08001836 goto done;
1837 else if (!tlen)
1838 goto done;
1839
1840 /*
1841 * now see if we have a frag_list to map
1842 */
David S. Millerfbb398a2009-06-09 00:18:59 -07001843 skb_walk_frags(skb, frag_iter) {
1844 if (!tlen)
1845 break;
Jens Axboe35f3d142010-05-20 10:43:18 +02001846 if (__skb_splice_bits(frag_iter, pipe, &offset, &tlen, &spd, sk))
David S. Millerfbb398a2009-06-09 00:18:59 -07001847 break;
Jens Axboe9c55e012007-11-06 23:30:13 -08001848 }
1849
1850done:
Jens Axboe9c55e012007-11-06 23:30:13 -08001851 if (spd.nr_pages) {
Jens Axboe9c55e012007-11-06 23:30:13 -08001852 /*
1853 * Drop the socket lock, otherwise we have reverse
1854 * locking dependencies between sk_lock and i_mutex
1855 * here as compared to sendfile(). We enter here
1856 * with the socket lock held, and splice_to_pipe() will
1857 * grab the pipe inode lock. For sendfile() emulation,
1858 * we call into ->sendpage() with the i_mutex lock held
1859 * and networking will grab the socket lock.
1860 */
Octavian Purdila293ad602008-06-04 15:45:58 -07001861 release_sock(sk);
Jens Axboe9c55e012007-11-06 23:30:13 -08001862 ret = splice_to_pipe(pipe, &spd);
Octavian Purdila293ad602008-06-04 15:45:58 -07001863 lock_sock(sk);
Jens Axboe9c55e012007-11-06 23:30:13 -08001864 }
1865
Jens Axboe35f3d142010-05-20 10:43:18 +02001866 return ret;
Jens Axboe9c55e012007-11-06 23:30:13 -08001867}
1868
Herbert Xu357b40a2005-04-19 22:30:14 -07001869/**
1870 * skb_store_bits - store bits from kernel buffer to skb
1871 * @skb: destination buffer
1872 * @offset: offset in destination
1873 * @from: source buffer
1874 * @len: number of bytes to copy
1875 *
1876 * Copy the specified number of bytes from the source buffer to the
1877 * destination skb. This function handles all the messy bits of
1878 * traversing fragment lists and such.
1879 */
1880
Stephen Hemminger0c6fcc82007-04-20 16:40:01 -07001881int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
Herbert Xu357b40a2005-04-19 22:30:14 -07001882{
David S. Miller1a028e52007-04-27 15:21:23 -07001883 int start = skb_headlen(skb);
David S. Millerfbb398a2009-06-09 00:18:59 -07001884 struct sk_buff *frag_iter;
1885 int i, copy;
Herbert Xu357b40a2005-04-19 22:30:14 -07001886
1887 if (offset > (int)skb->len - len)
1888 goto fault;
1889
David S. Miller1a028e52007-04-27 15:21:23 -07001890 if ((copy = start - offset) > 0) {
Herbert Xu357b40a2005-04-19 22:30:14 -07001891 if (copy > len)
1892 copy = len;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001893 skb_copy_to_linear_data_offset(skb, offset, from, copy);
Herbert Xu357b40a2005-04-19 22:30:14 -07001894 if ((len -= copy) == 0)
1895 return 0;
1896 offset += copy;
1897 from += copy;
1898 }
1899
1900 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1901 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
David S. Miller1a028e52007-04-27 15:21:23 -07001902 int end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001903
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001904 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07001905
Eric Dumazet9e903e02011-10-18 21:00:24 +00001906 end = start + skb_frag_size(frag);
Herbert Xu357b40a2005-04-19 22:30:14 -07001907 if ((copy = end - offset) > 0) {
1908 u8 *vaddr;
1909
1910 if (copy > len)
1911 copy = len;
1912
Eric Dumazet51c56b02012-04-05 11:35:15 +02001913 vaddr = kmap_atomic(skb_frag_page(frag));
David S. Miller1a028e52007-04-27 15:21:23 -07001914 memcpy(vaddr + frag->page_offset + offset - start,
1915 from, copy);
Eric Dumazet51c56b02012-04-05 11:35:15 +02001916 kunmap_atomic(vaddr);
Herbert Xu357b40a2005-04-19 22:30:14 -07001917
1918 if ((len -= copy) == 0)
1919 return 0;
1920 offset += copy;
1921 from += copy;
1922 }
David S. Miller1a028e52007-04-27 15:21:23 -07001923 start = end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001924 }
1925
David S. Millerfbb398a2009-06-09 00:18:59 -07001926 skb_walk_frags(skb, frag_iter) {
1927 int end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001928
David S. Millerfbb398a2009-06-09 00:18:59 -07001929 WARN_ON(start > offset + len);
Herbert Xu357b40a2005-04-19 22:30:14 -07001930
David S. Millerfbb398a2009-06-09 00:18:59 -07001931 end = start + frag_iter->len;
1932 if ((copy = end - offset) > 0) {
1933 if (copy > len)
1934 copy = len;
1935 if (skb_store_bits(frag_iter, offset - start,
1936 from, copy))
1937 goto fault;
1938 if ((len -= copy) == 0)
1939 return 0;
1940 offset += copy;
1941 from += copy;
Herbert Xu357b40a2005-04-19 22:30:14 -07001942 }
David S. Millerfbb398a2009-06-09 00:18:59 -07001943 start = end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001944 }
1945 if (!len)
1946 return 0;
1947
1948fault:
1949 return -EFAULT;
1950}
Herbert Xu357b40a2005-04-19 22:30:14 -07001951EXPORT_SYMBOL(skb_store_bits);
1952
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953/* Checksum skb data. */
Daniel Borkmann2817a332013-10-30 11:50:51 +01001954__wsum __skb_checksum(const struct sk_buff *skb, int offset, int len,
1955 __wsum csum, const struct skb_checksum_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956{
David S. Miller1a028e52007-04-27 15:21:23 -07001957 int start = skb_headlen(skb);
1958 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07001959 struct sk_buff *frag_iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 int pos = 0;
1961
1962 /* Checksum header. */
1963 if (copy > 0) {
1964 if (copy > len)
1965 copy = len;
Daniel Borkmann2817a332013-10-30 11:50:51 +01001966 csum = ops->update(skb->data + offset, copy, csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 if ((len -= copy) == 0)
1968 return csum;
1969 offset += copy;
1970 pos = copy;
1971 }
1972
1973 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07001974 int end;
Eric Dumazet51c56b02012-04-05 11:35:15 +02001975 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001977 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07001978
Eric Dumazet51c56b02012-04-05 11:35:15 +02001979 end = start + skb_frag_size(frag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 if ((copy = end - offset) > 0) {
Al Viro44bb9362006-11-14 21:36:14 -08001981 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 u8 *vaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983
1984 if (copy > len)
1985 copy = len;
Eric Dumazet51c56b02012-04-05 11:35:15 +02001986 vaddr = kmap_atomic(skb_frag_page(frag));
Daniel Borkmann2817a332013-10-30 11:50:51 +01001987 csum2 = ops->update(vaddr + frag->page_offset +
1988 offset - start, copy, 0);
Eric Dumazet51c56b02012-04-05 11:35:15 +02001989 kunmap_atomic(vaddr);
Daniel Borkmann2817a332013-10-30 11:50:51 +01001990 csum = ops->combine(csum, csum2, pos, copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 if (!(len -= copy))
1992 return csum;
1993 offset += copy;
1994 pos += copy;
1995 }
David S. Miller1a028e52007-04-27 15:21:23 -07001996 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 }
1998
David S. Millerfbb398a2009-06-09 00:18:59 -07001999 skb_walk_frags(skb, frag_iter) {
2000 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001
David S. Millerfbb398a2009-06-09 00:18:59 -07002002 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003
David S. Millerfbb398a2009-06-09 00:18:59 -07002004 end = start + frag_iter->len;
2005 if ((copy = end - offset) > 0) {
2006 __wsum csum2;
2007 if (copy > len)
2008 copy = len;
Daniel Borkmann2817a332013-10-30 11:50:51 +01002009 csum2 = __skb_checksum(frag_iter, offset - start,
2010 copy, 0, ops);
2011 csum = ops->combine(csum, csum2, pos, copy);
David S. Millerfbb398a2009-06-09 00:18:59 -07002012 if ((len -= copy) == 0)
2013 return csum;
2014 offset += copy;
2015 pos += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002017 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08002019 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020
2021 return csum;
2022}
Daniel Borkmann2817a332013-10-30 11:50:51 +01002023EXPORT_SYMBOL(__skb_checksum);
2024
2025__wsum skb_checksum(const struct sk_buff *skb, int offset,
2026 int len, __wsum csum)
2027{
2028 const struct skb_checksum_ops ops = {
Daniel Borkmanncea80ea2013-11-04 17:10:25 +01002029 .update = csum_partial_ext,
Daniel Borkmann2817a332013-10-30 11:50:51 +01002030 .combine = csum_block_add_ext,
2031 };
2032
2033 return __skb_checksum(skb, offset, len, csum, &ops);
2034}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002035EXPORT_SYMBOL(skb_checksum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036
2037/* Both of above in one bottle. */
2038
Al Viro81d77662006-11-14 21:37:33 -08002039__wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
2040 u8 *to, int len, __wsum csum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041{
David S. Miller1a028e52007-04-27 15:21:23 -07002042 int start = skb_headlen(skb);
2043 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07002044 struct sk_buff *frag_iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 int pos = 0;
2046
2047 /* Copy header. */
2048 if (copy > 0) {
2049 if (copy > len)
2050 copy = len;
2051 csum = csum_partial_copy_nocheck(skb->data + offset, to,
2052 copy, csum);
2053 if ((len -= copy) == 0)
2054 return csum;
2055 offset += copy;
2056 to += copy;
2057 pos = copy;
2058 }
2059
2060 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07002061 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002063 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07002064
Eric Dumazet9e903e02011-10-18 21:00:24 +00002065 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 if ((copy = end - offset) > 0) {
Al Viro50842052006-11-14 21:36:34 -08002067 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 u8 *vaddr;
2069 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2070
2071 if (copy > len)
2072 copy = len;
Eric Dumazet51c56b02012-04-05 11:35:15 +02002073 vaddr = kmap_atomic(skb_frag_page(frag));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 csum2 = csum_partial_copy_nocheck(vaddr +
David S. Miller1a028e52007-04-27 15:21:23 -07002075 frag->page_offset +
2076 offset - start, to,
2077 copy, 0);
Eric Dumazet51c56b02012-04-05 11:35:15 +02002078 kunmap_atomic(vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 csum = csum_block_add(csum, csum2, pos);
2080 if (!(len -= copy))
2081 return csum;
2082 offset += copy;
2083 to += copy;
2084 pos += copy;
2085 }
David S. Miller1a028e52007-04-27 15:21:23 -07002086 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 }
2088
David S. Millerfbb398a2009-06-09 00:18:59 -07002089 skb_walk_frags(skb, frag_iter) {
2090 __wsum csum2;
2091 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092
David S. Millerfbb398a2009-06-09 00:18:59 -07002093 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094
David S. Millerfbb398a2009-06-09 00:18:59 -07002095 end = start + frag_iter->len;
2096 if ((copy = end - offset) > 0) {
2097 if (copy > len)
2098 copy = len;
2099 csum2 = skb_copy_and_csum_bits(frag_iter,
2100 offset - start,
2101 to, copy, 0);
2102 csum = csum_block_add(csum, csum2, pos);
2103 if ((len -= copy) == 0)
2104 return csum;
2105 offset += copy;
2106 to += copy;
2107 pos += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002109 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08002111 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 return csum;
2113}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002114EXPORT_SYMBOL(skb_copy_and_csum_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115
Thomas Grafaf2806f2013-12-13 15:22:17 +01002116 /**
2117 * skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy()
2118 * @from: source buffer
2119 *
2120 * Calculates the amount of linear headroom needed in the 'to' skb passed
2121 * into skb_zerocopy().
2122 */
2123unsigned int
2124skb_zerocopy_headlen(const struct sk_buff *from)
2125{
2126 unsigned int hlen = 0;
2127
2128 if (!from->head_frag ||
2129 skb_headlen(from) < L1_CACHE_BYTES ||
2130 skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
2131 hlen = skb_headlen(from);
2132
2133 if (skb_has_frag_list(from))
2134 hlen = from->len;
2135
2136 return hlen;
2137}
2138EXPORT_SYMBOL_GPL(skb_zerocopy_headlen);
2139
2140/**
2141 * skb_zerocopy - Zero copy skb to skb
2142 * @to: destination buffer
Masanari Iida7fceb4d2014-01-29 01:05:28 +09002143 * @from: source buffer
Thomas Grafaf2806f2013-12-13 15:22:17 +01002144 * @len: number of bytes to copy from source buffer
2145 * @hlen: size of linear headroom in destination buffer
2146 *
2147 * Copies up to `len` bytes from `from` to `to` by creating references
2148 * to the frags in the source buffer.
2149 *
2150 * The `hlen` as calculated by skb_zerocopy_headlen() specifies the
2151 * headroom in the `to` buffer.
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002152 *
2153 * Return value:
2154 * 0: everything is OK
2155 * -ENOMEM: couldn't orphan frags of @from due to lack of memory
2156 * -EFAULT: skb_copy_bits() found some problem with skb geometry
Thomas Grafaf2806f2013-12-13 15:22:17 +01002157 */
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002158int
2159skb_zerocopy(struct sk_buff *to, struct sk_buff *from, int len, int hlen)
Thomas Grafaf2806f2013-12-13 15:22:17 +01002160{
2161 int i, j = 0;
2162 int plen = 0; /* length of skb->head fragment */
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002163 int ret;
Thomas Grafaf2806f2013-12-13 15:22:17 +01002164 struct page *page;
2165 unsigned int offset;
2166
2167 BUG_ON(!from->head_frag && !hlen);
2168
2169 /* dont bother with small payloads */
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002170 if (len <= skb_tailroom(to))
2171 return skb_copy_bits(from, 0, skb_put(to, len), len);
Thomas Grafaf2806f2013-12-13 15:22:17 +01002172
2173 if (hlen) {
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002174 ret = skb_copy_bits(from, 0, skb_put(to, hlen), hlen);
2175 if (unlikely(ret))
2176 return ret;
Thomas Grafaf2806f2013-12-13 15:22:17 +01002177 len -= hlen;
2178 } else {
2179 plen = min_t(int, skb_headlen(from), len);
2180 if (plen) {
2181 page = virt_to_head_page(from->head);
2182 offset = from->data - (unsigned char *)page_address(page);
2183 __skb_fill_page_desc(to, 0, page, offset, plen);
2184 get_page(page);
2185 j = 1;
2186 len -= plen;
2187 }
2188 }
2189
2190 to->truesize += len + plen;
2191 to->len += len + plen;
2192 to->data_len += len + plen;
2193
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002194 if (unlikely(skb_orphan_frags(from, GFP_ATOMIC))) {
2195 skb_tx_error(from);
2196 return -ENOMEM;
2197 }
2198
Thomas Grafaf2806f2013-12-13 15:22:17 +01002199 for (i = 0; i < skb_shinfo(from)->nr_frags; i++) {
2200 if (!len)
2201 break;
2202 skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i];
2203 skb_shinfo(to)->frags[j].size = min_t(int, skb_shinfo(to)->frags[j].size, len);
2204 len -= skb_shinfo(to)->frags[j].size;
2205 skb_frag_ref(to, j);
2206 j++;
2207 }
2208 skb_shinfo(to)->nr_frags = j;
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002209
2210 return 0;
Thomas Grafaf2806f2013-12-13 15:22:17 +01002211}
2212EXPORT_SYMBOL_GPL(skb_zerocopy);
2213
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
2215{
Al Virod3bc23e2006-11-14 21:24:49 -08002216 __wsum csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 long csstart;
2218
Patrick McHardy84fa7932006-08-29 16:44:56 -07002219 if (skb->ip_summed == CHECKSUM_PARTIAL)
Michał Mirosław55508d62010-12-14 15:24:08 +00002220 csstart = skb_checksum_start_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 else
2222 csstart = skb_headlen(skb);
2223
Kris Katterjohn09a62662006-01-08 22:24:28 -08002224 BUG_ON(csstart > skb_headlen(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03002226 skb_copy_from_linear_data(skb, to, csstart);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227
2228 csum = 0;
2229 if (csstart != skb->len)
2230 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
2231 skb->len - csstart, 0);
2232
Patrick McHardy84fa7932006-08-29 16:44:56 -07002233 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Al Viroff1dcad2006-11-20 18:07:29 -08002234 long csstuff = csstart + skb->csum_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235
Al Virod3bc23e2006-11-14 21:24:49 -08002236 *((__sum16 *)(to + csstuff)) = csum_fold(csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 }
2238}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002239EXPORT_SYMBOL(skb_copy_and_csum_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240
2241/**
2242 * skb_dequeue - remove from the head of the queue
2243 * @list: list to dequeue from
2244 *
2245 * Remove the head of the list. The list lock is taken so the function
2246 * may be used safely with other locking list functions. The head item is
2247 * returned or %NULL if the list is empty.
2248 */
2249
2250struct sk_buff *skb_dequeue(struct sk_buff_head *list)
2251{
2252 unsigned long flags;
2253 struct sk_buff *result;
2254
2255 spin_lock_irqsave(&list->lock, flags);
2256 result = __skb_dequeue(list);
2257 spin_unlock_irqrestore(&list->lock, flags);
2258 return result;
2259}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002260EXPORT_SYMBOL(skb_dequeue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261
2262/**
2263 * skb_dequeue_tail - remove from the tail of the queue
2264 * @list: list to dequeue from
2265 *
2266 * Remove the tail of the list. The list lock is taken so the function
2267 * may be used safely with other locking list functions. The tail item is
2268 * returned or %NULL if the list is empty.
2269 */
2270struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
2271{
2272 unsigned long flags;
2273 struct sk_buff *result;
2274
2275 spin_lock_irqsave(&list->lock, flags);
2276 result = __skb_dequeue_tail(list);
2277 spin_unlock_irqrestore(&list->lock, flags);
2278 return result;
2279}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002280EXPORT_SYMBOL(skb_dequeue_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281
2282/**
2283 * skb_queue_purge - empty a list
2284 * @list: list to empty
2285 *
2286 * Delete all buffers on an &sk_buff list. Each buffer is removed from
2287 * the list and one reference dropped. This function takes the list
2288 * lock and is atomic with respect to other list locking functions.
2289 */
2290void skb_queue_purge(struct sk_buff_head *list)
2291{
2292 struct sk_buff *skb;
2293 while ((skb = skb_dequeue(list)) != NULL)
2294 kfree_skb(skb);
2295}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002296EXPORT_SYMBOL(skb_queue_purge);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297
2298/**
2299 * skb_queue_head - queue a buffer at the list head
2300 * @list: list to use
2301 * @newsk: buffer to queue
2302 *
2303 * Queue a buffer at the start of the list. This function takes the
2304 * list lock and can be used safely with other locking &sk_buff functions
2305 * safely.
2306 *
2307 * A buffer cannot be placed on two lists at the same time.
2308 */
2309void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
2310{
2311 unsigned long flags;
2312
2313 spin_lock_irqsave(&list->lock, flags);
2314 __skb_queue_head(list, newsk);
2315 spin_unlock_irqrestore(&list->lock, flags);
2316}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002317EXPORT_SYMBOL(skb_queue_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318
2319/**
2320 * skb_queue_tail - queue a buffer at the list tail
2321 * @list: list to use
2322 * @newsk: buffer to queue
2323 *
2324 * Queue a buffer at the tail of the list. This function takes the
2325 * list lock and can be used safely with other locking &sk_buff functions
2326 * safely.
2327 *
2328 * A buffer cannot be placed on two lists at the same time.
2329 */
2330void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
2331{
2332 unsigned long flags;
2333
2334 spin_lock_irqsave(&list->lock, flags);
2335 __skb_queue_tail(list, newsk);
2336 spin_unlock_irqrestore(&list->lock, flags);
2337}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002338EXPORT_SYMBOL(skb_queue_tail);
David S. Miller8728b832005-08-09 19:25:21 -07002339
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340/**
2341 * skb_unlink - remove a buffer from a list
2342 * @skb: buffer to remove
David S. Miller8728b832005-08-09 19:25:21 -07002343 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 *
David S. Miller8728b832005-08-09 19:25:21 -07002345 * Remove a packet from a list. The list locks are taken and this
2346 * function is atomic with respect to other list locked calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 *
David S. Miller8728b832005-08-09 19:25:21 -07002348 * You must know what list the SKB is on.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 */
David S. Miller8728b832005-08-09 19:25:21 -07002350void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351{
David S. Miller8728b832005-08-09 19:25:21 -07002352 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353
David S. Miller8728b832005-08-09 19:25:21 -07002354 spin_lock_irqsave(&list->lock, flags);
2355 __skb_unlink(skb, list);
2356 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002358EXPORT_SYMBOL(skb_unlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360/**
2361 * skb_append - append a buffer
2362 * @old: buffer to insert after
2363 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07002364 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 *
2366 * Place a packet after a given packet in a list. The list locks are taken
2367 * and this function is atomic with respect to other list locked calls.
2368 * A buffer cannot be placed on two lists at the same time.
2369 */
David S. Miller8728b832005-08-09 19:25:21 -07002370void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371{
2372 unsigned long flags;
2373
David S. Miller8728b832005-08-09 19:25:21 -07002374 spin_lock_irqsave(&list->lock, flags);
Gerrit Renker7de6c032008-04-14 00:05:09 -07002375 __skb_queue_after(list, old, newsk);
David S. Miller8728b832005-08-09 19:25:21 -07002376 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002378EXPORT_SYMBOL(skb_append);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379
2380/**
2381 * skb_insert - insert a buffer
2382 * @old: buffer to insert before
2383 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07002384 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 *
David S. Miller8728b832005-08-09 19:25:21 -07002386 * Place a packet before a given packet in a list. The list locks are
2387 * taken and this function is atomic with respect to other list locked
2388 * calls.
2389 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 * A buffer cannot be placed on two lists at the same time.
2391 */
David S. Miller8728b832005-08-09 19:25:21 -07002392void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393{
2394 unsigned long flags;
2395
David S. Miller8728b832005-08-09 19:25:21 -07002396 spin_lock_irqsave(&list->lock, flags);
2397 __skb_insert(newsk, old->prev, old, list);
2398 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002400EXPORT_SYMBOL(skb_insert);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402static inline void skb_split_inside_header(struct sk_buff *skb,
2403 struct sk_buff* skb1,
2404 const u32 len, const int pos)
2405{
2406 int i;
2407
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03002408 skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
2409 pos - len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 /* And move data appendix as is. */
2411 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
2412 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
2413
2414 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
2415 skb_shinfo(skb)->nr_frags = 0;
2416 skb1->data_len = skb->data_len;
2417 skb1->len += skb1->data_len;
2418 skb->data_len = 0;
2419 skb->len = len;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002420 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421}
2422
2423static inline void skb_split_no_header(struct sk_buff *skb,
2424 struct sk_buff* skb1,
2425 const u32 len, int pos)
2426{
2427 int i, k = 0;
2428 const int nfrags = skb_shinfo(skb)->nr_frags;
2429
2430 skb_shinfo(skb)->nr_frags = 0;
2431 skb1->len = skb1->data_len = skb->len - len;
2432 skb->len = len;
2433 skb->data_len = len - pos;
2434
2435 for (i = 0; i < nfrags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00002436 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437
2438 if (pos + size > len) {
2439 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
2440
2441 if (pos < len) {
2442 /* Split frag.
2443 * We have two variants in this case:
2444 * 1. Move all the frag to the second
2445 * part, if it is possible. F.e.
2446 * this approach is mandatory for TUX,
2447 * where splitting is expensive.
2448 * 2. Split is accurately. We make this.
2449 */
Ian Campbellea2ab692011-08-22 23:44:58 +00002450 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002452 skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos);
2453 skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 skb_shinfo(skb)->nr_frags++;
2455 }
2456 k++;
2457 } else
2458 skb_shinfo(skb)->nr_frags++;
2459 pos += size;
2460 }
2461 skb_shinfo(skb1)->nr_frags = k;
2462}
2463
2464/**
2465 * skb_split - Split fragmented skb to two parts at length len.
2466 * @skb: the buffer to split
2467 * @skb1: the buffer to receive the second part
2468 * @len: new length for skb
2469 */
2470void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
2471{
2472 int pos = skb_headlen(skb);
2473
Amerigo Wang68534c62013-02-19 22:51:30 +00002474 skb_shinfo(skb1)->tx_flags = skb_shinfo(skb)->tx_flags & SKBTX_SHARED_FRAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 if (len < pos) /* Split line is inside header. */
2476 skb_split_inside_header(skb, skb1, len, pos);
2477 else /* Second chunk has no header, nothing to copy. */
2478 skb_split_no_header(skb, skb1, len, pos);
2479}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002480EXPORT_SYMBOL(skb_split);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481
Ilpo Järvinen9f782db2008-11-25 13:57:01 -08002482/* Shifting from/to a cloned skb is a no-go.
2483 *
2484 * Caller cannot keep skb_shinfo related pointers past calling here!
2485 */
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002486static int skb_prepare_for_shift(struct sk_buff *skb)
2487{
Ilpo Järvinen0ace2852008-11-24 21:30:21 -08002488 return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002489}
2490
2491/**
2492 * skb_shift - Shifts paged data partially from skb to another
2493 * @tgt: buffer into which tail data gets added
2494 * @skb: buffer from which the paged data comes from
2495 * @shiftlen: shift up to this many bytes
2496 *
2497 * Attempts to shift up to shiftlen worth of bytes, which may be less than
Feng King20e994a2011-11-21 01:47:11 +00002498 * the length of the skb, from skb to tgt. Returns number bytes shifted.
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002499 * It's up to caller to free skb if everything was shifted.
2500 *
2501 * If @tgt runs out of frags, the whole operation is aborted.
2502 *
2503 * Skb cannot include anything else but paged data while tgt is allowed
2504 * to have non-paged data as well.
2505 *
2506 * TODO: full sized shift could be optimized but that would need
2507 * specialized skb free'er to handle frags without up-to-date nr_frags.
2508 */
2509int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
2510{
2511 int from, to, merge, todo;
2512 struct skb_frag_struct *fragfrom, *fragto;
2513
2514 BUG_ON(shiftlen > skb->len);
2515 BUG_ON(skb_headlen(skb)); /* Would corrupt stream */
2516
2517 todo = shiftlen;
2518 from = 0;
2519 to = skb_shinfo(tgt)->nr_frags;
2520 fragfrom = &skb_shinfo(skb)->frags[from];
2521
2522 /* Actual merge is delayed until the point when we know we can
2523 * commit all, so that we don't have to undo partial changes
2524 */
2525 if (!to ||
Ian Campbellea2ab692011-08-22 23:44:58 +00002526 !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom),
2527 fragfrom->page_offset)) {
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002528 merge = -1;
2529 } else {
2530 merge = to - 1;
2531
Eric Dumazet9e903e02011-10-18 21:00:24 +00002532 todo -= skb_frag_size(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002533 if (todo < 0) {
2534 if (skb_prepare_for_shift(skb) ||
2535 skb_prepare_for_shift(tgt))
2536 return 0;
2537
Ilpo Järvinen9f782db2008-11-25 13:57:01 -08002538 /* All previous frag pointers might be stale! */
2539 fragfrom = &skb_shinfo(skb)->frags[from];
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002540 fragto = &skb_shinfo(tgt)->frags[merge];
2541
Eric Dumazet9e903e02011-10-18 21:00:24 +00002542 skb_frag_size_add(fragto, shiftlen);
2543 skb_frag_size_sub(fragfrom, shiftlen);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002544 fragfrom->page_offset += shiftlen;
2545
2546 goto onlymerged;
2547 }
2548
2549 from++;
2550 }
2551
2552 /* Skip full, not-fitting skb to avoid expensive operations */
2553 if ((shiftlen == skb->len) &&
2554 (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to))
2555 return 0;
2556
2557 if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt))
2558 return 0;
2559
2560 while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) {
2561 if (to == MAX_SKB_FRAGS)
2562 return 0;
2563
2564 fragfrom = &skb_shinfo(skb)->frags[from];
2565 fragto = &skb_shinfo(tgt)->frags[to];
2566
Eric Dumazet9e903e02011-10-18 21:00:24 +00002567 if (todo >= skb_frag_size(fragfrom)) {
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002568 *fragto = *fragfrom;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002569 todo -= skb_frag_size(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002570 from++;
2571 to++;
2572
2573 } else {
Ian Campbellea2ab692011-08-22 23:44:58 +00002574 __skb_frag_ref(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002575 fragto->page = fragfrom->page;
2576 fragto->page_offset = fragfrom->page_offset;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002577 skb_frag_size_set(fragto, todo);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002578
2579 fragfrom->page_offset += todo;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002580 skb_frag_size_sub(fragfrom, todo);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002581 todo = 0;
2582
2583 to++;
2584 break;
2585 }
2586 }
2587
2588 /* Ready to "commit" this state change to tgt */
2589 skb_shinfo(tgt)->nr_frags = to;
2590
2591 if (merge >= 0) {
2592 fragfrom = &skb_shinfo(skb)->frags[0];
2593 fragto = &skb_shinfo(tgt)->frags[merge];
2594
Eric Dumazet9e903e02011-10-18 21:00:24 +00002595 skb_frag_size_add(fragto, skb_frag_size(fragfrom));
Ian Campbellea2ab692011-08-22 23:44:58 +00002596 __skb_frag_unref(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002597 }
2598
2599 /* Reposition in the original skb */
2600 to = 0;
2601 while (from < skb_shinfo(skb)->nr_frags)
2602 skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++];
2603 skb_shinfo(skb)->nr_frags = to;
2604
2605 BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags);
2606
2607onlymerged:
2608 /* Most likely the tgt won't ever need its checksum anymore, skb on
2609 * the other hand might need it if it needs to be resent
2610 */
2611 tgt->ip_summed = CHECKSUM_PARTIAL;
2612 skb->ip_summed = CHECKSUM_PARTIAL;
2613
2614 /* Yak, is it really working this way? Some helper please? */
2615 skb->len -= shiftlen;
2616 skb->data_len -= shiftlen;
2617 skb->truesize -= shiftlen;
2618 tgt->len += shiftlen;
2619 tgt->data_len += shiftlen;
2620 tgt->truesize += shiftlen;
2621
2622 return shiftlen;
2623}
2624
Thomas Graf677e90e2005-06-23 20:59:51 -07002625/**
2626 * skb_prepare_seq_read - Prepare a sequential read of skb data
2627 * @skb: the buffer to read
2628 * @from: lower offset of data to be read
2629 * @to: upper offset of data to be read
2630 * @st: state variable
2631 *
2632 * Initializes the specified state variable. Must be called before
2633 * invoking skb_seq_read() for the first time.
2634 */
2635void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
2636 unsigned int to, struct skb_seq_state *st)
2637{
2638 st->lower_offset = from;
2639 st->upper_offset = to;
2640 st->root_skb = st->cur_skb = skb;
2641 st->frag_idx = st->stepped_offset = 0;
2642 st->frag_data = NULL;
2643}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002644EXPORT_SYMBOL(skb_prepare_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07002645
2646/**
2647 * skb_seq_read - Sequentially read skb data
2648 * @consumed: number of bytes consumed by the caller so far
2649 * @data: destination pointer for data to be returned
2650 * @st: state variable
2651 *
Mathias Krausebc323832013-11-07 14:18:26 +01002652 * Reads a block of skb data at @consumed relative to the
Thomas Graf677e90e2005-06-23 20:59:51 -07002653 * lower offset specified to skb_prepare_seq_read(). Assigns
Mathias Krausebc323832013-11-07 14:18:26 +01002654 * the head of the data block to @data and returns the length
Thomas Graf677e90e2005-06-23 20:59:51 -07002655 * of the block or 0 if the end of the skb data or the upper
2656 * offset has been reached.
2657 *
2658 * The caller is not required to consume all of the data
Mathias Krausebc323832013-11-07 14:18:26 +01002659 * returned, i.e. @consumed is typically set to the number
Thomas Graf677e90e2005-06-23 20:59:51 -07002660 * of bytes already consumed and the next call to
2661 * skb_seq_read() will return the remaining part of the block.
2662 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002663 * Note 1: The size of each block of data returned can be arbitrary,
Masanari Iidae793c0f2014-09-04 23:44:36 +09002664 * this limitation is the cost for zerocopy sequential
Thomas Graf677e90e2005-06-23 20:59:51 -07002665 * reads of potentially non linear data.
2666 *
Randy Dunlapbc2cda12008-02-13 15:03:25 -08002667 * Note 2: Fragment lists within fragments are not implemented
Thomas Graf677e90e2005-06-23 20:59:51 -07002668 * at the moment, state->root_skb could be replaced with
2669 * a stack for this purpose.
2670 */
2671unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
2672 struct skb_seq_state *st)
2673{
2674 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
2675 skb_frag_t *frag;
2676
Wedson Almeida Filhoaeb193e2013-06-23 23:33:48 -07002677 if (unlikely(abs_offset >= st->upper_offset)) {
2678 if (st->frag_data) {
2679 kunmap_atomic(st->frag_data);
2680 st->frag_data = NULL;
2681 }
Thomas Graf677e90e2005-06-23 20:59:51 -07002682 return 0;
Wedson Almeida Filhoaeb193e2013-06-23 23:33:48 -07002683 }
Thomas Graf677e90e2005-06-23 20:59:51 -07002684
2685next_skb:
Herbert Xu95e3b242009-01-29 16:07:52 -08002686 block_limit = skb_headlen(st->cur_skb) + st->stepped_offset;
Thomas Graf677e90e2005-06-23 20:59:51 -07002687
Thomas Chenault995b3372009-05-18 21:43:27 -07002688 if (abs_offset < block_limit && !st->frag_data) {
Herbert Xu95e3b242009-01-29 16:07:52 -08002689 *data = st->cur_skb->data + (abs_offset - st->stepped_offset);
Thomas Graf677e90e2005-06-23 20:59:51 -07002690 return block_limit - abs_offset;
2691 }
2692
2693 if (st->frag_idx == 0 && !st->frag_data)
2694 st->stepped_offset += skb_headlen(st->cur_skb);
2695
2696 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
2697 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
Eric Dumazet9e903e02011-10-18 21:00:24 +00002698 block_limit = skb_frag_size(frag) + st->stepped_offset;
Thomas Graf677e90e2005-06-23 20:59:51 -07002699
2700 if (abs_offset < block_limit) {
2701 if (!st->frag_data)
Eric Dumazet51c56b02012-04-05 11:35:15 +02002702 st->frag_data = kmap_atomic(skb_frag_page(frag));
Thomas Graf677e90e2005-06-23 20:59:51 -07002703
2704 *data = (u8 *) st->frag_data + frag->page_offset +
2705 (abs_offset - st->stepped_offset);
2706
2707 return block_limit - abs_offset;
2708 }
2709
2710 if (st->frag_data) {
Eric Dumazet51c56b02012-04-05 11:35:15 +02002711 kunmap_atomic(st->frag_data);
Thomas Graf677e90e2005-06-23 20:59:51 -07002712 st->frag_data = NULL;
2713 }
2714
2715 st->frag_idx++;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002716 st->stepped_offset += skb_frag_size(frag);
Thomas Graf677e90e2005-06-23 20:59:51 -07002717 }
2718
Olaf Kirch5b5a60d2007-06-23 23:11:52 -07002719 if (st->frag_data) {
Eric Dumazet51c56b02012-04-05 11:35:15 +02002720 kunmap_atomic(st->frag_data);
Olaf Kirch5b5a60d2007-06-23 23:11:52 -07002721 st->frag_data = NULL;
2722 }
2723
David S. Miller21dc3302010-08-23 00:13:46 -07002724 if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) {
Shyam Iyer71b33462009-01-29 16:12:42 -08002725 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
Thomas Graf677e90e2005-06-23 20:59:51 -07002726 st->frag_idx = 0;
2727 goto next_skb;
Shyam Iyer71b33462009-01-29 16:12:42 -08002728 } else if (st->cur_skb->next) {
2729 st->cur_skb = st->cur_skb->next;
Herbert Xu95e3b242009-01-29 16:07:52 -08002730 st->frag_idx = 0;
Thomas Graf677e90e2005-06-23 20:59:51 -07002731 goto next_skb;
2732 }
2733
2734 return 0;
2735}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002736EXPORT_SYMBOL(skb_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07002737
2738/**
2739 * skb_abort_seq_read - Abort a sequential read of skb data
2740 * @st: state variable
2741 *
2742 * Must be called if skb_seq_read() was not called until it
2743 * returned 0.
2744 */
2745void skb_abort_seq_read(struct skb_seq_state *st)
2746{
2747 if (st->frag_data)
Eric Dumazet51c56b02012-04-05 11:35:15 +02002748 kunmap_atomic(st->frag_data);
Thomas Graf677e90e2005-06-23 20:59:51 -07002749}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002750EXPORT_SYMBOL(skb_abort_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07002751
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002752#define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
2753
2754static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
2755 struct ts_config *conf,
2756 struct ts_state *state)
2757{
2758 return skb_seq_read(offset, text, TS_SKB_CB(state));
2759}
2760
2761static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
2762{
2763 skb_abort_seq_read(TS_SKB_CB(state));
2764}
2765
2766/**
2767 * skb_find_text - Find a text pattern in skb data
2768 * @skb: the buffer to look in
2769 * @from: search offset
2770 * @to: search limit
2771 * @config: textsearch configuration
2772 * @state: uninitialized textsearch state variable
2773 *
2774 * Finds a pattern in the skb data according to the specified
2775 * textsearch configuration. Use textsearch_next() to retrieve
2776 * subsequent occurrences of the pattern. Returns the offset
2777 * to the first occurrence or UINT_MAX if no match was found.
2778 */
2779unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
2780 unsigned int to, struct ts_config *config,
2781 struct ts_state *state)
2782{
Phil Oesterf72b9482006-06-26 00:00:57 -07002783 unsigned int ret;
2784
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002785 config->get_next_block = skb_ts_get_next_block;
2786 config->finish = skb_ts_finish;
2787
2788 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state));
2789
Phil Oesterf72b9482006-06-26 00:00:57 -07002790 ret = textsearch_find(config, state);
2791 return (ret <= to - from ? ret : UINT_MAX);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002792}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002793EXPORT_SYMBOL(skb_find_text);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002794
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002795/**
Ben Hutchings2c530402012-07-10 10:55:09 +00002796 * skb_append_datato_frags - append the user data to a skb
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002797 * @sk: sock structure
Masanari Iidae793c0f2014-09-04 23:44:36 +09002798 * @skb: skb structure to be appended with user data.
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002799 * @getfrag: call back function to be used for getting the user data
2800 * @from: pointer to user message iov
2801 * @length: length of the iov message
2802 *
2803 * Description: This procedure append the user data in the fragment part
2804 * of the skb if any page alloc fails user this procedure returns -ENOMEM
2805 */
2806int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
Martin Waitzdab96302005-12-05 13:40:12 -08002807 int (*getfrag)(void *from, char *to, int offset,
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002808 int len, int odd, struct sk_buff *skb),
2809 void *from, int length)
2810{
Eric Dumazetb2111722012-12-28 06:06:37 +00002811 int frg_cnt = skb_shinfo(skb)->nr_frags;
2812 int copy;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002813 int offset = 0;
2814 int ret;
Eric Dumazetb2111722012-12-28 06:06:37 +00002815 struct page_frag *pfrag = &current->task_frag;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002816
2817 do {
2818 /* Return error if we don't have space for new frag */
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002819 if (frg_cnt >= MAX_SKB_FRAGS)
Eric Dumazetb2111722012-12-28 06:06:37 +00002820 return -EMSGSIZE;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002821
Eric Dumazetb2111722012-12-28 06:06:37 +00002822 if (!sk_page_frag_refill(sk, pfrag))
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002823 return -ENOMEM;
2824
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002825 /* copy the user data to page */
Eric Dumazetb2111722012-12-28 06:06:37 +00002826 copy = min_t(int, length, pfrag->size - pfrag->offset);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002827
Eric Dumazetb2111722012-12-28 06:06:37 +00002828 ret = getfrag(from, page_address(pfrag->page) + pfrag->offset,
2829 offset, copy, 0, skb);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002830 if (ret < 0)
2831 return -EFAULT;
2832
2833 /* copy was successful so update the size parameters */
Eric Dumazetb2111722012-12-28 06:06:37 +00002834 skb_fill_page_desc(skb, frg_cnt, pfrag->page, pfrag->offset,
2835 copy);
2836 frg_cnt++;
2837 pfrag->offset += copy;
2838 get_page(pfrag->page);
2839
2840 skb->truesize += copy;
2841 atomic_add(copy, &sk->sk_wmem_alloc);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002842 skb->len += copy;
2843 skb->data_len += copy;
2844 offset += copy;
2845 length -= copy;
2846
2847 } while (length > 0);
2848
2849 return 0;
2850}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002851EXPORT_SYMBOL(skb_append_datato_frags);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002852
Herbert Xucbb042f2006-03-20 22:43:56 -08002853/**
2854 * skb_pull_rcsum - pull skb and update receive checksum
2855 * @skb: buffer to update
Herbert Xucbb042f2006-03-20 22:43:56 -08002856 * @len: length of data pulled
2857 *
2858 * This function performs an skb_pull on the packet and updates
Urs Thuermannfee54fa2008-02-12 22:03:25 -08002859 * the CHECKSUM_COMPLETE checksum. It should be used on
Patrick McHardy84fa7932006-08-29 16:44:56 -07002860 * receive path processing instead of skb_pull unless you know
2861 * that the checksum difference is zero (e.g., a valid IP header)
2862 * or you are setting ip_summed to CHECKSUM_NONE.
Herbert Xucbb042f2006-03-20 22:43:56 -08002863 */
2864unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
2865{
2866 BUG_ON(len > skb->len);
2867 skb->len -= len;
2868 BUG_ON(skb->len < skb->data_len);
2869 skb_postpull_rcsum(skb, skb->data, len);
2870 return skb->data += len;
2871}
Arnaldo Carvalho de Melof94691a2006-03-20 22:47:55 -08002872EXPORT_SYMBOL_GPL(skb_pull_rcsum);
2873
Herbert Xuf4c50d92006-06-22 03:02:40 -07002874/**
2875 * skb_segment - Perform protocol segmentation on skb.
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002876 * @head_skb: buffer to segment
Herbert Xu576a30e2006-06-27 13:22:38 -07002877 * @features: features for the output path (see dev->features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07002878 *
2879 * This function performs segmentation on the given skb. It returns
Ben Hutchings4c821d72008-04-13 21:52:48 -07002880 * a pointer to the first in a list of new skbs for the segments.
2881 * In case of error it returns ERR_PTR(err).
Herbert Xuf4c50d92006-06-22 03:02:40 -07002882 */
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002883struct sk_buff *skb_segment(struct sk_buff *head_skb,
2884 netdev_features_t features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07002885{
2886 struct sk_buff *segs = NULL;
2887 struct sk_buff *tail = NULL;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02002888 struct sk_buff *list_skb = skb_shinfo(head_skb)->frag_list;
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002889 skb_frag_t *frag = skb_shinfo(head_skb)->frags;
2890 unsigned int mss = skb_shinfo(head_skb)->gso_size;
2891 unsigned int doffset = head_skb->data - skb_mac_header(head_skb);
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02002892 struct sk_buff *frag_skb = head_skb;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002893 unsigned int offset = doffset;
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002894 unsigned int tnl_hlen = skb_tnl_header_len(head_skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002895 unsigned int headroom;
2896 unsigned int len;
Pravin B Shelarec5f0612013-03-07 09:28:01 +00002897 __be16 proto;
2898 bool csum;
Michał Mirosław04ed3e72011-01-24 15:32:47 -08002899 int sg = !!(features & NETIF_F_SG);
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002900 int nfrags = skb_shinfo(head_skb)->nr_frags;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002901 int err = -ENOMEM;
2902 int i = 0;
2903 int pos;
Vlad Yasevich53d64712014-03-27 17:26:18 -04002904 int dummy;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002905
Wei-Chun Chao5882a072014-06-08 23:48:54 -07002906 __skb_push(head_skb, doffset);
Vlad Yasevich53d64712014-03-27 17:26:18 -04002907 proto = skb_network_protocol(head_skb, &dummy);
Pravin B Shelarec5f0612013-03-07 09:28:01 +00002908 if (unlikely(!proto))
2909 return ERR_PTR(-EINVAL);
2910
Tom Herbert7e2b10c2014-06-04 17:20:02 -07002911 csum = !head_skb->encap_hdr_csum &&
2912 !!can_checksum_protocol(features, proto);
2913
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002914 headroom = skb_headroom(head_skb);
2915 pos = skb_headlen(head_skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002916
2917 do {
2918 struct sk_buff *nskb;
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02002919 skb_frag_t *nskb_frag;
Herbert Xuc8884ed2006-10-29 15:59:41 -08002920 int hsize;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002921 int size;
2922
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002923 len = head_skb->len - offset;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002924 if (len > mss)
2925 len = mss;
2926
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002927 hsize = skb_headlen(head_skb) - offset;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002928 if (hsize < 0)
2929 hsize = 0;
Herbert Xuc8884ed2006-10-29 15:59:41 -08002930 if (hsize > len || !sg)
2931 hsize = len;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002932
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02002933 if (!hsize && i >= nfrags && skb_headlen(list_skb) &&
2934 (skb_headlen(list_skb) == len || sg)) {
2935 BUG_ON(skb_headlen(list_skb) > len);
Herbert Xu89319d382008-12-15 23:26:06 -08002936
Herbert Xu9d8506c2013-11-21 11:10:04 -08002937 i = 0;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02002938 nfrags = skb_shinfo(list_skb)->nr_frags;
2939 frag = skb_shinfo(list_skb)->frags;
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02002940 frag_skb = list_skb;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02002941 pos += skb_headlen(list_skb);
Herbert Xu9d8506c2013-11-21 11:10:04 -08002942
2943 while (pos < offset + len) {
2944 BUG_ON(i >= nfrags);
2945
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02002946 size = skb_frag_size(frag);
Herbert Xu9d8506c2013-11-21 11:10:04 -08002947 if (pos + size > offset + len)
2948 break;
2949
2950 i++;
2951 pos += size;
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02002952 frag++;
Herbert Xu9d8506c2013-11-21 11:10:04 -08002953 }
2954
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02002955 nskb = skb_clone(list_skb, GFP_ATOMIC);
2956 list_skb = list_skb->next;
Herbert Xu89319d382008-12-15 23:26:06 -08002957
2958 if (unlikely(!nskb))
2959 goto err;
2960
Herbert Xu9d8506c2013-11-21 11:10:04 -08002961 if (unlikely(pskb_trim(nskb, len))) {
2962 kfree_skb(nskb);
2963 goto err;
2964 }
2965
Alexander Duyckec47ea82012-05-04 14:26:56 +00002966 hsize = skb_end_offset(nskb);
Herbert Xu89319d382008-12-15 23:26:06 -08002967 if (skb_cow_head(nskb, doffset + headroom)) {
2968 kfree_skb(nskb);
2969 goto err;
2970 }
2971
Alexander Duyckec47ea82012-05-04 14:26:56 +00002972 nskb->truesize += skb_end_offset(nskb) - hsize;
Herbert Xu89319d382008-12-15 23:26:06 -08002973 skb_release_head_state(nskb);
2974 __skb_push(nskb, doffset);
2975 } else {
Mel Gormanc93bdd02012-07-31 16:44:19 -07002976 nskb = __alloc_skb(hsize + doffset + headroom,
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002977 GFP_ATOMIC, skb_alloc_rx_flag(head_skb),
Mel Gormanc93bdd02012-07-31 16:44:19 -07002978 NUMA_NO_NODE);
Herbert Xu89319d382008-12-15 23:26:06 -08002979
2980 if (unlikely(!nskb))
2981 goto err;
2982
2983 skb_reserve(nskb, headroom);
2984 __skb_put(nskb, doffset);
2985 }
Herbert Xuf4c50d92006-06-22 03:02:40 -07002986
2987 if (segs)
2988 tail->next = nskb;
2989 else
2990 segs = nskb;
2991 tail = nskb;
2992
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002993 __copy_skb_header(nskb, head_skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002994
Eric Dumazet030737b2013-10-19 11:42:54 -07002995 skb_headers_offset_update(nskb, skb_headroom(nskb) - headroom);
Vlad Yasevichfcdfe3a2014-07-31 10:33:06 -04002996 skb_reset_mac_len(nskb);
Pravin B Shelar68c33162013-02-14 14:02:41 +00002997
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002998 skb_copy_from_linear_data_offset(head_skb, -tnl_hlen,
Pravin B Shelar68c33162013-02-14 14:02:41 +00002999 nskb->data - tnl_hlen,
3000 doffset + tnl_hlen);
Herbert Xu89319d382008-12-15 23:26:06 -08003001
Herbert Xu9d8506c2013-11-21 11:10:04 -08003002 if (nskb->len == len + doffset)
Simon Horman1cdbcb72013-05-19 15:46:49 +00003003 goto perform_csum_check;
Herbert Xu89319d382008-12-15 23:26:06 -08003004
Herbert Xuf4c50d92006-06-22 03:02:40 -07003005 if (!sg) {
Herbert Xu6f85a122008-08-15 14:55:02 -07003006 nskb->ip_summed = CHECKSUM_NONE;
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003007 nskb->csum = skb_copy_and_csum_bits(head_skb, offset,
Herbert Xuf4c50d92006-06-22 03:02:40 -07003008 skb_put(nskb, len),
3009 len, 0);
Tom Herbert7e2b10c2014-06-04 17:20:02 -07003010 SKB_GSO_CB(nskb)->csum_start =
Tom Herbertde843722014-06-25 12:51:01 -07003011 skb_headroom(nskb) + doffset;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003012 continue;
3013 }
3014
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003015 nskb_frag = skb_shinfo(nskb)->frags;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003016
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003017 skb_copy_from_linear_data_offset(head_skb, offset,
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03003018 skb_put(nskb, hsize), hsize);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003019
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003020 skb_shinfo(nskb)->tx_flags = skb_shinfo(head_skb)->tx_flags &
3021 SKBTX_SHARED_FRAG;
Eric Dumazetcef401d2013-01-25 20:34:37 +00003022
Herbert Xu9d8506c2013-11-21 11:10:04 -08003023 while (pos < offset + len) {
3024 if (i >= nfrags) {
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003025 BUG_ON(skb_headlen(list_skb));
Herbert Xu9d8506c2013-11-21 11:10:04 -08003026
3027 i = 0;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003028 nfrags = skb_shinfo(list_skb)->nr_frags;
3029 frag = skb_shinfo(list_skb)->frags;
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02003030 frag_skb = list_skb;
Herbert Xu9d8506c2013-11-21 11:10:04 -08003031
3032 BUG_ON(!nfrags);
3033
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003034 list_skb = list_skb->next;
Herbert Xu9d8506c2013-11-21 11:10:04 -08003035 }
3036
3037 if (unlikely(skb_shinfo(nskb)->nr_frags >=
3038 MAX_SKB_FRAGS)) {
3039 net_warn_ratelimited(
3040 "skb_segment: too many frags: %u %u\n",
3041 pos, mss);
3042 goto err;
3043 }
3044
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02003045 if (unlikely(skb_orphan_frags(frag_skb, GFP_ATOMIC)))
3046 goto err;
3047
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02003048 *nskb_frag = *frag;
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003049 __skb_frag_ref(nskb_frag);
3050 size = skb_frag_size(nskb_frag);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003051
3052 if (pos < offset) {
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003053 nskb_frag->page_offset += offset - pos;
3054 skb_frag_size_sub(nskb_frag, offset - pos);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003055 }
3056
Herbert Xu89319d382008-12-15 23:26:06 -08003057 skb_shinfo(nskb)->nr_frags++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003058
3059 if (pos + size <= offset + len) {
3060 i++;
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02003061 frag++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003062 pos += size;
3063 } else {
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003064 skb_frag_size_sub(nskb_frag, pos + size - (offset + len));
Herbert Xu89319d382008-12-15 23:26:06 -08003065 goto skip_fraglist;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003066 }
3067
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003068 nskb_frag++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003069 }
3070
Herbert Xu89319d382008-12-15 23:26:06 -08003071skip_fraglist:
Herbert Xuf4c50d92006-06-22 03:02:40 -07003072 nskb->data_len = len - hsize;
3073 nskb->len += nskb->data_len;
3074 nskb->truesize += nskb->data_len;
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003075
Simon Horman1cdbcb72013-05-19 15:46:49 +00003076perform_csum_check:
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003077 if (!csum) {
3078 nskb->csum = skb_checksum(nskb, doffset,
3079 nskb->len - doffset, 0);
3080 nskb->ip_summed = CHECKSUM_NONE;
Tom Herbert7e2b10c2014-06-04 17:20:02 -07003081 SKB_GSO_CB(nskb)->csum_start =
3082 skb_headroom(nskb) + doffset;
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003083 }
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003084 } while ((offset += len) < head_skb->len);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003085
Eric Dumazetbec3cfd2014-10-03 20:59:19 -07003086 /* Some callers want to get the end of the list.
3087 * Put it in segs->prev to avoid walking the list.
3088 * (see validate_xmit_skb_list() for example)
3089 */
3090 segs->prev = tail;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003091 return segs;
3092
3093err:
Eric Dumazet289dccb2013-12-20 14:29:08 -08003094 kfree_skb_list(segs);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003095 return ERR_PTR(err);
3096}
Herbert Xuf4c50d92006-06-22 03:02:40 -07003097EXPORT_SYMBOL_GPL(skb_segment);
3098
Herbert Xu71d93b32008-12-15 23:42:33 -08003099int skb_gro_receive(struct sk_buff **head, struct sk_buff *skb)
3100{
Eric Dumazet8a291112013-10-08 09:02:23 -07003101 struct skb_shared_info *pinfo, *skbinfo = skb_shinfo(skb);
Herbert Xu67147ba2009-05-26 18:50:22 +00003102 unsigned int offset = skb_gro_offset(skb);
3103 unsigned int headlen = skb_headlen(skb);
Eric Dumazet8a291112013-10-08 09:02:23 -07003104 struct sk_buff *nskb, *lp, *p = *head;
3105 unsigned int len = skb_gro_len(skb);
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003106 unsigned int delta_truesize;
Eric Dumazet8a291112013-10-08 09:02:23 -07003107 unsigned int headroom;
Herbert Xu71d93b32008-12-15 23:42:33 -08003108
Eric Dumazet8a291112013-10-08 09:02:23 -07003109 if (unlikely(p->len + len >= 65536))
Herbert Xu71d93b32008-12-15 23:42:33 -08003110 return -E2BIG;
3111
Eric Dumazet29e98242014-05-16 11:34:37 -07003112 lp = NAPI_GRO_CB(p)->last;
Eric Dumazet8a291112013-10-08 09:02:23 -07003113 pinfo = skb_shinfo(lp);
3114
3115 if (headlen <= offset) {
Herbert Xu42da6992009-05-26 18:50:19 +00003116 skb_frag_t *frag;
Herbert Xu66e92fc2009-05-26 18:50:32 +00003117 skb_frag_t *frag2;
Herbert Xu9aaa1562009-05-26 18:50:33 +00003118 int i = skbinfo->nr_frags;
3119 int nr_frags = pinfo->nr_frags + i;
Herbert Xu42da6992009-05-26 18:50:19 +00003120
Herbert Xu66e92fc2009-05-26 18:50:32 +00003121 if (nr_frags > MAX_SKB_FRAGS)
Eric Dumazet8a291112013-10-08 09:02:23 -07003122 goto merge;
Herbert Xu81705ad2009-01-29 14:19:51 +00003123
Eric Dumazet8a291112013-10-08 09:02:23 -07003124 offset -= headlen;
Herbert Xu9aaa1562009-05-26 18:50:33 +00003125 pinfo->nr_frags = nr_frags;
3126 skbinfo->nr_frags = 0;
Herbert Xuf5572062009-01-14 20:40:03 -08003127
Herbert Xu9aaa1562009-05-26 18:50:33 +00003128 frag = pinfo->frags + nr_frags;
3129 frag2 = skbinfo->frags + i;
Herbert Xu66e92fc2009-05-26 18:50:32 +00003130 do {
3131 *--frag = *--frag2;
3132 } while (--i);
3133
3134 frag->page_offset += offset;
Eric Dumazet9e903e02011-10-18 21:00:24 +00003135 skb_frag_size_sub(frag, offset);
Herbert Xu66e92fc2009-05-26 18:50:32 +00003136
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003137 /* all fragments truesize : remove (head size + sk_buff) */
Alexander Duyckec47ea82012-05-04 14:26:56 +00003138 delta_truesize = skb->truesize -
3139 SKB_TRUESIZE(skb_end_offset(skb));
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003140
Herbert Xuf5572062009-01-14 20:40:03 -08003141 skb->truesize -= skb->data_len;
3142 skb->len -= skb->data_len;
3143 skb->data_len = 0;
3144
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003145 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE;
Herbert Xu5d38a072009-01-04 16:13:40 -08003146 goto done;
Eric Dumazetd7e88832012-04-30 08:10:34 +00003147 } else if (skb->head_frag) {
3148 int nr_frags = pinfo->nr_frags;
3149 skb_frag_t *frag = pinfo->frags + nr_frags;
3150 struct page *page = virt_to_head_page(skb->head);
3151 unsigned int first_size = headlen - offset;
3152 unsigned int first_offset;
3153
3154 if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS)
Eric Dumazet8a291112013-10-08 09:02:23 -07003155 goto merge;
Eric Dumazetd7e88832012-04-30 08:10:34 +00003156
3157 first_offset = skb->data -
3158 (unsigned char *)page_address(page) +
3159 offset;
3160
3161 pinfo->nr_frags = nr_frags + 1 + skbinfo->nr_frags;
3162
3163 frag->page.p = page;
3164 frag->page_offset = first_offset;
3165 skb_frag_size_set(frag, first_size);
3166
3167 memcpy(frag + 1, skbinfo->frags, sizeof(*frag) * skbinfo->nr_frags);
3168 /* We dont need to clear skbinfo->nr_frags here */
3169
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003170 delta_truesize = skb->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
Eric Dumazetd7e88832012-04-30 08:10:34 +00003171 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD;
3172 goto done;
Eric Dumazet8a291112013-10-08 09:02:23 -07003173 }
Eric Dumazet73d3fe62014-09-29 10:34:29 -07003174 /* switch back to head shinfo */
3175 pinfo = skb_shinfo(p);
3176
Eric Dumazet8a291112013-10-08 09:02:23 -07003177 if (pinfo->frag_list)
3178 goto merge;
3179 if (skb_gro_len(p) != pinfo->gso_size)
Herbert Xu69c0cab2009-11-17 05:18:18 -08003180 return -E2BIG;
Herbert Xu71d93b32008-12-15 23:42:33 -08003181
3182 headroom = skb_headroom(p);
Eric Dumazet3d3be432010-09-01 00:50:51 +00003183 nskb = alloc_skb(headroom + skb_gro_offset(p), GFP_ATOMIC);
Herbert Xu71d93b32008-12-15 23:42:33 -08003184 if (unlikely(!nskb))
3185 return -ENOMEM;
3186
3187 __copy_skb_header(nskb, p);
3188 nskb->mac_len = p->mac_len;
3189
3190 skb_reserve(nskb, headroom);
Herbert Xu86911732009-01-29 14:19:50 +00003191 __skb_put(nskb, skb_gro_offset(p));
Herbert Xu71d93b32008-12-15 23:42:33 -08003192
Herbert Xu86911732009-01-29 14:19:50 +00003193 skb_set_mac_header(nskb, skb_mac_header(p) - p->data);
Herbert Xu71d93b32008-12-15 23:42:33 -08003194 skb_set_network_header(nskb, skb_network_offset(p));
3195 skb_set_transport_header(nskb, skb_transport_offset(p));
3196
Herbert Xu86911732009-01-29 14:19:50 +00003197 __skb_pull(p, skb_gro_offset(p));
3198 memcpy(skb_mac_header(nskb), skb_mac_header(p),
3199 p->data - skb_mac_header(p));
Herbert Xu71d93b32008-12-15 23:42:33 -08003200
Herbert Xu71d93b32008-12-15 23:42:33 -08003201 skb_shinfo(nskb)->frag_list = p;
Herbert Xu9aaa1562009-05-26 18:50:33 +00003202 skb_shinfo(nskb)->gso_size = pinfo->gso_size;
Herbert Xu622e0ca2010-05-20 23:07:56 -07003203 pinfo->gso_size = 0;
Eric Dumazetf4a775d2014-09-22 16:29:32 -07003204 __skb_header_release(p);
Eric Dumazetc3c7c252012-12-06 13:54:59 +00003205 NAPI_GRO_CB(nskb)->last = p;
Herbert Xu71d93b32008-12-15 23:42:33 -08003206
3207 nskb->data_len += p->len;
Eric Dumazetde8261c2012-02-13 04:09:20 +00003208 nskb->truesize += p->truesize;
Herbert Xu71d93b32008-12-15 23:42:33 -08003209 nskb->len += p->len;
3210
3211 *head = nskb;
3212 nskb->next = p->next;
3213 p->next = NULL;
3214
3215 p = nskb;
3216
3217merge:
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003218 delta_truesize = skb->truesize;
Herbert Xu67147ba2009-05-26 18:50:22 +00003219 if (offset > headlen) {
Michal Schmidtd1dc7ab2011-01-24 12:08:48 +00003220 unsigned int eat = offset - headlen;
3221
3222 skbinfo->frags[0].page_offset += eat;
Eric Dumazet9e903e02011-10-18 21:00:24 +00003223 skb_frag_size_sub(&skbinfo->frags[0], eat);
Michal Schmidtd1dc7ab2011-01-24 12:08:48 +00003224 skb->data_len -= eat;
3225 skb->len -= eat;
Herbert Xu67147ba2009-05-26 18:50:22 +00003226 offset = headlen;
Herbert Xu56035022009-02-05 21:26:52 -08003227 }
3228
Herbert Xu67147ba2009-05-26 18:50:22 +00003229 __skb_pull(skb, offset);
Herbert Xu56035022009-02-05 21:26:52 -08003230
Eric Dumazet29e98242014-05-16 11:34:37 -07003231 if (NAPI_GRO_CB(p)->last == p)
Eric Dumazet8a291112013-10-08 09:02:23 -07003232 skb_shinfo(p)->frag_list = skb;
3233 else
3234 NAPI_GRO_CB(p)->last->next = skb;
Eric Dumazetc3c7c252012-12-06 13:54:59 +00003235 NAPI_GRO_CB(p)->last = skb;
Eric Dumazetf4a775d2014-09-22 16:29:32 -07003236 __skb_header_release(skb);
Eric Dumazet8a291112013-10-08 09:02:23 -07003237 lp = p;
Herbert Xu71d93b32008-12-15 23:42:33 -08003238
Herbert Xu5d38a072009-01-04 16:13:40 -08003239done:
3240 NAPI_GRO_CB(p)->count++;
Herbert Xu37fe4732009-01-17 19:48:13 +00003241 p->data_len += len;
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003242 p->truesize += delta_truesize;
Herbert Xu37fe4732009-01-17 19:48:13 +00003243 p->len += len;
Eric Dumazet8a291112013-10-08 09:02:23 -07003244 if (lp != p) {
3245 lp->data_len += len;
3246 lp->truesize += delta_truesize;
3247 lp->len += len;
3248 }
Herbert Xu71d93b32008-12-15 23:42:33 -08003249 NAPI_GRO_CB(skb)->same_flow = 1;
3250 return 0;
3251}
Herbert Xu71d93b32008-12-15 23:42:33 -08003252
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253void __init skb_init(void)
3254{
3255 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
3256 sizeof(struct sk_buff),
3257 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07003258 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09003259 NULL);
David S. Millerd179cd12005-08-17 14:57:30 -07003260 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
Eric Dumazetd0bf4a92014-09-29 13:29:15 -07003261 sizeof(struct sk_buff_fclones),
David S. Millerd179cd12005-08-17 14:57:30 -07003262 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07003263 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09003264 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265}
3266
David Howells716ea3a2007-04-02 20:19:53 -07003267/**
3268 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
3269 * @skb: Socket buffer containing the buffers to be mapped
3270 * @sg: The scatter-gather list to map into
3271 * @offset: The offset into the buffer's contents to start mapping
3272 * @len: Length of buffer space to be mapped
3273 *
3274 * Fill the specified scatter-gather list with mappings/pointers into a
3275 * region of the buffer space attached to a socket buffer.
3276 */
David S. Miller51c739d2007-10-30 21:29:29 -07003277static int
3278__skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
David Howells716ea3a2007-04-02 20:19:53 -07003279{
David S. Miller1a028e52007-04-27 15:21:23 -07003280 int start = skb_headlen(skb);
3281 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07003282 struct sk_buff *frag_iter;
David Howells716ea3a2007-04-02 20:19:53 -07003283 int elt = 0;
3284
3285 if (copy > 0) {
3286 if (copy > len)
3287 copy = len;
Jens Axboe642f1492007-10-24 11:20:47 +02003288 sg_set_buf(sg, skb->data + offset, copy);
David Howells716ea3a2007-04-02 20:19:53 -07003289 elt++;
3290 if ((len -= copy) == 0)
3291 return elt;
3292 offset += copy;
3293 }
3294
3295 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07003296 int end;
David Howells716ea3a2007-04-02 20:19:53 -07003297
Ilpo Järvinen547b7922008-07-25 21:43:18 -07003298 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07003299
Eric Dumazet9e903e02011-10-18 21:00:24 +00003300 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
David Howells716ea3a2007-04-02 20:19:53 -07003301 if ((copy = end - offset) > 0) {
3302 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
3303
3304 if (copy > len)
3305 copy = len;
Ian Campbellea2ab692011-08-22 23:44:58 +00003306 sg_set_page(&sg[elt], skb_frag_page(frag), copy,
Jens Axboe642f1492007-10-24 11:20:47 +02003307 frag->page_offset+offset-start);
David Howells716ea3a2007-04-02 20:19:53 -07003308 elt++;
3309 if (!(len -= copy))
3310 return elt;
3311 offset += copy;
3312 }
David S. Miller1a028e52007-04-27 15:21:23 -07003313 start = end;
David Howells716ea3a2007-04-02 20:19:53 -07003314 }
3315
David S. Millerfbb398a2009-06-09 00:18:59 -07003316 skb_walk_frags(skb, frag_iter) {
3317 int end;
David Howells716ea3a2007-04-02 20:19:53 -07003318
David S. Millerfbb398a2009-06-09 00:18:59 -07003319 WARN_ON(start > offset + len);
David Howells716ea3a2007-04-02 20:19:53 -07003320
David S. Millerfbb398a2009-06-09 00:18:59 -07003321 end = start + frag_iter->len;
3322 if ((copy = end - offset) > 0) {
3323 if (copy > len)
3324 copy = len;
3325 elt += __skb_to_sgvec(frag_iter, sg+elt, offset - start,
3326 copy);
3327 if ((len -= copy) == 0)
3328 return elt;
3329 offset += copy;
David Howells716ea3a2007-04-02 20:19:53 -07003330 }
David S. Millerfbb398a2009-06-09 00:18:59 -07003331 start = end;
David Howells716ea3a2007-04-02 20:19:53 -07003332 }
3333 BUG_ON(len);
3334 return elt;
3335}
3336
Fan Du25a91d82014-01-18 09:54:23 +08003337/* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given
3338 * sglist without mark the sg which contain last skb data as the end.
3339 * So the caller can mannipulate sg list as will when padding new data after
3340 * the first call without calling sg_unmark_end to expend sg list.
3341 *
3342 * Scenario to use skb_to_sgvec_nomark:
3343 * 1. sg_init_table
3344 * 2. skb_to_sgvec_nomark(payload1)
3345 * 3. skb_to_sgvec_nomark(payload2)
3346 *
3347 * This is equivalent to:
3348 * 1. sg_init_table
3349 * 2. skb_to_sgvec(payload1)
3350 * 3. sg_unmark_end
3351 * 4. skb_to_sgvec(payload2)
3352 *
3353 * When mapping mutilple payload conditionally, skb_to_sgvec_nomark
3354 * is more preferable.
3355 */
3356int skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg,
3357 int offset, int len)
3358{
3359 return __skb_to_sgvec(skb, sg, offset, len);
3360}
3361EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark);
3362
David S. Miller51c739d2007-10-30 21:29:29 -07003363int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
3364{
3365 int nsg = __skb_to_sgvec(skb, sg, offset, len);
3366
Jens Axboec46f2332007-10-31 12:06:37 +01003367 sg_mark_end(&sg[nsg - 1]);
David S. Miller51c739d2007-10-30 21:29:29 -07003368
3369 return nsg;
3370}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003371EXPORT_SYMBOL_GPL(skb_to_sgvec);
David S. Miller51c739d2007-10-30 21:29:29 -07003372
David Howells716ea3a2007-04-02 20:19:53 -07003373/**
3374 * skb_cow_data - Check that a socket buffer's data buffers are writable
3375 * @skb: The socket buffer to check.
3376 * @tailbits: Amount of trailing space to be added
3377 * @trailer: Returned pointer to the skb where the @tailbits space begins
3378 *
3379 * Make sure that the data buffers attached to a socket buffer are
3380 * writable. If they are not, private copies are made of the data buffers
3381 * and the socket buffer is set to use these instead.
3382 *
3383 * If @tailbits is given, make sure that there is space to write @tailbits
3384 * bytes of data beyond current end of socket buffer. @trailer will be
3385 * set to point to the skb in which this space begins.
3386 *
3387 * The number of scatterlist elements required to completely map the
3388 * COW'd and extended socket buffer will be returned.
3389 */
3390int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
3391{
3392 int copyflag;
3393 int elt;
3394 struct sk_buff *skb1, **skb_p;
3395
3396 /* If skb is cloned or its head is paged, reallocate
3397 * head pulling out all the pages (pages are considered not writable
3398 * at the moment even if they are anonymous).
3399 */
3400 if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
3401 __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
3402 return -ENOMEM;
3403
3404 /* Easy case. Most of packets will go this way. */
David S. Miller21dc3302010-08-23 00:13:46 -07003405 if (!skb_has_frag_list(skb)) {
David Howells716ea3a2007-04-02 20:19:53 -07003406 /* A little of trouble, not enough of space for trailer.
3407 * This should not happen, when stack is tuned to generate
3408 * good frames. OK, on miss we reallocate and reserve even more
3409 * space, 128 bytes is fair. */
3410
3411 if (skb_tailroom(skb) < tailbits &&
3412 pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
3413 return -ENOMEM;
3414
3415 /* Voila! */
3416 *trailer = skb;
3417 return 1;
3418 }
3419
3420 /* Misery. We are in troubles, going to mincer fragments... */
3421
3422 elt = 1;
3423 skb_p = &skb_shinfo(skb)->frag_list;
3424 copyflag = 0;
3425
3426 while ((skb1 = *skb_p) != NULL) {
3427 int ntail = 0;
3428
3429 /* The fragment is partially pulled by someone,
3430 * this can happen on input. Copy it and everything
3431 * after it. */
3432
3433 if (skb_shared(skb1))
3434 copyflag = 1;
3435
3436 /* If the skb is the last, worry about trailer. */
3437
3438 if (skb1->next == NULL && tailbits) {
3439 if (skb_shinfo(skb1)->nr_frags ||
David S. Miller21dc3302010-08-23 00:13:46 -07003440 skb_has_frag_list(skb1) ||
David Howells716ea3a2007-04-02 20:19:53 -07003441 skb_tailroom(skb1) < tailbits)
3442 ntail = tailbits + 128;
3443 }
3444
3445 if (copyflag ||
3446 skb_cloned(skb1) ||
3447 ntail ||
3448 skb_shinfo(skb1)->nr_frags ||
David S. Miller21dc3302010-08-23 00:13:46 -07003449 skb_has_frag_list(skb1)) {
David Howells716ea3a2007-04-02 20:19:53 -07003450 struct sk_buff *skb2;
3451
3452 /* Fuck, we are miserable poor guys... */
3453 if (ntail == 0)
3454 skb2 = skb_copy(skb1, GFP_ATOMIC);
3455 else
3456 skb2 = skb_copy_expand(skb1,
3457 skb_headroom(skb1),
3458 ntail,
3459 GFP_ATOMIC);
3460 if (unlikely(skb2 == NULL))
3461 return -ENOMEM;
3462
3463 if (skb1->sk)
3464 skb_set_owner_w(skb2, skb1->sk);
3465
3466 /* Looking around. Are we still alive?
3467 * OK, link new skb, drop old one */
3468
3469 skb2->next = skb1->next;
3470 *skb_p = skb2;
3471 kfree_skb(skb1);
3472 skb1 = skb2;
3473 }
3474 elt++;
3475 *trailer = skb1;
3476 skb_p = &skb1->next;
3477 }
3478
3479 return elt;
3480}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003481EXPORT_SYMBOL_GPL(skb_cow_data);
David Howells716ea3a2007-04-02 20:19:53 -07003482
Eric Dumazetb1faf562010-05-31 23:44:05 -07003483static void sock_rmem_free(struct sk_buff *skb)
3484{
3485 struct sock *sk = skb->sk;
3486
3487 atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
3488}
3489
3490/*
3491 * Note: We dont mem charge error packets (no sk_forward_alloc changes)
3492 */
3493int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb)
3494{
3495 if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
Eric Dumazet95c96172012-04-15 05:58:06 +00003496 (unsigned int)sk->sk_rcvbuf)
Eric Dumazetb1faf562010-05-31 23:44:05 -07003497 return -ENOMEM;
3498
3499 skb_orphan(skb);
3500 skb->sk = sk;
3501 skb->destructor = sock_rmem_free;
3502 atomic_add(skb->truesize, &sk->sk_rmem_alloc);
3503
Eric Dumazetabb57ea2011-05-18 02:21:31 -04003504 /* before exiting rcu section, make sure dst is refcounted */
3505 skb_dst_force(skb);
3506
Eric Dumazetb1faf562010-05-31 23:44:05 -07003507 skb_queue_tail(&sk->sk_error_queue, skb);
3508 if (!sock_flag(sk, SOCK_DEAD))
David S. Miller676d2362014-04-11 16:15:36 -04003509 sk->sk_data_ready(sk);
Eric Dumazetb1faf562010-05-31 23:44:05 -07003510 return 0;
3511}
3512EXPORT_SYMBOL(sock_queue_err_skb);
3513
Willem de Bruijn364a9e92014-08-31 21:30:27 -04003514struct sk_buff *sock_dequeue_err_skb(struct sock *sk)
3515{
3516 struct sk_buff_head *q = &sk->sk_error_queue;
3517 struct sk_buff *skb, *skb_next;
3518 int err = 0;
3519
3520 spin_lock_bh(&q->lock);
3521 skb = __skb_dequeue(q);
3522 if (skb && (skb_next = skb_peek(q)))
3523 err = SKB_EXT_ERR(skb_next)->ee.ee_errno;
3524 spin_unlock_bh(&q->lock);
3525
3526 sk->sk_err = err;
3527 if (err)
3528 sk->sk_error_report(sk);
3529
3530 return skb;
3531}
3532EXPORT_SYMBOL(sock_dequeue_err_skb);
3533
Alexander Duyckcab41c42014-09-10 18:05:26 -04003534/**
3535 * skb_clone_sk - create clone of skb, and take reference to socket
3536 * @skb: the skb to clone
3537 *
3538 * This function creates a clone of a buffer that holds a reference on
3539 * sk_refcnt. Buffers created via this function are meant to be
3540 * returned using sock_queue_err_skb, or free via kfree_skb.
3541 *
3542 * When passing buffers allocated with this function to sock_queue_err_skb
3543 * it is necessary to wrap the call with sock_hold/sock_put in order to
3544 * prevent the socket from being released prior to being enqueued on
3545 * the sk_error_queue.
3546 */
Alexander Duyck62bccb82014-09-04 13:31:35 -04003547struct sk_buff *skb_clone_sk(struct sk_buff *skb)
3548{
3549 struct sock *sk = skb->sk;
3550 struct sk_buff *clone;
3551
3552 if (!sk || !atomic_inc_not_zero(&sk->sk_refcnt))
3553 return NULL;
3554
3555 clone = skb_clone(skb, GFP_ATOMIC);
3556 if (!clone) {
3557 sock_put(sk);
3558 return NULL;
3559 }
3560
3561 clone->sk = sk;
3562 clone->destructor = sock_efree;
3563
3564 return clone;
3565}
3566EXPORT_SYMBOL(skb_clone_sk);
3567
Alexander Duyck37846ef2014-09-04 13:31:10 -04003568static void __skb_complete_tx_timestamp(struct sk_buff *skb,
3569 struct sock *sk,
3570 int tstype)
Patrick Ohlyac45f602009-02-12 05:03:37 +00003571{
Patrick Ohlyac45f602009-02-12 05:03:37 +00003572 struct sock_exterr_skb *serr;
Patrick Ohlyac45f602009-02-12 05:03:37 +00003573 int err;
3574
Patrick Ohlyac45f602009-02-12 05:03:37 +00003575 serr = SKB_EXT_ERR(skb);
3576 memset(serr, 0, sizeof(*serr));
3577 serr->ee.ee_errno = ENOMSG;
3578 serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
Willem de Bruijne7fd2882014-08-04 22:11:48 -04003579 serr->ee.ee_info = tstype;
Willem de Bruijn4ed2d762014-08-04 22:11:49 -04003580 if (sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) {
Willem de Bruijn09c2d252014-08-04 22:11:47 -04003581 serr->ee.ee_data = skb_shinfo(skb)->tskey;
Willem de Bruijn4ed2d762014-08-04 22:11:49 -04003582 if (sk->sk_protocol == IPPROTO_TCP)
3583 serr->ee.ee_data -= sk->sk_tskey;
3584 }
Eric Dumazet29030372010-05-29 00:20:48 -07003585
Patrick Ohlyac45f602009-02-12 05:03:37 +00003586 err = sock_queue_err_skb(sk, skb);
Eric Dumazet29030372010-05-29 00:20:48 -07003587
Patrick Ohlyac45f602009-02-12 05:03:37 +00003588 if (err)
3589 kfree_skb(skb);
3590}
Alexander Duyck37846ef2014-09-04 13:31:10 -04003591
3592void skb_complete_tx_timestamp(struct sk_buff *skb,
3593 struct skb_shared_hwtstamps *hwtstamps)
3594{
3595 struct sock *sk = skb->sk;
3596
Alexander Duyck62bccb82014-09-04 13:31:35 -04003597 /* take a reference to prevent skb_orphan() from freeing the socket */
3598 sock_hold(sk);
Alexander Duyck37846ef2014-09-04 13:31:10 -04003599
Alexander Duyck62bccb82014-09-04 13:31:35 -04003600 *skb_hwtstamps(skb) = *hwtstamps;
3601 __skb_complete_tx_timestamp(skb, sk, SCM_TSTAMP_SND);
Alexander Duyck37846ef2014-09-04 13:31:10 -04003602
3603 sock_put(sk);
3604}
3605EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp);
3606
3607void __skb_tstamp_tx(struct sk_buff *orig_skb,
3608 struct skb_shared_hwtstamps *hwtstamps,
3609 struct sock *sk, int tstype)
3610{
3611 struct sk_buff *skb;
3612
3613 if (!sk)
3614 return;
3615
3616 if (hwtstamps)
3617 *skb_hwtstamps(orig_skb) = *hwtstamps;
3618 else
3619 orig_skb->tstamp = ktime_get_real();
3620
3621 skb = skb_clone(orig_skb, GFP_ATOMIC);
3622 if (!skb)
3623 return;
3624
3625 __skb_complete_tx_timestamp(skb, sk, tstype);
3626}
Willem de Bruijne7fd2882014-08-04 22:11:48 -04003627EXPORT_SYMBOL_GPL(__skb_tstamp_tx);
3628
3629void skb_tstamp_tx(struct sk_buff *orig_skb,
3630 struct skb_shared_hwtstamps *hwtstamps)
3631{
3632 return __skb_tstamp_tx(orig_skb, hwtstamps, orig_skb->sk,
3633 SCM_TSTAMP_SND);
3634}
Patrick Ohlyac45f602009-02-12 05:03:37 +00003635EXPORT_SYMBOL_GPL(skb_tstamp_tx);
3636
Johannes Berg6e3e9392011-11-09 10:15:42 +01003637void skb_complete_wifi_ack(struct sk_buff *skb, bool acked)
3638{
3639 struct sock *sk = skb->sk;
3640 struct sock_exterr_skb *serr;
3641 int err;
3642
3643 skb->wifi_acked_valid = 1;
3644 skb->wifi_acked = acked;
3645
3646 serr = SKB_EXT_ERR(skb);
3647 memset(serr, 0, sizeof(*serr));
3648 serr->ee.ee_errno = ENOMSG;
3649 serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS;
3650
Alexander Duyckbf7fa552014-09-10 18:05:42 -04003651 /* take a reference to prevent skb_orphan() from freeing the socket */
3652 sock_hold(sk);
3653
Johannes Berg6e3e9392011-11-09 10:15:42 +01003654 err = sock_queue_err_skb(sk, skb);
3655 if (err)
3656 kfree_skb(skb);
Alexander Duyckbf7fa552014-09-10 18:05:42 -04003657
3658 sock_put(sk);
Johannes Berg6e3e9392011-11-09 10:15:42 +01003659}
3660EXPORT_SYMBOL_GPL(skb_complete_wifi_ack);
3661
Patrick Ohlyac45f602009-02-12 05:03:37 +00003662
Rusty Russellf35d9d82008-02-04 23:49:54 -05003663/**
3664 * skb_partial_csum_set - set up and verify partial csum values for packet
3665 * @skb: the skb to set
3666 * @start: the number of bytes after skb->data to start checksumming.
3667 * @off: the offset from start to place the checksum.
3668 *
3669 * For untrusted partially-checksummed packets, we need to make sure the values
3670 * for skb->csum_start and skb->csum_offset are valid so we don't oops.
3671 *
3672 * This function checks and sets those values and skb->ip_summed: if this
3673 * returns false you should drop the packet.
3674 */
3675bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
3676{
Herbert Xu5ff8dda2009-06-04 01:22:01 +00003677 if (unlikely(start > skb_headlen(skb)) ||
3678 unlikely((int)start + off > skb_headlen(skb) - 2)) {
Joe Perchese87cc472012-05-13 21:56:26 +00003679 net_warn_ratelimited("bad partial csum: csum=%u/%u len=%u\n",
3680 start, off, skb_headlen(skb));
Rusty Russellf35d9d82008-02-04 23:49:54 -05003681 return false;
3682 }
3683 skb->ip_summed = CHECKSUM_PARTIAL;
3684 skb->csum_start = skb_headroom(skb) + start;
3685 skb->csum_offset = off;
Jason Wange5d5dec2013-03-26 23:11:20 +00003686 skb_set_transport_header(skb, start);
Rusty Russellf35d9d82008-02-04 23:49:54 -05003687 return true;
3688}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003689EXPORT_SYMBOL_GPL(skb_partial_csum_set);
Rusty Russellf35d9d82008-02-04 23:49:54 -05003690
Paul Durranted1f50c2014-01-09 10:02:46 +00003691static int skb_maybe_pull_tail(struct sk_buff *skb, unsigned int len,
3692 unsigned int max)
3693{
3694 if (skb_headlen(skb) >= len)
3695 return 0;
3696
3697 /* If we need to pullup then pullup to the max, so we
3698 * won't need to do it again.
3699 */
3700 if (max > skb->len)
3701 max = skb->len;
3702
3703 if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL)
3704 return -ENOMEM;
3705
3706 if (skb_headlen(skb) < len)
3707 return -EPROTO;
3708
3709 return 0;
3710}
3711
Jan Beulichf9708b42014-03-11 13:56:05 +00003712#define MAX_TCP_HDR_LEN (15 * 4)
3713
3714static __sum16 *skb_checksum_setup_ip(struct sk_buff *skb,
3715 typeof(IPPROTO_IP) proto,
3716 unsigned int off)
3717{
3718 switch (proto) {
3719 int err;
3720
3721 case IPPROTO_TCP:
3722 err = skb_maybe_pull_tail(skb, off + sizeof(struct tcphdr),
3723 off + MAX_TCP_HDR_LEN);
3724 if (!err && !skb_partial_csum_set(skb, off,
3725 offsetof(struct tcphdr,
3726 check)))
3727 err = -EPROTO;
3728 return err ? ERR_PTR(err) : &tcp_hdr(skb)->check;
3729
3730 case IPPROTO_UDP:
3731 err = skb_maybe_pull_tail(skb, off + sizeof(struct udphdr),
3732 off + sizeof(struct udphdr));
3733 if (!err && !skb_partial_csum_set(skb, off,
3734 offsetof(struct udphdr,
3735 check)))
3736 err = -EPROTO;
3737 return err ? ERR_PTR(err) : &udp_hdr(skb)->check;
3738 }
3739
3740 return ERR_PTR(-EPROTO);
3741}
3742
Paul Durranted1f50c2014-01-09 10:02:46 +00003743/* This value should be large enough to cover a tagged ethernet header plus
3744 * maximally sized IP and TCP or UDP headers.
3745 */
3746#define MAX_IP_HDR_LEN 128
3747
Jan Beulichf9708b42014-03-11 13:56:05 +00003748static int skb_checksum_setup_ipv4(struct sk_buff *skb, bool recalculate)
Paul Durranted1f50c2014-01-09 10:02:46 +00003749{
3750 unsigned int off;
3751 bool fragment;
Jan Beulichf9708b42014-03-11 13:56:05 +00003752 __sum16 *csum;
Paul Durranted1f50c2014-01-09 10:02:46 +00003753 int err;
3754
3755 fragment = false;
3756
3757 err = skb_maybe_pull_tail(skb,
3758 sizeof(struct iphdr),
3759 MAX_IP_HDR_LEN);
3760 if (err < 0)
3761 goto out;
3762
3763 if (ip_hdr(skb)->frag_off & htons(IP_OFFSET | IP_MF))
3764 fragment = true;
3765
3766 off = ip_hdrlen(skb);
3767
3768 err = -EPROTO;
3769
3770 if (fragment)
3771 goto out;
3772
Jan Beulichf9708b42014-03-11 13:56:05 +00003773 csum = skb_checksum_setup_ip(skb, ip_hdr(skb)->protocol, off);
3774 if (IS_ERR(csum))
3775 return PTR_ERR(csum);
Paul Durranted1f50c2014-01-09 10:02:46 +00003776
Jan Beulichf9708b42014-03-11 13:56:05 +00003777 if (recalculate)
3778 *csum = ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
3779 ip_hdr(skb)->daddr,
3780 skb->len - off,
3781 ip_hdr(skb)->protocol, 0);
Paul Durranted1f50c2014-01-09 10:02:46 +00003782 err = 0;
3783
3784out:
3785 return err;
3786}
3787
3788/* This value should be large enough to cover a tagged ethernet header plus
3789 * an IPv6 header, all options, and a maximal TCP or UDP header.
3790 */
3791#define MAX_IPV6_HDR_LEN 256
3792
3793#define OPT_HDR(type, skb, off) \
3794 (type *)(skb_network_header(skb) + (off))
3795
3796static int skb_checksum_setup_ipv6(struct sk_buff *skb, bool recalculate)
3797{
3798 int err;
3799 u8 nexthdr;
3800 unsigned int off;
3801 unsigned int len;
3802 bool fragment;
3803 bool done;
Jan Beulichf9708b42014-03-11 13:56:05 +00003804 __sum16 *csum;
Paul Durranted1f50c2014-01-09 10:02:46 +00003805
3806 fragment = false;
3807 done = false;
3808
3809 off = sizeof(struct ipv6hdr);
3810
3811 err = skb_maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN);
3812 if (err < 0)
3813 goto out;
3814
3815 nexthdr = ipv6_hdr(skb)->nexthdr;
3816
3817 len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len);
3818 while (off <= len && !done) {
3819 switch (nexthdr) {
3820 case IPPROTO_DSTOPTS:
3821 case IPPROTO_HOPOPTS:
3822 case IPPROTO_ROUTING: {
3823 struct ipv6_opt_hdr *hp;
3824
3825 err = skb_maybe_pull_tail(skb,
3826 off +
3827 sizeof(struct ipv6_opt_hdr),
3828 MAX_IPV6_HDR_LEN);
3829 if (err < 0)
3830 goto out;
3831
3832 hp = OPT_HDR(struct ipv6_opt_hdr, skb, off);
3833 nexthdr = hp->nexthdr;
3834 off += ipv6_optlen(hp);
3835 break;
3836 }
3837 case IPPROTO_AH: {
3838 struct ip_auth_hdr *hp;
3839
3840 err = skb_maybe_pull_tail(skb,
3841 off +
3842 sizeof(struct ip_auth_hdr),
3843 MAX_IPV6_HDR_LEN);
3844 if (err < 0)
3845 goto out;
3846
3847 hp = OPT_HDR(struct ip_auth_hdr, skb, off);
3848 nexthdr = hp->nexthdr;
3849 off += ipv6_authlen(hp);
3850 break;
3851 }
3852 case IPPROTO_FRAGMENT: {
3853 struct frag_hdr *hp;
3854
3855 err = skb_maybe_pull_tail(skb,
3856 off +
3857 sizeof(struct frag_hdr),
3858 MAX_IPV6_HDR_LEN);
3859 if (err < 0)
3860 goto out;
3861
3862 hp = OPT_HDR(struct frag_hdr, skb, off);
3863
3864 if (hp->frag_off & htons(IP6_OFFSET | IP6_MF))
3865 fragment = true;
3866
3867 nexthdr = hp->nexthdr;
3868 off += sizeof(struct frag_hdr);
3869 break;
3870 }
3871 default:
3872 done = true;
3873 break;
3874 }
3875 }
3876
3877 err = -EPROTO;
3878
3879 if (!done || fragment)
3880 goto out;
3881
Jan Beulichf9708b42014-03-11 13:56:05 +00003882 csum = skb_checksum_setup_ip(skb, nexthdr, off);
3883 if (IS_ERR(csum))
3884 return PTR_ERR(csum);
Paul Durranted1f50c2014-01-09 10:02:46 +00003885
Jan Beulichf9708b42014-03-11 13:56:05 +00003886 if (recalculate)
3887 *csum = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
3888 &ipv6_hdr(skb)->daddr,
3889 skb->len - off, nexthdr, 0);
Paul Durranted1f50c2014-01-09 10:02:46 +00003890 err = 0;
3891
3892out:
3893 return err;
3894}
3895
3896/**
3897 * skb_checksum_setup - set up partial checksum offset
3898 * @skb: the skb to set up
3899 * @recalculate: if true the pseudo-header checksum will be recalculated
3900 */
3901int skb_checksum_setup(struct sk_buff *skb, bool recalculate)
3902{
3903 int err;
3904
3905 switch (skb->protocol) {
3906 case htons(ETH_P_IP):
Jan Beulichf9708b42014-03-11 13:56:05 +00003907 err = skb_checksum_setup_ipv4(skb, recalculate);
Paul Durranted1f50c2014-01-09 10:02:46 +00003908 break;
3909
3910 case htons(ETH_P_IPV6):
3911 err = skb_checksum_setup_ipv6(skb, recalculate);
3912 break;
3913
3914 default:
3915 err = -EPROTO;
3916 break;
3917 }
3918
3919 return err;
3920}
3921EXPORT_SYMBOL(skb_checksum_setup);
3922
Ben Hutchings4497b072008-06-19 16:22:28 -07003923void __skb_warn_lro_forwarding(const struct sk_buff *skb)
3924{
Joe Perchese87cc472012-05-13 21:56:26 +00003925 net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n",
3926 skb->dev->name);
Ben Hutchings4497b072008-06-19 16:22:28 -07003927}
Ben Hutchings4497b072008-06-19 16:22:28 -07003928EXPORT_SYMBOL(__skb_warn_lro_forwarding);
Eric Dumazetbad43ca2012-05-19 03:02:02 +00003929
3930void kfree_skb_partial(struct sk_buff *skb, bool head_stolen)
3931{
Eric Dumazet3d861f62012-10-22 09:03:40 +00003932 if (head_stolen) {
3933 skb_release_head_state(skb);
Eric Dumazetbad43ca2012-05-19 03:02:02 +00003934 kmem_cache_free(skbuff_head_cache, skb);
Eric Dumazet3d861f62012-10-22 09:03:40 +00003935 } else {
Eric Dumazetbad43ca2012-05-19 03:02:02 +00003936 __kfree_skb(skb);
Eric Dumazet3d861f62012-10-22 09:03:40 +00003937 }
Eric Dumazetbad43ca2012-05-19 03:02:02 +00003938}
3939EXPORT_SYMBOL(kfree_skb_partial);
3940
3941/**
3942 * skb_try_coalesce - try to merge skb to prior one
3943 * @to: prior buffer
3944 * @from: buffer to add
3945 * @fragstolen: pointer to boolean
Randy Dunlapc6c4b972012-06-08 14:01:44 +00003946 * @delta_truesize: how much more was allocated than was requested
Eric Dumazetbad43ca2012-05-19 03:02:02 +00003947 */
3948bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
3949 bool *fragstolen, int *delta_truesize)
3950{
3951 int i, delta, len = from->len;
3952
3953 *fragstolen = false;
3954
3955 if (skb_cloned(to))
3956 return false;
3957
3958 if (len <= skb_tailroom(to)) {
Eric Dumazete93a0432014-09-15 04:19:52 -07003959 if (len)
3960 BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len));
Eric Dumazetbad43ca2012-05-19 03:02:02 +00003961 *delta_truesize = 0;
3962 return true;
3963 }
3964
3965 if (skb_has_frag_list(to) || skb_has_frag_list(from))
3966 return false;
3967
3968 if (skb_headlen(from) != 0) {
3969 struct page *page;
3970 unsigned int offset;
3971
3972 if (skb_shinfo(to)->nr_frags +
3973 skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
3974 return false;
3975
3976 if (skb_head_is_locked(from))
3977 return false;
3978
3979 delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
3980
3981 page = virt_to_head_page(from->head);
3982 offset = from->data - (unsigned char *)page_address(page);
3983
3984 skb_fill_page_desc(to, skb_shinfo(to)->nr_frags,
3985 page, offset, skb_headlen(from));
3986 *fragstolen = true;
3987 } else {
3988 if (skb_shinfo(to)->nr_frags +
3989 skb_shinfo(from)->nr_frags > MAX_SKB_FRAGS)
3990 return false;
3991
Weiping Panf4b549a2012-09-28 20:15:30 +00003992 delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from));
Eric Dumazetbad43ca2012-05-19 03:02:02 +00003993 }
3994
3995 WARN_ON_ONCE(delta < len);
3996
3997 memcpy(skb_shinfo(to)->frags + skb_shinfo(to)->nr_frags,
3998 skb_shinfo(from)->frags,
3999 skb_shinfo(from)->nr_frags * sizeof(skb_frag_t));
4000 skb_shinfo(to)->nr_frags += skb_shinfo(from)->nr_frags;
4001
4002 if (!skb_cloned(from))
4003 skb_shinfo(from)->nr_frags = 0;
4004
Li RongQing8ea853f2012-09-18 16:53:21 +00004005 /* if the skb is not cloned this does nothing
4006 * since we set nr_frags to 0.
4007 */
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004008 for (i = 0; i < skb_shinfo(from)->nr_frags; i++)
4009 skb_frag_ref(from, i);
4010
4011 to->truesize += delta;
4012 to->len += len;
4013 to->data_len += len;
4014
4015 *delta_truesize = delta;
4016 return true;
4017}
4018EXPORT_SYMBOL(skb_try_coalesce);
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004019
4020/**
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004021 * skb_scrub_packet - scrub an skb
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004022 *
4023 * @skb: buffer to clean
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004024 * @xnet: packet is crossing netns
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004025 *
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004026 * skb_scrub_packet can be used after encapsulating or decapsulting a packet
4027 * into/from a tunnel. Some information have to be cleared during these
4028 * operations.
4029 * skb_scrub_packet can also be used to clean a skb before injecting it in
4030 * another namespace (@xnet == true). We have to clear all information in the
4031 * skb that could impact namespace isolation.
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004032 */
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004033void skb_scrub_packet(struct sk_buff *skb, bool xnet)
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004034{
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004035 if (xnet)
4036 skb_orphan(skb);
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004037 skb->tstamp.tv64 = 0;
4038 skb->pkt_type = PACKET_HOST;
4039 skb->skb_iif = 0;
WANG Cong60ff7462014-05-04 16:39:18 -07004040 skb->ignore_df = 0;
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004041 skb_dst_drop(skb);
4042 skb->mark = 0;
4043 secpath_reset(skb);
4044 nf_reset(skb);
4045 nf_reset_trace(skb);
4046}
4047EXPORT_SYMBOL_GPL(skb_scrub_packet);
Florian Westphalde960aa2014-01-26 10:58:16 +01004048
4049/**
4050 * skb_gso_transport_seglen - Return length of individual segments of a gso packet
4051 *
4052 * @skb: GSO skb
4053 *
4054 * skb_gso_transport_seglen is used to determine the real size of the
4055 * individual segments, including Layer4 headers (TCP/UDP).
4056 *
4057 * The MAC/L2 or network (IP, IPv6) headers are not accounted for.
4058 */
4059unsigned int skb_gso_transport_seglen(const struct sk_buff *skb)
4060{
4061 const struct skb_shared_info *shinfo = skb_shinfo(skb);
Florian Westphalf993bc22014-10-20 13:49:18 +02004062 unsigned int thlen = 0;
Florian Westphalde960aa2014-01-26 10:58:16 +01004063
Florian Westphalf993bc22014-10-20 13:49:18 +02004064 if (skb->encapsulation) {
4065 thlen = skb_inner_transport_header(skb) -
4066 skb_transport_header(skb);
Florian Westphal6d39d582014-04-09 10:28:50 +02004067
Florian Westphalf993bc22014-10-20 13:49:18 +02004068 if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)))
4069 thlen += inner_tcp_hdrlen(skb);
4070 } else if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) {
4071 thlen = tcp_hdrlen(skb);
4072 }
Florian Westphal6d39d582014-04-09 10:28:50 +02004073 /* UFO sets gso_size to the size of the fragmentation
4074 * payload, i.e. the size of the L4 (UDP) header is already
4075 * accounted for.
4076 */
Florian Westphalf993bc22014-10-20 13:49:18 +02004077 return thlen + shinfo->gso_size;
Florian Westphalde960aa2014-01-26 10:58:16 +01004078}
4079EXPORT_SYMBOL_GPL(skb_gso_transport_seglen);
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04004080
4081static struct sk_buff *skb_reorder_vlan_header(struct sk_buff *skb)
4082{
4083 if (skb_cow(skb, skb_headroom(skb)) < 0) {
4084 kfree_skb(skb);
4085 return NULL;
4086 }
4087
4088 memmove(skb->data - ETH_HLEN, skb->data - VLAN_ETH_HLEN, 2 * ETH_ALEN);
4089 skb->mac_header += VLAN_HLEN;
4090 return skb;
4091}
4092
4093struct sk_buff *skb_vlan_untag(struct sk_buff *skb)
4094{
4095 struct vlan_hdr *vhdr;
4096 u16 vlan_tci;
4097
4098 if (unlikely(vlan_tx_tag_present(skb))) {
4099 /* vlan_tci is already set-up so leave this for another time */
4100 return skb;
4101 }
4102
4103 skb = skb_share_check(skb, GFP_ATOMIC);
4104 if (unlikely(!skb))
4105 goto err_free;
4106
4107 if (unlikely(!pskb_may_pull(skb, VLAN_HLEN)))
4108 goto err_free;
4109
4110 vhdr = (struct vlan_hdr *)skb->data;
4111 vlan_tci = ntohs(vhdr->h_vlan_TCI);
4112 __vlan_hwaccel_put_tag(skb, skb->protocol, vlan_tci);
4113
4114 skb_pull_rcsum(skb, VLAN_HLEN);
4115 vlan_set_encap_proto(skb, vhdr);
4116
4117 skb = skb_reorder_vlan_header(skb);
4118 if (unlikely(!skb))
4119 goto err_free;
4120
4121 skb_reset_network_header(skb);
4122 skb_reset_transport_header(skb);
4123 skb_reset_mac_len(skb);
4124
4125 return skb;
4126
4127err_free:
4128 kfree_skb(skb);
4129 return NULL;
4130}
4131EXPORT_SYMBOL(skb_vlan_untag);
Eric Dumazet2e4e4412014-09-17 04:49:49 -07004132
4133/**
4134 * alloc_skb_with_frags - allocate skb with page frags
4135 *
Masanari Iidade3f0d02014-10-09 12:58:08 +09004136 * @header_len: size of linear part
4137 * @data_len: needed length in frags
4138 * @max_page_order: max page order desired.
4139 * @errcode: pointer to error code if any
4140 * @gfp_mask: allocation mask
Eric Dumazet2e4e4412014-09-17 04:49:49 -07004141 *
4142 * This can be used to allocate a paged skb, given a maximal order for frags.
4143 */
4144struct sk_buff *alloc_skb_with_frags(unsigned long header_len,
4145 unsigned long data_len,
4146 int max_page_order,
4147 int *errcode,
4148 gfp_t gfp_mask)
4149{
4150 int npages = (data_len + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
4151 unsigned long chunk;
4152 struct sk_buff *skb;
4153 struct page *page;
4154 gfp_t gfp_head;
4155 int i;
4156
4157 *errcode = -EMSGSIZE;
4158 /* Note this test could be relaxed, if we succeed to allocate
4159 * high order pages...
4160 */
4161 if (npages > MAX_SKB_FRAGS)
4162 return NULL;
4163
4164 gfp_head = gfp_mask;
4165 if (gfp_head & __GFP_WAIT)
4166 gfp_head |= __GFP_REPEAT;
4167
4168 *errcode = -ENOBUFS;
4169 skb = alloc_skb(header_len, gfp_head);
4170 if (!skb)
4171 return NULL;
4172
4173 skb->truesize += npages << PAGE_SHIFT;
4174
4175 for (i = 0; npages > 0; i++) {
4176 int order = max_page_order;
4177
4178 while (order) {
4179 if (npages >= 1 << order) {
4180 page = alloc_pages(gfp_mask |
4181 __GFP_COMP |
4182 __GFP_NOWARN |
4183 __GFP_NORETRY,
4184 order);
4185 if (page)
4186 goto fill_page;
4187 /* Do not retry other high order allocations */
4188 order = 1;
4189 max_page_order = 0;
4190 }
4191 order--;
4192 }
4193 page = alloc_page(gfp_mask);
4194 if (!page)
4195 goto failure;
4196fill_page:
4197 chunk = min_t(unsigned long, data_len,
4198 PAGE_SIZE << order);
4199 skb_fill_page_desc(skb, i, page, 0, chunk);
4200 data_len -= chunk;
4201 npages -= 1 << order;
4202 }
4203 return skb;
4204
4205failure:
4206 kfree_skb(skb);
4207 return NULL;
4208}
4209EXPORT_SYMBOL(alloc_skb_with_frags);