Merge https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next
[linux-2.6-microblaze.git] / include / linux / skbuff.h
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  *      Definitions for the 'struct sk_buff' memory handlers.
4  *
5  *      Authors:
6  *              Alan Cox, <gw4pts@gw4pts.ampr.org>
7  *              Florian La Roche, <rzsfl@rz.uni-sb.de>
8  */
9
10 #ifndef _LINUX_SKBUFF_H
11 #define _LINUX_SKBUFF_H
12
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>
22
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>
40 #include <net/flow.h>
41 #include <net/page_pool.h>
42 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
43 #include <linux/netfilter/nf_conntrack_common.h>
44 #endif
45
46 /* The interface for checksum offload between the stack and networking drivers
47  * is as follows...
48  *
49  * A. IP checksum related features
50  *
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
54  * to its device.
55  *
56  * The checksum related features are:
57  *
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).
63  *
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).
72  *
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).
82  *
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.
88  *
89  * B. Checksumming of received packets by device. Indication of checksum
90  *    verification is set in skb->ip_summed. Possible values are:
91  *
92  * CHECKSUM_NONE:
93  *
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.
97  *
98  * CHECKSUM_UNNECESSARY:
99  *
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.
106  *
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.
115  *
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).
125  *
126  * CHECKSUM_COMPLETE:
127  *
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.
131  *
132  *   Notes:
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.
136  *
137  * CHECKSUM_PARTIAL:
138  *
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
147  *   be verified.
148  *
149  * C. Checksumming on transmit for non-GSO. The stack requests checksum offload
150  *    in the skb->ip_summed for a packet. Values are:
151  *
152  * CHECKSUM_PARTIAL:
153  *
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
161  *   correctly.
162  *
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).
167  *
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.
175  *
176  * CHECKSUM_NONE:
177  *
178  *   The skb was already checksummed by the protocol, or a checksum is not
179  *   required.
180  *
181  * CHECKSUM_UNNECESSARY:
182  *
183  *   This has the same meaning as CHECKSUM_NONE for checksum offload on
184  *   output.
185  *
186  * CHECKSUM_COMPLETE:
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.
189  *
190  * D. Non-IP checksum (CRC) offloads
191  *
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.
201  *
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.
209  *
210  * E. Checksumming on output with GSO.
211  *
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).
219  */
220
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
226
227 /* Maximum value in skb->csum_level */
228 #define SKB_MAX_CSUM_LEVEL      3
229
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))
237
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)))
242
243 struct ahash_request;
244 struct net_device;
245 struct scatterlist;
246 struct pipe_inode_info;
247 struct iov_iter;
248 struct napi_struct;
249 struct bpf_prog;
250 union bpf_attr;
251 struct skb_ext;
252
253 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
254 struct nf_bridge_info {
255         enum {
256                 BRNF_PROTO_UNCHANGED,
257                 BRNF_PROTO_8021Q,
258                 BRNF_PROTO_PPPOE
259         } orig_proto:8;
260         u8                      pkt_otherhost:1;
261         u8                      in_prerouting:1;
262         u8                      bridged_dnat:1;
263         __u16                   frag_max_size;
264         struct net_device       *physindev;
265
266         /* always valid & non-NULL from FORWARD on, for physdev match */
267         struct net_device       *physoutdev;
268         union {
269                 /* prerouting: detect dnat in orig/reply direction */
270                 __be32          ipv4_daddr;
271                 struct in6_addr ipv6_daddr;
272
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.
276                  */
277                 char neigh_header[8];
278         };
279 };
280 #endif
281
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.
286  */
287 struct tc_skb_ext {
288         __u32 chain;
289         __u16 mru;
290         __u16 zone;
291         u8 post_ct:1;
292         u8 post_ct_snat:1;
293         u8 post_ct_dnat:1;
294 };
295 #endif
296
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;
302         );
303
304         __u32           qlen;
305         spinlock_t      lock;
306 };
307
308 struct sk_buff;
309
310 /* The reason of skb drop, which is used in kfree_skb_reason().
311  * en...maybe they should be splited by group?
312  *
313  * Each item here should also be in 'TRACE_SKB_DROP_REASON', which is
314  * used to translate the reason to string.
315  */
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
327                                          * mode)
328                                          */
329         SKB_DROP_REASON_IP_CSUM,        /* IP checksum error */
330         SKB_DROP_REASON_IP_INHDR,       /* there is something wrong with
331                                          * IP header (see
332                                          * IPSTATS_MIB_INHDRERRORS)
333                                          */
334         SKB_DROP_REASON_IP_RPFILTER,    /* IP rpfilter validate failed.
335                                          * see the document for rp_filter
336                                          * in ip-sysctl.rst for more
337                                          * information
338                                          */
339         SKB_DROP_REASON_UNICAST_IN_L2_MULTICAST, /* destination address of L2
340                                                   * is multicast, but L3 is
341                                                   * unicast.
342                                                   */
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.
349                                          */
350         SKB_DROP_REASON_TCP_MD5NOTFOUND,        /* no MD5 hash and one
351                                                  * expected, corresponding
352                                                  * to LINUX_MIB_TCPMD5NOTFOUND
353                                                  */
354         SKB_DROP_REASON_TCP_MD5UNEXPECTED,      /* MD5 hash and we're not
355                                                  * expecting one, corresponding
356                                                  * to LINUX_MIB_TCPMD5UNEXPECTED
357                                                  */
358         SKB_DROP_REASON_TCP_MD5FAILURE, /* MD5 hash and its wrong,
359                                          * corresponding to
360                                          * LINUX_MIB_TCPMD5FAILURE
361                                          */
362         SKB_DROP_REASON_SOCKET_BACKLOG, /* failed to add skb to socket
363                                          * backlog (see
364                                          * LINUX_MIB_TCPBACKLOGDROP)
365                                          */
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
369                                          */
370         SKB_DROP_REASON_TCP_OLD_DATA,   /* the TCP data reveived is already
371                                          * received before (spurious retrans
372                                          * may happened), see
373                                          * LINUX_MIB_DELAYEDACKLOST
374                                          */
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
378                                          * window
379                                          */
380         SKB_DROP_REASON_TCP_OFOMERGE,   /* the data of skb is already in
381                                          * the ofo queue, corresponding to
382                                          * LINUX_MIB_TCPOFOMERGE
383                                          */
384         SKB_DROP_REASON_IP_OUTNOROUTES, /* route lookup failed */
385         SKB_DROP_REASON_BPF_CGROUP_EGRESS,      /* dropped by
386                                                  * BPF_PROG_TYPE_CGROUP_SKB
387                                                  * eBPF program
388                                                  */
389         SKB_DROP_REASON_IPV6DISABLED,   /* IPv6 is disabled on the device */
390         SKB_DROP_REASON_NEIGH_CREATEFAIL,       /* failed to create neigh
391                                                  * entry
392                                                  */
393         SKB_DROP_REASON_NEIGH_FAILED,   /* neigh entry in failed state */
394         SKB_DROP_REASON_NEIGH_QUEUEFULL,        /* arp_queue for neigh
395                                                  * entry is full
396                                                  */
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
401                                          * current qdisc)
402                                          */
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
408                                          */
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
414                                          * not supported
415                                          */
416         SKB_DROP_REASON_SKB_CSUM,       /* sk_buff checksum computation
417                                          * error
418                                          */
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()
424                                                  */
425         SKB_DROP_REASON_DEV_HDR,        /* device driver specific
426                                          * header/metadata is invalid
427                                          */
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
431          */
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()
439                                          */
440         SKB_DROP_REASON_TAP_FILTER,     /* dropped by (ebpf) filter directly
441                                          * attached to tun/tap, e.g., via
442                                          * TUNSETFILTEREBPF
443                                          */
444         SKB_DROP_REASON_TAP_TXFILTER,   /* dropped by tx filter implemented
445                                          * at tun/tap, e.g., check_filter()
446                                          */
447         SKB_DROP_REASON_MAX,
448 };
449
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.
453  *
454  * Since GRO uses frags we allocate at least 16 regardless of page
455  * size.
456  */
457 #if (65536/PAGE_SIZE + 1) < 16
458 #define MAX_SKB_FRAGS 16UL
459 #else
460 #define MAX_SKB_FRAGS (65536/PAGE_SIZE + 1)
461 #endif
462 extern int sysctl_max_skb_frags;
463
464 /* Set skb_shinfo(skb)->gso_size to this in case you want skb_segment to
465  * segment using its current segmentation instead.
466  */
467 #define GSO_BY_FRAGS    0xFFFF
468
469 typedef struct bio_vec skb_frag_t;
470
471 /**
472  * skb_frag_size() - Returns the size of a skb fragment
473  * @frag: skb fragment
474  */
475 static inline unsigned int skb_frag_size(const skb_frag_t *frag)
476 {
477         return frag->bv_len;
478 }
479
480 /**
481  * skb_frag_size_set() - Sets the size of a skb fragment
482  * @frag: skb fragment
483  * @size: size of fragment
484  */
485 static inline void skb_frag_size_set(skb_frag_t *frag, unsigned int size)
486 {
487         frag->bv_len = size;
488 }
489
490 /**
491  * skb_frag_size_add() - Increments the size of a skb fragment by @delta
492  * @frag: skb fragment
493  * @delta: value to add
494  */
495 static inline void skb_frag_size_add(skb_frag_t *frag, int delta)
496 {
497         frag->bv_len += delta;
498 }
499
500 /**
501  * skb_frag_size_sub() - Decrements the size of a skb fragment by @delta
502  * @frag: skb fragment
503  * @delta: value to subtract
504  */
505 static inline void skb_frag_size_sub(skb_frag_t *frag, int delta)
506 {
507         frag->bv_len -= delta;
508 }
509
510 /**
511  * skb_frag_must_loop - Test if %p is a high memory page
512  * @p: fragment's page
513  */
514 static inline bool skb_frag_must_loop(struct page *p)
515 {
516 #if defined(CONFIG_HIGHMEM)
517         if (IS_ENABLED(CONFIG_DEBUG_KMAP_LOCAL_FORCE_MAP) || PageHighMem(p))
518                 return true;
519 #endif
520         return false;
521 }
522
523 /**
524  *      skb_frag_foreach_page - loop over pages in a fragment
525  *
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.
535  *
536  *      A fragment can hold a compound page, in which case per-page
537  *      operations, notably kmap_atomic, must be called for each
538  *      regular page.
539  */
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,              \
545              copied = 0;                                                \
546              copied < f_len;                                            \
547              copied += p_len, p++, p_off = 0,                           \
548              p_len = min_t(u32, f_len - copied, PAGE_SIZE))             \
549
550 #define HAVE_HW_TIME_STAMP
551
552 /**
553  * struct skb_shared_hwtstamps - hardware time stamps
554  * @hwtstamp:   hardware time stamp transformed into duration
555  *              since arbitrary point in time
556  *
557  * Software time stamps generated by ktime_get_real() are stored in
558  * skb->tstamp.
559  *
560  * hwtstamps can only be compared against other hwtstamps from
561  * the same device.
562  *
563  * This structure is attached to packets as part of the
564  * &skb_shared_info. Use skb_hwtstamps() to get a pointer.
565  */
566 struct skb_shared_hwtstamps {
567         ktime_t hwtstamp;
568 };
569
570 /* Definitions for tx_flags in struct skb_shared_info */
571 enum {
572         /* generate hardware time stamp */
573         SKBTX_HW_TSTAMP = 1 << 0,
574
575         /* generate software time stamp when queueing packet to NIC */
576         SKBTX_SW_TSTAMP = 1 << 1,
577
578         /* device driver is going to provide hardware time stamp */
579         SKBTX_IN_PROGRESS = 1 << 2,
580
581         /* generate wifi status information (where possible) */
582         SKBTX_WIFI_STATUS = 1 << 4,
583
584         /* generate software time stamp when entering packet scheduling */
585         SKBTX_SCHED_TSTAMP = 1 << 6,
586 };
587
588 #define SKBTX_ANY_SW_TSTAMP     (SKBTX_SW_TSTAMP    | \
589                                  SKBTX_SCHED_TSTAMP)
590 #define SKBTX_ANY_TSTAMP        (SKBTX_HW_TSTAMP | SKBTX_ANY_SW_TSTAMP)
591
592 /* Definitions for flags in struct skb_shared_info */
593 enum {
594         /* use zcopy routines */
595         SKBFL_ZEROCOPY_ENABLE = BIT(0),
596
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
601          */
602         SKBFL_SHARED_FRAG = BIT(1),
603
604         /* segment contains only zerocopy data and should not be
605          * charged to the kernel memory.
606          */
607         SKBFL_PURE_ZEROCOPY = BIT(2),
608 };
609
610 #define SKBFL_ZEROCOPY_FRAG     (SKBFL_ZEROCOPY_ENABLE | SKBFL_SHARED_FRAG)
611 #define SKBFL_ALL_ZEROCOPY      (SKBFL_ZEROCOPY_FRAG | SKBFL_PURE_ZEROCOPY)
612
613 /*
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.
620  */
621 struct ubuf_info {
622         void (*callback)(struct sk_buff *, struct ubuf_info *,
623                          bool zerocopy_success);
624         union {
625                 struct {
626                         unsigned long desc;
627                         void *ctx;
628                 };
629                 struct {
630                         u32 id;
631                         u16 len;
632                         u16 zerocopy:1;
633                         u32 bytelen;
634                 };
635         };
636         refcount_t refcnt;
637         u8 flags;
638
639         struct mmpin {
640                 struct user_struct *user;
641                 unsigned int num_pg;
642         } mmp;
643 };
644
645 #define skb_uarg(SKB)   ((struct ubuf_info *)(skb_shinfo(SKB)->destructor_arg))
646
647 int mm_account_pinned_pages(struct mmpin *mmp, size_t size);
648 void mm_unaccount_pinned_pages(struct mmpin *mmp);
649
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);
653
654 void msg_zerocopy_put_abort(struct ubuf_info *uarg, bool have_uref);
655
656 void msg_zerocopy_callback(struct sk_buff *skb, struct ubuf_info *uarg,
657                            bool success);
658
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);
663
664 /* This data is invariant across clones and lives at
665  * the end of the header data, ie. at skb->end.
666  */
667 struct skb_shared_info {
668         __u8            flags;
669         __u8            meta_len;
670         __u8            nr_frags;
671         __u8            tx_flags;
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;
678         u32             tskey;
679
680         /*
681          * Warning : all fields before dataref are cleared in __alloc_skb()
682          */
683         atomic_t        dataref;
684         unsigned int    xdp_frags_size;
685
686         /* Intermediate layers must ensure that destructor_arg
687          * remains valid until skb destructor */
688         void *          destructor_arg;
689
690         /* must be last field, see pskb_expand_head() */
691         skb_frag_t      frags[MAX_SKB_FRAGS];
692 };
693
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.
698  *
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.
701  *
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.
704  */
705 #define SKB_DATAREF_SHIFT 16
706 #define SKB_DATAREF_MASK ((1 << SKB_DATAREF_SHIFT) - 1)
707
708
709 enum {
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) */
713 };
714
715 enum {
716         SKB_GSO_TCPV4 = 1 << 0,
717
718         /* This indicates the skb is from an untrusted source. */
719         SKB_GSO_DODGY = 1 << 1,
720
721         /* This indicates the tcp segment has CWR set. */
722         SKB_GSO_TCP_ECN = 1 << 2,
723
724         SKB_GSO_TCP_FIXEDID = 1 << 3,
725
726         SKB_GSO_TCPV6 = 1 << 4,
727
728         SKB_GSO_FCOE = 1 << 5,
729
730         SKB_GSO_GRE = 1 << 6,
731
732         SKB_GSO_GRE_CSUM = 1 << 7,
733
734         SKB_GSO_IPXIP4 = 1 << 8,
735
736         SKB_GSO_IPXIP6 = 1 << 9,
737
738         SKB_GSO_UDP_TUNNEL = 1 << 10,
739
740         SKB_GSO_UDP_TUNNEL_CSUM = 1 << 11,
741
742         SKB_GSO_PARTIAL = 1 << 12,
743
744         SKB_GSO_TUNNEL_REMCSUM = 1 << 13,
745
746         SKB_GSO_SCTP = 1 << 14,
747
748         SKB_GSO_ESP = 1 << 15,
749
750         SKB_GSO_UDP = 1 << 16,
751
752         SKB_GSO_UDP_L4 = 1 << 17,
753
754         SKB_GSO_FRAGLIST = 1 << 18,
755 };
756
757 #if BITS_PER_LONG > 32
758 #define NET_SKBUFF_DATA_USES_OFFSET 1
759 #endif
760
761 #ifdef NET_SKBUFF_DATA_USES_OFFSET
762 typedef unsigned int sk_buff_data_t;
763 #else
764 typedef unsigned char *sk_buff_data_t;
765 #endif
766
767 /**
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
775  *      @list: queue head
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
832  *              ports.
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
872  *      @end: End 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
878  */
879
880 struct sk_buff {
881         union {
882                 struct {
883                         /* These two members must be first to match sk_buff_head. */
884                         struct sk_buff          *next;
885                         struct sk_buff          *prev;
886
887                         union {
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.
892                                  */
893                                 unsigned long           dev_scratch;
894                         };
895                 };
896                 struct rb_node          rbnode; /* used in netem, ip4 defrag, and tcp stack */
897                 struct list_head        list;
898                 struct llist_node       ll_node;
899         };
900
901         union {
902                 struct sock             *sk;
903                 int                     ip_defrag_offset;
904         };
905
906         union {
907                 ktime_t         tstamp;
908                 u64             skb_mstamp_ns; /* earliest departure time */
909         };
910         /*
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.
915          */
916         char                    cb[48] __aligned(8);
917
918         union {
919                 struct {
920                         unsigned long   _skb_refdst;
921                         void            (*destructor)(struct sk_buff *skb);
922                 };
923                 struct list_head        tcp_tsorted_anchor;
924 #ifdef CONFIG_NET_SOCK_MSG
925                 unsigned long           _sk_redir;
926 #endif
927         };
928
929 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
930         unsigned long            _nfct;
931 #endif
932         unsigned int            len,
933                                 data_len;
934         __u16                   mac_len,
935                                 hdr_len;
936
937         /* Following fields are _not_ copied in __copy_skb_header()
938          * Note that queue_mapping is here mostly to fill a hole.
939          */
940         __u16                   queue_mapping;
941
942 /* if you move cloned around you also must adapt those constants */
943 #ifdef __BIG_ENDIAN_BITFIELD
944 #define CLONED_MASK     (1 << 7)
945 #else
946 #define CLONED_MASK     1
947 #endif
948 #define CLONED_OFFSET           offsetof(struct sk_buff, __cloned_offset)
949
950         /* private: */
951         __u8                    __cloned_offset[0];
952         /* public: */
953         __u8                    cloned:1,
954                                 nohdr:1,
955                                 fclone:2,
956                                 peeked:1,
957                                 head_frag:1,
958                                 pfmemalloc:1,
959                                 pp_recycle:1; /* page_pool recycle indicator */
960 #ifdef CONFIG_SKB_EXTENSIONS
961         __u8                    active_extensions;
962 #endif
963
964         /* Fields enclosed in headers group are copied
965          * using a single memcpy() in __copy_skb_header()
966          */
967         struct_group(headers,
968
969         /* private: */
970         __u8                    __pkt_type_offset[0];
971         /* public: */
972         __u8                    pkt_type:3; /* see PKT_TYPE_MAX */
973         __u8                    ignore_df:1;
974         __u8                    nf_trace:1;
975         __u8                    ip_summed:2;
976         __u8                    ooo_okay:1;
977
978         __u8                    l4_hash:1;
979         __u8                    sw_hash:1;
980         __u8                    wifi_acked_valid:1;
981         __u8                    wifi_acked:1;
982         __u8                    no_fcs:1;
983         /* Indicates the inner headers are valid in the skbuff. */
984         __u8                    encapsulation:1;
985         __u8                    encap_hdr_csum:1;
986         __u8                    csum_valid:1;
987
988         /* private: */
989         __u8                    __pkt_vlan_present_offset[0];
990         /* public: */
991         __u8                    vlan_present:1; /* See PKT_VLAN_PRESENT_BIT */
992         __u8                    csum_complete_sw:1;
993         __u8                    csum_level:2;
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 */
999 #endif
1000 #ifdef CONFIG_IPV6_NDISC_NODETYPE
1001         __u8                    ndisc_nodetype:2;
1002 #endif
1003
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;
1010 #endif
1011         __u8                    redirected:1;
1012 #ifdef CONFIG_NET_REDIRECT
1013         __u8                    from_ingress:1;
1014 #endif
1015 #ifdef CONFIG_NETFILTER_SKIP_EGRESS
1016         __u8                    nf_skip_egress:1;
1017 #endif
1018 #ifdef CONFIG_TLS_DEVICE
1019         __u8                    decrypted:1;
1020 #endif
1021         __u8                    slow_gro:1;
1022         __u8                    csum_not_inet:1;
1023
1024 #ifdef CONFIG_NET_SCHED
1025         __u16                   tc_index;       /* traffic control index */
1026 #endif
1027
1028         union {
1029                 __wsum          csum;
1030                 struct {
1031                         __u16   csum_start;
1032                         __u16   csum_offset;
1033                 };
1034         };
1035         __u32                   priority;
1036         int                     skb_iif;
1037         __u32                   hash;
1038         __be16                  vlan_proto;
1039         __u16                   vlan_tci;
1040 #if defined(CONFIG_NET_RX_BUSY_POLL) || defined(CONFIG_XPS)
1041         union {
1042                 unsigned int    napi_id;
1043                 unsigned int    sender_cpu;
1044         };
1045 #endif
1046 #ifdef CONFIG_NETWORK_SECMARK
1047         __u32           secmark;
1048 #endif
1049
1050         union {
1051                 __u32           mark;
1052                 __u32           reserved_tailroom;
1053         };
1054
1055         union {
1056                 __be16          inner_protocol;
1057                 __u8            inner_ipproto;
1058         };
1059
1060         __u16                   inner_transport_header;
1061         __u16                   inner_network_header;
1062         __u16                   inner_mac_header;
1063
1064         __be16                  protocol;
1065         __u16                   transport_header;
1066         __u16                   network_header;
1067         __u16                   mac_header;
1068
1069 #ifdef CONFIG_KCOV
1070         u64                     kcov_handle;
1071 #endif
1072
1073         ); /* end headers group */
1074
1075         /* These elements must be at the end, see alloc_skb() for details.  */
1076         sk_buff_data_t          tail;
1077         sk_buff_data_t          end;
1078         unsigned char           *head,
1079                                 *data;
1080         unsigned int            truesize;
1081         refcount_t              users;
1082
1083 #ifdef CONFIG_SKB_EXTENSIONS
1084         /* only useable after checking ->active_extensions != 0 */
1085         struct skb_ext          *extensions;
1086 #endif
1087 };
1088
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)
1092 #else
1093 #define PKT_TYPE_MAX    7
1094 #endif
1095 #define PKT_TYPE_OFFSET         offsetof(struct sk_buff, __pkt_type_offset)
1096
1097 /* if you move pkt_vlan_present, tc_at_ingress, or mono_delivery_time
1098  * around, you also must adapt these constants.
1099  */
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)
1104 #else
1105 #define PKT_VLAN_PRESENT_BIT    0
1106 #define TC_AT_INGRESS_MASK              (1 << 7)
1107 #define SKB_MONO_DELIVERY_TIME_MASK     (1 << 5)
1108 #endif
1109 #define PKT_VLAN_PRESENT_OFFSET offsetof(struct sk_buff, __pkt_vlan_present_offset)
1110
1111 #ifdef __KERNEL__
1112 /*
1113  *      Handling routines are only of interest to the kernel
1114  */
1115
1116 #define SKB_ALLOC_FCLONE        0x01
1117 #define SKB_ALLOC_RX            0x02
1118 #define SKB_ALLOC_NAPI          0x04
1119
1120 /**
1121  * skb_pfmemalloc - Test if the skb was allocated from PFMEMALLOC reserves
1122  * @skb: buffer
1123  */
1124 static inline bool skb_pfmemalloc(const struct sk_buff *skb)
1125 {
1126         return unlikely(skb->pfmemalloc);
1127 }
1128
1129 /*
1130  * skb might have a dst pointer attached, refcounted or not.
1131  * _skb_refdst low order bit is set if refcount was _not_ taken
1132  */
1133 #define SKB_DST_NOREF   1UL
1134 #define SKB_DST_PTRMASK ~(SKB_DST_NOREF)
1135
1136 /**
1137  * skb_dst - returns skb dst_entry
1138  * @skb: buffer
1139  *
1140  * Returns skb dst_entry, regardless of reference taken or not.
1141  */
1142 static inline struct dst_entry *skb_dst(const struct sk_buff *skb)
1143 {
1144         /* If refdst was not refcounted, check we still are in a
1145          * rcu_read_lock section
1146          */
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);
1151 }
1152
1153 /**
1154  * skb_dst_set - sets skb dst
1155  * @skb: buffer
1156  * @dst: dst entry
1157  *
1158  * Sets skb dst, assuming a reference was taken on dst and should
1159  * be released by skb_dst_drop()
1160  */
1161 static inline void skb_dst_set(struct sk_buff *skb, struct dst_entry *dst)
1162 {
1163         skb->slow_gro |= !!dst;
1164         skb->_skb_refdst = (unsigned long)dst;
1165 }
1166
1167 /**
1168  * skb_dst_set_noref - sets skb dst, hopefully, without taking reference
1169  * @skb: buffer
1170  * @dst: dst entry
1171  *
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.
1176  */
1177 static inline void skb_dst_set_noref(struct sk_buff *skb, struct dst_entry *dst)
1178 {
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;
1182 }
1183
1184 /**
1185  * skb_dst_is_noref - Test if skb dst isn't refcounted
1186  * @skb: buffer
1187  */
1188 static inline bool skb_dst_is_noref(const struct sk_buff *skb)
1189 {
1190         return (skb->_skb_refdst & SKB_DST_NOREF) && skb_dst(skb);
1191 }
1192
1193 /**
1194  * skb_rtable - Returns the skb &rtable
1195  * @skb: buffer
1196  */
1197 static inline struct rtable *skb_rtable(const struct sk_buff *skb)
1198 {
1199         return (struct rtable *)skb_dst(skb);
1200 }
1201
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.
1205 */
1206 static inline bool skb_pkt_type_ok(u32 ptype)
1207 {
1208         return ptype <= PACKET_OTHERHOST;
1209 }
1210
1211 /**
1212  * skb_napi_id - Returns the skb's NAPI id
1213  * @skb: buffer
1214  */
1215 static inline unsigned int skb_napi_id(const struct sk_buff *skb)
1216 {
1217 #ifdef CONFIG_NET_RX_BUSY_POLL
1218         return skb->napi_id;
1219 #else
1220         return 0;
1221 #endif
1222 }
1223
1224 /**
1225  * skb_unref - decrement the skb's reference count
1226  * @skb: buffer
1227  *
1228  * Returns true if we can free the skb.
1229  */
1230 static inline bool skb_unref(struct sk_buff *skb)
1231 {
1232         if (unlikely(!skb))
1233                 return false;
1234         if (likely(refcount_read(&skb->users) == 1))
1235                 smp_rmb();
1236         else if (likely(!refcount_dec_and_test(&skb->users)))
1237                 return false;
1238
1239         return true;
1240 }
1241
1242 void kfree_skb_reason(struct sk_buff *skb, enum skb_drop_reason reason);
1243
1244 /**
1245  *      kfree_skb - free an sk_buff with 'NOT_SPECIFIED' reason
1246  *      @skb: buffer to free
1247  */
1248 static inline void kfree_skb(struct sk_buff *skb)
1249 {
1250         kfree_skb_reason(skb, SKB_DROP_REASON_NOT_SPECIFIED);
1251 }
1252
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);
1258
1259 static inline void kfree_skb_list(struct sk_buff *segs)
1260 {
1261         kfree_skb_list_reason(segs, SKB_DROP_REASON_NOT_SPECIFIED);
1262 }
1263
1264 #ifdef CONFIG_TRACEPOINTS
1265 void consume_skb(struct sk_buff *skb);
1266 #else
1267 static inline void consume_skb(struct sk_buff *skb)
1268 {
1269         return kfree_skb(skb);
1270 }
1271 #endif
1272
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;
1276
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);
1280
1281 struct sk_buff *__alloc_skb(unsigned int size, gfp_t priority, int flags,
1282                             int node);
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);
1287
1288 struct sk_buff *napi_build_skb(void *data, unsigned int frag_size);
1289
1290 /**
1291  * alloc_skb - allocate a network buffer
1292  * @size: size to allocate
1293  * @priority: allocation mask
1294  *
1295  * This function is a convenient wrapper around __alloc_skb().
1296  */
1297 static inline struct sk_buff *alloc_skb(unsigned int size,
1298                                         gfp_t priority)
1299 {
1300         return __alloc_skb(size, priority, 0, NUMA_NO_NODE);
1301 }
1302
1303 struct sk_buff *alloc_skb_with_frags(unsigned long header_len,
1304                                      unsigned long data_len,
1305                                      int max_page_order,
1306                                      int *errcode,
1307                                      gfp_t gfp_mask);
1308 struct sk_buff *alloc_skb_for_msg(struct sk_buff *first);
1309
1310 /* Layout of fast clones : [skb1][skb2][fclone_ref] */
1311 struct sk_buff_fclones {
1312         struct sk_buff  skb1;
1313
1314         struct sk_buff  skb2;
1315
1316         refcount_t      fclone_ref;
1317 };
1318
1319 /**
1320  *      skb_fclone_busy - check if fclone is busy
1321  *      @sk: socket
1322  *      @skb: buffer
1323  *
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.
1327  */
1328 static inline bool skb_fclone_busy(const struct sock *sk,
1329                                    const struct sk_buff *skb)
1330 {
1331         const struct sk_buff_fclones *fclones;
1332
1333         fclones = container_of(skb, struct sk_buff_fclones, skb1);
1334
1335         return skb->fclone == SKB_FCLONE_ORIG &&
1336                refcount_read(&fclones->fclone_ref) > 1 &&
1337                READ_ONCE(fclones->skb2.sk) == sk;
1338 }
1339
1340 /**
1341  * alloc_skb_fclone - allocate a network buffer from fclone cache
1342  * @size: size to allocate
1343  * @priority: allocation mask
1344  *
1345  * This function is a convenient wrapper around __alloc_skb().
1346  */
1347 static inline struct sk_buff *alloc_skb_fclone(unsigned int size,
1348                                                gfp_t priority)
1349 {
1350         return __alloc_skb(size, priority, SKB_ALLOC_FCLONE, NUMA_NO_NODE);
1351 }
1352
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,
1362                                           gfp_t gfp_mask)
1363 {
1364         return __pskb_copy_fclone(skb, headroom, gfp_mask, false);
1365 }
1366
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);
1379
1380 /**
1381  *      skb_pad                 -       zero pad the tail of an skb
1382  *      @skb: buffer to pad
1383  *      @pad: space to pad
1384  *
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.
1388  *
1389  *      May return error in out of memory cases. The skb is freed on error.
1390  */
1391 static inline int skb_pad(struct sk_buff *skb, int pad)
1392 {
1393         return __skb_pad(skb, pad, true);
1394 }
1395 #define dev_kfree_skb(a)        consume_skb(a)
1396
1397 int skb_append_pagefrags(struct sk_buff *skb, struct page *page,
1398                          int offset, size_t size);
1399
1400 struct skb_seq_state {
1401         __u32           lower_offset;
1402         __u32           upper_offset;
1403         __u32           frag_idx;
1404         __u32           stepped_offset;
1405         struct sk_buff  *root_skb;
1406         struct sk_buff  *cur_skb;
1407         __u8            *frag_data;
1408         __u32           frag_off;
1409 };
1410
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);
1416
1417 unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
1418                            unsigned int to, struct ts_config *config);
1419
1420 /*
1421  * Packet hash types specify the type of hash in skb_set_hash.
1422  *
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).
1427  *
1428  * Properties of hashes:
1429  *
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
1432  *
1433  * A hash at a higher layer is considered to be more specific. A driver should
1434  * set the most specific hash possible.
1435  *
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.
1438  *
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
1444  * permits this.
1445  */
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 */
1451 };
1452
1453 static inline void skb_clear_hash(struct sk_buff *skb)
1454 {
1455         skb->hash = 0;
1456         skb->sw_hash = 0;
1457         skb->l4_hash = 0;
1458 }
1459
1460 static inline void skb_clear_hash_if_not_l4(struct sk_buff *skb)
1461 {
1462         if (!skb->l4_hash)
1463                 skb_clear_hash(skb);
1464 }
1465
1466 static inline void
1467 __skb_set_hash(struct sk_buff *skb, __u32 hash, bool is_sw, bool is_l4)
1468 {
1469         skb->l4_hash = is_l4;
1470         skb->sw_hash = is_sw;
1471         skb->hash = hash;
1472 }
1473
1474 static inline void
1475 skb_set_hash(struct sk_buff *skb, __u32 hash, enum pkt_hash_types type)
1476 {
1477         /* Used by drivers to set hash from HW */
1478         __skb_set_hash(skb, hash, false, type == PKT_HASH_TYPE_L4);
1479 }
1480
1481 static inline void
1482 __skb_set_sw_hash(struct sk_buff *skb, __u32 hash, bool is_l4)
1483 {
1484         __skb_set_hash(skb, hash, true, is_l4);
1485 }
1486
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);
1494
1495 static inline __be32 skb_flow_get_ports(const struct sk_buff *skb,
1496                                         int thoff, u8 ip_proto)
1497 {
1498         return __skb_flow_get_ports(skb, thoff, ip_proto, NULL, 0);
1499 }
1500
1501 void skb_flow_dissector_init(struct flow_dissector *flow_dissector,
1502                              const struct flow_dissector_key *key,
1503                              unsigned int key_count);
1504
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);
1508
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);
1514
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)
1518 {
1519         return __skb_flow_dissect(NULL, skb, flow_dissector,
1520                                   target_container, NULL, 0, 0, 0, flags);
1521 }
1522
1523 static inline bool skb_flow_dissect_flow_keys(const struct sk_buff *skb,
1524                                               struct flow_keys *flow,
1525                                               unsigned int flags)
1526 {
1527         memset(flow, 0, sizeof(*flow));
1528         return __skb_flow_dissect(NULL, skb, &flow_keys_dissector,
1529                                   flow, NULL, 0, 0, 0, flags);
1530 }
1531
1532 static inline bool
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)
1538 {
1539         memset(flow, 0, sizeof(*flow));
1540         return __skb_flow_dissect(net, skb, &flow_keys_basic_dissector, flow,
1541                                   data, proto, nhoff, hlen, flags);
1542 }
1543
1544 void skb_flow_dissect_meta(const struct sk_buff *skb,
1545                            struct flow_dissector *flow_dissector,
1546                            void *target_container);
1547
1548 /* Gets a skb connection tracking info, ctinfo map should be a
1549  * map of mapsize to translate enum ip_conntrack_info states
1550  * to user states.
1551  */
1552 void
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);
1558 void
1559 skb_flow_dissect_tunnel_info(const struct sk_buff *skb,
1560                              struct flow_dissector *flow_dissector,
1561                              void *target_container);
1562
1563 void skb_flow_dissect_hash(const struct sk_buff *skb,
1564                            struct flow_dissector *flow_dissector,
1565                            void *target_container);
1566
1567 static inline __u32 skb_get_hash(struct sk_buff *skb)
1568 {
1569         if (!skb->l4_hash && !skb->sw_hash)
1570                 __skb_get_hash(skb);
1571
1572         return skb->hash;
1573 }
1574
1575 static inline __u32 skb_get_hash_flowi6(struct sk_buff *skb, const struct flowi6 *fl6)
1576 {
1577         if (!skb->l4_hash && !skb->sw_hash) {
1578                 struct flow_keys keys;
1579                 __u32 hash = __get_hash_from_flowi6(fl6, &keys);
1580
1581                 __skb_set_sw_hash(skb, hash, flow_keys_have_l4(&keys));
1582         }
1583
1584         return skb->hash;
1585 }
1586
1587 __u32 skb_get_hash_perturb(const struct sk_buff *skb,
1588                            const siphash_key_t *perturb);
1589
1590 static inline __u32 skb_get_hash_raw(const struct sk_buff *skb)
1591 {
1592         return skb->hash;
1593 }
1594
1595 static inline void skb_copy_hash(struct sk_buff *to, const struct sk_buff *from)
1596 {
1597         to->hash = from->hash;
1598         to->sw_hash = from->sw_hash;
1599         to->l4_hash = from->l4_hash;
1600 };
1601
1602 static inline void skb_copy_decrypted(struct sk_buff *to,
1603                                       const struct sk_buff *from)
1604 {
1605 #ifdef CONFIG_TLS_DEVICE
1606         to->decrypted = from->decrypted;
1607 #endif
1608 }
1609
1610 #ifdef NET_SKBUFF_DATA_USES_OFFSET
1611 static inline unsigned char *skb_end_pointer(const struct sk_buff *skb)
1612 {
1613         return skb->head + skb->end;
1614 }
1615
1616 static inline unsigned int skb_end_offset(const struct sk_buff *skb)
1617 {
1618         return skb->end;
1619 }
1620
1621 static inline void skb_set_end_offset(struct sk_buff *skb, unsigned int offset)
1622 {
1623         skb->end = offset;
1624 }
1625 #else
1626 static inline unsigned char *skb_end_pointer(const struct sk_buff *skb)
1627 {
1628         return skb->end;
1629 }
1630
1631 static inline unsigned int skb_end_offset(const struct sk_buff *skb)
1632 {
1633         return skb->end - skb->head;
1634 }
1635
1636 static inline void skb_set_end_offset(struct sk_buff *skb, unsigned int offset)
1637 {
1638         skb->end = skb->head + offset;
1639 }
1640 #endif
1641
1642 /* Internal */
1643 #define skb_shinfo(SKB) ((struct skb_shared_info *)(skb_end_pointer(SKB)))
1644
1645 static inline struct skb_shared_hwtstamps *skb_hwtstamps(struct sk_buff *skb)
1646 {
1647         return &skb_shinfo(skb)->hwtstamps;
1648 }
1649
1650 static inline struct ubuf_info *skb_zcopy(struct sk_buff *skb)
1651 {
1652         bool is_zcopy = skb && skb_shinfo(skb)->flags & SKBFL_ZEROCOPY_ENABLE;
1653
1654         return is_zcopy ? skb_uarg(skb) : NULL;
1655 }
1656
1657 static inline bool skb_zcopy_pure(const struct sk_buff *skb)
1658 {
1659         return skb_shinfo(skb)->flags & SKBFL_PURE_ZEROCOPY;
1660 }
1661
1662 static inline bool skb_pure_zcopy_same(const struct sk_buff *skb1,
1663                                        const struct sk_buff *skb2)
1664 {
1665         return skb_zcopy_pure(skb1) == skb_zcopy_pure(skb2);
1666 }
1667
1668 static inline void net_zcopy_get(struct ubuf_info *uarg)
1669 {
1670         refcount_inc(&uarg->refcnt);
1671 }
1672
1673 static inline void skb_zcopy_init(struct sk_buff *skb, struct ubuf_info *uarg)
1674 {
1675         skb_shinfo(skb)->destructor_arg = uarg;
1676         skb_shinfo(skb)->flags |= uarg->flags;
1677 }
1678
1679 static inline void skb_zcopy_set(struct sk_buff *skb, struct ubuf_info *uarg,
1680                                  bool *have_ref)
1681 {
1682         if (skb && uarg && !skb_zcopy(skb)) {
1683                 if (unlikely(have_ref && *have_ref))
1684                         *have_ref = false;
1685                 else
1686                         net_zcopy_get(uarg);
1687                 skb_zcopy_init(skb, uarg);
1688         }
1689 }
1690
1691 static inline void skb_zcopy_set_nouarg(struct sk_buff *skb, void *val)
1692 {
1693         skb_shinfo(skb)->destructor_arg = (void *)((uintptr_t) val | 0x1UL);
1694         skb_shinfo(skb)->flags |= SKBFL_ZEROCOPY_FRAG;
1695 }
1696
1697 static inline bool skb_zcopy_is_nouarg(struct sk_buff *skb)
1698 {
1699         return (uintptr_t) skb_shinfo(skb)->destructor_arg & 0x1UL;
1700 }
1701
1702 static inline void *skb_zcopy_get_nouarg(struct sk_buff *skb)
1703 {
1704         return (void *)((uintptr_t) skb_shinfo(skb)->destructor_arg & ~0x1UL);
1705 }
1706
1707 static inline void net_zcopy_put(struct ubuf_info *uarg)
1708 {
1709         if (uarg)
1710                 uarg->callback(NULL, uarg, true);
1711 }
1712
1713 static inline void net_zcopy_put_abort(struct ubuf_info *uarg, bool have_uref)
1714 {
1715         if (uarg) {
1716                 if (uarg->callback == msg_zerocopy_callback)
1717                         msg_zerocopy_put_abort(uarg, have_uref);
1718                 else if (have_uref)
1719                         net_zcopy_put(uarg);
1720         }
1721 }
1722
1723 /* Release a reference on a zerocopy structure */
1724 static inline void skb_zcopy_clear(struct sk_buff *skb, bool zerocopy_success)
1725 {
1726         struct ubuf_info *uarg = skb_zcopy(skb);
1727
1728         if (uarg) {
1729                 if (!skb_zcopy_is_nouarg(skb))
1730                         uarg->callback(skb, uarg, zerocopy_success);
1731
1732                 skb_shinfo(skb)->flags &= ~SKBFL_ALL_ZEROCOPY;
1733         }
1734 }
1735
1736 static inline void skb_mark_not_on_list(struct sk_buff *skb)
1737 {
1738         skb->next = NULL;
1739 }
1740
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)
1745
1746 static inline void skb_list_del_init(struct sk_buff *skb)
1747 {
1748         __list_del_entry(&skb->list);
1749         skb_mark_not_on_list(skb);
1750 }
1751
1752 /**
1753  *      skb_queue_empty - check if a queue is empty
1754  *      @list: queue head
1755  *
1756  *      Returns true if the queue is empty, false otherwise.
1757  */
1758 static inline int skb_queue_empty(const struct sk_buff_head *list)
1759 {
1760         return list->next == (const struct sk_buff *) list;
1761 }
1762
1763 /**
1764  *      skb_queue_empty_lockless - check if a queue is empty
1765  *      @list: queue head
1766  *
1767  *      Returns true if the queue is empty, false otherwise.
1768  *      This variant can be used in lockless contexts.
1769  */
1770 static inline bool skb_queue_empty_lockless(const struct sk_buff_head *list)
1771 {
1772         return READ_ONCE(list->next) == (const struct sk_buff *) list;
1773 }
1774
1775
1776 /**
1777  *      skb_queue_is_last - check if skb is the last entry in the queue
1778  *      @list: queue head
1779  *      @skb: buffer
1780  *
1781  *      Returns true if @skb is the last buffer on the list.
1782  */
1783 static inline bool skb_queue_is_last(const struct sk_buff_head *list,
1784                                      const struct sk_buff *skb)
1785 {
1786         return skb->next == (const struct sk_buff *) list;
1787 }
1788
1789 /**
1790  *      skb_queue_is_first - check if skb is the first entry in the queue
1791  *      @list: queue head
1792  *      @skb: buffer
1793  *
1794  *      Returns true if @skb is the first buffer on the list.
1795  */
1796 static inline bool skb_queue_is_first(const struct sk_buff_head *list,
1797                                       const struct sk_buff *skb)
1798 {
1799         return skb->prev == (const struct sk_buff *) list;
1800 }
1801
1802 /**
1803  *      skb_queue_next - return the next packet in the queue
1804  *      @list: queue head
1805  *      @skb: current buffer
1806  *
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.
1809  */
1810 static inline struct sk_buff *skb_queue_next(const struct sk_buff_head *list,
1811                                              const struct sk_buff *skb)
1812 {
1813         /* This BUG_ON may seem severe, but if we just return then we
1814          * are going to dereference garbage.
1815          */
1816         BUG_ON(skb_queue_is_last(list, skb));
1817         return skb->next;
1818 }
1819
1820 /**
1821  *      skb_queue_prev - return the prev packet in the queue
1822  *      @list: queue head
1823  *      @skb: current buffer
1824  *
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.
1827  */
1828 static inline struct sk_buff *skb_queue_prev(const struct sk_buff_head *list,
1829                                              const struct sk_buff *skb)
1830 {
1831         /* This BUG_ON may seem severe, but if we just return then we
1832          * are going to dereference garbage.
1833          */
1834         BUG_ON(skb_queue_is_first(list, skb));
1835         return skb->prev;
1836 }
1837
1838 /**
1839  *      skb_get - reference buffer
1840  *      @skb: buffer to reference
1841  *
1842  *      Makes another reference to a socket buffer and returns a pointer
1843  *      to the buffer.
1844  */
1845 static inline struct sk_buff *skb_get(struct sk_buff *skb)
1846 {
1847         refcount_inc(&skb->users);
1848         return skb;
1849 }
1850
1851 /*
1852  * If users == 1, we are the only owner and can avoid redundant atomic changes.
1853  */
1854
1855 /**
1856  *      skb_cloned - is the buffer a clone
1857  *      @skb: buffer to check
1858  *
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.
1862  */
1863 static inline int skb_cloned(const struct sk_buff *skb)
1864 {
1865         return skb->cloned &&
1866                (atomic_read(&skb_shinfo(skb)->dataref) & SKB_DATAREF_MASK) != 1;
1867 }
1868
1869 static inline int skb_unclone(struct sk_buff *skb, gfp_t pri)
1870 {
1871         might_sleep_if(gfpflags_allow_blocking(pri));
1872
1873         if (skb_cloned(skb))
1874                 return pskb_expand_head(skb, 0, 0, pri);
1875
1876         return 0;
1877 }
1878
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.
1881  *
1882  * Indeed there is no guarantee that ksize(kmalloc(X)) == ksize(kmalloc(X))
1883  * when various debugging features are in place.
1884  */
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)
1887 {
1888         might_sleep_if(gfpflags_allow_blocking(pri));
1889
1890         if (skb_cloned(skb))
1891                 return __skb_unclone_keeptruesize(skb, pri);
1892         return 0;
1893 }
1894
1895 /**
1896  *      skb_header_cloned - is the header a clone
1897  *      @skb: buffer to check
1898  *
1899  *      Returns true if modifying the header part of the buffer requires
1900  *      the data to be copied.
1901  */
1902 static inline int skb_header_cloned(const struct sk_buff *skb)
1903 {
1904         int dataref;
1905
1906         if (!skb->cloned)
1907                 return 0;
1908
1909         dataref = atomic_read(&skb_shinfo(skb)->dataref);
1910         dataref = (dataref & SKB_DATAREF_MASK) - (dataref >> SKB_DATAREF_SHIFT);
1911         return dataref != 1;
1912 }
1913
1914 static inline int skb_header_unclone(struct sk_buff *skb, gfp_t pri)
1915 {
1916         might_sleep_if(gfpflags_allow_blocking(pri));
1917
1918         if (skb_header_cloned(skb))
1919                 return pskb_expand_head(skb, 0, 0, pri);
1920
1921         return 0;
1922 }
1923
1924 /**
1925  *      __skb_header_release - release reference to header
1926  *      @skb: buffer to operate on
1927  */
1928 static inline void __skb_header_release(struct sk_buff *skb)
1929 {
1930         skb->nohdr = 1;
1931         atomic_set(&skb_shinfo(skb)->dataref, 1 + (1 << SKB_DATAREF_SHIFT));
1932 }
1933
1934
1935 /**
1936  *      skb_shared - is the buffer shared
1937  *      @skb: buffer to check
1938  *
1939  *      Returns true if more than one person has a reference to this
1940  *      buffer.
1941  */
1942 static inline int skb_shared(const struct sk_buff *skb)
1943 {
1944         return refcount_read(&skb->users) != 1;
1945 }
1946
1947 /**
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
1951  *
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
1956  *      be GFP_ATOMIC.
1957  *
1958  *      NULL is returned on a memory allocation failure.
1959  */
1960 static inline struct sk_buff *skb_share_check(struct sk_buff *skb, gfp_t pri)
1961 {
1962         might_sleep_if(gfpflags_allow_blocking(pri));
1963         if (skb_shared(skb)) {
1964                 struct sk_buff *nskb = skb_clone(skb, pri);
1965
1966                 if (likely(nskb))
1967                         consume_skb(skb);
1968                 else
1969                         kfree_skb(skb);
1970                 skb = nskb;
1971         }
1972         return skb;
1973 }
1974
1975 /*
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.
1980  */
1981
1982 /**
1983  *      skb_unshare - make a copy of a shared buffer
1984  *      @skb: buffer to check
1985  *      @pri: priority for memory allocation
1986  *
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
1992  *
1993  *      %NULL is returned on a memory allocation failure.
1994  */
1995 static inline struct sk_buff *skb_unshare(struct sk_buff *skb,
1996                                           gfp_t pri)
1997 {
1998         might_sleep_if(gfpflags_allow_blocking(pri));
1999         if (skb_cloned(skb)) {
2000                 struct sk_buff *nskb = skb_copy(skb, pri);
2001
2002                 /* Free our shared copy */
2003                 if (likely(nskb))
2004                         consume_skb(skb);
2005                 else
2006                         kfree_skb(skb);
2007                 skb = nskb;
2008         }
2009         return skb;
2010 }
2011
2012 /**
2013  *      skb_peek - peek at the head of an &sk_buff_head
2014  *      @list_: list to peek at
2015  *
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.
2020  *
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.
2024  */
2025 static inline struct sk_buff *skb_peek(const struct sk_buff_head *list_)
2026 {
2027         struct sk_buff *skb = list_->next;
2028
2029         if (skb == (struct sk_buff *)list_)
2030                 skb = NULL;
2031         return skb;
2032 }
2033
2034 /**
2035  *      __skb_peek - peek at the head of a non-empty &sk_buff_head
2036  *      @list_: list to peek at
2037  *
2038  *      Like skb_peek(), but the caller knows that the list is not empty.
2039  */
2040 static inline struct sk_buff *__skb_peek(const struct sk_buff_head *list_)
2041 {
2042         return list_->next;
2043 }
2044
2045 /**
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
2049  *
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.
2053  */
2054 static inline struct sk_buff *skb_peek_next(struct sk_buff *skb,
2055                 const struct sk_buff_head *list_)
2056 {
2057         struct sk_buff *next = skb->next;
2058
2059         if (next == (struct sk_buff *)list_)
2060                 next = NULL;
2061         return next;
2062 }
2063
2064 /**
2065  *      skb_peek_tail - peek at the tail of an &sk_buff_head
2066  *      @list_: list to peek at
2067  *
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.
2072  *
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.
2076  */
2077 static inline struct sk_buff *skb_peek_tail(const struct sk_buff_head *list_)
2078 {
2079         struct sk_buff *skb = READ_ONCE(list_->prev);
2080
2081         if (skb == (struct sk_buff *)list_)
2082                 skb = NULL;
2083         return skb;
2084
2085 }
2086
2087 /**
2088  *      skb_queue_len   - get queue length
2089  *      @list_: list to measure
2090  *
2091  *      Return the length of an &sk_buff queue.
2092  */
2093 static inline __u32 skb_queue_len(const struct sk_buff_head *list_)
2094 {
2095         return list_->qlen;
2096 }
2097
2098 /**
2099  *      skb_queue_len_lockless  - get queue length
2100  *      @list_: list to measure
2101  *
2102  *      Return the length of an &sk_buff queue.
2103  *      This variant can be used in lockless contexts.
2104  */
2105 static inline __u32 skb_queue_len_lockless(const struct sk_buff_head *list_)
2106 {
2107         return READ_ONCE(list_->qlen);
2108 }
2109
2110 /**
2111  *      __skb_queue_head_init - initialize non-spinlock portions of sk_buff_head
2112  *      @list: queue to initialize
2113  *
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.
2119  */
2120 static inline void __skb_queue_head_init(struct sk_buff_head *list)
2121 {
2122         list->prev = list->next = (struct sk_buff *)list;
2123         list->qlen = 0;
2124 }
2125
2126 /*
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.
2133  */
2134 static inline void skb_queue_head_init(struct sk_buff_head *list)
2135 {
2136         spin_lock_init(&list->lock);
2137         __skb_queue_head_init(list);
2138 }
2139
2140 static inline void skb_queue_head_init_class(struct sk_buff_head *list,
2141                 struct lock_class_key *class)
2142 {
2143         skb_queue_head_init(list);
2144         lockdep_set_class(&list->lock, class);
2145 }
2146
2147 /*
2148  *      Insert an sk_buff on a list.
2149  *
2150  *      The "__skb_xxxx()" functions are the non-atomic ones that
2151  *      can only be called with interrupts disabled.
2152  */
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)
2156 {
2157         /* See skb_queue_empty_lockless() and skb_peek_tail()
2158          * for the opposite READ_ONCE()
2159          */
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);
2165 }
2166
2167 static inline void __skb_queue_splice(const struct sk_buff_head *list,
2168                                       struct sk_buff *prev,
2169                                       struct sk_buff *next)
2170 {
2171         struct sk_buff *first = list->next;
2172         struct sk_buff *last = list->prev;
2173
2174         WRITE_ONCE(first->prev, prev);
2175         WRITE_ONCE(prev->next, first);
2176
2177         WRITE_ONCE(last->next, next);
2178         WRITE_ONCE(next->prev, last);
2179 }
2180
2181 /**
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
2185  */
2186 static inline void skb_queue_splice(const struct sk_buff_head *list,
2187                                     struct sk_buff_head *head)
2188 {
2189         if (!skb_queue_empty(list)) {
2190                 __skb_queue_splice(list, (struct sk_buff *) head, head->next);
2191                 head->qlen += list->qlen;
2192         }
2193 }
2194
2195 /**
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
2199  *
2200  *      The list at @list is reinitialised
2201  */
2202 static inline void skb_queue_splice_init(struct sk_buff_head *list,
2203                                          struct sk_buff_head *head)
2204 {
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);
2209         }
2210 }
2211
2212 /**
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
2216  */
2217 static inline void skb_queue_splice_tail(const struct sk_buff_head *list,
2218                                          struct sk_buff_head *head)
2219 {
2220         if (!skb_queue_empty(list)) {
2221                 __skb_queue_splice(list, head->prev, (struct sk_buff *) head);
2222                 head->qlen += list->qlen;
2223         }
2224 }
2225
2226 /**
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
2230  *
2231  *      Each of the lists is a queue.
2232  *      The list at @list is reinitialised
2233  */
2234 static inline void skb_queue_splice_tail_init(struct sk_buff_head *list,
2235                                               struct sk_buff_head *head)
2236 {
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);
2241         }
2242 }
2243
2244 /**
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
2249  *
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.
2252  *
2253  *      A buffer cannot be placed on two lists at the same time.
2254  */
2255 static inline void __skb_queue_after(struct sk_buff_head *list,
2256                                      struct sk_buff *prev,
2257                                      struct sk_buff *newsk)
2258 {
2259         __skb_insert(newsk, prev, ((struct sk_buff_list *)prev)->next, list);
2260 }
2261
2262 void skb_append(struct sk_buff *old, struct sk_buff *newsk,
2263                 struct sk_buff_head *list);
2264
2265 static inline void __skb_queue_before(struct sk_buff_head *list,
2266                                       struct sk_buff *next,
2267                                       struct sk_buff *newsk)
2268 {
2269         __skb_insert(newsk, ((struct sk_buff_list *)next)->prev, next, list);
2270 }
2271
2272 /**
2273  *      __skb_queue_head - queue a buffer at the list head
2274  *      @list: list to use
2275  *      @newsk: buffer to queue
2276  *
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.
2279  *
2280  *      A buffer cannot be placed on two lists at the same time.
2281  */
2282 static inline void __skb_queue_head(struct sk_buff_head *list,
2283                                     struct sk_buff *newsk)
2284 {
2285         __skb_queue_after(list, (struct sk_buff *)list, newsk);
2286 }
2287 void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk);
2288
2289 /**
2290  *      __skb_queue_tail - queue a buffer at the list tail
2291  *      @list: list to use
2292  *      @newsk: buffer to queue
2293  *
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.
2296  *
2297  *      A buffer cannot be placed on two lists at the same time.
2298  */
2299 static inline void __skb_queue_tail(struct sk_buff_head *list,
2300                                    struct sk_buff *newsk)
2301 {
2302         __skb_queue_before(list, (struct sk_buff *)list, newsk);
2303 }
2304 void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk);
2305
2306 /*
2307  * remove sk_buff from list. _Must_ be called atomically, and with
2308  * the list known..
2309  */
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)
2312 {
2313         struct sk_buff *next, *prev;
2314
2315         WRITE_ONCE(list->qlen, list->qlen - 1);
2316         next       = skb->next;
2317         prev       = skb->prev;
2318         skb->next  = skb->prev = NULL;
2319         WRITE_ONCE(next->prev, prev);
2320         WRITE_ONCE(prev->next, next);
2321 }
2322
2323 /**
2324  *      __skb_dequeue - remove from the head of the queue
2325  *      @list: list to dequeue from
2326  *
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.
2330  */
2331 static inline struct sk_buff *__skb_dequeue(struct sk_buff_head *list)
2332 {
2333         struct sk_buff *skb = skb_peek(list);
2334         if (skb)
2335                 __skb_unlink(skb, list);
2336         return skb;
2337 }
2338 struct sk_buff *skb_dequeue(struct sk_buff_head *list);
2339
2340 /**
2341  *      __skb_dequeue_tail - remove from the tail of the queue
2342  *      @list: list to dequeue from
2343  *
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.
2347  */
2348 static inline struct sk_buff *__skb_dequeue_tail(struct sk_buff_head *list)
2349 {
2350         struct sk_buff *skb = skb_peek_tail(list);
2351         if (skb)
2352                 __skb_unlink(skb, list);
2353         return skb;
2354 }
2355 struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list);
2356
2357
2358 static inline bool skb_is_nonlinear(const struct sk_buff *skb)
2359 {
2360         return skb->data_len;
2361 }
2362
2363 static inline unsigned int skb_headlen(const struct sk_buff *skb)
2364 {
2365         return skb->len - skb->data_len;
2366 }
2367
2368 static inline unsigned int __skb_pagelen(const struct sk_buff *skb)
2369 {
2370         unsigned int i, len = 0;
2371
2372         for (i = skb_shinfo(skb)->nr_frags - 1; (int)i >= 0; i--)
2373                 len += skb_frag_size(&skb_shinfo(skb)->frags[i]);
2374         return len;
2375 }
2376
2377 static inline unsigned int skb_pagelen(const struct sk_buff *skb)
2378 {
2379         return skb_headlen(skb) + __skb_pagelen(skb);
2380 }
2381
2382 /**
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
2389  *
2390  * Initialises the @i'th fragment of @skb to point to &size bytes at
2391  * offset @off within @page.
2392  *
2393  * Does not take any additional reference on the fragment.
2394  */
2395 static inline void __skb_fill_page_desc(struct sk_buff *skb, int i,
2396                                         struct page *page, int off, int size)
2397 {
2398         skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2399
2400         /*
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).
2404          */
2405         frag->bv_page             = page;
2406         frag->bv_offset           = off;
2407         skb_frag_size_set(frag, size);
2408
2409         page = compound_head(page);
2410         if (page_is_pfmemalloc(page))
2411                 skb->pfmemalloc = true;
2412 }
2413
2414 /**
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
2421  *
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.
2425  *
2426  * Does not take any additional reference on the fragment.
2427  */
2428 static inline void skb_fill_page_desc(struct sk_buff *skb, int i,
2429                                       struct page *page, int off, int size)
2430 {
2431         __skb_fill_page_desc(skb, i, page, off, size);
2432         skb_shinfo(skb)->nr_frags = i + 1;
2433 }
2434
2435 void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off,
2436                      int size, unsigned int truesize);
2437
2438 void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size,
2439                           unsigned int truesize);
2440
2441 #define SKB_LINEAR_ASSERT(skb)  BUG_ON(skb_is_nonlinear(skb))
2442
2443 #ifdef NET_SKBUFF_DATA_USES_OFFSET
2444 static inline unsigned char *skb_tail_pointer(const struct sk_buff *skb)
2445 {
2446         return skb->head + skb->tail;
2447 }
2448
2449 static inline void skb_reset_tail_pointer(struct sk_buff *skb)
2450 {
2451         skb->tail = skb->data - skb->head;
2452 }
2453
2454 static inline void skb_set_tail_pointer(struct sk_buff *skb, const int offset)
2455 {
2456         skb_reset_tail_pointer(skb);
2457         skb->tail += offset;
2458 }
2459
2460 #else /* NET_SKBUFF_DATA_USES_OFFSET */
2461 static inline unsigned char *skb_tail_pointer(const struct sk_buff *skb)
2462 {
2463         return skb->tail;
2464 }
2465
2466 static inline void skb_reset_tail_pointer(struct sk_buff *skb)
2467 {
2468         skb->tail = skb->data;
2469 }
2470
2471 static inline void skb_set_tail_pointer(struct sk_buff *skb, const int offset)
2472 {
2473         skb->tail = skb->data + offset;
2474 }
2475
2476 #endif /* NET_SKBUFF_DATA_USES_OFFSET */
2477
2478 /*
2479  *      Add data to an sk_buff
2480  */
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)
2484 {
2485         void *tmp = skb_tail_pointer(skb);
2486         SKB_LINEAR_ASSERT(skb);
2487         skb->tail += len;
2488         skb->len  += len;
2489         return tmp;
2490 }
2491
2492 static inline void *__skb_put_zero(struct sk_buff *skb, unsigned int len)
2493 {
2494         void *tmp = __skb_put(skb, len);
2495
2496         memset(tmp, 0, len);
2497         return tmp;
2498 }
2499
2500 static inline void *__skb_put_data(struct sk_buff *skb, const void *data,
2501                                    unsigned int len)
2502 {
2503         void *tmp = __skb_put(skb, len);
2504
2505         memcpy(tmp, data, len);
2506         return tmp;
2507 }
2508
2509 static inline void __skb_put_u8(struct sk_buff *skb, u8 val)
2510 {
2511         *(u8 *)__skb_put(skb, 1) = val;
2512 }
2513
2514 static inline void *skb_put_zero(struct sk_buff *skb, unsigned int len)
2515 {
2516         void *tmp = skb_put(skb, len);
2517
2518         memset(tmp, 0, len);
2519
2520         return tmp;
2521 }
2522
2523 static inline void *skb_put_data(struct sk_buff *skb, const void *data,
2524                                  unsigned int len)
2525 {
2526         void *tmp = skb_put(skb, len);
2527
2528         memcpy(tmp, data, len);
2529
2530         return tmp;
2531 }
2532
2533 static inline void skb_put_u8(struct sk_buff *skb, u8 val)
2534 {
2535         *(u8 *)skb_put(skb, 1) = val;
2536 }
2537
2538 void *skb_push(struct sk_buff *skb, unsigned int len);
2539 static inline void *__skb_push(struct sk_buff *skb, unsigned int len)
2540 {
2541         skb->data -= len;
2542         skb->len  += len;
2543         return skb->data;
2544 }
2545
2546 void *skb_pull(struct sk_buff *skb, unsigned int len);
2547 static inline void *__skb_pull(struct sk_buff *skb, unsigned int len)
2548 {
2549         skb->len -= len;
2550         BUG_ON(skb->len < skb->data_len);
2551         return skb->data += len;
2552 }
2553
2554 static inline void *skb_pull_inline(struct sk_buff *skb, unsigned int len)
2555 {
2556         return unlikely(len > skb->len) ? NULL : __skb_pull(skb, len);
2557 }
2558
2559 void *skb_pull_data(struct sk_buff *skb, size_t len);
2560
2561 void *__pskb_pull_tail(struct sk_buff *skb, int delta);
2562
2563 static inline void *__pskb_pull(struct sk_buff *skb, unsigned int len)
2564 {
2565         if (len > skb_headlen(skb) &&
2566             !__pskb_pull_tail(skb, len - skb_headlen(skb)))
2567                 return NULL;
2568         skb->len -= len;
2569         return skb->data += len;
2570 }
2571
2572 static inline void *pskb_pull(struct sk_buff *skb, unsigned int len)
2573 {
2574         return unlikely(len > skb->len) ? NULL : __pskb_pull(skb, len);
2575 }
2576
2577 static inline bool pskb_may_pull(struct sk_buff *skb, unsigned int len)
2578 {
2579         if (likely(len <= skb_headlen(skb)))
2580                 return true;
2581         if (unlikely(len > skb->len))
2582                 return false;
2583         return __pskb_pull_tail(skb, len - skb_headlen(skb)) != NULL;
2584 }
2585
2586 void skb_condense(struct sk_buff *skb);
2587
2588 /**
2589  *      skb_headroom - bytes at buffer head
2590  *      @skb: buffer to check
2591  *
2592  *      Return the number of bytes of free space at the head of an &sk_buff.
2593  */
2594 static inline unsigned int skb_headroom(const struct sk_buff *skb)
2595 {
2596         return skb->data - skb->head;
2597 }
2598
2599 /**
2600  *      skb_tailroom - bytes at buffer end
2601  *      @skb: buffer to check
2602  *
2603  *      Return the number of bytes of free space at the tail of an sk_buff
2604  */
2605 static inline int skb_tailroom(const struct sk_buff *skb)
2606 {
2607         return skb_is_nonlinear(skb) ? 0 : skb->end - skb->tail;
2608 }
2609
2610 /**
2611  *      skb_availroom - bytes at buffer end
2612  *      @skb: buffer to check
2613  *
2614  *      Return the number of bytes of free space at the tail of an sk_buff
2615  *      allocated by sk_stream_alloc()
2616  */
2617 static inline int skb_availroom(const struct sk_buff *skb)
2618 {
2619         if (skb_is_nonlinear(skb))
2620                 return 0;
2621
2622         return skb->end - skb->tail - skb->reserved_tailroom;
2623 }
2624
2625 /**
2626  *      skb_reserve - adjust headroom
2627  *      @skb: buffer to alter
2628  *      @len: bytes to move
2629  *
2630  *      Increase the headroom of an empty &sk_buff by reducing the tail
2631  *      room. This is only allowed for an empty buffer.
2632  */
2633 static inline void skb_reserve(struct sk_buff *skb, int len)
2634 {
2635         skb->data += len;
2636         skb->tail += len;
2637 }
2638
2639 /**
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
2644  *
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
2647  *      needed_tailroom.
2648  *      The required headroom should already have been reserved before using
2649  *      this function.
2650  */
2651 static inline void skb_tailroom_reserve(struct sk_buff *skb, unsigned int mtu,
2652                                         unsigned int needed_tailroom)
2653 {
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;
2658         else
2659                 /* use up to all available space */
2660                 skb->reserved_tailroom = needed_tailroom;
2661 }
2662
2663 #define ENCAP_TYPE_ETHER        0
2664 #define ENCAP_TYPE_IPPROTO      1
2665
2666 static inline void skb_set_inner_protocol(struct sk_buff *skb,
2667                                           __be16 protocol)
2668 {
2669         skb->inner_protocol = protocol;
2670         skb->inner_protocol_type = ENCAP_TYPE_ETHER;
2671 }
2672
2673 static inline void skb_set_inner_ipproto(struct sk_buff *skb,
2674                                          __u8 ipproto)
2675 {
2676         skb->inner_ipproto = ipproto;
2677         skb->inner_protocol_type = ENCAP_TYPE_IPPROTO;
2678 }
2679
2680 static inline void skb_reset_inner_headers(struct sk_buff *skb)
2681 {
2682         skb->inner_mac_header = skb->mac_header;
2683         skb->inner_network_header = skb->network_header;
2684         skb->inner_transport_header = skb->transport_header;
2685 }
2686
2687 static inline void skb_reset_mac_len(struct sk_buff *skb)
2688 {
2689         skb->mac_len = skb->network_header - skb->mac_header;
2690 }
2691
2692 static inline unsigned char *skb_inner_transport_header(const struct sk_buff
2693                                                         *skb)
2694 {
2695         return skb->head + skb->inner_transport_header;
2696 }
2697
2698 static inline int skb_inner_transport_offset(const struct sk_buff *skb)
2699 {
2700         return skb_inner_transport_header(skb) - skb->data;
2701 }
2702
2703 static inline void skb_reset_inner_transport_header(struct sk_buff *skb)
2704 {
2705         skb->inner_transport_header = skb->data - skb->head;
2706 }
2707
2708 static inline void skb_set_inner_transport_header(struct sk_buff *skb,
2709                                                    const int offset)
2710 {
2711         skb_reset_inner_transport_header(skb);
2712         skb->inner_transport_header += offset;
2713 }
2714
2715 static inline unsigned char *skb_inner_network_header(const struct sk_buff *skb)
2716 {
2717         return skb->head + skb->inner_network_header;
2718 }
2719
2720 static inline void skb_reset_inner_network_header(struct sk_buff *skb)
2721 {
2722         skb->inner_network_header = skb->data - skb->head;
2723 }
2724
2725 static inline void skb_set_inner_network_header(struct sk_buff *skb,
2726                                                 const int offset)
2727 {
2728         skb_reset_inner_network_header(skb);
2729         skb->inner_network_header += offset;
2730 }
2731
2732 static inline unsigned char *skb_inner_mac_header(const struct sk_buff *skb)
2733 {
2734         return skb->head + skb->inner_mac_header;
2735 }
2736
2737 static inline void skb_reset_inner_mac_header(struct sk_buff *skb)
2738 {
2739         skb->inner_mac_header = skb->data - skb->head;
2740 }
2741
2742 static inline void skb_set_inner_mac_header(struct sk_buff *skb,
2743                                             const int offset)
2744 {
2745         skb_reset_inner_mac_header(skb);
2746         skb->inner_mac_header += offset;
2747 }
2748 static inline bool skb_transport_header_was_set(const struct sk_buff *skb)
2749 {
2750         return skb->transport_header != (typeof(skb->transport_header))~0U;
2751 }
2752
2753 static inline unsigned char *skb_transport_header(const struct sk_buff *skb)
2754 {
2755         return skb->head + skb->transport_header;
2756 }
2757
2758 static inline void skb_reset_transport_header(struct sk_buff *skb)
2759 {
2760         skb->transport_header = skb->data - skb->head;
2761 }
2762
2763 static inline void skb_set_transport_header(struct sk_buff *skb,
2764                                             const int offset)
2765 {
2766         skb_reset_transport_header(skb);
2767         skb->transport_header += offset;
2768 }
2769
2770 static inline unsigned char *skb_network_header(const struct sk_buff *skb)
2771 {
2772         return skb->head + skb->network_header;
2773 }
2774
2775 static inline void skb_reset_network_header(struct sk_buff *skb)
2776 {
2777         skb->network_header = skb->data - skb->head;
2778 }
2779
2780 static inline void skb_set_network_header(struct sk_buff *skb, const int offset)
2781 {
2782         skb_reset_network_header(skb);
2783         skb->network_header += offset;
2784 }
2785
2786 static inline unsigned char *skb_mac_header(const struct sk_buff *skb)
2787 {
2788         return skb->head + skb->mac_header;
2789 }
2790
2791 static inline int skb_mac_offset(const struct sk_buff *skb)
2792 {
2793         return skb_mac_header(skb) - skb->data;
2794 }
2795
2796 static inline u32 skb_mac_header_len(const struct sk_buff *skb)
2797 {
2798         return skb->network_header - skb->mac_header;
2799 }
2800
2801 static inline int skb_mac_header_was_set(const struct sk_buff *skb)
2802 {
2803         return skb->mac_header != (typeof(skb->mac_header))~0U;
2804 }
2805
2806 static inline void skb_unset_mac_header(struct sk_buff *skb)
2807 {
2808         skb->mac_header = (typeof(skb->mac_header))~0U;
2809 }
2810
2811 static inline void skb_reset_mac_header(struct sk_buff *skb)
2812 {
2813         skb->mac_header = skb->data - skb->head;
2814 }
2815
2816 static inline void skb_set_mac_header(struct sk_buff *skb, const int offset)
2817 {
2818         skb_reset_mac_header(skb);
2819         skb->mac_header += offset;
2820 }
2821
2822 static inline void skb_pop_mac_header(struct sk_buff *skb)
2823 {
2824         skb->mac_header = skb->network_header;
2825 }
2826
2827 static inline void skb_probe_transport_header(struct sk_buff *skb)
2828 {
2829         struct flow_keys_basic keys;
2830
2831         if (skb_transport_header_was_set(skb))
2832                 return;
2833
2834         if (skb_flow_dissect_flow_keys_basic(NULL, skb, &keys,
2835                                              NULL, 0, 0, 0, 0))
2836                 skb_set_transport_header(skb, keys.control.thoff);
2837 }
2838
2839 static inline void skb_mac_header_rebuild(struct sk_buff *skb)
2840 {
2841         if (skb_mac_header_was_set(skb)) {
2842                 const unsigned char *old_mac = skb_mac_header(skb);
2843
2844                 skb_set_mac_header(skb, -skb->mac_len);
2845                 memmove(skb_mac_header(skb), old_mac, skb->mac_len);
2846         }
2847 }
2848
2849 static inline int skb_checksum_start_offset(const struct sk_buff *skb)
2850 {
2851         return skb->csum_start - skb_headroom(skb);
2852 }
2853
2854 static inline unsigned char *skb_checksum_start(const struct sk_buff *skb)
2855 {
2856         return skb->head + skb->csum_start;
2857 }
2858
2859 static inline int skb_transport_offset(const struct sk_buff *skb)
2860 {
2861         return skb_transport_header(skb) - skb->data;
2862 }
2863
2864 static inline u32 skb_network_header_len(const struct sk_buff *skb)
2865 {
2866         return skb->transport_header - skb->network_header;
2867 }
2868
2869 static inline u32 skb_inner_network_header_len(const struct sk_buff *skb)
2870 {
2871         return skb->inner_transport_header - skb->inner_network_header;
2872 }
2873
2874 static inline int skb_network_offset(const struct sk_buff *skb)
2875 {
2876         return skb_network_header(skb) - skb->data;
2877 }
2878
2879 static inline int skb_inner_network_offset(const struct sk_buff *skb)
2880 {
2881         return skb_inner_network_header(skb) - skb->data;
2882 }
2883
2884 static inline int pskb_network_may_pull(struct sk_buff *skb, unsigned int len)
2885 {
2886         return pskb_may_pull(skb, skb_network_offset(skb) + len);
2887 }
2888
2889 /*
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
2893  * in software.
2894  *
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
2898  * with:
2899  *
2900  * skb_reserve(skb, NET_IP_ALIGN);
2901  *
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.
2905  *
2906  * Since this trade off varies between architectures, we allow NET_IP_ALIGN
2907  * to be overridden.
2908  */
2909 #ifndef NET_IP_ALIGN
2910 #define NET_IP_ALIGN    2
2911 #endif
2912
2913 /*
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.
2918  *
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.
2924  *
2925  * Various parts of the networking layer expect at least 32 bytes of
2926  * headroom, you should not reduce this.
2927  *
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)
2932  */
2933 #ifndef NET_SKB_PAD
2934 #define NET_SKB_PAD     max(32, L1_CACHE_BYTES)
2935 #endif
2936
2937 int ___pskb_trim(struct sk_buff *skb, unsigned int len);
2938
2939 static inline void __skb_set_length(struct sk_buff *skb, unsigned int len)
2940 {
2941         if (WARN_ON(skb_is_nonlinear(skb)))
2942                 return;
2943         skb->len = len;
2944         skb_set_tail_pointer(skb, len);
2945 }
2946
2947 static inline void __skb_trim(struct sk_buff *skb, unsigned int len)
2948 {
2949         __skb_set_length(skb, len);
2950 }
2951
2952 void skb_trim(struct sk_buff *skb, unsigned int len);
2953
2954 static inline int __pskb_trim(struct sk_buff *skb, unsigned int len)
2955 {
2956         if (skb->data_len)
2957                 return ___pskb_trim(skb, len);
2958         __skb_trim(skb, len);
2959         return 0;
2960 }
2961
2962 static inline int pskb_trim(struct sk_buff *skb, unsigned int len)
2963 {
2964         return (len < skb->len) ? __pskb_trim(skb, len) : 0;
2965 }
2966
2967 /**
2968  *      pskb_trim_unique - remove end from a paged unique (not cloned) buffer
2969  *      @skb: buffer to alter
2970  *      @len: new length
2971  *
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-
2974  *      of-memory.
2975  */
2976 static inline void pskb_trim_unique(struct sk_buff *skb, unsigned int len)
2977 {
2978         int err = pskb_trim(skb, len);
2979         BUG_ON(err);
2980 }
2981
2982 static inline int __skb_grow(struct sk_buff *skb, unsigned int len)
2983 {
2984         unsigned int diff = len - skb->len;
2985
2986         if (skb_tailroom(skb) < diff) {
2987                 int ret = pskb_expand_head(skb, 0, diff - skb_tailroom(skb),
2988                                            GFP_ATOMIC);
2989                 if (ret)
2990                         return ret;
2991         }
2992         __skb_set_length(skb, len);
2993         return 0;
2994 }
2995
2996 /**
2997  *      skb_orphan - orphan a buffer
2998  *      @skb: buffer to orphan
2999  *
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.
3003  */
3004 static inline void skb_orphan(struct sk_buff *skb)
3005 {
3006         if (skb->destructor) {
3007                 skb->destructor(skb);
3008                 skb->destructor = NULL;
3009                 skb->sk         = NULL;
3010         } else {
3011                 BUG_ON(skb->sk);
3012         }
3013 }
3014
3015 /**
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
3019  *
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.
3023  */
3024 static inline int skb_orphan_frags(struct sk_buff *skb, gfp_t gfp_mask)
3025 {
3026         if (likely(!skb_zcopy(skb)))
3027                 return 0;
3028         if (!skb_zcopy_is_nouarg(skb) &&
3029             skb_uarg(skb)->callback == msg_zerocopy_callback)
3030                 return 0;
3031         return skb_copy_ubufs(skb, gfp_mask);
3032 }
3033
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)
3036 {
3037         if (likely(!skb_zcopy(skb)))
3038                 return 0;
3039         return skb_copy_ubufs(skb, gfp_mask);
3040 }
3041
3042 /**
3043  *      __skb_queue_purge - empty a list
3044  *      @list: list to empty
3045  *
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.
3049  */
3050 static inline void __skb_queue_purge(struct sk_buff_head *list)
3051 {
3052         struct sk_buff *skb;
3053         while ((skb = __skb_dequeue(list)) != NULL)
3054                 kfree_skb(skb);
3055 }
3056 void skb_queue_purge(struct sk_buff_head *list);
3057
3058 unsigned int skb_rbtree_purge(struct rb_root *root);
3059
3060 void *__netdev_alloc_frag_align(unsigned int fragsz, unsigned int align_mask);
3061
3062 /**
3063  * netdev_alloc_frag - allocate a page fragment
3064  * @fragsz: fragment size
3065  *
3066  * Allocates a frag from a page for receive buffer.
3067  * Uses GFP_ATOMIC allocations.
3068  */
3069 static inline void *netdev_alloc_frag(unsigned int fragsz)
3070 {
3071         return __netdev_alloc_frag_align(fragsz, ~0u);
3072 }
3073
3074 static inline void *netdev_alloc_frag_align(unsigned int fragsz,
3075                                             unsigned int align)
3076 {
3077         WARN_ON_ONCE(!is_power_of_2(align));
3078         return __netdev_alloc_frag_align(fragsz, -align);
3079 }
3080
3081 struct sk_buff *__netdev_alloc_skb(struct net_device *dev, unsigned int length,
3082                                    gfp_t gfp_mask);
3083
3084 /**
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
3088  *
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.
3093  *
3094  *      %NULL is returned if there is no free memory. Although this function
3095  *      allocates memory it can be called from an interrupt.
3096  */
3097 static inline struct sk_buff *netdev_alloc_skb(struct net_device *dev,
3098                                                unsigned int length)
3099 {
3100         return __netdev_alloc_skb(dev, length, GFP_ATOMIC);
3101 }
3102
3103 /* legacy helper around __netdev_alloc_skb() */
3104 static inline struct sk_buff *__dev_alloc_skb(unsigned int length,
3105                                               gfp_t gfp_mask)
3106 {
3107         return __netdev_alloc_skb(NULL, length, gfp_mask);
3108 }
3109
3110 /* legacy helper around netdev_alloc_skb() */
3111 static inline struct sk_buff *dev_alloc_skb(unsigned int length)
3112 {
3113         return netdev_alloc_skb(NULL, length);
3114 }
3115
3116
3117 static inline struct sk_buff *__netdev_alloc_skb_ip_align(struct net_device *dev,
3118                 unsigned int length, gfp_t gfp)
3119 {
3120         struct sk_buff *skb = __netdev_alloc_skb(dev, length + NET_IP_ALIGN, gfp);
3121
3122         if (NET_IP_ALIGN && skb)
3123                 skb_reserve(skb, NET_IP_ALIGN);
3124         return skb;
3125 }
3126
3127 static inline struct sk_buff *netdev_alloc_skb_ip_align(struct net_device *dev,
3128                 unsigned int length)
3129 {
3130         return __netdev_alloc_skb_ip_align(dev, length, GFP_ATOMIC);
3131 }
3132
3133 static inline void skb_free_frag(void *addr)
3134 {
3135         page_frag_free(addr);
3136 }
3137
3138 void *__napi_alloc_frag_align(unsigned int fragsz, unsigned int align_mask);
3139
3140 static inline void *napi_alloc_frag(unsigned int fragsz)
3141 {
3142         return __napi_alloc_frag_align(fragsz, ~0u);
3143 }
3144
3145 static inline void *napi_alloc_frag_align(unsigned int fragsz,
3146                                           unsigned int align)
3147 {
3148         WARN_ON_ONCE(!is_power_of_2(align));
3149         return __napi_alloc_frag_align(fragsz, -align);
3150 }
3151
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)
3156 {
3157         return __napi_alloc_skb(napi, length, GFP_ATOMIC);
3158 }
3159 void napi_consume_skb(struct sk_buff *skb, int budget);
3160
3161 void napi_skb_free_stolen_head(struct sk_buff *skb);
3162 void __kfree_skb_defer(struct sk_buff *skb);
3163
3164 /**
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
3168  *
3169  * Allocate a new page.
3170  *
3171  * %NULL is returned if there is no free memory.
3172 */
3173 static inline struct page *__dev_alloc_pages(gfp_t gfp_mask,
3174                                              unsigned int order)
3175 {
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.
3183          */
3184         gfp_mask |= __GFP_COMP | __GFP_MEMALLOC;
3185
3186         return alloc_pages_node(NUMA_NO_NODE, gfp_mask, order);
3187 }
3188
3189 static inline struct page *dev_alloc_pages(unsigned int order)
3190 {
3191         return __dev_alloc_pages(GFP_ATOMIC | __GFP_NOWARN, order);
3192 }
3193
3194 /**
3195  * __dev_alloc_page - allocate a page for network Rx
3196  * @gfp_mask: allocation priority. Set __GFP_NOMEMALLOC if not for network Rx
3197  *
3198  * Allocate a new page.
3199  *
3200  * %NULL is returned if there is no free memory.
3201  */
3202 static inline struct page *__dev_alloc_page(gfp_t gfp_mask)
3203 {
3204         return __dev_alloc_pages(gfp_mask, 0);
3205 }
3206
3207 static inline struct page *dev_alloc_page(void)
3208 {
3209         return dev_alloc_pages(0);
3210 }
3211
3212 /**
3213  * dev_page_is_reusable - check whether a page can be reused for network Rx
3214  * @page: the page to test
3215  *
3216  * A page shouldn't be considered for reusing/recycling if it was allocated
3217  * under memory pressure or at a distant memory node.
3218  *
3219  * Returns false if this page should be returned to page allocator, true
3220  * otherwise.
3221  */
3222 static inline bool dev_page_is_reusable(const struct page *page)
3223 {
3224         return likely(page_to_nid(page) == numa_mem_id() &&
3225                       !page_is_pfmemalloc(page));
3226 }
3227
3228 /**
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
3232  */
3233 static inline void skb_propagate_pfmemalloc(const struct page *page,
3234                                             struct sk_buff *skb)
3235 {
3236         if (page_is_pfmemalloc(page))
3237                 skb->pfmemalloc = true;
3238 }
3239
3240 /**
3241  * skb_frag_off() - Returns the offset of a skb fragment
3242  * @frag: the paged fragment
3243  */
3244 static inline unsigned int skb_frag_off(const skb_frag_t *frag)
3245 {
3246         return frag->bv_offset;
3247 }
3248
3249 /**
3250  * skb_frag_off_add() - Increments the offset of a skb fragment by @delta
3251  * @frag: skb fragment
3252  * @delta: value to add
3253  */
3254 static inline void skb_frag_off_add(skb_frag_t *frag, int delta)
3255 {
3256         frag->bv_offset += delta;
3257 }
3258
3259 /**
3260  * skb_frag_off_set() - Sets the offset of a skb fragment
3261  * @frag: skb fragment
3262  * @offset: offset of fragment
3263  */
3264 static inline void skb_frag_off_set(skb_frag_t *frag, unsigned int offset)
3265 {
3266         frag->bv_offset = offset;
3267 }
3268
3269 /**
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
3273  */
3274 static inline void skb_frag_off_copy(skb_frag_t *fragto,
3275                                      const skb_frag_t *fragfrom)
3276 {
3277         fragto->bv_offset = fragfrom->bv_offset;
3278 }
3279
3280 /**
3281  * skb_frag_page - retrieve the page referred to by a paged fragment
3282  * @frag: the paged fragment
3283  *
3284  * Returns the &struct page associated with @frag.
3285  */
3286 static inline struct page *skb_frag_page(const skb_frag_t *frag)
3287 {
3288         return frag->bv_page;
3289 }
3290
3291 /**
3292  * __skb_frag_ref - take an addition reference on a paged fragment.
3293  * @frag: the paged fragment
3294  *
3295  * Takes an additional reference on the paged fragment @frag.
3296  */
3297 static inline void __skb_frag_ref(skb_frag_t *frag)
3298 {
3299         get_page(skb_frag_page(frag));
3300 }
3301
3302 /**
3303  * skb_frag_ref - take an addition reference on a paged fragment of an skb.
3304  * @skb: the buffer
3305  * @f: the fragment offset.
3306  *
3307  * Takes an additional reference on the @f'th paged fragment of @skb.
3308  */
3309 static inline void skb_frag_ref(struct sk_buff *skb, int f)
3310 {
3311         __skb_frag_ref(&skb_shinfo(skb)->frags[f]);
3312 }
3313
3314 /**
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
3318  *
3319  * Releases a reference on the paged fragment @frag
3320  * or recycles the page via the page_pool API.
3321  */
3322 static inline void __skb_frag_unref(skb_frag_t *frag, bool recycle)
3323 {
3324         struct page *page = skb_frag_page(frag);
3325
3326 #ifdef CONFIG_PAGE_POOL
3327         if (recycle && page_pool_return_skb_page(page))
3328                 return;
3329 #endif
3330         put_page(page);
3331 }
3332
3333 /**
3334  * skb_frag_unref - release a reference on a paged fragment of an skb.
3335  * @skb: the buffer
3336  * @f: the fragment offset
3337  *
3338  * Releases a reference on the @f'th paged fragment of @skb.
3339  */
3340 static inline void skb_frag_unref(struct sk_buff *skb, int f)
3341 {
3342         __skb_frag_unref(&skb_shinfo(skb)->frags[f], skb->pp_recycle);
3343 }
3344
3345 /**
3346  * skb_frag_address - gets the address of the data contained in a paged fragment
3347  * @frag: the paged fragment buffer
3348  *
3349  * Returns the address of the data within @frag. The page must already
3350  * be mapped.
3351  */
3352 static inline void *skb_frag_address(const skb_frag_t *frag)
3353 {
3354         return page_address(skb_frag_page(frag)) + skb_frag_off(frag);
3355 }
3356
3357 /**
3358  * skb_frag_address_safe - gets the address of the data contained in a paged fragment
3359  * @frag: the paged fragment buffer
3360  *
3361  * Returns the address of the data within @frag. Checks that the page
3362  * is mapped and returns %NULL otherwise.
3363  */
3364 static inline void *skb_frag_address_safe(const skb_frag_t *frag)
3365 {
3366         void *ptr = page_address(skb_frag_page(frag));
3367         if (unlikely(!ptr))
3368                 return NULL;
3369
3370         return ptr + skb_frag_off(frag);
3371 }
3372
3373 /**
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
3377  */
3378 static inline void skb_frag_page_copy(skb_frag_t *fragto,
3379                                       const skb_frag_t *fragfrom)
3380 {
3381         fragto->bv_page = fragfrom->bv_page;
3382 }
3383
3384 /**
3385  * __skb_frag_set_page - sets the page contained in a paged fragment
3386  * @frag: the paged fragment
3387  * @page: the page to set
3388  *
3389  * Sets the fragment @frag to contain @page.
3390  */
3391 static inline void __skb_frag_set_page(skb_frag_t *frag, struct page *page)
3392 {
3393         frag->bv_page = page;
3394 }
3395
3396 /**
3397  * skb_frag_set_page - sets the page contained in a paged fragment of an skb
3398  * @skb: the buffer
3399  * @f: the fragment offset
3400  * @page: the page to set
3401  *
3402  * Sets the @f'th fragment of @skb to contain @page.
3403  */
3404 static inline void skb_frag_set_page(struct sk_buff *skb, int f,
3405                                      struct page *page)
3406 {
3407         __skb_frag_set_page(&skb_shinfo(skb)->frags[f], page);
3408 }
3409
3410 bool skb_page_frag_refill(unsigned int sz, struct page_frag *pfrag, gfp_t prio);
3411
3412 /**
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_*``)
3420  *
3421  * Maps the page associated with @frag to @device.
3422  */
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)
3427 {
3428         return dma_map_page(dev, skb_frag_page(frag),
3429                             skb_frag_off(frag) + offset, size, dir);
3430 }
3431
3432 static inline struct sk_buff *pskb_copy(struct sk_buff *skb,
3433                                         gfp_t gfp_mask)
3434 {
3435         return __pskb_copy(skb, skb_headroom(skb), gfp_mask);
3436 }
3437
3438
3439 static inline struct sk_buff *pskb_copy_for_clone(struct sk_buff *skb,
3440                                                   gfp_t gfp_mask)
3441 {
3442         return __pskb_copy_fclone(skb, skb_headroom(skb), gfp_mask, true);
3443 }
3444
3445
3446 /**
3447  *      skb_clone_writable - is the header of a clone writable
3448  *      @skb: buffer to check
3449  *      @len: length up to which to write
3450  *
3451  *      Returns true if modifying the header part of the cloned buffer
3452  *      does not requires the data to be copied.
3453  */
3454 static inline int skb_clone_writable(const struct sk_buff *skb, unsigned int len)
3455 {
3456         return !skb_header_cloned(skb) &&
3457                skb_headroom(skb) + len <= skb->hdr_len;
3458 }
3459
3460 static inline int skb_try_make_writable(struct sk_buff *skb,
3461                                         unsigned int write_len)
3462 {
3463         return skb_cloned(skb) && !skb_clone_writable(skb, write_len) &&
3464                pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
3465 }
3466
3467 static inline int __skb_cow(struct sk_buff *skb, unsigned int headroom,
3468                             int cloned)
3469 {
3470         int delta = 0;
3471
3472         if (headroom > skb_headroom(skb))
3473                 delta = headroom - skb_headroom(skb);
3474
3475         if (delta || cloned)
3476                 return pskb_expand_head(skb, ALIGN(delta, NET_SKB_PAD), 0,
3477                                         GFP_ATOMIC);
3478         return 0;
3479 }
3480
3481 /**
3482  *      skb_cow - copy header of skb when it is required
3483  *      @skb: buffer to cow
3484  *      @headroom: needed headroom
3485  *
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.
3489  *
3490  *      The result is skb with writable area skb->head...skb->tail
3491  *      and at least @headroom of space at head.
3492  */
3493 static inline int skb_cow(struct sk_buff *skb, unsigned int headroom)
3494 {
3495         return __skb_cow(skb, headroom, skb_cloned(skb));
3496 }
3497
3498 /**
3499  *      skb_cow_head - skb_cow but only making the head writable
3500  *      @skb: buffer to cow
3501  *      @headroom: needed headroom
3502  *
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
3506  *      the data.
3507  */
3508 static inline int skb_cow_head(struct sk_buff *skb, unsigned int headroom)
3509 {
3510         return __skb_cow(skb, headroom, skb_header_cloned(skb));
3511 }
3512
3513 /**
3514  *      skb_padto       - pad an skbuff up to a minimal size
3515  *      @skb: buffer to pad
3516  *      @len: minimal length
3517  *
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.
3522  */
3523 static inline int skb_padto(struct sk_buff *skb, unsigned int len)
3524 {
3525         unsigned int size = skb->len;
3526         if (likely(size >= len))
3527                 return 0;
3528         return skb_pad(skb, len - size);
3529 }
3530
3531 /**
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
3536  *
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.
3541  */
3542 static inline int __must_check __skb_put_padto(struct sk_buff *skb,
3543                                                unsigned int len,
3544                                                bool free_on_error)
3545 {
3546         unsigned int size = skb->len;
3547
3548         if (unlikely(size < len)) {
3549                 len -= size;
3550                 if (__skb_pad(skb, len, free_on_error))
3551                         return -ENOMEM;
3552                 __skb_put(skb, len);
3553         }
3554         return 0;
3555 }
3556
3557 /**
3558  *      skb_put_padto - increase size and pad an skbuff up to a minimal size
3559  *      @skb: buffer to pad
3560  *      @len: minimal length
3561  *
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.
3566  */
3567 static inline int __must_check skb_put_padto(struct sk_buff *skb, unsigned int len)
3568 {
3569         return __skb_put_padto(skb, len, true);
3570 }
3571
3572 static inline int skb_add_data(struct sk_buff *skb,
3573                                struct iov_iter *from, int copy)
3574 {
3575         const int off = skb->len;
3576
3577         if (skb->ip_summed == CHECKSUM_NONE) {
3578                 __wsum csum = 0;
3579                 if (csum_and_copy_from_iter_full(skb_put(skb, copy), copy,
3580                                                  &csum, from)) {
3581                         skb->csum = csum_block_add(skb->csum, csum, off);
3582                         return 0;
3583                 }
3584         } else if (copy_from_iter_full(skb_put(skb, copy), copy, from))
3585                 return 0;
3586
3587         __skb_trim(skb, off);
3588         return -EFAULT;
3589 }
3590
3591 static inline bool skb_can_coalesce(struct sk_buff *skb, int i,
3592                                     const struct page *page, int off)
3593 {
3594         if (skb_zcopy(skb))
3595                 return false;
3596         if (i) {
3597                 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i - 1];
3598
3599                 return page == skb_frag_page(frag) &&
3600                        off == skb_frag_off(frag) + skb_frag_size(frag);
3601         }
3602         return false;
3603 }
3604
3605 static inline int __skb_linearize(struct sk_buff *skb)
3606 {
3607         return __pskb_pull_tail(skb, skb->data_len) ? 0 : -ENOMEM;
3608 }
3609
3610 /**
3611  *      skb_linearize - convert paged skb to linear one
3612  *      @skb: buffer to linarize
3613  *
3614  *      If there is no free memory -ENOMEM is returned, otherwise zero
3615  *      is returned and the old skb data released.
3616  */
3617 static inline int skb_linearize(struct sk_buff *skb)
3618 {
3619         return skb_is_nonlinear(skb) ? __skb_linearize(skb) : 0;
3620 }
3621
3622 /**
3623  * skb_has_shared_frag - can any frag be overwritten
3624  * @skb: buffer to test
3625  *
3626  * Return true if the skb has at least one frag that might be modified
3627  * by an external entity (as in vmsplice()/sendfile())
3628  */
3629 static inline bool skb_has_shared_frag(const struct sk_buff *skb)
3630 {
3631         return skb_is_nonlinear(skb) &&
3632                skb_shinfo(skb)->flags & SKBFL_SHARED_FRAG;
3633 }
3634
3635 /**
3636  *      skb_linearize_cow - make sure skb is linear and writable
3637  *      @skb: buffer to process
3638  *
3639  *      If there is no free memory -ENOMEM is returned, otherwise zero
3640  *      is returned and the old skb data released.
3641  */
3642 static inline int skb_linearize_cow(struct sk_buff *skb)
3643 {
3644         return skb_is_nonlinear(skb) || skb_cloned(skb) ?
3645                __skb_linearize(skb) : 0;
3646 }
3647
3648 static __always_inline void
3649 __skb_postpull_rcsum(struct sk_buff *skb, const void *start, unsigned int len,
3650                      unsigned int off)
3651 {
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;
3658 }
3659
3660 /**
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
3665  *
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.
3669  */
3670 static inline void skb_postpull_rcsum(struct sk_buff *skb,
3671                                       const void *start, unsigned int len)
3672 {
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;
3679 }
3680
3681 static __always_inline void
3682 __skb_postpush_rcsum(struct sk_buff *skb, const void *start, unsigned int len,
3683                      unsigned int off)
3684 {
3685         if (skb->ip_summed == CHECKSUM_COMPLETE)
3686                 skb->csum = csum_block_add(skb->csum,
3687                                            csum_partial(start, len, 0), off);
3688 }
3689
3690 /**
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
3695  *
3696  *      After doing a push on a received packet, you need to call this to
3697  *      update the CHECKSUM_COMPLETE checksum.
3698  */
3699 static inline void skb_postpush_rcsum(struct sk_buff *skb,
3700                                       const void *start, unsigned int len)
3701 {
3702         __skb_postpush_rcsum(skb, start, len, 0);
3703 }
3704
3705 void *skb_pull_rcsum(struct sk_buff *skb, unsigned int len);
3706
3707 /**
3708  *      skb_push_rcsum - push skb and update receive checksum
3709  *      @skb: buffer to update
3710  *      @len: length of data pulled
3711  *
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.
3717  */
3718 static inline void *skb_push_rcsum(struct sk_buff *skb, unsigned int len)
3719 {
3720         skb_push(skb, len);
3721         skb_postpush_rcsum(skb, skb->data, len);
3722         return skb->data;
3723 }
3724
3725 int pskb_trim_rcsum_slow(struct sk_buff *skb, unsigned int len);
3726 /**
3727  *      pskb_trim_rcsum - trim received skb and update checksum
3728  *      @skb: buffer to trim
3729  *      @len: new length
3730  *
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.
3734  */
3735
3736 static inline int pskb_trim_rcsum(struct sk_buff *skb, unsigned int len)
3737 {
3738         if (likely(len >= skb->len))
3739                 return 0;
3740         return pskb_trim_rcsum_slow(skb, len);
3741 }
3742
3743 static inline int __skb_trim_rcsum(struct sk_buff *skb, unsigned int len)
3744 {
3745         if (skb->ip_summed == CHECKSUM_COMPLETE)
3746                 skb->ip_summed = CHECKSUM_NONE;
3747         __skb_trim(skb, len);
3748         return 0;
3749 }
3750
3751 static inline int __skb_grow_rcsum(struct sk_buff *skb, unsigned int len)
3752 {
3753         if (skb->ip_summed == CHECKSUM_COMPLETE)
3754                 skb->ip_summed = CHECKSUM_NONE;
3755         return __skb_grow(skb, len);
3756 }
3757
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))
3763
3764 #define skb_queue_walk(queue, skb) \
3765                 for (skb = (queue)->next;                                       \
3766                      skb != (struct sk_buff *)(queue);                          \
3767                      skb = skb->next)
3768
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)
3773
3774 #define skb_queue_walk_from(queue, skb)                                         \
3775                 for (; skb != (struct sk_buff *)(queue);                        \
3776                      skb = skb->next)
3777
3778 #define skb_rbtree_walk(skb, root)                                              \
3779                 for (skb = skb_rb_first(root); skb != NULL;                     \
3780                      skb = skb_rb_next(skb))
3781
3782 #define skb_rbtree_walk_from(skb)                                               \
3783                 for (; skb != NULL;                                             \
3784                      skb = skb_rb_next(skb))
3785
3786 #define skb_rbtree_walk_from_safe(skb, tmp)                                     \
3787                 for (; tmp = skb ? skb_rb_next(skb) : NULL, (skb != NULL);      \
3788                      skb = tmp)
3789
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)
3794
3795 #define skb_queue_reverse_walk(queue, skb) \
3796                 for (skb = (queue)->prev;                                       \
3797                      skb != (struct sk_buff *)(queue);                          \
3798                      skb = skb->prev)
3799
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)
3804
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)
3809
3810 static inline bool skb_has_frag_list(const struct sk_buff *skb)
3811 {
3812         return skb_shinfo(skb)->frag_list != NULL;
3813 }
3814
3815 static inline void skb_frag_list_init(struct sk_buff *skb)
3816 {
3817         skb_shinfo(skb)->frag_list = NULL;
3818 }
3819
3820 #define skb_walk_frags(skb, iter)       \
3821         for (iter = skb_shinfo(skb)->frag_list; iter; iter = iter->next)
3822
3823
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,
3829                                           unsigned int flags,
3830                                           int *off, int *err,
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,
3840                                   int *err);
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)
3847 {
3848         return skb_copy_datagram_iter(from, offset, &msg->msg_iter, size);
3849 }
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)
3862 {
3863         __skb_free_datagram_locked(sk, skb, 0);
3864 }
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,
3869                               int len);
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,
3874                          int len);
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,
3879                  int len, int hlen);
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,
3899                  bool ethernet);
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,
3903                              gfp_t gfp);
3904
3905 static inline int memcpy_from_msg(void *data, struct msghdr *msg, int len)
3906 {
3907         return copy_from_iter_full(data, len, &msg->msg_iter) ? 0 : -EFAULT;
3908 }
3909
3910 static inline int memcpy_to_msg(struct msghdr *msg, void *data, int len)
3911 {
3912         return copy_to_iter(data, len, &msg->msg_iter) == len ? 0 : -EFAULT;
3913 }
3914
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);
3918 };
3919
3920 extern const struct skb_checksum_ops *crc32c_csum_stub __read_mostly;
3921
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,
3925                     __wsum csum);
3926
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)
3930 {
3931         if (likely(hlen - offset >= len))
3932                 return (void *)data + offset;
3933
3934         if (!skb || unlikely(skb_copy_bits(skb, offset, buffer, len) < 0))
3935                 return NULL;
3936
3937         return buffer;
3938 }
3939
3940 static inline void * __must_check
3941 skb_header_pointer(const struct sk_buff *skb, int offset, int len, void *buffer)
3942 {
3943         return __skb_header_pointer(skb, offset, len, skb->data,
3944                                     skb_headlen(skb), buffer);
3945 }
3946
3947 /**
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
3952  *
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.
3956  */
3957 static inline bool skb_needs_linearize(struct sk_buff *skb,
3958                                        netdev_features_t features)
3959 {
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)));
3963 }
3964
3965 static inline void skb_copy_from_linear_data(const struct sk_buff *skb,
3966                                              void *to,
3967                                              const unsigned int len)
3968 {
3969         memcpy(to, skb->data, len);
3970 }
3971
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)
3975 {
3976         memcpy(to, skb->data + offset, len);
3977 }
3978
3979 static inline void skb_copy_to_linear_data(struct sk_buff *skb,
3980                                            const void *from,
3981                                            const unsigned int len)
3982 {
3983         memcpy(skb->data, from, len);
3984 }
3985
3986 static inline void skb_copy_to_linear_data_offset(struct sk_buff *skb,
3987                                                   const int offset,
3988                                                   const void *from,
3989                                                   const unsigned int len)
3990 {
3991         memcpy(skb->data + offset, from, len);
3992 }
3993
3994 void skb_init(void);
3995
3996 static inline ktime_t skb_get_ktime(const struct sk_buff *skb)
3997 {
3998         return skb->tstamp;
3999 }
4000
4001 /**
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
4005  *
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
4008  *      it in stamp.
4009  */
4010 static inline void skb_get_timestamp(const struct sk_buff *skb,
4011                                      struct __kernel_old_timeval *stamp)
4012 {
4013         *stamp = ns_to_kernel_old_timeval(skb->tstamp);
4014 }
4015
4016 static inline void skb_get_new_timestamp(const struct sk_buff *skb,
4017                                          struct __kernel_sock_timeval *stamp)
4018 {
4019         struct timespec64 ts = ktime_to_timespec64(skb->tstamp);
4020
4021         stamp->tv_sec = ts.tv_sec;
4022         stamp->tv_usec = ts.tv_nsec / 1000;
4023 }
4024
4025 static inline void skb_get_timestampns(const struct sk_buff *skb,
4026                                        struct __kernel_old_timespec *stamp)
4027 {
4028         struct timespec64 ts = ktime_to_timespec64(skb->tstamp);
4029
4030         stamp->tv_sec = ts.tv_sec;
4031         stamp->tv_nsec = ts.tv_nsec;
4032 }
4033
4034 static inline void skb_get_new_timestampns(const struct sk_buff *skb,
4035                                            struct __kernel_timespec *stamp)
4036 {
4037         struct timespec64 ts = ktime_to_timespec64(skb->tstamp);
4038
4039         stamp->tv_sec = ts.tv_sec;
4040         stamp->tv_nsec = ts.tv_nsec;
4041 }
4042
4043 static inline void __net_timestamp(struct sk_buff *skb)
4044 {
4045         skb->tstamp = ktime_get_real();
4046         skb->mono_delivery_time = 0;
4047 }
4048
4049 static inline ktime_t net_timedelta(ktime_t t)
4050 {
4051         return ktime_sub(ktime_get_real(), t);
4052 }
4053
4054 static inline void skb_set_delivery_time(struct sk_buff *skb, ktime_t kt,
4055                                          bool mono)
4056 {
4057         skb->tstamp = kt;
4058         skb->mono_delivery_time = kt && mono;
4059 }
4060
4061 DECLARE_STATIC_KEY_FALSE(netstamp_needed_key);
4062
4063 /* It is used in the ingress path to clear the delivery_time.
4064  * If needed, set the skb->tstamp to the (rcv) timestamp.
4065  */
4066 static inline void skb_clear_delivery_time(struct sk_buff *skb)
4067 {
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();
4072                 else
4073                         skb->tstamp = 0;
4074         }
4075 }
4076
4077 static inline void skb_clear_tstamp(struct sk_buff *skb)
4078 {
4079         if (skb->mono_delivery_time)
4080                 return;
4081
4082         skb->tstamp = 0;
4083 }
4084
4085 static inline ktime_t skb_tstamp(const struct sk_buff *skb)
4086 {
4087         if (skb->mono_delivery_time)
4088                 return 0;
4089
4090         return skb->tstamp;
4091 }
4092
4093 static inline ktime_t skb_tstamp_cond(const struct sk_buff *skb, bool cond)
4094 {
4095         if (!skb->mono_delivery_time && skb->tstamp)
4096                 return skb->tstamp;
4097
4098         if (static_branch_unlikely(&netstamp_needed_key) || cond)
4099                 return ktime_get_real();
4100
4101         return 0;
4102 }
4103
4104 static inline u8 skb_metadata_len(const struct sk_buff *skb)
4105 {
4106         return skb_shinfo(skb)->meta_len;
4107 }
4108
4109 static inline void *skb_metadata_end(const struct sk_buff *skb)
4110 {
4111         return skb_mac_header(skb);
4112 }
4113
4114 static inline bool __skb_metadata_differs(const struct sk_buff *skb_a,
4115                                           const struct sk_buff *skb_b,
4116                                           u8 meta_len)
4117 {
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
4122         u64 diffs = 0;
4123
4124         switch (meta_len) {
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);
4128                 fallthrough;
4129         case 24: diffs |= __it_diff(a, b, 64);
4130                 fallthrough;
4131         case 16: diffs |= __it_diff(a, b, 64);
4132                 fallthrough;
4133         case  8: diffs |= __it_diff(a, b, 64);
4134                 break;
4135         case 28: diffs |= __it_diff(a, b, 64);
4136                 fallthrough;
4137         case 20: diffs |= __it_diff(a, b, 64);
4138                 fallthrough;
4139         case 12: diffs |= __it_diff(a, b, 64);
4140                 fallthrough;
4141         case  4: diffs |= __it_diff(a, b, 32);
4142                 break;
4143         }
4144         return diffs;
4145 #else
4146         return memcmp(a - meta_len, b - meta_len, meta_len);
4147 #endif
4148 }
4149
4150 static inline bool skb_metadata_differs(const struct sk_buff *skb_a,
4151                                         const struct sk_buff *skb_b)
4152 {
4153         u8 len_a = skb_metadata_len(skb_a);
4154         u8 len_b = skb_metadata_len(skb_b);
4155
4156         if (!(len_a | len_b))
4157                 return false;
4158
4159         return len_a != len_b ?
4160                true : __skb_metadata_differs(skb_a, skb_b, len_a);
4161 }
4162
4163 static inline void skb_metadata_set(struct sk_buff *skb, u8 meta_len)
4164 {
4165         skb_shinfo(skb)->meta_len = meta_len;
4166 }
4167
4168 static inline void skb_metadata_clear(struct sk_buff *skb)
4169 {
4170         skb_metadata_set(skb, 0);
4171 }
4172
4173 struct sk_buff *skb_clone_sk(struct sk_buff *skb);
4174
4175 #ifdef CONFIG_NETWORK_PHY_TIMESTAMPING
4176
4177 void skb_clone_tx_timestamp(struct sk_buff *skb);
4178 bool skb_defer_rx_timestamp(struct sk_buff *skb);
4179
4180 #else /* CONFIG_NETWORK_PHY_TIMESTAMPING */
4181
4182 static inline void skb_clone_tx_timestamp(struct sk_buff *skb)
4183 {
4184 }
4185
4186 static inline bool skb_defer_rx_timestamp(struct sk_buff *skb)
4187 {
4188         return false;
4189 }
4190
4191 #endif /* !CONFIG_NETWORK_PHY_TIMESTAMPING */
4192
4193 /**
4194  * skb_complete_tx_timestamp() - deliver cloned skb with tx timestamps
4195  *
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
4199  * timestamp.
4200  *
4201  * @skb: clone of the original outgoing packet
4202  * @hwtstamps: hardware time stamps
4203  *
4204  */
4205 void skb_complete_tx_timestamp(struct sk_buff *skb,
4206                                struct skb_shared_hwtstamps *hwtstamps);
4207
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);
4211
4212 /**
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
4216  *
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.
4222  */
4223 void skb_tstamp_tx(struct sk_buff *orig_skb,
4224                    struct skb_shared_hwtstamps *hwtstamps);
4225
4226 /**
4227  * skb_tx_timestamp() - Driver hook for transmit timestamping
4228  *
4229  * Ethernet MAC Drivers should call this function in their hard_xmit()
4230  * function immediately before giving the sk_buff to the MAC hardware.
4231  *
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.
4235  *
4236  * @skb: A socket buffer.
4237  */
4238 static inline void skb_tx_timestamp(struct sk_buff *skb)
4239 {
4240         skb_clone_tx_timestamp(skb);
4241         if (skb_shinfo(skb)->tx_flags & SKBTX_SW_TSTAMP)
4242                 skb_tstamp_tx(skb, NULL);
4243 }
4244
4245 /**
4246  * skb_complete_wifi_ack - deliver skb with wifi status
4247  *
4248  * @skb: the original outgoing packet
4249  * @acked: ack status
4250  *
4251  */
4252 void skb_complete_wifi_ack(struct sk_buff *skb, bool acked);
4253
4254 __sum16 __skb_checksum_complete_head(struct sk_buff *skb, int len);
4255 __sum16 __skb_checksum_complete(struct sk_buff *skb);
4256
4257 static inline int skb_csum_unnecessary(const struct sk_buff *skb)
4258 {
4259         return ((skb->ip_summed == CHECKSUM_UNNECESSARY) ||
4260                 skb->csum_valid ||
4261                 (skb->ip_summed == CHECKSUM_PARTIAL &&
4262                  skb_checksum_start_offset(skb) >= 0));
4263 }
4264
4265 /**
4266  *      skb_checksum_complete - Calculate checksum of an entire packet
4267  *      @skb: packet to process
4268  *
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
4272  *      checksum.
4273  *
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.
4280  */
4281 static inline __sum16 skb_checksum_complete(struct sk_buff *skb)
4282 {
4283         return skb_csum_unnecessary(skb) ?
4284                0 : __skb_checksum_complete(skb);
4285 }
4286
4287 static inline void __skb_decr_checksum_unnecessary(struct sk_buff *skb)
4288 {
4289         if (skb->ip_summed == CHECKSUM_UNNECESSARY) {
4290                 if (skb->csum_level == 0)
4291                         skb->ip_summed = CHECKSUM_NONE;
4292                 else
4293                         skb->csum_level--;
4294         }
4295 }
4296
4297 static inline void __skb_incr_checksum_unnecessary(struct sk_buff *skb)
4298 {
4299         if (skb->ip_summed == CHECKSUM_UNNECESSARY) {
4300                 if (skb->csum_level < SKB_MAX_CSUM_LEVEL)
4301                         skb->csum_level++;
4302         } else if (skb->ip_summed == CHECKSUM_NONE) {
4303                 skb->ip_summed = CHECKSUM_UNNECESSARY;
4304                 skb->csum_level = 0;
4305         }
4306 }
4307
4308 static inline void __skb_reset_checksum_unnecessary(struct sk_buff *skb)
4309 {
4310         if (skb->ip_summed == CHECKSUM_UNNECESSARY) {
4311                 skb->ip_summed = CHECKSUM_NONE;
4312                 skb->csum_level = 0;
4313         }
4314 }
4315
4316 /* Check if we need to perform checksum complete validation.
4317  *
4318  * Returns true if checksum complete is needed, false otherwise
4319  * (either checksum is unnecessary or zero checksum is allowed).
4320  */
4321 static inline bool __skb_checksum_validate_needed(struct sk_buff *skb,
4322                                                   bool zero_okay,
4323                                                   __sum16 check)
4324 {
4325         if (skb_csum_unnecessary(skb) || (zero_okay && !check)) {
4326                 skb->csum_valid = 1;
4327                 __skb_decr_checksum_unnecessary(skb);
4328                 return false;
4329         }
4330
4331         return true;
4332 }
4333
4334 /* For small packets <= CHECKSUM_BREAK perform checksum complete directly
4335  * in checksum_init.
4336  */
4337 #define CHECKSUM_BREAK 76
4338
4339 /* Unset checksum-complete
4340  *
4341  * Unset checksum complete can be done when packet is being modified
4342  * (uncompressed for instance) and checksum-complete value is
4343  * invalidated.
4344  */
4345 static inline void skb_checksum_complete_unset(struct sk_buff *skb)
4346 {
4347         if (skb->ip_summed == CHECKSUM_COMPLETE)
4348                 skb->ip_summed = CHECKSUM_NONE;
4349 }
4350
4351 /* Validate (init) checksum based on checksum complete.
4352  *
4353  * Return values:
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
4358  *
4359  */
4360 static inline __sum16 __skb_checksum_validate_complete(struct sk_buff *skb,
4361                                                        bool complete,
4362                                                        __wsum psum)
4363 {
4364         if (skb->ip_summed == CHECKSUM_COMPLETE) {
4365                 if (!csum_fold(csum_add(psum, skb->csum))) {
4366                         skb->csum_valid = 1;
4367                         return 0;
4368                 }
4369         }
4370
4371         skb->csum = psum;
4372
4373         if (complete || skb->len <= CHECKSUM_BREAK) {
4374                 __sum16 csum;
4375
4376                 csum = __skb_checksum_complete(skb);
4377                 skb->csum_valid = !csum;
4378                 return csum;
4379         }
4380
4381         return 0;
4382 }
4383
4384 static inline __wsum null_compute_pseudo(struct sk_buff *skb, int proto)
4385 {
4386         return 0;
4387 }
4388
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
4393  * pseudo header.
4394  *
4395  * Return values:
4396  *   0: checksum is validated or try to in skb_checksum_complete
4397  *   non-zero: value of invalid checksum
4398  */
4399 #define __skb_checksum_validate(skb, proto, complete,                   \
4400                                 zero_okay, check, compute_pseudo)       \
4401 ({                                                                      \
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));  \
4407         __ret;                                                          \
4408 })
4409
4410 #define skb_checksum_init(skb, proto, compute_pseudo)                   \
4411         __skb_checksum_validate(skb, proto, false, false, 0, compute_pseudo)
4412
4413 #define skb_checksum_init_zero_check(skb, proto, check, compute_pseudo) \
4414         __skb_checksum_validate(skb, proto, false, true, check, compute_pseudo)
4415
4416 #define skb_checksum_validate(skb, proto, compute_pseudo)               \
4417         __skb_checksum_validate(skb, proto, true, false, 0, compute_pseudo)
4418
4419 #define skb_checksum_validate_zero_check(skb, proto, check,             \
4420                                          compute_pseudo)                \
4421         __skb_checksum_validate(skb, proto, true, true, check, compute_pseudo)
4422
4423 #define skb_checksum_simple_validate(skb)                               \
4424         __skb_checksum_validate(skb, 0, true, false, 0, null_compute_pseudo)
4425
4426 static inline bool __skb_checksum_convert_check(struct sk_buff *skb)
4427 {
4428         return (skb->ip_summed == CHECKSUM_NONE && skb->csum_valid);
4429 }
4430
4431 static inline void __skb_checksum_convert(struct sk_buff *skb, __wsum pseudo)
4432 {
4433         skb->csum = ~pseudo;
4434         skb->ip_summed = CHECKSUM_COMPLETE;
4435 }
4436
4437 #define skb_checksum_try_convert(skb, proto, compute_pseudo)    \
4438 do {                                                                    \
4439         if (__skb_checksum_convert_check(skb))                          \
4440                 __skb_checksum_convert(skb, compute_pseudo(skb, proto)); \
4441 } while (0)
4442
4443 static inline void skb_remcsum_adjust_partial(struct sk_buff *skb, void *ptr,
4444                                               u16 start, u16 offset)
4445 {
4446         skb->ip_summed = CHECKSUM_PARTIAL;
4447         skb->csum_start = ((unsigned char *)ptr + start) - skb->head;
4448         skb->csum_offset = offset - start;
4449 }
4450
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.
4455  */
4456 static inline void skb_remcsum_process(struct sk_buff *skb, void *ptr,
4457                                        int start, int offset, bool nopartial)
4458 {
4459         __wsum delta;
4460
4461         if (!nopartial) {
4462                 skb_remcsum_adjust_partial(skb, ptr, start, offset);
4463                 return;
4464         }
4465
4466         if (unlikely(skb->ip_summed != CHECKSUM_COMPLETE)) {
4467                 __skb_checksum_complete(skb);
4468                 skb_postpull_rcsum(skb, skb->data, ptr - (void *)skb->data);
4469         }
4470
4471         delta = remcsum_adjust(ptr, skb->csum, start, offset);
4472
4473         /* Adjust skb->csum since we changed the packet */
4474         skb->csum = csum_add(skb->csum, delta);
4475 }
4476
4477 static inline struct nf_conntrack *skb_nfct(const struct sk_buff *skb)
4478 {
4479 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
4480         return (void *)(skb->_nfct & NFCT_PTRMASK);
4481 #else
4482         return NULL;
4483 #endif
4484 }
4485
4486 static inline unsigned long skb_get_nfct(const struct sk_buff *skb)
4487 {
4488 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
4489         return skb->_nfct;
4490 #else
4491         return 0UL;
4492 #endif
4493 }
4494
4495 static inline void skb_set_nfct(struct sk_buff *skb, unsigned long nfct)
4496 {
4497 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
4498         skb->slow_gro |= !!nfct;
4499         skb->_nfct = nfct;
4500 #endif
4501 }
4502
4503 #ifdef CONFIG_SKB_EXTENSIONS
4504 enum skb_ext_id {
4505 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
4506         SKB_EXT_BRIDGE_NF,
4507 #endif
4508 #ifdef CONFIG_XFRM
4509         SKB_EXT_SEC_PATH,
4510 #endif
4511 #if IS_ENABLED(CONFIG_NET_TC_SKB_EXT)
4512         TC_SKB_EXT,
4513 #endif
4514 #if IS_ENABLED(CONFIG_MPTCP)
4515         SKB_EXT_MPTCP,
4516 #endif
4517 #if IS_ENABLED(CONFIG_MCTP_FLOWS)
4518         SKB_EXT_MCTP,
4519 #endif
4520         SKB_EXT_NUM, /* must be last */
4521 };
4522
4523 /**
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
4529  *
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.
4532  */
4533 struct skb_ext {
4534         refcount_t refcnt;
4535         u8 offset[SKB_EXT_NUM]; /* in chunks of 8 bytes */
4536         u8 chunks;              /* same */
4537         char data[] __aligned(8);
4538 };
4539
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);
4546
4547 static inline void skb_ext_put(struct sk_buff *skb)
4548 {
4549         if (skb->active_extensions)
4550                 __skb_ext_put(skb->extensions);
4551 }
4552
4553 static inline void __skb_ext_copy(struct sk_buff *dst,
4554                                   const struct sk_buff *src)
4555 {
4556         dst->active_extensions = src->active_extensions;
4557
4558         if (src->active_extensions) {
4559                 struct skb_ext *ext = src->extensions;
4560
4561                 refcount_inc(&ext->refcnt);
4562                 dst->extensions = ext;
4563         }
4564 }
4565
4566 static inline void skb_ext_copy(struct sk_buff *dst, const struct sk_buff *src)
4567 {
4568         skb_ext_put(dst);
4569         __skb_ext_copy(dst, src);
4570 }
4571
4572 static inline bool __skb_ext_exist(const struct skb_ext *ext, enum skb_ext_id i)
4573 {
4574         return !!ext->offset[i];
4575 }
4576
4577 static inline bool skb_ext_exist(const struct sk_buff *skb, enum skb_ext_id id)
4578 {
4579         return skb->active_extensions & (1 << id);
4580 }
4581
4582 static inline void skb_ext_del(struct sk_buff *skb, enum skb_ext_id id)
4583 {
4584         if (skb_ext_exist(skb, id))
4585                 __skb_ext_del(skb, id);
4586 }
4587
4588 static inline void *skb_ext_find(const struct sk_buff *skb, enum skb_ext_id id)
4589 {
4590         if (skb_ext_exist(skb, id)) {
4591                 struct skb_ext *ext = skb->extensions;
4592
4593                 return (void *)ext + (ext->offset[id] << 3);
4594         }
4595
4596         return NULL;
4597 }
4598
4599 static inline void skb_ext_reset(struct sk_buff *skb)
4600 {
4601         if (unlikely(skb->active_extensions)) {
4602                 __skb_ext_put(skb->extensions);
4603                 skb->active_extensions = 0;
4604         }
4605 }
4606
4607 static inline bool skb_has_extensions(struct sk_buff *skb)
4608 {
4609         return unlikely(skb->active_extensions);
4610 }
4611 #else
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 */
4619
4620 static inline void nf_reset_ct(struct sk_buff *skb)
4621 {
4622 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
4623         nf_conntrack_put(skb_nfct(skb));
4624         skb->_nfct = 0;
4625 #endif
4626 }
4627
4628 static inline void nf_reset_trace(struct sk_buff *skb)
4629 {
4630 #if IS_ENABLED(CONFIG_NETFILTER_XT_TARGET_TRACE) || defined(CONFIG_NF_TABLES)
4631         skb->nf_trace = 0;
4632 #endif
4633 }
4634
4635 static inline void ipvs_reset(struct sk_buff *skb)
4636 {
4637 #if IS_ENABLED(CONFIG_IP_VS)
4638         skb->ipvs_property = 0;
4639 #endif
4640 }
4641
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,
4644                              bool copy)
4645 {
4646 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
4647         dst->_nfct = src->_nfct;
4648         nf_conntrack_get(skb_nfct(src));
4649 #endif
4650 #if IS_ENABLED(CONFIG_NETFILTER_XT_TARGET_TRACE) || defined(CONFIG_NF_TABLES)
4651         if (copy)
4652                 dst->nf_trace = src->nf_trace;
4653 #endif
4654 }
4655
4656 static inline void nf_copy(struct sk_buff *dst, const struct sk_buff *src)
4657 {
4658 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
4659         nf_conntrack_put(skb_nfct(dst));
4660 #endif
4661         dst->slow_gro = src->slow_gro;
4662         __nf_copy(dst, src, true);
4663 }
4664
4665 #ifdef CONFIG_NETWORK_SECMARK
4666 static inline void skb_copy_secmark(struct sk_buff *to, const struct sk_buff *from)
4667 {
4668         to->secmark = from->secmark;
4669 }
4670
4671 static inline void skb_init_secmark(struct sk_buff *skb)
4672 {
4673         skb->secmark = 0;
4674 }
4675 #else
4676 static inline void skb_copy_secmark(struct sk_buff *to, const struct sk_buff *from)
4677 { }
4678
4679 static inline void skb_init_secmark(struct sk_buff *skb)
4680 { }
4681 #endif
4682
4683 static inline int secpath_exists(const struct sk_buff *skb)
4684 {
4685 #ifdef CONFIG_XFRM
4686         return skb_ext_exist(skb, SKB_EXT_SEC_PATH);
4687 #else
4688         return 0;
4689 #endif
4690 }
4691
4692 static inline bool skb_irq_freeable(const struct sk_buff *skb)
4693 {
4694         return !skb->destructor &&
4695                 !secpath_exists(skb) &&
4696                 !skb_nfct(skb) &&
4697                 !skb->_skb_refdst &&
4698                 !skb_has_frag_list(skb);
4699 }
4700
4701 static inline void skb_set_queue_mapping(struct sk_buff *skb, u16 queue_mapping)
4702 {
4703         skb->queue_mapping = queue_mapping;
4704 }
4705
4706 static inline u16 skb_get_queue_mapping(const struct sk_buff *skb)
4707 {
4708         return skb->queue_mapping;
4709 }
4710
4711 static inline void skb_copy_queue_mapping(struct sk_buff *to, const struct sk_buff *from)
4712 {
4713         to->queue_mapping = from->queue_mapping;
4714 }
4715
4716 static inline void skb_record_rx_queue(struct sk_buff *skb, u16 rx_queue)
4717 {
4718         skb->queue_mapping = rx_queue + 1;
4719 }
4720
4721 static inline u16 skb_get_rx_queue(const struct sk_buff *skb)
4722 {
4723         return skb->queue_mapping - 1;
4724 }
4725
4726 static inline bool skb_rx_queue_recorded(const struct sk_buff *skb)
4727 {
4728         return skb->queue_mapping != 0;
4729 }
4730
4731 static inline void skb_set_dst_pending_confirm(struct sk_buff *skb, u32 val)
4732 {
4733         skb->dst_pending_confirm = val;
4734 }
4735
4736 static inline bool skb_get_dst_pending_confirm(const struct sk_buff *skb)
4737 {
4738         return skb->dst_pending_confirm != 0;
4739 }
4740
4741 static inline struct sec_path *skb_sec_path(const struct sk_buff *skb)
4742 {
4743 #ifdef CONFIG_XFRM
4744         return skb_ext_find(skb, SKB_EXT_SEC_PATH);
4745 #else
4746         return NULL;
4747 #endif
4748 }
4749
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.
4755  */
4756 struct skb_gso_cb {
4757         union {
4758                 int     mac_offset;
4759                 int     data_offset;
4760         };
4761         int     encap_level;
4762         __wsum  csum;
4763         __u16   csum_start;
4764 };
4765 #define SKB_GSO_CB_OFFSET       32
4766 #define SKB_GSO_CB(skb) ((struct skb_gso_cb *)((skb)->cb + SKB_GSO_CB_OFFSET))
4767
4768 static inline int skb_tnl_header_len(const struct sk_buff *inner_skb)
4769 {
4770         return (skb_mac_header(inner_skb) - inner_skb->head) -
4771                 SKB_GSO_CB(inner_skb)->mac_offset;
4772 }
4773
4774 static inline int gso_pskb_expand_head(struct sk_buff *skb, int extra)
4775 {
4776         int new_headroom, headroom;
4777         int ret;
4778
4779         headroom = skb_headroom(skb);
4780         ret = pskb_expand_head(skb, extra, 0, GFP_ATOMIC);
4781         if (ret)
4782                 return ret;
4783
4784         new_headroom = skb_headroom(skb);
4785         SKB_GSO_CB(skb)->mac_offset += (new_headroom - headroom);
4786         return 0;
4787 }
4788
4789 static inline void gso_reset_checksum(struct sk_buff *skb, __wsum res)
4790 {
4791         /* Do not update partial checksums if remote checksum is enabled. */
4792         if (skb->remcsum_offload)
4793                 return;
4794
4795         SKB_GSO_CB(skb)->csum = res;
4796         SKB_GSO_CB(skb)->csum_start = skb_checksum_start(skb) - skb->head;
4797 }
4798
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
4805  * header.
4806  */
4807 static inline __sum16 gso_make_checksum(struct sk_buff *skb, __wsum res)
4808 {
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;
4812
4813         SKB_GSO_CB(skb)->csum = res;
4814         SKB_GSO_CB(skb)->csum_start = csum_start - skb->head;
4815
4816         return csum_fold(csum_partial(csum_start, plen, partial));
4817 }
4818
4819 static inline bool skb_is_gso(const struct sk_buff *skb)
4820 {
4821         return skb_shinfo(skb)->gso_size;
4822 }
4823
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)
4826 {
4827         return skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6;
4828 }
4829
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)
4832 {
4833         return skb_shinfo(skb)->gso_type & SKB_GSO_SCTP;
4834 }
4835
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)
4838 {
4839         return skb_shinfo(skb)->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6);
4840 }
4841
4842 static inline void skb_gso_reset(struct sk_buff *skb)
4843 {
4844         skb_shinfo(skb)->gso_size = 0;
4845         skb_shinfo(skb)->gso_segs = 0;
4846         skb_shinfo(skb)->gso_type = 0;
4847 }
4848
4849 static inline void skb_increase_gso_size(struct skb_shared_info *shinfo,
4850                                          u16 increment)
4851 {
4852         if (WARN_ON_ONCE(shinfo->gso_size == GSO_BY_FRAGS))
4853                 return;
4854         shinfo->gso_size += increment;
4855 }
4856
4857 static inline void skb_decrease_gso_size(struct skb_shared_info *shinfo,
4858                                          u16 decrement)
4859 {
4860         if (WARN_ON_ONCE(shinfo->gso_size == GSO_BY_FRAGS))
4861                 return;
4862         shinfo->gso_size -= decrement;
4863 }
4864
4865 void __skb_warn_lro_forwarding(const struct sk_buff *skb);
4866
4867 static inline bool skb_warn_if_lro(const struct sk_buff *skb)
4868 {
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);
4872
4873         if (skb_is_nonlinear(skb) && shinfo->gso_size != 0 &&
4874             unlikely(shinfo->gso_type == 0)) {
4875                 __skb_warn_lro_forwarding(skb);
4876                 return true;
4877         }
4878         return false;
4879 }
4880
4881 static inline void skb_forward_csum(struct sk_buff *skb)
4882 {
4883         /* Unfortunately we don't support this one.  Any brave souls? */
4884         if (skb->ip_summed == CHECKSUM_COMPLETE)
4885                 skb->ip_summed = CHECKSUM_NONE;
4886 }
4887
4888 /**
4889  * skb_checksum_none_assert - make sure skb ip_summed is CHECKSUM_NONE
4890  * @skb: skb to check
4891  *
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.
4895  */
4896 static inline void skb_checksum_none_assert(const struct sk_buff *skb)
4897 {
4898 #ifdef DEBUG
4899         BUG_ON(skb->ip_summed != CHECKSUM_NONE);
4900 #endif
4901 }
4902
4903 bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off);
4904
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));
4909
4910 /**
4911  * skb_head_is_locked - Determine if the skb->head is locked down
4912  * @skb: skb to check
4913  *
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.
4918  */
4919 static inline bool skb_head_is_locked(const struct sk_buff *skb)
4920 {
4921         return !skb->head_frag || skb_cloned(skb);
4922 }
4923
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.
4932  */
4933 static inline __wsum lco_csum(struct sk_buff *skb)
4934 {
4935         unsigned char *csum_start = skb_checksum_start(skb);
4936         unsigned char *l4_hdr = skb_transport_header(skb);
4937         __wsum partial;
4938
4939         /* Start with complement of inner checksum adjustment */
4940         partial = ~csum_unfold(*(__force __sum16 *)(csum_start +
4941                                                     skb->csum_offset));
4942
4943         /* Add in checksum of our headers (incl. outer checksum
4944          * adjustment filled in by caller) and return result.
4945          */
4946         return csum_partial(l4_hdr, csum_start - l4_hdr, partial);
4947 }
4948
4949 static inline bool skb_is_redirected(const struct sk_buff *skb)
4950 {
4951         return skb->redirected;
4952 }
4953
4954 static inline void skb_set_redirected(struct sk_buff *skb, bool from_ingress)
4955 {
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);
4961 #endif
4962 }
4963
4964 static inline void skb_reset_redirect(struct sk_buff *skb)
4965 {
4966         skb->redirected = 0;
4967 }
4968
4969 static inline bool skb_csum_is_sctp(struct sk_buff *skb)
4970 {
4971         return skb->csum_not_inet;
4972 }
4973
4974 static inline void skb_set_kcov_handle(struct sk_buff *skb,
4975                                        const u64 kcov_handle)
4976 {
4977 #ifdef CONFIG_KCOV
4978         skb->kcov_handle = kcov_handle;
4979 #endif
4980 }
4981
4982 static inline u64 skb_get_kcov_handle(struct sk_buff *skb)
4983 {
4984 #ifdef CONFIG_KCOV
4985         return skb->kcov_handle;
4986 #else
4987         return 0;
4988 #endif
4989 }
4990
4991 #ifdef CONFIG_PAGE_POOL
4992 static inline void skb_mark_for_recycle(struct sk_buff *skb)
4993 {
4994         skb->pp_recycle = 1;
4995 }
4996 #endif
4997
4998 static inline bool skb_pp_recycle(struct sk_buff *skb, void *data)
4999 {
5000         if (!IS_ENABLED(CONFIG_PAGE_POOL) || !skb->pp_recycle)
5001                 return false;
5002         return page_pool_return_skb_page(virt_to_page(data));
5003 }
5004
5005 #endif  /* __KERNEL__ */
5006 #endif  /* _LINUX_SKBUFF_H */