12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds * Routines having to do with the 'struct sk_buff' memory handlers.
41da177e4SLinus Torvalds *
5113aa838SAlan Cox * Authors: Alan Cox <alan@lxorguk.ukuu.org.uk>
61da177e4SLinus Torvalds * Florian La Roche <rzsfl@rz.uni-sb.de>
71da177e4SLinus Torvalds *
81da177e4SLinus Torvalds * Fixes:
91da177e4SLinus Torvalds * Alan Cox : Fixed the worst of the load
101da177e4SLinus Torvalds * balancer bugs.
111da177e4SLinus Torvalds * Dave Platt : Interrupt stacking fix.
121da177e4SLinus Torvalds * Richard Kooijman : Timestamp fixes.
131da177e4SLinus Torvalds * Alan Cox : Changed buffer format.
141da177e4SLinus Torvalds * Alan Cox : destructor hook for AF_UNIX etc.
151da177e4SLinus Torvalds * Linus Torvalds : Better skb_clone.
161da177e4SLinus Torvalds * Alan Cox : Added skb_copy.
171da177e4SLinus Torvalds * Alan Cox : Added all the changed routines Linus
181da177e4SLinus Torvalds * only put in the headers
191da177e4SLinus Torvalds * Ray VanTassle : Fixed --skb->lock in free
201da177e4SLinus Torvalds * Alan Cox : skb_copy copy arp field
211da177e4SLinus Torvalds * Andi Kleen : slabified it.
221da177e4SLinus Torvalds * Robert Olsson : Removed skb_head_pool
231da177e4SLinus Torvalds *
241da177e4SLinus Torvalds * NOTE:
251da177e4SLinus Torvalds * The __skb_ routines should be called with interrupts
261da177e4SLinus Torvalds * disabled, or you better be *real* sure that the operation is atomic
271da177e4SLinus Torvalds * with respect to whatever list is being frobbed (e.g. via lock_sock()
281da177e4SLinus Torvalds * or via disabling bottom half handlers, etc).
291da177e4SLinus Torvalds */
301da177e4SLinus Torvalds
311da177e4SLinus Torvalds /*
321da177e4SLinus Torvalds * The functions in this file will not compile correctly with gcc 2.4.x
331da177e4SLinus Torvalds */
341da177e4SLinus Torvalds
35e005d193SJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
36e005d193SJoe Perches
371da177e4SLinus Torvalds #include <linux/module.h>
381da177e4SLinus Torvalds #include <linux/types.h>
391da177e4SLinus Torvalds #include <linux/kernel.h>
401da177e4SLinus Torvalds #include <linux/mm.h>
411da177e4SLinus Torvalds #include <linux/interrupt.h>
421da177e4SLinus Torvalds #include <linux/in.h>
431da177e4SLinus Torvalds #include <linux/inet.h>
441da177e4SLinus Torvalds #include <linux/slab.h>
45de960aa9SFlorian Westphal #include <linux/tcp.h>
46de960aa9SFlorian Westphal #include <linux/udp.h>
4790017accSMarcelo Ricardo Leitner #include <linux/sctp.h>
481da177e4SLinus Torvalds #include <linux/netdevice.h>
491da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_ACT
501da177e4SLinus Torvalds #include <net/pkt_sched.h>
511da177e4SLinus Torvalds #endif
521da177e4SLinus Torvalds #include <linux/string.h>
531da177e4SLinus Torvalds #include <linux/skbuff.h>
549c55e01cSJens Axboe #include <linux/splice.h>
551da177e4SLinus Torvalds #include <linux/cache.h>
561da177e4SLinus Torvalds #include <linux/rtnetlink.h>
571da177e4SLinus Torvalds #include <linux/init.h>
58716ea3a7SDavid Howells #include <linux/scatterlist.h>
59ac45f602SPatrick Ohly #include <linux/errqueue.h>
60268bb0ceSLinus Torvalds #include <linux/prefetch.h>
61071c0fc6SJohannes Berg #include <linux/bitfield.h>
620d5501c1SVlad Yasevich #include <linux/if_vlan.h>
632a2ea508SJohn Hurley #include <linux/mpls.h>
64183f47fcSSebastian Andrzej Siewior #include <linux/kcov.h>
651da177e4SLinus Torvalds
661da177e4SLinus Torvalds #include <net/protocol.h>
671da177e4SLinus Torvalds #include <net/dst.h>
681da177e4SLinus Torvalds #include <net/sock.h>
691da177e4SLinus Torvalds #include <net/checksum.h>
70d457a0e3SEric Dumazet #include <net/gso.h>
71ed1f50c3SPaul Durrant #include <net/ip6_checksum.h>
721da177e4SLinus Torvalds #include <net/xfrm.h>
738822e270SJohn Hurley #include <net/mpls.h>
743ee17bc7SMat Martineau #include <net/mptcp.h>
7578476d31SJeremy Kerr #include <net/mctp.h>
7675eaf63eSAlexander Lobakin #include <net/page_pool/helpers.h>
77071c0fc6SJohannes Berg #include <net/dropreason.h>
781da177e4SLinus Torvalds
797c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
80ad8d75ffSSteven Rostedt #include <trace/events/skb.h>
8151c56b00SEric Dumazet #include <linux/highmem.h>
82b245be1fSWillem de Bruijn #include <linux/capability.h>
83b245be1fSWillem de Bruijn #include <linux/user_namespace.h>
842544af03SMatteo Croce #include <linux/indirect_call_wrapper.h>
852195e2a0SJakub Kicinski #include <linux/textsearch.h>
86a1f8e7f7SAl Viro
8739564c3fSEric Dumazet #include "dev.h"
887f678defSVasily Averin #include "sock_destructor.h"
897b7ed885SBart Van Assche
90025a785fSJakub Kicinski struct kmem_cache *skbuff_cache __ro_after_init;
9108009a76SAlexey Dobriyan static struct kmem_cache *skbuff_fclone_cache __ro_after_init;
92df5042f4SFlorian Westphal #ifdef CONFIG_SKB_EXTENSIONS
93df5042f4SFlorian Westphal static struct kmem_cache *skbuff_ext_cache __ro_after_init;
94df5042f4SFlorian Westphal #endif
95bf9f1baaSEric Dumazet
96bf9f1baaSEric Dumazet
97bf9f1baaSEric Dumazet static struct kmem_cache *skb_small_head_cache __ro_after_init;
98bf9f1baaSEric Dumazet
99bf9f1baaSEric Dumazet #define SKB_SMALL_HEAD_SIZE SKB_HEAD_ALIGN(MAX_TCP_HEADER)
100bf9f1baaSEric Dumazet
101bf9f1baaSEric Dumazet /* We want SKB_SMALL_HEAD_CACHE_SIZE to not be a power of two.
102bf9f1baaSEric Dumazet * This should ensure that SKB_SMALL_HEAD_HEADROOM is a unique
103bf9f1baaSEric Dumazet * size, and we can differentiate heads from skb_small_head_cache
104bf9f1baaSEric Dumazet * vs system slabs by looking at their size (skb_end_offset()).
105bf9f1baaSEric Dumazet */
106bf9f1baaSEric Dumazet #define SKB_SMALL_HEAD_CACHE_SIZE \
107bf9f1baaSEric Dumazet (is_power_of_2(SKB_SMALL_HEAD_SIZE) ? \
108bf9f1baaSEric Dumazet (SKB_SMALL_HEAD_SIZE + L1_CACHE_BYTES) : \
109bf9f1baaSEric Dumazet SKB_SMALL_HEAD_SIZE)
110bf9f1baaSEric Dumazet
111bf9f1baaSEric Dumazet #define SKB_SMALL_HEAD_HEADROOM \
112bf9f1baaSEric Dumazet SKB_WITH_OVERHEAD(SKB_SMALL_HEAD_CACHE_SIZE)
113bf9f1baaSEric Dumazet
1145f74f82eSHans Westgaard Ry int sysctl_max_skb_frags __read_mostly = MAX_SKB_FRAGS;
1155f74f82eSHans Westgaard Ry EXPORT_SYMBOL(sysctl_max_skb_frags);
1161da177e4SLinus Torvalds
1179cb252c4SMenglong Dong #undef FN
1189cb252c4SMenglong Dong #define FN(reason) [SKB_DROP_REASON_##reason] = #reason,
119071c0fc6SJohannes Berg static const char * const drop_reasons[] = {
1200e84afe8SEric Dumazet [SKB_CONSUMED] = "CONSUMED",
1219cb252c4SMenglong Dong DEFINE_DROP_REASON(FN, FN)
1229cb252c4SMenglong Dong };
123071c0fc6SJohannes Berg
124071c0fc6SJohannes Berg static const struct drop_reason_list drop_reasons_core = {
125071c0fc6SJohannes Berg .reasons = drop_reasons,
126071c0fc6SJohannes Berg .n_reasons = ARRAY_SIZE(drop_reasons),
127071c0fc6SJohannes Berg };
128071c0fc6SJohannes Berg
129071c0fc6SJohannes Berg const struct drop_reason_list __rcu *
130071c0fc6SJohannes Berg drop_reasons_by_subsys[SKB_DROP_REASON_SUBSYS_NUM] = {
131071c0fc6SJohannes Berg [SKB_DROP_REASON_SUBSYS_CORE] = RCU_INITIALIZER(&drop_reasons_core),
132071c0fc6SJohannes Berg };
133071c0fc6SJohannes Berg EXPORT_SYMBOL(drop_reasons_by_subsys);
134071c0fc6SJohannes Berg
135071c0fc6SJohannes Berg /**
136071c0fc6SJohannes Berg * drop_reasons_register_subsys - register another drop reason subsystem
137071c0fc6SJohannes Berg * @subsys: the subsystem to register, must not be the core
138071c0fc6SJohannes Berg * @list: the list of drop reasons within the subsystem, must point to
139071c0fc6SJohannes Berg * a statically initialized list
140071c0fc6SJohannes Berg */
drop_reasons_register_subsys(enum skb_drop_reason_subsys subsys,const struct drop_reason_list * list)141071c0fc6SJohannes Berg void drop_reasons_register_subsys(enum skb_drop_reason_subsys subsys,
142071c0fc6SJohannes Berg const struct drop_reason_list *list)
143071c0fc6SJohannes Berg {
144071c0fc6SJohannes Berg if (WARN(subsys <= SKB_DROP_REASON_SUBSYS_CORE ||
145071c0fc6SJohannes Berg subsys >= ARRAY_SIZE(drop_reasons_by_subsys),
146071c0fc6SJohannes Berg "invalid subsystem %d\n", subsys))
147071c0fc6SJohannes Berg return;
148071c0fc6SJohannes Berg
149071c0fc6SJohannes Berg /* must point to statically allocated memory, so INIT is OK */
150071c0fc6SJohannes Berg RCU_INIT_POINTER(drop_reasons_by_subsys[subsys], list);
151071c0fc6SJohannes Berg }
152071c0fc6SJohannes Berg EXPORT_SYMBOL_GPL(drop_reasons_register_subsys);
153071c0fc6SJohannes Berg
154071c0fc6SJohannes Berg /**
155071c0fc6SJohannes Berg * drop_reasons_unregister_subsys - unregister a drop reason subsystem
156071c0fc6SJohannes Berg * @subsys: the subsystem to remove, must not be the core
157071c0fc6SJohannes Berg *
158071c0fc6SJohannes Berg * Note: This will synchronize_rcu() to ensure no users when it returns.
159071c0fc6SJohannes Berg */
drop_reasons_unregister_subsys(enum skb_drop_reason_subsys subsys)160071c0fc6SJohannes Berg void drop_reasons_unregister_subsys(enum skb_drop_reason_subsys subsys)
161071c0fc6SJohannes Berg {
162071c0fc6SJohannes Berg if (WARN(subsys <= SKB_DROP_REASON_SUBSYS_CORE ||
163071c0fc6SJohannes Berg subsys >= ARRAY_SIZE(drop_reasons_by_subsys),
164071c0fc6SJohannes Berg "invalid subsystem %d\n", subsys))
165071c0fc6SJohannes Berg return;
166071c0fc6SJohannes Berg
167071c0fc6SJohannes Berg RCU_INIT_POINTER(drop_reasons_by_subsys[subsys], NULL);
168071c0fc6SJohannes Berg
169071c0fc6SJohannes Berg synchronize_rcu();
170071c0fc6SJohannes Berg }
171071c0fc6SJohannes Berg EXPORT_SYMBOL_GPL(drop_reasons_unregister_subsys);
172ec43908dSMenglong Dong
1731da177e4SLinus Torvalds /**
174f05de73bSJean Sacren * skb_panic - private function for out-of-line support
1751da177e4SLinus Torvalds * @skb: buffer
1761da177e4SLinus Torvalds * @sz: size
177f05de73bSJean Sacren * @addr: address
17899d5851eSJames Hogan * @msg: skb_over_panic or skb_under_panic
1791da177e4SLinus Torvalds *
180f05de73bSJean Sacren * Out-of-line support for skb_put() and skb_push().
181f05de73bSJean Sacren * Called via the wrapper skb_over_panic() or skb_under_panic().
182f05de73bSJean Sacren * Keep out of line to prevent kernel bloat.
183f05de73bSJean Sacren * __builtin_return_address is not used because it is not always reliable.
1841da177e4SLinus Torvalds */
skb_panic(struct sk_buff * skb,unsigned int sz,void * addr,const char msg[])185f05de73bSJean Sacren static void skb_panic(struct sk_buff *skb, unsigned int sz, void *addr,
18699d5851eSJames Hogan const char msg[])
1871da177e4SLinus Torvalds {
18841a46913SJesper Dangaard Brouer pr_emerg("%s: text:%px len:%d put:%d head:%px data:%px tail:%#lx end:%#lx dev:%s\n",
18999d5851eSJames Hogan msg, addr, skb->len, sz, skb->head, skb->data,
1904305b541SArnaldo Carvalho de Melo (unsigned long)skb->tail, (unsigned long)skb->end,
19126095455SPatrick McHardy skb->dev ? skb->dev->name : "<NULL>");
1921da177e4SLinus Torvalds BUG();
1931da177e4SLinus Torvalds }
1941da177e4SLinus Torvalds
skb_over_panic(struct sk_buff * skb,unsigned int sz,void * addr)195f05de73bSJean Sacren static void skb_over_panic(struct sk_buff *skb, unsigned int sz, void *addr)
1961da177e4SLinus Torvalds {
197f05de73bSJean Sacren skb_panic(skb, sz, addr, __func__);
1981da177e4SLinus Torvalds }
1991da177e4SLinus Torvalds
skb_under_panic(struct sk_buff * skb,unsigned int sz,void * addr)200f05de73bSJean Sacren static void skb_under_panic(struct sk_buff *skb, unsigned int sz, void *addr)
201f05de73bSJean Sacren {
202f05de73bSJean Sacren skb_panic(skb, sz, addr, __func__);
203f05de73bSJean Sacren }
204c93bdd0eSMel Gorman
20550fad4b5SAlexander Lobakin #define NAPI_SKB_CACHE_SIZE 64
206f450d539SAlexander Lobakin #define NAPI_SKB_CACHE_BULK 16
207f450d539SAlexander Lobakin #define NAPI_SKB_CACHE_HALF (NAPI_SKB_CACHE_SIZE / 2)
20850fad4b5SAlexander Lobakin
209dbae2b06SPaolo Abeni #if PAGE_SIZE == SZ_4K
210dbae2b06SPaolo Abeni
211dbae2b06SPaolo Abeni #define NAPI_HAS_SMALL_PAGE_FRAG 1
212dbae2b06SPaolo Abeni #define NAPI_SMALL_PAGE_PFMEMALLOC(nc) ((nc).pfmemalloc)
213dbae2b06SPaolo Abeni
214dbae2b06SPaolo Abeni /* specialized page frag allocator using a single order 0 page
215dbae2b06SPaolo Abeni * and slicing it into 1K sized fragment. Constrained to systems
216dbae2b06SPaolo Abeni * with a very limited amount of 1K fragments fitting a single
217dbae2b06SPaolo Abeni * page - to avoid excessive truesize underestimation
218dbae2b06SPaolo Abeni */
219dbae2b06SPaolo Abeni
220dbae2b06SPaolo Abeni struct page_frag_1k {
221dbae2b06SPaolo Abeni void *va;
222dbae2b06SPaolo Abeni u16 offset;
223dbae2b06SPaolo Abeni bool pfmemalloc;
224dbae2b06SPaolo Abeni };
225dbae2b06SPaolo Abeni
page_frag_alloc_1k(struct page_frag_1k * nc,gfp_t gfp)226dbae2b06SPaolo Abeni static void *page_frag_alloc_1k(struct page_frag_1k *nc, gfp_t gfp)
227dbae2b06SPaolo Abeni {
228dbae2b06SPaolo Abeni struct page *page;
229dbae2b06SPaolo Abeni int offset;
230dbae2b06SPaolo Abeni
231dbae2b06SPaolo Abeni offset = nc->offset - SZ_1K;
232dbae2b06SPaolo Abeni if (likely(offset >= 0))
233dbae2b06SPaolo Abeni goto use_frag;
234dbae2b06SPaolo Abeni
235dbae2b06SPaolo Abeni page = alloc_pages_node(NUMA_NO_NODE, gfp, 0);
236dbae2b06SPaolo Abeni if (!page)
237dbae2b06SPaolo Abeni return NULL;
238dbae2b06SPaolo Abeni
239dbae2b06SPaolo Abeni nc->va = page_address(page);
240dbae2b06SPaolo Abeni nc->pfmemalloc = page_is_pfmemalloc(page);
241dbae2b06SPaolo Abeni offset = PAGE_SIZE - SZ_1K;
242dbae2b06SPaolo Abeni page_ref_add(page, offset / SZ_1K);
243dbae2b06SPaolo Abeni
244dbae2b06SPaolo Abeni use_frag:
245dbae2b06SPaolo Abeni nc->offset = offset;
246dbae2b06SPaolo Abeni return nc->va + offset;
247dbae2b06SPaolo Abeni }
248dbae2b06SPaolo Abeni #else
249dbae2b06SPaolo Abeni
250dbae2b06SPaolo Abeni /* the small page is actually unused in this build; add dummy helpers
251dbae2b06SPaolo Abeni * to please the compiler and avoid later preprocessor's conditionals
252dbae2b06SPaolo Abeni */
253dbae2b06SPaolo Abeni #define NAPI_HAS_SMALL_PAGE_FRAG 0
254dbae2b06SPaolo Abeni #define NAPI_SMALL_PAGE_PFMEMALLOC(nc) false
255dbae2b06SPaolo Abeni
256dbae2b06SPaolo Abeni struct page_frag_1k {
257dbae2b06SPaolo Abeni };
258dbae2b06SPaolo Abeni
page_frag_alloc_1k(struct page_frag_1k * nc,gfp_t gfp_mask)259dbae2b06SPaolo Abeni static void *page_frag_alloc_1k(struct page_frag_1k *nc, gfp_t gfp_mask)
260dbae2b06SPaolo Abeni {
261dbae2b06SPaolo Abeni return NULL;
262dbae2b06SPaolo Abeni }
263dbae2b06SPaolo Abeni
264dbae2b06SPaolo Abeni #endif
265dbae2b06SPaolo Abeni
26650fad4b5SAlexander Lobakin struct napi_alloc_cache {
26750fad4b5SAlexander Lobakin struct page_frag_cache page;
268dbae2b06SPaolo Abeni struct page_frag_1k page_small;
26950fad4b5SAlexander Lobakin unsigned int skb_count;
27050fad4b5SAlexander Lobakin void *skb_cache[NAPI_SKB_CACHE_SIZE];
27150fad4b5SAlexander Lobakin };
27250fad4b5SAlexander Lobakin
27350fad4b5SAlexander Lobakin static DEFINE_PER_CPU(struct page_frag_cache, netdev_alloc_cache);
27450fad4b5SAlexander Lobakin static DEFINE_PER_CPU(struct napi_alloc_cache, napi_alloc_cache);
27550fad4b5SAlexander Lobakin
276dbae2b06SPaolo Abeni /* Double check that napi_get_frags() allocates skbs with
277dbae2b06SPaolo Abeni * skb->head being backed by slab, not a page fragment.
278dbae2b06SPaolo Abeni * This is to make sure bug fixed in 3226b158e67c
279dbae2b06SPaolo Abeni * ("net: avoid 32 x truesize under-estimation for tiny skbs")
280dbae2b06SPaolo Abeni * does not accidentally come back.
281dbae2b06SPaolo Abeni */
napi_get_frags_check(struct napi_struct * napi)282dbae2b06SPaolo Abeni void napi_get_frags_check(struct napi_struct *napi)
283dbae2b06SPaolo Abeni {
284dbae2b06SPaolo Abeni struct sk_buff *skb;
285dbae2b06SPaolo Abeni
286dbae2b06SPaolo Abeni local_bh_disable();
287dbae2b06SPaolo Abeni skb = napi_get_frags(napi);
288dbae2b06SPaolo Abeni WARN_ON_ONCE(!NAPI_HAS_SMALL_PAGE_FRAG && skb && skb->head_frag);
289dbae2b06SPaolo Abeni napi_free_frags(napi);
290dbae2b06SPaolo Abeni local_bh_enable();
291dbae2b06SPaolo Abeni }
292dbae2b06SPaolo Abeni
__napi_alloc_frag_align(unsigned int fragsz,unsigned int align_mask)29332e3573fSYajun Deng void *__napi_alloc_frag_align(unsigned int fragsz, unsigned int align_mask)
29450fad4b5SAlexander Lobakin {
29550fad4b5SAlexander Lobakin struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
29650fad4b5SAlexander Lobakin
29750fad4b5SAlexander Lobakin fragsz = SKB_DATA_ALIGN(fragsz);
29850fad4b5SAlexander Lobakin
29932e3573fSYajun Deng return page_frag_alloc_align(&nc->page, fragsz, GFP_ATOMIC, align_mask);
30050fad4b5SAlexander Lobakin }
30150fad4b5SAlexander Lobakin EXPORT_SYMBOL(__napi_alloc_frag_align);
30250fad4b5SAlexander Lobakin
__netdev_alloc_frag_align(unsigned int fragsz,unsigned int align_mask)30350fad4b5SAlexander Lobakin void *__netdev_alloc_frag_align(unsigned int fragsz, unsigned int align_mask)
30450fad4b5SAlexander Lobakin {
30550fad4b5SAlexander Lobakin void *data;
30650fad4b5SAlexander Lobakin
30750fad4b5SAlexander Lobakin fragsz = SKB_DATA_ALIGN(fragsz);
308afa79d08SChangbin Du if (in_hardirq() || irqs_disabled()) {
30932e3573fSYajun Deng struct page_frag_cache *nc = this_cpu_ptr(&netdev_alloc_cache);
31032e3573fSYajun Deng
31150fad4b5SAlexander Lobakin data = page_frag_alloc_align(nc, fragsz, GFP_ATOMIC, align_mask);
31250fad4b5SAlexander Lobakin } else {
31332e3573fSYajun Deng struct napi_alloc_cache *nc;
31432e3573fSYajun Deng
31550fad4b5SAlexander Lobakin local_bh_disable();
31632e3573fSYajun Deng nc = this_cpu_ptr(&napi_alloc_cache);
31732e3573fSYajun Deng data = page_frag_alloc_align(&nc->page, fragsz, GFP_ATOMIC, align_mask);
31850fad4b5SAlexander Lobakin local_bh_enable();
31950fad4b5SAlexander Lobakin }
32050fad4b5SAlexander Lobakin return data;
32150fad4b5SAlexander Lobakin }
32250fad4b5SAlexander Lobakin EXPORT_SYMBOL(__netdev_alloc_frag_align);
32350fad4b5SAlexander Lobakin
napi_skb_cache_get(void)324f450d539SAlexander Lobakin static struct sk_buff *napi_skb_cache_get(void)
325f450d539SAlexander Lobakin {
326f450d539SAlexander Lobakin struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
327f450d539SAlexander Lobakin struct sk_buff *skb;
328f450d539SAlexander Lobakin
32949ae83fcSSieng Piaw Liew if (unlikely(!nc->skb_count)) {
330025a785fSJakub Kicinski nc->skb_count = kmem_cache_alloc_bulk(skbuff_cache,
331f450d539SAlexander Lobakin GFP_ATOMIC,
332f450d539SAlexander Lobakin NAPI_SKB_CACHE_BULK,
333f450d539SAlexander Lobakin nc->skb_cache);
334f450d539SAlexander Lobakin if (unlikely(!nc->skb_count))
335f450d539SAlexander Lobakin return NULL;
33649ae83fcSSieng Piaw Liew }
337f450d539SAlexander Lobakin
338f450d539SAlexander Lobakin skb = nc->skb_cache[--nc->skb_count];
339025a785fSJakub Kicinski kasan_unpoison_object_data(skbuff_cache, skb);
340f450d539SAlexander Lobakin
341f450d539SAlexander Lobakin return skb;
342f450d539SAlexander Lobakin }
343f450d539SAlexander Lobakin
__finalize_skb_around(struct sk_buff * skb,void * data,unsigned int size)344ce098da1SKees Cook static inline void __finalize_skb_around(struct sk_buff *skb, void *data,
345ce098da1SKees Cook unsigned int size)
346ba0509b6SJesper Dangaard Brouer {
347ba0509b6SJesper Dangaard Brouer struct skb_shared_info *shinfo;
348ba0509b6SJesper Dangaard Brouer
349ba0509b6SJesper Dangaard Brouer size -= SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
350ba0509b6SJesper Dangaard Brouer
351ba0509b6SJesper Dangaard Brouer /* Assumes caller memset cleared SKB */
352ba0509b6SJesper Dangaard Brouer skb->truesize = SKB_TRUESIZE(size);
353ba0509b6SJesper Dangaard Brouer refcount_set(&skb->users, 1);
354ba0509b6SJesper Dangaard Brouer skb->head = data;
355ba0509b6SJesper Dangaard Brouer skb->data = data;
356ba0509b6SJesper Dangaard Brouer skb_reset_tail_pointer(skb);
357763087daSEric Dumazet skb_set_end_offset(skb, size);
358ba0509b6SJesper Dangaard Brouer skb->mac_header = (typeof(skb->mac_header))~0U;
359ba0509b6SJesper Dangaard Brouer skb->transport_header = (typeof(skb->transport_header))~0U;
36068822bdfSEric Dumazet skb->alloc_cpu = raw_smp_processor_id();
361ba0509b6SJesper Dangaard Brouer /* make sure we initialize shinfo sequentially */
362ba0509b6SJesper Dangaard Brouer shinfo = skb_shinfo(skb);
363ba0509b6SJesper Dangaard Brouer memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
364ba0509b6SJesper Dangaard Brouer atomic_set(&shinfo->dataref, 1);
365ba0509b6SJesper Dangaard Brouer
3666370cc3bSAleksandr Nogikh skb_set_kcov_handle(skb, kcov_common_handle());
367ba0509b6SJesper Dangaard Brouer }
368ba0509b6SJesper Dangaard Brouer
__slab_build_skb(struct sk_buff * skb,void * data,unsigned int * size)369ce098da1SKees Cook static inline void *__slab_build_skb(struct sk_buff *skb, void *data,
370ce098da1SKees Cook unsigned int *size)
371ce098da1SKees Cook {
372ce098da1SKees Cook void *resized;
373ce098da1SKees Cook
374ce098da1SKees Cook /* Must find the allocation size (and grow it to match). */
375ce098da1SKees Cook *size = ksize(data);
376ce098da1SKees Cook /* krealloc() will immediately return "data" when
377ce098da1SKees Cook * "ksize(data)" is requested: it is the existing upper
378ce098da1SKees Cook * bounds. As a result, GFP_ATOMIC will be ignored. Note
379ce098da1SKees Cook * that this "new" pointer needs to be passed back to the
380ce098da1SKees Cook * caller for use so the __alloc_size hinting will be
381ce098da1SKees Cook * tracked correctly.
382ce098da1SKees Cook */
383ce098da1SKees Cook resized = krealloc(data, *size, GFP_ATOMIC);
384ce098da1SKees Cook WARN_ON_ONCE(resized != data);
385ce098da1SKees Cook return resized;
386ce098da1SKees Cook }
387ce098da1SKees Cook
388ce098da1SKees Cook /* build_skb() variant which can operate on slab buffers.
389ce098da1SKees Cook * Note that this should be used sparingly as slab buffers
390ce098da1SKees Cook * cannot be combined efficiently by GRO!
391ce098da1SKees Cook */
slab_build_skb(void * data)392ce098da1SKees Cook struct sk_buff *slab_build_skb(void *data)
393ce098da1SKees Cook {
394ce098da1SKees Cook struct sk_buff *skb;
395ce098da1SKees Cook unsigned int size;
396ce098da1SKees Cook
397025a785fSJakub Kicinski skb = kmem_cache_alloc(skbuff_cache, GFP_ATOMIC);
398ce098da1SKees Cook if (unlikely(!skb))
399ce098da1SKees Cook return NULL;
400ce098da1SKees Cook
401ce098da1SKees Cook memset(skb, 0, offsetof(struct sk_buff, tail));
402ce098da1SKees Cook data = __slab_build_skb(skb, data, &size);
403ce098da1SKees Cook __finalize_skb_around(skb, data, size);
404ce098da1SKees Cook
405ce098da1SKees Cook return skb;
406ce098da1SKees Cook }
407ce098da1SKees Cook EXPORT_SYMBOL(slab_build_skb);
408ce098da1SKees Cook
409ce098da1SKees Cook /* Caller must provide SKB that is memset cleared */
__build_skb_around(struct sk_buff * skb,void * data,unsigned int frag_size)410ce098da1SKees Cook static void __build_skb_around(struct sk_buff *skb, void *data,
411ce098da1SKees Cook unsigned int frag_size)
412ce098da1SKees Cook {
413ce098da1SKees Cook unsigned int size = frag_size;
414ce098da1SKees Cook
415ce098da1SKees Cook /* frag_size == 0 is considered deprecated now. Callers
416ce098da1SKees Cook * using slab buffer should use slab_build_skb() instead.
417ce098da1SKees Cook */
418ce098da1SKees Cook if (WARN_ONCE(size == 0, "Use slab_build_skb() instead"))
419ce098da1SKees Cook data = __slab_build_skb(skb, data, &size);
420ce098da1SKees Cook
421ce098da1SKees Cook __finalize_skb_around(skb, data, size);
422ce098da1SKees Cook }
423ce098da1SKees Cook
4241da177e4SLinus Torvalds /**
4252ea2f62cSEric Dumazet * __build_skb - build a network buffer
426b2b5ce9dSEric Dumazet * @data: data buffer provided by caller
427ce098da1SKees Cook * @frag_size: size of data (must not be 0)
428b2b5ce9dSEric Dumazet *
429b2b5ce9dSEric Dumazet * Allocate a new &sk_buff. Caller provides space holding head and
430ce098da1SKees Cook * skb_shared_info. @data must have been allocated from the page
431ce098da1SKees Cook * allocator or vmalloc(). (A @frag_size of 0 to indicate a kmalloc()
432ce098da1SKees Cook * allocation is deprecated, and callers should use slab_build_skb()
433ce098da1SKees Cook * instead.)
434b2b5ce9dSEric Dumazet * The return is the new skb buffer.
435b2b5ce9dSEric Dumazet * On a failure the return is %NULL, and @data is not freed.
436b2b5ce9dSEric Dumazet * Notes :
437b2b5ce9dSEric Dumazet * Before IO, driver allocates only data buffer where NIC put incoming frame
438b2b5ce9dSEric Dumazet * Driver should add room at head (NET_SKB_PAD) and
439b2b5ce9dSEric Dumazet * MUST add room at tail (SKB_DATA_ALIGN(skb_shared_info))
440b2b5ce9dSEric Dumazet * After IO, driver calls build_skb(), to allocate sk_buff and populate it
441b2b5ce9dSEric Dumazet * before giving packet to stack.
442b2b5ce9dSEric Dumazet * RX rings only contains data buffers, not full skbs.
443b2b5ce9dSEric Dumazet */
__build_skb(void * data,unsigned int frag_size)4442ea2f62cSEric Dumazet struct sk_buff *__build_skb(void *data, unsigned int frag_size)
445b2b5ce9dSEric Dumazet {
446b2b5ce9dSEric Dumazet struct sk_buff *skb;
447b2b5ce9dSEric Dumazet
448025a785fSJakub Kicinski skb = kmem_cache_alloc(skbuff_cache, GFP_ATOMIC);
449ba0509b6SJesper Dangaard Brouer if (unlikely(!skb))
450b2b5ce9dSEric Dumazet return NULL;
451b2b5ce9dSEric Dumazet
452b2b5ce9dSEric Dumazet memset(skb, 0, offsetof(struct sk_buff, tail));
453483126b3SAlexander Lobakin __build_skb_around(skb, data, frag_size);
454b2b5ce9dSEric Dumazet
455483126b3SAlexander Lobakin return skb;
456b2b5ce9dSEric Dumazet }
4572ea2f62cSEric Dumazet
4582ea2f62cSEric Dumazet /* build_skb() is wrapper over __build_skb(), that specifically
4592ea2f62cSEric Dumazet * takes care of skb->head and skb->pfmemalloc
4602ea2f62cSEric Dumazet */
build_skb(void * data,unsigned int frag_size)4612ea2f62cSEric Dumazet struct sk_buff *build_skb(void *data, unsigned int frag_size)
4622ea2f62cSEric Dumazet {
4632ea2f62cSEric Dumazet struct sk_buff *skb = __build_skb(data, frag_size);
4642ea2f62cSEric Dumazet
4653c640126SGal Pressman if (likely(skb && frag_size)) {
4662ea2f62cSEric Dumazet skb->head_frag = 1;
467566b6701SGal Pressman skb_propagate_pfmemalloc(virt_to_head_page(data), skb);
4682ea2f62cSEric Dumazet }
4692ea2f62cSEric Dumazet return skb;
4702ea2f62cSEric Dumazet }
471b2b5ce9dSEric Dumazet EXPORT_SYMBOL(build_skb);
472b2b5ce9dSEric Dumazet
473ba0509b6SJesper Dangaard Brouer /**
474ba0509b6SJesper Dangaard Brouer * build_skb_around - build a network buffer around provided skb
475ba0509b6SJesper Dangaard Brouer * @skb: sk_buff provide by caller, must be memset cleared
476ba0509b6SJesper Dangaard Brouer * @data: data buffer provided by caller
47712c1604aSJakub Kicinski * @frag_size: size of data
478ba0509b6SJesper Dangaard Brouer */
build_skb_around(struct sk_buff * skb,void * data,unsigned int frag_size)479ba0509b6SJesper Dangaard Brouer struct sk_buff *build_skb_around(struct sk_buff *skb,
480ba0509b6SJesper Dangaard Brouer void *data, unsigned int frag_size)
481ba0509b6SJesper Dangaard Brouer {
482ba0509b6SJesper Dangaard Brouer if (unlikely(!skb))
483ba0509b6SJesper Dangaard Brouer return NULL;
484ba0509b6SJesper Dangaard Brouer
485483126b3SAlexander Lobakin __build_skb_around(skb, data, frag_size);
486ba0509b6SJesper Dangaard Brouer
487483126b3SAlexander Lobakin if (frag_size) {
488ba0509b6SJesper Dangaard Brouer skb->head_frag = 1;
489566b6701SGal Pressman skb_propagate_pfmemalloc(virt_to_head_page(data), skb);
490ba0509b6SJesper Dangaard Brouer }
491ba0509b6SJesper Dangaard Brouer return skb;
492ba0509b6SJesper Dangaard Brouer }
493ba0509b6SJesper Dangaard Brouer EXPORT_SYMBOL(build_skb_around);
494ba0509b6SJesper Dangaard Brouer
495f450d539SAlexander Lobakin /**
496f450d539SAlexander Lobakin * __napi_build_skb - build a network buffer
497f450d539SAlexander Lobakin * @data: data buffer provided by caller
49812c1604aSJakub Kicinski * @frag_size: size of data
499f450d539SAlexander Lobakin *
500f450d539SAlexander Lobakin * Version of __build_skb() that uses NAPI percpu caches to obtain
501f450d539SAlexander Lobakin * skbuff_head instead of inplace allocation.
502f450d539SAlexander Lobakin *
503f450d539SAlexander Lobakin * Returns a new &sk_buff on success, %NULL on allocation failure.
504f450d539SAlexander Lobakin */
__napi_build_skb(void * data,unsigned int frag_size)505f450d539SAlexander Lobakin static struct sk_buff *__napi_build_skb(void *data, unsigned int frag_size)
506f450d539SAlexander Lobakin {
507f450d539SAlexander Lobakin struct sk_buff *skb;
508f450d539SAlexander Lobakin
509f450d539SAlexander Lobakin skb = napi_skb_cache_get();
510f450d539SAlexander Lobakin if (unlikely(!skb))
511f450d539SAlexander Lobakin return NULL;
512f450d539SAlexander Lobakin
513f450d539SAlexander Lobakin memset(skb, 0, offsetof(struct sk_buff, tail));
514f450d539SAlexander Lobakin __build_skb_around(skb, data, frag_size);
515f450d539SAlexander Lobakin
516f450d539SAlexander Lobakin return skb;
517f450d539SAlexander Lobakin }
518f450d539SAlexander Lobakin
519f450d539SAlexander Lobakin /**
520f450d539SAlexander Lobakin * napi_build_skb - build a network buffer
521f450d539SAlexander Lobakin * @data: data buffer provided by caller
52212c1604aSJakub Kicinski * @frag_size: size of data
523f450d539SAlexander Lobakin *
524f450d539SAlexander Lobakin * Version of __napi_build_skb() that takes care of skb->head_frag
525f450d539SAlexander Lobakin * and skb->pfmemalloc when the data is a page or page fragment.
526f450d539SAlexander Lobakin *
527f450d539SAlexander Lobakin * Returns a new &sk_buff on success, %NULL on allocation failure.
528f450d539SAlexander Lobakin */
napi_build_skb(void * data,unsigned int frag_size)529f450d539SAlexander Lobakin struct sk_buff *napi_build_skb(void *data, unsigned int frag_size)
530f450d539SAlexander Lobakin {
531f450d539SAlexander Lobakin struct sk_buff *skb = __napi_build_skb(data, frag_size);
532f450d539SAlexander Lobakin
533f450d539SAlexander Lobakin if (likely(skb) && frag_size) {
534f450d539SAlexander Lobakin skb->head_frag = 1;
535f450d539SAlexander Lobakin skb_propagate_pfmemalloc(virt_to_head_page(data), skb);
536f450d539SAlexander Lobakin }
537f450d539SAlexander Lobakin
538f450d539SAlexander Lobakin return skb;
539f450d539SAlexander Lobakin }
540f450d539SAlexander Lobakin EXPORT_SYMBOL(napi_build_skb);
541f450d539SAlexander Lobakin
5425381b23dSAlexander Lobakin /*
5435381b23dSAlexander Lobakin * kmalloc_reserve is a wrapper around kmalloc_node_track_caller that tells
5445381b23dSAlexander Lobakin * the caller if emergency pfmemalloc reserves are being used. If it is and
5455381b23dSAlexander Lobakin * the socket is later found to be SOCK_MEMALLOC then PFMEMALLOC reserves
5465381b23dSAlexander Lobakin * may be used. Otherwise, the packet data may be discarded until enough
5475381b23dSAlexander Lobakin * memory is free
5485381b23dSAlexander Lobakin */
kmalloc_reserve(unsigned int * size,gfp_t flags,int node,bool * pfmemalloc)5495c0e820cSEric Dumazet static void *kmalloc_reserve(unsigned int *size, gfp_t flags, int node,
550ef28095fSAlexander Lobakin bool *pfmemalloc)
5515381b23dSAlexander Lobakin {
5525381b23dSAlexander Lobakin bool ret_pfmemalloc = false;
553915d975bSEric Dumazet size_t obj_size;
5545c0e820cSEric Dumazet void *obj;
5555381b23dSAlexander Lobakin
5565c0e820cSEric Dumazet obj_size = SKB_HEAD_ALIGN(*size);
557bf9f1baaSEric Dumazet if (obj_size <= SKB_SMALL_HEAD_CACHE_SIZE &&
558bf9f1baaSEric Dumazet !(flags & KMALLOC_NOT_NORMAL_BITS)) {
559bf9f1baaSEric Dumazet obj = kmem_cache_alloc_node(skb_small_head_cache,
560bf9f1baaSEric Dumazet flags | __GFP_NOMEMALLOC | __GFP_NOWARN,
561bf9f1baaSEric Dumazet node);
562bf9f1baaSEric Dumazet *size = SKB_SMALL_HEAD_CACHE_SIZE;
563880ce5f2SEric Dumazet if (obj || !(gfp_pfmemalloc_allowed(flags)))
564bf9f1baaSEric Dumazet goto out;
565880ce5f2SEric Dumazet /* Try again but now we are using pfmemalloc reserves */
566880ce5f2SEric Dumazet ret_pfmemalloc = true;
567880ce5f2SEric Dumazet obj = kmem_cache_alloc_node(skb_small_head_cache, flags, node);
568880ce5f2SEric Dumazet goto out;
569bf9f1baaSEric Dumazet }
570915d975bSEric Dumazet
571915d975bSEric Dumazet obj_size = kmalloc_size_roundup(obj_size);
572915d975bSEric Dumazet /* The following cast might truncate high-order bits of obj_size, this
573915d975bSEric Dumazet * is harmless because kmalloc(obj_size >= 2^32) will fail anyway.
574915d975bSEric Dumazet */
575915d975bSEric Dumazet *size = (unsigned int)obj_size;
576915d975bSEric Dumazet
5775381b23dSAlexander Lobakin /*
5785381b23dSAlexander Lobakin * Try a regular allocation, when that fails and we're not entitled
5795381b23dSAlexander Lobakin * to the reserves, fail.
5805381b23dSAlexander Lobakin */
5815c0e820cSEric Dumazet obj = kmalloc_node_track_caller(obj_size,
5825381b23dSAlexander Lobakin flags | __GFP_NOMEMALLOC | __GFP_NOWARN,
5835381b23dSAlexander Lobakin node);
5845381b23dSAlexander Lobakin if (obj || !(gfp_pfmemalloc_allowed(flags)))
5855381b23dSAlexander Lobakin goto out;
5865381b23dSAlexander Lobakin
5875381b23dSAlexander Lobakin /* Try again but now we are using pfmemalloc reserves */
5885381b23dSAlexander Lobakin ret_pfmemalloc = true;
5895c0e820cSEric Dumazet obj = kmalloc_node_track_caller(obj_size, flags, node);
5905381b23dSAlexander Lobakin
5915381b23dSAlexander Lobakin out:
5925381b23dSAlexander Lobakin if (pfmemalloc)
5935381b23dSAlexander Lobakin *pfmemalloc = ret_pfmemalloc;
5945381b23dSAlexander Lobakin
5955381b23dSAlexander Lobakin return obj;
5965381b23dSAlexander Lobakin }
5975381b23dSAlexander Lobakin
5985381b23dSAlexander Lobakin /* Allocate a new skbuff. We do this ourselves so we can fill in a few
5995381b23dSAlexander Lobakin * 'private' fields and also do memory statistics to find all the
6005381b23dSAlexander Lobakin * [BEEP] leaks.
6015381b23dSAlexander Lobakin *
6025381b23dSAlexander Lobakin */
6035381b23dSAlexander Lobakin
6045381b23dSAlexander Lobakin /**
6055381b23dSAlexander Lobakin * __alloc_skb - allocate a network buffer
6065381b23dSAlexander Lobakin * @size: size to allocate
6075381b23dSAlexander Lobakin * @gfp_mask: allocation mask
6085381b23dSAlexander Lobakin * @flags: If SKB_ALLOC_FCLONE is set, allocate from fclone cache
6095381b23dSAlexander Lobakin * instead of head cache and allocate a cloned (child) skb.
6105381b23dSAlexander Lobakin * If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for
6115381b23dSAlexander Lobakin * allocations in case the data is required for writeback
6125381b23dSAlexander Lobakin * @node: numa node to allocate memory on
6135381b23dSAlexander Lobakin *
6145381b23dSAlexander Lobakin * Allocate a new &sk_buff. The returned buffer has no headroom and a
6155381b23dSAlexander Lobakin * tail room of at least size bytes. The object has a reference count
6165381b23dSAlexander Lobakin * of one. The return is the buffer. On a failure the return is %NULL.
6175381b23dSAlexander Lobakin *
6185381b23dSAlexander Lobakin * Buffers may only be allocated from interrupts using a @gfp_mask of
6195381b23dSAlexander Lobakin * %GFP_ATOMIC.
6205381b23dSAlexander Lobakin */
__alloc_skb(unsigned int size,gfp_t gfp_mask,int flags,int node)6215381b23dSAlexander Lobakin struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
6225381b23dSAlexander Lobakin int flags, int node)
6235381b23dSAlexander Lobakin {
6245381b23dSAlexander Lobakin struct kmem_cache *cache;
6255381b23dSAlexander Lobakin struct sk_buff *skb;
6265381b23dSAlexander Lobakin bool pfmemalloc;
627a5df6333SLi RongQing u8 *data;
6285381b23dSAlexander Lobakin
6295381b23dSAlexander Lobakin cache = (flags & SKB_ALLOC_FCLONE)
630025a785fSJakub Kicinski ? skbuff_fclone_cache : skbuff_cache;
6315381b23dSAlexander Lobakin
6325381b23dSAlexander Lobakin if (sk_memalloc_socks() && (flags & SKB_ALLOC_RX))
6335381b23dSAlexander Lobakin gfp_mask |= __GFP_MEMALLOC;
6345381b23dSAlexander Lobakin
6355381b23dSAlexander Lobakin /* Get the HEAD */
636d13612b5SAlexander Lobakin if ((flags & (SKB_ALLOC_FCLONE | SKB_ALLOC_NAPI)) == SKB_ALLOC_NAPI &&
637d13612b5SAlexander Lobakin likely(node == NUMA_NO_NODE || node == numa_mem_id()))
638d13612b5SAlexander Lobakin skb = napi_skb_cache_get();
639d13612b5SAlexander Lobakin else
640d13612b5SAlexander Lobakin skb = kmem_cache_alloc_node(cache, gfp_mask & ~GFP_DMA, node);
641df1ae022SAlexander Lobakin if (unlikely(!skb))
642df1ae022SAlexander Lobakin return NULL;
6435381b23dSAlexander Lobakin prefetchw(skb);
6445381b23dSAlexander Lobakin
6455381b23dSAlexander Lobakin /* We do our best to align skb_shared_info on a separate cache
6465381b23dSAlexander Lobakin * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives
6475381b23dSAlexander Lobakin * aligned memory blocks, unless SLUB/SLAB debug is enabled.
6485381b23dSAlexander Lobakin * Both skb->head and skb_shared_info are cache line aligned.
6495381b23dSAlexander Lobakin */
6505c0e820cSEric Dumazet data = kmalloc_reserve(&size, gfp_mask, node, &pfmemalloc);
651df1ae022SAlexander Lobakin if (unlikely(!data))
6525381b23dSAlexander Lobakin goto nodata;
65312d6c1d3SKees Cook /* kmalloc_size_roundup() might give us more room than requested.
6545381b23dSAlexander Lobakin * Put skb_shared_info exactly at the end of allocated zone,
6555381b23dSAlexander Lobakin * to allow max possible filling before reallocation.
6565381b23dSAlexander Lobakin */
65765998d2bSEric Dumazet prefetchw(data + SKB_WITH_OVERHEAD(size));
6585381b23dSAlexander Lobakin
6595381b23dSAlexander Lobakin /*
6605381b23dSAlexander Lobakin * Only clear those fields we need to clear, not those that we will
6615381b23dSAlexander Lobakin * actually initialise below. Hence, don't put any more fields after
6625381b23dSAlexander Lobakin * the tail pointer in struct sk_buff!
6635381b23dSAlexander Lobakin */
6645381b23dSAlexander Lobakin memset(skb, 0, offsetof(struct sk_buff, tail));
66565998d2bSEric Dumazet __build_skb_around(skb, data, size);
6665381b23dSAlexander Lobakin skb->pfmemalloc = pfmemalloc;
6675381b23dSAlexander Lobakin
6685381b23dSAlexander Lobakin if (flags & SKB_ALLOC_FCLONE) {
6695381b23dSAlexander Lobakin struct sk_buff_fclones *fclones;
6705381b23dSAlexander Lobakin
6715381b23dSAlexander Lobakin fclones = container_of(skb, struct sk_buff_fclones, skb1);
6725381b23dSAlexander Lobakin
6735381b23dSAlexander Lobakin skb->fclone = SKB_FCLONE_ORIG;
6745381b23dSAlexander Lobakin refcount_set(&fclones->fclone_ref, 1);
6755381b23dSAlexander Lobakin }
6765381b23dSAlexander Lobakin
6775381b23dSAlexander Lobakin return skb;
678df1ae022SAlexander Lobakin
6795381b23dSAlexander Lobakin nodata:
6805381b23dSAlexander Lobakin kmem_cache_free(cache, skb);
681df1ae022SAlexander Lobakin return NULL;
6825381b23dSAlexander Lobakin }
6835381b23dSAlexander Lobakin EXPORT_SYMBOL(__alloc_skb);
6845381b23dSAlexander Lobakin
6857ba7aeabSSebastian Andrzej Siewior /**
686fd11a83dSAlexander Duyck * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
687fd11a83dSAlexander Duyck * @dev: network device to receive on
688d7499160SMasanari Iida * @len: length to allocate
689fd11a83dSAlexander Duyck * @gfp_mask: get_free_pages mask, passed to alloc_skb
690fd11a83dSAlexander Duyck *
691fd11a83dSAlexander Duyck * Allocate a new &sk_buff and assign it a usage count of one. The
692fd11a83dSAlexander Duyck * buffer has NET_SKB_PAD headroom built in. Users should allocate
693fd11a83dSAlexander Duyck * the headroom they think they need without accounting for the
694fd11a83dSAlexander Duyck * built in space. The built in space is used for optimisations.
695fd11a83dSAlexander Duyck *
696fd11a83dSAlexander Duyck * %NULL is returned if there is no free memory.
697fd11a83dSAlexander Duyck */
__netdev_alloc_skb(struct net_device * dev,unsigned int len,gfp_t gfp_mask)6989451980aSAlexander Duyck struct sk_buff *__netdev_alloc_skb(struct net_device *dev, unsigned int len,
6999451980aSAlexander Duyck gfp_t gfp_mask)
700fd11a83dSAlexander Duyck {
701b63ae8caSAlexander Duyck struct page_frag_cache *nc;
702fd11a83dSAlexander Duyck struct sk_buff *skb;
7039451980aSAlexander Duyck bool pfmemalloc;
7049451980aSAlexander Duyck void *data;
705fd11a83dSAlexander Duyck
7069451980aSAlexander Duyck len += NET_SKB_PAD;
707fd11a83dSAlexander Duyck
70866c55602SAlexander Lobakin /* If requested length is either too small or too big,
70966c55602SAlexander Lobakin * we use kmalloc() for skb->head allocation.
71066c55602SAlexander Lobakin */
71166c55602SAlexander Lobakin if (len <= SKB_WITH_OVERHEAD(1024) ||
71266c55602SAlexander Lobakin len > SKB_WITH_OVERHEAD(PAGE_SIZE) ||
713d0164adcSMel Gorman (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) {
714a080e7bdSAlexander Duyck skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE);
715a080e7bdSAlexander Duyck if (!skb)
716a080e7bdSAlexander Duyck goto skb_fail;
717a080e7bdSAlexander Duyck goto skb_success;
718a080e7bdSAlexander Duyck }
7199451980aSAlexander Duyck
720115f1a5cSEric Dumazet len = SKB_HEAD_ALIGN(len);
7219451980aSAlexander Duyck
7229451980aSAlexander Duyck if (sk_memalloc_socks())
7239451980aSAlexander Duyck gfp_mask |= __GFP_MEMALLOC;
7249451980aSAlexander Duyck
725afa79d08SChangbin Du if (in_hardirq() || irqs_disabled()) {
7269451980aSAlexander Duyck nc = this_cpu_ptr(&netdev_alloc_cache);
7278c2dd3e4SAlexander Duyck data = page_frag_alloc(nc, len, gfp_mask);
7289451980aSAlexander Duyck pfmemalloc = nc->pfmemalloc;
72992dcabd7SSebastian Andrzej Siewior } else {
73092dcabd7SSebastian Andrzej Siewior local_bh_disable();
73192dcabd7SSebastian Andrzej Siewior nc = this_cpu_ptr(&napi_alloc_cache.page);
73292dcabd7SSebastian Andrzej Siewior data = page_frag_alloc(nc, len, gfp_mask);
73392dcabd7SSebastian Andrzej Siewior pfmemalloc = nc->pfmemalloc;
73492dcabd7SSebastian Andrzej Siewior local_bh_enable();
73592dcabd7SSebastian Andrzej Siewior }
7369451980aSAlexander Duyck
7379451980aSAlexander Duyck if (unlikely(!data))
7389451980aSAlexander Duyck return NULL;
7399451980aSAlexander Duyck
7409451980aSAlexander Duyck skb = __build_skb(data, len);
7419451980aSAlexander Duyck if (unlikely(!skb)) {
742181edb2bSAlexander Duyck skb_free_frag(data);
7439451980aSAlexander Duyck return NULL;
7449451980aSAlexander Duyck }
7459451980aSAlexander Duyck
7469451980aSAlexander Duyck if (pfmemalloc)
7479451980aSAlexander Duyck skb->pfmemalloc = 1;
7489451980aSAlexander Duyck skb->head_frag = 1;
7499451980aSAlexander Duyck
750a080e7bdSAlexander Duyck skb_success:
7518af27456SChristoph Hellwig skb_reserve(skb, NET_SKB_PAD);
7527b2e497aSChristoph Hellwig skb->dev = dev;
753fd11a83dSAlexander Duyck
754a080e7bdSAlexander Duyck skb_fail:
7558af27456SChristoph Hellwig return skb;
7568af27456SChristoph Hellwig }
757b4ac530fSDavid S. Miller EXPORT_SYMBOL(__netdev_alloc_skb);
7581da177e4SLinus Torvalds
759fd11a83dSAlexander Duyck /**
760fd11a83dSAlexander Duyck * __napi_alloc_skb - allocate skbuff for rx in a specific NAPI instance
761fd11a83dSAlexander Duyck * @napi: napi instance this buffer was allocated for
762d7499160SMasanari Iida * @len: length to allocate
763fd11a83dSAlexander Duyck * @gfp_mask: get_free_pages mask, passed to alloc_skb and alloc_pages
764fd11a83dSAlexander Duyck *
765fd11a83dSAlexander Duyck * Allocate a new sk_buff for use in NAPI receive. This buffer will
766fd11a83dSAlexander Duyck * attempt to allocate the head from a special reserved region used
767fd11a83dSAlexander Duyck * only for NAPI Rx allocation. By doing this we can save several
768fd11a83dSAlexander Duyck * CPU cycles by avoiding having to disable and re-enable IRQs.
769fd11a83dSAlexander Duyck *
770fd11a83dSAlexander Duyck * %NULL is returned if there is no free memory.
771fd11a83dSAlexander Duyck */
__napi_alloc_skb(struct napi_struct * napi,unsigned int len,gfp_t gfp_mask)7729451980aSAlexander Duyck struct sk_buff *__napi_alloc_skb(struct napi_struct *napi, unsigned int len,
7739451980aSAlexander Duyck gfp_t gfp_mask)
774fd11a83dSAlexander Duyck {
7753226b158SEric Dumazet struct napi_alloc_cache *nc;
776fd11a83dSAlexander Duyck struct sk_buff *skb;
777dbae2b06SPaolo Abeni bool pfmemalloc;
7789451980aSAlexander Duyck void *data;
779fd11a83dSAlexander Duyck
780ee2640dfSEric Dumazet DEBUG_NET_WARN_ON_ONCE(!in_softirq());
7819451980aSAlexander Duyck len += NET_SKB_PAD + NET_IP_ALIGN;
782fd11a83dSAlexander Duyck
7833226b158SEric Dumazet /* If requested length is either too small or too big,
7843226b158SEric Dumazet * we use kmalloc() for skb->head allocation.
785dbae2b06SPaolo Abeni * When the small frag allocator is available, prefer it over kmalloc
786dbae2b06SPaolo Abeni * for small fragments
7873226b158SEric Dumazet */
788dbae2b06SPaolo Abeni if ((!NAPI_HAS_SMALL_PAGE_FRAG && len <= SKB_WITH_OVERHEAD(1024)) ||
7893226b158SEric Dumazet len > SKB_WITH_OVERHEAD(PAGE_SIZE) ||
790d0164adcSMel Gorman (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) {
791cfb8ec65SAlexander Lobakin skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX | SKB_ALLOC_NAPI,
792cfb8ec65SAlexander Lobakin NUMA_NO_NODE);
793a080e7bdSAlexander Duyck if (!skb)
794a080e7bdSAlexander Duyck goto skb_fail;
795a080e7bdSAlexander Duyck goto skb_success;
796a080e7bdSAlexander Duyck }
7979451980aSAlexander Duyck
7983226b158SEric Dumazet nc = this_cpu_ptr(&napi_alloc_cache);
7999451980aSAlexander Duyck
8009451980aSAlexander Duyck if (sk_memalloc_socks())
8019451980aSAlexander Duyck gfp_mask |= __GFP_MEMALLOC;
8029451980aSAlexander Duyck
803dbae2b06SPaolo Abeni if (NAPI_HAS_SMALL_PAGE_FRAG && len <= SKB_WITH_OVERHEAD(1024)) {
804dbae2b06SPaolo Abeni /* we are artificially inflating the allocation size, but
805dbae2b06SPaolo Abeni * that is not as bad as it may look like, as:
806dbae2b06SPaolo Abeni * - 'len' less than GRO_MAX_HEAD makes little sense
807dbae2b06SPaolo Abeni * - On most systems, larger 'len' values lead to fragment
808dbae2b06SPaolo Abeni * size above 512 bytes
809dbae2b06SPaolo Abeni * - kmalloc would use the kmalloc-1k slab for such values
810dbae2b06SPaolo Abeni * - Builds with smaller GRO_MAX_HEAD will very likely do
811dbae2b06SPaolo Abeni * little networking, as that implies no WiFi and no
812dbae2b06SPaolo Abeni * tunnels support, and 32 bits arches.
813dbae2b06SPaolo Abeni */
814dbae2b06SPaolo Abeni len = SZ_1K;
815dbae2b06SPaolo Abeni
816dbae2b06SPaolo Abeni data = page_frag_alloc_1k(&nc->page_small, gfp_mask);
817dbae2b06SPaolo Abeni pfmemalloc = NAPI_SMALL_PAGE_PFMEMALLOC(nc->page_small);
818dbae2b06SPaolo Abeni } else {
819115f1a5cSEric Dumazet len = SKB_HEAD_ALIGN(len);
820dbae2b06SPaolo Abeni
8218c2dd3e4SAlexander Duyck data = page_frag_alloc(&nc->page, len, gfp_mask);
822dbae2b06SPaolo Abeni pfmemalloc = nc->page.pfmemalloc;
823dbae2b06SPaolo Abeni }
824dbae2b06SPaolo Abeni
8259451980aSAlexander Duyck if (unlikely(!data))
8269451980aSAlexander Duyck return NULL;
8279451980aSAlexander Duyck
828cfb8ec65SAlexander Lobakin skb = __napi_build_skb(data, len);
8299451980aSAlexander Duyck if (unlikely(!skb)) {
830181edb2bSAlexander Duyck skb_free_frag(data);
8319451980aSAlexander Duyck return NULL;
8329451980aSAlexander Duyck }
8339451980aSAlexander Duyck
834dbae2b06SPaolo Abeni if (pfmemalloc)
8359451980aSAlexander Duyck skb->pfmemalloc = 1;
8369451980aSAlexander Duyck skb->head_frag = 1;
8379451980aSAlexander Duyck
838a080e7bdSAlexander Duyck skb_success:
839fd11a83dSAlexander Duyck skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
840fd11a83dSAlexander Duyck skb->dev = napi->dev;
841fd11a83dSAlexander Duyck
842a080e7bdSAlexander Duyck skb_fail:
843fd11a83dSAlexander Duyck return skb;
844fd11a83dSAlexander Duyck }
845fd11a83dSAlexander Duyck EXPORT_SYMBOL(__napi_alloc_skb);
846fd11a83dSAlexander Duyck
skb_add_rx_frag(struct sk_buff * skb,int i,struct page * page,int off,int size,unsigned int truesize)847654bed16SPeter Zijlstra void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off,
84850269e19SEric Dumazet int size, unsigned int truesize)
849654bed16SPeter Zijlstra {
850654bed16SPeter Zijlstra skb_fill_page_desc(skb, i, page, off, size);
851654bed16SPeter Zijlstra skb->len += size;
852654bed16SPeter Zijlstra skb->data_len += size;
85350269e19SEric Dumazet skb->truesize += truesize;
854654bed16SPeter Zijlstra }
855654bed16SPeter Zijlstra EXPORT_SYMBOL(skb_add_rx_frag);
856654bed16SPeter Zijlstra
skb_coalesce_rx_frag(struct sk_buff * skb,int i,int size,unsigned int truesize)857f8e617e1SJason Wang void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size,
858f8e617e1SJason Wang unsigned int truesize)
859f8e617e1SJason Wang {
860f8e617e1SJason Wang skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
861f8e617e1SJason Wang
862f8e617e1SJason Wang skb_frag_size_add(frag, size);
863f8e617e1SJason Wang skb->len += size;
864f8e617e1SJason Wang skb->data_len += size;
865f8e617e1SJason Wang skb->truesize += truesize;
866f8e617e1SJason Wang }
867f8e617e1SJason Wang EXPORT_SYMBOL(skb_coalesce_rx_frag);
868f8e617e1SJason Wang
skb_drop_list(struct sk_buff ** listp)86927b437c8SHerbert Xu static void skb_drop_list(struct sk_buff **listp)
8701da177e4SLinus Torvalds {
871bd8a7036SEric Dumazet kfree_skb_list(*listp);
87227b437c8SHerbert Xu *listp = NULL;
8731da177e4SLinus Torvalds }
8741da177e4SLinus Torvalds
skb_drop_fraglist(struct sk_buff * skb)87527b437c8SHerbert Xu static inline void skb_drop_fraglist(struct sk_buff *skb)
87627b437c8SHerbert Xu {
87727b437c8SHerbert Xu skb_drop_list(&skb_shinfo(skb)->frag_list);
87827b437c8SHerbert Xu }
87927b437c8SHerbert Xu
skb_clone_fraglist(struct sk_buff * skb)8801da177e4SLinus Torvalds static void skb_clone_fraglist(struct sk_buff *skb)
8811da177e4SLinus Torvalds {
8821da177e4SLinus Torvalds struct sk_buff *list;
8831da177e4SLinus Torvalds
884fbb398a8SDavid S. Miller skb_walk_frags(skb, list)
8851da177e4SLinus Torvalds skb_get(list);
8861da177e4SLinus Torvalds }
8871da177e4SLinus Torvalds
88875eaf63eSAlexander Lobakin #if IS_ENABLED(CONFIG_PAGE_POOL)
napi_pp_put_page(struct page * page,bool napi_safe)88975eaf63eSAlexander Lobakin bool napi_pp_put_page(struct page *page, bool napi_safe)
89075eaf63eSAlexander Lobakin {
8915b899c33SAlexander Lobakin bool allow_direct = false;
89275eaf63eSAlexander Lobakin struct page_pool *pp;
89375eaf63eSAlexander Lobakin
89475eaf63eSAlexander Lobakin page = compound_head(page);
89575eaf63eSAlexander Lobakin
89675eaf63eSAlexander Lobakin /* page->pp_magic is OR'ed with PP_SIGNATURE after the allocation
89775eaf63eSAlexander Lobakin * in order to preserve any existing bits, such as bit 0 for the
89875eaf63eSAlexander Lobakin * head page of compound page and bit 1 for pfmemalloc page, so
89975eaf63eSAlexander Lobakin * mask those bits for freeing side when doing below checking,
90075eaf63eSAlexander Lobakin * and page_is_pfmemalloc() is checked in __page_pool_put_page()
90175eaf63eSAlexander Lobakin * to avoid recycling the pfmemalloc page.
90275eaf63eSAlexander Lobakin */
90375eaf63eSAlexander Lobakin if (unlikely((page->pp_magic & ~0x3UL) != PP_SIGNATURE))
90475eaf63eSAlexander Lobakin return false;
90575eaf63eSAlexander Lobakin
90675eaf63eSAlexander Lobakin pp = page->pp;
90775eaf63eSAlexander Lobakin
90875eaf63eSAlexander Lobakin /* Allow direct recycle if we have reasons to believe that we are
90975eaf63eSAlexander Lobakin * in the same context as the consumer would run, so there's
91075eaf63eSAlexander Lobakin * no possible race.
9114a36d018SAlexander Lobakin * __page_pool_put_page() makes sure we're not in hardirq context
9124a36d018SAlexander Lobakin * and interrupts are enabled prior to accessing the cache.
91375eaf63eSAlexander Lobakin */
9144a36d018SAlexander Lobakin if (napi_safe || in_softirq()) {
9155b899c33SAlexander Lobakin const struct napi_struct *napi = READ_ONCE(pp->p.napi);
9165b899c33SAlexander Lobakin
9175b899c33SAlexander Lobakin allow_direct = napi &&
91875eaf63eSAlexander Lobakin READ_ONCE(napi->list_owner) == smp_processor_id();
9195b899c33SAlexander Lobakin }
92075eaf63eSAlexander Lobakin
92175eaf63eSAlexander Lobakin /* Driver set this to memory recycling info. Reset it on recycle.
92275eaf63eSAlexander Lobakin * This will *not* work for NIC using a split-page memory model.
92375eaf63eSAlexander Lobakin * The page will be returned to the pool here regardless of the
92475eaf63eSAlexander Lobakin * 'flipped' fragment being in use or not.
92575eaf63eSAlexander Lobakin */
92675eaf63eSAlexander Lobakin page_pool_put_full_page(pp, page, allow_direct);
92775eaf63eSAlexander Lobakin
92875eaf63eSAlexander Lobakin return true;
92975eaf63eSAlexander Lobakin }
93075eaf63eSAlexander Lobakin EXPORT_SYMBOL(napi_pp_put_page);
93175eaf63eSAlexander Lobakin #endif
93275eaf63eSAlexander Lobakin
skb_pp_recycle(struct sk_buff * skb,void * data,bool napi_safe)933b07a2d97SJakub Kicinski static bool skb_pp_recycle(struct sk_buff *skb, void *data, bool napi_safe)
9344727bab4SYunsheng Lin {
9354727bab4SYunsheng Lin if (!IS_ENABLED(CONFIG_PAGE_POOL) || !skb->pp_recycle)
9364727bab4SYunsheng Lin return false;
93775eaf63eSAlexander Lobakin return napi_pp_put_page(virt_to_page(data), napi_safe);
9384727bab4SYunsheng Lin }
9394727bab4SYunsheng Lin
skb_kfree_head(void * head,unsigned int end_offset)940bf9f1baaSEric Dumazet static void skb_kfree_head(void *head, unsigned int end_offset)
941bf9f1baaSEric Dumazet {
942bf9f1baaSEric Dumazet if (end_offset == SKB_SMALL_HEAD_HEADROOM)
943bf9f1baaSEric Dumazet kmem_cache_free(skb_small_head_cache, head);
944bf9f1baaSEric Dumazet else
945bf9f1baaSEric Dumazet kfree(head);
946bf9f1baaSEric Dumazet }
947bf9f1baaSEric Dumazet
skb_free_head(struct sk_buff * skb,bool napi_safe)948b07a2d97SJakub Kicinski static void skb_free_head(struct sk_buff *skb, bool napi_safe)
949d3836f21SEric Dumazet {
950181edb2bSAlexander Duyck unsigned char *head = skb->head;
951181edb2bSAlexander Duyck
9526a5bcd84SIlias Apalodimas if (skb->head_frag) {
953b07a2d97SJakub Kicinski if (skb_pp_recycle(skb, head, napi_safe))
9546a5bcd84SIlias Apalodimas return;
955181edb2bSAlexander Duyck skb_free_frag(head);
9566a5bcd84SIlias Apalodimas } else {
957bf9f1baaSEric Dumazet skb_kfree_head(head, skb_end_offset(skb));
958d3836f21SEric Dumazet }
9596a5bcd84SIlias Apalodimas }
960d3836f21SEric Dumazet
skb_release_data(struct sk_buff * skb,enum skb_drop_reason reason,bool napi_safe)961b07a2d97SJakub Kicinski static void skb_release_data(struct sk_buff *skb, enum skb_drop_reason reason,
962b07a2d97SJakub Kicinski bool napi_safe)
9631da177e4SLinus Torvalds {
964ff04a771SEric Dumazet struct skb_shared_info *shinfo = skb_shinfo(skb);
9651da177e4SLinus Torvalds int i;
966ff04a771SEric Dumazet
967ff04a771SEric Dumazet if (skb->cloned &&
968ff04a771SEric Dumazet atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
969ff04a771SEric Dumazet &shinfo->dataref))
9702cc3aeb5SIlias Apalodimas goto exit;
971ff04a771SEric Dumazet
972753f1ca4SPavel Begunkov if (skb_zcopy(skb)) {
973753f1ca4SPavel Begunkov bool skip_unref = shinfo->flags & SKBFL_MANAGED_FRAG_REFS;
974753f1ca4SPavel Begunkov
97570c43167SJonathan Lemon skb_zcopy_clear(skb, true);
976753f1ca4SPavel Begunkov if (skip_unref)
977753f1ca4SPavel Begunkov goto free_head;
978753f1ca4SPavel Begunkov }
97970c43167SJonathan Lemon
980ff04a771SEric Dumazet for (i = 0; i < shinfo->nr_frags; i++)
9818c48eea3SJakub Kicinski napi_frag_unref(&shinfo->frags[i], skb->pp_recycle, napi_safe);
9821da177e4SLinus Torvalds
983753f1ca4SPavel Begunkov free_head:
984ff04a771SEric Dumazet if (shinfo->frag_list)
985511a3edaSEric Dumazet kfree_skb_list_reason(shinfo->frag_list, reason);
9861da177e4SLinus Torvalds
987b07a2d97SJakub Kicinski skb_free_head(skb, napi_safe);
9882cc3aeb5SIlias Apalodimas exit:
9892cc3aeb5SIlias Apalodimas /* When we clone an SKB we copy the reycling bit. The pp_recycle
9902cc3aeb5SIlias Apalodimas * bit is only set on the head though, so in order to avoid races
9912cc3aeb5SIlias Apalodimas * while trying to recycle fragments on __skb_frag_unref() we need
9922cc3aeb5SIlias Apalodimas * to make one SKB responsible for triggering the recycle path.
9932cc3aeb5SIlias Apalodimas * So disable the recycling bit if an SKB is cloned and we have
99458e61e41STom Rix * additional references to the fragmented part of the SKB.
9952cc3aeb5SIlias Apalodimas * Eventually the last SKB will have the recycling bit set and it's
9962cc3aeb5SIlias Apalodimas * dataref set to 0, which will trigger the recycling
9972cc3aeb5SIlias Apalodimas */
9982cc3aeb5SIlias Apalodimas skb->pp_recycle = 0;
9991da177e4SLinus Torvalds }
10001da177e4SLinus Torvalds
10011da177e4SLinus Torvalds /*
10021da177e4SLinus Torvalds * Free an skbuff by memory without cleaning the state.
10031da177e4SLinus Torvalds */
kfree_skbmem(struct sk_buff * skb)10042d4baff8SHerbert Xu static void kfree_skbmem(struct sk_buff *skb)
10051da177e4SLinus Torvalds {
1006d0bf4a9eSEric Dumazet struct sk_buff_fclones *fclones;
1007d179cd12SDavid S. Miller
1008d179cd12SDavid S. Miller switch (skb->fclone) {
1009d179cd12SDavid S. Miller case SKB_FCLONE_UNAVAILABLE:
1010025a785fSJakub Kicinski kmem_cache_free(skbuff_cache, skb);
10116ffe75ebSEric Dumazet return;
1012d179cd12SDavid S. Miller
1013d179cd12SDavid S. Miller case SKB_FCLONE_ORIG:
1014d0bf4a9eSEric Dumazet fclones = container_of(skb, struct sk_buff_fclones, skb1);
10156ffe75ebSEric Dumazet
10166ffe75ebSEric Dumazet /* We usually free the clone (TX completion) before original skb
10176ffe75ebSEric Dumazet * This test would have no chance to be true for the clone,
10186ffe75ebSEric Dumazet * while here, branch prediction will be good.
10196ffe75ebSEric Dumazet */
10202638595aSReshetova, Elena if (refcount_read(&fclones->fclone_ref) == 1)
10216ffe75ebSEric Dumazet goto fastpath;
1022d179cd12SDavid S. Miller break;
1023d179cd12SDavid S. Miller
10246ffe75ebSEric Dumazet default: /* SKB_FCLONE_CLONE */
1025d0bf4a9eSEric Dumazet fclones = container_of(skb, struct sk_buff_fclones, skb2);
1026d179cd12SDavid S. Miller break;
10273ff50b79SStephen Hemminger }
10282638595aSReshetova, Elena if (!refcount_dec_and_test(&fclones->fclone_ref))
10296ffe75ebSEric Dumazet return;
10306ffe75ebSEric Dumazet fastpath:
10316ffe75ebSEric Dumazet kmem_cache_free(skbuff_fclone_cache, fclones);
10321da177e4SLinus Torvalds }
10331da177e4SLinus Torvalds
skb_release_head_state(struct sk_buff * skb)10340a463c78SPaolo Abeni void skb_release_head_state(struct sk_buff *skb)
10351da177e4SLinus Torvalds {
1036adf30907SEric Dumazet skb_dst_drop(skb);
10371da177e4SLinus Torvalds if (skb->destructor) {
10387890e2f0SEric Dumazet DEBUG_NET_WARN_ON_ONCE(in_hardirq());
10391da177e4SLinus Torvalds skb->destructor(skb);
10401da177e4SLinus Torvalds }
1041a3bf7ae9SIgor Maravić #if IS_ENABLED(CONFIG_NF_CONNTRACK)
1042cb9c6836SFlorian Westphal nf_conntrack_put(skb_nfct(skb));
10432fc72c7bSKOVACS Krisztian #endif
1044df5042f4SFlorian Westphal skb_ext_put(skb);
104504a4bb55SLennert Buytenhek }
104604a4bb55SLennert Buytenhek
104704a4bb55SLennert Buytenhek /* Free everything but the sk_buff shell. */
skb_release_all(struct sk_buff * skb,enum skb_drop_reason reason,bool napi_safe)1048b07a2d97SJakub Kicinski static void skb_release_all(struct sk_buff *skb, enum skb_drop_reason reason,
1049b07a2d97SJakub Kicinski bool napi_safe)
105004a4bb55SLennert Buytenhek {
105104a4bb55SLennert Buytenhek skb_release_head_state(skb);
1052a28b1b90SFlorian Westphal if (likely(skb->head))
1053b07a2d97SJakub Kicinski skb_release_data(skb, reason, napi_safe);
10542d4baff8SHerbert Xu }
10551da177e4SLinus Torvalds
10562d4baff8SHerbert Xu /**
10572d4baff8SHerbert Xu * __kfree_skb - private function
10582d4baff8SHerbert Xu * @skb: buffer
10592d4baff8SHerbert Xu *
10602d4baff8SHerbert Xu * Free an sk_buff. Release anything attached to the buffer.
10612d4baff8SHerbert Xu * Clean the state. This is an internal helper function. Users should
10622d4baff8SHerbert Xu * always call kfree_skb
10632d4baff8SHerbert Xu */
10642d4baff8SHerbert Xu
__kfree_skb(struct sk_buff * skb)10652d4baff8SHerbert Xu void __kfree_skb(struct sk_buff *skb)
10662d4baff8SHerbert Xu {
1067b07a2d97SJakub Kicinski skb_release_all(skb, SKB_DROP_REASON_NOT_SPECIFIED, false);
10681da177e4SLinus Torvalds kfree_skbmem(skb);
10691da177e4SLinus Torvalds }
1070b4ac530fSDavid S. Miller EXPORT_SYMBOL(__kfree_skb);
10711da177e4SLinus Torvalds
1072a4650da2SJesper Dangaard Brouer static __always_inline
__kfree_skb_reason(struct sk_buff * skb,enum skb_drop_reason reason)1073a4650da2SJesper Dangaard Brouer bool __kfree_skb_reason(struct sk_buff *skb, enum skb_drop_reason reason)
1074a4650da2SJesper Dangaard Brouer {
1075a4650da2SJesper Dangaard Brouer if (unlikely(!skb_unref(skb)))
1076a4650da2SJesper Dangaard Brouer return false;
1077a4650da2SJesper Dangaard Brouer
1078071c0fc6SJohannes Berg DEBUG_NET_WARN_ON_ONCE(reason == SKB_NOT_DROPPED_YET ||
1079071c0fc6SJohannes Berg u32_get_bits(reason,
1080071c0fc6SJohannes Berg SKB_DROP_REASON_SUBSYS_MASK) >=
1081071c0fc6SJohannes Berg SKB_DROP_REASON_SUBSYS_NUM);
1082a4650da2SJesper Dangaard Brouer
1083a4650da2SJesper Dangaard Brouer if (reason == SKB_CONSUMED)
1084dd1b5278SEric Dumazet trace_consume_skb(skb, __builtin_return_address(0));
1085a4650da2SJesper Dangaard Brouer else
1086a4650da2SJesper Dangaard Brouer trace_kfree_skb(skb, __builtin_return_address(0), reason);
1087a4650da2SJesper Dangaard Brouer return true;
1088a4650da2SJesper Dangaard Brouer }
1089a4650da2SJesper Dangaard Brouer
10901da177e4SLinus Torvalds /**
1091c504e5c2SMenglong Dong * kfree_skb_reason - free an sk_buff with special reason
1092231d06aeSJörn Engel * @skb: buffer to free
1093c504e5c2SMenglong Dong * @reason: reason why this skb is dropped
1094231d06aeSJörn Engel *
1095231d06aeSJörn Engel * Drop a reference to the buffer and free it if the usage count has
1096c504e5c2SMenglong Dong * hit zero. Meanwhile, pass the drop reason to 'kfree_skb'
1097c504e5c2SMenglong Dong * tracepoint.
1098231d06aeSJörn Engel */
1099c205cc75SMenglong Dong void __fix_address
kfree_skb_reason(struct sk_buff * skb,enum skb_drop_reason reason)1100c205cc75SMenglong Dong kfree_skb_reason(struct sk_buff *skb, enum skb_drop_reason reason)
1101231d06aeSJörn Engel {
1102a4650da2SJesper Dangaard Brouer if (__kfree_skb_reason(skb, reason))
1103231d06aeSJörn Engel __kfree_skb(skb);
1104231d06aeSJörn Engel }
1105c504e5c2SMenglong Dong EXPORT_SYMBOL(kfree_skb_reason);
1106231d06aeSJörn Engel
1107eedade12SJesper Dangaard Brouer #define KFREE_SKB_BULK_SIZE 16
1108eedade12SJesper Dangaard Brouer
1109eedade12SJesper Dangaard Brouer struct skb_free_array {
1110eedade12SJesper Dangaard Brouer unsigned int skb_count;
1111eedade12SJesper Dangaard Brouer void *skb_array[KFREE_SKB_BULK_SIZE];
1112eedade12SJesper Dangaard Brouer };
1113eedade12SJesper Dangaard Brouer
kfree_skb_add_bulk(struct sk_buff * skb,struct skb_free_array * sa,enum skb_drop_reason reason)1114eedade12SJesper Dangaard Brouer static void kfree_skb_add_bulk(struct sk_buff *skb,
1115eedade12SJesper Dangaard Brouer struct skb_free_array *sa,
1116eedade12SJesper Dangaard Brouer enum skb_drop_reason reason)
1117eedade12SJesper Dangaard Brouer {
1118eedade12SJesper Dangaard Brouer /* if SKB is a clone, don't handle this case */
1119eedade12SJesper Dangaard Brouer if (unlikely(skb->fclone != SKB_FCLONE_UNAVAILABLE)) {
1120eedade12SJesper Dangaard Brouer __kfree_skb(skb);
1121eedade12SJesper Dangaard Brouer return;
1122eedade12SJesper Dangaard Brouer }
1123eedade12SJesper Dangaard Brouer
1124b07a2d97SJakub Kicinski skb_release_all(skb, reason, false);
1125eedade12SJesper Dangaard Brouer sa->skb_array[sa->skb_count++] = skb;
1126eedade12SJesper Dangaard Brouer
1127eedade12SJesper Dangaard Brouer if (unlikely(sa->skb_count == KFREE_SKB_BULK_SIZE)) {
1128025a785fSJakub Kicinski kmem_cache_free_bulk(skbuff_cache, KFREE_SKB_BULK_SIZE,
1129eedade12SJesper Dangaard Brouer sa->skb_array);
1130eedade12SJesper Dangaard Brouer sa->skb_count = 0;
1131eedade12SJesper Dangaard Brouer }
1132eedade12SJesper Dangaard Brouer }
1133eedade12SJesper Dangaard Brouer
1134a4650da2SJesper Dangaard Brouer void __fix_address
kfree_skb_list_reason(struct sk_buff * segs,enum skb_drop_reason reason)1135a4650da2SJesper Dangaard Brouer kfree_skb_list_reason(struct sk_buff *segs, enum skb_drop_reason reason)
1136bd8a7036SEric Dumazet {
1137eedade12SJesper Dangaard Brouer struct skb_free_array sa;
1138eedade12SJesper Dangaard Brouer
1139eedade12SJesper Dangaard Brouer sa.skb_count = 0;
1140eedade12SJesper Dangaard Brouer
1141bd8a7036SEric Dumazet while (segs) {
1142bd8a7036SEric Dumazet struct sk_buff *next = segs->next;
1143bd8a7036SEric Dumazet
11449dde0cd3SJesper Dangaard Brouer if (__kfree_skb_reason(segs, reason)) {
11459dde0cd3SJesper Dangaard Brouer skb_poison_list(segs);
1146eedade12SJesper Dangaard Brouer kfree_skb_add_bulk(segs, &sa, reason);
11479dde0cd3SJesper Dangaard Brouer }
1148eedade12SJesper Dangaard Brouer
1149bd8a7036SEric Dumazet segs = next;
1150bd8a7036SEric Dumazet }
1151eedade12SJesper Dangaard Brouer
1152eedade12SJesper Dangaard Brouer if (sa.skb_count)
1153025a785fSJakub Kicinski kmem_cache_free_bulk(skbuff_cache, sa.skb_count, sa.skb_array);
1154bd8a7036SEric Dumazet }
1155215b0f19SMenglong Dong EXPORT_SYMBOL(kfree_skb_list_reason);
1156bd8a7036SEric Dumazet
11576413139dSWillem de Bruijn /* Dump skb information and contents.
11586413139dSWillem de Bruijn *
11596413139dSWillem de Bruijn * Must only be called from net_ratelimit()-ed paths.
11606413139dSWillem de Bruijn *
1161302af7c6SVladimir Oltean * Dumps whole packets if full_pkt, only headers otherwise.
11626413139dSWillem de Bruijn */
skb_dump(const char * level,const struct sk_buff * skb,bool full_pkt)11636413139dSWillem de Bruijn void skb_dump(const char *level, const struct sk_buff *skb, bool full_pkt)
11646413139dSWillem de Bruijn {
11656413139dSWillem de Bruijn struct skb_shared_info *sh = skb_shinfo(skb);
11666413139dSWillem de Bruijn struct net_device *dev = skb->dev;
11676413139dSWillem de Bruijn struct sock *sk = skb->sk;
11686413139dSWillem de Bruijn struct sk_buff *list_skb;
11696413139dSWillem de Bruijn bool has_mac, has_trans;
11706413139dSWillem de Bruijn int headroom, tailroom;
11716413139dSWillem de Bruijn int i, len, seg_len;
11726413139dSWillem de Bruijn
11736413139dSWillem de Bruijn if (full_pkt)
11746413139dSWillem de Bruijn len = skb->len;
11756413139dSWillem de Bruijn else
11766413139dSWillem de Bruijn len = min_t(int, skb->len, MAX_HEADER + 128);
11776413139dSWillem de Bruijn
11786413139dSWillem de Bruijn headroom = skb_headroom(skb);
11796413139dSWillem de Bruijn tailroom = skb_tailroom(skb);
11806413139dSWillem de Bruijn
11816413139dSWillem de Bruijn has_mac = skb_mac_header_was_set(skb);
11826413139dSWillem de Bruijn has_trans = skb_transport_header_was_set(skb);
11836413139dSWillem de Bruijn
11846413139dSWillem de Bruijn printk("%sskb len=%u headroom=%u headlen=%u tailroom=%u\n"
11856413139dSWillem de Bruijn "mac=(%d,%d) net=(%d,%d) trans=%d\n"
11866413139dSWillem de Bruijn "shinfo(txflags=%u nr_frags=%u gso(size=%hu type=%u segs=%hu))\n"
11876413139dSWillem de Bruijn "csum(0x%x ip_summed=%u complete_sw=%u valid=%u level=%u)\n"
11886413139dSWillem de Bruijn "hash(0x%x sw=%u l4=%u) proto=0x%04x pkttype=%u iif=%d\n",
11896413139dSWillem de Bruijn level, skb->len, headroom, skb_headlen(skb), tailroom,
11906413139dSWillem de Bruijn has_mac ? skb->mac_header : -1,
11916413139dSWillem de Bruijn has_mac ? skb_mac_header_len(skb) : -1,
11926413139dSWillem de Bruijn skb->network_header,
11936413139dSWillem de Bruijn has_trans ? skb_network_header_len(skb) : -1,
11946413139dSWillem de Bruijn has_trans ? skb->transport_header : -1,
11956413139dSWillem de Bruijn sh->tx_flags, sh->nr_frags,
11966413139dSWillem de Bruijn sh->gso_size, sh->gso_type, sh->gso_segs,
11976413139dSWillem de Bruijn skb->csum, skb->ip_summed, skb->csum_complete_sw,
11986413139dSWillem de Bruijn skb->csum_valid, skb->csum_level,
11996413139dSWillem de Bruijn skb->hash, skb->sw_hash, skb->l4_hash,
12006413139dSWillem de Bruijn ntohs(skb->protocol), skb->pkt_type, skb->skb_iif);
12016413139dSWillem de Bruijn
12026413139dSWillem de Bruijn if (dev)
12038a03ef67SGal Pressman printk("%sdev name=%s feat=%pNF\n",
12046413139dSWillem de Bruijn level, dev->name, &dev->features);
12056413139dSWillem de Bruijn if (sk)
1206db8051f3SQian Cai printk("%ssk family=%hu type=%u proto=%u\n",
12076413139dSWillem de Bruijn level, sk->sk_family, sk->sk_type, sk->sk_protocol);
12086413139dSWillem de Bruijn
12096413139dSWillem de Bruijn if (full_pkt && headroom)
12106413139dSWillem de Bruijn print_hex_dump(level, "skb headroom: ", DUMP_PREFIX_OFFSET,
12116413139dSWillem de Bruijn 16, 1, skb->head, headroom, false);
12126413139dSWillem de Bruijn
12136413139dSWillem de Bruijn seg_len = min_t(int, skb_headlen(skb), len);
12146413139dSWillem de Bruijn if (seg_len)
12156413139dSWillem de Bruijn print_hex_dump(level, "skb linear: ", DUMP_PREFIX_OFFSET,
12166413139dSWillem de Bruijn 16, 1, skb->data, seg_len, false);
12176413139dSWillem de Bruijn len -= seg_len;
12186413139dSWillem de Bruijn
12196413139dSWillem de Bruijn if (full_pkt && tailroom)
12206413139dSWillem de Bruijn print_hex_dump(level, "skb tailroom: ", DUMP_PREFIX_OFFSET,
12216413139dSWillem de Bruijn 16, 1, skb_tail_pointer(skb), tailroom, false);
12226413139dSWillem de Bruijn
12236413139dSWillem de Bruijn for (i = 0; len && i < skb_shinfo(skb)->nr_frags; i++) {
12246413139dSWillem de Bruijn skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
12256413139dSWillem de Bruijn u32 p_off, p_len, copied;
12266413139dSWillem de Bruijn struct page *p;
12276413139dSWillem de Bruijn u8 *vaddr;
12286413139dSWillem de Bruijn
1229b54c9d5bSJonathan Lemon skb_frag_foreach_page(frag, skb_frag_off(frag),
12306413139dSWillem de Bruijn skb_frag_size(frag), p, p_off, p_len,
12316413139dSWillem de Bruijn copied) {
12326413139dSWillem de Bruijn seg_len = min_t(int, p_len, len);
12336413139dSWillem de Bruijn vaddr = kmap_atomic(p);
12346413139dSWillem de Bruijn print_hex_dump(level, "skb frag: ",
12356413139dSWillem de Bruijn DUMP_PREFIX_OFFSET,
12366413139dSWillem de Bruijn 16, 1, vaddr + p_off, seg_len, false);
12376413139dSWillem de Bruijn kunmap_atomic(vaddr);
12386413139dSWillem de Bruijn len -= seg_len;
12396413139dSWillem de Bruijn if (!len)
12406413139dSWillem de Bruijn break;
12416413139dSWillem de Bruijn }
12426413139dSWillem de Bruijn }
12436413139dSWillem de Bruijn
12446413139dSWillem de Bruijn if (full_pkt && skb_has_frag_list(skb)) {
12456413139dSWillem de Bruijn printk("skb fraglist:\n");
12466413139dSWillem de Bruijn skb_walk_frags(skb, list_skb)
12476413139dSWillem de Bruijn skb_dump(level, list_skb, true);
12486413139dSWillem de Bruijn }
12496413139dSWillem de Bruijn }
12506413139dSWillem de Bruijn EXPORT_SYMBOL(skb_dump);
12516413139dSWillem de Bruijn
1252d1a203eaSStephen Hemminger /**
125325121173SMichael S. Tsirkin * skb_tx_error - report an sk_buff xmit error
125425121173SMichael S. Tsirkin * @skb: buffer that triggered an error
125525121173SMichael S. Tsirkin *
125625121173SMichael S. Tsirkin * Report xmit error if a device callback is tracking this skb.
125725121173SMichael S. Tsirkin * skb must be freed afterwards.
125825121173SMichael S. Tsirkin */
skb_tx_error(struct sk_buff * skb)125925121173SMichael S. Tsirkin void skb_tx_error(struct sk_buff *skb)
126025121173SMichael S. Tsirkin {
1261753f1ca4SPavel Begunkov if (skb) {
1262753f1ca4SPavel Begunkov skb_zcopy_downgrade_managed(skb);
12631f8b977aSWillem de Bruijn skb_zcopy_clear(skb, true);
126425121173SMichael S. Tsirkin }
1265753f1ca4SPavel Begunkov }
126625121173SMichael S. Tsirkin EXPORT_SYMBOL(skb_tx_error);
126725121173SMichael S. Tsirkin
1268be769db2SHerbert Xu #ifdef CONFIG_TRACEPOINTS
126925121173SMichael S. Tsirkin /**
1270ead2ceb0SNeil Horman * consume_skb - free an skbuff
1271ead2ceb0SNeil Horman * @skb: buffer to free
1272ead2ceb0SNeil Horman *
1273ead2ceb0SNeil Horman * Drop a ref to the buffer and free it if the usage count has hit zero
1274ead2ceb0SNeil Horman * Functions identically to kfree_skb, but kfree_skb assumes that the frame
1275ead2ceb0SNeil Horman * is being dropped after a failure and notes that
1276ead2ceb0SNeil Horman */
consume_skb(struct sk_buff * skb)1277ead2ceb0SNeil Horman void consume_skb(struct sk_buff *skb)
1278ead2ceb0SNeil Horman {
12793889a803SPaolo Abeni if (!skb_unref(skb))
1280ead2ceb0SNeil Horman return;
12813889a803SPaolo Abeni
1282dd1b5278SEric Dumazet trace_consume_skb(skb, __builtin_return_address(0));
1283ead2ceb0SNeil Horman __kfree_skb(skb);
1284ead2ceb0SNeil Horman }
1285ead2ceb0SNeil Horman EXPORT_SYMBOL(consume_skb);
1286be769db2SHerbert Xu #endif
1287ead2ceb0SNeil Horman
12880a463c78SPaolo Abeni /**
1289c1639be9SMauro Carvalho Chehab * __consume_stateless_skb - free an skbuff, assuming it is stateless
12900a463c78SPaolo Abeni * @skb: buffer to free
12910a463c78SPaolo Abeni *
1292ca2c1418SPaolo Abeni * Alike consume_skb(), but this variant assumes that this is the last
1293ca2c1418SPaolo Abeni * skb reference and all the head states have been already dropped
12940a463c78SPaolo Abeni */
__consume_stateless_skb(struct sk_buff * skb)1295ca2c1418SPaolo Abeni void __consume_stateless_skb(struct sk_buff *skb)
12960a463c78SPaolo Abeni {
1297dd1b5278SEric Dumazet trace_consume_skb(skb, __builtin_return_address(0));
1298b07a2d97SJakub Kicinski skb_release_data(skb, SKB_CONSUMED, false);
12990a463c78SPaolo Abeni kfree_skbmem(skb);
13000a463c78SPaolo Abeni }
13010a463c78SPaolo Abeni
napi_skb_cache_put(struct sk_buff * skb)1302f450d539SAlexander Lobakin static void napi_skb_cache_put(struct sk_buff *skb)
1303795bb1c0SJesper Dangaard Brouer {
1304795bb1c0SJesper Dangaard Brouer struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
1305f450d539SAlexander Lobakin u32 i;
1306795bb1c0SJesper Dangaard Brouer
1307025a785fSJakub Kicinski kasan_poison_object_data(skbuff_cache, skb);
1308795bb1c0SJesper Dangaard Brouer nc->skb_cache[nc->skb_count++] = skb;
1309795bb1c0SJesper Dangaard Brouer
1310795bb1c0SJesper Dangaard Brouer if (unlikely(nc->skb_count == NAPI_SKB_CACHE_SIZE)) {
1311f450d539SAlexander Lobakin for (i = NAPI_SKB_CACHE_HALF; i < NAPI_SKB_CACHE_SIZE; i++)
1312025a785fSJakub Kicinski kasan_unpoison_object_data(skbuff_cache,
1313f450d539SAlexander Lobakin nc->skb_cache[i]);
1314f450d539SAlexander Lobakin
1315025a785fSJakub Kicinski kmem_cache_free_bulk(skbuff_cache, NAPI_SKB_CACHE_HALF,
1316f450d539SAlexander Lobakin nc->skb_cache + NAPI_SKB_CACHE_HALF);
1317f450d539SAlexander Lobakin nc->skb_count = NAPI_SKB_CACHE_HALF;
1318795bb1c0SJesper Dangaard Brouer }
1319795bb1c0SJesper Dangaard Brouer }
1320f450d539SAlexander Lobakin
__napi_kfree_skb(struct sk_buff * skb,enum skb_drop_reason reason)13218fa66e4aSJakub Kicinski void __napi_kfree_skb(struct sk_buff *skb, enum skb_drop_reason reason)
132215fad714SJesper Dangaard Brouer {
13238fa66e4aSJakub Kicinski skb_release_all(skb, reason, true);
13249243adfcSAlexander Lobakin napi_skb_cache_put(skb);
13259243adfcSAlexander Lobakin }
13269243adfcSAlexander Lobakin
napi_skb_free_stolen_head(struct sk_buff * skb)13279243adfcSAlexander Lobakin void napi_skb_free_stolen_head(struct sk_buff *skb)
13289243adfcSAlexander Lobakin {
13299efb4b5bSPaolo Abeni if (unlikely(skb->slow_gro)) {
13308550ff8dSPaul Blakey nf_reset_ct(skb);
13319243adfcSAlexander Lobakin skb_dst_drop(skb);
13329243adfcSAlexander Lobakin skb_ext_put(skb);
13335e10da53SPaolo Abeni skb_orphan(skb);
13349efb4b5bSPaolo Abeni skb->slow_gro = 0;
13359efb4b5bSPaolo Abeni }
1336f450d539SAlexander Lobakin napi_skb_cache_put(skb);
133715fad714SJesper Dangaard Brouer }
1338795bb1c0SJesper Dangaard Brouer
napi_consume_skb(struct sk_buff * skb,int budget)1339795bb1c0SJesper Dangaard Brouer void napi_consume_skb(struct sk_buff *skb, int budget)
1340795bb1c0SJesper Dangaard Brouer {
1341885eb0a5SJesper Dangaard Brouer /* Zero budget indicate non-NAPI context called us, like netpoll */
1342795bb1c0SJesper Dangaard Brouer if (unlikely(!budget)) {
1343885eb0a5SJesper Dangaard Brouer dev_consume_skb_any(skb);
1344795bb1c0SJesper Dangaard Brouer return;
1345795bb1c0SJesper Dangaard Brouer }
1346795bb1c0SJesper Dangaard Brouer
1347ee2640dfSEric Dumazet DEBUG_NET_WARN_ON_ONCE(!in_softirq());
13486454eca8SYunsheng Lin
13497608894eSPaolo Abeni if (!skb_unref(skb))
1350795bb1c0SJesper Dangaard Brouer return;
13517608894eSPaolo Abeni
1352795bb1c0SJesper Dangaard Brouer /* if reaching here SKB is ready to free */
1353dd1b5278SEric Dumazet trace_consume_skb(skb, __builtin_return_address(0));
1354795bb1c0SJesper Dangaard Brouer
1355795bb1c0SJesper Dangaard Brouer /* if SKB is a clone, don't handle this case */
1356abbdb5a7SEric Dumazet if (skb->fclone != SKB_FCLONE_UNAVAILABLE) {
1357795bb1c0SJesper Dangaard Brouer __kfree_skb(skb);
1358795bb1c0SJesper Dangaard Brouer return;
1359795bb1c0SJesper Dangaard Brouer }
1360795bb1c0SJesper Dangaard Brouer
1361b07a2d97SJakub Kicinski skb_release_all(skb, SKB_CONSUMED, !!budget);
1362f450d539SAlexander Lobakin napi_skb_cache_put(skb);
1363795bb1c0SJesper Dangaard Brouer }
1364795bb1c0SJesper Dangaard Brouer EXPORT_SYMBOL(napi_consume_skb);
1365795bb1c0SJesper Dangaard Brouer
136603f61041SKees Cook /* Make sure a field is contained by headers group */
1367b1937227SEric Dumazet #define CHECK_SKB_FIELD(field) \
136803f61041SKees Cook BUILD_BUG_ON(offsetof(struct sk_buff, field) != \
136903f61041SKees Cook offsetof(struct sk_buff, headers.field)); \
1370b1937227SEric Dumazet
__copy_skb_header(struct sk_buff * new,const struct sk_buff * old)1371dec18810SHerbert Xu static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
1372dec18810SHerbert Xu {
1373dec18810SHerbert Xu new->tstamp = old->tstamp;
1374b1937227SEric Dumazet /* We do not copy old->sk */
1375dec18810SHerbert Xu new->dev = old->dev;
1376b1937227SEric Dumazet memcpy(new->cb, old->cb, sizeof(old->cb));
13777fee226aSEric Dumazet skb_dst_copy(new, old);
1378df5042f4SFlorian Westphal __skb_ext_copy(new, old);
1379b1937227SEric Dumazet __nf_copy(new, old, false);
13806aa895b0SPatrick McHardy
138103f61041SKees Cook /* Note : this field could be in the headers group.
1382b1937227SEric Dumazet * It is not yet because we do not want to have a 16 bit hole
1383b1937227SEric Dumazet */
1384b1937227SEric Dumazet new->queue_mapping = old->queue_mapping;
138506021292SEliezer Tamir
138603f61041SKees Cook memcpy(&new->headers, &old->headers, sizeof(new->headers));
1387b1937227SEric Dumazet CHECK_SKB_FIELD(protocol);
1388b1937227SEric Dumazet CHECK_SKB_FIELD(csum);
1389b1937227SEric Dumazet CHECK_SKB_FIELD(hash);
1390b1937227SEric Dumazet CHECK_SKB_FIELD(priority);
1391b1937227SEric Dumazet CHECK_SKB_FIELD(skb_iif);
1392b1937227SEric Dumazet CHECK_SKB_FIELD(vlan_proto);
1393b1937227SEric Dumazet CHECK_SKB_FIELD(vlan_tci);
1394b1937227SEric Dumazet CHECK_SKB_FIELD(transport_header);
1395b1937227SEric Dumazet CHECK_SKB_FIELD(network_header);
1396b1937227SEric Dumazet CHECK_SKB_FIELD(mac_header);
1397b1937227SEric Dumazet CHECK_SKB_FIELD(inner_protocol);
1398b1937227SEric Dumazet CHECK_SKB_FIELD(inner_transport_header);
1399b1937227SEric Dumazet CHECK_SKB_FIELD(inner_network_header);
1400b1937227SEric Dumazet CHECK_SKB_FIELD(inner_mac_header);
1401b1937227SEric Dumazet CHECK_SKB_FIELD(mark);
1402b1937227SEric Dumazet #ifdef CONFIG_NETWORK_SECMARK
1403b1937227SEric Dumazet CHECK_SKB_FIELD(secmark);
1404b1937227SEric Dumazet #endif
1405e0d1095aSCong Wang #ifdef CONFIG_NET_RX_BUSY_POLL
1406b1937227SEric Dumazet CHECK_SKB_FIELD(napi_id);
140706021292SEliezer Tamir #endif
140868822bdfSEric Dumazet CHECK_SKB_FIELD(alloc_cpu);
14092bd82484SEric Dumazet #ifdef CONFIG_XPS
14102bd82484SEric Dumazet CHECK_SKB_FIELD(sender_cpu);
14112bd82484SEric Dumazet #endif
1412b1937227SEric Dumazet #ifdef CONFIG_NET_SCHED
1413b1937227SEric Dumazet CHECK_SKB_FIELD(tc_index);
1414b1937227SEric Dumazet #endif
1415b1937227SEric Dumazet
1416dec18810SHerbert Xu }
1417dec18810SHerbert Xu
141882c49a35SHerbert Xu /*
141982c49a35SHerbert Xu * You should not add any new code to this function. Add it to
142082c49a35SHerbert Xu * __copy_skb_header above instead.
142182c49a35SHerbert Xu */
__skb_clone(struct sk_buff * n,struct sk_buff * skb)1422e0053ec0SHerbert Xu static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
14231da177e4SLinus Torvalds {
14241da177e4SLinus Torvalds #define C(x) n->x = skb->x
14251da177e4SLinus Torvalds
14261da177e4SLinus Torvalds n->next = n->prev = NULL;
14271da177e4SLinus Torvalds n->sk = NULL;
1428dec18810SHerbert Xu __copy_skb_header(n, skb);
1429dec18810SHerbert Xu
14301da177e4SLinus Torvalds C(len);
14311da177e4SLinus Torvalds C(data_len);
14323e6b3b2eSAlexey Dobriyan C(mac_len);
1433334a8132SPatrick McHardy n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
143402f1c89dSPaul Moore n->cloned = 1;
14351da177e4SLinus Torvalds n->nohdr = 0;
1436b13dda9fSEric Dumazet n->peeked = 0;
1437e78bfb07SStefano Brivio C(pfmemalloc);
14386a5bcd84SIlias Apalodimas C(pp_recycle);
14391da177e4SLinus Torvalds n->destructor = NULL;
14401da177e4SLinus Torvalds C(tail);
14411da177e4SLinus Torvalds C(end);
144202f1c89dSPaul Moore C(head);
1443d3836f21SEric Dumazet C(head_frag);
144402f1c89dSPaul Moore C(data);
144502f1c89dSPaul Moore C(truesize);
144663354797SReshetova, Elena refcount_set(&n->users, 1);
14471da177e4SLinus Torvalds
14481da177e4SLinus Torvalds atomic_inc(&(skb_shinfo(skb)->dataref));
14491da177e4SLinus Torvalds skb->cloned = 1;
14501da177e4SLinus Torvalds
14511da177e4SLinus Torvalds return n;
1452e0053ec0SHerbert Xu #undef C
1453e0053ec0SHerbert Xu }
1454e0053ec0SHerbert Xu
1455e0053ec0SHerbert Xu /**
1456da29e4b4SJakub Kicinski * alloc_skb_for_msg() - allocate sk_buff to wrap frag list forming a msg
1457da29e4b4SJakub Kicinski * @first: first sk_buff of the msg
1458da29e4b4SJakub Kicinski */
alloc_skb_for_msg(struct sk_buff * first)1459da29e4b4SJakub Kicinski struct sk_buff *alloc_skb_for_msg(struct sk_buff *first)
1460da29e4b4SJakub Kicinski {
1461da29e4b4SJakub Kicinski struct sk_buff *n;
1462da29e4b4SJakub Kicinski
1463da29e4b4SJakub Kicinski n = alloc_skb(0, GFP_ATOMIC);
1464da29e4b4SJakub Kicinski if (!n)
1465da29e4b4SJakub Kicinski return NULL;
1466da29e4b4SJakub Kicinski
1467da29e4b4SJakub Kicinski n->len = first->len;
1468da29e4b4SJakub Kicinski n->data_len = first->len;
1469da29e4b4SJakub Kicinski n->truesize = first->truesize;
1470da29e4b4SJakub Kicinski
1471da29e4b4SJakub Kicinski skb_shinfo(n)->frag_list = first;
1472da29e4b4SJakub Kicinski
1473da29e4b4SJakub Kicinski __copy_skb_header(n, first);
1474da29e4b4SJakub Kicinski n->destructor = NULL;
1475da29e4b4SJakub Kicinski
1476da29e4b4SJakub Kicinski return n;
1477da29e4b4SJakub Kicinski }
1478da29e4b4SJakub Kicinski EXPORT_SYMBOL_GPL(alloc_skb_for_msg);
1479da29e4b4SJakub Kicinski
1480da29e4b4SJakub Kicinski /**
1481e0053ec0SHerbert Xu * skb_morph - morph one skb into another
1482e0053ec0SHerbert Xu * @dst: the skb to receive the contents
1483e0053ec0SHerbert Xu * @src: the skb to supply the contents
1484e0053ec0SHerbert Xu *
1485e0053ec0SHerbert Xu * This is identical to skb_clone except that the target skb is
1486e0053ec0SHerbert Xu * supplied by the user.
1487e0053ec0SHerbert Xu *
1488e0053ec0SHerbert Xu * The target skb is returned upon exit.
1489e0053ec0SHerbert Xu */
skb_morph(struct sk_buff * dst,struct sk_buff * src)1490e0053ec0SHerbert Xu struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
1491e0053ec0SHerbert Xu {
1492b07a2d97SJakub Kicinski skb_release_all(dst, SKB_CONSUMED, false);
1493e0053ec0SHerbert Xu return __skb_clone(dst, src);
1494e0053ec0SHerbert Xu }
1495e0053ec0SHerbert Xu EXPORT_SYMBOL_GPL(skb_morph);
1496e0053ec0SHerbert Xu
mm_account_pinned_pages(struct mmpin * mmp,size_t size)14976f89dbceSSowmini Varadhan int mm_account_pinned_pages(struct mmpin *mmp, size_t size)
1498a91dbff5SWillem de Bruijn {
149914ade6baSWillem de Bruijn unsigned long max_pg, num_pg, new_pg, old_pg, rlim;
1500a91dbff5SWillem de Bruijn struct user_struct *user;
1501a91dbff5SWillem de Bruijn
1502a91dbff5SWillem de Bruijn if (capable(CAP_IPC_LOCK) || !size)
1503a91dbff5SWillem de Bruijn return 0;
1504a91dbff5SWillem de Bruijn
150514ade6baSWillem de Bruijn rlim = rlimit(RLIMIT_MEMLOCK);
150614ade6baSWillem de Bruijn if (rlim == RLIM_INFINITY)
150714ade6baSWillem de Bruijn return 0;
150814ade6baSWillem de Bruijn
1509a91dbff5SWillem de Bruijn num_pg = (size >> PAGE_SHIFT) + 2; /* worst case */
151014ade6baSWillem de Bruijn max_pg = rlim >> PAGE_SHIFT;
1511a91dbff5SWillem de Bruijn user = mmp->user ? : current_user();
1512a91dbff5SWillem de Bruijn
1513a91dbff5SWillem de Bruijn old_pg = atomic_long_read(&user->locked_vm);
151457fc05e8SEric Dumazet do {
1515a91dbff5SWillem de Bruijn new_pg = old_pg + num_pg;
1516a91dbff5SWillem de Bruijn if (new_pg > max_pg)
1517a91dbff5SWillem de Bruijn return -ENOBUFS;
151857fc05e8SEric Dumazet } while (!atomic_long_try_cmpxchg(&user->locked_vm, &old_pg, new_pg));
1519a91dbff5SWillem de Bruijn
1520a91dbff5SWillem de Bruijn if (!mmp->user) {
1521a91dbff5SWillem de Bruijn mmp->user = get_uid(user);
1522a91dbff5SWillem de Bruijn mmp->num_pg = num_pg;
1523a91dbff5SWillem de Bruijn } else {
1524a91dbff5SWillem de Bruijn mmp->num_pg += num_pg;
1525a91dbff5SWillem de Bruijn }
1526a91dbff5SWillem de Bruijn
1527a91dbff5SWillem de Bruijn return 0;
1528a91dbff5SWillem de Bruijn }
15296f89dbceSSowmini Varadhan EXPORT_SYMBOL_GPL(mm_account_pinned_pages);
1530a91dbff5SWillem de Bruijn
mm_unaccount_pinned_pages(struct mmpin * mmp)15316f89dbceSSowmini Varadhan void mm_unaccount_pinned_pages(struct mmpin *mmp)
1532a91dbff5SWillem de Bruijn {
1533a91dbff5SWillem de Bruijn if (mmp->user) {
1534a91dbff5SWillem de Bruijn atomic_long_sub(mmp->num_pg, &mmp->user->locked_vm);
1535a91dbff5SWillem de Bruijn free_uid(mmp->user);
1536a91dbff5SWillem de Bruijn }
1537a91dbff5SWillem de Bruijn }
15386f89dbceSSowmini Varadhan EXPORT_SYMBOL_GPL(mm_unaccount_pinned_pages);
1539a91dbff5SWillem de Bruijn
msg_zerocopy_alloc(struct sock * sk,size_t size)1540c67b627eSDavid Ahern static struct ubuf_info *msg_zerocopy_alloc(struct sock *sk, size_t size)
154152267790SWillem de Bruijn {
1542e7d2b510SPavel Begunkov struct ubuf_info_msgzc *uarg;
154352267790SWillem de Bruijn struct sk_buff *skb;
154452267790SWillem de Bruijn
154552267790SWillem de Bruijn WARN_ON_ONCE(!in_task());
154652267790SWillem de Bruijn
154752267790SWillem de Bruijn skb = sock_omalloc(sk, 0, GFP_KERNEL);
154852267790SWillem de Bruijn if (!skb)
154952267790SWillem de Bruijn return NULL;
155052267790SWillem de Bruijn
155152267790SWillem de Bruijn BUILD_BUG_ON(sizeof(*uarg) > sizeof(skb->cb));
155252267790SWillem de Bruijn uarg = (void *)skb->cb;
1553a91dbff5SWillem de Bruijn uarg->mmp.user = NULL;
1554a91dbff5SWillem de Bruijn
1555a91dbff5SWillem de Bruijn if (mm_account_pinned_pages(&uarg->mmp, size)) {
1556a91dbff5SWillem de Bruijn kfree_skb(skb);
1557a91dbff5SWillem de Bruijn return NULL;
1558a91dbff5SWillem de Bruijn }
155952267790SWillem de Bruijn
1560e7d2b510SPavel Begunkov uarg->ubuf.callback = msg_zerocopy_callback;
15614ab6c99dSWillem de Bruijn uarg->id = ((u32)atomic_inc_return(&sk->sk_zckey)) - 1;
15624ab6c99dSWillem de Bruijn uarg->len = 1;
15634ab6c99dSWillem de Bruijn uarg->bytelen = size;
156452267790SWillem de Bruijn uarg->zerocopy = 1;
1565e7d2b510SPavel Begunkov uarg->ubuf.flags = SKBFL_ZEROCOPY_FRAG | SKBFL_DONT_ORPHAN;
1566e7d2b510SPavel Begunkov refcount_set(&uarg->ubuf.refcnt, 1);
156752267790SWillem de Bruijn sock_hold(sk);
156852267790SWillem de Bruijn
1569e7d2b510SPavel Begunkov return &uarg->ubuf;
157052267790SWillem de Bruijn }
157152267790SWillem de Bruijn
skb_from_uarg(struct ubuf_info_msgzc * uarg)1572e7d2b510SPavel Begunkov static inline struct sk_buff *skb_from_uarg(struct ubuf_info_msgzc *uarg)
157352267790SWillem de Bruijn {
157452267790SWillem de Bruijn return container_of((void *)uarg, struct sk_buff, cb);
157552267790SWillem de Bruijn }
157652267790SWillem de Bruijn
msg_zerocopy_realloc(struct sock * sk,size_t size,struct ubuf_info * uarg)15778c793822SJonathan Lemon struct ubuf_info *msg_zerocopy_realloc(struct sock *sk, size_t size,
15784ab6c99dSWillem de Bruijn struct ubuf_info *uarg)
15794ab6c99dSWillem de Bruijn {
15804ab6c99dSWillem de Bruijn if (uarg) {
1581e7d2b510SPavel Begunkov struct ubuf_info_msgzc *uarg_zc;
15824ab6c99dSWillem de Bruijn const u32 byte_limit = 1 << 19; /* limit to a few TSO */
15834ab6c99dSWillem de Bruijn u32 bytelen, next;
15844ab6c99dSWillem de Bruijn
15851b4b2b09SPavel Begunkov /* there might be non MSG_ZEROCOPY users */
15861b4b2b09SPavel Begunkov if (uarg->callback != msg_zerocopy_callback)
15871b4b2b09SPavel Begunkov return NULL;
15881b4b2b09SPavel Begunkov
15894ab6c99dSWillem de Bruijn /* realloc only when socket is locked (TCP, UDP cork),
15904ab6c99dSWillem de Bruijn * so uarg->len and sk_zckey access is serialized
15914ab6c99dSWillem de Bruijn */
15924ab6c99dSWillem de Bruijn if (!sock_owned_by_user(sk)) {
15934ab6c99dSWillem de Bruijn WARN_ON_ONCE(1);
15944ab6c99dSWillem de Bruijn return NULL;
15954ab6c99dSWillem de Bruijn }
15964ab6c99dSWillem de Bruijn
1597e7d2b510SPavel Begunkov uarg_zc = uarg_to_msgzc(uarg);
1598e7d2b510SPavel Begunkov bytelen = uarg_zc->bytelen + size;
1599e7d2b510SPavel Begunkov if (uarg_zc->len == USHRT_MAX - 1 || bytelen > byte_limit) {
16004ab6c99dSWillem de Bruijn /* TCP can create new skb to attach new uarg */
16014ab6c99dSWillem de Bruijn if (sk->sk_type == SOCK_STREAM)
16024ab6c99dSWillem de Bruijn goto new_alloc;
16034ab6c99dSWillem de Bruijn return NULL;
16044ab6c99dSWillem de Bruijn }
16054ab6c99dSWillem de Bruijn
16064ab6c99dSWillem de Bruijn next = (u32)atomic_read(&sk->sk_zckey);
1607e7d2b510SPavel Begunkov if ((u32)(uarg_zc->id + uarg_zc->len) == next) {
1608e7d2b510SPavel Begunkov if (mm_account_pinned_pages(&uarg_zc->mmp, size))
1609a91dbff5SWillem de Bruijn return NULL;
1610e7d2b510SPavel Begunkov uarg_zc->len++;
1611e7d2b510SPavel Begunkov uarg_zc->bytelen = bytelen;
16124ab6c99dSWillem de Bruijn atomic_set(&sk->sk_zckey, ++next);
1613100f6d8eSWillem de Bruijn
1614100f6d8eSWillem de Bruijn /* no extra ref when appending to datagram (MSG_MORE) */
1615100f6d8eSWillem de Bruijn if (sk->sk_type == SOCK_STREAM)
16168e044917SJonathan Lemon net_zcopy_get(uarg);
1617100f6d8eSWillem de Bruijn
16184ab6c99dSWillem de Bruijn return uarg;
16194ab6c99dSWillem de Bruijn }
16204ab6c99dSWillem de Bruijn }
16214ab6c99dSWillem de Bruijn
16224ab6c99dSWillem de Bruijn new_alloc:
16238c793822SJonathan Lemon return msg_zerocopy_alloc(sk, size);
16244ab6c99dSWillem de Bruijn }
16258c793822SJonathan Lemon EXPORT_SYMBOL_GPL(msg_zerocopy_realloc);
16264ab6c99dSWillem de Bruijn
skb_zerocopy_notify_extend(struct sk_buff * skb,u32 lo,u16 len)16274ab6c99dSWillem de Bruijn static bool skb_zerocopy_notify_extend(struct sk_buff *skb, u32 lo, u16 len)
16284ab6c99dSWillem de Bruijn {
16294ab6c99dSWillem de Bruijn struct sock_exterr_skb *serr = SKB_EXT_ERR(skb);
16304ab6c99dSWillem de Bruijn u32 old_lo, old_hi;
16314ab6c99dSWillem de Bruijn u64 sum_len;
16324ab6c99dSWillem de Bruijn
16334ab6c99dSWillem de Bruijn old_lo = serr->ee.ee_info;
16344ab6c99dSWillem de Bruijn old_hi = serr->ee.ee_data;
16354ab6c99dSWillem de Bruijn sum_len = old_hi - old_lo + 1ULL + len;
16364ab6c99dSWillem de Bruijn
16374ab6c99dSWillem de Bruijn if (sum_len >= (1ULL << 32))
16384ab6c99dSWillem de Bruijn return false;
16394ab6c99dSWillem de Bruijn
16404ab6c99dSWillem de Bruijn if (lo != old_hi + 1)
16414ab6c99dSWillem de Bruijn return false;
16424ab6c99dSWillem de Bruijn
16434ab6c99dSWillem de Bruijn serr->ee.ee_data += len;
16444ab6c99dSWillem de Bruijn return true;
16454ab6c99dSWillem de Bruijn }
16464ab6c99dSWillem de Bruijn
__msg_zerocopy_callback(struct ubuf_info_msgzc * uarg)1647e7d2b510SPavel Begunkov static void __msg_zerocopy_callback(struct ubuf_info_msgzc *uarg)
164852267790SWillem de Bruijn {
16494ab6c99dSWillem de Bruijn struct sk_buff *tail, *skb = skb_from_uarg(uarg);
165052267790SWillem de Bruijn struct sock_exterr_skb *serr;
165152267790SWillem de Bruijn struct sock *sk = skb->sk;
16524ab6c99dSWillem de Bruijn struct sk_buff_head *q;
16534ab6c99dSWillem de Bruijn unsigned long flags;
16543bdd5ee0SWillem de Bruijn bool is_zerocopy;
16554ab6c99dSWillem de Bruijn u32 lo, hi;
16564ab6c99dSWillem de Bruijn u16 len;
165752267790SWillem de Bruijn
1658ccaffff1SWillem de Bruijn mm_unaccount_pinned_pages(&uarg->mmp);
1659ccaffff1SWillem de Bruijn
16604ab6c99dSWillem de Bruijn /* if !len, there was only 1 call, and it was aborted
16614ab6c99dSWillem de Bruijn * so do not queue a completion notification
16624ab6c99dSWillem de Bruijn */
16634ab6c99dSWillem de Bruijn if (!uarg->len || sock_flag(sk, SOCK_DEAD))
166452267790SWillem de Bruijn goto release;
166552267790SWillem de Bruijn
16664ab6c99dSWillem de Bruijn len = uarg->len;
16674ab6c99dSWillem de Bruijn lo = uarg->id;
16684ab6c99dSWillem de Bruijn hi = uarg->id + len - 1;
16693bdd5ee0SWillem de Bruijn is_zerocopy = uarg->zerocopy;
16704ab6c99dSWillem de Bruijn
167152267790SWillem de Bruijn serr = SKB_EXT_ERR(skb);
167252267790SWillem de Bruijn memset(serr, 0, sizeof(*serr));
167352267790SWillem de Bruijn serr->ee.ee_errno = 0;
167452267790SWillem de Bruijn serr->ee.ee_origin = SO_EE_ORIGIN_ZEROCOPY;
16754ab6c99dSWillem de Bruijn serr->ee.ee_data = hi;
16764ab6c99dSWillem de Bruijn serr->ee.ee_info = lo;
16773bdd5ee0SWillem de Bruijn if (!is_zerocopy)
167852267790SWillem de Bruijn serr->ee.ee_code |= SO_EE_CODE_ZEROCOPY_COPIED;
167952267790SWillem de Bruijn
16804ab6c99dSWillem de Bruijn q = &sk->sk_error_queue;
16814ab6c99dSWillem de Bruijn spin_lock_irqsave(&q->lock, flags);
16824ab6c99dSWillem de Bruijn tail = skb_peek_tail(q);
16834ab6c99dSWillem de Bruijn if (!tail || SKB_EXT_ERR(tail)->ee.ee_origin != SO_EE_ORIGIN_ZEROCOPY ||
16844ab6c99dSWillem de Bruijn !skb_zerocopy_notify_extend(tail, lo, len)) {
16854ab6c99dSWillem de Bruijn __skb_queue_tail(q, skb);
168652267790SWillem de Bruijn skb = NULL;
16874ab6c99dSWillem de Bruijn }
16884ab6c99dSWillem de Bruijn spin_unlock_irqrestore(&q->lock, flags);
168952267790SWillem de Bruijn
1690e3ae2365SAlexander Aring sk_error_report(sk);
169152267790SWillem de Bruijn
169252267790SWillem de Bruijn release:
169352267790SWillem de Bruijn consume_skb(skb);
169452267790SWillem de Bruijn sock_put(sk);
169552267790SWillem de Bruijn }
169675518851SJonathan Lemon
msg_zerocopy_callback(struct sk_buff * skb,struct ubuf_info * uarg,bool success)16978c793822SJonathan Lemon void msg_zerocopy_callback(struct sk_buff *skb, struct ubuf_info *uarg,
169836177832SJonathan Lemon bool success)
169975518851SJonathan Lemon {
1700e7d2b510SPavel Begunkov struct ubuf_info_msgzc *uarg_zc = uarg_to_msgzc(uarg);
1701e7d2b510SPavel Begunkov
1702e7d2b510SPavel Begunkov uarg_zc->zerocopy = uarg_zc->zerocopy & success;
170375518851SJonathan Lemon
170475518851SJonathan Lemon if (refcount_dec_and_test(&uarg->refcnt))
1705e7d2b510SPavel Begunkov __msg_zerocopy_callback(uarg_zc);
170675518851SJonathan Lemon }
17078c793822SJonathan Lemon EXPORT_SYMBOL_GPL(msg_zerocopy_callback);
170852267790SWillem de Bruijn
msg_zerocopy_put_abort(struct ubuf_info * uarg,bool have_uref)17098c793822SJonathan Lemon void msg_zerocopy_put_abort(struct ubuf_info *uarg, bool have_uref)
171052267790SWillem de Bruijn {
1711e7d2b510SPavel Begunkov struct sock *sk = skb_from_uarg(uarg_to_msgzc(uarg))->sk;
171252267790SWillem de Bruijn
171352267790SWillem de Bruijn atomic_dec(&sk->sk_zckey);
1714e7d2b510SPavel Begunkov uarg_to_msgzc(uarg)->len--;
171552267790SWillem de Bruijn
171652900d22SWillem de Bruijn if (have_uref)
17178c793822SJonathan Lemon msg_zerocopy_callback(NULL, uarg, true);
171852267790SWillem de Bruijn }
17198c793822SJonathan Lemon EXPORT_SYMBOL_GPL(msg_zerocopy_put_abort);
172052267790SWillem de Bruijn
skb_zerocopy_iter_stream(struct sock * sk,struct sk_buff * skb,struct msghdr * msg,int len,struct ubuf_info * uarg)172152267790SWillem de Bruijn int skb_zerocopy_iter_stream(struct sock *sk, struct sk_buff *skb,
172252267790SWillem de Bruijn struct msghdr *msg, int len,
172352267790SWillem de Bruijn struct ubuf_info *uarg)
172452267790SWillem de Bruijn {
17254ab6c99dSWillem de Bruijn struct ubuf_info *orig_uarg = skb_zcopy(skb);
172652267790SWillem de Bruijn int err, orig_len = skb->len;
172752267790SWillem de Bruijn
17284ab6c99dSWillem de Bruijn /* An skb can only point to one uarg. This edge case happens when
17294ab6c99dSWillem de Bruijn * TCP appends to an skb, but zerocopy_realloc triggered a new alloc.
17304ab6c99dSWillem de Bruijn */
17314ab6c99dSWillem de Bruijn if (orig_uarg && uarg != orig_uarg)
17324ab6c99dSWillem de Bruijn return -EEXIST;
17334ab6c99dSWillem de Bruijn
1734ebe73a28SDavid Ahern err = __zerocopy_sg_from_iter(msg, sk, skb, &msg->msg_iter, len);
173552267790SWillem de Bruijn if (err == -EFAULT || (err == -EMSGSIZE && skb->len == orig_len)) {
173654d43117SWillem de Bruijn struct sock *save_sk = skb->sk;
173754d43117SWillem de Bruijn
173852267790SWillem de Bruijn /* Streams do not free skb on error. Reset to prev state. */
1739829b7bddSPavel Begunkov iov_iter_revert(&msg->msg_iter, skb->len - orig_len);
174054d43117SWillem de Bruijn skb->sk = sk;
174152267790SWillem de Bruijn ___pskb_trim(skb, orig_len);
174254d43117SWillem de Bruijn skb->sk = save_sk;
174352267790SWillem de Bruijn return err;
174452267790SWillem de Bruijn }
174552267790SWillem de Bruijn
174652900d22SWillem de Bruijn skb_zcopy_set(skb, uarg, NULL);
174752267790SWillem de Bruijn return skb->len - orig_len;
174852267790SWillem de Bruijn }
174952267790SWillem de Bruijn EXPORT_SYMBOL_GPL(skb_zerocopy_iter_stream);
175052267790SWillem de Bruijn
__skb_zcopy_downgrade_managed(struct sk_buff * skb)1751753f1ca4SPavel Begunkov void __skb_zcopy_downgrade_managed(struct sk_buff *skb)
1752753f1ca4SPavel Begunkov {
1753753f1ca4SPavel Begunkov int i;
1754753f1ca4SPavel Begunkov
1755753f1ca4SPavel Begunkov skb_shinfo(skb)->flags &= ~SKBFL_MANAGED_FRAG_REFS;
1756753f1ca4SPavel Begunkov for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1757753f1ca4SPavel Begunkov skb_frag_ref(skb, i);
1758753f1ca4SPavel Begunkov }
1759753f1ca4SPavel Begunkov EXPORT_SYMBOL_GPL(__skb_zcopy_downgrade_managed);
1760753f1ca4SPavel Begunkov
skb_zerocopy_clone(struct sk_buff * nskb,struct sk_buff * orig,gfp_t gfp_mask)17611f8b977aSWillem de Bruijn static int skb_zerocopy_clone(struct sk_buff *nskb, struct sk_buff *orig,
176252267790SWillem de Bruijn gfp_t gfp_mask)
176352267790SWillem de Bruijn {
176452267790SWillem de Bruijn if (skb_zcopy(orig)) {
176552267790SWillem de Bruijn if (skb_zcopy(nskb)) {
176652267790SWillem de Bruijn /* !gfp_mask callers are verified to !skb_zcopy(nskb) */
176752267790SWillem de Bruijn if (!gfp_mask) {
176852267790SWillem de Bruijn WARN_ON_ONCE(1);
176952267790SWillem de Bruijn return -ENOMEM;
177052267790SWillem de Bruijn }
177152267790SWillem de Bruijn if (skb_uarg(nskb) == skb_uarg(orig))
177252267790SWillem de Bruijn return 0;
177352267790SWillem de Bruijn if (skb_copy_ubufs(nskb, GFP_ATOMIC))
177452267790SWillem de Bruijn return -EIO;
177552267790SWillem de Bruijn }
177652900d22SWillem de Bruijn skb_zcopy_set(nskb, skb_uarg(orig), NULL);
177752267790SWillem de Bruijn }
177852267790SWillem de Bruijn return 0;
177952267790SWillem de Bruijn }
178052267790SWillem de Bruijn
17812c53040fSBen Hutchings /**
17822c53040fSBen Hutchings * skb_copy_ubufs - copy userspace skb frags buffers to kernel
178348c83012SMichael S. Tsirkin * @skb: the skb to modify
178448c83012SMichael S. Tsirkin * @gfp_mask: allocation priority
178548c83012SMichael S. Tsirkin *
178606b4feb3SJonathan Lemon * This must be called on skb with SKBFL_ZEROCOPY_ENABLE.
178748c83012SMichael S. Tsirkin * It will copy all frags into kernel and drop the reference
178848c83012SMichael S. Tsirkin * to userspace pages.
178948c83012SMichael S. Tsirkin *
179048c83012SMichael S. Tsirkin * If this function is called from an interrupt gfp_mask() must be
179148c83012SMichael S. Tsirkin * %GFP_ATOMIC.
179248c83012SMichael S. Tsirkin *
179348c83012SMichael S. Tsirkin * Returns 0 on success or a negative error code on failure
179448c83012SMichael S. Tsirkin * to allocate kernel memory to copy to.
179548c83012SMichael S. Tsirkin */
skb_copy_ubufs(struct sk_buff * skb,gfp_t gfp_mask)179648c83012SMichael S. Tsirkin int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask)
1797a6686f2fSShirley Ma {
1798a6686f2fSShirley Ma int num_frags = skb_shinfo(skb)->nr_frags;
1799a6686f2fSShirley Ma struct page *page, *head = NULL;
18007e692df3SEric Dumazet int i, order, psize, new_frags;
18013ece7826SWillem de Bruijn u32 d_off;
1802a6686f2fSShirley Ma
18033ece7826SWillem de Bruijn if (skb_shared(skb) || skb_unclone(skb, gfp_mask))
18043ece7826SWillem de Bruijn return -EINVAL;
18053ece7826SWillem de Bruijn
1806f72c4ac6SWillem de Bruijn if (!num_frags)
1807f72c4ac6SWillem de Bruijn goto release;
1808f72c4ac6SWillem de Bruijn
18097e692df3SEric Dumazet /* We might have to allocate high order pages, so compute what minimum
18107e692df3SEric Dumazet * page order is needed.
18117e692df3SEric Dumazet */
18127e692df3SEric Dumazet order = 0;
18137e692df3SEric Dumazet while ((PAGE_SIZE << order) * MAX_SKB_FRAGS < __skb_pagelen(skb))
18147e692df3SEric Dumazet order++;
18157e692df3SEric Dumazet psize = (PAGE_SIZE << order);
18167e692df3SEric Dumazet
18177e692df3SEric Dumazet new_frags = (__skb_pagelen(skb) + psize - 1) >> (PAGE_SHIFT + order);
18183ece7826SWillem de Bruijn for (i = 0; i < new_frags; i++) {
18197e692df3SEric Dumazet page = alloc_pages(gfp_mask | __GFP_COMP, order);
1820a6686f2fSShirley Ma if (!page) {
1821a6686f2fSShirley Ma while (head) {
182240dadff2SSunghan Suh struct page *next = (struct page *)page_private(head);
1823a6686f2fSShirley Ma put_page(head);
1824a6686f2fSShirley Ma head = next;
1825a6686f2fSShirley Ma }
1826a6686f2fSShirley Ma return -ENOMEM;
1827a6686f2fSShirley Ma }
18283ece7826SWillem de Bruijn set_page_private(page, (unsigned long)head);
18293ece7826SWillem de Bruijn head = page;
18303ece7826SWillem de Bruijn }
18313ece7826SWillem de Bruijn
18323ece7826SWillem de Bruijn page = head;
18333ece7826SWillem de Bruijn d_off = 0;
18343ece7826SWillem de Bruijn for (i = 0; i < num_frags; i++) {
18353ece7826SWillem de Bruijn skb_frag_t *f = &skb_shinfo(skb)->frags[i];
18363ece7826SWillem de Bruijn u32 p_off, p_len, copied;
18373ece7826SWillem de Bruijn struct page *p;
18383ece7826SWillem de Bruijn u8 *vaddr;
1839c613c209SWillem de Bruijn
1840b54c9d5bSJonathan Lemon skb_frag_foreach_page(f, skb_frag_off(f), skb_frag_size(f),
1841c613c209SWillem de Bruijn p, p_off, p_len, copied) {
18423ece7826SWillem de Bruijn u32 copy, done = 0;
1843c613c209SWillem de Bruijn vaddr = kmap_atomic(p);
18443ece7826SWillem de Bruijn
18453ece7826SWillem de Bruijn while (done < p_len) {
18467e692df3SEric Dumazet if (d_off == psize) {
18473ece7826SWillem de Bruijn d_off = 0;
18483ece7826SWillem de Bruijn page = (struct page *)page_private(page);
18493ece7826SWillem de Bruijn }
18507e692df3SEric Dumazet copy = min_t(u32, psize - d_off, p_len - done);
18513ece7826SWillem de Bruijn memcpy(page_address(page) + d_off,
18523ece7826SWillem de Bruijn vaddr + p_off + done, copy);
18533ece7826SWillem de Bruijn done += copy;
18543ece7826SWillem de Bruijn d_off += copy;
18553ece7826SWillem de Bruijn }
185651c56b00SEric Dumazet kunmap_atomic(vaddr);
1857c613c209SWillem de Bruijn }
1858a6686f2fSShirley Ma }
1859a6686f2fSShirley Ma
1860a6686f2fSShirley Ma /* skb frags release userspace buffers */
186102756ed4SKrishna Kumar for (i = 0; i < num_frags; i++)
1862a8605c60SIan Campbell skb_frag_unref(skb, i);
1863a6686f2fSShirley Ma
1864a6686f2fSShirley Ma /* skb frags point to kernel buffers */
18653ece7826SWillem de Bruijn for (i = 0; i < new_frags - 1; i++) {
18667e692df3SEric Dumazet __skb_fill_page_desc(skb, i, head, 0, psize);
186740dadff2SSunghan Suh head = (struct page *)page_private(head);
1868a6686f2fSShirley Ma }
18693ece7826SWillem de Bruijn __skb_fill_page_desc(skb, new_frags - 1, head, 0, d_off);
18703ece7826SWillem de Bruijn skb_shinfo(skb)->nr_frags = new_frags;
187148c83012SMichael S. Tsirkin
1872b90ddd56SWillem de Bruijn release:
18731f8b977aSWillem de Bruijn skb_zcopy_clear(skb, false);
1874a6686f2fSShirley Ma return 0;
1875a6686f2fSShirley Ma }
1876dcc0fb78SMichael S. Tsirkin EXPORT_SYMBOL_GPL(skb_copy_ubufs);
1877a6686f2fSShirley Ma
1878e0053ec0SHerbert Xu /**
1879e0053ec0SHerbert Xu * skb_clone - duplicate an sk_buff
1880e0053ec0SHerbert Xu * @skb: buffer to clone
1881e0053ec0SHerbert Xu * @gfp_mask: allocation priority
1882e0053ec0SHerbert Xu *
1883e0053ec0SHerbert Xu * Duplicate an &sk_buff. The new one is not owned by a socket. Both
1884e0053ec0SHerbert Xu * copies share the same packet data but not structure. The new
1885e0053ec0SHerbert Xu * buffer has a reference count of 1. If the allocation fails the
1886e0053ec0SHerbert Xu * function returns %NULL otherwise the new buffer is returned.
1887e0053ec0SHerbert Xu *
1888e0053ec0SHerbert Xu * If this function is called from an interrupt gfp_mask() must be
1889e0053ec0SHerbert Xu * %GFP_ATOMIC.
1890e0053ec0SHerbert Xu */
1891e0053ec0SHerbert Xu
skb_clone(struct sk_buff * skb,gfp_t gfp_mask)1892e0053ec0SHerbert Xu struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
1893e0053ec0SHerbert Xu {
1894d0bf4a9eSEric Dumazet struct sk_buff_fclones *fclones = container_of(skb,
1895d0bf4a9eSEric Dumazet struct sk_buff_fclones,
1896d0bf4a9eSEric Dumazet skb1);
18976ffe75ebSEric Dumazet struct sk_buff *n;
1898e0053ec0SHerbert Xu
189970008aa5SMichael S. Tsirkin if (skb_orphan_frags(skb, gfp_mask))
1900a6686f2fSShirley Ma return NULL;
1901a6686f2fSShirley Ma
1902e0053ec0SHerbert Xu if (skb->fclone == SKB_FCLONE_ORIG &&
19032638595aSReshetova, Elena refcount_read(&fclones->fclone_ref) == 1) {
19046ffe75ebSEric Dumazet n = &fclones->skb2;
19052638595aSReshetova, Elena refcount_set(&fclones->fclone_ref, 2);
1906c2dd4059SEric Dumazet n->fclone = SKB_FCLONE_CLONE;
1907e0053ec0SHerbert Xu } else {
1908c93bdd0eSMel Gorman if (skb_pfmemalloc(skb))
1909c93bdd0eSMel Gorman gfp_mask |= __GFP_MEMALLOC;
1910c93bdd0eSMel Gorman
1911025a785fSJakub Kicinski n = kmem_cache_alloc(skbuff_cache, gfp_mask);
1912e0053ec0SHerbert Xu if (!n)
1913e0053ec0SHerbert Xu return NULL;
1914fe55f6d5SVegard Nossum
1915e0053ec0SHerbert Xu n->fclone = SKB_FCLONE_UNAVAILABLE;
1916e0053ec0SHerbert Xu }
1917e0053ec0SHerbert Xu
1918e0053ec0SHerbert Xu return __skb_clone(n, skb);
19191da177e4SLinus Torvalds }
1920b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_clone);
19211da177e4SLinus Torvalds
skb_headers_offset_update(struct sk_buff * skb,int off)1922b0768a86SToshiaki Makita void skb_headers_offset_update(struct sk_buff *skb, int off)
1923f5b17294SPravin B Shelar {
1924030737bcSEric Dumazet /* Only adjust this if it actually is csum_start rather than csum */
1925030737bcSEric Dumazet if (skb->ip_summed == CHECKSUM_PARTIAL)
1926030737bcSEric Dumazet skb->csum_start += off;
1927f5b17294SPravin B Shelar /* {transport,network,mac}_header and tail are relative to skb->head */
1928f5b17294SPravin B Shelar skb->transport_header += off;
1929f5b17294SPravin B Shelar skb->network_header += off;
1930f5b17294SPravin B Shelar if (skb_mac_header_was_set(skb))
1931f5b17294SPravin B Shelar skb->mac_header += off;
1932f5b17294SPravin B Shelar skb->inner_transport_header += off;
1933f5b17294SPravin B Shelar skb->inner_network_header += off;
1934aefbd2b3SPravin B Shelar skb->inner_mac_header += off;
1935f5b17294SPravin B Shelar }
1936b0768a86SToshiaki Makita EXPORT_SYMBOL(skb_headers_offset_update);
1937f5b17294SPravin B Shelar
skb_copy_header(struct sk_buff * new,const struct sk_buff * old)193808303c18SIlya Lesokhin void skb_copy_header(struct sk_buff *new, const struct sk_buff *old)
19391da177e4SLinus Torvalds {
1940dec18810SHerbert Xu __copy_skb_header(new, old);
1941dec18810SHerbert Xu
19427967168cSHerbert Xu skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
19437967168cSHerbert Xu skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
19447967168cSHerbert Xu skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
19451da177e4SLinus Torvalds }
194608303c18SIlya Lesokhin EXPORT_SYMBOL(skb_copy_header);
19471da177e4SLinus Torvalds
skb_alloc_rx_flag(const struct sk_buff * skb)1948c93bdd0eSMel Gorman static inline int skb_alloc_rx_flag(const struct sk_buff *skb)
1949c93bdd0eSMel Gorman {
1950c93bdd0eSMel Gorman if (skb_pfmemalloc(skb))
1951c93bdd0eSMel Gorman return SKB_ALLOC_RX;
1952c93bdd0eSMel Gorman return 0;
1953c93bdd0eSMel Gorman }
1954c93bdd0eSMel Gorman
19551da177e4SLinus Torvalds /**
19561da177e4SLinus Torvalds * skb_copy - create private copy of an sk_buff
19571da177e4SLinus Torvalds * @skb: buffer to copy
19581da177e4SLinus Torvalds * @gfp_mask: allocation priority
19591da177e4SLinus Torvalds *
19601da177e4SLinus Torvalds * Make a copy of both an &sk_buff and its data. This is used when the
19611da177e4SLinus Torvalds * caller wishes to modify the data and needs a private copy of the
19621da177e4SLinus Torvalds * data to alter. Returns %NULL on failure or the pointer to the buffer
19631da177e4SLinus Torvalds * on success. The returned buffer has a reference count of 1.
19641da177e4SLinus Torvalds *
19651da177e4SLinus Torvalds * As by-product this function converts non-linear &sk_buff to linear
19661da177e4SLinus Torvalds * one, so that &sk_buff becomes completely private and caller is allowed
19671da177e4SLinus Torvalds * to modify all the data of returned buffer. This means that this
19681da177e4SLinus Torvalds * function is not recommended for use in circumstances when only
19691da177e4SLinus Torvalds * header is going to be modified. Use pskb_copy() instead.
19701da177e4SLinus Torvalds */
19711da177e4SLinus Torvalds
skb_copy(const struct sk_buff * skb,gfp_t gfp_mask)1972dd0fc66fSAl Viro struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
19731da177e4SLinus Torvalds {
1974cfe34d86SFelix Fietkau struct sk_buff *n;
1975cfe34d86SFelix Fietkau unsigned int size;
1976cfe34d86SFelix Fietkau int headerlen;
19776602cebbSEric Dumazet
1978cfe34d86SFelix Fietkau if (WARN_ON_ONCE(skb_shinfo(skb)->gso_type & SKB_GSO_FRAGLIST))
1979cfe34d86SFelix Fietkau return NULL;
1980cfe34d86SFelix Fietkau
1981cfe34d86SFelix Fietkau headerlen = skb_headroom(skb);
1982cfe34d86SFelix Fietkau size = skb_end_offset(skb) + skb->data_len;
1983cfe34d86SFelix Fietkau n = __alloc_skb(size, gfp_mask,
1984cfe34d86SFelix Fietkau skb_alloc_rx_flag(skb), NUMA_NO_NODE);
19851da177e4SLinus Torvalds if (!n)
19861da177e4SLinus Torvalds return NULL;
19871da177e4SLinus Torvalds
19881da177e4SLinus Torvalds /* Set the data pointer */
19891da177e4SLinus Torvalds skb_reserve(n, headerlen);
19901da177e4SLinus Torvalds /* Set the tail pointer and length */
19911da177e4SLinus Torvalds skb_put(n, skb->len);
19921da177e4SLinus Torvalds
19939f77fad3STim Hansen BUG_ON(skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len));
19941da177e4SLinus Torvalds
199508303c18SIlya Lesokhin skb_copy_header(n, skb);
19961da177e4SLinus Torvalds return n;
19971da177e4SLinus Torvalds }
1998b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy);
19991da177e4SLinus Torvalds
20001da177e4SLinus Torvalds /**
2001bad93e9dSOctavian Purdila * __pskb_copy_fclone - create copy of an sk_buff with private head.
20021da177e4SLinus Torvalds * @skb: buffer to copy
2003117632e6SEric Dumazet * @headroom: headroom of new skb
20041da177e4SLinus Torvalds * @gfp_mask: allocation priority
2005bad93e9dSOctavian Purdila * @fclone: if true allocate the copy of the skb from the fclone
2006bad93e9dSOctavian Purdila * cache instead of the head cache; it is recommended to set this
2007bad93e9dSOctavian Purdila * to true for the cases where the copy will likely be cloned
20081da177e4SLinus Torvalds *
20091da177e4SLinus Torvalds * Make a copy of both an &sk_buff and part of its data, located
20101da177e4SLinus Torvalds * in header. Fragmented data remain shared. This is used when
20111da177e4SLinus Torvalds * the caller wishes to modify only header of &sk_buff and needs
20121da177e4SLinus Torvalds * private copy of the header to alter. Returns %NULL on failure
20131da177e4SLinus Torvalds * or the pointer to the buffer on success.
20141da177e4SLinus Torvalds * The returned buffer has a reference count of 1.
20151da177e4SLinus Torvalds */
20161da177e4SLinus Torvalds
__pskb_copy_fclone(struct sk_buff * skb,int headroom,gfp_t gfp_mask,bool fclone)2017bad93e9dSOctavian Purdila struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom,
2018bad93e9dSOctavian Purdila gfp_t gfp_mask, bool fclone)
20191da177e4SLinus Torvalds {
2020117632e6SEric Dumazet unsigned int size = skb_headlen(skb) + headroom;
2021bad93e9dSOctavian Purdila int flags = skb_alloc_rx_flag(skb) | (fclone ? SKB_ALLOC_FCLONE : 0);
2022bad93e9dSOctavian Purdila struct sk_buff *n = __alloc_skb(size, gfp_mask, flags, NUMA_NO_NODE);
20236602cebbSEric Dumazet
20241da177e4SLinus Torvalds if (!n)
20251da177e4SLinus Torvalds goto out;
20261da177e4SLinus Torvalds
20271da177e4SLinus Torvalds /* Set the data pointer */
2028117632e6SEric Dumazet skb_reserve(n, headroom);
20291da177e4SLinus Torvalds /* Set the tail pointer and length */
20301da177e4SLinus Torvalds skb_put(n, skb_headlen(skb));
20311da177e4SLinus Torvalds /* Copy the bytes */
2032d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data(skb, n->data, n->len);
20331da177e4SLinus Torvalds
203425f484a6SHerbert Xu n->truesize += skb->data_len;
20351da177e4SLinus Torvalds n->data_len = skb->data_len;
20361da177e4SLinus Torvalds n->len = skb->len;
20371da177e4SLinus Torvalds
20381da177e4SLinus Torvalds if (skb_shinfo(skb)->nr_frags) {
20391da177e4SLinus Torvalds int i;
20401da177e4SLinus Torvalds
20411f8b977aSWillem de Bruijn if (skb_orphan_frags(skb, gfp_mask) ||
20421f8b977aSWillem de Bruijn skb_zerocopy_clone(n, skb, gfp_mask)) {
20431511022cSDan Carpenter kfree_skb(n);
20441511022cSDan Carpenter n = NULL;
2045a6686f2fSShirley Ma goto out;
2046a6686f2fSShirley Ma }
20471da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
20481da177e4SLinus Torvalds skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
2049ea2ab693SIan Campbell skb_frag_ref(skb, i);
20501da177e4SLinus Torvalds }
20511da177e4SLinus Torvalds skb_shinfo(n)->nr_frags = i;
20521da177e4SLinus Torvalds }
20531da177e4SLinus Torvalds
205421dc3301SDavid S. Miller if (skb_has_frag_list(skb)) {
20551da177e4SLinus Torvalds skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
20561da177e4SLinus Torvalds skb_clone_fraglist(n);
20571da177e4SLinus Torvalds }
20581da177e4SLinus Torvalds
205908303c18SIlya Lesokhin skb_copy_header(n, skb);
20601da177e4SLinus Torvalds out:
20611da177e4SLinus Torvalds return n;
20621da177e4SLinus Torvalds }
2063bad93e9dSOctavian Purdila EXPORT_SYMBOL(__pskb_copy_fclone);
20641da177e4SLinus Torvalds
20651da177e4SLinus Torvalds /**
20661da177e4SLinus Torvalds * pskb_expand_head - reallocate header of &sk_buff
20671da177e4SLinus Torvalds * @skb: buffer to reallocate
20681da177e4SLinus Torvalds * @nhead: room to add at head
20691da177e4SLinus Torvalds * @ntail: room to add at tail
20701da177e4SLinus Torvalds * @gfp_mask: allocation priority
20711da177e4SLinus Torvalds *
2072bc32383cSMathias Krause * Expands (or creates identical copy, if @nhead and @ntail are zero)
2073bc32383cSMathias Krause * header of @skb. &sk_buff itself is not changed. &sk_buff MUST have
20741da177e4SLinus Torvalds * reference count of 1. Returns zero in the case of success or error,
20751da177e4SLinus Torvalds * if expansion failed. In the last case, &sk_buff is not changed.
20761da177e4SLinus Torvalds *
20771da177e4SLinus Torvalds * All the pointers pointing into skb header may change and must be
20781da177e4SLinus Torvalds * reloaded after call to this function.
20791da177e4SLinus Torvalds */
20801da177e4SLinus Torvalds
pskb_expand_head(struct sk_buff * skb,int nhead,int ntail,gfp_t gfp_mask)208186a76cafSVictor Fusco int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
2082dd0fc66fSAl Viro gfp_t gfp_mask)
20831da177e4SLinus Torvalds {
208412d6c1d3SKees Cook unsigned int osize = skb_end_offset(skb);
208512d6c1d3SKees Cook unsigned int size = osize + nhead + ntail;
20861da177e4SLinus Torvalds long off;
2087158f323bSEric Dumazet u8 *data;
208812d6c1d3SKees Cook int i;
20891da177e4SLinus Torvalds
20904edd87adSHerbert Xu BUG_ON(nhead < 0);
20914edd87adSHerbert Xu
20929f77fad3STim Hansen BUG_ON(skb_shared(skb));
20931da177e4SLinus Torvalds
2094753f1ca4SPavel Begunkov skb_zcopy_downgrade_managed(skb);
2095753f1ca4SPavel Begunkov
2096c93bdd0eSMel Gorman if (skb_pfmemalloc(skb))
2097c93bdd0eSMel Gorman gfp_mask |= __GFP_MEMALLOC;
209812d6c1d3SKees Cook
20995c0e820cSEric Dumazet data = kmalloc_reserve(&size, gfp_mask, NUMA_NO_NODE, NULL);
21001da177e4SLinus Torvalds if (!data)
21011da177e4SLinus Torvalds goto nodata;
210212d6c1d3SKees Cook size = SKB_WITH_OVERHEAD(size);
21031da177e4SLinus Torvalds
21041da177e4SLinus Torvalds /* Copy only real data... and, alas, header. This should be
21056602cebbSEric Dumazet * optimized for the cases when header is void.
21066602cebbSEric Dumazet */
21076602cebbSEric Dumazet memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head);
21086602cebbSEric Dumazet
21096602cebbSEric Dumazet memcpy((struct skb_shared_info *)(data + size),
21106602cebbSEric Dumazet skb_shinfo(skb),
2111fed66381SEric Dumazet offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags]));
21121da177e4SLinus Torvalds
21133e24591aSAlexander Duyck /*
21143e24591aSAlexander Duyck * if shinfo is shared we must drop the old head gracefully, but if it
21153e24591aSAlexander Duyck * is not we can just drop the old head and let the existing refcount
21163e24591aSAlexander Duyck * be since all we did is relocate the values
21173e24591aSAlexander Duyck */
21183e24591aSAlexander Duyck if (skb_cloned(skb)) {
211970008aa5SMichael S. Tsirkin if (skb_orphan_frags(skb, gfp_mask))
2120a6686f2fSShirley Ma goto nofrags;
21211f8b977aSWillem de Bruijn if (skb_zcopy(skb))
2122c1d1b437SEric Dumazet refcount_inc(&skb_uarg(skb)->refcnt);
21231da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
2124ea2ab693SIan Campbell skb_frag_ref(skb, i);
21251da177e4SLinus Torvalds
212621dc3301SDavid S. Miller if (skb_has_frag_list(skb))
21271da177e4SLinus Torvalds skb_clone_fraglist(skb);
21281da177e4SLinus Torvalds
2129b07a2d97SJakub Kicinski skb_release_data(skb, SKB_CONSUMED, false);
21303e24591aSAlexander Duyck } else {
2131b07a2d97SJakub Kicinski skb_free_head(skb, false);
21321fd63041SEric Dumazet }
21331da177e4SLinus Torvalds off = (data + nhead) - skb->head;
21341da177e4SLinus Torvalds
21351da177e4SLinus Torvalds skb->head = data;
2136d3836f21SEric Dumazet skb->head_frag = 0;
21371da177e4SLinus Torvalds skb->data += off;
2138763087daSEric Dumazet
2139763087daSEric Dumazet skb_set_end_offset(skb, size);
21404305b541SArnaldo Carvalho de Melo #ifdef NET_SKBUFF_DATA_USES_OFFSET
214156eb8882SPatrick McHardy off = nhead;
214256eb8882SPatrick McHardy #endif
214327a884dcSArnaldo Carvalho de Melo skb->tail += off;
2144b41abb42SPeter Pan(潘卫平) skb_headers_offset_update(skb, nhead);
21451da177e4SLinus Torvalds skb->cloned = 0;
2146334a8132SPatrick McHardy skb->hdr_len = 0;
21471da177e4SLinus Torvalds skb->nohdr = 0;
21481da177e4SLinus Torvalds atomic_set(&skb_shinfo(skb)->dataref, 1);
2149158f323bSEric Dumazet
2150de8f3a83SDaniel Borkmann skb_metadata_clear(skb);
2151de8f3a83SDaniel Borkmann
2152158f323bSEric Dumazet /* It is not generally safe to change skb->truesize.
2153158f323bSEric Dumazet * For the moment, we really care of rx path, or
2154158f323bSEric Dumazet * when skb is orphaned (not attached to a socket).
2155158f323bSEric Dumazet */
2156158f323bSEric Dumazet if (!skb->sk || skb->destructor == sock_edemux)
2157158f323bSEric Dumazet skb->truesize += size - osize;
2158158f323bSEric Dumazet
21591da177e4SLinus Torvalds return 0;
21601da177e4SLinus Torvalds
2161a6686f2fSShirley Ma nofrags:
2162bf9f1baaSEric Dumazet skb_kfree_head(data, size);
21631da177e4SLinus Torvalds nodata:
21641da177e4SLinus Torvalds return -ENOMEM;
21651da177e4SLinus Torvalds }
2166b4ac530fSDavid S. Miller EXPORT_SYMBOL(pskb_expand_head);
21671da177e4SLinus Torvalds
21681da177e4SLinus Torvalds /* Make private copy of skb with writable head and some headroom */
21691da177e4SLinus Torvalds
skb_realloc_headroom(struct sk_buff * skb,unsigned int headroom)21701da177e4SLinus Torvalds struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
21711da177e4SLinus Torvalds {
21721da177e4SLinus Torvalds struct sk_buff *skb2;
21731da177e4SLinus Torvalds int delta = headroom - skb_headroom(skb);
21741da177e4SLinus Torvalds
21751da177e4SLinus Torvalds if (delta <= 0)
21761da177e4SLinus Torvalds skb2 = pskb_copy(skb, GFP_ATOMIC);
21771da177e4SLinus Torvalds else {
21781da177e4SLinus Torvalds skb2 = skb_clone(skb, GFP_ATOMIC);
21791da177e4SLinus Torvalds if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
21801da177e4SLinus Torvalds GFP_ATOMIC)) {
21811da177e4SLinus Torvalds kfree_skb(skb2);
21821da177e4SLinus Torvalds skb2 = NULL;
21831da177e4SLinus Torvalds }
21841da177e4SLinus Torvalds }
21851da177e4SLinus Torvalds return skb2;
21861da177e4SLinus Torvalds }
2187b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_realloc_headroom);
21881da177e4SLinus Torvalds
2189880ce5f2SEric Dumazet /* Note: We plan to rework this in linux-6.4 */
__skb_unclone_keeptruesize(struct sk_buff * skb,gfp_t pri)21902b88cba5SEric Dumazet int __skb_unclone_keeptruesize(struct sk_buff *skb, gfp_t pri)
21912b88cba5SEric Dumazet {
21922b88cba5SEric Dumazet unsigned int saved_end_offset, saved_truesize;
21932b88cba5SEric Dumazet struct skb_shared_info *shinfo;
21942b88cba5SEric Dumazet int res;
21952b88cba5SEric Dumazet
21962b88cba5SEric Dumazet saved_end_offset = skb_end_offset(skb);
21972b88cba5SEric Dumazet saved_truesize = skb->truesize;
21982b88cba5SEric Dumazet
21992b88cba5SEric Dumazet res = pskb_expand_head(skb, 0, 0, pri);
22002b88cba5SEric Dumazet if (res)
22012b88cba5SEric Dumazet return res;
22022b88cba5SEric Dumazet
22032b88cba5SEric Dumazet skb->truesize = saved_truesize;
22042b88cba5SEric Dumazet
22052b88cba5SEric Dumazet if (likely(skb_end_offset(skb) == saved_end_offset))
22062b88cba5SEric Dumazet return 0;
22072b88cba5SEric Dumazet
2208880ce5f2SEric Dumazet /* We can not change skb->end if the original or new value
2209880ce5f2SEric Dumazet * is SKB_SMALL_HEAD_HEADROOM, as it might break skb_kfree_head().
2210880ce5f2SEric Dumazet */
2211880ce5f2SEric Dumazet if (saved_end_offset == SKB_SMALL_HEAD_HEADROOM ||
2212880ce5f2SEric Dumazet skb_end_offset(skb) == SKB_SMALL_HEAD_HEADROOM) {
2213880ce5f2SEric Dumazet /* We think this path should not be taken.
2214880ce5f2SEric Dumazet * Add a temporary trace to warn us just in case.
2215880ce5f2SEric Dumazet */
2216880ce5f2SEric Dumazet pr_err_once("__skb_unclone_keeptruesize() skb_end_offset() %u -> %u\n",
2217880ce5f2SEric Dumazet saved_end_offset, skb_end_offset(skb));
2218880ce5f2SEric Dumazet WARN_ON_ONCE(1);
2219880ce5f2SEric Dumazet return 0;
2220880ce5f2SEric Dumazet }
2221880ce5f2SEric Dumazet
22222b88cba5SEric Dumazet shinfo = skb_shinfo(skb);
22232b88cba5SEric Dumazet
22242b88cba5SEric Dumazet /* We are about to change back skb->end,
22252b88cba5SEric Dumazet * we need to move skb_shinfo() to its new location.
22262b88cba5SEric Dumazet */
22272b88cba5SEric Dumazet memmove(skb->head + saved_end_offset,
22282b88cba5SEric Dumazet shinfo,
22292b88cba5SEric Dumazet offsetof(struct skb_shared_info, frags[shinfo->nr_frags]));
22302b88cba5SEric Dumazet
22312b88cba5SEric Dumazet skb_set_end_offset(skb, saved_end_offset);
22322b88cba5SEric Dumazet
22332b88cba5SEric Dumazet return 0;
22342b88cba5SEric Dumazet }
22352b88cba5SEric Dumazet
22361da177e4SLinus Torvalds /**
2237f1260ff1SVasily Averin * skb_expand_head - reallocate header of &sk_buff
2238f1260ff1SVasily Averin * @skb: buffer to reallocate
2239f1260ff1SVasily Averin * @headroom: needed headroom
2240f1260ff1SVasily Averin *
2241f1260ff1SVasily Averin * Unlike skb_realloc_headroom, this one does not allocate a new skb
2242f1260ff1SVasily Averin * if possible; copies skb->sk to new skb as needed
2243f1260ff1SVasily Averin * and frees original skb in case of failures.
2244f1260ff1SVasily Averin *
2245f1260ff1SVasily Averin * It expect increased headroom and generates warning otherwise.
2246f1260ff1SVasily Averin */
2247f1260ff1SVasily Averin
skb_expand_head(struct sk_buff * skb,unsigned int headroom)2248f1260ff1SVasily Averin struct sk_buff *skb_expand_head(struct sk_buff *skb, unsigned int headroom)
2249f1260ff1SVasily Averin {
2250f1260ff1SVasily Averin int delta = headroom - skb_headroom(skb);
22517f678defSVasily Averin int osize = skb_end_offset(skb);
22527f678defSVasily Averin struct sock *sk = skb->sk;
2253f1260ff1SVasily Averin
2254f1260ff1SVasily Averin if (WARN_ONCE(delta <= 0,
2255f1260ff1SVasily Averin "%s is expecting an increase in the headroom", __func__))
2256f1260ff1SVasily Averin return skb;
2257f1260ff1SVasily Averin
22587f678defSVasily Averin delta = SKB_DATA_ALIGN(delta);
22597f678defSVasily Averin /* pskb_expand_head() might crash, if skb is shared. */
22607f678defSVasily Averin if (skb_shared(skb) || !is_skb_wmem(skb)) {
2261f1260ff1SVasily Averin struct sk_buff *nskb = skb_clone(skb, GFP_ATOMIC);
2262f1260ff1SVasily Averin
22637f678defSVasily Averin if (unlikely(!nskb))
22647f678defSVasily Averin goto fail;
22657f678defSVasily Averin
22667f678defSVasily Averin if (sk)
22677f678defSVasily Averin skb_set_owner_w(nskb, sk);
2268f1260ff1SVasily Averin consume_skb(skb);
2269f1260ff1SVasily Averin skb = nskb;
2270f1260ff1SVasily Averin }
22717f678defSVasily Averin if (pskb_expand_head(skb, delta, 0, GFP_ATOMIC))
22727f678defSVasily Averin goto fail;
22737f678defSVasily Averin
22747f678defSVasily Averin if (sk && is_skb_wmem(skb)) {
22757f678defSVasily Averin delta = skb_end_offset(skb) - osize;
22767f678defSVasily Averin refcount_add(delta, &sk->sk_wmem_alloc);
22777f678defSVasily Averin skb->truesize += delta;
2278f1260ff1SVasily Averin }
2279f1260ff1SVasily Averin return skb;
22807f678defSVasily Averin
22817f678defSVasily Averin fail:
22827f678defSVasily Averin kfree_skb(skb);
22837f678defSVasily Averin return NULL;
2284f1260ff1SVasily Averin }
2285f1260ff1SVasily Averin EXPORT_SYMBOL(skb_expand_head);
2286f1260ff1SVasily Averin
2287f1260ff1SVasily Averin /**
22881da177e4SLinus Torvalds * skb_copy_expand - copy and expand sk_buff
22891da177e4SLinus Torvalds * @skb: buffer to copy
22901da177e4SLinus Torvalds * @newheadroom: new free bytes at head
22911da177e4SLinus Torvalds * @newtailroom: new free bytes at tail
22921da177e4SLinus Torvalds * @gfp_mask: allocation priority
22931da177e4SLinus Torvalds *
22941da177e4SLinus Torvalds * Make a copy of both an &sk_buff and its data and while doing so
22951da177e4SLinus Torvalds * allocate additional space.
22961da177e4SLinus Torvalds *
22971da177e4SLinus Torvalds * This is used when the caller wishes to modify the data and needs a
22981da177e4SLinus Torvalds * private copy of the data to alter as well as more space for new fields.
22991da177e4SLinus Torvalds * Returns %NULL on failure or the pointer to the buffer
23001da177e4SLinus Torvalds * on success. The returned buffer has a reference count of 1.
23011da177e4SLinus Torvalds *
23021da177e4SLinus Torvalds * You must pass %GFP_ATOMIC as the allocation priority if this function
23031da177e4SLinus Torvalds * is called from an interrupt.
23041da177e4SLinus Torvalds */
skb_copy_expand(const struct sk_buff * skb,int newheadroom,int newtailroom,gfp_t gfp_mask)23051da177e4SLinus Torvalds struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
230686a76cafSVictor Fusco int newheadroom, int newtailroom,
2307dd0fc66fSAl Viro gfp_t gfp_mask)
23081da177e4SLinus Torvalds {
23091da177e4SLinus Torvalds /*
23101da177e4SLinus Torvalds * Allocate the copy buffer
23111da177e4SLinus Torvalds */
2312cfe34d86SFelix Fietkau int head_copy_len, head_copy_off;
2313cfe34d86SFelix Fietkau struct sk_buff *n;
2314cfe34d86SFelix Fietkau int oldheadroom;
2315cfe34d86SFelix Fietkau
2316cfe34d86SFelix Fietkau if (WARN_ON_ONCE(skb_shinfo(skb)->gso_type & SKB_GSO_FRAGLIST))
2317cfe34d86SFelix Fietkau return NULL;
2318cfe34d86SFelix Fietkau
2319cfe34d86SFelix Fietkau oldheadroom = skb_headroom(skb);
2320cfe34d86SFelix Fietkau n = __alloc_skb(newheadroom + skb->len + newtailroom,
2321c93bdd0eSMel Gorman gfp_mask, skb_alloc_rx_flag(skb),
2322c93bdd0eSMel Gorman NUMA_NO_NODE);
23231da177e4SLinus Torvalds if (!n)
23241da177e4SLinus Torvalds return NULL;
23251da177e4SLinus Torvalds
23261da177e4SLinus Torvalds skb_reserve(n, newheadroom);
23271da177e4SLinus Torvalds
23281da177e4SLinus Torvalds /* Set the tail pointer and length */
23291da177e4SLinus Torvalds skb_put(n, skb->len);
23301da177e4SLinus Torvalds
2331efd1e8d5SPatrick McHardy head_copy_len = oldheadroom;
23321da177e4SLinus Torvalds head_copy_off = 0;
23331da177e4SLinus Torvalds if (newheadroom <= head_copy_len)
23341da177e4SLinus Torvalds head_copy_len = newheadroom;
23351da177e4SLinus Torvalds else
23361da177e4SLinus Torvalds head_copy_off = newheadroom - head_copy_len;
23371da177e4SLinus Torvalds
23381da177e4SLinus Torvalds /* Copy the linear header and data. */
23399f77fad3STim Hansen BUG_ON(skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
23409f77fad3STim Hansen skb->len + head_copy_len));
23411da177e4SLinus Torvalds
234208303c18SIlya Lesokhin skb_copy_header(n, skb);
23431da177e4SLinus Torvalds
2344030737bcSEric Dumazet skb_headers_offset_update(n, newheadroom - oldheadroom);
2345efd1e8d5SPatrick McHardy
23461da177e4SLinus Torvalds return n;
23471da177e4SLinus Torvalds }
2348b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_expand);
23491da177e4SLinus Torvalds
23501da177e4SLinus Torvalds /**
2351cd0a137aSFlorian Fainelli * __skb_pad - zero pad the tail of an skb
23521da177e4SLinus Torvalds * @skb: buffer to pad
23531da177e4SLinus Torvalds * @pad: space to pad
2354cd0a137aSFlorian Fainelli * @free_on_error: free buffer on error
23551da177e4SLinus Torvalds *
23561da177e4SLinus Torvalds * Ensure that a buffer is followed by a padding area that is zero
23571da177e4SLinus Torvalds * filled. Used by network drivers which may DMA or transfer data
23581da177e4SLinus Torvalds * beyond the buffer end onto the wire.
23591da177e4SLinus Torvalds *
2360cd0a137aSFlorian Fainelli * May return error in out of memory cases. The skb is freed on error
2361cd0a137aSFlorian Fainelli * if @free_on_error is true.
23621da177e4SLinus Torvalds */
23631da177e4SLinus Torvalds
__skb_pad(struct sk_buff * skb,int pad,bool free_on_error)2364cd0a137aSFlorian Fainelli int __skb_pad(struct sk_buff *skb, int pad, bool free_on_error)
23651da177e4SLinus Torvalds {
23665b057c6bSHerbert Xu int err;
23675b057c6bSHerbert Xu int ntail;
23681da177e4SLinus Torvalds
23691da177e4SLinus Torvalds /* If the skbuff is non linear tailroom is always zero.. */
23705b057c6bSHerbert Xu if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
23711da177e4SLinus Torvalds memset(skb->data+skb->len, 0, pad);
23725b057c6bSHerbert Xu return 0;
23731da177e4SLinus Torvalds }
23741da177e4SLinus Torvalds
23754305b541SArnaldo Carvalho de Melo ntail = skb->data_len + pad - (skb->end - skb->tail);
23765b057c6bSHerbert Xu if (likely(skb_cloned(skb) || ntail > 0)) {
23775b057c6bSHerbert Xu err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
23785b057c6bSHerbert Xu if (unlikely(err))
23795b057c6bSHerbert Xu goto free_skb;
23805b057c6bSHerbert Xu }
23815b057c6bSHerbert Xu
23825b057c6bSHerbert Xu /* FIXME: The use of this function with non-linear skb's really needs
23835b057c6bSHerbert Xu * to be audited.
23845b057c6bSHerbert Xu */
23855b057c6bSHerbert Xu err = skb_linearize(skb);
23865b057c6bSHerbert Xu if (unlikely(err))
23875b057c6bSHerbert Xu goto free_skb;
23885b057c6bSHerbert Xu
23895b057c6bSHerbert Xu memset(skb->data + skb->len, 0, pad);
23905b057c6bSHerbert Xu return 0;
23915b057c6bSHerbert Xu
23925b057c6bSHerbert Xu free_skb:
2393cd0a137aSFlorian Fainelli if (free_on_error)
23941da177e4SLinus Torvalds kfree_skb(skb);
23955b057c6bSHerbert Xu return err;
23961da177e4SLinus Torvalds }
2397cd0a137aSFlorian Fainelli EXPORT_SYMBOL(__skb_pad);
23981da177e4SLinus Torvalds
23990dde3e16SIlpo Järvinen /**
24000c7ddf36SMathias Krause * pskb_put - add data to the tail of a potentially fragmented buffer
24010c7ddf36SMathias Krause * @skb: start of the buffer to use
24020c7ddf36SMathias Krause * @tail: tail fragment of the buffer to use
24030c7ddf36SMathias Krause * @len: amount of data to add
24040c7ddf36SMathias Krause *
24050c7ddf36SMathias Krause * This function extends the used data area of the potentially
24060c7ddf36SMathias Krause * fragmented buffer. @tail must be the last fragment of @skb -- or
24070c7ddf36SMathias Krause * @skb itself. If this would exceed the total buffer size the kernel
24080c7ddf36SMathias Krause * will panic. A pointer to the first byte of the extra data is
24090c7ddf36SMathias Krause * returned.
24100c7ddf36SMathias Krause */
24110c7ddf36SMathias Krause
pskb_put(struct sk_buff * skb,struct sk_buff * tail,int len)24124df864c1SJohannes Berg void *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len)
24130c7ddf36SMathias Krause {
24140c7ddf36SMathias Krause if (tail != skb) {
24150c7ddf36SMathias Krause skb->data_len += len;
24160c7ddf36SMathias Krause skb->len += len;
24170c7ddf36SMathias Krause }
24180c7ddf36SMathias Krause return skb_put(tail, len);
24190c7ddf36SMathias Krause }
24200c7ddf36SMathias Krause EXPORT_SYMBOL_GPL(pskb_put);
24210c7ddf36SMathias Krause
24220c7ddf36SMathias Krause /**
24230dde3e16SIlpo Järvinen * skb_put - add data to a buffer
24240dde3e16SIlpo Järvinen * @skb: buffer to use
24250dde3e16SIlpo Järvinen * @len: amount of data to add
24260dde3e16SIlpo Järvinen *
24270dde3e16SIlpo Järvinen * This function extends the used data area of the buffer. If this would
24280dde3e16SIlpo Järvinen * exceed the total buffer size the kernel will panic. A pointer to the
24290dde3e16SIlpo Järvinen * first byte of the extra data is returned.
24300dde3e16SIlpo Järvinen */
skb_put(struct sk_buff * skb,unsigned int len)24314df864c1SJohannes Berg void *skb_put(struct sk_buff *skb, unsigned int len)
24320dde3e16SIlpo Järvinen {
24334df864c1SJohannes Berg void *tmp = skb_tail_pointer(skb);
24340dde3e16SIlpo Järvinen SKB_LINEAR_ASSERT(skb);
24350dde3e16SIlpo Järvinen skb->tail += len;
24360dde3e16SIlpo Järvinen skb->len += len;
24370dde3e16SIlpo Järvinen if (unlikely(skb->tail > skb->end))
24380dde3e16SIlpo Järvinen skb_over_panic(skb, len, __builtin_return_address(0));
24390dde3e16SIlpo Järvinen return tmp;
24400dde3e16SIlpo Järvinen }
24410dde3e16SIlpo Järvinen EXPORT_SYMBOL(skb_put);
24420dde3e16SIlpo Järvinen
24436be8ac2fSIlpo Järvinen /**
2444c2aa270aSIlpo Järvinen * skb_push - add data to the start of a buffer
2445c2aa270aSIlpo Järvinen * @skb: buffer to use
2446c2aa270aSIlpo Järvinen * @len: amount of data to add
2447c2aa270aSIlpo Järvinen *
2448c2aa270aSIlpo Järvinen * This function extends the used data area of the buffer at the buffer
2449c2aa270aSIlpo Järvinen * start. If this would exceed the total buffer headroom the kernel will
2450c2aa270aSIlpo Järvinen * panic. A pointer to the first byte of the extra data is returned.
2451c2aa270aSIlpo Järvinen */
skb_push(struct sk_buff * skb,unsigned int len)2452d58ff351SJohannes Berg void *skb_push(struct sk_buff *skb, unsigned int len)
2453c2aa270aSIlpo Järvinen {
2454c2aa270aSIlpo Järvinen skb->data -= len;
2455c2aa270aSIlpo Järvinen skb->len += len;
2456c2aa270aSIlpo Järvinen if (unlikely(skb->data < skb->head))
2457c2aa270aSIlpo Järvinen skb_under_panic(skb, len, __builtin_return_address(0));
2458c2aa270aSIlpo Järvinen return skb->data;
2459c2aa270aSIlpo Järvinen }
2460c2aa270aSIlpo Järvinen EXPORT_SYMBOL(skb_push);
2461c2aa270aSIlpo Järvinen
2462c2aa270aSIlpo Järvinen /**
24636be8ac2fSIlpo Järvinen * skb_pull - remove data from the start of a buffer
24646be8ac2fSIlpo Järvinen * @skb: buffer to use
24656be8ac2fSIlpo Järvinen * @len: amount of data to remove
24666be8ac2fSIlpo Järvinen *
24676be8ac2fSIlpo Järvinen * This function removes data from the start of a buffer, returning
24686be8ac2fSIlpo Järvinen * the memory to the headroom. A pointer to the next data in the buffer
24696be8ac2fSIlpo Järvinen * is returned. Once the data has been pulled future pushes will overwrite
24706be8ac2fSIlpo Järvinen * the old data.
24716be8ac2fSIlpo Järvinen */
skb_pull(struct sk_buff * skb,unsigned int len)2472af72868bSJohannes Berg void *skb_pull(struct sk_buff *skb, unsigned int len)
24736be8ac2fSIlpo Järvinen {
247447d29646SDavid S. Miller return skb_pull_inline(skb, len);
24756be8ac2fSIlpo Järvinen }
24766be8ac2fSIlpo Järvinen EXPORT_SYMBOL(skb_pull);
24776be8ac2fSIlpo Järvinen
2478419ae74eSIlpo Järvinen /**
247913244cccSLuiz Augusto von Dentz * skb_pull_data - remove data from the start of a buffer returning its
248013244cccSLuiz Augusto von Dentz * original position.
248113244cccSLuiz Augusto von Dentz * @skb: buffer to use
248213244cccSLuiz Augusto von Dentz * @len: amount of data to remove
248313244cccSLuiz Augusto von Dentz *
248413244cccSLuiz Augusto von Dentz * This function removes data from the start of a buffer, returning
248513244cccSLuiz Augusto von Dentz * the memory to the headroom. A pointer to the original data in the buffer
248613244cccSLuiz Augusto von Dentz * is returned after checking if there is enough data to pull. Once the
248713244cccSLuiz Augusto von Dentz * data has been pulled future pushes will overwrite the old data.
248813244cccSLuiz Augusto von Dentz */
skb_pull_data(struct sk_buff * skb,size_t len)248913244cccSLuiz Augusto von Dentz void *skb_pull_data(struct sk_buff *skb, size_t len)
249013244cccSLuiz Augusto von Dentz {
249113244cccSLuiz Augusto von Dentz void *data = skb->data;
249213244cccSLuiz Augusto von Dentz
249313244cccSLuiz Augusto von Dentz if (skb->len < len)
249413244cccSLuiz Augusto von Dentz return NULL;
249513244cccSLuiz Augusto von Dentz
249613244cccSLuiz Augusto von Dentz skb_pull(skb, len);
249713244cccSLuiz Augusto von Dentz
249813244cccSLuiz Augusto von Dentz return data;
249913244cccSLuiz Augusto von Dentz }
250013244cccSLuiz Augusto von Dentz EXPORT_SYMBOL(skb_pull_data);
250113244cccSLuiz Augusto von Dentz
250213244cccSLuiz Augusto von Dentz /**
2503419ae74eSIlpo Järvinen * skb_trim - remove end from a buffer
2504419ae74eSIlpo Järvinen * @skb: buffer to alter
2505419ae74eSIlpo Järvinen * @len: new length
2506419ae74eSIlpo Järvinen *
2507419ae74eSIlpo Järvinen * Cut the length of a buffer down by removing data from the tail. If
2508419ae74eSIlpo Järvinen * the buffer is already under the length specified it is not modified.
2509419ae74eSIlpo Järvinen * The skb must be linear.
2510419ae74eSIlpo Järvinen */
skb_trim(struct sk_buff * skb,unsigned int len)2511419ae74eSIlpo Järvinen void skb_trim(struct sk_buff *skb, unsigned int len)
2512419ae74eSIlpo Järvinen {
2513419ae74eSIlpo Järvinen if (skb->len > len)
2514419ae74eSIlpo Järvinen __skb_trim(skb, len);
2515419ae74eSIlpo Järvinen }
2516419ae74eSIlpo Järvinen EXPORT_SYMBOL(skb_trim);
2517419ae74eSIlpo Järvinen
25183cc0e873SHerbert Xu /* Trims skb to length len. It can change skb pointers.
25191da177e4SLinus Torvalds */
25201da177e4SLinus Torvalds
___pskb_trim(struct sk_buff * skb,unsigned int len)25213cc0e873SHerbert Xu int ___pskb_trim(struct sk_buff *skb, unsigned int len)
25221da177e4SLinus Torvalds {
252327b437c8SHerbert Xu struct sk_buff **fragp;
252427b437c8SHerbert Xu struct sk_buff *frag;
25251da177e4SLinus Torvalds int offset = skb_headlen(skb);
25261da177e4SLinus Torvalds int nfrags = skb_shinfo(skb)->nr_frags;
25271da177e4SLinus Torvalds int i;
252827b437c8SHerbert Xu int err;
252927b437c8SHerbert Xu
253027b437c8SHerbert Xu if (skb_cloned(skb) &&
253127b437c8SHerbert Xu unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
253227b437c8SHerbert Xu return err;
25331da177e4SLinus Torvalds
2534f4d26fb3SHerbert Xu i = 0;
2535f4d26fb3SHerbert Xu if (offset >= len)
2536f4d26fb3SHerbert Xu goto drop_pages;
2537f4d26fb3SHerbert Xu
2538f4d26fb3SHerbert Xu for (; i < nfrags; i++) {
25399e903e08SEric Dumazet int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]);
254027b437c8SHerbert Xu
254127b437c8SHerbert Xu if (end < len) {
25421da177e4SLinus Torvalds offset = end;
254327b437c8SHerbert Xu continue;
25441da177e4SLinus Torvalds }
25451da177e4SLinus Torvalds
25469e903e08SEric Dumazet skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset);
254727b437c8SHerbert Xu
2548f4d26fb3SHerbert Xu drop_pages:
254927b437c8SHerbert Xu skb_shinfo(skb)->nr_frags = i;
255027b437c8SHerbert Xu
255127b437c8SHerbert Xu for (; i < nfrags; i++)
2552ea2ab693SIan Campbell skb_frag_unref(skb, i);
255327b437c8SHerbert Xu
255421dc3301SDavid S. Miller if (skb_has_frag_list(skb))
255527b437c8SHerbert Xu skb_drop_fraglist(skb);
2556f4d26fb3SHerbert Xu goto done;
255727b437c8SHerbert Xu }
255827b437c8SHerbert Xu
255927b437c8SHerbert Xu for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
256027b437c8SHerbert Xu fragp = &frag->next) {
256127b437c8SHerbert Xu int end = offset + frag->len;
256227b437c8SHerbert Xu
256327b437c8SHerbert Xu if (skb_shared(frag)) {
256427b437c8SHerbert Xu struct sk_buff *nfrag;
256527b437c8SHerbert Xu
256627b437c8SHerbert Xu nfrag = skb_clone(frag, GFP_ATOMIC);
256727b437c8SHerbert Xu if (unlikely(!nfrag))
256827b437c8SHerbert Xu return -ENOMEM;
256927b437c8SHerbert Xu
257027b437c8SHerbert Xu nfrag->next = frag->next;
257185bb2a60SEric Dumazet consume_skb(frag);
257227b437c8SHerbert Xu frag = nfrag;
257327b437c8SHerbert Xu *fragp = frag;
257427b437c8SHerbert Xu }
257527b437c8SHerbert Xu
257627b437c8SHerbert Xu if (end < len) {
257727b437c8SHerbert Xu offset = end;
257827b437c8SHerbert Xu continue;
257927b437c8SHerbert Xu }
258027b437c8SHerbert Xu
258127b437c8SHerbert Xu if (end > len &&
258227b437c8SHerbert Xu unlikely((err = pskb_trim(frag, len - offset))))
258327b437c8SHerbert Xu return err;
258427b437c8SHerbert Xu
258527b437c8SHerbert Xu if (frag->next)
258627b437c8SHerbert Xu skb_drop_list(&frag->next);
258727b437c8SHerbert Xu break;
258827b437c8SHerbert Xu }
258927b437c8SHerbert Xu
2590f4d26fb3SHerbert Xu done:
259127b437c8SHerbert Xu if (len > skb_headlen(skb)) {
25921da177e4SLinus Torvalds skb->data_len -= skb->len - len;
25931da177e4SLinus Torvalds skb->len = len;
25941da177e4SLinus Torvalds } else {
25951da177e4SLinus Torvalds skb->len = len;
25961da177e4SLinus Torvalds skb->data_len = 0;
259727a884dcSArnaldo Carvalho de Melo skb_set_tail_pointer(skb, len);
25981da177e4SLinus Torvalds }
25991da177e4SLinus Torvalds
2600c21b48ccSEric Dumazet if (!skb->sk || skb->destructor == sock_edemux)
2601c21b48ccSEric Dumazet skb_condense(skb);
26021da177e4SLinus Torvalds return 0;
26031da177e4SLinus Torvalds }
2604b4ac530fSDavid S. Miller EXPORT_SYMBOL(___pskb_trim);
26051da177e4SLinus Torvalds
260688078d98SEric Dumazet /* Note : use pskb_trim_rcsum() instead of calling this directly
260788078d98SEric Dumazet */
pskb_trim_rcsum_slow(struct sk_buff * skb,unsigned int len)260888078d98SEric Dumazet int pskb_trim_rcsum_slow(struct sk_buff *skb, unsigned int len)
260988078d98SEric Dumazet {
261088078d98SEric Dumazet if (skb->ip_summed == CHECKSUM_COMPLETE) {
261188078d98SEric Dumazet int delta = skb->len - len;
261288078d98SEric Dumazet
2613d55bef50SDimitris Michailidis skb->csum = csum_block_sub(skb->csum,
2614d55bef50SDimitris Michailidis skb_checksum(skb, len, delta, 0),
2615d55bef50SDimitris Michailidis len);
261654970a2fSVasily Averin } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
261754970a2fSVasily Averin int hdlen = (len > skb_headlen(skb)) ? skb_headlen(skb) : len;
261854970a2fSVasily Averin int offset = skb_checksum_start_offset(skb) + skb->csum_offset;
261954970a2fSVasily Averin
262054970a2fSVasily Averin if (offset + sizeof(__sum16) > hdlen)
262154970a2fSVasily Averin return -EINVAL;
262288078d98SEric Dumazet }
262388078d98SEric Dumazet return __pskb_trim(skb, len);
262488078d98SEric Dumazet }
262588078d98SEric Dumazet EXPORT_SYMBOL(pskb_trim_rcsum_slow);
262688078d98SEric Dumazet
26271da177e4SLinus Torvalds /**
26281da177e4SLinus Torvalds * __pskb_pull_tail - advance tail of skb header
26291da177e4SLinus Torvalds * @skb: buffer to reallocate
26301da177e4SLinus Torvalds * @delta: number of bytes to advance tail
26311da177e4SLinus Torvalds *
26321da177e4SLinus Torvalds * The function makes a sense only on a fragmented &sk_buff,
26331da177e4SLinus Torvalds * it expands header moving its tail forward and copying necessary
26341da177e4SLinus Torvalds * data from fragmented part.
26351da177e4SLinus Torvalds *
26361da177e4SLinus Torvalds * &sk_buff MUST have reference count of 1.
26371da177e4SLinus Torvalds *
26381da177e4SLinus Torvalds * Returns %NULL (and &sk_buff does not change) if pull failed
26391da177e4SLinus Torvalds * or value of new tail of skb in the case of success.
26401da177e4SLinus Torvalds *
26411da177e4SLinus Torvalds * All the pointers pointing into skb header may change and must be
26421da177e4SLinus Torvalds * reloaded after call to this function.
26431da177e4SLinus Torvalds */
26441da177e4SLinus Torvalds
26451da177e4SLinus Torvalds /* Moves tail of skb head forward, copying data from fragmented part,
26461da177e4SLinus Torvalds * when it is necessary.
26471da177e4SLinus Torvalds * 1. It may fail due to malloc failure.
26481da177e4SLinus Torvalds * 2. It may change skb pointers.
26491da177e4SLinus Torvalds *
26501da177e4SLinus Torvalds * It is pretty complicated. Luckily, it is called only in exceptional cases.
26511da177e4SLinus Torvalds */
__pskb_pull_tail(struct sk_buff * skb,int delta)2652af72868bSJohannes Berg void *__pskb_pull_tail(struct sk_buff *skb, int delta)
26531da177e4SLinus Torvalds {
26541da177e4SLinus Torvalds /* If skb has not enough free space at tail, get new one
26551da177e4SLinus Torvalds * plus 128 bytes for future expansions. If we have enough
26561da177e4SLinus Torvalds * room at tail, reallocate without expansion only if skb is cloned.
26571da177e4SLinus Torvalds */
26584305b541SArnaldo Carvalho de Melo int i, k, eat = (skb->tail + delta) - skb->end;
26591da177e4SLinus Torvalds
26601da177e4SLinus Torvalds if (eat > 0 || skb_cloned(skb)) {
26611da177e4SLinus Torvalds if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
26621da177e4SLinus Torvalds GFP_ATOMIC))
26631da177e4SLinus Torvalds return NULL;
26641da177e4SLinus Torvalds }
26651da177e4SLinus Torvalds
26669f77fad3STim Hansen BUG_ON(skb_copy_bits(skb, skb_headlen(skb),
26679f77fad3STim Hansen skb_tail_pointer(skb), delta));
26681da177e4SLinus Torvalds
26691da177e4SLinus Torvalds /* Optimization: no fragments, no reasons to preestimate
26701da177e4SLinus Torvalds * size of pulled pages. Superb.
26711da177e4SLinus Torvalds */
267221dc3301SDavid S. Miller if (!skb_has_frag_list(skb))
26731da177e4SLinus Torvalds goto pull_pages;
26741da177e4SLinus Torvalds
26751da177e4SLinus Torvalds /* Estimate size of pulled pages. */
26761da177e4SLinus Torvalds eat = delta;
26771da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
26789e903e08SEric Dumazet int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
26799e903e08SEric Dumazet
26809e903e08SEric Dumazet if (size >= eat)
26811da177e4SLinus Torvalds goto pull_pages;
26829e903e08SEric Dumazet eat -= size;
26831da177e4SLinus Torvalds }
26841da177e4SLinus Torvalds
26851da177e4SLinus Torvalds /* If we need update frag list, we are in troubles.
268609001b03SWenhua Shi * Certainly, it is possible to add an offset to skb data,
26871da177e4SLinus Torvalds * but taking into account that pulling is expected to
26881da177e4SLinus Torvalds * be very rare operation, it is worth to fight against
26891da177e4SLinus Torvalds * further bloating skb head and crucify ourselves here instead.
26901da177e4SLinus Torvalds * Pure masohism, indeed. 8)8)
26911da177e4SLinus Torvalds */
26921da177e4SLinus Torvalds if (eat) {
26931da177e4SLinus Torvalds struct sk_buff *list = skb_shinfo(skb)->frag_list;
26941da177e4SLinus Torvalds struct sk_buff *clone = NULL;
26951da177e4SLinus Torvalds struct sk_buff *insp = NULL;
26961da177e4SLinus Torvalds
26971da177e4SLinus Torvalds do {
26981da177e4SLinus Torvalds if (list->len <= eat) {
26991da177e4SLinus Torvalds /* Eaten as whole. */
27001da177e4SLinus Torvalds eat -= list->len;
27011da177e4SLinus Torvalds list = list->next;
27021da177e4SLinus Torvalds insp = list;
27031da177e4SLinus Torvalds } else {
27041da177e4SLinus Torvalds /* Eaten partially. */
27052d7afdcbSSubash Abhinov Kasiviswanathan if (skb_is_gso(skb) && !list->head_frag &&
27062d7afdcbSSubash Abhinov Kasiviswanathan skb_headlen(list))
27072d7afdcbSSubash Abhinov Kasiviswanathan skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
27081da177e4SLinus Torvalds
27091da177e4SLinus Torvalds if (skb_shared(list)) {
27101da177e4SLinus Torvalds /* Sucks! We need to fork list. :-( */
27111da177e4SLinus Torvalds clone = skb_clone(list, GFP_ATOMIC);
27121da177e4SLinus Torvalds if (!clone)
27131da177e4SLinus Torvalds return NULL;
27141da177e4SLinus Torvalds insp = list->next;
27151da177e4SLinus Torvalds list = clone;
27161da177e4SLinus Torvalds } else {
27171da177e4SLinus Torvalds /* This may be pulled without
27181da177e4SLinus Torvalds * problems. */
27191da177e4SLinus Torvalds insp = list;
27201da177e4SLinus Torvalds }
27211da177e4SLinus Torvalds if (!pskb_pull(list, eat)) {
27221da177e4SLinus Torvalds kfree_skb(clone);
27231da177e4SLinus Torvalds return NULL;
27241da177e4SLinus Torvalds }
27251da177e4SLinus Torvalds break;
27261da177e4SLinus Torvalds }
27271da177e4SLinus Torvalds } while (eat);
27281da177e4SLinus Torvalds
27291da177e4SLinus Torvalds /* Free pulled out fragments. */
27301da177e4SLinus Torvalds while ((list = skb_shinfo(skb)->frag_list) != insp) {
27311da177e4SLinus Torvalds skb_shinfo(skb)->frag_list = list->next;
2732ef527f96SEric Dumazet consume_skb(list);
27331da177e4SLinus Torvalds }
27341da177e4SLinus Torvalds /* And insert new clone at head. */
27351da177e4SLinus Torvalds if (clone) {
27361da177e4SLinus Torvalds clone->next = list;
27371da177e4SLinus Torvalds skb_shinfo(skb)->frag_list = clone;
27381da177e4SLinus Torvalds }
27391da177e4SLinus Torvalds }
27401da177e4SLinus Torvalds /* Success! Now we may commit changes to skb data. */
27411da177e4SLinus Torvalds
27421da177e4SLinus Torvalds pull_pages:
27431da177e4SLinus Torvalds eat = delta;
27441da177e4SLinus Torvalds k = 0;
27451da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
27469e903e08SEric Dumazet int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
27479e903e08SEric Dumazet
27489e903e08SEric Dumazet if (size <= eat) {
2749ea2ab693SIan Campbell skb_frag_unref(skb, i);
27509e903e08SEric Dumazet eat -= size;
27511da177e4SLinus Torvalds } else {
2752b54c9d5bSJonathan Lemon skb_frag_t *frag = &skb_shinfo(skb)->frags[k];
2753b54c9d5bSJonathan Lemon
2754b54c9d5bSJonathan Lemon *frag = skb_shinfo(skb)->frags[i];
27551da177e4SLinus Torvalds if (eat) {
2756b54c9d5bSJonathan Lemon skb_frag_off_add(frag, eat);
2757b54c9d5bSJonathan Lemon skb_frag_size_sub(frag, eat);
27583ccc6c6fSlinzhang if (!i)
27593ccc6c6fSlinzhang goto end;
27601da177e4SLinus Torvalds eat = 0;
27611da177e4SLinus Torvalds }
27621da177e4SLinus Torvalds k++;
27631da177e4SLinus Torvalds }
27641da177e4SLinus Torvalds }
27651da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = k;
27661da177e4SLinus Torvalds
27673ccc6c6fSlinzhang end:
27681da177e4SLinus Torvalds skb->tail += delta;
27691da177e4SLinus Torvalds skb->data_len -= delta;
27701da177e4SLinus Torvalds
27711f8b977aSWillem de Bruijn if (!skb->data_len)
27721f8b977aSWillem de Bruijn skb_zcopy_clear(skb, false);
27731f8b977aSWillem de Bruijn
277427a884dcSArnaldo Carvalho de Melo return skb_tail_pointer(skb);
27751da177e4SLinus Torvalds }
2776b4ac530fSDavid S. Miller EXPORT_SYMBOL(__pskb_pull_tail);
27771da177e4SLinus Torvalds
277822019b17SEric Dumazet /**
277922019b17SEric Dumazet * skb_copy_bits - copy bits from skb to kernel buffer
278022019b17SEric Dumazet * @skb: source skb
278122019b17SEric Dumazet * @offset: offset in source
278222019b17SEric Dumazet * @to: destination buffer
278322019b17SEric Dumazet * @len: number of bytes to copy
278422019b17SEric Dumazet *
278522019b17SEric Dumazet * Copy the specified number of bytes from the source skb to the
278622019b17SEric Dumazet * destination buffer.
278722019b17SEric Dumazet *
278822019b17SEric Dumazet * CAUTION ! :
278922019b17SEric Dumazet * If its prototype is ever changed,
279022019b17SEric Dumazet * check arch/{*}/net/{*}.S files,
279122019b17SEric Dumazet * since it is called from BPF assembly code.
279222019b17SEric Dumazet */
skb_copy_bits(const struct sk_buff * skb,int offset,void * to,int len)27931da177e4SLinus Torvalds int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
27941da177e4SLinus Torvalds {
27951a028e50SDavid S. Miller int start = skb_headlen(skb);
2796fbb398a8SDavid S. Miller struct sk_buff *frag_iter;
2797fbb398a8SDavid S. Miller int i, copy;
27981da177e4SLinus Torvalds
27991da177e4SLinus Torvalds if (offset > (int)skb->len - len)
28001da177e4SLinus Torvalds goto fault;
28011da177e4SLinus Torvalds
28021da177e4SLinus Torvalds /* Copy header. */
28031a028e50SDavid S. Miller if ((copy = start - offset) > 0) {
28041da177e4SLinus Torvalds if (copy > len)
28051da177e4SLinus Torvalds copy = len;
2806d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data_offset(skb, offset, to, copy);
28071da177e4SLinus Torvalds if ((len -= copy) == 0)
28081da177e4SLinus Torvalds return 0;
28091da177e4SLinus Torvalds offset += copy;
28101da177e4SLinus Torvalds to += copy;
28111da177e4SLinus Torvalds }
28121da177e4SLinus Torvalds
28131da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
28141a028e50SDavid S. Miller int end;
281551c56b00SEric Dumazet skb_frag_t *f = &skb_shinfo(skb)->frags[i];
28161da177e4SLinus Torvalds
2817547b792cSIlpo Järvinen WARN_ON(start > offset + len);
28181a028e50SDavid S. Miller
281951c56b00SEric Dumazet end = start + skb_frag_size(f);
28201da177e4SLinus Torvalds if ((copy = end - offset) > 0) {
2821c613c209SWillem de Bruijn u32 p_off, p_len, copied;
2822c613c209SWillem de Bruijn struct page *p;
28231da177e4SLinus Torvalds u8 *vaddr;
28241da177e4SLinus Torvalds
28251da177e4SLinus Torvalds if (copy > len)
28261da177e4SLinus Torvalds copy = len;
28271da177e4SLinus Torvalds
2828c613c209SWillem de Bruijn skb_frag_foreach_page(f,
2829b54c9d5bSJonathan Lemon skb_frag_off(f) + offset - start,
2830c613c209SWillem de Bruijn copy, p, p_off, p_len, copied) {
2831c613c209SWillem de Bruijn vaddr = kmap_atomic(p);
2832c613c209SWillem de Bruijn memcpy(to + copied, vaddr + p_off, p_len);
283351c56b00SEric Dumazet kunmap_atomic(vaddr);
2834c613c209SWillem de Bruijn }
28351da177e4SLinus Torvalds
28361da177e4SLinus Torvalds if ((len -= copy) == 0)
28371da177e4SLinus Torvalds return 0;
28381da177e4SLinus Torvalds offset += copy;
28391da177e4SLinus Torvalds to += copy;
28401da177e4SLinus Torvalds }
28411a028e50SDavid S. Miller start = end;
28421da177e4SLinus Torvalds }
28431da177e4SLinus Torvalds
2844fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) {
28451a028e50SDavid S. Miller int end;
28461da177e4SLinus Torvalds
2847547b792cSIlpo Järvinen WARN_ON(start > offset + len);
28481a028e50SDavid S. Miller
2849fbb398a8SDavid S. Miller end = start + frag_iter->len;
28501da177e4SLinus Torvalds if ((copy = end - offset) > 0) {
28511da177e4SLinus Torvalds if (copy > len)
28521da177e4SLinus Torvalds copy = len;
2853fbb398a8SDavid S. Miller if (skb_copy_bits(frag_iter, offset - start, to, copy))
28541da177e4SLinus Torvalds goto fault;
28551da177e4SLinus Torvalds if ((len -= copy) == 0)
28561da177e4SLinus Torvalds return 0;
28571da177e4SLinus Torvalds offset += copy;
28581da177e4SLinus Torvalds to += copy;
28591da177e4SLinus Torvalds }
28601a028e50SDavid S. Miller start = end;
28611da177e4SLinus Torvalds }
2862a6686f2fSShirley Ma
28631da177e4SLinus Torvalds if (!len)
28641da177e4SLinus Torvalds return 0;
28651da177e4SLinus Torvalds
28661da177e4SLinus Torvalds fault:
28671da177e4SLinus Torvalds return -EFAULT;
28681da177e4SLinus Torvalds }
2869b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_bits);
28701da177e4SLinus Torvalds
28719c55e01cSJens Axboe /*
28729c55e01cSJens Axboe * Callback from splice_to_pipe(), if we need to release some pages
28739c55e01cSJens Axboe * at the end of the spd in case we error'ed out in filling the pipe.
28749c55e01cSJens Axboe */
sock_spd_release(struct splice_pipe_desc * spd,unsigned int i)28759c55e01cSJens Axboe static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
28769c55e01cSJens Axboe {
28778b9d3728SJarek Poplawski put_page(spd->pages[i]);
28788b9d3728SJarek Poplawski }
28799c55e01cSJens Axboe
linear_to_page(struct page * page,unsigned int * len,unsigned int * offset,struct sock * sk)2880a108d5f3SDavid S. Miller static struct page *linear_to_page(struct page *page, unsigned int *len,
28814fb66994SJarek Poplawski unsigned int *offset,
288218aafc62SEric Dumazet struct sock *sk)
28838b9d3728SJarek Poplawski {
28845640f768SEric Dumazet struct page_frag *pfrag = sk_page_frag(sk);
28858b9d3728SJarek Poplawski
28865640f768SEric Dumazet if (!sk_page_frag_refill(sk, pfrag))
28878b9d3728SJarek Poplawski return NULL;
28884fb66994SJarek Poplawski
28895640f768SEric Dumazet *len = min_t(unsigned int, *len, pfrag->size - pfrag->offset);
28904fb66994SJarek Poplawski
28915640f768SEric Dumazet memcpy(page_address(pfrag->page) + pfrag->offset,
28925640f768SEric Dumazet page_address(page) + *offset, *len);
28935640f768SEric Dumazet *offset = pfrag->offset;
28945640f768SEric Dumazet pfrag->offset += *len;
28954fb66994SJarek Poplawski
28965640f768SEric Dumazet return pfrag->page;
28979c55e01cSJens Axboe }
28989c55e01cSJens Axboe
spd_can_coalesce(const struct splice_pipe_desc * spd,struct page * page,unsigned int offset)289941c73a0dSEric Dumazet static bool spd_can_coalesce(const struct splice_pipe_desc *spd,
290041c73a0dSEric Dumazet struct page *page,
290141c73a0dSEric Dumazet unsigned int offset)
290241c73a0dSEric Dumazet {
290341c73a0dSEric Dumazet return spd->nr_pages &&
290441c73a0dSEric Dumazet spd->pages[spd->nr_pages - 1] == page &&
290541c73a0dSEric Dumazet (spd->partial[spd->nr_pages - 1].offset +
290641c73a0dSEric Dumazet spd->partial[spd->nr_pages - 1].len == offset);
290741c73a0dSEric Dumazet }
290841c73a0dSEric Dumazet
29099c55e01cSJens Axboe /*
29109c55e01cSJens Axboe * Fill page/offset/length into spd, if it can hold more pages.
29119c55e01cSJens Axboe */
spd_fill_page(struct splice_pipe_desc * spd,struct pipe_inode_info * pipe,struct page * page,unsigned int * len,unsigned int offset,bool linear,struct sock * sk)2912a108d5f3SDavid S. Miller static bool spd_fill_page(struct splice_pipe_desc *spd,
291335f3d14dSJens Axboe struct pipe_inode_info *pipe, struct page *page,
29144fb66994SJarek Poplawski unsigned int *len, unsigned int offset,
291518aafc62SEric Dumazet bool linear,
29167a67e56fSJarek Poplawski struct sock *sk)
29179c55e01cSJens Axboe {
291841c73a0dSEric Dumazet if (unlikely(spd->nr_pages == MAX_SKB_FRAGS))
2919a108d5f3SDavid S. Miller return true;
29209c55e01cSJens Axboe
29218b9d3728SJarek Poplawski if (linear) {
292218aafc62SEric Dumazet page = linear_to_page(page, len, &offset, sk);
29238b9d3728SJarek Poplawski if (!page)
2924a108d5f3SDavid S. Miller return true;
292541c73a0dSEric Dumazet }
292641c73a0dSEric Dumazet if (spd_can_coalesce(spd, page, offset)) {
292741c73a0dSEric Dumazet spd->partial[spd->nr_pages - 1].len += *len;
2928a108d5f3SDavid S. Miller return false;
292941c73a0dSEric Dumazet }
29308b9d3728SJarek Poplawski get_page(page);
29319c55e01cSJens Axboe spd->pages[spd->nr_pages] = page;
29324fb66994SJarek Poplawski spd->partial[spd->nr_pages].len = *len;
29339c55e01cSJens Axboe spd->partial[spd->nr_pages].offset = offset;
29349c55e01cSJens Axboe spd->nr_pages++;
29358b9d3728SJarek Poplawski
2936a108d5f3SDavid S. Miller return false;
29379c55e01cSJens Axboe }
29389c55e01cSJens Axboe
__splice_segment(struct page * page,unsigned int poff,unsigned int plen,unsigned int * off,unsigned int * len,struct splice_pipe_desc * spd,bool linear,struct sock * sk,struct pipe_inode_info * pipe)2939a108d5f3SDavid S. Miller static bool __splice_segment(struct page *page, unsigned int poff,
29402870c43dSOctavian Purdila unsigned int plen, unsigned int *off,
294118aafc62SEric Dumazet unsigned int *len,
2942d7ccf7c0SEric Dumazet struct splice_pipe_desc *spd, bool linear,
294335f3d14dSJens Axboe struct sock *sk,
294435f3d14dSJens Axboe struct pipe_inode_info *pipe)
29459c55e01cSJens Axboe {
29462870c43dSOctavian Purdila if (!*len)
2947a108d5f3SDavid S. Miller return true;
29489c55e01cSJens Axboe
29492870c43dSOctavian Purdila /* skip this segment if already processed */
29502870c43dSOctavian Purdila if (*off >= plen) {
29512870c43dSOctavian Purdila *off -= plen;
2952a108d5f3SDavid S. Miller return false;
29532870c43dSOctavian Purdila }
29542870c43dSOctavian Purdila
29552870c43dSOctavian Purdila /* ignore any bits we already processed */
29569ca1b22dSEric Dumazet poff += *off;
29579ca1b22dSEric Dumazet plen -= *off;
29582870c43dSOctavian Purdila *off = 0;
29592870c43dSOctavian Purdila
296018aafc62SEric Dumazet do {
296118aafc62SEric Dumazet unsigned int flen = min(*len, plen);
29622870c43dSOctavian Purdila
296318aafc62SEric Dumazet if (spd_fill_page(spd, pipe, page, &flen, poff,
296418aafc62SEric Dumazet linear, sk))
2965a108d5f3SDavid S. Miller return true;
296618aafc62SEric Dumazet poff += flen;
296718aafc62SEric Dumazet plen -= flen;
29682870c43dSOctavian Purdila *len -= flen;
296918aafc62SEric Dumazet } while (*len && plen);
29702870c43dSOctavian Purdila
2971a108d5f3SDavid S. Miller return false;
2972db43a282SOctavian Purdila }
29739c55e01cSJens Axboe
29749c55e01cSJens Axboe /*
2975a108d5f3SDavid S. Miller * Map linear and fragment data from the skb to spd. It reports true if the
29762870c43dSOctavian Purdila * pipe is full or if we already spliced the requested length.
29779c55e01cSJens Axboe */
__skb_splice_bits(struct sk_buff * skb,struct pipe_inode_info * pipe,unsigned int * offset,unsigned int * len,struct splice_pipe_desc * spd,struct sock * sk)2978a108d5f3SDavid S. Miller static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe,
297935f3d14dSJens Axboe unsigned int *offset, unsigned int *len,
298035f3d14dSJens Axboe struct splice_pipe_desc *spd, struct sock *sk)
29812870c43dSOctavian Purdila {
29822870c43dSOctavian Purdila int seg;
2983fa9835e5STom Herbert struct sk_buff *iter;
29849c55e01cSJens Axboe
29851d0c0b32SEric Dumazet /* map the linear part :
29862996d31fSAlexander Duyck * If skb->head_frag is set, this 'linear' part is backed by a
29872996d31fSAlexander Duyck * fragment, and if the head is not shared with any clones then
29882996d31fSAlexander Duyck * we can avoid a copy since we own the head portion of this page.
29899c55e01cSJens Axboe */
29902870c43dSOctavian Purdila if (__splice_segment(virt_to_page(skb->data),
29912870c43dSOctavian Purdila (unsigned long) skb->data & (PAGE_SIZE - 1),
29922870c43dSOctavian Purdila skb_headlen(skb),
299318aafc62SEric Dumazet offset, len, spd,
29943a7c1ee4SAlexander Duyck skb_head_is_locked(skb),
29951d0c0b32SEric Dumazet sk, pipe))
2996a108d5f3SDavid S. Miller return true;
29979c55e01cSJens Axboe
29989c55e01cSJens Axboe /*
29999c55e01cSJens Axboe * then map the fragments
30009c55e01cSJens Axboe */
30019c55e01cSJens Axboe for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
30029c55e01cSJens Axboe const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
30039c55e01cSJens Axboe
3004ea2ab693SIan Campbell if (__splice_segment(skb_frag_page(f),
3005b54c9d5bSJonathan Lemon skb_frag_off(f), skb_frag_size(f),
300618aafc62SEric Dumazet offset, len, spd, false, sk, pipe))
3007a108d5f3SDavid S. Miller return true;
30089c55e01cSJens Axboe }
30099c55e01cSJens Axboe
3010fa9835e5STom Herbert skb_walk_frags(skb, iter) {
3011fa9835e5STom Herbert if (*offset >= iter->len) {
3012fa9835e5STom Herbert *offset -= iter->len;
3013fa9835e5STom Herbert continue;
3014fa9835e5STom Herbert }
3015fa9835e5STom Herbert /* __skb_splice_bits() only fails if the output has no room
3016fa9835e5STom Herbert * left, so no point in going over the frag_list for the error
3017fa9835e5STom Herbert * case.
3018fa9835e5STom Herbert */
3019fa9835e5STom Herbert if (__skb_splice_bits(iter, pipe, offset, len, spd, sk))
3020fa9835e5STom Herbert return true;
3021fa9835e5STom Herbert }
3022fa9835e5STom Herbert
3023a108d5f3SDavid S. Miller return false;
30249c55e01cSJens Axboe }
30259c55e01cSJens Axboe
30269c55e01cSJens Axboe /*
30279c55e01cSJens Axboe * Map data from the skb to a pipe. Should handle both the linear part,
3028fa9835e5STom Herbert * the fragments, and the frag list.
30299c55e01cSJens Axboe */
skb_splice_bits(struct sk_buff * skb,struct sock * sk,unsigned int offset,struct pipe_inode_info * pipe,unsigned int tlen,unsigned int flags)3030a60e3cc7SHannes Frederic Sowa int skb_splice_bits(struct sk_buff *skb, struct sock *sk, unsigned int offset,
30319c55e01cSJens Axboe struct pipe_inode_info *pipe, unsigned int tlen,
303225869262SAl Viro unsigned int flags)
30339c55e01cSJens Axboe {
303441c73a0dSEric Dumazet struct partial_page partial[MAX_SKB_FRAGS];
303541c73a0dSEric Dumazet struct page *pages[MAX_SKB_FRAGS];
30369c55e01cSJens Axboe struct splice_pipe_desc spd = {
30379c55e01cSJens Axboe .pages = pages,
30389c55e01cSJens Axboe .partial = partial,
3039047fe360SEric Dumazet .nr_pages_max = MAX_SKB_FRAGS,
304028a625cbSMiklos Szeredi .ops = &nosteal_pipe_buf_ops,
30419c55e01cSJens Axboe .spd_release = sock_spd_release,
30429c55e01cSJens Axboe };
304335f3d14dSJens Axboe int ret = 0;
304435f3d14dSJens Axboe
3045fa9835e5STom Herbert __skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk);
30469c55e01cSJens Axboe
3047a60e3cc7SHannes Frederic Sowa if (spd.nr_pages)
304825869262SAl Viro ret = splice_to_pipe(pipe, &spd);
30499c55e01cSJens Axboe
305035f3d14dSJens Axboe return ret;
30519c55e01cSJens Axboe }
30522b514574SHannes Frederic Sowa EXPORT_SYMBOL_GPL(skb_splice_bits);
30539c55e01cSJens Axboe
sendmsg_locked(struct sock * sk,struct msghdr * msg)3054c729ed6fSDavid Howells static int sendmsg_locked(struct sock *sk, struct msghdr *msg)
3055c729ed6fSDavid Howells {
3056c729ed6fSDavid Howells struct socket *sock = sk->sk_socket;
3057c729ed6fSDavid Howells size_t size = msg_data_left(msg);
3058c729ed6fSDavid Howells
3059c729ed6fSDavid Howells if (!sock)
3060c729ed6fSDavid Howells return -EINVAL;
3061c729ed6fSDavid Howells
3062c729ed6fSDavid Howells if (!sock->ops->sendmsg_locked)
3063c729ed6fSDavid Howells return sock_no_sendmsg_locked(sk, msg, size);
3064c729ed6fSDavid Howells
3065c729ed6fSDavid Howells return sock->ops->sendmsg_locked(sk, msg, size);
3066c729ed6fSDavid Howells }
3067c729ed6fSDavid Howells
sendmsg_unlocked(struct sock * sk,struct msghdr * msg)3068c729ed6fSDavid Howells static int sendmsg_unlocked(struct sock *sk, struct msghdr *msg)
30690739cd28SCong Wang {
30700739cd28SCong Wang struct socket *sock = sk->sk_socket;
30710739cd28SCong Wang
30720739cd28SCong Wang if (!sock)
30730739cd28SCong Wang return -EINVAL;
3074c729ed6fSDavid Howells return sock_sendmsg(sock, msg);
30750739cd28SCong Wang }
30760739cd28SCong Wang
3077c729ed6fSDavid Howells typedef int (*sendmsg_func)(struct sock *sk, struct msghdr *msg);
__skb_send_sock(struct sock * sk,struct sk_buff * skb,int offset,int len,sendmsg_func sendmsg)30780739cd28SCong Wang static int __skb_send_sock(struct sock *sk, struct sk_buff *skb, int offset,
3079c729ed6fSDavid Howells int len, sendmsg_func sendmsg)
308020bf50deSTom Herbert {
308120bf50deSTom Herbert unsigned int orig_len = len;
308220bf50deSTom Herbert struct sk_buff *head = skb;
308320bf50deSTom Herbert unsigned short fragidx;
308420bf50deSTom Herbert int slen, ret;
308520bf50deSTom Herbert
308620bf50deSTom Herbert do_frag_list:
308720bf50deSTom Herbert
308820bf50deSTom Herbert /* Deal with head data */
308920bf50deSTom Herbert while (offset < skb_headlen(skb) && len) {
309020bf50deSTom Herbert struct kvec kv;
309120bf50deSTom Herbert struct msghdr msg;
309220bf50deSTom Herbert
309320bf50deSTom Herbert slen = min_t(int, len, skb_headlen(skb) - offset);
309420bf50deSTom Herbert kv.iov_base = skb->data + offset;
3095db5980d8SJohn Fastabend kv.iov_len = slen;
309620bf50deSTom Herbert memset(&msg, 0, sizeof(msg));
3097bd95e678SJohn Fastabend msg.msg_flags = MSG_DONTWAIT;
309820bf50deSTom Herbert
3099c729ed6fSDavid Howells iov_iter_kvec(&msg.msg_iter, ITER_SOURCE, &kv, 1, slen);
3100c729ed6fSDavid Howells ret = INDIRECT_CALL_2(sendmsg, sendmsg_locked,
3101c729ed6fSDavid Howells sendmsg_unlocked, sk, &msg);
310220bf50deSTom Herbert if (ret <= 0)
310320bf50deSTom Herbert goto error;
310420bf50deSTom Herbert
310520bf50deSTom Herbert offset += ret;
310620bf50deSTom Herbert len -= ret;
310720bf50deSTom Herbert }
310820bf50deSTom Herbert
310920bf50deSTom Herbert /* All the data was skb head? */
311020bf50deSTom Herbert if (!len)
311120bf50deSTom Herbert goto out;
311220bf50deSTom Herbert
311320bf50deSTom Herbert /* Make offset relative to start of frags */
311420bf50deSTom Herbert offset -= skb_headlen(skb);
311520bf50deSTom Herbert
311620bf50deSTom Herbert /* Find where we are in frag list */
311720bf50deSTom Herbert for (fragidx = 0; fragidx < skb_shinfo(skb)->nr_frags; fragidx++) {
311820bf50deSTom Herbert skb_frag_t *frag = &skb_shinfo(skb)->frags[fragidx];
311920bf50deSTom Herbert
3120d8e18a51SMatthew Wilcox (Oracle) if (offset < skb_frag_size(frag))
312120bf50deSTom Herbert break;
312220bf50deSTom Herbert
3123d8e18a51SMatthew Wilcox (Oracle) offset -= skb_frag_size(frag);
312420bf50deSTom Herbert }
312520bf50deSTom Herbert
312620bf50deSTom Herbert for (; len && fragidx < skb_shinfo(skb)->nr_frags; fragidx++) {
312720bf50deSTom Herbert skb_frag_t *frag = &skb_shinfo(skb)->frags[fragidx];
312820bf50deSTom Herbert
3129d8e18a51SMatthew Wilcox (Oracle) slen = min_t(size_t, len, skb_frag_size(frag) - offset);
313020bf50deSTom Herbert
313120bf50deSTom Herbert while (slen) {
3132c729ed6fSDavid Howells struct bio_vec bvec;
3133c729ed6fSDavid Howells struct msghdr msg = {
3134c729ed6fSDavid Howells .msg_flags = MSG_SPLICE_PAGES | MSG_DONTWAIT,
3135c729ed6fSDavid Howells };
3136c729ed6fSDavid Howells
3137c729ed6fSDavid Howells bvec_set_page(&bvec, skb_frag_page(frag), slen,
3138c729ed6fSDavid Howells skb_frag_off(frag) + offset);
3139c729ed6fSDavid Howells iov_iter_bvec(&msg.msg_iter, ITER_SOURCE, &bvec, 1,
3140c729ed6fSDavid Howells slen);
3141c729ed6fSDavid Howells
3142c729ed6fSDavid Howells ret = INDIRECT_CALL_2(sendmsg, sendmsg_locked,
3143c729ed6fSDavid Howells sendmsg_unlocked, sk, &msg);
314420bf50deSTom Herbert if (ret <= 0)
314520bf50deSTom Herbert goto error;
314620bf50deSTom Herbert
314720bf50deSTom Herbert len -= ret;
314820bf50deSTom Herbert offset += ret;
314920bf50deSTom Herbert slen -= ret;
315020bf50deSTom Herbert }
315120bf50deSTom Herbert
315220bf50deSTom Herbert offset = 0;
315320bf50deSTom Herbert }
315420bf50deSTom Herbert
315520bf50deSTom Herbert if (len) {
315620bf50deSTom Herbert /* Process any frag lists */
315720bf50deSTom Herbert
315820bf50deSTom Herbert if (skb == head) {
315920bf50deSTom Herbert if (skb_has_frag_list(skb)) {
316020bf50deSTom Herbert skb = skb_shinfo(skb)->frag_list;
316120bf50deSTom Herbert goto do_frag_list;
316220bf50deSTom Herbert }
316320bf50deSTom Herbert } else if (skb->next) {
316420bf50deSTom Herbert skb = skb->next;
316520bf50deSTom Herbert goto do_frag_list;
316620bf50deSTom Herbert }
316720bf50deSTom Herbert }
316820bf50deSTom Herbert
316920bf50deSTom Herbert out:
317020bf50deSTom Herbert return orig_len - len;
317120bf50deSTom Herbert
317220bf50deSTom Herbert error:
317320bf50deSTom Herbert return orig_len == len ? ret : orig_len - len;
317420bf50deSTom Herbert }
31750739cd28SCong Wang
31760739cd28SCong Wang /* Send skb data on a socket. Socket must be locked. */
skb_send_sock_locked(struct sock * sk,struct sk_buff * skb,int offset,int len)31770739cd28SCong Wang int skb_send_sock_locked(struct sock *sk, struct sk_buff *skb, int offset,
31780739cd28SCong Wang int len)
31790739cd28SCong Wang {
3180c729ed6fSDavid Howells return __skb_send_sock(sk, skb, offset, len, sendmsg_locked);
31810739cd28SCong Wang }
318220bf50deSTom Herbert EXPORT_SYMBOL_GPL(skb_send_sock_locked);
318320bf50deSTom Herbert
31840739cd28SCong Wang /* Send skb data on a socket. Socket must be unlocked. */
skb_send_sock(struct sock * sk,struct sk_buff * skb,int offset,int len)31850739cd28SCong Wang int skb_send_sock(struct sock *sk, struct sk_buff *skb, int offset, int len)
31860739cd28SCong Wang {
3187c729ed6fSDavid Howells return __skb_send_sock(sk, skb, offset, len, sendmsg_unlocked);
31880739cd28SCong Wang }
31890739cd28SCong Wang
3190357b40a1SHerbert Xu /**
3191357b40a1SHerbert Xu * skb_store_bits - store bits from kernel buffer to skb
3192357b40a1SHerbert Xu * @skb: destination buffer
3193357b40a1SHerbert Xu * @offset: offset in destination
3194357b40a1SHerbert Xu * @from: source buffer
3195357b40a1SHerbert Xu * @len: number of bytes to copy
3196357b40a1SHerbert Xu *
3197357b40a1SHerbert Xu * Copy the specified number of bytes from the source buffer to the
3198357b40a1SHerbert Xu * destination skb. This function handles all the messy bits of
3199357b40a1SHerbert Xu * traversing fragment lists and such.
3200357b40a1SHerbert Xu */
3201357b40a1SHerbert Xu
skb_store_bits(struct sk_buff * skb,int offset,const void * from,int len)32020c6fcc8aSStephen Hemminger int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
3203357b40a1SHerbert Xu {
32041a028e50SDavid S. Miller int start = skb_headlen(skb);
3205fbb398a8SDavid S. Miller struct sk_buff *frag_iter;
3206fbb398a8SDavid S. Miller int i, copy;
3207357b40a1SHerbert Xu
3208357b40a1SHerbert Xu if (offset > (int)skb->len - len)
3209357b40a1SHerbert Xu goto fault;
3210357b40a1SHerbert Xu
32111a028e50SDavid S. Miller if ((copy = start - offset) > 0) {
3212357b40a1SHerbert Xu if (copy > len)
3213357b40a1SHerbert Xu copy = len;
321427d7ff46SArnaldo Carvalho de Melo skb_copy_to_linear_data_offset(skb, offset, from, copy);
3215357b40a1SHerbert Xu if ((len -= copy) == 0)
3216357b40a1SHerbert Xu return 0;
3217357b40a1SHerbert Xu offset += copy;
3218357b40a1SHerbert Xu from += copy;
3219357b40a1SHerbert Xu }
3220357b40a1SHerbert Xu
3221357b40a1SHerbert Xu for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
3222357b40a1SHerbert Xu skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
32231a028e50SDavid S. Miller int end;
3224357b40a1SHerbert Xu
3225547b792cSIlpo Järvinen WARN_ON(start > offset + len);
32261a028e50SDavid S. Miller
32279e903e08SEric Dumazet end = start + skb_frag_size(frag);
3228357b40a1SHerbert Xu if ((copy = end - offset) > 0) {
3229c613c209SWillem de Bruijn u32 p_off, p_len, copied;
3230c613c209SWillem de Bruijn struct page *p;
3231357b40a1SHerbert Xu u8 *vaddr;
3232357b40a1SHerbert Xu
3233357b40a1SHerbert Xu if (copy > len)
3234357b40a1SHerbert Xu copy = len;
3235357b40a1SHerbert Xu
3236c613c209SWillem de Bruijn skb_frag_foreach_page(frag,
3237b54c9d5bSJonathan Lemon skb_frag_off(frag) + offset - start,
3238c613c209SWillem de Bruijn copy, p, p_off, p_len, copied) {
3239c613c209SWillem de Bruijn vaddr = kmap_atomic(p);
3240c613c209SWillem de Bruijn memcpy(vaddr + p_off, from + copied, p_len);
324151c56b00SEric Dumazet kunmap_atomic(vaddr);
3242c613c209SWillem de Bruijn }
3243357b40a1SHerbert Xu
3244357b40a1SHerbert Xu if ((len -= copy) == 0)
3245357b40a1SHerbert Xu return 0;
3246357b40a1SHerbert Xu offset += copy;
3247357b40a1SHerbert Xu from += copy;
3248357b40a1SHerbert Xu }
32491a028e50SDavid S. Miller start = end;
3250357b40a1SHerbert Xu }
3251357b40a1SHerbert Xu
3252fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) {
32531a028e50SDavid S. Miller int end;
3254357b40a1SHerbert Xu
3255547b792cSIlpo Järvinen WARN_ON(start > offset + len);
32561a028e50SDavid S. Miller
3257fbb398a8SDavid S. Miller end = start + frag_iter->len;
3258357b40a1SHerbert Xu if ((copy = end - offset) > 0) {
3259357b40a1SHerbert Xu if (copy > len)
3260357b40a1SHerbert Xu copy = len;
3261fbb398a8SDavid S. Miller if (skb_store_bits(frag_iter, offset - start,
32621a028e50SDavid S. Miller from, copy))
3263357b40a1SHerbert Xu goto fault;
3264357b40a1SHerbert Xu if ((len -= copy) == 0)
3265357b40a1SHerbert Xu return 0;
3266357b40a1SHerbert Xu offset += copy;
3267357b40a1SHerbert Xu from += copy;
3268357b40a1SHerbert Xu }
32691a028e50SDavid S. Miller start = end;
3270357b40a1SHerbert Xu }
3271357b40a1SHerbert Xu if (!len)
3272357b40a1SHerbert Xu return 0;
3273357b40a1SHerbert Xu
3274357b40a1SHerbert Xu fault:
3275357b40a1SHerbert Xu return -EFAULT;
3276357b40a1SHerbert Xu }
3277357b40a1SHerbert Xu EXPORT_SYMBOL(skb_store_bits);
3278357b40a1SHerbert Xu
32791da177e4SLinus Torvalds /* Checksum skb data. */
__skb_checksum(const struct sk_buff * skb,int offset,int len,__wsum csum,const struct skb_checksum_ops * ops)32802817a336SDaniel Borkmann __wsum __skb_checksum(const struct sk_buff *skb, int offset, int len,
32812817a336SDaniel Borkmann __wsum csum, const struct skb_checksum_ops *ops)
32821da177e4SLinus Torvalds {
32831a028e50SDavid S. Miller int start = skb_headlen(skb);
32841a028e50SDavid S. Miller int i, copy = start - offset;
3285fbb398a8SDavid S. Miller struct sk_buff *frag_iter;
32861da177e4SLinus Torvalds int pos = 0;
32871da177e4SLinus Torvalds
32881da177e4SLinus Torvalds /* Checksum header. */
32891da177e4SLinus Torvalds if (copy > 0) {
32901da177e4SLinus Torvalds if (copy > len)
32911da177e4SLinus Torvalds copy = len;
32922544af03SMatteo Croce csum = INDIRECT_CALL_1(ops->update, csum_partial_ext,
32932544af03SMatteo Croce skb->data + offset, copy, csum);
32941da177e4SLinus Torvalds if ((len -= copy) == 0)
32951da177e4SLinus Torvalds return csum;
32961da177e4SLinus Torvalds offset += copy;
32971da177e4SLinus Torvalds pos = copy;
32981da177e4SLinus Torvalds }
32991da177e4SLinus Torvalds
33001da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
33011a028e50SDavid S. Miller int end;
330251c56b00SEric Dumazet skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
33031da177e4SLinus Torvalds
3304547b792cSIlpo Järvinen WARN_ON(start > offset + len);
33051a028e50SDavid S. Miller
330651c56b00SEric Dumazet end = start + skb_frag_size(frag);
33071da177e4SLinus Torvalds if ((copy = end - offset) > 0) {
3308c613c209SWillem de Bruijn u32 p_off, p_len, copied;
3309c613c209SWillem de Bruijn struct page *p;
331044bb9363SAl Viro __wsum csum2;
33111da177e4SLinus Torvalds u8 *vaddr;
33121da177e4SLinus Torvalds
33131da177e4SLinus Torvalds if (copy > len)
33141da177e4SLinus Torvalds copy = len;
3315c613c209SWillem de Bruijn
3316c613c209SWillem de Bruijn skb_frag_foreach_page(frag,
3317b54c9d5bSJonathan Lemon skb_frag_off(frag) + offset - start,
3318c613c209SWillem de Bruijn copy, p, p_off, p_len, copied) {
3319c613c209SWillem de Bruijn vaddr = kmap_atomic(p);
33202544af03SMatteo Croce csum2 = INDIRECT_CALL_1(ops->update,
33212544af03SMatteo Croce csum_partial_ext,
33222544af03SMatteo Croce vaddr + p_off, p_len, 0);
332351c56b00SEric Dumazet kunmap_atomic(vaddr);
33242544af03SMatteo Croce csum = INDIRECT_CALL_1(ops->combine,
33252544af03SMatteo Croce csum_block_add_ext, csum,
33262544af03SMatteo Croce csum2, pos, p_len);
3327c613c209SWillem de Bruijn pos += p_len;
3328c613c209SWillem de Bruijn }
3329c613c209SWillem de Bruijn
33301da177e4SLinus Torvalds if (!(len -= copy))
33311da177e4SLinus Torvalds return csum;
33321da177e4SLinus Torvalds offset += copy;
33331da177e4SLinus Torvalds }
33341a028e50SDavid S. Miller start = end;
33351da177e4SLinus Torvalds }
33361da177e4SLinus Torvalds
3337fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) {
33381a028e50SDavid S. Miller int end;
33391da177e4SLinus Torvalds
3340547b792cSIlpo Järvinen WARN_ON(start > offset + len);
33411a028e50SDavid S. Miller
3342fbb398a8SDavid S. Miller end = start + frag_iter->len;
33431da177e4SLinus Torvalds if ((copy = end - offset) > 0) {
33445f92a738SAl Viro __wsum csum2;
33451da177e4SLinus Torvalds if (copy > len)
33461da177e4SLinus Torvalds copy = len;
33472817a336SDaniel Borkmann csum2 = __skb_checksum(frag_iter, offset - start,
33482817a336SDaniel Borkmann copy, 0, ops);
33492544af03SMatteo Croce csum = INDIRECT_CALL_1(ops->combine, csum_block_add_ext,
33502544af03SMatteo Croce csum, csum2, pos, copy);
33511da177e4SLinus Torvalds if ((len -= copy) == 0)
33521da177e4SLinus Torvalds return csum;
33531da177e4SLinus Torvalds offset += copy;
33541da177e4SLinus Torvalds pos += copy;
33551da177e4SLinus Torvalds }
33561a028e50SDavid S. Miller start = end;
33571da177e4SLinus Torvalds }
335809a62660SKris Katterjohn BUG_ON(len);
33591da177e4SLinus Torvalds
33601da177e4SLinus Torvalds return csum;
33611da177e4SLinus Torvalds }
33622817a336SDaniel Borkmann EXPORT_SYMBOL(__skb_checksum);
33632817a336SDaniel Borkmann
skb_checksum(const struct sk_buff * skb,int offset,int len,__wsum csum)33642817a336SDaniel Borkmann __wsum skb_checksum(const struct sk_buff *skb, int offset,
33652817a336SDaniel Borkmann int len, __wsum csum)
33662817a336SDaniel Borkmann {
33672817a336SDaniel Borkmann const struct skb_checksum_ops ops = {
3368cea80ea8SDaniel Borkmann .update = csum_partial_ext,
33692817a336SDaniel Borkmann .combine = csum_block_add_ext,
33702817a336SDaniel Borkmann };
33712817a336SDaniel Borkmann
33722817a336SDaniel Borkmann return __skb_checksum(skb, offset, len, csum, &ops);
33732817a336SDaniel Borkmann }
3374b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_checksum);
33751da177e4SLinus Torvalds
33761da177e4SLinus Torvalds /* Both of above in one bottle. */
33771da177e4SLinus Torvalds
skb_copy_and_csum_bits(const struct sk_buff * skb,int offset,u8 * to,int len)337881d77662SAl Viro __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
33798d5930dfSAl Viro u8 *to, int len)
33801da177e4SLinus Torvalds {
33811a028e50SDavid S. Miller int start = skb_headlen(skb);
33821a028e50SDavid S. Miller int i, copy = start - offset;
3383fbb398a8SDavid S. Miller struct sk_buff *frag_iter;
33841da177e4SLinus Torvalds int pos = 0;
33858d5930dfSAl Viro __wsum csum = 0;
33861da177e4SLinus Torvalds
33871da177e4SLinus Torvalds /* Copy header. */
33881da177e4SLinus Torvalds if (copy > 0) {
33891da177e4SLinus Torvalds if (copy > len)
33901da177e4SLinus Torvalds copy = len;
33911da177e4SLinus Torvalds csum = csum_partial_copy_nocheck(skb->data + offset, to,
3392cc44c17bSAl Viro copy);
33931da177e4SLinus Torvalds if ((len -= copy) == 0)
33941da177e4SLinus Torvalds return csum;
33951da177e4SLinus Torvalds offset += copy;
33961da177e4SLinus Torvalds to += copy;
33971da177e4SLinus Torvalds pos = copy;
33981da177e4SLinus Torvalds }
33991da177e4SLinus Torvalds
34001da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
34011a028e50SDavid S. Miller int end;
34021da177e4SLinus Torvalds
3403547b792cSIlpo Järvinen WARN_ON(start > offset + len);
34041a028e50SDavid S. Miller
34059e903e08SEric Dumazet end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
34061da177e4SLinus Torvalds if ((copy = end - offset) > 0) {
3407c613c209SWillem de Bruijn skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
3408c613c209SWillem de Bruijn u32 p_off, p_len, copied;
3409c613c209SWillem de Bruijn struct page *p;
34105084205fSAl Viro __wsum csum2;
34111da177e4SLinus Torvalds u8 *vaddr;
34121da177e4SLinus Torvalds
34131da177e4SLinus Torvalds if (copy > len)
34141da177e4SLinus Torvalds copy = len;
3415c613c209SWillem de Bruijn
3416c613c209SWillem de Bruijn skb_frag_foreach_page(frag,
3417b54c9d5bSJonathan Lemon skb_frag_off(frag) + offset - start,
3418c613c209SWillem de Bruijn copy, p, p_off, p_len, copied) {
3419c613c209SWillem de Bruijn vaddr = kmap_atomic(p);
3420c613c209SWillem de Bruijn csum2 = csum_partial_copy_nocheck(vaddr + p_off,
3421c613c209SWillem de Bruijn to + copied,
3422cc44c17bSAl Viro p_len);
342351c56b00SEric Dumazet kunmap_atomic(vaddr);
34241da177e4SLinus Torvalds csum = csum_block_add(csum, csum2, pos);
3425c613c209SWillem de Bruijn pos += p_len;
3426c613c209SWillem de Bruijn }
3427c613c209SWillem de Bruijn
34281da177e4SLinus Torvalds if (!(len -= copy))
34291da177e4SLinus Torvalds return csum;
34301da177e4SLinus Torvalds offset += copy;
34311da177e4SLinus Torvalds to += copy;
34321da177e4SLinus Torvalds }
34331a028e50SDavid S. Miller start = end;
34341da177e4SLinus Torvalds }
34351da177e4SLinus Torvalds
3436fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) {
343781d77662SAl Viro __wsum csum2;
34381a028e50SDavid S. Miller int end;
34391da177e4SLinus Torvalds
3440547b792cSIlpo Järvinen WARN_ON(start > offset + len);
34411a028e50SDavid S. Miller
3442fbb398a8SDavid S. Miller end = start + frag_iter->len;
34431da177e4SLinus Torvalds if ((copy = end - offset) > 0) {
34441da177e4SLinus Torvalds if (copy > len)
34451da177e4SLinus Torvalds copy = len;
3446fbb398a8SDavid S. Miller csum2 = skb_copy_and_csum_bits(frag_iter,
34471a028e50SDavid S. Miller offset - start,
34488d5930dfSAl Viro to, copy);
34491da177e4SLinus Torvalds csum = csum_block_add(csum, csum2, pos);
34501da177e4SLinus Torvalds if ((len -= copy) == 0)
34511da177e4SLinus Torvalds return csum;
34521da177e4SLinus Torvalds offset += copy;
34531da177e4SLinus Torvalds to += copy;
34541da177e4SLinus Torvalds pos += copy;
34551da177e4SLinus Torvalds }
34561a028e50SDavid S. Miller start = end;
34571da177e4SLinus Torvalds }
345809a62660SKris Katterjohn BUG_ON(len);
34591da177e4SLinus Torvalds return csum;
34601da177e4SLinus Torvalds }
3461b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_and_csum_bits);
34621da177e4SLinus Torvalds
__skb_checksum_complete_head(struct sk_buff * skb,int len)346349f8e832SCong Wang __sum16 __skb_checksum_complete_head(struct sk_buff *skb, int len)
346449f8e832SCong Wang {
346549f8e832SCong Wang __sum16 sum;
346649f8e832SCong Wang
346749f8e832SCong Wang sum = csum_fold(skb_checksum(skb, 0, len, skb->csum));
346814641931SCong Wang /* See comments in __skb_checksum_complete(). */
346949f8e832SCong Wang if (likely(!sum)) {
347049f8e832SCong Wang if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) &&
347149f8e832SCong Wang !skb->csum_complete_sw)
34727fe50ac8SCong Wang netdev_rx_csum_fault(skb->dev, skb);
347349f8e832SCong Wang }
347449f8e832SCong Wang if (!skb_shared(skb))
347549f8e832SCong Wang skb->csum_valid = !sum;
347649f8e832SCong Wang return sum;
347749f8e832SCong Wang }
347849f8e832SCong Wang EXPORT_SYMBOL(__skb_checksum_complete_head);
347949f8e832SCong Wang
348014641931SCong Wang /* This function assumes skb->csum already holds pseudo header's checksum,
348114641931SCong Wang * which has been changed from the hardware checksum, for example, by
348214641931SCong Wang * __skb_checksum_validate_complete(). And, the original skb->csum must
348314641931SCong Wang * have been validated unsuccessfully for CHECKSUM_COMPLETE case.
348414641931SCong Wang *
348514641931SCong Wang * It returns non-zero if the recomputed checksum is still invalid, otherwise
348614641931SCong Wang * zero. The new checksum is stored back into skb->csum unless the skb is
348714641931SCong Wang * shared.
348814641931SCong Wang */
__skb_checksum_complete(struct sk_buff * skb)348949f8e832SCong Wang __sum16 __skb_checksum_complete(struct sk_buff *skb)
349049f8e832SCong Wang {
349149f8e832SCong Wang __wsum csum;
349249f8e832SCong Wang __sum16 sum;
349349f8e832SCong Wang
349449f8e832SCong Wang csum = skb_checksum(skb, 0, skb->len, 0);
349549f8e832SCong Wang
349649f8e832SCong Wang sum = csum_fold(csum_add(skb->csum, csum));
349714641931SCong Wang /* This check is inverted, because we already knew the hardware
349814641931SCong Wang * checksum is invalid before calling this function. So, if the
349914641931SCong Wang * re-computed checksum is valid instead, then we have a mismatch
350014641931SCong Wang * between the original skb->csum and skb_checksum(). This means either
350114641931SCong Wang * the original hardware checksum is incorrect or we screw up skb->csum
350214641931SCong Wang * when moving skb->data around.
350314641931SCong Wang */
350449f8e832SCong Wang if (likely(!sum)) {
350549f8e832SCong Wang if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) &&
350649f8e832SCong Wang !skb->csum_complete_sw)
35077fe50ac8SCong Wang netdev_rx_csum_fault(skb->dev, skb);
350849f8e832SCong Wang }
350949f8e832SCong Wang
351049f8e832SCong Wang if (!skb_shared(skb)) {
351149f8e832SCong Wang /* Save full packet checksum */
351249f8e832SCong Wang skb->csum = csum;
351349f8e832SCong Wang skb->ip_summed = CHECKSUM_COMPLETE;
351449f8e832SCong Wang skb->csum_complete_sw = 1;
351549f8e832SCong Wang skb->csum_valid = !sum;
351649f8e832SCong Wang }
351749f8e832SCong Wang
351849f8e832SCong Wang return sum;
351949f8e832SCong Wang }
352049f8e832SCong Wang EXPORT_SYMBOL(__skb_checksum_complete);
352149f8e832SCong Wang
warn_crc32c_csum_update(const void * buff,int len,__wsum sum)35229617813dSDavide Caratti static __wsum warn_crc32c_csum_update(const void *buff, int len, __wsum sum)
35239617813dSDavide Caratti {
35249617813dSDavide Caratti net_warn_ratelimited(
35259617813dSDavide Caratti "%s: attempt to compute crc32c without libcrc32c.ko\n",
35269617813dSDavide Caratti __func__);
35279617813dSDavide Caratti return 0;
35289617813dSDavide Caratti }
35299617813dSDavide Caratti
warn_crc32c_csum_combine(__wsum csum,__wsum csum2,int offset,int len)35309617813dSDavide Caratti static __wsum warn_crc32c_csum_combine(__wsum csum, __wsum csum2,
35319617813dSDavide Caratti int offset, int len)
35329617813dSDavide Caratti {
35339617813dSDavide Caratti net_warn_ratelimited(
35349617813dSDavide Caratti "%s: attempt to compute crc32c without libcrc32c.ko\n",
35359617813dSDavide Caratti __func__);
35369617813dSDavide Caratti return 0;
35379617813dSDavide Caratti }
35389617813dSDavide Caratti
35399617813dSDavide Caratti static const struct skb_checksum_ops default_crc32c_ops = {
35409617813dSDavide Caratti .update = warn_crc32c_csum_update,
35419617813dSDavide Caratti .combine = warn_crc32c_csum_combine,
35429617813dSDavide Caratti };
35439617813dSDavide Caratti
35449617813dSDavide Caratti const struct skb_checksum_ops *crc32c_csum_stub __read_mostly =
35459617813dSDavide Caratti &default_crc32c_ops;
35469617813dSDavide Caratti EXPORT_SYMBOL(crc32c_csum_stub);
35479617813dSDavide Caratti
3548af2806f8SThomas Graf /**
3549af2806f8SThomas Graf * skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy()
3550af2806f8SThomas Graf * @from: source buffer
3551af2806f8SThomas Graf *
3552af2806f8SThomas Graf * Calculates the amount of linear headroom needed in the 'to' skb passed
3553af2806f8SThomas Graf * into skb_zerocopy().
3554af2806f8SThomas Graf */
3555af2806f8SThomas Graf unsigned int
skb_zerocopy_headlen(const struct sk_buff * from)3556af2806f8SThomas Graf skb_zerocopy_headlen(const struct sk_buff *from)
3557af2806f8SThomas Graf {
3558af2806f8SThomas Graf unsigned int hlen = 0;
3559af2806f8SThomas Graf
3560af2806f8SThomas Graf if (!from->head_frag ||
3561af2806f8SThomas Graf skb_headlen(from) < L1_CACHE_BYTES ||
3562a17ad096SPravin B Shelar skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS) {
3563af2806f8SThomas Graf hlen = skb_headlen(from);
3564a17ad096SPravin B Shelar if (!hlen)
3565a17ad096SPravin B Shelar hlen = from->len;
3566a17ad096SPravin B Shelar }
3567af2806f8SThomas Graf
3568af2806f8SThomas Graf if (skb_has_frag_list(from))
3569af2806f8SThomas Graf hlen = from->len;
3570af2806f8SThomas Graf
3571af2806f8SThomas Graf return hlen;
3572af2806f8SThomas Graf }
3573af2806f8SThomas Graf EXPORT_SYMBOL_GPL(skb_zerocopy_headlen);
3574af2806f8SThomas Graf
3575af2806f8SThomas Graf /**
3576af2806f8SThomas Graf * skb_zerocopy - Zero copy skb to skb
3577af2806f8SThomas Graf * @to: destination buffer
35787fceb4deSMasanari Iida * @from: source buffer
3579af2806f8SThomas Graf * @len: number of bytes to copy from source buffer
3580af2806f8SThomas Graf * @hlen: size of linear headroom in destination buffer
3581af2806f8SThomas Graf *
3582af2806f8SThomas Graf * Copies up to `len` bytes from `from` to `to` by creating references
3583af2806f8SThomas Graf * to the frags in the source buffer.
3584af2806f8SThomas Graf *
3585af2806f8SThomas Graf * The `hlen` as calculated by skb_zerocopy_headlen() specifies the
3586af2806f8SThomas Graf * headroom in the `to` buffer.
358736d5fe6aSZoltan Kiss *
358836d5fe6aSZoltan Kiss * Return value:
358936d5fe6aSZoltan Kiss * 0: everything is OK
359036d5fe6aSZoltan Kiss * -ENOMEM: couldn't orphan frags of @from due to lack of memory
359136d5fe6aSZoltan Kiss * -EFAULT: skb_copy_bits() found some problem with skb geometry
3592af2806f8SThomas Graf */
359336d5fe6aSZoltan Kiss int
skb_zerocopy(struct sk_buff * to,struct sk_buff * from,int len,int hlen)359436d5fe6aSZoltan Kiss skb_zerocopy(struct sk_buff *to, struct sk_buff *from, int len, int hlen)
3595af2806f8SThomas Graf {
3596af2806f8SThomas Graf int i, j = 0;
3597af2806f8SThomas Graf int plen = 0; /* length of skb->head fragment */
359836d5fe6aSZoltan Kiss int ret;
3599af2806f8SThomas Graf struct page *page;
3600af2806f8SThomas Graf unsigned int offset;
3601af2806f8SThomas Graf
3602af2806f8SThomas Graf BUG_ON(!from->head_frag && !hlen);
3603af2806f8SThomas Graf
3604af2806f8SThomas Graf /* dont bother with small payloads */
360536d5fe6aSZoltan Kiss if (len <= skb_tailroom(to))
360636d5fe6aSZoltan Kiss return skb_copy_bits(from, 0, skb_put(to, len), len);
3607af2806f8SThomas Graf
3608af2806f8SThomas Graf if (hlen) {
360936d5fe6aSZoltan Kiss ret = skb_copy_bits(from, 0, skb_put(to, hlen), hlen);
361036d5fe6aSZoltan Kiss if (unlikely(ret))
361136d5fe6aSZoltan Kiss return ret;
3612af2806f8SThomas Graf len -= hlen;
3613af2806f8SThomas Graf } else {
3614af2806f8SThomas Graf plen = min_t(int, skb_headlen(from), len);
3615af2806f8SThomas Graf if (plen) {
3616af2806f8SThomas Graf page = virt_to_head_page(from->head);
3617af2806f8SThomas Graf offset = from->data - (unsigned char *)page_address(page);
3618af2806f8SThomas Graf __skb_fill_page_desc(to, 0, page, offset, plen);
3619af2806f8SThomas Graf get_page(page);
3620af2806f8SThomas Graf j = 1;
3621af2806f8SThomas Graf len -= plen;
3622af2806f8SThomas Graf }
3623af2806f8SThomas Graf }
3624af2806f8SThomas Graf
3625ede57d58SRichard Gobert skb_len_add(to, len + plen);
3626af2806f8SThomas Graf
362736d5fe6aSZoltan Kiss if (unlikely(skb_orphan_frags(from, GFP_ATOMIC))) {
362836d5fe6aSZoltan Kiss skb_tx_error(from);
362936d5fe6aSZoltan Kiss return -ENOMEM;
363036d5fe6aSZoltan Kiss }
36311f8b977aSWillem de Bruijn skb_zerocopy_clone(to, from, GFP_ATOMIC);
363236d5fe6aSZoltan Kiss
3633af2806f8SThomas Graf for (i = 0; i < skb_shinfo(from)->nr_frags; i++) {
3634d8e18a51SMatthew Wilcox (Oracle) int size;
3635d8e18a51SMatthew Wilcox (Oracle)
3636af2806f8SThomas Graf if (!len)
3637af2806f8SThomas Graf break;
3638af2806f8SThomas Graf skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i];
3639d8e18a51SMatthew Wilcox (Oracle) size = min_t(int, skb_frag_size(&skb_shinfo(to)->frags[j]),
3640d8e18a51SMatthew Wilcox (Oracle) len);
3641d8e18a51SMatthew Wilcox (Oracle) skb_frag_size_set(&skb_shinfo(to)->frags[j], size);
3642d8e18a51SMatthew Wilcox (Oracle) len -= size;
3643af2806f8SThomas Graf skb_frag_ref(to, j);
3644af2806f8SThomas Graf j++;
3645af2806f8SThomas Graf }
3646af2806f8SThomas Graf skb_shinfo(to)->nr_frags = j;
364736d5fe6aSZoltan Kiss
364836d5fe6aSZoltan Kiss return 0;
3649af2806f8SThomas Graf }
3650af2806f8SThomas Graf EXPORT_SYMBOL_GPL(skb_zerocopy);
3651af2806f8SThomas Graf
skb_copy_and_csum_dev(const struct sk_buff * skb,u8 * to)36521da177e4SLinus Torvalds void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
36531da177e4SLinus Torvalds {
3654d3bc23e7SAl Viro __wsum csum;
36551da177e4SLinus Torvalds long csstart;
36561da177e4SLinus Torvalds
365784fa7933SPatrick McHardy if (skb->ip_summed == CHECKSUM_PARTIAL)
365855508d60SMichał Mirosław csstart = skb_checksum_start_offset(skb);
36591da177e4SLinus Torvalds else
36601da177e4SLinus Torvalds csstart = skb_headlen(skb);
36611da177e4SLinus Torvalds
366209a62660SKris Katterjohn BUG_ON(csstart > skb_headlen(skb));
36631da177e4SLinus Torvalds
3664d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data(skb, to, csstart);
36651da177e4SLinus Torvalds
36661da177e4SLinus Torvalds csum = 0;
36671da177e4SLinus Torvalds if (csstart != skb->len)
36681da177e4SLinus Torvalds csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
36698d5930dfSAl Viro skb->len - csstart);
36701da177e4SLinus Torvalds
367184fa7933SPatrick McHardy if (skb->ip_summed == CHECKSUM_PARTIAL) {
3672ff1dcadbSAl Viro long csstuff = csstart + skb->csum_offset;
36731da177e4SLinus Torvalds
3674d3bc23e7SAl Viro *((__sum16 *)(to + csstuff)) = csum_fold(csum);
36751da177e4SLinus Torvalds }
36761da177e4SLinus Torvalds }
3677b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_and_csum_dev);
36781da177e4SLinus Torvalds
36791da177e4SLinus Torvalds /**
36801da177e4SLinus Torvalds * skb_dequeue - remove from the head of the queue
36811da177e4SLinus Torvalds * @list: list to dequeue from
36821da177e4SLinus Torvalds *
36831da177e4SLinus Torvalds * Remove the head of the list. The list lock is taken so the function
36841da177e4SLinus Torvalds * may be used safely with other locking list functions. The head item is
36851da177e4SLinus Torvalds * returned or %NULL if the list is empty.
36861da177e4SLinus Torvalds */
36871da177e4SLinus Torvalds
skb_dequeue(struct sk_buff_head * list)36881da177e4SLinus Torvalds struct sk_buff *skb_dequeue(struct sk_buff_head *list)
36891da177e4SLinus Torvalds {
36901da177e4SLinus Torvalds unsigned long flags;
36911da177e4SLinus Torvalds struct sk_buff *result;
36921da177e4SLinus Torvalds
36931da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags);
36941da177e4SLinus Torvalds result = __skb_dequeue(list);
36951da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags);
36961da177e4SLinus Torvalds return result;
36971da177e4SLinus Torvalds }
3698b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_dequeue);
36991da177e4SLinus Torvalds
37001da177e4SLinus Torvalds /**
37011da177e4SLinus Torvalds * skb_dequeue_tail - remove from the tail of the queue
37021da177e4SLinus Torvalds * @list: list to dequeue from
37031da177e4SLinus Torvalds *
37041da177e4SLinus Torvalds * Remove the tail of the list. The list lock is taken so the function
37051da177e4SLinus Torvalds * may be used safely with other locking list functions. The tail item is
37061da177e4SLinus Torvalds * returned or %NULL if the list is empty.
37071da177e4SLinus Torvalds */
skb_dequeue_tail(struct sk_buff_head * list)37081da177e4SLinus Torvalds struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
37091da177e4SLinus Torvalds {
37101da177e4SLinus Torvalds unsigned long flags;
37111da177e4SLinus Torvalds struct sk_buff *result;
37121da177e4SLinus Torvalds
37131da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags);
37141da177e4SLinus Torvalds result = __skb_dequeue_tail(list);
37151da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags);
37161da177e4SLinus Torvalds return result;
37171da177e4SLinus Torvalds }
3718b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_dequeue_tail);
37191da177e4SLinus Torvalds
37201da177e4SLinus Torvalds /**
37214025d3e7SEric Dumazet * skb_queue_purge_reason - empty a list
37221da177e4SLinus Torvalds * @list: list to empty
37234025d3e7SEric Dumazet * @reason: drop reason
37241da177e4SLinus Torvalds *
37251da177e4SLinus Torvalds * Delete all buffers on an &sk_buff list. Each buffer is removed from
37261da177e4SLinus Torvalds * the list and one reference dropped. This function takes the list
37271da177e4SLinus Torvalds * lock and is atomic with respect to other list locking functions.
37281da177e4SLinus Torvalds */
skb_queue_purge_reason(struct sk_buff_head * list,enum skb_drop_reason reason)37294025d3e7SEric Dumazet void skb_queue_purge_reason(struct sk_buff_head *list,
37304025d3e7SEric Dumazet enum skb_drop_reason reason)
37311da177e4SLinus Torvalds {
37321da177e4SLinus Torvalds struct sk_buff *skb;
37334025d3e7SEric Dumazet
37341da177e4SLinus Torvalds while ((skb = skb_dequeue(list)) != NULL)
37354025d3e7SEric Dumazet kfree_skb_reason(skb, reason);
37361da177e4SLinus Torvalds }
37374025d3e7SEric Dumazet EXPORT_SYMBOL(skb_queue_purge_reason);
37381da177e4SLinus Torvalds
37391da177e4SLinus Torvalds /**
37409f5afeaeSYaogong Wang * skb_rbtree_purge - empty a skb rbtree
37419f5afeaeSYaogong Wang * @root: root of the rbtree to empty
3742385114deSPeter Oskolkov * Return value: the sum of truesizes of all purged skbs.
37439f5afeaeSYaogong Wang *
37449f5afeaeSYaogong Wang * Delete all buffers on an &sk_buff rbtree. Each buffer is removed from
37459f5afeaeSYaogong Wang * the list and one reference dropped. This function does not take
37469f5afeaeSYaogong Wang * any lock. Synchronization should be handled by the caller (e.g., TCP
37479f5afeaeSYaogong Wang * out-of-order queue is protected by the socket lock).
37489f5afeaeSYaogong Wang */
skb_rbtree_purge(struct rb_root * root)3749385114deSPeter Oskolkov unsigned int skb_rbtree_purge(struct rb_root *root)
37509f5afeaeSYaogong Wang {
37517c90584cSEric Dumazet struct rb_node *p = rb_first(root);
3752385114deSPeter Oskolkov unsigned int sum = 0;
37539f5afeaeSYaogong Wang
37547c90584cSEric Dumazet while (p) {
37557c90584cSEric Dumazet struct sk_buff *skb = rb_entry(p, struct sk_buff, rbnode);
37567c90584cSEric Dumazet
37577c90584cSEric Dumazet p = rb_next(p);
37587c90584cSEric Dumazet rb_erase(&skb->rbnode, root);
3759385114deSPeter Oskolkov sum += skb->truesize;
37609f5afeaeSYaogong Wang kfree_skb(skb);
37617c90584cSEric Dumazet }
3762385114deSPeter Oskolkov return sum;
37639f5afeaeSYaogong Wang }
37649f5afeaeSYaogong Wang
skb_errqueue_purge(struct sk_buff_head * list)37650f158b32SEric Dumazet void skb_errqueue_purge(struct sk_buff_head *list)
37660f158b32SEric Dumazet {
37670f158b32SEric Dumazet struct sk_buff *skb, *next;
37680f158b32SEric Dumazet struct sk_buff_head kill;
37690f158b32SEric Dumazet unsigned long flags;
37700f158b32SEric Dumazet
37710f158b32SEric Dumazet __skb_queue_head_init(&kill);
37720f158b32SEric Dumazet
37730f158b32SEric Dumazet spin_lock_irqsave(&list->lock, flags);
37740f158b32SEric Dumazet skb_queue_walk_safe(list, skb, next) {
37750f158b32SEric Dumazet if (SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ZEROCOPY ||
37760f158b32SEric Dumazet SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_TIMESTAMPING)
37770f158b32SEric Dumazet continue;
37780f158b32SEric Dumazet __skb_unlink(skb, list);
37790f158b32SEric Dumazet __skb_queue_tail(&kill, skb);
37800f158b32SEric Dumazet }
37810f158b32SEric Dumazet spin_unlock_irqrestore(&list->lock, flags);
37820f158b32SEric Dumazet __skb_queue_purge(&kill);
37830f158b32SEric Dumazet }
37840f158b32SEric Dumazet EXPORT_SYMBOL(skb_errqueue_purge);
37850f158b32SEric Dumazet
37869f5afeaeSYaogong Wang /**
37871da177e4SLinus Torvalds * skb_queue_head - queue a buffer at the list head
37881da177e4SLinus Torvalds * @list: list to use
37891da177e4SLinus Torvalds * @newsk: buffer to queue
37901da177e4SLinus Torvalds *
37911da177e4SLinus Torvalds * Queue a buffer at the start of the list. This function takes the
37921da177e4SLinus Torvalds * list lock and can be used safely with other locking &sk_buff functions
37931da177e4SLinus Torvalds * safely.
37941da177e4SLinus Torvalds *
37951da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time.
37961da177e4SLinus Torvalds */
skb_queue_head(struct sk_buff_head * list,struct sk_buff * newsk)37971da177e4SLinus Torvalds void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
37981da177e4SLinus Torvalds {
37991da177e4SLinus Torvalds unsigned long flags;
38001da177e4SLinus Torvalds
38011da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags);
38021da177e4SLinus Torvalds __skb_queue_head(list, newsk);
38031da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags);
38041da177e4SLinus Torvalds }
3805b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_head);
38061da177e4SLinus Torvalds
38071da177e4SLinus Torvalds /**
38081da177e4SLinus Torvalds * skb_queue_tail - queue a buffer at the list tail
38091da177e4SLinus Torvalds * @list: list to use
38101da177e4SLinus Torvalds * @newsk: buffer to queue
38111da177e4SLinus Torvalds *
38121da177e4SLinus Torvalds * Queue a buffer at the tail of the list. This function takes the
38131da177e4SLinus Torvalds * list lock and can be used safely with other locking &sk_buff functions
38141da177e4SLinus Torvalds * safely.
38151da177e4SLinus Torvalds *
38161da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time.
38171da177e4SLinus Torvalds */
skb_queue_tail(struct sk_buff_head * list,struct sk_buff * newsk)38181da177e4SLinus Torvalds void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
38191da177e4SLinus Torvalds {
38201da177e4SLinus Torvalds unsigned long flags;
38211da177e4SLinus Torvalds
38221da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags);
38231da177e4SLinus Torvalds __skb_queue_tail(list, newsk);
38241da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags);
38251da177e4SLinus Torvalds }
3826b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_tail);
38278728b834SDavid S. Miller
38281da177e4SLinus Torvalds /**
38291da177e4SLinus Torvalds * skb_unlink - remove a buffer from a list
38301da177e4SLinus Torvalds * @skb: buffer to remove
38318728b834SDavid S. Miller * @list: list to use
38321da177e4SLinus Torvalds *
38338728b834SDavid S. Miller * Remove a packet from a list. The list locks are taken and this
38348728b834SDavid S. Miller * function is atomic with respect to other list locked calls
38351da177e4SLinus Torvalds *
38368728b834SDavid S. Miller * You must know what list the SKB is on.
38371da177e4SLinus Torvalds */
skb_unlink(struct sk_buff * skb,struct sk_buff_head * list)38388728b834SDavid S. Miller void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
38391da177e4SLinus Torvalds {
38401da177e4SLinus Torvalds unsigned long flags;
38411da177e4SLinus Torvalds
38421da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags);
38438728b834SDavid S. Miller __skb_unlink(skb, list);
38441da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags);
38451da177e4SLinus Torvalds }
3846b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_unlink);
38471da177e4SLinus Torvalds
38481da177e4SLinus Torvalds /**
38491da177e4SLinus Torvalds * skb_append - append a buffer
38501da177e4SLinus Torvalds * @old: buffer to insert after
38511da177e4SLinus Torvalds * @newsk: buffer to insert
38528728b834SDavid S. Miller * @list: list to use
38531da177e4SLinus Torvalds *
38541da177e4SLinus Torvalds * Place a packet after a given packet in a list. The list locks are taken
38551da177e4SLinus Torvalds * and this function is atomic with respect to other list locked calls.
38561da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time.
38571da177e4SLinus Torvalds */
skb_append(struct sk_buff * old,struct sk_buff * newsk,struct sk_buff_head * list)38588728b834SDavid S. Miller void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
38591da177e4SLinus Torvalds {
38601da177e4SLinus Torvalds unsigned long flags;
38611da177e4SLinus Torvalds
38628728b834SDavid S. Miller spin_lock_irqsave(&list->lock, flags);
38637de6c033SGerrit Renker __skb_queue_after(list, old, newsk);
38648728b834SDavid S. Miller spin_unlock_irqrestore(&list->lock, flags);
38651da177e4SLinus Torvalds }
3866b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_append);
38671da177e4SLinus Torvalds
skb_split_inside_header(struct sk_buff * skb,struct sk_buff * skb1,const u32 len,const int pos)38681da177e4SLinus Torvalds static inline void skb_split_inside_header(struct sk_buff *skb,
38691da177e4SLinus Torvalds struct sk_buff* skb1,
38701da177e4SLinus Torvalds const u32 len, const int pos)
38711da177e4SLinus Torvalds {
38721da177e4SLinus Torvalds int i;
38731da177e4SLinus Torvalds
3874d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
3875d626f62bSArnaldo Carvalho de Melo pos - len);
38761da177e4SLinus Torvalds /* And move data appendix as is. */
38771da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
38781da177e4SLinus Torvalds skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
38791da177e4SLinus Torvalds
38801da177e4SLinus Torvalds skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
38811da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = 0;
38821da177e4SLinus Torvalds skb1->data_len = skb->data_len;
38831da177e4SLinus Torvalds skb1->len += skb1->data_len;
38841da177e4SLinus Torvalds skb->data_len = 0;
38851da177e4SLinus Torvalds skb->len = len;
388627a884dcSArnaldo Carvalho de Melo skb_set_tail_pointer(skb, len);
38871da177e4SLinus Torvalds }
38881da177e4SLinus Torvalds
skb_split_no_header(struct sk_buff * skb,struct sk_buff * skb1,const u32 len,int pos)38891da177e4SLinus Torvalds static inline void skb_split_no_header(struct sk_buff *skb,
38901da177e4SLinus Torvalds struct sk_buff* skb1,
38911da177e4SLinus Torvalds const u32 len, int pos)
38921da177e4SLinus Torvalds {
38931da177e4SLinus Torvalds int i, k = 0;
38941da177e4SLinus Torvalds const int nfrags = skb_shinfo(skb)->nr_frags;
38951da177e4SLinus Torvalds
38961da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = 0;
38971da177e4SLinus Torvalds skb1->len = skb1->data_len = skb->len - len;
38981da177e4SLinus Torvalds skb->len = len;
38991da177e4SLinus Torvalds skb->data_len = len - pos;
39001da177e4SLinus Torvalds
39011da177e4SLinus Torvalds for (i = 0; i < nfrags; i++) {
39029e903e08SEric Dumazet int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
39031da177e4SLinus Torvalds
39041da177e4SLinus Torvalds if (pos + size > len) {
39051da177e4SLinus Torvalds skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
39061da177e4SLinus Torvalds
39071da177e4SLinus Torvalds if (pos < len) {
39081da177e4SLinus Torvalds /* Split frag.
39091da177e4SLinus Torvalds * We have two variants in this case:
39101da177e4SLinus Torvalds * 1. Move all the frag to the second
39111da177e4SLinus Torvalds * part, if it is possible. F.e.
39121da177e4SLinus Torvalds * this approach is mandatory for TUX,
39131da177e4SLinus Torvalds * where splitting is expensive.
39141da177e4SLinus Torvalds * 2. Split is accurately. We make this.
39151da177e4SLinus Torvalds */
3916ea2ab693SIan Campbell skb_frag_ref(skb, i);
3917b54c9d5bSJonathan Lemon skb_frag_off_add(&skb_shinfo(skb1)->frags[0], len - pos);
39189e903e08SEric Dumazet skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos);
39199e903e08SEric Dumazet skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos);
39201da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags++;
39211da177e4SLinus Torvalds }
39221da177e4SLinus Torvalds k++;
39231da177e4SLinus Torvalds } else
39241da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags++;
39251da177e4SLinus Torvalds pos += size;
39261da177e4SLinus Torvalds }
39271da177e4SLinus Torvalds skb_shinfo(skb1)->nr_frags = k;
39281da177e4SLinus Torvalds }
39291da177e4SLinus Torvalds
39301da177e4SLinus Torvalds /**
39311da177e4SLinus Torvalds * skb_split - Split fragmented skb to two parts at length len.
39321da177e4SLinus Torvalds * @skb: the buffer to split
39331da177e4SLinus Torvalds * @skb1: the buffer to receive the second part
39341da177e4SLinus Torvalds * @len: new length for skb
39351da177e4SLinus Torvalds */
skb_split(struct sk_buff * skb,struct sk_buff * skb1,const u32 len)39361da177e4SLinus Torvalds void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
39371da177e4SLinus Torvalds {
39381da177e4SLinus Torvalds int pos = skb_headlen(skb);
39399b65b17dSTalal Ahmad const int zc_flags = SKBFL_SHARED_FRAG | SKBFL_PURE_ZEROCOPY;
39401da177e4SLinus Torvalds
3941753f1ca4SPavel Begunkov skb_zcopy_downgrade_managed(skb);
3942753f1ca4SPavel Begunkov
39439b65b17dSTalal Ahmad skb_shinfo(skb1)->flags |= skb_shinfo(skb)->flags & zc_flags;
39441f8b977aSWillem de Bruijn skb_zerocopy_clone(skb1, skb, 0);
39451da177e4SLinus Torvalds if (len < pos) /* Split line is inside header. */
39461da177e4SLinus Torvalds skb_split_inside_header(skb, skb1, len, pos);
39471da177e4SLinus Torvalds else /* Second chunk has no header, nothing to copy. */
39481da177e4SLinus Torvalds skb_split_no_header(skb, skb1, len, pos);
39491da177e4SLinus Torvalds }
3950b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_split);
39511da177e4SLinus Torvalds
39529f782db3SIlpo Järvinen /* Shifting from/to a cloned skb is a no-go.
39539f782db3SIlpo Järvinen *
39549f782db3SIlpo Järvinen * Caller cannot keep skb_shinfo related pointers past calling here!
39559f782db3SIlpo Järvinen */
skb_prepare_for_shift(struct sk_buff * skb)3956832d11c5SIlpo Järvinen static int skb_prepare_for_shift(struct sk_buff *skb)
3957832d11c5SIlpo Järvinen {
3958c4777efaSEric Dumazet return skb_unclone_keeptruesize(skb, GFP_ATOMIC);
3959832d11c5SIlpo Järvinen }
3960832d11c5SIlpo Järvinen
3961832d11c5SIlpo Järvinen /**
3962832d11c5SIlpo Järvinen * skb_shift - Shifts paged data partially from skb to another
3963832d11c5SIlpo Järvinen * @tgt: buffer into which tail data gets added
3964832d11c5SIlpo Järvinen * @skb: buffer from which the paged data comes from
3965832d11c5SIlpo Järvinen * @shiftlen: shift up to this many bytes
3966832d11c5SIlpo Järvinen *
3967832d11c5SIlpo Järvinen * Attempts to shift up to shiftlen worth of bytes, which may be less than
396820e994a0SFeng King * the length of the skb, from skb to tgt. Returns number bytes shifted.
3969832d11c5SIlpo Järvinen * It's up to caller to free skb if everything was shifted.
3970832d11c5SIlpo Järvinen *
3971832d11c5SIlpo Järvinen * If @tgt runs out of frags, the whole operation is aborted.
3972832d11c5SIlpo Järvinen *
3973832d11c5SIlpo Järvinen * Skb cannot include anything else but paged data while tgt is allowed
3974832d11c5SIlpo Järvinen * to have non-paged data as well.
3975832d11c5SIlpo Järvinen *
3976832d11c5SIlpo Järvinen * TODO: full sized shift could be optimized but that would need
3977832d11c5SIlpo Järvinen * specialized skb free'er to handle frags without up-to-date nr_frags.
3978832d11c5SIlpo Järvinen */
skb_shift(struct sk_buff * tgt,struct sk_buff * skb,int shiftlen)3979832d11c5SIlpo Järvinen int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
3980832d11c5SIlpo Järvinen {
3981832d11c5SIlpo Järvinen int from, to, merge, todo;
3982d8e18a51SMatthew Wilcox (Oracle) skb_frag_t *fragfrom, *fragto;
3983832d11c5SIlpo Järvinen
3984832d11c5SIlpo Järvinen BUG_ON(shiftlen > skb->len);
3985f8071cdeSEric Dumazet
3986f8071cdeSEric Dumazet if (skb_headlen(skb))
3987f8071cdeSEric Dumazet return 0;
39881f8b977aSWillem de Bruijn if (skb_zcopy(tgt) || skb_zcopy(skb))
39891f8b977aSWillem de Bruijn return 0;
3990832d11c5SIlpo Järvinen
3991832d11c5SIlpo Järvinen todo = shiftlen;
3992832d11c5SIlpo Järvinen from = 0;
3993832d11c5SIlpo Järvinen to = skb_shinfo(tgt)->nr_frags;
3994832d11c5SIlpo Järvinen fragfrom = &skb_shinfo(skb)->frags[from];
3995832d11c5SIlpo Järvinen
3996832d11c5SIlpo Järvinen /* Actual merge is delayed until the point when we know we can
3997832d11c5SIlpo Järvinen * commit all, so that we don't have to undo partial changes
3998832d11c5SIlpo Järvinen */
3999832d11c5SIlpo Järvinen if (!to ||
4000ea2ab693SIan Campbell !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom),
4001b54c9d5bSJonathan Lemon skb_frag_off(fragfrom))) {
4002832d11c5SIlpo Järvinen merge = -1;
4003832d11c5SIlpo Järvinen } else {
4004832d11c5SIlpo Järvinen merge = to - 1;
4005832d11c5SIlpo Järvinen
40069e903e08SEric Dumazet todo -= skb_frag_size(fragfrom);
4007832d11c5SIlpo Järvinen if (todo < 0) {
4008832d11c5SIlpo Järvinen if (skb_prepare_for_shift(skb) ||
4009832d11c5SIlpo Järvinen skb_prepare_for_shift(tgt))
4010832d11c5SIlpo Järvinen return 0;
4011832d11c5SIlpo Järvinen
40129f782db3SIlpo Järvinen /* All previous frag pointers might be stale! */
40139f782db3SIlpo Järvinen fragfrom = &skb_shinfo(skb)->frags[from];
4014832d11c5SIlpo Järvinen fragto = &skb_shinfo(tgt)->frags[merge];
4015832d11c5SIlpo Järvinen
40169e903e08SEric Dumazet skb_frag_size_add(fragto, shiftlen);
40179e903e08SEric Dumazet skb_frag_size_sub(fragfrom, shiftlen);
4018b54c9d5bSJonathan Lemon skb_frag_off_add(fragfrom, shiftlen);
4019832d11c5SIlpo Järvinen
4020832d11c5SIlpo Järvinen goto onlymerged;
4021832d11c5SIlpo Järvinen }
4022832d11c5SIlpo Järvinen
4023832d11c5SIlpo Järvinen from++;
4024832d11c5SIlpo Järvinen }
4025832d11c5SIlpo Järvinen
4026832d11c5SIlpo Järvinen /* Skip full, not-fitting skb to avoid expensive operations */
4027832d11c5SIlpo Järvinen if ((shiftlen == skb->len) &&
4028832d11c5SIlpo Järvinen (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to))
4029832d11c5SIlpo Järvinen return 0;
4030832d11c5SIlpo Järvinen
4031832d11c5SIlpo Järvinen if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt))
4032832d11c5SIlpo Järvinen return 0;
4033832d11c5SIlpo Järvinen
4034832d11c5SIlpo Järvinen while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) {
4035832d11c5SIlpo Järvinen if (to == MAX_SKB_FRAGS)
4036832d11c5SIlpo Järvinen return 0;
4037832d11c5SIlpo Järvinen
4038832d11c5SIlpo Järvinen fragfrom = &skb_shinfo(skb)->frags[from];
4039832d11c5SIlpo Järvinen fragto = &skb_shinfo(tgt)->frags[to];
4040832d11c5SIlpo Järvinen
40419e903e08SEric Dumazet if (todo >= skb_frag_size(fragfrom)) {
4042832d11c5SIlpo Järvinen *fragto = *fragfrom;
40439e903e08SEric Dumazet todo -= skb_frag_size(fragfrom);
4044832d11c5SIlpo Järvinen from++;
4045832d11c5SIlpo Järvinen to++;
4046832d11c5SIlpo Järvinen
4047832d11c5SIlpo Järvinen } else {
4048ea2ab693SIan Campbell __skb_frag_ref(fragfrom);
4049b54c9d5bSJonathan Lemon skb_frag_page_copy(fragto, fragfrom);
4050b54c9d5bSJonathan Lemon skb_frag_off_copy(fragto, fragfrom);
40519e903e08SEric Dumazet skb_frag_size_set(fragto, todo);
4052832d11c5SIlpo Järvinen
4053b54c9d5bSJonathan Lemon skb_frag_off_add(fragfrom, todo);
40549e903e08SEric Dumazet skb_frag_size_sub(fragfrom, todo);
4055832d11c5SIlpo Järvinen todo = 0;
4056832d11c5SIlpo Järvinen
4057832d11c5SIlpo Järvinen to++;
4058832d11c5SIlpo Järvinen break;
4059832d11c5SIlpo Järvinen }
4060832d11c5SIlpo Järvinen }
4061832d11c5SIlpo Järvinen
4062832d11c5SIlpo Järvinen /* Ready to "commit" this state change to tgt */
4063832d11c5SIlpo Järvinen skb_shinfo(tgt)->nr_frags = to;
4064832d11c5SIlpo Järvinen
4065832d11c5SIlpo Järvinen if (merge >= 0) {
4066832d11c5SIlpo Järvinen fragfrom = &skb_shinfo(skb)->frags[0];
4067832d11c5SIlpo Järvinen fragto = &skb_shinfo(tgt)->frags[merge];
4068832d11c5SIlpo Järvinen
40699e903e08SEric Dumazet skb_frag_size_add(fragto, skb_frag_size(fragfrom));
40706a5bcd84SIlias Apalodimas __skb_frag_unref(fragfrom, skb->pp_recycle);
4071832d11c5SIlpo Järvinen }
4072832d11c5SIlpo Järvinen
4073832d11c5SIlpo Järvinen /* Reposition in the original skb */
4074832d11c5SIlpo Järvinen to = 0;
4075832d11c5SIlpo Järvinen while (from < skb_shinfo(skb)->nr_frags)
4076832d11c5SIlpo Järvinen skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++];
4077832d11c5SIlpo Järvinen skb_shinfo(skb)->nr_frags = to;
4078832d11c5SIlpo Järvinen
4079832d11c5SIlpo Järvinen BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags);
4080832d11c5SIlpo Järvinen
4081832d11c5SIlpo Järvinen onlymerged:
4082832d11c5SIlpo Järvinen /* Most likely the tgt won't ever need its checksum anymore, skb on
4083832d11c5SIlpo Järvinen * the other hand might need it if it needs to be resent
4084832d11c5SIlpo Järvinen */
4085832d11c5SIlpo Järvinen tgt->ip_summed = CHECKSUM_PARTIAL;
4086832d11c5SIlpo Järvinen skb->ip_summed = CHECKSUM_PARTIAL;
4087832d11c5SIlpo Järvinen
4088ede57d58SRichard Gobert skb_len_add(skb, -shiftlen);
4089ede57d58SRichard Gobert skb_len_add(tgt, shiftlen);
4090832d11c5SIlpo Järvinen
4091832d11c5SIlpo Järvinen return shiftlen;
4092832d11c5SIlpo Järvinen }
4093832d11c5SIlpo Järvinen
4094677e90edSThomas Graf /**
4095677e90edSThomas Graf * skb_prepare_seq_read - Prepare a sequential read of skb data
4096677e90edSThomas Graf * @skb: the buffer to read
4097677e90edSThomas Graf * @from: lower offset of data to be read
4098677e90edSThomas Graf * @to: upper offset of data to be read
4099677e90edSThomas Graf * @st: state variable
4100677e90edSThomas Graf *
4101677e90edSThomas Graf * Initializes the specified state variable. Must be called before
4102677e90edSThomas Graf * invoking skb_seq_read() for the first time.
4103677e90edSThomas Graf */
skb_prepare_seq_read(struct sk_buff * skb,unsigned int from,unsigned int to,struct skb_seq_state * st)4104677e90edSThomas Graf void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
4105677e90edSThomas Graf unsigned int to, struct skb_seq_state *st)
4106677e90edSThomas Graf {
4107677e90edSThomas Graf st->lower_offset = from;
4108677e90edSThomas Graf st->upper_offset = to;
4109677e90edSThomas Graf st->root_skb = st->cur_skb = skb;
4110677e90edSThomas Graf st->frag_idx = st->stepped_offset = 0;
4111677e90edSThomas Graf st->frag_data = NULL;
411297550f6fSWillem de Bruijn st->frag_off = 0;
4113677e90edSThomas Graf }
4114b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_prepare_seq_read);
4115677e90edSThomas Graf
4116677e90edSThomas Graf /**
4117677e90edSThomas Graf * skb_seq_read - Sequentially read skb data
4118677e90edSThomas Graf * @consumed: number of bytes consumed by the caller so far
4119677e90edSThomas Graf * @data: destination pointer for data to be returned
4120677e90edSThomas Graf * @st: state variable
4121677e90edSThomas Graf *
4122bc32383cSMathias Krause * Reads a block of skb data at @consumed relative to the
4123677e90edSThomas Graf * lower offset specified to skb_prepare_seq_read(). Assigns
4124bc32383cSMathias Krause * the head of the data block to @data and returns the length
4125677e90edSThomas Graf * of the block or 0 if the end of the skb data or the upper
4126677e90edSThomas Graf * offset has been reached.
4127677e90edSThomas Graf *
4128677e90edSThomas Graf * The caller is not required to consume all of the data
4129bc32383cSMathias Krause * returned, i.e. @consumed is typically set to the number
4130677e90edSThomas Graf * of bytes already consumed and the next call to
4131677e90edSThomas Graf * skb_seq_read() will return the remaining part of the block.
4132677e90edSThomas Graf *
413325985edcSLucas De Marchi * Note 1: The size of each block of data returned can be arbitrary,
4134e793c0f7SMasanari Iida * this limitation is the cost for zerocopy sequential
4135677e90edSThomas Graf * reads of potentially non linear data.
4136677e90edSThomas Graf *
4137bc2cda1eSRandy Dunlap * Note 2: Fragment lists within fragments are not implemented
4138677e90edSThomas Graf * at the moment, state->root_skb could be replaced with
4139677e90edSThomas Graf * a stack for this purpose.
4140677e90edSThomas Graf */
skb_seq_read(unsigned int consumed,const u8 ** data,struct skb_seq_state * st)4141677e90edSThomas Graf unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
4142677e90edSThomas Graf struct skb_seq_state *st)
4143677e90edSThomas Graf {
4144677e90edSThomas Graf unsigned int block_limit, abs_offset = consumed + st->lower_offset;
4145677e90edSThomas Graf skb_frag_t *frag;
4146677e90edSThomas Graf
4147aeb193eaSWedson Almeida Filho if (unlikely(abs_offset >= st->upper_offset)) {
4148aeb193eaSWedson Almeida Filho if (st->frag_data) {
4149aeb193eaSWedson Almeida Filho kunmap_atomic(st->frag_data);
4150aeb193eaSWedson Almeida Filho st->frag_data = NULL;
4151aeb193eaSWedson Almeida Filho }
4152677e90edSThomas Graf return 0;
4153aeb193eaSWedson Almeida Filho }
4154677e90edSThomas Graf
4155677e90edSThomas Graf next_skb:
415695e3b24cSHerbert Xu block_limit = skb_headlen(st->cur_skb) + st->stepped_offset;
4157677e90edSThomas Graf
4158995b3379SThomas Chenault if (abs_offset < block_limit && !st->frag_data) {
415995e3b24cSHerbert Xu *data = st->cur_skb->data + (abs_offset - st->stepped_offset);
4160677e90edSThomas Graf return block_limit - abs_offset;
4161677e90edSThomas Graf }
4162677e90edSThomas Graf
4163677e90edSThomas Graf if (st->frag_idx == 0 && !st->frag_data)
4164677e90edSThomas Graf st->stepped_offset += skb_headlen(st->cur_skb);
4165677e90edSThomas Graf
4166677e90edSThomas Graf while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
416797550f6fSWillem de Bruijn unsigned int pg_idx, pg_off, pg_sz;
4168677e90edSThomas Graf
416997550f6fSWillem de Bruijn frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
417097550f6fSWillem de Bruijn
417197550f6fSWillem de Bruijn pg_idx = 0;
417297550f6fSWillem de Bruijn pg_off = skb_frag_off(frag);
417397550f6fSWillem de Bruijn pg_sz = skb_frag_size(frag);
417497550f6fSWillem de Bruijn
417597550f6fSWillem de Bruijn if (skb_frag_must_loop(skb_frag_page(frag))) {
417697550f6fSWillem de Bruijn pg_idx = (pg_off + st->frag_off) >> PAGE_SHIFT;
417797550f6fSWillem de Bruijn pg_off = offset_in_page(pg_off + st->frag_off);
417897550f6fSWillem de Bruijn pg_sz = min_t(unsigned int, pg_sz - st->frag_off,
417997550f6fSWillem de Bruijn PAGE_SIZE - pg_off);
418097550f6fSWillem de Bruijn }
418197550f6fSWillem de Bruijn
418297550f6fSWillem de Bruijn block_limit = pg_sz + st->stepped_offset;
4183677e90edSThomas Graf if (abs_offset < block_limit) {
4184677e90edSThomas Graf if (!st->frag_data)
418597550f6fSWillem de Bruijn st->frag_data = kmap_atomic(skb_frag_page(frag) + pg_idx);
4186677e90edSThomas Graf
418797550f6fSWillem de Bruijn *data = (u8 *)st->frag_data + pg_off +
4188677e90edSThomas Graf (abs_offset - st->stepped_offset);
4189677e90edSThomas Graf
4190677e90edSThomas Graf return block_limit - abs_offset;
4191677e90edSThomas Graf }
4192677e90edSThomas Graf
4193677e90edSThomas Graf if (st->frag_data) {
419451c56b00SEric Dumazet kunmap_atomic(st->frag_data);
4195677e90edSThomas Graf st->frag_data = NULL;
4196677e90edSThomas Graf }
4197677e90edSThomas Graf
419897550f6fSWillem de Bruijn st->stepped_offset += pg_sz;
419997550f6fSWillem de Bruijn st->frag_off += pg_sz;
420097550f6fSWillem de Bruijn if (st->frag_off == skb_frag_size(frag)) {
420197550f6fSWillem de Bruijn st->frag_off = 0;
4202677e90edSThomas Graf st->frag_idx++;
420397550f6fSWillem de Bruijn }
4204677e90edSThomas Graf }
4205677e90edSThomas Graf
42065b5a60daSOlaf Kirch if (st->frag_data) {
420751c56b00SEric Dumazet kunmap_atomic(st->frag_data);
42085b5a60daSOlaf Kirch st->frag_data = NULL;
42095b5a60daSOlaf Kirch }
42105b5a60daSOlaf Kirch
421121dc3301SDavid S. Miller if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) {
4212677e90edSThomas Graf st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
421395e3b24cSHerbert Xu st->frag_idx = 0;
4214677e90edSThomas Graf goto next_skb;
421571b3346dSShyam Iyer } else if (st->cur_skb->next) {
421671b3346dSShyam Iyer st->cur_skb = st->cur_skb->next;
421771b3346dSShyam Iyer st->frag_idx = 0;
4218677e90edSThomas Graf goto next_skb;
4219677e90edSThomas Graf }
4220677e90edSThomas Graf
4221677e90edSThomas Graf return 0;
4222677e90edSThomas Graf }
4223b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_seq_read);
4224677e90edSThomas Graf
4225677e90edSThomas Graf /**
4226677e90edSThomas Graf * skb_abort_seq_read - Abort a sequential read of skb data
4227677e90edSThomas Graf * @st: state variable
4228677e90edSThomas Graf *
4229677e90edSThomas Graf * Must be called if skb_seq_read() was not called until it
4230677e90edSThomas Graf * returned 0.
4231677e90edSThomas Graf */
skb_abort_seq_read(struct skb_seq_state * st)4232677e90edSThomas Graf void skb_abort_seq_read(struct skb_seq_state *st)
4233677e90edSThomas Graf {
4234677e90edSThomas Graf if (st->frag_data)
423551c56b00SEric Dumazet kunmap_atomic(st->frag_data);
4236677e90edSThomas Graf }
4237b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_abort_seq_read);
4238677e90edSThomas Graf
42393fc7e8a6SThomas Graf #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
42403fc7e8a6SThomas Graf
skb_ts_get_next_block(unsigned int offset,const u8 ** text,struct ts_config * conf,struct ts_state * state)42413fc7e8a6SThomas Graf static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
42423fc7e8a6SThomas Graf struct ts_config *conf,
42433fc7e8a6SThomas Graf struct ts_state *state)
42443fc7e8a6SThomas Graf {
42453fc7e8a6SThomas Graf return skb_seq_read(offset, text, TS_SKB_CB(state));
42463fc7e8a6SThomas Graf }
42473fc7e8a6SThomas Graf
skb_ts_finish(struct ts_config * conf,struct ts_state * state)42483fc7e8a6SThomas Graf static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
42493fc7e8a6SThomas Graf {
42503fc7e8a6SThomas Graf skb_abort_seq_read(TS_SKB_CB(state));
42513fc7e8a6SThomas Graf }
42523fc7e8a6SThomas Graf
42533fc7e8a6SThomas Graf /**
42543fc7e8a6SThomas Graf * skb_find_text - Find a text pattern in skb data
42553fc7e8a6SThomas Graf * @skb: the buffer to look in
42563fc7e8a6SThomas Graf * @from: search offset
42573fc7e8a6SThomas Graf * @to: search limit
42583fc7e8a6SThomas Graf * @config: textsearch configuration
42593fc7e8a6SThomas Graf *
42603fc7e8a6SThomas Graf * Finds a pattern in the skb data according to the specified
42613fc7e8a6SThomas Graf * textsearch configuration. Use textsearch_next() to retrieve
42623fc7e8a6SThomas Graf * subsequent occurrences of the pattern. Returns the offset
42633fc7e8a6SThomas Graf * to the first occurrence or UINT_MAX if no match was found.
42643fc7e8a6SThomas Graf */
skb_find_text(struct sk_buff * skb,unsigned int from,unsigned int to,struct ts_config * config)42653fc7e8a6SThomas Graf unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
4266059a2440SBojan Prtvar unsigned int to, struct ts_config *config)
42673fc7e8a6SThomas Graf {
42687802db1dSPhil Sutter unsigned int patlen = config->ops->get_pattern_len(config);
4269059a2440SBojan Prtvar struct ts_state state;
4270f72b948dSPhil Oester unsigned int ret;
4271f72b948dSPhil Oester
4272b228c9b0SWillem de Bruijn BUILD_BUG_ON(sizeof(struct skb_seq_state) > sizeof(state.cb));
4273b228c9b0SWillem de Bruijn
42743fc7e8a6SThomas Graf config->get_next_block = skb_ts_get_next_block;
42753fc7e8a6SThomas Graf config->finish = skb_ts_finish;
42763fc7e8a6SThomas Graf
4277059a2440SBojan Prtvar skb_prepare_seq_read(skb, from, to, TS_SKB_CB(&state));
42783fc7e8a6SThomas Graf
4279059a2440SBojan Prtvar ret = textsearch_find(config, &state);
42807802db1dSPhil Sutter return (ret + patlen <= to - from ? ret : UINT_MAX);
42813fc7e8a6SThomas Graf }
4282b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_find_text);
42833fc7e8a6SThomas Graf
skb_append_pagefrags(struct sk_buff * skb,struct page * page,int offset,size_t size,size_t max_frags)4284be12a1feSHannes Frederic Sowa int skb_append_pagefrags(struct sk_buff *skb, struct page *page,
428596449f90SDavid Howells int offset, size_t size, size_t max_frags)
4286be12a1feSHannes Frederic Sowa {
4287be12a1feSHannes Frederic Sowa int i = skb_shinfo(skb)->nr_frags;
4288be12a1feSHannes Frederic Sowa
4289be12a1feSHannes Frederic Sowa if (skb_can_coalesce(skb, i, page, offset)) {
4290be12a1feSHannes Frederic Sowa skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], size);
429196449f90SDavid Howells } else if (i < max_frags) {
4292753f1ca4SPavel Begunkov skb_zcopy_downgrade_managed(skb);
4293be12a1feSHannes Frederic Sowa get_page(page);
4294228ebc41SEric Dumazet skb_fill_page_desc_noacc(skb, i, page, offset, size);
4295be12a1feSHannes Frederic Sowa } else {
4296be12a1feSHannes Frederic Sowa return -EMSGSIZE;
4297be12a1feSHannes Frederic Sowa }
4298be12a1feSHannes Frederic Sowa
4299be12a1feSHannes Frederic Sowa return 0;
4300be12a1feSHannes Frederic Sowa }
4301be12a1feSHannes Frederic Sowa EXPORT_SYMBOL_GPL(skb_append_pagefrags);
4302be12a1feSHannes Frederic Sowa
4303cbb042f9SHerbert Xu /**
4304cbb042f9SHerbert Xu * skb_pull_rcsum - pull skb and update receive checksum
4305cbb042f9SHerbert Xu * @skb: buffer to update
4306cbb042f9SHerbert Xu * @len: length of data pulled
4307cbb042f9SHerbert Xu *
4308cbb042f9SHerbert Xu * This function performs an skb_pull on the packet and updates
4309fee54fa5SUrs Thuermann * the CHECKSUM_COMPLETE checksum. It should be used on
431084fa7933SPatrick McHardy * receive path processing instead of skb_pull unless you know
431184fa7933SPatrick McHardy * that the checksum difference is zero (e.g., a valid IP header)
431284fa7933SPatrick McHardy * or you are setting ip_summed to CHECKSUM_NONE.
4313cbb042f9SHerbert Xu */
skb_pull_rcsum(struct sk_buff * skb,unsigned int len)4314af72868bSJohannes Berg void *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
4315cbb042f9SHerbert Xu {
431631b33dfbSPravin B Shelar unsigned char *data = skb->data;
431731b33dfbSPravin B Shelar
4318cbb042f9SHerbert Xu BUG_ON(len > skb->len);
431931b33dfbSPravin B Shelar __skb_pull(skb, len);
432031b33dfbSPravin B Shelar skb_postpull_rcsum(skb, data, len);
432131b33dfbSPravin B Shelar return skb->data;
4322cbb042f9SHerbert Xu }
4323f94691acSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(skb_pull_rcsum);
4324f94691acSArnaldo Carvalho de Melo
skb_head_frag_to_page_desc(struct sk_buff * frag_skb)432513acc94eSYonghong Song static inline skb_frag_t skb_head_frag_to_page_desc(struct sk_buff *frag_skb)
432613acc94eSYonghong Song {
432713acc94eSYonghong Song skb_frag_t head_frag;
432813acc94eSYonghong Song struct page *page;
432913acc94eSYonghong Song
433013acc94eSYonghong Song page = virt_to_head_page(frag_skb->head);
4331b51f4113SYunsheng Lin skb_frag_fill_page_desc(&head_frag, page, frag_skb->data -
4332b51f4113SYunsheng Lin (unsigned char *)page_address(page),
4333b51f4113SYunsheng Lin skb_headlen(frag_skb));
433413acc94eSYonghong Song return head_frag;
433513acc94eSYonghong Song }
433613acc94eSYonghong Song
skb_segment_list(struct sk_buff * skb,netdev_features_t features,unsigned int offset)43373a1296a3SSteffen Klassert struct sk_buff *skb_segment_list(struct sk_buff *skb,
43383a1296a3SSteffen Klassert netdev_features_t features,
43393a1296a3SSteffen Klassert unsigned int offset)
43403a1296a3SSteffen Klassert {
43413a1296a3SSteffen Klassert struct sk_buff *list_skb = skb_shinfo(skb)->frag_list;
43423a1296a3SSteffen Klassert unsigned int tnl_hlen = skb_tnl_header_len(skb);
43433a1296a3SSteffen Klassert unsigned int delta_truesize = 0;
43443a1296a3SSteffen Klassert unsigned int delta_len = 0;
43453a1296a3SSteffen Klassert struct sk_buff *tail = NULL;
434653475c5dSDongseok Yi struct sk_buff *nskb, *tmp;
4347cf3ab8d4SLina Wang int len_diff, err;
43483a1296a3SSteffen Klassert
43493a1296a3SSteffen Klassert skb_push(skb, -skb_network_offset(skb) + offset);
43503a1296a3SSteffen Klassert
4351c329b261SPaolo Abeni /* Ensure the head is writeable before touching the shared info */
4352c329b261SPaolo Abeni err = skb_unclone(skb, GFP_ATOMIC);
4353c329b261SPaolo Abeni if (err)
4354c329b261SPaolo Abeni goto err_linearize;
4355c329b261SPaolo Abeni
43563a1296a3SSteffen Klassert skb_shinfo(skb)->frag_list = NULL;
43573a1296a3SSteffen Klassert
4358876e8ca8SYan Zhai while (list_skb) {
43593a1296a3SSteffen Klassert nskb = list_skb;
43603a1296a3SSteffen Klassert list_skb = list_skb->next;
43613a1296a3SSteffen Klassert
436253475c5dSDongseok Yi err = 0;
4363224102deSlena wang delta_truesize += nskb->truesize;
436453475c5dSDongseok Yi if (skb_shared(nskb)) {
436553475c5dSDongseok Yi tmp = skb_clone(nskb, GFP_ATOMIC);
436653475c5dSDongseok Yi if (tmp) {
436753475c5dSDongseok Yi consume_skb(nskb);
436853475c5dSDongseok Yi nskb = tmp;
436953475c5dSDongseok Yi err = skb_unclone(nskb, GFP_ATOMIC);
437053475c5dSDongseok Yi } else {
437153475c5dSDongseok Yi err = -ENOMEM;
437253475c5dSDongseok Yi }
437353475c5dSDongseok Yi }
437453475c5dSDongseok Yi
43753a1296a3SSteffen Klassert if (!tail)
43763a1296a3SSteffen Klassert skb->next = nskb;
43773a1296a3SSteffen Klassert else
43783a1296a3SSteffen Klassert tail->next = nskb;
43793a1296a3SSteffen Klassert
438053475c5dSDongseok Yi if (unlikely(err)) {
438153475c5dSDongseok Yi nskb->next = list_skb;
438253475c5dSDongseok Yi goto err_linearize;
438353475c5dSDongseok Yi }
438453475c5dSDongseok Yi
43853a1296a3SSteffen Klassert tail = nskb;
43863a1296a3SSteffen Klassert
43873a1296a3SSteffen Klassert delta_len += nskb->len;
43883a1296a3SSteffen Klassert
43893a1296a3SSteffen Klassert skb_push(nskb, -skb_network_offset(nskb) + offset);
43903a1296a3SSteffen Klassert
4391cf673ed0SFlorian Westphal skb_release_head_state(nskb);
4392cf3ab8d4SLina Wang len_diff = skb_network_header_len(nskb) - skb_network_header_len(skb);
43933a1296a3SSteffen Klassert __copy_skb_header(nskb, skb);
43943a1296a3SSteffen Klassert
43953a1296a3SSteffen Klassert skb_headers_offset_update(nskb, skb_headroom(nskb) - skb_headroom(skb));
4396cf3ab8d4SLina Wang nskb->transport_header += len_diff;
43973a1296a3SSteffen Klassert skb_copy_from_linear_data_offset(skb, -tnl_hlen,
43983a1296a3SSteffen Klassert nskb->data - tnl_hlen,
43993a1296a3SSteffen Klassert offset + tnl_hlen);
44003a1296a3SSteffen Klassert
44013a1296a3SSteffen Klassert if (skb_needs_linearize(nskb, features) &&
44023a1296a3SSteffen Klassert __skb_linearize(nskb))
44033a1296a3SSteffen Klassert goto err_linearize;
4404876e8ca8SYan Zhai }
44053a1296a3SSteffen Klassert
44063a1296a3SSteffen Klassert skb->truesize = skb->truesize - delta_truesize;
44073a1296a3SSteffen Klassert skb->data_len = skb->data_len - delta_len;
44083a1296a3SSteffen Klassert skb->len = skb->len - delta_len;
44093a1296a3SSteffen Klassert
44103a1296a3SSteffen Klassert skb_gso_reset(skb);
44113a1296a3SSteffen Klassert
44123a1296a3SSteffen Klassert skb->prev = tail;
44133a1296a3SSteffen Klassert
44143a1296a3SSteffen Klassert if (skb_needs_linearize(skb, features) &&
44153a1296a3SSteffen Klassert __skb_linearize(skb))
44163a1296a3SSteffen Klassert goto err_linearize;
44173a1296a3SSteffen Klassert
44183a1296a3SSteffen Klassert skb_get(skb);
44193a1296a3SSteffen Klassert
44203a1296a3SSteffen Klassert return skb;
44213a1296a3SSteffen Klassert
44223a1296a3SSteffen Klassert err_linearize:
44233a1296a3SSteffen Klassert kfree_skb_list(skb->next);
44243a1296a3SSteffen Klassert skb->next = NULL;
44253a1296a3SSteffen Klassert return ERR_PTR(-ENOMEM);
44263a1296a3SSteffen Klassert }
44273a1296a3SSteffen Klassert EXPORT_SYMBOL_GPL(skb_segment_list);
44283a1296a3SSteffen Klassert
4429f4c50d99SHerbert Xu /**
4430f4c50d99SHerbert Xu * skb_segment - Perform protocol segmentation on skb.
4431df5771ffSMichael S. Tsirkin * @head_skb: buffer to segment
4432576a30ebSHerbert Xu * @features: features for the output path (see dev->features)
4433f4c50d99SHerbert Xu *
4434f4c50d99SHerbert Xu * This function performs segmentation on the given skb. It returns
44354c821d75SBen Hutchings * a pointer to the first in a list of new skbs for the segments.
44364c821d75SBen Hutchings * In case of error it returns ERR_PTR(err).
4437f4c50d99SHerbert Xu */
skb_segment(struct sk_buff * head_skb,netdev_features_t features)4438df5771ffSMichael S. Tsirkin struct sk_buff *skb_segment(struct sk_buff *head_skb,
4439df5771ffSMichael S. Tsirkin netdev_features_t features)
4440f4c50d99SHerbert Xu {
4441f4c50d99SHerbert Xu struct sk_buff *segs = NULL;
4442f4c50d99SHerbert Xu struct sk_buff *tail = NULL;
44431a4cedafSMichael S. Tsirkin struct sk_buff *list_skb = skb_shinfo(head_skb)->frag_list;
4444df5771ffSMichael S. Tsirkin unsigned int mss = skb_shinfo(head_skb)->gso_size;
4445df5771ffSMichael S. Tsirkin unsigned int doffset = head_skb->data - skb_mac_header(head_skb);
4446f4c50d99SHerbert Xu unsigned int offset = doffset;
4447df5771ffSMichael S. Tsirkin unsigned int tnl_hlen = skb_tnl_header_len(head_skb);
4448802ab55aSAlexander Duyck unsigned int partial_segs = 0;
4449f4c50d99SHerbert Xu unsigned int headroom;
4450802ab55aSAlexander Duyck unsigned int len = head_skb->len;
44512ea35288SMohamed Khalfella struct sk_buff *frag_skb;
44522ea35288SMohamed Khalfella skb_frag_t *frag;
4453ec5f0615SPravin B Shelar __be16 proto;
445436c98382SAlexander Duyck bool csum, sg;
4455f4c50d99SHerbert Xu int err = -ENOMEM;
4456f4c50d99SHerbert Xu int i = 0;
44572ea35288SMohamed Khalfella int nfrags, pos;
4458f4c50d99SHerbert Xu
44599e4b7a99SJiri Benc if ((skb_shinfo(head_skb)->gso_type & SKB_GSO_DODGY) &&
44609e4b7a99SJiri Benc mss != GSO_BY_FRAGS && mss != skb_headlen(head_skb)) {
44619e4b7a99SJiri Benc struct sk_buff *check_skb;
44629e4b7a99SJiri Benc
44639e4b7a99SJiri Benc for (check_skb = list_skb; check_skb; check_skb = check_skb->next) {
44649e4b7a99SJiri Benc if (skb_headlen(check_skb) && !check_skb->head_frag) {
44659e4b7a99SJiri Benc /* gso_size is untrusted, and we have a frag_list with
44669e4b7a99SJiri Benc * a linear non head_frag item.
44673dcbdb13SShmulik Ladkani *
44689e4b7a99SJiri Benc * If head_skb's headlen does not fit requested gso_size,
44699e4b7a99SJiri Benc * it means that the frag_list members do NOT terminate
44709e4b7a99SJiri Benc * on exact gso_size boundaries. Hence we cannot perform
44719e4b7a99SJiri Benc * skb_frag_t page sharing. Therefore we must fallback to
44729e4b7a99SJiri Benc * copying the frag_list skbs; we do so by disabling SG.
44733dcbdb13SShmulik Ladkani */
44743dcbdb13SShmulik Ladkani features &= ~NETIF_F_SG;
44759e4b7a99SJiri Benc break;
44769e4b7a99SJiri Benc }
44779e4b7a99SJiri Benc }
44783dcbdb13SShmulik Ladkani }
44793dcbdb13SShmulik Ladkani
44805882a07cSWei-Chun Chao __skb_push(head_skb, doffset);
44812f631133SMiaohe Lin proto = skb_network_protocol(head_skb, NULL);
4482ec5f0615SPravin B Shelar if (unlikely(!proto))
4483ec5f0615SPravin B Shelar return ERR_PTR(-EINVAL);
4484ec5f0615SPravin B Shelar
448536c98382SAlexander Duyck sg = !!(features & NETIF_F_SG);
4486f245d079SAlexander Duyck csum = !!can_checksum_protocol(features, proto);
44877e2b10c1STom Herbert
448807b26c94SSteffen Klassert if (sg && csum && (mss != GSO_BY_FRAGS)) {
448907b26c94SSteffen Klassert if (!(features & NETIF_F_GSO_PARTIAL)) {
449007b26c94SSteffen Klassert struct sk_buff *iter;
449143170c4eSIlan Tayari unsigned int frag_len;
449207b26c94SSteffen Klassert
449307b26c94SSteffen Klassert if (!list_skb ||
449407b26c94SSteffen Klassert !net_gso_ok(features, skb_shinfo(head_skb)->gso_type))
449507b26c94SSteffen Klassert goto normal;
449607b26c94SSteffen Klassert
449743170c4eSIlan Tayari /* If we get here then all the required
449843170c4eSIlan Tayari * GSO features except frag_list are supported.
449943170c4eSIlan Tayari * Try to split the SKB to multiple GSO SKBs
450043170c4eSIlan Tayari * with no frag_list.
450143170c4eSIlan Tayari * Currently we can do that only when the buffers don't
450243170c4eSIlan Tayari * have a linear part and all the buffers except
450343170c4eSIlan Tayari * the last are of the same length.
450407b26c94SSteffen Klassert */
450543170c4eSIlan Tayari frag_len = list_skb->len;
450607b26c94SSteffen Klassert skb_walk_frags(head_skb, iter) {
450743170c4eSIlan Tayari if (frag_len != iter->len && iter->next)
450843170c4eSIlan Tayari goto normal;
4509eaffadbbSIlan Tayari if (skb_headlen(iter) && !iter->head_frag)
451007b26c94SSteffen Klassert goto normal;
451107b26c94SSteffen Klassert
451207b26c94SSteffen Klassert len -= iter->len;
451307b26c94SSteffen Klassert }
451443170c4eSIlan Tayari
451543170c4eSIlan Tayari if (len != frag_len)
451643170c4eSIlan Tayari goto normal;
451707b26c94SSteffen Klassert }
451807b26c94SSteffen Klassert
4519802ab55aSAlexander Duyck /* GSO partial only requires that we trim off any excess that
4520802ab55aSAlexander Duyck * doesn't fit into an MSS sized block, so take care of that
4521802ab55aSAlexander Duyck * now.
452295b3904aSEric Dumazet * Cap len to not accidentally hit GSO_BY_FRAGS.
4523802ab55aSAlexander Duyck */
452495b3904aSEric Dumazet partial_segs = min(len, GSO_BY_FRAGS - 1U) / mss;
4525d7fb5a80SAlexander Duyck if (partial_segs > 1)
4526802ab55aSAlexander Duyck mss *= partial_segs;
4527d7fb5a80SAlexander Duyck else
4528d7fb5a80SAlexander Duyck partial_segs = 0;
4529802ab55aSAlexander Duyck }
4530802ab55aSAlexander Duyck
453107b26c94SSteffen Klassert normal:
4532df5771ffSMichael S. Tsirkin headroom = skb_headroom(head_skb);
4533df5771ffSMichael S. Tsirkin pos = skb_headlen(head_skb);
4534f4c50d99SHerbert Xu
45352ea35288SMohamed Khalfella if (skb_orphan_frags(head_skb, GFP_ATOMIC))
45362ea35288SMohamed Khalfella return ERR_PTR(-ENOMEM);
45372ea35288SMohamed Khalfella
45382ea35288SMohamed Khalfella nfrags = skb_shinfo(head_skb)->nr_frags;
45392ea35288SMohamed Khalfella frag = skb_shinfo(head_skb)->frags;
45402ea35288SMohamed Khalfella frag_skb = head_skb;
45412ea35288SMohamed Khalfella
4542f4c50d99SHerbert Xu do {
4543f4c50d99SHerbert Xu struct sk_buff *nskb;
45448cb19905SMichael S. Tsirkin skb_frag_t *nskb_frag;
4545c8884eddSHerbert Xu int hsize;
4546f4c50d99SHerbert Xu int size;
4547f4c50d99SHerbert Xu
45483953c46cSMarcelo Ricardo Leitner if (unlikely(mss == GSO_BY_FRAGS)) {
45493953c46cSMarcelo Ricardo Leitner len = list_skb->len;
45503953c46cSMarcelo Ricardo Leitner } else {
4551df5771ffSMichael S. Tsirkin len = head_skb->len - offset;
4552f4c50d99SHerbert Xu if (len > mss)
4553f4c50d99SHerbert Xu len = mss;
45543953c46cSMarcelo Ricardo Leitner }
4555f4c50d99SHerbert Xu
4556df5771ffSMichael S. Tsirkin hsize = skb_headlen(head_skb) - offset;
4557f4c50d99SHerbert Xu
4558dbd50f23SXin Long if (hsize <= 0 && i >= nfrags && skb_headlen(list_skb) &&
45591a4cedafSMichael S. Tsirkin (skb_headlen(list_skb) == len || sg)) {
45601a4cedafSMichael S. Tsirkin BUG_ON(skb_headlen(list_skb) > len);
456189319d38SHerbert Xu
45622ea35288SMohamed Khalfella nskb = skb_clone(list_skb, GFP_ATOMIC);
45632ea35288SMohamed Khalfella if (unlikely(!nskb))
45642ea35288SMohamed Khalfella goto err;
45652ea35288SMohamed Khalfella
45669d8506ccSHerbert Xu i = 0;
45671a4cedafSMichael S. Tsirkin nfrags = skb_shinfo(list_skb)->nr_frags;
45681a4cedafSMichael S. Tsirkin frag = skb_shinfo(list_skb)->frags;
45691fd819ecSMichael S. Tsirkin frag_skb = list_skb;
45701a4cedafSMichael S. Tsirkin pos += skb_headlen(list_skb);
45719d8506ccSHerbert Xu
45729d8506ccSHerbert Xu while (pos < offset + len) {
45739d8506ccSHerbert Xu BUG_ON(i >= nfrags);
45749d8506ccSHerbert Xu
45754e1beba1SMichael S. Tsirkin size = skb_frag_size(frag);
45769d8506ccSHerbert Xu if (pos + size > offset + len)
45779d8506ccSHerbert Xu break;
45789d8506ccSHerbert Xu
45799d8506ccSHerbert Xu i++;
45809d8506ccSHerbert Xu pos += size;
45814e1beba1SMichael S. Tsirkin frag++;
45829d8506ccSHerbert Xu }
45839d8506ccSHerbert Xu
45841a4cedafSMichael S. Tsirkin list_skb = list_skb->next;
458589319d38SHerbert Xu
45869d8506ccSHerbert Xu if (unlikely(pskb_trim(nskb, len))) {
45879d8506ccSHerbert Xu kfree_skb(nskb);
45889d8506ccSHerbert Xu goto err;
45899d8506ccSHerbert Xu }
45909d8506ccSHerbert Xu
4591ec47ea82SAlexander Duyck hsize = skb_end_offset(nskb);
459289319d38SHerbert Xu if (skb_cow_head(nskb, doffset + headroom)) {
459389319d38SHerbert Xu kfree_skb(nskb);
459489319d38SHerbert Xu goto err;
459589319d38SHerbert Xu }
459689319d38SHerbert Xu
4597ec47ea82SAlexander Duyck nskb->truesize += skb_end_offset(nskb) - hsize;
459889319d38SHerbert Xu skb_release_head_state(nskb);
459989319d38SHerbert Xu __skb_push(nskb, doffset);
460089319d38SHerbert Xu } else {
460100b229f7SPaolo Abeni if (hsize < 0)
460200b229f7SPaolo Abeni hsize = 0;
4603dbd50f23SXin Long if (hsize > len || !sg)
4604dbd50f23SXin Long hsize = len;
4605dbd50f23SXin Long
4606c93bdd0eSMel Gorman nskb = __alloc_skb(hsize + doffset + headroom,
4607df5771ffSMichael S. Tsirkin GFP_ATOMIC, skb_alloc_rx_flag(head_skb),
4608c93bdd0eSMel Gorman NUMA_NO_NODE);
460989319d38SHerbert Xu
461089319d38SHerbert Xu if (unlikely(!nskb))
461189319d38SHerbert Xu goto err;
461289319d38SHerbert Xu
461389319d38SHerbert Xu skb_reserve(nskb, headroom);
461489319d38SHerbert Xu __skb_put(nskb, doffset);
461589319d38SHerbert Xu }
461689319d38SHerbert Xu
4617f4c50d99SHerbert Xu if (segs)
4618f4c50d99SHerbert Xu tail->next = nskb;
4619f4c50d99SHerbert Xu else
4620f4c50d99SHerbert Xu segs = nskb;
4621f4c50d99SHerbert Xu tail = nskb;
4622f4c50d99SHerbert Xu
4623df5771ffSMichael S. Tsirkin __copy_skb_header(nskb, head_skb);
4624f4c50d99SHerbert Xu
4625030737bcSEric Dumazet skb_headers_offset_update(nskb, skb_headroom(nskb) - headroom);
4626fcdfe3a7SVlad Yasevich skb_reset_mac_len(nskb);
462768c33163SPravin B Shelar
4628df5771ffSMichael S. Tsirkin skb_copy_from_linear_data_offset(head_skb, -tnl_hlen,
462968c33163SPravin B Shelar nskb->data - tnl_hlen,
463068c33163SPravin B Shelar doffset + tnl_hlen);
463189319d38SHerbert Xu
46329d8506ccSHerbert Xu if (nskb->len == len + doffset)
46331cdbcb79SSimon Horman goto perform_csum_check;
463489319d38SHerbert Xu
46357fbeffedSAlexander Duyck if (!sg) {
46361454c9faSYadu Kishore if (!csum) {
46377fbeffedSAlexander Duyck if (!nskb->remcsum_offload)
46386f85a124SHerbert Xu nskb->ip_summed = CHECKSUM_NONE;
463976443456SAlexander Duyck SKB_GSO_CB(nskb)->csum =
464076443456SAlexander Duyck skb_copy_and_csum_bits(head_skb, offset,
46411454c9faSYadu Kishore skb_put(nskb,
46421454c9faSYadu Kishore len),
46438d5930dfSAl Viro len);
46447e2b10c1STom Herbert SKB_GSO_CB(nskb)->csum_start =
4645de843723STom Herbert skb_headroom(nskb) + doffset;
46461454c9faSYadu Kishore } else {
4647c624c58eSlily if (skb_copy_bits(head_skb, offset, skb_put(nskb, len), len))
4648c624c58eSlily goto err;
46491454c9faSYadu Kishore }
4650f4c50d99SHerbert Xu continue;
4651f4c50d99SHerbert Xu }
4652f4c50d99SHerbert Xu
46538cb19905SMichael S. Tsirkin nskb_frag = skb_shinfo(nskb)->frags;
4654f4c50d99SHerbert Xu
4655df5771ffSMichael S. Tsirkin skb_copy_from_linear_data_offset(head_skb, offset,
4656d626f62bSArnaldo Carvalho de Melo skb_put(nskb, hsize), hsize);
4657f4c50d99SHerbert Xu
465806b4feb3SJonathan Lemon skb_shinfo(nskb)->flags |= skb_shinfo(head_skb)->flags &
465906b4feb3SJonathan Lemon SKBFL_SHARED_FRAG;
4660cef401deSEric Dumazet
46612ea35288SMohamed Khalfella if (skb_zerocopy_clone(nskb, frag_skb, GFP_ATOMIC))
4662bf5c25d6SWillem de Bruijn goto err;
4663bf5c25d6SWillem de Bruijn
46649d8506ccSHerbert Xu while (pos < offset + len) {
46659d8506ccSHerbert Xu if (i >= nfrags) {
46662ea35288SMohamed Khalfella if (skb_orphan_frags(list_skb, GFP_ATOMIC) ||
46672ea35288SMohamed Khalfella skb_zerocopy_clone(nskb, list_skb,
46682ea35288SMohamed Khalfella GFP_ATOMIC))
46692ea35288SMohamed Khalfella goto err;
46702ea35288SMohamed Khalfella
46719d8506ccSHerbert Xu i = 0;
46721a4cedafSMichael S. Tsirkin nfrags = skb_shinfo(list_skb)->nr_frags;
46731a4cedafSMichael S. Tsirkin frag = skb_shinfo(list_skb)->frags;
46741fd819ecSMichael S. Tsirkin frag_skb = list_skb;
467513acc94eSYonghong Song if (!skb_headlen(list_skb)) {
46769d8506ccSHerbert Xu BUG_ON(!nfrags);
467713acc94eSYonghong Song } else {
467813acc94eSYonghong Song BUG_ON(!list_skb->head_frag);
46799d8506ccSHerbert Xu
468013acc94eSYonghong Song /* to make room for head_frag. */
468113acc94eSYonghong Song i--;
468213acc94eSYonghong Song frag--;
468313acc94eSYonghong Song }
4684bf5c25d6SWillem de Bruijn
46851a4cedafSMichael S. Tsirkin list_skb = list_skb->next;
46869d8506ccSHerbert Xu }
46879d8506ccSHerbert Xu
46889d8506ccSHerbert Xu if (unlikely(skb_shinfo(nskb)->nr_frags >=
46899d8506ccSHerbert Xu MAX_SKB_FRAGS)) {
46909d8506ccSHerbert Xu net_warn_ratelimited(
46919d8506ccSHerbert Xu "skb_segment: too many frags: %u %u\n",
46929d8506ccSHerbert Xu pos, mss);
4693ff907a11SEric Dumazet err = -EINVAL;
46949d8506ccSHerbert Xu goto err;
46959d8506ccSHerbert Xu }
46969d8506ccSHerbert Xu
469713acc94eSYonghong Song *nskb_frag = (i < 0) ? skb_head_frag_to_page_desc(frag_skb) : *frag;
46988cb19905SMichael S. Tsirkin __skb_frag_ref(nskb_frag);
46998cb19905SMichael S. Tsirkin size = skb_frag_size(nskb_frag);
4700f4c50d99SHerbert Xu
4701f4c50d99SHerbert Xu if (pos < offset) {
4702b54c9d5bSJonathan Lemon skb_frag_off_add(nskb_frag, offset - pos);
47038cb19905SMichael S. Tsirkin skb_frag_size_sub(nskb_frag, offset - pos);
4704f4c50d99SHerbert Xu }
4705f4c50d99SHerbert Xu
470689319d38SHerbert Xu skb_shinfo(nskb)->nr_frags++;
4707f4c50d99SHerbert Xu
4708f4c50d99SHerbert Xu if (pos + size <= offset + len) {
4709f4c50d99SHerbert Xu i++;
47104e1beba1SMichael S. Tsirkin frag++;
4711f4c50d99SHerbert Xu pos += size;
4712f4c50d99SHerbert Xu } else {
47138cb19905SMichael S. Tsirkin skb_frag_size_sub(nskb_frag, pos + size - (offset + len));
471489319d38SHerbert Xu goto skip_fraglist;
4715f4c50d99SHerbert Xu }
4716f4c50d99SHerbert Xu
47178cb19905SMichael S. Tsirkin nskb_frag++;
4718f4c50d99SHerbert Xu }
4719f4c50d99SHerbert Xu
472089319d38SHerbert Xu skip_fraglist:
4721f4c50d99SHerbert Xu nskb->data_len = len - hsize;
4722f4c50d99SHerbert Xu nskb->len += nskb->data_len;
4723f4c50d99SHerbert Xu nskb->truesize += nskb->data_len;
4724ec5f0615SPravin B Shelar
47251cdbcb79SSimon Horman perform_csum_check:
47267fbeffedSAlexander Duyck if (!csum) {
4727ff907a11SEric Dumazet if (skb_has_shared_frag(nskb) &&
4728ff907a11SEric Dumazet __skb_linearize(nskb))
4729ddff00d4SAlexander Duyck goto err;
4730ff907a11SEric Dumazet
47317fbeffedSAlexander Duyck if (!nskb->remcsum_offload)
4732ec5f0615SPravin B Shelar nskb->ip_summed = CHECKSUM_NONE;
473376443456SAlexander Duyck SKB_GSO_CB(nskb)->csum =
473476443456SAlexander Duyck skb_checksum(nskb, doffset,
473576443456SAlexander Duyck nskb->len - doffset, 0);
47367e2b10c1STom Herbert SKB_GSO_CB(nskb)->csum_start =
47377e2b10c1STom Herbert skb_headroom(nskb) + doffset;
4738ec5f0615SPravin B Shelar }
4739df5771ffSMichael S. Tsirkin } while ((offset += len) < head_skb->len);
4740f4c50d99SHerbert Xu
4741bec3cfdcSEric Dumazet /* Some callers want to get the end of the list.
4742bec3cfdcSEric Dumazet * Put it in segs->prev to avoid walking the list.
4743bec3cfdcSEric Dumazet * (see validate_xmit_skb_list() for example)
4744bec3cfdcSEric Dumazet */
4745bec3cfdcSEric Dumazet segs->prev = tail;
4746432c856fSToshiaki Makita
4747802ab55aSAlexander Duyck if (partial_segs) {
474807b26c94SSteffen Klassert struct sk_buff *iter;
4749802ab55aSAlexander Duyck int type = skb_shinfo(head_skb)->gso_type;
475007b26c94SSteffen Klassert unsigned short gso_size = skb_shinfo(head_skb)->gso_size;
4751802ab55aSAlexander Duyck
4752802ab55aSAlexander Duyck /* Update type to add partial and then remove dodgy if set */
475307b26c94SSteffen Klassert type |= (features & NETIF_F_GSO_PARTIAL) / NETIF_F_GSO_PARTIAL * SKB_GSO_PARTIAL;
4754802ab55aSAlexander Duyck type &= ~SKB_GSO_DODGY;
4755802ab55aSAlexander Duyck
4756802ab55aSAlexander Duyck /* Update GSO info and prepare to start updating headers on
4757802ab55aSAlexander Duyck * our way back down the stack of protocols.
4758802ab55aSAlexander Duyck */
475907b26c94SSteffen Klassert for (iter = segs; iter; iter = iter->next) {
476007b26c94SSteffen Klassert skb_shinfo(iter)->gso_size = gso_size;
476107b26c94SSteffen Klassert skb_shinfo(iter)->gso_segs = partial_segs;
476207b26c94SSteffen Klassert skb_shinfo(iter)->gso_type = type;
476307b26c94SSteffen Klassert SKB_GSO_CB(iter)->data_offset = skb_headroom(iter) + doffset;
476407b26c94SSteffen Klassert }
476507b26c94SSteffen Klassert
476607b26c94SSteffen Klassert if (tail->len - doffset <= gso_size)
476707b26c94SSteffen Klassert skb_shinfo(tail)->gso_size = 0;
476807b26c94SSteffen Klassert else if (tail != segs)
476907b26c94SSteffen Klassert skb_shinfo(tail)->gso_segs = DIV_ROUND_UP(tail->len - doffset, gso_size);
4770802ab55aSAlexander Duyck }
4771802ab55aSAlexander Duyck
4772432c856fSToshiaki Makita /* Following permits correct backpressure, for protocols
4773432c856fSToshiaki Makita * using skb_set_owner_w().
4774432c856fSToshiaki Makita * Idea is to tranfert ownership from head_skb to last segment.
4775432c856fSToshiaki Makita */
4776432c856fSToshiaki Makita if (head_skb->destructor == sock_wfree) {
4777432c856fSToshiaki Makita swap(tail->truesize, head_skb->truesize);
4778432c856fSToshiaki Makita swap(tail->destructor, head_skb->destructor);
4779432c856fSToshiaki Makita swap(tail->sk, head_skb->sk);
4780432c856fSToshiaki Makita }
4781f4c50d99SHerbert Xu return segs;
4782f4c50d99SHerbert Xu
4783f4c50d99SHerbert Xu err:
4784289dccbeSEric Dumazet kfree_skb_list(segs);
4785f4c50d99SHerbert Xu return ERR_PTR(err);
4786f4c50d99SHerbert Xu }
4787f4c50d99SHerbert Xu EXPORT_SYMBOL_GPL(skb_segment);
4788f4c50d99SHerbert Xu
4789df5042f4SFlorian Westphal #ifdef CONFIG_SKB_EXTENSIONS
4790df5042f4SFlorian Westphal #define SKB_EXT_ALIGN_VALUE 8
4791df5042f4SFlorian Westphal #define SKB_EXT_CHUNKSIZEOF(x) (ALIGN((sizeof(x)), SKB_EXT_ALIGN_VALUE) / SKB_EXT_ALIGN_VALUE)
4792df5042f4SFlorian Westphal
4793df5042f4SFlorian Westphal static const u8 skb_ext_type_len[] = {
4794df5042f4SFlorian Westphal #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
4795df5042f4SFlorian Westphal [SKB_EXT_BRIDGE_NF] = SKB_EXT_CHUNKSIZEOF(struct nf_bridge_info),
4796df5042f4SFlorian Westphal #endif
47974165079bSFlorian Westphal #ifdef CONFIG_XFRM
47984165079bSFlorian Westphal [SKB_EXT_SEC_PATH] = SKB_EXT_CHUNKSIZEOF(struct sec_path),
47994165079bSFlorian Westphal #endif
480095a7233cSPaul Blakey #if IS_ENABLED(CONFIG_NET_TC_SKB_EXT)
480195a7233cSPaul Blakey [TC_SKB_EXT] = SKB_EXT_CHUNKSIZEOF(struct tc_skb_ext),
480295a7233cSPaul Blakey #endif
48033ee17bc7SMat Martineau #if IS_ENABLED(CONFIG_MPTCP)
48043ee17bc7SMat Martineau [SKB_EXT_MPTCP] = SKB_EXT_CHUNKSIZEOF(struct mptcp_ext),
48053ee17bc7SMat Martineau #endif
480678476d31SJeremy Kerr #if IS_ENABLED(CONFIG_MCTP_FLOWS)
480778476d31SJeremy Kerr [SKB_EXT_MCTP] = SKB_EXT_CHUNKSIZEOF(struct mctp_flow),
480878476d31SJeremy Kerr #endif
4809df5042f4SFlorian Westphal };
4810df5042f4SFlorian Westphal
skb_ext_total_length(void)4811df5042f4SFlorian Westphal static __always_inline unsigned int skb_ext_total_length(void)
4812df5042f4SFlorian Westphal {
48135d21d0a6SThomas Weißschuh unsigned int l = SKB_EXT_CHUNKSIZEOF(struct skb_ext);
48145d21d0a6SThomas Weißschuh int i;
48155d21d0a6SThomas Weißschuh
48165d21d0a6SThomas Weißschuh for (i = 0; i < ARRAY_SIZE(skb_ext_type_len); i++)
48175d21d0a6SThomas Weißschuh l += skb_ext_type_len[i];
48185d21d0a6SThomas Weißschuh
48195d21d0a6SThomas Weißschuh return l;
4820df5042f4SFlorian Westphal }
4821df5042f4SFlorian Westphal
skb_extensions_init(void)4822df5042f4SFlorian Westphal static void skb_extensions_init(void)
4823df5042f4SFlorian Westphal {
4824df5042f4SFlorian Westphal BUILD_BUG_ON(SKB_EXT_NUM >= 8);
482592b8881bSThomas Weißschuh #if !IS_ENABLED(CONFIG_KCOV_INSTRUMENT_ALL)
4826df5042f4SFlorian Westphal BUILD_BUG_ON(skb_ext_total_length() > 255);
482792b8881bSThomas Weißschuh #endif
4828df5042f4SFlorian Westphal
4829df5042f4SFlorian Westphal skbuff_ext_cache = kmem_cache_create("skbuff_ext_cache",
4830df5042f4SFlorian Westphal SKB_EXT_ALIGN_VALUE * skb_ext_total_length(),
4831df5042f4SFlorian Westphal 0,
4832df5042f4SFlorian Westphal SLAB_HWCACHE_ALIGN|SLAB_PANIC,
4833df5042f4SFlorian Westphal NULL);
4834df5042f4SFlorian Westphal }
4835df5042f4SFlorian Westphal #else
skb_extensions_init(void)4836df5042f4SFlorian Westphal static void skb_extensions_init(void) {}
4837df5042f4SFlorian Westphal #endif
4838df5042f4SFlorian Westphal
48390a064316SJesper Dangaard Brouer /* The SKB kmem_cache slab is critical for network performance. Never
48400a064316SJesper Dangaard Brouer * merge/alias the slab with similar sized objects. This avoids fragmentation
48410a064316SJesper Dangaard Brouer * that hurts performance of kmem_cache_{alloc,free}_bulk APIs.
48420a064316SJesper Dangaard Brouer */
48430a064316SJesper Dangaard Brouer #ifndef CONFIG_SLUB_TINY
48440a064316SJesper Dangaard Brouer #define FLAG_SKB_NO_MERGE SLAB_NO_MERGE
48450a064316SJesper Dangaard Brouer #else /* CONFIG_SLUB_TINY - simple loop in kmem_cache_alloc_bulk */
48460a064316SJesper Dangaard Brouer #define FLAG_SKB_NO_MERGE 0
48470a064316SJesper Dangaard Brouer #endif
48480a064316SJesper Dangaard Brouer
skb_init(void)48491da177e4SLinus Torvalds void __init skb_init(void)
48501da177e4SLinus Torvalds {
4851025a785fSJakub Kicinski skbuff_cache = kmem_cache_create_usercopy("skbuff_head_cache",
48521da177e4SLinus Torvalds sizeof(struct sk_buff),
48531da177e4SLinus Torvalds 0,
48540a064316SJesper Dangaard Brouer SLAB_HWCACHE_ALIGN|SLAB_PANIC|
48550a064316SJesper Dangaard Brouer FLAG_SKB_NO_MERGE,
485679a8a642SKees Cook offsetof(struct sk_buff, cb),
485779a8a642SKees Cook sizeof_field(struct sk_buff, cb),
485820c2df83SPaul Mundt NULL);
4859d179cd12SDavid S. Miller skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
4860d0bf4a9eSEric Dumazet sizeof(struct sk_buff_fclones),
4861d179cd12SDavid S. Miller 0,
4862e5d679f3SAlexey Dobriyan SLAB_HWCACHE_ALIGN|SLAB_PANIC,
486320c2df83SPaul Mundt NULL);
48640b34d680SEric Dumazet /* usercopy should only access first SKB_SMALL_HEAD_HEADROOM bytes.
48650b34d680SEric Dumazet * struct skb_shared_info is located at the end of skb->head,
48660b34d680SEric Dumazet * and should not be copied to/from user.
48670b34d680SEric Dumazet */
48680b34d680SEric Dumazet skb_small_head_cache = kmem_cache_create_usercopy("skbuff_small_head",
4869bf9f1baaSEric Dumazet SKB_SMALL_HEAD_CACHE_SIZE,
4870bf9f1baaSEric Dumazet 0,
4871bf9f1baaSEric Dumazet SLAB_HWCACHE_ALIGN | SLAB_PANIC,
48720b34d680SEric Dumazet 0,
48730b34d680SEric Dumazet SKB_SMALL_HEAD_HEADROOM,
4874bf9f1baaSEric Dumazet NULL);
4875df5042f4SFlorian Westphal skb_extensions_init();
48761da177e4SLinus Torvalds }
48771da177e4SLinus Torvalds
487851c739d1SDavid S. Miller static int
__skb_to_sgvec(struct sk_buff * skb,struct scatterlist * sg,int offset,int len,unsigned int recursion_level)487948a1df65SJason A. Donenfeld __skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len,
488048a1df65SJason A. Donenfeld unsigned int recursion_level)
4881716ea3a7SDavid Howells {
48821a028e50SDavid S. Miller int start = skb_headlen(skb);
48831a028e50SDavid S. Miller int i, copy = start - offset;
4884fbb398a8SDavid S. Miller struct sk_buff *frag_iter;
4885716ea3a7SDavid Howells int elt = 0;
4886716ea3a7SDavid Howells
488748a1df65SJason A. Donenfeld if (unlikely(recursion_level >= 24))
488848a1df65SJason A. Donenfeld return -EMSGSIZE;
488948a1df65SJason A. Donenfeld
4890716ea3a7SDavid Howells if (copy > 0) {
4891716ea3a7SDavid Howells if (copy > len)
4892716ea3a7SDavid Howells copy = len;
4893642f1490SJens Axboe sg_set_buf(sg, skb->data + offset, copy);
4894716ea3a7SDavid Howells elt++;
4895716ea3a7SDavid Howells if ((len -= copy) == 0)
4896716ea3a7SDavid Howells return elt;
4897716ea3a7SDavid Howells offset += copy;
4898716ea3a7SDavid Howells }
4899716ea3a7SDavid Howells
4900716ea3a7SDavid Howells for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
49011a028e50SDavid S. Miller int end;
4902716ea3a7SDavid Howells
4903547b792cSIlpo Järvinen WARN_ON(start > offset + len);
49041a028e50SDavid S. Miller
49059e903e08SEric Dumazet end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
4906716ea3a7SDavid Howells if ((copy = end - offset) > 0) {
4907716ea3a7SDavid Howells skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
490848a1df65SJason A. Donenfeld if (unlikely(elt && sg_is_last(&sg[elt - 1])))
490948a1df65SJason A. Donenfeld return -EMSGSIZE;
4910716ea3a7SDavid Howells
4911716ea3a7SDavid Howells if (copy > len)
4912716ea3a7SDavid Howells copy = len;
4913ea2ab693SIan Campbell sg_set_page(&sg[elt], skb_frag_page(frag), copy,
4914b54c9d5bSJonathan Lemon skb_frag_off(frag) + offset - start);
4915716ea3a7SDavid Howells elt++;
4916716ea3a7SDavid Howells if (!(len -= copy))
4917716ea3a7SDavid Howells return elt;
4918716ea3a7SDavid Howells offset += copy;
4919716ea3a7SDavid Howells }
49201a028e50SDavid S. Miller start = end;
4921716ea3a7SDavid Howells }
4922716ea3a7SDavid Howells
4923fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) {
492448a1df65SJason A. Donenfeld int end, ret;
4925716ea3a7SDavid Howells
4926547b792cSIlpo Järvinen WARN_ON(start > offset + len);
49271a028e50SDavid S. Miller
4928fbb398a8SDavid S. Miller end = start + frag_iter->len;
4929716ea3a7SDavid Howells if ((copy = end - offset) > 0) {
493048a1df65SJason A. Donenfeld if (unlikely(elt && sg_is_last(&sg[elt - 1])))
493148a1df65SJason A. Donenfeld return -EMSGSIZE;
493248a1df65SJason A. Donenfeld
4933716ea3a7SDavid Howells if (copy > len)
4934716ea3a7SDavid Howells copy = len;
493548a1df65SJason A. Donenfeld ret = __skb_to_sgvec(frag_iter, sg+elt, offset - start,
493648a1df65SJason A. Donenfeld copy, recursion_level + 1);
493748a1df65SJason A. Donenfeld if (unlikely(ret < 0))
493848a1df65SJason A. Donenfeld return ret;
493948a1df65SJason A. Donenfeld elt += ret;
4940716ea3a7SDavid Howells if ((len -= copy) == 0)
4941716ea3a7SDavid Howells return elt;
4942716ea3a7SDavid Howells offset += copy;
4943716ea3a7SDavid Howells }
49441a028e50SDavid S. Miller start = end;
4945716ea3a7SDavid Howells }
4946716ea3a7SDavid Howells BUG_ON(len);
4947716ea3a7SDavid Howells return elt;
4948716ea3a7SDavid Howells }
4949716ea3a7SDavid Howells
495048a1df65SJason A. Donenfeld /**
495148a1df65SJason A. Donenfeld * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
495248a1df65SJason A. Donenfeld * @skb: Socket buffer containing the buffers to be mapped
495348a1df65SJason A. Donenfeld * @sg: The scatter-gather list to map into
495448a1df65SJason A. Donenfeld * @offset: The offset into the buffer's contents to start mapping
495548a1df65SJason A. Donenfeld * @len: Length of buffer space to be mapped
495648a1df65SJason A. Donenfeld *
495748a1df65SJason A. Donenfeld * Fill the specified scatter-gather list with mappings/pointers into a
495848a1df65SJason A. Donenfeld * region of the buffer space attached to a socket buffer. Returns either
495948a1df65SJason A. Donenfeld * the number of scatterlist items used, or -EMSGSIZE if the contents
496048a1df65SJason A. Donenfeld * could not fit.
496148a1df65SJason A. Donenfeld */
skb_to_sgvec(struct sk_buff * skb,struct scatterlist * sg,int offset,int len)496248a1df65SJason A. Donenfeld int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
496348a1df65SJason A. Donenfeld {
496448a1df65SJason A. Donenfeld int nsg = __skb_to_sgvec(skb, sg, offset, len, 0);
496548a1df65SJason A. Donenfeld
496648a1df65SJason A. Donenfeld if (nsg <= 0)
496748a1df65SJason A. Donenfeld return nsg;
496848a1df65SJason A. Donenfeld
496948a1df65SJason A. Donenfeld sg_mark_end(&sg[nsg - 1]);
497048a1df65SJason A. Donenfeld
497148a1df65SJason A. Donenfeld return nsg;
497248a1df65SJason A. Donenfeld }
497348a1df65SJason A. Donenfeld EXPORT_SYMBOL_GPL(skb_to_sgvec);
497448a1df65SJason A. Donenfeld
497525a91d8dSFan Du /* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given
497625a91d8dSFan Du * sglist without mark the sg which contain last skb data as the end.
497725a91d8dSFan Du * So the caller can mannipulate sg list as will when padding new data after
497825a91d8dSFan Du * the first call without calling sg_unmark_end to expend sg list.
497925a91d8dSFan Du *
498025a91d8dSFan Du * Scenario to use skb_to_sgvec_nomark:
498125a91d8dSFan Du * 1. sg_init_table
498225a91d8dSFan Du * 2. skb_to_sgvec_nomark(payload1)
498325a91d8dSFan Du * 3. skb_to_sgvec_nomark(payload2)
498425a91d8dSFan Du *
498525a91d8dSFan Du * This is equivalent to:
498625a91d8dSFan Du * 1. sg_init_table
498725a91d8dSFan Du * 2. skb_to_sgvec(payload1)
498825a91d8dSFan Du * 3. sg_unmark_end
498925a91d8dSFan Du * 4. skb_to_sgvec(payload2)
499025a91d8dSFan Du *
499125a91d8dSFan Du * When mapping mutilple payload conditionally, skb_to_sgvec_nomark
499225a91d8dSFan Du * is more preferable.
499325a91d8dSFan Du */
skb_to_sgvec_nomark(struct sk_buff * skb,struct scatterlist * sg,int offset,int len)499425a91d8dSFan Du int skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg,
499525a91d8dSFan Du int offset, int len)
499625a91d8dSFan Du {
499748a1df65SJason A. Donenfeld return __skb_to_sgvec(skb, sg, offset, len, 0);
499825a91d8dSFan Du }
499925a91d8dSFan Du EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark);
500025a91d8dSFan Du
500151c739d1SDavid S. Miller
500251c739d1SDavid S. Miller
5003716ea3a7SDavid Howells /**
5004716ea3a7SDavid Howells * skb_cow_data - Check that a socket buffer's data buffers are writable
5005716ea3a7SDavid Howells * @skb: The socket buffer to check.
5006716ea3a7SDavid Howells * @tailbits: Amount of trailing space to be added
5007716ea3a7SDavid Howells * @trailer: Returned pointer to the skb where the @tailbits space begins
5008716ea3a7SDavid Howells *
5009716ea3a7SDavid Howells * Make sure that the data buffers attached to a socket buffer are
5010716ea3a7SDavid Howells * writable. If they are not, private copies are made of the data buffers
5011716ea3a7SDavid Howells * and the socket buffer is set to use these instead.
5012716ea3a7SDavid Howells *
5013716ea3a7SDavid Howells * If @tailbits is given, make sure that there is space to write @tailbits
5014716ea3a7SDavid Howells * bytes of data beyond current end of socket buffer. @trailer will be
5015716ea3a7SDavid Howells * set to point to the skb in which this space begins.
5016716ea3a7SDavid Howells *
5017716ea3a7SDavid Howells * The number of scatterlist elements required to completely map the
5018716ea3a7SDavid Howells * COW'd and extended socket buffer will be returned.
5019716ea3a7SDavid Howells */
skb_cow_data(struct sk_buff * skb,int tailbits,struct sk_buff ** trailer)5020716ea3a7SDavid Howells int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
5021716ea3a7SDavid Howells {
5022716ea3a7SDavid Howells int copyflag;
5023716ea3a7SDavid Howells int elt;
5024716ea3a7SDavid Howells struct sk_buff *skb1, **skb_p;
5025716ea3a7SDavid Howells
5026716ea3a7SDavid Howells /* If skb is cloned or its head is paged, reallocate
5027716ea3a7SDavid Howells * head pulling out all the pages (pages are considered not writable
5028716ea3a7SDavid Howells * at the moment even if they are anonymous).
5029716ea3a7SDavid Howells */
5030716ea3a7SDavid Howells if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
5031c15fc199SMiaohe Lin !__pskb_pull_tail(skb, __skb_pagelen(skb)))
5032716ea3a7SDavid Howells return -ENOMEM;
5033716ea3a7SDavid Howells
5034716ea3a7SDavid Howells /* Easy case. Most of packets will go this way. */
503521dc3301SDavid S. Miller if (!skb_has_frag_list(skb)) {
5036716ea3a7SDavid Howells /* A little of trouble, not enough of space for trailer.
5037716ea3a7SDavid Howells * This should not happen, when stack is tuned to generate
5038716ea3a7SDavid Howells * good frames. OK, on miss we reallocate and reserve even more
5039716ea3a7SDavid Howells * space, 128 bytes is fair. */
5040716ea3a7SDavid Howells
5041716ea3a7SDavid Howells if (skb_tailroom(skb) < tailbits &&
5042716ea3a7SDavid Howells pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
5043716ea3a7SDavid Howells return -ENOMEM;
5044716ea3a7SDavid Howells
5045716ea3a7SDavid Howells /* Voila! */
5046716ea3a7SDavid Howells *trailer = skb;
5047716ea3a7SDavid Howells return 1;
5048716ea3a7SDavid Howells }
5049716ea3a7SDavid Howells
5050716ea3a7SDavid Howells /* Misery. We are in troubles, going to mincer fragments... */
5051716ea3a7SDavid Howells
5052716ea3a7SDavid Howells elt = 1;
5053716ea3a7SDavid Howells skb_p = &skb_shinfo(skb)->frag_list;
5054716ea3a7SDavid Howells copyflag = 0;
5055716ea3a7SDavid Howells
5056716ea3a7SDavid Howells while ((skb1 = *skb_p) != NULL) {
5057716ea3a7SDavid Howells int ntail = 0;
5058716ea3a7SDavid Howells
5059716ea3a7SDavid Howells /* The fragment is partially pulled by someone,
5060716ea3a7SDavid Howells * this can happen on input. Copy it and everything
5061716ea3a7SDavid Howells * after it. */
5062716ea3a7SDavid Howells
5063716ea3a7SDavid Howells if (skb_shared(skb1))
5064716ea3a7SDavid Howells copyflag = 1;
5065716ea3a7SDavid Howells
5066716ea3a7SDavid Howells /* If the skb is the last, worry about trailer. */
5067716ea3a7SDavid Howells
5068716ea3a7SDavid Howells if (skb1->next == NULL && tailbits) {
5069716ea3a7SDavid Howells if (skb_shinfo(skb1)->nr_frags ||
507021dc3301SDavid S. Miller skb_has_frag_list(skb1) ||
5071716ea3a7SDavid Howells skb_tailroom(skb1) < tailbits)
5072716ea3a7SDavid Howells ntail = tailbits + 128;
5073716ea3a7SDavid Howells }
5074716ea3a7SDavid Howells
5075716ea3a7SDavid Howells if (copyflag ||
5076716ea3a7SDavid Howells skb_cloned(skb1) ||
5077716ea3a7SDavid Howells ntail ||
5078716ea3a7SDavid Howells skb_shinfo(skb1)->nr_frags ||
507921dc3301SDavid S. Miller skb_has_frag_list(skb1)) {
5080716ea3a7SDavid Howells struct sk_buff *skb2;
5081716ea3a7SDavid Howells
5082716ea3a7SDavid Howells /* Fuck, we are miserable poor guys... */
5083716ea3a7SDavid Howells if (ntail == 0)
5084716ea3a7SDavid Howells skb2 = skb_copy(skb1, GFP_ATOMIC);
5085716ea3a7SDavid Howells else
5086716ea3a7SDavid Howells skb2 = skb_copy_expand(skb1,
5087716ea3a7SDavid Howells skb_headroom(skb1),
5088716ea3a7SDavid Howells ntail,
5089716ea3a7SDavid Howells GFP_ATOMIC);
5090716ea3a7SDavid Howells if (unlikely(skb2 == NULL))
5091716ea3a7SDavid Howells return -ENOMEM;
5092716ea3a7SDavid Howells
5093716ea3a7SDavid Howells if (skb1->sk)
5094716ea3a7SDavid Howells skb_set_owner_w(skb2, skb1->sk);
5095716ea3a7SDavid Howells
5096716ea3a7SDavid Howells /* Looking around. Are we still alive?
5097716ea3a7SDavid Howells * OK, link new skb, drop old one */
5098716ea3a7SDavid Howells
5099716ea3a7SDavid Howells skb2->next = skb1->next;
5100716ea3a7SDavid Howells *skb_p = skb2;
5101716ea3a7SDavid Howells kfree_skb(skb1);
5102716ea3a7SDavid Howells skb1 = skb2;
5103716ea3a7SDavid Howells }
5104716ea3a7SDavid Howells elt++;
5105716ea3a7SDavid Howells *trailer = skb1;
5106716ea3a7SDavid Howells skb_p = &skb1->next;
5107716ea3a7SDavid Howells }
5108716ea3a7SDavid Howells
5109716ea3a7SDavid Howells return elt;
5110716ea3a7SDavid Howells }
5111b4ac530fSDavid S. Miller EXPORT_SYMBOL_GPL(skb_cow_data);
5112716ea3a7SDavid Howells
sock_rmem_free(struct sk_buff * skb)5113b1faf566SEric Dumazet static void sock_rmem_free(struct sk_buff *skb)
5114b1faf566SEric Dumazet {
5115b1faf566SEric Dumazet struct sock *sk = skb->sk;
5116b1faf566SEric Dumazet
5117b1faf566SEric Dumazet atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
5118b1faf566SEric Dumazet }
5119b1faf566SEric Dumazet
skb_set_err_queue(struct sk_buff * skb)51208605330aSSoheil Hassas Yeganeh static void skb_set_err_queue(struct sk_buff *skb)
51218605330aSSoheil Hassas Yeganeh {
51228605330aSSoheil Hassas Yeganeh /* pkt_type of skbs received on local sockets is never PACKET_OUTGOING.
51238605330aSSoheil Hassas Yeganeh * So, it is safe to (mis)use it to mark skbs on the error queue.
51248605330aSSoheil Hassas Yeganeh */
51258605330aSSoheil Hassas Yeganeh skb->pkt_type = PACKET_OUTGOING;
51268605330aSSoheil Hassas Yeganeh BUILD_BUG_ON(PACKET_OUTGOING == 0);
51278605330aSSoheil Hassas Yeganeh }
51288605330aSSoheil Hassas Yeganeh
5129b1faf566SEric Dumazet /*
5130b1faf566SEric Dumazet * Note: We dont mem charge error packets (no sk_forward_alloc changes)
5131b1faf566SEric Dumazet */
sock_queue_err_skb(struct sock * sk,struct sk_buff * skb)5132b1faf566SEric Dumazet int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb)
5133b1faf566SEric Dumazet {
5134b1faf566SEric Dumazet if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
5135ebb3b78dSEric Dumazet (unsigned int)READ_ONCE(sk->sk_rcvbuf))
5136b1faf566SEric Dumazet return -ENOMEM;
5137b1faf566SEric Dumazet
5138b1faf566SEric Dumazet skb_orphan(skb);
5139b1faf566SEric Dumazet skb->sk = sk;
5140b1faf566SEric Dumazet skb->destructor = sock_rmem_free;
5141b1faf566SEric Dumazet atomic_add(skb->truesize, &sk->sk_rmem_alloc);
51428605330aSSoheil Hassas Yeganeh skb_set_err_queue(skb);
5143b1faf566SEric Dumazet
5144abb57ea4SEric Dumazet /* before exiting rcu section, make sure dst is refcounted */
5145abb57ea4SEric Dumazet skb_dst_force(skb);
5146abb57ea4SEric Dumazet
5147b1faf566SEric Dumazet skb_queue_tail(&sk->sk_error_queue, skb);
5148b1faf566SEric Dumazet if (!sock_flag(sk, SOCK_DEAD))
5149e3ae2365SAlexander Aring sk_error_report(sk);
5150b1faf566SEric Dumazet return 0;
5151b1faf566SEric Dumazet }
5152b1faf566SEric Dumazet EXPORT_SYMBOL(sock_queue_err_skb);
5153b1faf566SEric Dumazet
is_icmp_err_skb(const struct sk_buff * skb)515483a1a1a7SSoheil Hassas Yeganeh static bool is_icmp_err_skb(const struct sk_buff *skb)
515583a1a1a7SSoheil Hassas Yeganeh {
515683a1a1a7SSoheil Hassas Yeganeh return skb && (SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP ||
515783a1a1a7SSoheil Hassas Yeganeh SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP6);
515883a1a1a7SSoheil Hassas Yeganeh }
515983a1a1a7SSoheil Hassas Yeganeh
sock_dequeue_err_skb(struct sock * sk)5160364a9e93SWillem de Bruijn struct sk_buff *sock_dequeue_err_skb(struct sock *sk)
5161364a9e93SWillem de Bruijn {
5162364a9e93SWillem de Bruijn struct sk_buff_head *q = &sk->sk_error_queue;
516383a1a1a7SSoheil Hassas Yeganeh struct sk_buff *skb, *skb_next = NULL;
516483a1a1a7SSoheil Hassas Yeganeh bool icmp_next = false;
5165997d5c3fSEric Dumazet unsigned long flags;
5166364a9e93SWillem de Bruijn
5167997d5c3fSEric Dumazet spin_lock_irqsave(&q->lock, flags);
5168364a9e93SWillem de Bruijn skb = __skb_dequeue(q);
516938b25793SSoheil Hassas Yeganeh if (skb && (skb_next = skb_peek(q))) {
517083a1a1a7SSoheil Hassas Yeganeh icmp_next = is_icmp_err_skb(skb_next);
517138b25793SSoheil Hassas Yeganeh if (icmp_next)
5172985f7337SWillem de Bruijn sk->sk_err = SKB_EXT_ERR(skb_next)->ee.ee_errno;
517338b25793SSoheil Hassas Yeganeh }
5174997d5c3fSEric Dumazet spin_unlock_irqrestore(&q->lock, flags);
5175364a9e93SWillem de Bruijn
517683a1a1a7SSoheil Hassas Yeganeh if (is_icmp_err_skb(skb) && !icmp_next)
517783a1a1a7SSoheil Hassas Yeganeh sk->sk_err = 0;
517883a1a1a7SSoheil Hassas Yeganeh
517983a1a1a7SSoheil Hassas Yeganeh if (skb_next)
5180e3ae2365SAlexander Aring sk_error_report(sk);
5181364a9e93SWillem de Bruijn
5182364a9e93SWillem de Bruijn return skb;
5183364a9e93SWillem de Bruijn }
5184364a9e93SWillem de Bruijn EXPORT_SYMBOL(sock_dequeue_err_skb);
5185364a9e93SWillem de Bruijn
5186cab41c47SAlexander Duyck /**
5187cab41c47SAlexander Duyck * skb_clone_sk - create clone of skb, and take reference to socket
5188cab41c47SAlexander Duyck * @skb: the skb to clone
5189cab41c47SAlexander Duyck *
5190cab41c47SAlexander Duyck * This function creates a clone of a buffer that holds a reference on
5191cab41c47SAlexander Duyck * sk_refcnt. Buffers created via this function are meant to be
5192cab41c47SAlexander Duyck * returned using sock_queue_err_skb, or free via kfree_skb.
5193cab41c47SAlexander Duyck *
5194cab41c47SAlexander Duyck * When passing buffers allocated with this function to sock_queue_err_skb
5195cab41c47SAlexander Duyck * it is necessary to wrap the call with sock_hold/sock_put in order to
5196cab41c47SAlexander Duyck * prevent the socket from being released prior to being enqueued on
5197cab41c47SAlexander Duyck * the sk_error_queue.
5198cab41c47SAlexander Duyck */
skb_clone_sk(struct sk_buff * skb)519962bccb8cSAlexander Duyck struct sk_buff *skb_clone_sk(struct sk_buff *skb)
520062bccb8cSAlexander Duyck {
520162bccb8cSAlexander Duyck struct sock *sk = skb->sk;
520262bccb8cSAlexander Duyck struct sk_buff *clone;
520362bccb8cSAlexander Duyck
520441c6d650SReshetova, Elena if (!sk || !refcount_inc_not_zero(&sk->sk_refcnt))
520562bccb8cSAlexander Duyck return NULL;
520662bccb8cSAlexander Duyck
520762bccb8cSAlexander Duyck clone = skb_clone(skb, GFP_ATOMIC);
520862bccb8cSAlexander Duyck if (!clone) {
520962bccb8cSAlexander Duyck sock_put(sk);
521062bccb8cSAlexander Duyck return NULL;
521162bccb8cSAlexander Duyck }
521262bccb8cSAlexander Duyck
521362bccb8cSAlexander Duyck clone->sk = sk;
521462bccb8cSAlexander Duyck clone->destructor = sock_efree;
521562bccb8cSAlexander Duyck
521662bccb8cSAlexander Duyck return clone;
521762bccb8cSAlexander Duyck }
521862bccb8cSAlexander Duyck EXPORT_SYMBOL(skb_clone_sk);
521962bccb8cSAlexander Duyck
__skb_complete_tx_timestamp(struct sk_buff * skb,struct sock * sk,int tstype,bool opt_stats)522037846ef0SAlexander Duyck static void __skb_complete_tx_timestamp(struct sk_buff *skb,
522137846ef0SAlexander Duyck struct sock *sk,
52224ef1b286SSoheil Hassas Yeganeh int tstype,
52234ef1b286SSoheil Hassas Yeganeh bool opt_stats)
5224ac45f602SPatrick Ohly {
5225ac45f602SPatrick Ohly struct sock_exterr_skb *serr;
5226ac45f602SPatrick Ohly int err;
5227ac45f602SPatrick Ohly
52284ef1b286SSoheil Hassas Yeganeh BUILD_BUG_ON(sizeof(struct sock_exterr_skb) > sizeof(skb->cb));
52294ef1b286SSoheil Hassas Yeganeh
5230ac45f602SPatrick Ohly serr = SKB_EXT_ERR(skb);
5231ac45f602SPatrick Ohly memset(serr, 0, sizeof(*serr));
5232ac45f602SPatrick Ohly serr->ee.ee_errno = ENOMSG;
5233ac45f602SPatrick Ohly serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
5234e7fd2885SWillem de Bruijn serr->ee.ee_info = tstype;
52354ef1b286SSoheil Hassas Yeganeh serr->opt_stats = opt_stats;
52361862d620SWillem de Bruijn serr->header.h4.iif = skb->dev ? skb->dev->ifindex : 0;
5237e3390b30SEric Dumazet if (READ_ONCE(sk->sk_tsflags) & SOF_TIMESTAMPING_OPT_ID) {
523809c2d251SWillem de Bruijn serr->ee.ee_data = skb_shinfo(skb)->tskey;
523942f67eeaSEric Dumazet if (sk_is_tcp(sk))
5240a1cdec57SEric Dumazet serr->ee.ee_data -= atomic_read(&sk->sk_tskey);
52414ed2d765SWillem de Bruijn }
524229030374SEric Dumazet
5243ac45f602SPatrick Ohly err = sock_queue_err_skb(sk, skb);
524429030374SEric Dumazet
5245ac45f602SPatrick Ohly if (err)
5246ac45f602SPatrick Ohly kfree_skb(skb);
5247ac45f602SPatrick Ohly }
524837846ef0SAlexander Duyck
skb_may_tx_timestamp(struct sock * sk,bool tsonly)5249b245be1fSWillem de Bruijn static bool skb_may_tx_timestamp(struct sock *sk, bool tsonly)
5250b245be1fSWillem de Bruijn {
5251b245be1fSWillem de Bruijn bool ret;
5252b245be1fSWillem de Bruijn
5253d2154b0aSKuniyuki Iwashima if (likely(READ_ONCE(sysctl_tstamp_allow_data) || tsonly))
5254b245be1fSWillem de Bruijn return true;
5255b245be1fSWillem de Bruijn
5256b245be1fSWillem de Bruijn read_lock_bh(&sk->sk_callback_lock);
5257b245be1fSWillem de Bruijn ret = sk->sk_socket && sk->sk_socket->file &&
5258b245be1fSWillem de Bruijn file_ns_capable(sk->sk_socket->file, &init_user_ns, CAP_NET_RAW);
5259b245be1fSWillem de Bruijn read_unlock_bh(&sk->sk_callback_lock);
5260b245be1fSWillem de Bruijn return ret;
5261b245be1fSWillem de Bruijn }
5262b245be1fSWillem de Bruijn
skb_complete_tx_timestamp(struct sk_buff * skb,struct skb_shared_hwtstamps * hwtstamps)526337846ef0SAlexander Duyck void skb_complete_tx_timestamp(struct sk_buff *skb,
526437846ef0SAlexander Duyck struct skb_shared_hwtstamps *hwtstamps)
526537846ef0SAlexander Duyck {
526637846ef0SAlexander Duyck struct sock *sk = skb->sk;
526737846ef0SAlexander Duyck
5268b245be1fSWillem de Bruijn if (!skb_may_tx_timestamp(sk, false))
526935b99dffSWillem de Bruijn goto err;
5270b245be1fSWillem de Bruijn
52719ac25fc0SEric Dumazet /* Take a reference to prevent skb_orphan() from freeing the socket,
52729ac25fc0SEric Dumazet * but only if the socket refcount is not zero.
52739ac25fc0SEric Dumazet */
527441c6d650SReshetova, Elena if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) {
527537846ef0SAlexander Duyck *skb_hwtstamps(skb) = *hwtstamps;
52764ef1b286SSoheil Hassas Yeganeh __skb_complete_tx_timestamp(skb, sk, SCM_TSTAMP_SND, false);
527737846ef0SAlexander Duyck sock_put(sk);
527835b99dffSWillem de Bruijn return;
527937846ef0SAlexander Duyck }
528035b99dffSWillem de Bruijn
528135b99dffSWillem de Bruijn err:
528235b99dffSWillem de Bruijn kfree_skb(skb);
52839ac25fc0SEric Dumazet }
528437846ef0SAlexander Duyck EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp);
528537846ef0SAlexander Duyck
__skb_tstamp_tx(struct sk_buff * orig_skb,const struct sk_buff * ack_skb,struct skb_shared_hwtstamps * hwtstamps,struct sock * sk,int tstype)528637846ef0SAlexander Duyck void __skb_tstamp_tx(struct sk_buff *orig_skb,
5287e7ed11eeSYousuk Seung const struct sk_buff *ack_skb,
528837846ef0SAlexander Duyck struct skb_shared_hwtstamps *hwtstamps,
528937846ef0SAlexander Duyck struct sock *sk, int tstype)
529037846ef0SAlexander Duyck {
529137846ef0SAlexander Duyck struct sk_buff *skb;
52924ef1b286SSoheil Hassas Yeganeh bool tsonly, opt_stats = false;
5293e3390b30SEric Dumazet u32 tsflags;
529437846ef0SAlexander Duyck
52953a8dd971SWillem de Bruijn if (!sk)
52963a8dd971SWillem de Bruijn return;
52973a8dd971SWillem de Bruijn
5298e3390b30SEric Dumazet tsflags = READ_ONCE(sk->sk_tsflags);
5299e3390b30SEric Dumazet if (!hwtstamps && !(tsflags & SOF_TIMESTAMPING_OPT_TX_SWHW) &&
5300b50a5c70SMiroslav Lichvar skb_shinfo(orig_skb)->tx_flags & SKBTX_IN_PROGRESS)
5301b50a5c70SMiroslav Lichvar return;
5302b50a5c70SMiroslav Lichvar
5303e3390b30SEric Dumazet tsonly = tsflags & SOF_TIMESTAMPING_OPT_TSONLY;
53043a8dd971SWillem de Bruijn if (!skb_may_tx_timestamp(sk, tsonly))
530537846ef0SAlexander Duyck return;
530637846ef0SAlexander Duyck
53071c885808SFrancis Yan if (tsonly) {
53081c885808SFrancis Yan #ifdef CONFIG_INET
5309e3390b30SEric Dumazet if ((tsflags & SOF_TIMESTAMPING_OPT_STATS) &&
531042f67eeaSEric Dumazet sk_is_tcp(sk)) {
5311e7ed11eeSYousuk Seung skb = tcp_get_timestamping_opt_stats(sk, orig_skb,
5312e7ed11eeSYousuk Seung ack_skb);
53134ef1b286SSoheil Hassas Yeganeh opt_stats = true;
53144ef1b286SSoheil Hassas Yeganeh } else
53151c885808SFrancis Yan #endif
53161c885808SFrancis Yan skb = alloc_skb(0, GFP_ATOMIC);
53171c885808SFrancis Yan } else {
531837846ef0SAlexander Duyck skb = skb_clone(orig_skb, GFP_ATOMIC);
531950749f2dSKuniyuki Iwashima
53208a02fb71SPratyush Yadav if (skb_orphan_frags_rx(skb, GFP_ATOMIC)) {
53218a02fb71SPratyush Yadav kfree_skb(skb);
532250749f2dSKuniyuki Iwashima return;
53231c885808SFrancis Yan }
53248a02fb71SPratyush Yadav }
532537846ef0SAlexander Duyck if (!skb)
532637846ef0SAlexander Duyck return;
532737846ef0SAlexander Duyck
532849ca0d8bSWillem de Bruijn if (tsonly) {
5329fff88030SWillem de Bruijn skb_shinfo(skb)->tx_flags |= skb_shinfo(orig_skb)->tx_flags &
5330fff88030SWillem de Bruijn SKBTX_ANY_TSTAMP;
533149ca0d8bSWillem de Bruijn skb_shinfo(skb)->tskey = skb_shinfo(orig_skb)->tskey;
533249ca0d8bSWillem de Bruijn }
533349ca0d8bSWillem de Bruijn
533449ca0d8bSWillem de Bruijn if (hwtstamps)
533549ca0d8bSWillem de Bruijn *skb_hwtstamps(skb) = *hwtstamps;
533649ca0d8bSWillem de Bruijn else
5337d93376f5SMartin KaFai Lau __net_timestamp(skb);
533849ca0d8bSWillem de Bruijn
53394ef1b286SSoheil Hassas Yeganeh __skb_complete_tx_timestamp(skb, sk, tstype, opt_stats);
534037846ef0SAlexander Duyck }
5341e7fd2885SWillem de Bruijn EXPORT_SYMBOL_GPL(__skb_tstamp_tx);
5342e7fd2885SWillem de Bruijn
skb_tstamp_tx(struct sk_buff * orig_skb,struct skb_shared_hwtstamps * hwtstamps)5343e7fd2885SWillem de Bruijn void skb_tstamp_tx(struct sk_buff *orig_skb,
5344e7fd2885SWillem de Bruijn struct skb_shared_hwtstamps *hwtstamps)
5345e7fd2885SWillem de Bruijn {
5346e7ed11eeSYousuk Seung return __skb_tstamp_tx(orig_skb, NULL, hwtstamps, orig_skb->sk,
5347e7fd2885SWillem de Bruijn SCM_TSTAMP_SND);
5348e7fd2885SWillem de Bruijn }
5349ac45f602SPatrick Ohly EXPORT_SYMBOL_GPL(skb_tstamp_tx);
5350ac45f602SPatrick Ohly
5351eb6fba75SJakub Kicinski #ifdef CONFIG_WIRELESS
skb_complete_wifi_ack(struct sk_buff * skb,bool acked)53526e3e939fSJohannes Berg void skb_complete_wifi_ack(struct sk_buff *skb, bool acked)
53536e3e939fSJohannes Berg {
53546e3e939fSJohannes Berg struct sock *sk = skb->sk;
53556e3e939fSJohannes Berg struct sock_exterr_skb *serr;
5356dd4f1072SEric Dumazet int err = 1;
53576e3e939fSJohannes Berg
53586e3e939fSJohannes Berg skb->wifi_acked_valid = 1;
53596e3e939fSJohannes Berg skb->wifi_acked = acked;
53606e3e939fSJohannes Berg
53616e3e939fSJohannes Berg serr = SKB_EXT_ERR(skb);
53626e3e939fSJohannes Berg memset(serr, 0, sizeof(*serr));
53636e3e939fSJohannes Berg serr->ee.ee_errno = ENOMSG;
53646e3e939fSJohannes Berg serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS;
53656e3e939fSJohannes Berg
5366dd4f1072SEric Dumazet /* Take a reference to prevent skb_orphan() from freeing the socket,
5367dd4f1072SEric Dumazet * but only if the socket refcount is not zero.
5368dd4f1072SEric Dumazet */
536941c6d650SReshetova, Elena if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) {
53706e3e939fSJohannes Berg err = sock_queue_err_skb(sk, skb);
5371dd4f1072SEric Dumazet sock_put(sk);
5372dd4f1072SEric Dumazet }
53736e3e939fSJohannes Berg if (err)
53746e3e939fSJohannes Berg kfree_skb(skb);
53756e3e939fSJohannes Berg }
53766e3e939fSJohannes Berg EXPORT_SYMBOL_GPL(skb_complete_wifi_ack);
5377eb6fba75SJakub Kicinski #endif /* CONFIG_WIRELESS */
53786e3e939fSJohannes Berg
5379f35d9d8aSRusty Russell /**
5380f35d9d8aSRusty Russell * skb_partial_csum_set - set up and verify partial csum values for packet
5381f35d9d8aSRusty Russell * @skb: the skb to set
5382f35d9d8aSRusty Russell * @start: the number of bytes after skb->data to start checksumming.
5383f35d9d8aSRusty Russell * @off: the offset from start to place the checksum.
5384f35d9d8aSRusty Russell *
5385f35d9d8aSRusty Russell * For untrusted partially-checksummed packets, we need to make sure the values
5386f35d9d8aSRusty Russell * for skb->csum_start and skb->csum_offset are valid so we don't oops.
5387f35d9d8aSRusty Russell *
5388f35d9d8aSRusty Russell * This function checks and sets those values and skb->ip_summed: if this
5389f35d9d8aSRusty Russell * returns false you should drop the packet.
5390f35d9d8aSRusty Russell */
skb_partial_csum_set(struct sk_buff * skb,u16 start,u16 off)5391f35d9d8aSRusty Russell bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
5392f35d9d8aSRusty Russell {
539352b5d6f5SEric Dumazet u32 csum_end = (u32)start + (u32)off + sizeof(__sum16);
539452b5d6f5SEric Dumazet u32 csum_start = skb_headroom(skb) + (u32)start;
539552b5d6f5SEric Dumazet
5396424f8416SEric Dumazet if (unlikely(csum_start >= U16_MAX || csum_end > skb_headlen(skb))) {
539752b5d6f5SEric Dumazet net_warn_ratelimited("bad partial csum: csum=%u/%u headroom=%u headlen=%u\n",
539852b5d6f5SEric Dumazet start, off, skb_headroom(skb), skb_headlen(skb));
5399f35d9d8aSRusty Russell return false;
5400f35d9d8aSRusty Russell }
5401f35d9d8aSRusty Russell skb->ip_summed = CHECKSUM_PARTIAL;
540252b5d6f5SEric Dumazet skb->csum_start = csum_start;
5403f35d9d8aSRusty Russell skb->csum_offset = off;
5404424f8416SEric Dumazet skb->transport_header = csum_start;
5405f35d9d8aSRusty Russell return true;
5406f35d9d8aSRusty Russell }
5407b4ac530fSDavid S. Miller EXPORT_SYMBOL_GPL(skb_partial_csum_set);
5408f35d9d8aSRusty Russell
skb_maybe_pull_tail(struct sk_buff * skb,unsigned int len,unsigned int max)5409ed1f50c3SPaul Durrant static int skb_maybe_pull_tail(struct sk_buff *skb, unsigned int len,
5410ed1f50c3SPaul Durrant unsigned int max)
5411ed1f50c3SPaul Durrant {
5412ed1f50c3SPaul Durrant if (skb_headlen(skb) >= len)
5413ed1f50c3SPaul Durrant return 0;
5414ed1f50c3SPaul Durrant
5415ed1f50c3SPaul Durrant /* If we need to pullup then pullup to the max, so we
5416ed1f50c3SPaul Durrant * won't need to do it again.
5417ed1f50c3SPaul Durrant */
5418ed1f50c3SPaul Durrant if (max > skb->len)
5419ed1f50c3SPaul Durrant max = skb->len;
5420ed1f50c3SPaul Durrant
5421ed1f50c3SPaul Durrant if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL)
5422ed1f50c3SPaul Durrant return -ENOMEM;
5423ed1f50c3SPaul Durrant
5424ed1f50c3SPaul Durrant if (skb_headlen(skb) < len)
5425ed1f50c3SPaul Durrant return -EPROTO;
5426ed1f50c3SPaul Durrant
5427ed1f50c3SPaul Durrant return 0;
5428ed1f50c3SPaul Durrant }
5429ed1f50c3SPaul Durrant
5430f9708b43SJan Beulich #define MAX_TCP_HDR_LEN (15 * 4)
5431f9708b43SJan Beulich
skb_checksum_setup_ip(struct sk_buff * skb,typeof (IPPROTO_IP)proto,unsigned int off)5432f9708b43SJan Beulich static __sum16 *skb_checksum_setup_ip(struct sk_buff *skb,
5433f9708b43SJan Beulich typeof(IPPROTO_IP) proto,
5434f9708b43SJan Beulich unsigned int off)
5435f9708b43SJan Beulich {
5436f9708b43SJan Beulich int err;
5437f9708b43SJan Beulich
5438161d1792SKees Cook switch (proto) {
5439f9708b43SJan Beulich case IPPROTO_TCP:
5440f9708b43SJan Beulich err = skb_maybe_pull_tail(skb, off + sizeof(struct tcphdr),
5441f9708b43SJan Beulich off + MAX_TCP_HDR_LEN);
5442f9708b43SJan Beulich if (!err && !skb_partial_csum_set(skb, off,
5443f9708b43SJan Beulich offsetof(struct tcphdr,
5444f9708b43SJan Beulich check)))
5445f9708b43SJan Beulich err = -EPROTO;
5446f9708b43SJan Beulich return err ? ERR_PTR(err) : &tcp_hdr(skb)->check;
5447f9708b43SJan Beulich
5448f9708b43SJan Beulich case IPPROTO_UDP:
5449f9708b43SJan Beulich err = skb_maybe_pull_tail(skb, off + sizeof(struct udphdr),
5450f9708b43SJan Beulich off + sizeof(struct udphdr));
5451f9708b43SJan Beulich if (!err && !skb_partial_csum_set(skb, off,
5452f9708b43SJan Beulich offsetof(struct udphdr,
5453f9708b43SJan Beulich check)))
5454f9708b43SJan Beulich err = -EPROTO;
5455f9708b43SJan Beulich return err ? ERR_PTR(err) : &udp_hdr(skb)->check;
5456f9708b43SJan Beulich }
5457f9708b43SJan Beulich
5458f9708b43SJan Beulich return ERR_PTR(-EPROTO);
5459f9708b43SJan Beulich }
5460f9708b43SJan Beulich
5461ed1f50c3SPaul Durrant /* This value should be large enough to cover a tagged ethernet header plus
5462ed1f50c3SPaul Durrant * maximally sized IP and TCP or UDP headers.
5463ed1f50c3SPaul Durrant */
5464ed1f50c3SPaul Durrant #define MAX_IP_HDR_LEN 128
5465ed1f50c3SPaul Durrant
skb_checksum_setup_ipv4(struct sk_buff * skb,bool recalculate)5466f9708b43SJan Beulich static int skb_checksum_setup_ipv4(struct sk_buff *skb, bool recalculate)
5467ed1f50c3SPaul Durrant {
5468ed1f50c3SPaul Durrant unsigned int off;
5469ed1f50c3SPaul Durrant bool fragment;
5470f9708b43SJan Beulich __sum16 *csum;
5471ed1f50c3SPaul Durrant int err;
5472ed1f50c3SPaul Durrant
5473ed1f50c3SPaul Durrant fragment = false;
5474ed1f50c3SPaul Durrant
5475ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb,
5476ed1f50c3SPaul Durrant sizeof(struct iphdr),
5477ed1f50c3SPaul Durrant MAX_IP_HDR_LEN);
5478ed1f50c3SPaul Durrant if (err < 0)
5479ed1f50c3SPaul Durrant goto out;
5480ed1f50c3SPaul Durrant
548111f920d2SMiaohe Lin if (ip_is_fragment(ip_hdr(skb)))
5482ed1f50c3SPaul Durrant fragment = true;
5483ed1f50c3SPaul Durrant
5484ed1f50c3SPaul Durrant off = ip_hdrlen(skb);
5485ed1f50c3SPaul Durrant
5486ed1f50c3SPaul Durrant err = -EPROTO;
5487ed1f50c3SPaul Durrant
5488ed1f50c3SPaul Durrant if (fragment)
5489ed1f50c3SPaul Durrant goto out;
5490ed1f50c3SPaul Durrant
5491f9708b43SJan Beulich csum = skb_checksum_setup_ip(skb, ip_hdr(skb)->protocol, off);
5492f9708b43SJan Beulich if (IS_ERR(csum))
5493f9708b43SJan Beulich return PTR_ERR(csum);
5494ed1f50c3SPaul Durrant
5495ed1f50c3SPaul Durrant if (recalculate)
5496f9708b43SJan Beulich *csum = ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
5497ed1f50c3SPaul Durrant ip_hdr(skb)->daddr,
5498ed1f50c3SPaul Durrant skb->len - off,
5499f9708b43SJan Beulich ip_hdr(skb)->protocol, 0);
5500ed1f50c3SPaul Durrant err = 0;
5501ed1f50c3SPaul Durrant
5502ed1f50c3SPaul Durrant out:
5503ed1f50c3SPaul Durrant return err;
5504ed1f50c3SPaul Durrant }
5505ed1f50c3SPaul Durrant
5506ed1f50c3SPaul Durrant /* This value should be large enough to cover a tagged ethernet header plus
5507ed1f50c3SPaul Durrant * an IPv6 header, all options, and a maximal TCP or UDP header.
5508ed1f50c3SPaul Durrant */
5509ed1f50c3SPaul Durrant #define MAX_IPV6_HDR_LEN 256
5510ed1f50c3SPaul Durrant
5511ed1f50c3SPaul Durrant #define OPT_HDR(type, skb, off) \
5512ed1f50c3SPaul Durrant (type *)(skb_network_header(skb) + (off))
5513ed1f50c3SPaul Durrant
skb_checksum_setup_ipv6(struct sk_buff * skb,bool recalculate)5514ed1f50c3SPaul Durrant static int skb_checksum_setup_ipv6(struct sk_buff *skb, bool recalculate)
5515ed1f50c3SPaul Durrant {
5516ed1f50c3SPaul Durrant int err;
5517ed1f50c3SPaul Durrant u8 nexthdr;
5518ed1f50c3SPaul Durrant unsigned int off;
5519ed1f50c3SPaul Durrant unsigned int len;
5520ed1f50c3SPaul Durrant bool fragment;
5521ed1f50c3SPaul Durrant bool done;
5522f9708b43SJan Beulich __sum16 *csum;
5523ed1f50c3SPaul Durrant
5524ed1f50c3SPaul Durrant fragment = false;
5525ed1f50c3SPaul Durrant done = false;
5526ed1f50c3SPaul Durrant
5527ed1f50c3SPaul Durrant off = sizeof(struct ipv6hdr);
5528ed1f50c3SPaul Durrant
5529ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN);
5530ed1f50c3SPaul Durrant if (err < 0)
5531ed1f50c3SPaul Durrant goto out;
5532ed1f50c3SPaul Durrant
5533ed1f50c3SPaul Durrant nexthdr = ipv6_hdr(skb)->nexthdr;
5534ed1f50c3SPaul Durrant
5535ed1f50c3SPaul Durrant len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len);
5536ed1f50c3SPaul Durrant while (off <= len && !done) {
5537ed1f50c3SPaul Durrant switch (nexthdr) {
5538ed1f50c3SPaul Durrant case IPPROTO_DSTOPTS:
5539ed1f50c3SPaul Durrant case IPPROTO_HOPOPTS:
5540ed1f50c3SPaul Durrant case IPPROTO_ROUTING: {
5541ed1f50c3SPaul Durrant struct ipv6_opt_hdr *hp;
5542ed1f50c3SPaul Durrant
5543ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb,
5544ed1f50c3SPaul Durrant off +
5545ed1f50c3SPaul Durrant sizeof(struct ipv6_opt_hdr),
5546ed1f50c3SPaul Durrant MAX_IPV6_HDR_LEN);
5547ed1f50c3SPaul Durrant if (err < 0)
5548ed1f50c3SPaul Durrant goto out;
5549ed1f50c3SPaul Durrant
5550ed1f50c3SPaul Durrant hp = OPT_HDR(struct ipv6_opt_hdr, skb, off);
5551ed1f50c3SPaul Durrant nexthdr = hp->nexthdr;
5552ed1f50c3SPaul Durrant off += ipv6_optlen(hp);
5553ed1f50c3SPaul Durrant break;
5554ed1f50c3SPaul Durrant }
5555ed1f50c3SPaul Durrant case IPPROTO_AH: {
5556ed1f50c3SPaul Durrant struct ip_auth_hdr *hp;
5557ed1f50c3SPaul Durrant
5558ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb,
5559ed1f50c3SPaul Durrant off +
5560ed1f50c3SPaul Durrant sizeof(struct ip_auth_hdr),
5561ed1f50c3SPaul Durrant MAX_IPV6_HDR_LEN);
5562ed1f50c3SPaul Durrant if (err < 0)
5563ed1f50c3SPaul Durrant goto out;
5564ed1f50c3SPaul Durrant
5565ed1f50c3SPaul Durrant hp = OPT_HDR(struct ip_auth_hdr, skb, off);
5566ed1f50c3SPaul Durrant nexthdr = hp->nexthdr;
5567ed1f50c3SPaul Durrant off += ipv6_authlen(hp);
5568ed1f50c3SPaul Durrant break;
5569ed1f50c3SPaul Durrant }
5570ed1f50c3SPaul Durrant case IPPROTO_FRAGMENT: {
5571ed1f50c3SPaul Durrant struct frag_hdr *hp;
5572ed1f50c3SPaul Durrant
5573ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb,
5574ed1f50c3SPaul Durrant off +
5575ed1f50c3SPaul Durrant sizeof(struct frag_hdr),
5576ed1f50c3SPaul Durrant MAX_IPV6_HDR_LEN);
5577ed1f50c3SPaul Durrant if (err < 0)
5578ed1f50c3SPaul Durrant goto out;
5579ed1f50c3SPaul Durrant
5580ed1f50c3SPaul Durrant hp = OPT_HDR(struct frag_hdr, skb, off);
5581ed1f50c3SPaul Durrant
5582ed1f50c3SPaul Durrant if (hp->frag_off & htons(IP6_OFFSET | IP6_MF))
5583ed1f50c3SPaul Durrant fragment = true;
5584ed1f50c3SPaul Durrant
5585ed1f50c3SPaul Durrant nexthdr = hp->nexthdr;
5586ed1f50c3SPaul Durrant off += sizeof(struct frag_hdr);
5587ed1f50c3SPaul Durrant break;
5588ed1f50c3SPaul Durrant }
5589ed1f50c3SPaul Durrant default:
5590ed1f50c3SPaul Durrant done = true;
5591ed1f50c3SPaul Durrant break;
5592ed1f50c3SPaul Durrant }
5593ed1f50c3SPaul Durrant }
5594ed1f50c3SPaul Durrant
5595ed1f50c3SPaul Durrant err = -EPROTO;
5596ed1f50c3SPaul Durrant
5597ed1f50c3SPaul Durrant if (!done || fragment)
5598ed1f50c3SPaul Durrant goto out;
5599ed1f50c3SPaul Durrant
5600f9708b43SJan Beulich csum = skb_checksum_setup_ip(skb, nexthdr, off);
5601f9708b43SJan Beulich if (IS_ERR(csum))
5602f9708b43SJan Beulich return PTR_ERR(csum);
5603ed1f50c3SPaul Durrant
5604ed1f50c3SPaul Durrant if (recalculate)
5605f9708b43SJan Beulich *csum = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
5606ed1f50c3SPaul Durrant &ipv6_hdr(skb)->daddr,
5607f9708b43SJan Beulich skb->len - off, nexthdr, 0);
5608ed1f50c3SPaul Durrant err = 0;
5609ed1f50c3SPaul Durrant
5610ed1f50c3SPaul Durrant out:
5611ed1f50c3SPaul Durrant return err;
5612ed1f50c3SPaul Durrant }
5613ed1f50c3SPaul Durrant
5614ed1f50c3SPaul Durrant /**
5615ed1f50c3SPaul Durrant * skb_checksum_setup - set up partial checksum offset
5616ed1f50c3SPaul Durrant * @skb: the skb to set up
5617ed1f50c3SPaul Durrant * @recalculate: if true the pseudo-header checksum will be recalculated
5618ed1f50c3SPaul Durrant */
skb_checksum_setup(struct sk_buff * skb,bool recalculate)5619ed1f50c3SPaul Durrant int skb_checksum_setup(struct sk_buff *skb, bool recalculate)
5620ed1f50c3SPaul Durrant {
5621ed1f50c3SPaul Durrant int err;
5622ed1f50c3SPaul Durrant
5623ed1f50c3SPaul Durrant switch (skb->protocol) {
5624ed1f50c3SPaul Durrant case htons(ETH_P_IP):
5625f9708b43SJan Beulich err = skb_checksum_setup_ipv4(skb, recalculate);
5626ed1f50c3SPaul Durrant break;
5627ed1f50c3SPaul Durrant
5628ed1f50c3SPaul Durrant case htons(ETH_P_IPV6):
5629ed1f50c3SPaul Durrant err = skb_checksum_setup_ipv6(skb, recalculate);
5630ed1f50c3SPaul Durrant break;
5631ed1f50c3SPaul Durrant
5632ed1f50c3SPaul Durrant default:
5633ed1f50c3SPaul Durrant err = -EPROTO;
5634ed1f50c3SPaul Durrant break;
5635ed1f50c3SPaul Durrant }
5636ed1f50c3SPaul Durrant
5637ed1f50c3SPaul Durrant return err;
5638ed1f50c3SPaul Durrant }
5639ed1f50c3SPaul Durrant EXPORT_SYMBOL(skb_checksum_setup);
5640ed1f50c3SPaul Durrant
56419afd85c9SLinus Lüssing /**
56429afd85c9SLinus Lüssing * skb_checksum_maybe_trim - maybe trims the given skb
56439afd85c9SLinus Lüssing * @skb: the skb to check
56449afd85c9SLinus Lüssing * @transport_len: the data length beyond the network header
56459afd85c9SLinus Lüssing *
56469afd85c9SLinus Lüssing * Checks whether the given skb has data beyond the given transport length.
56479afd85c9SLinus Lüssing * If so, returns a cloned skb trimmed to this transport length.
56489afd85c9SLinus Lüssing * Otherwise returns the provided skb. Returns NULL in error cases
56499afd85c9SLinus Lüssing * (e.g. transport_len exceeds skb length or out-of-memory).
56509afd85c9SLinus Lüssing *
5651a516993fSLinus Lüssing * Caller needs to set the skb transport header and free any returned skb if it
5652a516993fSLinus Lüssing * differs from the provided skb.
56539afd85c9SLinus Lüssing */
skb_checksum_maybe_trim(struct sk_buff * skb,unsigned int transport_len)56549afd85c9SLinus Lüssing static struct sk_buff *skb_checksum_maybe_trim(struct sk_buff *skb,
56559afd85c9SLinus Lüssing unsigned int transport_len)
56569afd85c9SLinus Lüssing {
56579afd85c9SLinus Lüssing struct sk_buff *skb_chk;
56589afd85c9SLinus Lüssing unsigned int len = skb_transport_offset(skb) + transport_len;
56599afd85c9SLinus Lüssing int ret;
56609afd85c9SLinus Lüssing
5661a516993fSLinus Lüssing if (skb->len < len)
56629afd85c9SLinus Lüssing return NULL;
5663a516993fSLinus Lüssing else if (skb->len == len)
56649afd85c9SLinus Lüssing return skb;
56659afd85c9SLinus Lüssing
56669afd85c9SLinus Lüssing skb_chk = skb_clone(skb, GFP_ATOMIC);
56679afd85c9SLinus Lüssing if (!skb_chk)
56689afd85c9SLinus Lüssing return NULL;
56699afd85c9SLinus Lüssing
56709afd85c9SLinus Lüssing ret = pskb_trim_rcsum(skb_chk, len);
56719afd85c9SLinus Lüssing if (ret) {
56729afd85c9SLinus Lüssing kfree_skb(skb_chk);
56739afd85c9SLinus Lüssing return NULL;
56749afd85c9SLinus Lüssing }
56759afd85c9SLinus Lüssing
56769afd85c9SLinus Lüssing return skb_chk;
56779afd85c9SLinus Lüssing }
56789afd85c9SLinus Lüssing
56799afd85c9SLinus Lüssing /**
56809afd85c9SLinus Lüssing * skb_checksum_trimmed - validate checksum of an skb
56819afd85c9SLinus Lüssing * @skb: the skb to check
56829afd85c9SLinus Lüssing * @transport_len: the data length beyond the network header
56839afd85c9SLinus Lüssing * @skb_chkf: checksum function to use
56849afd85c9SLinus Lüssing *
56859afd85c9SLinus Lüssing * Applies the given checksum function skb_chkf to the provided skb.
56869afd85c9SLinus Lüssing * Returns a checked and maybe trimmed skb. Returns NULL on error.
56879afd85c9SLinus Lüssing *
56889afd85c9SLinus Lüssing * If the skb has data beyond the given transport length, then a
56899afd85c9SLinus Lüssing * trimmed & cloned skb is checked and returned.
56909afd85c9SLinus Lüssing *
5691a516993fSLinus Lüssing * Caller needs to set the skb transport header and free any returned skb if it
5692a516993fSLinus Lüssing * differs from the provided skb.
56939afd85c9SLinus Lüssing */
skb_checksum_trimmed(struct sk_buff * skb,unsigned int transport_len,__sum16 (* skb_chkf)(struct sk_buff * skb))56949afd85c9SLinus Lüssing struct sk_buff *skb_checksum_trimmed(struct sk_buff *skb,
56959afd85c9SLinus Lüssing unsigned int transport_len,
56969afd85c9SLinus Lüssing __sum16(*skb_chkf)(struct sk_buff *skb))
56979afd85c9SLinus Lüssing {
56989afd85c9SLinus Lüssing struct sk_buff *skb_chk;
56999afd85c9SLinus Lüssing unsigned int offset = skb_transport_offset(skb);
5700fcba67c9SLinus Lüssing __sum16 ret;
57019afd85c9SLinus Lüssing
57029afd85c9SLinus Lüssing skb_chk = skb_checksum_maybe_trim(skb, transport_len);
57039afd85c9SLinus Lüssing if (!skb_chk)
5704a516993fSLinus Lüssing goto err;
57059afd85c9SLinus Lüssing
5706a516993fSLinus Lüssing if (!pskb_may_pull(skb_chk, offset))
5707a516993fSLinus Lüssing goto err;
57089afd85c9SLinus Lüssing
57099b368814SLinus Lüssing skb_pull_rcsum(skb_chk, offset);
57109afd85c9SLinus Lüssing ret = skb_chkf(skb_chk);
57119b368814SLinus Lüssing skb_push_rcsum(skb_chk, offset);
57129afd85c9SLinus Lüssing
5713a516993fSLinus Lüssing if (ret)
5714a516993fSLinus Lüssing goto err;
57159afd85c9SLinus Lüssing
57169afd85c9SLinus Lüssing return skb_chk;
5717a516993fSLinus Lüssing
5718a516993fSLinus Lüssing err:
5719a516993fSLinus Lüssing if (skb_chk && skb_chk != skb)
5720a516993fSLinus Lüssing kfree_skb(skb_chk);
5721a516993fSLinus Lüssing
5722a516993fSLinus Lüssing return NULL;
5723a516993fSLinus Lüssing
57249afd85c9SLinus Lüssing }
57259afd85c9SLinus Lüssing EXPORT_SYMBOL(skb_checksum_trimmed);
57269afd85c9SLinus Lüssing
__skb_warn_lro_forwarding(const struct sk_buff * skb)57274497b076SBen Hutchings void __skb_warn_lro_forwarding(const struct sk_buff *skb)
57284497b076SBen Hutchings {
5729e87cc472SJoe Perches net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n",
5730e87cc472SJoe Perches skb->dev->name);
57314497b076SBen Hutchings }
57324497b076SBen Hutchings EXPORT_SYMBOL(__skb_warn_lro_forwarding);
5733bad43ca8SEric Dumazet
kfree_skb_partial(struct sk_buff * skb,bool head_stolen)5734bad43ca8SEric Dumazet void kfree_skb_partial(struct sk_buff *skb, bool head_stolen)
5735bad43ca8SEric Dumazet {
57363d861f66SEric Dumazet if (head_stolen) {
57373d861f66SEric Dumazet skb_release_head_state(skb);
5738025a785fSJakub Kicinski kmem_cache_free(skbuff_cache, skb);
57393d861f66SEric Dumazet } else {
5740bad43ca8SEric Dumazet __kfree_skb(skb);
5741bad43ca8SEric Dumazet }
57423d861f66SEric Dumazet }
5743bad43ca8SEric Dumazet EXPORT_SYMBOL(kfree_skb_partial);
5744bad43ca8SEric Dumazet
5745bad43ca8SEric Dumazet /**
5746bad43ca8SEric Dumazet * skb_try_coalesce - try to merge skb to prior one
5747bad43ca8SEric Dumazet * @to: prior buffer
5748bad43ca8SEric Dumazet * @from: buffer to add
5749bad43ca8SEric Dumazet * @fragstolen: pointer to boolean
5750c6c4b97cSRandy Dunlap * @delta_truesize: how much more was allocated than was requested
5751bad43ca8SEric Dumazet */
skb_try_coalesce(struct sk_buff * to,struct sk_buff * from,bool * fragstolen,int * delta_truesize)5752bad43ca8SEric Dumazet bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
5753bad43ca8SEric Dumazet bool *fragstolen, int *delta_truesize)
5754bad43ca8SEric Dumazet {
5755c818fa9eSEric Dumazet struct skb_shared_info *to_shinfo, *from_shinfo;
5756bad43ca8SEric Dumazet int i, delta, len = from->len;
5757bad43ca8SEric Dumazet
5758bad43ca8SEric Dumazet *fragstolen = false;
5759bad43ca8SEric Dumazet
5760bad43ca8SEric Dumazet if (skb_cloned(to))
5761bad43ca8SEric Dumazet return false;
5762bad43ca8SEric Dumazet
57630646dc31SLiang Chen /* In general, avoid mixing page_pool and non-page_pool allocated
57640646dc31SLiang Chen * pages within the same SKB. Additionally avoid dealing with clones
57650646dc31SLiang Chen * with page_pool pages, in case the SKB is using page_pool fragment
57661effe8caSJean-Philippe Brucker * references (PP_FLAG_PAGE_FRAG). Since we only take full page
57671effe8caSJean-Philippe Brucker * references for cloned SKBs at the moment that would result in
57681effe8caSJean-Philippe Brucker * inconsistent reference counts.
57690646dc31SLiang Chen * In theory we could take full references if @from is cloned and
57700646dc31SLiang Chen * !@to->pp_recycle but its tricky (due to potential race with
57710646dc31SLiang Chen * the clone disappearing) and rare, so not worth dealing with.
57726a5bcd84SIlias Apalodimas */
57730646dc31SLiang Chen if (to->pp_recycle != from->pp_recycle ||
57740646dc31SLiang Chen (from->pp_recycle && skb_cloned(from)))
57756a5bcd84SIlias Apalodimas return false;
57766a5bcd84SIlias Apalodimas
5777bad43ca8SEric Dumazet if (len <= skb_tailroom(to)) {
5778e93a0435SEric Dumazet if (len)
5779bad43ca8SEric Dumazet BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len));
5780bad43ca8SEric Dumazet *delta_truesize = 0;
5781bad43ca8SEric Dumazet return true;
5782bad43ca8SEric Dumazet }
5783bad43ca8SEric Dumazet
5784c818fa9eSEric Dumazet to_shinfo = skb_shinfo(to);
5785c818fa9eSEric Dumazet from_shinfo = skb_shinfo(from);
5786c818fa9eSEric Dumazet if (to_shinfo->frag_list || from_shinfo->frag_list)
5787bad43ca8SEric Dumazet return false;
57881f8b977aSWillem de Bruijn if (skb_zcopy(to) || skb_zcopy(from))
57891f8b977aSWillem de Bruijn return false;
5790bad43ca8SEric Dumazet
5791bad43ca8SEric Dumazet if (skb_headlen(from) != 0) {
5792bad43ca8SEric Dumazet struct page *page;
5793bad43ca8SEric Dumazet unsigned int offset;
5794bad43ca8SEric Dumazet
5795c818fa9eSEric Dumazet if (to_shinfo->nr_frags +
5796c818fa9eSEric Dumazet from_shinfo->nr_frags >= MAX_SKB_FRAGS)
5797bad43ca8SEric Dumazet return false;
5798bad43ca8SEric Dumazet
5799bad43ca8SEric Dumazet if (skb_head_is_locked(from))
5800bad43ca8SEric Dumazet return false;
5801bad43ca8SEric Dumazet
5802bad43ca8SEric Dumazet delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
5803bad43ca8SEric Dumazet
5804bad43ca8SEric Dumazet page = virt_to_head_page(from->head);
5805bad43ca8SEric Dumazet offset = from->data - (unsigned char *)page_address(page);
5806bad43ca8SEric Dumazet
5807c818fa9eSEric Dumazet skb_fill_page_desc(to, to_shinfo->nr_frags,
5808bad43ca8SEric Dumazet page, offset, skb_headlen(from));
5809bad43ca8SEric Dumazet *fragstolen = true;
5810bad43ca8SEric Dumazet } else {
5811c818fa9eSEric Dumazet if (to_shinfo->nr_frags +
5812c818fa9eSEric Dumazet from_shinfo->nr_frags > MAX_SKB_FRAGS)
5813bad43ca8SEric Dumazet return false;
5814bad43ca8SEric Dumazet
5815f4b549a5SWeiping Pan delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from));
5816bad43ca8SEric Dumazet }
5817bad43ca8SEric Dumazet
5818bad43ca8SEric Dumazet WARN_ON_ONCE(delta < len);
5819bad43ca8SEric Dumazet
5820c818fa9eSEric Dumazet memcpy(to_shinfo->frags + to_shinfo->nr_frags,
5821c818fa9eSEric Dumazet from_shinfo->frags,
5822c818fa9eSEric Dumazet from_shinfo->nr_frags * sizeof(skb_frag_t));
5823c818fa9eSEric Dumazet to_shinfo->nr_frags += from_shinfo->nr_frags;
5824bad43ca8SEric Dumazet
5825bad43ca8SEric Dumazet if (!skb_cloned(from))
5826c818fa9eSEric Dumazet from_shinfo->nr_frags = 0;
5827bad43ca8SEric Dumazet
58288ea853fdSLi RongQing /* if the skb is not cloned this does nothing
58298ea853fdSLi RongQing * since we set nr_frags to 0.
58308ea853fdSLi RongQing */
5831c818fa9eSEric Dumazet for (i = 0; i < from_shinfo->nr_frags; i++)
5832c818fa9eSEric Dumazet __skb_frag_ref(&from_shinfo->frags[i]);
5833bad43ca8SEric Dumazet
5834bad43ca8SEric Dumazet to->truesize += delta;
5835bad43ca8SEric Dumazet to->len += len;
5836bad43ca8SEric Dumazet to->data_len += len;
5837bad43ca8SEric Dumazet
5838bad43ca8SEric Dumazet *delta_truesize = delta;
5839bad43ca8SEric Dumazet return true;
5840bad43ca8SEric Dumazet }
5841bad43ca8SEric Dumazet EXPORT_SYMBOL(skb_try_coalesce);
5842621e84d6SNicolas Dichtel
5843621e84d6SNicolas Dichtel /**
58448b27f277SNicolas Dichtel * skb_scrub_packet - scrub an skb
5845621e84d6SNicolas Dichtel *
5846621e84d6SNicolas Dichtel * @skb: buffer to clean
58478b27f277SNicolas Dichtel * @xnet: packet is crossing netns
5848621e84d6SNicolas Dichtel *
58498b27f277SNicolas Dichtel * skb_scrub_packet can be used after encapsulating or decapsulting a packet
58508b27f277SNicolas Dichtel * into/from a tunnel. Some information have to be cleared during these
58518b27f277SNicolas Dichtel * operations.
58528b27f277SNicolas Dichtel * skb_scrub_packet can also be used to clean a skb before injecting it in
58538b27f277SNicolas Dichtel * another namespace (@xnet == true). We have to clear all information in the
58548b27f277SNicolas Dichtel * skb that could impact namespace isolation.
5855621e84d6SNicolas Dichtel */
skb_scrub_packet(struct sk_buff * skb,bool xnet)58568b27f277SNicolas Dichtel void skb_scrub_packet(struct sk_buff *skb, bool xnet)
5857621e84d6SNicolas Dichtel {
5858621e84d6SNicolas Dichtel skb->pkt_type = PACKET_HOST;
5859621e84d6SNicolas Dichtel skb->skb_iif = 0;
586060ff7467SWANG Cong skb->ignore_df = 0;
5861621e84d6SNicolas Dichtel skb_dst_drop(skb);
5862174e2381SFlorian Westphal skb_ext_reset(skb);
5863895b5c9fSFlorian Westphal nf_reset_ct(skb);
5864621e84d6SNicolas Dichtel nf_reset_trace(skb);
5865213dd74aSHerbert Xu
58666f9a5069SPetr Machata #ifdef CONFIG_NET_SWITCHDEV
58676f9a5069SPetr Machata skb->offload_fwd_mark = 0;
5868875e8939SIdo Schimmel skb->offload_l3_fwd_mark = 0;
58696f9a5069SPetr Machata #endif
5870*abdbedfeSPhilo Lu ipvs_reset(skb);
58716f9a5069SPetr Machata
5872213dd74aSHerbert Xu if (!xnet)
5873213dd74aSHerbert Xu return;
5874213dd74aSHerbert Xu
5875213dd74aSHerbert Xu skb->mark = 0;
5876de799101SMartin KaFai Lau skb_clear_tstamp(skb);
5877621e84d6SNicolas Dichtel }
5878621e84d6SNicolas Dichtel EXPORT_SYMBOL_GPL(skb_scrub_packet);
5879de960aa9SFlorian Westphal
skb_reorder_vlan_header(struct sk_buff * skb)58800d5501c1SVlad Yasevich static struct sk_buff *skb_reorder_vlan_header(struct sk_buff *skb)
58810d5501c1SVlad Yasevich {
5882d85e8be2SYuya Kusakabe int mac_len, meta_len;
5883d85e8be2SYuya Kusakabe void *meta;
58844bbb3e0eSToshiaki Makita
58850d5501c1SVlad Yasevich if (skb_cow(skb, skb_headroom(skb)) < 0) {
58860d5501c1SVlad Yasevich kfree_skb(skb);
58870d5501c1SVlad Yasevich return NULL;
58880d5501c1SVlad Yasevich }
58890d5501c1SVlad Yasevich
58904bbb3e0eSToshiaki Makita mac_len = skb->data - skb_mac_header(skb);
5891ae474573SToshiaki Makita if (likely(mac_len > VLAN_HLEN + ETH_TLEN)) {
58924bbb3e0eSToshiaki Makita memmove(skb_mac_header(skb) + VLAN_HLEN, skb_mac_header(skb),
58934bbb3e0eSToshiaki Makita mac_len - VLAN_HLEN - ETH_TLEN);
5894ae474573SToshiaki Makita }
5895d85e8be2SYuya Kusakabe
5896d85e8be2SYuya Kusakabe meta_len = skb_metadata_len(skb);
5897d85e8be2SYuya Kusakabe if (meta_len) {
5898d85e8be2SYuya Kusakabe meta = skb_metadata_end(skb) - meta_len;
5899d85e8be2SYuya Kusakabe memmove(meta + VLAN_HLEN, meta, meta_len);
5900d85e8be2SYuya Kusakabe }
5901d85e8be2SYuya Kusakabe
59020d5501c1SVlad Yasevich skb->mac_header += VLAN_HLEN;
59030d5501c1SVlad Yasevich return skb;
59040d5501c1SVlad Yasevich }
59050d5501c1SVlad Yasevich
skb_vlan_untag(struct sk_buff * skb)59060d5501c1SVlad Yasevich struct sk_buff *skb_vlan_untag(struct sk_buff *skb)
59070d5501c1SVlad Yasevich {
59080d5501c1SVlad Yasevich struct vlan_hdr *vhdr;
59090d5501c1SVlad Yasevich u16 vlan_tci;
59100d5501c1SVlad Yasevich
5911df8a39deSJiri Pirko if (unlikely(skb_vlan_tag_present(skb))) {
59120d5501c1SVlad Yasevich /* vlan_tci is already set-up so leave this for another time */
59130d5501c1SVlad Yasevich return skb;
59140d5501c1SVlad Yasevich }
59150d5501c1SVlad Yasevich
59160d5501c1SVlad Yasevich skb = skb_share_check(skb, GFP_ATOMIC);
59170d5501c1SVlad Yasevich if (unlikely(!skb))
59180d5501c1SVlad Yasevich goto err_free;
591955eff0ebSMiaohe Lin /* We may access the two bytes after vlan_hdr in vlan_set_encap_proto(). */
592055eff0ebSMiaohe Lin if (unlikely(!pskb_may_pull(skb, VLAN_HLEN + sizeof(unsigned short))))
59210d5501c1SVlad Yasevich goto err_free;
59220d5501c1SVlad Yasevich
59230d5501c1SVlad Yasevich vhdr = (struct vlan_hdr *)skb->data;
59240d5501c1SVlad Yasevich vlan_tci = ntohs(vhdr->h_vlan_TCI);
59250d5501c1SVlad Yasevich __vlan_hwaccel_put_tag(skb, skb->protocol, vlan_tci);
59260d5501c1SVlad Yasevich
59270d5501c1SVlad Yasevich skb_pull_rcsum(skb, VLAN_HLEN);
59280d5501c1SVlad Yasevich vlan_set_encap_proto(skb, vhdr);
59290d5501c1SVlad Yasevich
59300d5501c1SVlad Yasevich skb = skb_reorder_vlan_header(skb);
59310d5501c1SVlad Yasevich if (unlikely(!skb))
59320d5501c1SVlad Yasevich goto err_free;
59330d5501c1SVlad Yasevich
59340d5501c1SVlad Yasevich skb_reset_network_header(skb);
59358be33ecfSAlexander Lobakin if (!skb_transport_header_was_set(skb))
59360d5501c1SVlad Yasevich skb_reset_transport_header(skb);
59370d5501c1SVlad Yasevich skb_reset_mac_len(skb);
59380d5501c1SVlad Yasevich
59390d5501c1SVlad Yasevich return skb;
59400d5501c1SVlad Yasevich
59410d5501c1SVlad Yasevich err_free:
59420d5501c1SVlad Yasevich kfree_skb(skb);
59430d5501c1SVlad Yasevich return NULL;
59440d5501c1SVlad Yasevich }
59450d5501c1SVlad Yasevich EXPORT_SYMBOL(skb_vlan_untag);
59462e4e4410SEric Dumazet
skb_ensure_writable(struct sk_buff * skb,unsigned int write_len)594792ece280SLiu Jian int skb_ensure_writable(struct sk_buff *skb, unsigned int write_len)
5948e2195121SJiri Pirko {
5949e2195121SJiri Pirko if (!pskb_may_pull(skb, write_len))
5950e2195121SJiri Pirko return -ENOMEM;
5951e2195121SJiri Pirko
5952e2195121SJiri Pirko if (!skb_cloned(skb) || skb_clone_writable(skb, write_len))
5953e2195121SJiri Pirko return 0;
5954e2195121SJiri Pirko
5955e2195121SJiri Pirko return pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
5956e2195121SJiri Pirko }
5957e2195121SJiri Pirko EXPORT_SYMBOL(skb_ensure_writable);
5958e2195121SJiri Pirko
5959bfca4c52SShmulik Ladkani /* remove VLAN header from packet and update csum accordingly.
5960bfca4c52SShmulik Ladkani * expects a non skb_vlan_tag_present skb with a vlan tag payload
5961bfca4c52SShmulik Ladkani */
__skb_vlan_pop(struct sk_buff * skb,u16 * vlan_tci)5962bfca4c52SShmulik Ladkani int __skb_vlan_pop(struct sk_buff *skb, u16 *vlan_tci)
596393515d53SJiri Pirko {
5964b6a79208SShmulik Ladkani int offset = skb->data - skb_mac_header(skb);
596593515d53SJiri Pirko int err;
596693515d53SJiri Pirko
5967b6a79208SShmulik Ladkani if (WARN_ONCE(offset,
5968b6a79208SShmulik Ladkani "__skb_vlan_pop got skb with skb->data not at mac header (offset %d)\n",
5969b6a79208SShmulik Ladkani offset)) {
5970b6a79208SShmulik Ladkani return -EINVAL;
5971b6a79208SShmulik Ladkani }
5972b6a79208SShmulik Ladkani
597393515d53SJiri Pirko err = skb_ensure_writable(skb, VLAN_ETH_HLEN);
597493515d53SJiri Pirko if (unlikely(err))
5975b6a79208SShmulik Ladkani return err;
597693515d53SJiri Pirko
597793515d53SJiri Pirko skb_postpull_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
597893515d53SJiri Pirko
59790bcf2e4aSVladimir Oltean vlan_remove_tag(skb, vlan_tci);
598093515d53SJiri Pirko
598193515d53SJiri Pirko skb->mac_header += VLAN_HLEN;
598293515d53SJiri Pirko
598393515d53SJiri Pirko if (skb_network_offset(skb) < ETH_HLEN)
598493515d53SJiri Pirko skb_set_network_header(skb, ETH_HLEN);
598593515d53SJiri Pirko
598693515d53SJiri Pirko skb_reset_mac_len(skb);
598793515d53SJiri Pirko
598893515d53SJiri Pirko return err;
598993515d53SJiri Pirko }
5990bfca4c52SShmulik Ladkani EXPORT_SYMBOL(__skb_vlan_pop);
599193515d53SJiri Pirko
5992b6a79208SShmulik Ladkani /* Pop a vlan tag either from hwaccel or from payload.
5993b6a79208SShmulik Ladkani * Expects skb->data at mac header.
5994b6a79208SShmulik Ladkani */
skb_vlan_pop(struct sk_buff * skb)599593515d53SJiri Pirko int skb_vlan_pop(struct sk_buff *skb)
599693515d53SJiri Pirko {
599793515d53SJiri Pirko u16 vlan_tci;
599893515d53SJiri Pirko __be16 vlan_proto;
599993515d53SJiri Pirko int err;
600093515d53SJiri Pirko
6001df8a39deSJiri Pirko if (likely(skb_vlan_tag_present(skb))) {
6002b1817524SMichał Mirosław __vlan_hwaccel_clear_tag(skb);
600393515d53SJiri Pirko } else {
6004ecf4ee41SShmulik Ladkani if (unlikely(!eth_type_vlan(skb->protocol)))
600593515d53SJiri Pirko return 0;
600693515d53SJiri Pirko
600793515d53SJiri Pirko err = __skb_vlan_pop(skb, &vlan_tci);
600893515d53SJiri Pirko if (err)
600993515d53SJiri Pirko return err;
601093515d53SJiri Pirko }
601193515d53SJiri Pirko /* move next vlan tag to hw accel tag */
6012ecf4ee41SShmulik Ladkani if (likely(!eth_type_vlan(skb->protocol)))
601393515d53SJiri Pirko return 0;
601493515d53SJiri Pirko
601593515d53SJiri Pirko vlan_proto = skb->protocol;
601693515d53SJiri Pirko err = __skb_vlan_pop(skb, &vlan_tci);
601793515d53SJiri Pirko if (unlikely(err))
601893515d53SJiri Pirko return err;
601993515d53SJiri Pirko
602093515d53SJiri Pirko __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
602193515d53SJiri Pirko return 0;
602293515d53SJiri Pirko }
602393515d53SJiri Pirko EXPORT_SYMBOL(skb_vlan_pop);
602493515d53SJiri Pirko
6025b6a79208SShmulik Ladkani /* Push a vlan tag either into hwaccel or into payload (if hwaccel tag present).
6026b6a79208SShmulik Ladkani * Expects skb->data at mac header.
6027b6a79208SShmulik Ladkani */
skb_vlan_push(struct sk_buff * skb,__be16 vlan_proto,u16 vlan_tci)602893515d53SJiri Pirko int skb_vlan_push(struct sk_buff *skb, __be16 vlan_proto, u16 vlan_tci)
602993515d53SJiri Pirko {
6030df8a39deSJiri Pirko if (skb_vlan_tag_present(skb)) {
6031b6a79208SShmulik Ladkani int offset = skb->data - skb_mac_header(skb);
603293515d53SJiri Pirko int err;
603393515d53SJiri Pirko
6034b6a79208SShmulik Ladkani if (WARN_ONCE(offset,
6035b6a79208SShmulik Ladkani "skb_vlan_push got skb with skb->data not at mac header (offset %d)\n",
6036b6a79208SShmulik Ladkani offset)) {
6037b6a79208SShmulik Ladkani return -EINVAL;
6038b6a79208SShmulik Ladkani }
6039b6a79208SShmulik Ladkani
604093515d53SJiri Pirko err = __vlan_insert_tag(skb, skb->vlan_proto,
6041df8a39deSJiri Pirko skb_vlan_tag_get(skb));
6042b6a79208SShmulik Ladkani if (err)
604393515d53SJiri Pirko return err;
60449241e2dfSDaniel Borkmann
604593515d53SJiri Pirko skb->protocol = skb->vlan_proto;
604693515d53SJiri Pirko skb->mac_len += VLAN_HLEN;
604793515d53SJiri Pirko
60486b83d28aSDaniel Borkmann skb_postpush_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
604993515d53SJiri Pirko }
605093515d53SJiri Pirko __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
605193515d53SJiri Pirko return 0;
605293515d53SJiri Pirko }
605393515d53SJiri Pirko EXPORT_SYMBOL(skb_vlan_push);
605493515d53SJiri Pirko
605519fbcb36SGuillaume Nault /**
605619fbcb36SGuillaume Nault * skb_eth_pop() - Drop the Ethernet header at the head of a packet
605719fbcb36SGuillaume Nault *
605819fbcb36SGuillaume Nault * @skb: Socket buffer to modify
605919fbcb36SGuillaume Nault *
606019fbcb36SGuillaume Nault * Drop the Ethernet header of @skb.
606119fbcb36SGuillaume Nault *
606219fbcb36SGuillaume Nault * Expects that skb->data points to the mac header and that no VLAN tags are
606319fbcb36SGuillaume Nault * present.
606419fbcb36SGuillaume Nault *
606519fbcb36SGuillaume Nault * Returns 0 on success, -errno otherwise.
606619fbcb36SGuillaume Nault */
skb_eth_pop(struct sk_buff * skb)606719fbcb36SGuillaume Nault int skb_eth_pop(struct sk_buff *skb)
606819fbcb36SGuillaume Nault {
606919fbcb36SGuillaume Nault if (!pskb_may_pull(skb, ETH_HLEN) || skb_vlan_tagged(skb) ||
607019fbcb36SGuillaume Nault skb_network_offset(skb) < ETH_HLEN)
607119fbcb36SGuillaume Nault return -EPROTO;
607219fbcb36SGuillaume Nault
607319fbcb36SGuillaume Nault skb_pull_rcsum(skb, ETH_HLEN);
607419fbcb36SGuillaume Nault skb_reset_mac_header(skb);
607519fbcb36SGuillaume Nault skb_reset_mac_len(skb);
607619fbcb36SGuillaume Nault
607719fbcb36SGuillaume Nault return 0;
607819fbcb36SGuillaume Nault }
607919fbcb36SGuillaume Nault EXPORT_SYMBOL(skb_eth_pop);
608019fbcb36SGuillaume Nault
608119fbcb36SGuillaume Nault /**
608219fbcb36SGuillaume Nault * skb_eth_push() - Add a new Ethernet header at the head of a packet
608319fbcb36SGuillaume Nault *
608419fbcb36SGuillaume Nault * @skb: Socket buffer to modify
608519fbcb36SGuillaume Nault * @dst: Destination MAC address of the new header
608619fbcb36SGuillaume Nault * @src: Source MAC address of the new header
608719fbcb36SGuillaume Nault *
608819fbcb36SGuillaume Nault * Prepend @skb with a new Ethernet header.
608919fbcb36SGuillaume Nault *
609019fbcb36SGuillaume Nault * Expects that skb->data points to the mac header, which must be empty.
609119fbcb36SGuillaume Nault *
609219fbcb36SGuillaume Nault * Returns 0 on success, -errno otherwise.
609319fbcb36SGuillaume Nault */
skb_eth_push(struct sk_buff * skb,const unsigned char * dst,const unsigned char * src)609419fbcb36SGuillaume Nault int skb_eth_push(struct sk_buff *skb, const unsigned char *dst,
609519fbcb36SGuillaume Nault const unsigned char *src)
609619fbcb36SGuillaume Nault {
609719fbcb36SGuillaume Nault struct ethhdr *eth;
609819fbcb36SGuillaume Nault int err;
609919fbcb36SGuillaume Nault
610019fbcb36SGuillaume Nault if (skb_network_offset(skb) || skb_vlan_tag_present(skb))
610119fbcb36SGuillaume Nault return -EPROTO;
610219fbcb36SGuillaume Nault
610319fbcb36SGuillaume Nault err = skb_cow_head(skb, sizeof(*eth));
610419fbcb36SGuillaume Nault if (err < 0)
610519fbcb36SGuillaume Nault return err;
610619fbcb36SGuillaume Nault
610719fbcb36SGuillaume Nault skb_push(skb, sizeof(*eth));
610819fbcb36SGuillaume Nault skb_reset_mac_header(skb);
610919fbcb36SGuillaume Nault skb_reset_mac_len(skb);
611019fbcb36SGuillaume Nault
611119fbcb36SGuillaume Nault eth = eth_hdr(skb);
611219fbcb36SGuillaume Nault ether_addr_copy(eth->h_dest, dst);
611319fbcb36SGuillaume Nault ether_addr_copy(eth->h_source, src);
611419fbcb36SGuillaume Nault eth->h_proto = skb->protocol;
611519fbcb36SGuillaume Nault
611619fbcb36SGuillaume Nault skb_postpush_rcsum(skb, eth, sizeof(*eth));
611719fbcb36SGuillaume Nault
611819fbcb36SGuillaume Nault return 0;
611919fbcb36SGuillaume Nault }
612019fbcb36SGuillaume Nault EXPORT_SYMBOL(skb_eth_push);
612119fbcb36SGuillaume Nault
61228822e270SJohn Hurley /* Update the ethertype of hdr and the skb csum value if required. */
skb_mod_eth_type(struct sk_buff * skb,struct ethhdr * hdr,__be16 ethertype)61238822e270SJohn Hurley static void skb_mod_eth_type(struct sk_buff *skb, struct ethhdr *hdr,
61248822e270SJohn Hurley __be16 ethertype)
61258822e270SJohn Hurley {
61268822e270SJohn Hurley if (skb->ip_summed == CHECKSUM_COMPLETE) {
61278822e270SJohn Hurley __be16 diff[] = { ~hdr->h_proto, ethertype };
61288822e270SJohn Hurley
61298822e270SJohn Hurley skb->csum = csum_partial((char *)diff, sizeof(diff), skb->csum);
61308822e270SJohn Hurley }
61318822e270SJohn Hurley
61328822e270SJohn Hurley hdr->h_proto = ethertype;
61338822e270SJohn Hurley }
61348822e270SJohn Hurley
61358822e270SJohn Hurley /**
6136e7dbfed1SMartin Varghese * skb_mpls_push() - push a new MPLS header after mac_len bytes from start of
6137e7dbfed1SMartin Varghese * the packet
61388822e270SJohn Hurley *
61398822e270SJohn Hurley * @skb: buffer
61408822e270SJohn Hurley * @mpls_lse: MPLS label stack entry to push
61418822e270SJohn Hurley * @mpls_proto: ethertype of the new MPLS header (expects 0x8847 or 0x8848)
6142fa4e0f88SDavide Caratti * @mac_len: length of the MAC header
6143e7dbfed1SMartin Varghese * @ethernet: flag to indicate if the resulting packet after skb_mpls_push is
6144e7dbfed1SMartin Varghese * ethernet
61458822e270SJohn Hurley *
61468822e270SJohn Hurley * Expects skb->data at mac header.
61478822e270SJohn Hurley *
61488822e270SJohn Hurley * Returns 0 on success, -errno otherwise.
61498822e270SJohn Hurley */
skb_mpls_push(struct sk_buff * skb,__be32 mpls_lse,__be16 mpls_proto,int mac_len,bool ethernet)6150fa4e0f88SDavide Caratti int skb_mpls_push(struct sk_buff *skb, __be32 mpls_lse, __be16 mpls_proto,
6151d04ac224SMartin Varghese int mac_len, bool ethernet)
61528822e270SJohn Hurley {
61538822e270SJohn Hurley struct mpls_shim_hdr *lse;
61548822e270SJohn Hurley int err;
61558822e270SJohn Hurley
61568822e270SJohn Hurley if (unlikely(!eth_p_mpls(mpls_proto)))
61578822e270SJohn Hurley return -EINVAL;
61588822e270SJohn Hurley
61598822e270SJohn Hurley /* Networking stack does not allow simultaneous Tunnel and MPLS GSO. */
61608822e270SJohn Hurley if (skb->encapsulation)
61618822e270SJohn Hurley return -EINVAL;
61628822e270SJohn Hurley
61638822e270SJohn Hurley err = skb_cow_head(skb, MPLS_HLEN);
61648822e270SJohn Hurley if (unlikely(err))
61658822e270SJohn Hurley return err;
61668822e270SJohn Hurley
61678822e270SJohn Hurley if (!skb->inner_protocol) {
6168e7dbfed1SMartin Varghese skb_set_inner_network_header(skb, skb_network_offset(skb));
61698822e270SJohn Hurley skb_set_inner_protocol(skb, skb->protocol);
61708822e270SJohn Hurley }
61718822e270SJohn Hurley
61728822e270SJohn Hurley skb_push(skb, MPLS_HLEN);
61738822e270SJohn Hurley memmove(skb_mac_header(skb) - MPLS_HLEN, skb_mac_header(skb),
6174fa4e0f88SDavide Caratti mac_len);
61758822e270SJohn Hurley skb_reset_mac_header(skb);
6176fa4e0f88SDavide Caratti skb_set_network_header(skb, mac_len);
6177e7dbfed1SMartin Varghese skb_reset_mac_len(skb);
61788822e270SJohn Hurley
61798822e270SJohn Hurley lse = mpls_hdr(skb);
61808822e270SJohn Hurley lse->label_stack_entry = mpls_lse;
61818822e270SJohn Hurley skb_postpush_rcsum(skb, lse, MPLS_HLEN);
61828822e270SJohn Hurley
61834296adc3SGuillaume Nault if (ethernet && mac_len >= ETH_HLEN)
61848822e270SJohn Hurley skb_mod_eth_type(skb, eth_hdr(skb), mpls_proto);
61858822e270SJohn Hurley skb->protocol = mpls_proto;
61868822e270SJohn Hurley
61878822e270SJohn Hurley return 0;
61888822e270SJohn Hurley }
61898822e270SJohn Hurley EXPORT_SYMBOL_GPL(skb_mpls_push);
61908822e270SJohn Hurley
61912e4e4410SEric Dumazet /**
6192ed246ceeSJohn Hurley * skb_mpls_pop() - pop the outermost MPLS header
6193ed246ceeSJohn Hurley *
6194ed246ceeSJohn Hurley * @skb: buffer
6195ed246ceeSJohn Hurley * @next_proto: ethertype of header after popped MPLS header
6196fa4e0f88SDavide Caratti * @mac_len: length of the MAC header
619776f99f98SMartin Varghese * @ethernet: flag to indicate if the packet is ethernet
6198ed246ceeSJohn Hurley *
6199ed246ceeSJohn Hurley * Expects skb->data at mac header.
6200ed246ceeSJohn Hurley *
6201ed246ceeSJohn Hurley * Returns 0 on success, -errno otherwise.
6202ed246ceeSJohn Hurley */
skb_mpls_pop(struct sk_buff * skb,__be16 next_proto,int mac_len,bool ethernet)6203040b5cfbSMartin Varghese int skb_mpls_pop(struct sk_buff *skb, __be16 next_proto, int mac_len,
6204040b5cfbSMartin Varghese bool ethernet)
6205ed246ceeSJohn Hurley {
6206ed246ceeSJohn Hurley int err;
6207ed246ceeSJohn Hurley
6208ed246ceeSJohn Hurley if (unlikely(!eth_p_mpls(skb->protocol)))
6209dedc5a08SDavide Caratti return 0;
6210ed246ceeSJohn Hurley
6211fa4e0f88SDavide Caratti err = skb_ensure_writable(skb, mac_len + MPLS_HLEN);
6212ed246ceeSJohn Hurley if (unlikely(err))
6213ed246ceeSJohn Hurley return err;
6214ed246ceeSJohn Hurley
6215ed246ceeSJohn Hurley skb_postpull_rcsum(skb, mpls_hdr(skb), MPLS_HLEN);
6216ed246ceeSJohn Hurley memmove(skb_mac_header(skb) + MPLS_HLEN, skb_mac_header(skb),
6217fa4e0f88SDavide Caratti mac_len);
6218ed246ceeSJohn Hurley
6219ed246ceeSJohn Hurley __skb_pull(skb, MPLS_HLEN);
6220ed246ceeSJohn Hurley skb_reset_mac_header(skb);
6221fa4e0f88SDavide Caratti skb_set_network_header(skb, mac_len);
6222ed246ceeSJohn Hurley
62234296adc3SGuillaume Nault if (ethernet && mac_len >= ETH_HLEN) {
6224ed246ceeSJohn Hurley struct ethhdr *hdr;
6225ed246ceeSJohn Hurley
6226ed246ceeSJohn Hurley /* use mpls_hdr() to get ethertype to account for VLANs. */
6227ed246ceeSJohn Hurley hdr = (struct ethhdr *)((void *)mpls_hdr(skb) - ETH_HLEN);
6228ed246ceeSJohn Hurley skb_mod_eth_type(skb, hdr, next_proto);
6229ed246ceeSJohn Hurley }
6230ed246ceeSJohn Hurley skb->protocol = next_proto;
6231ed246ceeSJohn Hurley
6232ed246ceeSJohn Hurley return 0;
6233ed246ceeSJohn Hurley }
6234ed246ceeSJohn Hurley EXPORT_SYMBOL_GPL(skb_mpls_pop);
6235ed246ceeSJohn Hurley
6236ed246ceeSJohn Hurley /**
6237d27cf5c5SJohn Hurley * skb_mpls_update_lse() - modify outermost MPLS header and update csum
6238d27cf5c5SJohn Hurley *
6239d27cf5c5SJohn Hurley * @skb: buffer
6240d27cf5c5SJohn Hurley * @mpls_lse: new MPLS label stack entry to update to
6241d27cf5c5SJohn Hurley *
6242d27cf5c5SJohn Hurley * Expects skb->data at mac header.
6243d27cf5c5SJohn Hurley *
6244d27cf5c5SJohn Hurley * Returns 0 on success, -errno otherwise.
6245d27cf5c5SJohn Hurley */
skb_mpls_update_lse(struct sk_buff * skb,__be32 mpls_lse)6246d27cf5c5SJohn Hurley int skb_mpls_update_lse(struct sk_buff *skb, __be32 mpls_lse)
6247d27cf5c5SJohn Hurley {
6248d27cf5c5SJohn Hurley int err;
6249d27cf5c5SJohn Hurley
6250d27cf5c5SJohn Hurley if (unlikely(!eth_p_mpls(skb->protocol)))
6251d27cf5c5SJohn Hurley return -EINVAL;
6252d27cf5c5SJohn Hurley
6253d27cf5c5SJohn Hurley err = skb_ensure_writable(skb, skb->mac_len + MPLS_HLEN);
6254d27cf5c5SJohn Hurley if (unlikely(err))
6255d27cf5c5SJohn Hurley return err;
6256d27cf5c5SJohn Hurley
6257d27cf5c5SJohn Hurley if (skb->ip_summed == CHECKSUM_COMPLETE) {
6258d27cf5c5SJohn Hurley __be32 diff[] = { ~mpls_hdr(skb)->label_stack_entry, mpls_lse };
6259d27cf5c5SJohn Hurley
6260d27cf5c5SJohn Hurley skb->csum = csum_partial((char *)diff, sizeof(diff), skb->csum);
6261d27cf5c5SJohn Hurley }
6262d27cf5c5SJohn Hurley
6263d27cf5c5SJohn Hurley mpls_hdr(skb)->label_stack_entry = mpls_lse;
6264d27cf5c5SJohn Hurley
6265d27cf5c5SJohn Hurley return 0;
6266d27cf5c5SJohn Hurley }
6267d27cf5c5SJohn Hurley EXPORT_SYMBOL_GPL(skb_mpls_update_lse);
6268d27cf5c5SJohn Hurley
6269d27cf5c5SJohn Hurley /**
62702a2ea508SJohn Hurley * skb_mpls_dec_ttl() - decrement the TTL of the outermost MPLS header
62712a2ea508SJohn Hurley *
62722a2ea508SJohn Hurley * @skb: buffer
62732a2ea508SJohn Hurley *
62742a2ea508SJohn Hurley * Expects skb->data at mac header.
62752a2ea508SJohn Hurley *
62762a2ea508SJohn Hurley * Returns 0 on success, -errno otherwise.
62772a2ea508SJohn Hurley */
skb_mpls_dec_ttl(struct sk_buff * skb)62782a2ea508SJohn Hurley int skb_mpls_dec_ttl(struct sk_buff *skb)
62792a2ea508SJohn Hurley {
62802a2ea508SJohn Hurley u32 lse;
62812a2ea508SJohn Hurley u8 ttl;
62822a2ea508SJohn Hurley
62832a2ea508SJohn Hurley if (unlikely(!eth_p_mpls(skb->protocol)))
62842a2ea508SJohn Hurley return -EINVAL;
62852a2ea508SJohn Hurley
628613de4ed9SDavide Caratti if (!pskb_may_pull(skb, skb_network_offset(skb) + MPLS_HLEN))
628713de4ed9SDavide Caratti return -ENOMEM;
628813de4ed9SDavide Caratti
62892a2ea508SJohn Hurley lse = be32_to_cpu(mpls_hdr(skb)->label_stack_entry);
62902a2ea508SJohn Hurley ttl = (lse & MPLS_LS_TTL_MASK) >> MPLS_LS_TTL_SHIFT;
62912a2ea508SJohn Hurley if (!--ttl)
62922a2ea508SJohn Hurley return -EINVAL;
62932a2ea508SJohn Hurley
62942a2ea508SJohn Hurley lse &= ~MPLS_LS_TTL_MASK;
62952a2ea508SJohn Hurley lse |= ttl << MPLS_LS_TTL_SHIFT;
62962a2ea508SJohn Hurley
62972a2ea508SJohn Hurley return skb_mpls_update_lse(skb, cpu_to_be32(lse));
62982a2ea508SJohn Hurley }
62992a2ea508SJohn Hurley EXPORT_SYMBOL_GPL(skb_mpls_dec_ttl);
63002a2ea508SJohn Hurley
63012a2ea508SJohn Hurley /**
63022e4e4410SEric Dumazet * alloc_skb_with_frags - allocate skb with page frags
63032e4e4410SEric Dumazet *
6304de3f0d0eSMasanari Iida * @header_len: size of linear part
6305de3f0d0eSMasanari Iida * @data_len: needed length in frags
630609c2c907SEric Dumazet * @order: max page order desired.
6307de3f0d0eSMasanari Iida * @errcode: pointer to error code if any
6308de3f0d0eSMasanari Iida * @gfp_mask: allocation mask
63092e4e4410SEric Dumazet *
63102e4e4410SEric Dumazet * This can be used to allocate a paged skb, given a maximal order for frags.
63112e4e4410SEric Dumazet */
alloc_skb_with_frags(unsigned long header_len,unsigned long data_len,int order,int * errcode,gfp_t gfp_mask)63122e4e4410SEric Dumazet struct sk_buff *alloc_skb_with_frags(unsigned long header_len,
63132e4e4410SEric Dumazet unsigned long data_len,
631409c2c907SEric Dumazet int order,
63152e4e4410SEric Dumazet int *errcode,
63162e4e4410SEric Dumazet gfp_t gfp_mask)
63172e4e4410SEric Dumazet {
63182e4e4410SEric Dumazet unsigned long chunk;
63192e4e4410SEric Dumazet struct sk_buff *skb;
63202e4e4410SEric Dumazet struct page *page;
632109c2c907SEric Dumazet int nr_frags = 0;
63222e4e4410SEric Dumazet
63232e4e4410SEric Dumazet *errcode = -EMSGSIZE;
632409c2c907SEric Dumazet if (unlikely(data_len > MAX_SKB_FRAGS * (PAGE_SIZE << order)))
63252e4e4410SEric Dumazet return NULL;
63262e4e4410SEric Dumazet
63272e4e4410SEric Dumazet *errcode = -ENOBUFS;
6328f8c468e8SDavid Rientjes skb = alloc_skb(header_len, gfp_mask);
63292e4e4410SEric Dumazet if (!skb)
63302e4e4410SEric Dumazet return NULL;
63312e4e4410SEric Dumazet
633209c2c907SEric Dumazet while (data_len) {
633309c2c907SEric Dumazet if (nr_frags == MAX_SKB_FRAGS - 1)
633409c2c907SEric Dumazet goto failure;
633509c2c907SEric Dumazet while (order && PAGE_ALIGN(data_len) < (PAGE_SIZE << order))
633609c2c907SEric Dumazet order--;
63372e4e4410SEric Dumazet
633809c2c907SEric Dumazet if (order) {
6339d0164adcSMel Gorman page = alloc_pages((gfp_mask & ~__GFP_DIRECT_RECLAIM) |
63402e4e4410SEric Dumazet __GFP_COMP |
6341d14b56f5SMichal Hocko __GFP_NOWARN,
63422e4e4410SEric Dumazet order);
634309c2c907SEric Dumazet if (!page) {
63442e4e4410SEric Dumazet order--;
634509c2c907SEric Dumazet continue;
63462e4e4410SEric Dumazet }
634709c2c907SEric Dumazet } else {
63482e4e4410SEric Dumazet page = alloc_page(gfp_mask);
63492e4e4410SEric Dumazet if (!page)
63502e4e4410SEric Dumazet goto failure;
635109c2c907SEric Dumazet }
63522e4e4410SEric Dumazet chunk = min_t(unsigned long, data_len,
63532e4e4410SEric Dumazet PAGE_SIZE << order);
635409c2c907SEric Dumazet skb_fill_page_desc(skb, nr_frags, page, 0, chunk);
635509c2c907SEric Dumazet nr_frags++;
635609c2c907SEric Dumazet skb->truesize += (PAGE_SIZE << order);
63572e4e4410SEric Dumazet data_len -= chunk;
63582e4e4410SEric Dumazet }
63592e4e4410SEric Dumazet return skb;
63602e4e4410SEric Dumazet
63612e4e4410SEric Dumazet failure:
63622e4e4410SEric Dumazet kfree_skb(skb);
63632e4e4410SEric Dumazet return NULL;
63642e4e4410SEric Dumazet }
63652e4e4410SEric Dumazet EXPORT_SYMBOL(alloc_skb_with_frags);
63666fa01ccdSSowmini Varadhan
63676fa01ccdSSowmini Varadhan /* carve out the first off bytes from skb when off < headlen */
pskb_carve_inside_header(struct sk_buff * skb,const u32 off,const int headlen,gfp_t gfp_mask)63686fa01ccdSSowmini Varadhan static int pskb_carve_inside_header(struct sk_buff *skb, const u32 off,
63696fa01ccdSSowmini Varadhan const int headlen, gfp_t gfp_mask)
63706fa01ccdSSowmini Varadhan {
63716fa01ccdSSowmini Varadhan int i;
637212d6c1d3SKees Cook unsigned int size = skb_end_offset(skb);
63736fa01ccdSSowmini Varadhan int new_hlen = headlen - off;
63746fa01ccdSSowmini Varadhan u8 *data;
63756fa01ccdSSowmini Varadhan
63766fa01ccdSSowmini Varadhan if (skb_pfmemalloc(skb))
63776fa01ccdSSowmini Varadhan gfp_mask |= __GFP_MEMALLOC;
637812d6c1d3SKees Cook
63795c0e820cSEric Dumazet data = kmalloc_reserve(&size, gfp_mask, NUMA_NO_NODE, NULL);
63806fa01ccdSSowmini Varadhan if (!data)
63816fa01ccdSSowmini Varadhan return -ENOMEM;
638212d6c1d3SKees Cook size = SKB_WITH_OVERHEAD(size);
63836fa01ccdSSowmini Varadhan
63846fa01ccdSSowmini Varadhan /* Copy real data, and all frags */
63856fa01ccdSSowmini Varadhan skb_copy_from_linear_data_offset(skb, off, data, new_hlen);
63866fa01ccdSSowmini Varadhan skb->len -= off;
63876fa01ccdSSowmini Varadhan
63886fa01ccdSSowmini Varadhan memcpy((struct skb_shared_info *)(data + size),
63896fa01ccdSSowmini Varadhan skb_shinfo(skb),
63906fa01ccdSSowmini Varadhan offsetof(struct skb_shared_info,
63916fa01ccdSSowmini Varadhan frags[skb_shinfo(skb)->nr_frags]));
63926fa01ccdSSowmini Varadhan if (skb_cloned(skb)) {
63936fa01ccdSSowmini Varadhan /* drop the old head gracefully */
63946fa01ccdSSowmini Varadhan if (skb_orphan_frags(skb, gfp_mask)) {
6395bf9f1baaSEric Dumazet skb_kfree_head(data, size);
63966fa01ccdSSowmini Varadhan return -ENOMEM;
63976fa01ccdSSowmini Varadhan }
63986fa01ccdSSowmini Varadhan for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
63996fa01ccdSSowmini Varadhan skb_frag_ref(skb, i);
64006fa01ccdSSowmini Varadhan if (skb_has_frag_list(skb))
64016fa01ccdSSowmini Varadhan skb_clone_fraglist(skb);
6402b07a2d97SJakub Kicinski skb_release_data(skb, SKB_CONSUMED, false);
64036fa01ccdSSowmini Varadhan } else {
64046fa01ccdSSowmini Varadhan /* we can reuse existing recount- all we did was
64056fa01ccdSSowmini Varadhan * relocate values
64066fa01ccdSSowmini Varadhan */
6407b07a2d97SJakub Kicinski skb_free_head(skb, false);
64086fa01ccdSSowmini Varadhan }
64096fa01ccdSSowmini Varadhan
64106fa01ccdSSowmini Varadhan skb->head = data;
64116fa01ccdSSowmini Varadhan skb->data = data;
64126fa01ccdSSowmini Varadhan skb->head_frag = 0;
6413763087daSEric Dumazet skb_set_end_offset(skb, size);
64146fa01ccdSSowmini Varadhan skb_set_tail_pointer(skb, skb_headlen(skb));
64156fa01ccdSSowmini Varadhan skb_headers_offset_update(skb, 0);
64166fa01ccdSSowmini Varadhan skb->cloned = 0;
64176fa01ccdSSowmini Varadhan skb->hdr_len = 0;
64186fa01ccdSSowmini Varadhan skb->nohdr = 0;
64196fa01ccdSSowmini Varadhan atomic_set(&skb_shinfo(skb)->dataref, 1);
64206fa01ccdSSowmini Varadhan
64216fa01ccdSSowmini Varadhan return 0;
64226fa01ccdSSowmini Varadhan }
64236fa01ccdSSowmini Varadhan
64246fa01ccdSSowmini Varadhan static int pskb_carve(struct sk_buff *skb, const u32 off, gfp_t gfp);
64256fa01ccdSSowmini Varadhan
64266fa01ccdSSowmini Varadhan /* carve out the first eat bytes from skb's frag_list. May recurse into
64276fa01ccdSSowmini Varadhan * pskb_carve()
64286fa01ccdSSowmini Varadhan */
pskb_carve_frag_list(struct sk_buff * skb,struct skb_shared_info * shinfo,int eat,gfp_t gfp_mask)64296fa01ccdSSowmini Varadhan static int pskb_carve_frag_list(struct sk_buff *skb,
64306fa01ccdSSowmini Varadhan struct skb_shared_info *shinfo, int eat,
64316fa01ccdSSowmini Varadhan gfp_t gfp_mask)
64326fa01ccdSSowmini Varadhan {
64336fa01ccdSSowmini Varadhan struct sk_buff *list = shinfo->frag_list;
64346fa01ccdSSowmini Varadhan struct sk_buff *clone = NULL;
64356fa01ccdSSowmini Varadhan struct sk_buff *insp = NULL;
64366fa01ccdSSowmini Varadhan
64376fa01ccdSSowmini Varadhan do {
64386fa01ccdSSowmini Varadhan if (!list) {
64396fa01ccdSSowmini Varadhan pr_err("Not enough bytes to eat. Want %d\n", eat);
64406fa01ccdSSowmini Varadhan return -EFAULT;
64416fa01ccdSSowmini Varadhan }
64426fa01ccdSSowmini Varadhan if (list->len <= eat) {
64436fa01ccdSSowmini Varadhan /* Eaten as whole. */
64446fa01ccdSSowmini Varadhan eat -= list->len;
64456fa01ccdSSowmini Varadhan list = list->next;
64466fa01ccdSSowmini Varadhan insp = list;
64476fa01ccdSSowmini Varadhan } else {
64486fa01ccdSSowmini Varadhan /* Eaten partially. */
64496fa01ccdSSowmini Varadhan if (skb_shared(list)) {
64506fa01ccdSSowmini Varadhan clone = skb_clone(list, gfp_mask);
64516fa01ccdSSowmini Varadhan if (!clone)
64526fa01ccdSSowmini Varadhan return -ENOMEM;
64536fa01ccdSSowmini Varadhan insp = list->next;
64546fa01ccdSSowmini Varadhan list = clone;
64556fa01ccdSSowmini Varadhan } else {
64566fa01ccdSSowmini Varadhan /* This may be pulled without problems. */
64576fa01ccdSSowmini Varadhan insp = list;
64586fa01ccdSSowmini Varadhan }
64596fa01ccdSSowmini Varadhan if (pskb_carve(list, eat, gfp_mask) < 0) {
64606fa01ccdSSowmini Varadhan kfree_skb(clone);
64616fa01ccdSSowmini Varadhan return -ENOMEM;
64626fa01ccdSSowmini Varadhan }
64636fa01ccdSSowmini Varadhan break;
64646fa01ccdSSowmini Varadhan }
64656fa01ccdSSowmini Varadhan } while (eat);
64666fa01ccdSSowmini Varadhan
64676fa01ccdSSowmini Varadhan /* Free pulled out fragments. */
64686fa01ccdSSowmini Varadhan while ((list = shinfo->frag_list) != insp) {
64696fa01ccdSSowmini Varadhan shinfo->frag_list = list->next;
6470ef527f96SEric Dumazet consume_skb(list);
64716fa01ccdSSowmini Varadhan }
64726fa01ccdSSowmini Varadhan /* And insert new clone at head. */
64736fa01ccdSSowmini Varadhan if (clone) {
64746fa01ccdSSowmini Varadhan clone->next = list;
64756fa01ccdSSowmini Varadhan shinfo->frag_list = clone;
64766fa01ccdSSowmini Varadhan }
64776fa01ccdSSowmini Varadhan return 0;
64786fa01ccdSSowmini Varadhan }
64796fa01ccdSSowmini Varadhan
64806fa01ccdSSowmini Varadhan /* carve off first len bytes from skb. Split line (off) is in the
64816fa01ccdSSowmini Varadhan * non-linear part of skb
64826fa01ccdSSowmini Varadhan */
pskb_carve_inside_nonlinear(struct sk_buff * skb,const u32 off,int pos,gfp_t gfp_mask)64836fa01ccdSSowmini Varadhan static int pskb_carve_inside_nonlinear(struct sk_buff *skb, const u32 off,
64846fa01ccdSSowmini Varadhan int pos, gfp_t gfp_mask)
64856fa01ccdSSowmini Varadhan {
64866fa01ccdSSowmini Varadhan int i, k = 0;
648712d6c1d3SKees Cook unsigned int size = skb_end_offset(skb);
64886fa01ccdSSowmini Varadhan u8 *data;
64896fa01ccdSSowmini Varadhan const int nfrags = skb_shinfo(skb)->nr_frags;
64906fa01ccdSSowmini Varadhan struct skb_shared_info *shinfo;
64916fa01ccdSSowmini Varadhan
64926fa01ccdSSowmini Varadhan if (skb_pfmemalloc(skb))
64936fa01ccdSSowmini Varadhan gfp_mask |= __GFP_MEMALLOC;
649412d6c1d3SKees Cook
64955c0e820cSEric Dumazet data = kmalloc_reserve(&size, gfp_mask, NUMA_NO_NODE, NULL);
64966fa01ccdSSowmini Varadhan if (!data)
64976fa01ccdSSowmini Varadhan return -ENOMEM;
649812d6c1d3SKees Cook size = SKB_WITH_OVERHEAD(size);
64996fa01ccdSSowmini Varadhan
65006fa01ccdSSowmini Varadhan memcpy((struct skb_shared_info *)(data + size),
6501e3ec1e8cSMiaohe Lin skb_shinfo(skb), offsetof(struct skb_shared_info, frags[0]));
65026fa01ccdSSowmini Varadhan if (skb_orphan_frags(skb, gfp_mask)) {
6503bf9f1baaSEric Dumazet skb_kfree_head(data, size);
65046fa01ccdSSowmini Varadhan return -ENOMEM;
65056fa01ccdSSowmini Varadhan }
65066fa01ccdSSowmini Varadhan shinfo = (struct skb_shared_info *)(data + size);
65076fa01ccdSSowmini Varadhan for (i = 0; i < nfrags; i++) {
65086fa01ccdSSowmini Varadhan int fsize = skb_frag_size(&skb_shinfo(skb)->frags[i]);
65096fa01ccdSSowmini Varadhan
65106fa01ccdSSowmini Varadhan if (pos + fsize > off) {
65116fa01ccdSSowmini Varadhan shinfo->frags[k] = skb_shinfo(skb)->frags[i];
65126fa01ccdSSowmini Varadhan
65136fa01ccdSSowmini Varadhan if (pos < off) {
65146fa01ccdSSowmini Varadhan /* Split frag.
65156fa01ccdSSowmini Varadhan * We have two variants in this case:
65166fa01ccdSSowmini Varadhan * 1. Move all the frag to the second
65176fa01ccdSSowmini Varadhan * part, if it is possible. F.e.
65186fa01ccdSSowmini Varadhan * this approach is mandatory for TUX,
65196fa01ccdSSowmini Varadhan * where splitting is expensive.
65206fa01ccdSSowmini Varadhan * 2. Split is accurately. We make this.
65216fa01ccdSSowmini Varadhan */
6522b54c9d5bSJonathan Lemon skb_frag_off_add(&shinfo->frags[0], off - pos);
65236fa01ccdSSowmini Varadhan skb_frag_size_sub(&shinfo->frags[0], off - pos);
65246fa01ccdSSowmini Varadhan }
65256fa01ccdSSowmini Varadhan skb_frag_ref(skb, i);
65266fa01ccdSSowmini Varadhan k++;
65276fa01ccdSSowmini Varadhan }
65286fa01ccdSSowmini Varadhan pos += fsize;
65296fa01ccdSSowmini Varadhan }
65306fa01ccdSSowmini Varadhan shinfo->nr_frags = k;
65316fa01ccdSSowmini Varadhan if (skb_has_frag_list(skb))
65326fa01ccdSSowmini Varadhan skb_clone_fraglist(skb);
65336fa01ccdSSowmini Varadhan
65346fa01ccdSSowmini Varadhan /* split line is in frag list */
6535eabe8618SMiaohe Lin if (k == 0 && pskb_carve_frag_list(skb, shinfo, off - pos, gfp_mask)) {
6536eabe8618SMiaohe Lin /* skb_frag_unref() is not needed here as shinfo->nr_frags = 0. */
6537eabe8618SMiaohe Lin if (skb_has_frag_list(skb))
6538eabe8618SMiaohe Lin kfree_skb_list(skb_shinfo(skb)->frag_list);
6539bf9f1baaSEric Dumazet skb_kfree_head(data, size);
6540eabe8618SMiaohe Lin return -ENOMEM;
65416fa01ccdSSowmini Varadhan }
6542b07a2d97SJakub Kicinski skb_release_data(skb, SKB_CONSUMED, false);
65436fa01ccdSSowmini Varadhan
65446fa01ccdSSowmini Varadhan skb->head = data;
65456fa01ccdSSowmini Varadhan skb->head_frag = 0;
65466fa01ccdSSowmini Varadhan skb->data = data;
6547763087daSEric Dumazet skb_set_end_offset(skb, size);
65486fa01ccdSSowmini Varadhan skb_reset_tail_pointer(skb);
65496fa01ccdSSowmini Varadhan skb_headers_offset_update(skb, 0);
65506fa01ccdSSowmini Varadhan skb->cloned = 0;
65516fa01ccdSSowmini Varadhan skb->hdr_len = 0;
65526fa01ccdSSowmini Varadhan skb->nohdr = 0;
65536fa01ccdSSowmini Varadhan skb->len -= off;
65546fa01ccdSSowmini Varadhan skb->data_len = skb->len;
65556fa01ccdSSowmini Varadhan atomic_set(&skb_shinfo(skb)->dataref, 1);
65566fa01ccdSSowmini Varadhan return 0;
65576fa01ccdSSowmini Varadhan }
65586fa01ccdSSowmini Varadhan
65596fa01ccdSSowmini Varadhan /* remove len bytes from the beginning of the skb */
pskb_carve(struct sk_buff * skb,const u32 len,gfp_t gfp)65606fa01ccdSSowmini Varadhan static int pskb_carve(struct sk_buff *skb, const u32 len, gfp_t gfp)
65616fa01ccdSSowmini Varadhan {
65626fa01ccdSSowmini Varadhan int headlen = skb_headlen(skb);
65636fa01ccdSSowmini Varadhan
65646fa01ccdSSowmini Varadhan if (len < headlen)
65656fa01ccdSSowmini Varadhan return pskb_carve_inside_header(skb, len, headlen, gfp);
65666fa01ccdSSowmini Varadhan else
65676fa01ccdSSowmini Varadhan return pskb_carve_inside_nonlinear(skb, len, headlen, gfp);
65686fa01ccdSSowmini Varadhan }
65696fa01ccdSSowmini Varadhan
65706fa01ccdSSowmini Varadhan /* Extract to_copy bytes starting at off from skb, and return this in
65716fa01ccdSSowmini Varadhan * a new skb
65726fa01ccdSSowmini Varadhan */
pskb_extract(struct sk_buff * skb,int off,int to_copy,gfp_t gfp)65736fa01ccdSSowmini Varadhan struct sk_buff *pskb_extract(struct sk_buff *skb, int off,
65746fa01ccdSSowmini Varadhan int to_copy, gfp_t gfp)
65756fa01ccdSSowmini Varadhan {
65766fa01ccdSSowmini Varadhan struct sk_buff *clone = skb_clone(skb, gfp);
65776fa01ccdSSowmini Varadhan
65786fa01ccdSSowmini Varadhan if (!clone)
65796fa01ccdSSowmini Varadhan return NULL;
65806fa01ccdSSowmini Varadhan
65816fa01ccdSSowmini Varadhan if (pskb_carve(clone, off, gfp) < 0 ||
65826fa01ccdSSowmini Varadhan pskb_trim(clone, to_copy)) {
65836fa01ccdSSowmini Varadhan kfree_skb(clone);
65846fa01ccdSSowmini Varadhan return NULL;
65856fa01ccdSSowmini Varadhan }
65866fa01ccdSSowmini Varadhan return clone;
65876fa01ccdSSowmini Varadhan }
65886fa01ccdSSowmini Varadhan EXPORT_SYMBOL(pskb_extract);
6589c8c8b127SEric Dumazet
6590c8c8b127SEric Dumazet /**
6591c8c8b127SEric Dumazet * skb_condense - try to get rid of fragments/frag_list if possible
6592c8c8b127SEric Dumazet * @skb: buffer
6593c8c8b127SEric Dumazet *
6594c8c8b127SEric Dumazet * Can be used to save memory before skb is added to a busy queue.
6595c8c8b127SEric Dumazet * If packet has bytes in frags and enough tail room in skb->head,
6596c8c8b127SEric Dumazet * pull all of them, so that we can free the frags right now and adjust
6597c8c8b127SEric Dumazet * truesize.
6598c8c8b127SEric Dumazet * Notes:
6599c8c8b127SEric Dumazet * We do not reallocate skb->head thus can not fail.
6600c8c8b127SEric Dumazet * Caller must re-evaluate skb->truesize if needed.
6601c8c8b127SEric Dumazet */
skb_condense(struct sk_buff * skb)6602c8c8b127SEric Dumazet void skb_condense(struct sk_buff *skb)
6603c8c8b127SEric Dumazet {
66043174fed9SEric Dumazet if (skb->data_len) {
66053174fed9SEric Dumazet if (skb->data_len > skb->end - skb->tail ||
6606c8c8b127SEric Dumazet skb_cloned(skb))
6607c8c8b127SEric Dumazet return;
6608c8c8b127SEric Dumazet
6609c8c8b127SEric Dumazet /* Nice, we can free page frag(s) right now */
6610c8c8b127SEric Dumazet __pskb_pull_tail(skb, skb->data_len);
66113174fed9SEric Dumazet }
66123174fed9SEric Dumazet /* At this point, skb->truesize might be over estimated,
66133174fed9SEric Dumazet * because skb had a fragment, and fragments do not tell
66143174fed9SEric Dumazet * their truesize.
66153174fed9SEric Dumazet * When we pulled its content into skb->head, fragment
66163174fed9SEric Dumazet * was freed, but __pskb_pull_tail() could not possibly
66173174fed9SEric Dumazet * adjust skb->truesize, not knowing the frag truesize.
6618c8c8b127SEric Dumazet */
6619c8c8b127SEric Dumazet skb->truesize = SKB_TRUESIZE(skb_end_offset(skb));
6620c8c8b127SEric Dumazet }
6621d57a3a15SDavid Howells EXPORT_SYMBOL(skb_condense);
6622df5042f4SFlorian Westphal
6623df5042f4SFlorian Westphal #ifdef CONFIG_SKB_EXTENSIONS
skb_ext_get_ptr(struct skb_ext * ext,enum skb_ext_id id)6624df5042f4SFlorian Westphal static void *skb_ext_get_ptr(struct skb_ext *ext, enum skb_ext_id id)
6625df5042f4SFlorian Westphal {
6626df5042f4SFlorian Westphal return (void *)ext + (ext->offset[id] * SKB_EXT_ALIGN_VALUE);
6627df5042f4SFlorian Westphal }
6628df5042f4SFlorian Westphal
66298b69a803SPaolo Abeni /**
66308b69a803SPaolo Abeni * __skb_ext_alloc - allocate a new skb extensions storage
66318b69a803SPaolo Abeni *
66324930f483SFlorian Westphal * @flags: See kmalloc().
66334930f483SFlorian Westphal *
66348b69a803SPaolo Abeni * Returns the newly allocated pointer. The pointer can later attached to a
66358b69a803SPaolo Abeni * skb via __skb_ext_set().
66368b69a803SPaolo Abeni * Note: caller must handle the skb_ext as an opaque data.
66378b69a803SPaolo Abeni */
__skb_ext_alloc(gfp_t flags)66384930f483SFlorian Westphal struct skb_ext *__skb_ext_alloc(gfp_t flags)
6639df5042f4SFlorian Westphal {
66404930f483SFlorian Westphal struct skb_ext *new = kmem_cache_alloc(skbuff_ext_cache, flags);
6641df5042f4SFlorian Westphal
6642df5042f4SFlorian Westphal if (new) {
6643df5042f4SFlorian Westphal memset(new->offset, 0, sizeof(new->offset));
6644df5042f4SFlorian Westphal refcount_set(&new->refcnt, 1);
6645df5042f4SFlorian Westphal }
6646df5042f4SFlorian Westphal
6647df5042f4SFlorian Westphal return new;
6648df5042f4SFlorian Westphal }
6649df5042f4SFlorian Westphal
skb_ext_maybe_cow(struct skb_ext * old,unsigned int old_active)66504165079bSFlorian Westphal static struct skb_ext *skb_ext_maybe_cow(struct skb_ext *old,
66514165079bSFlorian Westphal unsigned int old_active)
6652df5042f4SFlorian Westphal {
6653df5042f4SFlorian Westphal struct skb_ext *new;
6654df5042f4SFlorian Westphal
6655df5042f4SFlorian Westphal if (refcount_read(&old->refcnt) == 1)
6656df5042f4SFlorian Westphal return old;
6657df5042f4SFlorian Westphal
6658df5042f4SFlorian Westphal new = kmem_cache_alloc(skbuff_ext_cache, GFP_ATOMIC);
6659df5042f4SFlorian Westphal if (!new)
6660df5042f4SFlorian Westphal return NULL;
6661df5042f4SFlorian Westphal
6662df5042f4SFlorian Westphal memcpy(new, old, old->chunks * SKB_EXT_ALIGN_VALUE);
6663df5042f4SFlorian Westphal refcount_set(&new->refcnt, 1);
6664df5042f4SFlorian Westphal
66654165079bSFlorian Westphal #ifdef CONFIG_XFRM
66664165079bSFlorian Westphal if (old_active & (1 << SKB_EXT_SEC_PATH)) {
66674165079bSFlorian Westphal struct sec_path *sp = skb_ext_get_ptr(old, SKB_EXT_SEC_PATH);
66684165079bSFlorian Westphal unsigned int i;
66694165079bSFlorian Westphal
66704165079bSFlorian Westphal for (i = 0; i < sp->len; i++)
66714165079bSFlorian Westphal xfrm_state_hold(sp->xvec[i]);
66724165079bSFlorian Westphal }
66734165079bSFlorian Westphal #endif
6674be503653SJeremy Kerr #ifdef CONFIG_MCTP_FLOWS
6675be503653SJeremy Kerr if (old_active & (1 << SKB_EXT_MCTP)) {
6676be503653SJeremy Kerr struct mctp_flow *flow = skb_ext_get_ptr(old, SKB_EXT_MCTP);
6677be503653SJeremy Kerr
6678be503653SJeremy Kerr if (flow->key)
6679be503653SJeremy Kerr refcount_inc(&flow->key->refs);
6680be503653SJeremy Kerr }
6681be503653SJeremy Kerr #endif
6682df5042f4SFlorian Westphal __skb_ext_put(old);
6683df5042f4SFlorian Westphal return new;
6684df5042f4SFlorian Westphal }
6685df5042f4SFlorian Westphal
6686df5042f4SFlorian Westphal /**
66878b69a803SPaolo Abeni * __skb_ext_set - attach the specified extension storage to this skb
66888b69a803SPaolo Abeni * @skb: buffer
66898b69a803SPaolo Abeni * @id: extension id
66908b69a803SPaolo Abeni * @ext: extension storage previously allocated via __skb_ext_alloc()
66918b69a803SPaolo Abeni *
66928b69a803SPaolo Abeni * Existing extensions, if any, are cleared.
66938b69a803SPaolo Abeni *
66948b69a803SPaolo Abeni * Returns the pointer to the extension.
66958b69a803SPaolo Abeni */
__skb_ext_set(struct sk_buff * skb,enum skb_ext_id id,struct skb_ext * ext)66968b69a803SPaolo Abeni void *__skb_ext_set(struct sk_buff *skb, enum skb_ext_id id,
66978b69a803SPaolo Abeni struct skb_ext *ext)
66988b69a803SPaolo Abeni {
66998b69a803SPaolo Abeni unsigned int newlen, newoff = SKB_EXT_CHUNKSIZEOF(*ext);
67008b69a803SPaolo Abeni
67018b69a803SPaolo Abeni skb_ext_put(skb);
67028b69a803SPaolo Abeni newlen = newoff + skb_ext_type_len[id];
67038b69a803SPaolo Abeni ext->chunks = newlen;
67048b69a803SPaolo Abeni ext->offset[id] = newoff;
67058b69a803SPaolo Abeni skb->extensions = ext;
67068b69a803SPaolo Abeni skb->active_extensions = 1 << id;
67078b69a803SPaolo Abeni return skb_ext_get_ptr(ext, id);
67088b69a803SPaolo Abeni }
67098b69a803SPaolo Abeni
67108b69a803SPaolo Abeni /**
6711df5042f4SFlorian Westphal * skb_ext_add - allocate space for given extension, COW if needed
6712df5042f4SFlorian Westphal * @skb: buffer
6713df5042f4SFlorian Westphal * @id: extension to allocate space for
6714df5042f4SFlorian Westphal *
6715df5042f4SFlorian Westphal * Allocates enough space for the given extension.
6716df5042f4SFlorian Westphal * If the extension is already present, a pointer to that extension
6717df5042f4SFlorian Westphal * is returned.
6718df5042f4SFlorian Westphal *
6719df5042f4SFlorian Westphal * If the skb was cloned, COW applies and the returned memory can be
6720df5042f4SFlorian Westphal * modified without changing the extension space of clones buffers.
6721df5042f4SFlorian Westphal *
6722df5042f4SFlorian Westphal * Returns pointer to the extension or NULL on allocation failure.
6723df5042f4SFlorian Westphal */
skb_ext_add(struct sk_buff * skb,enum skb_ext_id id)6724df5042f4SFlorian Westphal void *skb_ext_add(struct sk_buff *skb, enum skb_ext_id id)
6725df5042f4SFlorian Westphal {
6726df5042f4SFlorian Westphal struct skb_ext *new, *old = NULL;
6727df5042f4SFlorian Westphal unsigned int newlen, newoff;
6728df5042f4SFlorian Westphal
6729df5042f4SFlorian Westphal if (skb->active_extensions) {
6730df5042f4SFlorian Westphal old = skb->extensions;
6731df5042f4SFlorian Westphal
67324165079bSFlorian Westphal new = skb_ext_maybe_cow(old, skb->active_extensions);
6733df5042f4SFlorian Westphal if (!new)
6734df5042f4SFlorian Westphal return NULL;
6735df5042f4SFlorian Westphal
6736682ec859SPaolo Abeni if (__skb_ext_exist(new, id))
6737df5042f4SFlorian Westphal goto set_active;
6738df5042f4SFlorian Westphal
6739e94e50bdSPaolo Abeni newoff = new->chunks;
6740df5042f4SFlorian Westphal } else {
6741df5042f4SFlorian Westphal newoff = SKB_EXT_CHUNKSIZEOF(*new);
6742df5042f4SFlorian Westphal
67434930f483SFlorian Westphal new = __skb_ext_alloc(GFP_ATOMIC);
6744df5042f4SFlorian Westphal if (!new)
6745df5042f4SFlorian Westphal return NULL;
6746df5042f4SFlorian Westphal }
6747df5042f4SFlorian Westphal
6748df5042f4SFlorian Westphal newlen = newoff + skb_ext_type_len[id];
6749df5042f4SFlorian Westphal new->chunks = newlen;
6750df5042f4SFlorian Westphal new->offset[id] = newoff;
6751df5042f4SFlorian Westphal set_active:
6752b0999f38SPaolo Abeni skb->slow_gro = 1;
6753682ec859SPaolo Abeni skb->extensions = new;
6754df5042f4SFlorian Westphal skb->active_extensions |= 1 << id;
6755df5042f4SFlorian Westphal return skb_ext_get_ptr(new, id);
6756df5042f4SFlorian Westphal }
6757df5042f4SFlorian Westphal EXPORT_SYMBOL(skb_ext_add);
6758df5042f4SFlorian Westphal
67594165079bSFlorian Westphal #ifdef CONFIG_XFRM
skb_ext_put_sp(struct sec_path * sp)67604165079bSFlorian Westphal static void skb_ext_put_sp(struct sec_path *sp)
67614165079bSFlorian Westphal {
67624165079bSFlorian Westphal unsigned int i;
67634165079bSFlorian Westphal
67644165079bSFlorian Westphal for (i = 0; i < sp->len; i++)
67654165079bSFlorian Westphal xfrm_state_put(sp->xvec[i]);
67664165079bSFlorian Westphal }
67674165079bSFlorian Westphal #endif
67684165079bSFlorian Westphal
676978476d31SJeremy Kerr #ifdef CONFIG_MCTP_FLOWS
skb_ext_put_mctp(struct mctp_flow * flow)677078476d31SJeremy Kerr static void skb_ext_put_mctp(struct mctp_flow *flow)
677178476d31SJeremy Kerr {
677278476d31SJeremy Kerr if (flow->key)
677378476d31SJeremy Kerr mctp_key_unref(flow->key);
677478476d31SJeremy Kerr }
677578476d31SJeremy Kerr #endif
677678476d31SJeremy Kerr
__skb_ext_del(struct sk_buff * skb,enum skb_ext_id id)6777df5042f4SFlorian Westphal void __skb_ext_del(struct sk_buff *skb, enum skb_ext_id id)
6778df5042f4SFlorian Westphal {
6779df5042f4SFlorian Westphal struct skb_ext *ext = skb->extensions;
6780df5042f4SFlorian Westphal
6781df5042f4SFlorian Westphal skb->active_extensions &= ~(1 << id);
6782df5042f4SFlorian Westphal if (skb->active_extensions == 0) {
6783df5042f4SFlorian Westphal skb->extensions = NULL;
6784df5042f4SFlorian Westphal __skb_ext_put(ext);
67854165079bSFlorian Westphal #ifdef CONFIG_XFRM
67864165079bSFlorian Westphal } else if (id == SKB_EXT_SEC_PATH &&
67874165079bSFlorian Westphal refcount_read(&ext->refcnt) == 1) {
67884165079bSFlorian Westphal struct sec_path *sp = skb_ext_get_ptr(ext, SKB_EXT_SEC_PATH);
67894165079bSFlorian Westphal
67904165079bSFlorian Westphal skb_ext_put_sp(sp);
67914165079bSFlorian Westphal sp->len = 0;
67924165079bSFlorian Westphal #endif
6793df5042f4SFlorian Westphal }
6794df5042f4SFlorian Westphal }
6795df5042f4SFlorian Westphal EXPORT_SYMBOL(__skb_ext_del);
6796df5042f4SFlorian Westphal
__skb_ext_put(struct skb_ext * ext)6797df5042f4SFlorian Westphal void __skb_ext_put(struct skb_ext *ext)
6798df5042f4SFlorian Westphal {
6799df5042f4SFlorian Westphal /* If this is last clone, nothing can increment
6800df5042f4SFlorian Westphal * it after check passes. Avoids one atomic op.
6801df5042f4SFlorian Westphal */
6802df5042f4SFlorian Westphal if (refcount_read(&ext->refcnt) == 1)
6803df5042f4SFlorian Westphal goto free_now;
6804df5042f4SFlorian Westphal
6805df5042f4SFlorian Westphal if (!refcount_dec_and_test(&ext->refcnt))
6806df5042f4SFlorian Westphal return;
6807df5042f4SFlorian Westphal free_now:
68084165079bSFlorian Westphal #ifdef CONFIG_XFRM
68094165079bSFlorian Westphal if (__skb_ext_exist(ext, SKB_EXT_SEC_PATH))
68104165079bSFlorian Westphal skb_ext_put_sp(skb_ext_get_ptr(ext, SKB_EXT_SEC_PATH));
68114165079bSFlorian Westphal #endif
681278476d31SJeremy Kerr #ifdef CONFIG_MCTP_FLOWS
681378476d31SJeremy Kerr if (__skb_ext_exist(ext, SKB_EXT_MCTP))
681478476d31SJeremy Kerr skb_ext_put_mctp(skb_ext_get_ptr(ext, SKB_EXT_MCTP));
681578476d31SJeremy Kerr #endif
68164165079bSFlorian Westphal
6817df5042f4SFlorian Westphal kmem_cache_free(skbuff_ext_cache, ext);
6818df5042f4SFlorian Westphal }
6819df5042f4SFlorian Westphal EXPORT_SYMBOL(__skb_ext_put);
6820df5042f4SFlorian Westphal #endif /* CONFIG_SKB_EXTENSIONS */
682168822bdfSEric Dumazet
682268822bdfSEric Dumazet /**
682368822bdfSEric Dumazet * skb_attempt_defer_free - queue skb for remote freeing
682468822bdfSEric Dumazet * @skb: buffer
682568822bdfSEric Dumazet *
682668822bdfSEric Dumazet * Put @skb in a per-cpu list, using the cpu which
682768822bdfSEric Dumazet * allocated the skb/pages to reduce false sharing
682868822bdfSEric Dumazet * and memory zone spinlock contention.
682968822bdfSEric Dumazet */
skb_attempt_defer_free(struct sk_buff * skb)683068822bdfSEric Dumazet void skb_attempt_defer_free(struct sk_buff *skb)
683168822bdfSEric Dumazet {
683268822bdfSEric Dumazet int cpu = skb->alloc_cpu;
683368822bdfSEric Dumazet struct softnet_data *sd;
683439564c3fSEric Dumazet unsigned int defer_max;
683568822bdfSEric Dumazet bool kick;
683668822bdfSEric Dumazet
683768822bdfSEric Dumazet if (WARN_ON_ONCE(cpu >= nr_cpu_ids) ||
683868822bdfSEric Dumazet !cpu_online(cpu) ||
683968822bdfSEric Dumazet cpu == raw_smp_processor_id()) {
684039564c3fSEric Dumazet nodefer: __kfree_skb(skb);
684168822bdfSEric Dumazet return;
684268822bdfSEric Dumazet }
684368822bdfSEric Dumazet
6844e8e1ce84SEric Dumazet DEBUG_NET_WARN_ON_ONCE(skb_dst(skb));
6845e8e1ce84SEric Dumazet DEBUG_NET_WARN_ON_ONCE(skb->destructor);
6846e8e1ce84SEric Dumazet
684768822bdfSEric Dumazet sd = &per_cpu(softnet_data, cpu);
684839564c3fSEric Dumazet defer_max = READ_ONCE(sysctl_skb_defer_max);
684939564c3fSEric Dumazet if (READ_ONCE(sd->defer_count) >= defer_max)
685039564c3fSEric Dumazet goto nodefer;
685139564c3fSEric Dumazet
6852931e93bdSEric Dumazet spin_lock_bh(&sd->defer_lock);
6853c09b0cd2SJakub Kicinski /* Send an IPI every time queue reaches half capacity. */
6854c09b0cd2SJakub Kicinski kick = sd->defer_count == (defer_max >> 1);
6855c09b0cd2SJakub Kicinski /* Paired with the READ_ONCE() few lines above */
6856c09b0cd2SJakub Kicinski WRITE_ONCE(sd->defer_count, sd->defer_count + 1);
6857c09b0cd2SJakub Kicinski
685868822bdfSEric Dumazet skb->next = sd->defer_list;
685968822bdfSEric Dumazet /* Paired with READ_ONCE() in skb_defer_free_flush() */
686068822bdfSEric Dumazet WRITE_ONCE(sd->defer_list, skb);
6861931e93bdSEric Dumazet spin_unlock_bh(&sd->defer_lock);
686268822bdfSEric Dumazet
686368822bdfSEric Dumazet /* Make sure to trigger NET_RX_SOFTIRQ on the remote CPU
686468822bdfSEric Dumazet * if we are unlucky enough (this seems very unlikely).
686568822bdfSEric Dumazet */
686697e719a8SEric Dumazet if (unlikely(kick) && !cmpxchg(&sd->defer_ipi_scheduled, 0, 1))
686768822bdfSEric Dumazet smp_call_function_single_async(cpu, &sd->defer_csd);
686868822bdfSEric Dumazet }
68692e910b95SDavid Howells
skb_splice_csum_page(struct sk_buff * skb,struct page * page,size_t offset,size_t len)68702e910b95SDavid Howells static void skb_splice_csum_page(struct sk_buff *skb, struct page *page,
68712e910b95SDavid Howells size_t offset, size_t len)
68722e910b95SDavid Howells {
68732e910b95SDavid Howells const char *kaddr;
68742e910b95SDavid Howells __wsum csum;
68752e910b95SDavid Howells
68762e910b95SDavid Howells kaddr = kmap_local_page(page);
68772e910b95SDavid Howells csum = csum_partial(kaddr + offset, len, 0);
68782e910b95SDavid Howells kunmap_local(kaddr);
68792e910b95SDavid Howells skb->csum = csum_block_add(skb->csum, csum, skb->len);
68802e910b95SDavid Howells }
68812e910b95SDavid Howells
68822e910b95SDavid Howells /**
68832e910b95SDavid Howells * skb_splice_from_iter - Splice (or copy) pages to skbuff
68842e910b95SDavid Howells * @skb: The buffer to add pages to
68852e910b95SDavid Howells * @iter: Iterator representing the pages to be added
68862e910b95SDavid Howells * @maxsize: Maximum amount of pages to be added
68872e910b95SDavid Howells * @gfp: Allocation flags
68882e910b95SDavid Howells *
68892e910b95SDavid Howells * This is a common helper function for supporting MSG_SPLICE_PAGES. It
68902e910b95SDavid Howells * extracts pages from an iterator and adds them to the socket buffer if
68912e910b95SDavid Howells * possible, copying them to fragments if not possible (such as if they're slab
68922e910b95SDavid Howells * pages).
68932e910b95SDavid Howells *
68942e910b95SDavid Howells * Returns the amount of data spliced/copied or -EMSGSIZE if there's
68952e910b95SDavid Howells * insufficient space in the buffer to transfer anything.
68962e910b95SDavid Howells */
skb_splice_from_iter(struct sk_buff * skb,struct iov_iter * iter,ssize_t maxsize,gfp_t gfp)68972e910b95SDavid Howells ssize_t skb_splice_from_iter(struct sk_buff *skb, struct iov_iter *iter,
68982e910b95SDavid Howells ssize_t maxsize, gfp_t gfp)
68992e910b95SDavid Howells {
69002e910b95SDavid Howells size_t frag_limit = READ_ONCE(sysctl_max_skb_frags);
69012e910b95SDavid Howells struct page *pages[8], **ppages = pages;
69022e910b95SDavid Howells ssize_t spliced = 0, ret = 0;
69032e910b95SDavid Howells unsigned int i;
69042e910b95SDavid Howells
69052e910b95SDavid Howells while (iter->count > 0) {
6906ef1bc119SDan Carpenter ssize_t space, nr, len;
6907ef1bc119SDan Carpenter size_t off;
69082e910b95SDavid Howells
69092e910b95SDavid Howells ret = -EMSGSIZE;
69102e910b95SDavid Howells space = frag_limit - skb_shinfo(skb)->nr_frags;
69112e910b95SDavid Howells if (space < 0)
69122e910b95SDavid Howells break;
69132e910b95SDavid Howells
69142e910b95SDavid Howells /* We might be able to coalesce without increasing nr_frags */
69152e910b95SDavid Howells nr = clamp_t(size_t, space, 1, ARRAY_SIZE(pages));
69162e910b95SDavid Howells
69172e910b95SDavid Howells len = iov_iter_extract_pages(iter, &ppages, maxsize, nr, 0, &off);
69182e910b95SDavid Howells if (len <= 0) {
69192e910b95SDavid Howells ret = len ?: -EIO;
69202e910b95SDavid Howells break;
69212e910b95SDavid Howells }
69222e910b95SDavid Howells
69232e910b95SDavid Howells i = 0;
69242e910b95SDavid Howells do {
69252e910b95SDavid Howells struct page *page = pages[i++];
69262e910b95SDavid Howells size_t part = min_t(size_t, PAGE_SIZE - off, len);
69272e910b95SDavid Howells
69282e910b95SDavid Howells ret = -EIO;
69292e910b95SDavid Howells if (WARN_ON_ONCE(!sendpage_ok(page)))
69302e910b95SDavid Howells goto out;
69312e910b95SDavid Howells
69322e910b95SDavid Howells ret = skb_append_pagefrags(skb, page, off, part,
69332e910b95SDavid Howells frag_limit);
69342e910b95SDavid Howells if (ret < 0) {
69352e910b95SDavid Howells iov_iter_revert(iter, len);
69362e910b95SDavid Howells goto out;
69372e910b95SDavid Howells }
69382e910b95SDavid Howells
69392e910b95SDavid Howells if (skb->ip_summed == CHECKSUM_NONE)
69402e910b95SDavid Howells skb_splice_csum_page(skb, page, off, part);
69412e910b95SDavid Howells
69422e910b95SDavid Howells off = 0;
69432e910b95SDavid Howells spliced += part;
69442e910b95SDavid Howells maxsize -= part;
69452e910b95SDavid Howells len -= part;
69462e910b95SDavid Howells } while (len > 0);
69472e910b95SDavid Howells
69482e910b95SDavid Howells if (maxsize <= 0)
69492e910b95SDavid Howells break;
69502e910b95SDavid Howells }
69512e910b95SDavid Howells
69522e910b95SDavid Howells out:
69532e910b95SDavid Howells skb_len_add(skb, spliced);
69542e910b95SDavid Howells return spliced ?: ret;
69552e910b95SDavid Howells }
69562e910b95SDavid Howells EXPORT_SYMBOL(skb_splice_from_iter);
6957