1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 * Definitions for the 'struct sk_buff' memory handlers.
10 #ifndef _LINUX_SKBUFF_H
11 #define _LINUX_SKBUFF_H
13 #include <linux/kernel.h>
14 #include <linux/compiler.h>
15 #include <linux/time.h>
16 #include <linux/bug.h>
17 #include <linux/bvec.h>
18 #include <linux/cache.h>
19 #include <linux/rbtree.h>
20 #include <linux/socket.h>
21 #include <linux/refcount.h>
23 #include <linux/atomic.h>
24 #include <asm/types.h>
25 #include <linux/spinlock.h>
26 #include <linux/net.h>
27 #include <linux/textsearch.h>
28 #include <net/checksum.h>
29 #include <linux/rcupdate.h>
30 #include <linux/hrtimer.h>
31 #include <linux/dma-mapping.h>
32 #include <linux/netdev_features.h>
33 #include <linux/sched.h>
34 #include <linux/sched/clock.h>
35 #include <net/flow_dissector.h>
36 #include <linux/splice.h>
37 #include <linux/in6.h>
38 #include <linux/if_packet.h>
39 #include <linux/llist.h>
41 #include <net/page_pool.h>
42 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
43 #include <linux/netfilter/nf_conntrack_common.h>
46 /* The interface for checksum offload between the stack and networking drivers
49 * A. IP checksum related features
51 * Drivers advertise checksum offload capabilities in the features of a device.
52 * From the stack's point of view these are capabilities offered by the driver.
53 * A driver typically only advertises features that it is capable of offloading
56 * The checksum related features are:
58 * NETIF_F_HW_CSUM - The driver (or its device) is able to compute one
59 * IP (one's complement) checksum for any combination
60 * of protocols or protocol layering. The checksum is
61 * computed and set in a packet per the CHECKSUM_PARTIAL
62 * interface (see below).
64 * NETIF_F_IP_CSUM - Driver (device) is only able to checksum plain
65 * TCP or UDP packets over IPv4. These are specifically
66 * unencapsulated packets of the form IPv4|TCP or
67 * IPv4|UDP where the Protocol field in the IPv4 header
68 * is TCP or UDP. The IPv4 header may contain IP options.
69 * This feature cannot be set in features for a device
70 * with NETIF_F_HW_CSUM also set. This feature is being
71 * DEPRECATED (see below).
73 * NETIF_F_IPV6_CSUM - Driver (device) is only able to checksum plain
74 * TCP or UDP packets over IPv6. These are specifically
75 * unencapsulated packets of the form IPv6|TCP or
76 * IPv6|UDP where the Next Header field in the IPv6
77 * header is either TCP or UDP. IPv6 extension headers
78 * are not supported with this feature. This feature
79 * cannot be set in features for a device with
80 * NETIF_F_HW_CSUM also set. This feature is being
81 * DEPRECATED (see below).
83 * NETIF_F_RXCSUM - Driver (device) performs receive checksum offload.
84 * This flag is only used to disable the RX checksum
85 * feature for a device. The stack will accept receive
86 * checksum indication in packets received on a device
87 * regardless of whether NETIF_F_RXCSUM is set.
89 * B. Checksumming of received packets by device. Indication of checksum
90 * verification is set in skb->ip_summed. Possible values are:
94 * Device did not checksum this packet e.g. due to lack of capabilities.
95 * The packet contains full (though not verified) checksum in packet but
96 * not in skb->csum. Thus, skb->csum is undefined in this case.
98 * CHECKSUM_UNNECESSARY:
100 * The hardware you're dealing with doesn't calculate the full checksum
101 * (as in CHECKSUM_COMPLETE), but it does parse headers and verify checksums
102 * for specific protocols. For such packets it will set CHECKSUM_UNNECESSARY
103 * if their checksums are okay. skb->csum is still undefined in this case
104 * though. A driver or device must never modify the checksum field in the
105 * packet even if checksum is verified.
107 * CHECKSUM_UNNECESSARY is applicable to following protocols:
108 * TCP: IPv6 and IPv4.
109 * UDP: IPv4 and IPv6. A device may apply CHECKSUM_UNNECESSARY to a
110 * zero UDP checksum for either IPv4 or IPv6, the networking stack
111 * may perform further validation in this case.
112 * GRE: only if the checksum is present in the header.
113 * SCTP: indicates the CRC in SCTP header has been validated.
114 * FCOE: indicates the CRC in FC frame has been validated.
116 * skb->csum_level indicates the number of consecutive checksums found in
117 * the packet minus one that have been verified as CHECKSUM_UNNECESSARY.
118 * For instance if a device receives an IPv6->UDP->GRE->IPv4->TCP packet
119 * and a device is able to verify the checksums for UDP (possibly zero),
120 * GRE (checksum flag is set) and TCP, skb->csum_level would be set to
121 * two. If the device were only able to verify the UDP checksum and not
122 * GRE, either because it doesn't support GRE checksum or because GRE
123 * checksum is bad, skb->csum_level would be set to zero (TCP checksum is
124 * not considered in this case).
128 * This is the most generic way. The device supplied checksum of the _whole_
129 * packet as seen by netif_rx() and fills in skb->csum. This means the
130 * hardware doesn't need to parse L3/L4 headers to implement this.
133 * - Even if device supports only some protocols, but is able to produce
134 * skb->csum, it MUST use CHECKSUM_COMPLETE, not CHECKSUM_UNNECESSARY.
135 * - CHECKSUM_COMPLETE is not applicable to SCTP and FCoE protocols.
139 * A checksum is set up to be offloaded to a device as described in the
140 * output description for CHECKSUM_PARTIAL. This may occur on a packet
141 * received directly from another Linux OS, e.g., a virtualized Linux kernel
142 * on the same host, or it may be set in the input path in GRO or remote
143 * checksum offload. For the purposes of checksum verification, the checksum
144 * referred to by skb->csum_start + skb->csum_offset and any preceding
145 * checksums in the packet are considered verified. Any checksums in the
146 * packet that are after the checksum being offloaded are not considered to
149 * C. Checksumming on transmit for non-GSO. The stack requests checksum offload
150 * in the skb->ip_summed for a packet. Values are:
154 * The driver is required to checksum the packet as seen by hard_start_xmit()
155 * from skb->csum_start up to the end, and to record/write the checksum at
156 * offset skb->csum_start + skb->csum_offset. A driver may verify that the
157 * csum_start and csum_offset values are valid values given the length and
158 * offset of the packet, but it should not attempt to validate that the
159 * checksum refers to a legitimate transport layer checksum -- it is the
160 * purview of the stack to validate that csum_start and csum_offset are set
163 * When the stack requests checksum offload for a packet, the driver MUST
164 * ensure that the checksum is set correctly. A driver can either offload the
165 * checksum calculation to the device, or call skb_checksum_help (in the case
166 * that the device does not support offload for a particular checksum).
168 * NETIF_F_IP_CSUM and NETIF_F_IPV6_CSUM are being deprecated in favor of
169 * NETIF_F_HW_CSUM. New devices should use NETIF_F_HW_CSUM to indicate
170 * checksum offload capability.
171 * skb_csum_hwoffload_help() can be called to resolve CHECKSUM_PARTIAL based
172 * on network device checksumming capabilities: if a packet does not match
173 * them, skb_checksum_help or skb_crc32c_help (depending on the value of
174 * csum_not_inet, see item D.) is called to resolve the checksum.
178 * The skb was already checksummed by the protocol, or a checksum is not
181 * CHECKSUM_UNNECESSARY:
183 * This has the same meaning as CHECKSUM_NONE for checksum offload on
187 * Not used in checksum output. If a driver observes a packet with this value
188 * set in skbuff, it should treat the packet as if CHECKSUM_NONE were set.
190 * D. Non-IP checksum (CRC) offloads
192 * NETIF_F_SCTP_CRC - This feature indicates that a device is capable of
193 * offloading the SCTP CRC in a packet. To perform this offload the stack
194 * will set csum_start and csum_offset accordingly, set ip_summed to
195 * CHECKSUM_PARTIAL and set csum_not_inet to 1, to provide an indication in
196 * the skbuff that the CHECKSUM_PARTIAL refers to CRC32c.
197 * A driver that supports both IP checksum offload and SCTP CRC32c offload
198 * must verify which offload is configured for a packet by testing the
199 * value of skb->csum_not_inet; skb_crc32c_csum_help is provided to resolve
200 * CHECKSUM_PARTIAL on skbs where csum_not_inet is set to 1.
202 * NETIF_F_FCOE_CRC - This feature indicates that a device is capable of
203 * offloading the FCOE CRC in a packet. To perform this offload the stack
204 * will set ip_summed to CHECKSUM_PARTIAL and set csum_start and csum_offset
205 * accordingly. Note that there is no indication in the skbuff that the
206 * CHECKSUM_PARTIAL refers to an FCOE checksum, so a driver that supports
207 * both IP checksum offload and FCOE CRC offload must verify which offload
208 * is configured for a packet, presumably by inspecting packet headers.
210 * E. Checksumming on output with GSO.
212 * In the case of a GSO packet (skb_is_gso(skb) is true), checksum offload
213 * is implied by the SKB_GSO_* flags in gso_type. Most obviously, if the
214 * gso_type is SKB_GSO_TCPV4 or SKB_GSO_TCPV6, TCP checksum offload as
215 * part of the GSO operation is implied. If a checksum is being offloaded
216 * with GSO then ip_summed is CHECKSUM_PARTIAL, and both csum_start and
217 * csum_offset are set to refer to the outermost checksum being offloaded
218 * (two offloaded checksums are possible with UDP encapsulation).
221 /* Don't change this without changing skb_csum_unnecessary! */
222 #define CHECKSUM_NONE 0
223 #define CHECKSUM_UNNECESSARY 1
224 #define CHECKSUM_COMPLETE 2
225 #define CHECKSUM_PARTIAL 3
227 /* Maximum value in skb->csum_level */
228 #define SKB_MAX_CSUM_LEVEL 3
230 #define SKB_DATA_ALIGN(X) ALIGN(X, SMP_CACHE_BYTES)
231 #define SKB_WITH_OVERHEAD(X) \
232 ((X) - SKB_DATA_ALIGN(sizeof(struct skb_shared_info)))
233 #define SKB_MAX_ORDER(X, ORDER) \
234 SKB_WITH_OVERHEAD((PAGE_SIZE << (ORDER)) - (X))
235 #define SKB_MAX_HEAD(X) (SKB_MAX_ORDER((X), 0))
236 #define SKB_MAX_ALLOC (SKB_MAX_ORDER(0, 2))
238 /* return minimum truesize of one skb containing X bytes of data */
239 #define SKB_TRUESIZE(X) ((X) + \
240 SKB_DATA_ALIGN(sizeof(struct sk_buff)) + \
241 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)))
243 struct ahash_request;
246 struct pipe_inode_info;
253 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
254 struct nf_bridge_info {
256 BRNF_PROTO_UNCHANGED,
264 struct net_device *physindev;
266 /* always valid & non-NULL from FORWARD on, for physdev match */
267 struct net_device *physoutdev;
269 /* prerouting: detect dnat in orig/reply direction */
271 struct in6_addr ipv6_daddr;
273 /* after prerouting + nat detected: store original source
274 * mac since neigh resolution overwrites it, only used while
275 * skb is out in neigh layer.
277 char neigh_header[8];
282 #if IS_ENABLED(CONFIG_NET_TC_SKB_EXT)
283 /* Chain in tc_skb_ext will be used to share the tc chain with
284 * ovs recirc_id. It will be set to the current chain by tc
285 * and read by ovs to recirc_id.
297 struct sk_buff_head {
298 /* These two members must be first to match sk_buff. */
299 struct_group_tagged(sk_buff_list, list,
300 struct sk_buff *next;
301 struct sk_buff *prev;
310 /* The reason of skb drop, which is used in kfree_skb_reason().
311 * en...maybe they should be splited by group?
313 * Each item here should also be in 'TRACE_SKB_DROP_REASON', which is
314 * used to translate the reason to string.
316 enum skb_drop_reason {
317 SKB_NOT_DROPPED_YET = 0,
318 SKB_DROP_REASON_NOT_SPECIFIED, /* drop reason is not specified */
319 SKB_DROP_REASON_NO_SOCKET, /* socket not found */
320 SKB_DROP_REASON_PKT_TOO_SMALL, /* packet size is too small */
321 SKB_DROP_REASON_TCP_CSUM, /* TCP checksum error */
322 SKB_DROP_REASON_SOCKET_FILTER, /* dropped by socket filter */
323 SKB_DROP_REASON_UDP_CSUM, /* UDP checksum error */
324 SKB_DROP_REASON_NETFILTER_DROP, /* dropped by netfilter */
325 SKB_DROP_REASON_OTHERHOST, /* packet don't belong to current
326 * host (interface is in promisc
329 SKB_DROP_REASON_IP_CSUM, /* IP checksum error */
330 SKB_DROP_REASON_IP_INHDR, /* there is something wrong with
332 * IPSTATS_MIB_INHDRERRORS)
334 SKB_DROP_REASON_IP_RPFILTER, /* IP rpfilter validate failed.
335 * see the document for rp_filter
336 * in ip-sysctl.rst for more
339 SKB_DROP_REASON_UNICAST_IN_L2_MULTICAST, /* destination address of L2
340 * is multicast, but L3 is
343 SKB_DROP_REASON_XFRM_POLICY, /* xfrm policy check failed */
344 SKB_DROP_REASON_IP_NOPROTO, /* no support for IP protocol */
345 SKB_DROP_REASON_SOCKET_RCVBUFF, /* socket receive buff is full */
346 SKB_DROP_REASON_PROTO_MEM, /* proto memory limition, such as
347 * udp packet drop out of
348 * udp_memory_allocated.
350 SKB_DROP_REASON_TCP_MD5NOTFOUND, /* no MD5 hash and one
351 * expected, corresponding
352 * to LINUX_MIB_TCPMD5NOTFOUND
354 SKB_DROP_REASON_TCP_MD5UNEXPECTED, /* MD5 hash and we're not
355 * expecting one, corresponding
356 * to LINUX_MIB_TCPMD5UNEXPECTED
358 SKB_DROP_REASON_TCP_MD5FAILURE, /* MD5 hash and its wrong,
360 * LINUX_MIB_TCPMD5FAILURE
362 SKB_DROP_REASON_SOCKET_BACKLOG, /* failed to add skb to socket
364 * LINUX_MIB_TCPBACKLOGDROP)
366 SKB_DROP_REASON_TCP_FLAGS, /* TCP flags invalid */
367 SKB_DROP_REASON_TCP_ZEROWINDOW, /* TCP receive window size is zero,
368 * see LINUX_MIB_TCPZEROWINDOWDROP
370 SKB_DROP_REASON_TCP_OLD_DATA, /* the TCP data reveived is already
371 * received before (spurious retrans
373 * LINUX_MIB_DELAYEDACKLOST
375 SKB_DROP_REASON_TCP_OVERWINDOW, /* the TCP data is out of window,
376 * the seq of the first byte exceed
377 * the right edges of receive
380 SKB_DROP_REASON_TCP_OFOMERGE, /* the data of skb is already in
381 * the ofo queue, corresponding to
382 * LINUX_MIB_TCPOFOMERGE
384 SKB_DROP_REASON_IP_OUTNOROUTES, /* route lookup failed */
385 SKB_DROP_REASON_BPF_CGROUP_EGRESS, /* dropped by
386 * BPF_PROG_TYPE_CGROUP_SKB
389 SKB_DROP_REASON_IPV6DISABLED, /* IPv6 is disabled on the device */
390 SKB_DROP_REASON_NEIGH_CREATEFAIL, /* failed to create neigh
393 SKB_DROP_REASON_NEIGH_FAILED, /* neigh entry in failed state */
394 SKB_DROP_REASON_NEIGH_QUEUEFULL, /* arp_queue for neigh
397 SKB_DROP_REASON_NEIGH_DEAD, /* neigh entry is dead */
398 SKB_DROP_REASON_TC_EGRESS, /* dropped in TC egress HOOK */
399 SKB_DROP_REASON_QDISC_DROP, /* dropped by qdisc when packet
400 * outputting (failed to enqueue to
403 SKB_DROP_REASON_CPU_BACKLOG, /* failed to enqueue the skb to
404 * the per CPU backlog queue. This
405 * can be caused by backlog queue
406 * full (see netdev_max_backlog in
407 * net.rst) or RPS flow limit
409 SKB_DROP_REASON_XDP, /* dropped by XDP in input path */
410 SKB_DROP_REASON_TC_INGRESS, /* dropped in TC ingress HOOK */
411 SKB_DROP_REASON_PTYPE_ABSENT, /* not packet_type found to handle
412 * the skb. For an etner packet,
413 * this means that L3 protocol is
416 SKB_DROP_REASON_SKB_CSUM, /* sk_buff checksum computation
419 SKB_DROP_REASON_SKB_GSO_SEG, /* gso segmentation error */
420 SKB_DROP_REASON_SKB_UCOPY_FAULT, /* failed to copy data from
421 * user space, e.g., via
422 * zerocopy_sg_from_iter()
423 * or skb_orphan_frags_rx()
425 SKB_DROP_REASON_DEV_HDR, /* device driver specific
426 * header/metadata is invalid
428 /* the device is not ready to xmit/recv due to any of its data
429 * structure that is not up/ready/initialized, e.g., the IFF_UP is
430 * not set, or driver specific tun->tfiles[txq] is not initialized
432 SKB_DROP_REASON_DEV_READY,
433 SKB_DROP_REASON_FULL_RING, /* ring buffer is full */
434 SKB_DROP_REASON_NOMEM, /* error due to OOM */
435 SKB_DROP_REASON_HDR_TRUNC, /* failed to trunc/extract the header
436 * from networking data, e.g., failed
437 * to pull the protocol header from
438 * frags via pskb_may_pull()
440 SKB_DROP_REASON_TAP_FILTER, /* dropped by (ebpf) filter directly
441 * attached to tun/tap, e.g., via
444 SKB_DROP_REASON_TAP_TXFILTER, /* dropped by tx filter implemented
445 * at tun/tap, e.g., check_filter()
450 /* To allow 64K frame to be packed as single skb without frag_list we
451 * require 64K/PAGE_SIZE pages plus 1 additional page to allow for
452 * buffers which do not start on a page boundary.
454 * Since GRO uses frags we allocate at least 16 regardless of page
457 #if (65536/PAGE_SIZE + 1) < 16
458 #define MAX_SKB_FRAGS 16UL
460 #define MAX_SKB_FRAGS (65536/PAGE_SIZE + 1)
462 extern int sysctl_max_skb_frags;
464 /* Set skb_shinfo(skb)->gso_size to this in case you want skb_segment to
465 * segment using its current segmentation instead.
467 #define GSO_BY_FRAGS 0xFFFF
469 typedef struct bio_vec skb_frag_t;
472 * skb_frag_size() - Returns the size of a skb fragment
473 * @frag: skb fragment
475 static inline unsigned int skb_frag_size(const skb_frag_t *frag)
481 * skb_frag_size_set() - Sets the size of a skb fragment
482 * @frag: skb fragment
483 * @size: size of fragment
485 static inline void skb_frag_size_set(skb_frag_t *frag, unsigned int size)
491 * skb_frag_size_add() - Increments the size of a skb fragment by @delta
492 * @frag: skb fragment
493 * @delta: value to add
495 static inline void skb_frag_size_add(skb_frag_t *frag, int delta)
497 frag->bv_len += delta;
501 * skb_frag_size_sub() - Decrements the size of a skb fragment by @delta
502 * @frag: skb fragment
503 * @delta: value to subtract
505 static inline void skb_frag_size_sub(skb_frag_t *frag, int delta)
507 frag->bv_len -= delta;
511 * skb_frag_must_loop - Test if %p is a high memory page
512 * @p: fragment's page
514 static inline bool skb_frag_must_loop(struct page *p)
516 #if defined(CONFIG_HIGHMEM)
517 if (IS_ENABLED(CONFIG_DEBUG_KMAP_LOCAL_FORCE_MAP) || PageHighMem(p))
524 * skb_frag_foreach_page - loop over pages in a fragment
526 * @f: skb frag to operate on
527 * @f_off: offset from start of f->bv_page
528 * @f_len: length from f_off to loop over
529 * @p: (temp var) current page
530 * @p_off: (temp var) offset from start of current page,
531 * non-zero only on first page.
532 * @p_len: (temp var) length in current page,
533 * < PAGE_SIZE only on first and last page.
534 * @copied: (temp var) length so far, excluding current p_len.
536 * A fragment can hold a compound page, in which case per-page
537 * operations, notably kmap_atomic, must be called for each
540 #define skb_frag_foreach_page(f, f_off, f_len, p, p_off, p_len, copied) \
541 for (p = skb_frag_page(f) + ((f_off) >> PAGE_SHIFT), \
542 p_off = (f_off) & (PAGE_SIZE - 1), \
543 p_len = skb_frag_must_loop(p) ? \
544 min_t(u32, f_len, PAGE_SIZE - p_off) : f_len, \
547 copied += p_len, p++, p_off = 0, \
548 p_len = min_t(u32, f_len - copied, PAGE_SIZE)) \
550 #define HAVE_HW_TIME_STAMP
553 * struct skb_shared_hwtstamps - hardware time stamps
554 * @hwtstamp: hardware time stamp transformed into duration
555 * since arbitrary point in time
557 * Software time stamps generated by ktime_get_real() are stored in
560 * hwtstamps can only be compared against other hwtstamps from
563 * This structure is attached to packets as part of the
564 * &skb_shared_info. Use skb_hwtstamps() to get a pointer.
566 struct skb_shared_hwtstamps {
570 /* Definitions for tx_flags in struct skb_shared_info */
572 /* generate hardware time stamp */
573 SKBTX_HW_TSTAMP = 1 << 0,
575 /* generate software time stamp when queueing packet to NIC */
576 SKBTX_SW_TSTAMP = 1 << 1,
578 /* device driver is going to provide hardware time stamp */
579 SKBTX_IN_PROGRESS = 1 << 2,
581 /* generate wifi status information (where possible) */
582 SKBTX_WIFI_STATUS = 1 << 4,
584 /* generate software time stamp when entering packet scheduling */
585 SKBTX_SCHED_TSTAMP = 1 << 6,
588 #define SKBTX_ANY_SW_TSTAMP (SKBTX_SW_TSTAMP | \
590 #define SKBTX_ANY_TSTAMP (SKBTX_HW_TSTAMP | SKBTX_ANY_SW_TSTAMP)
592 /* Definitions for flags in struct skb_shared_info */
594 /* use zcopy routines */
595 SKBFL_ZEROCOPY_ENABLE = BIT(0),
597 /* This indicates at least one fragment might be overwritten
598 * (as in vmsplice(), sendfile() ...)
599 * If we need to compute a TX checksum, we'll need to copy
600 * all frags to avoid possible bad checksum
602 SKBFL_SHARED_FRAG = BIT(1),
604 /* segment contains only zerocopy data and should not be
605 * charged to the kernel memory.
607 SKBFL_PURE_ZEROCOPY = BIT(2),
610 #define SKBFL_ZEROCOPY_FRAG (SKBFL_ZEROCOPY_ENABLE | SKBFL_SHARED_FRAG)
611 #define SKBFL_ALL_ZEROCOPY (SKBFL_ZEROCOPY_FRAG | SKBFL_PURE_ZEROCOPY)
614 * The callback notifies userspace to release buffers when skb DMA is done in
615 * lower device, the skb last reference should be 0 when calling this.
616 * The zerocopy_success argument is true if zero copy transmit occurred,
617 * false on data copy or out of memory error caused by data copy attempt.
618 * The ctx field is used to track device context.
619 * The desc field is used to track userspace buffer index.
622 void (*callback)(struct sk_buff *, struct ubuf_info *,
623 bool zerocopy_success);
640 struct user_struct *user;
645 #define skb_uarg(SKB) ((struct ubuf_info *)(skb_shinfo(SKB)->destructor_arg))
647 int mm_account_pinned_pages(struct mmpin *mmp, size_t size);
648 void mm_unaccount_pinned_pages(struct mmpin *mmp);
650 struct ubuf_info *msg_zerocopy_alloc(struct sock *sk, size_t size);
651 struct ubuf_info *msg_zerocopy_realloc(struct sock *sk, size_t size,
652 struct ubuf_info *uarg);
654 void msg_zerocopy_put_abort(struct ubuf_info *uarg, bool have_uref);
656 void msg_zerocopy_callback(struct sk_buff *skb, struct ubuf_info *uarg,
659 int skb_zerocopy_iter_dgram(struct sk_buff *skb, struct msghdr *msg, int len);
660 int skb_zerocopy_iter_stream(struct sock *sk, struct sk_buff *skb,
661 struct msghdr *msg, int len,
662 struct ubuf_info *uarg);
664 /* This data is invariant across clones and lives at
665 * the end of the header data, ie. at skb->end.
667 struct skb_shared_info {
672 unsigned short gso_size;
673 /* Warning: this field is not always filled in (UFO)! */
674 unsigned short gso_segs;
675 struct sk_buff *frag_list;
676 struct skb_shared_hwtstamps hwtstamps;
677 unsigned int gso_type;
681 * Warning : all fields before dataref are cleared in __alloc_skb()
684 unsigned int xdp_frags_size;
686 /* Intermediate layers must ensure that destructor_arg
687 * remains valid until skb destructor */
688 void * destructor_arg;
690 /* must be last field, see pskb_expand_head() */
691 skb_frag_t frags[MAX_SKB_FRAGS];
694 /* We divide dataref into two halves. The higher 16 bits hold references
695 * to the payload part of skb->data. The lower 16 bits hold references to
696 * the entire skb->data. A clone of a headerless skb holds the length of
697 * the header in skb->hdr_len.
699 * All users must obey the rule that the skb->data reference count must be
700 * greater than or equal to the payload reference count.
702 * Holding a reference to the payload part means that the user does not
703 * care about modifications to the header part of skb->data.
705 #define SKB_DATAREF_SHIFT 16
706 #define SKB_DATAREF_MASK ((1 << SKB_DATAREF_SHIFT) - 1)
710 SKB_FCLONE_UNAVAILABLE, /* skb has no fclone (from head_cache) */
711 SKB_FCLONE_ORIG, /* orig skb (from fclone_cache) */
712 SKB_FCLONE_CLONE, /* companion fclone skb (from fclone_cache) */
716 SKB_GSO_TCPV4 = 1 << 0,
718 /* This indicates the skb is from an untrusted source. */
719 SKB_GSO_DODGY = 1 << 1,
721 /* This indicates the tcp segment has CWR set. */
722 SKB_GSO_TCP_ECN = 1 << 2,
724 SKB_GSO_TCP_FIXEDID = 1 << 3,
726 SKB_GSO_TCPV6 = 1 << 4,
728 SKB_GSO_FCOE = 1 << 5,
730 SKB_GSO_GRE = 1 << 6,
732 SKB_GSO_GRE_CSUM = 1 << 7,
734 SKB_GSO_IPXIP4 = 1 << 8,
736 SKB_GSO_IPXIP6 = 1 << 9,
738 SKB_GSO_UDP_TUNNEL = 1 << 10,
740 SKB_GSO_UDP_TUNNEL_CSUM = 1 << 11,
742 SKB_GSO_PARTIAL = 1 << 12,
744 SKB_GSO_TUNNEL_REMCSUM = 1 << 13,
746 SKB_GSO_SCTP = 1 << 14,
748 SKB_GSO_ESP = 1 << 15,
750 SKB_GSO_UDP = 1 << 16,
752 SKB_GSO_UDP_L4 = 1 << 17,
754 SKB_GSO_FRAGLIST = 1 << 18,
757 #if BITS_PER_LONG > 32
758 #define NET_SKBUFF_DATA_USES_OFFSET 1
761 #ifdef NET_SKBUFF_DATA_USES_OFFSET
762 typedef unsigned int sk_buff_data_t;
764 typedef unsigned char *sk_buff_data_t;
768 * struct sk_buff - socket buffer
769 * @next: Next buffer in list
770 * @prev: Previous buffer in list
771 * @tstamp: Time we arrived/left
772 * @skb_mstamp_ns: (aka @tstamp) earliest departure time; start point
773 * for retransmit timer
774 * @rbnode: RB tree node, alternative to next/prev for netem/tcp
776 * @ll_node: anchor in an llist (eg socket defer_list)
777 * @sk: Socket we are owned by
778 * @ip_defrag_offset: (aka @sk) alternate use of @sk, used in
779 * fragmentation management
780 * @dev: Device we arrived on/are leaving by
781 * @dev_scratch: (aka @dev) alternate use of @dev when @dev would be %NULL
782 * @cb: Control buffer. Free for use by every layer. Put private vars here
783 * @_skb_refdst: destination entry (with norefcount bit)
784 * @sp: the security path, used for xfrm
785 * @len: Length of actual data
786 * @data_len: Data length
787 * @mac_len: Length of link layer header
788 * @hdr_len: writable header length of cloned skb
789 * @csum: Checksum (must include start/offset pair)
790 * @csum_start: Offset from skb->head where checksumming should start
791 * @csum_offset: Offset from csum_start where checksum should be stored
792 * @priority: Packet queueing priority
793 * @ignore_df: allow local fragmentation
794 * @cloned: Head may be cloned (check refcnt to be sure)
795 * @ip_summed: Driver fed us an IP checksum
796 * @nohdr: Payload reference only, must not modify header
797 * @pkt_type: Packet class
798 * @fclone: skbuff clone status
799 * @ipvs_property: skbuff is owned by ipvs
800 * @inner_protocol_type: whether the inner protocol is
801 * ENCAP_TYPE_ETHER or ENCAP_TYPE_IPPROTO
802 * @remcsum_offload: remote checksum offload is enabled
803 * @offload_fwd_mark: Packet was L2-forwarded in hardware
804 * @offload_l3_fwd_mark: Packet was L3-forwarded in hardware
805 * @tc_skip_classify: do not classify packet. set by IFB device
806 * @tc_at_ingress: used within tc_classify to distinguish in/egress
807 * @redirected: packet was redirected by packet classifier
808 * @from_ingress: packet was redirected from the ingress path
809 * @nf_skip_egress: packet shall skip nf egress - see netfilter_netdev.h
810 * @peeked: this packet has been seen already, so stats have been
811 * done for it, don't do them again
812 * @nf_trace: netfilter packet trace flag
813 * @protocol: Packet protocol from driver
814 * @destructor: Destruct function
815 * @tcp_tsorted_anchor: list structure for TCP (tp->tsorted_sent_queue)
816 * @_sk_redir: socket redirection information for skmsg
817 * @_nfct: Associated connection, if any (with nfctinfo bits)
818 * @nf_bridge: Saved data about a bridged frame - see br_netfilter.c
819 * @skb_iif: ifindex of device we arrived on
820 * @tc_index: Traffic control index
821 * @hash: the packet hash
822 * @queue_mapping: Queue mapping for multiqueue devices
823 * @head_frag: skb was allocated from page fragments,
824 * not allocated by kmalloc() or vmalloc().
825 * @pfmemalloc: skbuff was allocated from PFMEMALLOC reserves
826 * @pp_recycle: mark the packet for recycling instead of freeing (implies
827 * page_pool support on driver)
828 * @active_extensions: active extensions (skb_ext_id types)
829 * @ndisc_nodetype: router type (from link layer)
830 * @ooo_okay: allow the mapping of a socket to a queue to be changed
831 * @l4_hash: indicate hash is a canonical 4-tuple hash over transport
833 * @sw_hash: indicates hash was computed in software stack
834 * @wifi_acked_valid: wifi_acked was set
835 * @wifi_acked: whether frame was acked on wifi or not
836 * @no_fcs: Request NIC to treat last 4 bytes as Ethernet FCS
837 * @encapsulation: indicates the inner headers in the skbuff are valid
838 * @encap_hdr_csum: software checksum is needed
839 * @csum_valid: checksum is already valid
840 * @csum_not_inet: use CRC32c to resolve CHECKSUM_PARTIAL
841 * @csum_complete_sw: checksum was completed by software
842 * @csum_level: indicates the number of consecutive checksums found in
843 * the packet minus one that have been verified as
844 * CHECKSUM_UNNECESSARY (max 3)
845 * @dst_pending_confirm: need to confirm neighbour
846 * @decrypted: Decrypted SKB
847 * @slow_gro: state present at GRO time, slower prepare step required
848 * @mono_delivery_time: When set, skb->tstamp has the
849 * delivery_time in mono clock base (i.e. EDT). Otherwise, the
850 * skb->tstamp has the (rcv) timestamp at ingress and
851 * delivery_time at egress.
852 * @napi_id: id of the NAPI struct this skb came from
853 * @sender_cpu: (aka @napi_id) source CPU in XPS
854 * @secmark: security marking
855 * @mark: Generic packet mark
856 * @reserved_tailroom: (aka @mark) number of bytes of free space available
857 * at the tail of an sk_buff
858 * @vlan_present: VLAN tag is present
859 * @vlan_proto: vlan encapsulation protocol
860 * @vlan_tci: vlan tag control information
861 * @inner_protocol: Protocol (encapsulation)
862 * @inner_ipproto: (aka @inner_protocol) stores ipproto when
863 * skb->inner_protocol_type == ENCAP_TYPE_IPPROTO;
864 * @inner_transport_header: Inner transport layer header (encapsulation)
865 * @inner_network_header: Network layer header (encapsulation)
866 * @inner_mac_header: Link layer header (encapsulation)
867 * @transport_header: Transport layer header
868 * @network_header: Network layer header
869 * @mac_header: Link layer header
870 * @kcov_handle: KCOV remote handle for remote coverage collection
871 * @tail: Tail pointer
873 * @head: Head of buffer
874 * @data: Data head pointer
875 * @truesize: Buffer size
876 * @users: User count - see {datagram,tcp}.c
877 * @extensions: allocated extensions, valid if active_extensions is nonzero
883 /* These two members must be first to match sk_buff_head. */
884 struct sk_buff *next;
885 struct sk_buff *prev;
888 struct net_device *dev;
889 /* Some protocols might use this space to store information,
890 * while device pointer would be NULL.
891 * UDP receive path is one user.
893 unsigned long dev_scratch;
896 struct rb_node rbnode; /* used in netem, ip4 defrag, and tcp stack */
897 struct list_head list;
898 struct llist_node ll_node;
903 int ip_defrag_offset;
908 u64 skb_mstamp_ns; /* earliest departure time */
911 * This is the control buffer. It is free to use for every
912 * layer. Please put your private variables there. If you
913 * want to keep them across layers you have to do a skb_clone()
914 * first. This is owned by whoever has the skb queued ATM.
916 char cb[48] __aligned(8);
920 unsigned long _skb_refdst;
921 void (*destructor)(struct sk_buff *skb);
923 struct list_head tcp_tsorted_anchor;
924 #ifdef CONFIG_NET_SOCK_MSG
925 unsigned long _sk_redir;
929 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
937 /* Following fields are _not_ copied in __copy_skb_header()
938 * Note that queue_mapping is here mostly to fill a hole.
942 /* if you move cloned around you also must adapt those constants */
943 #ifdef __BIG_ENDIAN_BITFIELD
944 #define CLONED_MASK (1 << 7)
946 #define CLONED_MASK 1
948 #define CLONED_OFFSET offsetof(struct sk_buff, __cloned_offset)
951 __u8 __cloned_offset[0];
959 pp_recycle:1; /* page_pool recycle indicator */
960 #ifdef CONFIG_SKB_EXTENSIONS
961 __u8 active_extensions;
964 /* Fields enclosed in headers group are copied
965 * using a single memcpy() in __copy_skb_header()
967 struct_group(headers,
970 __u8 __pkt_type_offset[0];
972 __u8 pkt_type:3; /* see PKT_TYPE_MAX */
980 __u8 wifi_acked_valid:1;
983 /* Indicates the inner headers are valid in the skbuff. */
984 __u8 encapsulation:1;
985 __u8 encap_hdr_csum:1;
989 __u8 __pkt_vlan_present_offset[0];
991 __u8 vlan_present:1; /* See PKT_VLAN_PRESENT_BIT */
992 __u8 csum_complete_sw:1;
994 __u8 dst_pending_confirm:1;
995 __u8 mono_delivery_time:1; /* See SKB_MONO_DELIVERY_TIME_MASK */
996 #ifdef CONFIG_NET_CLS_ACT
997 __u8 tc_skip_classify:1;
998 __u8 tc_at_ingress:1; /* See TC_AT_INGRESS_MASK */
1000 #ifdef CONFIG_IPV6_NDISC_NODETYPE
1001 __u8 ndisc_nodetype:2;
1004 __u8 ipvs_property:1;
1005 __u8 inner_protocol_type:1;
1006 __u8 remcsum_offload:1;
1007 #ifdef CONFIG_NET_SWITCHDEV
1008 __u8 offload_fwd_mark:1;
1009 __u8 offload_l3_fwd_mark:1;
1012 #ifdef CONFIG_NET_REDIRECT
1013 __u8 from_ingress:1;
1015 #ifdef CONFIG_NETFILTER_SKIP_EGRESS
1016 __u8 nf_skip_egress:1;
1018 #ifdef CONFIG_TLS_DEVICE
1022 __u8 csum_not_inet:1;
1024 #ifdef CONFIG_NET_SCHED
1025 __u16 tc_index; /* traffic control index */
1040 #if defined(CONFIG_NET_RX_BUSY_POLL) || defined(CONFIG_XPS)
1042 unsigned int napi_id;
1043 unsigned int sender_cpu;
1046 #ifdef CONFIG_NETWORK_SECMARK
1052 __u32 reserved_tailroom;
1056 __be16 inner_protocol;
1060 __u16 inner_transport_header;
1061 __u16 inner_network_header;
1062 __u16 inner_mac_header;
1065 __u16 transport_header;
1066 __u16 network_header;
1073 ); /* end headers group */
1075 /* These elements must be at the end, see alloc_skb() for details. */
1076 sk_buff_data_t tail;
1078 unsigned char *head,
1080 unsigned int truesize;
1083 #ifdef CONFIG_SKB_EXTENSIONS
1084 /* only useable after checking ->active_extensions != 0 */
1085 struct skb_ext *extensions;
1089 /* if you move pkt_type around you also must adapt those constants */
1090 #ifdef __BIG_ENDIAN_BITFIELD
1091 #define PKT_TYPE_MAX (7 << 5)
1093 #define PKT_TYPE_MAX 7
1095 #define PKT_TYPE_OFFSET offsetof(struct sk_buff, __pkt_type_offset)
1097 /* if you move pkt_vlan_present, tc_at_ingress, or mono_delivery_time
1098 * around, you also must adapt these constants.
1100 #ifdef __BIG_ENDIAN_BITFIELD
1101 #define PKT_VLAN_PRESENT_BIT 7
1102 #define TC_AT_INGRESS_MASK (1 << 0)
1103 #define SKB_MONO_DELIVERY_TIME_MASK (1 << 2)
1105 #define PKT_VLAN_PRESENT_BIT 0
1106 #define TC_AT_INGRESS_MASK (1 << 7)
1107 #define SKB_MONO_DELIVERY_TIME_MASK (1 << 5)
1109 #define PKT_VLAN_PRESENT_OFFSET offsetof(struct sk_buff, __pkt_vlan_present_offset)
1113 * Handling routines are only of interest to the kernel
1116 #define SKB_ALLOC_FCLONE 0x01
1117 #define SKB_ALLOC_RX 0x02
1118 #define SKB_ALLOC_NAPI 0x04
1121 * skb_pfmemalloc - Test if the skb was allocated from PFMEMALLOC reserves
1124 static inline bool skb_pfmemalloc(const struct sk_buff *skb)
1126 return unlikely(skb->pfmemalloc);
1130 * skb might have a dst pointer attached, refcounted or not.
1131 * _skb_refdst low order bit is set if refcount was _not_ taken
1133 #define SKB_DST_NOREF 1UL
1134 #define SKB_DST_PTRMASK ~(SKB_DST_NOREF)
1137 * skb_dst - returns skb dst_entry
1140 * Returns skb dst_entry, regardless of reference taken or not.
1142 static inline struct dst_entry *skb_dst(const struct sk_buff *skb)
1144 /* If refdst was not refcounted, check we still are in a
1145 * rcu_read_lock section
1147 WARN_ON((skb->_skb_refdst & SKB_DST_NOREF) &&
1148 !rcu_read_lock_held() &&
1149 !rcu_read_lock_bh_held());
1150 return (struct dst_entry *)(skb->_skb_refdst & SKB_DST_PTRMASK);
1154 * skb_dst_set - sets skb dst
1158 * Sets skb dst, assuming a reference was taken on dst and should
1159 * be released by skb_dst_drop()
1161 static inline void skb_dst_set(struct sk_buff *skb, struct dst_entry *dst)
1163 skb->slow_gro |= !!dst;
1164 skb->_skb_refdst = (unsigned long)dst;
1168 * skb_dst_set_noref - sets skb dst, hopefully, without taking reference
1172 * Sets skb dst, assuming a reference was not taken on dst.
1173 * If dst entry is cached, we do not take reference and dst_release
1174 * will be avoided by refdst_drop. If dst entry is not cached, we take
1175 * reference, so that last dst_release can destroy the dst immediately.
1177 static inline void skb_dst_set_noref(struct sk_buff *skb, struct dst_entry *dst)
1179 WARN_ON(!rcu_read_lock_held() && !rcu_read_lock_bh_held());
1180 skb->slow_gro |= !!dst;
1181 skb->_skb_refdst = (unsigned long)dst | SKB_DST_NOREF;
1185 * skb_dst_is_noref - Test if skb dst isn't refcounted
1188 static inline bool skb_dst_is_noref(const struct sk_buff *skb)
1190 return (skb->_skb_refdst & SKB_DST_NOREF) && skb_dst(skb);
1194 * skb_rtable - Returns the skb &rtable
1197 static inline struct rtable *skb_rtable(const struct sk_buff *skb)
1199 return (struct rtable *)skb_dst(skb);
1202 /* For mangling skb->pkt_type from user space side from applications
1203 * such as nft, tc, etc, we only allow a conservative subset of
1204 * possible pkt_types to be set.
1206 static inline bool skb_pkt_type_ok(u32 ptype)
1208 return ptype <= PACKET_OTHERHOST;
1212 * skb_napi_id - Returns the skb's NAPI id
1215 static inline unsigned int skb_napi_id(const struct sk_buff *skb)
1217 #ifdef CONFIG_NET_RX_BUSY_POLL
1218 return skb->napi_id;
1225 * skb_unref - decrement the skb's reference count
1228 * Returns true if we can free the skb.
1230 static inline bool skb_unref(struct sk_buff *skb)
1234 if (likely(refcount_read(&skb->users) == 1))
1236 else if (likely(!refcount_dec_and_test(&skb->users)))
1242 void kfree_skb_reason(struct sk_buff *skb, enum skb_drop_reason reason);
1245 * kfree_skb - free an sk_buff with 'NOT_SPECIFIED' reason
1246 * @skb: buffer to free
1248 static inline void kfree_skb(struct sk_buff *skb)
1250 kfree_skb_reason(skb, SKB_DROP_REASON_NOT_SPECIFIED);
1253 void skb_release_head_state(struct sk_buff *skb);
1254 void kfree_skb_list_reason(struct sk_buff *segs,
1255 enum skb_drop_reason reason);
1256 void skb_dump(const char *level, const struct sk_buff *skb, bool full_pkt);
1257 void skb_tx_error(struct sk_buff *skb);
1259 static inline void kfree_skb_list(struct sk_buff *segs)
1261 kfree_skb_list_reason(segs, SKB_DROP_REASON_NOT_SPECIFIED);
1264 #ifdef CONFIG_TRACEPOINTS
1265 void consume_skb(struct sk_buff *skb);
1267 static inline void consume_skb(struct sk_buff *skb)
1269 return kfree_skb(skb);
1273 void __consume_stateless_skb(struct sk_buff *skb);
1274 void __kfree_skb(struct sk_buff *skb);
1275 extern struct kmem_cache *skbuff_head_cache;
1277 void kfree_skb_partial(struct sk_buff *skb, bool head_stolen);
1278 bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
1279 bool *fragstolen, int *delta_truesize);
1281 struct sk_buff *__alloc_skb(unsigned int size, gfp_t priority, int flags,
1283 struct sk_buff *__build_skb(void *data, unsigned int frag_size);
1284 struct sk_buff *build_skb(void *data, unsigned int frag_size);
1285 struct sk_buff *build_skb_around(struct sk_buff *skb,
1286 void *data, unsigned int frag_size);
1288 struct sk_buff *napi_build_skb(void *data, unsigned int frag_size);
1291 * alloc_skb - allocate a network buffer
1292 * @size: size to allocate
1293 * @priority: allocation mask
1295 * This function is a convenient wrapper around __alloc_skb().
1297 static inline struct sk_buff *alloc_skb(unsigned int size,
1300 return __alloc_skb(size, priority, 0, NUMA_NO_NODE);
1303 struct sk_buff *alloc_skb_with_frags(unsigned long header_len,
1304 unsigned long data_len,
1308 struct sk_buff *alloc_skb_for_msg(struct sk_buff *first);
1310 /* Layout of fast clones : [skb1][skb2][fclone_ref] */
1311 struct sk_buff_fclones {
1312 struct sk_buff skb1;
1314 struct sk_buff skb2;
1316 refcount_t fclone_ref;
1320 * skb_fclone_busy - check if fclone is busy
1324 * Returns true if skb is a fast clone, and its clone is not freed.
1325 * Some drivers call skb_orphan() in their ndo_start_xmit(),
1326 * so we also check that this didnt happen.
1328 static inline bool skb_fclone_busy(const struct sock *sk,
1329 const struct sk_buff *skb)
1331 const struct sk_buff_fclones *fclones;
1333 fclones = container_of(skb, struct sk_buff_fclones, skb1);
1335 return skb->fclone == SKB_FCLONE_ORIG &&
1336 refcount_read(&fclones->fclone_ref) > 1 &&
1337 READ_ONCE(fclones->skb2.sk) == sk;
1341 * alloc_skb_fclone - allocate a network buffer from fclone cache
1342 * @size: size to allocate
1343 * @priority: allocation mask
1345 * This function is a convenient wrapper around __alloc_skb().
1347 static inline struct sk_buff *alloc_skb_fclone(unsigned int size,
1350 return __alloc_skb(size, priority, SKB_ALLOC_FCLONE, NUMA_NO_NODE);
1353 struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src);
1354 void skb_headers_offset_update(struct sk_buff *skb, int off);
1355 int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask);
1356 struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t priority);
1357 void skb_copy_header(struct sk_buff *new, const struct sk_buff *old);
1358 struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t priority);
1359 struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom,
1360 gfp_t gfp_mask, bool fclone);
1361 static inline struct sk_buff *__pskb_copy(struct sk_buff *skb, int headroom,
1364 return __pskb_copy_fclone(skb, headroom, gfp_mask, false);
1367 int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail, gfp_t gfp_mask);
1368 struct sk_buff *skb_realloc_headroom(struct sk_buff *skb,
1369 unsigned int headroom);
1370 struct sk_buff *skb_expand_head(struct sk_buff *skb, unsigned int headroom);
1371 struct sk_buff *skb_copy_expand(const struct sk_buff *skb, int newheadroom,
1372 int newtailroom, gfp_t priority);
1373 int __must_check skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg,
1374 int offset, int len);
1375 int __must_check skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg,
1376 int offset, int len);
1377 int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer);
1378 int __skb_pad(struct sk_buff *skb, int pad, bool free_on_error);
1381 * skb_pad - zero pad the tail of an skb
1382 * @skb: buffer to pad
1383 * @pad: space to pad
1385 * Ensure that a buffer is followed by a padding area that is zero
1386 * filled. Used by network drivers which may DMA or transfer data
1387 * beyond the buffer end onto the wire.
1389 * May return error in out of memory cases. The skb is freed on error.
1391 static inline int skb_pad(struct sk_buff *skb, int pad)
1393 return __skb_pad(skb, pad, true);
1395 #define dev_kfree_skb(a) consume_skb(a)
1397 int skb_append_pagefrags(struct sk_buff *skb, struct page *page,
1398 int offset, size_t size);
1400 struct skb_seq_state {
1404 __u32 stepped_offset;
1405 struct sk_buff *root_skb;
1406 struct sk_buff *cur_skb;
1411 void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
1412 unsigned int to, struct skb_seq_state *st);
1413 unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
1414 struct skb_seq_state *st);
1415 void skb_abort_seq_read(struct skb_seq_state *st);
1417 unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
1418 unsigned int to, struct ts_config *config);
1421 * Packet hash types specify the type of hash in skb_set_hash.
1423 * Hash types refer to the protocol layer addresses which are used to
1424 * construct a packet's hash. The hashes are used to differentiate or identify
1425 * flows of the protocol layer for the hash type. Hash types are either
1426 * layer-2 (L2), layer-3 (L3), or layer-4 (L4).
1428 * Properties of hashes:
1430 * 1) Two packets in different flows have different hash values
1431 * 2) Two packets in the same flow should have the same hash value
1433 * A hash at a higher layer is considered to be more specific. A driver should
1434 * set the most specific hash possible.
1436 * A driver cannot indicate a more specific hash than the layer at which a hash
1437 * was computed. For instance an L3 hash cannot be set as an L4 hash.
1439 * A driver may indicate a hash level which is less specific than the
1440 * actual layer the hash was computed on. For instance, a hash computed
1441 * at L4 may be considered an L3 hash. This should only be done if the
1442 * driver can't unambiguously determine that the HW computed the hash at
1443 * the higher layer. Note that the "should" in the second property above
1446 enum pkt_hash_types {
1447 PKT_HASH_TYPE_NONE, /* Undefined type */
1448 PKT_HASH_TYPE_L2, /* Input: src_MAC, dest_MAC */
1449 PKT_HASH_TYPE_L3, /* Input: src_IP, dst_IP */
1450 PKT_HASH_TYPE_L4, /* Input: src_IP, dst_IP, src_port, dst_port */
1453 static inline void skb_clear_hash(struct sk_buff *skb)
1460 static inline void skb_clear_hash_if_not_l4(struct sk_buff *skb)
1463 skb_clear_hash(skb);
1467 __skb_set_hash(struct sk_buff *skb, __u32 hash, bool is_sw, bool is_l4)
1469 skb->l4_hash = is_l4;
1470 skb->sw_hash = is_sw;
1475 skb_set_hash(struct sk_buff *skb, __u32 hash, enum pkt_hash_types type)
1477 /* Used by drivers to set hash from HW */
1478 __skb_set_hash(skb, hash, false, type == PKT_HASH_TYPE_L4);
1482 __skb_set_sw_hash(struct sk_buff *skb, __u32 hash, bool is_l4)
1484 __skb_set_hash(skb, hash, true, is_l4);
1487 void __skb_get_hash(struct sk_buff *skb);
1488 u32 __skb_get_hash_symmetric(const struct sk_buff *skb);
1489 u32 skb_get_poff(const struct sk_buff *skb);
1490 u32 __skb_get_poff(const struct sk_buff *skb, const void *data,
1491 const struct flow_keys_basic *keys, int hlen);
1492 __be32 __skb_flow_get_ports(const struct sk_buff *skb, int thoff, u8 ip_proto,
1493 const void *data, int hlen_proto);
1495 static inline __be32 skb_flow_get_ports(const struct sk_buff *skb,
1496 int thoff, u8 ip_proto)
1498 return __skb_flow_get_ports(skb, thoff, ip_proto, NULL, 0);
1501 void skb_flow_dissector_init(struct flow_dissector *flow_dissector,
1502 const struct flow_dissector_key *key,
1503 unsigned int key_count);
1505 struct bpf_flow_dissector;
1506 bool bpf_flow_dissect(struct bpf_prog *prog, struct bpf_flow_dissector *ctx,
1507 __be16 proto, int nhoff, int hlen, unsigned int flags);
1509 bool __skb_flow_dissect(const struct net *net,
1510 const struct sk_buff *skb,
1511 struct flow_dissector *flow_dissector,
1512 void *target_container, const void *data,
1513 __be16 proto, int nhoff, int hlen, unsigned int flags);
1515 static inline bool skb_flow_dissect(const struct sk_buff *skb,
1516 struct flow_dissector *flow_dissector,
1517 void *target_container, unsigned int flags)
1519 return __skb_flow_dissect(NULL, skb, flow_dissector,
1520 target_container, NULL, 0, 0, 0, flags);
1523 static inline bool skb_flow_dissect_flow_keys(const struct sk_buff *skb,
1524 struct flow_keys *flow,
1527 memset(flow, 0, sizeof(*flow));
1528 return __skb_flow_dissect(NULL, skb, &flow_keys_dissector,
1529 flow, NULL, 0, 0, 0, flags);
1533 skb_flow_dissect_flow_keys_basic(const struct net *net,
1534 const struct sk_buff *skb,
1535 struct flow_keys_basic *flow,
1536 const void *data, __be16 proto,
1537 int nhoff, int hlen, unsigned int flags)
1539 memset(flow, 0, sizeof(*flow));
1540 return __skb_flow_dissect(net, skb, &flow_keys_basic_dissector, flow,
1541 data, proto, nhoff, hlen, flags);
1544 void skb_flow_dissect_meta(const struct sk_buff *skb,
1545 struct flow_dissector *flow_dissector,
1546 void *target_container);
1548 /* Gets a skb connection tracking info, ctinfo map should be a
1549 * map of mapsize to translate enum ip_conntrack_info states
1553 skb_flow_dissect_ct(const struct sk_buff *skb,
1554 struct flow_dissector *flow_dissector,
1555 void *target_container,
1556 u16 *ctinfo_map, size_t mapsize,
1557 bool post_ct, u16 zone);
1559 skb_flow_dissect_tunnel_info(const struct sk_buff *skb,
1560 struct flow_dissector *flow_dissector,
1561 void *target_container);
1563 void skb_flow_dissect_hash(const struct sk_buff *skb,
1564 struct flow_dissector *flow_dissector,
1565 void *target_container);
1567 static inline __u32 skb_get_hash(struct sk_buff *skb)
1569 if (!skb->l4_hash && !skb->sw_hash)
1570 __skb_get_hash(skb);
1575 static inline __u32 skb_get_hash_flowi6(struct sk_buff *skb, const struct flowi6 *fl6)
1577 if (!skb->l4_hash && !skb->sw_hash) {
1578 struct flow_keys keys;
1579 __u32 hash = __get_hash_from_flowi6(fl6, &keys);
1581 __skb_set_sw_hash(skb, hash, flow_keys_have_l4(&keys));
1587 __u32 skb_get_hash_perturb(const struct sk_buff *skb,
1588 const siphash_key_t *perturb);
1590 static inline __u32 skb_get_hash_raw(const struct sk_buff *skb)
1595 static inline void skb_copy_hash(struct sk_buff *to, const struct sk_buff *from)
1597 to->hash = from->hash;
1598 to->sw_hash = from->sw_hash;
1599 to->l4_hash = from->l4_hash;
1602 static inline void skb_copy_decrypted(struct sk_buff *to,
1603 const struct sk_buff *from)
1605 #ifdef CONFIG_TLS_DEVICE
1606 to->decrypted = from->decrypted;
1610 #ifdef NET_SKBUFF_DATA_USES_OFFSET
1611 static inline unsigned char *skb_end_pointer(const struct sk_buff *skb)
1613 return skb->head + skb->end;
1616 static inline unsigned int skb_end_offset(const struct sk_buff *skb)
1621 static inline void skb_set_end_offset(struct sk_buff *skb, unsigned int offset)
1626 static inline unsigned char *skb_end_pointer(const struct sk_buff *skb)
1631 static inline unsigned int skb_end_offset(const struct sk_buff *skb)
1633 return skb->end - skb->head;
1636 static inline void skb_set_end_offset(struct sk_buff *skb, unsigned int offset)
1638 skb->end = skb->head + offset;
1643 #define skb_shinfo(SKB) ((struct skb_shared_info *)(skb_end_pointer(SKB)))
1645 static inline struct skb_shared_hwtstamps *skb_hwtstamps(struct sk_buff *skb)
1647 return &skb_shinfo(skb)->hwtstamps;
1650 static inline struct ubuf_info *skb_zcopy(struct sk_buff *skb)
1652 bool is_zcopy = skb && skb_shinfo(skb)->flags & SKBFL_ZEROCOPY_ENABLE;
1654 return is_zcopy ? skb_uarg(skb) : NULL;
1657 static inline bool skb_zcopy_pure(const struct sk_buff *skb)
1659 return skb_shinfo(skb)->flags & SKBFL_PURE_ZEROCOPY;
1662 static inline bool skb_pure_zcopy_same(const struct sk_buff *skb1,
1663 const struct sk_buff *skb2)
1665 return skb_zcopy_pure(skb1) == skb_zcopy_pure(skb2);
1668 static inline void net_zcopy_get(struct ubuf_info *uarg)
1670 refcount_inc(&uarg->refcnt);
1673 static inline void skb_zcopy_init(struct sk_buff *skb, struct ubuf_info *uarg)
1675 skb_shinfo(skb)->destructor_arg = uarg;
1676 skb_shinfo(skb)->flags |= uarg->flags;
1679 static inline void skb_zcopy_set(struct sk_buff *skb, struct ubuf_info *uarg,
1682 if (skb && uarg && !skb_zcopy(skb)) {
1683 if (unlikely(have_ref && *have_ref))
1686 net_zcopy_get(uarg);
1687 skb_zcopy_init(skb, uarg);
1691 static inline void skb_zcopy_set_nouarg(struct sk_buff *skb, void *val)
1693 skb_shinfo(skb)->destructor_arg = (void *)((uintptr_t) val | 0x1UL);
1694 skb_shinfo(skb)->flags |= SKBFL_ZEROCOPY_FRAG;
1697 static inline bool skb_zcopy_is_nouarg(struct sk_buff *skb)
1699 return (uintptr_t) skb_shinfo(skb)->destructor_arg & 0x1UL;
1702 static inline void *skb_zcopy_get_nouarg(struct sk_buff *skb)
1704 return (void *)((uintptr_t) skb_shinfo(skb)->destructor_arg & ~0x1UL);
1707 static inline void net_zcopy_put(struct ubuf_info *uarg)
1710 uarg->callback(NULL, uarg, true);
1713 static inline void net_zcopy_put_abort(struct ubuf_info *uarg, bool have_uref)
1716 if (uarg->callback == msg_zerocopy_callback)
1717 msg_zerocopy_put_abort(uarg, have_uref);
1719 net_zcopy_put(uarg);
1723 /* Release a reference on a zerocopy structure */
1724 static inline void skb_zcopy_clear(struct sk_buff *skb, bool zerocopy_success)
1726 struct ubuf_info *uarg = skb_zcopy(skb);
1729 if (!skb_zcopy_is_nouarg(skb))
1730 uarg->callback(skb, uarg, zerocopy_success);
1732 skb_shinfo(skb)->flags &= ~SKBFL_ALL_ZEROCOPY;
1736 static inline void skb_mark_not_on_list(struct sk_buff *skb)
1741 /* Iterate through singly-linked GSO fragments of an skb. */
1742 #define skb_list_walk_safe(first, skb, next_skb) \
1743 for ((skb) = (first), (next_skb) = (skb) ? (skb)->next : NULL; (skb); \
1744 (skb) = (next_skb), (next_skb) = (skb) ? (skb)->next : NULL)
1746 static inline void skb_list_del_init(struct sk_buff *skb)
1748 __list_del_entry(&skb->list);
1749 skb_mark_not_on_list(skb);
1753 * skb_queue_empty - check if a queue is empty
1756 * Returns true if the queue is empty, false otherwise.
1758 static inline int skb_queue_empty(const struct sk_buff_head *list)
1760 return list->next == (const struct sk_buff *) list;
1764 * skb_queue_empty_lockless - check if a queue is empty
1767 * Returns true if the queue is empty, false otherwise.
1768 * This variant can be used in lockless contexts.
1770 static inline bool skb_queue_empty_lockless(const struct sk_buff_head *list)
1772 return READ_ONCE(list->next) == (const struct sk_buff *) list;
1777 * skb_queue_is_last - check if skb is the last entry in the queue
1781 * Returns true if @skb is the last buffer on the list.
1783 static inline bool skb_queue_is_last(const struct sk_buff_head *list,
1784 const struct sk_buff *skb)
1786 return skb->next == (const struct sk_buff *) list;
1790 * skb_queue_is_first - check if skb is the first entry in the queue
1794 * Returns true if @skb is the first buffer on the list.
1796 static inline bool skb_queue_is_first(const struct sk_buff_head *list,
1797 const struct sk_buff *skb)
1799 return skb->prev == (const struct sk_buff *) list;
1803 * skb_queue_next - return the next packet in the queue
1805 * @skb: current buffer
1807 * Return the next packet in @list after @skb. It is only valid to
1808 * call this if skb_queue_is_last() evaluates to false.
1810 static inline struct sk_buff *skb_queue_next(const struct sk_buff_head *list,
1811 const struct sk_buff *skb)
1813 /* This BUG_ON may seem severe, but if we just return then we
1814 * are going to dereference garbage.
1816 BUG_ON(skb_queue_is_last(list, skb));
1821 * skb_queue_prev - return the prev packet in the queue
1823 * @skb: current buffer
1825 * Return the prev packet in @list before @skb. It is only valid to
1826 * call this if skb_queue_is_first() evaluates to false.
1828 static inline struct sk_buff *skb_queue_prev(const struct sk_buff_head *list,
1829 const struct sk_buff *skb)
1831 /* This BUG_ON may seem severe, but if we just return then we
1832 * are going to dereference garbage.
1834 BUG_ON(skb_queue_is_first(list, skb));
1839 * skb_get - reference buffer
1840 * @skb: buffer to reference
1842 * Makes another reference to a socket buffer and returns a pointer
1845 static inline struct sk_buff *skb_get(struct sk_buff *skb)
1847 refcount_inc(&skb->users);
1852 * If users == 1, we are the only owner and can avoid redundant atomic changes.
1856 * skb_cloned - is the buffer a clone
1857 * @skb: buffer to check
1859 * Returns true if the buffer was generated with skb_clone() and is
1860 * one of multiple shared copies of the buffer. Cloned buffers are
1861 * shared data so must not be written to under normal circumstances.
1863 static inline int skb_cloned(const struct sk_buff *skb)
1865 return skb->cloned &&
1866 (atomic_read(&skb_shinfo(skb)->dataref) & SKB_DATAREF_MASK) != 1;
1869 static inline int skb_unclone(struct sk_buff *skb, gfp_t pri)
1871 might_sleep_if(gfpflags_allow_blocking(pri));
1873 if (skb_cloned(skb))
1874 return pskb_expand_head(skb, 0, 0, pri);
1879 /* This variant of skb_unclone() makes sure skb->truesize
1880 * and skb_end_offset() are not changed, whenever a new skb->head is needed.
1882 * Indeed there is no guarantee that ksize(kmalloc(X)) == ksize(kmalloc(X))
1883 * when various debugging features are in place.
1885 int __skb_unclone_keeptruesize(struct sk_buff *skb, gfp_t pri);
1886 static inline int skb_unclone_keeptruesize(struct sk_buff *skb, gfp_t pri)
1888 might_sleep_if(gfpflags_allow_blocking(pri));
1890 if (skb_cloned(skb))
1891 return __skb_unclone_keeptruesize(skb, pri);
1896 * skb_header_cloned - is the header a clone
1897 * @skb: buffer to check
1899 * Returns true if modifying the header part of the buffer requires
1900 * the data to be copied.
1902 static inline int skb_header_cloned(const struct sk_buff *skb)
1909 dataref = atomic_read(&skb_shinfo(skb)->dataref);
1910 dataref = (dataref & SKB_DATAREF_MASK) - (dataref >> SKB_DATAREF_SHIFT);
1911 return dataref != 1;
1914 static inline int skb_header_unclone(struct sk_buff *skb, gfp_t pri)
1916 might_sleep_if(gfpflags_allow_blocking(pri));
1918 if (skb_header_cloned(skb))
1919 return pskb_expand_head(skb, 0, 0, pri);
1925 * __skb_header_release - release reference to header
1926 * @skb: buffer to operate on
1928 static inline void __skb_header_release(struct sk_buff *skb)
1931 atomic_set(&skb_shinfo(skb)->dataref, 1 + (1 << SKB_DATAREF_SHIFT));
1936 * skb_shared - is the buffer shared
1937 * @skb: buffer to check
1939 * Returns true if more than one person has a reference to this
1942 static inline int skb_shared(const struct sk_buff *skb)
1944 return refcount_read(&skb->users) != 1;
1948 * skb_share_check - check if buffer is shared and if so clone it
1949 * @skb: buffer to check
1950 * @pri: priority for memory allocation
1952 * If the buffer is shared the buffer is cloned and the old copy
1953 * drops a reference. A new clone with a single reference is returned.
1954 * If the buffer is not shared the original buffer is returned. When
1955 * being called from interrupt status or with spinlocks held pri must
1958 * NULL is returned on a memory allocation failure.
1960 static inline struct sk_buff *skb_share_check(struct sk_buff *skb, gfp_t pri)
1962 might_sleep_if(gfpflags_allow_blocking(pri));
1963 if (skb_shared(skb)) {
1964 struct sk_buff *nskb = skb_clone(skb, pri);
1976 * Copy shared buffers into a new sk_buff. We effectively do COW on
1977 * packets to handle cases where we have a local reader and forward
1978 * and a couple of other messy ones. The normal one is tcpdumping
1979 * a packet thats being forwarded.
1983 * skb_unshare - make a copy of a shared buffer
1984 * @skb: buffer to check
1985 * @pri: priority for memory allocation
1987 * If the socket buffer is a clone then this function creates a new
1988 * copy of the data, drops a reference count on the old copy and returns
1989 * the new copy with the reference count at 1. If the buffer is not a clone
1990 * the original buffer is returned. When called with a spinlock held or
1991 * from interrupt state @pri must be %GFP_ATOMIC
1993 * %NULL is returned on a memory allocation failure.
1995 static inline struct sk_buff *skb_unshare(struct sk_buff *skb,
1998 might_sleep_if(gfpflags_allow_blocking(pri));
1999 if (skb_cloned(skb)) {
2000 struct sk_buff *nskb = skb_copy(skb, pri);
2002 /* Free our shared copy */
2013 * skb_peek - peek at the head of an &sk_buff_head
2014 * @list_: list to peek at
2016 * Peek an &sk_buff. Unlike most other operations you _MUST_
2017 * be careful with this one. A peek leaves the buffer on the
2018 * list and someone else may run off with it. You must hold
2019 * the appropriate locks or have a private queue to do this.
2021 * Returns %NULL for an empty list or a pointer to the head element.
2022 * The reference count is not incremented and the reference is therefore
2023 * volatile. Use with caution.
2025 static inline struct sk_buff *skb_peek(const struct sk_buff_head *list_)
2027 struct sk_buff *skb = list_->next;
2029 if (skb == (struct sk_buff *)list_)
2035 * __skb_peek - peek at the head of a non-empty &sk_buff_head
2036 * @list_: list to peek at
2038 * Like skb_peek(), but the caller knows that the list is not empty.
2040 static inline struct sk_buff *__skb_peek(const struct sk_buff_head *list_)
2046 * skb_peek_next - peek skb following the given one from a queue
2047 * @skb: skb to start from
2048 * @list_: list to peek at
2050 * Returns %NULL when the end of the list is met or a pointer to the
2051 * next element. The reference count is not incremented and the
2052 * reference is therefore volatile. Use with caution.
2054 static inline struct sk_buff *skb_peek_next(struct sk_buff *skb,
2055 const struct sk_buff_head *list_)
2057 struct sk_buff *next = skb->next;
2059 if (next == (struct sk_buff *)list_)
2065 * skb_peek_tail - peek at the tail of an &sk_buff_head
2066 * @list_: list to peek at
2068 * Peek an &sk_buff. Unlike most other operations you _MUST_
2069 * be careful with this one. A peek leaves the buffer on the
2070 * list and someone else may run off with it. You must hold
2071 * the appropriate locks or have a private queue to do this.
2073 * Returns %NULL for an empty list or a pointer to the tail element.
2074 * The reference count is not incremented and the reference is therefore
2075 * volatile. Use with caution.
2077 static inline struct sk_buff *skb_peek_tail(const struct sk_buff_head *list_)
2079 struct sk_buff *skb = READ_ONCE(list_->prev);
2081 if (skb == (struct sk_buff *)list_)
2088 * skb_queue_len - get queue length
2089 * @list_: list to measure
2091 * Return the length of an &sk_buff queue.
2093 static inline __u32 skb_queue_len(const struct sk_buff_head *list_)
2099 * skb_queue_len_lockless - get queue length
2100 * @list_: list to measure
2102 * Return the length of an &sk_buff queue.
2103 * This variant can be used in lockless contexts.
2105 static inline __u32 skb_queue_len_lockless(const struct sk_buff_head *list_)
2107 return READ_ONCE(list_->qlen);
2111 * __skb_queue_head_init - initialize non-spinlock portions of sk_buff_head
2112 * @list: queue to initialize
2114 * This initializes only the list and queue length aspects of
2115 * an sk_buff_head object. This allows to initialize the list
2116 * aspects of an sk_buff_head without reinitializing things like
2117 * the spinlock. It can also be used for on-stack sk_buff_head
2118 * objects where the spinlock is known to not be used.
2120 static inline void __skb_queue_head_init(struct sk_buff_head *list)
2122 list->prev = list->next = (struct sk_buff *)list;
2127 * This function creates a split out lock class for each invocation;
2128 * this is needed for now since a whole lot of users of the skb-queue
2129 * infrastructure in drivers have different locking usage (in hardirq)
2130 * than the networking core (in softirq only). In the long run either the
2131 * network layer or drivers should need annotation to consolidate the
2132 * main types of usage into 3 classes.
2134 static inline void skb_queue_head_init(struct sk_buff_head *list)
2136 spin_lock_init(&list->lock);
2137 __skb_queue_head_init(list);
2140 static inline void skb_queue_head_init_class(struct sk_buff_head *list,
2141 struct lock_class_key *class)
2143 skb_queue_head_init(list);
2144 lockdep_set_class(&list->lock, class);
2148 * Insert an sk_buff on a list.
2150 * The "__skb_xxxx()" functions are the non-atomic ones that
2151 * can only be called with interrupts disabled.
2153 static inline void __skb_insert(struct sk_buff *newsk,
2154 struct sk_buff *prev, struct sk_buff *next,
2155 struct sk_buff_head *list)
2157 /* See skb_queue_empty_lockless() and skb_peek_tail()
2158 * for the opposite READ_ONCE()
2160 WRITE_ONCE(newsk->next, next);
2161 WRITE_ONCE(newsk->prev, prev);
2162 WRITE_ONCE(((struct sk_buff_list *)next)->prev, newsk);
2163 WRITE_ONCE(((struct sk_buff_list *)prev)->next, newsk);
2164 WRITE_ONCE(list->qlen, list->qlen + 1);
2167 static inline void __skb_queue_splice(const struct sk_buff_head *list,
2168 struct sk_buff *prev,
2169 struct sk_buff *next)
2171 struct sk_buff *first = list->next;
2172 struct sk_buff *last = list->prev;
2174 WRITE_ONCE(first->prev, prev);
2175 WRITE_ONCE(prev->next, first);
2177 WRITE_ONCE(last->next, next);
2178 WRITE_ONCE(next->prev, last);
2182 * skb_queue_splice - join two skb lists, this is designed for stacks
2183 * @list: the new list to add
2184 * @head: the place to add it in the first list
2186 static inline void skb_queue_splice(const struct sk_buff_head *list,
2187 struct sk_buff_head *head)
2189 if (!skb_queue_empty(list)) {
2190 __skb_queue_splice(list, (struct sk_buff *) head, head->next);
2191 head->qlen += list->qlen;
2196 * skb_queue_splice_init - join two skb lists and reinitialise the emptied list
2197 * @list: the new list to add
2198 * @head: the place to add it in the first list
2200 * The list at @list is reinitialised
2202 static inline void skb_queue_splice_init(struct sk_buff_head *list,
2203 struct sk_buff_head *head)
2205 if (!skb_queue_empty(list)) {
2206 __skb_queue_splice(list, (struct sk_buff *) head, head->next);
2207 head->qlen += list->qlen;
2208 __skb_queue_head_init(list);
2213 * skb_queue_splice_tail - join two skb lists, each list being a queue
2214 * @list: the new list to add
2215 * @head: the place to add it in the first list
2217 static inline void skb_queue_splice_tail(const struct sk_buff_head *list,
2218 struct sk_buff_head *head)
2220 if (!skb_queue_empty(list)) {
2221 __skb_queue_splice(list, head->prev, (struct sk_buff *) head);
2222 head->qlen += list->qlen;
2227 * skb_queue_splice_tail_init - join two skb lists and reinitialise the emptied list
2228 * @list: the new list to add
2229 * @head: the place to add it in the first list
2231 * Each of the lists is a queue.
2232 * The list at @list is reinitialised
2234 static inline void skb_queue_splice_tail_init(struct sk_buff_head *list,
2235 struct sk_buff_head *head)
2237 if (!skb_queue_empty(list)) {
2238 __skb_queue_splice(list, head->prev, (struct sk_buff *) head);
2239 head->qlen += list->qlen;
2240 __skb_queue_head_init(list);
2245 * __skb_queue_after - queue a buffer at the list head
2246 * @list: list to use
2247 * @prev: place after this buffer
2248 * @newsk: buffer to queue
2250 * Queue a buffer int the middle of a list. This function takes no locks
2251 * and you must therefore hold required locks before calling it.
2253 * A buffer cannot be placed on two lists at the same time.
2255 static inline void __skb_queue_after(struct sk_buff_head *list,
2256 struct sk_buff *prev,
2257 struct sk_buff *newsk)
2259 __skb_insert(newsk, prev, ((struct sk_buff_list *)prev)->next, list);
2262 void skb_append(struct sk_buff *old, struct sk_buff *newsk,
2263 struct sk_buff_head *list);
2265 static inline void __skb_queue_before(struct sk_buff_head *list,
2266 struct sk_buff *next,
2267 struct sk_buff *newsk)
2269 __skb_insert(newsk, ((struct sk_buff_list *)next)->prev, next, list);
2273 * __skb_queue_head - queue a buffer at the list head
2274 * @list: list to use
2275 * @newsk: buffer to queue
2277 * Queue a buffer at the start of a list. This function takes no locks
2278 * and you must therefore hold required locks before calling it.
2280 * A buffer cannot be placed on two lists at the same time.
2282 static inline void __skb_queue_head(struct sk_buff_head *list,
2283 struct sk_buff *newsk)
2285 __skb_queue_after(list, (struct sk_buff *)list, newsk);
2287 void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk);
2290 * __skb_queue_tail - queue a buffer at the list tail
2291 * @list: list to use
2292 * @newsk: buffer to queue
2294 * Queue a buffer at the end of a list. This function takes no locks
2295 * and you must therefore hold required locks before calling it.
2297 * A buffer cannot be placed on two lists at the same time.
2299 static inline void __skb_queue_tail(struct sk_buff_head *list,
2300 struct sk_buff *newsk)
2302 __skb_queue_before(list, (struct sk_buff *)list, newsk);
2304 void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk);
2307 * remove sk_buff from list. _Must_ be called atomically, and with
2310 void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list);
2311 static inline void __skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
2313 struct sk_buff *next, *prev;
2315 WRITE_ONCE(list->qlen, list->qlen - 1);
2318 skb->next = skb->prev = NULL;
2319 WRITE_ONCE(next->prev, prev);
2320 WRITE_ONCE(prev->next, next);
2324 * __skb_dequeue - remove from the head of the queue
2325 * @list: list to dequeue from
2327 * Remove the head of the list. This function does not take any locks
2328 * so must be used with appropriate locks held only. The head item is
2329 * returned or %NULL if the list is empty.
2331 static inline struct sk_buff *__skb_dequeue(struct sk_buff_head *list)
2333 struct sk_buff *skb = skb_peek(list);
2335 __skb_unlink(skb, list);
2338 struct sk_buff *skb_dequeue(struct sk_buff_head *list);
2341 * __skb_dequeue_tail - remove from the tail of the queue
2342 * @list: list to dequeue from
2344 * Remove the tail of the list. This function does not take any locks
2345 * so must be used with appropriate locks held only. The tail item is
2346 * returned or %NULL if the list is empty.
2348 static inline struct sk_buff *__skb_dequeue_tail(struct sk_buff_head *list)
2350 struct sk_buff *skb = skb_peek_tail(list);
2352 __skb_unlink(skb, list);
2355 struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list);
2358 static inline bool skb_is_nonlinear(const struct sk_buff *skb)
2360 return skb->data_len;
2363 static inline unsigned int skb_headlen(const struct sk_buff *skb)
2365 return skb->len - skb->data_len;
2368 static inline unsigned int __skb_pagelen(const struct sk_buff *skb)
2370 unsigned int i, len = 0;
2372 for (i = skb_shinfo(skb)->nr_frags - 1; (int)i >= 0; i--)
2373 len += skb_frag_size(&skb_shinfo(skb)->frags[i]);
2377 static inline unsigned int skb_pagelen(const struct sk_buff *skb)
2379 return skb_headlen(skb) + __skb_pagelen(skb);
2383 * __skb_fill_page_desc - initialise a paged fragment in an skb
2384 * @skb: buffer containing fragment to be initialised
2385 * @i: paged fragment index to initialise
2386 * @page: the page to use for this fragment
2387 * @off: the offset to the data with @page
2388 * @size: the length of the data
2390 * Initialises the @i'th fragment of @skb to point to &size bytes at
2391 * offset @off within @page.
2393 * Does not take any additional reference on the fragment.
2395 static inline void __skb_fill_page_desc(struct sk_buff *skb, int i,
2396 struct page *page, int off, int size)
2398 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2401 * Propagate page pfmemalloc to the skb if we can. The problem is
2402 * that not all callers have unique ownership of the page but rely
2403 * on page_is_pfmemalloc doing the right thing(tm).
2405 frag->bv_page = page;
2406 frag->bv_offset = off;
2407 skb_frag_size_set(frag, size);
2409 page = compound_head(page);
2410 if (page_is_pfmemalloc(page))
2411 skb->pfmemalloc = true;
2415 * skb_fill_page_desc - initialise a paged fragment in an skb
2416 * @skb: buffer containing fragment to be initialised
2417 * @i: paged fragment index to initialise
2418 * @page: the page to use for this fragment
2419 * @off: the offset to the data with @page
2420 * @size: the length of the data
2422 * As per __skb_fill_page_desc() -- initialises the @i'th fragment of
2423 * @skb to point to @size bytes at offset @off within @page. In
2424 * addition updates @skb such that @i is the last fragment.
2426 * Does not take any additional reference on the fragment.
2428 static inline void skb_fill_page_desc(struct sk_buff *skb, int i,
2429 struct page *page, int off, int size)
2431 __skb_fill_page_desc(skb, i, page, off, size);
2432 skb_shinfo(skb)->nr_frags = i + 1;
2435 void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off,
2436 int size, unsigned int truesize);
2438 void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size,
2439 unsigned int truesize);
2441 #define SKB_LINEAR_ASSERT(skb) BUG_ON(skb_is_nonlinear(skb))
2443 #ifdef NET_SKBUFF_DATA_USES_OFFSET
2444 static inline unsigned char *skb_tail_pointer(const struct sk_buff *skb)
2446 return skb->head + skb->tail;
2449 static inline void skb_reset_tail_pointer(struct sk_buff *skb)
2451 skb->tail = skb->data - skb->head;
2454 static inline void skb_set_tail_pointer(struct sk_buff *skb, const int offset)
2456 skb_reset_tail_pointer(skb);
2457 skb->tail += offset;
2460 #else /* NET_SKBUFF_DATA_USES_OFFSET */
2461 static inline unsigned char *skb_tail_pointer(const struct sk_buff *skb)
2466 static inline void skb_reset_tail_pointer(struct sk_buff *skb)
2468 skb->tail = skb->data;
2471 static inline void skb_set_tail_pointer(struct sk_buff *skb, const int offset)
2473 skb->tail = skb->data + offset;
2476 #endif /* NET_SKBUFF_DATA_USES_OFFSET */
2479 * Add data to an sk_buff
2481 void *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len);
2482 void *skb_put(struct sk_buff *skb, unsigned int len);
2483 static inline void *__skb_put(struct sk_buff *skb, unsigned int len)
2485 void *tmp = skb_tail_pointer(skb);
2486 SKB_LINEAR_ASSERT(skb);
2492 static inline void *__skb_put_zero(struct sk_buff *skb, unsigned int len)
2494 void *tmp = __skb_put(skb, len);
2496 memset(tmp, 0, len);
2500 static inline void *__skb_put_data(struct sk_buff *skb, const void *data,
2503 void *tmp = __skb_put(skb, len);
2505 memcpy(tmp, data, len);
2509 static inline void __skb_put_u8(struct sk_buff *skb, u8 val)
2511 *(u8 *)__skb_put(skb, 1) = val;
2514 static inline void *skb_put_zero(struct sk_buff *skb, unsigned int len)
2516 void *tmp = skb_put(skb, len);
2518 memset(tmp, 0, len);
2523 static inline void *skb_put_data(struct sk_buff *skb, const void *data,
2526 void *tmp = skb_put(skb, len);
2528 memcpy(tmp, data, len);
2533 static inline void skb_put_u8(struct sk_buff *skb, u8 val)
2535 *(u8 *)skb_put(skb, 1) = val;
2538 void *skb_push(struct sk_buff *skb, unsigned int len);
2539 static inline void *__skb_push(struct sk_buff *skb, unsigned int len)
2546 void *skb_pull(struct sk_buff *skb, unsigned int len);
2547 static inline void *__skb_pull(struct sk_buff *skb, unsigned int len)
2550 BUG_ON(skb->len < skb->data_len);
2551 return skb->data += len;
2554 static inline void *skb_pull_inline(struct sk_buff *skb, unsigned int len)
2556 return unlikely(len > skb->len) ? NULL : __skb_pull(skb, len);
2559 void *skb_pull_data(struct sk_buff *skb, size_t len);
2561 void *__pskb_pull_tail(struct sk_buff *skb, int delta);
2563 static inline void *__pskb_pull(struct sk_buff *skb, unsigned int len)
2565 if (len > skb_headlen(skb) &&
2566 !__pskb_pull_tail(skb, len - skb_headlen(skb)))
2569 return skb->data += len;
2572 static inline void *pskb_pull(struct sk_buff *skb, unsigned int len)
2574 return unlikely(len > skb->len) ? NULL : __pskb_pull(skb, len);
2577 static inline bool pskb_may_pull(struct sk_buff *skb, unsigned int len)
2579 if (likely(len <= skb_headlen(skb)))
2581 if (unlikely(len > skb->len))
2583 return __pskb_pull_tail(skb, len - skb_headlen(skb)) != NULL;
2586 void skb_condense(struct sk_buff *skb);
2589 * skb_headroom - bytes at buffer head
2590 * @skb: buffer to check
2592 * Return the number of bytes of free space at the head of an &sk_buff.
2594 static inline unsigned int skb_headroom(const struct sk_buff *skb)
2596 return skb->data - skb->head;
2600 * skb_tailroom - bytes at buffer end
2601 * @skb: buffer to check
2603 * Return the number of bytes of free space at the tail of an sk_buff
2605 static inline int skb_tailroom(const struct sk_buff *skb)
2607 return skb_is_nonlinear(skb) ? 0 : skb->end - skb->tail;
2611 * skb_availroom - bytes at buffer end
2612 * @skb: buffer to check
2614 * Return the number of bytes of free space at the tail of an sk_buff
2615 * allocated by sk_stream_alloc()
2617 static inline int skb_availroom(const struct sk_buff *skb)
2619 if (skb_is_nonlinear(skb))
2622 return skb->end - skb->tail - skb->reserved_tailroom;
2626 * skb_reserve - adjust headroom
2627 * @skb: buffer to alter
2628 * @len: bytes to move
2630 * Increase the headroom of an empty &sk_buff by reducing the tail
2631 * room. This is only allowed for an empty buffer.
2633 static inline void skb_reserve(struct sk_buff *skb, int len)
2640 * skb_tailroom_reserve - adjust reserved_tailroom
2641 * @skb: buffer to alter
2642 * @mtu: maximum amount of headlen permitted
2643 * @needed_tailroom: minimum amount of reserved_tailroom
2645 * Set reserved_tailroom so that headlen can be as large as possible but
2646 * not larger than mtu and tailroom cannot be smaller than
2648 * The required headroom should already have been reserved before using
2651 static inline void skb_tailroom_reserve(struct sk_buff *skb, unsigned int mtu,
2652 unsigned int needed_tailroom)
2654 SKB_LINEAR_ASSERT(skb);
2655 if (mtu < skb_tailroom(skb) - needed_tailroom)
2656 /* use at most mtu */
2657 skb->reserved_tailroom = skb_tailroom(skb) - mtu;
2659 /* use up to all available space */
2660 skb->reserved_tailroom = needed_tailroom;
2663 #define ENCAP_TYPE_ETHER 0
2664 #define ENCAP_TYPE_IPPROTO 1
2666 static inline void skb_set_inner_protocol(struct sk_buff *skb,
2669 skb->inner_protocol = protocol;
2670 skb->inner_protocol_type = ENCAP_TYPE_ETHER;
2673 static inline void skb_set_inner_ipproto(struct sk_buff *skb,
2676 skb->inner_ipproto = ipproto;
2677 skb->inner_protocol_type = ENCAP_TYPE_IPPROTO;
2680 static inline void skb_reset_inner_headers(struct sk_buff *skb)
2682 skb->inner_mac_header = skb->mac_header;
2683 skb->inner_network_header = skb->network_header;
2684 skb->inner_transport_header = skb->transport_header;
2687 static inline void skb_reset_mac_len(struct sk_buff *skb)
2689 skb->mac_len = skb->network_header - skb->mac_header;
2692 static inline unsigned char *skb_inner_transport_header(const struct sk_buff
2695 return skb->head + skb->inner_transport_header;
2698 static inline int skb_inner_transport_offset(const struct sk_buff *skb)
2700 return skb_inner_transport_header(skb) - skb->data;
2703 static inline void skb_reset_inner_transport_header(struct sk_buff *skb)
2705 skb->inner_transport_header = skb->data - skb->head;
2708 static inline void skb_set_inner_transport_header(struct sk_buff *skb,
2711 skb_reset_inner_transport_header(skb);
2712 skb->inner_transport_header += offset;
2715 static inline unsigned char *skb_inner_network_header(const struct sk_buff *skb)
2717 return skb->head + skb->inner_network_header;
2720 static inline void skb_reset_inner_network_header(struct sk_buff *skb)
2722 skb->inner_network_header = skb->data - skb->head;
2725 static inline void skb_set_inner_network_header(struct sk_buff *skb,
2728 skb_reset_inner_network_header(skb);
2729 skb->inner_network_header += offset;
2732 static inline unsigned char *skb_inner_mac_header(const struct sk_buff *skb)
2734 return skb->head + skb->inner_mac_header;
2737 static inline void skb_reset_inner_mac_header(struct sk_buff *skb)
2739 skb->inner_mac_header = skb->data - skb->head;
2742 static inline void skb_set_inner_mac_header(struct sk_buff *skb,
2745 skb_reset_inner_mac_header(skb);
2746 skb->inner_mac_header += offset;
2748 static inline bool skb_transport_header_was_set(const struct sk_buff *skb)
2750 return skb->transport_header != (typeof(skb->transport_header))~0U;
2753 static inline unsigned char *skb_transport_header(const struct sk_buff *skb)
2755 return skb->head + skb->transport_header;
2758 static inline void skb_reset_transport_header(struct sk_buff *skb)
2760 skb->transport_header = skb->data - skb->head;
2763 static inline void skb_set_transport_header(struct sk_buff *skb,
2766 skb_reset_transport_header(skb);
2767 skb->transport_header += offset;
2770 static inline unsigned char *skb_network_header(const struct sk_buff *skb)
2772 return skb->head + skb->network_header;
2775 static inline void skb_reset_network_header(struct sk_buff *skb)
2777 skb->network_header = skb->data - skb->head;
2780 static inline void skb_set_network_header(struct sk_buff *skb, const int offset)
2782 skb_reset_network_header(skb);
2783 skb->network_header += offset;
2786 static inline unsigned char *skb_mac_header(const struct sk_buff *skb)
2788 return skb->head + skb->mac_header;
2791 static inline int skb_mac_offset(const struct sk_buff *skb)
2793 return skb_mac_header(skb) - skb->data;
2796 static inline u32 skb_mac_header_len(const struct sk_buff *skb)
2798 return skb->network_header - skb->mac_header;
2801 static inline int skb_mac_header_was_set(const struct sk_buff *skb)
2803 return skb->mac_header != (typeof(skb->mac_header))~0U;
2806 static inline void skb_unset_mac_header(struct sk_buff *skb)
2808 skb->mac_header = (typeof(skb->mac_header))~0U;
2811 static inline void skb_reset_mac_header(struct sk_buff *skb)
2813 skb->mac_header = skb->data - skb->head;
2816 static inline void skb_set_mac_header(struct sk_buff *skb, const int offset)
2818 skb_reset_mac_header(skb);
2819 skb->mac_header += offset;
2822 static inline void skb_pop_mac_header(struct sk_buff *skb)
2824 skb->mac_header = skb->network_header;
2827 static inline void skb_probe_transport_header(struct sk_buff *skb)
2829 struct flow_keys_basic keys;
2831 if (skb_transport_header_was_set(skb))
2834 if (skb_flow_dissect_flow_keys_basic(NULL, skb, &keys,
2836 skb_set_transport_header(skb, keys.control.thoff);
2839 static inline void skb_mac_header_rebuild(struct sk_buff *skb)
2841 if (skb_mac_header_was_set(skb)) {
2842 const unsigned char *old_mac = skb_mac_header(skb);
2844 skb_set_mac_header(skb, -skb->mac_len);
2845 memmove(skb_mac_header(skb), old_mac, skb->mac_len);
2849 static inline int skb_checksum_start_offset(const struct sk_buff *skb)
2851 return skb->csum_start - skb_headroom(skb);
2854 static inline unsigned char *skb_checksum_start(const struct sk_buff *skb)
2856 return skb->head + skb->csum_start;
2859 static inline int skb_transport_offset(const struct sk_buff *skb)
2861 return skb_transport_header(skb) - skb->data;
2864 static inline u32 skb_network_header_len(const struct sk_buff *skb)
2866 return skb->transport_header - skb->network_header;
2869 static inline u32 skb_inner_network_header_len(const struct sk_buff *skb)
2871 return skb->inner_transport_header - skb->inner_network_header;
2874 static inline int skb_network_offset(const struct sk_buff *skb)
2876 return skb_network_header(skb) - skb->data;
2879 static inline int skb_inner_network_offset(const struct sk_buff *skb)
2881 return skb_inner_network_header(skb) - skb->data;
2884 static inline int pskb_network_may_pull(struct sk_buff *skb, unsigned int len)
2886 return pskb_may_pull(skb, skb_network_offset(skb) + len);
2890 * CPUs often take a performance hit when accessing unaligned memory
2891 * locations. The actual performance hit varies, it can be small if the
2892 * hardware handles it or large if we have to take an exception and fix it
2895 * Since an ethernet header is 14 bytes network drivers often end up with
2896 * the IP header at an unaligned offset. The IP header can be aligned by
2897 * shifting the start of the packet by 2 bytes. Drivers should do this
2900 * skb_reserve(skb, NET_IP_ALIGN);
2902 * The downside to this alignment of the IP header is that the DMA is now
2903 * unaligned. On some architectures the cost of an unaligned DMA is high
2904 * and this cost outweighs the gains made by aligning the IP header.
2906 * Since this trade off varies between architectures, we allow NET_IP_ALIGN
2909 #ifndef NET_IP_ALIGN
2910 #define NET_IP_ALIGN 2
2914 * The networking layer reserves some headroom in skb data (via
2915 * dev_alloc_skb). This is used to avoid having to reallocate skb data when
2916 * the header has to grow. In the default case, if the header has to grow
2917 * 32 bytes or less we avoid the reallocation.
2919 * Unfortunately this headroom changes the DMA alignment of the resulting
2920 * network packet. As for NET_IP_ALIGN, this unaligned DMA is expensive
2921 * on some architectures. An architecture can override this value,
2922 * perhaps setting it to a cacheline in size (since that will maintain
2923 * cacheline alignment of the DMA). It must be a power of 2.
2925 * Various parts of the networking layer expect at least 32 bytes of
2926 * headroom, you should not reduce this.
2928 * Using max(32, L1_CACHE_BYTES) makes sense (especially with RPS)
2929 * to reduce average number of cache lines per packet.
2930 * get_rps_cpu() for example only access one 64 bytes aligned block :
2931 * NET_IP_ALIGN(2) + ethernet_header(14) + IP_header(20/40) + ports(8)
2934 #define NET_SKB_PAD max(32, L1_CACHE_BYTES)
2937 int ___pskb_trim(struct sk_buff *skb, unsigned int len);
2939 static inline void __skb_set_length(struct sk_buff *skb, unsigned int len)
2941 if (WARN_ON(skb_is_nonlinear(skb)))
2944 skb_set_tail_pointer(skb, len);
2947 static inline void __skb_trim(struct sk_buff *skb, unsigned int len)
2949 __skb_set_length(skb, len);
2952 void skb_trim(struct sk_buff *skb, unsigned int len);
2954 static inline int __pskb_trim(struct sk_buff *skb, unsigned int len)
2957 return ___pskb_trim(skb, len);
2958 __skb_trim(skb, len);
2962 static inline int pskb_trim(struct sk_buff *skb, unsigned int len)
2964 return (len < skb->len) ? __pskb_trim(skb, len) : 0;
2968 * pskb_trim_unique - remove end from a paged unique (not cloned) buffer
2969 * @skb: buffer to alter
2972 * This is identical to pskb_trim except that the caller knows that
2973 * the skb is not cloned so we should never get an error due to out-
2976 static inline void pskb_trim_unique(struct sk_buff *skb, unsigned int len)
2978 int err = pskb_trim(skb, len);
2982 static inline int __skb_grow(struct sk_buff *skb, unsigned int len)
2984 unsigned int diff = len - skb->len;
2986 if (skb_tailroom(skb) < diff) {
2987 int ret = pskb_expand_head(skb, 0, diff - skb_tailroom(skb),
2992 __skb_set_length(skb, len);
2997 * skb_orphan - orphan a buffer
2998 * @skb: buffer to orphan
3000 * If a buffer currently has an owner then we call the owner's
3001 * destructor function and make the @skb unowned. The buffer continues
3002 * to exist but is no longer charged to its former owner.
3004 static inline void skb_orphan(struct sk_buff *skb)
3006 if (skb->destructor) {
3007 skb->destructor(skb);
3008 skb->destructor = NULL;
3016 * skb_orphan_frags - orphan the frags contained in a buffer
3017 * @skb: buffer to orphan frags from
3018 * @gfp_mask: allocation mask for replacement pages
3020 * For each frag in the SKB which needs a destructor (i.e. has an
3021 * owner) create a copy of that frag and release the original
3022 * page by calling the destructor.
3024 static inline int skb_orphan_frags(struct sk_buff *skb, gfp_t gfp_mask)
3026 if (likely(!skb_zcopy(skb)))
3028 if (!skb_zcopy_is_nouarg(skb) &&
3029 skb_uarg(skb)->callback == msg_zerocopy_callback)
3031 return skb_copy_ubufs(skb, gfp_mask);
3034 /* Frags must be orphaned, even if refcounted, if skb might loop to rx path */
3035 static inline int skb_orphan_frags_rx(struct sk_buff *skb, gfp_t gfp_mask)
3037 if (likely(!skb_zcopy(skb)))
3039 return skb_copy_ubufs(skb, gfp_mask);
3043 * __skb_queue_purge - empty a list
3044 * @list: list to empty
3046 * Delete all buffers on an &sk_buff list. Each buffer is removed from
3047 * the list and one reference dropped. This function does not take the
3048 * list lock and the caller must hold the relevant locks to use it.
3050 static inline void __skb_queue_purge(struct sk_buff_head *list)
3052 struct sk_buff *skb;
3053 while ((skb = __skb_dequeue(list)) != NULL)
3056 void skb_queue_purge(struct sk_buff_head *list);
3058 unsigned int skb_rbtree_purge(struct rb_root *root);
3060 void *__netdev_alloc_frag_align(unsigned int fragsz, unsigned int align_mask);
3063 * netdev_alloc_frag - allocate a page fragment
3064 * @fragsz: fragment size
3066 * Allocates a frag from a page for receive buffer.
3067 * Uses GFP_ATOMIC allocations.
3069 static inline void *netdev_alloc_frag(unsigned int fragsz)
3071 return __netdev_alloc_frag_align(fragsz, ~0u);
3074 static inline void *netdev_alloc_frag_align(unsigned int fragsz,
3077 WARN_ON_ONCE(!is_power_of_2(align));
3078 return __netdev_alloc_frag_align(fragsz, -align);
3081 struct sk_buff *__netdev_alloc_skb(struct net_device *dev, unsigned int length,
3085 * netdev_alloc_skb - allocate an skbuff for rx on a specific device
3086 * @dev: network device to receive on
3087 * @length: length to allocate
3089 * Allocate a new &sk_buff and assign it a usage count of one. The
3090 * buffer has unspecified headroom built in. Users should allocate
3091 * the headroom they think they need without accounting for the
3092 * built in space. The built in space is used for optimisations.
3094 * %NULL is returned if there is no free memory. Although this function
3095 * allocates memory it can be called from an interrupt.
3097 static inline struct sk_buff *netdev_alloc_skb(struct net_device *dev,
3098 unsigned int length)
3100 return __netdev_alloc_skb(dev, length, GFP_ATOMIC);
3103 /* legacy helper around __netdev_alloc_skb() */
3104 static inline struct sk_buff *__dev_alloc_skb(unsigned int length,
3107 return __netdev_alloc_skb(NULL, length, gfp_mask);
3110 /* legacy helper around netdev_alloc_skb() */
3111 static inline struct sk_buff *dev_alloc_skb(unsigned int length)
3113 return netdev_alloc_skb(NULL, length);
3117 static inline struct sk_buff *__netdev_alloc_skb_ip_align(struct net_device *dev,
3118 unsigned int length, gfp_t gfp)
3120 struct sk_buff *skb = __netdev_alloc_skb(dev, length + NET_IP_ALIGN, gfp);
3122 if (NET_IP_ALIGN && skb)
3123 skb_reserve(skb, NET_IP_ALIGN);
3127 static inline struct sk_buff *netdev_alloc_skb_ip_align(struct net_device *dev,
3128 unsigned int length)
3130 return __netdev_alloc_skb_ip_align(dev, length, GFP_ATOMIC);
3133 static inline void skb_free_frag(void *addr)
3135 page_frag_free(addr);
3138 void *__napi_alloc_frag_align(unsigned int fragsz, unsigned int align_mask);
3140 static inline void *napi_alloc_frag(unsigned int fragsz)
3142 return __napi_alloc_frag_align(fragsz, ~0u);
3145 static inline void *napi_alloc_frag_align(unsigned int fragsz,
3148 WARN_ON_ONCE(!is_power_of_2(align));
3149 return __napi_alloc_frag_align(fragsz, -align);
3152 struct sk_buff *__napi_alloc_skb(struct napi_struct *napi,
3153 unsigned int length, gfp_t gfp_mask);
3154 static inline struct sk_buff *napi_alloc_skb(struct napi_struct *napi,
3155 unsigned int length)
3157 return __napi_alloc_skb(napi, length, GFP_ATOMIC);
3159 void napi_consume_skb(struct sk_buff *skb, int budget);
3161 void napi_skb_free_stolen_head(struct sk_buff *skb);
3162 void __kfree_skb_defer(struct sk_buff *skb);
3165 * __dev_alloc_pages - allocate page for network Rx
3166 * @gfp_mask: allocation priority. Set __GFP_NOMEMALLOC if not for network Rx
3167 * @order: size of the allocation
3169 * Allocate a new page.
3171 * %NULL is returned if there is no free memory.
3173 static inline struct page *__dev_alloc_pages(gfp_t gfp_mask,
3176 /* This piece of code contains several assumptions.
3177 * 1. This is for device Rx, therefor a cold page is preferred.
3178 * 2. The expectation is the user wants a compound page.
3179 * 3. If requesting a order 0 page it will not be compound
3180 * due to the check to see if order has a value in prep_new_page
3181 * 4. __GFP_MEMALLOC is ignored if __GFP_NOMEMALLOC is set due to
3182 * code in gfp_to_alloc_flags that should be enforcing this.
3184 gfp_mask |= __GFP_COMP | __GFP_MEMALLOC;
3186 return alloc_pages_node(NUMA_NO_NODE, gfp_mask, order);
3189 static inline struct page *dev_alloc_pages(unsigned int order)
3191 return __dev_alloc_pages(GFP_ATOMIC | __GFP_NOWARN, order);
3195 * __dev_alloc_page - allocate a page for network Rx
3196 * @gfp_mask: allocation priority. Set __GFP_NOMEMALLOC if not for network Rx
3198 * Allocate a new page.
3200 * %NULL is returned if there is no free memory.
3202 static inline struct page *__dev_alloc_page(gfp_t gfp_mask)
3204 return __dev_alloc_pages(gfp_mask, 0);
3207 static inline struct page *dev_alloc_page(void)
3209 return dev_alloc_pages(0);
3213 * dev_page_is_reusable - check whether a page can be reused for network Rx
3214 * @page: the page to test
3216 * A page shouldn't be considered for reusing/recycling if it was allocated
3217 * under memory pressure or at a distant memory node.
3219 * Returns false if this page should be returned to page allocator, true
3222 static inline bool dev_page_is_reusable(const struct page *page)
3224 return likely(page_to_nid(page) == numa_mem_id() &&
3225 !page_is_pfmemalloc(page));
3229 * skb_propagate_pfmemalloc - Propagate pfmemalloc if skb is allocated after RX page
3230 * @page: The page that was allocated from skb_alloc_page
3231 * @skb: The skb that may need pfmemalloc set
3233 static inline void skb_propagate_pfmemalloc(const struct page *page,
3234 struct sk_buff *skb)
3236 if (page_is_pfmemalloc(page))
3237 skb->pfmemalloc = true;
3241 * skb_frag_off() - Returns the offset of a skb fragment
3242 * @frag: the paged fragment
3244 static inline unsigned int skb_frag_off(const skb_frag_t *frag)
3246 return frag->bv_offset;
3250 * skb_frag_off_add() - Increments the offset of a skb fragment by @delta
3251 * @frag: skb fragment
3252 * @delta: value to add
3254 static inline void skb_frag_off_add(skb_frag_t *frag, int delta)
3256 frag->bv_offset += delta;
3260 * skb_frag_off_set() - Sets the offset of a skb fragment
3261 * @frag: skb fragment
3262 * @offset: offset of fragment
3264 static inline void skb_frag_off_set(skb_frag_t *frag, unsigned int offset)
3266 frag->bv_offset = offset;
3270 * skb_frag_off_copy() - Sets the offset of a skb fragment from another fragment
3271 * @fragto: skb fragment where offset is set
3272 * @fragfrom: skb fragment offset is copied from
3274 static inline void skb_frag_off_copy(skb_frag_t *fragto,
3275 const skb_frag_t *fragfrom)
3277 fragto->bv_offset = fragfrom->bv_offset;
3281 * skb_frag_page - retrieve the page referred to by a paged fragment
3282 * @frag: the paged fragment
3284 * Returns the &struct page associated with @frag.
3286 static inline struct page *skb_frag_page(const skb_frag_t *frag)
3288 return frag->bv_page;
3292 * __skb_frag_ref - take an addition reference on a paged fragment.
3293 * @frag: the paged fragment
3295 * Takes an additional reference on the paged fragment @frag.
3297 static inline void __skb_frag_ref(skb_frag_t *frag)
3299 get_page(skb_frag_page(frag));
3303 * skb_frag_ref - take an addition reference on a paged fragment of an skb.
3305 * @f: the fragment offset.
3307 * Takes an additional reference on the @f'th paged fragment of @skb.
3309 static inline void skb_frag_ref(struct sk_buff *skb, int f)
3311 __skb_frag_ref(&skb_shinfo(skb)->frags[f]);
3315 * __skb_frag_unref - release a reference on a paged fragment.
3316 * @frag: the paged fragment
3317 * @recycle: recycle the page if allocated via page_pool
3319 * Releases a reference on the paged fragment @frag
3320 * or recycles the page via the page_pool API.
3322 static inline void __skb_frag_unref(skb_frag_t *frag, bool recycle)
3324 struct page *page = skb_frag_page(frag);
3326 #ifdef CONFIG_PAGE_POOL
3327 if (recycle && page_pool_return_skb_page(page))
3334 * skb_frag_unref - release a reference on a paged fragment of an skb.
3336 * @f: the fragment offset
3338 * Releases a reference on the @f'th paged fragment of @skb.
3340 static inline void skb_frag_unref(struct sk_buff *skb, int f)
3342 __skb_frag_unref(&skb_shinfo(skb)->frags[f], skb->pp_recycle);
3346 * skb_frag_address - gets the address of the data contained in a paged fragment
3347 * @frag: the paged fragment buffer
3349 * Returns the address of the data within @frag. The page must already
3352 static inline void *skb_frag_address(const skb_frag_t *frag)
3354 return page_address(skb_frag_page(frag)) + skb_frag_off(frag);
3358 * skb_frag_address_safe - gets the address of the data contained in a paged fragment
3359 * @frag: the paged fragment buffer
3361 * Returns the address of the data within @frag. Checks that the page
3362 * is mapped and returns %NULL otherwise.
3364 static inline void *skb_frag_address_safe(const skb_frag_t *frag)
3366 void *ptr = page_address(skb_frag_page(frag));
3370 return ptr + skb_frag_off(frag);
3374 * skb_frag_page_copy() - sets the page in a fragment from another fragment
3375 * @fragto: skb fragment where page is set
3376 * @fragfrom: skb fragment page is copied from
3378 static inline void skb_frag_page_copy(skb_frag_t *fragto,
3379 const skb_frag_t *fragfrom)
3381 fragto->bv_page = fragfrom->bv_page;
3385 * __skb_frag_set_page - sets the page contained in a paged fragment
3386 * @frag: the paged fragment
3387 * @page: the page to set
3389 * Sets the fragment @frag to contain @page.
3391 static inline void __skb_frag_set_page(skb_frag_t *frag, struct page *page)
3393 frag->bv_page = page;
3397 * skb_frag_set_page - sets the page contained in a paged fragment of an skb
3399 * @f: the fragment offset
3400 * @page: the page to set
3402 * Sets the @f'th fragment of @skb to contain @page.
3404 static inline void skb_frag_set_page(struct sk_buff *skb, int f,
3407 __skb_frag_set_page(&skb_shinfo(skb)->frags[f], page);
3410 bool skb_page_frag_refill(unsigned int sz, struct page_frag *pfrag, gfp_t prio);
3413 * skb_frag_dma_map - maps a paged fragment via the DMA API
3414 * @dev: the device to map the fragment to
3415 * @frag: the paged fragment to map
3416 * @offset: the offset within the fragment (starting at the
3417 * fragment's own offset)
3418 * @size: the number of bytes to map
3419 * @dir: the direction of the mapping (``PCI_DMA_*``)
3421 * Maps the page associated with @frag to @device.
3423 static inline dma_addr_t skb_frag_dma_map(struct device *dev,
3424 const skb_frag_t *frag,
3425 size_t offset, size_t size,
3426 enum dma_data_direction dir)
3428 return dma_map_page(dev, skb_frag_page(frag),
3429 skb_frag_off(frag) + offset, size, dir);
3432 static inline struct sk_buff *pskb_copy(struct sk_buff *skb,
3435 return __pskb_copy(skb, skb_headroom(skb), gfp_mask);
3439 static inline struct sk_buff *pskb_copy_for_clone(struct sk_buff *skb,
3442 return __pskb_copy_fclone(skb, skb_headroom(skb), gfp_mask, true);
3447 * skb_clone_writable - is the header of a clone writable
3448 * @skb: buffer to check
3449 * @len: length up to which to write
3451 * Returns true if modifying the header part of the cloned buffer
3452 * does not requires the data to be copied.
3454 static inline int skb_clone_writable(const struct sk_buff *skb, unsigned int len)
3456 return !skb_header_cloned(skb) &&
3457 skb_headroom(skb) + len <= skb->hdr_len;
3460 static inline int skb_try_make_writable(struct sk_buff *skb,
3461 unsigned int write_len)
3463 return skb_cloned(skb) && !skb_clone_writable(skb, write_len) &&
3464 pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
3467 static inline int __skb_cow(struct sk_buff *skb, unsigned int headroom,
3472 if (headroom > skb_headroom(skb))
3473 delta = headroom - skb_headroom(skb);
3475 if (delta || cloned)
3476 return pskb_expand_head(skb, ALIGN(delta, NET_SKB_PAD), 0,
3482 * skb_cow - copy header of skb when it is required
3483 * @skb: buffer to cow
3484 * @headroom: needed headroom
3486 * If the skb passed lacks sufficient headroom or its data part
3487 * is shared, data is reallocated. If reallocation fails, an error
3488 * is returned and original skb is not changed.
3490 * The result is skb with writable area skb->head...skb->tail
3491 * and at least @headroom of space at head.
3493 static inline int skb_cow(struct sk_buff *skb, unsigned int headroom)
3495 return __skb_cow(skb, headroom, skb_cloned(skb));
3499 * skb_cow_head - skb_cow but only making the head writable
3500 * @skb: buffer to cow
3501 * @headroom: needed headroom
3503 * This function is identical to skb_cow except that we replace the
3504 * skb_cloned check by skb_header_cloned. It should be used when
3505 * you only need to push on some header and do not need to modify
3508 static inline int skb_cow_head(struct sk_buff *skb, unsigned int headroom)
3510 return __skb_cow(skb, headroom, skb_header_cloned(skb));
3514 * skb_padto - pad an skbuff up to a minimal size
3515 * @skb: buffer to pad
3516 * @len: minimal length
3518 * Pads up a buffer to ensure the trailing bytes exist and are
3519 * blanked. If the buffer already contains sufficient data it
3520 * is untouched. Otherwise it is extended. Returns zero on
3521 * success. The skb is freed on error.
3523 static inline int skb_padto(struct sk_buff *skb, unsigned int len)
3525 unsigned int size = skb->len;
3526 if (likely(size >= len))
3528 return skb_pad(skb, len - size);
3532 * __skb_put_padto - increase size and pad an skbuff up to a minimal size
3533 * @skb: buffer to pad
3534 * @len: minimal length
3535 * @free_on_error: free buffer on error
3537 * Pads up a buffer to ensure the trailing bytes exist and are
3538 * blanked. If the buffer already contains sufficient data it
3539 * is untouched. Otherwise it is extended. Returns zero on
3540 * success. The skb is freed on error if @free_on_error is true.
3542 static inline int __must_check __skb_put_padto(struct sk_buff *skb,
3546 unsigned int size = skb->len;
3548 if (unlikely(size < len)) {
3550 if (__skb_pad(skb, len, free_on_error))
3552 __skb_put(skb, len);
3558 * skb_put_padto - increase size and pad an skbuff up to a minimal size
3559 * @skb: buffer to pad
3560 * @len: minimal length
3562 * Pads up a buffer to ensure the trailing bytes exist and are
3563 * blanked. If the buffer already contains sufficient data it
3564 * is untouched. Otherwise it is extended. Returns zero on
3565 * success. The skb is freed on error.
3567 static inline int __must_check skb_put_padto(struct sk_buff *skb, unsigned int len)
3569 return __skb_put_padto(skb, len, true);
3572 static inline int skb_add_data(struct sk_buff *skb,
3573 struct iov_iter *from, int copy)
3575 const int off = skb->len;
3577 if (skb->ip_summed == CHECKSUM_NONE) {
3579 if (csum_and_copy_from_iter_full(skb_put(skb, copy), copy,
3581 skb->csum = csum_block_add(skb->csum, csum, off);
3584 } else if (copy_from_iter_full(skb_put(skb, copy), copy, from))
3587 __skb_trim(skb, off);
3591 static inline bool skb_can_coalesce(struct sk_buff *skb, int i,
3592 const struct page *page, int off)
3597 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i - 1];
3599 return page == skb_frag_page(frag) &&
3600 off == skb_frag_off(frag) + skb_frag_size(frag);
3605 static inline int __skb_linearize(struct sk_buff *skb)
3607 return __pskb_pull_tail(skb, skb->data_len) ? 0 : -ENOMEM;
3611 * skb_linearize - convert paged skb to linear one
3612 * @skb: buffer to linarize
3614 * If there is no free memory -ENOMEM is returned, otherwise zero
3615 * is returned and the old skb data released.
3617 static inline int skb_linearize(struct sk_buff *skb)
3619 return skb_is_nonlinear(skb) ? __skb_linearize(skb) : 0;
3623 * skb_has_shared_frag - can any frag be overwritten
3624 * @skb: buffer to test
3626 * Return true if the skb has at least one frag that might be modified
3627 * by an external entity (as in vmsplice()/sendfile())
3629 static inline bool skb_has_shared_frag(const struct sk_buff *skb)
3631 return skb_is_nonlinear(skb) &&
3632 skb_shinfo(skb)->flags & SKBFL_SHARED_FRAG;
3636 * skb_linearize_cow - make sure skb is linear and writable
3637 * @skb: buffer to process
3639 * If there is no free memory -ENOMEM is returned, otherwise zero
3640 * is returned and the old skb data released.
3642 static inline int skb_linearize_cow(struct sk_buff *skb)
3644 return skb_is_nonlinear(skb) || skb_cloned(skb) ?
3645 __skb_linearize(skb) : 0;
3648 static __always_inline void
3649 __skb_postpull_rcsum(struct sk_buff *skb, const void *start, unsigned int len,
3652 if (skb->ip_summed == CHECKSUM_COMPLETE)
3653 skb->csum = csum_block_sub(skb->csum,
3654 csum_partial(start, len, 0), off);
3655 else if (skb->ip_summed == CHECKSUM_PARTIAL &&
3656 skb_checksum_start_offset(skb) < 0)
3657 skb->ip_summed = CHECKSUM_NONE;
3661 * skb_postpull_rcsum - update checksum for received skb after pull
3662 * @skb: buffer to update
3663 * @start: start of data before pull
3664 * @len: length of data pulled
3666 * After doing a pull on a received packet, you need to call this to
3667 * update the CHECKSUM_COMPLETE checksum, or set ip_summed to
3668 * CHECKSUM_NONE so that it can be recomputed from scratch.
3670 static inline void skb_postpull_rcsum(struct sk_buff *skb,
3671 const void *start, unsigned int len)
3673 if (skb->ip_summed == CHECKSUM_COMPLETE)
3674 skb->csum = wsum_negate(csum_partial(start, len,
3675 wsum_negate(skb->csum)));
3676 else if (skb->ip_summed == CHECKSUM_PARTIAL &&
3677 skb_checksum_start_offset(skb) < 0)
3678 skb->ip_summed = CHECKSUM_NONE;
3681 static __always_inline void
3682 __skb_postpush_rcsum(struct sk_buff *skb, const void *start, unsigned int len,
3685 if (skb->ip_summed == CHECKSUM_COMPLETE)
3686 skb->csum = csum_block_add(skb->csum,
3687 csum_partial(start, len, 0), off);
3691 * skb_postpush_rcsum - update checksum for received skb after push
3692 * @skb: buffer to update
3693 * @start: start of data after push
3694 * @len: length of data pushed
3696 * After doing a push on a received packet, you need to call this to
3697 * update the CHECKSUM_COMPLETE checksum.
3699 static inline void skb_postpush_rcsum(struct sk_buff *skb,
3700 const void *start, unsigned int len)
3702 __skb_postpush_rcsum(skb, start, len, 0);
3705 void *skb_pull_rcsum(struct sk_buff *skb, unsigned int len);
3708 * skb_push_rcsum - push skb and update receive checksum
3709 * @skb: buffer to update
3710 * @len: length of data pulled
3712 * This function performs an skb_push on the packet and updates
3713 * the CHECKSUM_COMPLETE checksum. It should be used on
3714 * receive path processing instead of skb_push unless you know
3715 * that the checksum difference is zero (e.g., a valid IP header)
3716 * or you are setting ip_summed to CHECKSUM_NONE.
3718 static inline void *skb_push_rcsum(struct sk_buff *skb, unsigned int len)
3721 skb_postpush_rcsum(skb, skb->data, len);
3725 int pskb_trim_rcsum_slow(struct sk_buff *skb, unsigned int len);
3727 * pskb_trim_rcsum - trim received skb and update checksum
3728 * @skb: buffer to trim
3731 * This is exactly the same as pskb_trim except that it ensures the
3732 * checksum of received packets are still valid after the operation.
3733 * It can change skb pointers.
3736 static inline int pskb_trim_rcsum(struct sk_buff *skb, unsigned int len)
3738 if (likely(len >= skb->len))
3740 return pskb_trim_rcsum_slow(skb, len);
3743 static inline int __skb_trim_rcsum(struct sk_buff *skb, unsigned int len)
3745 if (skb->ip_summed == CHECKSUM_COMPLETE)
3746 skb->ip_summed = CHECKSUM_NONE;
3747 __skb_trim(skb, len);
3751 static inline int __skb_grow_rcsum(struct sk_buff *skb, unsigned int len)
3753 if (skb->ip_summed == CHECKSUM_COMPLETE)
3754 skb->ip_summed = CHECKSUM_NONE;
3755 return __skb_grow(skb, len);
3758 #define rb_to_skb(rb) rb_entry_safe(rb, struct sk_buff, rbnode)
3759 #define skb_rb_first(root) rb_to_skb(rb_first(root))
3760 #define skb_rb_last(root) rb_to_skb(rb_last(root))
3761 #define skb_rb_next(skb) rb_to_skb(rb_next(&(skb)->rbnode))
3762 #define skb_rb_prev(skb) rb_to_skb(rb_prev(&(skb)->rbnode))
3764 #define skb_queue_walk(queue, skb) \
3765 for (skb = (queue)->next; \
3766 skb != (struct sk_buff *)(queue); \
3769 #define skb_queue_walk_safe(queue, skb, tmp) \
3770 for (skb = (queue)->next, tmp = skb->next; \
3771 skb != (struct sk_buff *)(queue); \
3772 skb = tmp, tmp = skb->next)
3774 #define skb_queue_walk_from(queue, skb) \
3775 for (; skb != (struct sk_buff *)(queue); \
3778 #define skb_rbtree_walk(skb, root) \
3779 for (skb = skb_rb_first(root); skb != NULL; \
3780 skb = skb_rb_next(skb))
3782 #define skb_rbtree_walk_from(skb) \
3783 for (; skb != NULL; \
3784 skb = skb_rb_next(skb))
3786 #define skb_rbtree_walk_from_safe(skb, tmp) \
3787 for (; tmp = skb ? skb_rb_next(skb) : NULL, (skb != NULL); \
3790 #define skb_queue_walk_from_safe(queue, skb, tmp) \
3791 for (tmp = skb->next; \
3792 skb != (struct sk_buff *)(queue); \
3793 skb = tmp, tmp = skb->next)
3795 #define skb_queue_reverse_walk(queue, skb) \
3796 for (skb = (queue)->prev; \
3797 skb != (struct sk_buff *)(queue); \
3800 #define skb_queue_reverse_walk_safe(queue, skb, tmp) \
3801 for (skb = (queue)->prev, tmp = skb->prev; \
3802 skb != (struct sk_buff *)(queue); \
3803 skb = tmp, tmp = skb->prev)
3805 #define skb_queue_reverse_walk_from_safe(queue, skb, tmp) \
3806 for (tmp = skb->prev; \
3807 skb != (struct sk_buff *)(queue); \
3808 skb = tmp, tmp = skb->prev)
3810 static inline bool skb_has_frag_list(const struct sk_buff *skb)
3812 return skb_shinfo(skb)->frag_list != NULL;
3815 static inline void skb_frag_list_init(struct sk_buff *skb)
3817 skb_shinfo(skb)->frag_list = NULL;
3820 #define skb_walk_frags(skb, iter) \
3821 for (iter = skb_shinfo(skb)->frag_list; iter; iter = iter->next)
3824 int __skb_wait_for_more_packets(struct sock *sk, struct sk_buff_head *queue,
3825 int *err, long *timeo_p,
3826 const struct sk_buff *skb);
3827 struct sk_buff *__skb_try_recv_from_queue(struct sock *sk,
3828 struct sk_buff_head *queue,
3831 struct sk_buff **last);
3832 struct sk_buff *__skb_try_recv_datagram(struct sock *sk,
3833 struct sk_buff_head *queue,
3834 unsigned int flags, int *off, int *err,
3835 struct sk_buff **last);
3836 struct sk_buff *__skb_recv_datagram(struct sock *sk,
3837 struct sk_buff_head *sk_queue,
3838 unsigned int flags, int *off, int *err);
3839 struct sk_buff *skb_recv_datagram(struct sock *sk, unsigned flags, int noblock,
3841 __poll_t datagram_poll(struct file *file, struct socket *sock,
3842 struct poll_table_struct *wait);
3843 int skb_copy_datagram_iter(const struct sk_buff *from, int offset,
3844 struct iov_iter *to, int size);
3845 static inline int skb_copy_datagram_msg(const struct sk_buff *from, int offset,
3846 struct msghdr *msg, int size)
3848 return skb_copy_datagram_iter(from, offset, &msg->msg_iter, size);
3850 int skb_copy_and_csum_datagram_msg(struct sk_buff *skb, int hlen,
3851 struct msghdr *msg);
3852 int skb_copy_and_hash_datagram_iter(const struct sk_buff *skb, int offset,
3853 struct iov_iter *to, int len,
3854 struct ahash_request *hash);
3855 int skb_copy_datagram_from_iter(struct sk_buff *skb, int offset,
3856 struct iov_iter *from, int len);
3857 int zerocopy_sg_from_iter(struct sk_buff *skb, struct iov_iter *frm);
3858 void skb_free_datagram(struct sock *sk, struct sk_buff *skb);
3859 void __skb_free_datagram_locked(struct sock *sk, struct sk_buff *skb, int len);
3860 static inline void skb_free_datagram_locked(struct sock *sk,
3861 struct sk_buff *skb)
3863 __skb_free_datagram_locked(sk, skb, 0);
3865 int skb_kill_datagram(struct sock *sk, struct sk_buff *skb, unsigned int flags);
3866 int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len);
3867 int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len);
3868 __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset, u8 *to,
3870 int skb_splice_bits(struct sk_buff *skb, struct sock *sk, unsigned int offset,
3871 struct pipe_inode_info *pipe, unsigned int len,
3872 unsigned int flags);
3873 int skb_send_sock_locked(struct sock *sk, struct sk_buff *skb, int offset,
3875 int skb_send_sock(struct sock *sk, struct sk_buff *skb, int offset, int len);
3876 void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to);
3877 unsigned int skb_zerocopy_headlen(const struct sk_buff *from);
3878 int skb_zerocopy(struct sk_buff *to, struct sk_buff *from,
3880 void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len);
3881 int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen);
3882 void skb_scrub_packet(struct sk_buff *skb, bool xnet);
3883 bool skb_gso_validate_network_len(const struct sk_buff *skb, unsigned int mtu);
3884 bool skb_gso_validate_mac_len(const struct sk_buff *skb, unsigned int len);
3885 struct sk_buff *skb_segment(struct sk_buff *skb, netdev_features_t features);
3886 struct sk_buff *skb_segment_list(struct sk_buff *skb, netdev_features_t features,
3887 unsigned int offset);
3888 struct sk_buff *skb_vlan_untag(struct sk_buff *skb);
3889 int skb_ensure_writable(struct sk_buff *skb, int write_len);
3890 int __skb_vlan_pop(struct sk_buff *skb, u16 *vlan_tci);
3891 int skb_vlan_pop(struct sk_buff *skb);
3892 int skb_vlan_push(struct sk_buff *skb, __be16 vlan_proto, u16 vlan_tci);
3893 int skb_eth_pop(struct sk_buff *skb);
3894 int skb_eth_push(struct sk_buff *skb, const unsigned char *dst,
3895 const unsigned char *src);
3896 int skb_mpls_push(struct sk_buff *skb, __be32 mpls_lse, __be16 mpls_proto,
3897 int mac_len, bool ethernet);
3898 int skb_mpls_pop(struct sk_buff *skb, __be16 next_proto, int mac_len,
3900 int skb_mpls_update_lse(struct sk_buff *skb, __be32 mpls_lse);
3901 int skb_mpls_dec_ttl(struct sk_buff *skb);
3902 struct sk_buff *pskb_extract(struct sk_buff *skb, int off, int to_copy,
3905 static inline int memcpy_from_msg(void *data, struct msghdr *msg, int len)
3907 return copy_from_iter_full(data, len, &msg->msg_iter) ? 0 : -EFAULT;
3910 static inline int memcpy_to_msg(struct msghdr *msg, void *data, int len)
3912 return copy_to_iter(data, len, &msg->msg_iter) == len ? 0 : -EFAULT;
3915 struct skb_checksum_ops {
3916 __wsum (*update)(const void *mem, int len, __wsum wsum);
3917 __wsum (*combine)(__wsum csum, __wsum csum2, int offset, int len);
3920 extern const struct skb_checksum_ops *crc32c_csum_stub __read_mostly;
3922 __wsum __skb_checksum(const struct sk_buff *skb, int offset, int len,
3923 __wsum csum, const struct skb_checksum_ops *ops);
3924 __wsum skb_checksum(const struct sk_buff *skb, int offset, int len,
3927 static inline void * __must_check
3928 __skb_header_pointer(const struct sk_buff *skb, int offset, int len,
3929 const void *data, int hlen, void *buffer)
3931 if (likely(hlen - offset >= len))
3932 return (void *)data + offset;
3934 if (!skb || unlikely(skb_copy_bits(skb, offset, buffer, len) < 0))
3940 static inline void * __must_check
3941 skb_header_pointer(const struct sk_buff *skb, int offset, int len, void *buffer)
3943 return __skb_header_pointer(skb, offset, len, skb->data,
3944 skb_headlen(skb), buffer);
3948 * skb_needs_linearize - check if we need to linearize a given skb
3949 * depending on the given device features.
3950 * @skb: socket buffer to check
3951 * @features: net device features
3953 * Returns true if either:
3954 * 1. skb has frag_list and the device doesn't support FRAGLIST, or
3955 * 2. skb is fragmented and the device does not support SG.
3957 static inline bool skb_needs_linearize(struct sk_buff *skb,
3958 netdev_features_t features)
3960 return skb_is_nonlinear(skb) &&
3961 ((skb_has_frag_list(skb) && !(features & NETIF_F_FRAGLIST)) ||
3962 (skb_shinfo(skb)->nr_frags && !(features & NETIF_F_SG)));
3965 static inline void skb_copy_from_linear_data(const struct sk_buff *skb,
3967 const unsigned int len)
3969 memcpy(to, skb->data, len);
3972 static inline void skb_copy_from_linear_data_offset(const struct sk_buff *skb,
3973 const int offset, void *to,
3974 const unsigned int len)
3976 memcpy(to, skb->data + offset, len);
3979 static inline void skb_copy_to_linear_data(struct sk_buff *skb,
3981 const unsigned int len)
3983 memcpy(skb->data, from, len);
3986 static inline void skb_copy_to_linear_data_offset(struct sk_buff *skb,
3989 const unsigned int len)
3991 memcpy(skb->data + offset, from, len);
3994 void skb_init(void);
3996 static inline ktime_t skb_get_ktime(const struct sk_buff *skb)
4002 * skb_get_timestamp - get timestamp from a skb
4003 * @skb: skb to get stamp from
4004 * @stamp: pointer to struct __kernel_old_timeval to store stamp in
4006 * Timestamps are stored in the skb as offsets to a base timestamp.
4007 * This function converts the offset back to a struct timeval and stores
4010 static inline void skb_get_timestamp(const struct sk_buff *skb,
4011 struct __kernel_old_timeval *stamp)
4013 *stamp = ns_to_kernel_old_timeval(skb->tstamp);
4016 static inline void skb_get_new_timestamp(const struct sk_buff *skb,
4017 struct __kernel_sock_timeval *stamp)
4019 struct timespec64 ts = ktime_to_timespec64(skb->tstamp);
4021 stamp->tv_sec = ts.tv_sec;
4022 stamp->tv_usec = ts.tv_nsec / 1000;
4025 static inline void skb_get_timestampns(const struct sk_buff *skb,
4026 struct __kernel_old_timespec *stamp)
4028 struct timespec64 ts = ktime_to_timespec64(skb->tstamp);
4030 stamp->tv_sec = ts.tv_sec;
4031 stamp->tv_nsec = ts.tv_nsec;
4034 static inline void skb_get_new_timestampns(const struct sk_buff *skb,
4035 struct __kernel_timespec *stamp)
4037 struct timespec64 ts = ktime_to_timespec64(skb->tstamp);
4039 stamp->tv_sec = ts.tv_sec;
4040 stamp->tv_nsec = ts.tv_nsec;
4043 static inline void __net_timestamp(struct sk_buff *skb)
4045 skb->tstamp = ktime_get_real();
4046 skb->mono_delivery_time = 0;
4049 static inline ktime_t net_timedelta(ktime_t t)
4051 return ktime_sub(ktime_get_real(), t);
4054 static inline void skb_set_delivery_time(struct sk_buff *skb, ktime_t kt,
4058 skb->mono_delivery_time = kt && mono;
4061 DECLARE_STATIC_KEY_FALSE(netstamp_needed_key);
4063 /* It is used in the ingress path to clear the delivery_time.
4064 * If needed, set the skb->tstamp to the (rcv) timestamp.
4066 static inline void skb_clear_delivery_time(struct sk_buff *skb)
4068 if (skb->mono_delivery_time) {
4069 skb->mono_delivery_time = 0;
4070 if (static_branch_unlikely(&netstamp_needed_key))
4071 skb->tstamp = ktime_get_real();
4077 static inline void skb_clear_tstamp(struct sk_buff *skb)
4079 if (skb->mono_delivery_time)
4085 static inline ktime_t skb_tstamp(const struct sk_buff *skb)
4087 if (skb->mono_delivery_time)
4093 static inline ktime_t skb_tstamp_cond(const struct sk_buff *skb, bool cond)
4095 if (!skb->mono_delivery_time && skb->tstamp)
4098 if (static_branch_unlikely(&netstamp_needed_key) || cond)
4099 return ktime_get_real();
4104 static inline u8 skb_metadata_len(const struct sk_buff *skb)
4106 return skb_shinfo(skb)->meta_len;
4109 static inline void *skb_metadata_end(const struct sk_buff *skb)
4111 return skb_mac_header(skb);
4114 static inline bool __skb_metadata_differs(const struct sk_buff *skb_a,
4115 const struct sk_buff *skb_b,
4118 const void *a = skb_metadata_end(skb_a);
4119 const void *b = skb_metadata_end(skb_b);
4120 /* Using more efficient varaiant than plain call to memcmp(). */
4121 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64
4125 #define __it(x, op) (x -= sizeof(u##op))
4126 #define __it_diff(a, b, op) (*(u##op *)__it(a, op)) ^ (*(u##op *)__it(b, op))
4127 case 32: diffs |= __it_diff(a, b, 64);
4129 case 24: diffs |= __it_diff(a, b, 64);
4131 case 16: diffs |= __it_diff(a, b, 64);
4133 case 8: diffs |= __it_diff(a, b, 64);
4135 case 28: diffs |= __it_diff(a, b, 64);
4137 case 20: diffs |= __it_diff(a, b, 64);
4139 case 12: diffs |= __it_diff(a, b, 64);
4141 case 4: diffs |= __it_diff(a, b, 32);
4146 return memcmp(a - meta_len, b - meta_len, meta_len);
4150 static inline bool skb_metadata_differs(const struct sk_buff *skb_a,
4151 const struct sk_buff *skb_b)
4153 u8 len_a = skb_metadata_len(skb_a);
4154 u8 len_b = skb_metadata_len(skb_b);
4156 if (!(len_a | len_b))
4159 return len_a != len_b ?
4160 true : __skb_metadata_differs(skb_a, skb_b, len_a);
4163 static inline void skb_metadata_set(struct sk_buff *skb, u8 meta_len)
4165 skb_shinfo(skb)->meta_len = meta_len;
4168 static inline void skb_metadata_clear(struct sk_buff *skb)
4170 skb_metadata_set(skb, 0);
4173 struct sk_buff *skb_clone_sk(struct sk_buff *skb);
4175 #ifdef CONFIG_NETWORK_PHY_TIMESTAMPING
4177 void skb_clone_tx_timestamp(struct sk_buff *skb);
4178 bool skb_defer_rx_timestamp(struct sk_buff *skb);
4180 #else /* CONFIG_NETWORK_PHY_TIMESTAMPING */
4182 static inline void skb_clone_tx_timestamp(struct sk_buff *skb)
4186 static inline bool skb_defer_rx_timestamp(struct sk_buff *skb)
4191 #endif /* !CONFIG_NETWORK_PHY_TIMESTAMPING */
4194 * skb_complete_tx_timestamp() - deliver cloned skb with tx timestamps
4196 * PHY drivers may accept clones of transmitted packets for
4197 * timestamping via their phy_driver.txtstamp method. These drivers
4198 * must call this function to return the skb back to the stack with a
4201 * @skb: clone of the original outgoing packet
4202 * @hwtstamps: hardware time stamps
4205 void skb_complete_tx_timestamp(struct sk_buff *skb,
4206 struct skb_shared_hwtstamps *hwtstamps);
4208 void __skb_tstamp_tx(struct sk_buff *orig_skb, const struct sk_buff *ack_skb,
4209 struct skb_shared_hwtstamps *hwtstamps,
4210 struct sock *sk, int tstype);
4213 * skb_tstamp_tx - queue clone of skb with send time stamps
4214 * @orig_skb: the original outgoing packet
4215 * @hwtstamps: hardware time stamps, may be NULL if not available
4217 * If the skb has a socket associated, then this function clones the
4218 * skb (thus sharing the actual data and optional structures), stores
4219 * the optional hardware time stamping information (if non NULL) or
4220 * generates a software time stamp (otherwise), then queues the clone
4221 * to the error queue of the socket. Errors are silently ignored.
4223 void skb_tstamp_tx(struct sk_buff *orig_skb,
4224 struct skb_shared_hwtstamps *hwtstamps);
4227 * skb_tx_timestamp() - Driver hook for transmit timestamping
4229 * Ethernet MAC Drivers should call this function in their hard_xmit()
4230 * function immediately before giving the sk_buff to the MAC hardware.
4232 * Specifically, one should make absolutely sure that this function is
4233 * called before TX completion of this packet can trigger. Otherwise
4234 * the packet could potentially already be freed.
4236 * @skb: A socket buffer.
4238 static inline void skb_tx_timestamp(struct sk_buff *skb)
4240 skb_clone_tx_timestamp(skb);
4241 if (skb_shinfo(skb)->tx_flags & SKBTX_SW_TSTAMP)
4242 skb_tstamp_tx(skb, NULL);
4246 * skb_complete_wifi_ack - deliver skb with wifi status
4248 * @skb: the original outgoing packet
4249 * @acked: ack status
4252 void skb_complete_wifi_ack(struct sk_buff *skb, bool acked);
4254 __sum16 __skb_checksum_complete_head(struct sk_buff *skb, int len);
4255 __sum16 __skb_checksum_complete(struct sk_buff *skb);
4257 static inline int skb_csum_unnecessary(const struct sk_buff *skb)
4259 return ((skb->ip_summed == CHECKSUM_UNNECESSARY) ||
4261 (skb->ip_summed == CHECKSUM_PARTIAL &&
4262 skb_checksum_start_offset(skb) >= 0));
4266 * skb_checksum_complete - Calculate checksum of an entire packet
4267 * @skb: packet to process
4269 * This function calculates the checksum over the entire packet plus
4270 * the value of skb->csum. The latter can be used to supply the
4271 * checksum of a pseudo header as used by TCP/UDP. It returns the
4274 * For protocols that contain complete checksums such as ICMP/TCP/UDP,
4275 * this function can be used to verify that checksum on received
4276 * packets. In that case the function should return zero if the
4277 * checksum is correct. In particular, this function will return zero
4278 * if skb->ip_summed is CHECKSUM_UNNECESSARY which indicates that the
4279 * hardware has already verified the correctness of the checksum.
4281 static inline __sum16 skb_checksum_complete(struct sk_buff *skb)
4283 return skb_csum_unnecessary(skb) ?
4284 0 : __skb_checksum_complete(skb);
4287 static inline void __skb_decr_checksum_unnecessary(struct sk_buff *skb)
4289 if (skb->ip_summed == CHECKSUM_UNNECESSARY) {
4290 if (skb->csum_level == 0)
4291 skb->ip_summed = CHECKSUM_NONE;
4297 static inline void __skb_incr_checksum_unnecessary(struct sk_buff *skb)
4299 if (skb->ip_summed == CHECKSUM_UNNECESSARY) {
4300 if (skb->csum_level < SKB_MAX_CSUM_LEVEL)
4302 } else if (skb->ip_summed == CHECKSUM_NONE) {
4303 skb->ip_summed = CHECKSUM_UNNECESSARY;
4304 skb->csum_level = 0;
4308 static inline void __skb_reset_checksum_unnecessary(struct sk_buff *skb)
4310 if (skb->ip_summed == CHECKSUM_UNNECESSARY) {
4311 skb->ip_summed = CHECKSUM_NONE;
4312 skb->csum_level = 0;
4316 /* Check if we need to perform checksum complete validation.
4318 * Returns true if checksum complete is needed, false otherwise
4319 * (either checksum is unnecessary or zero checksum is allowed).
4321 static inline bool __skb_checksum_validate_needed(struct sk_buff *skb,
4325 if (skb_csum_unnecessary(skb) || (zero_okay && !check)) {
4326 skb->csum_valid = 1;
4327 __skb_decr_checksum_unnecessary(skb);
4334 /* For small packets <= CHECKSUM_BREAK perform checksum complete directly
4337 #define CHECKSUM_BREAK 76
4339 /* Unset checksum-complete
4341 * Unset checksum complete can be done when packet is being modified
4342 * (uncompressed for instance) and checksum-complete value is
4345 static inline void skb_checksum_complete_unset(struct sk_buff *skb)
4347 if (skb->ip_summed == CHECKSUM_COMPLETE)
4348 skb->ip_summed = CHECKSUM_NONE;
4351 /* Validate (init) checksum based on checksum complete.
4354 * 0: checksum is validated or try to in skb_checksum_complete. In the latter
4355 * case the ip_summed will not be CHECKSUM_UNNECESSARY and the pseudo
4356 * checksum is stored in skb->csum for use in __skb_checksum_complete
4357 * non-zero: value of invalid checksum
4360 static inline __sum16 __skb_checksum_validate_complete(struct sk_buff *skb,
4364 if (skb->ip_summed == CHECKSUM_COMPLETE) {
4365 if (!csum_fold(csum_add(psum, skb->csum))) {
4366 skb->csum_valid = 1;
4373 if (complete || skb->len <= CHECKSUM_BREAK) {
4376 csum = __skb_checksum_complete(skb);
4377 skb->csum_valid = !csum;
4384 static inline __wsum null_compute_pseudo(struct sk_buff *skb, int proto)
4389 /* Perform checksum validate (init). Note that this is a macro since we only
4390 * want to calculate the pseudo header which is an input function if necessary.
4391 * First we try to validate without any computation (checksum unnecessary) and
4392 * then calculate based on checksum complete calling the function to compute
4396 * 0: checksum is validated or try to in skb_checksum_complete
4397 * non-zero: value of invalid checksum
4399 #define __skb_checksum_validate(skb, proto, complete, \
4400 zero_okay, check, compute_pseudo) \
4402 __sum16 __ret = 0; \
4403 skb->csum_valid = 0; \
4404 if (__skb_checksum_validate_needed(skb, zero_okay, check)) \
4405 __ret = __skb_checksum_validate_complete(skb, \
4406 complete, compute_pseudo(skb, proto)); \
4410 #define skb_checksum_init(skb, proto, compute_pseudo) \
4411 __skb_checksum_validate(skb, proto, false, false, 0, compute_pseudo)
4413 #define skb_checksum_init_zero_check(skb, proto, check, compute_pseudo) \
4414 __skb_checksum_validate(skb, proto, false, true, check, compute_pseudo)
4416 #define skb_checksum_validate(skb, proto, compute_pseudo) \
4417 __skb_checksum_validate(skb, proto, true, false, 0, compute_pseudo)
4419 #define skb_checksum_validate_zero_check(skb, proto, check, \
4421 __skb_checksum_validate(skb, proto, true, true, check, compute_pseudo)
4423 #define skb_checksum_simple_validate(skb) \
4424 __skb_checksum_validate(skb, 0, true, false, 0, null_compute_pseudo)
4426 static inline bool __skb_checksum_convert_check(struct sk_buff *skb)
4428 return (skb->ip_summed == CHECKSUM_NONE && skb->csum_valid);
4431 static inline void __skb_checksum_convert(struct sk_buff *skb, __wsum pseudo)
4433 skb->csum = ~pseudo;
4434 skb->ip_summed = CHECKSUM_COMPLETE;
4437 #define skb_checksum_try_convert(skb, proto, compute_pseudo) \
4439 if (__skb_checksum_convert_check(skb)) \
4440 __skb_checksum_convert(skb, compute_pseudo(skb, proto)); \
4443 static inline void skb_remcsum_adjust_partial(struct sk_buff *skb, void *ptr,
4444 u16 start, u16 offset)
4446 skb->ip_summed = CHECKSUM_PARTIAL;
4447 skb->csum_start = ((unsigned char *)ptr + start) - skb->head;
4448 skb->csum_offset = offset - start;
4451 /* Update skbuf and packet to reflect the remote checksum offload operation.
4452 * When called, ptr indicates the starting point for skb->csum when
4453 * ip_summed is CHECKSUM_COMPLETE. If we need create checksum complete
4454 * here, skb_postpull_rcsum is done so skb->csum start is ptr.
4456 static inline void skb_remcsum_process(struct sk_buff *skb, void *ptr,
4457 int start, int offset, bool nopartial)
4462 skb_remcsum_adjust_partial(skb, ptr, start, offset);
4466 if (unlikely(skb->ip_summed != CHECKSUM_COMPLETE)) {
4467 __skb_checksum_complete(skb);
4468 skb_postpull_rcsum(skb, skb->data, ptr - (void *)skb->data);
4471 delta = remcsum_adjust(ptr, skb->csum, start, offset);
4473 /* Adjust skb->csum since we changed the packet */
4474 skb->csum = csum_add(skb->csum, delta);
4477 static inline struct nf_conntrack *skb_nfct(const struct sk_buff *skb)
4479 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
4480 return (void *)(skb->_nfct & NFCT_PTRMASK);
4486 static inline unsigned long skb_get_nfct(const struct sk_buff *skb)
4488 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
4495 static inline void skb_set_nfct(struct sk_buff *skb, unsigned long nfct)
4497 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
4498 skb->slow_gro |= !!nfct;
4503 #ifdef CONFIG_SKB_EXTENSIONS
4505 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
4511 #if IS_ENABLED(CONFIG_NET_TC_SKB_EXT)
4514 #if IS_ENABLED(CONFIG_MPTCP)
4517 #if IS_ENABLED(CONFIG_MCTP_FLOWS)
4520 SKB_EXT_NUM, /* must be last */
4524 * struct skb_ext - sk_buff extensions
4525 * @refcnt: 1 on allocation, deallocated on 0
4526 * @offset: offset to add to @data to obtain extension address
4527 * @chunks: size currently allocated, stored in SKB_EXT_ALIGN_SHIFT units
4528 * @data: start of extension data, variable sized
4530 * Note: offsets/lengths are stored in chunks of 8 bytes, this allows
4531 * to use 'u8' types while allowing up to 2kb worth of extension data.
4535 u8 offset[SKB_EXT_NUM]; /* in chunks of 8 bytes */
4536 u8 chunks; /* same */
4537 char data[] __aligned(8);
4540 struct skb_ext *__skb_ext_alloc(gfp_t flags);
4541 void *__skb_ext_set(struct sk_buff *skb, enum skb_ext_id id,
4542 struct skb_ext *ext);
4543 void *skb_ext_add(struct sk_buff *skb, enum skb_ext_id id);
4544 void __skb_ext_del(struct sk_buff *skb, enum skb_ext_id id);
4545 void __skb_ext_put(struct skb_ext *ext);
4547 static inline void skb_ext_put(struct sk_buff *skb)
4549 if (skb->active_extensions)
4550 __skb_ext_put(skb->extensions);
4553 static inline void __skb_ext_copy(struct sk_buff *dst,
4554 const struct sk_buff *src)
4556 dst->active_extensions = src->active_extensions;
4558 if (src->active_extensions) {
4559 struct skb_ext *ext = src->extensions;
4561 refcount_inc(&ext->refcnt);
4562 dst->extensions = ext;
4566 static inline void skb_ext_copy(struct sk_buff *dst, const struct sk_buff *src)
4569 __skb_ext_copy(dst, src);
4572 static inline bool __skb_ext_exist(const struct skb_ext *ext, enum skb_ext_id i)
4574 return !!ext->offset[i];
4577 static inline bool skb_ext_exist(const struct sk_buff *skb, enum skb_ext_id id)
4579 return skb->active_extensions & (1 << id);
4582 static inline void skb_ext_del(struct sk_buff *skb, enum skb_ext_id id)
4584 if (skb_ext_exist(skb, id))
4585 __skb_ext_del(skb, id);
4588 static inline void *skb_ext_find(const struct sk_buff *skb, enum skb_ext_id id)
4590 if (skb_ext_exist(skb, id)) {
4591 struct skb_ext *ext = skb->extensions;
4593 return (void *)ext + (ext->offset[id] << 3);
4599 static inline void skb_ext_reset(struct sk_buff *skb)
4601 if (unlikely(skb->active_extensions)) {
4602 __skb_ext_put(skb->extensions);
4603 skb->active_extensions = 0;
4607 static inline bool skb_has_extensions(struct sk_buff *skb)
4609 return unlikely(skb->active_extensions);
4612 static inline void skb_ext_put(struct sk_buff *skb) {}
4613 static inline void skb_ext_reset(struct sk_buff *skb) {}
4614 static inline void skb_ext_del(struct sk_buff *skb, int unused) {}
4615 static inline void __skb_ext_copy(struct sk_buff *d, const struct sk_buff *s) {}
4616 static inline void skb_ext_copy(struct sk_buff *dst, const struct sk_buff *s) {}
4617 static inline bool skb_has_extensions(struct sk_buff *skb) { return false; }
4618 #endif /* CONFIG_SKB_EXTENSIONS */
4620 static inline void nf_reset_ct(struct sk_buff *skb)
4622 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
4623 nf_conntrack_put(skb_nfct(skb));
4628 static inline void nf_reset_trace(struct sk_buff *skb)
4630 #if IS_ENABLED(CONFIG_NETFILTER_XT_TARGET_TRACE) || defined(CONFIG_NF_TABLES)
4635 static inline void ipvs_reset(struct sk_buff *skb)
4637 #if IS_ENABLED(CONFIG_IP_VS)
4638 skb->ipvs_property = 0;
4642 /* Note: This doesn't put any conntrack info in dst. */
4643 static inline void __nf_copy(struct sk_buff *dst, const struct sk_buff *src,
4646 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
4647 dst->_nfct = src->_nfct;
4648 nf_conntrack_get(skb_nfct(src));
4650 #if IS_ENABLED(CONFIG_NETFILTER_XT_TARGET_TRACE) || defined(CONFIG_NF_TABLES)
4652 dst->nf_trace = src->nf_trace;
4656 static inline void nf_copy(struct sk_buff *dst, const struct sk_buff *src)
4658 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
4659 nf_conntrack_put(skb_nfct(dst));
4661 dst->slow_gro = src->slow_gro;
4662 __nf_copy(dst, src, true);
4665 #ifdef CONFIG_NETWORK_SECMARK
4666 static inline void skb_copy_secmark(struct sk_buff *to, const struct sk_buff *from)
4668 to->secmark = from->secmark;
4671 static inline void skb_init_secmark(struct sk_buff *skb)
4676 static inline void skb_copy_secmark(struct sk_buff *to, const struct sk_buff *from)
4679 static inline void skb_init_secmark(struct sk_buff *skb)
4683 static inline int secpath_exists(const struct sk_buff *skb)
4686 return skb_ext_exist(skb, SKB_EXT_SEC_PATH);
4692 static inline bool skb_irq_freeable(const struct sk_buff *skb)
4694 return !skb->destructor &&
4695 !secpath_exists(skb) &&
4697 !skb->_skb_refdst &&
4698 !skb_has_frag_list(skb);
4701 static inline void skb_set_queue_mapping(struct sk_buff *skb, u16 queue_mapping)
4703 skb->queue_mapping = queue_mapping;
4706 static inline u16 skb_get_queue_mapping(const struct sk_buff *skb)
4708 return skb->queue_mapping;
4711 static inline void skb_copy_queue_mapping(struct sk_buff *to, const struct sk_buff *from)
4713 to->queue_mapping = from->queue_mapping;
4716 static inline void skb_record_rx_queue(struct sk_buff *skb, u16 rx_queue)
4718 skb->queue_mapping = rx_queue + 1;
4721 static inline u16 skb_get_rx_queue(const struct sk_buff *skb)
4723 return skb->queue_mapping - 1;
4726 static inline bool skb_rx_queue_recorded(const struct sk_buff *skb)
4728 return skb->queue_mapping != 0;
4731 static inline void skb_set_dst_pending_confirm(struct sk_buff *skb, u32 val)
4733 skb->dst_pending_confirm = val;
4736 static inline bool skb_get_dst_pending_confirm(const struct sk_buff *skb)
4738 return skb->dst_pending_confirm != 0;
4741 static inline struct sec_path *skb_sec_path(const struct sk_buff *skb)
4744 return skb_ext_find(skb, SKB_EXT_SEC_PATH);
4750 /* Keeps track of mac header offset relative to skb->head.
4751 * It is useful for TSO of Tunneling protocol. e.g. GRE.
4752 * For non-tunnel skb it points to skb_mac_header() and for
4753 * tunnel skb it points to outer mac header.
4754 * Keeps track of level of encapsulation of network headers.
4765 #define SKB_GSO_CB_OFFSET 32
4766 #define SKB_GSO_CB(skb) ((struct skb_gso_cb *)((skb)->cb + SKB_GSO_CB_OFFSET))
4768 static inline int skb_tnl_header_len(const struct sk_buff *inner_skb)
4770 return (skb_mac_header(inner_skb) - inner_skb->head) -
4771 SKB_GSO_CB(inner_skb)->mac_offset;
4774 static inline int gso_pskb_expand_head(struct sk_buff *skb, int extra)
4776 int new_headroom, headroom;
4779 headroom = skb_headroom(skb);
4780 ret = pskb_expand_head(skb, extra, 0, GFP_ATOMIC);
4784 new_headroom = skb_headroom(skb);
4785 SKB_GSO_CB(skb)->mac_offset += (new_headroom - headroom);
4789 static inline void gso_reset_checksum(struct sk_buff *skb, __wsum res)
4791 /* Do not update partial checksums if remote checksum is enabled. */
4792 if (skb->remcsum_offload)
4795 SKB_GSO_CB(skb)->csum = res;
4796 SKB_GSO_CB(skb)->csum_start = skb_checksum_start(skb) - skb->head;
4799 /* Compute the checksum for a gso segment. First compute the checksum value
4800 * from the start of transport header to SKB_GSO_CB(skb)->csum_start, and
4801 * then add in skb->csum (checksum from csum_start to end of packet).
4802 * skb->csum and csum_start are then updated to reflect the checksum of the
4803 * resultant packet starting from the transport header-- the resultant checksum
4804 * is in the res argument (i.e. normally zero or ~ of checksum of a pseudo
4807 static inline __sum16 gso_make_checksum(struct sk_buff *skb, __wsum res)
4809 unsigned char *csum_start = skb_transport_header(skb);
4810 int plen = (skb->head + SKB_GSO_CB(skb)->csum_start) - csum_start;
4811 __wsum partial = SKB_GSO_CB(skb)->csum;
4813 SKB_GSO_CB(skb)->csum = res;
4814 SKB_GSO_CB(skb)->csum_start = csum_start - skb->head;
4816 return csum_fold(csum_partial(csum_start, plen, partial));
4819 static inline bool skb_is_gso(const struct sk_buff *skb)
4821 return skb_shinfo(skb)->gso_size;
4824 /* Note: Should be called only if skb_is_gso(skb) is true */
4825 static inline bool skb_is_gso_v6(const struct sk_buff *skb)
4827 return skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6;
4830 /* Note: Should be called only if skb_is_gso(skb) is true */
4831 static inline bool skb_is_gso_sctp(const struct sk_buff *skb)
4833 return skb_shinfo(skb)->gso_type & SKB_GSO_SCTP;
4836 /* Note: Should be called only if skb_is_gso(skb) is true */
4837 static inline bool skb_is_gso_tcp(const struct sk_buff *skb)
4839 return skb_shinfo(skb)->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6);
4842 static inline void skb_gso_reset(struct sk_buff *skb)
4844 skb_shinfo(skb)->gso_size = 0;
4845 skb_shinfo(skb)->gso_segs = 0;
4846 skb_shinfo(skb)->gso_type = 0;
4849 static inline void skb_increase_gso_size(struct skb_shared_info *shinfo,
4852 if (WARN_ON_ONCE(shinfo->gso_size == GSO_BY_FRAGS))
4854 shinfo->gso_size += increment;
4857 static inline void skb_decrease_gso_size(struct skb_shared_info *shinfo,
4860 if (WARN_ON_ONCE(shinfo->gso_size == GSO_BY_FRAGS))
4862 shinfo->gso_size -= decrement;
4865 void __skb_warn_lro_forwarding(const struct sk_buff *skb);
4867 static inline bool skb_warn_if_lro(const struct sk_buff *skb)
4869 /* LRO sets gso_size but not gso_type, whereas if GSO is really
4870 * wanted then gso_type will be set. */
4871 const struct skb_shared_info *shinfo = skb_shinfo(skb);
4873 if (skb_is_nonlinear(skb) && shinfo->gso_size != 0 &&
4874 unlikely(shinfo->gso_type == 0)) {
4875 __skb_warn_lro_forwarding(skb);
4881 static inline void skb_forward_csum(struct sk_buff *skb)
4883 /* Unfortunately we don't support this one. Any brave souls? */
4884 if (skb->ip_summed == CHECKSUM_COMPLETE)
4885 skb->ip_summed = CHECKSUM_NONE;
4889 * skb_checksum_none_assert - make sure skb ip_summed is CHECKSUM_NONE
4890 * @skb: skb to check
4892 * fresh skbs have their ip_summed set to CHECKSUM_NONE.
4893 * Instead of forcing ip_summed to CHECKSUM_NONE, we can
4894 * use this helper, to document places where we make this assertion.
4896 static inline void skb_checksum_none_assert(const struct sk_buff *skb)
4899 BUG_ON(skb->ip_summed != CHECKSUM_NONE);
4903 bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off);
4905 int skb_checksum_setup(struct sk_buff *skb, bool recalculate);
4906 struct sk_buff *skb_checksum_trimmed(struct sk_buff *skb,
4907 unsigned int transport_len,
4908 __sum16(*skb_chkf)(struct sk_buff *skb));
4911 * skb_head_is_locked - Determine if the skb->head is locked down
4912 * @skb: skb to check
4914 * The head on skbs build around a head frag can be removed if they are
4915 * not cloned. This function returns true if the skb head is locked down
4916 * due to either being allocated via kmalloc, or by being a clone with
4917 * multiple references to the head.
4919 static inline bool skb_head_is_locked(const struct sk_buff *skb)
4921 return !skb->head_frag || skb_cloned(skb);
4924 /* Local Checksum Offload.
4925 * Compute outer checksum based on the assumption that the
4926 * inner checksum will be offloaded later.
4927 * See Documentation/networking/checksum-offloads.rst for
4928 * explanation of how this works.
4929 * Fill in outer checksum adjustment (e.g. with sum of outer
4930 * pseudo-header) before calling.
4931 * Also ensure that inner checksum is in linear data area.
4933 static inline __wsum lco_csum(struct sk_buff *skb)
4935 unsigned char *csum_start = skb_checksum_start(skb);
4936 unsigned char *l4_hdr = skb_transport_header(skb);
4939 /* Start with complement of inner checksum adjustment */
4940 partial = ~csum_unfold(*(__force __sum16 *)(csum_start +
4943 /* Add in checksum of our headers (incl. outer checksum
4944 * adjustment filled in by caller) and return result.
4946 return csum_partial(l4_hdr, csum_start - l4_hdr, partial);
4949 static inline bool skb_is_redirected(const struct sk_buff *skb)
4951 return skb->redirected;
4954 static inline void skb_set_redirected(struct sk_buff *skb, bool from_ingress)
4956 skb->redirected = 1;
4957 #ifdef CONFIG_NET_REDIRECT
4958 skb->from_ingress = from_ingress;
4959 if (skb->from_ingress)
4960 skb_clear_tstamp(skb);
4964 static inline void skb_reset_redirect(struct sk_buff *skb)
4966 skb->redirected = 0;
4969 static inline bool skb_csum_is_sctp(struct sk_buff *skb)
4971 return skb->csum_not_inet;
4974 static inline void skb_set_kcov_handle(struct sk_buff *skb,
4975 const u64 kcov_handle)
4978 skb->kcov_handle = kcov_handle;
4982 static inline u64 skb_get_kcov_handle(struct sk_buff *skb)
4985 return skb->kcov_handle;
4991 #ifdef CONFIG_PAGE_POOL
4992 static inline void skb_mark_for_recycle(struct sk_buff *skb)
4994 skb->pp_recycle = 1;
4998 static inline bool skb_pp_recycle(struct sk_buff *skb, void *data)
5000 if (!IS_ENABLED(CONFIG_PAGE_POOL) || !skb->pp_recycle)
5002 return page_pool_return_skb_page(virt_to_page(data));
5005 #endif /* __KERNEL__ */
5006 #endif /* _LINUX_SKBUFF_H */