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