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