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