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