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