blob: 02769fa4f5c8df157c067ca55d7101de18f25d85 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Routines having to do with the 'struct sk_buff' memory handlers.
3 *
Alan Cox113aa832008-10-13 19:01:08 -07004 * Authors: Alan Cox <alan@lxorguk.ukuu.org.uk>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Florian La Roche <rzsfl@rz.uni-sb.de>
6 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * Fixes:
8 * Alan Cox : Fixed the worst of the load
9 * balancer bugs.
10 * Dave Platt : Interrupt stacking fix.
11 * Richard Kooijman : Timestamp fixes.
12 * Alan Cox : Changed buffer format.
13 * Alan Cox : destructor hook for AF_UNIX etc.
14 * Linus Torvalds : Better skb_clone.
15 * Alan Cox : Added skb_copy.
16 * Alan Cox : Added all the changed routines Linus
17 * only put in the headers
18 * Ray VanTassle : Fixed --skb->lock in free
19 * Alan Cox : skb_copy copy arp field
20 * Andi Kleen : slabified it.
21 * Robert Olsson : Removed skb_head_pool
22 *
23 * NOTE:
24 * The __skb_ routines should be called with interrupts
25 * disabled, or you better be *real* sure that the operation is atomic
26 * with respect to whatever list is being frobbed (e.g. via lock_sock()
27 * or via disabling bottom half handlers, etc).
28 *
29 * This program is free software; you can redistribute it and/or
30 * modify it under the terms of the GNU General Public License
31 * as published by the Free Software Foundation; either version
32 * 2 of the License, or (at your option) any later version.
33 */
34
35/*
36 * The functions in this file will not compile correctly with gcc 2.4.x
37 */
38
Joe Perchese005d192012-05-16 19:58:40 +000039#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/module.h>
42#include <linux/types.h>
43#include <linux/kernel.h>
Vegard Nossumfe55f6d2008-08-30 12:16:35 +020044#include <linux/kmemcheck.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/mm.h>
46#include <linux/interrupt.h>
47#include <linux/in.h>
48#include <linux/inet.h>
49#include <linux/slab.h>
Florian Westphalde960aa2014-01-26 10:58:16 +010050#include <linux/tcp.h>
51#include <linux/udp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <linux/netdevice.h>
53#ifdef CONFIG_NET_CLS_ACT
54#include <net/pkt_sched.h>
55#endif
56#include <linux/string.h>
57#include <linux/skbuff.h>
Jens Axboe9c55e012007-11-06 23:30:13 -080058#include <linux/splice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#include <linux/cache.h>
60#include <linux/rtnetlink.h>
61#include <linux/init.h>
David Howells716ea3a2007-04-02 20:19:53 -070062#include <linux/scatterlist.h>
Patrick Ohlyac45f602009-02-12 05:03:37 +000063#include <linux/errqueue.h>
Linus Torvalds268bb0c2011-05-20 12:50:29 -070064#include <linux/prefetch.h>
Vlad Yasevich0d5501c2014-08-08 14:42:13 -040065#include <linux/if_vlan.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67#include <net/protocol.h>
68#include <net/dst.h>
69#include <net/sock.h>
70#include <net/checksum.h>
Paul Durranted1f50c2014-01-09 10:02:46 +000071#include <net/ip6_checksum.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#include <net/xfrm.h>
73
74#include <asm/uaccess.h>
Steven Rostedtad8d75f2009-04-14 19:39:12 -040075#include <trace/events/skb.h>
Eric Dumazet51c56b02012-04-05 11:35:15 +020076#include <linux/highmem.h>
Willem de Bruijnb245be12015-01-30 13:29:32 -050077#include <linux/capability.h>
78#include <linux/user_namespace.h>
Al Viroa1f8e7f72006-10-19 16:08:53 -040079
Eric Dumazetd7e88832012-04-30 08:10:34 +000080struct kmem_cache *skbuff_head_cache __read_mostly;
Christoph Lametere18b8902006-12-06 20:33:20 -080081static struct kmem_cache *skbuff_fclone_cache __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Linus Torvalds1da177e2005-04-16 15:20:36 -070083/**
Jean Sacrenf05de732013-02-11 13:30:38 +000084 * skb_panic - private function for out-of-line support
85 * @skb: buffer
86 * @sz: size
87 * @addr: address
James Hogan99d58512013-02-13 11:20:27 +000088 * @msg: skb_over_panic or skb_under_panic
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 *
Jean Sacrenf05de732013-02-11 13:30:38 +000090 * Out-of-line support for skb_put() and skb_push().
91 * Called via the wrapper skb_over_panic() or skb_under_panic().
92 * Keep out of line to prevent kernel bloat.
93 * __builtin_return_address is not used because it is not always reliable.
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 */
Jean Sacrenf05de732013-02-11 13:30:38 +000095static void skb_panic(struct sk_buff *skb, unsigned int sz, void *addr,
James Hogan99d58512013-02-13 11:20:27 +000096 const char msg[])
Linus Torvalds1da177e2005-04-16 15:20:36 -070097{
Joe Perchese005d192012-05-16 19:58:40 +000098 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 +000099 msg, addr, skb->len, sz, skb->head, skb->data,
Joe Perchese005d192012-05-16 19:58:40 +0000100 (unsigned long)skb->tail, (unsigned long)skb->end,
101 skb->dev ? skb->dev->name : "<NULL>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 BUG();
103}
104
Jean Sacrenf05de732013-02-11 13:30:38 +0000105static void skb_over_panic(struct sk_buff *skb, unsigned int sz, void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106{
Jean Sacrenf05de732013-02-11 13:30:38 +0000107 skb_panic(skb, sz, addr, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108}
109
Jean Sacrenf05de732013-02-11 13:30:38 +0000110static void skb_under_panic(struct sk_buff *skb, unsigned int sz, void *addr)
111{
112 skb_panic(skb, sz, addr, __func__);
113}
Mel Gormanc93bdd02012-07-31 16:44:19 -0700114
115/*
116 * kmalloc_reserve is a wrapper around kmalloc_node_track_caller that tells
117 * the caller if emergency pfmemalloc reserves are being used. If it is and
118 * the socket is later found to be SOCK_MEMALLOC then PFMEMALLOC reserves
119 * may be used. Otherwise, the packet data may be discarded until enough
120 * memory is free
121 */
122#define kmalloc_reserve(size, gfp, node, pfmemalloc) \
123 __kmalloc_reserve(size, gfp, node, _RET_IP_, pfmemalloc)
stephen hemminger61c5e882012-12-28 18:24:28 +0000124
125static void *__kmalloc_reserve(size_t size, gfp_t flags, int node,
126 unsigned long ip, bool *pfmemalloc)
Mel Gormanc93bdd02012-07-31 16:44:19 -0700127{
128 void *obj;
129 bool ret_pfmemalloc = false;
130
131 /*
132 * Try a regular allocation, when that fails and we're not entitled
133 * to the reserves, fail.
134 */
135 obj = kmalloc_node_track_caller(size,
136 flags | __GFP_NOMEMALLOC | __GFP_NOWARN,
137 node);
138 if (obj || !(gfp_pfmemalloc_allowed(flags)))
139 goto out;
140
141 /* Try again but now we are using pfmemalloc reserves */
142 ret_pfmemalloc = true;
143 obj = kmalloc_node_track_caller(size, flags, node);
144
145out:
146 if (pfmemalloc)
147 *pfmemalloc = ret_pfmemalloc;
148
149 return obj;
150}
151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152/* Allocate a new skbuff. We do this ourselves so we can fill in a few
153 * 'private' fields and also do memory statistics to find all the
154 * [BEEP] leaks.
155 *
156 */
157
Patrick McHardy0ebd0ac2013-04-17 06:46:58 +0000158struct sk_buff *__alloc_skb_head(gfp_t gfp_mask, int node)
159{
160 struct sk_buff *skb;
161
162 /* Get the HEAD */
163 skb = kmem_cache_alloc_node(skbuff_head_cache,
164 gfp_mask & ~__GFP_DMA, node);
165 if (!skb)
166 goto out;
167
168 /*
169 * Only clear those fields we need to clear, not those that we will
170 * actually initialise below. Hence, don't put any more fields after
171 * the tail pointer in struct sk_buff!
172 */
173 memset(skb, 0, offsetof(struct sk_buff, tail));
Pablo Neira5e71d9d2013-06-03 09:28:43 +0000174 skb->head = NULL;
Patrick McHardy0ebd0ac2013-04-17 06:46:58 +0000175 skb->truesize = sizeof(struct sk_buff);
176 atomic_set(&skb->users, 1);
177
Cong Wang35d04612013-05-29 15:16:05 +0800178 skb->mac_header = (typeof(skb->mac_header))~0U;
Patrick McHardy0ebd0ac2013-04-17 06:46:58 +0000179out:
180 return skb;
181}
182
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183/**
David S. Millerd179cd12005-08-17 14:57:30 -0700184 * __alloc_skb - allocate a network buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 * @size: size to allocate
186 * @gfp_mask: allocation mask
Mel Gormanc93bdd02012-07-31 16:44:19 -0700187 * @flags: If SKB_ALLOC_FCLONE is set, allocate from fclone cache
188 * instead of head cache and allocate a cloned (child) skb.
189 * If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for
190 * allocations in case the data is required for writeback
Christoph Hellwigb30973f2006-12-06 20:32:36 -0800191 * @node: numa node to allocate memory on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 *
193 * Allocate a new &sk_buff. The returned buffer has no headroom and a
Ben Hutchings94b60422012-06-06 15:23:37 +0000194 * tail room of at least size bytes. The object has a reference count
195 * of one. The return is the buffer. On a failure the return is %NULL.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 *
197 * Buffers may only be allocated from interrupts using a @gfp_mask of
198 * %GFP_ATOMIC.
199 */
Al Virodd0fc662005-10-07 07:46:04 +0100200struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
Mel Gormanc93bdd02012-07-31 16:44:19 -0700201 int flags, int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
Christoph Lametere18b8902006-12-06 20:33:20 -0800203 struct kmem_cache *cache;
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800204 struct skb_shared_info *shinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 struct sk_buff *skb;
206 u8 *data;
Mel Gormanc93bdd02012-07-31 16:44:19 -0700207 bool pfmemalloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Mel Gormanc93bdd02012-07-31 16:44:19 -0700209 cache = (flags & SKB_ALLOC_FCLONE)
210 ? skbuff_fclone_cache : skbuff_head_cache;
211
212 if (sk_memalloc_socks() && (flags & SKB_ALLOC_RX))
213 gfp_mask |= __GFP_MEMALLOC;
Herbert Xu8798b3f2006-01-23 16:32:45 -0800214
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 /* Get the HEAD */
Christoph Hellwigb30973f2006-12-06 20:32:36 -0800216 skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 if (!skb)
218 goto out;
Eric Dumazetec7d2f22010-05-05 01:07:37 -0700219 prefetchw(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
Eric Dumazet87fb4b72011-10-13 07:28:54 +0000221 /* We do our best to align skb_shared_info on a separate cache
222 * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives
223 * aligned memory blocks, unless SLUB/SLAB debug is enabled.
224 * Both skb->head and skb_shared_info are cache line aligned.
225 */
Tony Lindgrenbc417e32011-11-02 13:40:28 +0000226 size = SKB_DATA_ALIGN(size);
Eric Dumazet87fb4b72011-10-13 07:28:54 +0000227 size += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Mel Gormanc93bdd02012-07-31 16:44:19 -0700228 data = kmalloc_reserve(size, gfp_mask, node, &pfmemalloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 if (!data)
230 goto nodata;
Eric Dumazet87fb4b72011-10-13 07:28:54 +0000231 /* kmalloc(size) might give us more room than requested.
232 * Put skb_shared_info exactly at the end of allocated zone,
233 * to allow max possible filling before reallocation.
234 */
235 size = SKB_WITH_OVERHEAD(ksize(data));
Eric Dumazetec7d2f22010-05-05 01:07:37 -0700236 prefetchw(data + size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
Arnaldo Carvalho de Meloca0605a2007-03-19 10:48:59 -0300238 /*
Johannes Bergc8005782008-05-03 20:56:42 -0700239 * Only clear those fields we need to clear, not those that we will
240 * actually initialise below. Hence, don't put any more fields after
241 * the tail pointer in struct sk_buff!
Arnaldo Carvalho de Meloca0605a2007-03-19 10:48:59 -0300242 */
243 memset(skb, 0, offsetof(struct sk_buff, tail));
Eric Dumazet87fb4b72011-10-13 07:28:54 +0000244 /* Account for allocated memory : skb + skb->head */
245 skb->truesize = SKB_TRUESIZE(size);
Mel Gormanc93bdd02012-07-31 16:44:19 -0700246 skb->pfmemalloc = pfmemalloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 atomic_set(&skb->users, 1);
248 skb->head = data;
249 skb->data = data;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700250 skb_reset_tail_pointer(skb);
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700251 skb->end = skb->tail + size;
Cong Wang35d04612013-05-29 15:16:05 +0800252 skb->mac_header = (typeof(skb->mac_header))~0U;
253 skb->transport_header = (typeof(skb->transport_header))~0U;
Stephen Hemminger19633e12009-06-17 05:23:27 +0000254
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800255 /* make sure we initialize shinfo sequentially */
256 shinfo = skb_shinfo(skb);
Eric Dumazetec7d2f22010-05-05 01:07:37 -0700257 memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800258 atomic_set(&shinfo->dataref, 1);
Eric Dumazetc2aa3662011-01-25 23:18:38 +0000259 kmemcheck_annotate_variable(shinfo->destructor_arg);
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800260
Mel Gormanc93bdd02012-07-31 16:44:19 -0700261 if (flags & SKB_ALLOC_FCLONE) {
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700262 struct sk_buff_fclones *fclones;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700264 fclones = container_of(skb, struct sk_buff_fclones, skb1);
265
266 kmemcheck_annotate_bitfield(&fclones->skb2, flags1);
David S. Millerd179cd12005-08-17 14:57:30 -0700267 skb->fclone = SKB_FCLONE_ORIG;
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700268 atomic_set(&fclones->fclone_ref, 1);
David S. Millerd179cd12005-08-17 14:57:30 -0700269
Eric Dumazet6ffe75e2014-12-03 17:04:39 -0800270 fclones->skb2.fclone = SKB_FCLONE_CLONE;
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700271 fclones->skb2.pfmemalloc = pfmemalloc;
David S. Millerd179cd12005-08-17 14:57:30 -0700272 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273out:
274 return skb;
275nodata:
Herbert Xu8798b3f2006-01-23 16:32:45 -0800276 kmem_cache_free(cache, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 skb = NULL;
278 goto out;
279}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800280EXPORT_SYMBOL(__alloc_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
282/**
Eric Dumazet2ea2f622015-04-24 16:05:01 -0700283 * __build_skb - build a network buffer
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000284 * @data: data buffer provided by caller
Eric Dumazet2ea2f622015-04-24 16:05:01 -0700285 * @frag_size: size of data, or 0 if head was kmalloced
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000286 *
287 * Allocate a new &sk_buff. Caller provides space holding head and
Florian Fainellideceb4c2013-07-23 20:22:39 +0100288 * skb_shared_info. @data must have been allocated by kmalloc() only if
Eric Dumazet2ea2f622015-04-24 16:05:01 -0700289 * @frag_size is 0, otherwise data should come from the page allocator
290 * or vmalloc()
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000291 * The return is the new skb buffer.
292 * On a failure the return is %NULL, and @data is not freed.
293 * Notes :
294 * Before IO, driver allocates only data buffer where NIC put incoming frame
295 * Driver should add room at head (NET_SKB_PAD) and
296 * MUST add room at tail (SKB_DATA_ALIGN(skb_shared_info))
297 * After IO, driver calls build_skb(), to allocate sk_buff and populate it
298 * before giving packet to stack.
299 * RX rings only contains data buffers, not full skbs.
300 */
Eric Dumazet2ea2f622015-04-24 16:05:01 -0700301struct sk_buff *__build_skb(void *data, unsigned int frag_size)
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000302{
303 struct skb_shared_info *shinfo;
304 struct sk_buff *skb;
Eric Dumazetd3836f22012-04-27 00:33:38 +0000305 unsigned int size = frag_size ? : ksize(data);
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000306
307 skb = kmem_cache_alloc(skbuff_head_cache, GFP_ATOMIC);
308 if (!skb)
309 return NULL;
310
Eric Dumazetd3836f22012-04-27 00:33:38 +0000311 size -= SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000312
313 memset(skb, 0, offsetof(struct sk_buff, tail));
314 skb->truesize = SKB_TRUESIZE(size);
315 atomic_set(&skb->users, 1);
316 skb->head = data;
317 skb->data = data;
318 skb_reset_tail_pointer(skb);
319 skb->end = skb->tail + size;
Cong Wang35d04612013-05-29 15:16:05 +0800320 skb->mac_header = (typeof(skb->mac_header))~0U;
321 skb->transport_header = (typeof(skb->transport_header))~0U;
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000322
323 /* make sure we initialize shinfo sequentially */
324 shinfo = skb_shinfo(skb);
325 memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
326 atomic_set(&shinfo->dataref, 1);
327 kmemcheck_annotate_variable(shinfo->destructor_arg);
328
329 return skb;
330}
Eric Dumazet2ea2f622015-04-24 16:05:01 -0700331
332/* build_skb() is wrapper over __build_skb(), that specifically
333 * takes care of skb->head and skb->pfmemalloc
334 * This means that if @frag_size is not zero, then @data must be backed
335 * by a page fragment, not kmalloc() or vmalloc()
336 */
337struct sk_buff *build_skb(void *data, unsigned int frag_size)
338{
339 struct sk_buff *skb = __build_skb(data, frag_size);
340
341 if (skb && frag_size) {
342 skb->head_frag = 1;
343 if (virt_to_head_page(data)->pfmemalloc)
344 skb->pfmemalloc = 1;
345 }
346 return skb;
347}
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000348EXPORT_SYMBOL(build_skb);
349
Alexander Duyckb63ae8c2015-05-06 21:11:57 -0700350static DEFINE_PER_CPU(struct page_frag_cache, netdev_alloc_cache);
351static DEFINE_PER_CPU(struct page_frag_cache, napi_alloc_cache);
Alexander Duyckffde7322014-12-09 19:40:42 -0800352
353static void *__netdev_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
354{
Alexander Duyckb63ae8c2015-05-06 21:11:57 -0700355 struct page_frag_cache *nc;
Alexander Duyckffde7322014-12-09 19:40:42 -0800356 unsigned long flags;
357 void *data;
358
359 local_irq_save(flags);
Alexander Duyck94519802015-05-06 21:11:40 -0700360 nc = this_cpu_ptr(&netdev_alloc_cache);
361 data = __alloc_page_frag(nc, fragsz, gfp_mask);
Eric Dumazet6f532612012-05-18 05:12:12 +0000362 local_irq_restore(flags);
363 return data;
364}
Mel Gormanc93bdd02012-07-31 16:44:19 -0700365
366/**
367 * netdev_alloc_frag - allocate a page fragment
368 * @fragsz: fragment size
369 *
370 * Allocates a frag from a page for receive buffer.
371 * Uses GFP_ATOMIC allocations.
372 */
373void *netdev_alloc_frag(unsigned int fragsz)
374{
375 return __netdev_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD);
376}
Eric Dumazet6f532612012-05-18 05:12:12 +0000377EXPORT_SYMBOL(netdev_alloc_frag);
378
Alexander Duyckffde7322014-12-09 19:40:42 -0800379static void *__napi_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
380{
Alexander Duyckb63ae8c2015-05-06 21:11:57 -0700381 struct page_frag_cache *nc = this_cpu_ptr(&napi_alloc_cache);
Alexander Duyck94519802015-05-06 21:11:40 -0700382
383 return __alloc_page_frag(nc, fragsz, gfp_mask);
Alexander Duyckffde7322014-12-09 19:40:42 -0800384}
385
386void *napi_alloc_frag(unsigned int fragsz)
387{
388 return __napi_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD);
389}
390EXPORT_SYMBOL(napi_alloc_frag);
391
Eric Dumazet6f532612012-05-18 05:12:12 +0000392/**
Alexander Duyckfd11a832014-12-09 19:40:49 -0800393 * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
394 * @dev: network device to receive on
395 * @length: length to allocate
396 * @gfp_mask: get_free_pages mask, passed to alloc_skb
397 *
398 * Allocate a new &sk_buff and assign it a usage count of one. The
399 * buffer has NET_SKB_PAD headroom built in. Users should allocate
400 * the headroom they think they need without accounting for the
401 * built in space. The built in space is used for optimisations.
402 *
403 * %NULL is returned if there is no free memory.
404 */
Alexander Duyck94519802015-05-06 21:11:40 -0700405struct sk_buff *__netdev_alloc_skb(struct net_device *dev, unsigned int len,
406 gfp_t gfp_mask)
Alexander Duyckfd11a832014-12-09 19:40:49 -0800407{
Alexander Duyckb63ae8c2015-05-06 21:11:57 -0700408 struct page_frag_cache *nc;
Alexander Duyck94519802015-05-06 21:11:40 -0700409 unsigned long flags;
Alexander Duyckfd11a832014-12-09 19:40:49 -0800410 struct sk_buff *skb;
Alexander Duyck94519802015-05-06 21:11:40 -0700411 bool pfmemalloc;
412 void *data;
Alexander Duyckfd11a832014-12-09 19:40:49 -0800413
Alexander Duyck94519802015-05-06 21:11:40 -0700414 len += NET_SKB_PAD;
Alexander Duyckfd11a832014-12-09 19:40:49 -0800415
Alexander Duyck94519802015-05-06 21:11:40 -0700416 if ((len > SKB_WITH_OVERHEAD(PAGE_SIZE)) ||
Alexander Duycka080e7b2015-05-13 13:34:13 -0700417 (gfp_mask & (__GFP_WAIT | GFP_DMA))) {
418 skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE);
419 if (!skb)
420 goto skb_fail;
421 goto skb_success;
422 }
Alexander Duyck94519802015-05-06 21:11:40 -0700423
424 len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
425 len = SKB_DATA_ALIGN(len);
426
427 if (sk_memalloc_socks())
428 gfp_mask |= __GFP_MEMALLOC;
429
430 local_irq_save(flags);
431
432 nc = this_cpu_ptr(&netdev_alloc_cache);
433 data = __alloc_page_frag(nc, len, gfp_mask);
434 pfmemalloc = nc->pfmemalloc;
435
436 local_irq_restore(flags);
437
438 if (unlikely(!data))
439 return NULL;
440
441 skb = __build_skb(data, len);
442 if (unlikely(!skb)) {
Alexander Duyck181edb22015-05-06 21:12:03 -0700443 skb_free_frag(data);
Alexander Duyck94519802015-05-06 21:11:40 -0700444 return NULL;
Christoph Hellwig7b2e4972006-08-07 16:09:04 -0700445 }
Alexander Duyckfd11a832014-12-09 19:40:49 -0800446
Alexander Duyck94519802015-05-06 21:11:40 -0700447 /* use OR instead of assignment to avoid clearing of bits in mask */
448 if (pfmemalloc)
449 skb->pfmemalloc = 1;
450 skb->head_frag = 1;
451
Alexander Duycka080e7b2015-05-13 13:34:13 -0700452skb_success:
Alexander Duyck94519802015-05-06 21:11:40 -0700453 skb_reserve(skb, NET_SKB_PAD);
454 skb->dev = dev;
455
Alexander Duycka080e7b2015-05-13 13:34:13 -0700456skb_fail:
Christoph Hellwig8af27452006-07-31 22:35:23 -0700457 return skb;
458}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800459EXPORT_SYMBOL(__netdev_alloc_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
Alexander Duyckfd11a832014-12-09 19:40:49 -0800461/**
462 * __napi_alloc_skb - allocate skbuff for rx in a specific NAPI instance
463 * @napi: napi instance this buffer was allocated for
464 * @length: length to allocate
465 * @gfp_mask: get_free_pages mask, passed to alloc_skb and alloc_pages
466 *
467 * Allocate a new sk_buff for use in NAPI receive. This buffer will
468 * attempt to allocate the head from a special reserved region used
469 * only for NAPI Rx allocation. By doing this we can save several
470 * CPU cycles by avoiding having to disable and re-enable IRQs.
471 *
472 * %NULL is returned if there is no free memory.
473 */
Alexander Duyck94519802015-05-06 21:11:40 -0700474struct sk_buff *__napi_alloc_skb(struct napi_struct *napi, unsigned int len,
475 gfp_t gfp_mask)
Alexander Duyckfd11a832014-12-09 19:40:49 -0800476{
Alexander Duyckb63ae8c2015-05-06 21:11:57 -0700477 struct page_frag_cache *nc = this_cpu_ptr(&napi_alloc_cache);
Alexander Duyckfd11a832014-12-09 19:40:49 -0800478 struct sk_buff *skb;
Alexander Duyck94519802015-05-06 21:11:40 -0700479 void *data;
Alexander Duyckfd11a832014-12-09 19:40:49 -0800480
Alexander Duyck94519802015-05-06 21:11:40 -0700481 len += NET_SKB_PAD + NET_IP_ALIGN;
Alexander Duyckfd11a832014-12-09 19:40:49 -0800482
Alexander Duyck94519802015-05-06 21:11:40 -0700483 if ((len > SKB_WITH_OVERHEAD(PAGE_SIZE)) ||
Alexander Duycka080e7b2015-05-13 13:34:13 -0700484 (gfp_mask & (__GFP_WAIT | GFP_DMA))) {
485 skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE);
486 if (!skb)
487 goto skb_fail;
488 goto skb_success;
489 }
Alexander Duyck94519802015-05-06 21:11:40 -0700490
491 len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
492 len = SKB_DATA_ALIGN(len);
493
494 if (sk_memalloc_socks())
495 gfp_mask |= __GFP_MEMALLOC;
496
497 data = __alloc_page_frag(nc, len, gfp_mask);
498 if (unlikely(!data))
499 return NULL;
500
501 skb = __build_skb(data, len);
502 if (unlikely(!skb)) {
Alexander Duyck181edb22015-05-06 21:12:03 -0700503 skb_free_frag(data);
Alexander Duyck94519802015-05-06 21:11:40 -0700504 return NULL;
Alexander Duyckfd11a832014-12-09 19:40:49 -0800505 }
506
Alexander Duyck94519802015-05-06 21:11:40 -0700507 /* use OR instead of assignment to avoid clearing of bits in mask */
508 if (nc->pfmemalloc)
509 skb->pfmemalloc = 1;
510 skb->head_frag = 1;
511
Alexander Duycka080e7b2015-05-13 13:34:13 -0700512skb_success:
Alexander Duyck94519802015-05-06 21:11:40 -0700513 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
514 skb->dev = napi->dev;
515
Alexander Duycka080e7b2015-05-13 13:34:13 -0700516skb_fail:
Alexander Duyckfd11a832014-12-09 19:40:49 -0800517 return skb;
518}
519EXPORT_SYMBOL(__napi_alloc_skb);
520
Peter Zijlstra654bed12008-10-07 14:22:33 -0700521void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off,
Eric Dumazet50269e12012-03-23 23:59:33 +0000522 int size, unsigned int truesize)
Peter Zijlstra654bed12008-10-07 14:22:33 -0700523{
524 skb_fill_page_desc(skb, i, page, off, size);
525 skb->len += size;
526 skb->data_len += size;
Eric Dumazet50269e12012-03-23 23:59:33 +0000527 skb->truesize += truesize;
Peter Zijlstra654bed12008-10-07 14:22:33 -0700528}
529EXPORT_SYMBOL(skb_add_rx_frag);
530
Jason Wangf8e617e2013-11-01 14:07:47 +0800531void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size,
532 unsigned int truesize)
533{
534 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
535
536 skb_frag_size_add(frag, size);
537 skb->len += size;
538 skb->data_len += size;
539 skb->truesize += truesize;
540}
541EXPORT_SYMBOL(skb_coalesce_rx_frag);
542
Herbert Xu27b437c2006-07-13 19:26:39 -0700543static void skb_drop_list(struct sk_buff **listp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544{
Eric Dumazetbd8a7032013-06-24 06:26:00 -0700545 kfree_skb_list(*listp);
Herbert Xu27b437c2006-07-13 19:26:39 -0700546 *listp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547}
548
Herbert Xu27b437c2006-07-13 19:26:39 -0700549static inline void skb_drop_fraglist(struct sk_buff *skb)
550{
551 skb_drop_list(&skb_shinfo(skb)->frag_list);
552}
553
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554static void skb_clone_fraglist(struct sk_buff *skb)
555{
556 struct sk_buff *list;
557
David S. Millerfbb398a2009-06-09 00:18:59 -0700558 skb_walk_frags(skb, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 skb_get(list);
560}
561
Eric Dumazetd3836f22012-04-27 00:33:38 +0000562static void skb_free_head(struct sk_buff *skb)
563{
Alexander Duyck181edb22015-05-06 21:12:03 -0700564 unsigned char *head = skb->head;
565
Eric Dumazetd3836f22012-04-27 00:33:38 +0000566 if (skb->head_frag)
Alexander Duyck181edb22015-05-06 21:12:03 -0700567 skb_free_frag(head);
Eric Dumazetd3836f22012-04-27 00:33:38 +0000568 else
Alexander Duyck181edb22015-05-06 21:12:03 -0700569 kfree(head);
Eric Dumazetd3836f22012-04-27 00:33:38 +0000570}
571
Adrian Bunk5bba1712006-06-29 13:02:35 -0700572static void skb_release_data(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573{
Eric Dumazetff04a772014-09-23 18:39:30 -0700574 struct skb_shared_info *shinfo = skb_shinfo(skb);
575 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
Eric Dumazetff04a772014-09-23 18:39:30 -0700577 if (skb->cloned &&
578 atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
579 &shinfo->dataref))
580 return;
Shirley Maa6686f22011-07-06 12:22:12 +0000581
Eric Dumazetff04a772014-09-23 18:39:30 -0700582 for (i = 0; i < shinfo->nr_frags; i++)
583 __skb_frag_unref(&shinfo->frags[i]);
Shirley Maa6686f22011-07-06 12:22:12 +0000584
Eric Dumazetff04a772014-09-23 18:39:30 -0700585 /*
586 * If skb buf is from userspace, we need to notify the caller
587 * the lower device DMA has done;
588 */
589 if (shinfo->tx_flags & SKBTX_DEV_ZEROCOPY) {
590 struct ubuf_info *uarg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
Eric Dumazetff04a772014-09-23 18:39:30 -0700592 uarg = shinfo->destructor_arg;
593 if (uarg->callback)
594 uarg->callback(uarg, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 }
Eric Dumazetff04a772014-09-23 18:39:30 -0700596
597 if (shinfo->frag_list)
598 kfree_skb_list(shinfo->frag_list);
599
600 skb_free_head(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601}
602
603/*
604 * Free an skbuff by memory without cleaning the state.
605 */
Herbert Xu2d4baff2007-11-26 23:11:19 +0800606static void kfree_skbmem(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607{
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700608 struct sk_buff_fclones *fclones;
David S. Millerd179cd12005-08-17 14:57:30 -0700609
David S. Millerd179cd12005-08-17 14:57:30 -0700610 switch (skb->fclone) {
611 case SKB_FCLONE_UNAVAILABLE:
612 kmem_cache_free(skbuff_head_cache, skb);
Eric Dumazet6ffe75e2014-12-03 17:04:39 -0800613 return;
David S. Millerd179cd12005-08-17 14:57:30 -0700614
615 case SKB_FCLONE_ORIG:
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700616 fclones = container_of(skb, struct sk_buff_fclones, skb1);
Eric Dumazet6ffe75e2014-12-03 17:04:39 -0800617
618 /* We usually free the clone (TX completion) before original skb
619 * This test would have no chance to be true for the clone,
620 * while here, branch prediction will be good.
621 */
622 if (atomic_read(&fclones->fclone_ref) == 1)
623 goto fastpath;
David S. Millerd179cd12005-08-17 14:57:30 -0700624 break;
625
Eric Dumazet6ffe75e2014-12-03 17:04:39 -0800626 default: /* SKB_FCLONE_CLONE */
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700627 fclones = container_of(skb, struct sk_buff_fclones, skb2);
David S. Millerd179cd12005-08-17 14:57:30 -0700628 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700629 }
Eric Dumazet6ffe75e2014-12-03 17:04:39 -0800630 if (!atomic_dec_and_test(&fclones->fclone_ref))
631 return;
632fastpath:
633 kmem_cache_free(skbuff_fclone_cache, fclones);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634}
635
Lennert Buytenhek04a4bb52008-10-01 02:33:12 -0700636static void skb_release_head_state(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637{
Eric Dumazetadf30902009-06-02 05:19:30 +0000638 skb_dst_drop(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639#ifdef CONFIG_XFRM
640 secpath_put(skb->sp);
641#endif
Stephen Hemminger9c2b3322005-04-19 22:39:42 -0700642 if (skb->destructor) {
643 WARN_ON(in_irq());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 skb->destructor(skb);
645 }
Igor Maravića3bf7ae2011-12-12 02:58:22 +0000646#if IS_ENABLED(CONFIG_NF_CONNTRACK)
Yasuyuki Kozakai5f79e0f2007-03-23 11:17:07 -0700647 nf_conntrack_put(skb->nfct);
KOVACS Krisztian2fc72c72011-01-12 20:25:08 +0100648#endif
Pablo Neira Ayuso1109a902014-10-01 11:19:17 +0200649#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 nf_bridge_put(skb->nf_bridge);
651#endif
Lennert Buytenhek04a4bb52008-10-01 02:33:12 -0700652}
653
654/* Free everything but the sk_buff shell. */
655static void skb_release_all(struct sk_buff *skb)
656{
657 skb_release_head_state(skb);
Pablo Neira5e71d9d2013-06-03 09:28:43 +0000658 if (likely(skb->head))
Patrick McHardy0ebd0ac2013-04-17 06:46:58 +0000659 skb_release_data(skb);
Herbert Xu2d4baff2007-11-26 23:11:19 +0800660}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
Herbert Xu2d4baff2007-11-26 23:11:19 +0800662/**
663 * __kfree_skb - private function
664 * @skb: buffer
665 *
666 * Free an sk_buff. Release anything attached to the buffer.
667 * Clean the state. This is an internal helper function. Users should
668 * always call kfree_skb
669 */
670
671void __kfree_skb(struct sk_buff *skb)
672{
673 skb_release_all(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 kfree_skbmem(skb);
675}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800676EXPORT_SYMBOL(__kfree_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
678/**
Jörn Engel231d06a2006-03-20 21:28:35 -0800679 * kfree_skb - free an sk_buff
680 * @skb: buffer to free
681 *
682 * Drop a reference to the buffer and free it if the usage count has
683 * hit zero.
684 */
685void kfree_skb(struct sk_buff *skb)
686{
687 if (unlikely(!skb))
688 return;
689 if (likely(atomic_read(&skb->users) == 1))
690 smp_rmb();
691 else if (likely(!atomic_dec_and_test(&skb->users)))
692 return;
Neil Hormanead2ceb2009-03-11 09:49:55 +0000693 trace_kfree_skb(skb, __builtin_return_address(0));
Jörn Engel231d06a2006-03-20 21:28:35 -0800694 __kfree_skb(skb);
695}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800696EXPORT_SYMBOL(kfree_skb);
Jörn Engel231d06a2006-03-20 21:28:35 -0800697
Eric Dumazetbd8a7032013-06-24 06:26:00 -0700698void kfree_skb_list(struct sk_buff *segs)
699{
700 while (segs) {
701 struct sk_buff *next = segs->next;
702
703 kfree_skb(segs);
704 segs = next;
705 }
706}
707EXPORT_SYMBOL(kfree_skb_list);
708
Stephen Hemmingerd1a203e2008-11-01 21:01:09 -0700709/**
Michael S. Tsirkin25121172012-11-01 09:16:28 +0000710 * skb_tx_error - report an sk_buff xmit error
711 * @skb: buffer that triggered an error
712 *
713 * Report xmit error if a device callback is tracking this skb.
714 * skb must be freed afterwards.
715 */
716void skb_tx_error(struct sk_buff *skb)
717{
718 if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) {
719 struct ubuf_info *uarg;
720
721 uarg = skb_shinfo(skb)->destructor_arg;
722 if (uarg->callback)
723 uarg->callback(uarg, false);
724 skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
725 }
726}
727EXPORT_SYMBOL(skb_tx_error);
728
729/**
Neil Hormanead2ceb2009-03-11 09:49:55 +0000730 * consume_skb - free an skbuff
731 * @skb: buffer to free
732 *
733 * Drop a ref to the buffer and free it if the usage count has hit zero
734 * Functions identically to kfree_skb, but kfree_skb assumes that the frame
735 * is being dropped after a failure and notes that
736 */
737void consume_skb(struct sk_buff *skb)
738{
739 if (unlikely(!skb))
740 return;
741 if (likely(atomic_read(&skb->users) == 1))
742 smp_rmb();
743 else if (likely(!atomic_dec_and_test(&skb->users)))
744 return;
Koki Sanagi07dc22e2010-08-23 18:46:12 +0900745 trace_consume_skb(skb);
Neil Hormanead2ceb2009-03-11 09:49:55 +0000746 __kfree_skb(skb);
747}
748EXPORT_SYMBOL(consume_skb);
749
Eric Dumazetb1937222014-09-28 22:18:47 -0700750/* Make sure a field is enclosed inside headers_start/headers_end section */
751#define CHECK_SKB_FIELD(field) \
752 BUILD_BUG_ON(offsetof(struct sk_buff, field) < \
753 offsetof(struct sk_buff, headers_start)); \
754 BUILD_BUG_ON(offsetof(struct sk_buff, field) > \
755 offsetof(struct sk_buff, headers_end)); \
756
Herbert Xudec18812007-10-14 00:37:30 -0700757static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
758{
759 new->tstamp = old->tstamp;
Eric Dumazetb1937222014-09-28 22:18:47 -0700760 /* We do not copy old->sk */
Herbert Xudec18812007-10-14 00:37:30 -0700761 new->dev = old->dev;
Eric Dumazetb1937222014-09-28 22:18:47 -0700762 memcpy(new->cb, old->cb, sizeof(old->cb));
Eric Dumazet7fee2262010-05-11 23:19:48 +0000763 skb_dst_copy(new, old);
Alexey Dobriyandef8b4f2008-10-28 13:24:06 -0700764#ifdef CONFIG_XFRM
Herbert Xudec18812007-10-14 00:37:30 -0700765 new->sp = secpath_get(old->sp);
766#endif
Eric Dumazetb1937222014-09-28 22:18:47 -0700767 __nf_copy(new, old, false);
Patrick McHardy6aa895b02008-07-14 22:49:06 -0700768
Eric Dumazetb1937222014-09-28 22:18:47 -0700769 /* Note : this field could be in headers_start/headers_end section
770 * It is not yet because we do not want to have a 16 bit hole
771 */
772 new->queue_mapping = old->queue_mapping;
Eliezer Tamir06021292013-06-10 11:39:50 +0300773
Eric Dumazetb1937222014-09-28 22:18:47 -0700774 memcpy(&new->headers_start, &old->headers_start,
775 offsetof(struct sk_buff, headers_end) -
776 offsetof(struct sk_buff, headers_start));
777 CHECK_SKB_FIELD(protocol);
778 CHECK_SKB_FIELD(csum);
779 CHECK_SKB_FIELD(hash);
780 CHECK_SKB_FIELD(priority);
781 CHECK_SKB_FIELD(skb_iif);
782 CHECK_SKB_FIELD(vlan_proto);
783 CHECK_SKB_FIELD(vlan_tci);
784 CHECK_SKB_FIELD(transport_header);
785 CHECK_SKB_FIELD(network_header);
786 CHECK_SKB_FIELD(mac_header);
787 CHECK_SKB_FIELD(inner_protocol);
788 CHECK_SKB_FIELD(inner_transport_header);
789 CHECK_SKB_FIELD(inner_network_header);
790 CHECK_SKB_FIELD(inner_mac_header);
791 CHECK_SKB_FIELD(mark);
792#ifdef CONFIG_NETWORK_SECMARK
793 CHECK_SKB_FIELD(secmark);
794#endif
Cong Wange0d10952013-08-01 11:10:25 +0800795#ifdef CONFIG_NET_RX_BUSY_POLL
Eric Dumazetb1937222014-09-28 22:18:47 -0700796 CHECK_SKB_FIELD(napi_id);
Eliezer Tamir06021292013-06-10 11:39:50 +0300797#endif
Eric Dumazet2bd82482015-02-03 23:48:24 -0800798#ifdef CONFIG_XPS
799 CHECK_SKB_FIELD(sender_cpu);
800#endif
Eric Dumazetb1937222014-09-28 22:18:47 -0700801#ifdef CONFIG_NET_SCHED
802 CHECK_SKB_FIELD(tc_index);
803#ifdef CONFIG_NET_CLS_ACT
804 CHECK_SKB_FIELD(tc_verd);
805#endif
806#endif
807
Herbert Xudec18812007-10-14 00:37:30 -0700808}
809
Herbert Xu82c49a32009-05-22 22:11:37 +0000810/*
811 * You should not add any new code to this function. Add it to
812 * __copy_skb_header above instead.
813 */
Herbert Xue0053ec2007-10-14 00:37:52 -0700814static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816#define C(x) n->x = skb->x
817
818 n->next = n->prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 n->sk = NULL;
Herbert Xudec18812007-10-14 00:37:30 -0700820 __copy_skb_header(n, skb);
821
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 C(len);
823 C(data_len);
Alexey Dobriyan3e6b3b22007-03-16 15:00:46 -0700824 C(mac_len);
Patrick McHardy334a8132007-06-25 04:35:20 -0700825 n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
Paul Moore02f1c892008-01-07 21:56:41 -0800826 n->cloned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 n->nohdr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 n->destructor = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 C(tail);
830 C(end);
Paul Moore02f1c892008-01-07 21:56:41 -0800831 C(head);
Eric Dumazetd3836f22012-04-27 00:33:38 +0000832 C(head_frag);
Paul Moore02f1c892008-01-07 21:56:41 -0800833 C(data);
834 C(truesize);
835 atomic_set(&n->users, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
837 atomic_inc(&(skb_shinfo(skb)->dataref));
838 skb->cloned = 1;
839
840 return n;
Herbert Xue0053ec2007-10-14 00:37:52 -0700841#undef C
842}
843
844/**
845 * skb_morph - morph one skb into another
846 * @dst: the skb to receive the contents
847 * @src: the skb to supply the contents
848 *
849 * This is identical to skb_clone except that the target skb is
850 * supplied by the user.
851 *
852 * The target skb is returned upon exit.
853 */
854struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
855{
Herbert Xu2d4baff2007-11-26 23:11:19 +0800856 skb_release_all(dst);
Herbert Xue0053ec2007-10-14 00:37:52 -0700857 return __skb_clone(dst, src);
858}
859EXPORT_SYMBOL_GPL(skb_morph);
860
Ben Hutchings2c530402012-07-10 10:55:09 +0000861/**
862 * skb_copy_ubufs - copy userspace skb frags buffers to kernel
Michael S. Tsirkin48c83012011-08-31 08:03:29 +0000863 * @skb: the skb to modify
864 * @gfp_mask: allocation priority
865 *
866 * This must be called on SKBTX_DEV_ZEROCOPY skb.
867 * It will copy all frags into kernel and drop the reference
868 * to userspace pages.
869 *
870 * If this function is called from an interrupt gfp_mask() must be
871 * %GFP_ATOMIC.
872 *
873 * Returns 0 on success or a negative error code on failure
874 * to allocate kernel memory to copy to.
875 */
876int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask)
Shirley Maa6686f22011-07-06 12:22:12 +0000877{
878 int i;
879 int num_frags = skb_shinfo(skb)->nr_frags;
880 struct page *page, *head = NULL;
881 struct ubuf_info *uarg = skb_shinfo(skb)->destructor_arg;
882
883 for (i = 0; i < num_frags; i++) {
884 u8 *vaddr;
885 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
886
Krishna Kumar02756ed2012-07-17 02:05:29 +0000887 page = alloc_page(gfp_mask);
Shirley Maa6686f22011-07-06 12:22:12 +0000888 if (!page) {
889 while (head) {
Sunghan Suh40dadff2013-07-12 16:17:23 +0900890 struct page *next = (struct page *)page_private(head);
Shirley Maa6686f22011-07-06 12:22:12 +0000891 put_page(head);
892 head = next;
893 }
894 return -ENOMEM;
895 }
Eric Dumazet51c56b02012-04-05 11:35:15 +0200896 vaddr = kmap_atomic(skb_frag_page(f));
Shirley Maa6686f22011-07-06 12:22:12 +0000897 memcpy(page_address(page),
Eric Dumazet9e903e02011-10-18 21:00:24 +0000898 vaddr + f->page_offset, skb_frag_size(f));
Eric Dumazet51c56b02012-04-05 11:35:15 +0200899 kunmap_atomic(vaddr);
Sunghan Suh40dadff2013-07-12 16:17:23 +0900900 set_page_private(page, (unsigned long)head);
Shirley Maa6686f22011-07-06 12:22:12 +0000901 head = page;
902 }
903
904 /* skb frags release userspace buffers */
Krishna Kumar02756ed2012-07-17 02:05:29 +0000905 for (i = 0; i < num_frags; i++)
Ian Campbella8605c62011-10-19 23:01:49 +0000906 skb_frag_unref(skb, i);
Shirley Maa6686f22011-07-06 12:22:12 +0000907
Michael S. Tsirkine19d6762012-11-01 09:16:22 +0000908 uarg->callback(uarg, false);
Shirley Maa6686f22011-07-06 12:22:12 +0000909
910 /* skb frags point to kernel buffers */
Krishna Kumar02756ed2012-07-17 02:05:29 +0000911 for (i = num_frags - 1; i >= 0; i--) {
912 __skb_fill_page_desc(skb, i, head, 0,
913 skb_shinfo(skb)->frags[i].size);
Sunghan Suh40dadff2013-07-12 16:17:23 +0900914 head = (struct page *)page_private(head);
Shirley Maa6686f22011-07-06 12:22:12 +0000915 }
Michael S. Tsirkin48c83012011-08-31 08:03:29 +0000916
917 skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
Shirley Maa6686f22011-07-06 12:22:12 +0000918 return 0;
919}
Michael S. Tsirkindcc0fb72012-07-20 09:23:20 +0000920EXPORT_SYMBOL_GPL(skb_copy_ubufs);
Shirley Maa6686f22011-07-06 12:22:12 +0000921
Herbert Xue0053ec2007-10-14 00:37:52 -0700922/**
923 * skb_clone - duplicate an sk_buff
924 * @skb: buffer to clone
925 * @gfp_mask: allocation priority
926 *
927 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
928 * copies share the same packet data but not structure. The new
929 * buffer has a reference count of 1. If the allocation fails the
930 * function returns %NULL otherwise the new buffer is returned.
931 *
932 * If this function is called from an interrupt gfp_mask() must be
933 * %GFP_ATOMIC.
934 */
935
936struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
937{
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700938 struct sk_buff_fclones *fclones = container_of(skb,
939 struct sk_buff_fclones,
940 skb1);
Eric Dumazet6ffe75e2014-12-03 17:04:39 -0800941 struct sk_buff *n;
Herbert Xue0053ec2007-10-14 00:37:52 -0700942
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +0000943 if (skb_orphan_frags(skb, gfp_mask))
944 return NULL;
Shirley Maa6686f22011-07-06 12:22:12 +0000945
Herbert Xue0053ec2007-10-14 00:37:52 -0700946 if (skb->fclone == SKB_FCLONE_ORIG &&
Eric Dumazet6ffe75e2014-12-03 17:04:39 -0800947 atomic_read(&fclones->fclone_ref) == 1) {
948 n = &fclones->skb2;
949 atomic_set(&fclones->fclone_ref, 2);
Herbert Xue0053ec2007-10-14 00:37:52 -0700950 } else {
Mel Gormanc93bdd02012-07-31 16:44:19 -0700951 if (skb_pfmemalloc(skb))
952 gfp_mask |= __GFP_MEMALLOC;
953
Herbert Xue0053ec2007-10-14 00:37:52 -0700954 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
955 if (!n)
956 return NULL;
Vegard Nossumfe55f6d2008-08-30 12:16:35 +0200957
958 kmemcheck_annotate_bitfield(n, flags1);
Herbert Xue0053ec2007-10-14 00:37:52 -0700959 n->fclone = SKB_FCLONE_UNAVAILABLE;
960 }
961
962 return __skb_clone(n, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800964EXPORT_SYMBOL(skb_clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
Pravin B Shelarf5b17292013-03-07 13:21:40 +0000966static void skb_headers_offset_update(struct sk_buff *skb, int off)
967{
Eric Dumazet030737b2013-10-19 11:42:54 -0700968 /* Only adjust this if it actually is csum_start rather than csum */
969 if (skb->ip_summed == CHECKSUM_PARTIAL)
970 skb->csum_start += off;
Pravin B Shelarf5b17292013-03-07 13:21:40 +0000971 /* {transport,network,mac}_header and tail are relative to skb->head */
972 skb->transport_header += off;
973 skb->network_header += off;
974 if (skb_mac_header_was_set(skb))
975 skb->mac_header += off;
976 skb->inner_transport_header += off;
977 skb->inner_network_header += off;
Pravin B Shelaraefbd2b2013-03-07 13:21:46 +0000978 skb->inner_mac_header += off;
Pravin B Shelarf5b17292013-03-07 13:21:40 +0000979}
980
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
982{
Herbert Xudec18812007-10-14 00:37:30 -0700983 __copy_skb_header(new, old);
984
Herbert Xu79671682006-06-22 02:40:14 -0700985 skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
986 skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
987 skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988}
989
Mel Gormanc93bdd02012-07-31 16:44:19 -0700990static inline int skb_alloc_rx_flag(const struct sk_buff *skb)
991{
992 if (skb_pfmemalloc(skb))
993 return SKB_ALLOC_RX;
994 return 0;
995}
996
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997/**
998 * skb_copy - create private copy of an sk_buff
999 * @skb: buffer to copy
1000 * @gfp_mask: allocation priority
1001 *
1002 * Make a copy of both an &sk_buff and its data. This is used when the
1003 * caller wishes to modify the data and needs a private copy of the
1004 * data to alter. Returns %NULL on failure or the pointer to the buffer
1005 * on success. The returned buffer has a reference count of 1.
1006 *
1007 * As by-product this function converts non-linear &sk_buff to linear
1008 * one, so that &sk_buff becomes completely private and caller is allowed
1009 * to modify all the data of returned buffer. This means that this
1010 * function is not recommended for use in circumstances when only
1011 * header is going to be modified. Use pskb_copy() instead.
1012 */
1013
Al Virodd0fc662005-10-07 07:46:04 +01001014struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015{
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001016 int headerlen = skb_headroom(skb);
Alexander Duyckec47ea82012-05-04 14:26:56 +00001017 unsigned int size = skb_end_offset(skb) + skb->data_len;
Mel Gormanc93bdd02012-07-31 16:44:19 -07001018 struct sk_buff *n = __alloc_skb(size, gfp_mask,
1019 skb_alloc_rx_flag(skb), NUMA_NO_NODE);
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001020
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 if (!n)
1022 return NULL;
1023
1024 /* Set the data pointer */
1025 skb_reserve(n, headerlen);
1026 /* Set the tail pointer and length */
1027 skb_put(n, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
1029 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
1030 BUG();
1031
1032 copy_skb_header(n, skb);
1033 return n;
1034}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001035EXPORT_SYMBOL(skb_copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
1037/**
Octavian Purdilabad93e92014-06-12 01:36:26 +03001038 * __pskb_copy_fclone - create copy of an sk_buff with private head.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 * @skb: buffer to copy
Eric Dumazet117632e2011-12-03 21:39:53 +00001040 * @headroom: headroom of new skb
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 * @gfp_mask: allocation priority
Octavian Purdilabad93e92014-06-12 01:36:26 +03001042 * @fclone: if true allocate the copy of the skb from the fclone
1043 * cache instead of the head cache; it is recommended to set this
1044 * to true for the cases where the copy will likely be cloned
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 *
1046 * Make a copy of both an &sk_buff and part of its data, located
1047 * in header. Fragmented data remain shared. This is used when
1048 * the caller wishes to modify only header of &sk_buff and needs
1049 * private copy of the header to alter. Returns %NULL on failure
1050 * or the pointer to the buffer on success.
1051 * The returned buffer has a reference count of 1.
1052 */
1053
Octavian Purdilabad93e92014-06-12 01:36:26 +03001054struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom,
1055 gfp_t gfp_mask, bool fclone)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056{
Eric Dumazet117632e2011-12-03 21:39:53 +00001057 unsigned int size = skb_headlen(skb) + headroom;
Octavian Purdilabad93e92014-06-12 01:36:26 +03001058 int flags = skb_alloc_rx_flag(skb) | (fclone ? SKB_ALLOC_FCLONE : 0);
1059 struct sk_buff *n = __alloc_skb(size, gfp_mask, flags, NUMA_NO_NODE);
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001060
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 if (!n)
1062 goto out;
1063
1064 /* Set the data pointer */
Eric Dumazet117632e2011-12-03 21:39:53 +00001065 skb_reserve(n, headroom);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 /* Set the tail pointer and length */
1067 skb_put(n, skb_headlen(skb));
1068 /* Copy the bytes */
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001069 skb_copy_from_linear_data(skb, n->data, n->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
Herbert Xu25f484a2006-11-07 14:57:15 -08001071 n->truesize += skb->data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 n->data_len = skb->data_len;
1073 n->len = skb->len;
1074
1075 if (skb_shinfo(skb)->nr_frags) {
1076 int i;
1077
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +00001078 if (skb_orphan_frags(skb, gfp_mask)) {
1079 kfree_skb(n);
1080 n = NULL;
1081 goto out;
Shirley Maa6686f22011-07-06 12:22:12 +00001082 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1084 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
Ian Campbellea2ab692011-08-22 23:44:58 +00001085 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 }
1087 skb_shinfo(n)->nr_frags = i;
1088 }
1089
David S. Miller21dc3302010-08-23 00:13:46 -07001090 if (skb_has_frag_list(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
1092 skb_clone_fraglist(n);
1093 }
1094
1095 copy_skb_header(n, skb);
1096out:
1097 return n;
1098}
Octavian Purdilabad93e92014-06-12 01:36:26 +03001099EXPORT_SYMBOL(__pskb_copy_fclone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
1101/**
1102 * pskb_expand_head - reallocate header of &sk_buff
1103 * @skb: buffer to reallocate
1104 * @nhead: room to add at head
1105 * @ntail: room to add at tail
1106 * @gfp_mask: allocation priority
1107 *
Mathias Krausebc323832013-11-07 14:18:26 +01001108 * Expands (or creates identical copy, if @nhead and @ntail are zero)
1109 * header of @skb. &sk_buff itself is not changed. &sk_buff MUST have
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 * reference count of 1. Returns zero in the case of success or error,
1111 * if expansion failed. In the last case, &sk_buff is not changed.
1112 *
1113 * All the pointers pointing into skb header may change and must be
1114 * reloaded after call to this function.
1115 */
1116
Victor Fusco86a76ca2005-07-08 14:57:47 -07001117int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
Al Virodd0fc662005-10-07 07:46:04 +01001118 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119{
1120 int i;
1121 u8 *data;
Alexander Duyckec47ea82012-05-04 14:26:56 +00001122 int size = nhead + skb_end_offset(skb) + ntail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 long off;
1124
Herbert Xu4edd87a2008-10-01 07:09:38 -07001125 BUG_ON(nhead < 0);
1126
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 if (skb_shared(skb))
1128 BUG();
1129
1130 size = SKB_DATA_ALIGN(size);
1131
Mel Gormanc93bdd02012-07-31 16:44:19 -07001132 if (skb_pfmemalloc(skb))
1133 gfp_mask |= __GFP_MEMALLOC;
1134 data = kmalloc_reserve(size + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
1135 gfp_mask, NUMA_NO_NODE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 if (!data)
1137 goto nodata;
Eric Dumazet87151b82012-04-10 20:08:39 +00001138 size = SKB_WITH_OVERHEAD(ksize(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
1140 /* Copy only real data... and, alas, header. This should be
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001141 * optimized for the cases when header is void.
1142 */
1143 memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head);
1144
1145 memcpy((struct skb_shared_info *)(data + size),
1146 skb_shinfo(skb),
Eric Dumazetfed66382010-07-22 19:09:08 +00001147 offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
Alexander Duyck3e245912012-05-04 14:26:51 +00001149 /*
1150 * if shinfo is shared we must drop the old head gracefully, but if it
1151 * is not we can just drop the old head and let the existing refcount
1152 * be since all we did is relocate the values
1153 */
1154 if (skb_cloned(skb)) {
Shirley Maa6686f22011-07-06 12:22:12 +00001155 /* copy this zero copy skb frags */
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +00001156 if (skb_orphan_frags(skb, gfp_mask))
1157 goto nofrags;
Eric Dumazet1fd63042010-09-02 23:09:32 +00001158 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
Ian Campbellea2ab692011-08-22 23:44:58 +00001159 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
Eric Dumazet1fd63042010-09-02 23:09:32 +00001161 if (skb_has_frag_list(skb))
1162 skb_clone_fraglist(skb);
1163
1164 skb_release_data(skb);
Alexander Duyck3e245912012-05-04 14:26:51 +00001165 } else {
1166 skb_free_head(skb);
Eric Dumazet1fd63042010-09-02 23:09:32 +00001167 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 off = (data + nhead) - skb->head;
1169
1170 skb->head = data;
Eric Dumazetd3836f22012-04-27 00:33:38 +00001171 skb->head_frag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 skb->data += off;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001173#ifdef NET_SKBUFF_DATA_USES_OFFSET
1174 skb->end = size;
Patrick McHardy56eb8882007-04-09 11:45:04 -07001175 off = nhead;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001176#else
1177 skb->end = skb->head + size;
Patrick McHardy56eb8882007-04-09 11:45:04 -07001178#endif
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001179 skb->tail += off;
Peter Pan(潘卫平)b41abb42013-06-06 21:27:21 +08001180 skb_headers_offset_update(skb, nhead);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 skb->cloned = 0;
Patrick McHardy334a8132007-06-25 04:35:20 -07001182 skb->hdr_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 skb->nohdr = 0;
1184 atomic_set(&skb_shinfo(skb)->dataref, 1);
1185 return 0;
1186
Shirley Maa6686f22011-07-06 12:22:12 +00001187nofrags:
1188 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189nodata:
1190 return -ENOMEM;
1191}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001192EXPORT_SYMBOL(pskb_expand_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
1194/* Make private copy of skb with writable head and some headroom */
1195
1196struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
1197{
1198 struct sk_buff *skb2;
1199 int delta = headroom - skb_headroom(skb);
1200
1201 if (delta <= 0)
1202 skb2 = pskb_copy(skb, GFP_ATOMIC);
1203 else {
1204 skb2 = skb_clone(skb, GFP_ATOMIC);
1205 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
1206 GFP_ATOMIC)) {
1207 kfree_skb(skb2);
1208 skb2 = NULL;
1209 }
1210 }
1211 return skb2;
1212}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001213EXPORT_SYMBOL(skb_realloc_headroom);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
1215/**
1216 * skb_copy_expand - copy and expand sk_buff
1217 * @skb: buffer to copy
1218 * @newheadroom: new free bytes at head
1219 * @newtailroom: new free bytes at tail
1220 * @gfp_mask: allocation priority
1221 *
1222 * Make a copy of both an &sk_buff and its data and while doing so
1223 * allocate additional space.
1224 *
1225 * This is used when the caller wishes to modify the data and needs a
1226 * private copy of the data to alter as well as more space for new fields.
1227 * Returns %NULL on failure or the pointer to the buffer
1228 * on success. The returned buffer has a reference count of 1.
1229 *
1230 * You must pass %GFP_ATOMIC as the allocation priority if this function
1231 * is called from an interrupt.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 */
1233struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
Victor Fusco86a76ca2005-07-08 14:57:47 -07001234 int newheadroom, int newtailroom,
Al Virodd0fc662005-10-07 07:46:04 +01001235 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236{
1237 /*
1238 * Allocate the copy buffer
1239 */
Mel Gormanc93bdd02012-07-31 16:44:19 -07001240 struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom,
1241 gfp_mask, skb_alloc_rx_flag(skb),
1242 NUMA_NO_NODE);
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001243 int oldheadroom = skb_headroom(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 int head_copy_len, head_copy_off;
1245
1246 if (!n)
1247 return NULL;
1248
1249 skb_reserve(n, newheadroom);
1250
1251 /* Set the tail pointer and length */
1252 skb_put(n, skb->len);
1253
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001254 head_copy_len = oldheadroom;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 head_copy_off = 0;
1256 if (newheadroom <= head_copy_len)
1257 head_copy_len = newheadroom;
1258 else
1259 head_copy_off = newheadroom - head_copy_len;
1260
1261 /* Copy the linear header and data. */
1262 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
1263 skb->len + head_copy_len))
1264 BUG();
1265
1266 copy_skb_header(n, skb);
1267
Eric Dumazet030737b2013-10-19 11:42:54 -07001268 skb_headers_offset_update(n, newheadroom - oldheadroom);
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001269
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 return n;
1271}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001272EXPORT_SYMBOL(skb_copy_expand);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
1274/**
1275 * skb_pad - zero pad the tail of an skb
1276 * @skb: buffer to pad
1277 * @pad: space to pad
1278 *
1279 * Ensure that a buffer is followed by a padding area that is zero
1280 * filled. Used by network drivers which may DMA or transfer data
1281 * beyond the buffer end onto the wire.
1282 *
Herbert Xu5b057c62006-06-23 02:06:41 -07001283 * May return error in out of memory cases. The skb is freed on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001285
Herbert Xu5b057c62006-06-23 02:06:41 -07001286int skb_pad(struct sk_buff *skb, int pad)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287{
Herbert Xu5b057c62006-06-23 02:06:41 -07001288 int err;
1289 int ntail;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001290
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 /* If the skbuff is non linear tailroom is always zero.. */
Herbert Xu5b057c62006-06-23 02:06:41 -07001292 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 memset(skb->data+skb->len, 0, pad);
Herbert Xu5b057c62006-06-23 02:06:41 -07001294 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 }
Herbert Xu5b057c62006-06-23 02:06:41 -07001296
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001297 ntail = skb->data_len + pad - (skb->end - skb->tail);
Herbert Xu5b057c62006-06-23 02:06:41 -07001298 if (likely(skb_cloned(skb) || ntail > 0)) {
1299 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
1300 if (unlikely(err))
1301 goto free_skb;
1302 }
1303
1304 /* FIXME: The use of this function with non-linear skb's really needs
1305 * to be audited.
1306 */
1307 err = skb_linearize(skb);
1308 if (unlikely(err))
1309 goto free_skb;
1310
1311 memset(skb->data + skb->len, 0, pad);
1312 return 0;
1313
1314free_skb:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 kfree_skb(skb);
Herbert Xu5b057c62006-06-23 02:06:41 -07001316 return err;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001317}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001318EXPORT_SYMBOL(skb_pad);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001319
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -07001320/**
Mathias Krause0c7ddf32013-11-07 14:18:24 +01001321 * pskb_put - add data to the tail of a potentially fragmented buffer
1322 * @skb: start of the buffer to use
1323 * @tail: tail fragment of the buffer to use
1324 * @len: amount of data to add
1325 *
1326 * This function extends the used data area of the potentially
1327 * fragmented buffer. @tail must be the last fragment of @skb -- or
1328 * @skb itself. If this would exceed the total buffer size the kernel
1329 * will panic. A pointer to the first byte of the extra data is
1330 * returned.
1331 */
1332
1333unsigned char *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len)
1334{
1335 if (tail != skb) {
1336 skb->data_len += len;
1337 skb->len += len;
1338 }
1339 return skb_put(tail, len);
1340}
1341EXPORT_SYMBOL_GPL(pskb_put);
1342
1343/**
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -07001344 * skb_put - add data to a buffer
1345 * @skb: buffer to use
1346 * @len: amount of data to add
1347 *
1348 * This function extends the used data area of the buffer. If this would
1349 * exceed the total buffer size the kernel will panic. A pointer to the
1350 * first byte of the extra data is returned.
1351 */
1352unsigned char *skb_put(struct sk_buff *skb, unsigned int len)
1353{
1354 unsigned char *tmp = skb_tail_pointer(skb);
1355 SKB_LINEAR_ASSERT(skb);
1356 skb->tail += len;
1357 skb->len += len;
1358 if (unlikely(skb->tail > skb->end))
1359 skb_over_panic(skb, len, __builtin_return_address(0));
1360 return tmp;
1361}
1362EXPORT_SYMBOL(skb_put);
1363
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001364/**
Ilpo Järvinenc2aa2702008-03-27 17:52:40 -07001365 * skb_push - add data to the start of a buffer
1366 * @skb: buffer to use
1367 * @len: amount of data to add
1368 *
1369 * This function extends the used data area of the buffer at the buffer
1370 * start. If this would exceed the total buffer headroom the kernel will
1371 * panic. A pointer to the first byte of the extra data is returned.
1372 */
1373unsigned char *skb_push(struct sk_buff *skb, unsigned int len)
1374{
1375 skb->data -= len;
1376 skb->len += len;
1377 if (unlikely(skb->data<skb->head))
1378 skb_under_panic(skb, len, __builtin_return_address(0));
1379 return skb->data;
1380}
1381EXPORT_SYMBOL(skb_push);
1382
1383/**
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001384 * skb_pull - remove data from the start of a buffer
1385 * @skb: buffer to use
1386 * @len: amount of data to remove
1387 *
1388 * This function removes data from the start of a buffer, returning
1389 * the memory to the headroom. A pointer to the next data in the buffer
1390 * is returned. Once the data has been pulled future pushes will overwrite
1391 * the old data.
1392 */
1393unsigned char *skb_pull(struct sk_buff *skb, unsigned int len)
1394{
David S. Miller47d29642010-05-02 02:21:44 -07001395 return skb_pull_inline(skb, len);
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001396}
1397EXPORT_SYMBOL(skb_pull);
1398
Ilpo Järvinen419ae742008-03-27 17:54:01 -07001399/**
1400 * skb_trim - remove end from a buffer
1401 * @skb: buffer to alter
1402 * @len: new length
1403 *
1404 * Cut the length of a buffer down by removing data from the tail. If
1405 * the buffer is already under the length specified it is not modified.
1406 * The skb must be linear.
1407 */
1408void skb_trim(struct sk_buff *skb, unsigned int len)
1409{
1410 if (skb->len > len)
1411 __skb_trim(skb, len);
1412}
1413EXPORT_SYMBOL(skb_trim);
1414
Herbert Xu3cc0e872006-06-09 16:13:38 -07001415/* Trims skb to length len. It can change skb pointers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 */
1417
Herbert Xu3cc0e872006-06-09 16:13:38 -07001418int ___pskb_trim(struct sk_buff *skb, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419{
Herbert Xu27b437c2006-07-13 19:26:39 -07001420 struct sk_buff **fragp;
1421 struct sk_buff *frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 int offset = skb_headlen(skb);
1423 int nfrags = skb_shinfo(skb)->nr_frags;
1424 int i;
Herbert Xu27b437c2006-07-13 19:26:39 -07001425 int err;
1426
1427 if (skb_cloned(skb) &&
1428 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
1429 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001431 i = 0;
1432 if (offset >= len)
1433 goto drop_pages;
1434
1435 for (; i < nfrags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001436 int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]);
Herbert Xu27b437c2006-07-13 19:26:39 -07001437
1438 if (end < len) {
1439 offset = end;
1440 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 }
Herbert Xu27b437c2006-07-13 19:26:39 -07001442
Eric Dumazet9e903e02011-10-18 21:00:24 +00001443 skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset);
Herbert Xu27b437c2006-07-13 19:26:39 -07001444
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001445drop_pages:
Herbert Xu27b437c2006-07-13 19:26:39 -07001446 skb_shinfo(skb)->nr_frags = i;
1447
1448 for (; i < nfrags; i++)
Ian Campbellea2ab692011-08-22 23:44:58 +00001449 skb_frag_unref(skb, i);
Herbert Xu27b437c2006-07-13 19:26:39 -07001450
David S. Miller21dc3302010-08-23 00:13:46 -07001451 if (skb_has_frag_list(skb))
Herbert Xu27b437c2006-07-13 19:26:39 -07001452 skb_drop_fraglist(skb);
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001453 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 }
1455
Herbert Xu27b437c2006-07-13 19:26:39 -07001456 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
1457 fragp = &frag->next) {
1458 int end = offset + frag->len;
1459
1460 if (skb_shared(frag)) {
1461 struct sk_buff *nfrag;
1462
1463 nfrag = skb_clone(frag, GFP_ATOMIC);
1464 if (unlikely(!nfrag))
1465 return -ENOMEM;
1466
1467 nfrag->next = frag->next;
Eric Dumazet85bb2a62012-04-19 02:24:53 +00001468 consume_skb(frag);
Herbert Xu27b437c2006-07-13 19:26:39 -07001469 frag = nfrag;
1470 *fragp = frag;
1471 }
1472
1473 if (end < len) {
1474 offset = end;
1475 continue;
1476 }
1477
1478 if (end > len &&
1479 unlikely((err = pskb_trim(frag, len - offset))))
1480 return err;
1481
1482 if (frag->next)
1483 skb_drop_list(&frag->next);
1484 break;
1485 }
1486
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001487done:
Herbert Xu27b437c2006-07-13 19:26:39 -07001488 if (len > skb_headlen(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 skb->data_len -= skb->len - len;
1490 skb->len = len;
1491 } else {
Herbert Xu27b437c2006-07-13 19:26:39 -07001492 skb->len = len;
1493 skb->data_len = 0;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001494 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 }
1496
1497 return 0;
1498}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001499EXPORT_SYMBOL(___pskb_trim);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500
1501/**
1502 * __pskb_pull_tail - advance tail of skb header
1503 * @skb: buffer to reallocate
1504 * @delta: number of bytes to advance tail
1505 *
1506 * The function makes a sense only on a fragmented &sk_buff,
1507 * it expands header moving its tail forward and copying necessary
1508 * data from fragmented part.
1509 *
1510 * &sk_buff MUST have reference count of 1.
1511 *
1512 * Returns %NULL (and &sk_buff does not change) if pull failed
1513 * or value of new tail of skb in the case of success.
1514 *
1515 * All the pointers pointing into skb header may change and must be
1516 * reloaded after call to this function.
1517 */
1518
1519/* Moves tail of skb head forward, copying data from fragmented part,
1520 * when it is necessary.
1521 * 1. It may fail due to malloc failure.
1522 * 2. It may change skb pointers.
1523 *
1524 * It is pretty complicated. Luckily, it is called only in exceptional cases.
1525 */
1526unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
1527{
1528 /* If skb has not enough free space at tail, get new one
1529 * plus 128 bytes for future expansions. If we have enough
1530 * room at tail, reallocate without expansion only if skb is cloned.
1531 */
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001532 int i, k, eat = (skb->tail + delta) - skb->end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533
1534 if (eat > 0 || skb_cloned(skb)) {
1535 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
1536 GFP_ATOMIC))
1537 return NULL;
1538 }
1539
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001540 if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 BUG();
1542
1543 /* Optimization: no fragments, no reasons to preestimate
1544 * size of pulled pages. Superb.
1545 */
David S. Miller21dc3302010-08-23 00:13:46 -07001546 if (!skb_has_frag_list(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 goto pull_pages;
1548
1549 /* Estimate size of pulled pages. */
1550 eat = delta;
1551 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001552 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1553
1554 if (size >= eat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 goto pull_pages;
Eric Dumazet9e903e02011-10-18 21:00:24 +00001556 eat -= size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 }
1558
1559 /* If we need update frag list, we are in troubles.
1560 * Certainly, it possible to add an offset to skb data,
1561 * but taking into account that pulling is expected to
1562 * be very rare operation, it is worth to fight against
1563 * further bloating skb head and crucify ourselves here instead.
1564 * Pure masohism, indeed. 8)8)
1565 */
1566 if (eat) {
1567 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1568 struct sk_buff *clone = NULL;
1569 struct sk_buff *insp = NULL;
1570
1571 do {
Kris Katterjohn09a62662006-01-08 22:24:28 -08001572 BUG_ON(!list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573
1574 if (list->len <= eat) {
1575 /* Eaten as whole. */
1576 eat -= list->len;
1577 list = list->next;
1578 insp = list;
1579 } else {
1580 /* Eaten partially. */
1581
1582 if (skb_shared(list)) {
1583 /* Sucks! We need to fork list. :-( */
1584 clone = skb_clone(list, GFP_ATOMIC);
1585 if (!clone)
1586 return NULL;
1587 insp = list->next;
1588 list = clone;
1589 } else {
1590 /* This may be pulled without
1591 * problems. */
1592 insp = list;
1593 }
1594 if (!pskb_pull(list, eat)) {
Wei Yongjunf3fbbe02009-02-25 00:37:32 +00001595 kfree_skb(clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 return NULL;
1597 }
1598 break;
1599 }
1600 } while (eat);
1601
1602 /* Free pulled out fragments. */
1603 while ((list = skb_shinfo(skb)->frag_list) != insp) {
1604 skb_shinfo(skb)->frag_list = list->next;
1605 kfree_skb(list);
1606 }
1607 /* And insert new clone at head. */
1608 if (clone) {
1609 clone->next = list;
1610 skb_shinfo(skb)->frag_list = clone;
1611 }
1612 }
1613 /* Success! Now we may commit changes to skb data. */
1614
1615pull_pages:
1616 eat = delta;
1617 k = 0;
1618 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001619 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1620
1621 if (size <= eat) {
Ian Campbellea2ab692011-08-22 23:44:58 +00001622 skb_frag_unref(skb, i);
Eric Dumazet9e903e02011-10-18 21:00:24 +00001623 eat -= size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 } else {
1625 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
1626 if (eat) {
1627 skb_shinfo(skb)->frags[k].page_offset += eat;
Eric Dumazet9e903e02011-10-18 21:00:24 +00001628 skb_frag_size_sub(&skb_shinfo(skb)->frags[k], eat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 eat = 0;
1630 }
1631 k++;
1632 }
1633 }
1634 skb_shinfo(skb)->nr_frags = k;
1635
1636 skb->tail += delta;
1637 skb->data_len -= delta;
1638
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001639 return skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001641EXPORT_SYMBOL(__pskb_pull_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642
Eric Dumazet22019b12011-07-29 18:37:31 +00001643/**
1644 * skb_copy_bits - copy bits from skb to kernel buffer
1645 * @skb: source skb
1646 * @offset: offset in source
1647 * @to: destination buffer
1648 * @len: number of bytes to copy
1649 *
1650 * Copy the specified number of bytes from the source skb to the
1651 * destination buffer.
1652 *
1653 * CAUTION ! :
1654 * If its prototype is ever changed,
1655 * check arch/{*}/net/{*}.S files,
1656 * since it is called from BPF assembly code.
1657 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
1659{
David S. Miller1a028e52007-04-27 15:21:23 -07001660 int start = skb_headlen(skb);
David S. Millerfbb398a2009-06-09 00:18:59 -07001661 struct sk_buff *frag_iter;
1662 int i, copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663
1664 if (offset > (int)skb->len - len)
1665 goto fault;
1666
1667 /* Copy header. */
David S. Miller1a028e52007-04-27 15:21:23 -07001668 if ((copy = start - offset) > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 if (copy > len)
1670 copy = len;
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001671 skb_copy_from_linear_data_offset(skb, offset, to, copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 if ((len -= copy) == 0)
1673 return 0;
1674 offset += copy;
1675 to += copy;
1676 }
1677
1678 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07001679 int end;
Eric Dumazet51c56b02012-04-05 11:35:15 +02001680 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001682 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07001683
Eric Dumazet51c56b02012-04-05 11:35:15 +02001684 end = start + skb_frag_size(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 if ((copy = end - offset) > 0) {
1686 u8 *vaddr;
1687
1688 if (copy > len)
1689 copy = len;
1690
Eric Dumazet51c56b02012-04-05 11:35:15 +02001691 vaddr = kmap_atomic(skb_frag_page(f));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 memcpy(to,
Eric Dumazet51c56b02012-04-05 11:35:15 +02001693 vaddr + f->page_offset + offset - start,
1694 copy);
1695 kunmap_atomic(vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696
1697 if ((len -= copy) == 0)
1698 return 0;
1699 offset += copy;
1700 to += copy;
1701 }
David S. Miller1a028e52007-04-27 15:21:23 -07001702 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 }
1704
David S. Millerfbb398a2009-06-09 00:18:59 -07001705 skb_walk_frags(skb, frag_iter) {
1706 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707
David S. Millerfbb398a2009-06-09 00:18:59 -07001708 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709
David S. Millerfbb398a2009-06-09 00:18:59 -07001710 end = start + frag_iter->len;
1711 if ((copy = end - offset) > 0) {
1712 if (copy > len)
1713 copy = len;
1714 if (skb_copy_bits(frag_iter, offset - start, to, copy))
1715 goto fault;
1716 if ((len -= copy) == 0)
1717 return 0;
1718 offset += copy;
1719 to += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 }
David S. Millerfbb398a2009-06-09 00:18:59 -07001721 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 }
Shirley Maa6686f22011-07-06 12:22:12 +00001723
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 if (!len)
1725 return 0;
1726
1727fault:
1728 return -EFAULT;
1729}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001730EXPORT_SYMBOL(skb_copy_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731
Jens Axboe9c55e012007-11-06 23:30:13 -08001732/*
1733 * Callback from splice_to_pipe(), if we need to release some pages
1734 * at the end of the spd in case we error'ed out in filling the pipe.
1735 */
1736static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
1737{
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001738 put_page(spd->pages[i]);
1739}
Jens Axboe9c55e012007-11-06 23:30:13 -08001740
David S. Millera108d5f2012-04-23 23:06:11 -04001741static struct page *linear_to_page(struct page *page, unsigned int *len,
1742 unsigned int *offset,
Eric Dumazet18aafc62013-01-11 14:46:37 +00001743 struct sock *sk)
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001744{
Eric Dumazet5640f762012-09-23 23:04:42 +00001745 struct page_frag *pfrag = sk_page_frag(sk);
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001746
Eric Dumazet5640f762012-09-23 23:04:42 +00001747 if (!sk_page_frag_refill(sk, pfrag))
1748 return NULL;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001749
Eric Dumazet5640f762012-09-23 23:04:42 +00001750 *len = min_t(unsigned int, *len, pfrag->size - pfrag->offset);
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001751
Eric Dumazet5640f762012-09-23 23:04:42 +00001752 memcpy(page_address(pfrag->page) + pfrag->offset,
1753 page_address(page) + *offset, *len);
1754 *offset = pfrag->offset;
1755 pfrag->offset += *len;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001756
Eric Dumazet5640f762012-09-23 23:04:42 +00001757 return pfrag->page;
Jens Axboe9c55e012007-11-06 23:30:13 -08001758}
1759
Eric Dumazet41c73a02012-04-22 12:26:16 +00001760static bool spd_can_coalesce(const struct splice_pipe_desc *spd,
1761 struct page *page,
1762 unsigned int offset)
1763{
1764 return spd->nr_pages &&
1765 spd->pages[spd->nr_pages - 1] == page &&
1766 (spd->partial[spd->nr_pages - 1].offset +
1767 spd->partial[spd->nr_pages - 1].len == offset);
1768}
1769
Jens Axboe9c55e012007-11-06 23:30:13 -08001770/*
1771 * Fill page/offset/length into spd, if it can hold more pages.
1772 */
David S. Millera108d5f2012-04-23 23:06:11 -04001773static bool spd_fill_page(struct splice_pipe_desc *spd,
1774 struct pipe_inode_info *pipe, struct page *page,
1775 unsigned int *len, unsigned int offset,
Eric Dumazet18aafc62013-01-11 14:46:37 +00001776 bool linear,
David S. Millera108d5f2012-04-23 23:06:11 -04001777 struct sock *sk)
Jens Axboe9c55e012007-11-06 23:30:13 -08001778{
Eric Dumazet41c73a02012-04-22 12:26:16 +00001779 if (unlikely(spd->nr_pages == MAX_SKB_FRAGS))
David S. Millera108d5f2012-04-23 23:06:11 -04001780 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08001781
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001782 if (linear) {
Eric Dumazet18aafc62013-01-11 14:46:37 +00001783 page = linear_to_page(page, len, &offset, sk);
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001784 if (!page)
David S. Millera108d5f2012-04-23 23:06:11 -04001785 return true;
Eric Dumazet41c73a02012-04-22 12:26:16 +00001786 }
1787 if (spd_can_coalesce(spd, page, offset)) {
1788 spd->partial[spd->nr_pages - 1].len += *len;
David S. Millera108d5f2012-04-23 23:06:11 -04001789 return false;
Eric Dumazet41c73a02012-04-22 12:26:16 +00001790 }
1791 get_page(page);
Jens Axboe9c55e012007-11-06 23:30:13 -08001792 spd->pages[spd->nr_pages] = page;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001793 spd->partial[spd->nr_pages].len = *len;
Jens Axboe9c55e012007-11-06 23:30:13 -08001794 spd->partial[spd->nr_pages].offset = offset;
Jens Axboe9c55e012007-11-06 23:30:13 -08001795 spd->nr_pages++;
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001796
David S. Millera108d5f2012-04-23 23:06:11 -04001797 return false;
Jens Axboe9c55e012007-11-06 23:30:13 -08001798}
1799
David S. Millera108d5f2012-04-23 23:06:11 -04001800static bool __splice_segment(struct page *page, unsigned int poff,
1801 unsigned int plen, unsigned int *off,
Eric Dumazet18aafc62013-01-11 14:46:37 +00001802 unsigned int *len,
Eric Dumazetd7ccf7c2012-04-23 23:35:04 -04001803 struct splice_pipe_desc *spd, bool linear,
David S. Millera108d5f2012-04-23 23:06:11 -04001804 struct sock *sk,
1805 struct pipe_inode_info *pipe)
Octavian Purdila2870c432008-07-15 00:49:11 -07001806{
1807 if (!*len)
David S. Millera108d5f2012-04-23 23:06:11 -04001808 return true;
Octavian Purdila2870c432008-07-15 00:49:11 -07001809
1810 /* skip this segment if already processed */
1811 if (*off >= plen) {
1812 *off -= plen;
David S. Millera108d5f2012-04-23 23:06:11 -04001813 return false;
Octavian Purdiladb43a282008-06-27 17:27:21 -07001814 }
Jens Axboe9c55e012007-11-06 23:30:13 -08001815
Octavian Purdila2870c432008-07-15 00:49:11 -07001816 /* ignore any bits we already processed */
Eric Dumazet9ca1b222013-01-05 21:31:18 +00001817 poff += *off;
1818 plen -= *off;
1819 *off = 0;
Octavian Purdila2870c432008-07-15 00:49:11 -07001820
Eric Dumazet18aafc62013-01-11 14:46:37 +00001821 do {
1822 unsigned int flen = min(*len, plen);
Octavian Purdila2870c432008-07-15 00:49:11 -07001823
Eric Dumazet18aafc62013-01-11 14:46:37 +00001824 if (spd_fill_page(spd, pipe, page, &flen, poff,
1825 linear, sk))
1826 return true;
1827 poff += flen;
1828 plen -= flen;
1829 *len -= flen;
1830 } while (*len && plen);
Octavian Purdila2870c432008-07-15 00:49:11 -07001831
David S. Millera108d5f2012-04-23 23:06:11 -04001832 return false;
Octavian Purdila2870c432008-07-15 00:49:11 -07001833}
1834
1835/*
David S. Millera108d5f2012-04-23 23:06:11 -04001836 * Map linear and fragment data from the skb to spd. It reports true if the
Octavian Purdila2870c432008-07-15 00:49:11 -07001837 * pipe is full or if we already spliced the requested length.
1838 */
David S. Millera108d5f2012-04-23 23:06:11 -04001839static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe,
1840 unsigned int *offset, unsigned int *len,
1841 struct splice_pipe_desc *spd, struct sock *sk)
Octavian Purdila2870c432008-07-15 00:49:11 -07001842{
1843 int seg;
1844
Eric Dumazet1d0c0b32012-04-27 02:10:03 +00001845 /* map the linear part :
Alexander Duyck2996d312012-05-02 18:18:42 +00001846 * If skb->head_frag is set, this 'linear' part is backed by a
1847 * fragment, and if the head is not shared with any clones then
1848 * we can avoid a copy since we own the head portion of this page.
Jens Axboe9c55e012007-11-06 23:30:13 -08001849 */
Octavian Purdila2870c432008-07-15 00:49:11 -07001850 if (__splice_segment(virt_to_page(skb->data),
1851 (unsigned long) skb->data & (PAGE_SIZE - 1),
1852 skb_headlen(skb),
Eric Dumazet18aafc62013-01-11 14:46:37 +00001853 offset, len, spd,
Alexander Duyck3a7c1ee42012-05-03 01:09:42 +00001854 skb_head_is_locked(skb),
Eric Dumazet1d0c0b32012-04-27 02:10:03 +00001855 sk, pipe))
David S. Millera108d5f2012-04-23 23:06:11 -04001856 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08001857
1858 /*
1859 * then map the fragments
1860 */
Jens Axboe9c55e012007-11-06 23:30:13 -08001861 for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
1862 const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
1863
Ian Campbellea2ab692011-08-22 23:44:58 +00001864 if (__splice_segment(skb_frag_page(f),
Eric Dumazet9e903e02011-10-18 21:00:24 +00001865 f->page_offset, skb_frag_size(f),
Eric Dumazet18aafc62013-01-11 14:46:37 +00001866 offset, len, spd, false, sk, pipe))
David S. Millera108d5f2012-04-23 23:06:11 -04001867 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08001868 }
1869
David S. Millera108d5f2012-04-23 23:06:11 -04001870 return false;
Jens Axboe9c55e012007-11-06 23:30:13 -08001871}
1872
Hannes Frederic Sowaa60e3cc2015-05-21 17:00:00 +02001873ssize_t skb_socket_splice(struct sock *sk,
1874 struct pipe_inode_info *pipe,
1875 struct splice_pipe_desc *spd)
1876{
1877 int ret;
1878
1879 /* Drop the socket lock, otherwise we have reverse
1880 * locking dependencies between sk_lock and i_mutex
1881 * here as compared to sendfile(). We enter here
1882 * with the socket lock held, and splice_to_pipe() will
1883 * grab the pipe inode lock. For sendfile() emulation,
1884 * we call into ->sendpage() with the i_mutex lock held
1885 * and networking will grab the socket lock.
1886 */
1887 release_sock(sk);
1888 ret = splice_to_pipe(pipe, spd);
1889 lock_sock(sk);
1890
1891 return ret;
1892}
1893
Jens Axboe9c55e012007-11-06 23:30:13 -08001894/*
1895 * Map data from the skb to a pipe. Should handle both the linear part,
1896 * the fragments, and the frag list. It does NOT handle frag lists within
1897 * the frag list, if such a thing exists. We'd probably need to recurse to
1898 * handle that cleanly.
1899 */
Hannes Frederic Sowaa60e3cc2015-05-21 17:00:00 +02001900int skb_splice_bits(struct sk_buff *skb, struct sock *sk, unsigned int offset,
Jens Axboe9c55e012007-11-06 23:30:13 -08001901 struct pipe_inode_info *pipe, unsigned int tlen,
Hannes Frederic Sowaa60e3cc2015-05-21 17:00:00 +02001902 unsigned int flags,
1903 ssize_t (*splice_cb)(struct sock *,
1904 struct pipe_inode_info *,
1905 struct splice_pipe_desc *))
Jens Axboe9c55e012007-11-06 23:30:13 -08001906{
Eric Dumazet41c73a02012-04-22 12:26:16 +00001907 struct partial_page partial[MAX_SKB_FRAGS];
1908 struct page *pages[MAX_SKB_FRAGS];
Jens Axboe9c55e012007-11-06 23:30:13 -08001909 struct splice_pipe_desc spd = {
1910 .pages = pages,
1911 .partial = partial,
Eric Dumazet047fe362012-06-12 15:24:40 +02001912 .nr_pages_max = MAX_SKB_FRAGS,
Jens Axboe9c55e012007-11-06 23:30:13 -08001913 .flags = flags,
Miklos Szeredi28a625c2014-01-22 19:36:57 +01001914 .ops = &nosteal_pipe_buf_ops,
Jens Axboe9c55e012007-11-06 23:30:13 -08001915 .spd_release = sock_spd_release,
1916 };
David S. Millerfbb398a2009-06-09 00:18:59 -07001917 struct sk_buff *frag_iter;
Jens Axboe35f3d142010-05-20 10:43:18 +02001918 int ret = 0;
1919
Jens Axboe9c55e012007-11-06 23:30:13 -08001920 /*
1921 * __skb_splice_bits() only fails if the output has no room left,
1922 * so no point in going over the frag_list for the error case.
1923 */
Jens Axboe35f3d142010-05-20 10:43:18 +02001924 if (__skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk))
Jens Axboe9c55e012007-11-06 23:30:13 -08001925 goto done;
1926 else if (!tlen)
1927 goto done;
1928
1929 /*
1930 * now see if we have a frag_list to map
1931 */
David S. Millerfbb398a2009-06-09 00:18:59 -07001932 skb_walk_frags(skb, frag_iter) {
1933 if (!tlen)
1934 break;
Jens Axboe35f3d142010-05-20 10:43:18 +02001935 if (__skb_splice_bits(frag_iter, pipe, &offset, &tlen, &spd, sk))
David S. Millerfbb398a2009-06-09 00:18:59 -07001936 break;
Jens Axboe9c55e012007-11-06 23:30:13 -08001937 }
1938
1939done:
Hannes Frederic Sowaa60e3cc2015-05-21 17:00:00 +02001940 if (spd.nr_pages)
1941 ret = splice_cb(sk, pipe, &spd);
Jens Axboe9c55e012007-11-06 23:30:13 -08001942
Jens Axboe35f3d142010-05-20 10:43:18 +02001943 return ret;
Jens Axboe9c55e012007-11-06 23:30:13 -08001944}
1945
Herbert Xu357b40a2005-04-19 22:30:14 -07001946/**
1947 * skb_store_bits - store bits from kernel buffer to skb
1948 * @skb: destination buffer
1949 * @offset: offset in destination
1950 * @from: source buffer
1951 * @len: number of bytes to copy
1952 *
1953 * Copy the specified number of bytes from the source buffer to the
1954 * destination skb. This function handles all the messy bits of
1955 * traversing fragment lists and such.
1956 */
1957
Stephen Hemminger0c6fcc82007-04-20 16:40:01 -07001958int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
Herbert Xu357b40a2005-04-19 22:30:14 -07001959{
David S. Miller1a028e52007-04-27 15:21:23 -07001960 int start = skb_headlen(skb);
David S. Millerfbb398a2009-06-09 00:18:59 -07001961 struct sk_buff *frag_iter;
1962 int i, copy;
Herbert Xu357b40a2005-04-19 22:30:14 -07001963
1964 if (offset > (int)skb->len - len)
1965 goto fault;
1966
David S. Miller1a028e52007-04-27 15:21:23 -07001967 if ((copy = start - offset) > 0) {
Herbert Xu357b40a2005-04-19 22:30:14 -07001968 if (copy > len)
1969 copy = len;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001970 skb_copy_to_linear_data_offset(skb, offset, from, copy);
Herbert Xu357b40a2005-04-19 22:30:14 -07001971 if ((len -= copy) == 0)
1972 return 0;
1973 offset += copy;
1974 from += copy;
1975 }
1976
1977 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1978 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
David S. Miller1a028e52007-04-27 15:21:23 -07001979 int end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001980
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001981 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07001982
Eric Dumazet9e903e02011-10-18 21:00:24 +00001983 end = start + skb_frag_size(frag);
Herbert Xu357b40a2005-04-19 22:30:14 -07001984 if ((copy = end - offset) > 0) {
1985 u8 *vaddr;
1986
1987 if (copy > len)
1988 copy = len;
1989
Eric Dumazet51c56b02012-04-05 11:35:15 +02001990 vaddr = kmap_atomic(skb_frag_page(frag));
David S. Miller1a028e52007-04-27 15:21:23 -07001991 memcpy(vaddr + frag->page_offset + offset - start,
1992 from, copy);
Eric Dumazet51c56b02012-04-05 11:35:15 +02001993 kunmap_atomic(vaddr);
Herbert Xu357b40a2005-04-19 22:30:14 -07001994
1995 if ((len -= copy) == 0)
1996 return 0;
1997 offset += copy;
1998 from += copy;
1999 }
David S. Miller1a028e52007-04-27 15:21:23 -07002000 start = end;
Herbert Xu357b40a2005-04-19 22:30:14 -07002001 }
2002
David S. Millerfbb398a2009-06-09 00:18:59 -07002003 skb_walk_frags(skb, frag_iter) {
2004 int end;
Herbert Xu357b40a2005-04-19 22:30:14 -07002005
David S. Millerfbb398a2009-06-09 00:18:59 -07002006 WARN_ON(start > offset + len);
Herbert Xu357b40a2005-04-19 22:30:14 -07002007
David S. Millerfbb398a2009-06-09 00:18:59 -07002008 end = start + frag_iter->len;
2009 if ((copy = end - offset) > 0) {
2010 if (copy > len)
2011 copy = len;
2012 if (skb_store_bits(frag_iter, offset - start,
2013 from, copy))
2014 goto fault;
2015 if ((len -= copy) == 0)
2016 return 0;
2017 offset += copy;
2018 from += copy;
Herbert Xu357b40a2005-04-19 22:30:14 -07002019 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002020 start = end;
Herbert Xu357b40a2005-04-19 22:30:14 -07002021 }
2022 if (!len)
2023 return 0;
2024
2025fault:
2026 return -EFAULT;
2027}
Herbert Xu357b40a2005-04-19 22:30:14 -07002028EXPORT_SYMBOL(skb_store_bits);
2029
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030/* Checksum skb data. */
Daniel Borkmann2817a332013-10-30 11:50:51 +01002031__wsum __skb_checksum(const struct sk_buff *skb, int offset, int len,
2032 __wsum csum, const struct skb_checksum_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033{
David S. Miller1a028e52007-04-27 15:21:23 -07002034 int start = skb_headlen(skb);
2035 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07002036 struct sk_buff *frag_iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 int pos = 0;
2038
2039 /* Checksum header. */
2040 if (copy > 0) {
2041 if (copy > len)
2042 copy = len;
Daniel Borkmann2817a332013-10-30 11:50:51 +01002043 csum = ops->update(skb->data + offset, copy, csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 if ((len -= copy) == 0)
2045 return csum;
2046 offset += copy;
2047 pos = copy;
2048 }
2049
2050 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07002051 int end;
Eric Dumazet51c56b02012-04-05 11:35:15 +02002052 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002054 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07002055
Eric Dumazet51c56b02012-04-05 11:35:15 +02002056 end = start + skb_frag_size(frag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 if ((copy = end - offset) > 0) {
Al Viro44bb9362006-11-14 21:36:14 -08002058 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 u8 *vaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060
2061 if (copy > len)
2062 copy = len;
Eric Dumazet51c56b02012-04-05 11:35:15 +02002063 vaddr = kmap_atomic(skb_frag_page(frag));
Daniel Borkmann2817a332013-10-30 11:50:51 +01002064 csum2 = ops->update(vaddr + frag->page_offset +
2065 offset - start, copy, 0);
Eric Dumazet51c56b02012-04-05 11:35:15 +02002066 kunmap_atomic(vaddr);
Daniel Borkmann2817a332013-10-30 11:50:51 +01002067 csum = ops->combine(csum, csum2, pos, copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 if (!(len -= copy))
2069 return csum;
2070 offset += copy;
2071 pos += copy;
2072 }
David S. Miller1a028e52007-04-27 15:21:23 -07002073 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 }
2075
David S. Millerfbb398a2009-06-09 00:18:59 -07002076 skb_walk_frags(skb, frag_iter) {
2077 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078
David S. Millerfbb398a2009-06-09 00:18:59 -07002079 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080
David S. Millerfbb398a2009-06-09 00:18:59 -07002081 end = start + frag_iter->len;
2082 if ((copy = end - offset) > 0) {
2083 __wsum csum2;
2084 if (copy > len)
2085 copy = len;
Daniel Borkmann2817a332013-10-30 11:50:51 +01002086 csum2 = __skb_checksum(frag_iter, offset - start,
2087 copy, 0, ops);
2088 csum = ops->combine(csum, csum2, pos, copy);
David S. Millerfbb398a2009-06-09 00:18:59 -07002089 if ((len -= copy) == 0)
2090 return csum;
2091 offset += copy;
2092 pos += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002094 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08002096 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097
2098 return csum;
2099}
Daniel Borkmann2817a332013-10-30 11:50:51 +01002100EXPORT_SYMBOL(__skb_checksum);
2101
2102__wsum skb_checksum(const struct sk_buff *skb, int offset,
2103 int len, __wsum csum)
2104{
2105 const struct skb_checksum_ops ops = {
Daniel Borkmanncea80ea2013-11-04 17:10:25 +01002106 .update = csum_partial_ext,
Daniel Borkmann2817a332013-10-30 11:50:51 +01002107 .combine = csum_block_add_ext,
2108 };
2109
2110 return __skb_checksum(skb, offset, len, csum, &ops);
2111}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002112EXPORT_SYMBOL(skb_checksum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113
2114/* Both of above in one bottle. */
2115
Al Viro81d77662006-11-14 21:37:33 -08002116__wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
2117 u8 *to, int len, __wsum csum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118{
David S. Miller1a028e52007-04-27 15:21:23 -07002119 int start = skb_headlen(skb);
2120 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07002121 struct sk_buff *frag_iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 int pos = 0;
2123
2124 /* Copy header. */
2125 if (copy > 0) {
2126 if (copy > len)
2127 copy = len;
2128 csum = csum_partial_copy_nocheck(skb->data + offset, to,
2129 copy, csum);
2130 if ((len -= copy) == 0)
2131 return csum;
2132 offset += copy;
2133 to += copy;
2134 pos = copy;
2135 }
2136
2137 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07002138 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002140 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07002141
Eric Dumazet9e903e02011-10-18 21:00:24 +00002142 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 if ((copy = end - offset) > 0) {
Al Viro50842052006-11-14 21:36:34 -08002144 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 u8 *vaddr;
2146 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2147
2148 if (copy > len)
2149 copy = len;
Eric Dumazet51c56b02012-04-05 11:35:15 +02002150 vaddr = kmap_atomic(skb_frag_page(frag));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 csum2 = csum_partial_copy_nocheck(vaddr +
David S. Miller1a028e52007-04-27 15:21:23 -07002152 frag->page_offset +
2153 offset - start, to,
2154 copy, 0);
Eric Dumazet51c56b02012-04-05 11:35:15 +02002155 kunmap_atomic(vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 csum = csum_block_add(csum, csum2, pos);
2157 if (!(len -= copy))
2158 return csum;
2159 offset += copy;
2160 to += copy;
2161 pos += copy;
2162 }
David S. Miller1a028e52007-04-27 15:21:23 -07002163 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 }
2165
David S. Millerfbb398a2009-06-09 00:18:59 -07002166 skb_walk_frags(skb, frag_iter) {
2167 __wsum csum2;
2168 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169
David S. Millerfbb398a2009-06-09 00:18:59 -07002170 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171
David S. Millerfbb398a2009-06-09 00:18:59 -07002172 end = start + frag_iter->len;
2173 if ((copy = end - offset) > 0) {
2174 if (copy > len)
2175 copy = len;
2176 csum2 = skb_copy_and_csum_bits(frag_iter,
2177 offset - start,
2178 to, copy, 0);
2179 csum = csum_block_add(csum, csum2, pos);
2180 if ((len -= copy) == 0)
2181 return csum;
2182 offset += copy;
2183 to += copy;
2184 pos += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002186 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08002188 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 return csum;
2190}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002191EXPORT_SYMBOL(skb_copy_and_csum_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192
Thomas Grafaf2806f2013-12-13 15:22:17 +01002193 /**
2194 * skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy()
2195 * @from: source buffer
2196 *
2197 * Calculates the amount of linear headroom needed in the 'to' skb passed
2198 * into skb_zerocopy().
2199 */
2200unsigned int
2201skb_zerocopy_headlen(const struct sk_buff *from)
2202{
2203 unsigned int hlen = 0;
2204
2205 if (!from->head_frag ||
2206 skb_headlen(from) < L1_CACHE_BYTES ||
2207 skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
2208 hlen = skb_headlen(from);
2209
2210 if (skb_has_frag_list(from))
2211 hlen = from->len;
2212
2213 return hlen;
2214}
2215EXPORT_SYMBOL_GPL(skb_zerocopy_headlen);
2216
2217/**
2218 * skb_zerocopy - Zero copy skb to skb
2219 * @to: destination buffer
Masanari Iida7fceb4d2014-01-29 01:05:28 +09002220 * @from: source buffer
Thomas Grafaf2806f2013-12-13 15:22:17 +01002221 * @len: number of bytes to copy from source buffer
2222 * @hlen: size of linear headroom in destination buffer
2223 *
2224 * Copies up to `len` bytes from `from` to `to` by creating references
2225 * to the frags in the source buffer.
2226 *
2227 * The `hlen` as calculated by skb_zerocopy_headlen() specifies the
2228 * headroom in the `to` buffer.
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002229 *
2230 * Return value:
2231 * 0: everything is OK
2232 * -ENOMEM: couldn't orphan frags of @from due to lack of memory
2233 * -EFAULT: skb_copy_bits() found some problem with skb geometry
Thomas Grafaf2806f2013-12-13 15:22:17 +01002234 */
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002235int
2236skb_zerocopy(struct sk_buff *to, struct sk_buff *from, int len, int hlen)
Thomas Grafaf2806f2013-12-13 15:22:17 +01002237{
2238 int i, j = 0;
2239 int plen = 0; /* length of skb->head fragment */
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002240 int ret;
Thomas Grafaf2806f2013-12-13 15:22:17 +01002241 struct page *page;
2242 unsigned int offset;
2243
2244 BUG_ON(!from->head_frag && !hlen);
2245
2246 /* dont bother with small payloads */
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002247 if (len <= skb_tailroom(to))
2248 return skb_copy_bits(from, 0, skb_put(to, len), len);
Thomas Grafaf2806f2013-12-13 15:22:17 +01002249
2250 if (hlen) {
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002251 ret = skb_copy_bits(from, 0, skb_put(to, hlen), hlen);
2252 if (unlikely(ret))
2253 return ret;
Thomas Grafaf2806f2013-12-13 15:22:17 +01002254 len -= hlen;
2255 } else {
2256 plen = min_t(int, skb_headlen(from), len);
2257 if (plen) {
2258 page = virt_to_head_page(from->head);
2259 offset = from->data - (unsigned char *)page_address(page);
2260 __skb_fill_page_desc(to, 0, page, offset, plen);
2261 get_page(page);
2262 j = 1;
2263 len -= plen;
2264 }
2265 }
2266
2267 to->truesize += len + plen;
2268 to->len += len + plen;
2269 to->data_len += len + plen;
2270
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002271 if (unlikely(skb_orphan_frags(from, GFP_ATOMIC))) {
2272 skb_tx_error(from);
2273 return -ENOMEM;
2274 }
2275
Thomas Grafaf2806f2013-12-13 15:22:17 +01002276 for (i = 0; i < skb_shinfo(from)->nr_frags; i++) {
2277 if (!len)
2278 break;
2279 skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i];
2280 skb_shinfo(to)->frags[j].size = min_t(int, skb_shinfo(to)->frags[j].size, len);
2281 len -= skb_shinfo(to)->frags[j].size;
2282 skb_frag_ref(to, j);
2283 j++;
2284 }
2285 skb_shinfo(to)->nr_frags = j;
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00002286
2287 return 0;
Thomas Grafaf2806f2013-12-13 15:22:17 +01002288}
2289EXPORT_SYMBOL_GPL(skb_zerocopy);
2290
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
2292{
Al Virod3bc23e2006-11-14 21:24:49 -08002293 __wsum csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 long csstart;
2295
Patrick McHardy84fa7932006-08-29 16:44:56 -07002296 if (skb->ip_summed == CHECKSUM_PARTIAL)
Michał Mirosław55508d62010-12-14 15:24:08 +00002297 csstart = skb_checksum_start_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 else
2299 csstart = skb_headlen(skb);
2300
Kris Katterjohn09a62662006-01-08 22:24:28 -08002301 BUG_ON(csstart > skb_headlen(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03002303 skb_copy_from_linear_data(skb, to, csstart);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304
2305 csum = 0;
2306 if (csstart != skb->len)
2307 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
2308 skb->len - csstart, 0);
2309
Patrick McHardy84fa7932006-08-29 16:44:56 -07002310 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Al Viroff1dcad2006-11-20 18:07:29 -08002311 long csstuff = csstart + skb->csum_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312
Al Virod3bc23e2006-11-14 21:24:49 -08002313 *((__sum16 *)(to + csstuff)) = csum_fold(csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 }
2315}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002316EXPORT_SYMBOL(skb_copy_and_csum_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317
2318/**
2319 * skb_dequeue - remove from the head of the queue
2320 * @list: list to dequeue from
2321 *
2322 * Remove the head of the list. The list lock is taken so the function
2323 * may be used safely with other locking list functions. The head item is
2324 * returned or %NULL if the list is empty.
2325 */
2326
2327struct sk_buff *skb_dequeue(struct sk_buff_head *list)
2328{
2329 unsigned long flags;
2330 struct sk_buff *result;
2331
2332 spin_lock_irqsave(&list->lock, flags);
2333 result = __skb_dequeue(list);
2334 spin_unlock_irqrestore(&list->lock, flags);
2335 return result;
2336}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002337EXPORT_SYMBOL(skb_dequeue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338
2339/**
2340 * skb_dequeue_tail - remove from the tail of the queue
2341 * @list: list to dequeue from
2342 *
2343 * Remove the tail of the list. The list lock is taken so the function
2344 * may be used safely with other locking list functions. The tail item is
2345 * returned or %NULL if the list is empty.
2346 */
2347struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
2348{
2349 unsigned long flags;
2350 struct sk_buff *result;
2351
2352 spin_lock_irqsave(&list->lock, flags);
2353 result = __skb_dequeue_tail(list);
2354 spin_unlock_irqrestore(&list->lock, flags);
2355 return result;
2356}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002357EXPORT_SYMBOL(skb_dequeue_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358
2359/**
2360 * skb_queue_purge - empty a list
2361 * @list: list to empty
2362 *
2363 * Delete all buffers on an &sk_buff list. Each buffer is removed from
2364 * the list and one reference dropped. This function takes the list
2365 * lock and is atomic with respect to other list locking functions.
2366 */
2367void skb_queue_purge(struct sk_buff_head *list)
2368{
2369 struct sk_buff *skb;
2370 while ((skb = skb_dequeue(list)) != NULL)
2371 kfree_skb(skb);
2372}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002373EXPORT_SYMBOL(skb_queue_purge);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374
2375/**
2376 * skb_queue_head - queue a buffer at the list head
2377 * @list: list to use
2378 * @newsk: buffer to queue
2379 *
2380 * Queue a buffer at the start of the list. This function takes the
2381 * list lock and can be used safely with other locking &sk_buff functions
2382 * safely.
2383 *
2384 * A buffer cannot be placed on two lists at the same time.
2385 */
2386void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
2387{
2388 unsigned long flags;
2389
2390 spin_lock_irqsave(&list->lock, flags);
2391 __skb_queue_head(list, newsk);
2392 spin_unlock_irqrestore(&list->lock, flags);
2393}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002394EXPORT_SYMBOL(skb_queue_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395
2396/**
2397 * skb_queue_tail - queue a buffer at the list tail
2398 * @list: list to use
2399 * @newsk: buffer to queue
2400 *
2401 * Queue a buffer at the tail of the list. This function takes the
2402 * list lock and can be used safely with other locking &sk_buff functions
2403 * safely.
2404 *
2405 * A buffer cannot be placed on two lists at the same time.
2406 */
2407void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
2408{
2409 unsigned long flags;
2410
2411 spin_lock_irqsave(&list->lock, flags);
2412 __skb_queue_tail(list, newsk);
2413 spin_unlock_irqrestore(&list->lock, flags);
2414}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002415EXPORT_SYMBOL(skb_queue_tail);
David S. Miller8728b832005-08-09 19:25:21 -07002416
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417/**
2418 * skb_unlink - remove a buffer from a list
2419 * @skb: buffer to remove
David S. Miller8728b832005-08-09 19:25:21 -07002420 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 *
David S. Miller8728b832005-08-09 19:25:21 -07002422 * Remove a packet from a list. The list locks are taken and this
2423 * function is atomic with respect to other list locked calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 *
David S. Miller8728b832005-08-09 19:25:21 -07002425 * You must know what list the SKB is on.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 */
David S. Miller8728b832005-08-09 19:25:21 -07002427void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428{
David S. Miller8728b832005-08-09 19:25:21 -07002429 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430
David S. Miller8728b832005-08-09 19:25:21 -07002431 spin_lock_irqsave(&list->lock, flags);
2432 __skb_unlink(skb, list);
2433 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002435EXPORT_SYMBOL(skb_unlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437/**
2438 * skb_append - append a buffer
2439 * @old: buffer to insert after
2440 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07002441 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 *
2443 * Place a packet after a given packet in a list. The list locks are taken
2444 * and this function is atomic with respect to other list locked calls.
2445 * A buffer cannot be placed on two lists at the same time.
2446 */
David S. Miller8728b832005-08-09 19:25:21 -07002447void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448{
2449 unsigned long flags;
2450
David S. Miller8728b832005-08-09 19:25:21 -07002451 spin_lock_irqsave(&list->lock, flags);
Gerrit Renker7de6c032008-04-14 00:05:09 -07002452 __skb_queue_after(list, old, newsk);
David S. Miller8728b832005-08-09 19:25:21 -07002453 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002455EXPORT_SYMBOL(skb_append);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456
2457/**
2458 * skb_insert - insert a buffer
2459 * @old: buffer to insert before
2460 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07002461 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 *
David S. Miller8728b832005-08-09 19:25:21 -07002463 * Place a packet before a given packet in a list. The list locks are
2464 * taken and this function is atomic with respect to other list locked
2465 * calls.
2466 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 * A buffer cannot be placed on two lists at the same time.
2468 */
David S. Miller8728b832005-08-09 19:25:21 -07002469void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470{
2471 unsigned long flags;
2472
David S. Miller8728b832005-08-09 19:25:21 -07002473 spin_lock_irqsave(&list->lock, flags);
2474 __skb_insert(newsk, old->prev, old, list);
2475 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002477EXPORT_SYMBOL(skb_insert);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479static inline void skb_split_inside_header(struct sk_buff *skb,
2480 struct sk_buff* skb1,
2481 const u32 len, const int pos)
2482{
2483 int i;
2484
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03002485 skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
2486 pos - len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 /* And move data appendix as is. */
2488 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
2489 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
2490
2491 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
2492 skb_shinfo(skb)->nr_frags = 0;
2493 skb1->data_len = skb->data_len;
2494 skb1->len += skb1->data_len;
2495 skb->data_len = 0;
2496 skb->len = len;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002497 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498}
2499
2500static inline void skb_split_no_header(struct sk_buff *skb,
2501 struct sk_buff* skb1,
2502 const u32 len, int pos)
2503{
2504 int i, k = 0;
2505 const int nfrags = skb_shinfo(skb)->nr_frags;
2506
2507 skb_shinfo(skb)->nr_frags = 0;
2508 skb1->len = skb1->data_len = skb->len - len;
2509 skb->len = len;
2510 skb->data_len = len - pos;
2511
2512 for (i = 0; i < nfrags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00002513 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514
2515 if (pos + size > len) {
2516 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
2517
2518 if (pos < len) {
2519 /* Split frag.
2520 * We have two variants in this case:
2521 * 1. Move all the frag to the second
2522 * part, if it is possible. F.e.
2523 * this approach is mandatory for TUX,
2524 * where splitting is expensive.
2525 * 2. Split is accurately. We make this.
2526 */
Ian Campbellea2ab692011-08-22 23:44:58 +00002527 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002529 skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos);
2530 skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 skb_shinfo(skb)->nr_frags++;
2532 }
2533 k++;
2534 } else
2535 skb_shinfo(skb)->nr_frags++;
2536 pos += size;
2537 }
2538 skb_shinfo(skb1)->nr_frags = k;
2539}
2540
2541/**
2542 * skb_split - Split fragmented skb to two parts at length len.
2543 * @skb: the buffer to split
2544 * @skb1: the buffer to receive the second part
2545 * @len: new length for skb
2546 */
2547void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
2548{
2549 int pos = skb_headlen(skb);
2550
Amerigo Wang68534c62013-02-19 22:51:30 +00002551 skb_shinfo(skb1)->tx_flags = skb_shinfo(skb)->tx_flags & SKBTX_SHARED_FRAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 if (len < pos) /* Split line is inside header. */
2553 skb_split_inside_header(skb, skb1, len, pos);
2554 else /* Second chunk has no header, nothing to copy. */
2555 skb_split_no_header(skb, skb1, len, pos);
2556}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002557EXPORT_SYMBOL(skb_split);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558
Ilpo Järvinen9f782db2008-11-25 13:57:01 -08002559/* Shifting from/to a cloned skb is a no-go.
2560 *
2561 * Caller cannot keep skb_shinfo related pointers past calling here!
2562 */
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002563static int skb_prepare_for_shift(struct sk_buff *skb)
2564{
Ilpo Järvinen0ace2852008-11-24 21:30:21 -08002565 return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002566}
2567
2568/**
2569 * skb_shift - Shifts paged data partially from skb to another
2570 * @tgt: buffer into which tail data gets added
2571 * @skb: buffer from which the paged data comes from
2572 * @shiftlen: shift up to this many bytes
2573 *
2574 * Attempts to shift up to shiftlen worth of bytes, which may be less than
Feng King20e994a2011-11-21 01:47:11 +00002575 * the length of the skb, from skb to tgt. Returns number bytes shifted.
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002576 * It's up to caller to free skb if everything was shifted.
2577 *
2578 * If @tgt runs out of frags, the whole operation is aborted.
2579 *
2580 * Skb cannot include anything else but paged data while tgt is allowed
2581 * to have non-paged data as well.
2582 *
2583 * TODO: full sized shift could be optimized but that would need
2584 * specialized skb free'er to handle frags without up-to-date nr_frags.
2585 */
2586int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
2587{
2588 int from, to, merge, todo;
2589 struct skb_frag_struct *fragfrom, *fragto;
2590
2591 BUG_ON(shiftlen > skb->len);
2592 BUG_ON(skb_headlen(skb)); /* Would corrupt stream */
2593
2594 todo = shiftlen;
2595 from = 0;
2596 to = skb_shinfo(tgt)->nr_frags;
2597 fragfrom = &skb_shinfo(skb)->frags[from];
2598
2599 /* Actual merge is delayed until the point when we know we can
2600 * commit all, so that we don't have to undo partial changes
2601 */
2602 if (!to ||
Ian Campbellea2ab692011-08-22 23:44:58 +00002603 !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom),
2604 fragfrom->page_offset)) {
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002605 merge = -1;
2606 } else {
2607 merge = to - 1;
2608
Eric Dumazet9e903e02011-10-18 21:00:24 +00002609 todo -= skb_frag_size(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002610 if (todo < 0) {
2611 if (skb_prepare_for_shift(skb) ||
2612 skb_prepare_for_shift(tgt))
2613 return 0;
2614
Ilpo Järvinen9f782db2008-11-25 13:57:01 -08002615 /* All previous frag pointers might be stale! */
2616 fragfrom = &skb_shinfo(skb)->frags[from];
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002617 fragto = &skb_shinfo(tgt)->frags[merge];
2618
Eric Dumazet9e903e02011-10-18 21:00:24 +00002619 skb_frag_size_add(fragto, shiftlen);
2620 skb_frag_size_sub(fragfrom, shiftlen);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002621 fragfrom->page_offset += shiftlen;
2622
2623 goto onlymerged;
2624 }
2625
2626 from++;
2627 }
2628
2629 /* Skip full, not-fitting skb to avoid expensive operations */
2630 if ((shiftlen == skb->len) &&
2631 (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to))
2632 return 0;
2633
2634 if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt))
2635 return 0;
2636
2637 while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) {
2638 if (to == MAX_SKB_FRAGS)
2639 return 0;
2640
2641 fragfrom = &skb_shinfo(skb)->frags[from];
2642 fragto = &skb_shinfo(tgt)->frags[to];
2643
Eric Dumazet9e903e02011-10-18 21:00:24 +00002644 if (todo >= skb_frag_size(fragfrom)) {
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002645 *fragto = *fragfrom;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002646 todo -= skb_frag_size(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002647 from++;
2648 to++;
2649
2650 } else {
Ian Campbellea2ab692011-08-22 23:44:58 +00002651 __skb_frag_ref(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002652 fragto->page = fragfrom->page;
2653 fragto->page_offset = fragfrom->page_offset;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002654 skb_frag_size_set(fragto, todo);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002655
2656 fragfrom->page_offset += todo;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002657 skb_frag_size_sub(fragfrom, todo);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002658 todo = 0;
2659
2660 to++;
2661 break;
2662 }
2663 }
2664
2665 /* Ready to "commit" this state change to tgt */
2666 skb_shinfo(tgt)->nr_frags = to;
2667
2668 if (merge >= 0) {
2669 fragfrom = &skb_shinfo(skb)->frags[0];
2670 fragto = &skb_shinfo(tgt)->frags[merge];
2671
Eric Dumazet9e903e02011-10-18 21:00:24 +00002672 skb_frag_size_add(fragto, skb_frag_size(fragfrom));
Ian Campbellea2ab692011-08-22 23:44:58 +00002673 __skb_frag_unref(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002674 }
2675
2676 /* Reposition in the original skb */
2677 to = 0;
2678 while (from < skb_shinfo(skb)->nr_frags)
2679 skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++];
2680 skb_shinfo(skb)->nr_frags = to;
2681
2682 BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags);
2683
2684onlymerged:
2685 /* Most likely the tgt won't ever need its checksum anymore, skb on
2686 * the other hand might need it if it needs to be resent
2687 */
2688 tgt->ip_summed = CHECKSUM_PARTIAL;
2689 skb->ip_summed = CHECKSUM_PARTIAL;
2690
2691 /* Yak, is it really working this way? Some helper please? */
2692 skb->len -= shiftlen;
2693 skb->data_len -= shiftlen;
2694 skb->truesize -= shiftlen;
2695 tgt->len += shiftlen;
2696 tgt->data_len += shiftlen;
2697 tgt->truesize += shiftlen;
2698
2699 return shiftlen;
2700}
2701
Thomas Graf677e90e2005-06-23 20:59:51 -07002702/**
2703 * skb_prepare_seq_read - Prepare a sequential read of skb data
2704 * @skb: the buffer to read
2705 * @from: lower offset of data to be read
2706 * @to: upper offset of data to be read
2707 * @st: state variable
2708 *
2709 * Initializes the specified state variable. Must be called before
2710 * invoking skb_seq_read() for the first time.
2711 */
2712void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
2713 unsigned int to, struct skb_seq_state *st)
2714{
2715 st->lower_offset = from;
2716 st->upper_offset = to;
2717 st->root_skb = st->cur_skb = skb;
2718 st->frag_idx = st->stepped_offset = 0;
2719 st->frag_data = NULL;
2720}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002721EXPORT_SYMBOL(skb_prepare_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07002722
2723/**
2724 * skb_seq_read - Sequentially read skb data
2725 * @consumed: number of bytes consumed by the caller so far
2726 * @data: destination pointer for data to be returned
2727 * @st: state variable
2728 *
Mathias Krausebc323832013-11-07 14:18:26 +01002729 * Reads a block of skb data at @consumed relative to the
Thomas Graf677e90e2005-06-23 20:59:51 -07002730 * lower offset specified to skb_prepare_seq_read(). Assigns
Mathias Krausebc323832013-11-07 14:18:26 +01002731 * the head of the data block to @data and returns the length
Thomas Graf677e90e2005-06-23 20:59:51 -07002732 * of the block or 0 if the end of the skb data or the upper
2733 * offset has been reached.
2734 *
2735 * The caller is not required to consume all of the data
Mathias Krausebc323832013-11-07 14:18:26 +01002736 * returned, i.e. @consumed is typically set to the number
Thomas Graf677e90e2005-06-23 20:59:51 -07002737 * of bytes already consumed and the next call to
2738 * skb_seq_read() will return the remaining part of the block.
2739 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002740 * Note 1: The size of each block of data returned can be arbitrary,
Masanari Iidae793c0f2014-09-04 23:44:36 +09002741 * this limitation is the cost for zerocopy sequential
Thomas Graf677e90e2005-06-23 20:59:51 -07002742 * reads of potentially non linear data.
2743 *
Randy Dunlapbc2cda12008-02-13 15:03:25 -08002744 * Note 2: Fragment lists within fragments are not implemented
Thomas Graf677e90e2005-06-23 20:59:51 -07002745 * at the moment, state->root_skb could be replaced with
2746 * a stack for this purpose.
2747 */
2748unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
2749 struct skb_seq_state *st)
2750{
2751 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
2752 skb_frag_t *frag;
2753
Wedson Almeida Filhoaeb193e2013-06-23 23:33:48 -07002754 if (unlikely(abs_offset >= st->upper_offset)) {
2755 if (st->frag_data) {
2756 kunmap_atomic(st->frag_data);
2757 st->frag_data = NULL;
2758 }
Thomas Graf677e90e2005-06-23 20:59:51 -07002759 return 0;
Wedson Almeida Filhoaeb193e2013-06-23 23:33:48 -07002760 }
Thomas Graf677e90e2005-06-23 20:59:51 -07002761
2762next_skb:
Herbert Xu95e3b242009-01-29 16:07:52 -08002763 block_limit = skb_headlen(st->cur_skb) + st->stepped_offset;
Thomas Graf677e90e2005-06-23 20:59:51 -07002764
Thomas Chenault995b3372009-05-18 21:43:27 -07002765 if (abs_offset < block_limit && !st->frag_data) {
Herbert Xu95e3b242009-01-29 16:07:52 -08002766 *data = st->cur_skb->data + (abs_offset - st->stepped_offset);
Thomas Graf677e90e2005-06-23 20:59:51 -07002767 return block_limit - abs_offset;
2768 }
2769
2770 if (st->frag_idx == 0 && !st->frag_data)
2771 st->stepped_offset += skb_headlen(st->cur_skb);
2772
2773 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
2774 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
Eric Dumazet9e903e02011-10-18 21:00:24 +00002775 block_limit = skb_frag_size(frag) + st->stepped_offset;
Thomas Graf677e90e2005-06-23 20:59:51 -07002776
2777 if (abs_offset < block_limit) {
2778 if (!st->frag_data)
Eric Dumazet51c56b02012-04-05 11:35:15 +02002779 st->frag_data = kmap_atomic(skb_frag_page(frag));
Thomas Graf677e90e2005-06-23 20:59:51 -07002780
2781 *data = (u8 *) st->frag_data + frag->page_offset +
2782 (abs_offset - st->stepped_offset);
2783
2784 return block_limit - abs_offset;
2785 }
2786
2787 if (st->frag_data) {
Eric Dumazet51c56b02012-04-05 11:35:15 +02002788 kunmap_atomic(st->frag_data);
Thomas Graf677e90e2005-06-23 20:59:51 -07002789 st->frag_data = NULL;
2790 }
2791
2792 st->frag_idx++;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002793 st->stepped_offset += skb_frag_size(frag);
Thomas Graf677e90e2005-06-23 20:59:51 -07002794 }
2795
Olaf Kirch5b5a60d2007-06-23 23:11:52 -07002796 if (st->frag_data) {
Eric Dumazet51c56b02012-04-05 11:35:15 +02002797 kunmap_atomic(st->frag_data);
Olaf Kirch5b5a60d2007-06-23 23:11:52 -07002798 st->frag_data = NULL;
2799 }
2800
David S. Miller21dc3302010-08-23 00:13:46 -07002801 if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) {
Shyam Iyer71b33462009-01-29 16:12:42 -08002802 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
Thomas Graf677e90e2005-06-23 20:59:51 -07002803 st->frag_idx = 0;
2804 goto next_skb;
Shyam Iyer71b33462009-01-29 16:12:42 -08002805 } else if (st->cur_skb->next) {
2806 st->cur_skb = st->cur_skb->next;
Herbert Xu95e3b242009-01-29 16:07:52 -08002807 st->frag_idx = 0;
Thomas Graf677e90e2005-06-23 20:59:51 -07002808 goto next_skb;
2809 }
2810
2811 return 0;
2812}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002813EXPORT_SYMBOL(skb_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07002814
2815/**
2816 * skb_abort_seq_read - Abort a sequential read of skb data
2817 * @st: state variable
2818 *
2819 * Must be called if skb_seq_read() was not called until it
2820 * returned 0.
2821 */
2822void skb_abort_seq_read(struct skb_seq_state *st)
2823{
2824 if (st->frag_data)
Eric Dumazet51c56b02012-04-05 11:35:15 +02002825 kunmap_atomic(st->frag_data);
Thomas Graf677e90e2005-06-23 20:59:51 -07002826}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002827EXPORT_SYMBOL(skb_abort_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07002828
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002829#define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
2830
2831static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
2832 struct ts_config *conf,
2833 struct ts_state *state)
2834{
2835 return skb_seq_read(offset, text, TS_SKB_CB(state));
2836}
2837
2838static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
2839{
2840 skb_abort_seq_read(TS_SKB_CB(state));
2841}
2842
2843/**
2844 * skb_find_text - Find a text pattern in skb data
2845 * @skb: the buffer to look in
2846 * @from: search offset
2847 * @to: search limit
2848 * @config: textsearch configuration
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002849 *
2850 * Finds a pattern in the skb data according to the specified
2851 * textsearch configuration. Use textsearch_next() to retrieve
2852 * subsequent occurrences of the pattern. Returns the offset
2853 * to the first occurrence or UINT_MAX if no match was found.
2854 */
2855unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
Bojan Prtvar059a2442015-02-22 11:46:35 +01002856 unsigned int to, struct ts_config *config)
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002857{
Bojan Prtvar059a2442015-02-22 11:46:35 +01002858 struct ts_state state;
Phil Oesterf72b9482006-06-26 00:00:57 -07002859 unsigned int ret;
2860
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002861 config->get_next_block = skb_ts_get_next_block;
2862 config->finish = skb_ts_finish;
2863
Bojan Prtvar059a2442015-02-22 11:46:35 +01002864 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(&state));
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002865
Bojan Prtvar059a2442015-02-22 11:46:35 +01002866 ret = textsearch_find(config, &state);
Phil Oesterf72b9482006-06-26 00:00:57 -07002867 return (ret <= to - from ? ret : UINT_MAX);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002868}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002869EXPORT_SYMBOL(skb_find_text);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002870
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002871/**
Ben Hutchings2c530402012-07-10 10:55:09 +00002872 * skb_append_datato_frags - append the user data to a skb
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002873 * @sk: sock structure
Masanari Iidae793c0f2014-09-04 23:44:36 +09002874 * @skb: skb structure to be appended with user data.
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002875 * @getfrag: call back function to be used for getting the user data
2876 * @from: pointer to user message iov
2877 * @length: length of the iov message
2878 *
2879 * Description: This procedure append the user data in the fragment part
2880 * of the skb if any page alloc fails user this procedure returns -ENOMEM
2881 */
2882int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
Martin Waitzdab96302005-12-05 13:40:12 -08002883 int (*getfrag)(void *from, char *to, int offset,
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002884 int len, int odd, struct sk_buff *skb),
2885 void *from, int length)
2886{
Eric Dumazetb2111722012-12-28 06:06:37 +00002887 int frg_cnt = skb_shinfo(skb)->nr_frags;
2888 int copy;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002889 int offset = 0;
2890 int ret;
Eric Dumazetb2111722012-12-28 06:06:37 +00002891 struct page_frag *pfrag = &current->task_frag;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002892
2893 do {
2894 /* Return error if we don't have space for new frag */
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002895 if (frg_cnt >= MAX_SKB_FRAGS)
Eric Dumazetb2111722012-12-28 06:06:37 +00002896 return -EMSGSIZE;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002897
Eric Dumazetb2111722012-12-28 06:06:37 +00002898 if (!sk_page_frag_refill(sk, pfrag))
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002899 return -ENOMEM;
2900
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002901 /* copy the user data to page */
Eric Dumazetb2111722012-12-28 06:06:37 +00002902 copy = min_t(int, length, pfrag->size - pfrag->offset);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002903
Eric Dumazetb2111722012-12-28 06:06:37 +00002904 ret = getfrag(from, page_address(pfrag->page) + pfrag->offset,
2905 offset, copy, 0, skb);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002906 if (ret < 0)
2907 return -EFAULT;
2908
2909 /* copy was successful so update the size parameters */
Eric Dumazetb2111722012-12-28 06:06:37 +00002910 skb_fill_page_desc(skb, frg_cnt, pfrag->page, pfrag->offset,
2911 copy);
2912 frg_cnt++;
2913 pfrag->offset += copy;
2914 get_page(pfrag->page);
2915
2916 skb->truesize += copy;
2917 atomic_add(copy, &sk->sk_wmem_alloc);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002918 skb->len += copy;
2919 skb->data_len += copy;
2920 offset += copy;
2921 length -= copy;
2922
2923 } while (length > 0);
2924
2925 return 0;
2926}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002927EXPORT_SYMBOL(skb_append_datato_frags);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002928
Hannes Frederic Sowabe12a1f2015-05-21 16:59:58 +02002929int skb_append_pagefrags(struct sk_buff *skb, struct page *page,
2930 int offset, size_t size)
2931{
2932 int i = skb_shinfo(skb)->nr_frags;
2933
2934 if (skb_can_coalesce(skb, i, page, offset)) {
2935 skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], size);
2936 } else if (i < MAX_SKB_FRAGS) {
2937 get_page(page);
2938 skb_fill_page_desc(skb, i, page, offset, size);
2939 } else {
2940 return -EMSGSIZE;
2941 }
2942
2943 return 0;
2944}
2945EXPORT_SYMBOL_GPL(skb_append_pagefrags);
2946
Herbert Xucbb042f2006-03-20 22:43:56 -08002947/**
2948 * skb_pull_rcsum - pull skb and update receive checksum
2949 * @skb: buffer to update
Herbert Xucbb042f2006-03-20 22:43:56 -08002950 * @len: length of data pulled
2951 *
2952 * This function performs an skb_pull on the packet and updates
Urs Thuermannfee54fa2008-02-12 22:03:25 -08002953 * the CHECKSUM_COMPLETE checksum. It should be used on
Patrick McHardy84fa7932006-08-29 16:44:56 -07002954 * receive path processing instead of skb_pull unless you know
2955 * that the checksum difference is zero (e.g., a valid IP header)
2956 * or you are setting ip_summed to CHECKSUM_NONE.
Herbert Xucbb042f2006-03-20 22:43:56 -08002957 */
2958unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
2959{
2960 BUG_ON(len > skb->len);
2961 skb->len -= len;
2962 BUG_ON(skb->len < skb->data_len);
2963 skb_postpull_rcsum(skb, skb->data, len);
2964 return skb->data += len;
2965}
Arnaldo Carvalho de Melof94691a2006-03-20 22:47:55 -08002966EXPORT_SYMBOL_GPL(skb_pull_rcsum);
2967
Herbert Xuf4c50d92006-06-22 03:02:40 -07002968/**
2969 * skb_segment - Perform protocol segmentation on skb.
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002970 * @head_skb: buffer to segment
Herbert Xu576a30e2006-06-27 13:22:38 -07002971 * @features: features for the output path (see dev->features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07002972 *
2973 * This function performs segmentation on the given skb. It returns
Ben Hutchings4c821d72008-04-13 21:52:48 -07002974 * a pointer to the first in a list of new skbs for the segments.
2975 * In case of error it returns ERR_PTR(err).
Herbert Xuf4c50d92006-06-22 03:02:40 -07002976 */
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002977struct sk_buff *skb_segment(struct sk_buff *head_skb,
2978 netdev_features_t features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07002979{
2980 struct sk_buff *segs = NULL;
2981 struct sk_buff *tail = NULL;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02002982 struct sk_buff *list_skb = skb_shinfo(head_skb)->frag_list;
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002983 skb_frag_t *frag = skb_shinfo(head_skb)->frags;
2984 unsigned int mss = skb_shinfo(head_skb)->gso_size;
2985 unsigned int doffset = head_skb->data - skb_mac_header(head_skb);
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02002986 struct sk_buff *frag_skb = head_skb;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002987 unsigned int offset = doffset;
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002988 unsigned int tnl_hlen = skb_tnl_header_len(head_skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002989 unsigned int headroom;
2990 unsigned int len;
Pravin B Shelarec5f0612013-03-07 09:28:01 +00002991 __be16 proto;
2992 bool csum;
Michał Mirosław04ed3e72011-01-24 15:32:47 -08002993 int sg = !!(features & NETIF_F_SG);
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02002994 int nfrags = skb_shinfo(head_skb)->nr_frags;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002995 int err = -ENOMEM;
2996 int i = 0;
2997 int pos;
Vlad Yasevich53d64712014-03-27 17:26:18 -04002998 int dummy;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002999
Wei-Chun Chao5882a072014-06-08 23:48:54 -07003000 __skb_push(head_skb, doffset);
Vlad Yasevich53d64712014-03-27 17:26:18 -04003001 proto = skb_network_protocol(head_skb, &dummy);
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003002 if (unlikely(!proto))
3003 return ERR_PTR(-EINVAL);
3004
Tom Herbert7e2b10c2014-06-04 17:20:02 -07003005 csum = !head_skb->encap_hdr_csum &&
3006 !!can_checksum_protocol(features, proto);
3007
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003008 headroom = skb_headroom(head_skb);
3009 pos = skb_headlen(head_skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003010
3011 do {
3012 struct sk_buff *nskb;
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003013 skb_frag_t *nskb_frag;
Herbert Xuc8884ed2006-10-29 15:59:41 -08003014 int hsize;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003015 int size;
3016
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003017 len = head_skb->len - offset;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003018 if (len > mss)
3019 len = mss;
3020
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003021 hsize = skb_headlen(head_skb) - offset;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003022 if (hsize < 0)
3023 hsize = 0;
Herbert Xuc8884ed2006-10-29 15:59:41 -08003024 if (hsize > len || !sg)
3025 hsize = len;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003026
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003027 if (!hsize && i >= nfrags && skb_headlen(list_skb) &&
3028 (skb_headlen(list_skb) == len || sg)) {
3029 BUG_ON(skb_headlen(list_skb) > len);
Herbert Xu89319d382008-12-15 23:26:06 -08003030
Herbert Xu9d8506c2013-11-21 11:10:04 -08003031 i = 0;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003032 nfrags = skb_shinfo(list_skb)->nr_frags;
3033 frag = skb_shinfo(list_skb)->frags;
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02003034 frag_skb = list_skb;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003035 pos += skb_headlen(list_skb);
Herbert Xu9d8506c2013-11-21 11:10:04 -08003036
3037 while (pos < offset + len) {
3038 BUG_ON(i >= nfrags);
3039
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02003040 size = skb_frag_size(frag);
Herbert Xu9d8506c2013-11-21 11:10:04 -08003041 if (pos + size > offset + len)
3042 break;
3043
3044 i++;
3045 pos += size;
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02003046 frag++;
Herbert Xu9d8506c2013-11-21 11:10:04 -08003047 }
3048
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003049 nskb = skb_clone(list_skb, GFP_ATOMIC);
3050 list_skb = list_skb->next;
Herbert Xu89319d382008-12-15 23:26:06 -08003051
3052 if (unlikely(!nskb))
3053 goto err;
3054
Herbert Xu9d8506c2013-11-21 11:10:04 -08003055 if (unlikely(pskb_trim(nskb, len))) {
3056 kfree_skb(nskb);
3057 goto err;
3058 }
3059
Alexander Duyckec47ea82012-05-04 14:26:56 +00003060 hsize = skb_end_offset(nskb);
Herbert Xu89319d382008-12-15 23:26:06 -08003061 if (skb_cow_head(nskb, doffset + headroom)) {
3062 kfree_skb(nskb);
3063 goto err;
3064 }
3065
Alexander Duyckec47ea82012-05-04 14:26:56 +00003066 nskb->truesize += skb_end_offset(nskb) - hsize;
Herbert Xu89319d382008-12-15 23:26:06 -08003067 skb_release_head_state(nskb);
3068 __skb_push(nskb, doffset);
3069 } else {
Mel Gormanc93bdd02012-07-31 16:44:19 -07003070 nskb = __alloc_skb(hsize + doffset + headroom,
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003071 GFP_ATOMIC, skb_alloc_rx_flag(head_skb),
Mel Gormanc93bdd02012-07-31 16:44:19 -07003072 NUMA_NO_NODE);
Herbert Xu89319d382008-12-15 23:26:06 -08003073
3074 if (unlikely(!nskb))
3075 goto err;
3076
3077 skb_reserve(nskb, headroom);
3078 __skb_put(nskb, doffset);
3079 }
Herbert Xuf4c50d92006-06-22 03:02:40 -07003080
3081 if (segs)
3082 tail->next = nskb;
3083 else
3084 segs = nskb;
3085 tail = nskb;
3086
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003087 __copy_skb_header(nskb, head_skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003088
Eric Dumazet030737b2013-10-19 11:42:54 -07003089 skb_headers_offset_update(nskb, skb_headroom(nskb) - headroom);
Vlad Yasevichfcdfe3a2014-07-31 10:33:06 -04003090 skb_reset_mac_len(nskb);
Pravin B Shelar68c33162013-02-14 14:02:41 +00003091
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003092 skb_copy_from_linear_data_offset(head_skb, -tnl_hlen,
Pravin B Shelar68c33162013-02-14 14:02:41 +00003093 nskb->data - tnl_hlen,
3094 doffset + tnl_hlen);
Herbert Xu89319d382008-12-15 23:26:06 -08003095
Herbert Xu9d8506c2013-11-21 11:10:04 -08003096 if (nskb->len == len + doffset)
Simon Horman1cdbcb72013-05-19 15:46:49 +00003097 goto perform_csum_check;
Herbert Xu89319d382008-12-15 23:26:06 -08003098
Tom Herberte585f232014-11-04 09:06:54 -08003099 if (!sg && !nskb->remcsum_offload) {
Herbert Xu6f85a122008-08-15 14:55:02 -07003100 nskb->ip_summed = CHECKSUM_NONE;
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003101 nskb->csum = skb_copy_and_csum_bits(head_skb, offset,
Herbert Xuf4c50d92006-06-22 03:02:40 -07003102 skb_put(nskb, len),
3103 len, 0);
Tom Herbert7e2b10c2014-06-04 17:20:02 -07003104 SKB_GSO_CB(nskb)->csum_start =
Tom Herbertde843722014-06-25 12:51:01 -07003105 skb_headroom(nskb) + doffset;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003106 continue;
3107 }
3108
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003109 nskb_frag = skb_shinfo(nskb)->frags;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003110
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003111 skb_copy_from_linear_data_offset(head_skb, offset,
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03003112 skb_put(nskb, hsize), hsize);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003113
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003114 skb_shinfo(nskb)->tx_flags = skb_shinfo(head_skb)->tx_flags &
3115 SKBTX_SHARED_FRAG;
Eric Dumazetcef401d2013-01-25 20:34:37 +00003116
Herbert Xu9d8506c2013-11-21 11:10:04 -08003117 while (pos < offset + len) {
3118 if (i >= nfrags) {
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003119 BUG_ON(skb_headlen(list_skb));
Herbert Xu9d8506c2013-11-21 11:10:04 -08003120
3121 i = 0;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003122 nfrags = skb_shinfo(list_skb)->nr_frags;
3123 frag = skb_shinfo(list_skb)->frags;
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02003124 frag_skb = list_skb;
Herbert Xu9d8506c2013-11-21 11:10:04 -08003125
3126 BUG_ON(!nfrags);
3127
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003128 list_skb = list_skb->next;
Herbert Xu9d8506c2013-11-21 11:10:04 -08003129 }
3130
3131 if (unlikely(skb_shinfo(nskb)->nr_frags >=
3132 MAX_SKB_FRAGS)) {
3133 net_warn_ratelimited(
3134 "skb_segment: too many frags: %u %u\n",
3135 pos, mss);
3136 goto err;
3137 }
3138
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02003139 if (unlikely(skb_orphan_frags(frag_skb, GFP_ATOMIC)))
3140 goto err;
3141
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02003142 *nskb_frag = *frag;
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003143 __skb_frag_ref(nskb_frag);
3144 size = skb_frag_size(nskb_frag);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003145
3146 if (pos < offset) {
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003147 nskb_frag->page_offset += offset - pos;
3148 skb_frag_size_sub(nskb_frag, offset - pos);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003149 }
3150
Herbert Xu89319d382008-12-15 23:26:06 -08003151 skb_shinfo(nskb)->nr_frags++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003152
3153 if (pos + size <= offset + len) {
3154 i++;
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02003155 frag++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003156 pos += size;
3157 } else {
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003158 skb_frag_size_sub(nskb_frag, pos + size - (offset + len));
Herbert Xu89319d382008-12-15 23:26:06 -08003159 goto skip_fraglist;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003160 }
3161
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02003162 nskb_frag++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003163 }
3164
Herbert Xu89319d382008-12-15 23:26:06 -08003165skip_fraglist:
Herbert Xuf4c50d92006-06-22 03:02:40 -07003166 nskb->data_len = len - hsize;
3167 nskb->len += nskb->data_len;
3168 nskb->truesize += nskb->data_len;
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003169
Simon Horman1cdbcb72013-05-19 15:46:49 +00003170perform_csum_check:
Tom Herberte585f232014-11-04 09:06:54 -08003171 if (!csum && !nskb->remcsum_offload) {
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003172 nskb->csum = skb_checksum(nskb, doffset,
3173 nskb->len - doffset, 0);
3174 nskb->ip_summed = CHECKSUM_NONE;
Tom Herbert7e2b10c2014-06-04 17:20:02 -07003175 SKB_GSO_CB(nskb)->csum_start =
3176 skb_headroom(nskb) + doffset;
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003177 }
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003178 } while ((offset += len) < head_skb->len);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003179
Eric Dumazetbec3cfd2014-10-03 20:59:19 -07003180 /* Some callers want to get the end of the list.
3181 * Put it in segs->prev to avoid walking the list.
3182 * (see validate_xmit_skb_list() for example)
3183 */
3184 segs->prev = tail;
Toshiaki Makita432c8562014-10-27 10:30:51 -07003185
3186 /* Following permits correct backpressure, for protocols
3187 * using skb_set_owner_w().
3188 * Idea is to tranfert ownership from head_skb to last segment.
3189 */
3190 if (head_skb->destructor == sock_wfree) {
3191 swap(tail->truesize, head_skb->truesize);
3192 swap(tail->destructor, head_skb->destructor);
3193 swap(tail->sk, head_skb->sk);
3194 }
Herbert Xuf4c50d92006-06-22 03:02:40 -07003195 return segs;
3196
3197err:
Eric Dumazet289dccb2013-12-20 14:29:08 -08003198 kfree_skb_list(segs);
Herbert Xuf4c50d92006-06-22 03:02:40 -07003199 return ERR_PTR(err);
3200}
Herbert Xuf4c50d92006-06-22 03:02:40 -07003201EXPORT_SYMBOL_GPL(skb_segment);
3202
Herbert Xu71d93b32008-12-15 23:42:33 -08003203int skb_gro_receive(struct sk_buff **head, struct sk_buff *skb)
3204{
Eric Dumazet8a291112013-10-08 09:02:23 -07003205 struct skb_shared_info *pinfo, *skbinfo = skb_shinfo(skb);
Herbert Xu67147ba2009-05-26 18:50:22 +00003206 unsigned int offset = skb_gro_offset(skb);
3207 unsigned int headlen = skb_headlen(skb);
Eric Dumazet8a291112013-10-08 09:02:23 -07003208 unsigned int len = skb_gro_len(skb);
Eric Dumazet58025e42015-03-05 13:47:48 -08003209 struct sk_buff *lp, *p = *head;
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003210 unsigned int delta_truesize;
Herbert Xu71d93b32008-12-15 23:42:33 -08003211
Eric Dumazet8a291112013-10-08 09:02:23 -07003212 if (unlikely(p->len + len >= 65536))
Herbert Xu71d93b32008-12-15 23:42:33 -08003213 return -E2BIG;
3214
Eric Dumazet29e98242014-05-16 11:34:37 -07003215 lp = NAPI_GRO_CB(p)->last;
Eric Dumazet8a291112013-10-08 09:02:23 -07003216 pinfo = skb_shinfo(lp);
3217
3218 if (headlen <= offset) {
Herbert Xu42da6992009-05-26 18:50:19 +00003219 skb_frag_t *frag;
Herbert Xu66e92fc2009-05-26 18:50:32 +00003220 skb_frag_t *frag2;
Herbert Xu9aaa1562009-05-26 18:50:33 +00003221 int i = skbinfo->nr_frags;
3222 int nr_frags = pinfo->nr_frags + i;
Herbert Xu42da6992009-05-26 18:50:19 +00003223
Herbert Xu66e92fc2009-05-26 18:50:32 +00003224 if (nr_frags > MAX_SKB_FRAGS)
Eric Dumazet8a291112013-10-08 09:02:23 -07003225 goto merge;
Herbert Xu81705ad2009-01-29 14:19:51 +00003226
Eric Dumazet8a291112013-10-08 09:02:23 -07003227 offset -= headlen;
Herbert Xu9aaa1562009-05-26 18:50:33 +00003228 pinfo->nr_frags = nr_frags;
3229 skbinfo->nr_frags = 0;
Herbert Xuf5572062009-01-14 20:40:03 -08003230
Herbert Xu9aaa1562009-05-26 18:50:33 +00003231 frag = pinfo->frags + nr_frags;
3232 frag2 = skbinfo->frags + i;
Herbert Xu66e92fc2009-05-26 18:50:32 +00003233 do {
3234 *--frag = *--frag2;
3235 } while (--i);
3236
3237 frag->page_offset += offset;
Eric Dumazet9e903e02011-10-18 21:00:24 +00003238 skb_frag_size_sub(frag, offset);
Herbert Xu66e92fc2009-05-26 18:50:32 +00003239
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003240 /* all fragments truesize : remove (head size + sk_buff) */
Alexander Duyckec47ea82012-05-04 14:26:56 +00003241 delta_truesize = skb->truesize -
3242 SKB_TRUESIZE(skb_end_offset(skb));
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003243
Herbert Xuf5572062009-01-14 20:40:03 -08003244 skb->truesize -= skb->data_len;
3245 skb->len -= skb->data_len;
3246 skb->data_len = 0;
3247
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003248 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE;
Herbert Xu5d38a072009-01-04 16:13:40 -08003249 goto done;
Eric Dumazetd7e88832012-04-30 08:10:34 +00003250 } else if (skb->head_frag) {
3251 int nr_frags = pinfo->nr_frags;
3252 skb_frag_t *frag = pinfo->frags + nr_frags;
3253 struct page *page = virt_to_head_page(skb->head);
3254 unsigned int first_size = headlen - offset;
3255 unsigned int first_offset;
3256
3257 if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS)
Eric Dumazet8a291112013-10-08 09:02:23 -07003258 goto merge;
Eric Dumazetd7e88832012-04-30 08:10:34 +00003259
3260 first_offset = skb->data -
3261 (unsigned char *)page_address(page) +
3262 offset;
3263
3264 pinfo->nr_frags = nr_frags + 1 + skbinfo->nr_frags;
3265
3266 frag->page.p = page;
3267 frag->page_offset = first_offset;
3268 skb_frag_size_set(frag, first_size);
3269
3270 memcpy(frag + 1, skbinfo->frags, sizeof(*frag) * skbinfo->nr_frags);
3271 /* We dont need to clear skbinfo->nr_frags here */
3272
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003273 delta_truesize = skb->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
Eric Dumazetd7e88832012-04-30 08:10:34 +00003274 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD;
3275 goto done;
Eric Dumazet8a291112013-10-08 09:02:23 -07003276 }
Herbert Xu71d93b32008-12-15 23:42:33 -08003277
3278merge:
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003279 delta_truesize = skb->truesize;
Herbert Xu67147ba2009-05-26 18:50:22 +00003280 if (offset > headlen) {
Michal Schmidtd1dc7ab2011-01-24 12:08:48 +00003281 unsigned int eat = offset - headlen;
3282
3283 skbinfo->frags[0].page_offset += eat;
Eric Dumazet9e903e02011-10-18 21:00:24 +00003284 skb_frag_size_sub(&skbinfo->frags[0], eat);
Michal Schmidtd1dc7ab2011-01-24 12:08:48 +00003285 skb->data_len -= eat;
3286 skb->len -= eat;
Herbert Xu67147ba2009-05-26 18:50:22 +00003287 offset = headlen;
Herbert Xu56035022009-02-05 21:26:52 -08003288 }
3289
Herbert Xu67147ba2009-05-26 18:50:22 +00003290 __skb_pull(skb, offset);
Herbert Xu56035022009-02-05 21:26:52 -08003291
Eric Dumazet29e98242014-05-16 11:34:37 -07003292 if (NAPI_GRO_CB(p)->last == p)
Eric Dumazet8a291112013-10-08 09:02:23 -07003293 skb_shinfo(p)->frag_list = skb;
3294 else
3295 NAPI_GRO_CB(p)->last->next = skb;
Eric Dumazetc3c7c252012-12-06 13:54:59 +00003296 NAPI_GRO_CB(p)->last = skb;
Eric Dumazetf4a775d2014-09-22 16:29:32 -07003297 __skb_header_release(skb);
Eric Dumazet8a291112013-10-08 09:02:23 -07003298 lp = p;
Herbert Xu71d93b32008-12-15 23:42:33 -08003299
Herbert Xu5d38a072009-01-04 16:13:40 -08003300done:
3301 NAPI_GRO_CB(p)->count++;
Herbert Xu37fe4732009-01-17 19:48:13 +00003302 p->data_len += len;
Eric Dumazet715dc1f2012-05-02 23:33:21 +00003303 p->truesize += delta_truesize;
Herbert Xu37fe4732009-01-17 19:48:13 +00003304 p->len += len;
Eric Dumazet8a291112013-10-08 09:02:23 -07003305 if (lp != p) {
3306 lp->data_len += len;
3307 lp->truesize += delta_truesize;
3308 lp->len += len;
3309 }
Herbert Xu71d93b32008-12-15 23:42:33 -08003310 NAPI_GRO_CB(skb)->same_flow = 1;
3311 return 0;
3312}
Herbert Xu71d93b32008-12-15 23:42:33 -08003313
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314void __init skb_init(void)
3315{
3316 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
3317 sizeof(struct sk_buff),
3318 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07003319 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09003320 NULL);
David S. Millerd179cd12005-08-17 14:57:30 -07003321 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
Eric Dumazetd0bf4a92014-09-29 13:29:15 -07003322 sizeof(struct sk_buff_fclones),
David S. Millerd179cd12005-08-17 14:57:30 -07003323 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07003324 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09003325 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326}
3327
David Howells716ea3a2007-04-02 20:19:53 -07003328/**
3329 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
3330 * @skb: Socket buffer containing the buffers to be mapped
3331 * @sg: The scatter-gather list to map into
3332 * @offset: The offset into the buffer's contents to start mapping
3333 * @len: Length of buffer space to be mapped
3334 *
3335 * Fill the specified scatter-gather list with mappings/pointers into a
3336 * region of the buffer space attached to a socket buffer.
3337 */
David S. Miller51c739d2007-10-30 21:29:29 -07003338static int
3339__skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
David Howells716ea3a2007-04-02 20:19:53 -07003340{
David S. Miller1a028e52007-04-27 15:21:23 -07003341 int start = skb_headlen(skb);
3342 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07003343 struct sk_buff *frag_iter;
David Howells716ea3a2007-04-02 20:19:53 -07003344 int elt = 0;
3345
3346 if (copy > 0) {
3347 if (copy > len)
3348 copy = len;
Jens Axboe642f1492007-10-24 11:20:47 +02003349 sg_set_buf(sg, skb->data + offset, copy);
David Howells716ea3a2007-04-02 20:19:53 -07003350 elt++;
3351 if ((len -= copy) == 0)
3352 return elt;
3353 offset += copy;
3354 }
3355
3356 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07003357 int end;
David Howells716ea3a2007-04-02 20:19:53 -07003358
Ilpo Järvinen547b7922008-07-25 21:43:18 -07003359 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07003360
Eric Dumazet9e903e02011-10-18 21:00:24 +00003361 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
David Howells716ea3a2007-04-02 20:19:53 -07003362 if ((copy = end - offset) > 0) {
3363 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
3364
3365 if (copy > len)
3366 copy = len;
Ian Campbellea2ab692011-08-22 23:44:58 +00003367 sg_set_page(&sg[elt], skb_frag_page(frag), copy,
Jens Axboe642f1492007-10-24 11:20:47 +02003368 frag->page_offset+offset-start);
David Howells716ea3a2007-04-02 20:19:53 -07003369 elt++;
3370 if (!(len -= copy))
3371 return elt;
3372 offset += copy;
3373 }
David S. Miller1a028e52007-04-27 15:21:23 -07003374 start = end;
David Howells716ea3a2007-04-02 20:19:53 -07003375 }
3376
David S. Millerfbb398a2009-06-09 00:18:59 -07003377 skb_walk_frags(skb, frag_iter) {
3378 int end;
David Howells716ea3a2007-04-02 20:19:53 -07003379
David S. Millerfbb398a2009-06-09 00:18:59 -07003380 WARN_ON(start > offset + len);
David Howells716ea3a2007-04-02 20:19:53 -07003381
David S. Millerfbb398a2009-06-09 00:18:59 -07003382 end = start + frag_iter->len;
3383 if ((copy = end - offset) > 0) {
3384 if (copy > len)
3385 copy = len;
3386 elt += __skb_to_sgvec(frag_iter, sg+elt, offset - start,
3387 copy);
3388 if ((len -= copy) == 0)
3389 return elt;
3390 offset += copy;
David Howells716ea3a2007-04-02 20:19:53 -07003391 }
David S. Millerfbb398a2009-06-09 00:18:59 -07003392 start = end;
David Howells716ea3a2007-04-02 20:19:53 -07003393 }
3394 BUG_ON(len);
3395 return elt;
3396}
3397
Fan Du25a91d82014-01-18 09:54:23 +08003398/* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given
3399 * sglist without mark the sg which contain last skb data as the end.
3400 * So the caller can mannipulate sg list as will when padding new data after
3401 * the first call without calling sg_unmark_end to expend sg list.
3402 *
3403 * Scenario to use skb_to_sgvec_nomark:
3404 * 1. sg_init_table
3405 * 2. skb_to_sgvec_nomark(payload1)
3406 * 3. skb_to_sgvec_nomark(payload2)
3407 *
3408 * This is equivalent to:
3409 * 1. sg_init_table
3410 * 2. skb_to_sgvec(payload1)
3411 * 3. sg_unmark_end
3412 * 4. skb_to_sgvec(payload2)
3413 *
3414 * When mapping mutilple payload conditionally, skb_to_sgvec_nomark
3415 * is more preferable.
3416 */
3417int skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg,
3418 int offset, int len)
3419{
3420 return __skb_to_sgvec(skb, sg, offset, len);
3421}
3422EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark);
3423
David S. Miller51c739d2007-10-30 21:29:29 -07003424int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
3425{
3426 int nsg = __skb_to_sgvec(skb, sg, offset, len);
3427
Jens Axboec46f2332007-10-31 12:06:37 +01003428 sg_mark_end(&sg[nsg - 1]);
David S. Miller51c739d2007-10-30 21:29:29 -07003429
3430 return nsg;
3431}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003432EXPORT_SYMBOL_GPL(skb_to_sgvec);
David S. Miller51c739d2007-10-30 21:29:29 -07003433
David Howells716ea3a2007-04-02 20:19:53 -07003434/**
3435 * skb_cow_data - Check that a socket buffer's data buffers are writable
3436 * @skb: The socket buffer to check.
3437 * @tailbits: Amount of trailing space to be added
3438 * @trailer: Returned pointer to the skb where the @tailbits space begins
3439 *
3440 * Make sure that the data buffers attached to a socket buffer are
3441 * writable. If they are not, private copies are made of the data buffers
3442 * and the socket buffer is set to use these instead.
3443 *
3444 * If @tailbits is given, make sure that there is space to write @tailbits
3445 * bytes of data beyond current end of socket buffer. @trailer will be
3446 * set to point to the skb in which this space begins.
3447 *
3448 * The number of scatterlist elements required to completely map the
3449 * COW'd and extended socket buffer will be returned.
3450 */
3451int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
3452{
3453 int copyflag;
3454 int elt;
3455 struct sk_buff *skb1, **skb_p;
3456
3457 /* If skb is cloned or its head is paged, reallocate
3458 * head pulling out all the pages (pages are considered not writable
3459 * at the moment even if they are anonymous).
3460 */
3461 if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
3462 __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
3463 return -ENOMEM;
3464
3465 /* Easy case. Most of packets will go this way. */
David S. Miller21dc3302010-08-23 00:13:46 -07003466 if (!skb_has_frag_list(skb)) {
David Howells716ea3a2007-04-02 20:19:53 -07003467 /* A little of trouble, not enough of space for trailer.
3468 * This should not happen, when stack is tuned to generate
3469 * good frames. OK, on miss we reallocate and reserve even more
3470 * space, 128 bytes is fair. */
3471
3472 if (skb_tailroom(skb) < tailbits &&
3473 pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
3474 return -ENOMEM;
3475
3476 /* Voila! */
3477 *trailer = skb;
3478 return 1;
3479 }
3480
3481 /* Misery. We are in troubles, going to mincer fragments... */
3482
3483 elt = 1;
3484 skb_p = &skb_shinfo(skb)->frag_list;
3485 copyflag = 0;
3486
3487 while ((skb1 = *skb_p) != NULL) {
3488 int ntail = 0;
3489
3490 /* The fragment is partially pulled by someone,
3491 * this can happen on input. Copy it and everything
3492 * after it. */
3493
3494 if (skb_shared(skb1))
3495 copyflag = 1;
3496
3497 /* If the skb is the last, worry about trailer. */
3498
3499 if (skb1->next == NULL && tailbits) {
3500 if (skb_shinfo(skb1)->nr_frags ||
David S. Miller21dc3302010-08-23 00:13:46 -07003501 skb_has_frag_list(skb1) ||
David Howells716ea3a2007-04-02 20:19:53 -07003502 skb_tailroom(skb1) < tailbits)
3503 ntail = tailbits + 128;
3504 }
3505
3506 if (copyflag ||
3507 skb_cloned(skb1) ||
3508 ntail ||
3509 skb_shinfo(skb1)->nr_frags ||
David S. Miller21dc3302010-08-23 00:13:46 -07003510 skb_has_frag_list(skb1)) {
David Howells716ea3a2007-04-02 20:19:53 -07003511 struct sk_buff *skb2;
3512
3513 /* Fuck, we are miserable poor guys... */
3514 if (ntail == 0)
3515 skb2 = skb_copy(skb1, GFP_ATOMIC);
3516 else
3517 skb2 = skb_copy_expand(skb1,
3518 skb_headroom(skb1),
3519 ntail,
3520 GFP_ATOMIC);
3521 if (unlikely(skb2 == NULL))
3522 return -ENOMEM;
3523
3524 if (skb1->sk)
3525 skb_set_owner_w(skb2, skb1->sk);
3526
3527 /* Looking around. Are we still alive?
3528 * OK, link new skb, drop old one */
3529
3530 skb2->next = skb1->next;
3531 *skb_p = skb2;
3532 kfree_skb(skb1);
3533 skb1 = skb2;
3534 }
3535 elt++;
3536 *trailer = skb1;
3537 skb_p = &skb1->next;
3538 }
3539
3540 return elt;
3541}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003542EXPORT_SYMBOL_GPL(skb_cow_data);
David Howells716ea3a2007-04-02 20:19:53 -07003543
Eric Dumazetb1faf562010-05-31 23:44:05 -07003544static void sock_rmem_free(struct sk_buff *skb)
3545{
3546 struct sock *sk = skb->sk;
3547
3548 atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
3549}
3550
3551/*
3552 * Note: We dont mem charge error packets (no sk_forward_alloc changes)
3553 */
3554int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb)
3555{
3556 if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
Eric Dumazet95c96172012-04-15 05:58:06 +00003557 (unsigned int)sk->sk_rcvbuf)
Eric Dumazetb1faf562010-05-31 23:44:05 -07003558 return -ENOMEM;
3559
3560 skb_orphan(skb);
3561 skb->sk = sk;
3562 skb->destructor = sock_rmem_free;
3563 atomic_add(skb->truesize, &sk->sk_rmem_alloc);
3564
Eric Dumazetabb57ea2011-05-18 02:21:31 -04003565 /* before exiting rcu section, make sure dst is refcounted */
3566 skb_dst_force(skb);
3567
Eric Dumazetb1faf562010-05-31 23:44:05 -07003568 skb_queue_tail(&sk->sk_error_queue, skb);
3569 if (!sock_flag(sk, SOCK_DEAD))
David S. Miller676d2362014-04-11 16:15:36 -04003570 sk->sk_data_ready(sk);
Eric Dumazetb1faf562010-05-31 23:44:05 -07003571 return 0;
3572}
3573EXPORT_SYMBOL(sock_queue_err_skb);
3574
Willem de Bruijn364a9e92014-08-31 21:30:27 -04003575struct sk_buff *sock_dequeue_err_skb(struct sock *sk)
3576{
3577 struct sk_buff_head *q = &sk->sk_error_queue;
3578 struct sk_buff *skb, *skb_next;
Eric Dumazet997d5c32015-02-18 05:47:55 -08003579 unsigned long flags;
Willem de Bruijn364a9e92014-08-31 21:30:27 -04003580 int err = 0;
3581
Eric Dumazet997d5c32015-02-18 05:47:55 -08003582 spin_lock_irqsave(&q->lock, flags);
Willem de Bruijn364a9e92014-08-31 21:30:27 -04003583 skb = __skb_dequeue(q);
3584 if (skb && (skb_next = skb_peek(q)))
3585 err = SKB_EXT_ERR(skb_next)->ee.ee_errno;
Eric Dumazet997d5c32015-02-18 05:47:55 -08003586 spin_unlock_irqrestore(&q->lock, flags);
Willem de Bruijn364a9e92014-08-31 21:30:27 -04003587
3588 sk->sk_err = err;
3589 if (err)
3590 sk->sk_error_report(sk);
3591
3592 return skb;
3593}
3594EXPORT_SYMBOL(sock_dequeue_err_skb);
3595
Alexander Duyckcab41c42014-09-10 18:05:26 -04003596/**
3597 * skb_clone_sk - create clone of skb, and take reference to socket
3598 * @skb: the skb to clone
3599 *
3600 * This function creates a clone of a buffer that holds a reference on
3601 * sk_refcnt. Buffers created via this function are meant to be
3602 * returned using sock_queue_err_skb, or free via kfree_skb.
3603 *
3604 * When passing buffers allocated with this function to sock_queue_err_skb
3605 * it is necessary to wrap the call with sock_hold/sock_put in order to
3606 * prevent the socket from being released prior to being enqueued on
3607 * the sk_error_queue.
3608 */
Alexander Duyck62bccb82014-09-04 13:31:35 -04003609struct sk_buff *skb_clone_sk(struct sk_buff *skb)
3610{
3611 struct sock *sk = skb->sk;
3612 struct sk_buff *clone;
3613
3614 if (!sk || !atomic_inc_not_zero(&sk->sk_refcnt))
3615 return NULL;
3616
3617 clone = skb_clone(skb, GFP_ATOMIC);
3618 if (!clone) {
3619 sock_put(sk);
3620 return NULL;
3621 }
3622
3623 clone->sk = sk;
3624 clone->destructor = sock_efree;
3625
3626 return clone;
3627}
3628EXPORT_SYMBOL(skb_clone_sk);
3629
Alexander Duyck37846ef2014-09-04 13:31:10 -04003630static void __skb_complete_tx_timestamp(struct sk_buff *skb,
3631 struct sock *sk,
3632 int tstype)
Patrick Ohlyac45f602009-02-12 05:03:37 +00003633{
Patrick Ohlyac45f602009-02-12 05:03:37 +00003634 struct sock_exterr_skb *serr;
Patrick Ohlyac45f602009-02-12 05:03:37 +00003635 int err;
3636
Patrick Ohlyac45f602009-02-12 05:03:37 +00003637 serr = SKB_EXT_ERR(skb);
3638 memset(serr, 0, sizeof(*serr));
3639 serr->ee.ee_errno = ENOMSG;
3640 serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
Willem de Bruijne7fd2882014-08-04 22:11:48 -04003641 serr->ee.ee_info = tstype;
Willem de Bruijn4ed2d762014-08-04 22:11:49 -04003642 if (sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) {
Willem de Bruijn09c2d252014-08-04 22:11:47 -04003643 serr->ee.ee_data = skb_shinfo(skb)->tskey;
Willem de Bruijn4ed2d762014-08-04 22:11:49 -04003644 if (sk->sk_protocol == IPPROTO_TCP)
3645 serr->ee.ee_data -= sk->sk_tskey;
3646 }
Eric Dumazet29030372010-05-29 00:20:48 -07003647
Patrick Ohlyac45f602009-02-12 05:03:37 +00003648 err = sock_queue_err_skb(sk, skb);
Eric Dumazet29030372010-05-29 00:20:48 -07003649
Patrick Ohlyac45f602009-02-12 05:03:37 +00003650 if (err)
3651 kfree_skb(skb);
3652}
Alexander Duyck37846ef2014-09-04 13:31:10 -04003653
Willem de Bruijnb245be12015-01-30 13:29:32 -05003654static bool skb_may_tx_timestamp(struct sock *sk, bool tsonly)
3655{
3656 bool ret;
3657
3658 if (likely(sysctl_tstamp_allow_data || tsonly))
3659 return true;
3660
3661 read_lock_bh(&sk->sk_callback_lock);
3662 ret = sk->sk_socket && sk->sk_socket->file &&
3663 file_ns_capable(sk->sk_socket->file, &init_user_ns, CAP_NET_RAW);
3664 read_unlock_bh(&sk->sk_callback_lock);
3665 return ret;
3666}
3667
Alexander Duyck37846ef2014-09-04 13:31:10 -04003668void skb_complete_tx_timestamp(struct sk_buff *skb,
3669 struct skb_shared_hwtstamps *hwtstamps)
3670{
3671 struct sock *sk = skb->sk;
3672
Willem de Bruijnb245be12015-01-30 13:29:32 -05003673 if (!skb_may_tx_timestamp(sk, false))
3674 return;
3675
Alexander Duyck62bccb82014-09-04 13:31:35 -04003676 /* take a reference to prevent skb_orphan() from freeing the socket */
3677 sock_hold(sk);
Alexander Duyck37846ef2014-09-04 13:31:10 -04003678
Alexander Duyck62bccb82014-09-04 13:31:35 -04003679 *skb_hwtstamps(skb) = *hwtstamps;
3680 __skb_complete_tx_timestamp(skb, sk, SCM_TSTAMP_SND);
Alexander Duyck37846ef2014-09-04 13:31:10 -04003681
3682 sock_put(sk);
3683}
3684EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp);
3685
3686void __skb_tstamp_tx(struct sk_buff *orig_skb,
3687 struct skb_shared_hwtstamps *hwtstamps,
3688 struct sock *sk, int tstype)
3689{
3690 struct sk_buff *skb;
Willem de Bruijn3a8dd972015-03-11 15:43:55 -04003691 bool tsonly;
Alexander Duyck37846ef2014-09-04 13:31:10 -04003692
Willem de Bruijn3a8dd972015-03-11 15:43:55 -04003693 if (!sk)
3694 return;
3695
3696 tsonly = sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TSONLY;
3697 if (!skb_may_tx_timestamp(sk, tsonly))
Alexander Duyck37846ef2014-09-04 13:31:10 -04003698 return;
3699
Willem de Bruijn49ca0d82015-01-30 13:29:31 -05003700 if (tsonly)
3701 skb = alloc_skb(0, GFP_ATOMIC);
Alexander Duyck37846ef2014-09-04 13:31:10 -04003702 else
Willem de Bruijn49ca0d82015-01-30 13:29:31 -05003703 skb = skb_clone(orig_skb, GFP_ATOMIC);
Alexander Duyck37846ef2014-09-04 13:31:10 -04003704 if (!skb)
3705 return;
3706
Willem de Bruijn49ca0d82015-01-30 13:29:31 -05003707 if (tsonly) {
3708 skb_shinfo(skb)->tx_flags = skb_shinfo(orig_skb)->tx_flags;
3709 skb_shinfo(skb)->tskey = skb_shinfo(orig_skb)->tskey;
3710 }
3711
3712 if (hwtstamps)
3713 *skb_hwtstamps(skb) = *hwtstamps;
3714 else
3715 skb->tstamp = ktime_get_real();
3716
Alexander Duyck37846ef2014-09-04 13:31:10 -04003717 __skb_complete_tx_timestamp(skb, sk, tstype);
3718}
Willem de Bruijne7fd2882014-08-04 22:11:48 -04003719EXPORT_SYMBOL_GPL(__skb_tstamp_tx);
3720
3721void skb_tstamp_tx(struct sk_buff *orig_skb,
3722 struct skb_shared_hwtstamps *hwtstamps)
3723{
3724 return __skb_tstamp_tx(orig_skb, hwtstamps, orig_skb->sk,
3725 SCM_TSTAMP_SND);
3726}
Patrick Ohlyac45f602009-02-12 05:03:37 +00003727EXPORT_SYMBOL_GPL(skb_tstamp_tx);
3728
Johannes Berg6e3e9392011-11-09 10:15:42 +01003729void skb_complete_wifi_ack(struct sk_buff *skb, bool acked)
3730{
3731 struct sock *sk = skb->sk;
3732 struct sock_exterr_skb *serr;
3733 int err;
3734
3735 skb->wifi_acked_valid = 1;
3736 skb->wifi_acked = acked;
3737
3738 serr = SKB_EXT_ERR(skb);
3739 memset(serr, 0, sizeof(*serr));
3740 serr->ee.ee_errno = ENOMSG;
3741 serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS;
3742
Alexander Duyckbf7fa552014-09-10 18:05:42 -04003743 /* take a reference to prevent skb_orphan() from freeing the socket */
3744 sock_hold(sk);
3745
Johannes Berg6e3e9392011-11-09 10:15:42 +01003746 err = sock_queue_err_skb(sk, skb);
3747 if (err)
3748 kfree_skb(skb);
Alexander Duyckbf7fa552014-09-10 18:05:42 -04003749
3750 sock_put(sk);
Johannes Berg6e3e9392011-11-09 10:15:42 +01003751}
3752EXPORT_SYMBOL_GPL(skb_complete_wifi_ack);
3753
Rusty Russellf35d9d82008-02-04 23:49:54 -05003754/**
3755 * skb_partial_csum_set - set up and verify partial csum values for packet
3756 * @skb: the skb to set
3757 * @start: the number of bytes after skb->data to start checksumming.
3758 * @off: the offset from start to place the checksum.
3759 *
3760 * For untrusted partially-checksummed packets, we need to make sure the values
3761 * for skb->csum_start and skb->csum_offset are valid so we don't oops.
3762 *
3763 * This function checks and sets those values and skb->ip_summed: if this
3764 * returns false you should drop the packet.
3765 */
3766bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
3767{
Herbert Xu5ff8dda2009-06-04 01:22:01 +00003768 if (unlikely(start > skb_headlen(skb)) ||
3769 unlikely((int)start + off > skb_headlen(skb) - 2)) {
Joe Perchese87cc472012-05-13 21:56:26 +00003770 net_warn_ratelimited("bad partial csum: csum=%u/%u len=%u\n",
3771 start, off, skb_headlen(skb));
Rusty Russellf35d9d82008-02-04 23:49:54 -05003772 return false;
3773 }
3774 skb->ip_summed = CHECKSUM_PARTIAL;
3775 skb->csum_start = skb_headroom(skb) + start;
3776 skb->csum_offset = off;
Jason Wange5d5dec2013-03-26 23:11:20 +00003777 skb_set_transport_header(skb, start);
Rusty Russellf35d9d82008-02-04 23:49:54 -05003778 return true;
3779}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003780EXPORT_SYMBOL_GPL(skb_partial_csum_set);
Rusty Russellf35d9d82008-02-04 23:49:54 -05003781
Paul Durranted1f50c2014-01-09 10:02:46 +00003782static int skb_maybe_pull_tail(struct sk_buff *skb, unsigned int len,
3783 unsigned int max)
3784{
3785 if (skb_headlen(skb) >= len)
3786 return 0;
3787
3788 /* If we need to pullup then pullup to the max, so we
3789 * won't need to do it again.
3790 */
3791 if (max > skb->len)
3792 max = skb->len;
3793
3794 if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL)
3795 return -ENOMEM;
3796
3797 if (skb_headlen(skb) < len)
3798 return -EPROTO;
3799
3800 return 0;
3801}
3802
Jan Beulichf9708b42014-03-11 13:56:05 +00003803#define MAX_TCP_HDR_LEN (15 * 4)
3804
3805static __sum16 *skb_checksum_setup_ip(struct sk_buff *skb,
3806 typeof(IPPROTO_IP) proto,
3807 unsigned int off)
3808{
3809 switch (proto) {
3810 int err;
3811
3812 case IPPROTO_TCP:
3813 err = skb_maybe_pull_tail(skb, off + sizeof(struct tcphdr),
3814 off + MAX_TCP_HDR_LEN);
3815 if (!err && !skb_partial_csum_set(skb, off,
3816 offsetof(struct tcphdr,
3817 check)))
3818 err = -EPROTO;
3819 return err ? ERR_PTR(err) : &tcp_hdr(skb)->check;
3820
3821 case IPPROTO_UDP:
3822 err = skb_maybe_pull_tail(skb, off + sizeof(struct udphdr),
3823 off + sizeof(struct udphdr));
3824 if (!err && !skb_partial_csum_set(skb, off,
3825 offsetof(struct udphdr,
3826 check)))
3827 err = -EPROTO;
3828 return err ? ERR_PTR(err) : &udp_hdr(skb)->check;
3829 }
3830
3831 return ERR_PTR(-EPROTO);
3832}
3833
Paul Durranted1f50c2014-01-09 10:02:46 +00003834/* This value should be large enough to cover a tagged ethernet header plus
3835 * maximally sized IP and TCP or UDP headers.
3836 */
3837#define MAX_IP_HDR_LEN 128
3838
Jan Beulichf9708b42014-03-11 13:56:05 +00003839static int skb_checksum_setup_ipv4(struct sk_buff *skb, bool recalculate)
Paul Durranted1f50c2014-01-09 10:02:46 +00003840{
3841 unsigned int off;
3842 bool fragment;
Jan Beulichf9708b42014-03-11 13:56:05 +00003843 __sum16 *csum;
Paul Durranted1f50c2014-01-09 10:02:46 +00003844 int err;
3845
3846 fragment = false;
3847
3848 err = skb_maybe_pull_tail(skb,
3849 sizeof(struct iphdr),
3850 MAX_IP_HDR_LEN);
3851 if (err < 0)
3852 goto out;
3853
3854 if (ip_hdr(skb)->frag_off & htons(IP_OFFSET | IP_MF))
3855 fragment = true;
3856
3857 off = ip_hdrlen(skb);
3858
3859 err = -EPROTO;
3860
3861 if (fragment)
3862 goto out;
3863
Jan Beulichf9708b42014-03-11 13:56:05 +00003864 csum = skb_checksum_setup_ip(skb, ip_hdr(skb)->protocol, off);
3865 if (IS_ERR(csum))
3866 return PTR_ERR(csum);
Paul Durranted1f50c2014-01-09 10:02:46 +00003867
Jan Beulichf9708b42014-03-11 13:56:05 +00003868 if (recalculate)
3869 *csum = ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
3870 ip_hdr(skb)->daddr,
3871 skb->len - off,
3872 ip_hdr(skb)->protocol, 0);
Paul Durranted1f50c2014-01-09 10:02:46 +00003873 err = 0;
3874
3875out:
3876 return err;
3877}
3878
3879/* This value should be large enough to cover a tagged ethernet header plus
3880 * an IPv6 header, all options, and a maximal TCP or UDP header.
3881 */
3882#define MAX_IPV6_HDR_LEN 256
3883
3884#define OPT_HDR(type, skb, off) \
3885 (type *)(skb_network_header(skb) + (off))
3886
3887static int skb_checksum_setup_ipv6(struct sk_buff *skb, bool recalculate)
3888{
3889 int err;
3890 u8 nexthdr;
3891 unsigned int off;
3892 unsigned int len;
3893 bool fragment;
3894 bool done;
Jan Beulichf9708b42014-03-11 13:56:05 +00003895 __sum16 *csum;
Paul Durranted1f50c2014-01-09 10:02:46 +00003896
3897 fragment = false;
3898 done = false;
3899
3900 off = sizeof(struct ipv6hdr);
3901
3902 err = skb_maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN);
3903 if (err < 0)
3904 goto out;
3905
3906 nexthdr = ipv6_hdr(skb)->nexthdr;
3907
3908 len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len);
3909 while (off <= len && !done) {
3910 switch (nexthdr) {
3911 case IPPROTO_DSTOPTS:
3912 case IPPROTO_HOPOPTS:
3913 case IPPROTO_ROUTING: {
3914 struct ipv6_opt_hdr *hp;
3915
3916 err = skb_maybe_pull_tail(skb,
3917 off +
3918 sizeof(struct ipv6_opt_hdr),
3919 MAX_IPV6_HDR_LEN);
3920 if (err < 0)
3921 goto out;
3922
3923 hp = OPT_HDR(struct ipv6_opt_hdr, skb, off);
3924 nexthdr = hp->nexthdr;
3925 off += ipv6_optlen(hp);
3926 break;
3927 }
3928 case IPPROTO_AH: {
3929 struct ip_auth_hdr *hp;
3930
3931 err = skb_maybe_pull_tail(skb,
3932 off +
3933 sizeof(struct ip_auth_hdr),
3934 MAX_IPV6_HDR_LEN);
3935 if (err < 0)
3936 goto out;
3937
3938 hp = OPT_HDR(struct ip_auth_hdr, skb, off);
3939 nexthdr = hp->nexthdr;
3940 off += ipv6_authlen(hp);
3941 break;
3942 }
3943 case IPPROTO_FRAGMENT: {
3944 struct frag_hdr *hp;
3945
3946 err = skb_maybe_pull_tail(skb,
3947 off +
3948 sizeof(struct frag_hdr),
3949 MAX_IPV6_HDR_LEN);
3950 if (err < 0)
3951 goto out;
3952
3953 hp = OPT_HDR(struct frag_hdr, skb, off);
3954
3955 if (hp->frag_off & htons(IP6_OFFSET | IP6_MF))
3956 fragment = true;
3957
3958 nexthdr = hp->nexthdr;
3959 off += sizeof(struct frag_hdr);
3960 break;
3961 }
3962 default:
3963 done = true;
3964 break;
3965 }
3966 }
3967
3968 err = -EPROTO;
3969
3970 if (!done || fragment)
3971 goto out;
3972
Jan Beulichf9708b42014-03-11 13:56:05 +00003973 csum = skb_checksum_setup_ip(skb, nexthdr, off);
3974 if (IS_ERR(csum))
3975 return PTR_ERR(csum);
Paul Durranted1f50c2014-01-09 10:02:46 +00003976
Jan Beulichf9708b42014-03-11 13:56:05 +00003977 if (recalculate)
3978 *csum = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
3979 &ipv6_hdr(skb)->daddr,
3980 skb->len - off, nexthdr, 0);
Paul Durranted1f50c2014-01-09 10:02:46 +00003981 err = 0;
3982
3983out:
3984 return err;
3985}
3986
3987/**
3988 * skb_checksum_setup - set up partial checksum offset
3989 * @skb: the skb to set up
3990 * @recalculate: if true the pseudo-header checksum will be recalculated
3991 */
3992int skb_checksum_setup(struct sk_buff *skb, bool recalculate)
3993{
3994 int err;
3995
3996 switch (skb->protocol) {
3997 case htons(ETH_P_IP):
Jan Beulichf9708b42014-03-11 13:56:05 +00003998 err = skb_checksum_setup_ipv4(skb, recalculate);
Paul Durranted1f50c2014-01-09 10:02:46 +00003999 break;
4000
4001 case htons(ETH_P_IPV6):
4002 err = skb_checksum_setup_ipv6(skb, recalculate);
4003 break;
4004
4005 default:
4006 err = -EPROTO;
4007 break;
4008 }
4009
4010 return err;
4011}
4012EXPORT_SYMBOL(skb_checksum_setup);
4013
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004014/**
4015 * skb_checksum_maybe_trim - maybe trims the given skb
4016 * @skb: the skb to check
4017 * @transport_len: the data length beyond the network header
4018 *
4019 * Checks whether the given skb has data beyond the given transport length.
4020 * If so, returns a cloned skb trimmed to this transport length.
4021 * Otherwise returns the provided skb. Returns NULL in error cases
4022 * (e.g. transport_len exceeds skb length or out-of-memory).
4023 *
4024 * Caller needs to set the skb transport header and release the returned skb.
4025 * Provided skb is consumed.
4026 */
4027static struct sk_buff *skb_checksum_maybe_trim(struct sk_buff *skb,
4028 unsigned int transport_len)
4029{
4030 struct sk_buff *skb_chk;
4031 unsigned int len = skb_transport_offset(skb) + transport_len;
4032 int ret;
4033
4034 if (skb->len < len) {
4035 kfree_skb(skb);
4036 return NULL;
4037 } else if (skb->len == len) {
4038 return skb;
4039 }
4040
4041 skb_chk = skb_clone(skb, GFP_ATOMIC);
4042 kfree_skb(skb);
4043
4044 if (!skb_chk)
4045 return NULL;
4046
4047 ret = pskb_trim_rcsum(skb_chk, len);
4048 if (ret) {
4049 kfree_skb(skb_chk);
4050 return NULL;
4051 }
4052
4053 return skb_chk;
4054}
4055
4056/**
4057 * skb_checksum_trimmed - validate checksum of an skb
4058 * @skb: the skb to check
4059 * @transport_len: the data length beyond the network header
4060 * @skb_chkf: checksum function to use
4061 *
4062 * Applies the given checksum function skb_chkf to the provided skb.
4063 * Returns a checked and maybe trimmed skb. Returns NULL on error.
4064 *
4065 * If the skb has data beyond the given transport length, then a
4066 * trimmed & cloned skb is checked and returned.
4067 *
4068 * Caller needs to set the skb transport header and release the returned skb.
4069 * Provided skb is consumed.
4070 */
4071struct sk_buff *skb_checksum_trimmed(struct sk_buff *skb,
4072 unsigned int transport_len,
4073 __sum16(*skb_chkf)(struct sk_buff *skb))
4074{
4075 struct sk_buff *skb_chk;
4076 unsigned int offset = skb_transport_offset(skb);
Linus Lüssingfcba67c2015-05-05 00:19:35 +02004077 __sum16 ret;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02004078
4079 skb_chk = skb_checksum_maybe_trim(skb, transport_len);
4080 if (!skb_chk)
4081 return NULL;
4082
4083 if (!pskb_may_pull(skb_chk, offset)) {
4084 kfree_skb(skb_chk);
4085 return NULL;
4086 }
4087
4088 __skb_pull(skb_chk, offset);
4089 ret = skb_chkf(skb_chk);
4090 __skb_push(skb_chk, offset);
4091
4092 if (ret) {
4093 kfree_skb(skb_chk);
4094 return NULL;
4095 }
4096
4097 return skb_chk;
4098}
4099EXPORT_SYMBOL(skb_checksum_trimmed);
4100
Ben Hutchings4497b072008-06-19 16:22:28 -07004101void __skb_warn_lro_forwarding(const struct sk_buff *skb)
4102{
Joe Perchese87cc472012-05-13 21:56:26 +00004103 net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n",
4104 skb->dev->name);
Ben Hutchings4497b072008-06-19 16:22:28 -07004105}
Ben Hutchings4497b072008-06-19 16:22:28 -07004106EXPORT_SYMBOL(__skb_warn_lro_forwarding);
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004107
4108void kfree_skb_partial(struct sk_buff *skb, bool head_stolen)
4109{
Eric Dumazet3d861f62012-10-22 09:03:40 +00004110 if (head_stolen) {
4111 skb_release_head_state(skb);
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004112 kmem_cache_free(skbuff_head_cache, skb);
Eric Dumazet3d861f62012-10-22 09:03:40 +00004113 } else {
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004114 __kfree_skb(skb);
Eric Dumazet3d861f62012-10-22 09:03:40 +00004115 }
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004116}
4117EXPORT_SYMBOL(kfree_skb_partial);
4118
4119/**
4120 * skb_try_coalesce - try to merge skb to prior one
4121 * @to: prior buffer
4122 * @from: buffer to add
4123 * @fragstolen: pointer to boolean
Randy Dunlapc6c4b972012-06-08 14:01:44 +00004124 * @delta_truesize: how much more was allocated than was requested
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004125 */
4126bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
4127 bool *fragstolen, int *delta_truesize)
4128{
4129 int i, delta, len = from->len;
4130
4131 *fragstolen = false;
4132
4133 if (skb_cloned(to))
4134 return false;
4135
4136 if (len <= skb_tailroom(to)) {
Eric Dumazete93a0432014-09-15 04:19:52 -07004137 if (len)
4138 BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len));
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004139 *delta_truesize = 0;
4140 return true;
4141 }
4142
4143 if (skb_has_frag_list(to) || skb_has_frag_list(from))
4144 return false;
4145
4146 if (skb_headlen(from) != 0) {
4147 struct page *page;
4148 unsigned int offset;
4149
4150 if (skb_shinfo(to)->nr_frags +
4151 skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
4152 return false;
4153
4154 if (skb_head_is_locked(from))
4155 return false;
4156
4157 delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
4158
4159 page = virt_to_head_page(from->head);
4160 offset = from->data - (unsigned char *)page_address(page);
4161
4162 skb_fill_page_desc(to, skb_shinfo(to)->nr_frags,
4163 page, offset, skb_headlen(from));
4164 *fragstolen = true;
4165 } else {
4166 if (skb_shinfo(to)->nr_frags +
4167 skb_shinfo(from)->nr_frags > MAX_SKB_FRAGS)
4168 return false;
4169
Weiping Panf4b549a2012-09-28 20:15:30 +00004170 delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from));
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004171 }
4172
4173 WARN_ON_ONCE(delta < len);
4174
4175 memcpy(skb_shinfo(to)->frags + skb_shinfo(to)->nr_frags,
4176 skb_shinfo(from)->frags,
4177 skb_shinfo(from)->nr_frags * sizeof(skb_frag_t));
4178 skb_shinfo(to)->nr_frags += skb_shinfo(from)->nr_frags;
4179
4180 if (!skb_cloned(from))
4181 skb_shinfo(from)->nr_frags = 0;
4182
Li RongQing8ea853f2012-09-18 16:53:21 +00004183 /* if the skb is not cloned this does nothing
4184 * since we set nr_frags to 0.
4185 */
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004186 for (i = 0; i < skb_shinfo(from)->nr_frags; i++)
4187 skb_frag_ref(from, i);
4188
4189 to->truesize += delta;
4190 to->len += len;
4191 to->data_len += len;
4192
4193 *delta_truesize = delta;
4194 return true;
4195}
4196EXPORT_SYMBOL(skb_try_coalesce);
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004197
4198/**
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004199 * skb_scrub_packet - scrub an skb
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004200 *
4201 * @skb: buffer to clean
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004202 * @xnet: packet is crossing netns
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004203 *
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004204 * skb_scrub_packet can be used after encapsulating or decapsulting a packet
4205 * into/from a tunnel. Some information have to be cleared during these
4206 * operations.
4207 * skb_scrub_packet can also be used to clean a skb before injecting it in
4208 * another namespace (@xnet == true). We have to clear all information in the
4209 * skb that could impact namespace isolation.
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004210 */
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02004211void skb_scrub_packet(struct sk_buff *skb, bool xnet)
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004212{
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004213 skb->tstamp.tv64 = 0;
4214 skb->pkt_type = PACKET_HOST;
4215 skb->skb_iif = 0;
WANG Cong60ff7462014-05-04 16:39:18 -07004216 skb->ignore_df = 0;
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004217 skb_dst_drop(skb);
Eric Dumazetc29390c2015-03-11 18:42:02 -07004218 skb_sender_cpu_clear(skb);
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004219 secpath_reset(skb);
4220 nf_reset(skb);
4221 nf_reset_trace(skb);
Herbert Xu213dd742015-04-16 09:03:27 +08004222
4223 if (!xnet)
4224 return;
4225
4226 skb_orphan(skb);
4227 skb->mark = 0;
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02004228}
4229EXPORT_SYMBOL_GPL(skb_scrub_packet);
Florian Westphalde960aa2014-01-26 10:58:16 +01004230
4231/**
4232 * skb_gso_transport_seglen - Return length of individual segments of a gso packet
4233 *
4234 * @skb: GSO skb
4235 *
4236 * skb_gso_transport_seglen is used to determine the real size of the
4237 * individual segments, including Layer4 headers (TCP/UDP).
4238 *
4239 * The MAC/L2 or network (IP, IPv6) headers are not accounted for.
4240 */
4241unsigned int skb_gso_transport_seglen(const struct sk_buff *skb)
4242{
4243 const struct skb_shared_info *shinfo = skb_shinfo(skb);
Florian Westphalf993bc22014-10-20 13:49:18 +02004244 unsigned int thlen = 0;
Florian Westphalde960aa2014-01-26 10:58:16 +01004245
Florian Westphalf993bc22014-10-20 13:49:18 +02004246 if (skb->encapsulation) {
4247 thlen = skb_inner_transport_header(skb) -
4248 skb_transport_header(skb);
Florian Westphal6d39d582014-04-09 10:28:50 +02004249
Florian Westphalf993bc22014-10-20 13:49:18 +02004250 if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)))
4251 thlen += inner_tcp_hdrlen(skb);
4252 } else if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) {
4253 thlen = tcp_hdrlen(skb);
4254 }
Florian Westphal6d39d582014-04-09 10:28:50 +02004255 /* UFO sets gso_size to the size of the fragmentation
4256 * payload, i.e. the size of the L4 (UDP) header is already
4257 * accounted for.
4258 */
Florian Westphalf993bc22014-10-20 13:49:18 +02004259 return thlen + shinfo->gso_size;
Florian Westphalde960aa2014-01-26 10:58:16 +01004260}
4261EXPORT_SYMBOL_GPL(skb_gso_transport_seglen);
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04004262
4263static struct sk_buff *skb_reorder_vlan_header(struct sk_buff *skb)
4264{
4265 if (skb_cow(skb, skb_headroom(skb)) < 0) {
4266 kfree_skb(skb);
4267 return NULL;
4268 }
4269
4270 memmove(skb->data - ETH_HLEN, skb->data - VLAN_ETH_HLEN, 2 * ETH_ALEN);
4271 skb->mac_header += VLAN_HLEN;
4272 return skb;
4273}
4274
4275struct sk_buff *skb_vlan_untag(struct sk_buff *skb)
4276{
4277 struct vlan_hdr *vhdr;
4278 u16 vlan_tci;
4279
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01004280 if (unlikely(skb_vlan_tag_present(skb))) {
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04004281 /* vlan_tci is already set-up so leave this for another time */
4282 return skb;
4283 }
4284
4285 skb = skb_share_check(skb, GFP_ATOMIC);
4286 if (unlikely(!skb))
4287 goto err_free;
4288
4289 if (unlikely(!pskb_may_pull(skb, VLAN_HLEN)))
4290 goto err_free;
4291
4292 vhdr = (struct vlan_hdr *)skb->data;
4293 vlan_tci = ntohs(vhdr->h_vlan_TCI);
4294 __vlan_hwaccel_put_tag(skb, skb->protocol, vlan_tci);
4295
4296 skb_pull_rcsum(skb, VLAN_HLEN);
4297 vlan_set_encap_proto(skb, vhdr);
4298
4299 skb = skb_reorder_vlan_header(skb);
4300 if (unlikely(!skb))
4301 goto err_free;
4302
4303 skb_reset_network_header(skb);
4304 skb_reset_transport_header(skb);
4305 skb_reset_mac_len(skb);
4306
4307 return skb;
4308
4309err_free:
4310 kfree_skb(skb);
4311 return NULL;
4312}
4313EXPORT_SYMBOL(skb_vlan_untag);
Eric Dumazet2e4e4412014-09-17 04:49:49 -07004314
Jiri Pirkoe2195122014-11-19 14:05:01 +01004315int skb_ensure_writable(struct sk_buff *skb, int write_len)
4316{
4317 if (!pskb_may_pull(skb, write_len))
4318 return -ENOMEM;
4319
4320 if (!skb_cloned(skb) || skb_clone_writable(skb, write_len))
4321 return 0;
4322
4323 return pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
4324}
4325EXPORT_SYMBOL(skb_ensure_writable);
4326
Jiri Pirko93515d52014-11-19 14:05:02 +01004327/* remove VLAN header from packet and update csum accordingly. */
4328static int __skb_vlan_pop(struct sk_buff *skb, u16 *vlan_tci)
4329{
4330 struct vlan_hdr *vhdr;
4331 unsigned int offset = skb->data - skb_mac_header(skb);
4332 int err;
4333
4334 __skb_push(skb, offset);
4335 err = skb_ensure_writable(skb, VLAN_ETH_HLEN);
4336 if (unlikely(err))
4337 goto pull;
4338
4339 skb_postpull_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
4340
4341 vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN);
4342 *vlan_tci = ntohs(vhdr->h_vlan_TCI);
4343
4344 memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN);
4345 __skb_pull(skb, VLAN_HLEN);
4346
4347 vlan_set_encap_proto(skb, vhdr);
4348 skb->mac_header += VLAN_HLEN;
4349
4350 if (skb_network_offset(skb) < ETH_HLEN)
4351 skb_set_network_header(skb, ETH_HLEN);
4352
4353 skb_reset_mac_len(skb);
4354pull:
4355 __skb_pull(skb, offset);
4356
4357 return err;
4358}
4359
4360int skb_vlan_pop(struct sk_buff *skb)
4361{
4362 u16 vlan_tci;
4363 __be16 vlan_proto;
4364 int err;
4365
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01004366 if (likely(skb_vlan_tag_present(skb))) {
Jiri Pirko93515d52014-11-19 14:05:02 +01004367 skb->vlan_tci = 0;
4368 } else {
4369 if (unlikely((skb->protocol != htons(ETH_P_8021Q) &&
4370 skb->protocol != htons(ETH_P_8021AD)) ||
4371 skb->len < VLAN_ETH_HLEN))
4372 return 0;
4373
4374 err = __skb_vlan_pop(skb, &vlan_tci);
4375 if (err)
4376 return err;
4377 }
4378 /* move next vlan tag to hw accel tag */
4379 if (likely((skb->protocol != htons(ETH_P_8021Q) &&
4380 skb->protocol != htons(ETH_P_8021AD)) ||
4381 skb->len < VLAN_ETH_HLEN))
4382 return 0;
4383
4384 vlan_proto = skb->protocol;
4385 err = __skb_vlan_pop(skb, &vlan_tci);
4386 if (unlikely(err))
4387 return err;
4388
4389 __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
4390 return 0;
4391}
4392EXPORT_SYMBOL(skb_vlan_pop);
4393
4394int skb_vlan_push(struct sk_buff *skb, __be16 vlan_proto, u16 vlan_tci)
4395{
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01004396 if (skb_vlan_tag_present(skb)) {
Jiri Pirko93515d52014-11-19 14:05:02 +01004397 unsigned int offset = skb->data - skb_mac_header(skb);
4398 int err;
4399
4400 /* __vlan_insert_tag expect skb->data pointing to mac header.
4401 * So change skb->data before calling it and change back to
4402 * original position later
4403 */
4404 __skb_push(skb, offset);
4405 err = __vlan_insert_tag(skb, skb->vlan_proto,
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01004406 skb_vlan_tag_get(skb));
Jiri Pirko93515d52014-11-19 14:05:02 +01004407 if (err)
4408 return err;
4409 skb->protocol = skb->vlan_proto;
4410 skb->mac_len += VLAN_HLEN;
4411 __skb_pull(skb, offset);
4412
4413 if (skb->ip_summed == CHECKSUM_COMPLETE)
4414 skb->csum = csum_add(skb->csum, csum_partial(skb->data
4415 + (2 * ETH_ALEN), VLAN_HLEN, 0));
4416 }
4417 __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
4418 return 0;
4419}
4420EXPORT_SYMBOL(skb_vlan_push);
4421
Eric Dumazet2e4e4412014-09-17 04:49:49 -07004422/**
4423 * alloc_skb_with_frags - allocate skb with page frags
4424 *
Masanari Iidade3f0d02014-10-09 12:58:08 +09004425 * @header_len: size of linear part
4426 * @data_len: needed length in frags
4427 * @max_page_order: max page order desired.
4428 * @errcode: pointer to error code if any
4429 * @gfp_mask: allocation mask
Eric Dumazet2e4e4412014-09-17 04:49:49 -07004430 *
4431 * This can be used to allocate a paged skb, given a maximal order for frags.
4432 */
4433struct sk_buff *alloc_skb_with_frags(unsigned long header_len,
4434 unsigned long data_len,
4435 int max_page_order,
4436 int *errcode,
4437 gfp_t gfp_mask)
4438{
4439 int npages = (data_len + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
4440 unsigned long chunk;
4441 struct sk_buff *skb;
4442 struct page *page;
4443 gfp_t gfp_head;
4444 int i;
4445
4446 *errcode = -EMSGSIZE;
4447 /* Note this test could be relaxed, if we succeed to allocate
4448 * high order pages...
4449 */
4450 if (npages > MAX_SKB_FRAGS)
4451 return NULL;
4452
4453 gfp_head = gfp_mask;
4454 if (gfp_head & __GFP_WAIT)
4455 gfp_head |= __GFP_REPEAT;
4456
4457 *errcode = -ENOBUFS;
4458 skb = alloc_skb(header_len, gfp_head);
4459 if (!skb)
4460 return NULL;
4461
4462 skb->truesize += npages << PAGE_SHIFT;
4463
4464 for (i = 0; npages > 0; i++) {
4465 int order = max_page_order;
4466
4467 while (order) {
4468 if (npages >= 1 << order) {
4469 page = alloc_pages(gfp_mask |
4470 __GFP_COMP |
4471 __GFP_NOWARN |
4472 __GFP_NORETRY,
4473 order);
4474 if (page)
4475 goto fill_page;
4476 /* Do not retry other high order allocations */
4477 order = 1;
4478 max_page_order = 0;
4479 }
4480 order--;
4481 }
4482 page = alloc_page(gfp_mask);
4483 if (!page)
4484 goto failure;
4485fill_page:
4486 chunk = min_t(unsigned long, data_len,
4487 PAGE_SIZE << order);
4488 skb_fill_page_desc(skb, i, page, 0, chunk);
4489 data_len -= chunk;
4490 npages -= 1 << order;
4491 }
4492 return skb;
4493
4494failure:
4495 kfree_skb(skb);
4496 return NULL;
4497}
4498EXPORT_SYMBOL(alloc_skb_with_frags);