Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Routines having to do with the 'struct sk_buff' memory handlers. |
| 3 | * |
Alan Cox | 113aa83 | 2008-10-13 19:01:08 -0700 | [diff] [blame] | 4 | * Authors: Alan Cox <alan@lxorguk.ukuu.org.uk> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * Florian La Roche <rzsfl@rz.uni-sb.de> |
| 6 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * 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 Perches | e005d19 | 2012-05-16 19:58:40 +0000 | [diff] [blame] | 39 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 40 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | #include <linux/module.h> |
| 42 | #include <linux/types.h> |
| 43 | #include <linux/kernel.h> |
Vegard Nossum | fe55f6d | 2008-08-30 12:16:35 +0200 | [diff] [blame] | 44 | #include <linux/kmemcheck.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | #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 Westphal | de960aa | 2014-01-26 10:58:16 +0100 | [diff] [blame] | 50 | #include <linux/tcp.h> |
| 51 | #include <linux/udp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | #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 Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 58 | #include <linux/splice.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | #include <linux/cache.h> |
| 60 | #include <linux/rtnetlink.h> |
| 61 | #include <linux/init.h> |
David Howells | 716ea3a | 2007-04-02 20:19:53 -0700 | [diff] [blame] | 62 | #include <linux/scatterlist.h> |
Patrick Ohly | ac45f60 | 2009-02-12 05:03:37 +0000 | [diff] [blame] | 63 | #include <linux/errqueue.h> |
Linus Torvalds | 268bb0c | 2011-05-20 12:50:29 -0700 | [diff] [blame] | 64 | #include <linux/prefetch.h> |
Vlad Yasevich | 0d5501c | 2014-08-08 14:42:13 -0400 | [diff] [blame] | 65 | #include <linux/if_vlan.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | |
| 67 | #include <net/protocol.h> |
| 68 | #include <net/dst.h> |
| 69 | #include <net/sock.h> |
| 70 | #include <net/checksum.h> |
Paul Durrant | ed1f50c | 2014-01-09 10:02:46 +0000 | [diff] [blame] | 71 | #include <net/ip6_checksum.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | #include <net/xfrm.h> |
| 73 | |
| 74 | #include <asm/uaccess.h> |
Steven Rostedt | ad8d75f | 2009-04-14 19:39:12 -0400 | [diff] [blame] | 75 | #include <trace/events/skb.h> |
Eric Dumazet | 51c56b0 | 2012-04-05 11:35:15 +0200 | [diff] [blame] | 76 | #include <linux/highmem.h> |
Willem de Bruijn | b245be1 | 2015-01-30 13:29:32 -0500 | [diff] [blame] | 77 | #include <linux/capability.h> |
| 78 | #include <linux/user_namespace.h> |
Al Viro | a1f8e7f7 | 2006-10-19 16:08:53 -0400 | [diff] [blame] | 79 | |
Eric Dumazet | d7e8883 | 2012-04-30 08:10:34 +0000 | [diff] [blame] | 80 | struct kmem_cache *skbuff_head_cache __read_mostly; |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 81 | static struct kmem_cache *skbuff_fclone_cache __read_mostly; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | /** |
Jean Sacren | f05de73 | 2013-02-11 13:30:38 +0000 | [diff] [blame] | 84 | * skb_panic - private function for out-of-line support |
| 85 | * @skb: buffer |
| 86 | * @sz: size |
| 87 | * @addr: address |
James Hogan | 99d5851 | 2013-02-13 11:20:27 +0000 | [diff] [blame] | 88 | * @msg: skb_over_panic or skb_under_panic |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | * |
Jean Sacren | f05de73 | 2013-02-11 13:30:38 +0000 | [diff] [blame] | 90 | * 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | */ |
Jean Sacren | f05de73 | 2013-02-11 13:30:38 +0000 | [diff] [blame] | 95 | static void skb_panic(struct sk_buff *skb, unsigned int sz, void *addr, |
James Hogan | 99d5851 | 2013-02-13 11:20:27 +0000 | [diff] [blame] | 96 | const char msg[]) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | { |
Joe Perches | e005d19 | 2012-05-16 19:58:40 +0000 | [diff] [blame] | 98 | pr_emerg("%s: text:%p len:%d put:%d head:%p data:%p tail:%#lx end:%#lx dev:%s\n", |
James Hogan | 99d5851 | 2013-02-13 11:20:27 +0000 | [diff] [blame] | 99 | msg, addr, skb->len, sz, skb->head, skb->data, |
Joe Perches | e005d19 | 2012-05-16 19:58:40 +0000 | [diff] [blame] | 100 | (unsigned long)skb->tail, (unsigned long)skb->end, |
| 101 | skb->dev ? skb->dev->name : "<NULL>"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | BUG(); |
| 103 | } |
| 104 | |
Jean Sacren | f05de73 | 2013-02-11 13:30:38 +0000 | [diff] [blame] | 105 | static void skb_over_panic(struct sk_buff *skb, unsigned int sz, void *addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | { |
Jean Sacren | f05de73 | 2013-02-11 13:30:38 +0000 | [diff] [blame] | 107 | skb_panic(skb, sz, addr, __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Jean Sacren | f05de73 | 2013-02-11 13:30:38 +0000 | [diff] [blame] | 110 | static void skb_under_panic(struct sk_buff *skb, unsigned int sz, void *addr) |
| 111 | { |
| 112 | skb_panic(skb, sz, addr, __func__); |
| 113 | } |
Mel Gorman | c93bdd0 | 2012-07-31 16:44:19 -0700 | [diff] [blame] | 114 | |
| 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 hemminger | 61c5e88 | 2012-12-28 18:24:28 +0000 | [diff] [blame] | 124 | |
| 125 | static void *__kmalloc_reserve(size_t size, gfp_t flags, int node, |
| 126 | unsigned long ip, bool *pfmemalloc) |
Mel Gorman | c93bdd0 | 2012-07-31 16:44:19 -0700 | [diff] [blame] | 127 | { |
| 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 | |
| 145 | out: |
| 146 | if (pfmemalloc) |
| 147 | *pfmemalloc = ret_pfmemalloc; |
| 148 | |
| 149 | return obj; |
| 150 | } |
| 151 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | /* 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 McHardy | 0ebd0ac | 2013-04-17 06:46:58 +0000 | [diff] [blame] | 158 | struct 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 Neira | 5e71d9d | 2013-06-03 09:28:43 +0000 | [diff] [blame] | 174 | skb->head = NULL; |
Patrick McHardy | 0ebd0ac | 2013-04-17 06:46:58 +0000 | [diff] [blame] | 175 | skb->truesize = sizeof(struct sk_buff); |
| 176 | atomic_set(&skb->users, 1); |
| 177 | |
Cong Wang | 35d0461 | 2013-05-29 15:16:05 +0800 | [diff] [blame] | 178 | skb->mac_header = (typeof(skb->mac_header))~0U; |
Patrick McHardy | 0ebd0ac | 2013-04-17 06:46:58 +0000 | [diff] [blame] | 179 | out: |
| 180 | return skb; |
| 181 | } |
| 182 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | /** |
David S. Miller | d179cd1 | 2005-08-17 14:57:30 -0700 | [diff] [blame] | 184 | * __alloc_skb - allocate a network buffer |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | * @size: size to allocate |
| 186 | * @gfp_mask: allocation mask |
Mel Gorman | c93bdd0 | 2012-07-31 16:44:19 -0700 | [diff] [blame] | 187 | * @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 Hellwig | b30973f | 2006-12-06 20:32:36 -0800 | [diff] [blame] | 191 | * @node: numa node to allocate memory on |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | * |
| 193 | * Allocate a new &sk_buff. The returned buffer has no headroom and a |
Ben Hutchings | 94b6042 | 2012-06-06 15:23:37 +0000 | [diff] [blame] | 194 | * 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | * |
| 197 | * Buffers may only be allocated from interrupts using a @gfp_mask of |
| 198 | * %GFP_ATOMIC. |
| 199 | */ |
Al Viro | dd0fc66 | 2005-10-07 07:46:04 +0100 | [diff] [blame] | 200 | struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask, |
Mel Gorman | c93bdd0 | 2012-07-31 16:44:19 -0700 | [diff] [blame] | 201 | int flags, int node) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | { |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 203 | struct kmem_cache *cache; |
Benjamin LaHaise | 4947d3e | 2006-01-03 14:06:50 -0800 | [diff] [blame] | 204 | struct skb_shared_info *shinfo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | struct sk_buff *skb; |
| 206 | u8 *data; |
Mel Gorman | c93bdd0 | 2012-07-31 16:44:19 -0700 | [diff] [blame] | 207 | bool pfmemalloc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | |
Mel Gorman | c93bdd0 | 2012-07-31 16:44:19 -0700 | [diff] [blame] | 209 | 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 Xu | 8798b3f | 2006-01-23 16:32:45 -0800 | [diff] [blame] | 214 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | /* Get the HEAD */ |
Christoph Hellwig | b30973f | 2006-12-06 20:32:36 -0800 | [diff] [blame] | 216 | skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | if (!skb) |
| 218 | goto out; |
Eric Dumazet | ec7d2f2 | 2010-05-05 01:07:37 -0700 | [diff] [blame] | 219 | prefetchw(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | |
Eric Dumazet | 87fb4b7 | 2011-10-13 07:28:54 +0000 | [diff] [blame] | 221 | /* 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 Lindgren | bc417e3 | 2011-11-02 13:40:28 +0000 | [diff] [blame] | 226 | size = SKB_DATA_ALIGN(size); |
Eric Dumazet | 87fb4b7 | 2011-10-13 07:28:54 +0000 | [diff] [blame] | 227 | size += SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); |
Mel Gorman | c93bdd0 | 2012-07-31 16:44:19 -0700 | [diff] [blame] | 228 | data = kmalloc_reserve(size, gfp_mask, node, &pfmemalloc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | if (!data) |
| 230 | goto nodata; |
Eric Dumazet | 87fb4b7 | 2011-10-13 07:28:54 +0000 | [diff] [blame] | 231 | /* 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 Dumazet | ec7d2f2 | 2010-05-05 01:07:37 -0700 | [diff] [blame] | 236 | prefetchw(data + size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | |
Arnaldo Carvalho de Melo | ca0605a | 2007-03-19 10:48:59 -0300 | [diff] [blame] | 238 | /* |
Johannes Berg | c800578 | 2008-05-03 20:56:42 -0700 | [diff] [blame] | 239 | * 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 Melo | ca0605a | 2007-03-19 10:48:59 -0300 | [diff] [blame] | 242 | */ |
| 243 | memset(skb, 0, offsetof(struct sk_buff, tail)); |
Eric Dumazet | 87fb4b7 | 2011-10-13 07:28:54 +0000 | [diff] [blame] | 244 | /* Account for allocated memory : skb + skb->head */ |
| 245 | skb->truesize = SKB_TRUESIZE(size); |
Mel Gorman | c93bdd0 | 2012-07-31 16:44:19 -0700 | [diff] [blame] | 246 | skb->pfmemalloc = pfmemalloc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | atomic_set(&skb->users, 1); |
| 248 | skb->head = data; |
| 249 | skb->data = data; |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 250 | skb_reset_tail_pointer(skb); |
Arnaldo Carvalho de Melo | 4305b54 | 2007-04-19 20:43:29 -0700 | [diff] [blame] | 251 | skb->end = skb->tail + size; |
Cong Wang | 35d0461 | 2013-05-29 15:16:05 +0800 | [diff] [blame] | 252 | skb->mac_header = (typeof(skb->mac_header))~0U; |
| 253 | skb->transport_header = (typeof(skb->transport_header))~0U; |
Stephen Hemminger | 19633e1 | 2009-06-17 05:23:27 +0000 | [diff] [blame] | 254 | |
Benjamin LaHaise | 4947d3e | 2006-01-03 14:06:50 -0800 | [diff] [blame] | 255 | /* make sure we initialize shinfo sequentially */ |
| 256 | shinfo = skb_shinfo(skb); |
Eric Dumazet | ec7d2f2 | 2010-05-05 01:07:37 -0700 | [diff] [blame] | 257 | memset(shinfo, 0, offsetof(struct skb_shared_info, dataref)); |
Benjamin LaHaise | 4947d3e | 2006-01-03 14:06:50 -0800 | [diff] [blame] | 258 | atomic_set(&shinfo->dataref, 1); |
Eric Dumazet | c2aa366 | 2011-01-25 23:18:38 +0000 | [diff] [blame] | 259 | kmemcheck_annotate_variable(shinfo->destructor_arg); |
Benjamin LaHaise | 4947d3e | 2006-01-03 14:06:50 -0800 | [diff] [blame] | 260 | |
Mel Gorman | c93bdd0 | 2012-07-31 16:44:19 -0700 | [diff] [blame] | 261 | if (flags & SKB_ALLOC_FCLONE) { |
Eric Dumazet | d0bf4a9 | 2014-09-29 13:29:15 -0700 | [diff] [blame] | 262 | struct sk_buff_fclones *fclones; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | |
Eric Dumazet | d0bf4a9 | 2014-09-29 13:29:15 -0700 | [diff] [blame] | 264 | fclones = container_of(skb, struct sk_buff_fclones, skb1); |
| 265 | |
| 266 | kmemcheck_annotate_bitfield(&fclones->skb2, flags1); |
David S. Miller | d179cd1 | 2005-08-17 14:57:30 -0700 | [diff] [blame] | 267 | skb->fclone = SKB_FCLONE_ORIG; |
Eric Dumazet | d0bf4a9 | 2014-09-29 13:29:15 -0700 | [diff] [blame] | 268 | atomic_set(&fclones->fclone_ref, 1); |
David S. Miller | d179cd1 | 2005-08-17 14:57:30 -0700 | [diff] [blame] | 269 | |
Eric Dumazet | 6ffe75eb | 2014-12-03 17:04:39 -0800 | [diff] [blame] | 270 | fclones->skb2.fclone = SKB_FCLONE_CLONE; |
Eric Dumazet | d0bf4a9 | 2014-09-29 13:29:15 -0700 | [diff] [blame] | 271 | fclones->skb2.pfmemalloc = pfmemalloc; |
David S. Miller | d179cd1 | 2005-08-17 14:57:30 -0700 | [diff] [blame] | 272 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | out: |
| 274 | return skb; |
| 275 | nodata: |
Herbert Xu | 8798b3f | 2006-01-23 16:32:45 -0800 | [diff] [blame] | 276 | kmem_cache_free(cache, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | skb = NULL; |
| 278 | goto out; |
| 279 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 280 | EXPORT_SYMBOL(__alloc_skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | |
| 282 | /** |
Eric Dumazet | b2b5ce9 | 2011-11-14 06:03:34 +0000 | [diff] [blame] | 283 | * build_skb - build a network buffer |
| 284 | * @data: data buffer provided by caller |
Eric Dumazet | d3836f2 | 2012-04-27 00:33:38 +0000 | [diff] [blame] | 285 | * @frag_size: size of fragment, or 0 if head was kmalloced |
Eric Dumazet | b2b5ce9 | 2011-11-14 06:03:34 +0000 | [diff] [blame] | 286 | * |
| 287 | * Allocate a new &sk_buff. Caller provides space holding head and |
Florian Fainelli | deceb4c | 2013-07-23 20:22:39 +0100 | [diff] [blame] | 288 | * skb_shared_info. @data must have been allocated by kmalloc() only if |
| 289 | * @frag_size is 0, otherwise data should come from the page allocator. |
Eric Dumazet | b2b5ce9 | 2011-11-14 06:03:34 +0000 | [diff] [blame] | 290 | * The return is the new skb buffer. |
| 291 | * On a failure the return is %NULL, and @data is not freed. |
| 292 | * Notes : |
| 293 | * Before IO, driver allocates only data buffer where NIC put incoming frame |
| 294 | * Driver should add room at head (NET_SKB_PAD) and |
| 295 | * MUST add room at tail (SKB_DATA_ALIGN(skb_shared_info)) |
| 296 | * After IO, driver calls build_skb(), to allocate sk_buff and populate it |
| 297 | * before giving packet to stack. |
| 298 | * RX rings only contains data buffers, not full skbs. |
| 299 | */ |
Eric Dumazet | d3836f2 | 2012-04-27 00:33:38 +0000 | [diff] [blame] | 300 | struct sk_buff *build_skb(void *data, unsigned int frag_size) |
Eric Dumazet | b2b5ce9 | 2011-11-14 06:03:34 +0000 | [diff] [blame] | 301 | { |
| 302 | struct skb_shared_info *shinfo; |
| 303 | struct sk_buff *skb; |
Eric Dumazet | d3836f2 | 2012-04-27 00:33:38 +0000 | [diff] [blame] | 304 | unsigned int size = frag_size ? : ksize(data); |
Eric Dumazet | b2b5ce9 | 2011-11-14 06:03:34 +0000 | [diff] [blame] | 305 | |
| 306 | skb = kmem_cache_alloc(skbuff_head_cache, GFP_ATOMIC); |
| 307 | if (!skb) |
| 308 | return NULL; |
| 309 | |
Eric Dumazet | d3836f2 | 2012-04-27 00:33:38 +0000 | [diff] [blame] | 310 | size -= SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); |
Eric Dumazet | b2b5ce9 | 2011-11-14 06:03:34 +0000 | [diff] [blame] | 311 | |
| 312 | memset(skb, 0, offsetof(struct sk_buff, tail)); |
| 313 | skb->truesize = SKB_TRUESIZE(size); |
Eric Dumazet | d3836f2 | 2012-04-27 00:33:38 +0000 | [diff] [blame] | 314 | skb->head_frag = frag_size != 0; |
Eric Dumazet | b2b5ce9 | 2011-11-14 06:03:34 +0000 | [diff] [blame] | 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 Wang | 35d0461 | 2013-05-29 15:16:05 +0800 | [diff] [blame] | 320 | skb->mac_header = (typeof(skb->mac_header))~0U; |
| 321 | skb->transport_header = (typeof(skb->transport_header))~0U; |
Eric Dumazet | b2b5ce9 | 2011-11-14 06:03:34 +0000 | [diff] [blame] | 322 | |
| 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 | } |
| 331 | EXPORT_SYMBOL(build_skb); |
| 332 | |
Eric Dumazet | a1c7fff | 2012-05-17 07:34:16 +0000 | [diff] [blame] | 333 | struct netdev_alloc_cache { |
Eric Dumazet | 69b08f6 | 2012-09-26 06:46:57 +0000 | [diff] [blame] | 334 | struct page_frag frag; |
| 335 | /* we maintain a pagecount bias, so that we dont dirty cache line |
| 336 | * containing page->_count every time we allocate a fragment. |
| 337 | */ |
| 338 | unsigned int pagecnt_bias; |
Eric Dumazet | a1c7fff | 2012-05-17 07:34:16 +0000 | [diff] [blame] | 339 | }; |
| 340 | static DEFINE_PER_CPU(struct netdev_alloc_cache, netdev_alloc_cache); |
Alexander Duyck | ffde732 | 2014-12-09 19:40:42 -0800 | [diff] [blame] | 341 | static DEFINE_PER_CPU(struct netdev_alloc_cache, napi_alloc_cache); |
Eric Dumazet | a1c7fff | 2012-05-17 07:34:16 +0000 | [diff] [blame] | 342 | |
Alexander Duyck | ffde732 | 2014-12-09 19:40:42 -0800 | [diff] [blame] | 343 | static struct page *__page_frag_refill(struct netdev_alloc_cache *nc, |
| 344 | gfp_t gfp_mask) |
Eric Dumazet | 6f53261 | 2012-05-18 05:12:12 +0000 | [diff] [blame] | 345 | { |
Alexander Duyck | ffde732 | 2014-12-09 19:40:42 -0800 | [diff] [blame] | 346 | const unsigned int order = NETDEV_FRAG_PAGE_MAX_ORDER; |
| 347 | struct page *page = NULL; |
| 348 | gfp_t gfp = gfp_mask; |
Eric Dumazet | 6f53261 | 2012-05-18 05:12:12 +0000 | [diff] [blame] | 349 | |
Alexander Duyck | ffde732 | 2014-12-09 19:40:42 -0800 | [diff] [blame] | 350 | if (order) { |
| 351 | gfp_mask |= __GFP_COMP | __GFP_NOWARN | __GFP_NORETRY; |
| 352 | page = alloc_pages_node(NUMA_NO_NODE, gfp_mask, order); |
| 353 | nc->frag.size = PAGE_SIZE << (page ? order : 0); |
| 354 | } |
| 355 | |
| 356 | if (unlikely(!page)) |
| 357 | page = alloc_pages_node(NUMA_NO_NODE, gfp, 0); |
| 358 | |
| 359 | nc->frag.page = page; |
| 360 | |
| 361 | return page; |
| 362 | } |
| 363 | |
| 364 | static void *__alloc_page_frag(struct netdev_alloc_cache __percpu *cache, |
| 365 | unsigned int fragsz, gfp_t gfp_mask) |
| 366 | { |
| 367 | struct netdev_alloc_cache *nc = this_cpu_ptr(cache); |
| 368 | struct page *page = nc->frag.page; |
| 369 | unsigned int size; |
| 370 | int offset; |
| 371 | |
| 372 | if (unlikely(!page)) { |
Eric Dumazet | 6f53261 | 2012-05-18 05:12:12 +0000 | [diff] [blame] | 373 | refill: |
Alexander Duyck | ffde732 | 2014-12-09 19:40:42 -0800 | [diff] [blame] | 374 | page = __page_frag_refill(nc, gfp_mask); |
| 375 | if (!page) |
| 376 | return NULL; |
Eric Dumazet | 69b08f6 | 2012-09-26 06:46:57 +0000 | [diff] [blame] | 377 | |
Alexander Duyck | ffde732 | 2014-12-09 19:40:42 -0800 | [diff] [blame] | 378 | /* if size can vary use frag.size else just use PAGE_SIZE */ |
| 379 | size = NETDEV_FRAG_PAGE_MAX_ORDER ? nc->frag.size : PAGE_SIZE; |
| 380 | |
Eric Dumazet | 4c45058 | 2014-10-10 04:48:18 -0700 | [diff] [blame] | 381 | /* Even if we own the page, we do not use atomic_set(). |
| 382 | * This would break get_page_unless_zero() users. |
| 383 | */ |
Alexander Duyck | ffde732 | 2014-12-09 19:40:42 -0800 | [diff] [blame] | 384 | atomic_add(size - 1, &page->_count); |
| 385 | |
| 386 | /* reset page count bias and offset to start of new frag */ |
| 387 | nc->pagecnt_bias = size; |
| 388 | nc->frag.offset = size; |
Eric Dumazet | 6f53261 | 2012-05-18 05:12:12 +0000 | [diff] [blame] | 389 | } |
Alexander Duyck | 540eb7b | 2012-07-12 14:23:50 +0000 | [diff] [blame] | 390 | |
Alexander Duyck | ffde732 | 2014-12-09 19:40:42 -0800 | [diff] [blame] | 391 | offset = nc->frag.offset - fragsz; |
| 392 | if (unlikely(offset < 0)) { |
| 393 | if (!atomic_sub_and_test(nc->pagecnt_bias, &page->_count)) |
| 394 | goto refill; |
| 395 | |
| 396 | /* if size can vary use frag.size else just use PAGE_SIZE */ |
| 397 | size = NETDEV_FRAG_PAGE_MAX_ORDER ? nc->frag.size : PAGE_SIZE; |
| 398 | |
| 399 | /* OK, page count is 0, we can safely set it */ |
| 400 | atomic_set(&page->_count, size); |
| 401 | |
| 402 | /* reset page count bias and offset to start of new frag */ |
| 403 | nc->pagecnt_bias = size; |
| 404 | offset = size - fragsz; |
Eric Dumazet | 6f53261 | 2012-05-18 05:12:12 +0000 | [diff] [blame] | 405 | } |
Alexander Duyck | 540eb7b | 2012-07-12 14:23:50 +0000 | [diff] [blame] | 406 | |
Alexander Duyck | 540eb7b | 2012-07-12 14:23:50 +0000 | [diff] [blame] | 407 | nc->pagecnt_bias--; |
Alexander Duyck | ffde732 | 2014-12-09 19:40:42 -0800 | [diff] [blame] | 408 | nc->frag.offset = offset; |
| 409 | |
| 410 | return page_address(page) + offset; |
| 411 | } |
| 412 | |
| 413 | static void *__netdev_alloc_frag(unsigned int fragsz, gfp_t gfp_mask) |
| 414 | { |
| 415 | unsigned long flags; |
| 416 | void *data; |
| 417 | |
| 418 | local_irq_save(flags); |
| 419 | data = __alloc_page_frag(&netdev_alloc_cache, fragsz, gfp_mask); |
Eric Dumazet | 6f53261 | 2012-05-18 05:12:12 +0000 | [diff] [blame] | 420 | local_irq_restore(flags); |
| 421 | return data; |
| 422 | } |
Mel Gorman | c93bdd0 | 2012-07-31 16:44:19 -0700 | [diff] [blame] | 423 | |
| 424 | /** |
| 425 | * netdev_alloc_frag - allocate a page fragment |
| 426 | * @fragsz: fragment size |
| 427 | * |
| 428 | * Allocates a frag from a page for receive buffer. |
| 429 | * Uses GFP_ATOMIC allocations. |
| 430 | */ |
| 431 | void *netdev_alloc_frag(unsigned int fragsz) |
| 432 | { |
| 433 | return __netdev_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD); |
| 434 | } |
Eric Dumazet | 6f53261 | 2012-05-18 05:12:12 +0000 | [diff] [blame] | 435 | EXPORT_SYMBOL(netdev_alloc_frag); |
| 436 | |
Alexander Duyck | ffde732 | 2014-12-09 19:40:42 -0800 | [diff] [blame] | 437 | static void *__napi_alloc_frag(unsigned int fragsz, gfp_t gfp_mask) |
| 438 | { |
| 439 | return __alloc_page_frag(&napi_alloc_cache, fragsz, gfp_mask); |
| 440 | } |
| 441 | |
| 442 | void *napi_alloc_frag(unsigned int fragsz) |
| 443 | { |
| 444 | return __napi_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD); |
| 445 | } |
| 446 | EXPORT_SYMBOL(napi_alloc_frag); |
| 447 | |
Eric Dumazet | 6f53261 | 2012-05-18 05:12:12 +0000 | [diff] [blame] | 448 | /** |
Alexander Duyck | fd11a83 | 2014-12-09 19:40:49 -0800 | [diff] [blame] | 449 | * __alloc_rx_skb - allocate an skbuff for rx |
Christoph Hellwig | 8af2745 | 2006-07-31 22:35:23 -0700 | [diff] [blame] | 450 | * @length: length to allocate |
| 451 | * @gfp_mask: get_free_pages mask, passed to alloc_skb |
Alexander Duyck | fd11a83 | 2014-12-09 19:40:49 -0800 | [diff] [blame] | 452 | * @flags: If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for |
| 453 | * allocations in case we have to fallback to __alloc_skb() |
| 454 | * If SKB_ALLOC_NAPI is set, page fragment will be allocated |
| 455 | * from napi_cache instead of netdev_cache. |
Christoph Hellwig | 8af2745 | 2006-07-31 22:35:23 -0700 | [diff] [blame] | 456 | * |
| 457 | * Allocate a new &sk_buff and assign it a usage count of one. The |
| 458 | * buffer has unspecified headroom built in. Users should allocate |
| 459 | * the headroom they think they need without accounting for the |
| 460 | * built in space. The built in space is used for optimisations. |
| 461 | * |
| 462 | * %NULL is returned if there is no free memory. |
| 463 | */ |
Alexander Duyck | fd11a83 | 2014-12-09 19:40:49 -0800 | [diff] [blame] | 464 | static struct sk_buff *__alloc_rx_skb(unsigned int length, gfp_t gfp_mask, |
| 465 | int flags) |
Christoph Hellwig | 8af2745 | 2006-07-31 22:35:23 -0700 | [diff] [blame] | 466 | { |
Eric Dumazet | 6f53261 | 2012-05-18 05:12:12 +0000 | [diff] [blame] | 467 | struct sk_buff *skb = NULL; |
Alexander Duyck | fd11a83 | 2014-12-09 19:40:49 -0800 | [diff] [blame] | 468 | unsigned int fragsz = SKB_DATA_ALIGN(length) + |
Eric Dumazet | a1c7fff | 2012-05-17 07:34:16 +0000 | [diff] [blame] | 469 | SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); |
Christoph Hellwig | 8af2745 | 2006-07-31 22:35:23 -0700 | [diff] [blame] | 470 | |
Eric Dumazet | 310e158 | 2012-07-16 13:15:52 +0200 | [diff] [blame] | 471 | if (fragsz <= PAGE_SIZE && !(gfp_mask & (__GFP_WAIT | GFP_DMA))) { |
Mel Gorman | c93bdd0 | 2012-07-31 16:44:19 -0700 | [diff] [blame] | 472 | void *data; |
| 473 | |
| 474 | if (sk_memalloc_socks()) |
| 475 | gfp_mask |= __GFP_MEMALLOC; |
| 476 | |
Alexander Duyck | fd11a83 | 2014-12-09 19:40:49 -0800 | [diff] [blame] | 477 | data = (flags & SKB_ALLOC_NAPI) ? |
| 478 | __napi_alloc_frag(fragsz, gfp_mask) : |
| 479 | __netdev_alloc_frag(fragsz, gfp_mask); |
Eric Dumazet | a1c7fff | 2012-05-17 07:34:16 +0000 | [diff] [blame] | 480 | |
Eric Dumazet | 6f53261 | 2012-05-18 05:12:12 +0000 | [diff] [blame] | 481 | if (likely(data)) { |
| 482 | skb = build_skb(data, fragsz); |
| 483 | if (unlikely(!skb)) |
| 484 | put_page(virt_to_head_page(data)); |
Eric Dumazet | a1c7fff | 2012-05-17 07:34:16 +0000 | [diff] [blame] | 485 | } |
Eric Dumazet | a1c7fff | 2012-05-17 07:34:16 +0000 | [diff] [blame] | 486 | } else { |
Alexander Duyck | fd11a83 | 2014-12-09 19:40:49 -0800 | [diff] [blame] | 487 | skb = __alloc_skb(length, gfp_mask, |
Mel Gorman | c93bdd0 | 2012-07-31 16:44:19 -0700 | [diff] [blame] | 488 | SKB_ALLOC_RX, NUMA_NO_NODE); |
Eric Dumazet | a1c7fff | 2012-05-17 07:34:16 +0000 | [diff] [blame] | 489 | } |
Alexander Duyck | fd11a83 | 2014-12-09 19:40:49 -0800 | [diff] [blame] | 490 | return skb; |
| 491 | } |
| 492 | |
| 493 | /** |
| 494 | * __netdev_alloc_skb - allocate an skbuff for rx on a specific device |
| 495 | * @dev: network device to receive on |
| 496 | * @length: length to allocate |
| 497 | * @gfp_mask: get_free_pages mask, passed to alloc_skb |
| 498 | * |
| 499 | * Allocate a new &sk_buff and assign it a usage count of one. The |
| 500 | * buffer has NET_SKB_PAD headroom built in. Users should allocate |
| 501 | * the headroom they think they need without accounting for the |
| 502 | * built in space. The built in space is used for optimisations. |
| 503 | * |
| 504 | * %NULL is returned if there is no free memory. |
| 505 | */ |
| 506 | struct sk_buff *__netdev_alloc_skb(struct net_device *dev, |
| 507 | unsigned int length, gfp_t gfp_mask) |
| 508 | { |
| 509 | struct sk_buff *skb; |
| 510 | |
| 511 | length += NET_SKB_PAD; |
| 512 | skb = __alloc_rx_skb(length, gfp_mask, 0); |
| 513 | |
Christoph Hellwig | 7b2e497 | 2006-08-07 16:09:04 -0700 | [diff] [blame] | 514 | if (likely(skb)) { |
Christoph Hellwig | 8af2745 | 2006-07-31 22:35:23 -0700 | [diff] [blame] | 515 | skb_reserve(skb, NET_SKB_PAD); |
Christoph Hellwig | 7b2e497 | 2006-08-07 16:09:04 -0700 | [diff] [blame] | 516 | skb->dev = dev; |
| 517 | } |
Alexander Duyck | fd11a83 | 2014-12-09 19:40:49 -0800 | [diff] [blame] | 518 | |
Christoph Hellwig | 8af2745 | 2006-07-31 22:35:23 -0700 | [diff] [blame] | 519 | return skb; |
| 520 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 521 | EXPORT_SYMBOL(__netdev_alloc_skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | |
Alexander Duyck | fd11a83 | 2014-12-09 19:40:49 -0800 | [diff] [blame] | 523 | /** |
| 524 | * __napi_alloc_skb - allocate skbuff for rx in a specific NAPI instance |
| 525 | * @napi: napi instance this buffer was allocated for |
| 526 | * @length: length to allocate |
| 527 | * @gfp_mask: get_free_pages mask, passed to alloc_skb and alloc_pages |
| 528 | * |
| 529 | * Allocate a new sk_buff for use in NAPI receive. This buffer will |
| 530 | * attempt to allocate the head from a special reserved region used |
| 531 | * only for NAPI Rx allocation. By doing this we can save several |
| 532 | * CPU cycles by avoiding having to disable and re-enable IRQs. |
| 533 | * |
| 534 | * %NULL is returned if there is no free memory. |
| 535 | */ |
| 536 | struct sk_buff *__napi_alloc_skb(struct napi_struct *napi, |
| 537 | unsigned int length, gfp_t gfp_mask) |
| 538 | { |
| 539 | struct sk_buff *skb; |
| 540 | |
| 541 | length += NET_SKB_PAD + NET_IP_ALIGN; |
| 542 | skb = __alloc_rx_skb(length, gfp_mask, SKB_ALLOC_NAPI); |
| 543 | |
| 544 | if (likely(skb)) { |
| 545 | skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN); |
| 546 | skb->dev = napi->dev; |
| 547 | } |
| 548 | |
| 549 | return skb; |
| 550 | } |
| 551 | EXPORT_SYMBOL(__napi_alloc_skb); |
| 552 | |
Peter Zijlstra | 654bed1 | 2008-10-07 14:22:33 -0700 | [diff] [blame] | 553 | void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off, |
Eric Dumazet | 50269e1 | 2012-03-23 23:59:33 +0000 | [diff] [blame] | 554 | int size, unsigned int truesize) |
Peter Zijlstra | 654bed1 | 2008-10-07 14:22:33 -0700 | [diff] [blame] | 555 | { |
| 556 | skb_fill_page_desc(skb, i, page, off, size); |
| 557 | skb->len += size; |
| 558 | skb->data_len += size; |
Eric Dumazet | 50269e1 | 2012-03-23 23:59:33 +0000 | [diff] [blame] | 559 | skb->truesize += truesize; |
Peter Zijlstra | 654bed1 | 2008-10-07 14:22:33 -0700 | [diff] [blame] | 560 | } |
| 561 | EXPORT_SYMBOL(skb_add_rx_frag); |
| 562 | |
Jason Wang | f8e617e | 2013-11-01 14:07:47 +0800 | [diff] [blame] | 563 | void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size, |
| 564 | unsigned int truesize) |
| 565 | { |
| 566 | skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; |
| 567 | |
| 568 | skb_frag_size_add(frag, size); |
| 569 | skb->len += size; |
| 570 | skb->data_len += size; |
| 571 | skb->truesize += truesize; |
| 572 | } |
| 573 | EXPORT_SYMBOL(skb_coalesce_rx_frag); |
| 574 | |
Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 575 | static void skb_drop_list(struct sk_buff **listp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | { |
Eric Dumazet | bd8a703 | 2013-06-24 06:26:00 -0700 | [diff] [blame] | 577 | kfree_skb_list(*listp); |
Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 578 | *listp = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | } |
| 580 | |
Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 581 | static inline void skb_drop_fraglist(struct sk_buff *skb) |
| 582 | { |
| 583 | skb_drop_list(&skb_shinfo(skb)->frag_list); |
| 584 | } |
| 585 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | static void skb_clone_fraglist(struct sk_buff *skb) |
| 587 | { |
| 588 | struct sk_buff *list; |
| 589 | |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 590 | skb_walk_frags(skb, list) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | skb_get(list); |
| 592 | } |
| 593 | |
Eric Dumazet | d3836f2 | 2012-04-27 00:33:38 +0000 | [diff] [blame] | 594 | static void skb_free_head(struct sk_buff *skb) |
| 595 | { |
| 596 | if (skb->head_frag) |
| 597 | put_page(virt_to_head_page(skb->head)); |
| 598 | else |
| 599 | kfree(skb->head); |
| 600 | } |
| 601 | |
Adrian Bunk | 5bba171 | 2006-06-29 13:02:35 -0700 | [diff] [blame] | 602 | static void skb_release_data(struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | { |
Eric Dumazet | ff04a77 | 2014-09-23 18:39:30 -0700 | [diff] [blame] | 604 | struct skb_shared_info *shinfo = skb_shinfo(skb); |
| 605 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | |
Eric Dumazet | ff04a77 | 2014-09-23 18:39:30 -0700 | [diff] [blame] | 607 | if (skb->cloned && |
| 608 | atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1, |
| 609 | &shinfo->dataref)) |
| 610 | return; |
Shirley Ma | a6686f2 | 2011-07-06 12:22:12 +0000 | [diff] [blame] | 611 | |
Eric Dumazet | ff04a77 | 2014-09-23 18:39:30 -0700 | [diff] [blame] | 612 | for (i = 0; i < shinfo->nr_frags; i++) |
| 613 | __skb_frag_unref(&shinfo->frags[i]); |
Shirley Ma | a6686f2 | 2011-07-06 12:22:12 +0000 | [diff] [blame] | 614 | |
Eric Dumazet | ff04a77 | 2014-09-23 18:39:30 -0700 | [diff] [blame] | 615 | /* |
| 616 | * If skb buf is from userspace, we need to notify the caller |
| 617 | * the lower device DMA has done; |
| 618 | */ |
| 619 | if (shinfo->tx_flags & SKBTX_DEV_ZEROCOPY) { |
| 620 | struct ubuf_info *uarg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | |
Eric Dumazet | ff04a77 | 2014-09-23 18:39:30 -0700 | [diff] [blame] | 622 | uarg = shinfo->destructor_arg; |
| 623 | if (uarg->callback) |
| 624 | uarg->callback(uarg, true); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | } |
Eric Dumazet | ff04a77 | 2014-09-23 18:39:30 -0700 | [diff] [blame] | 626 | |
| 627 | if (shinfo->frag_list) |
| 628 | kfree_skb_list(shinfo->frag_list); |
| 629 | |
| 630 | skb_free_head(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | /* |
| 634 | * Free an skbuff by memory without cleaning the state. |
| 635 | */ |
Herbert Xu | 2d4baff | 2007-11-26 23:11:19 +0800 | [diff] [blame] | 636 | static void kfree_skbmem(struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | { |
Eric Dumazet | d0bf4a9 | 2014-09-29 13:29:15 -0700 | [diff] [blame] | 638 | struct sk_buff_fclones *fclones; |
David S. Miller | d179cd1 | 2005-08-17 14:57:30 -0700 | [diff] [blame] | 639 | |
David S. Miller | d179cd1 | 2005-08-17 14:57:30 -0700 | [diff] [blame] | 640 | switch (skb->fclone) { |
| 641 | case SKB_FCLONE_UNAVAILABLE: |
| 642 | kmem_cache_free(skbuff_head_cache, skb); |
Eric Dumazet | 6ffe75eb | 2014-12-03 17:04:39 -0800 | [diff] [blame] | 643 | return; |
David S. Miller | d179cd1 | 2005-08-17 14:57:30 -0700 | [diff] [blame] | 644 | |
| 645 | case SKB_FCLONE_ORIG: |
Eric Dumazet | d0bf4a9 | 2014-09-29 13:29:15 -0700 | [diff] [blame] | 646 | fclones = container_of(skb, struct sk_buff_fclones, skb1); |
Eric Dumazet | 6ffe75eb | 2014-12-03 17:04:39 -0800 | [diff] [blame] | 647 | |
| 648 | /* We usually free the clone (TX completion) before original skb |
| 649 | * This test would have no chance to be true for the clone, |
| 650 | * while here, branch prediction will be good. |
| 651 | */ |
| 652 | if (atomic_read(&fclones->fclone_ref) == 1) |
| 653 | goto fastpath; |
David S. Miller | d179cd1 | 2005-08-17 14:57:30 -0700 | [diff] [blame] | 654 | break; |
| 655 | |
Eric Dumazet | 6ffe75eb | 2014-12-03 17:04:39 -0800 | [diff] [blame] | 656 | default: /* SKB_FCLONE_CLONE */ |
Eric Dumazet | d0bf4a9 | 2014-09-29 13:29:15 -0700 | [diff] [blame] | 657 | fclones = container_of(skb, struct sk_buff_fclones, skb2); |
David S. Miller | d179cd1 | 2005-08-17 14:57:30 -0700 | [diff] [blame] | 658 | break; |
Stephen Hemminger | 3ff50b7 | 2007-04-20 17:09:22 -0700 | [diff] [blame] | 659 | } |
Eric Dumazet | 6ffe75eb | 2014-12-03 17:04:39 -0800 | [diff] [blame] | 660 | if (!atomic_dec_and_test(&fclones->fclone_ref)) |
| 661 | return; |
| 662 | fastpath: |
| 663 | kmem_cache_free(skbuff_fclone_cache, fclones); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | } |
| 665 | |
Lennert Buytenhek | 04a4bb5 | 2008-10-01 02:33:12 -0700 | [diff] [blame] | 666 | static void skb_release_head_state(struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 667 | { |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 668 | skb_dst_drop(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | #ifdef CONFIG_XFRM |
| 670 | secpath_put(skb->sp); |
| 671 | #endif |
Stephen Hemminger | 9c2b332 | 2005-04-19 22:39:42 -0700 | [diff] [blame] | 672 | if (skb->destructor) { |
| 673 | WARN_ON(in_irq()); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | skb->destructor(skb); |
| 675 | } |
Igor Maravić | a3bf7ae | 2011-12-12 02:58:22 +0000 | [diff] [blame] | 676 | #if IS_ENABLED(CONFIG_NF_CONNTRACK) |
Yasuyuki Kozakai | 5f79e0f | 2007-03-23 11:17:07 -0700 | [diff] [blame] | 677 | nf_conntrack_put(skb->nfct); |
KOVACS Krisztian | 2fc72c7 | 2011-01-12 20:25:08 +0100 | [diff] [blame] | 678 | #endif |
Pablo Neira Ayuso | 1109a90 | 2014-10-01 11:19:17 +0200 | [diff] [blame] | 679 | #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 680 | nf_bridge_put(skb->nf_bridge); |
| 681 | #endif |
Lennert Buytenhek | 04a4bb5 | 2008-10-01 02:33:12 -0700 | [diff] [blame] | 682 | } |
| 683 | |
| 684 | /* Free everything but the sk_buff shell. */ |
| 685 | static void skb_release_all(struct sk_buff *skb) |
| 686 | { |
| 687 | skb_release_head_state(skb); |
Pablo Neira | 5e71d9d | 2013-06-03 09:28:43 +0000 | [diff] [blame] | 688 | if (likely(skb->head)) |
Patrick McHardy | 0ebd0ac | 2013-04-17 06:46:58 +0000 | [diff] [blame] | 689 | skb_release_data(skb); |
Herbert Xu | 2d4baff | 2007-11-26 23:11:19 +0800 | [diff] [blame] | 690 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | |
Herbert Xu | 2d4baff | 2007-11-26 23:11:19 +0800 | [diff] [blame] | 692 | /** |
| 693 | * __kfree_skb - private function |
| 694 | * @skb: buffer |
| 695 | * |
| 696 | * Free an sk_buff. Release anything attached to the buffer. |
| 697 | * Clean the state. This is an internal helper function. Users should |
| 698 | * always call kfree_skb |
| 699 | */ |
| 700 | |
| 701 | void __kfree_skb(struct sk_buff *skb) |
| 702 | { |
| 703 | skb_release_all(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 704 | kfree_skbmem(skb); |
| 705 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 706 | EXPORT_SYMBOL(__kfree_skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 707 | |
| 708 | /** |
Jörn Engel | 231d06a | 2006-03-20 21:28:35 -0800 | [diff] [blame] | 709 | * kfree_skb - free an sk_buff |
| 710 | * @skb: buffer to free |
| 711 | * |
| 712 | * Drop a reference to the buffer and free it if the usage count has |
| 713 | * hit zero. |
| 714 | */ |
| 715 | void kfree_skb(struct sk_buff *skb) |
| 716 | { |
| 717 | if (unlikely(!skb)) |
| 718 | return; |
| 719 | if (likely(atomic_read(&skb->users) == 1)) |
| 720 | smp_rmb(); |
| 721 | else if (likely(!atomic_dec_and_test(&skb->users))) |
| 722 | return; |
Neil Horman | ead2ceb | 2009-03-11 09:49:55 +0000 | [diff] [blame] | 723 | trace_kfree_skb(skb, __builtin_return_address(0)); |
Jörn Engel | 231d06a | 2006-03-20 21:28:35 -0800 | [diff] [blame] | 724 | __kfree_skb(skb); |
| 725 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 726 | EXPORT_SYMBOL(kfree_skb); |
Jörn Engel | 231d06a | 2006-03-20 21:28:35 -0800 | [diff] [blame] | 727 | |
Eric Dumazet | bd8a703 | 2013-06-24 06:26:00 -0700 | [diff] [blame] | 728 | void kfree_skb_list(struct sk_buff *segs) |
| 729 | { |
| 730 | while (segs) { |
| 731 | struct sk_buff *next = segs->next; |
| 732 | |
| 733 | kfree_skb(segs); |
| 734 | segs = next; |
| 735 | } |
| 736 | } |
| 737 | EXPORT_SYMBOL(kfree_skb_list); |
| 738 | |
Stephen Hemminger | d1a203e | 2008-11-01 21:01:09 -0700 | [diff] [blame] | 739 | /** |
Michael S. Tsirkin | 2512117 | 2012-11-01 09:16:28 +0000 | [diff] [blame] | 740 | * skb_tx_error - report an sk_buff xmit error |
| 741 | * @skb: buffer that triggered an error |
| 742 | * |
| 743 | * Report xmit error if a device callback is tracking this skb. |
| 744 | * skb must be freed afterwards. |
| 745 | */ |
| 746 | void skb_tx_error(struct sk_buff *skb) |
| 747 | { |
| 748 | if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) { |
| 749 | struct ubuf_info *uarg; |
| 750 | |
| 751 | uarg = skb_shinfo(skb)->destructor_arg; |
| 752 | if (uarg->callback) |
| 753 | uarg->callback(uarg, false); |
| 754 | skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY; |
| 755 | } |
| 756 | } |
| 757 | EXPORT_SYMBOL(skb_tx_error); |
| 758 | |
| 759 | /** |
Neil Horman | ead2ceb | 2009-03-11 09:49:55 +0000 | [diff] [blame] | 760 | * consume_skb - free an skbuff |
| 761 | * @skb: buffer to free |
| 762 | * |
| 763 | * Drop a ref to the buffer and free it if the usage count has hit zero |
| 764 | * Functions identically to kfree_skb, but kfree_skb assumes that the frame |
| 765 | * is being dropped after a failure and notes that |
| 766 | */ |
| 767 | void consume_skb(struct sk_buff *skb) |
| 768 | { |
| 769 | if (unlikely(!skb)) |
| 770 | return; |
| 771 | if (likely(atomic_read(&skb->users) == 1)) |
| 772 | smp_rmb(); |
| 773 | else if (likely(!atomic_dec_and_test(&skb->users))) |
| 774 | return; |
Koki Sanagi | 07dc22e | 2010-08-23 18:46:12 +0900 | [diff] [blame] | 775 | trace_consume_skb(skb); |
Neil Horman | ead2ceb | 2009-03-11 09:49:55 +0000 | [diff] [blame] | 776 | __kfree_skb(skb); |
| 777 | } |
| 778 | EXPORT_SYMBOL(consume_skb); |
| 779 | |
Eric Dumazet | b193722 | 2014-09-28 22:18:47 -0700 | [diff] [blame] | 780 | /* Make sure a field is enclosed inside headers_start/headers_end section */ |
| 781 | #define CHECK_SKB_FIELD(field) \ |
| 782 | BUILD_BUG_ON(offsetof(struct sk_buff, field) < \ |
| 783 | offsetof(struct sk_buff, headers_start)); \ |
| 784 | BUILD_BUG_ON(offsetof(struct sk_buff, field) > \ |
| 785 | offsetof(struct sk_buff, headers_end)); \ |
| 786 | |
Herbert Xu | dec1881 | 2007-10-14 00:37:30 -0700 | [diff] [blame] | 787 | static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old) |
| 788 | { |
| 789 | new->tstamp = old->tstamp; |
Eric Dumazet | b193722 | 2014-09-28 22:18:47 -0700 | [diff] [blame] | 790 | /* We do not copy old->sk */ |
Herbert Xu | dec1881 | 2007-10-14 00:37:30 -0700 | [diff] [blame] | 791 | new->dev = old->dev; |
Eric Dumazet | b193722 | 2014-09-28 22:18:47 -0700 | [diff] [blame] | 792 | memcpy(new->cb, old->cb, sizeof(old->cb)); |
Eric Dumazet | 7fee226 | 2010-05-11 23:19:48 +0000 | [diff] [blame] | 793 | skb_dst_copy(new, old); |
Alexey Dobriyan | def8b4f | 2008-10-28 13:24:06 -0700 | [diff] [blame] | 794 | #ifdef CONFIG_XFRM |
Herbert Xu | dec1881 | 2007-10-14 00:37:30 -0700 | [diff] [blame] | 795 | new->sp = secpath_get(old->sp); |
| 796 | #endif |
Eric Dumazet | b193722 | 2014-09-28 22:18:47 -0700 | [diff] [blame] | 797 | __nf_copy(new, old, false); |
Patrick McHardy | 6aa895b | 2008-07-14 22:49:06 -0700 | [diff] [blame] | 798 | |
Eric Dumazet | b193722 | 2014-09-28 22:18:47 -0700 | [diff] [blame] | 799 | /* Note : this field could be in headers_start/headers_end section |
| 800 | * It is not yet because we do not want to have a 16 bit hole |
| 801 | */ |
| 802 | new->queue_mapping = old->queue_mapping; |
Eliezer Tamir | 0602129 | 2013-06-10 11:39:50 +0300 | [diff] [blame] | 803 | |
Eric Dumazet | b193722 | 2014-09-28 22:18:47 -0700 | [diff] [blame] | 804 | memcpy(&new->headers_start, &old->headers_start, |
| 805 | offsetof(struct sk_buff, headers_end) - |
| 806 | offsetof(struct sk_buff, headers_start)); |
| 807 | CHECK_SKB_FIELD(protocol); |
| 808 | CHECK_SKB_FIELD(csum); |
| 809 | CHECK_SKB_FIELD(hash); |
| 810 | CHECK_SKB_FIELD(priority); |
| 811 | CHECK_SKB_FIELD(skb_iif); |
| 812 | CHECK_SKB_FIELD(vlan_proto); |
| 813 | CHECK_SKB_FIELD(vlan_tci); |
| 814 | CHECK_SKB_FIELD(transport_header); |
| 815 | CHECK_SKB_FIELD(network_header); |
| 816 | CHECK_SKB_FIELD(mac_header); |
| 817 | CHECK_SKB_FIELD(inner_protocol); |
| 818 | CHECK_SKB_FIELD(inner_transport_header); |
| 819 | CHECK_SKB_FIELD(inner_network_header); |
| 820 | CHECK_SKB_FIELD(inner_mac_header); |
| 821 | CHECK_SKB_FIELD(mark); |
| 822 | #ifdef CONFIG_NETWORK_SECMARK |
| 823 | CHECK_SKB_FIELD(secmark); |
| 824 | #endif |
Cong Wang | e0d1095 | 2013-08-01 11:10:25 +0800 | [diff] [blame] | 825 | #ifdef CONFIG_NET_RX_BUSY_POLL |
Eric Dumazet | b193722 | 2014-09-28 22:18:47 -0700 | [diff] [blame] | 826 | CHECK_SKB_FIELD(napi_id); |
Eliezer Tamir | 0602129 | 2013-06-10 11:39:50 +0300 | [diff] [blame] | 827 | #endif |
Eric Dumazet | 2bd8248 | 2015-02-03 23:48:24 -0800 | [diff] [blame] | 828 | #ifdef CONFIG_XPS |
| 829 | CHECK_SKB_FIELD(sender_cpu); |
| 830 | #endif |
Eric Dumazet | b193722 | 2014-09-28 22:18:47 -0700 | [diff] [blame] | 831 | #ifdef CONFIG_NET_SCHED |
| 832 | CHECK_SKB_FIELD(tc_index); |
| 833 | #ifdef CONFIG_NET_CLS_ACT |
| 834 | CHECK_SKB_FIELD(tc_verd); |
| 835 | #endif |
| 836 | #endif |
| 837 | |
Herbert Xu | dec1881 | 2007-10-14 00:37:30 -0700 | [diff] [blame] | 838 | } |
| 839 | |
Herbert Xu | 82c49a3 | 2009-05-22 22:11:37 +0000 | [diff] [blame] | 840 | /* |
| 841 | * You should not add any new code to this function. Add it to |
| 842 | * __copy_skb_header above instead. |
| 843 | */ |
Herbert Xu | e0053ec | 2007-10-14 00:37:52 -0700 | [diff] [blame] | 844 | static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 845 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 846 | #define C(x) n->x = skb->x |
| 847 | |
| 848 | n->next = n->prev = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 849 | n->sk = NULL; |
Herbert Xu | dec1881 | 2007-10-14 00:37:30 -0700 | [diff] [blame] | 850 | __copy_skb_header(n, skb); |
| 851 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 852 | C(len); |
| 853 | C(data_len); |
Alexey Dobriyan | 3e6b3b2 | 2007-03-16 15:00:46 -0700 | [diff] [blame] | 854 | C(mac_len); |
Patrick McHardy | 334a813 | 2007-06-25 04:35:20 -0700 | [diff] [blame] | 855 | n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len; |
Paul Moore | 02f1c89 | 2008-01-07 21:56:41 -0800 | [diff] [blame] | 856 | n->cloned = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 857 | n->nohdr = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 858 | n->destructor = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 859 | C(tail); |
| 860 | C(end); |
Paul Moore | 02f1c89 | 2008-01-07 21:56:41 -0800 | [diff] [blame] | 861 | C(head); |
Eric Dumazet | d3836f2 | 2012-04-27 00:33:38 +0000 | [diff] [blame] | 862 | C(head_frag); |
Paul Moore | 02f1c89 | 2008-01-07 21:56:41 -0800 | [diff] [blame] | 863 | C(data); |
| 864 | C(truesize); |
| 865 | atomic_set(&n->users, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 866 | |
| 867 | atomic_inc(&(skb_shinfo(skb)->dataref)); |
| 868 | skb->cloned = 1; |
| 869 | |
| 870 | return n; |
Herbert Xu | e0053ec | 2007-10-14 00:37:52 -0700 | [diff] [blame] | 871 | #undef C |
| 872 | } |
| 873 | |
| 874 | /** |
| 875 | * skb_morph - morph one skb into another |
| 876 | * @dst: the skb to receive the contents |
| 877 | * @src: the skb to supply the contents |
| 878 | * |
| 879 | * This is identical to skb_clone except that the target skb is |
| 880 | * supplied by the user. |
| 881 | * |
| 882 | * The target skb is returned upon exit. |
| 883 | */ |
| 884 | struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src) |
| 885 | { |
Herbert Xu | 2d4baff | 2007-11-26 23:11:19 +0800 | [diff] [blame] | 886 | skb_release_all(dst); |
Herbert Xu | e0053ec | 2007-10-14 00:37:52 -0700 | [diff] [blame] | 887 | return __skb_clone(dst, src); |
| 888 | } |
| 889 | EXPORT_SYMBOL_GPL(skb_morph); |
| 890 | |
Ben Hutchings | 2c53040 | 2012-07-10 10:55:09 +0000 | [diff] [blame] | 891 | /** |
| 892 | * skb_copy_ubufs - copy userspace skb frags buffers to kernel |
Michael S. Tsirkin | 48c8301 | 2011-08-31 08:03:29 +0000 | [diff] [blame] | 893 | * @skb: the skb to modify |
| 894 | * @gfp_mask: allocation priority |
| 895 | * |
| 896 | * This must be called on SKBTX_DEV_ZEROCOPY skb. |
| 897 | * It will copy all frags into kernel and drop the reference |
| 898 | * to userspace pages. |
| 899 | * |
| 900 | * If this function is called from an interrupt gfp_mask() must be |
| 901 | * %GFP_ATOMIC. |
| 902 | * |
| 903 | * Returns 0 on success or a negative error code on failure |
| 904 | * to allocate kernel memory to copy to. |
| 905 | */ |
| 906 | int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask) |
Shirley Ma | a6686f2 | 2011-07-06 12:22:12 +0000 | [diff] [blame] | 907 | { |
| 908 | int i; |
| 909 | int num_frags = skb_shinfo(skb)->nr_frags; |
| 910 | struct page *page, *head = NULL; |
| 911 | struct ubuf_info *uarg = skb_shinfo(skb)->destructor_arg; |
| 912 | |
| 913 | for (i = 0; i < num_frags; i++) { |
| 914 | u8 *vaddr; |
| 915 | skb_frag_t *f = &skb_shinfo(skb)->frags[i]; |
| 916 | |
Krishna Kumar | 02756ed | 2012-07-17 02:05:29 +0000 | [diff] [blame] | 917 | page = alloc_page(gfp_mask); |
Shirley Ma | a6686f2 | 2011-07-06 12:22:12 +0000 | [diff] [blame] | 918 | if (!page) { |
| 919 | while (head) { |
Sunghan Suh | 40dadff | 2013-07-12 16:17:23 +0900 | [diff] [blame] | 920 | struct page *next = (struct page *)page_private(head); |
Shirley Ma | a6686f2 | 2011-07-06 12:22:12 +0000 | [diff] [blame] | 921 | put_page(head); |
| 922 | head = next; |
| 923 | } |
| 924 | return -ENOMEM; |
| 925 | } |
Eric Dumazet | 51c56b0 | 2012-04-05 11:35:15 +0200 | [diff] [blame] | 926 | vaddr = kmap_atomic(skb_frag_page(f)); |
Shirley Ma | a6686f2 | 2011-07-06 12:22:12 +0000 | [diff] [blame] | 927 | memcpy(page_address(page), |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 928 | vaddr + f->page_offset, skb_frag_size(f)); |
Eric Dumazet | 51c56b0 | 2012-04-05 11:35:15 +0200 | [diff] [blame] | 929 | kunmap_atomic(vaddr); |
Sunghan Suh | 40dadff | 2013-07-12 16:17:23 +0900 | [diff] [blame] | 930 | set_page_private(page, (unsigned long)head); |
Shirley Ma | a6686f2 | 2011-07-06 12:22:12 +0000 | [diff] [blame] | 931 | head = page; |
| 932 | } |
| 933 | |
| 934 | /* skb frags release userspace buffers */ |
Krishna Kumar | 02756ed | 2012-07-17 02:05:29 +0000 | [diff] [blame] | 935 | for (i = 0; i < num_frags; i++) |
Ian Campbell | a8605c6 | 2011-10-19 23:01:49 +0000 | [diff] [blame] | 936 | skb_frag_unref(skb, i); |
Shirley Ma | a6686f2 | 2011-07-06 12:22:12 +0000 | [diff] [blame] | 937 | |
Michael S. Tsirkin | e19d676 | 2012-11-01 09:16:22 +0000 | [diff] [blame] | 938 | uarg->callback(uarg, false); |
Shirley Ma | a6686f2 | 2011-07-06 12:22:12 +0000 | [diff] [blame] | 939 | |
| 940 | /* skb frags point to kernel buffers */ |
Krishna Kumar | 02756ed | 2012-07-17 02:05:29 +0000 | [diff] [blame] | 941 | for (i = num_frags - 1; i >= 0; i--) { |
| 942 | __skb_fill_page_desc(skb, i, head, 0, |
| 943 | skb_shinfo(skb)->frags[i].size); |
Sunghan Suh | 40dadff | 2013-07-12 16:17:23 +0900 | [diff] [blame] | 944 | head = (struct page *)page_private(head); |
Shirley Ma | a6686f2 | 2011-07-06 12:22:12 +0000 | [diff] [blame] | 945 | } |
Michael S. Tsirkin | 48c8301 | 2011-08-31 08:03:29 +0000 | [diff] [blame] | 946 | |
| 947 | skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY; |
Shirley Ma | a6686f2 | 2011-07-06 12:22:12 +0000 | [diff] [blame] | 948 | return 0; |
| 949 | } |
Michael S. Tsirkin | dcc0fb7 | 2012-07-20 09:23:20 +0000 | [diff] [blame] | 950 | EXPORT_SYMBOL_GPL(skb_copy_ubufs); |
Shirley Ma | a6686f2 | 2011-07-06 12:22:12 +0000 | [diff] [blame] | 951 | |
Herbert Xu | e0053ec | 2007-10-14 00:37:52 -0700 | [diff] [blame] | 952 | /** |
| 953 | * skb_clone - duplicate an sk_buff |
| 954 | * @skb: buffer to clone |
| 955 | * @gfp_mask: allocation priority |
| 956 | * |
| 957 | * Duplicate an &sk_buff. The new one is not owned by a socket. Both |
| 958 | * copies share the same packet data but not structure. The new |
| 959 | * buffer has a reference count of 1. If the allocation fails the |
| 960 | * function returns %NULL otherwise the new buffer is returned. |
| 961 | * |
| 962 | * If this function is called from an interrupt gfp_mask() must be |
| 963 | * %GFP_ATOMIC. |
| 964 | */ |
| 965 | |
| 966 | struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask) |
| 967 | { |
Eric Dumazet | d0bf4a9 | 2014-09-29 13:29:15 -0700 | [diff] [blame] | 968 | struct sk_buff_fclones *fclones = container_of(skb, |
| 969 | struct sk_buff_fclones, |
| 970 | skb1); |
Eric Dumazet | 6ffe75eb | 2014-12-03 17:04:39 -0800 | [diff] [blame] | 971 | struct sk_buff *n; |
Herbert Xu | e0053ec | 2007-10-14 00:37:52 -0700 | [diff] [blame] | 972 | |
Michael S. Tsirkin | 70008aa | 2012-07-20 09:23:10 +0000 | [diff] [blame] | 973 | if (skb_orphan_frags(skb, gfp_mask)) |
| 974 | return NULL; |
Shirley Ma | a6686f2 | 2011-07-06 12:22:12 +0000 | [diff] [blame] | 975 | |
Herbert Xu | e0053ec | 2007-10-14 00:37:52 -0700 | [diff] [blame] | 976 | if (skb->fclone == SKB_FCLONE_ORIG && |
Eric Dumazet | 6ffe75eb | 2014-12-03 17:04:39 -0800 | [diff] [blame] | 977 | atomic_read(&fclones->fclone_ref) == 1) { |
| 978 | n = &fclones->skb2; |
| 979 | atomic_set(&fclones->fclone_ref, 2); |
Herbert Xu | e0053ec | 2007-10-14 00:37:52 -0700 | [diff] [blame] | 980 | } else { |
Mel Gorman | c93bdd0 | 2012-07-31 16:44:19 -0700 | [diff] [blame] | 981 | if (skb_pfmemalloc(skb)) |
| 982 | gfp_mask |= __GFP_MEMALLOC; |
| 983 | |
Herbert Xu | e0053ec | 2007-10-14 00:37:52 -0700 | [diff] [blame] | 984 | n = kmem_cache_alloc(skbuff_head_cache, gfp_mask); |
| 985 | if (!n) |
| 986 | return NULL; |
Vegard Nossum | fe55f6d | 2008-08-30 12:16:35 +0200 | [diff] [blame] | 987 | |
| 988 | kmemcheck_annotate_bitfield(n, flags1); |
Herbert Xu | e0053ec | 2007-10-14 00:37:52 -0700 | [diff] [blame] | 989 | n->fclone = SKB_FCLONE_UNAVAILABLE; |
| 990 | } |
| 991 | |
| 992 | return __skb_clone(n, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 993 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 994 | EXPORT_SYMBOL(skb_clone); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 995 | |
Pravin B Shelar | f5b1729 | 2013-03-07 13:21:40 +0000 | [diff] [blame] | 996 | static void skb_headers_offset_update(struct sk_buff *skb, int off) |
| 997 | { |
Eric Dumazet | 030737b | 2013-10-19 11:42:54 -0700 | [diff] [blame] | 998 | /* Only adjust this if it actually is csum_start rather than csum */ |
| 999 | if (skb->ip_summed == CHECKSUM_PARTIAL) |
| 1000 | skb->csum_start += off; |
Pravin B Shelar | f5b1729 | 2013-03-07 13:21:40 +0000 | [diff] [blame] | 1001 | /* {transport,network,mac}_header and tail are relative to skb->head */ |
| 1002 | skb->transport_header += off; |
| 1003 | skb->network_header += off; |
| 1004 | if (skb_mac_header_was_set(skb)) |
| 1005 | skb->mac_header += off; |
| 1006 | skb->inner_transport_header += off; |
| 1007 | skb->inner_network_header += off; |
Pravin B Shelar | aefbd2b | 2013-03-07 13:21:46 +0000 | [diff] [blame] | 1008 | skb->inner_mac_header += off; |
Pravin B Shelar | f5b1729 | 2013-03-07 13:21:40 +0000 | [diff] [blame] | 1009 | } |
| 1010 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1011 | static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old) |
| 1012 | { |
Herbert Xu | dec1881 | 2007-10-14 00:37:30 -0700 | [diff] [blame] | 1013 | __copy_skb_header(new, old); |
| 1014 | |
Herbert Xu | 7967168 | 2006-06-22 02:40:14 -0700 | [diff] [blame] | 1015 | skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size; |
| 1016 | skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs; |
| 1017 | skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1018 | } |
| 1019 | |
Mel Gorman | c93bdd0 | 2012-07-31 16:44:19 -0700 | [diff] [blame] | 1020 | static inline int skb_alloc_rx_flag(const struct sk_buff *skb) |
| 1021 | { |
| 1022 | if (skb_pfmemalloc(skb)) |
| 1023 | return SKB_ALLOC_RX; |
| 1024 | return 0; |
| 1025 | } |
| 1026 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1027 | /** |
| 1028 | * skb_copy - create private copy of an sk_buff |
| 1029 | * @skb: buffer to copy |
| 1030 | * @gfp_mask: allocation priority |
| 1031 | * |
| 1032 | * Make a copy of both an &sk_buff and its data. This is used when the |
| 1033 | * caller wishes to modify the data and needs a private copy of the |
| 1034 | * data to alter. Returns %NULL on failure or the pointer to the buffer |
| 1035 | * on success. The returned buffer has a reference count of 1. |
| 1036 | * |
| 1037 | * As by-product this function converts non-linear &sk_buff to linear |
| 1038 | * one, so that &sk_buff becomes completely private and caller is allowed |
| 1039 | * to modify all the data of returned buffer. This means that this |
| 1040 | * function is not recommended for use in circumstances when only |
| 1041 | * header is going to be modified. Use pskb_copy() instead. |
| 1042 | */ |
| 1043 | |
Al Viro | dd0fc66 | 2005-10-07 07:46:04 +0100 | [diff] [blame] | 1044 | struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1045 | { |
Eric Dumazet | 6602ceb | 2010-09-01 05:25:10 +0000 | [diff] [blame] | 1046 | int headerlen = skb_headroom(skb); |
Alexander Duyck | ec47ea8 | 2012-05-04 14:26:56 +0000 | [diff] [blame] | 1047 | unsigned int size = skb_end_offset(skb) + skb->data_len; |
Mel Gorman | c93bdd0 | 2012-07-31 16:44:19 -0700 | [diff] [blame] | 1048 | struct sk_buff *n = __alloc_skb(size, gfp_mask, |
| 1049 | skb_alloc_rx_flag(skb), NUMA_NO_NODE); |
Eric Dumazet | 6602ceb | 2010-09-01 05:25:10 +0000 | [diff] [blame] | 1050 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1051 | if (!n) |
| 1052 | return NULL; |
| 1053 | |
| 1054 | /* Set the data pointer */ |
| 1055 | skb_reserve(n, headerlen); |
| 1056 | /* Set the tail pointer and length */ |
| 1057 | skb_put(n, skb->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1058 | |
| 1059 | if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len)) |
| 1060 | BUG(); |
| 1061 | |
| 1062 | copy_skb_header(n, skb); |
| 1063 | return n; |
| 1064 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 1065 | EXPORT_SYMBOL(skb_copy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1066 | |
| 1067 | /** |
Octavian Purdila | bad93e9 | 2014-06-12 01:36:26 +0300 | [diff] [blame] | 1068 | * __pskb_copy_fclone - create copy of an sk_buff with private head. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1069 | * @skb: buffer to copy |
Eric Dumazet | 117632e | 2011-12-03 21:39:53 +0000 | [diff] [blame] | 1070 | * @headroom: headroom of new skb |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1071 | * @gfp_mask: allocation priority |
Octavian Purdila | bad93e9 | 2014-06-12 01:36:26 +0300 | [diff] [blame] | 1072 | * @fclone: if true allocate the copy of the skb from the fclone |
| 1073 | * cache instead of the head cache; it is recommended to set this |
| 1074 | * to true for the cases where the copy will likely be cloned |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1075 | * |
| 1076 | * Make a copy of both an &sk_buff and part of its data, located |
| 1077 | * in header. Fragmented data remain shared. This is used when |
| 1078 | * the caller wishes to modify only header of &sk_buff and needs |
| 1079 | * private copy of the header to alter. Returns %NULL on failure |
| 1080 | * or the pointer to the buffer on success. |
| 1081 | * The returned buffer has a reference count of 1. |
| 1082 | */ |
| 1083 | |
Octavian Purdila | bad93e9 | 2014-06-12 01:36:26 +0300 | [diff] [blame] | 1084 | struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom, |
| 1085 | gfp_t gfp_mask, bool fclone) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1086 | { |
Eric Dumazet | 117632e | 2011-12-03 21:39:53 +0000 | [diff] [blame] | 1087 | unsigned int size = skb_headlen(skb) + headroom; |
Octavian Purdila | bad93e9 | 2014-06-12 01:36:26 +0300 | [diff] [blame] | 1088 | int flags = skb_alloc_rx_flag(skb) | (fclone ? SKB_ALLOC_FCLONE : 0); |
| 1089 | struct sk_buff *n = __alloc_skb(size, gfp_mask, flags, NUMA_NO_NODE); |
Eric Dumazet | 6602ceb | 2010-09-01 05:25:10 +0000 | [diff] [blame] | 1090 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1091 | if (!n) |
| 1092 | goto out; |
| 1093 | |
| 1094 | /* Set the data pointer */ |
Eric Dumazet | 117632e | 2011-12-03 21:39:53 +0000 | [diff] [blame] | 1095 | skb_reserve(n, headroom); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1096 | /* Set the tail pointer and length */ |
| 1097 | skb_put(n, skb_headlen(skb)); |
| 1098 | /* Copy the bytes */ |
Arnaldo Carvalho de Melo | d626f62 | 2007-03-27 18:55:52 -0300 | [diff] [blame] | 1099 | skb_copy_from_linear_data(skb, n->data, n->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1100 | |
Herbert Xu | 25f484a | 2006-11-07 14:57:15 -0800 | [diff] [blame] | 1101 | n->truesize += skb->data_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1102 | n->data_len = skb->data_len; |
| 1103 | n->len = skb->len; |
| 1104 | |
| 1105 | if (skb_shinfo(skb)->nr_frags) { |
| 1106 | int i; |
| 1107 | |
Michael S. Tsirkin | 70008aa | 2012-07-20 09:23:10 +0000 | [diff] [blame] | 1108 | if (skb_orphan_frags(skb, gfp_mask)) { |
| 1109 | kfree_skb(n); |
| 1110 | n = NULL; |
| 1111 | goto out; |
Shirley Ma | a6686f2 | 2011-07-06 12:22:12 +0000 | [diff] [blame] | 1112 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1113 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
| 1114 | skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i]; |
Ian Campbell | ea2ab69 | 2011-08-22 23:44:58 +0000 | [diff] [blame] | 1115 | skb_frag_ref(skb, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1116 | } |
| 1117 | skb_shinfo(n)->nr_frags = i; |
| 1118 | } |
| 1119 | |
David S. Miller | 21dc330 | 2010-08-23 00:13:46 -0700 | [diff] [blame] | 1120 | if (skb_has_frag_list(skb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1121 | skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list; |
| 1122 | skb_clone_fraglist(n); |
| 1123 | } |
| 1124 | |
| 1125 | copy_skb_header(n, skb); |
| 1126 | out: |
| 1127 | return n; |
| 1128 | } |
Octavian Purdila | bad93e9 | 2014-06-12 01:36:26 +0300 | [diff] [blame] | 1129 | EXPORT_SYMBOL(__pskb_copy_fclone); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1130 | |
| 1131 | /** |
| 1132 | * pskb_expand_head - reallocate header of &sk_buff |
| 1133 | * @skb: buffer to reallocate |
| 1134 | * @nhead: room to add at head |
| 1135 | * @ntail: room to add at tail |
| 1136 | * @gfp_mask: allocation priority |
| 1137 | * |
Mathias Krause | bc32383 | 2013-11-07 14:18:26 +0100 | [diff] [blame] | 1138 | * Expands (or creates identical copy, if @nhead and @ntail are zero) |
| 1139 | * header of @skb. &sk_buff itself is not changed. &sk_buff MUST have |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1140 | * reference count of 1. Returns zero in the case of success or error, |
| 1141 | * if expansion failed. In the last case, &sk_buff is not changed. |
| 1142 | * |
| 1143 | * All the pointers pointing into skb header may change and must be |
| 1144 | * reloaded after call to this function. |
| 1145 | */ |
| 1146 | |
Victor Fusco | 86a76ca | 2005-07-08 14:57:47 -0700 | [diff] [blame] | 1147 | int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail, |
Al Viro | dd0fc66 | 2005-10-07 07:46:04 +0100 | [diff] [blame] | 1148 | gfp_t gfp_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1149 | { |
| 1150 | int i; |
| 1151 | u8 *data; |
Alexander Duyck | ec47ea8 | 2012-05-04 14:26:56 +0000 | [diff] [blame] | 1152 | int size = nhead + skb_end_offset(skb) + ntail; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1153 | long off; |
| 1154 | |
Herbert Xu | 4edd87a | 2008-10-01 07:09:38 -0700 | [diff] [blame] | 1155 | BUG_ON(nhead < 0); |
| 1156 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1157 | if (skb_shared(skb)) |
| 1158 | BUG(); |
| 1159 | |
| 1160 | size = SKB_DATA_ALIGN(size); |
| 1161 | |
Mel Gorman | c93bdd0 | 2012-07-31 16:44:19 -0700 | [diff] [blame] | 1162 | if (skb_pfmemalloc(skb)) |
| 1163 | gfp_mask |= __GFP_MEMALLOC; |
| 1164 | data = kmalloc_reserve(size + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)), |
| 1165 | gfp_mask, NUMA_NO_NODE, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1166 | if (!data) |
| 1167 | goto nodata; |
Eric Dumazet | 87151b8 | 2012-04-10 20:08:39 +0000 | [diff] [blame] | 1168 | size = SKB_WITH_OVERHEAD(ksize(data)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1169 | |
| 1170 | /* Copy only real data... and, alas, header. This should be |
Eric Dumazet | 6602ceb | 2010-09-01 05:25:10 +0000 | [diff] [blame] | 1171 | * optimized for the cases when header is void. |
| 1172 | */ |
| 1173 | memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head); |
| 1174 | |
| 1175 | memcpy((struct skb_shared_info *)(data + size), |
| 1176 | skb_shinfo(skb), |
Eric Dumazet | fed6638 | 2010-07-22 19:09:08 +0000 | [diff] [blame] | 1177 | offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags])); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1178 | |
Alexander Duyck | 3e24591 | 2012-05-04 14:26:51 +0000 | [diff] [blame] | 1179 | /* |
| 1180 | * if shinfo is shared we must drop the old head gracefully, but if it |
| 1181 | * is not we can just drop the old head and let the existing refcount |
| 1182 | * be since all we did is relocate the values |
| 1183 | */ |
| 1184 | if (skb_cloned(skb)) { |
Shirley Ma | a6686f2 | 2011-07-06 12:22:12 +0000 | [diff] [blame] | 1185 | /* copy this zero copy skb frags */ |
Michael S. Tsirkin | 70008aa | 2012-07-20 09:23:10 +0000 | [diff] [blame] | 1186 | if (skb_orphan_frags(skb, gfp_mask)) |
| 1187 | goto nofrags; |
Eric Dumazet | 1fd6304 | 2010-09-02 23:09:32 +0000 | [diff] [blame] | 1188 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) |
Ian Campbell | ea2ab69 | 2011-08-22 23:44:58 +0000 | [diff] [blame] | 1189 | skb_frag_ref(skb, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1190 | |
Eric Dumazet | 1fd6304 | 2010-09-02 23:09:32 +0000 | [diff] [blame] | 1191 | if (skb_has_frag_list(skb)) |
| 1192 | skb_clone_fraglist(skb); |
| 1193 | |
| 1194 | skb_release_data(skb); |
Alexander Duyck | 3e24591 | 2012-05-04 14:26:51 +0000 | [diff] [blame] | 1195 | } else { |
| 1196 | skb_free_head(skb); |
Eric Dumazet | 1fd6304 | 2010-09-02 23:09:32 +0000 | [diff] [blame] | 1197 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1198 | off = (data + nhead) - skb->head; |
| 1199 | |
| 1200 | skb->head = data; |
Eric Dumazet | d3836f2 | 2012-04-27 00:33:38 +0000 | [diff] [blame] | 1201 | skb->head_frag = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1202 | skb->data += off; |
Arnaldo Carvalho de Melo | 4305b54 | 2007-04-19 20:43:29 -0700 | [diff] [blame] | 1203 | #ifdef NET_SKBUFF_DATA_USES_OFFSET |
| 1204 | skb->end = size; |
Patrick McHardy | 56eb888 | 2007-04-09 11:45:04 -0700 | [diff] [blame] | 1205 | off = nhead; |
Arnaldo Carvalho de Melo | 4305b54 | 2007-04-19 20:43:29 -0700 | [diff] [blame] | 1206 | #else |
| 1207 | skb->end = skb->head + size; |
Patrick McHardy | 56eb888 | 2007-04-09 11:45:04 -0700 | [diff] [blame] | 1208 | #endif |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 1209 | skb->tail += off; |
Peter Pan(潘卫平) | b41abb4 | 2013-06-06 21:27:21 +0800 | [diff] [blame] | 1210 | skb_headers_offset_update(skb, nhead); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1211 | skb->cloned = 0; |
Patrick McHardy | 334a813 | 2007-06-25 04:35:20 -0700 | [diff] [blame] | 1212 | skb->hdr_len = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1213 | skb->nohdr = 0; |
| 1214 | atomic_set(&skb_shinfo(skb)->dataref, 1); |
| 1215 | return 0; |
| 1216 | |
Shirley Ma | a6686f2 | 2011-07-06 12:22:12 +0000 | [diff] [blame] | 1217 | nofrags: |
| 1218 | kfree(data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1219 | nodata: |
| 1220 | return -ENOMEM; |
| 1221 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 1222 | EXPORT_SYMBOL(pskb_expand_head); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1223 | |
| 1224 | /* Make private copy of skb with writable head and some headroom */ |
| 1225 | |
| 1226 | struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom) |
| 1227 | { |
| 1228 | struct sk_buff *skb2; |
| 1229 | int delta = headroom - skb_headroom(skb); |
| 1230 | |
| 1231 | if (delta <= 0) |
| 1232 | skb2 = pskb_copy(skb, GFP_ATOMIC); |
| 1233 | else { |
| 1234 | skb2 = skb_clone(skb, GFP_ATOMIC); |
| 1235 | if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0, |
| 1236 | GFP_ATOMIC)) { |
| 1237 | kfree_skb(skb2); |
| 1238 | skb2 = NULL; |
| 1239 | } |
| 1240 | } |
| 1241 | return skb2; |
| 1242 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 1243 | EXPORT_SYMBOL(skb_realloc_headroom); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1244 | |
| 1245 | /** |
| 1246 | * skb_copy_expand - copy and expand sk_buff |
| 1247 | * @skb: buffer to copy |
| 1248 | * @newheadroom: new free bytes at head |
| 1249 | * @newtailroom: new free bytes at tail |
| 1250 | * @gfp_mask: allocation priority |
| 1251 | * |
| 1252 | * Make a copy of both an &sk_buff and its data and while doing so |
| 1253 | * allocate additional space. |
| 1254 | * |
| 1255 | * This is used when the caller wishes to modify the data and needs a |
| 1256 | * private copy of the data to alter as well as more space for new fields. |
| 1257 | * Returns %NULL on failure or the pointer to the buffer |
| 1258 | * on success. The returned buffer has a reference count of 1. |
| 1259 | * |
| 1260 | * You must pass %GFP_ATOMIC as the allocation priority if this function |
| 1261 | * is called from an interrupt. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1262 | */ |
| 1263 | struct sk_buff *skb_copy_expand(const struct sk_buff *skb, |
Victor Fusco | 86a76ca | 2005-07-08 14:57:47 -0700 | [diff] [blame] | 1264 | int newheadroom, int newtailroom, |
Al Viro | dd0fc66 | 2005-10-07 07:46:04 +0100 | [diff] [blame] | 1265 | gfp_t gfp_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1266 | { |
| 1267 | /* |
| 1268 | * Allocate the copy buffer |
| 1269 | */ |
Mel Gorman | c93bdd0 | 2012-07-31 16:44:19 -0700 | [diff] [blame] | 1270 | struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom, |
| 1271 | gfp_mask, skb_alloc_rx_flag(skb), |
| 1272 | NUMA_NO_NODE); |
Patrick McHardy | efd1e8d | 2007-04-10 18:30:09 -0700 | [diff] [blame] | 1273 | int oldheadroom = skb_headroom(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1274 | int head_copy_len, head_copy_off; |
| 1275 | |
| 1276 | if (!n) |
| 1277 | return NULL; |
| 1278 | |
| 1279 | skb_reserve(n, newheadroom); |
| 1280 | |
| 1281 | /* Set the tail pointer and length */ |
| 1282 | skb_put(n, skb->len); |
| 1283 | |
Patrick McHardy | efd1e8d | 2007-04-10 18:30:09 -0700 | [diff] [blame] | 1284 | head_copy_len = oldheadroom; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1285 | head_copy_off = 0; |
| 1286 | if (newheadroom <= head_copy_len) |
| 1287 | head_copy_len = newheadroom; |
| 1288 | else |
| 1289 | head_copy_off = newheadroom - head_copy_len; |
| 1290 | |
| 1291 | /* Copy the linear header and data. */ |
| 1292 | if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off, |
| 1293 | skb->len + head_copy_len)) |
| 1294 | BUG(); |
| 1295 | |
| 1296 | copy_skb_header(n, skb); |
| 1297 | |
Eric Dumazet | 030737b | 2013-10-19 11:42:54 -0700 | [diff] [blame] | 1298 | skb_headers_offset_update(n, newheadroom - oldheadroom); |
Patrick McHardy | efd1e8d | 2007-04-10 18:30:09 -0700 | [diff] [blame] | 1299 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1300 | return n; |
| 1301 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 1302 | EXPORT_SYMBOL(skb_copy_expand); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1303 | |
| 1304 | /** |
| 1305 | * skb_pad - zero pad the tail of an skb |
| 1306 | * @skb: buffer to pad |
| 1307 | * @pad: space to pad |
| 1308 | * |
| 1309 | * Ensure that a buffer is followed by a padding area that is zero |
| 1310 | * filled. Used by network drivers which may DMA or transfer data |
| 1311 | * beyond the buffer end onto the wire. |
| 1312 | * |
Herbert Xu | 5b057c6 | 2006-06-23 02:06:41 -0700 | [diff] [blame] | 1313 | * May return error in out of memory cases. The skb is freed on error. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1314 | */ |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 1315 | |
Herbert Xu | 5b057c6 | 2006-06-23 02:06:41 -0700 | [diff] [blame] | 1316 | int skb_pad(struct sk_buff *skb, int pad) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1317 | { |
Herbert Xu | 5b057c6 | 2006-06-23 02:06:41 -0700 | [diff] [blame] | 1318 | int err; |
| 1319 | int ntail; |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 1320 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1321 | /* If the skbuff is non linear tailroom is always zero.. */ |
Herbert Xu | 5b057c6 | 2006-06-23 02:06:41 -0700 | [diff] [blame] | 1322 | if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1323 | memset(skb->data+skb->len, 0, pad); |
Herbert Xu | 5b057c6 | 2006-06-23 02:06:41 -0700 | [diff] [blame] | 1324 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1325 | } |
Herbert Xu | 5b057c6 | 2006-06-23 02:06:41 -0700 | [diff] [blame] | 1326 | |
Arnaldo Carvalho de Melo | 4305b54 | 2007-04-19 20:43:29 -0700 | [diff] [blame] | 1327 | ntail = skb->data_len + pad - (skb->end - skb->tail); |
Herbert Xu | 5b057c6 | 2006-06-23 02:06:41 -0700 | [diff] [blame] | 1328 | if (likely(skb_cloned(skb) || ntail > 0)) { |
| 1329 | err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC); |
| 1330 | if (unlikely(err)) |
| 1331 | goto free_skb; |
| 1332 | } |
| 1333 | |
| 1334 | /* FIXME: The use of this function with non-linear skb's really needs |
| 1335 | * to be audited. |
| 1336 | */ |
| 1337 | err = skb_linearize(skb); |
| 1338 | if (unlikely(err)) |
| 1339 | goto free_skb; |
| 1340 | |
| 1341 | memset(skb->data + skb->len, 0, pad); |
| 1342 | return 0; |
| 1343 | |
| 1344 | free_skb: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1345 | kfree_skb(skb); |
Herbert Xu | 5b057c6 | 2006-06-23 02:06:41 -0700 | [diff] [blame] | 1346 | return err; |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 1347 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 1348 | EXPORT_SYMBOL(skb_pad); |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 1349 | |
Ilpo Järvinen | 0dde3e1 | 2008-03-27 17:43:41 -0700 | [diff] [blame] | 1350 | /** |
Mathias Krause | 0c7ddf3 | 2013-11-07 14:18:24 +0100 | [diff] [blame] | 1351 | * pskb_put - add data to the tail of a potentially fragmented buffer |
| 1352 | * @skb: start of the buffer to use |
| 1353 | * @tail: tail fragment of the buffer to use |
| 1354 | * @len: amount of data to add |
| 1355 | * |
| 1356 | * This function extends the used data area of the potentially |
| 1357 | * fragmented buffer. @tail must be the last fragment of @skb -- or |
| 1358 | * @skb itself. If this would exceed the total buffer size the kernel |
| 1359 | * will panic. A pointer to the first byte of the extra data is |
| 1360 | * returned. |
| 1361 | */ |
| 1362 | |
| 1363 | unsigned char *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len) |
| 1364 | { |
| 1365 | if (tail != skb) { |
| 1366 | skb->data_len += len; |
| 1367 | skb->len += len; |
| 1368 | } |
| 1369 | return skb_put(tail, len); |
| 1370 | } |
| 1371 | EXPORT_SYMBOL_GPL(pskb_put); |
| 1372 | |
| 1373 | /** |
Ilpo Järvinen | 0dde3e1 | 2008-03-27 17:43:41 -0700 | [diff] [blame] | 1374 | * skb_put - add data to a buffer |
| 1375 | * @skb: buffer to use |
| 1376 | * @len: amount of data to add |
| 1377 | * |
| 1378 | * This function extends the used data area of the buffer. If this would |
| 1379 | * exceed the total buffer size the kernel will panic. A pointer to the |
| 1380 | * first byte of the extra data is returned. |
| 1381 | */ |
| 1382 | unsigned char *skb_put(struct sk_buff *skb, unsigned int len) |
| 1383 | { |
| 1384 | unsigned char *tmp = skb_tail_pointer(skb); |
| 1385 | SKB_LINEAR_ASSERT(skb); |
| 1386 | skb->tail += len; |
| 1387 | skb->len += len; |
| 1388 | if (unlikely(skb->tail > skb->end)) |
| 1389 | skb_over_panic(skb, len, __builtin_return_address(0)); |
| 1390 | return tmp; |
| 1391 | } |
| 1392 | EXPORT_SYMBOL(skb_put); |
| 1393 | |
Ilpo Järvinen | 6be8ac2 | 2008-03-27 17:47:24 -0700 | [diff] [blame] | 1394 | /** |
Ilpo Järvinen | c2aa270 | 2008-03-27 17:52:40 -0700 | [diff] [blame] | 1395 | * skb_push - add data to the start of a buffer |
| 1396 | * @skb: buffer to use |
| 1397 | * @len: amount of data to add |
| 1398 | * |
| 1399 | * This function extends the used data area of the buffer at the buffer |
| 1400 | * start. If this would exceed the total buffer headroom the kernel will |
| 1401 | * panic. A pointer to the first byte of the extra data is returned. |
| 1402 | */ |
| 1403 | unsigned char *skb_push(struct sk_buff *skb, unsigned int len) |
| 1404 | { |
| 1405 | skb->data -= len; |
| 1406 | skb->len += len; |
| 1407 | if (unlikely(skb->data<skb->head)) |
| 1408 | skb_under_panic(skb, len, __builtin_return_address(0)); |
| 1409 | return skb->data; |
| 1410 | } |
| 1411 | EXPORT_SYMBOL(skb_push); |
| 1412 | |
| 1413 | /** |
Ilpo Järvinen | 6be8ac2 | 2008-03-27 17:47:24 -0700 | [diff] [blame] | 1414 | * skb_pull - remove data from the start of a buffer |
| 1415 | * @skb: buffer to use |
| 1416 | * @len: amount of data to remove |
| 1417 | * |
| 1418 | * This function removes data from the start of a buffer, returning |
| 1419 | * the memory to the headroom. A pointer to the next data in the buffer |
| 1420 | * is returned. Once the data has been pulled future pushes will overwrite |
| 1421 | * the old data. |
| 1422 | */ |
| 1423 | unsigned char *skb_pull(struct sk_buff *skb, unsigned int len) |
| 1424 | { |
David S. Miller | 47d2964 | 2010-05-02 02:21:44 -0700 | [diff] [blame] | 1425 | return skb_pull_inline(skb, len); |
Ilpo Järvinen | 6be8ac2 | 2008-03-27 17:47:24 -0700 | [diff] [blame] | 1426 | } |
| 1427 | EXPORT_SYMBOL(skb_pull); |
| 1428 | |
Ilpo Järvinen | 419ae74 | 2008-03-27 17:54:01 -0700 | [diff] [blame] | 1429 | /** |
| 1430 | * skb_trim - remove end from a buffer |
| 1431 | * @skb: buffer to alter |
| 1432 | * @len: new length |
| 1433 | * |
| 1434 | * Cut the length of a buffer down by removing data from the tail. If |
| 1435 | * the buffer is already under the length specified it is not modified. |
| 1436 | * The skb must be linear. |
| 1437 | */ |
| 1438 | void skb_trim(struct sk_buff *skb, unsigned int len) |
| 1439 | { |
| 1440 | if (skb->len > len) |
| 1441 | __skb_trim(skb, len); |
| 1442 | } |
| 1443 | EXPORT_SYMBOL(skb_trim); |
| 1444 | |
Herbert Xu | 3cc0e87 | 2006-06-09 16:13:38 -0700 | [diff] [blame] | 1445 | /* Trims skb to length len. It can change skb pointers. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1446 | */ |
| 1447 | |
Herbert Xu | 3cc0e87 | 2006-06-09 16:13:38 -0700 | [diff] [blame] | 1448 | int ___pskb_trim(struct sk_buff *skb, unsigned int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1449 | { |
Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 1450 | struct sk_buff **fragp; |
| 1451 | struct sk_buff *frag; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1452 | int offset = skb_headlen(skb); |
| 1453 | int nfrags = skb_shinfo(skb)->nr_frags; |
| 1454 | int i; |
Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 1455 | int err; |
| 1456 | |
| 1457 | if (skb_cloned(skb) && |
| 1458 | unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC)))) |
| 1459 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1460 | |
Herbert Xu | f4d26fb | 2006-07-30 20:20:28 -0700 | [diff] [blame] | 1461 | i = 0; |
| 1462 | if (offset >= len) |
| 1463 | goto drop_pages; |
| 1464 | |
| 1465 | for (; i < nfrags; i++) { |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 1466 | int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]); |
Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 1467 | |
| 1468 | if (end < len) { |
| 1469 | offset = end; |
| 1470 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1471 | } |
Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 1472 | |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 1473 | skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset); |
Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 1474 | |
Herbert Xu | f4d26fb | 2006-07-30 20:20:28 -0700 | [diff] [blame] | 1475 | drop_pages: |
Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 1476 | skb_shinfo(skb)->nr_frags = i; |
| 1477 | |
| 1478 | for (; i < nfrags; i++) |
Ian Campbell | ea2ab69 | 2011-08-22 23:44:58 +0000 | [diff] [blame] | 1479 | skb_frag_unref(skb, i); |
Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 1480 | |
David S. Miller | 21dc330 | 2010-08-23 00:13:46 -0700 | [diff] [blame] | 1481 | if (skb_has_frag_list(skb)) |
Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 1482 | skb_drop_fraglist(skb); |
Herbert Xu | f4d26fb | 2006-07-30 20:20:28 -0700 | [diff] [blame] | 1483 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1484 | } |
| 1485 | |
Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 1486 | for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp); |
| 1487 | fragp = &frag->next) { |
| 1488 | int end = offset + frag->len; |
| 1489 | |
| 1490 | if (skb_shared(frag)) { |
| 1491 | struct sk_buff *nfrag; |
| 1492 | |
| 1493 | nfrag = skb_clone(frag, GFP_ATOMIC); |
| 1494 | if (unlikely(!nfrag)) |
| 1495 | return -ENOMEM; |
| 1496 | |
| 1497 | nfrag->next = frag->next; |
Eric Dumazet | 85bb2a6 | 2012-04-19 02:24:53 +0000 | [diff] [blame] | 1498 | consume_skb(frag); |
Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 1499 | frag = nfrag; |
| 1500 | *fragp = frag; |
| 1501 | } |
| 1502 | |
| 1503 | if (end < len) { |
| 1504 | offset = end; |
| 1505 | continue; |
| 1506 | } |
| 1507 | |
| 1508 | if (end > len && |
| 1509 | unlikely((err = pskb_trim(frag, len - offset)))) |
| 1510 | return err; |
| 1511 | |
| 1512 | if (frag->next) |
| 1513 | skb_drop_list(&frag->next); |
| 1514 | break; |
| 1515 | } |
| 1516 | |
Herbert Xu | f4d26fb | 2006-07-30 20:20:28 -0700 | [diff] [blame] | 1517 | done: |
Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 1518 | if (len > skb_headlen(skb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1519 | skb->data_len -= skb->len - len; |
| 1520 | skb->len = len; |
| 1521 | } else { |
Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 1522 | skb->len = len; |
| 1523 | skb->data_len = 0; |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 1524 | skb_set_tail_pointer(skb, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1525 | } |
| 1526 | |
| 1527 | return 0; |
| 1528 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 1529 | EXPORT_SYMBOL(___pskb_trim); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1530 | |
| 1531 | /** |
| 1532 | * __pskb_pull_tail - advance tail of skb header |
| 1533 | * @skb: buffer to reallocate |
| 1534 | * @delta: number of bytes to advance tail |
| 1535 | * |
| 1536 | * The function makes a sense only on a fragmented &sk_buff, |
| 1537 | * it expands header moving its tail forward and copying necessary |
| 1538 | * data from fragmented part. |
| 1539 | * |
| 1540 | * &sk_buff MUST have reference count of 1. |
| 1541 | * |
| 1542 | * Returns %NULL (and &sk_buff does not change) if pull failed |
| 1543 | * or value of new tail of skb in the case of success. |
| 1544 | * |
| 1545 | * All the pointers pointing into skb header may change and must be |
| 1546 | * reloaded after call to this function. |
| 1547 | */ |
| 1548 | |
| 1549 | /* Moves tail of skb head forward, copying data from fragmented part, |
| 1550 | * when it is necessary. |
| 1551 | * 1. It may fail due to malloc failure. |
| 1552 | * 2. It may change skb pointers. |
| 1553 | * |
| 1554 | * It is pretty complicated. Luckily, it is called only in exceptional cases. |
| 1555 | */ |
| 1556 | unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta) |
| 1557 | { |
| 1558 | /* If skb has not enough free space at tail, get new one |
| 1559 | * plus 128 bytes for future expansions. If we have enough |
| 1560 | * room at tail, reallocate without expansion only if skb is cloned. |
| 1561 | */ |
Arnaldo Carvalho de Melo | 4305b54 | 2007-04-19 20:43:29 -0700 | [diff] [blame] | 1562 | int i, k, eat = (skb->tail + delta) - skb->end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1563 | |
| 1564 | if (eat > 0 || skb_cloned(skb)) { |
| 1565 | if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0, |
| 1566 | GFP_ATOMIC)) |
| 1567 | return NULL; |
| 1568 | } |
| 1569 | |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 1570 | if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1571 | BUG(); |
| 1572 | |
| 1573 | /* Optimization: no fragments, no reasons to preestimate |
| 1574 | * size of pulled pages. Superb. |
| 1575 | */ |
David S. Miller | 21dc330 | 2010-08-23 00:13:46 -0700 | [diff] [blame] | 1576 | if (!skb_has_frag_list(skb)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1577 | goto pull_pages; |
| 1578 | |
| 1579 | /* Estimate size of pulled pages. */ |
| 1580 | eat = delta; |
| 1581 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 1582 | int size = skb_frag_size(&skb_shinfo(skb)->frags[i]); |
| 1583 | |
| 1584 | if (size >= eat) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1585 | goto pull_pages; |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 1586 | eat -= size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1587 | } |
| 1588 | |
| 1589 | /* If we need update frag list, we are in troubles. |
| 1590 | * Certainly, it possible to add an offset to skb data, |
| 1591 | * but taking into account that pulling is expected to |
| 1592 | * be very rare operation, it is worth to fight against |
| 1593 | * further bloating skb head and crucify ourselves here instead. |
| 1594 | * Pure masohism, indeed. 8)8) |
| 1595 | */ |
| 1596 | if (eat) { |
| 1597 | struct sk_buff *list = skb_shinfo(skb)->frag_list; |
| 1598 | struct sk_buff *clone = NULL; |
| 1599 | struct sk_buff *insp = NULL; |
| 1600 | |
| 1601 | do { |
Kris Katterjohn | 09a6266 | 2006-01-08 22:24:28 -0800 | [diff] [blame] | 1602 | BUG_ON(!list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1603 | |
| 1604 | if (list->len <= eat) { |
| 1605 | /* Eaten as whole. */ |
| 1606 | eat -= list->len; |
| 1607 | list = list->next; |
| 1608 | insp = list; |
| 1609 | } else { |
| 1610 | /* Eaten partially. */ |
| 1611 | |
| 1612 | if (skb_shared(list)) { |
| 1613 | /* Sucks! We need to fork list. :-( */ |
| 1614 | clone = skb_clone(list, GFP_ATOMIC); |
| 1615 | if (!clone) |
| 1616 | return NULL; |
| 1617 | insp = list->next; |
| 1618 | list = clone; |
| 1619 | } else { |
| 1620 | /* This may be pulled without |
| 1621 | * problems. */ |
| 1622 | insp = list; |
| 1623 | } |
| 1624 | if (!pskb_pull(list, eat)) { |
Wei Yongjun | f3fbbe0 | 2009-02-25 00:37:32 +0000 | [diff] [blame] | 1625 | kfree_skb(clone); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1626 | return NULL; |
| 1627 | } |
| 1628 | break; |
| 1629 | } |
| 1630 | } while (eat); |
| 1631 | |
| 1632 | /* Free pulled out fragments. */ |
| 1633 | while ((list = skb_shinfo(skb)->frag_list) != insp) { |
| 1634 | skb_shinfo(skb)->frag_list = list->next; |
| 1635 | kfree_skb(list); |
| 1636 | } |
| 1637 | /* And insert new clone at head. */ |
| 1638 | if (clone) { |
| 1639 | clone->next = list; |
| 1640 | skb_shinfo(skb)->frag_list = clone; |
| 1641 | } |
| 1642 | } |
| 1643 | /* Success! Now we may commit changes to skb data. */ |
| 1644 | |
| 1645 | pull_pages: |
| 1646 | eat = delta; |
| 1647 | k = 0; |
| 1648 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 1649 | int size = skb_frag_size(&skb_shinfo(skb)->frags[i]); |
| 1650 | |
| 1651 | if (size <= eat) { |
Ian Campbell | ea2ab69 | 2011-08-22 23:44:58 +0000 | [diff] [blame] | 1652 | skb_frag_unref(skb, i); |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 1653 | eat -= size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1654 | } else { |
| 1655 | skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i]; |
| 1656 | if (eat) { |
| 1657 | skb_shinfo(skb)->frags[k].page_offset += eat; |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 1658 | skb_frag_size_sub(&skb_shinfo(skb)->frags[k], eat); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1659 | eat = 0; |
| 1660 | } |
| 1661 | k++; |
| 1662 | } |
| 1663 | } |
| 1664 | skb_shinfo(skb)->nr_frags = k; |
| 1665 | |
| 1666 | skb->tail += delta; |
| 1667 | skb->data_len -= delta; |
| 1668 | |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 1669 | return skb_tail_pointer(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1670 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 1671 | EXPORT_SYMBOL(__pskb_pull_tail); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1672 | |
Eric Dumazet | 22019b1 | 2011-07-29 18:37:31 +0000 | [diff] [blame] | 1673 | /** |
| 1674 | * skb_copy_bits - copy bits from skb to kernel buffer |
| 1675 | * @skb: source skb |
| 1676 | * @offset: offset in source |
| 1677 | * @to: destination buffer |
| 1678 | * @len: number of bytes to copy |
| 1679 | * |
| 1680 | * Copy the specified number of bytes from the source skb to the |
| 1681 | * destination buffer. |
| 1682 | * |
| 1683 | * CAUTION ! : |
| 1684 | * If its prototype is ever changed, |
| 1685 | * check arch/{*}/net/{*}.S files, |
| 1686 | * since it is called from BPF assembly code. |
| 1687 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1688 | int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len) |
| 1689 | { |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 1690 | int start = skb_headlen(skb); |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 1691 | struct sk_buff *frag_iter; |
| 1692 | int i, copy; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1693 | |
| 1694 | if (offset > (int)skb->len - len) |
| 1695 | goto fault; |
| 1696 | |
| 1697 | /* Copy header. */ |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 1698 | if ((copy = start - offset) > 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1699 | if (copy > len) |
| 1700 | copy = len; |
Arnaldo Carvalho de Melo | d626f62 | 2007-03-27 18:55:52 -0300 | [diff] [blame] | 1701 | skb_copy_from_linear_data_offset(skb, offset, to, copy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1702 | if ((len -= copy) == 0) |
| 1703 | return 0; |
| 1704 | offset += copy; |
| 1705 | to += copy; |
| 1706 | } |
| 1707 | |
| 1708 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 1709 | int end; |
Eric Dumazet | 51c56b0 | 2012-04-05 11:35:15 +0200 | [diff] [blame] | 1710 | skb_frag_t *f = &skb_shinfo(skb)->frags[i]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1711 | |
Ilpo Järvinen | 547b792 | 2008-07-25 21:43:18 -0700 | [diff] [blame] | 1712 | WARN_ON(start > offset + len); |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 1713 | |
Eric Dumazet | 51c56b0 | 2012-04-05 11:35:15 +0200 | [diff] [blame] | 1714 | end = start + skb_frag_size(f); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1715 | if ((copy = end - offset) > 0) { |
| 1716 | u8 *vaddr; |
| 1717 | |
| 1718 | if (copy > len) |
| 1719 | copy = len; |
| 1720 | |
Eric Dumazet | 51c56b0 | 2012-04-05 11:35:15 +0200 | [diff] [blame] | 1721 | vaddr = kmap_atomic(skb_frag_page(f)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1722 | memcpy(to, |
Eric Dumazet | 51c56b0 | 2012-04-05 11:35:15 +0200 | [diff] [blame] | 1723 | vaddr + f->page_offset + offset - start, |
| 1724 | copy); |
| 1725 | kunmap_atomic(vaddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1726 | |
| 1727 | if ((len -= copy) == 0) |
| 1728 | return 0; |
| 1729 | offset += copy; |
| 1730 | to += copy; |
| 1731 | } |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 1732 | start = end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1733 | } |
| 1734 | |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 1735 | skb_walk_frags(skb, frag_iter) { |
| 1736 | int end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1737 | |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 1738 | WARN_ON(start > offset + len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1739 | |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 1740 | end = start + frag_iter->len; |
| 1741 | if ((copy = end - offset) > 0) { |
| 1742 | if (copy > len) |
| 1743 | copy = len; |
| 1744 | if (skb_copy_bits(frag_iter, offset - start, to, copy)) |
| 1745 | goto fault; |
| 1746 | if ((len -= copy) == 0) |
| 1747 | return 0; |
| 1748 | offset += copy; |
| 1749 | to += copy; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1750 | } |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 1751 | start = end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1752 | } |
Shirley Ma | a6686f2 | 2011-07-06 12:22:12 +0000 | [diff] [blame] | 1753 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1754 | if (!len) |
| 1755 | return 0; |
| 1756 | |
| 1757 | fault: |
| 1758 | return -EFAULT; |
| 1759 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 1760 | EXPORT_SYMBOL(skb_copy_bits); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1761 | |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 1762 | /* |
| 1763 | * Callback from splice_to_pipe(), if we need to release some pages |
| 1764 | * at the end of the spd in case we error'ed out in filling the pipe. |
| 1765 | */ |
| 1766 | static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i) |
| 1767 | { |
Jarek Poplawski | 8b9d372 | 2009-01-19 17:03:56 -0800 | [diff] [blame] | 1768 | put_page(spd->pages[i]); |
| 1769 | } |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 1770 | |
David S. Miller | a108d5f | 2012-04-23 23:06:11 -0400 | [diff] [blame] | 1771 | static struct page *linear_to_page(struct page *page, unsigned int *len, |
| 1772 | unsigned int *offset, |
Eric Dumazet | 18aafc6 | 2013-01-11 14:46:37 +0000 | [diff] [blame] | 1773 | struct sock *sk) |
Jarek Poplawski | 8b9d372 | 2009-01-19 17:03:56 -0800 | [diff] [blame] | 1774 | { |
Eric Dumazet | 5640f76 | 2012-09-23 23:04:42 +0000 | [diff] [blame] | 1775 | struct page_frag *pfrag = sk_page_frag(sk); |
Jarek Poplawski | 8b9d372 | 2009-01-19 17:03:56 -0800 | [diff] [blame] | 1776 | |
Eric Dumazet | 5640f76 | 2012-09-23 23:04:42 +0000 | [diff] [blame] | 1777 | if (!sk_page_frag_refill(sk, pfrag)) |
| 1778 | return NULL; |
Jarek Poplawski | 4fb6699 | 2009-02-01 00:41:42 -0800 | [diff] [blame] | 1779 | |
Eric Dumazet | 5640f76 | 2012-09-23 23:04:42 +0000 | [diff] [blame] | 1780 | *len = min_t(unsigned int, *len, pfrag->size - pfrag->offset); |
Jarek Poplawski | 4fb6699 | 2009-02-01 00:41:42 -0800 | [diff] [blame] | 1781 | |
Eric Dumazet | 5640f76 | 2012-09-23 23:04:42 +0000 | [diff] [blame] | 1782 | memcpy(page_address(pfrag->page) + pfrag->offset, |
| 1783 | page_address(page) + *offset, *len); |
| 1784 | *offset = pfrag->offset; |
| 1785 | pfrag->offset += *len; |
Jarek Poplawski | 4fb6699 | 2009-02-01 00:41:42 -0800 | [diff] [blame] | 1786 | |
Eric Dumazet | 5640f76 | 2012-09-23 23:04:42 +0000 | [diff] [blame] | 1787 | return pfrag->page; |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 1788 | } |
| 1789 | |
Eric Dumazet | 41c73a0 | 2012-04-22 12:26:16 +0000 | [diff] [blame] | 1790 | static bool spd_can_coalesce(const struct splice_pipe_desc *spd, |
| 1791 | struct page *page, |
| 1792 | unsigned int offset) |
| 1793 | { |
| 1794 | return spd->nr_pages && |
| 1795 | spd->pages[spd->nr_pages - 1] == page && |
| 1796 | (spd->partial[spd->nr_pages - 1].offset + |
| 1797 | spd->partial[spd->nr_pages - 1].len == offset); |
| 1798 | } |
| 1799 | |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 1800 | /* |
| 1801 | * Fill page/offset/length into spd, if it can hold more pages. |
| 1802 | */ |
David S. Miller | a108d5f | 2012-04-23 23:06:11 -0400 | [diff] [blame] | 1803 | static bool spd_fill_page(struct splice_pipe_desc *spd, |
| 1804 | struct pipe_inode_info *pipe, struct page *page, |
| 1805 | unsigned int *len, unsigned int offset, |
Eric Dumazet | 18aafc6 | 2013-01-11 14:46:37 +0000 | [diff] [blame] | 1806 | bool linear, |
David S. Miller | a108d5f | 2012-04-23 23:06:11 -0400 | [diff] [blame] | 1807 | struct sock *sk) |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 1808 | { |
Eric Dumazet | 41c73a0 | 2012-04-22 12:26:16 +0000 | [diff] [blame] | 1809 | if (unlikely(spd->nr_pages == MAX_SKB_FRAGS)) |
David S. Miller | a108d5f | 2012-04-23 23:06:11 -0400 | [diff] [blame] | 1810 | return true; |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 1811 | |
Jarek Poplawski | 8b9d372 | 2009-01-19 17:03:56 -0800 | [diff] [blame] | 1812 | if (linear) { |
Eric Dumazet | 18aafc6 | 2013-01-11 14:46:37 +0000 | [diff] [blame] | 1813 | page = linear_to_page(page, len, &offset, sk); |
Jarek Poplawski | 8b9d372 | 2009-01-19 17:03:56 -0800 | [diff] [blame] | 1814 | if (!page) |
David S. Miller | a108d5f | 2012-04-23 23:06:11 -0400 | [diff] [blame] | 1815 | return true; |
Eric Dumazet | 41c73a0 | 2012-04-22 12:26:16 +0000 | [diff] [blame] | 1816 | } |
| 1817 | if (spd_can_coalesce(spd, page, offset)) { |
| 1818 | spd->partial[spd->nr_pages - 1].len += *len; |
David S. Miller | a108d5f | 2012-04-23 23:06:11 -0400 | [diff] [blame] | 1819 | return false; |
Eric Dumazet | 41c73a0 | 2012-04-22 12:26:16 +0000 | [diff] [blame] | 1820 | } |
| 1821 | get_page(page); |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 1822 | spd->pages[spd->nr_pages] = page; |
Jarek Poplawski | 4fb6699 | 2009-02-01 00:41:42 -0800 | [diff] [blame] | 1823 | spd->partial[spd->nr_pages].len = *len; |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 1824 | spd->partial[spd->nr_pages].offset = offset; |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 1825 | spd->nr_pages++; |
Jarek Poplawski | 8b9d372 | 2009-01-19 17:03:56 -0800 | [diff] [blame] | 1826 | |
David S. Miller | a108d5f | 2012-04-23 23:06:11 -0400 | [diff] [blame] | 1827 | return false; |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 1828 | } |
| 1829 | |
David S. Miller | a108d5f | 2012-04-23 23:06:11 -0400 | [diff] [blame] | 1830 | static bool __splice_segment(struct page *page, unsigned int poff, |
| 1831 | unsigned int plen, unsigned int *off, |
Eric Dumazet | 18aafc6 | 2013-01-11 14:46:37 +0000 | [diff] [blame] | 1832 | unsigned int *len, |
Eric Dumazet | d7ccf7c | 2012-04-23 23:35:04 -0400 | [diff] [blame] | 1833 | struct splice_pipe_desc *spd, bool linear, |
David S. Miller | a108d5f | 2012-04-23 23:06:11 -0400 | [diff] [blame] | 1834 | struct sock *sk, |
| 1835 | struct pipe_inode_info *pipe) |
Octavian Purdila | 2870c43 | 2008-07-15 00:49:11 -0700 | [diff] [blame] | 1836 | { |
| 1837 | if (!*len) |
David S. Miller | a108d5f | 2012-04-23 23:06:11 -0400 | [diff] [blame] | 1838 | return true; |
Octavian Purdila | 2870c43 | 2008-07-15 00:49:11 -0700 | [diff] [blame] | 1839 | |
| 1840 | /* skip this segment if already processed */ |
| 1841 | if (*off >= plen) { |
| 1842 | *off -= plen; |
David S. Miller | a108d5f | 2012-04-23 23:06:11 -0400 | [diff] [blame] | 1843 | return false; |
Octavian Purdila | db43a28 | 2008-06-27 17:27:21 -0700 | [diff] [blame] | 1844 | } |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 1845 | |
Octavian Purdila | 2870c43 | 2008-07-15 00:49:11 -0700 | [diff] [blame] | 1846 | /* ignore any bits we already processed */ |
Eric Dumazet | 9ca1b22 | 2013-01-05 21:31:18 +0000 | [diff] [blame] | 1847 | poff += *off; |
| 1848 | plen -= *off; |
| 1849 | *off = 0; |
Octavian Purdila | 2870c43 | 2008-07-15 00:49:11 -0700 | [diff] [blame] | 1850 | |
Eric Dumazet | 18aafc6 | 2013-01-11 14:46:37 +0000 | [diff] [blame] | 1851 | do { |
| 1852 | unsigned int flen = min(*len, plen); |
Octavian Purdila | 2870c43 | 2008-07-15 00:49:11 -0700 | [diff] [blame] | 1853 | |
Eric Dumazet | 18aafc6 | 2013-01-11 14:46:37 +0000 | [diff] [blame] | 1854 | if (spd_fill_page(spd, pipe, page, &flen, poff, |
| 1855 | linear, sk)) |
| 1856 | return true; |
| 1857 | poff += flen; |
| 1858 | plen -= flen; |
| 1859 | *len -= flen; |
| 1860 | } while (*len && plen); |
Octavian Purdila | 2870c43 | 2008-07-15 00:49:11 -0700 | [diff] [blame] | 1861 | |
David S. Miller | a108d5f | 2012-04-23 23:06:11 -0400 | [diff] [blame] | 1862 | return false; |
Octavian Purdila | 2870c43 | 2008-07-15 00:49:11 -0700 | [diff] [blame] | 1863 | } |
| 1864 | |
| 1865 | /* |
David S. Miller | a108d5f | 2012-04-23 23:06:11 -0400 | [diff] [blame] | 1866 | * Map linear and fragment data from the skb to spd. It reports true if the |
Octavian Purdila | 2870c43 | 2008-07-15 00:49:11 -0700 | [diff] [blame] | 1867 | * pipe is full or if we already spliced the requested length. |
| 1868 | */ |
David S. Miller | a108d5f | 2012-04-23 23:06:11 -0400 | [diff] [blame] | 1869 | static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe, |
| 1870 | unsigned int *offset, unsigned int *len, |
| 1871 | struct splice_pipe_desc *spd, struct sock *sk) |
Octavian Purdila | 2870c43 | 2008-07-15 00:49:11 -0700 | [diff] [blame] | 1872 | { |
| 1873 | int seg; |
| 1874 | |
Eric Dumazet | 1d0c0b3 | 2012-04-27 02:10:03 +0000 | [diff] [blame] | 1875 | /* map the linear part : |
Alexander Duyck | 2996d31 | 2012-05-02 18:18:42 +0000 | [diff] [blame] | 1876 | * If skb->head_frag is set, this 'linear' part is backed by a |
| 1877 | * fragment, and if the head is not shared with any clones then |
| 1878 | * we can avoid a copy since we own the head portion of this page. |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 1879 | */ |
Octavian Purdila | 2870c43 | 2008-07-15 00:49:11 -0700 | [diff] [blame] | 1880 | if (__splice_segment(virt_to_page(skb->data), |
| 1881 | (unsigned long) skb->data & (PAGE_SIZE - 1), |
| 1882 | skb_headlen(skb), |
Eric Dumazet | 18aafc6 | 2013-01-11 14:46:37 +0000 | [diff] [blame] | 1883 | offset, len, spd, |
Alexander Duyck | 3a7c1ee4 | 2012-05-03 01:09:42 +0000 | [diff] [blame] | 1884 | skb_head_is_locked(skb), |
Eric Dumazet | 1d0c0b3 | 2012-04-27 02:10:03 +0000 | [diff] [blame] | 1885 | sk, pipe)) |
David S. Miller | a108d5f | 2012-04-23 23:06:11 -0400 | [diff] [blame] | 1886 | return true; |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 1887 | |
| 1888 | /* |
| 1889 | * then map the fragments |
| 1890 | */ |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 1891 | for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) { |
| 1892 | const skb_frag_t *f = &skb_shinfo(skb)->frags[seg]; |
| 1893 | |
Ian Campbell | ea2ab69 | 2011-08-22 23:44:58 +0000 | [diff] [blame] | 1894 | if (__splice_segment(skb_frag_page(f), |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 1895 | f->page_offset, skb_frag_size(f), |
Eric Dumazet | 18aafc6 | 2013-01-11 14:46:37 +0000 | [diff] [blame] | 1896 | offset, len, spd, false, sk, pipe)) |
David S. Miller | a108d5f | 2012-04-23 23:06:11 -0400 | [diff] [blame] | 1897 | return true; |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 1898 | } |
| 1899 | |
David S. Miller | a108d5f | 2012-04-23 23:06:11 -0400 | [diff] [blame] | 1900 | return false; |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 1901 | } |
| 1902 | |
| 1903 | /* |
| 1904 | * Map data from the skb to a pipe. Should handle both the linear part, |
| 1905 | * the fragments, and the frag list. It does NOT handle frag lists within |
| 1906 | * the frag list, if such a thing exists. We'd probably need to recurse to |
| 1907 | * handle that cleanly. |
| 1908 | */ |
Jarek Poplawski | 8b9d372 | 2009-01-19 17:03:56 -0800 | [diff] [blame] | 1909 | int skb_splice_bits(struct sk_buff *skb, unsigned int offset, |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 1910 | struct pipe_inode_info *pipe, unsigned int tlen, |
| 1911 | unsigned int flags) |
| 1912 | { |
Eric Dumazet | 41c73a0 | 2012-04-22 12:26:16 +0000 | [diff] [blame] | 1913 | struct partial_page partial[MAX_SKB_FRAGS]; |
| 1914 | struct page *pages[MAX_SKB_FRAGS]; |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 1915 | struct splice_pipe_desc spd = { |
| 1916 | .pages = pages, |
| 1917 | .partial = partial, |
Eric Dumazet | 047fe36 | 2012-06-12 15:24:40 +0200 | [diff] [blame] | 1918 | .nr_pages_max = MAX_SKB_FRAGS, |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 1919 | .flags = flags, |
Miklos Szeredi | 28a625c | 2014-01-22 19:36:57 +0100 | [diff] [blame] | 1920 | .ops = &nosteal_pipe_buf_ops, |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 1921 | .spd_release = sock_spd_release, |
| 1922 | }; |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 1923 | struct sk_buff *frag_iter; |
Jarek Poplawski | 7a67e56 | 2009-04-30 05:41:19 -0700 | [diff] [blame] | 1924 | struct sock *sk = skb->sk; |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 1925 | int ret = 0; |
| 1926 | |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 1927 | /* |
| 1928 | * __skb_splice_bits() only fails if the output has no room left, |
| 1929 | * so no point in going over the frag_list for the error case. |
| 1930 | */ |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 1931 | if (__skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk)) |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 1932 | goto done; |
| 1933 | else if (!tlen) |
| 1934 | goto done; |
| 1935 | |
| 1936 | /* |
| 1937 | * now see if we have a frag_list to map |
| 1938 | */ |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 1939 | skb_walk_frags(skb, frag_iter) { |
| 1940 | if (!tlen) |
| 1941 | break; |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 1942 | if (__skb_splice_bits(frag_iter, pipe, &offset, &tlen, &spd, sk)) |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 1943 | break; |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 1944 | } |
| 1945 | |
| 1946 | done: |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 1947 | if (spd.nr_pages) { |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 1948 | /* |
| 1949 | * Drop the socket lock, otherwise we have reverse |
| 1950 | * locking dependencies between sk_lock and i_mutex |
| 1951 | * here as compared to sendfile(). We enter here |
| 1952 | * with the socket lock held, and splice_to_pipe() will |
| 1953 | * grab the pipe inode lock. For sendfile() emulation, |
| 1954 | * we call into ->sendpage() with the i_mutex lock held |
| 1955 | * and networking will grab the socket lock. |
| 1956 | */ |
Octavian Purdila | 293ad60 | 2008-06-04 15:45:58 -0700 | [diff] [blame] | 1957 | release_sock(sk); |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 1958 | ret = splice_to_pipe(pipe, &spd); |
Octavian Purdila | 293ad60 | 2008-06-04 15:45:58 -0700 | [diff] [blame] | 1959 | lock_sock(sk); |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 1960 | } |
| 1961 | |
Jens Axboe | 35f3d14 | 2010-05-20 10:43:18 +0200 | [diff] [blame] | 1962 | return ret; |
Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 1963 | } |
| 1964 | |
Herbert Xu | 357b40a | 2005-04-19 22:30:14 -0700 | [diff] [blame] | 1965 | /** |
| 1966 | * skb_store_bits - store bits from kernel buffer to skb |
| 1967 | * @skb: destination buffer |
| 1968 | * @offset: offset in destination |
| 1969 | * @from: source buffer |
| 1970 | * @len: number of bytes to copy |
| 1971 | * |
| 1972 | * Copy the specified number of bytes from the source buffer to the |
| 1973 | * destination skb. This function handles all the messy bits of |
| 1974 | * traversing fragment lists and such. |
| 1975 | */ |
| 1976 | |
Stephen Hemminger | 0c6fcc8 | 2007-04-20 16:40:01 -0700 | [diff] [blame] | 1977 | int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len) |
Herbert Xu | 357b40a | 2005-04-19 22:30:14 -0700 | [diff] [blame] | 1978 | { |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 1979 | int start = skb_headlen(skb); |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 1980 | struct sk_buff *frag_iter; |
| 1981 | int i, copy; |
Herbert Xu | 357b40a | 2005-04-19 22:30:14 -0700 | [diff] [blame] | 1982 | |
| 1983 | if (offset > (int)skb->len - len) |
| 1984 | goto fault; |
| 1985 | |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 1986 | if ((copy = start - offset) > 0) { |
Herbert Xu | 357b40a | 2005-04-19 22:30:14 -0700 | [diff] [blame] | 1987 | if (copy > len) |
| 1988 | copy = len; |
Arnaldo Carvalho de Melo | 27d7ff4 | 2007-03-31 11:55:19 -0300 | [diff] [blame] | 1989 | skb_copy_to_linear_data_offset(skb, offset, from, copy); |
Herbert Xu | 357b40a | 2005-04-19 22:30:14 -0700 | [diff] [blame] | 1990 | if ((len -= copy) == 0) |
| 1991 | return 0; |
| 1992 | offset += copy; |
| 1993 | from += copy; |
| 1994 | } |
| 1995 | |
| 1996 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
| 1997 | skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 1998 | int end; |
Herbert Xu | 357b40a | 2005-04-19 22:30:14 -0700 | [diff] [blame] | 1999 | |
Ilpo Järvinen | 547b792 | 2008-07-25 21:43:18 -0700 | [diff] [blame] | 2000 | WARN_ON(start > offset + len); |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 2001 | |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 2002 | end = start + skb_frag_size(frag); |
Herbert Xu | 357b40a | 2005-04-19 22:30:14 -0700 | [diff] [blame] | 2003 | if ((copy = end - offset) > 0) { |
| 2004 | u8 *vaddr; |
| 2005 | |
| 2006 | if (copy > len) |
| 2007 | copy = len; |
| 2008 | |
Eric Dumazet | 51c56b0 | 2012-04-05 11:35:15 +0200 | [diff] [blame] | 2009 | vaddr = kmap_atomic(skb_frag_page(frag)); |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 2010 | memcpy(vaddr + frag->page_offset + offset - start, |
| 2011 | from, copy); |
Eric Dumazet | 51c56b0 | 2012-04-05 11:35:15 +0200 | [diff] [blame] | 2012 | kunmap_atomic(vaddr); |
Herbert Xu | 357b40a | 2005-04-19 22:30:14 -0700 | [diff] [blame] | 2013 | |
| 2014 | if ((len -= copy) == 0) |
| 2015 | return 0; |
| 2016 | offset += copy; |
| 2017 | from += copy; |
| 2018 | } |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 2019 | start = end; |
Herbert Xu | 357b40a | 2005-04-19 22:30:14 -0700 | [diff] [blame] | 2020 | } |
| 2021 | |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 2022 | skb_walk_frags(skb, frag_iter) { |
| 2023 | int end; |
Herbert Xu | 357b40a | 2005-04-19 22:30:14 -0700 | [diff] [blame] | 2024 | |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 2025 | WARN_ON(start > offset + len); |
Herbert Xu | 357b40a | 2005-04-19 22:30:14 -0700 | [diff] [blame] | 2026 | |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 2027 | end = start + frag_iter->len; |
| 2028 | if ((copy = end - offset) > 0) { |
| 2029 | if (copy > len) |
| 2030 | copy = len; |
| 2031 | if (skb_store_bits(frag_iter, offset - start, |
| 2032 | from, copy)) |
| 2033 | goto fault; |
| 2034 | if ((len -= copy) == 0) |
| 2035 | return 0; |
| 2036 | offset += copy; |
| 2037 | from += copy; |
Herbert Xu | 357b40a | 2005-04-19 22:30:14 -0700 | [diff] [blame] | 2038 | } |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 2039 | start = end; |
Herbert Xu | 357b40a | 2005-04-19 22:30:14 -0700 | [diff] [blame] | 2040 | } |
| 2041 | if (!len) |
| 2042 | return 0; |
| 2043 | |
| 2044 | fault: |
| 2045 | return -EFAULT; |
| 2046 | } |
Herbert Xu | 357b40a | 2005-04-19 22:30:14 -0700 | [diff] [blame] | 2047 | EXPORT_SYMBOL(skb_store_bits); |
| 2048 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2049 | /* Checksum skb data. */ |
Daniel Borkmann | 2817a33 | 2013-10-30 11:50:51 +0100 | [diff] [blame] | 2050 | __wsum __skb_checksum(const struct sk_buff *skb, int offset, int len, |
| 2051 | __wsum csum, const struct skb_checksum_ops *ops) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2052 | { |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 2053 | int start = skb_headlen(skb); |
| 2054 | int i, copy = start - offset; |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 2055 | struct sk_buff *frag_iter; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2056 | int pos = 0; |
| 2057 | |
| 2058 | /* Checksum header. */ |
| 2059 | if (copy > 0) { |
| 2060 | if (copy > len) |
| 2061 | copy = len; |
Daniel Borkmann | 2817a33 | 2013-10-30 11:50:51 +0100 | [diff] [blame] | 2062 | csum = ops->update(skb->data + offset, copy, csum); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2063 | if ((len -= copy) == 0) |
| 2064 | return csum; |
| 2065 | offset += copy; |
| 2066 | pos = copy; |
| 2067 | } |
| 2068 | |
| 2069 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 2070 | int end; |
Eric Dumazet | 51c56b0 | 2012-04-05 11:35:15 +0200 | [diff] [blame] | 2071 | skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2072 | |
Ilpo Järvinen | 547b792 | 2008-07-25 21:43:18 -0700 | [diff] [blame] | 2073 | WARN_ON(start > offset + len); |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 2074 | |
Eric Dumazet | 51c56b0 | 2012-04-05 11:35:15 +0200 | [diff] [blame] | 2075 | end = start + skb_frag_size(frag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2076 | if ((copy = end - offset) > 0) { |
Al Viro | 44bb936 | 2006-11-14 21:36:14 -0800 | [diff] [blame] | 2077 | __wsum csum2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2078 | u8 *vaddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2079 | |
| 2080 | if (copy > len) |
| 2081 | copy = len; |
Eric Dumazet | 51c56b0 | 2012-04-05 11:35:15 +0200 | [diff] [blame] | 2082 | vaddr = kmap_atomic(skb_frag_page(frag)); |
Daniel Borkmann | 2817a33 | 2013-10-30 11:50:51 +0100 | [diff] [blame] | 2083 | csum2 = ops->update(vaddr + frag->page_offset + |
| 2084 | offset - start, copy, 0); |
Eric Dumazet | 51c56b0 | 2012-04-05 11:35:15 +0200 | [diff] [blame] | 2085 | kunmap_atomic(vaddr); |
Daniel Borkmann | 2817a33 | 2013-10-30 11:50:51 +0100 | [diff] [blame] | 2086 | csum = ops->combine(csum, csum2, pos, copy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2087 | if (!(len -= copy)) |
| 2088 | return csum; |
| 2089 | offset += copy; |
| 2090 | pos += copy; |
| 2091 | } |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 2092 | start = end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2093 | } |
| 2094 | |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 2095 | skb_walk_frags(skb, frag_iter) { |
| 2096 | int end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2097 | |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 2098 | WARN_ON(start > offset + len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2099 | |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 2100 | end = start + frag_iter->len; |
| 2101 | if ((copy = end - offset) > 0) { |
| 2102 | __wsum csum2; |
| 2103 | if (copy > len) |
| 2104 | copy = len; |
Daniel Borkmann | 2817a33 | 2013-10-30 11:50:51 +0100 | [diff] [blame] | 2105 | csum2 = __skb_checksum(frag_iter, offset - start, |
| 2106 | copy, 0, ops); |
| 2107 | csum = ops->combine(csum, csum2, pos, copy); |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 2108 | if ((len -= copy) == 0) |
| 2109 | return csum; |
| 2110 | offset += copy; |
| 2111 | pos += copy; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2112 | } |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 2113 | start = end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2114 | } |
Kris Katterjohn | 09a6266 | 2006-01-08 22:24:28 -0800 | [diff] [blame] | 2115 | BUG_ON(len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2116 | |
| 2117 | return csum; |
| 2118 | } |
Daniel Borkmann | 2817a33 | 2013-10-30 11:50:51 +0100 | [diff] [blame] | 2119 | EXPORT_SYMBOL(__skb_checksum); |
| 2120 | |
| 2121 | __wsum skb_checksum(const struct sk_buff *skb, int offset, |
| 2122 | int len, __wsum csum) |
| 2123 | { |
| 2124 | const struct skb_checksum_ops ops = { |
Daniel Borkmann | cea80ea | 2013-11-04 17:10:25 +0100 | [diff] [blame] | 2125 | .update = csum_partial_ext, |
Daniel Borkmann | 2817a33 | 2013-10-30 11:50:51 +0100 | [diff] [blame] | 2126 | .combine = csum_block_add_ext, |
| 2127 | }; |
| 2128 | |
| 2129 | return __skb_checksum(skb, offset, len, csum, &ops); |
| 2130 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 2131 | EXPORT_SYMBOL(skb_checksum); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2132 | |
| 2133 | /* Both of above in one bottle. */ |
| 2134 | |
Al Viro | 81d7766 | 2006-11-14 21:37:33 -0800 | [diff] [blame] | 2135 | __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset, |
| 2136 | u8 *to, int len, __wsum csum) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2137 | { |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 2138 | int start = skb_headlen(skb); |
| 2139 | int i, copy = start - offset; |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 2140 | struct sk_buff *frag_iter; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2141 | int pos = 0; |
| 2142 | |
| 2143 | /* Copy header. */ |
| 2144 | if (copy > 0) { |
| 2145 | if (copy > len) |
| 2146 | copy = len; |
| 2147 | csum = csum_partial_copy_nocheck(skb->data + offset, to, |
| 2148 | copy, csum); |
| 2149 | if ((len -= copy) == 0) |
| 2150 | return csum; |
| 2151 | offset += copy; |
| 2152 | to += copy; |
| 2153 | pos = copy; |
| 2154 | } |
| 2155 | |
| 2156 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 2157 | int end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2158 | |
Ilpo Järvinen | 547b792 | 2008-07-25 21:43:18 -0700 | [diff] [blame] | 2159 | WARN_ON(start > offset + len); |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 2160 | |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 2161 | end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2162 | if ((copy = end - offset) > 0) { |
Al Viro | 5084205 | 2006-11-14 21:36:34 -0800 | [diff] [blame] | 2163 | __wsum csum2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2164 | u8 *vaddr; |
| 2165 | skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; |
| 2166 | |
| 2167 | if (copy > len) |
| 2168 | copy = len; |
Eric Dumazet | 51c56b0 | 2012-04-05 11:35:15 +0200 | [diff] [blame] | 2169 | vaddr = kmap_atomic(skb_frag_page(frag)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2170 | csum2 = csum_partial_copy_nocheck(vaddr + |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 2171 | frag->page_offset + |
| 2172 | offset - start, to, |
| 2173 | copy, 0); |
Eric Dumazet | 51c56b0 | 2012-04-05 11:35:15 +0200 | [diff] [blame] | 2174 | kunmap_atomic(vaddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2175 | csum = csum_block_add(csum, csum2, pos); |
| 2176 | if (!(len -= copy)) |
| 2177 | return csum; |
| 2178 | offset += copy; |
| 2179 | to += copy; |
| 2180 | pos += copy; |
| 2181 | } |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 2182 | start = end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2183 | } |
| 2184 | |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 2185 | skb_walk_frags(skb, frag_iter) { |
| 2186 | __wsum csum2; |
| 2187 | int end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2188 | |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 2189 | WARN_ON(start > offset + len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2190 | |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 2191 | end = start + frag_iter->len; |
| 2192 | if ((copy = end - offset) > 0) { |
| 2193 | if (copy > len) |
| 2194 | copy = len; |
| 2195 | csum2 = skb_copy_and_csum_bits(frag_iter, |
| 2196 | offset - start, |
| 2197 | to, copy, 0); |
| 2198 | csum = csum_block_add(csum, csum2, pos); |
| 2199 | if ((len -= copy) == 0) |
| 2200 | return csum; |
| 2201 | offset += copy; |
| 2202 | to += copy; |
| 2203 | pos += copy; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2204 | } |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 2205 | start = end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2206 | } |
Kris Katterjohn | 09a6266 | 2006-01-08 22:24:28 -0800 | [diff] [blame] | 2207 | BUG_ON(len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2208 | return csum; |
| 2209 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 2210 | EXPORT_SYMBOL(skb_copy_and_csum_bits); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2211 | |
Thomas Graf | af2806f | 2013-12-13 15:22:17 +0100 | [diff] [blame] | 2212 | /** |
| 2213 | * skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy() |
| 2214 | * @from: source buffer |
| 2215 | * |
| 2216 | * Calculates the amount of linear headroom needed in the 'to' skb passed |
| 2217 | * into skb_zerocopy(). |
| 2218 | */ |
| 2219 | unsigned int |
| 2220 | skb_zerocopy_headlen(const struct sk_buff *from) |
| 2221 | { |
| 2222 | unsigned int hlen = 0; |
| 2223 | |
| 2224 | if (!from->head_frag || |
| 2225 | skb_headlen(from) < L1_CACHE_BYTES || |
| 2226 | skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS) |
| 2227 | hlen = skb_headlen(from); |
| 2228 | |
| 2229 | if (skb_has_frag_list(from)) |
| 2230 | hlen = from->len; |
| 2231 | |
| 2232 | return hlen; |
| 2233 | } |
| 2234 | EXPORT_SYMBOL_GPL(skb_zerocopy_headlen); |
| 2235 | |
| 2236 | /** |
| 2237 | * skb_zerocopy - Zero copy skb to skb |
| 2238 | * @to: destination buffer |
Masanari Iida | 7fceb4d | 2014-01-29 01:05:28 +0900 | [diff] [blame] | 2239 | * @from: source buffer |
Thomas Graf | af2806f | 2013-12-13 15:22:17 +0100 | [diff] [blame] | 2240 | * @len: number of bytes to copy from source buffer |
| 2241 | * @hlen: size of linear headroom in destination buffer |
| 2242 | * |
| 2243 | * Copies up to `len` bytes from `from` to `to` by creating references |
| 2244 | * to the frags in the source buffer. |
| 2245 | * |
| 2246 | * The `hlen` as calculated by skb_zerocopy_headlen() specifies the |
| 2247 | * headroom in the `to` buffer. |
Zoltan Kiss | 36d5fe6 | 2014-03-26 22:37:45 +0000 | [diff] [blame] | 2248 | * |
| 2249 | * Return value: |
| 2250 | * 0: everything is OK |
| 2251 | * -ENOMEM: couldn't orphan frags of @from due to lack of memory |
| 2252 | * -EFAULT: skb_copy_bits() found some problem with skb geometry |
Thomas Graf | af2806f | 2013-12-13 15:22:17 +0100 | [diff] [blame] | 2253 | */ |
Zoltan Kiss | 36d5fe6 | 2014-03-26 22:37:45 +0000 | [diff] [blame] | 2254 | int |
| 2255 | skb_zerocopy(struct sk_buff *to, struct sk_buff *from, int len, int hlen) |
Thomas Graf | af2806f | 2013-12-13 15:22:17 +0100 | [diff] [blame] | 2256 | { |
| 2257 | int i, j = 0; |
| 2258 | int plen = 0; /* length of skb->head fragment */ |
Zoltan Kiss | 36d5fe6 | 2014-03-26 22:37:45 +0000 | [diff] [blame] | 2259 | int ret; |
Thomas Graf | af2806f | 2013-12-13 15:22:17 +0100 | [diff] [blame] | 2260 | struct page *page; |
| 2261 | unsigned int offset; |
| 2262 | |
| 2263 | BUG_ON(!from->head_frag && !hlen); |
| 2264 | |
| 2265 | /* dont bother with small payloads */ |
Zoltan Kiss | 36d5fe6 | 2014-03-26 22:37:45 +0000 | [diff] [blame] | 2266 | if (len <= skb_tailroom(to)) |
| 2267 | return skb_copy_bits(from, 0, skb_put(to, len), len); |
Thomas Graf | af2806f | 2013-12-13 15:22:17 +0100 | [diff] [blame] | 2268 | |
| 2269 | if (hlen) { |
Zoltan Kiss | 36d5fe6 | 2014-03-26 22:37:45 +0000 | [diff] [blame] | 2270 | ret = skb_copy_bits(from, 0, skb_put(to, hlen), hlen); |
| 2271 | if (unlikely(ret)) |
| 2272 | return ret; |
Thomas Graf | af2806f | 2013-12-13 15:22:17 +0100 | [diff] [blame] | 2273 | len -= hlen; |
| 2274 | } else { |
| 2275 | plen = min_t(int, skb_headlen(from), len); |
| 2276 | if (plen) { |
| 2277 | page = virt_to_head_page(from->head); |
| 2278 | offset = from->data - (unsigned char *)page_address(page); |
| 2279 | __skb_fill_page_desc(to, 0, page, offset, plen); |
| 2280 | get_page(page); |
| 2281 | j = 1; |
| 2282 | len -= plen; |
| 2283 | } |
| 2284 | } |
| 2285 | |
| 2286 | to->truesize += len + plen; |
| 2287 | to->len += len + plen; |
| 2288 | to->data_len += len + plen; |
| 2289 | |
Zoltan Kiss | 36d5fe6 | 2014-03-26 22:37:45 +0000 | [diff] [blame] | 2290 | if (unlikely(skb_orphan_frags(from, GFP_ATOMIC))) { |
| 2291 | skb_tx_error(from); |
| 2292 | return -ENOMEM; |
| 2293 | } |
| 2294 | |
Thomas Graf | af2806f | 2013-12-13 15:22:17 +0100 | [diff] [blame] | 2295 | for (i = 0; i < skb_shinfo(from)->nr_frags; i++) { |
| 2296 | if (!len) |
| 2297 | break; |
| 2298 | skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i]; |
| 2299 | skb_shinfo(to)->frags[j].size = min_t(int, skb_shinfo(to)->frags[j].size, len); |
| 2300 | len -= skb_shinfo(to)->frags[j].size; |
| 2301 | skb_frag_ref(to, j); |
| 2302 | j++; |
| 2303 | } |
| 2304 | skb_shinfo(to)->nr_frags = j; |
Zoltan Kiss | 36d5fe6 | 2014-03-26 22:37:45 +0000 | [diff] [blame] | 2305 | |
| 2306 | return 0; |
Thomas Graf | af2806f | 2013-12-13 15:22:17 +0100 | [diff] [blame] | 2307 | } |
| 2308 | EXPORT_SYMBOL_GPL(skb_zerocopy); |
| 2309 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2310 | void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to) |
| 2311 | { |
Al Viro | d3bc23e | 2006-11-14 21:24:49 -0800 | [diff] [blame] | 2312 | __wsum csum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2313 | long csstart; |
| 2314 | |
Patrick McHardy | 84fa793 | 2006-08-29 16:44:56 -0700 | [diff] [blame] | 2315 | if (skb->ip_summed == CHECKSUM_PARTIAL) |
Michał Mirosław | 55508d6 | 2010-12-14 15:24:08 +0000 | [diff] [blame] | 2316 | csstart = skb_checksum_start_offset(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2317 | else |
| 2318 | csstart = skb_headlen(skb); |
| 2319 | |
Kris Katterjohn | 09a6266 | 2006-01-08 22:24:28 -0800 | [diff] [blame] | 2320 | BUG_ON(csstart > skb_headlen(skb)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2321 | |
Arnaldo Carvalho de Melo | d626f62 | 2007-03-27 18:55:52 -0300 | [diff] [blame] | 2322 | skb_copy_from_linear_data(skb, to, csstart); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2323 | |
| 2324 | csum = 0; |
| 2325 | if (csstart != skb->len) |
| 2326 | csum = skb_copy_and_csum_bits(skb, csstart, to + csstart, |
| 2327 | skb->len - csstart, 0); |
| 2328 | |
Patrick McHardy | 84fa793 | 2006-08-29 16:44:56 -0700 | [diff] [blame] | 2329 | if (skb->ip_summed == CHECKSUM_PARTIAL) { |
Al Viro | ff1dcad | 2006-11-20 18:07:29 -0800 | [diff] [blame] | 2330 | long csstuff = csstart + skb->csum_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2331 | |
Al Viro | d3bc23e | 2006-11-14 21:24:49 -0800 | [diff] [blame] | 2332 | *((__sum16 *)(to + csstuff)) = csum_fold(csum); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2333 | } |
| 2334 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 2335 | EXPORT_SYMBOL(skb_copy_and_csum_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2336 | |
| 2337 | /** |
| 2338 | * skb_dequeue - remove from the head of the queue |
| 2339 | * @list: list to dequeue from |
| 2340 | * |
| 2341 | * Remove the head of the list. The list lock is taken so the function |
| 2342 | * may be used safely with other locking list functions. The head item is |
| 2343 | * returned or %NULL if the list is empty. |
| 2344 | */ |
| 2345 | |
| 2346 | struct sk_buff *skb_dequeue(struct sk_buff_head *list) |
| 2347 | { |
| 2348 | unsigned long flags; |
| 2349 | struct sk_buff *result; |
| 2350 | |
| 2351 | spin_lock_irqsave(&list->lock, flags); |
| 2352 | result = __skb_dequeue(list); |
| 2353 | spin_unlock_irqrestore(&list->lock, flags); |
| 2354 | return result; |
| 2355 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 2356 | EXPORT_SYMBOL(skb_dequeue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2357 | |
| 2358 | /** |
| 2359 | * skb_dequeue_tail - remove from the tail of the queue |
| 2360 | * @list: list to dequeue from |
| 2361 | * |
| 2362 | * Remove the tail of the list. The list lock is taken so the function |
| 2363 | * may be used safely with other locking list functions. The tail item is |
| 2364 | * returned or %NULL if the list is empty. |
| 2365 | */ |
| 2366 | struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list) |
| 2367 | { |
| 2368 | unsigned long flags; |
| 2369 | struct sk_buff *result; |
| 2370 | |
| 2371 | spin_lock_irqsave(&list->lock, flags); |
| 2372 | result = __skb_dequeue_tail(list); |
| 2373 | spin_unlock_irqrestore(&list->lock, flags); |
| 2374 | return result; |
| 2375 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 2376 | EXPORT_SYMBOL(skb_dequeue_tail); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2377 | |
| 2378 | /** |
| 2379 | * skb_queue_purge - empty a list |
| 2380 | * @list: list to empty |
| 2381 | * |
| 2382 | * Delete all buffers on an &sk_buff list. Each buffer is removed from |
| 2383 | * the list and one reference dropped. This function takes the list |
| 2384 | * lock and is atomic with respect to other list locking functions. |
| 2385 | */ |
| 2386 | void skb_queue_purge(struct sk_buff_head *list) |
| 2387 | { |
| 2388 | struct sk_buff *skb; |
| 2389 | while ((skb = skb_dequeue(list)) != NULL) |
| 2390 | kfree_skb(skb); |
| 2391 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 2392 | EXPORT_SYMBOL(skb_queue_purge); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2393 | |
| 2394 | /** |
| 2395 | * skb_queue_head - queue a buffer at the list head |
| 2396 | * @list: list to use |
| 2397 | * @newsk: buffer to queue |
| 2398 | * |
| 2399 | * Queue a buffer at the start of the list. This function takes the |
| 2400 | * list lock and can be used safely with other locking &sk_buff functions |
| 2401 | * safely. |
| 2402 | * |
| 2403 | * A buffer cannot be placed on two lists at the same time. |
| 2404 | */ |
| 2405 | void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk) |
| 2406 | { |
| 2407 | unsigned long flags; |
| 2408 | |
| 2409 | spin_lock_irqsave(&list->lock, flags); |
| 2410 | __skb_queue_head(list, newsk); |
| 2411 | spin_unlock_irqrestore(&list->lock, flags); |
| 2412 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 2413 | EXPORT_SYMBOL(skb_queue_head); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2414 | |
| 2415 | /** |
| 2416 | * skb_queue_tail - queue a buffer at the list tail |
| 2417 | * @list: list to use |
| 2418 | * @newsk: buffer to queue |
| 2419 | * |
| 2420 | * Queue a buffer at the tail of the list. This function takes the |
| 2421 | * list lock and can be used safely with other locking &sk_buff functions |
| 2422 | * safely. |
| 2423 | * |
| 2424 | * A buffer cannot be placed on two lists at the same time. |
| 2425 | */ |
| 2426 | void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk) |
| 2427 | { |
| 2428 | unsigned long flags; |
| 2429 | |
| 2430 | spin_lock_irqsave(&list->lock, flags); |
| 2431 | __skb_queue_tail(list, newsk); |
| 2432 | spin_unlock_irqrestore(&list->lock, flags); |
| 2433 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 2434 | EXPORT_SYMBOL(skb_queue_tail); |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 2435 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2436 | /** |
| 2437 | * skb_unlink - remove a buffer from a list |
| 2438 | * @skb: buffer to remove |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 2439 | * @list: list to use |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2440 | * |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 2441 | * Remove a packet from a list. The list locks are taken and this |
| 2442 | * function is atomic with respect to other list locked calls |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2443 | * |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 2444 | * You must know what list the SKB is on. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2445 | */ |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 2446 | void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2447 | { |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 2448 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2449 | |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 2450 | spin_lock_irqsave(&list->lock, flags); |
| 2451 | __skb_unlink(skb, list); |
| 2452 | spin_unlock_irqrestore(&list->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2453 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 2454 | EXPORT_SYMBOL(skb_unlink); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2455 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2456 | /** |
| 2457 | * skb_append - append a buffer |
| 2458 | * @old: buffer to insert after |
| 2459 | * @newsk: buffer to insert |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 2460 | * @list: list to use |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2461 | * |
| 2462 | * Place a packet after a given packet in a list. The list locks are taken |
| 2463 | * and this function is atomic with respect to other list locked calls. |
| 2464 | * A buffer cannot be placed on two lists at the same time. |
| 2465 | */ |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 2466 | void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2467 | { |
| 2468 | unsigned long flags; |
| 2469 | |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 2470 | spin_lock_irqsave(&list->lock, flags); |
Gerrit Renker | 7de6c03 | 2008-04-14 00:05:09 -0700 | [diff] [blame] | 2471 | __skb_queue_after(list, old, newsk); |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 2472 | spin_unlock_irqrestore(&list->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2473 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 2474 | EXPORT_SYMBOL(skb_append); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2475 | |
| 2476 | /** |
| 2477 | * skb_insert - insert a buffer |
| 2478 | * @old: buffer to insert before |
| 2479 | * @newsk: buffer to insert |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 2480 | * @list: list to use |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2481 | * |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 2482 | * Place a packet before a given packet in a list. The list locks are |
| 2483 | * taken and this function is atomic with respect to other list locked |
| 2484 | * calls. |
| 2485 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2486 | * A buffer cannot be placed on two lists at the same time. |
| 2487 | */ |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 2488 | void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2489 | { |
| 2490 | unsigned long flags; |
| 2491 | |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 2492 | spin_lock_irqsave(&list->lock, flags); |
| 2493 | __skb_insert(newsk, old->prev, old, list); |
| 2494 | spin_unlock_irqrestore(&list->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2495 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 2496 | EXPORT_SYMBOL(skb_insert); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2497 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2498 | static inline void skb_split_inside_header(struct sk_buff *skb, |
| 2499 | struct sk_buff* skb1, |
| 2500 | const u32 len, const int pos) |
| 2501 | { |
| 2502 | int i; |
| 2503 | |
Arnaldo Carvalho de Melo | d626f62 | 2007-03-27 18:55:52 -0300 | [diff] [blame] | 2504 | skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len), |
| 2505 | pos - len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2506 | /* And move data appendix as is. */ |
| 2507 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) |
| 2508 | skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i]; |
| 2509 | |
| 2510 | skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags; |
| 2511 | skb_shinfo(skb)->nr_frags = 0; |
| 2512 | skb1->data_len = skb->data_len; |
| 2513 | skb1->len += skb1->data_len; |
| 2514 | skb->data_len = 0; |
| 2515 | skb->len = len; |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 2516 | skb_set_tail_pointer(skb, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2517 | } |
| 2518 | |
| 2519 | static inline void skb_split_no_header(struct sk_buff *skb, |
| 2520 | struct sk_buff* skb1, |
| 2521 | const u32 len, int pos) |
| 2522 | { |
| 2523 | int i, k = 0; |
| 2524 | const int nfrags = skb_shinfo(skb)->nr_frags; |
| 2525 | |
| 2526 | skb_shinfo(skb)->nr_frags = 0; |
| 2527 | skb1->len = skb1->data_len = skb->len - len; |
| 2528 | skb->len = len; |
| 2529 | skb->data_len = len - pos; |
| 2530 | |
| 2531 | for (i = 0; i < nfrags; i++) { |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 2532 | int size = skb_frag_size(&skb_shinfo(skb)->frags[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2533 | |
| 2534 | if (pos + size > len) { |
| 2535 | skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i]; |
| 2536 | |
| 2537 | if (pos < len) { |
| 2538 | /* Split frag. |
| 2539 | * We have two variants in this case: |
| 2540 | * 1. Move all the frag to the second |
| 2541 | * part, if it is possible. F.e. |
| 2542 | * this approach is mandatory for TUX, |
| 2543 | * where splitting is expensive. |
| 2544 | * 2. Split is accurately. We make this. |
| 2545 | */ |
Ian Campbell | ea2ab69 | 2011-08-22 23:44:58 +0000 | [diff] [blame] | 2546 | skb_frag_ref(skb, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2547 | skb_shinfo(skb1)->frags[0].page_offset += len - pos; |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 2548 | skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos); |
| 2549 | skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2550 | skb_shinfo(skb)->nr_frags++; |
| 2551 | } |
| 2552 | k++; |
| 2553 | } else |
| 2554 | skb_shinfo(skb)->nr_frags++; |
| 2555 | pos += size; |
| 2556 | } |
| 2557 | skb_shinfo(skb1)->nr_frags = k; |
| 2558 | } |
| 2559 | |
| 2560 | /** |
| 2561 | * skb_split - Split fragmented skb to two parts at length len. |
| 2562 | * @skb: the buffer to split |
| 2563 | * @skb1: the buffer to receive the second part |
| 2564 | * @len: new length for skb |
| 2565 | */ |
| 2566 | void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len) |
| 2567 | { |
| 2568 | int pos = skb_headlen(skb); |
| 2569 | |
Amerigo Wang | 68534c6 | 2013-02-19 22:51:30 +0000 | [diff] [blame] | 2570 | skb_shinfo(skb1)->tx_flags = skb_shinfo(skb)->tx_flags & SKBTX_SHARED_FRAG; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2571 | if (len < pos) /* Split line is inside header. */ |
| 2572 | skb_split_inside_header(skb, skb1, len, pos); |
| 2573 | else /* Second chunk has no header, nothing to copy. */ |
| 2574 | skb_split_no_header(skb, skb1, len, pos); |
| 2575 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 2576 | EXPORT_SYMBOL(skb_split); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2577 | |
Ilpo Järvinen | 9f782db | 2008-11-25 13:57:01 -0800 | [diff] [blame] | 2578 | /* Shifting from/to a cloned skb is a no-go. |
| 2579 | * |
| 2580 | * Caller cannot keep skb_shinfo related pointers past calling here! |
| 2581 | */ |
Ilpo Järvinen | 832d11c | 2008-11-24 21:20:15 -0800 | [diff] [blame] | 2582 | static int skb_prepare_for_shift(struct sk_buff *skb) |
| 2583 | { |
Ilpo Järvinen | 0ace285 | 2008-11-24 21:30:21 -0800 | [diff] [blame] | 2584 | return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC); |
Ilpo Järvinen | 832d11c | 2008-11-24 21:20:15 -0800 | [diff] [blame] | 2585 | } |
| 2586 | |
| 2587 | /** |
| 2588 | * skb_shift - Shifts paged data partially from skb to another |
| 2589 | * @tgt: buffer into which tail data gets added |
| 2590 | * @skb: buffer from which the paged data comes from |
| 2591 | * @shiftlen: shift up to this many bytes |
| 2592 | * |
| 2593 | * Attempts to shift up to shiftlen worth of bytes, which may be less than |
Feng King | 20e994a | 2011-11-21 01:47:11 +0000 | [diff] [blame] | 2594 | * the length of the skb, from skb to tgt. Returns number bytes shifted. |
Ilpo Järvinen | 832d11c | 2008-11-24 21:20:15 -0800 | [diff] [blame] | 2595 | * It's up to caller to free skb if everything was shifted. |
| 2596 | * |
| 2597 | * If @tgt runs out of frags, the whole operation is aborted. |
| 2598 | * |
| 2599 | * Skb cannot include anything else but paged data while tgt is allowed |
| 2600 | * to have non-paged data as well. |
| 2601 | * |
| 2602 | * TODO: full sized shift could be optimized but that would need |
| 2603 | * specialized skb free'er to handle frags without up-to-date nr_frags. |
| 2604 | */ |
| 2605 | int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen) |
| 2606 | { |
| 2607 | int from, to, merge, todo; |
| 2608 | struct skb_frag_struct *fragfrom, *fragto; |
| 2609 | |
| 2610 | BUG_ON(shiftlen > skb->len); |
| 2611 | BUG_ON(skb_headlen(skb)); /* Would corrupt stream */ |
| 2612 | |
| 2613 | todo = shiftlen; |
| 2614 | from = 0; |
| 2615 | to = skb_shinfo(tgt)->nr_frags; |
| 2616 | fragfrom = &skb_shinfo(skb)->frags[from]; |
| 2617 | |
| 2618 | /* Actual merge is delayed until the point when we know we can |
| 2619 | * commit all, so that we don't have to undo partial changes |
| 2620 | */ |
| 2621 | if (!to || |
Ian Campbell | ea2ab69 | 2011-08-22 23:44:58 +0000 | [diff] [blame] | 2622 | !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom), |
| 2623 | fragfrom->page_offset)) { |
Ilpo Järvinen | 832d11c | 2008-11-24 21:20:15 -0800 | [diff] [blame] | 2624 | merge = -1; |
| 2625 | } else { |
| 2626 | merge = to - 1; |
| 2627 | |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 2628 | todo -= skb_frag_size(fragfrom); |
Ilpo Järvinen | 832d11c | 2008-11-24 21:20:15 -0800 | [diff] [blame] | 2629 | if (todo < 0) { |
| 2630 | if (skb_prepare_for_shift(skb) || |
| 2631 | skb_prepare_for_shift(tgt)) |
| 2632 | return 0; |
| 2633 | |
Ilpo Järvinen | 9f782db | 2008-11-25 13:57:01 -0800 | [diff] [blame] | 2634 | /* All previous frag pointers might be stale! */ |
| 2635 | fragfrom = &skb_shinfo(skb)->frags[from]; |
Ilpo Järvinen | 832d11c | 2008-11-24 21:20:15 -0800 | [diff] [blame] | 2636 | fragto = &skb_shinfo(tgt)->frags[merge]; |
| 2637 | |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 2638 | skb_frag_size_add(fragto, shiftlen); |
| 2639 | skb_frag_size_sub(fragfrom, shiftlen); |
Ilpo Järvinen | 832d11c | 2008-11-24 21:20:15 -0800 | [diff] [blame] | 2640 | fragfrom->page_offset += shiftlen; |
| 2641 | |
| 2642 | goto onlymerged; |
| 2643 | } |
| 2644 | |
| 2645 | from++; |
| 2646 | } |
| 2647 | |
| 2648 | /* Skip full, not-fitting skb to avoid expensive operations */ |
| 2649 | if ((shiftlen == skb->len) && |
| 2650 | (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to)) |
| 2651 | return 0; |
| 2652 | |
| 2653 | if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt)) |
| 2654 | return 0; |
| 2655 | |
| 2656 | while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) { |
| 2657 | if (to == MAX_SKB_FRAGS) |
| 2658 | return 0; |
| 2659 | |
| 2660 | fragfrom = &skb_shinfo(skb)->frags[from]; |
| 2661 | fragto = &skb_shinfo(tgt)->frags[to]; |
| 2662 | |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 2663 | if (todo >= skb_frag_size(fragfrom)) { |
Ilpo Järvinen | 832d11c | 2008-11-24 21:20:15 -0800 | [diff] [blame] | 2664 | *fragto = *fragfrom; |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 2665 | todo -= skb_frag_size(fragfrom); |
Ilpo Järvinen | 832d11c | 2008-11-24 21:20:15 -0800 | [diff] [blame] | 2666 | from++; |
| 2667 | to++; |
| 2668 | |
| 2669 | } else { |
Ian Campbell | ea2ab69 | 2011-08-22 23:44:58 +0000 | [diff] [blame] | 2670 | __skb_frag_ref(fragfrom); |
Ilpo Järvinen | 832d11c | 2008-11-24 21:20:15 -0800 | [diff] [blame] | 2671 | fragto->page = fragfrom->page; |
| 2672 | fragto->page_offset = fragfrom->page_offset; |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 2673 | skb_frag_size_set(fragto, todo); |
Ilpo Järvinen | 832d11c | 2008-11-24 21:20:15 -0800 | [diff] [blame] | 2674 | |
| 2675 | fragfrom->page_offset += todo; |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 2676 | skb_frag_size_sub(fragfrom, todo); |
Ilpo Järvinen | 832d11c | 2008-11-24 21:20:15 -0800 | [diff] [blame] | 2677 | todo = 0; |
| 2678 | |
| 2679 | to++; |
| 2680 | break; |
| 2681 | } |
| 2682 | } |
| 2683 | |
| 2684 | /* Ready to "commit" this state change to tgt */ |
| 2685 | skb_shinfo(tgt)->nr_frags = to; |
| 2686 | |
| 2687 | if (merge >= 0) { |
| 2688 | fragfrom = &skb_shinfo(skb)->frags[0]; |
| 2689 | fragto = &skb_shinfo(tgt)->frags[merge]; |
| 2690 | |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 2691 | skb_frag_size_add(fragto, skb_frag_size(fragfrom)); |
Ian Campbell | ea2ab69 | 2011-08-22 23:44:58 +0000 | [diff] [blame] | 2692 | __skb_frag_unref(fragfrom); |
Ilpo Järvinen | 832d11c | 2008-11-24 21:20:15 -0800 | [diff] [blame] | 2693 | } |
| 2694 | |
| 2695 | /* Reposition in the original skb */ |
| 2696 | to = 0; |
| 2697 | while (from < skb_shinfo(skb)->nr_frags) |
| 2698 | skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++]; |
| 2699 | skb_shinfo(skb)->nr_frags = to; |
| 2700 | |
| 2701 | BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags); |
| 2702 | |
| 2703 | onlymerged: |
| 2704 | /* Most likely the tgt won't ever need its checksum anymore, skb on |
| 2705 | * the other hand might need it if it needs to be resent |
| 2706 | */ |
| 2707 | tgt->ip_summed = CHECKSUM_PARTIAL; |
| 2708 | skb->ip_summed = CHECKSUM_PARTIAL; |
| 2709 | |
| 2710 | /* Yak, is it really working this way? Some helper please? */ |
| 2711 | skb->len -= shiftlen; |
| 2712 | skb->data_len -= shiftlen; |
| 2713 | skb->truesize -= shiftlen; |
| 2714 | tgt->len += shiftlen; |
| 2715 | tgt->data_len += shiftlen; |
| 2716 | tgt->truesize += shiftlen; |
| 2717 | |
| 2718 | return shiftlen; |
| 2719 | } |
| 2720 | |
Thomas Graf | 677e90e | 2005-06-23 20:59:51 -0700 | [diff] [blame] | 2721 | /** |
| 2722 | * skb_prepare_seq_read - Prepare a sequential read of skb data |
| 2723 | * @skb: the buffer to read |
| 2724 | * @from: lower offset of data to be read |
| 2725 | * @to: upper offset of data to be read |
| 2726 | * @st: state variable |
| 2727 | * |
| 2728 | * Initializes the specified state variable. Must be called before |
| 2729 | * invoking skb_seq_read() for the first time. |
| 2730 | */ |
| 2731 | void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from, |
| 2732 | unsigned int to, struct skb_seq_state *st) |
| 2733 | { |
| 2734 | st->lower_offset = from; |
| 2735 | st->upper_offset = to; |
| 2736 | st->root_skb = st->cur_skb = skb; |
| 2737 | st->frag_idx = st->stepped_offset = 0; |
| 2738 | st->frag_data = NULL; |
| 2739 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 2740 | EXPORT_SYMBOL(skb_prepare_seq_read); |
Thomas Graf | 677e90e | 2005-06-23 20:59:51 -0700 | [diff] [blame] | 2741 | |
| 2742 | /** |
| 2743 | * skb_seq_read - Sequentially read skb data |
| 2744 | * @consumed: number of bytes consumed by the caller so far |
| 2745 | * @data: destination pointer for data to be returned |
| 2746 | * @st: state variable |
| 2747 | * |
Mathias Krause | bc32383 | 2013-11-07 14:18:26 +0100 | [diff] [blame] | 2748 | * Reads a block of skb data at @consumed relative to the |
Thomas Graf | 677e90e | 2005-06-23 20:59:51 -0700 | [diff] [blame] | 2749 | * lower offset specified to skb_prepare_seq_read(). Assigns |
Mathias Krause | bc32383 | 2013-11-07 14:18:26 +0100 | [diff] [blame] | 2750 | * the head of the data block to @data and returns the length |
Thomas Graf | 677e90e | 2005-06-23 20:59:51 -0700 | [diff] [blame] | 2751 | * of the block or 0 if the end of the skb data or the upper |
| 2752 | * offset has been reached. |
| 2753 | * |
| 2754 | * The caller is not required to consume all of the data |
Mathias Krause | bc32383 | 2013-11-07 14:18:26 +0100 | [diff] [blame] | 2755 | * returned, i.e. @consumed is typically set to the number |
Thomas Graf | 677e90e | 2005-06-23 20:59:51 -0700 | [diff] [blame] | 2756 | * of bytes already consumed and the next call to |
| 2757 | * skb_seq_read() will return the remaining part of the block. |
| 2758 | * |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 2759 | * Note 1: The size of each block of data returned can be arbitrary, |
Masanari Iida | e793c0f | 2014-09-04 23:44:36 +0900 | [diff] [blame] | 2760 | * this limitation is the cost for zerocopy sequential |
Thomas Graf | 677e90e | 2005-06-23 20:59:51 -0700 | [diff] [blame] | 2761 | * reads of potentially non linear data. |
| 2762 | * |
Randy Dunlap | bc2cda1 | 2008-02-13 15:03:25 -0800 | [diff] [blame] | 2763 | * Note 2: Fragment lists within fragments are not implemented |
Thomas Graf | 677e90e | 2005-06-23 20:59:51 -0700 | [diff] [blame] | 2764 | * at the moment, state->root_skb could be replaced with |
| 2765 | * a stack for this purpose. |
| 2766 | */ |
| 2767 | unsigned int skb_seq_read(unsigned int consumed, const u8 **data, |
| 2768 | struct skb_seq_state *st) |
| 2769 | { |
| 2770 | unsigned int block_limit, abs_offset = consumed + st->lower_offset; |
| 2771 | skb_frag_t *frag; |
| 2772 | |
Wedson Almeida Filho | aeb193e | 2013-06-23 23:33:48 -0700 | [diff] [blame] | 2773 | if (unlikely(abs_offset >= st->upper_offset)) { |
| 2774 | if (st->frag_data) { |
| 2775 | kunmap_atomic(st->frag_data); |
| 2776 | st->frag_data = NULL; |
| 2777 | } |
Thomas Graf | 677e90e | 2005-06-23 20:59:51 -0700 | [diff] [blame] | 2778 | return 0; |
Wedson Almeida Filho | aeb193e | 2013-06-23 23:33:48 -0700 | [diff] [blame] | 2779 | } |
Thomas Graf | 677e90e | 2005-06-23 20:59:51 -0700 | [diff] [blame] | 2780 | |
| 2781 | next_skb: |
Herbert Xu | 95e3b24 | 2009-01-29 16:07:52 -0800 | [diff] [blame] | 2782 | block_limit = skb_headlen(st->cur_skb) + st->stepped_offset; |
Thomas Graf | 677e90e | 2005-06-23 20:59:51 -0700 | [diff] [blame] | 2783 | |
Thomas Chenault | 995b337 | 2009-05-18 21:43:27 -0700 | [diff] [blame] | 2784 | if (abs_offset < block_limit && !st->frag_data) { |
Herbert Xu | 95e3b24 | 2009-01-29 16:07:52 -0800 | [diff] [blame] | 2785 | *data = st->cur_skb->data + (abs_offset - st->stepped_offset); |
Thomas Graf | 677e90e | 2005-06-23 20:59:51 -0700 | [diff] [blame] | 2786 | return block_limit - abs_offset; |
| 2787 | } |
| 2788 | |
| 2789 | if (st->frag_idx == 0 && !st->frag_data) |
| 2790 | st->stepped_offset += skb_headlen(st->cur_skb); |
| 2791 | |
| 2792 | while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) { |
| 2793 | frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx]; |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 2794 | block_limit = skb_frag_size(frag) + st->stepped_offset; |
Thomas Graf | 677e90e | 2005-06-23 20:59:51 -0700 | [diff] [blame] | 2795 | |
| 2796 | if (abs_offset < block_limit) { |
| 2797 | if (!st->frag_data) |
Eric Dumazet | 51c56b0 | 2012-04-05 11:35:15 +0200 | [diff] [blame] | 2798 | st->frag_data = kmap_atomic(skb_frag_page(frag)); |
Thomas Graf | 677e90e | 2005-06-23 20:59:51 -0700 | [diff] [blame] | 2799 | |
| 2800 | *data = (u8 *) st->frag_data + frag->page_offset + |
| 2801 | (abs_offset - st->stepped_offset); |
| 2802 | |
| 2803 | return block_limit - abs_offset; |
| 2804 | } |
| 2805 | |
| 2806 | if (st->frag_data) { |
Eric Dumazet | 51c56b0 | 2012-04-05 11:35:15 +0200 | [diff] [blame] | 2807 | kunmap_atomic(st->frag_data); |
Thomas Graf | 677e90e | 2005-06-23 20:59:51 -0700 | [diff] [blame] | 2808 | st->frag_data = NULL; |
| 2809 | } |
| 2810 | |
| 2811 | st->frag_idx++; |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 2812 | st->stepped_offset += skb_frag_size(frag); |
Thomas Graf | 677e90e | 2005-06-23 20:59:51 -0700 | [diff] [blame] | 2813 | } |
| 2814 | |
Olaf Kirch | 5b5a60d | 2007-06-23 23:11:52 -0700 | [diff] [blame] | 2815 | if (st->frag_data) { |
Eric Dumazet | 51c56b0 | 2012-04-05 11:35:15 +0200 | [diff] [blame] | 2816 | kunmap_atomic(st->frag_data); |
Olaf Kirch | 5b5a60d | 2007-06-23 23:11:52 -0700 | [diff] [blame] | 2817 | st->frag_data = NULL; |
| 2818 | } |
| 2819 | |
David S. Miller | 21dc330 | 2010-08-23 00:13:46 -0700 | [diff] [blame] | 2820 | if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) { |
Shyam Iyer | 71b3346 | 2009-01-29 16:12:42 -0800 | [diff] [blame] | 2821 | st->cur_skb = skb_shinfo(st->root_skb)->frag_list; |
Thomas Graf | 677e90e | 2005-06-23 20:59:51 -0700 | [diff] [blame] | 2822 | st->frag_idx = 0; |
| 2823 | goto next_skb; |
Shyam Iyer | 71b3346 | 2009-01-29 16:12:42 -0800 | [diff] [blame] | 2824 | } else if (st->cur_skb->next) { |
| 2825 | st->cur_skb = st->cur_skb->next; |
Herbert Xu | 95e3b24 | 2009-01-29 16:07:52 -0800 | [diff] [blame] | 2826 | st->frag_idx = 0; |
Thomas Graf | 677e90e | 2005-06-23 20:59:51 -0700 | [diff] [blame] | 2827 | goto next_skb; |
| 2828 | } |
| 2829 | |
| 2830 | return 0; |
| 2831 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 2832 | EXPORT_SYMBOL(skb_seq_read); |
Thomas Graf | 677e90e | 2005-06-23 20:59:51 -0700 | [diff] [blame] | 2833 | |
| 2834 | /** |
| 2835 | * skb_abort_seq_read - Abort a sequential read of skb data |
| 2836 | * @st: state variable |
| 2837 | * |
| 2838 | * Must be called if skb_seq_read() was not called until it |
| 2839 | * returned 0. |
| 2840 | */ |
| 2841 | void skb_abort_seq_read(struct skb_seq_state *st) |
| 2842 | { |
| 2843 | if (st->frag_data) |
Eric Dumazet | 51c56b0 | 2012-04-05 11:35:15 +0200 | [diff] [blame] | 2844 | kunmap_atomic(st->frag_data); |
Thomas Graf | 677e90e | 2005-06-23 20:59:51 -0700 | [diff] [blame] | 2845 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 2846 | EXPORT_SYMBOL(skb_abort_seq_read); |
Thomas Graf | 677e90e | 2005-06-23 20:59:51 -0700 | [diff] [blame] | 2847 | |
Thomas Graf | 3fc7e8a | 2005-06-23 21:00:17 -0700 | [diff] [blame] | 2848 | #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb)) |
| 2849 | |
| 2850 | static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text, |
| 2851 | struct ts_config *conf, |
| 2852 | struct ts_state *state) |
| 2853 | { |
| 2854 | return skb_seq_read(offset, text, TS_SKB_CB(state)); |
| 2855 | } |
| 2856 | |
| 2857 | static void skb_ts_finish(struct ts_config *conf, struct ts_state *state) |
| 2858 | { |
| 2859 | skb_abort_seq_read(TS_SKB_CB(state)); |
| 2860 | } |
| 2861 | |
| 2862 | /** |
| 2863 | * skb_find_text - Find a text pattern in skb data |
| 2864 | * @skb: the buffer to look in |
| 2865 | * @from: search offset |
| 2866 | * @to: search limit |
| 2867 | * @config: textsearch configuration |
Thomas Graf | 3fc7e8a | 2005-06-23 21:00:17 -0700 | [diff] [blame] | 2868 | * |
| 2869 | * Finds a pattern in the skb data according to the specified |
| 2870 | * textsearch configuration. Use textsearch_next() to retrieve |
| 2871 | * subsequent occurrences of the pattern. Returns the offset |
| 2872 | * to the first occurrence or UINT_MAX if no match was found. |
| 2873 | */ |
| 2874 | unsigned int skb_find_text(struct sk_buff *skb, unsigned int from, |
Bojan Prtvar | 059a244 | 2015-02-22 11:46:35 +0100 | [diff] [blame^] | 2875 | unsigned int to, struct ts_config *config) |
Thomas Graf | 3fc7e8a | 2005-06-23 21:00:17 -0700 | [diff] [blame] | 2876 | { |
Bojan Prtvar | 059a244 | 2015-02-22 11:46:35 +0100 | [diff] [blame^] | 2877 | struct ts_state state; |
Phil Oester | f72b948 | 2006-06-26 00:00:57 -0700 | [diff] [blame] | 2878 | unsigned int ret; |
| 2879 | |
Thomas Graf | 3fc7e8a | 2005-06-23 21:00:17 -0700 | [diff] [blame] | 2880 | config->get_next_block = skb_ts_get_next_block; |
| 2881 | config->finish = skb_ts_finish; |
| 2882 | |
Bojan Prtvar | 059a244 | 2015-02-22 11:46:35 +0100 | [diff] [blame^] | 2883 | skb_prepare_seq_read(skb, from, to, TS_SKB_CB(&state)); |
Thomas Graf | 3fc7e8a | 2005-06-23 21:00:17 -0700 | [diff] [blame] | 2884 | |
Bojan Prtvar | 059a244 | 2015-02-22 11:46:35 +0100 | [diff] [blame^] | 2885 | ret = textsearch_find(config, &state); |
Phil Oester | f72b948 | 2006-06-26 00:00:57 -0700 | [diff] [blame] | 2886 | return (ret <= to - from ? ret : UINT_MAX); |
Thomas Graf | 3fc7e8a | 2005-06-23 21:00:17 -0700 | [diff] [blame] | 2887 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 2888 | EXPORT_SYMBOL(skb_find_text); |
Thomas Graf | 3fc7e8a | 2005-06-23 21:00:17 -0700 | [diff] [blame] | 2889 | |
Ananda Raju | e89e9cf | 2005-10-18 15:46:41 -0700 | [diff] [blame] | 2890 | /** |
Ben Hutchings | 2c53040 | 2012-07-10 10:55:09 +0000 | [diff] [blame] | 2891 | * skb_append_datato_frags - append the user data to a skb |
Ananda Raju | e89e9cf | 2005-10-18 15:46:41 -0700 | [diff] [blame] | 2892 | * @sk: sock structure |
Masanari Iida | e793c0f | 2014-09-04 23:44:36 +0900 | [diff] [blame] | 2893 | * @skb: skb structure to be appended with user data. |
Ananda Raju | e89e9cf | 2005-10-18 15:46:41 -0700 | [diff] [blame] | 2894 | * @getfrag: call back function to be used for getting the user data |
| 2895 | * @from: pointer to user message iov |
| 2896 | * @length: length of the iov message |
| 2897 | * |
| 2898 | * Description: This procedure append the user data in the fragment part |
| 2899 | * of the skb if any page alloc fails user this procedure returns -ENOMEM |
| 2900 | */ |
| 2901 | int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb, |
Martin Waitz | dab9630 | 2005-12-05 13:40:12 -0800 | [diff] [blame] | 2902 | int (*getfrag)(void *from, char *to, int offset, |
Ananda Raju | e89e9cf | 2005-10-18 15:46:41 -0700 | [diff] [blame] | 2903 | int len, int odd, struct sk_buff *skb), |
| 2904 | void *from, int length) |
| 2905 | { |
Eric Dumazet | b211172 | 2012-12-28 06:06:37 +0000 | [diff] [blame] | 2906 | int frg_cnt = skb_shinfo(skb)->nr_frags; |
| 2907 | int copy; |
Ananda Raju | e89e9cf | 2005-10-18 15:46:41 -0700 | [diff] [blame] | 2908 | int offset = 0; |
| 2909 | int ret; |
Eric Dumazet | b211172 | 2012-12-28 06:06:37 +0000 | [diff] [blame] | 2910 | struct page_frag *pfrag = ¤t->task_frag; |
Ananda Raju | e89e9cf | 2005-10-18 15:46:41 -0700 | [diff] [blame] | 2911 | |
| 2912 | do { |
| 2913 | /* Return error if we don't have space for new frag */ |
Ananda Raju | e89e9cf | 2005-10-18 15:46:41 -0700 | [diff] [blame] | 2914 | if (frg_cnt >= MAX_SKB_FRAGS) |
Eric Dumazet | b211172 | 2012-12-28 06:06:37 +0000 | [diff] [blame] | 2915 | return -EMSGSIZE; |
Ananda Raju | e89e9cf | 2005-10-18 15:46:41 -0700 | [diff] [blame] | 2916 | |
Eric Dumazet | b211172 | 2012-12-28 06:06:37 +0000 | [diff] [blame] | 2917 | if (!sk_page_frag_refill(sk, pfrag)) |
Ananda Raju | e89e9cf | 2005-10-18 15:46:41 -0700 | [diff] [blame] | 2918 | return -ENOMEM; |
| 2919 | |
Ananda Raju | e89e9cf | 2005-10-18 15:46:41 -0700 | [diff] [blame] | 2920 | /* copy the user data to page */ |
Eric Dumazet | b211172 | 2012-12-28 06:06:37 +0000 | [diff] [blame] | 2921 | copy = min_t(int, length, pfrag->size - pfrag->offset); |
Ananda Raju | e89e9cf | 2005-10-18 15:46:41 -0700 | [diff] [blame] | 2922 | |
Eric Dumazet | b211172 | 2012-12-28 06:06:37 +0000 | [diff] [blame] | 2923 | ret = getfrag(from, page_address(pfrag->page) + pfrag->offset, |
| 2924 | offset, copy, 0, skb); |
Ananda Raju | e89e9cf | 2005-10-18 15:46:41 -0700 | [diff] [blame] | 2925 | if (ret < 0) |
| 2926 | return -EFAULT; |
| 2927 | |
| 2928 | /* copy was successful so update the size parameters */ |
Eric Dumazet | b211172 | 2012-12-28 06:06:37 +0000 | [diff] [blame] | 2929 | skb_fill_page_desc(skb, frg_cnt, pfrag->page, pfrag->offset, |
| 2930 | copy); |
| 2931 | frg_cnt++; |
| 2932 | pfrag->offset += copy; |
| 2933 | get_page(pfrag->page); |
| 2934 | |
| 2935 | skb->truesize += copy; |
| 2936 | atomic_add(copy, &sk->sk_wmem_alloc); |
Ananda Raju | e89e9cf | 2005-10-18 15:46:41 -0700 | [diff] [blame] | 2937 | skb->len += copy; |
| 2938 | skb->data_len += copy; |
| 2939 | offset += copy; |
| 2940 | length -= copy; |
| 2941 | |
| 2942 | } while (length > 0); |
| 2943 | |
| 2944 | return 0; |
| 2945 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 2946 | EXPORT_SYMBOL(skb_append_datato_frags); |
Ananda Raju | e89e9cf | 2005-10-18 15:46:41 -0700 | [diff] [blame] | 2947 | |
Herbert Xu | cbb042f | 2006-03-20 22:43:56 -0800 | [diff] [blame] | 2948 | /** |
| 2949 | * skb_pull_rcsum - pull skb and update receive checksum |
| 2950 | * @skb: buffer to update |
Herbert Xu | cbb042f | 2006-03-20 22:43:56 -0800 | [diff] [blame] | 2951 | * @len: length of data pulled |
| 2952 | * |
| 2953 | * This function performs an skb_pull on the packet and updates |
Urs Thuermann | fee54fa | 2008-02-12 22:03:25 -0800 | [diff] [blame] | 2954 | * the CHECKSUM_COMPLETE checksum. It should be used on |
Patrick McHardy | 84fa793 | 2006-08-29 16:44:56 -0700 | [diff] [blame] | 2955 | * receive path processing instead of skb_pull unless you know |
| 2956 | * that the checksum difference is zero (e.g., a valid IP header) |
| 2957 | * or you are setting ip_summed to CHECKSUM_NONE. |
Herbert Xu | cbb042f | 2006-03-20 22:43:56 -0800 | [diff] [blame] | 2958 | */ |
| 2959 | unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len) |
| 2960 | { |
| 2961 | BUG_ON(len > skb->len); |
| 2962 | skb->len -= len; |
| 2963 | BUG_ON(skb->len < skb->data_len); |
| 2964 | skb_postpull_rcsum(skb, skb->data, len); |
| 2965 | return skb->data += len; |
| 2966 | } |
Arnaldo Carvalho de Melo | f94691a | 2006-03-20 22:47:55 -0800 | [diff] [blame] | 2967 | EXPORT_SYMBOL_GPL(skb_pull_rcsum); |
| 2968 | |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 2969 | /** |
| 2970 | * skb_segment - Perform protocol segmentation on skb. |
Michael S. Tsirkin | df5771f | 2014-03-10 18:29:19 +0200 | [diff] [blame] | 2971 | * @head_skb: buffer to segment |
Herbert Xu | 576a30e | 2006-06-27 13:22:38 -0700 | [diff] [blame] | 2972 | * @features: features for the output path (see dev->features) |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 2973 | * |
| 2974 | * This function performs segmentation on the given skb. It returns |
Ben Hutchings | 4c821d7 | 2008-04-13 21:52:48 -0700 | [diff] [blame] | 2975 | * a pointer to the first in a list of new skbs for the segments. |
| 2976 | * In case of error it returns ERR_PTR(err). |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 2977 | */ |
Michael S. Tsirkin | df5771f | 2014-03-10 18:29:19 +0200 | [diff] [blame] | 2978 | struct sk_buff *skb_segment(struct sk_buff *head_skb, |
| 2979 | netdev_features_t features) |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 2980 | { |
| 2981 | struct sk_buff *segs = NULL; |
| 2982 | struct sk_buff *tail = NULL; |
Michael S. Tsirkin | 1a4ceda | 2014-03-10 19:27:59 +0200 | [diff] [blame] | 2983 | struct sk_buff *list_skb = skb_shinfo(head_skb)->frag_list; |
Michael S. Tsirkin | df5771f | 2014-03-10 18:29:19 +0200 | [diff] [blame] | 2984 | skb_frag_t *frag = skb_shinfo(head_skb)->frags; |
| 2985 | unsigned int mss = skb_shinfo(head_skb)->gso_size; |
| 2986 | unsigned int doffset = head_skb->data - skb_mac_header(head_skb); |
Michael S. Tsirkin | 1fd819e | 2014-03-10 19:28:08 +0200 | [diff] [blame] | 2987 | struct sk_buff *frag_skb = head_skb; |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 2988 | unsigned int offset = doffset; |
Michael S. Tsirkin | df5771f | 2014-03-10 18:29:19 +0200 | [diff] [blame] | 2989 | unsigned int tnl_hlen = skb_tnl_header_len(head_skb); |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 2990 | unsigned int headroom; |
| 2991 | unsigned int len; |
Pravin B Shelar | ec5f061 | 2013-03-07 09:28:01 +0000 | [diff] [blame] | 2992 | __be16 proto; |
| 2993 | bool csum; |
Michał Mirosław | 04ed3e7 | 2011-01-24 15:32:47 -0800 | [diff] [blame] | 2994 | int sg = !!(features & NETIF_F_SG); |
Michael S. Tsirkin | df5771f | 2014-03-10 18:29:19 +0200 | [diff] [blame] | 2995 | int nfrags = skb_shinfo(head_skb)->nr_frags; |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 2996 | int err = -ENOMEM; |
| 2997 | int i = 0; |
| 2998 | int pos; |
Vlad Yasevich | 53d6471 | 2014-03-27 17:26:18 -0400 | [diff] [blame] | 2999 | int dummy; |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 3000 | |
Wei-Chun Chao | 5882a07 | 2014-06-08 23:48:54 -0700 | [diff] [blame] | 3001 | __skb_push(head_skb, doffset); |
Vlad Yasevich | 53d6471 | 2014-03-27 17:26:18 -0400 | [diff] [blame] | 3002 | proto = skb_network_protocol(head_skb, &dummy); |
Pravin B Shelar | ec5f061 | 2013-03-07 09:28:01 +0000 | [diff] [blame] | 3003 | if (unlikely(!proto)) |
| 3004 | return ERR_PTR(-EINVAL); |
| 3005 | |
Tom Herbert | 7e2b10c | 2014-06-04 17:20:02 -0700 | [diff] [blame] | 3006 | csum = !head_skb->encap_hdr_csum && |
| 3007 | !!can_checksum_protocol(features, proto); |
| 3008 | |
Michael S. Tsirkin | df5771f | 2014-03-10 18:29:19 +0200 | [diff] [blame] | 3009 | headroom = skb_headroom(head_skb); |
| 3010 | pos = skb_headlen(head_skb); |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 3011 | |
| 3012 | do { |
| 3013 | struct sk_buff *nskb; |
Michael S. Tsirkin | 8cb1990 | 2014-03-10 18:29:04 +0200 | [diff] [blame] | 3014 | skb_frag_t *nskb_frag; |
Herbert Xu | c8884ed | 2006-10-29 15:59:41 -0800 | [diff] [blame] | 3015 | int hsize; |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 3016 | int size; |
| 3017 | |
Michael S. Tsirkin | df5771f | 2014-03-10 18:29:19 +0200 | [diff] [blame] | 3018 | len = head_skb->len - offset; |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 3019 | if (len > mss) |
| 3020 | len = mss; |
| 3021 | |
Michael S. Tsirkin | df5771f | 2014-03-10 18:29:19 +0200 | [diff] [blame] | 3022 | hsize = skb_headlen(head_skb) - offset; |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 3023 | if (hsize < 0) |
| 3024 | hsize = 0; |
Herbert Xu | c8884ed | 2006-10-29 15:59:41 -0800 | [diff] [blame] | 3025 | if (hsize > len || !sg) |
| 3026 | hsize = len; |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 3027 | |
Michael S. Tsirkin | 1a4ceda | 2014-03-10 19:27:59 +0200 | [diff] [blame] | 3028 | if (!hsize && i >= nfrags && skb_headlen(list_skb) && |
| 3029 | (skb_headlen(list_skb) == len || sg)) { |
| 3030 | BUG_ON(skb_headlen(list_skb) > len); |
Herbert Xu | 89319d38 | 2008-12-15 23:26:06 -0800 | [diff] [blame] | 3031 | |
Herbert Xu | 9d8506c | 2013-11-21 11:10:04 -0800 | [diff] [blame] | 3032 | i = 0; |
Michael S. Tsirkin | 1a4ceda | 2014-03-10 19:27:59 +0200 | [diff] [blame] | 3033 | nfrags = skb_shinfo(list_skb)->nr_frags; |
| 3034 | frag = skb_shinfo(list_skb)->frags; |
Michael S. Tsirkin | 1fd819e | 2014-03-10 19:28:08 +0200 | [diff] [blame] | 3035 | frag_skb = list_skb; |
Michael S. Tsirkin | 1a4ceda | 2014-03-10 19:27:59 +0200 | [diff] [blame] | 3036 | pos += skb_headlen(list_skb); |
Herbert Xu | 9d8506c | 2013-11-21 11:10:04 -0800 | [diff] [blame] | 3037 | |
| 3038 | while (pos < offset + len) { |
| 3039 | BUG_ON(i >= nfrags); |
| 3040 | |
Michael S. Tsirkin | 4e1beba | 2014-03-10 18:29:14 +0200 | [diff] [blame] | 3041 | size = skb_frag_size(frag); |
Herbert Xu | 9d8506c | 2013-11-21 11:10:04 -0800 | [diff] [blame] | 3042 | if (pos + size > offset + len) |
| 3043 | break; |
| 3044 | |
| 3045 | i++; |
| 3046 | pos += size; |
Michael S. Tsirkin | 4e1beba | 2014-03-10 18:29:14 +0200 | [diff] [blame] | 3047 | frag++; |
Herbert Xu | 9d8506c | 2013-11-21 11:10:04 -0800 | [diff] [blame] | 3048 | } |
| 3049 | |
Michael S. Tsirkin | 1a4ceda | 2014-03-10 19:27:59 +0200 | [diff] [blame] | 3050 | nskb = skb_clone(list_skb, GFP_ATOMIC); |
| 3051 | list_skb = list_skb->next; |
Herbert Xu | 89319d38 | 2008-12-15 23:26:06 -0800 | [diff] [blame] | 3052 | |
| 3053 | if (unlikely(!nskb)) |
| 3054 | goto err; |
| 3055 | |
Herbert Xu | 9d8506c | 2013-11-21 11:10:04 -0800 | [diff] [blame] | 3056 | if (unlikely(pskb_trim(nskb, len))) { |
| 3057 | kfree_skb(nskb); |
| 3058 | goto err; |
| 3059 | } |
| 3060 | |
Alexander Duyck | ec47ea8 | 2012-05-04 14:26:56 +0000 | [diff] [blame] | 3061 | hsize = skb_end_offset(nskb); |
Herbert Xu | 89319d38 | 2008-12-15 23:26:06 -0800 | [diff] [blame] | 3062 | if (skb_cow_head(nskb, doffset + headroom)) { |
| 3063 | kfree_skb(nskb); |
| 3064 | goto err; |
| 3065 | } |
| 3066 | |
Alexander Duyck | ec47ea8 | 2012-05-04 14:26:56 +0000 | [diff] [blame] | 3067 | nskb->truesize += skb_end_offset(nskb) - hsize; |
Herbert Xu | 89319d38 | 2008-12-15 23:26:06 -0800 | [diff] [blame] | 3068 | skb_release_head_state(nskb); |
| 3069 | __skb_push(nskb, doffset); |
| 3070 | } else { |
Mel Gorman | c93bdd0 | 2012-07-31 16:44:19 -0700 | [diff] [blame] | 3071 | nskb = __alloc_skb(hsize + doffset + headroom, |
Michael S. Tsirkin | df5771f | 2014-03-10 18:29:19 +0200 | [diff] [blame] | 3072 | GFP_ATOMIC, skb_alloc_rx_flag(head_skb), |
Mel Gorman | c93bdd0 | 2012-07-31 16:44:19 -0700 | [diff] [blame] | 3073 | NUMA_NO_NODE); |
Herbert Xu | 89319d38 | 2008-12-15 23:26:06 -0800 | [diff] [blame] | 3074 | |
| 3075 | if (unlikely(!nskb)) |
| 3076 | goto err; |
| 3077 | |
| 3078 | skb_reserve(nskb, headroom); |
| 3079 | __skb_put(nskb, doffset); |
| 3080 | } |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 3081 | |
| 3082 | if (segs) |
| 3083 | tail->next = nskb; |
| 3084 | else |
| 3085 | segs = nskb; |
| 3086 | tail = nskb; |
| 3087 | |
Michael S. Tsirkin | df5771f | 2014-03-10 18:29:19 +0200 | [diff] [blame] | 3088 | __copy_skb_header(nskb, head_skb); |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 3089 | |
Eric Dumazet | 030737b | 2013-10-19 11:42:54 -0700 | [diff] [blame] | 3090 | skb_headers_offset_update(nskb, skb_headroom(nskb) - headroom); |
Vlad Yasevich | fcdfe3a | 2014-07-31 10:33:06 -0400 | [diff] [blame] | 3091 | skb_reset_mac_len(nskb); |
Pravin B Shelar | 68c3316 | 2013-02-14 14:02:41 +0000 | [diff] [blame] | 3092 | |
Michael S. Tsirkin | df5771f | 2014-03-10 18:29:19 +0200 | [diff] [blame] | 3093 | skb_copy_from_linear_data_offset(head_skb, -tnl_hlen, |
Pravin B Shelar | 68c3316 | 2013-02-14 14:02:41 +0000 | [diff] [blame] | 3094 | nskb->data - tnl_hlen, |
| 3095 | doffset + tnl_hlen); |
Herbert Xu | 89319d38 | 2008-12-15 23:26:06 -0800 | [diff] [blame] | 3096 | |
Herbert Xu | 9d8506c | 2013-11-21 11:10:04 -0800 | [diff] [blame] | 3097 | if (nskb->len == len + doffset) |
Simon Horman | 1cdbcb7 | 2013-05-19 15:46:49 +0000 | [diff] [blame] | 3098 | goto perform_csum_check; |
Herbert Xu | 89319d38 | 2008-12-15 23:26:06 -0800 | [diff] [blame] | 3099 | |
Tom Herbert | e585f23 | 2014-11-04 09:06:54 -0800 | [diff] [blame] | 3100 | if (!sg && !nskb->remcsum_offload) { |
Herbert Xu | 6f85a12 | 2008-08-15 14:55:02 -0700 | [diff] [blame] | 3101 | nskb->ip_summed = CHECKSUM_NONE; |
Michael S. Tsirkin | df5771f | 2014-03-10 18:29:19 +0200 | [diff] [blame] | 3102 | nskb->csum = skb_copy_and_csum_bits(head_skb, offset, |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 3103 | skb_put(nskb, len), |
| 3104 | len, 0); |
Tom Herbert | 7e2b10c | 2014-06-04 17:20:02 -0700 | [diff] [blame] | 3105 | SKB_GSO_CB(nskb)->csum_start = |
Tom Herbert | de84372 | 2014-06-25 12:51:01 -0700 | [diff] [blame] | 3106 | skb_headroom(nskb) + doffset; |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 3107 | continue; |
| 3108 | } |
| 3109 | |
Michael S. Tsirkin | 8cb1990 | 2014-03-10 18:29:04 +0200 | [diff] [blame] | 3110 | nskb_frag = skb_shinfo(nskb)->frags; |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 3111 | |
Michael S. Tsirkin | df5771f | 2014-03-10 18:29:19 +0200 | [diff] [blame] | 3112 | skb_copy_from_linear_data_offset(head_skb, offset, |
Arnaldo Carvalho de Melo | d626f62 | 2007-03-27 18:55:52 -0300 | [diff] [blame] | 3113 | skb_put(nskb, hsize), hsize); |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 3114 | |
Michael S. Tsirkin | df5771f | 2014-03-10 18:29:19 +0200 | [diff] [blame] | 3115 | skb_shinfo(nskb)->tx_flags = skb_shinfo(head_skb)->tx_flags & |
| 3116 | SKBTX_SHARED_FRAG; |
Eric Dumazet | cef401d | 2013-01-25 20:34:37 +0000 | [diff] [blame] | 3117 | |
Herbert Xu | 9d8506c | 2013-11-21 11:10:04 -0800 | [diff] [blame] | 3118 | while (pos < offset + len) { |
| 3119 | if (i >= nfrags) { |
Michael S. Tsirkin | 1a4ceda | 2014-03-10 19:27:59 +0200 | [diff] [blame] | 3120 | BUG_ON(skb_headlen(list_skb)); |
Herbert Xu | 9d8506c | 2013-11-21 11:10:04 -0800 | [diff] [blame] | 3121 | |
| 3122 | i = 0; |
Michael S. Tsirkin | 1a4ceda | 2014-03-10 19:27:59 +0200 | [diff] [blame] | 3123 | nfrags = skb_shinfo(list_skb)->nr_frags; |
| 3124 | frag = skb_shinfo(list_skb)->frags; |
Michael S. Tsirkin | 1fd819e | 2014-03-10 19:28:08 +0200 | [diff] [blame] | 3125 | frag_skb = list_skb; |
Herbert Xu | 9d8506c | 2013-11-21 11:10:04 -0800 | [diff] [blame] | 3126 | |
| 3127 | BUG_ON(!nfrags); |
| 3128 | |
Michael S. Tsirkin | 1a4ceda | 2014-03-10 19:27:59 +0200 | [diff] [blame] | 3129 | list_skb = list_skb->next; |
Herbert Xu | 9d8506c | 2013-11-21 11:10:04 -0800 | [diff] [blame] | 3130 | } |
| 3131 | |
| 3132 | if (unlikely(skb_shinfo(nskb)->nr_frags >= |
| 3133 | MAX_SKB_FRAGS)) { |
| 3134 | net_warn_ratelimited( |
| 3135 | "skb_segment: too many frags: %u %u\n", |
| 3136 | pos, mss); |
| 3137 | goto err; |
| 3138 | } |
| 3139 | |
Michael S. Tsirkin | 1fd819e | 2014-03-10 19:28:08 +0200 | [diff] [blame] | 3140 | if (unlikely(skb_orphan_frags(frag_skb, GFP_ATOMIC))) |
| 3141 | goto err; |
| 3142 | |
Michael S. Tsirkin | 4e1beba | 2014-03-10 18:29:14 +0200 | [diff] [blame] | 3143 | *nskb_frag = *frag; |
Michael S. Tsirkin | 8cb1990 | 2014-03-10 18:29:04 +0200 | [diff] [blame] | 3144 | __skb_frag_ref(nskb_frag); |
| 3145 | size = skb_frag_size(nskb_frag); |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 3146 | |
| 3147 | if (pos < offset) { |
Michael S. Tsirkin | 8cb1990 | 2014-03-10 18:29:04 +0200 | [diff] [blame] | 3148 | nskb_frag->page_offset += offset - pos; |
| 3149 | skb_frag_size_sub(nskb_frag, offset - pos); |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 3150 | } |
| 3151 | |
Herbert Xu | 89319d38 | 2008-12-15 23:26:06 -0800 | [diff] [blame] | 3152 | skb_shinfo(nskb)->nr_frags++; |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 3153 | |
| 3154 | if (pos + size <= offset + len) { |
| 3155 | i++; |
Michael S. Tsirkin | 4e1beba | 2014-03-10 18:29:14 +0200 | [diff] [blame] | 3156 | frag++; |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 3157 | pos += size; |
| 3158 | } else { |
Michael S. Tsirkin | 8cb1990 | 2014-03-10 18:29:04 +0200 | [diff] [blame] | 3159 | skb_frag_size_sub(nskb_frag, pos + size - (offset + len)); |
Herbert Xu | 89319d38 | 2008-12-15 23:26:06 -0800 | [diff] [blame] | 3160 | goto skip_fraglist; |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 3161 | } |
| 3162 | |
Michael S. Tsirkin | 8cb1990 | 2014-03-10 18:29:04 +0200 | [diff] [blame] | 3163 | nskb_frag++; |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 3164 | } |
| 3165 | |
Herbert Xu | 89319d38 | 2008-12-15 23:26:06 -0800 | [diff] [blame] | 3166 | skip_fraglist: |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 3167 | nskb->data_len = len - hsize; |
| 3168 | nskb->len += nskb->data_len; |
| 3169 | nskb->truesize += nskb->data_len; |
Pravin B Shelar | ec5f061 | 2013-03-07 09:28:01 +0000 | [diff] [blame] | 3170 | |
Simon Horman | 1cdbcb7 | 2013-05-19 15:46:49 +0000 | [diff] [blame] | 3171 | perform_csum_check: |
Tom Herbert | e585f23 | 2014-11-04 09:06:54 -0800 | [diff] [blame] | 3172 | if (!csum && !nskb->remcsum_offload) { |
Pravin B Shelar | ec5f061 | 2013-03-07 09:28:01 +0000 | [diff] [blame] | 3173 | nskb->csum = skb_checksum(nskb, doffset, |
| 3174 | nskb->len - doffset, 0); |
| 3175 | nskb->ip_summed = CHECKSUM_NONE; |
Tom Herbert | 7e2b10c | 2014-06-04 17:20:02 -0700 | [diff] [blame] | 3176 | SKB_GSO_CB(nskb)->csum_start = |
| 3177 | skb_headroom(nskb) + doffset; |
Pravin B Shelar | ec5f061 | 2013-03-07 09:28:01 +0000 | [diff] [blame] | 3178 | } |
Michael S. Tsirkin | df5771f | 2014-03-10 18:29:19 +0200 | [diff] [blame] | 3179 | } while ((offset += len) < head_skb->len); |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 3180 | |
Eric Dumazet | bec3cfd | 2014-10-03 20:59:19 -0700 | [diff] [blame] | 3181 | /* Some callers want to get the end of the list. |
| 3182 | * Put it in segs->prev to avoid walking the list. |
| 3183 | * (see validate_xmit_skb_list() for example) |
| 3184 | */ |
| 3185 | segs->prev = tail; |
Toshiaki Makita | 432c856 | 2014-10-27 10:30:51 -0700 | [diff] [blame] | 3186 | |
| 3187 | /* Following permits correct backpressure, for protocols |
| 3188 | * using skb_set_owner_w(). |
| 3189 | * Idea is to tranfert ownership from head_skb to last segment. |
| 3190 | */ |
| 3191 | if (head_skb->destructor == sock_wfree) { |
| 3192 | swap(tail->truesize, head_skb->truesize); |
| 3193 | swap(tail->destructor, head_skb->destructor); |
| 3194 | swap(tail->sk, head_skb->sk); |
| 3195 | } |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 3196 | return segs; |
| 3197 | |
| 3198 | err: |
Eric Dumazet | 289dccb | 2013-12-20 14:29:08 -0800 | [diff] [blame] | 3199 | kfree_skb_list(segs); |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 3200 | return ERR_PTR(err); |
| 3201 | } |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 3202 | EXPORT_SYMBOL_GPL(skb_segment); |
| 3203 | |
Herbert Xu | 71d93b3 | 2008-12-15 23:42:33 -0800 | [diff] [blame] | 3204 | int skb_gro_receive(struct sk_buff **head, struct sk_buff *skb) |
| 3205 | { |
Eric Dumazet | 8a29111 | 2013-10-08 09:02:23 -0700 | [diff] [blame] | 3206 | struct skb_shared_info *pinfo, *skbinfo = skb_shinfo(skb); |
Herbert Xu | 67147ba | 2009-05-26 18:50:22 +0000 | [diff] [blame] | 3207 | unsigned int offset = skb_gro_offset(skb); |
| 3208 | unsigned int headlen = skb_headlen(skb); |
Eric Dumazet | 8a29111 | 2013-10-08 09:02:23 -0700 | [diff] [blame] | 3209 | struct sk_buff *nskb, *lp, *p = *head; |
| 3210 | unsigned int len = skb_gro_len(skb); |
Eric Dumazet | 715dc1f | 2012-05-02 23:33:21 +0000 | [diff] [blame] | 3211 | unsigned int delta_truesize; |
Eric Dumazet | 8a29111 | 2013-10-08 09:02:23 -0700 | [diff] [blame] | 3212 | unsigned int headroom; |
Herbert Xu | 71d93b3 | 2008-12-15 23:42:33 -0800 | [diff] [blame] | 3213 | |
Eric Dumazet | 8a29111 | 2013-10-08 09:02:23 -0700 | [diff] [blame] | 3214 | if (unlikely(p->len + len >= 65536)) |
Herbert Xu | 71d93b3 | 2008-12-15 23:42:33 -0800 | [diff] [blame] | 3215 | return -E2BIG; |
| 3216 | |
Eric Dumazet | 29e9824 | 2014-05-16 11:34:37 -0700 | [diff] [blame] | 3217 | lp = NAPI_GRO_CB(p)->last; |
Eric Dumazet | 8a29111 | 2013-10-08 09:02:23 -0700 | [diff] [blame] | 3218 | pinfo = skb_shinfo(lp); |
| 3219 | |
| 3220 | if (headlen <= offset) { |
Herbert Xu | 42da699 | 2009-05-26 18:50:19 +0000 | [diff] [blame] | 3221 | skb_frag_t *frag; |
Herbert Xu | 66e92fc | 2009-05-26 18:50:32 +0000 | [diff] [blame] | 3222 | skb_frag_t *frag2; |
Herbert Xu | 9aaa156 | 2009-05-26 18:50:33 +0000 | [diff] [blame] | 3223 | int i = skbinfo->nr_frags; |
| 3224 | int nr_frags = pinfo->nr_frags + i; |
Herbert Xu | 42da699 | 2009-05-26 18:50:19 +0000 | [diff] [blame] | 3225 | |
Herbert Xu | 66e92fc | 2009-05-26 18:50:32 +0000 | [diff] [blame] | 3226 | if (nr_frags > MAX_SKB_FRAGS) |
Eric Dumazet | 8a29111 | 2013-10-08 09:02:23 -0700 | [diff] [blame] | 3227 | goto merge; |
Herbert Xu | 81705ad | 2009-01-29 14:19:51 +0000 | [diff] [blame] | 3228 | |
Eric Dumazet | 8a29111 | 2013-10-08 09:02:23 -0700 | [diff] [blame] | 3229 | offset -= headlen; |
Herbert Xu | 9aaa156 | 2009-05-26 18:50:33 +0000 | [diff] [blame] | 3230 | pinfo->nr_frags = nr_frags; |
| 3231 | skbinfo->nr_frags = 0; |
Herbert Xu | f557206 | 2009-01-14 20:40:03 -0800 | [diff] [blame] | 3232 | |
Herbert Xu | 9aaa156 | 2009-05-26 18:50:33 +0000 | [diff] [blame] | 3233 | frag = pinfo->frags + nr_frags; |
| 3234 | frag2 = skbinfo->frags + i; |
Herbert Xu | 66e92fc | 2009-05-26 18:50:32 +0000 | [diff] [blame] | 3235 | do { |
| 3236 | *--frag = *--frag2; |
| 3237 | } while (--i); |
| 3238 | |
| 3239 | frag->page_offset += offset; |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 3240 | skb_frag_size_sub(frag, offset); |
Herbert Xu | 66e92fc | 2009-05-26 18:50:32 +0000 | [diff] [blame] | 3241 | |
Eric Dumazet | 715dc1f | 2012-05-02 23:33:21 +0000 | [diff] [blame] | 3242 | /* all fragments truesize : remove (head size + sk_buff) */ |
Alexander Duyck | ec47ea8 | 2012-05-04 14:26:56 +0000 | [diff] [blame] | 3243 | delta_truesize = skb->truesize - |
| 3244 | SKB_TRUESIZE(skb_end_offset(skb)); |
Eric Dumazet | 715dc1f | 2012-05-02 23:33:21 +0000 | [diff] [blame] | 3245 | |
Herbert Xu | f557206 | 2009-01-14 20:40:03 -0800 | [diff] [blame] | 3246 | skb->truesize -= skb->data_len; |
| 3247 | skb->len -= skb->data_len; |
| 3248 | skb->data_len = 0; |
| 3249 | |
Eric Dumazet | 715dc1f | 2012-05-02 23:33:21 +0000 | [diff] [blame] | 3250 | NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE; |
Herbert Xu | 5d38a07 | 2009-01-04 16:13:40 -0800 | [diff] [blame] | 3251 | goto done; |
Eric Dumazet | d7e8883 | 2012-04-30 08:10:34 +0000 | [diff] [blame] | 3252 | } else if (skb->head_frag) { |
| 3253 | int nr_frags = pinfo->nr_frags; |
| 3254 | skb_frag_t *frag = pinfo->frags + nr_frags; |
| 3255 | struct page *page = virt_to_head_page(skb->head); |
| 3256 | unsigned int first_size = headlen - offset; |
| 3257 | unsigned int first_offset; |
| 3258 | |
| 3259 | if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS) |
Eric Dumazet | 8a29111 | 2013-10-08 09:02:23 -0700 | [diff] [blame] | 3260 | goto merge; |
Eric Dumazet | d7e8883 | 2012-04-30 08:10:34 +0000 | [diff] [blame] | 3261 | |
| 3262 | first_offset = skb->data - |
| 3263 | (unsigned char *)page_address(page) + |
| 3264 | offset; |
| 3265 | |
| 3266 | pinfo->nr_frags = nr_frags + 1 + skbinfo->nr_frags; |
| 3267 | |
| 3268 | frag->page.p = page; |
| 3269 | frag->page_offset = first_offset; |
| 3270 | skb_frag_size_set(frag, first_size); |
| 3271 | |
| 3272 | memcpy(frag + 1, skbinfo->frags, sizeof(*frag) * skbinfo->nr_frags); |
| 3273 | /* We dont need to clear skbinfo->nr_frags here */ |
| 3274 | |
Eric Dumazet | 715dc1f | 2012-05-02 23:33:21 +0000 | [diff] [blame] | 3275 | delta_truesize = skb->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff)); |
Eric Dumazet | d7e8883 | 2012-04-30 08:10:34 +0000 | [diff] [blame] | 3276 | NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD; |
| 3277 | goto done; |
Eric Dumazet | 8a29111 | 2013-10-08 09:02:23 -0700 | [diff] [blame] | 3278 | } |
Eric Dumazet | 73d3fe6 | 2014-09-29 10:34:29 -0700 | [diff] [blame] | 3279 | /* switch back to head shinfo */ |
| 3280 | pinfo = skb_shinfo(p); |
| 3281 | |
Eric Dumazet | 8a29111 | 2013-10-08 09:02:23 -0700 | [diff] [blame] | 3282 | if (pinfo->frag_list) |
| 3283 | goto merge; |
| 3284 | if (skb_gro_len(p) != pinfo->gso_size) |
Herbert Xu | 69c0cab | 2009-11-17 05:18:18 -0800 | [diff] [blame] | 3285 | return -E2BIG; |
Herbert Xu | 71d93b3 | 2008-12-15 23:42:33 -0800 | [diff] [blame] | 3286 | |
| 3287 | headroom = skb_headroom(p); |
Eric Dumazet | 3d3be43 | 2010-09-01 00:50:51 +0000 | [diff] [blame] | 3288 | nskb = alloc_skb(headroom + skb_gro_offset(p), GFP_ATOMIC); |
Herbert Xu | 71d93b3 | 2008-12-15 23:42:33 -0800 | [diff] [blame] | 3289 | if (unlikely(!nskb)) |
| 3290 | return -ENOMEM; |
| 3291 | |
| 3292 | __copy_skb_header(nskb, p); |
| 3293 | nskb->mac_len = p->mac_len; |
| 3294 | |
| 3295 | skb_reserve(nskb, headroom); |
Herbert Xu | 8691173 | 2009-01-29 14:19:50 +0000 | [diff] [blame] | 3296 | __skb_put(nskb, skb_gro_offset(p)); |
Herbert Xu | 71d93b3 | 2008-12-15 23:42:33 -0800 | [diff] [blame] | 3297 | |
Herbert Xu | 8691173 | 2009-01-29 14:19:50 +0000 | [diff] [blame] | 3298 | skb_set_mac_header(nskb, skb_mac_header(p) - p->data); |
Herbert Xu | 71d93b3 | 2008-12-15 23:42:33 -0800 | [diff] [blame] | 3299 | skb_set_network_header(nskb, skb_network_offset(p)); |
| 3300 | skb_set_transport_header(nskb, skb_transport_offset(p)); |
| 3301 | |
Herbert Xu | 8691173 | 2009-01-29 14:19:50 +0000 | [diff] [blame] | 3302 | __skb_pull(p, skb_gro_offset(p)); |
| 3303 | memcpy(skb_mac_header(nskb), skb_mac_header(p), |
| 3304 | p->data - skb_mac_header(p)); |
Herbert Xu | 71d93b3 | 2008-12-15 23:42:33 -0800 | [diff] [blame] | 3305 | |
Herbert Xu | 71d93b3 | 2008-12-15 23:42:33 -0800 | [diff] [blame] | 3306 | skb_shinfo(nskb)->frag_list = p; |
Herbert Xu | 9aaa156 | 2009-05-26 18:50:33 +0000 | [diff] [blame] | 3307 | skb_shinfo(nskb)->gso_size = pinfo->gso_size; |
Herbert Xu | 622e0ca | 2010-05-20 23:07:56 -0700 | [diff] [blame] | 3308 | pinfo->gso_size = 0; |
Eric Dumazet | f4a775d | 2014-09-22 16:29:32 -0700 | [diff] [blame] | 3309 | __skb_header_release(p); |
Eric Dumazet | c3c7c25 | 2012-12-06 13:54:59 +0000 | [diff] [blame] | 3310 | NAPI_GRO_CB(nskb)->last = p; |
Herbert Xu | 71d93b3 | 2008-12-15 23:42:33 -0800 | [diff] [blame] | 3311 | |
| 3312 | nskb->data_len += p->len; |
Eric Dumazet | de8261c | 2012-02-13 04:09:20 +0000 | [diff] [blame] | 3313 | nskb->truesize += p->truesize; |
Herbert Xu | 71d93b3 | 2008-12-15 23:42:33 -0800 | [diff] [blame] | 3314 | nskb->len += p->len; |
| 3315 | |
| 3316 | *head = nskb; |
| 3317 | nskb->next = p->next; |
| 3318 | p->next = NULL; |
| 3319 | |
| 3320 | p = nskb; |
| 3321 | |
| 3322 | merge: |
Eric Dumazet | 715dc1f | 2012-05-02 23:33:21 +0000 | [diff] [blame] | 3323 | delta_truesize = skb->truesize; |
Herbert Xu | 67147ba | 2009-05-26 18:50:22 +0000 | [diff] [blame] | 3324 | if (offset > headlen) { |
Michal Schmidt | d1dc7ab | 2011-01-24 12:08:48 +0000 | [diff] [blame] | 3325 | unsigned int eat = offset - headlen; |
| 3326 | |
| 3327 | skbinfo->frags[0].page_offset += eat; |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 3328 | skb_frag_size_sub(&skbinfo->frags[0], eat); |
Michal Schmidt | d1dc7ab | 2011-01-24 12:08:48 +0000 | [diff] [blame] | 3329 | skb->data_len -= eat; |
| 3330 | skb->len -= eat; |
Herbert Xu | 67147ba | 2009-05-26 18:50:22 +0000 | [diff] [blame] | 3331 | offset = headlen; |
Herbert Xu | 5603502 | 2009-02-05 21:26:52 -0800 | [diff] [blame] | 3332 | } |
| 3333 | |
Herbert Xu | 67147ba | 2009-05-26 18:50:22 +0000 | [diff] [blame] | 3334 | __skb_pull(skb, offset); |
Herbert Xu | 5603502 | 2009-02-05 21:26:52 -0800 | [diff] [blame] | 3335 | |
Eric Dumazet | 29e9824 | 2014-05-16 11:34:37 -0700 | [diff] [blame] | 3336 | if (NAPI_GRO_CB(p)->last == p) |
Eric Dumazet | 8a29111 | 2013-10-08 09:02:23 -0700 | [diff] [blame] | 3337 | skb_shinfo(p)->frag_list = skb; |
| 3338 | else |
| 3339 | NAPI_GRO_CB(p)->last->next = skb; |
Eric Dumazet | c3c7c25 | 2012-12-06 13:54:59 +0000 | [diff] [blame] | 3340 | NAPI_GRO_CB(p)->last = skb; |
Eric Dumazet | f4a775d | 2014-09-22 16:29:32 -0700 | [diff] [blame] | 3341 | __skb_header_release(skb); |
Eric Dumazet | 8a29111 | 2013-10-08 09:02:23 -0700 | [diff] [blame] | 3342 | lp = p; |
Herbert Xu | 71d93b3 | 2008-12-15 23:42:33 -0800 | [diff] [blame] | 3343 | |
Herbert Xu | 5d38a07 | 2009-01-04 16:13:40 -0800 | [diff] [blame] | 3344 | done: |
| 3345 | NAPI_GRO_CB(p)->count++; |
Herbert Xu | 37fe473 | 2009-01-17 19:48:13 +0000 | [diff] [blame] | 3346 | p->data_len += len; |
Eric Dumazet | 715dc1f | 2012-05-02 23:33:21 +0000 | [diff] [blame] | 3347 | p->truesize += delta_truesize; |
Herbert Xu | 37fe473 | 2009-01-17 19:48:13 +0000 | [diff] [blame] | 3348 | p->len += len; |
Eric Dumazet | 8a29111 | 2013-10-08 09:02:23 -0700 | [diff] [blame] | 3349 | if (lp != p) { |
| 3350 | lp->data_len += len; |
| 3351 | lp->truesize += delta_truesize; |
| 3352 | lp->len += len; |
| 3353 | } |
Herbert Xu | 71d93b3 | 2008-12-15 23:42:33 -0800 | [diff] [blame] | 3354 | NAPI_GRO_CB(skb)->same_flow = 1; |
| 3355 | return 0; |
| 3356 | } |
Herbert Xu | 71d93b3 | 2008-12-15 23:42:33 -0800 | [diff] [blame] | 3357 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3358 | void __init skb_init(void) |
| 3359 | { |
| 3360 | skbuff_head_cache = kmem_cache_create("skbuff_head_cache", |
| 3361 | sizeof(struct sk_buff), |
| 3362 | 0, |
Alexey Dobriyan | e5d679f33 | 2006-08-26 19:25:52 -0700 | [diff] [blame] | 3363 | SLAB_HWCACHE_ALIGN|SLAB_PANIC, |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 3364 | NULL); |
David S. Miller | d179cd1 | 2005-08-17 14:57:30 -0700 | [diff] [blame] | 3365 | skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache", |
Eric Dumazet | d0bf4a9 | 2014-09-29 13:29:15 -0700 | [diff] [blame] | 3366 | sizeof(struct sk_buff_fclones), |
David S. Miller | d179cd1 | 2005-08-17 14:57:30 -0700 | [diff] [blame] | 3367 | 0, |
Alexey Dobriyan | e5d679f33 | 2006-08-26 19:25:52 -0700 | [diff] [blame] | 3368 | SLAB_HWCACHE_ALIGN|SLAB_PANIC, |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 3369 | NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3370 | } |
| 3371 | |
David Howells | 716ea3a | 2007-04-02 20:19:53 -0700 | [diff] [blame] | 3372 | /** |
| 3373 | * skb_to_sgvec - Fill a scatter-gather list from a socket buffer |
| 3374 | * @skb: Socket buffer containing the buffers to be mapped |
| 3375 | * @sg: The scatter-gather list to map into |
| 3376 | * @offset: The offset into the buffer's contents to start mapping |
| 3377 | * @len: Length of buffer space to be mapped |
| 3378 | * |
| 3379 | * Fill the specified scatter-gather list with mappings/pointers into a |
| 3380 | * region of the buffer space attached to a socket buffer. |
| 3381 | */ |
David S. Miller | 51c739d | 2007-10-30 21:29:29 -0700 | [diff] [blame] | 3382 | static int |
| 3383 | __skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len) |
David Howells | 716ea3a | 2007-04-02 20:19:53 -0700 | [diff] [blame] | 3384 | { |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 3385 | int start = skb_headlen(skb); |
| 3386 | int i, copy = start - offset; |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 3387 | struct sk_buff *frag_iter; |
David Howells | 716ea3a | 2007-04-02 20:19:53 -0700 | [diff] [blame] | 3388 | int elt = 0; |
| 3389 | |
| 3390 | if (copy > 0) { |
| 3391 | if (copy > len) |
| 3392 | copy = len; |
Jens Axboe | 642f14903 | 2007-10-24 11:20:47 +0200 | [diff] [blame] | 3393 | sg_set_buf(sg, skb->data + offset, copy); |
David Howells | 716ea3a | 2007-04-02 20:19:53 -0700 | [diff] [blame] | 3394 | elt++; |
| 3395 | if ((len -= copy) == 0) |
| 3396 | return elt; |
| 3397 | offset += copy; |
| 3398 | } |
| 3399 | |
| 3400 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 3401 | int end; |
David Howells | 716ea3a | 2007-04-02 20:19:53 -0700 | [diff] [blame] | 3402 | |
Ilpo Järvinen | 547b792 | 2008-07-25 21:43:18 -0700 | [diff] [blame] | 3403 | WARN_ON(start > offset + len); |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 3404 | |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 3405 | end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]); |
David Howells | 716ea3a | 2007-04-02 20:19:53 -0700 | [diff] [blame] | 3406 | if ((copy = end - offset) > 0) { |
| 3407 | skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; |
| 3408 | |
| 3409 | if (copy > len) |
| 3410 | copy = len; |
Ian Campbell | ea2ab69 | 2011-08-22 23:44:58 +0000 | [diff] [blame] | 3411 | sg_set_page(&sg[elt], skb_frag_page(frag), copy, |
Jens Axboe | 642f14903 | 2007-10-24 11:20:47 +0200 | [diff] [blame] | 3412 | frag->page_offset+offset-start); |
David Howells | 716ea3a | 2007-04-02 20:19:53 -0700 | [diff] [blame] | 3413 | elt++; |
| 3414 | if (!(len -= copy)) |
| 3415 | return elt; |
| 3416 | offset += copy; |
| 3417 | } |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 3418 | start = end; |
David Howells | 716ea3a | 2007-04-02 20:19:53 -0700 | [diff] [blame] | 3419 | } |
| 3420 | |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 3421 | skb_walk_frags(skb, frag_iter) { |
| 3422 | int end; |
David Howells | 716ea3a | 2007-04-02 20:19:53 -0700 | [diff] [blame] | 3423 | |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 3424 | WARN_ON(start > offset + len); |
David Howells | 716ea3a | 2007-04-02 20:19:53 -0700 | [diff] [blame] | 3425 | |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 3426 | end = start + frag_iter->len; |
| 3427 | if ((copy = end - offset) > 0) { |
| 3428 | if (copy > len) |
| 3429 | copy = len; |
| 3430 | elt += __skb_to_sgvec(frag_iter, sg+elt, offset - start, |
| 3431 | copy); |
| 3432 | if ((len -= copy) == 0) |
| 3433 | return elt; |
| 3434 | offset += copy; |
David Howells | 716ea3a | 2007-04-02 20:19:53 -0700 | [diff] [blame] | 3435 | } |
David S. Miller | fbb398a | 2009-06-09 00:18:59 -0700 | [diff] [blame] | 3436 | start = end; |
David Howells | 716ea3a | 2007-04-02 20:19:53 -0700 | [diff] [blame] | 3437 | } |
| 3438 | BUG_ON(len); |
| 3439 | return elt; |
| 3440 | } |
| 3441 | |
Fan Du | 25a91d8 | 2014-01-18 09:54:23 +0800 | [diff] [blame] | 3442 | /* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given |
| 3443 | * sglist without mark the sg which contain last skb data as the end. |
| 3444 | * So the caller can mannipulate sg list as will when padding new data after |
| 3445 | * the first call without calling sg_unmark_end to expend sg list. |
| 3446 | * |
| 3447 | * Scenario to use skb_to_sgvec_nomark: |
| 3448 | * 1. sg_init_table |
| 3449 | * 2. skb_to_sgvec_nomark(payload1) |
| 3450 | * 3. skb_to_sgvec_nomark(payload2) |
| 3451 | * |
| 3452 | * This is equivalent to: |
| 3453 | * 1. sg_init_table |
| 3454 | * 2. skb_to_sgvec(payload1) |
| 3455 | * 3. sg_unmark_end |
| 3456 | * 4. skb_to_sgvec(payload2) |
| 3457 | * |
| 3458 | * When mapping mutilple payload conditionally, skb_to_sgvec_nomark |
| 3459 | * is more preferable. |
| 3460 | */ |
| 3461 | int skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg, |
| 3462 | int offset, int len) |
| 3463 | { |
| 3464 | return __skb_to_sgvec(skb, sg, offset, len); |
| 3465 | } |
| 3466 | EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark); |
| 3467 | |
David S. Miller | 51c739d | 2007-10-30 21:29:29 -0700 | [diff] [blame] | 3468 | int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len) |
| 3469 | { |
| 3470 | int nsg = __skb_to_sgvec(skb, sg, offset, len); |
| 3471 | |
Jens Axboe | c46f233 | 2007-10-31 12:06:37 +0100 | [diff] [blame] | 3472 | sg_mark_end(&sg[nsg - 1]); |
David S. Miller | 51c739d | 2007-10-30 21:29:29 -0700 | [diff] [blame] | 3473 | |
| 3474 | return nsg; |
| 3475 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 3476 | EXPORT_SYMBOL_GPL(skb_to_sgvec); |
David S. Miller | 51c739d | 2007-10-30 21:29:29 -0700 | [diff] [blame] | 3477 | |
David Howells | 716ea3a | 2007-04-02 20:19:53 -0700 | [diff] [blame] | 3478 | /** |
| 3479 | * skb_cow_data - Check that a socket buffer's data buffers are writable |
| 3480 | * @skb: The socket buffer to check. |
| 3481 | * @tailbits: Amount of trailing space to be added |
| 3482 | * @trailer: Returned pointer to the skb where the @tailbits space begins |
| 3483 | * |
| 3484 | * Make sure that the data buffers attached to a socket buffer are |
| 3485 | * writable. If they are not, private copies are made of the data buffers |
| 3486 | * and the socket buffer is set to use these instead. |
| 3487 | * |
| 3488 | * If @tailbits is given, make sure that there is space to write @tailbits |
| 3489 | * bytes of data beyond current end of socket buffer. @trailer will be |
| 3490 | * set to point to the skb in which this space begins. |
| 3491 | * |
| 3492 | * The number of scatterlist elements required to completely map the |
| 3493 | * COW'd and extended socket buffer will be returned. |
| 3494 | */ |
| 3495 | int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer) |
| 3496 | { |
| 3497 | int copyflag; |
| 3498 | int elt; |
| 3499 | struct sk_buff *skb1, **skb_p; |
| 3500 | |
| 3501 | /* If skb is cloned or its head is paged, reallocate |
| 3502 | * head pulling out all the pages (pages are considered not writable |
| 3503 | * at the moment even if they are anonymous). |
| 3504 | */ |
| 3505 | if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) && |
| 3506 | __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL) |
| 3507 | return -ENOMEM; |
| 3508 | |
| 3509 | /* Easy case. Most of packets will go this way. */ |
David S. Miller | 21dc330 | 2010-08-23 00:13:46 -0700 | [diff] [blame] | 3510 | if (!skb_has_frag_list(skb)) { |
David Howells | 716ea3a | 2007-04-02 20:19:53 -0700 | [diff] [blame] | 3511 | /* A little of trouble, not enough of space for trailer. |
| 3512 | * This should not happen, when stack is tuned to generate |
| 3513 | * good frames. OK, on miss we reallocate and reserve even more |
| 3514 | * space, 128 bytes is fair. */ |
| 3515 | |
| 3516 | if (skb_tailroom(skb) < tailbits && |
| 3517 | pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC)) |
| 3518 | return -ENOMEM; |
| 3519 | |
| 3520 | /* Voila! */ |
| 3521 | *trailer = skb; |
| 3522 | return 1; |
| 3523 | } |
| 3524 | |
| 3525 | /* Misery. We are in troubles, going to mincer fragments... */ |
| 3526 | |
| 3527 | elt = 1; |
| 3528 | skb_p = &skb_shinfo(skb)->frag_list; |
| 3529 | copyflag = 0; |
| 3530 | |
| 3531 | while ((skb1 = *skb_p) != NULL) { |
| 3532 | int ntail = 0; |
| 3533 | |
| 3534 | /* The fragment is partially pulled by someone, |
| 3535 | * this can happen on input. Copy it and everything |
| 3536 | * after it. */ |
| 3537 | |
| 3538 | if (skb_shared(skb1)) |
| 3539 | copyflag = 1; |
| 3540 | |
| 3541 | /* If the skb is the last, worry about trailer. */ |
| 3542 | |
| 3543 | if (skb1->next == NULL && tailbits) { |
| 3544 | if (skb_shinfo(skb1)->nr_frags || |
David S. Miller | 21dc330 | 2010-08-23 00:13:46 -0700 | [diff] [blame] | 3545 | skb_has_frag_list(skb1) || |
David Howells | 716ea3a | 2007-04-02 20:19:53 -0700 | [diff] [blame] | 3546 | skb_tailroom(skb1) < tailbits) |
| 3547 | ntail = tailbits + 128; |
| 3548 | } |
| 3549 | |
| 3550 | if (copyflag || |
| 3551 | skb_cloned(skb1) || |
| 3552 | ntail || |
| 3553 | skb_shinfo(skb1)->nr_frags || |
David S. Miller | 21dc330 | 2010-08-23 00:13:46 -0700 | [diff] [blame] | 3554 | skb_has_frag_list(skb1)) { |
David Howells | 716ea3a | 2007-04-02 20:19:53 -0700 | [diff] [blame] | 3555 | struct sk_buff *skb2; |
| 3556 | |
| 3557 | /* Fuck, we are miserable poor guys... */ |
| 3558 | if (ntail == 0) |
| 3559 | skb2 = skb_copy(skb1, GFP_ATOMIC); |
| 3560 | else |
| 3561 | skb2 = skb_copy_expand(skb1, |
| 3562 | skb_headroom(skb1), |
| 3563 | ntail, |
| 3564 | GFP_ATOMIC); |
| 3565 | if (unlikely(skb2 == NULL)) |
| 3566 | return -ENOMEM; |
| 3567 | |
| 3568 | if (skb1->sk) |
| 3569 | skb_set_owner_w(skb2, skb1->sk); |
| 3570 | |
| 3571 | /* Looking around. Are we still alive? |
| 3572 | * OK, link new skb, drop old one */ |
| 3573 | |
| 3574 | skb2->next = skb1->next; |
| 3575 | *skb_p = skb2; |
| 3576 | kfree_skb(skb1); |
| 3577 | skb1 = skb2; |
| 3578 | } |
| 3579 | elt++; |
| 3580 | *trailer = skb1; |
| 3581 | skb_p = &skb1->next; |
| 3582 | } |
| 3583 | |
| 3584 | return elt; |
| 3585 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 3586 | EXPORT_SYMBOL_GPL(skb_cow_data); |
David Howells | 716ea3a | 2007-04-02 20:19:53 -0700 | [diff] [blame] | 3587 | |
Eric Dumazet | b1faf56 | 2010-05-31 23:44:05 -0700 | [diff] [blame] | 3588 | static void sock_rmem_free(struct sk_buff *skb) |
| 3589 | { |
| 3590 | struct sock *sk = skb->sk; |
| 3591 | |
| 3592 | atomic_sub(skb->truesize, &sk->sk_rmem_alloc); |
| 3593 | } |
| 3594 | |
| 3595 | /* |
| 3596 | * Note: We dont mem charge error packets (no sk_forward_alloc changes) |
| 3597 | */ |
| 3598 | int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb) |
| 3599 | { |
| 3600 | if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >= |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 3601 | (unsigned int)sk->sk_rcvbuf) |
Eric Dumazet | b1faf56 | 2010-05-31 23:44:05 -0700 | [diff] [blame] | 3602 | return -ENOMEM; |
| 3603 | |
| 3604 | skb_orphan(skb); |
| 3605 | skb->sk = sk; |
| 3606 | skb->destructor = sock_rmem_free; |
| 3607 | atomic_add(skb->truesize, &sk->sk_rmem_alloc); |
| 3608 | |
Eric Dumazet | abb57ea | 2011-05-18 02:21:31 -0400 | [diff] [blame] | 3609 | /* before exiting rcu section, make sure dst is refcounted */ |
| 3610 | skb_dst_force(skb); |
| 3611 | |
Eric Dumazet | b1faf56 | 2010-05-31 23:44:05 -0700 | [diff] [blame] | 3612 | skb_queue_tail(&sk->sk_error_queue, skb); |
| 3613 | if (!sock_flag(sk, SOCK_DEAD)) |
David S. Miller | 676d236 | 2014-04-11 16:15:36 -0400 | [diff] [blame] | 3614 | sk->sk_data_ready(sk); |
Eric Dumazet | b1faf56 | 2010-05-31 23:44:05 -0700 | [diff] [blame] | 3615 | return 0; |
| 3616 | } |
| 3617 | EXPORT_SYMBOL(sock_queue_err_skb); |
| 3618 | |
Willem de Bruijn | 364a9e9 | 2014-08-31 21:30:27 -0400 | [diff] [blame] | 3619 | struct sk_buff *sock_dequeue_err_skb(struct sock *sk) |
| 3620 | { |
| 3621 | struct sk_buff_head *q = &sk->sk_error_queue; |
| 3622 | struct sk_buff *skb, *skb_next; |
| 3623 | int err = 0; |
| 3624 | |
| 3625 | spin_lock_bh(&q->lock); |
| 3626 | skb = __skb_dequeue(q); |
| 3627 | if (skb && (skb_next = skb_peek(q))) |
| 3628 | err = SKB_EXT_ERR(skb_next)->ee.ee_errno; |
| 3629 | spin_unlock_bh(&q->lock); |
| 3630 | |
| 3631 | sk->sk_err = err; |
| 3632 | if (err) |
| 3633 | sk->sk_error_report(sk); |
| 3634 | |
| 3635 | return skb; |
| 3636 | } |
| 3637 | EXPORT_SYMBOL(sock_dequeue_err_skb); |
| 3638 | |
Alexander Duyck | cab41c4 | 2014-09-10 18:05:26 -0400 | [diff] [blame] | 3639 | /** |
| 3640 | * skb_clone_sk - create clone of skb, and take reference to socket |
| 3641 | * @skb: the skb to clone |
| 3642 | * |
| 3643 | * This function creates a clone of a buffer that holds a reference on |
| 3644 | * sk_refcnt. Buffers created via this function are meant to be |
| 3645 | * returned using sock_queue_err_skb, or free via kfree_skb. |
| 3646 | * |
| 3647 | * When passing buffers allocated with this function to sock_queue_err_skb |
| 3648 | * it is necessary to wrap the call with sock_hold/sock_put in order to |
| 3649 | * prevent the socket from being released prior to being enqueued on |
| 3650 | * the sk_error_queue. |
| 3651 | */ |
Alexander Duyck | 62bccb8 | 2014-09-04 13:31:35 -0400 | [diff] [blame] | 3652 | struct sk_buff *skb_clone_sk(struct sk_buff *skb) |
| 3653 | { |
| 3654 | struct sock *sk = skb->sk; |
| 3655 | struct sk_buff *clone; |
| 3656 | |
| 3657 | if (!sk || !atomic_inc_not_zero(&sk->sk_refcnt)) |
| 3658 | return NULL; |
| 3659 | |
| 3660 | clone = skb_clone(skb, GFP_ATOMIC); |
| 3661 | if (!clone) { |
| 3662 | sock_put(sk); |
| 3663 | return NULL; |
| 3664 | } |
| 3665 | |
| 3666 | clone->sk = sk; |
| 3667 | clone->destructor = sock_efree; |
| 3668 | |
| 3669 | return clone; |
| 3670 | } |
| 3671 | EXPORT_SYMBOL(skb_clone_sk); |
| 3672 | |
Alexander Duyck | 37846ef | 2014-09-04 13:31:10 -0400 | [diff] [blame] | 3673 | static void __skb_complete_tx_timestamp(struct sk_buff *skb, |
| 3674 | struct sock *sk, |
| 3675 | int tstype) |
Patrick Ohly | ac45f60 | 2009-02-12 05:03:37 +0000 | [diff] [blame] | 3676 | { |
Patrick Ohly | ac45f60 | 2009-02-12 05:03:37 +0000 | [diff] [blame] | 3677 | struct sock_exterr_skb *serr; |
Patrick Ohly | ac45f60 | 2009-02-12 05:03:37 +0000 | [diff] [blame] | 3678 | int err; |
| 3679 | |
Patrick Ohly | ac45f60 | 2009-02-12 05:03:37 +0000 | [diff] [blame] | 3680 | serr = SKB_EXT_ERR(skb); |
| 3681 | memset(serr, 0, sizeof(*serr)); |
| 3682 | serr->ee.ee_errno = ENOMSG; |
| 3683 | serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING; |
Willem de Bruijn | e7fd288 | 2014-08-04 22:11:48 -0400 | [diff] [blame] | 3684 | serr->ee.ee_info = tstype; |
Willem de Bruijn | 4ed2d76 | 2014-08-04 22:11:49 -0400 | [diff] [blame] | 3685 | if (sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) { |
Willem de Bruijn | 09c2d25 | 2014-08-04 22:11:47 -0400 | [diff] [blame] | 3686 | serr->ee.ee_data = skb_shinfo(skb)->tskey; |
Willem de Bruijn | 4ed2d76 | 2014-08-04 22:11:49 -0400 | [diff] [blame] | 3687 | if (sk->sk_protocol == IPPROTO_TCP) |
| 3688 | serr->ee.ee_data -= sk->sk_tskey; |
| 3689 | } |
Eric Dumazet | 2903037 | 2010-05-29 00:20:48 -0700 | [diff] [blame] | 3690 | |
Patrick Ohly | ac45f60 | 2009-02-12 05:03:37 +0000 | [diff] [blame] | 3691 | err = sock_queue_err_skb(sk, skb); |
Eric Dumazet | 2903037 | 2010-05-29 00:20:48 -0700 | [diff] [blame] | 3692 | |
Patrick Ohly | ac45f60 | 2009-02-12 05:03:37 +0000 | [diff] [blame] | 3693 | if (err) |
| 3694 | kfree_skb(skb); |
| 3695 | } |
Alexander Duyck | 37846ef | 2014-09-04 13:31:10 -0400 | [diff] [blame] | 3696 | |
Willem de Bruijn | b245be1 | 2015-01-30 13:29:32 -0500 | [diff] [blame] | 3697 | static bool skb_may_tx_timestamp(struct sock *sk, bool tsonly) |
| 3698 | { |
| 3699 | bool ret; |
| 3700 | |
| 3701 | if (likely(sysctl_tstamp_allow_data || tsonly)) |
| 3702 | return true; |
| 3703 | |
| 3704 | read_lock_bh(&sk->sk_callback_lock); |
| 3705 | ret = sk->sk_socket && sk->sk_socket->file && |
| 3706 | file_ns_capable(sk->sk_socket->file, &init_user_ns, CAP_NET_RAW); |
| 3707 | read_unlock_bh(&sk->sk_callback_lock); |
| 3708 | return ret; |
| 3709 | } |
| 3710 | |
Alexander Duyck | 37846ef | 2014-09-04 13:31:10 -0400 | [diff] [blame] | 3711 | void skb_complete_tx_timestamp(struct sk_buff *skb, |
| 3712 | struct skb_shared_hwtstamps *hwtstamps) |
| 3713 | { |
| 3714 | struct sock *sk = skb->sk; |
| 3715 | |
Willem de Bruijn | b245be1 | 2015-01-30 13:29:32 -0500 | [diff] [blame] | 3716 | if (!skb_may_tx_timestamp(sk, false)) |
| 3717 | return; |
| 3718 | |
Alexander Duyck | 62bccb8 | 2014-09-04 13:31:35 -0400 | [diff] [blame] | 3719 | /* take a reference to prevent skb_orphan() from freeing the socket */ |
| 3720 | sock_hold(sk); |
Alexander Duyck | 37846ef | 2014-09-04 13:31:10 -0400 | [diff] [blame] | 3721 | |
Alexander Duyck | 62bccb8 | 2014-09-04 13:31:35 -0400 | [diff] [blame] | 3722 | *skb_hwtstamps(skb) = *hwtstamps; |
| 3723 | __skb_complete_tx_timestamp(skb, sk, SCM_TSTAMP_SND); |
Alexander Duyck | 37846ef | 2014-09-04 13:31:10 -0400 | [diff] [blame] | 3724 | |
| 3725 | sock_put(sk); |
| 3726 | } |
| 3727 | EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp); |
| 3728 | |
| 3729 | void __skb_tstamp_tx(struct sk_buff *orig_skb, |
| 3730 | struct skb_shared_hwtstamps *hwtstamps, |
| 3731 | struct sock *sk, int tstype) |
| 3732 | { |
| 3733 | struct sk_buff *skb; |
Willem de Bruijn | 49ca0d8 | 2015-01-30 13:29:31 -0500 | [diff] [blame] | 3734 | bool tsonly = sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TSONLY; |
Alexander Duyck | 37846ef | 2014-09-04 13:31:10 -0400 | [diff] [blame] | 3735 | |
Willem de Bruijn | b245be1 | 2015-01-30 13:29:32 -0500 | [diff] [blame] | 3736 | if (!sk || !skb_may_tx_timestamp(sk, tsonly)) |
Alexander Duyck | 37846ef | 2014-09-04 13:31:10 -0400 | [diff] [blame] | 3737 | return; |
| 3738 | |
Willem de Bruijn | 49ca0d8 | 2015-01-30 13:29:31 -0500 | [diff] [blame] | 3739 | if (tsonly) |
| 3740 | skb = alloc_skb(0, GFP_ATOMIC); |
Alexander Duyck | 37846ef | 2014-09-04 13:31:10 -0400 | [diff] [blame] | 3741 | else |
Willem de Bruijn | 49ca0d8 | 2015-01-30 13:29:31 -0500 | [diff] [blame] | 3742 | skb = skb_clone(orig_skb, GFP_ATOMIC); |
Alexander Duyck | 37846ef | 2014-09-04 13:31:10 -0400 | [diff] [blame] | 3743 | if (!skb) |
| 3744 | return; |
| 3745 | |
Willem de Bruijn | 49ca0d8 | 2015-01-30 13:29:31 -0500 | [diff] [blame] | 3746 | if (tsonly) { |
| 3747 | skb_shinfo(skb)->tx_flags = skb_shinfo(orig_skb)->tx_flags; |
| 3748 | skb_shinfo(skb)->tskey = skb_shinfo(orig_skb)->tskey; |
| 3749 | } |
| 3750 | |
| 3751 | if (hwtstamps) |
| 3752 | *skb_hwtstamps(skb) = *hwtstamps; |
| 3753 | else |
| 3754 | skb->tstamp = ktime_get_real(); |
| 3755 | |
Alexander Duyck | 37846ef | 2014-09-04 13:31:10 -0400 | [diff] [blame] | 3756 | __skb_complete_tx_timestamp(skb, sk, tstype); |
| 3757 | } |
Willem de Bruijn | e7fd288 | 2014-08-04 22:11:48 -0400 | [diff] [blame] | 3758 | EXPORT_SYMBOL_GPL(__skb_tstamp_tx); |
| 3759 | |
| 3760 | void skb_tstamp_tx(struct sk_buff *orig_skb, |
| 3761 | struct skb_shared_hwtstamps *hwtstamps) |
| 3762 | { |
| 3763 | return __skb_tstamp_tx(orig_skb, hwtstamps, orig_skb->sk, |
| 3764 | SCM_TSTAMP_SND); |
| 3765 | } |
Patrick Ohly | ac45f60 | 2009-02-12 05:03:37 +0000 | [diff] [blame] | 3766 | EXPORT_SYMBOL_GPL(skb_tstamp_tx); |
| 3767 | |
Johannes Berg | 6e3e939 | 2011-11-09 10:15:42 +0100 | [diff] [blame] | 3768 | void skb_complete_wifi_ack(struct sk_buff *skb, bool acked) |
| 3769 | { |
| 3770 | struct sock *sk = skb->sk; |
| 3771 | struct sock_exterr_skb *serr; |
| 3772 | int err; |
| 3773 | |
| 3774 | skb->wifi_acked_valid = 1; |
| 3775 | skb->wifi_acked = acked; |
| 3776 | |
| 3777 | serr = SKB_EXT_ERR(skb); |
| 3778 | memset(serr, 0, sizeof(*serr)); |
| 3779 | serr->ee.ee_errno = ENOMSG; |
| 3780 | serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS; |
| 3781 | |
Alexander Duyck | bf7fa55 | 2014-09-10 18:05:42 -0400 | [diff] [blame] | 3782 | /* take a reference to prevent skb_orphan() from freeing the socket */ |
| 3783 | sock_hold(sk); |
| 3784 | |
Johannes Berg | 6e3e939 | 2011-11-09 10:15:42 +0100 | [diff] [blame] | 3785 | err = sock_queue_err_skb(sk, skb); |
| 3786 | if (err) |
| 3787 | kfree_skb(skb); |
Alexander Duyck | bf7fa55 | 2014-09-10 18:05:42 -0400 | [diff] [blame] | 3788 | |
| 3789 | sock_put(sk); |
Johannes Berg | 6e3e939 | 2011-11-09 10:15:42 +0100 | [diff] [blame] | 3790 | } |
| 3791 | EXPORT_SYMBOL_GPL(skb_complete_wifi_ack); |
| 3792 | |
Patrick Ohly | ac45f60 | 2009-02-12 05:03:37 +0000 | [diff] [blame] | 3793 | |
Rusty Russell | f35d9d8 | 2008-02-04 23:49:54 -0500 | [diff] [blame] | 3794 | /** |
| 3795 | * skb_partial_csum_set - set up and verify partial csum values for packet |
| 3796 | * @skb: the skb to set |
| 3797 | * @start: the number of bytes after skb->data to start checksumming. |
| 3798 | * @off: the offset from start to place the checksum. |
| 3799 | * |
| 3800 | * For untrusted partially-checksummed packets, we need to make sure the values |
| 3801 | * for skb->csum_start and skb->csum_offset are valid so we don't oops. |
| 3802 | * |
| 3803 | * This function checks and sets those values and skb->ip_summed: if this |
| 3804 | * returns false you should drop the packet. |
| 3805 | */ |
| 3806 | bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off) |
| 3807 | { |
Herbert Xu | 5ff8dda | 2009-06-04 01:22:01 +0000 | [diff] [blame] | 3808 | if (unlikely(start > skb_headlen(skb)) || |
| 3809 | unlikely((int)start + off > skb_headlen(skb) - 2)) { |
Joe Perches | e87cc47 | 2012-05-13 21:56:26 +0000 | [diff] [blame] | 3810 | net_warn_ratelimited("bad partial csum: csum=%u/%u len=%u\n", |
| 3811 | start, off, skb_headlen(skb)); |
Rusty Russell | f35d9d8 | 2008-02-04 23:49:54 -0500 | [diff] [blame] | 3812 | return false; |
| 3813 | } |
| 3814 | skb->ip_summed = CHECKSUM_PARTIAL; |
| 3815 | skb->csum_start = skb_headroom(skb) + start; |
| 3816 | skb->csum_offset = off; |
Jason Wang | e5d5dec | 2013-03-26 23:11:20 +0000 | [diff] [blame] | 3817 | skb_set_transport_header(skb, start); |
Rusty Russell | f35d9d8 | 2008-02-04 23:49:54 -0500 | [diff] [blame] | 3818 | return true; |
| 3819 | } |
David S. Miller | b4ac530fc | 2009-02-10 02:09:24 -0800 | [diff] [blame] | 3820 | EXPORT_SYMBOL_GPL(skb_partial_csum_set); |
Rusty Russell | f35d9d8 | 2008-02-04 23:49:54 -0500 | [diff] [blame] | 3821 | |
Paul Durrant | ed1f50c | 2014-01-09 10:02:46 +0000 | [diff] [blame] | 3822 | static int skb_maybe_pull_tail(struct sk_buff *skb, unsigned int len, |
| 3823 | unsigned int max) |
| 3824 | { |
| 3825 | if (skb_headlen(skb) >= len) |
| 3826 | return 0; |
| 3827 | |
| 3828 | /* If we need to pullup then pullup to the max, so we |
| 3829 | * won't need to do it again. |
| 3830 | */ |
| 3831 | if (max > skb->len) |
| 3832 | max = skb->len; |
| 3833 | |
| 3834 | if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL) |
| 3835 | return -ENOMEM; |
| 3836 | |
| 3837 | if (skb_headlen(skb) < len) |
| 3838 | return -EPROTO; |
| 3839 | |
| 3840 | return 0; |
| 3841 | } |
| 3842 | |
Jan Beulich | f9708b4 | 2014-03-11 13:56:05 +0000 | [diff] [blame] | 3843 | #define MAX_TCP_HDR_LEN (15 * 4) |
| 3844 | |
| 3845 | static __sum16 *skb_checksum_setup_ip(struct sk_buff *skb, |
| 3846 | typeof(IPPROTO_IP) proto, |
| 3847 | unsigned int off) |
| 3848 | { |
| 3849 | switch (proto) { |
| 3850 | int err; |
| 3851 | |
| 3852 | case IPPROTO_TCP: |
| 3853 | err = skb_maybe_pull_tail(skb, off + sizeof(struct tcphdr), |
| 3854 | off + MAX_TCP_HDR_LEN); |
| 3855 | if (!err && !skb_partial_csum_set(skb, off, |
| 3856 | offsetof(struct tcphdr, |
| 3857 | check))) |
| 3858 | err = -EPROTO; |
| 3859 | return err ? ERR_PTR(err) : &tcp_hdr(skb)->check; |
| 3860 | |
| 3861 | case IPPROTO_UDP: |
| 3862 | err = skb_maybe_pull_tail(skb, off + sizeof(struct udphdr), |
| 3863 | off + sizeof(struct udphdr)); |
| 3864 | if (!err && !skb_partial_csum_set(skb, off, |
| 3865 | offsetof(struct udphdr, |
| 3866 | check))) |
| 3867 | err = -EPROTO; |
| 3868 | return err ? ERR_PTR(err) : &udp_hdr(skb)->check; |
| 3869 | } |
| 3870 | |
| 3871 | return ERR_PTR(-EPROTO); |
| 3872 | } |
| 3873 | |
Paul Durrant | ed1f50c | 2014-01-09 10:02:46 +0000 | [diff] [blame] | 3874 | /* This value should be large enough to cover a tagged ethernet header plus |
| 3875 | * maximally sized IP and TCP or UDP headers. |
| 3876 | */ |
| 3877 | #define MAX_IP_HDR_LEN 128 |
| 3878 | |
Jan Beulich | f9708b4 | 2014-03-11 13:56:05 +0000 | [diff] [blame] | 3879 | static int skb_checksum_setup_ipv4(struct sk_buff *skb, bool recalculate) |
Paul Durrant | ed1f50c | 2014-01-09 10:02:46 +0000 | [diff] [blame] | 3880 | { |
| 3881 | unsigned int off; |
| 3882 | bool fragment; |
Jan Beulich | f9708b4 | 2014-03-11 13:56:05 +0000 | [diff] [blame] | 3883 | __sum16 *csum; |
Paul Durrant | ed1f50c | 2014-01-09 10:02:46 +0000 | [diff] [blame] | 3884 | int err; |
| 3885 | |
| 3886 | fragment = false; |
| 3887 | |
| 3888 | err = skb_maybe_pull_tail(skb, |
| 3889 | sizeof(struct iphdr), |
| 3890 | MAX_IP_HDR_LEN); |
| 3891 | if (err < 0) |
| 3892 | goto out; |
| 3893 | |
| 3894 | if (ip_hdr(skb)->frag_off & htons(IP_OFFSET | IP_MF)) |
| 3895 | fragment = true; |
| 3896 | |
| 3897 | off = ip_hdrlen(skb); |
| 3898 | |
| 3899 | err = -EPROTO; |
| 3900 | |
| 3901 | if (fragment) |
| 3902 | goto out; |
| 3903 | |
Jan Beulich | f9708b4 | 2014-03-11 13:56:05 +0000 | [diff] [blame] | 3904 | csum = skb_checksum_setup_ip(skb, ip_hdr(skb)->protocol, off); |
| 3905 | if (IS_ERR(csum)) |
| 3906 | return PTR_ERR(csum); |
Paul Durrant | ed1f50c | 2014-01-09 10:02:46 +0000 | [diff] [blame] | 3907 | |
Jan Beulich | f9708b4 | 2014-03-11 13:56:05 +0000 | [diff] [blame] | 3908 | if (recalculate) |
| 3909 | *csum = ~csum_tcpudp_magic(ip_hdr(skb)->saddr, |
| 3910 | ip_hdr(skb)->daddr, |
| 3911 | skb->len - off, |
| 3912 | ip_hdr(skb)->protocol, 0); |
Paul Durrant | ed1f50c | 2014-01-09 10:02:46 +0000 | [diff] [blame] | 3913 | err = 0; |
| 3914 | |
| 3915 | out: |
| 3916 | return err; |
| 3917 | } |
| 3918 | |
| 3919 | /* This value should be large enough to cover a tagged ethernet header plus |
| 3920 | * an IPv6 header, all options, and a maximal TCP or UDP header. |
| 3921 | */ |
| 3922 | #define MAX_IPV6_HDR_LEN 256 |
| 3923 | |
| 3924 | #define OPT_HDR(type, skb, off) \ |
| 3925 | (type *)(skb_network_header(skb) + (off)) |
| 3926 | |
| 3927 | static int skb_checksum_setup_ipv6(struct sk_buff *skb, bool recalculate) |
| 3928 | { |
| 3929 | int err; |
| 3930 | u8 nexthdr; |
| 3931 | unsigned int off; |
| 3932 | unsigned int len; |
| 3933 | bool fragment; |
| 3934 | bool done; |
Jan Beulich | f9708b4 | 2014-03-11 13:56:05 +0000 | [diff] [blame] | 3935 | __sum16 *csum; |
Paul Durrant | ed1f50c | 2014-01-09 10:02:46 +0000 | [diff] [blame] | 3936 | |
| 3937 | fragment = false; |
| 3938 | done = false; |
| 3939 | |
| 3940 | off = sizeof(struct ipv6hdr); |
| 3941 | |
| 3942 | err = skb_maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN); |
| 3943 | if (err < 0) |
| 3944 | goto out; |
| 3945 | |
| 3946 | nexthdr = ipv6_hdr(skb)->nexthdr; |
| 3947 | |
| 3948 | len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len); |
| 3949 | while (off <= len && !done) { |
| 3950 | switch (nexthdr) { |
| 3951 | case IPPROTO_DSTOPTS: |
| 3952 | case IPPROTO_HOPOPTS: |
| 3953 | case IPPROTO_ROUTING: { |
| 3954 | struct ipv6_opt_hdr *hp; |
| 3955 | |
| 3956 | err = skb_maybe_pull_tail(skb, |
| 3957 | off + |
| 3958 | sizeof(struct ipv6_opt_hdr), |
| 3959 | MAX_IPV6_HDR_LEN); |
| 3960 | if (err < 0) |
| 3961 | goto out; |
| 3962 | |
| 3963 | hp = OPT_HDR(struct ipv6_opt_hdr, skb, off); |
| 3964 | nexthdr = hp->nexthdr; |
| 3965 | off += ipv6_optlen(hp); |
| 3966 | break; |
| 3967 | } |
| 3968 | case IPPROTO_AH: { |
| 3969 | struct ip_auth_hdr *hp; |
| 3970 | |
| 3971 | err = skb_maybe_pull_tail(skb, |
| 3972 | off + |
| 3973 | sizeof(struct ip_auth_hdr), |
| 3974 | MAX_IPV6_HDR_LEN); |
| 3975 | if (err < 0) |
| 3976 | goto out; |
| 3977 | |
| 3978 | hp = OPT_HDR(struct ip_auth_hdr, skb, off); |
| 3979 | nexthdr = hp->nexthdr; |
| 3980 | off += ipv6_authlen(hp); |
| 3981 | break; |
| 3982 | } |
| 3983 | case IPPROTO_FRAGMENT: { |
| 3984 | struct frag_hdr *hp; |
| 3985 | |
| 3986 | err = skb_maybe_pull_tail(skb, |
| 3987 | off + |
| 3988 | sizeof(struct frag_hdr), |
| 3989 | MAX_IPV6_HDR_LEN); |
| 3990 | if (err < 0) |
| 3991 | goto out; |
| 3992 | |
| 3993 | hp = OPT_HDR(struct frag_hdr, skb, off); |
| 3994 | |
| 3995 | if (hp->frag_off & htons(IP6_OFFSET | IP6_MF)) |
| 3996 | fragment = true; |
| 3997 | |
| 3998 | nexthdr = hp->nexthdr; |
| 3999 | off += sizeof(struct frag_hdr); |
| 4000 | break; |
| 4001 | } |
| 4002 | default: |
| 4003 | done = true; |
| 4004 | break; |
| 4005 | } |
| 4006 | } |
| 4007 | |
| 4008 | err = -EPROTO; |
| 4009 | |
| 4010 | if (!done || fragment) |
| 4011 | goto out; |
| 4012 | |
Jan Beulich | f9708b4 | 2014-03-11 13:56:05 +0000 | [diff] [blame] | 4013 | csum = skb_checksum_setup_ip(skb, nexthdr, off); |
| 4014 | if (IS_ERR(csum)) |
| 4015 | return PTR_ERR(csum); |
Paul Durrant | ed1f50c | 2014-01-09 10:02:46 +0000 | [diff] [blame] | 4016 | |
Jan Beulich | f9708b4 | 2014-03-11 13:56:05 +0000 | [diff] [blame] | 4017 | if (recalculate) |
| 4018 | *csum = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr, |
| 4019 | &ipv6_hdr(skb)->daddr, |
| 4020 | skb->len - off, nexthdr, 0); |
Paul Durrant | ed1f50c | 2014-01-09 10:02:46 +0000 | [diff] [blame] | 4021 | err = 0; |
| 4022 | |
| 4023 | out: |
| 4024 | return err; |
| 4025 | } |
| 4026 | |
| 4027 | /** |
| 4028 | * skb_checksum_setup - set up partial checksum offset |
| 4029 | * @skb: the skb to set up |
| 4030 | * @recalculate: if true the pseudo-header checksum will be recalculated |
| 4031 | */ |
| 4032 | int skb_checksum_setup(struct sk_buff *skb, bool recalculate) |
| 4033 | { |
| 4034 | int err; |
| 4035 | |
| 4036 | switch (skb->protocol) { |
| 4037 | case htons(ETH_P_IP): |
Jan Beulich | f9708b4 | 2014-03-11 13:56:05 +0000 | [diff] [blame] | 4038 | err = skb_checksum_setup_ipv4(skb, recalculate); |
Paul Durrant | ed1f50c | 2014-01-09 10:02:46 +0000 | [diff] [blame] | 4039 | break; |
| 4040 | |
| 4041 | case htons(ETH_P_IPV6): |
| 4042 | err = skb_checksum_setup_ipv6(skb, recalculate); |
| 4043 | break; |
| 4044 | |
| 4045 | default: |
| 4046 | err = -EPROTO; |
| 4047 | break; |
| 4048 | } |
| 4049 | |
| 4050 | return err; |
| 4051 | } |
| 4052 | EXPORT_SYMBOL(skb_checksum_setup); |
| 4053 | |
Ben Hutchings | 4497b07 | 2008-06-19 16:22:28 -0700 | [diff] [blame] | 4054 | void __skb_warn_lro_forwarding(const struct sk_buff *skb) |
| 4055 | { |
Joe Perches | e87cc47 | 2012-05-13 21:56:26 +0000 | [diff] [blame] | 4056 | net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n", |
| 4057 | skb->dev->name); |
Ben Hutchings | 4497b07 | 2008-06-19 16:22:28 -0700 | [diff] [blame] | 4058 | } |
Ben Hutchings | 4497b07 | 2008-06-19 16:22:28 -0700 | [diff] [blame] | 4059 | EXPORT_SYMBOL(__skb_warn_lro_forwarding); |
Eric Dumazet | bad43ca | 2012-05-19 03:02:02 +0000 | [diff] [blame] | 4060 | |
| 4061 | void kfree_skb_partial(struct sk_buff *skb, bool head_stolen) |
| 4062 | { |
Eric Dumazet | 3d861f6 | 2012-10-22 09:03:40 +0000 | [diff] [blame] | 4063 | if (head_stolen) { |
| 4064 | skb_release_head_state(skb); |
Eric Dumazet | bad43ca | 2012-05-19 03:02:02 +0000 | [diff] [blame] | 4065 | kmem_cache_free(skbuff_head_cache, skb); |
Eric Dumazet | 3d861f6 | 2012-10-22 09:03:40 +0000 | [diff] [blame] | 4066 | } else { |
Eric Dumazet | bad43ca | 2012-05-19 03:02:02 +0000 | [diff] [blame] | 4067 | __kfree_skb(skb); |
Eric Dumazet | 3d861f6 | 2012-10-22 09:03:40 +0000 | [diff] [blame] | 4068 | } |
Eric Dumazet | bad43ca | 2012-05-19 03:02:02 +0000 | [diff] [blame] | 4069 | } |
| 4070 | EXPORT_SYMBOL(kfree_skb_partial); |
| 4071 | |
| 4072 | /** |
| 4073 | * skb_try_coalesce - try to merge skb to prior one |
| 4074 | * @to: prior buffer |
| 4075 | * @from: buffer to add |
| 4076 | * @fragstolen: pointer to boolean |
Randy Dunlap | c6c4b97 | 2012-06-08 14:01:44 +0000 | [diff] [blame] | 4077 | * @delta_truesize: how much more was allocated than was requested |
Eric Dumazet | bad43ca | 2012-05-19 03:02:02 +0000 | [diff] [blame] | 4078 | */ |
| 4079 | bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from, |
| 4080 | bool *fragstolen, int *delta_truesize) |
| 4081 | { |
| 4082 | int i, delta, len = from->len; |
| 4083 | |
| 4084 | *fragstolen = false; |
| 4085 | |
| 4086 | if (skb_cloned(to)) |
| 4087 | return false; |
| 4088 | |
| 4089 | if (len <= skb_tailroom(to)) { |
Eric Dumazet | e93a043 | 2014-09-15 04:19:52 -0700 | [diff] [blame] | 4090 | if (len) |
| 4091 | BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len)); |
Eric Dumazet | bad43ca | 2012-05-19 03:02:02 +0000 | [diff] [blame] | 4092 | *delta_truesize = 0; |
| 4093 | return true; |
| 4094 | } |
| 4095 | |
| 4096 | if (skb_has_frag_list(to) || skb_has_frag_list(from)) |
| 4097 | return false; |
| 4098 | |
| 4099 | if (skb_headlen(from) != 0) { |
| 4100 | struct page *page; |
| 4101 | unsigned int offset; |
| 4102 | |
| 4103 | if (skb_shinfo(to)->nr_frags + |
| 4104 | skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS) |
| 4105 | return false; |
| 4106 | |
| 4107 | if (skb_head_is_locked(from)) |
| 4108 | return false; |
| 4109 | |
| 4110 | delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff)); |
| 4111 | |
| 4112 | page = virt_to_head_page(from->head); |
| 4113 | offset = from->data - (unsigned char *)page_address(page); |
| 4114 | |
| 4115 | skb_fill_page_desc(to, skb_shinfo(to)->nr_frags, |
| 4116 | page, offset, skb_headlen(from)); |
| 4117 | *fragstolen = true; |
| 4118 | } else { |
| 4119 | if (skb_shinfo(to)->nr_frags + |
| 4120 | skb_shinfo(from)->nr_frags > MAX_SKB_FRAGS) |
| 4121 | return false; |
| 4122 | |
Weiping Pan | f4b549a | 2012-09-28 20:15:30 +0000 | [diff] [blame] | 4123 | delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from)); |
Eric Dumazet | bad43ca | 2012-05-19 03:02:02 +0000 | [diff] [blame] | 4124 | } |
| 4125 | |
| 4126 | WARN_ON_ONCE(delta < len); |
| 4127 | |
| 4128 | memcpy(skb_shinfo(to)->frags + skb_shinfo(to)->nr_frags, |
| 4129 | skb_shinfo(from)->frags, |
| 4130 | skb_shinfo(from)->nr_frags * sizeof(skb_frag_t)); |
| 4131 | skb_shinfo(to)->nr_frags += skb_shinfo(from)->nr_frags; |
| 4132 | |
| 4133 | if (!skb_cloned(from)) |
| 4134 | skb_shinfo(from)->nr_frags = 0; |
| 4135 | |
Li RongQing | 8ea853f | 2012-09-18 16:53:21 +0000 | [diff] [blame] | 4136 | /* if the skb is not cloned this does nothing |
| 4137 | * since we set nr_frags to 0. |
| 4138 | */ |
Eric Dumazet | bad43ca | 2012-05-19 03:02:02 +0000 | [diff] [blame] | 4139 | for (i = 0; i < skb_shinfo(from)->nr_frags; i++) |
| 4140 | skb_frag_ref(from, i); |
| 4141 | |
| 4142 | to->truesize += delta; |
| 4143 | to->len += len; |
| 4144 | to->data_len += len; |
| 4145 | |
| 4146 | *delta_truesize = delta; |
| 4147 | return true; |
| 4148 | } |
| 4149 | EXPORT_SYMBOL(skb_try_coalesce); |
Nicolas Dichtel | 621e84d | 2013-06-26 16:11:27 +0200 | [diff] [blame] | 4150 | |
| 4151 | /** |
Nicolas Dichtel | 8b27f27 | 2013-09-02 15:34:56 +0200 | [diff] [blame] | 4152 | * skb_scrub_packet - scrub an skb |
Nicolas Dichtel | 621e84d | 2013-06-26 16:11:27 +0200 | [diff] [blame] | 4153 | * |
| 4154 | * @skb: buffer to clean |
Nicolas Dichtel | 8b27f27 | 2013-09-02 15:34:56 +0200 | [diff] [blame] | 4155 | * @xnet: packet is crossing netns |
Nicolas Dichtel | 621e84d | 2013-06-26 16:11:27 +0200 | [diff] [blame] | 4156 | * |
Nicolas Dichtel | 8b27f27 | 2013-09-02 15:34:56 +0200 | [diff] [blame] | 4157 | * skb_scrub_packet can be used after encapsulating or decapsulting a packet |
| 4158 | * into/from a tunnel. Some information have to be cleared during these |
| 4159 | * operations. |
| 4160 | * skb_scrub_packet can also be used to clean a skb before injecting it in |
| 4161 | * another namespace (@xnet == true). We have to clear all information in the |
| 4162 | * skb that could impact namespace isolation. |
Nicolas Dichtel | 621e84d | 2013-06-26 16:11:27 +0200 | [diff] [blame] | 4163 | */ |
Nicolas Dichtel | 8b27f27 | 2013-09-02 15:34:56 +0200 | [diff] [blame] | 4164 | void skb_scrub_packet(struct sk_buff *skb, bool xnet) |
Nicolas Dichtel | 621e84d | 2013-06-26 16:11:27 +0200 | [diff] [blame] | 4165 | { |
Nicolas Dichtel | 8b27f27 | 2013-09-02 15:34:56 +0200 | [diff] [blame] | 4166 | if (xnet) |
| 4167 | skb_orphan(skb); |
Nicolas Dichtel | 621e84d | 2013-06-26 16:11:27 +0200 | [diff] [blame] | 4168 | skb->tstamp.tv64 = 0; |
| 4169 | skb->pkt_type = PACKET_HOST; |
| 4170 | skb->skb_iif = 0; |
WANG Cong | 60ff746 | 2014-05-04 16:39:18 -0700 | [diff] [blame] | 4171 | skb->ignore_df = 0; |
Nicolas Dichtel | 621e84d | 2013-06-26 16:11:27 +0200 | [diff] [blame] | 4172 | skb_dst_drop(skb); |
| 4173 | skb->mark = 0; |
Eric Dumazet | 2bd8248 | 2015-02-03 23:48:24 -0800 | [diff] [blame] | 4174 | skb->sender_cpu = 0; |
Thomas Graf | b8fb4e0 | 2014-12-23 01:13:18 +0100 | [diff] [blame] | 4175 | skb_init_secmark(skb); |
Nicolas Dichtel | 621e84d | 2013-06-26 16:11:27 +0200 | [diff] [blame] | 4176 | secpath_reset(skb); |
| 4177 | nf_reset(skb); |
| 4178 | nf_reset_trace(skb); |
| 4179 | } |
| 4180 | EXPORT_SYMBOL_GPL(skb_scrub_packet); |
Florian Westphal | de960aa | 2014-01-26 10:58:16 +0100 | [diff] [blame] | 4181 | |
| 4182 | /** |
| 4183 | * skb_gso_transport_seglen - Return length of individual segments of a gso packet |
| 4184 | * |
| 4185 | * @skb: GSO skb |
| 4186 | * |
| 4187 | * skb_gso_transport_seglen is used to determine the real size of the |
| 4188 | * individual segments, including Layer4 headers (TCP/UDP). |
| 4189 | * |
| 4190 | * The MAC/L2 or network (IP, IPv6) headers are not accounted for. |
| 4191 | */ |
| 4192 | unsigned int skb_gso_transport_seglen(const struct sk_buff *skb) |
| 4193 | { |
| 4194 | const struct skb_shared_info *shinfo = skb_shinfo(skb); |
Florian Westphal | f993bc2 | 2014-10-20 13:49:18 +0200 | [diff] [blame] | 4195 | unsigned int thlen = 0; |
Florian Westphal | de960aa | 2014-01-26 10:58:16 +0100 | [diff] [blame] | 4196 | |
Florian Westphal | f993bc2 | 2014-10-20 13:49:18 +0200 | [diff] [blame] | 4197 | if (skb->encapsulation) { |
| 4198 | thlen = skb_inner_transport_header(skb) - |
| 4199 | skb_transport_header(skb); |
Florian Westphal | 6d39d58 | 2014-04-09 10:28:50 +0200 | [diff] [blame] | 4200 | |
Florian Westphal | f993bc2 | 2014-10-20 13:49:18 +0200 | [diff] [blame] | 4201 | if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) |
| 4202 | thlen += inner_tcp_hdrlen(skb); |
| 4203 | } else if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) { |
| 4204 | thlen = tcp_hdrlen(skb); |
| 4205 | } |
Florian Westphal | 6d39d58 | 2014-04-09 10:28:50 +0200 | [diff] [blame] | 4206 | /* UFO sets gso_size to the size of the fragmentation |
| 4207 | * payload, i.e. the size of the L4 (UDP) header is already |
| 4208 | * accounted for. |
| 4209 | */ |
Florian Westphal | f993bc2 | 2014-10-20 13:49:18 +0200 | [diff] [blame] | 4210 | return thlen + shinfo->gso_size; |
Florian Westphal | de960aa | 2014-01-26 10:58:16 +0100 | [diff] [blame] | 4211 | } |
| 4212 | EXPORT_SYMBOL_GPL(skb_gso_transport_seglen); |
Vlad Yasevich | 0d5501c | 2014-08-08 14:42:13 -0400 | [diff] [blame] | 4213 | |
| 4214 | static struct sk_buff *skb_reorder_vlan_header(struct sk_buff *skb) |
| 4215 | { |
| 4216 | if (skb_cow(skb, skb_headroom(skb)) < 0) { |
| 4217 | kfree_skb(skb); |
| 4218 | return NULL; |
| 4219 | } |
| 4220 | |
| 4221 | memmove(skb->data - ETH_HLEN, skb->data - VLAN_ETH_HLEN, 2 * ETH_ALEN); |
| 4222 | skb->mac_header += VLAN_HLEN; |
| 4223 | return skb; |
| 4224 | } |
| 4225 | |
| 4226 | struct sk_buff *skb_vlan_untag(struct sk_buff *skb) |
| 4227 | { |
| 4228 | struct vlan_hdr *vhdr; |
| 4229 | u16 vlan_tci; |
| 4230 | |
Jiri Pirko | df8a39d | 2015-01-13 17:13:44 +0100 | [diff] [blame] | 4231 | if (unlikely(skb_vlan_tag_present(skb))) { |
Vlad Yasevich | 0d5501c | 2014-08-08 14:42:13 -0400 | [diff] [blame] | 4232 | /* vlan_tci is already set-up so leave this for another time */ |
| 4233 | return skb; |
| 4234 | } |
| 4235 | |
| 4236 | skb = skb_share_check(skb, GFP_ATOMIC); |
| 4237 | if (unlikely(!skb)) |
| 4238 | goto err_free; |
| 4239 | |
| 4240 | if (unlikely(!pskb_may_pull(skb, VLAN_HLEN))) |
| 4241 | goto err_free; |
| 4242 | |
| 4243 | vhdr = (struct vlan_hdr *)skb->data; |
| 4244 | vlan_tci = ntohs(vhdr->h_vlan_TCI); |
| 4245 | __vlan_hwaccel_put_tag(skb, skb->protocol, vlan_tci); |
| 4246 | |
| 4247 | skb_pull_rcsum(skb, VLAN_HLEN); |
| 4248 | vlan_set_encap_proto(skb, vhdr); |
| 4249 | |
| 4250 | skb = skb_reorder_vlan_header(skb); |
| 4251 | if (unlikely(!skb)) |
| 4252 | goto err_free; |
| 4253 | |
| 4254 | skb_reset_network_header(skb); |
| 4255 | skb_reset_transport_header(skb); |
| 4256 | skb_reset_mac_len(skb); |
| 4257 | |
| 4258 | return skb; |
| 4259 | |
| 4260 | err_free: |
| 4261 | kfree_skb(skb); |
| 4262 | return NULL; |
| 4263 | } |
| 4264 | EXPORT_SYMBOL(skb_vlan_untag); |
Eric Dumazet | 2e4e441 | 2014-09-17 04:49:49 -0700 | [diff] [blame] | 4265 | |
Jiri Pirko | e219512 | 2014-11-19 14:05:01 +0100 | [diff] [blame] | 4266 | int skb_ensure_writable(struct sk_buff *skb, int write_len) |
| 4267 | { |
| 4268 | if (!pskb_may_pull(skb, write_len)) |
| 4269 | return -ENOMEM; |
| 4270 | |
| 4271 | if (!skb_cloned(skb) || skb_clone_writable(skb, write_len)) |
| 4272 | return 0; |
| 4273 | |
| 4274 | return pskb_expand_head(skb, 0, 0, GFP_ATOMIC); |
| 4275 | } |
| 4276 | EXPORT_SYMBOL(skb_ensure_writable); |
| 4277 | |
Jiri Pirko | 93515d5 | 2014-11-19 14:05:02 +0100 | [diff] [blame] | 4278 | /* remove VLAN header from packet and update csum accordingly. */ |
| 4279 | static int __skb_vlan_pop(struct sk_buff *skb, u16 *vlan_tci) |
| 4280 | { |
| 4281 | struct vlan_hdr *vhdr; |
| 4282 | unsigned int offset = skb->data - skb_mac_header(skb); |
| 4283 | int err; |
| 4284 | |
| 4285 | __skb_push(skb, offset); |
| 4286 | err = skb_ensure_writable(skb, VLAN_ETH_HLEN); |
| 4287 | if (unlikely(err)) |
| 4288 | goto pull; |
| 4289 | |
| 4290 | skb_postpull_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN); |
| 4291 | |
| 4292 | vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN); |
| 4293 | *vlan_tci = ntohs(vhdr->h_vlan_TCI); |
| 4294 | |
| 4295 | memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN); |
| 4296 | __skb_pull(skb, VLAN_HLEN); |
| 4297 | |
| 4298 | vlan_set_encap_proto(skb, vhdr); |
| 4299 | skb->mac_header += VLAN_HLEN; |
| 4300 | |
| 4301 | if (skb_network_offset(skb) < ETH_HLEN) |
| 4302 | skb_set_network_header(skb, ETH_HLEN); |
| 4303 | |
| 4304 | skb_reset_mac_len(skb); |
| 4305 | pull: |
| 4306 | __skb_pull(skb, offset); |
| 4307 | |
| 4308 | return err; |
| 4309 | } |
| 4310 | |
| 4311 | int skb_vlan_pop(struct sk_buff *skb) |
| 4312 | { |
| 4313 | u16 vlan_tci; |
| 4314 | __be16 vlan_proto; |
| 4315 | int err; |
| 4316 | |
Jiri Pirko | df8a39d | 2015-01-13 17:13:44 +0100 | [diff] [blame] | 4317 | if (likely(skb_vlan_tag_present(skb))) { |
Jiri Pirko | 93515d5 | 2014-11-19 14:05:02 +0100 | [diff] [blame] | 4318 | skb->vlan_tci = 0; |
| 4319 | } else { |
| 4320 | if (unlikely((skb->protocol != htons(ETH_P_8021Q) && |
| 4321 | skb->protocol != htons(ETH_P_8021AD)) || |
| 4322 | skb->len < VLAN_ETH_HLEN)) |
| 4323 | return 0; |
| 4324 | |
| 4325 | err = __skb_vlan_pop(skb, &vlan_tci); |
| 4326 | if (err) |
| 4327 | return err; |
| 4328 | } |
| 4329 | /* move next vlan tag to hw accel tag */ |
| 4330 | if (likely((skb->protocol != htons(ETH_P_8021Q) && |
| 4331 | skb->protocol != htons(ETH_P_8021AD)) || |
| 4332 | skb->len < VLAN_ETH_HLEN)) |
| 4333 | return 0; |
| 4334 | |
| 4335 | vlan_proto = skb->protocol; |
| 4336 | err = __skb_vlan_pop(skb, &vlan_tci); |
| 4337 | if (unlikely(err)) |
| 4338 | return err; |
| 4339 | |
| 4340 | __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci); |
| 4341 | return 0; |
| 4342 | } |
| 4343 | EXPORT_SYMBOL(skb_vlan_pop); |
| 4344 | |
| 4345 | int skb_vlan_push(struct sk_buff *skb, __be16 vlan_proto, u16 vlan_tci) |
| 4346 | { |
Jiri Pirko | df8a39d | 2015-01-13 17:13:44 +0100 | [diff] [blame] | 4347 | if (skb_vlan_tag_present(skb)) { |
Jiri Pirko | 93515d5 | 2014-11-19 14:05:02 +0100 | [diff] [blame] | 4348 | unsigned int offset = skb->data - skb_mac_header(skb); |
| 4349 | int err; |
| 4350 | |
| 4351 | /* __vlan_insert_tag expect skb->data pointing to mac header. |
| 4352 | * So change skb->data before calling it and change back to |
| 4353 | * original position later |
| 4354 | */ |
| 4355 | __skb_push(skb, offset); |
| 4356 | err = __vlan_insert_tag(skb, skb->vlan_proto, |
Jiri Pirko | df8a39d | 2015-01-13 17:13:44 +0100 | [diff] [blame] | 4357 | skb_vlan_tag_get(skb)); |
Jiri Pirko | 93515d5 | 2014-11-19 14:05:02 +0100 | [diff] [blame] | 4358 | if (err) |
| 4359 | return err; |
| 4360 | skb->protocol = skb->vlan_proto; |
| 4361 | skb->mac_len += VLAN_HLEN; |
| 4362 | __skb_pull(skb, offset); |
| 4363 | |
| 4364 | if (skb->ip_summed == CHECKSUM_COMPLETE) |
| 4365 | skb->csum = csum_add(skb->csum, csum_partial(skb->data |
| 4366 | + (2 * ETH_ALEN), VLAN_HLEN, 0)); |
| 4367 | } |
| 4368 | __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci); |
| 4369 | return 0; |
| 4370 | } |
| 4371 | EXPORT_SYMBOL(skb_vlan_push); |
| 4372 | |
Eric Dumazet | 2e4e441 | 2014-09-17 04:49:49 -0700 | [diff] [blame] | 4373 | /** |
| 4374 | * alloc_skb_with_frags - allocate skb with page frags |
| 4375 | * |
Masanari Iida | de3f0d0 | 2014-10-09 12:58:08 +0900 | [diff] [blame] | 4376 | * @header_len: size of linear part |
| 4377 | * @data_len: needed length in frags |
| 4378 | * @max_page_order: max page order desired. |
| 4379 | * @errcode: pointer to error code if any |
| 4380 | * @gfp_mask: allocation mask |
Eric Dumazet | 2e4e441 | 2014-09-17 04:49:49 -0700 | [diff] [blame] | 4381 | * |
| 4382 | * This can be used to allocate a paged skb, given a maximal order for frags. |
| 4383 | */ |
| 4384 | struct sk_buff *alloc_skb_with_frags(unsigned long header_len, |
| 4385 | unsigned long data_len, |
| 4386 | int max_page_order, |
| 4387 | int *errcode, |
| 4388 | gfp_t gfp_mask) |
| 4389 | { |
| 4390 | int npages = (data_len + (PAGE_SIZE - 1)) >> PAGE_SHIFT; |
| 4391 | unsigned long chunk; |
| 4392 | struct sk_buff *skb; |
| 4393 | struct page *page; |
| 4394 | gfp_t gfp_head; |
| 4395 | int i; |
| 4396 | |
| 4397 | *errcode = -EMSGSIZE; |
| 4398 | /* Note this test could be relaxed, if we succeed to allocate |
| 4399 | * high order pages... |
| 4400 | */ |
| 4401 | if (npages > MAX_SKB_FRAGS) |
| 4402 | return NULL; |
| 4403 | |
| 4404 | gfp_head = gfp_mask; |
| 4405 | if (gfp_head & __GFP_WAIT) |
| 4406 | gfp_head |= __GFP_REPEAT; |
| 4407 | |
| 4408 | *errcode = -ENOBUFS; |
| 4409 | skb = alloc_skb(header_len, gfp_head); |
| 4410 | if (!skb) |
| 4411 | return NULL; |
| 4412 | |
| 4413 | skb->truesize += npages << PAGE_SHIFT; |
| 4414 | |
| 4415 | for (i = 0; npages > 0; i++) { |
| 4416 | int order = max_page_order; |
| 4417 | |
| 4418 | while (order) { |
| 4419 | if (npages >= 1 << order) { |
| 4420 | page = alloc_pages(gfp_mask | |
| 4421 | __GFP_COMP | |
| 4422 | __GFP_NOWARN | |
| 4423 | __GFP_NORETRY, |
| 4424 | order); |
| 4425 | if (page) |
| 4426 | goto fill_page; |
| 4427 | /* Do not retry other high order allocations */ |
| 4428 | order = 1; |
| 4429 | max_page_order = 0; |
| 4430 | } |
| 4431 | order--; |
| 4432 | } |
| 4433 | page = alloc_page(gfp_mask); |
| 4434 | if (!page) |
| 4435 | goto failure; |
| 4436 | fill_page: |
| 4437 | chunk = min_t(unsigned long, data_len, |
| 4438 | PAGE_SIZE << order); |
| 4439 | skb_fill_page_desc(skb, i, page, 0, chunk); |
| 4440 | data_len -= chunk; |
| 4441 | npages -= 1 << order; |
| 4442 | } |
| 4443 | return skb; |
| 4444 | |
| 4445 | failure: |
| 4446 | kfree_skb(skb); |
| 4447 | return NULL; |
| 4448 | } |
| 4449 | EXPORT_SYMBOL(alloc_skb_with_frags); |