at v5.16 138 kB view raw
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 <net/flow.h> 40#include <net/page_pool.h> 41#if IS_ENABLED(CONFIG_NF_CONNTRACK) 42#include <linux/netfilter/nf_conntrack_common.h> 43#endif 44 45/* The interface for checksum offload between the stack and networking drivers 46 * is as follows... 47 * 48 * A. IP checksum related features 49 * 50 * Drivers advertise checksum offload capabilities in the features of a device. 51 * From the stack's point of view these are capabilities offered by the driver. 52 * A driver typically only advertises features that it is capable of offloading 53 * to its device. 54 * 55 * The checksum related features are: 56 * 57 * NETIF_F_HW_CSUM - The driver (or its device) is able to compute one 58 * IP (one's complement) checksum for any combination 59 * of protocols or protocol layering. The checksum is 60 * computed and set in a packet per the CHECKSUM_PARTIAL 61 * interface (see below). 62 * 63 * NETIF_F_IP_CSUM - Driver (device) is only able to checksum plain 64 * TCP or UDP packets over IPv4. These are specifically 65 * unencapsulated packets of the form IPv4|TCP or 66 * IPv4|UDP where the Protocol field in the IPv4 header 67 * is TCP or UDP. The IPv4 header may contain IP options. 68 * This feature cannot be set in features for a device 69 * with NETIF_F_HW_CSUM also set. This feature is being 70 * DEPRECATED (see below). 71 * 72 * NETIF_F_IPV6_CSUM - Driver (device) is only able to checksum plain 73 * TCP or UDP packets over IPv6. These are specifically 74 * unencapsulated packets of the form IPv6|TCP or 75 * IPv6|UDP where the Next Header field in the IPv6 76 * header is either TCP or UDP. IPv6 extension headers 77 * are not supported with this feature. This feature 78 * cannot be set in features for a device with 79 * NETIF_F_HW_CSUM also set. This feature is being 80 * DEPRECATED (see below). 81 * 82 * NETIF_F_RXCSUM - Driver (device) performs receive checksum offload. 83 * This flag is only used to disable the RX checksum 84 * feature for a device. The stack will accept receive 85 * checksum indication in packets received on a device 86 * regardless of whether NETIF_F_RXCSUM is set. 87 * 88 * B. Checksumming of received packets by device. Indication of checksum 89 * verification is set in skb->ip_summed. Possible values are: 90 * 91 * CHECKSUM_NONE: 92 * 93 * Device did not checksum this packet e.g. due to lack of capabilities. 94 * The packet contains full (though not verified) checksum in packet but 95 * not in skb->csum. Thus, skb->csum is undefined in this case. 96 * 97 * CHECKSUM_UNNECESSARY: 98 * 99 * The hardware you're dealing with doesn't calculate the full checksum 100 * (as in CHECKSUM_COMPLETE), but it does parse headers and verify checksums 101 * for specific protocols. For such packets it will set CHECKSUM_UNNECESSARY 102 * if their checksums are okay. skb->csum is still undefined in this case 103 * though. A driver or device must never modify the checksum field in the 104 * packet even if checksum is verified. 105 * 106 * CHECKSUM_UNNECESSARY is applicable to following protocols: 107 * TCP: IPv6 and IPv4. 108 * UDP: IPv4 and IPv6. A device may apply CHECKSUM_UNNECESSARY to a 109 * zero UDP checksum for either IPv4 or IPv6, the networking stack 110 * may perform further validation in this case. 111 * GRE: only if the checksum is present in the header. 112 * SCTP: indicates the CRC in SCTP header has been validated. 113 * FCOE: indicates the CRC in FC frame has been validated. 114 * 115 * skb->csum_level indicates the number of consecutive checksums found in 116 * the packet minus one that have been verified as CHECKSUM_UNNECESSARY. 117 * For instance if a device receives an IPv6->UDP->GRE->IPv4->TCP packet 118 * and a device is able to verify the checksums for UDP (possibly zero), 119 * GRE (checksum flag is set) and TCP, skb->csum_level would be set to 120 * two. If the device were only able to verify the UDP checksum and not 121 * GRE, either because it doesn't support GRE checksum or because GRE 122 * checksum is bad, skb->csum_level would be set to zero (TCP checksum is 123 * not considered in this case). 124 * 125 * CHECKSUM_COMPLETE: 126 * 127 * This is the most generic way. The device supplied checksum of the _whole_ 128 * packet as seen by netif_rx() and fills in skb->csum. This means the 129 * hardware doesn't need to parse L3/L4 headers to implement this. 130 * 131 * Notes: 132 * - Even if device supports only some protocols, but is able to produce 133 * skb->csum, it MUST use CHECKSUM_COMPLETE, not CHECKSUM_UNNECESSARY. 134 * - CHECKSUM_COMPLETE is not applicable to SCTP and FCoE protocols. 135 * 136 * CHECKSUM_PARTIAL: 137 * 138 * A checksum is set up to be offloaded to a device as described in the 139 * output description for CHECKSUM_PARTIAL. This may occur on a packet 140 * received directly from another Linux OS, e.g., a virtualized Linux kernel 141 * on the same host, or it may be set in the input path in GRO or remote 142 * checksum offload. For the purposes of checksum verification, the checksum 143 * referred to by skb->csum_start + skb->csum_offset and any preceding 144 * checksums in the packet are considered verified. Any checksums in the 145 * packet that are after the checksum being offloaded are not considered to 146 * be verified. 147 * 148 * C. Checksumming on transmit for non-GSO. The stack requests checksum offload 149 * in the skb->ip_summed for a packet. Values are: 150 * 151 * CHECKSUM_PARTIAL: 152 * 153 * The driver is required to checksum the packet as seen by hard_start_xmit() 154 * from skb->csum_start up to the end, and to record/write the checksum at 155 * offset skb->csum_start + skb->csum_offset. A driver may verify that the 156 * csum_start and csum_offset values are valid values given the length and 157 * offset of the packet, but it should not attempt to validate that the 158 * checksum refers to a legitimate transport layer checksum -- it is the 159 * purview of the stack to validate that csum_start and csum_offset are set 160 * correctly. 161 * 162 * When the stack requests checksum offload for a packet, the driver MUST 163 * ensure that the checksum is set correctly. A driver can either offload the 164 * checksum calculation to the device, or call skb_checksum_help (in the case 165 * that the device does not support offload for a particular checksum). 166 * 167 * NETIF_F_IP_CSUM and NETIF_F_IPV6_CSUM are being deprecated in favor of 168 * NETIF_F_HW_CSUM. New devices should use NETIF_F_HW_CSUM to indicate 169 * checksum offload capability. 170 * skb_csum_hwoffload_help() can be called to resolve CHECKSUM_PARTIAL based 171 * on network device checksumming capabilities: if a packet does not match 172 * them, skb_checksum_help or skb_crc32c_help (depending on the value of 173 * csum_not_inet, see item D.) is called to resolve the checksum. 174 * 175 * CHECKSUM_NONE: 176 * 177 * The skb was already checksummed by the protocol, or a checksum is not 178 * required. 179 * 180 * CHECKSUM_UNNECESSARY: 181 * 182 * This has the same meaning as CHECKSUM_NONE for checksum offload on 183 * output. 184 * 185 * CHECKSUM_COMPLETE: 186 * Not used in checksum output. If a driver observes a packet with this value 187 * set in skbuff, it should treat the packet as if CHECKSUM_NONE were set. 188 * 189 * D. Non-IP checksum (CRC) offloads 190 * 191 * NETIF_F_SCTP_CRC - This feature indicates that a device is capable of 192 * offloading the SCTP CRC in a packet. To perform this offload the stack 193 * will set csum_start and csum_offset accordingly, set ip_summed to 194 * CHECKSUM_PARTIAL and set csum_not_inet to 1, to provide an indication in 195 * the skbuff that the CHECKSUM_PARTIAL refers to CRC32c. 196 * A driver that supports both IP checksum offload and SCTP CRC32c offload 197 * must verify which offload is configured for a packet by testing the 198 * value of skb->csum_not_inet; skb_crc32c_csum_help is provided to resolve 199 * CHECKSUM_PARTIAL on skbs where csum_not_inet is set to 1. 200 * 201 * NETIF_F_FCOE_CRC - This feature indicates that a device is capable of 202 * offloading the FCOE CRC in a packet. To perform this offload the stack 203 * will set ip_summed to CHECKSUM_PARTIAL and set csum_start and csum_offset 204 * accordingly. Note that there is no indication in the skbuff that the 205 * CHECKSUM_PARTIAL refers to an FCOE checksum, so a driver that supports 206 * both IP checksum offload and FCOE CRC offload must verify which offload 207 * is configured for a packet, presumably by inspecting packet headers. 208 * 209 * E. Checksumming on output with GSO. 210 * 211 * In the case of a GSO packet (skb_is_gso(skb) is true), checksum offload 212 * is implied by the SKB_GSO_* flags in gso_type. Most obviously, if the 213 * gso_type is SKB_GSO_TCPV4 or SKB_GSO_TCPV6, TCP checksum offload as 214 * part of the GSO operation is implied. If a checksum is being offloaded 215 * with GSO then ip_summed is CHECKSUM_PARTIAL, and both csum_start and 216 * csum_offset are set to refer to the outermost checksum being offloaded 217 * (two offloaded checksums are possible with UDP encapsulation). 218 */ 219 220/* Don't change this without changing skb_csum_unnecessary! */ 221#define CHECKSUM_NONE 0 222#define CHECKSUM_UNNECESSARY 1 223#define CHECKSUM_COMPLETE 2 224#define CHECKSUM_PARTIAL 3 225 226/* Maximum value in skb->csum_level */ 227#define SKB_MAX_CSUM_LEVEL 3 228 229#define SKB_DATA_ALIGN(X) ALIGN(X, SMP_CACHE_BYTES) 230#define SKB_WITH_OVERHEAD(X) \ 231 ((X) - SKB_DATA_ALIGN(sizeof(struct skb_shared_info))) 232#define SKB_MAX_ORDER(X, ORDER) \ 233 SKB_WITH_OVERHEAD((PAGE_SIZE << (ORDER)) - (X)) 234#define SKB_MAX_HEAD(X) (SKB_MAX_ORDER((X), 0)) 235#define SKB_MAX_ALLOC (SKB_MAX_ORDER(0, 2)) 236 237/* return minimum truesize of one skb containing X bytes of data */ 238#define SKB_TRUESIZE(X) ((X) + \ 239 SKB_DATA_ALIGN(sizeof(struct sk_buff)) + \ 240 SKB_DATA_ALIGN(sizeof(struct skb_shared_info))) 241 242struct ahash_request; 243struct net_device; 244struct scatterlist; 245struct pipe_inode_info; 246struct iov_iter; 247struct napi_struct; 248struct bpf_prog; 249union bpf_attr; 250struct skb_ext; 251 252#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER) 253struct nf_bridge_info { 254 enum { 255 BRNF_PROTO_UNCHANGED, 256 BRNF_PROTO_8021Q, 257 BRNF_PROTO_PPPOE 258 } orig_proto:8; 259 u8 pkt_otherhost:1; 260 u8 in_prerouting:1; 261 u8 bridged_dnat:1; 262 __u16 frag_max_size; 263 struct net_device *physindev; 264 265 /* always valid & non-NULL from FORWARD on, for physdev match */ 266 struct net_device *physoutdev; 267 union { 268 /* prerouting: detect dnat in orig/reply direction */ 269 __be32 ipv4_daddr; 270 struct in6_addr ipv6_daddr; 271 272 /* after prerouting + nat detected: store original source 273 * mac since neigh resolution overwrites it, only used while 274 * skb is out in neigh layer. 275 */ 276 char neigh_header[8]; 277 }; 278}; 279#endif 280 281#if IS_ENABLED(CONFIG_NET_TC_SKB_EXT) 282/* Chain in tc_skb_ext will be used to share the tc chain with 283 * ovs recirc_id. It will be set to the current chain by tc 284 * and read by ovs to recirc_id. 285 */ 286struct tc_skb_ext { 287 __u32 chain; 288 __u16 mru; 289 __u16 zone; 290 bool post_ct; 291}; 292#endif 293 294struct sk_buff_head { 295 /* These two members must be first. */ 296 struct sk_buff *next; 297 struct sk_buff *prev; 298 299 __u32 qlen; 300 spinlock_t lock; 301}; 302 303struct sk_buff; 304 305/* To allow 64K frame to be packed as single skb without frag_list we 306 * require 64K/PAGE_SIZE pages plus 1 additional page to allow for 307 * buffers which do not start on a page boundary. 308 * 309 * Since GRO uses frags we allocate at least 16 regardless of page 310 * size. 311 */ 312#if (65536/PAGE_SIZE + 1) < 16 313#define MAX_SKB_FRAGS 16UL 314#else 315#define MAX_SKB_FRAGS (65536/PAGE_SIZE + 1) 316#endif 317extern int sysctl_max_skb_frags; 318 319/* Set skb_shinfo(skb)->gso_size to this in case you want skb_segment to 320 * segment using its current segmentation instead. 321 */ 322#define GSO_BY_FRAGS 0xFFFF 323 324typedef struct bio_vec skb_frag_t; 325 326/** 327 * skb_frag_size() - Returns the size of a skb fragment 328 * @frag: skb fragment 329 */ 330static inline unsigned int skb_frag_size(const skb_frag_t *frag) 331{ 332 return frag->bv_len; 333} 334 335/** 336 * skb_frag_size_set() - Sets the size of a skb fragment 337 * @frag: skb fragment 338 * @size: size of fragment 339 */ 340static inline void skb_frag_size_set(skb_frag_t *frag, unsigned int size) 341{ 342 frag->bv_len = size; 343} 344 345/** 346 * skb_frag_size_add() - Increments the size of a skb fragment by @delta 347 * @frag: skb fragment 348 * @delta: value to add 349 */ 350static inline void skb_frag_size_add(skb_frag_t *frag, int delta) 351{ 352 frag->bv_len += delta; 353} 354 355/** 356 * skb_frag_size_sub() - Decrements the size of a skb fragment by @delta 357 * @frag: skb fragment 358 * @delta: value to subtract 359 */ 360static inline void skb_frag_size_sub(skb_frag_t *frag, int delta) 361{ 362 frag->bv_len -= delta; 363} 364 365/** 366 * skb_frag_must_loop - Test if %p is a high memory page 367 * @p: fragment's page 368 */ 369static inline bool skb_frag_must_loop(struct page *p) 370{ 371#if defined(CONFIG_HIGHMEM) 372 if (IS_ENABLED(CONFIG_DEBUG_KMAP_LOCAL_FORCE_MAP) || PageHighMem(p)) 373 return true; 374#endif 375 return false; 376} 377 378/** 379 * skb_frag_foreach_page - loop over pages in a fragment 380 * 381 * @f: skb frag to operate on 382 * @f_off: offset from start of f->bv_page 383 * @f_len: length from f_off to loop over 384 * @p: (temp var) current page 385 * @p_off: (temp var) offset from start of current page, 386 * non-zero only on first page. 387 * @p_len: (temp var) length in current page, 388 * < PAGE_SIZE only on first and last page. 389 * @copied: (temp var) length so far, excluding current p_len. 390 * 391 * A fragment can hold a compound page, in which case per-page 392 * operations, notably kmap_atomic, must be called for each 393 * regular page. 394 */ 395#define skb_frag_foreach_page(f, f_off, f_len, p, p_off, p_len, copied) \ 396 for (p = skb_frag_page(f) + ((f_off) >> PAGE_SHIFT), \ 397 p_off = (f_off) & (PAGE_SIZE - 1), \ 398 p_len = skb_frag_must_loop(p) ? \ 399 min_t(u32, f_len, PAGE_SIZE - p_off) : f_len, \ 400 copied = 0; \ 401 copied < f_len; \ 402 copied += p_len, p++, p_off = 0, \ 403 p_len = min_t(u32, f_len - copied, PAGE_SIZE)) \ 404 405#define HAVE_HW_TIME_STAMP 406 407/** 408 * struct skb_shared_hwtstamps - hardware time stamps 409 * @hwtstamp: hardware time stamp transformed into duration 410 * since arbitrary point in time 411 * 412 * Software time stamps generated by ktime_get_real() are stored in 413 * skb->tstamp. 414 * 415 * hwtstamps can only be compared against other hwtstamps from 416 * the same device. 417 * 418 * This structure is attached to packets as part of the 419 * &skb_shared_info. Use skb_hwtstamps() to get a pointer. 420 */ 421struct skb_shared_hwtstamps { 422 ktime_t hwtstamp; 423}; 424 425/* Definitions for tx_flags in struct skb_shared_info */ 426enum { 427 /* generate hardware time stamp */ 428 SKBTX_HW_TSTAMP = 1 << 0, 429 430 /* generate software time stamp when queueing packet to NIC */ 431 SKBTX_SW_TSTAMP = 1 << 1, 432 433 /* device driver is going to provide hardware time stamp */ 434 SKBTX_IN_PROGRESS = 1 << 2, 435 436 /* generate wifi status information (where possible) */ 437 SKBTX_WIFI_STATUS = 1 << 4, 438 439 /* generate software time stamp when entering packet scheduling */ 440 SKBTX_SCHED_TSTAMP = 1 << 6, 441}; 442 443#define SKBTX_ANY_SW_TSTAMP (SKBTX_SW_TSTAMP | \ 444 SKBTX_SCHED_TSTAMP) 445#define SKBTX_ANY_TSTAMP (SKBTX_HW_TSTAMP | SKBTX_ANY_SW_TSTAMP) 446 447/* Definitions for flags in struct skb_shared_info */ 448enum { 449 /* use zcopy routines */ 450 SKBFL_ZEROCOPY_ENABLE = BIT(0), 451 452 /* This indicates at least one fragment might be overwritten 453 * (as in vmsplice(), sendfile() ...) 454 * If we need to compute a TX checksum, we'll need to copy 455 * all frags to avoid possible bad checksum 456 */ 457 SKBFL_SHARED_FRAG = BIT(1), 458 459 /* segment contains only zerocopy data and should not be 460 * charged to the kernel memory. 461 */ 462 SKBFL_PURE_ZEROCOPY = BIT(2), 463}; 464 465#define SKBFL_ZEROCOPY_FRAG (SKBFL_ZEROCOPY_ENABLE | SKBFL_SHARED_FRAG) 466#define SKBFL_ALL_ZEROCOPY (SKBFL_ZEROCOPY_FRAG | SKBFL_PURE_ZEROCOPY) 467 468/* 469 * The callback notifies userspace to release buffers when skb DMA is done in 470 * lower device, the skb last reference should be 0 when calling this. 471 * The zerocopy_success argument is true if zero copy transmit occurred, 472 * false on data copy or out of memory error caused by data copy attempt. 473 * The ctx field is used to track device context. 474 * The desc field is used to track userspace buffer index. 475 */ 476struct ubuf_info { 477 void (*callback)(struct sk_buff *, struct ubuf_info *, 478 bool zerocopy_success); 479 union { 480 struct { 481 unsigned long desc; 482 void *ctx; 483 }; 484 struct { 485 u32 id; 486 u16 len; 487 u16 zerocopy:1; 488 u32 bytelen; 489 }; 490 }; 491 refcount_t refcnt; 492 u8 flags; 493 494 struct mmpin { 495 struct user_struct *user; 496 unsigned int num_pg; 497 } mmp; 498}; 499 500#define skb_uarg(SKB) ((struct ubuf_info *)(skb_shinfo(SKB)->destructor_arg)) 501 502int mm_account_pinned_pages(struct mmpin *mmp, size_t size); 503void mm_unaccount_pinned_pages(struct mmpin *mmp); 504 505struct ubuf_info *msg_zerocopy_alloc(struct sock *sk, size_t size); 506struct ubuf_info *msg_zerocopy_realloc(struct sock *sk, size_t size, 507 struct ubuf_info *uarg); 508 509void msg_zerocopy_put_abort(struct ubuf_info *uarg, bool have_uref); 510 511void msg_zerocopy_callback(struct sk_buff *skb, struct ubuf_info *uarg, 512 bool success); 513 514int skb_zerocopy_iter_dgram(struct sk_buff *skb, struct msghdr *msg, int len); 515int skb_zerocopy_iter_stream(struct sock *sk, struct sk_buff *skb, 516 struct msghdr *msg, int len, 517 struct ubuf_info *uarg); 518 519/* This data is invariant across clones and lives at 520 * the end of the header data, ie. at skb->end. 521 */ 522struct skb_shared_info { 523 __u8 flags; 524 __u8 meta_len; 525 __u8 nr_frags; 526 __u8 tx_flags; 527 unsigned short gso_size; 528 /* Warning: this field is not always filled in (UFO)! */ 529 unsigned short gso_segs; 530 struct sk_buff *frag_list; 531 struct skb_shared_hwtstamps hwtstamps; 532 unsigned int gso_type; 533 u32 tskey; 534 535 /* 536 * Warning : all fields before dataref are cleared in __alloc_skb() 537 */ 538 atomic_t dataref; 539 540 /* Intermediate layers must ensure that destructor_arg 541 * remains valid until skb destructor */ 542 void * destructor_arg; 543 544 /* must be last field, see pskb_expand_head() */ 545 skb_frag_t frags[MAX_SKB_FRAGS]; 546}; 547 548/* We divide dataref into two halves. The higher 16 bits hold references 549 * to the payload part of skb->data. The lower 16 bits hold references to 550 * the entire skb->data. A clone of a headerless skb holds the length of 551 * the header in skb->hdr_len. 552 * 553 * All users must obey the rule that the skb->data reference count must be 554 * greater than or equal to the payload reference count. 555 * 556 * Holding a reference to the payload part means that the user does not 557 * care about modifications to the header part of skb->data. 558 */ 559#define SKB_DATAREF_SHIFT 16 560#define SKB_DATAREF_MASK ((1 << SKB_DATAREF_SHIFT) - 1) 561 562 563enum { 564 SKB_FCLONE_UNAVAILABLE, /* skb has no fclone (from head_cache) */ 565 SKB_FCLONE_ORIG, /* orig skb (from fclone_cache) */ 566 SKB_FCLONE_CLONE, /* companion fclone skb (from fclone_cache) */ 567}; 568 569enum { 570 SKB_GSO_TCPV4 = 1 << 0, 571 572 /* This indicates the skb is from an untrusted source. */ 573 SKB_GSO_DODGY = 1 << 1, 574 575 /* This indicates the tcp segment has CWR set. */ 576 SKB_GSO_TCP_ECN = 1 << 2, 577 578 SKB_GSO_TCP_FIXEDID = 1 << 3, 579 580 SKB_GSO_TCPV6 = 1 << 4, 581 582 SKB_GSO_FCOE = 1 << 5, 583 584 SKB_GSO_GRE = 1 << 6, 585 586 SKB_GSO_GRE_CSUM = 1 << 7, 587 588 SKB_GSO_IPXIP4 = 1 << 8, 589 590 SKB_GSO_IPXIP6 = 1 << 9, 591 592 SKB_GSO_UDP_TUNNEL = 1 << 10, 593 594 SKB_GSO_UDP_TUNNEL_CSUM = 1 << 11, 595 596 SKB_GSO_PARTIAL = 1 << 12, 597 598 SKB_GSO_TUNNEL_REMCSUM = 1 << 13, 599 600 SKB_GSO_SCTP = 1 << 14, 601 602 SKB_GSO_ESP = 1 << 15, 603 604 SKB_GSO_UDP = 1 << 16, 605 606 SKB_GSO_UDP_L4 = 1 << 17, 607 608 SKB_GSO_FRAGLIST = 1 << 18, 609}; 610 611#if BITS_PER_LONG > 32 612#define NET_SKBUFF_DATA_USES_OFFSET 1 613#endif 614 615#ifdef NET_SKBUFF_DATA_USES_OFFSET 616typedef unsigned int sk_buff_data_t; 617#else 618typedef unsigned char *sk_buff_data_t; 619#endif 620 621/** 622 * struct sk_buff - socket buffer 623 * @next: Next buffer in list 624 * @prev: Previous buffer in list 625 * @tstamp: Time we arrived/left 626 * @skb_mstamp_ns: (aka @tstamp) earliest departure time; start point 627 * for retransmit timer 628 * @rbnode: RB tree node, alternative to next/prev for netem/tcp 629 * @list: queue head 630 * @sk: Socket we are owned by 631 * @ip_defrag_offset: (aka @sk) alternate use of @sk, used in 632 * fragmentation management 633 * @dev: Device we arrived on/are leaving by 634 * @dev_scratch: (aka @dev) alternate use of @dev when @dev would be %NULL 635 * @cb: Control buffer. Free for use by every layer. Put private vars here 636 * @_skb_refdst: destination entry (with norefcount bit) 637 * @sp: the security path, used for xfrm 638 * @len: Length of actual data 639 * @data_len: Data length 640 * @mac_len: Length of link layer header 641 * @hdr_len: writable header length of cloned skb 642 * @csum: Checksum (must include start/offset pair) 643 * @csum_start: Offset from skb->head where checksumming should start 644 * @csum_offset: Offset from csum_start where checksum should be stored 645 * @priority: Packet queueing priority 646 * @ignore_df: allow local fragmentation 647 * @cloned: Head may be cloned (check refcnt to be sure) 648 * @ip_summed: Driver fed us an IP checksum 649 * @nohdr: Payload reference only, must not modify header 650 * @pkt_type: Packet class 651 * @fclone: skbuff clone status 652 * @ipvs_property: skbuff is owned by ipvs 653 * @inner_protocol_type: whether the inner protocol is 654 * ENCAP_TYPE_ETHER or ENCAP_TYPE_IPPROTO 655 * @remcsum_offload: remote checksum offload is enabled 656 * @offload_fwd_mark: Packet was L2-forwarded in hardware 657 * @offload_l3_fwd_mark: Packet was L3-forwarded in hardware 658 * @tc_skip_classify: do not classify packet. set by IFB device 659 * @tc_at_ingress: used within tc_classify to distinguish in/egress 660 * @redirected: packet was redirected by packet classifier 661 * @from_ingress: packet was redirected from the ingress path 662 * @nf_skip_egress: packet shall skip nf egress - see netfilter_netdev.h 663 * @peeked: this packet has been seen already, so stats have been 664 * done for it, don't do them again 665 * @nf_trace: netfilter packet trace flag 666 * @protocol: Packet protocol from driver 667 * @destructor: Destruct function 668 * @tcp_tsorted_anchor: list structure for TCP (tp->tsorted_sent_queue) 669 * @_sk_redir: socket redirection information for skmsg 670 * @_nfct: Associated connection, if any (with nfctinfo bits) 671 * @nf_bridge: Saved data about a bridged frame - see br_netfilter.c 672 * @skb_iif: ifindex of device we arrived on 673 * @tc_index: Traffic control index 674 * @hash: the packet hash 675 * @queue_mapping: Queue mapping for multiqueue devices 676 * @head_frag: skb was allocated from page fragments, 677 * not allocated by kmalloc() or vmalloc(). 678 * @pfmemalloc: skbuff was allocated from PFMEMALLOC reserves 679 * @pp_recycle: mark the packet for recycling instead of freeing (implies 680 * page_pool support on driver) 681 * @active_extensions: active extensions (skb_ext_id types) 682 * @ndisc_nodetype: router type (from link layer) 683 * @ooo_okay: allow the mapping of a socket to a queue to be changed 684 * @l4_hash: indicate hash is a canonical 4-tuple hash over transport 685 * ports. 686 * @sw_hash: indicates hash was computed in software stack 687 * @wifi_acked_valid: wifi_acked was set 688 * @wifi_acked: whether frame was acked on wifi or not 689 * @no_fcs: Request NIC to treat last 4 bytes as Ethernet FCS 690 * @encapsulation: indicates the inner headers in the skbuff are valid 691 * @encap_hdr_csum: software checksum is needed 692 * @csum_valid: checksum is already valid 693 * @csum_not_inet: use CRC32c to resolve CHECKSUM_PARTIAL 694 * @csum_complete_sw: checksum was completed by software 695 * @csum_level: indicates the number of consecutive checksums found in 696 * the packet minus one that have been verified as 697 * CHECKSUM_UNNECESSARY (max 3) 698 * @dst_pending_confirm: need to confirm neighbour 699 * @decrypted: Decrypted SKB 700 * @slow_gro: state present at GRO time, slower prepare step required 701 * @napi_id: id of the NAPI struct this skb came from 702 * @sender_cpu: (aka @napi_id) source CPU in XPS 703 * @secmark: security marking 704 * @mark: Generic packet mark 705 * @reserved_tailroom: (aka @mark) number of bytes of free space available 706 * at the tail of an sk_buff 707 * @vlan_present: VLAN tag is present 708 * @vlan_proto: vlan encapsulation protocol 709 * @vlan_tci: vlan tag control information 710 * @inner_protocol: Protocol (encapsulation) 711 * @inner_ipproto: (aka @inner_protocol) stores ipproto when 712 * skb->inner_protocol_type == ENCAP_TYPE_IPPROTO; 713 * @inner_transport_header: Inner transport layer header (encapsulation) 714 * @inner_network_header: Network layer header (encapsulation) 715 * @inner_mac_header: Link layer header (encapsulation) 716 * @transport_header: Transport layer header 717 * @network_header: Network layer header 718 * @mac_header: Link layer header 719 * @kcov_handle: KCOV remote handle for remote coverage collection 720 * @tail: Tail pointer 721 * @end: End pointer 722 * @head: Head of buffer 723 * @data: Data head pointer 724 * @truesize: Buffer size 725 * @users: User count - see {datagram,tcp}.c 726 * @extensions: allocated extensions, valid if active_extensions is nonzero 727 */ 728 729struct sk_buff { 730 union { 731 struct { 732 /* These two members must be first. */ 733 struct sk_buff *next; 734 struct sk_buff *prev; 735 736 union { 737 struct net_device *dev; 738 /* Some protocols might use this space to store information, 739 * while device pointer would be NULL. 740 * UDP receive path is one user. 741 */ 742 unsigned long dev_scratch; 743 }; 744 }; 745 struct rb_node rbnode; /* used in netem, ip4 defrag, and tcp stack */ 746 struct list_head list; 747 }; 748 749 union { 750 struct sock *sk; 751 int ip_defrag_offset; 752 }; 753 754 union { 755 ktime_t tstamp; 756 u64 skb_mstamp_ns; /* earliest departure time */ 757 }; 758 /* 759 * This is the control buffer. It is free to use for every 760 * layer. Please put your private variables there. If you 761 * want to keep them across layers you have to do a skb_clone() 762 * first. This is owned by whoever has the skb queued ATM. 763 */ 764 char cb[48] __aligned(8); 765 766 union { 767 struct { 768 unsigned long _skb_refdst; 769 void (*destructor)(struct sk_buff *skb); 770 }; 771 struct list_head tcp_tsorted_anchor; 772#ifdef CONFIG_NET_SOCK_MSG 773 unsigned long _sk_redir; 774#endif 775 }; 776 777#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE) 778 unsigned long _nfct; 779#endif 780 unsigned int len, 781 data_len; 782 __u16 mac_len, 783 hdr_len; 784 785 /* Following fields are _not_ copied in __copy_skb_header() 786 * Note that queue_mapping is here mostly to fill a hole. 787 */ 788 __u16 queue_mapping; 789 790/* if you move cloned around you also must adapt those constants */ 791#ifdef __BIG_ENDIAN_BITFIELD 792#define CLONED_MASK (1 << 7) 793#else 794#define CLONED_MASK 1 795#endif 796#define CLONED_OFFSET() offsetof(struct sk_buff, __cloned_offset) 797 798 /* private: */ 799 __u8 __cloned_offset[0]; 800 /* public: */ 801 __u8 cloned:1, 802 nohdr:1, 803 fclone:2, 804 peeked:1, 805 head_frag:1, 806 pfmemalloc:1, 807 pp_recycle:1; /* page_pool recycle indicator */ 808#ifdef CONFIG_SKB_EXTENSIONS 809 __u8 active_extensions; 810#endif 811 812 /* fields enclosed in headers_start/headers_end are copied 813 * using a single memcpy() in __copy_skb_header() 814 */ 815 /* private: */ 816 __u32 headers_start[0]; 817 /* public: */ 818 819/* if you move pkt_type around you also must adapt those constants */ 820#ifdef __BIG_ENDIAN_BITFIELD 821#define PKT_TYPE_MAX (7 << 5) 822#else 823#define PKT_TYPE_MAX 7 824#endif 825#define PKT_TYPE_OFFSET() offsetof(struct sk_buff, __pkt_type_offset) 826 827 /* private: */ 828 __u8 __pkt_type_offset[0]; 829 /* public: */ 830 __u8 pkt_type:3; 831 __u8 ignore_df:1; 832 __u8 nf_trace:1; 833 __u8 ip_summed:2; 834 __u8 ooo_okay:1; 835 836 __u8 l4_hash:1; 837 __u8 sw_hash:1; 838 __u8 wifi_acked_valid:1; 839 __u8 wifi_acked:1; 840 __u8 no_fcs:1; 841 /* Indicates the inner headers are valid in the skbuff. */ 842 __u8 encapsulation:1; 843 __u8 encap_hdr_csum:1; 844 __u8 csum_valid:1; 845 846#ifdef __BIG_ENDIAN_BITFIELD 847#define PKT_VLAN_PRESENT_BIT 7 848#else 849#define PKT_VLAN_PRESENT_BIT 0 850#endif 851#define PKT_VLAN_PRESENT_OFFSET() offsetof(struct sk_buff, __pkt_vlan_present_offset) 852 /* private: */ 853 __u8 __pkt_vlan_present_offset[0]; 854 /* public: */ 855 __u8 vlan_present:1; 856 __u8 csum_complete_sw:1; 857 __u8 csum_level:2; 858 __u8 csum_not_inet:1; 859 __u8 dst_pending_confirm:1; 860#ifdef CONFIG_IPV6_NDISC_NODETYPE 861 __u8 ndisc_nodetype:2; 862#endif 863 864 __u8 ipvs_property:1; 865 __u8 inner_protocol_type:1; 866 __u8 remcsum_offload:1; 867#ifdef CONFIG_NET_SWITCHDEV 868 __u8 offload_fwd_mark:1; 869 __u8 offload_l3_fwd_mark:1; 870#endif 871#ifdef CONFIG_NET_CLS_ACT 872 __u8 tc_skip_classify:1; 873 __u8 tc_at_ingress:1; 874#endif 875 __u8 redirected:1; 876#ifdef CONFIG_NET_REDIRECT 877 __u8 from_ingress:1; 878#endif 879#ifdef CONFIG_NETFILTER_SKIP_EGRESS 880 __u8 nf_skip_egress:1; 881#endif 882#ifdef CONFIG_TLS_DEVICE 883 __u8 decrypted:1; 884#endif 885 __u8 slow_gro:1; 886 887#ifdef CONFIG_NET_SCHED 888 __u16 tc_index; /* traffic control index */ 889#endif 890 891 union { 892 __wsum csum; 893 struct { 894 __u16 csum_start; 895 __u16 csum_offset; 896 }; 897 }; 898 __u32 priority; 899 int skb_iif; 900 __u32 hash; 901 __be16 vlan_proto; 902 __u16 vlan_tci; 903#if defined(CONFIG_NET_RX_BUSY_POLL) || defined(CONFIG_XPS) 904 union { 905 unsigned int napi_id; 906 unsigned int sender_cpu; 907 }; 908#endif 909#ifdef CONFIG_NETWORK_SECMARK 910 __u32 secmark; 911#endif 912 913 union { 914 __u32 mark; 915 __u32 reserved_tailroom; 916 }; 917 918 union { 919 __be16 inner_protocol; 920 __u8 inner_ipproto; 921 }; 922 923 __u16 inner_transport_header; 924 __u16 inner_network_header; 925 __u16 inner_mac_header; 926 927 __be16 protocol; 928 __u16 transport_header; 929 __u16 network_header; 930 __u16 mac_header; 931 932#ifdef CONFIG_KCOV 933 u64 kcov_handle; 934#endif 935 936 /* private: */ 937 __u32 headers_end[0]; 938 /* public: */ 939 940 /* These elements must be at the end, see alloc_skb() for details. */ 941 sk_buff_data_t tail; 942 sk_buff_data_t end; 943 unsigned char *head, 944 *data; 945 unsigned int truesize; 946 refcount_t users; 947 948#ifdef CONFIG_SKB_EXTENSIONS 949 /* only useable after checking ->active_extensions != 0 */ 950 struct skb_ext *extensions; 951#endif 952}; 953 954#ifdef __KERNEL__ 955/* 956 * Handling routines are only of interest to the kernel 957 */ 958 959#define SKB_ALLOC_FCLONE 0x01 960#define SKB_ALLOC_RX 0x02 961#define SKB_ALLOC_NAPI 0x04 962 963/** 964 * skb_pfmemalloc - Test if the skb was allocated from PFMEMALLOC reserves 965 * @skb: buffer 966 */ 967static inline bool skb_pfmemalloc(const struct sk_buff *skb) 968{ 969 return unlikely(skb->pfmemalloc); 970} 971 972/* 973 * skb might have a dst pointer attached, refcounted or not. 974 * _skb_refdst low order bit is set if refcount was _not_ taken 975 */ 976#define SKB_DST_NOREF 1UL 977#define SKB_DST_PTRMASK ~(SKB_DST_NOREF) 978 979/** 980 * skb_dst - returns skb dst_entry 981 * @skb: buffer 982 * 983 * Returns skb dst_entry, regardless of reference taken or not. 984 */ 985static inline struct dst_entry *skb_dst(const struct sk_buff *skb) 986{ 987 /* If refdst was not refcounted, check we still are in a 988 * rcu_read_lock section 989 */ 990 WARN_ON((skb->_skb_refdst & SKB_DST_NOREF) && 991 !rcu_read_lock_held() && 992 !rcu_read_lock_bh_held()); 993 return (struct dst_entry *)(skb->_skb_refdst & SKB_DST_PTRMASK); 994} 995 996/** 997 * skb_dst_set - sets skb dst 998 * @skb: buffer 999 * @dst: dst entry 1000 * 1001 * Sets skb dst, assuming a reference was taken on dst and should 1002 * be released by skb_dst_drop() 1003 */ 1004static inline void skb_dst_set(struct sk_buff *skb, struct dst_entry *dst) 1005{ 1006 skb->slow_gro |= !!dst; 1007 skb->_skb_refdst = (unsigned long)dst; 1008} 1009 1010/** 1011 * skb_dst_set_noref - sets skb dst, hopefully, without taking reference 1012 * @skb: buffer 1013 * @dst: dst entry 1014 * 1015 * Sets skb dst, assuming a reference was not taken on dst. 1016 * If dst entry is cached, we do not take reference and dst_release 1017 * will be avoided by refdst_drop. If dst entry is not cached, we take 1018 * reference, so that last dst_release can destroy the dst immediately. 1019 */ 1020static inline void skb_dst_set_noref(struct sk_buff *skb, struct dst_entry *dst) 1021{ 1022 WARN_ON(!rcu_read_lock_held() && !rcu_read_lock_bh_held()); 1023 skb->slow_gro |= !!dst; 1024 skb->_skb_refdst = (unsigned long)dst | SKB_DST_NOREF; 1025} 1026 1027/** 1028 * skb_dst_is_noref - Test if skb dst isn't refcounted 1029 * @skb: buffer 1030 */ 1031static inline bool skb_dst_is_noref(const struct sk_buff *skb) 1032{ 1033 return (skb->_skb_refdst & SKB_DST_NOREF) && skb_dst(skb); 1034} 1035 1036/** 1037 * skb_rtable - Returns the skb &rtable 1038 * @skb: buffer 1039 */ 1040static inline struct rtable *skb_rtable(const struct sk_buff *skb) 1041{ 1042 return (struct rtable *)skb_dst(skb); 1043} 1044 1045/* For mangling skb->pkt_type from user space side from applications 1046 * such as nft, tc, etc, we only allow a conservative subset of 1047 * possible pkt_types to be set. 1048*/ 1049static inline bool skb_pkt_type_ok(u32 ptype) 1050{ 1051 return ptype <= PACKET_OTHERHOST; 1052} 1053 1054/** 1055 * skb_napi_id - Returns the skb's NAPI id 1056 * @skb: buffer 1057 */ 1058static inline unsigned int skb_napi_id(const struct sk_buff *skb) 1059{ 1060#ifdef CONFIG_NET_RX_BUSY_POLL 1061 return skb->napi_id; 1062#else 1063 return 0; 1064#endif 1065} 1066 1067/** 1068 * skb_unref - decrement the skb's reference count 1069 * @skb: buffer 1070 * 1071 * Returns true if we can free the skb. 1072 */ 1073static inline bool skb_unref(struct sk_buff *skb) 1074{ 1075 if (unlikely(!skb)) 1076 return false; 1077 if (likely(refcount_read(&skb->users) == 1)) 1078 smp_rmb(); 1079 else if (likely(!refcount_dec_and_test(&skb->users))) 1080 return false; 1081 1082 return true; 1083} 1084 1085void skb_release_head_state(struct sk_buff *skb); 1086void kfree_skb(struct sk_buff *skb); 1087void kfree_skb_list(struct sk_buff *segs); 1088void skb_dump(const char *level, const struct sk_buff *skb, bool full_pkt); 1089void skb_tx_error(struct sk_buff *skb); 1090 1091#ifdef CONFIG_TRACEPOINTS 1092void consume_skb(struct sk_buff *skb); 1093#else 1094static inline void consume_skb(struct sk_buff *skb) 1095{ 1096 return kfree_skb(skb); 1097} 1098#endif 1099 1100void __consume_stateless_skb(struct sk_buff *skb); 1101void __kfree_skb(struct sk_buff *skb); 1102extern struct kmem_cache *skbuff_head_cache; 1103 1104void kfree_skb_partial(struct sk_buff *skb, bool head_stolen); 1105bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from, 1106 bool *fragstolen, int *delta_truesize); 1107 1108struct sk_buff *__alloc_skb(unsigned int size, gfp_t priority, int flags, 1109 int node); 1110struct sk_buff *__build_skb(void *data, unsigned int frag_size); 1111struct sk_buff *build_skb(void *data, unsigned int frag_size); 1112struct sk_buff *build_skb_around(struct sk_buff *skb, 1113 void *data, unsigned int frag_size); 1114 1115struct sk_buff *napi_build_skb(void *data, unsigned int frag_size); 1116 1117/** 1118 * alloc_skb - allocate a network buffer 1119 * @size: size to allocate 1120 * @priority: allocation mask 1121 * 1122 * This function is a convenient wrapper around __alloc_skb(). 1123 */ 1124static inline struct sk_buff *alloc_skb(unsigned int size, 1125 gfp_t priority) 1126{ 1127 return __alloc_skb(size, priority, 0, NUMA_NO_NODE); 1128} 1129 1130struct sk_buff *alloc_skb_with_frags(unsigned long header_len, 1131 unsigned long data_len, 1132 int max_page_order, 1133 int *errcode, 1134 gfp_t gfp_mask); 1135struct sk_buff *alloc_skb_for_msg(struct sk_buff *first); 1136 1137/* Layout of fast clones : [skb1][skb2][fclone_ref] */ 1138struct sk_buff_fclones { 1139 struct sk_buff skb1; 1140 1141 struct sk_buff skb2; 1142 1143 refcount_t fclone_ref; 1144}; 1145 1146/** 1147 * skb_fclone_busy - check if fclone is busy 1148 * @sk: socket 1149 * @skb: buffer 1150 * 1151 * Returns true if skb is a fast clone, and its clone is not freed. 1152 * Some drivers call skb_orphan() in their ndo_start_xmit(), 1153 * so we also check that this didnt happen. 1154 */ 1155static inline bool skb_fclone_busy(const struct sock *sk, 1156 const struct sk_buff *skb) 1157{ 1158 const struct sk_buff_fclones *fclones; 1159 1160 fclones = container_of(skb, struct sk_buff_fclones, skb1); 1161 1162 return skb->fclone == SKB_FCLONE_ORIG && 1163 refcount_read(&fclones->fclone_ref) > 1 && 1164 READ_ONCE(fclones->skb2.sk) == sk; 1165} 1166 1167/** 1168 * alloc_skb_fclone - allocate a network buffer from fclone cache 1169 * @size: size to allocate 1170 * @priority: allocation mask 1171 * 1172 * This function is a convenient wrapper around __alloc_skb(). 1173 */ 1174static inline struct sk_buff *alloc_skb_fclone(unsigned int size, 1175 gfp_t priority) 1176{ 1177 return __alloc_skb(size, priority, SKB_ALLOC_FCLONE, NUMA_NO_NODE); 1178} 1179 1180struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src); 1181void skb_headers_offset_update(struct sk_buff *skb, int off); 1182int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask); 1183struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t priority); 1184void skb_copy_header(struct sk_buff *new, const struct sk_buff *old); 1185struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t priority); 1186struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom, 1187 gfp_t gfp_mask, bool fclone); 1188static inline struct sk_buff *__pskb_copy(struct sk_buff *skb, int headroom, 1189 gfp_t gfp_mask) 1190{ 1191 return __pskb_copy_fclone(skb, headroom, gfp_mask, false); 1192} 1193 1194int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail, gfp_t gfp_mask); 1195struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, 1196 unsigned int headroom); 1197struct sk_buff *skb_expand_head(struct sk_buff *skb, unsigned int headroom); 1198struct sk_buff *skb_copy_expand(const struct sk_buff *skb, int newheadroom, 1199 int newtailroom, gfp_t priority); 1200int __must_check skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg, 1201 int offset, int len); 1202int __must_check skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, 1203 int offset, int len); 1204int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer); 1205int __skb_pad(struct sk_buff *skb, int pad, bool free_on_error); 1206 1207/** 1208 * skb_pad - zero pad the tail of an skb 1209 * @skb: buffer to pad 1210 * @pad: space to pad 1211 * 1212 * Ensure that a buffer is followed by a padding area that is zero 1213 * filled. Used by network drivers which may DMA or transfer data 1214 * beyond the buffer end onto the wire. 1215 * 1216 * May return error in out of memory cases. The skb is freed on error. 1217 */ 1218static inline int skb_pad(struct sk_buff *skb, int pad) 1219{ 1220 return __skb_pad(skb, pad, true); 1221} 1222#define dev_kfree_skb(a) consume_skb(a) 1223 1224int skb_append_pagefrags(struct sk_buff *skb, struct page *page, 1225 int offset, size_t size); 1226 1227struct skb_seq_state { 1228 __u32 lower_offset; 1229 __u32 upper_offset; 1230 __u32 frag_idx; 1231 __u32 stepped_offset; 1232 struct sk_buff *root_skb; 1233 struct sk_buff *cur_skb; 1234 __u8 *frag_data; 1235 __u32 frag_off; 1236}; 1237 1238void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from, 1239 unsigned int to, struct skb_seq_state *st); 1240unsigned int skb_seq_read(unsigned int consumed, const u8 **data, 1241 struct skb_seq_state *st); 1242void skb_abort_seq_read(struct skb_seq_state *st); 1243 1244unsigned int skb_find_text(struct sk_buff *skb, unsigned int from, 1245 unsigned int to, struct ts_config *config); 1246 1247/* 1248 * Packet hash types specify the type of hash in skb_set_hash. 1249 * 1250 * Hash types refer to the protocol layer addresses which are used to 1251 * construct a packet's hash. The hashes are used to differentiate or identify 1252 * flows of the protocol layer for the hash type. Hash types are either 1253 * layer-2 (L2), layer-3 (L3), or layer-4 (L4). 1254 * 1255 * Properties of hashes: 1256 * 1257 * 1) Two packets in different flows have different hash values 1258 * 2) Two packets in the same flow should have the same hash value 1259 * 1260 * A hash at a higher layer is considered to be more specific. A driver should 1261 * set the most specific hash possible. 1262 * 1263 * A driver cannot indicate a more specific hash than the layer at which a hash 1264 * was computed. For instance an L3 hash cannot be set as an L4 hash. 1265 * 1266 * A driver may indicate a hash level which is less specific than the 1267 * actual layer the hash was computed on. For instance, a hash computed 1268 * at L4 may be considered an L3 hash. This should only be done if the 1269 * driver can't unambiguously determine that the HW computed the hash at 1270 * the higher layer. Note that the "should" in the second property above 1271 * permits this. 1272 */ 1273enum pkt_hash_types { 1274 PKT_HASH_TYPE_NONE, /* Undefined type */ 1275 PKT_HASH_TYPE_L2, /* Input: src_MAC, dest_MAC */ 1276 PKT_HASH_TYPE_L3, /* Input: src_IP, dst_IP */ 1277 PKT_HASH_TYPE_L4, /* Input: src_IP, dst_IP, src_port, dst_port */ 1278}; 1279 1280static inline void skb_clear_hash(struct sk_buff *skb) 1281{ 1282 skb->hash = 0; 1283 skb->sw_hash = 0; 1284 skb->l4_hash = 0; 1285} 1286 1287static inline void skb_clear_hash_if_not_l4(struct sk_buff *skb) 1288{ 1289 if (!skb->l4_hash) 1290 skb_clear_hash(skb); 1291} 1292 1293static inline void 1294__skb_set_hash(struct sk_buff *skb, __u32 hash, bool is_sw, bool is_l4) 1295{ 1296 skb->l4_hash = is_l4; 1297 skb->sw_hash = is_sw; 1298 skb->hash = hash; 1299} 1300 1301static inline void 1302skb_set_hash(struct sk_buff *skb, __u32 hash, enum pkt_hash_types type) 1303{ 1304 /* Used by drivers to set hash from HW */ 1305 __skb_set_hash(skb, hash, false, type == PKT_HASH_TYPE_L4); 1306} 1307 1308static inline void 1309__skb_set_sw_hash(struct sk_buff *skb, __u32 hash, bool is_l4) 1310{ 1311 __skb_set_hash(skb, hash, true, is_l4); 1312} 1313 1314void __skb_get_hash(struct sk_buff *skb); 1315u32 __skb_get_hash_symmetric(const struct sk_buff *skb); 1316u32 skb_get_poff(const struct sk_buff *skb); 1317u32 __skb_get_poff(const struct sk_buff *skb, const void *data, 1318 const struct flow_keys_basic *keys, int hlen); 1319__be32 __skb_flow_get_ports(const struct sk_buff *skb, int thoff, u8 ip_proto, 1320 const void *data, int hlen_proto); 1321 1322static inline __be32 skb_flow_get_ports(const struct sk_buff *skb, 1323 int thoff, u8 ip_proto) 1324{ 1325 return __skb_flow_get_ports(skb, thoff, ip_proto, NULL, 0); 1326} 1327 1328void skb_flow_dissector_init(struct flow_dissector *flow_dissector, 1329 const struct flow_dissector_key *key, 1330 unsigned int key_count); 1331 1332struct bpf_flow_dissector; 1333bool bpf_flow_dissect(struct bpf_prog *prog, struct bpf_flow_dissector *ctx, 1334 __be16 proto, int nhoff, int hlen, unsigned int flags); 1335 1336bool __skb_flow_dissect(const struct net *net, 1337 const struct sk_buff *skb, 1338 struct flow_dissector *flow_dissector, 1339 void *target_container, const void *data, 1340 __be16 proto, int nhoff, int hlen, unsigned int flags); 1341 1342static inline bool skb_flow_dissect(const struct sk_buff *skb, 1343 struct flow_dissector *flow_dissector, 1344 void *target_container, unsigned int flags) 1345{ 1346 return __skb_flow_dissect(NULL, skb, flow_dissector, 1347 target_container, NULL, 0, 0, 0, flags); 1348} 1349 1350static inline bool skb_flow_dissect_flow_keys(const struct sk_buff *skb, 1351 struct flow_keys *flow, 1352 unsigned int flags) 1353{ 1354 memset(flow, 0, sizeof(*flow)); 1355 return __skb_flow_dissect(NULL, skb, &flow_keys_dissector, 1356 flow, NULL, 0, 0, 0, flags); 1357} 1358 1359static inline bool 1360skb_flow_dissect_flow_keys_basic(const struct net *net, 1361 const struct sk_buff *skb, 1362 struct flow_keys_basic *flow, 1363 const void *data, __be16 proto, 1364 int nhoff, int hlen, unsigned int flags) 1365{ 1366 memset(flow, 0, sizeof(*flow)); 1367 return __skb_flow_dissect(net, skb, &flow_keys_basic_dissector, flow, 1368 data, proto, nhoff, hlen, flags); 1369} 1370 1371void skb_flow_dissect_meta(const struct sk_buff *skb, 1372 struct flow_dissector *flow_dissector, 1373 void *target_container); 1374 1375/* Gets a skb connection tracking info, ctinfo map should be a 1376 * map of mapsize to translate enum ip_conntrack_info states 1377 * to user states. 1378 */ 1379void 1380skb_flow_dissect_ct(const struct sk_buff *skb, 1381 struct flow_dissector *flow_dissector, 1382 void *target_container, 1383 u16 *ctinfo_map, size_t mapsize, 1384 bool post_ct, u16 zone); 1385void 1386skb_flow_dissect_tunnel_info(const struct sk_buff *skb, 1387 struct flow_dissector *flow_dissector, 1388 void *target_container); 1389 1390void skb_flow_dissect_hash(const struct sk_buff *skb, 1391 struct flow_dissector *flow_dissector, 1392 void *target_container); 1393 1394static inline __u32 skb_get_hash(struct sk_buff *skb) 1395{ 1396 if (!skb->l4_hash && !skb->sw_hash) 1397 __skb_get_hash(skb); 1398 1399 return skb->hash; 1400} 1401 1402static inline __u32 skb_get_hash_flowi6(struct sk_buff *skb, const struct flowi6 *fl6) 1403{ 1404 if (!skb->l4_hash && !skb->sw_hash) { 1405 struct flow_keys keys; 1406 __u32 hash = __get_hash_from_flowi6(fl6, &keys); 1407 1408 __skb_set_sw_hash(skb, hash, flow_keys_have_l4(&keys)); 1409 } 1410 1411 return skb->hash; 1412} 1413 1414__u32 skb_get_hash_perturb(const struct sk_buff *skb, 1415 const siphash_key_t *perturb); 1416 1417static inline __u32 skb_get_hash_raw(const struct sk_buff *skb) 1418{ 1419 return skb->hash; 1420} 1421 1422static inline void skb_copy_hash(struct sk_buff *to, const struct sk_buff *from) 1423{ 1424 to->hash = from->hash; 1425 to->sw_hash = from->sw_hash; 1426 to->l4_hash = from->l4_hash; 1427}; 1428 1429static inline void skb_copy_decrypted(struct sk_buff *to, 1430 const struct sk_buff *from) 1431{ 1432#ifdef CONFIG_TLS_DEVICE 1433 to->decrypted = from->decrypted; 1434#endif 1435} 1436 1437#ifdef NET_SKBUFF_DATA_USES_OFFSET 1438static inline unsigned char *skb_end_pointer(const struct sk_buff *skb) 1439{ 1440 return skb->head + skb->end; 1441} 1442 1443static inline unsigned int skb_end_offset(const struct sk_buff *skb) 1444{ 1445 return skb->end; 1446} 1447#else 1448static inline unsigned char *skb_end_pointer(const struct sk_buff *skb) 1449{ 1450 return skb->end; 1451} 1452 1453static inline unsigned int skb_end_offset(const struct sk_buff *skb) 1454{ 1455 return skb->end - skb->head; 1456} 1457#endif 1458 1459/* Internal */ 1460#define skb_shinfo(SKB) ((struct skb_shared_info *)(skb_end_pointer(SKB))) 1461 1462static inline struct skb_shared_hwtstamps *skb_hwtstamps(struct sk_buff *skb) 1463{ 1464 return &skb_shinfo(skb)->hwtstamps; 1465} 1466 1467static inline struct ubuf_info *skb_zcopy(struct sk_buff *skb) 1468{ 1469 bool is_zcopy = skb && skb_shinfo(skb)->flags & SKBFL_ZEROCOPY_ENABLE; 1470 1471 return is_zcopy ? skb_uarg(skb) : NULL; 1472} 1473 1474static inline bool skb_zcopy_pure(const struct sk_buff *skb) 1475{ 1476 return skb_shinfo(skb)->flags & SKBFL_PURE_ZEROCOPY; 1477} 1478 1479static inline bool skb_pure_zcopy_same(const struct sk_buff *skb1, 1480 const struct sk_buff *skb2) 1481{ 1482 return skb_zcopy_pure(skb1) == skb_zcopy_pure(skb2); 1483} 1484 1485static inline void net_zcopy_get(struct ubuf_info *uarg) 1486{ 1487 refcount_inc(&uarg->refcnt); 1488} 1489 1490static inline void skb_zcopy_init(struct sk_buff *skb, struct ubuf_info *uarg) 1491{ 1492 skb_shinfo(skb)->destructor_arg = uarg; 1493 skb_shinfo(skb)->flags |= uarg->flags; 1494} 1495 1496static inline void skb_zcopy_set(struct sk_buff *skb, struct ubuf_info *uarg, 1497 bool *have_ref) 1498{ 1499 if (skb && uarg && !skb_zcopy(skb)) { 1500 if (unlikely(have_ref && *have_ref)) 1501 *have_ref = false; 1502 else 1503 net_zcopy_get(uarg); 1504 skb_zcopy_init(skb, uarg); 1505 } 1506} 1507 1508static inline void skb_zcopy_set_nouarg(struct sk_buff *skb, void *val) 1509{ 1510 skb_shinfo(skb)->destructor_arg = (void *)((uintptr_t) val | 0x1UL); 1511 skb_shinfo(skb)->flags |= SKBFL_ZEROCOPY_FRAG; 1512} 1513 1514static inline bool skb_zcopy_is_nouarg(struct sk_buff *skb) 1515{ 1516 return (uintptr_t) skb_shinfo(skb)->destructor_arg & 0x1UL; 1517} 1518 1519static inline void *skb_zcopy_get_nouarg(struct sk_buff *skb) 1520{ 1521 return (void *)((uintptr_t) skb_shinfo(skb)->destructor_arg & ~0x1UL); 1522} 1523 1524static inline void net_zcopy_put(struct ubuf_info *uarg) 1525{ 1526 if (uarg) 1527 uarg->callback(NULL, uarg, true); 1528} 1529 1530static inline void net_zcopy_put_abort(struct ubuf_info *uarg, bool have_uref) 1531{ 1532 if (uarg) { 1533 if (uarg->callback == msg_zerocopy_callback) 1534 msg_zerocopy_put_abort(uarg, have_uref); 1535 else if (have_uref) 1536 net_zcopy_put(uarg); 1537 } 1538} 1539 1540/* Release a reference on a zerocopy structure */ 1541static inline void skb_zcopy_clear(struct sk_buff *skb, bool zerocopy_success) 1542{ 1543 struct ubuf_info *uarg = skb_zcopy(skb); 1544 1545 if (uarg) { 1546 if (!skb_zcopy_is_nouarg(skb)) 1547 uarg->callback(skb, uarg, zerocopy_success); 1548 1549 skb_shinfo(skb)->flags &= ~SKBFL_ALL_ZEROCOPY; 1550 } 1551} 1552 1553static inline void skb_mark_not_on_list(struct sk_buff *skb) 1554{ 1555 skb->next = NULL; 1556} 1557 1558/* Iterate through singly-linked GSO fragments of an skb. */ 1559#define skb_list_walk_safe(first, skb, next_skb) \ 1560 for ((skb) = (first), (next_skb) = (skb) ? (skb)->next : NULL; (skb); \ 1561 (skb) = (next_skb), (next_skb) = (skb) ? (skb)->next : NULL) 1562 1563static inline void skb_list_del_init(struct sk_buff *skb) 1564{ 1565 __list_del_entry(&skb->list); 1566 skb_mark_not_on_list(skb); 1567} 1568 1569/** 1570 * skb_queue_empty - check if a queue is empty 1571 * @list: queue head 1572 * 1573 * Returns true if the queue is empty, false otherwise. 1574 */ 1575static inline int skb_queue_empty(const struct sk_buff_head *list) 1576{ 1577 return list->next == (const struct sk_buff *) list; 1578} 1579 1580/** 1581 * skb_queue_empty_lockless - check if a queue is empty 1582 * @list: queue head 1583 * 1584 * Returns true if the queue is empty, false otherwise. 1585 * This variant can be used in lockless contexts. 1586 */ 1587static inline bool skb_queue_empty_lockless(const struct sk_buff_head *list) 1588{ 1589 return READ_ONCE(list->next) == (const struct sk_buff *) list; 1590} 1591 1592 1593/** 1594 * skb_queue_is_last - check if skb is the last entry in the queue 1595 * @list: queue head 1596 * @skb: buffer 1597 * 1598 * Returns true if @skb is the last buffer on the list. 1599 */ 1600static inline bool skb_queue_is_last(const struct sk_buff_head *list, 1601 const struct sk_buff *skb) 1602{ 1603 return skb->next == (const struct sk_buff *) list; 1604} 1605 1606/** 1607 * skb_queue_is_first - check if skb is the first entry in the queue 1608 * @list: queue head 1609 * @skb: buffer 1610 * 1611 * Returns true if @skb is the first buffer on the list. 1612 */ 1613static inline bool skb_queue_is_first(const struct sk_buff_head *list, 1614 const struct sk_buff *skb) 1615{ 1616 return skb->prev == (const struct sk_buff *) list; 1617} 1618 1619/** 1620 * skb_queue_next - return the next packet in the queue 1621 * @list: queue head 1622 * @skb: current buffer 1623 * 1624 * Return the next packet in @list after @skb. It is only valid to 1625 * call this if skb_queue_is_last() evaluates to false. 1626 */ 1627static inline struct sk_buff *skb_queue_next(const struct sk_buff_head *list, 1628 const struct sk_buff *skb) 1629{ 1630 /* This BUG_ON may seem severe, but if we just return then we 1631 * are going to dereference garbage. 1632 */ 1633 BUG_ON(skb_queue_is_last(list, skb)); 1634 return skb->next; 1635} 1636 1637/** 1638 * skb_queue_prev - return the prev packet in the queue 1639 * @list: queue head 1640 * @skb: current buffer 1641 * 1642 * Return the prev packet in @list before @skb. It is only valid to 1643 * call this if skb_queue_is_first() evaluates to false. 1644 */ 1645static inline struct sk_buff *skb_queue_prev(const struct sk_buff_head *list, 1646 const struct sk_buff *skb) 1647{ 1648 /* This BUG_ON may seem severe, but if we just return then we 1649 * are going to dereference garbage. 1650 */ 1651 BUG_ON(skb_queue_is_first(list, skb)); 1652 return skb->prev; 1653} 1654 1655/** 1656 * skb_get - reference buffer 1657 * @skb: buffer to reference 1658 * 1659 * Makes another reference to a socket buffer and returns a pointer 1660 * to the buffer. 1661 */ 1662static inline struct sk_buff *skb_get(struct sk_buff *skb) 1663{ 1664 refcount_inc(&skb->users); 1665 return skb; 1666} 1667 1668/* 1669 * If users == 1, we are the only owner and can avoid redundant atomic changes. 1670 */ 1671 1672/** 1673 * skb_cloned - is the buffer a clone 1674 * @skb: buffer to check 1675 * 1676 * Returns true if the buffer was generated with skb_clone() and is 1677 * one of multiple shared copies of the buffer. Cloned buffers are 1678 * shared data so must not be written to under normal circumstances. 1679 */ 1680static inline int skb_cloned(const struct sk_buff *skb) 1681{ 1682 return skb->cloned && 1683 (atomic_read(&skb_shinfo(skb)->dataref) & SKB_DATAREF_MASK) != 1; 1684} 1685 1686static inline int skb_unclone(struct sk_buff *skb, gfp_t pri) 1687{ 1688 might_sleep_if(gfpflags_allow_blocking(pri)); 1689 1690 if (skb_cloned(skb)) 1691 return pskb_expand_head(skb, 0, 0, pri); 1692 1693 return 0; 1694} 1695 1696/* This variant of skb_unclone() makes sure skb->truesize is not changed */ 1697static inline int skb_unclone_keeptruesize(struct sk_buff *skb, gfp_t pri) 1698{ 1699 might_sleep_if(gfpflags_allow_blocking(pri)); 1700 1701 if (skb_cloned(skb)) { 1702 unsigned int save = skb->truesize; 1703 int res; 1704 1705 res = pskb_expand_head(skb, 0, 0, pri); 1706 skb->truesize = save; 1707 return res; 1708 } 1709 return 0; 1710} 1711 1712/** 1713 * skb_header_cloned - is the header a clone 1714 * @skb: buffer to check 1715 * 1716 * Returns true if modifying the header part of the buffer requires 1717 * the data to be copied. 1718 */ 1719static inline int skb_header_cloned(const struct sk_buff *skb) 1720{ 1721 int dataref; 1722 1723 if (!skb->cloned) 1724 return 0; 1725 1726 dataref = atomic_read(&skb_shinfo(skb)->dataref); 1727 dataref = (dataref & SKB_DATAREF_MASK) - (dataref >> SKB_DATAREF_SHIFT); 1728 return dataref != 1; 1729} 1730 1731static inline int skb_header_unclone(struct sk_buff *skb, gfp_t pri) 1732{ 1733 might_sleep_if(gfpflags_allow_blocking(pri)); 1734 1735 if (skb_header_cloned(skb)) 1736 return pskb_expand_head(skb, 0, 0, pri); 1737 1738 return 0; 1739} 1740 1741/** 1742 * __skb_header_release - release reference to header 1743 * @skb: buffer to operate on 1744 */ 1745static inline void __skb_header_release(struct sk_buff *skb) 1746{ 1747 skb->nohdr = 1; 1748 atomic_set(&skb_shinfo(skb)->dataref, 1 + (1 << SKB_DATAREF_SHIFT)); 1749} 1750 1751 1752/** 1753 * skb_shared - is the buffer shared 1754 * @skb: buffer to check 1755 * 1756 * Returns true if more than one person has a reference to this 1757 * buffer. 1758 */ 1759static inline int skb_shared(const struct sk_buff *skb) 1760{ 1761 return refcount_read(&skb->users) != 1; 1762} 1763 1764/** 1765 * skb_share_check - check if buffer is shared and if so clone it 1766 * @skb: buffer to check 1767 * @pri: priority for memory allocation 1768 * 1769 * If the buffer is shared the buffer is cloned and the old copy 1770 * drops a reference. A new clone with a single reference is returned. 1771 * If the buffer is not shared the original buffer is returned. When 1772 * being called from interrupt status or with spinlocks held pri must 1773 * be GFP_ATOMIC. 1774 * 1775 * NULL is returned on a memory allocation failure. 1776 */ 1777static inline struct sk_buff *skb_share_check(struct sk_buff *skb, gfp_t pri) 1778{ 1779 might_sleep_if(gfpflags_allow_blocking(pri)); 1780 if (skb_shared(skb)) { 1781 struct sk_buff *nskb = skb_clone(skb, pri); 1782 1783 if (likely(nskb)) 1784 consume_skb(skb); 1785 else 1786 kfree_skb(skb); 1787 skb = nskb; 1788 } 1789 return skb; 1790} 1791 1792/* 1793 * Copy shared buffers into a new sk_buff. We effectively do COW on 1794 * packets to handle cases where we have a local reader and forward 1795 * and a couple of other messy ones. The normal one is tcpdumping 1796 * a packet thats being forwarded. 1797 */ 1798 1799/** 1800 * skb_unshare - make a copy of a shared buffer 1801 * @skb: buffer to check 1802 * @pri: priority for memory allocation 1803 * 1804 * If the socket buffer is a clone then this function creates a new 1805 * copy of the data, drops a reference count on the old copy and returns 1806 * the new copy with the reference count at 1. If the buffer is not a clone 1807 * the original buffer is returned. When called with a spinlock held or 1808 * from interrupt state @pri must be %GFP_ATOMIC 1809 * 1810 * %NULL is returned on a memory allocation failure. 1811 */ 1812static inline struct sk_buff *skb_unshare(struct sk_buff *skb, 1813 gfp_t pri) 1814{ 1815 might_sleep_if(gfpflags_allow_blocking(pri)); 1816 if (skb_cloned(skb)) { 1817 struct sk_buff *nskb = skb_copy(skb, pri); 1818 1819 /* Free our shared copy */ 1820 if (likely(nskb)) 1821 consume_skb(skb); 1822 else 1823 kfree_skb(skb); 1824 skb = nskb; 1825 } 1826 return skb; 1827} 1828 1829/** 1830 * skb_peek - peek at the head of an &sk_buff_head 1831 * @list_: list to peek at 1832 * 1833 * Peek an &sk_buff. Unlike most other operations you _MUST_ 1834 * be careful with this one. A peek leaves the buffer on the 1835 * list and someone else may run off with it. You must hold 1836 * the appropriate locks or have a private queue to do this. 1837 * 1838 * Returns %NULL for an empty list or a pointer to the head element. 1839 * The reference count is not incremented and the reference is therefore 1840 * volatile. Use with caution. 1841 */ 1842static inline struct sk_buff *skb_peek(const struct sk_buff_head *list_) 1843{ 1844 struct sk_buff *skb = list_->next; 1845 1846 if (skb == (struct sk_buff *)list_) 1847 skb = NULL; 1848 return skb; 1849} 1850 1851/** 1852 * __skb_peek - peek at the head of a non-empty &sk_buff_head 1853 * @list_: list to peek at 1854 * 1855 * Like skb_peek(), but the caller knows that the list is not empty. 1856 */ 1857static inline struct sk_buff *__skb_peek(const struct sk_buff_head *list_) 1858{ 1859 return list_->next; 1860} 1861 1862/** 1863 * skb_peek_next - peek skb following the given one from a queue 1864 * @skb: skb to start from 1865 * @list_: list to peek at 1866 * 1867 * Returns %NULL when the end of the list is met or a pointer to the 1868 * next element. The reference count is not incremented and the 1869 * reference is therefore volatile. Use with caution. 1870 */ 1871static inline struct sk_buff *skb_peek_next(struct sk_buff *skb, 1872 const struct sk_buff_head *list_) 1873{ 1874 struct sk_buff *next = skb->next; 1875 1876 if (next == (struct sk_buff *)list_) 1877 next = NULL; 1878 return next; 1879} 1880 1881/** 1882 * skb_peek_tail - peek at the tail of an &sk_buff_head 1883 * @list_: list to peek at 1884 * 1885 * Peek an &sk_buff. Unlike most other operations you _MUST_ 1886 * be careful with this one. A peek leaves the buffer on the 1887 * list and someone else may run off with it. You must hold 1888 * the appropriate locks or have a private queue to do this. 1889 * 1890 * Returns %NULL for an empty list or a pointer to the tail element. 1891 * The reference count is not incremented and the reference is therefore 1892 * volatile. Use with caution. 1893 */ 1894static inline struct sk_buff *skb_peek_tail(const struct sk_buff_head *list_) 1895{ 1896 struct sk_buff *skb = READ_ONCE(list_->prev); 1897 1898 if (skb == (struct sk_buff *)list_) 1899 skb = NULL; 1900 return skb; 1901 1902} 1903 1904/** 1905 * skb_queue_len - get queue length 1906 * @list_: list to measure 1907 * 1908 * Return the length of an &sk_buff queue. 1909 */ 1910static inline __u32 skb_queue_len(const struct sk_buff_head *list_) 1911{ 1912 return list_->qlen; 1913} 1914 1915/** 1916 * skb_queue_len_lockless - get queue length 1917 * @list_: list to measure 1918 * 1919 * Return the length of an &sk_buff queue. 1920 * This variant can be used in lockless contexts. 1921 */ 1922static inline __u32 skb_queue_len_lockless(const struct sk_buff_head *list_) 1923{ 1924 return READ_ONCE(list_->qlen); 1925} 1926 1927/** 1928 * __skb_queue_head_init - initialize non-spinlock portions of sk_buff_head 1929 * @list: queue to initialize 1930 * 1931 * This initializes only the list and queue length aspects of 1932 * an sk_buff_head object. This allows to initialize the list 1933 * aspects of an sk_buff_head without reinitializing things like 1934 * the spinlock. It can also be used for on-stack sk_buff_head 1935 * objects where the spinlock is known to not be used. 1936 */ 1937static inline void __skb_queue_head_init(struct sk_buff_head *list) 1938{ 1939 list->prev = list->next = (struct sk_buff *)list; 1940 list->qlen = 0; 1941} 1942 1943/* 1944 * This function creates a split out lock class for each invocation; 1945 * this is needed for now since a whole lot of users of the skb-queue 1946 * infrastructure in drivers have different locking usage (in hardirq) 1947 * than the networking core (in softirq only). In the long run either the 1948 * network layer or drivers should need annotation to consolidate the 1949 * main types of usage into 3 classes. 1950 */ 1951static inline void skb_queue_head_init(struct sk_buff_head *list) 1952{ 1953 spin_lock_init(&list->lock); 1954 __skb_queue_head_init(list); 1955} 1956 1957static inline void skb_queue_head_init_class(struct sk_buff_head *list, 1958 struct lock_class_key *class) 1959{ 1960 skb_queue_head_init(list); 1961 lockdep_set_class(&list->lock, class); 1962} 1963 1964/* 1965 * Insert an sk_buff on a list. 1966 * 1967 * The "__skb_xxxx()" functions are the non-atomic ones that 1968 * can only be called with interrupts disabled. 1969 */ 1970static inline void __skb_insert(struct sk_buff *newsk, 1971 struct sk_buff *prev, struct sk_buff *next, 1972 struct sk_buff_head *list) 1973{ 1974 /* See skb_queue_empty_lockless() and skb_peek_tail() 1975 * for the opposite READ_ONCE() 1976 */ 1977 WRITE_ONCE(newsk->next, next); 1978 WRITE_ONCE(newsk->prev, prev); 1979 WRITE_ONCE(next->prev, newsk); 1980 WRITE_ONCE(prev->next, newsk); 1981 WRITE_ONCE(list->qlen, list->qlen + 1); 1982} 1983 1984static inline void __skb_queue_splice(const struct sk_buff_head *list, 1985 struct sk_buff *prev, 1986 struct sk_buff *next) 1987{ 1988 struct sk_buff *first = list->next; 1989 struct sk_buff *last = list->prev; 1990 1991 WRITE_ONCE(first->prev, prev); 1992 WRITE_ONCE(prev->next, first); 1993 1994 WRITE_ONCE(last->next, next); 1995 WRITE_ONCE(next->prev, last); 1996} 1997 1998/** 1999 * skb_queue_splice - join two skb lists, this is designed for stacks 2000 * @list: the new list to add 2001 * @head: the place to add it in the first list 2002 */ 2003static inline void skb_queue_splice(const struct sk_buff_head *list, 2004 struct sk_buff_head *head) 2005{ 2006 if (!skb_queue_empty(list)) { 2007 __skb_queue_splice(list, (struct sk_buff *) head, head->next); 2008 head->qlen += list->qlen; 2009 } 2010} 2011 2012/** 2013 * skb_queue_splice_init - join two skb lists and reinitialise the emptied list 2014 * @list: the new list to add 2015 * @head: the place to add it in the first list 2016 * 2017 * The list at @list is reinitialised 2018 */ 2019static inline void skb_queue_splice_init(struct sk_buff_head *list, 2020 struct sk_buff_head *head) 2021{ 2022 if (!skb_queue_empty(list)) { 2023 __skb_queue_splice(list, (struct sk_buff *) head, head->next); 2024 head->qlen += list->qlen; 2025 __skb_queue_head_init(list); 2026 } 2027} 2028 2029/** 2030 * skb_queue_splice_tail - join two skb lists, each list being a queue 2031 * @list: the new list to add 2032 * @head: the place to add it in the first list 2033 */ 2034static inline void skb_queue_splice_tail(const struct sk_buff_head *list, 2035 struct sk_buff_head *head) 2036{ 2037 if (!skb_queue_empty(list)) { 2038 __skb_queue_splice(list, head->prev, (struct sk_buff *) head); 2039 head->qlen += list->qlen; 2040 } 2041} 2042 2043/** 2044 * skb_queue_splice_tail_init - join two skb lists and reinitialise the emptied list 2045 * @list: the new list to add 2046 * @head: the place to add it in the first list 2047 * 2048 * Each of the lists is a queue. 2049 * The list at @list is reinitialised 2050 */ 2051static inline void skb_queue_splice_tail_init(struct sk_buff_head *list, 2052 struct sk_buff_head *head) 2053{ 2054 if (!skb_queue_empty(list)) { 2055 __skb_queue_splice(list, head->prev, (struct sk_buff *) head); 2056 head->qlen += list->qlen; 2057 __skb_queue_head_init(list); 2058 } 2059} 2060 2061/** 2062 * __skb_queue_after - queue a buffer at the list head 2063 * @list: list to use 2064 * @prev: place after this buffer 2065 * @newsk: buffer to queue 2066 * 2067 * Queue a buffer int the middle of a list. This function takes no locks 2068 * and you must therefore hold required locks before calling it. 2069 * 2070 * A buffer cannot be placed on two lists at the same time. 2071 */ 2072static inline void __skb_queue_after(struct sk_buff_head *list, 2073 struct sk_buff *prev, 2074 struct sk_buff *newsk) 2075{ 2076 __skb_insert(newsk, prev, prev->next, list); 2077} 2078 2079void skb_append(struct sk_buff *old, struct sk_buff *newsk, 2080 struct sk_buff_head *list); 2081 2082static inline void __skb_queue_before(struct sk_buff_head *list, 2083 struct sk_buff *next, 2084 struct sk_buff *newsk) 2085{ 2086 __skb_insert(newsk, next->prev, next, list); 2087} 2088 2089/** 2090 * __skb_queue_head - queue a buffer at the list head 2091 * @list: list to use 2092 * @newsk: buffer to queue 2093 * 2094 * Queue a buffer at the start of a list. This function takes no locks 2095 * and you must therefore hold required locks before calling it. 2096 * 2097 * A buffer cannot be placed on two lists at the same time. 2098 */ 2099static inline void __skb_queue_head(struct sk_buff_head *list, 2100 struct sk_buff *newsk) 2101{ 2102 __skb_queue_after(list, (struct sk_buff *)list, newsk); 2103} 2104void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk); 2105 2106/** 2107 * __skb_queue_tail - queue a buffer at the list tail 2108 * @list: list to use 2109 * @newsk: buffer to queue 2110 * 2111 * Queue a buffer at the end of a list. This function takes no locks 2112 * and you must therefore hold required locks before calling it. 2113 * 2114 * A buffer cannot be placed on two lists at the same time. 2115 */ 2116static inline void __skb_queue_tail(struct sk_buff_head *list, 2117 struct sk_buff *newsk) 2118{ 2119 __skb_queue_before(list, (struct sk_buff *)list, newsk); 2120} 2121void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk); 2122 2123/* 2124 * remove sk_buff from list. _Must_ be called atomically, and with 2125 * the list known.. 2126 */ 2127void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list); 2128static inline void __skb_unlink(struct sk_buff *skb, struct sk_buff_head *list) 2129{ 2130 struct sk_buff *next, *prev; 2131 2132 WRITE_ONCE(list->qlen, list->qlen - 1); 2133 next = skb->next; 2134 prev = skb->prev; 2135 skb->next = skb->prev = NULL; 2136 WRITE_ONCE(next->prev, prev); 2137 WRITE_ONCE(prev->next, next); 2138} 2139 2140/** 2141 * __skb_dequeue - remove from the head of the queue 2142 * @list: list to dequeue from 2143 * 2144 * Remove the head of the list. This function does not take any locks 2145 * so must be used with appropriate locks held only. The head item is 2146 * returned or %NULL if the list is empty. 2147 */ 2148static inline struct sk_buff *__skb_dequeue(struct sk_buff_head *list) 2149{ 2150 struct sk_buff *skb = skb_peek(list); 2151 if (skb) 2152 __skb_unlink(skb, list); 2153 return skb; 2154} 2155struct sk_buff *skb_dequeue(struct sk_buff_head *list); 2156 2157/** 2158 * __skb_dequeue_tail - remove from the tail of the queue 2159 * @list: list to dequeue from 2160 * 2161 * Remove the tail of the list. This function does not take any locks 2162 * so must be used with appropriate locks held only. The tail item is 2163 * returned or %NULL if the list is empty. 2164 */ 2165static inline struct sk_buff *__skb_dequeue_tail(struct sk_buff_head *list) 2166{ 2167 struct sk_buff *skb = skb_peek_tail(list); 2168 if (skb) 2169 __skb_unlink(skb, list); 2170 return skb; 2171} 2172struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list); 2173 2174 2175static inline bool skb_is_nonlinear(const struct sk_buff *skb) 2176{ 2177 return skb->data_len; 2178} 2179 2180static inline unsigned int skb_headlen(const struct sk_buff *skb) 2181{ 2182 return skb->len - skb->data_len; 2183} 2184 2185static inline unsigned int __skb_pagelen(const struct sk_buff *skb) 2186{ 2187 unsigned int i, len = 0; 2188 2189 for (i = skb_shinfo(skb)->nr_frags - 1; (int)i >= 0; i--) 2190 len += skb_frag_size(&skb_shinfo(skb)->frags[i]); 2191 return len; 2192} 2193 2194static inline unsigned int skb_pagelen(const struct sk_buff *skb) 2195{ 2196 return skb_headlen(skb) + __skb_pagelen(skb); 2197} 2198 2199/** 2200 * __skb_fill_page_desc - initialise a paged fragment in an skb 2201 * @skb: buffer containing fragment to be initialised 2202 * @i: paged fragment index to initialise 2203 * @page: the page to use for this fragment 2204 * @off: the offset to the data with @page 2205 * @size: the length of the data 2206 * 2207 * Initialises the @i'th fragment of @skb to point to &size bytes at 2208 * offset @off within @page. 2209 * 2210 * Does not take any additional reference on the fragment. 2211 */ 2212static inline void __skb_fill_page_desc(struct sk_buff *skb, int i, 2213 struct page *page, int off, int size) 2214{ 2215 skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 2216 2217 /* 2218 * Propagate page pfmemalloc to the skb if we can. The problem is 2219 * that not all callers have unique ownership of the page but rely 2220 * on page_is_pfmemalloc doing the right thing(tm). 2221 */ 2222 frag->bv_page = page; 2223 frag->bv_offset = off; 2224 skb_frag_size_set(frag, size); 2225 2226 page = compound_head(page); 2227 if (page_is_pfmemalloc(page)) 2228 skb->pfmemalloc = true; 2229} 2230 2231/** 2232 * skb_fill_page_desc - initialise a paged fragment in an skb 2233 * @skb: buffer containing fragment to be initialised 2234 * @i: paged fragment index to initialise 2235 * @page: the page to use for this fragment 2236 * @off: the offset to the data with @page 2237 * @size: the length of the data 2238 * 2239 * As per __skb_fill_page_desc() -- initialises the @i'th fragment of 2240 * @skb to point to @size bytes at offset @off within @page. In 2241 * addition updates @skb such that @i is the last fragment. 2242 * 2243 * Does not take any additional reference on the fragment. 2244 */ 2245static inline void skb_fill_page_desc(struct sk_buff *skb, int i, 2246 struct page *page, int off, int size) 2247{ 2248 __skb_fill_page_desc(skb, i, page, off, size); 2249 skb_shinfo(skb)->nr_frags = i + 1; 2250} 2251 2252void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off, 2253 int size, unsigned int truesize); 2254 2255void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size, 2256 unsigned int truesize); 2257 2258#define SKB_LINEAR_ASSERT(skb) BUG_ON(skb_is_nonlinear(skb)) 2259 2260#ifdef NET_SKBUFF_DATA_USES_OFFSET 2261static inline unsigned char *skb_tail_pointer(const struct sk_buff *skb) 2262{ 2263 return skb->head + skb->tail; 2264} 2265 2266static inline void skb_reset_tail_pointer(struct sk_buff *skb) 2267{ 2268 skb->tail = skb->data - skb->head; 2269} 2270 2271static inline void skb_set_tail_pointer(struct sk_buff *skb, const int offset) 2272{ 2273 skb_reset_tail_pointer(skb); 2274 skb->tail += offset; 2275} 2276 2277#else /* NET_SKBUFF_DATA_USES_OFFSET */ 2278static inline unsigned char *skb_tail_pointer(const struct sk_buff *skb) 2279{ 2280 return skb->tail; 2281} 2282 2283static inline void skb_reset_tail_pointer(struct sk_buff *skb) 2284{ 2285 skb->tail = skb->data; 2286} 2287 2288static inline void skb_set_tail_pointer(struct sk_buff *skb, const int offset) 2289{ 2290 skb->tail = skb->data + offset; 2291} 2292 2293#endif /* NET_SKBUFF_DATA_USES_OFFSET */ 2294 2295/* 2296 * Add data to an sk_buff 2297 */ 2298void *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len); 2299void *skb_put(struct sk_buff *skb, unsigned int len); 2300static inline void *__skb_put(struct sk_buff *skb, unsigned int len) 2301{ 2302 void *tmp = skb_tail_pointer(skb); 2303 SKB_LINEAR_ASSERT(skb); 2304 skb->tail += len; 2305 skb->len += len; 2306 return tmp; 2307} 2308 2309static inline void *__skb_put_zero(struct sk_buff *skb, unsigned int len) 2310{ 2311 void *tmp = __skb_put(skb, len); 2312 2313 memset(tmp, 0, len); 2314 return tmp; 2315} 2316 2317static inline void *__skb_put_data(struct sk_buff *skb, const void *data, 2318 unsigned int len) 2319{ 2320 void *tmp = __skb_put(skb, len); 2321 2322 memcpy(tmp, data, len); 2323 return tmp; 2324} 2325 2326static inline void __skb_put_u8(struct sk_buff *skb, u8 val) 2327{ 2328 *(u8 *)__skb_put(skb, 1) = val; 2329} 2330 2331static inline void *skb_put_zero(struct sk_buff *skb, unsigned int len) 2332{ 2333 void *tmp = skb_put(skb, len); 2334 2335 memset(tmp, 0, len); 2336 2337 return tmp; 2338} 2339 2340static inline void *skb_put_data(struct sk_buff *skb, const void *data, 2341 unsigned int len) 2342{ 2343 void *tmp = skb_put(skb, len); 2344 2345 memcpy(tmp, data, len); 2346 2347 return tmp; 2348} 2349 2350static inline void skb_put_u8(struct sk_buff *skb, u8 val) 2351{ 2352 *(u8 *)skb_put(skb, 1) = val; 2353} 2354 2355void *skb_push(struct sk_buff *skb, unsigned int len); 2356static inline void *__skb_push(struct sk_buff *skb, unsigned int len) 2357{ 2358 skb->data -= len; 2359 skb->len += len; 2360 return skb->data; 2361} 2362 2363void *skb_pull(struct sk_buff *skb, unsigned int len); 2364static inline void *__skb_pull(struct sk_buff *skb, unsigned int len) 2365{ 2366 skb->len -= len; 2367 BUG_ON(skb->len < skb->data_len); 2368 return skb->data += len; 2369} 2370 2371static inline void *skb_pull_inline(struct sk_buff *skb, unsigned int len) 2372{ 2373 return unlikely(len > skb->len) ? NULL : __skb_pull(skb, len); 2374} 2375 2376void *__pskb_pull_tail(struct sk_buff *skb, int delta); 2377 2378static inline void *__pskb_pull(struct sk_buff *skb, unsigned int len) 2379{ 2380 if (len > skb_headlen(skb) && 2381 !__pskb_pull_tail(skb, len - skb_headlen(skb))) 2382 return NULL; 2383 skb->len -= len; 2384 return skb->data += len; 2385} 2386 2387static inline void *pskb_pull(struct sk_buff *skb, unsigned int len) 2388{ 2389 return unlikely(len > skb->len) ? NULL : __pskb_pull(skb, len); 2390} 2391 2392static inline bool pskb_may_pull(struct sk_buff *skb, unsigned int len) 2393{ 2394 if (likely(len <= skb_headlen(skb))) 2395 return true; 2396 if (unlikely(len > skb->len)) 2397 return false; 2398 return __pskb_pull_tail(skb, len - skb_headlen(skb)) != NULL; 2399} 2400 2401void skb_condense(struct sk_buff *skb); 2402 2403/** 2404 * skb_headroom - bytes at buffer head 2405 * @skb: buffer to check 2406 * 2407 * Return the number of bytes of free space at the head of an &sk_buff. 2408 */ 2409static inline unsigned int skb_headroom(const struct sk_buff *skb) 2410{ 2411 return skb->data - skb->head; 2412} 2413 2414/** 2415 * skb_tailroom - bytes at buffer end 2416 * @skb: buffer to check 2417 * 2418 * Return the number of bytes of free space at the tail of an sk_buff 2419 */ 2420static inline int skb_tailroom(const struct sk_buff *skb) 2421{ 2422 return skb_is_nonlinear(skb) ? 0 : skb->end - skb->tail; 2423} 2424 2425/** 2426 * skb_availroom - bytes at buffer end 2427 * @skb: buffer to check 2428 * 2429 * Return the number of bytes of free space at the tail of an sk_buff 2430 * allocated by sk_stream_alloc() 2431 */ 2432static inline int skb_availroom(const struct sk_buff *skb) 2433{ 2434 if (skb_is_nonlinear(skb)) 2435 return 0; 2436 2437 return skb->end - skb->tail - skb->reserved_tailroom; 2438} 2439 2440/** 2441 * skb_reserve - adjust headroom 2442 * @skb: buffer to alter 2443 * @len: bytes to move 2444 * 2445 * Increase the headroom of an empty &sk_buff by reducing the tail 2446 * room. This is only allowed for an empty buffer. 2447 */ 2448static inline void skb_reserve(struct sk_buff *skb, int len) 2449{ 2450 skb->data += len; 2451 skb->tail += len; 2452} 2453 2454/** 2455 * skb_tailroom_reserve - adjust reserved_tailroom 2456 * @skb: buffer to alter 2457 * @mtu: maximum amount of headlen permitted 2458 * @needed_tailroom: minimum amount of reserved_tailroom 2459 * 2460 * Set reserved_tailroom so that headlen can be as large as possible but 2461 * not larger than mtu and tailroom cannot be smaller than 2462 * needed_tailroom. 2463 * The required headroom should already have been reserved before using 2464 * this function. 2465 */ 2466static inline void skb_tailroom_reserve(struct sk_buff *skb, unsigned int mtu, 2467 unsigned int needed_tailroom) 2468{ 2469 SKB_LINEAR_ASSERT(skb); 2470 if (mtu < skb_tailroom(skb) - needed_tailroom) 2471 /* use at most mtu */ 2472 skb->reserved_tailroom = skb_tailroom(skb) - mtu; 2473 else 2474 /* use up to all available space */ 2475 skb->reserved_tailroom = needed_tailroom; 2476} 2477 2478#define ENCAP_TYPE_ETHER 0 2479#define ENCAP_TYPE_IPPROTO 1 2480 2481static inline void skb_set_inner_protocol(struct sk_buff *skb, 2482 __be16 protocol) 2483{ 2484 skb->inner_protocol = protocol; 2485 skb->inner_protocol_type = ENCAP_TYPE_ETHER; 2486} 2487 2488static inline void skb_set_inner_ipproto(struct sk_buff *skb, 2489 __u8 ipproto) 2490{ 2491 skb->inner_ipproto = ipproto; 2492 skb->inner_protocol_type = ENCAP_TYPE_IPPROTO; 2493} 2494 2495static inline void skb_reset_inner_headers(struct sk_buff *skb) 2496{ 2497 skb->inner_mac_header = skb->mac_header; 2498 skb->inner_network_header = skb->network_header; 2499 skb->inner_transport_header = skb->transport_header; 2500} 2501 2502static inline void skb_reset_mac_len(struct sk_buff *skb) 2503{ 2504 skb->mac_len = skb->network_header - skb->mac_header; 2505} 2506 2507static inline unsigned char *skb_inner_transport_header(const struct sk_buff 2508 *skb) 2509{ 2510 return skb->head + skb->inner_transport_header; 2511} 2512 2513static inline int skb_inner_transport_offset(const struct sk_buff *skb) 2514{ 2515 return skb_inner_transport_header(skb) - skb->data; 2516} 2517 2518static inline void skb_reset_inner_transport_header(struct sk_buff *skb) 2519{ 2520 skb->inner_transport_header = skb->data - skb->head; 2521} 2522 2523static inline void skb_set_inner_transport_header(struct sk_buff *skb, 2524 const int offset) 2525{ 2526 skb_reset_inner_transport_header(skb); 2527 skb->inner_transport_header += offset; 2528} 2529 2530static inline unsigned char *skb_inner_network_header(const struct sk_buff *skb) 2531{ 2532 return skb->head + skb->inner_network_header; 2533} 2534 2535static inline void skb_reset_inner_network_header(struct sk_buff *skb) 2536{ 2537 skb->inner_network_header = skb->data - skb->head; 2538} 2539 2540static inline void skb_set_inner_network_header(struct sk_buff *skb, 2541 const int offset) 2542{ 2543 skb_reset_inner_network_header(skb); 2544 skb->inner_network_header += offset; 2545} 2546 2547static inline unsigned char *skb_inner_mac_header(const struct sk_buff *skb) 2548{ 2549 return skb->head + skb->inner_mac_header; 2550} 2551 2552static inline void skb_reset_inner_mac_header(struct sk_buff *skb) 2553{ 2554 skb->inner_mac_header = skb->data - skb->head; 2555} 2556 2557static inline void skb_set_inner_mac_header(struct sk_buff *skb, 2558 const int offset) 2559{ 2560 skb_reset_inner_mac_header(skb); 2561 skb->inner_mac_header += offset; 2562} 2563static inline bool skb_transport_header_was_set(const struct sk_buff *skb) 2564{ 2565 return skb->transport_header != (typeof(skb->transport_header))~0U; 2566} 2567 2568static inline unsigned char *skb_transport_header(const struct sk_buff *skb) 2569{ 2570 return skb->head + skb->transport_header; 2571} 2572 2573static inline void skb_reset_transport_header(struct sk_buff *skb) 2574{ 2575 skb->transport_header = skb->data - skb->head; 2576} 2577 2578static inline void skb_set_transport_header(struct sk_buff *skb, 2579 const int offset) 2580{ 2581 skb_reset_transport_header(skb); 2582 skb->transport_header += offset; 2583} 2584 2585static inline unsigned char *skb_network_header(const struct sk_buff *skb) 2586{ 2587 return skb->head + skb->network_header; 2588} 2589 2590static inline void skb_reset_network_header(struct sk_buff *skb) 2591{ 2592 skb->network_header = skb->data - skb->head; 2593} 2594 2595static inline void skb_set_network_header(struct sk_buff *skb, const int offset) 2596{ 2597 skb_reset_network_header(skb); 2598 skb->network_header += offset; 2599} 2600 2601static inline unsigned char *skb_mac_header(const struct sk_buff *skb) 2602{ 2603 return skb->head + skb->mac_header; 2604} 2605 2606static inline int skb_mac_offset(const struct sk_buff *skb) 2607{ 2608 return skb_mac_header(skb) - skb->data; 2609} 2610 2611static inline u32 skb_mac_header_len(const struct sk_buff *skb) 2612{ 2613 return skb->network_header - skb->mac_header; 2614} 2615 2616static inline int skb_mac_header_was_set(const struct sk_buff *skb) 2617{ 2618 return skb->mac_header != (typeof(skb->mac_header))~0U; 2619} 2620 2621static inline void skb_unset_mac_header(struct sk_buff *skb) 2622{ 2623 skb->mac_header = (typeof(skb->mac_header))~0U; 2624} 2625 2626static inline void skb_reset_mac_header(struct sk_buff *skb) 2627{ 2628 skb->mac_header = skb->data - skb->head; 2629} 2630 2631static inline void skb_set_mac_header(struct sk_buff *skb, const int offset) 2632{ 2633 skb_reset_mac_header(skb); 2634 skb->mac_header += offset; 2635} 2636 2637static inline void skb_pop_mac_header(struct sk_buff *skb) 2638{ 2639 skb->mac_header = skb->network_header; 2640} 2641 2642static inline void skb_probe_transport_header(struct sk_buff *skb) 2643{ 2644 struct flow_keys_basic keys; 2645 2646 if (skb_transport_header_was_set(skb)) 2647 return; 2648 2649 if (skb_flow_dissect_flow_keys_basic(NULL, skb, &keys, 2650 NULL, 0, 0, 0, 0)) 2651 skb_set_transport_header(skb, keys.control.thoff); 2652} 2653 2654static inline void skb_mac_header_rebuild(struct sk_buff *skb) 2655{ 2656 if (skb_mac_header_was_set(skb)) { 2657 const unsigned char *old_mac = skb_mac_header(skb); 2658 2659 skb_set_mac_header(skb, -skb->mac_len); 2660 memmove(skb_mac_header(skb), old_mac, skb->mac_len); 2661 } 2662} 2663 2664static inline int skb_checksum_start_offset(const struct sk_buff *skb) 2665{ 2666 return skb->csum_start - skb_headroom(skb); 2667} 2668 2669static inline unsigned char *skb_checksum_start(const struct sk_buff *skb) 2670{ 2671 return skb->head + skb->csum_start; 2672} 2673 2674static inline int skb_transport_offset(const struct sk_buff *skb) 2675{ 2676 return skb_transport_header(skb) - skb->data; 2677} 2678 2679static inline u32 skb_network_header_len(const struct sk_buff *skb) 2680{ 2681 return skb->transport_header - skb->network_header; 2682} 2683 2684static inline u32 skb_inner_network_header_len(const struct sk_buff *skb) 2685{ 2686 return skb->inner_transport_header - skb->inner_network_header; 2687} 2688 2689static inline int skb_network_offset(const struct sk_buff *skb) 2690{ 2691 return skb_network_header(skb) - skb->data; 2692} 2693 2694static inline int skb_inner_network_offset(const struct sk_buff *skb) 2695{ 2696 return skb_inner_network_header(skb) - skb->data; 2697} 2698 2699static inline int pskb_network_may_pull(struct sk_buff *skb, unsigned int len) 2700{ 2701 return pskb_may_pull(skb, skb_network_offset(skb) + len); 2702} 2703 2704/* 2705 * CPUs often take a performance hit when accessing unaligned memory 2706 * locations. The actual performance hit varies, it can be small if the 2707 * hardware handles it or large if we have to take an exception and fix it 2708 * in software. 2709 * 2710 * Since an ethernet header is 14 bytes network drivers often end up with 2711 * the IP header at an unaligned offset. The IP header can be aligned by 2712 * shifting the start of the packet by 2 bytes. Drivers should do this 2713 * with: 2714 * 2715 * skb_reserve(skb, NET_IP_ALIGN); 2716 * 2717 * The downside to this alignment of the IP header is that the DMA is now 2718 * unaligned. On some architectures the cost of an unaligned DMA is high 2719 * and this cost outweighs the gains made by aligning the IP header. 2720 * 2721 * Since this trade off varies between architectures, we allow NET_IP_ALIGN 2722 * to be overridden. 2723 */ 2724#ifndef NET_IP_ALIGN 2725#define NET_IP_ALIGN 2 2726#endif 2727 2728/* 2729 * The networking layer reserves some headroom in skb data (via 2730 * dev_alloc_skb). This is used to avoid having to reallocate skb data when 2731 * the header has to grow. In the default case, if the header has to grow 2732 * 32 bytes or less we avoid the reallocation. 2733 * 2734 * Unfortunately this headroom changes the DMA alignment of the resulting 2735 * network packet. As for NET_IP_ALIGN, this unaligned DMA is expensive 2736 * on some architectures. An architecture can override this value, 2737 * perhaps setting it to a cacheline in size (since that will maintain 2738 * cacheline alignment of the DMA). It must be a power of 2. 2739 * 2740 * Various parts of the networking layer expect at least 32 bytes of 2741 * headroom, you should not reduce this. 2742 * 2743 * Using max(32, L1_CACHE_BYTES) makes sense (especially with RPS) 2744 * to reduce average number of cache lines per packet. 2745 * get_rps_cpu() for example only access one 64 bytes aligned block : 2746 * NET_IP_ALIGN(2) + ethernet_header(14) + IP_header(20/40) + ports(8) 2747 */ 2748#ifndef NET_SKB_PAD 2749#define NET_SKB_PAD max(32, L1_CACHE_BYTES) 2750#endif 2751 2752int ___pskb_trim(struct sk_buff *skb, unsigned int len); 2753 2754static inline void __skb_set_length(struct sk_buff *skb, unsigned int len) 2755{ 2756 if (WARN_ON(skb_is_nonlinear(skb))) 2757 return; 2758 skb->len = len; 2759 skb_set_tail_pointer(skb, len); 2760} 2761 2762static inline void __skb_trim(struct sk_buff *skb, unsigned int len) 2763{ 2764 __skb_set_length(skb, len); 2765} 2766 2767void skb_trim(struct sk_buff *skb, unsigned int len); 2768 2769static inline int __pskb_trim(struct sk_buff *skb, unsigned int len) 2770{ 2771 if (skb->data_len) 2772 return ___pskb_trim(skb, len); 2773 __skb_trim(skb, len); 2774 return 0; 2775} 2776 2777static inline int pskb_trim(struct sk_buff *skb, unsigned int len) 2778{ 2779 return (len < skb->len) ? __pskb_trim(skb, len) : 0; 2780} 2781 2782/** 2783 * pskb_trim_unique - remove end from a paged unique (not cloned) buffer 2784 * @skb: buffer to alter 2785 * @len: new length 2786 * 2787 * This is identical to pskb_trim except that the caller knows that 2788 * the skb is not cloned so we should never get an error due to out- 2789 * of-memory. 2790 */ 2791static inline void pskb_trim_unique(struct sk_buff *skb, unsigned int len) 2792{ 2793 int err = pskb_trim(skb, len); 2794 BUG_ON(err); 2795} 2796 2797static inline int __skb_grow(struct sk_buff *skb, unsigned int len) 2798{ 2799 unsigned int diff = len - skb->len; 2800 2801 if (skb_tailroom(skb) < diff) { 2802 int ret = pskb_expand_head(skb, 0, diff - skb_tailroom(skb), 2803 GFP_ATOMIC); 2804 if (ret) 2805 return ret; 2806 } 2807 __skb_set_length(skb, len); 2808 return 0; 2809} 2810 2811/** 2812 * skb_orphan - orphan a buffer 2813 * @skb: buffer to orphan 2814 * 2815 * If a buffer currently has an owner then we call the owner's 2816 * destructor function and make the @skb unowned. The buffer continues 2817 * to exist but is no longer charged to its former owner. 2818 */ 2819static inline void skb_orphan(struct sk_buff *skb) 2820{ 2821 if (skb->destructor) { 2822 skb->destructor(skb); 2823 skb->destructor = NULL; 2824 skb->sk = NULL; 2825 } else { 2826 BUG_ON(skb->sk); 2827 } 2828} 2829 2830/** 2831 * skb_orphan_frags - orphan the frags contained in a buffer 2832 * @skb: buffer to orphan frags from 2833 * @gfp_mask: allocation mask for replacement pages 2834 * 2835 * For each frag in the SKB which needs a destructor (i.e. has an 2836 * owner) create a copy of that frag and release the original 2837 * page by calling the destructor. 2838 */ 2839static inline int skb_orphan_frags(struct sk_buff *skb, gfp_t gfp_mask) 2840{ 2841 if (likely(!skb_zcopy(skb))) 2842 return 0; 2843 if (!skb_zcopy_is_nouarg(skb) && 2844 skb_uarg(skb)->callback == msg_zerocopy_callback) 2845 return 0; 2846 return skb_copy_ubufs(skb, gfp_mask); 2847} 2848 2849/* Frags must be orphaned, even if refcounted, if skb might loop to rx path */ 2850static inline int skb_orphan_frags_rx(struct sk_buff *skb, gfp_t gfp_mask) 2851{ 2852 if (likely(!skb_zcopy(skb))) 2853 return 0; 2854 return skb_copy_ubufs(skb, gfp_mask); 2855} 2856 2857/** 2858 * __skb_queue_purge - empty a list 2859 * @list: list to empty 2860 * 2861 * Delete all buffers on an &sk_buff list. Each buffer is removed from 2862 * the list and one reference dropped. This function does not take the 2863 * list lock and the caller must hold the relevant locks to use it. 2864 */ 2865static inline void __skb_queue_purge(struct sk_buff_head *list) 2866{ 2867 struct sk_buff *skb; 2868 while ((skb = __skb_dequeue(list)) != NULL) 2869 kfree_skb(skb); 2870} 2871void skb_queue_purge(struct sk_buff_head *list); 2872 2873unsigned int skb_rbtree_purge(struct rb_root *root); 2874 2875void *__netdev_alloc_frag_align(unsigned int fragsz, unsigned int align_mask); 2876 2877/** 2878 * netdev_alloc_frag - allocate a page fragment 2879 * @fragsz: fragment size 2880 * 2881 * Allocates a frag from a page for receive buffer. 2882 * Uses GFP_ATOMIC allocations. 2883 */ 2884static inline void *netdev_alloc_frag(unsigned int fragsz) 2885{ 2886 return __netdev_alloc_frag_align(fragsz, ~0u); 2887} 2888 2889static inline void *netdev_alloc_frag_align(unsigned int fragsz, 2890 unsigned int align) 2891{ 2892 WARN_ON_ONCE(!is_power_of_2(align)); 2893 return __netdev_alloc_frag_align(fragsz, -align); 2894} 2895 2896struct sk_buff *__netdev_alloc_skb(struct net_device *dev, unsigned int length, 2897 gfp_t gfp_mask); 2898 2899/** 2900 * netdev_alloc_skb - allocate an skbuff for rx on a specific device 2901 * @dev: network device to receive on 2902 * @length: length to allocate 2903 * 2904 * Allocate a new &sk_buff and assign it a usage count of one. The 2905 * buffer has unspecified headroom built in. Users should allocate 2906 * the headroom they think they need without accounting for the 2907 * built in space. The built in space is used for optimisations. 2908 * 2909 * %NULL is returned if there is no free memory. Although this function 2910 * allocates memory it can be called from an interrupt. 2911 */ 2912static inline struct sk_buff *netdev_alloc_skb(struct net_device *dev, 2913 unsigned int length) 2914{ 2915 return __netdev_alloc_skb(dev, length, GFP_ATOMIC); 2916} 2917 2918/* legacy helper around __netdev_alloc_skb() */ 2919static inline struct sk_buff *__dev_alloc_skb(unsigned int length, 2920 gfp_t gfp_mask) 2921{ 2922 return __netdev_alloc_skb(NULL, length, gfp_mask); 2923} 2924 2925/* legacy helper around netdev_alloc_skb() */ 2926static inline struct sk_buff *dev_alloc_skb(unsigned int length) 2927{ 2928 return netdev_alloc_skb(NULL, length); 2929} 2930 2931 2932static inline struct sk_buff *__netdev_alloc_skb_ip_align(struct net_device *dev, 2933 unsigned int length, gfp_t gfp) 2934{ 2935 struct sk_buff *skb = __netdev_alloc_skb(dev, length + NET_IP_ALIGN, gfp); 2936 2937 if (NET_IP_ALIGN && skb) 2938 skb_reserve(skb, NET_IP_ALIGN); 2939 return skb; 2940} 2941 2942static inline struct sk_buff *netdev_alloc_skb_ip_align(struct net_device *dev, 2943 unsigned int length) 2944{ 2945 return __netdev_alloc_skb_ip_align(dev, length, GFP_ATOMIC); 2946} 2947 2948static inline void skb_free_frag(void *addr) 2949{ 2950 page_frag_free(addr); 2951} 2952 2953void *__napi_alloc_frag_align(unsigned int fragsz, unsigned int align_mask); 2954 2955static inline void *napi_alloc_frag(unsigned int fragsz) 2956{ 2957 return __napi_alloc_frag_align(fragsz, ~0u); 2958} 2959 2960static inline void *napi_alloc_frag_align(unsigned int fragsz, 2961 unsigned int align) 2962{ 2963 WARN_ON_ONCE(!is_power_of_2(align)); 2964 return __napi_alloc_frag_align(fragsz, -align); 2965} 2966 2967struct sk_buff *__napi_alloc_skb(struct napi_struct *napi, 2968 unsigned int length, gfp_t gfp_mask); 2969static inline struct sk_buff *napi_alloc_skb(struct napi_struct *napi, 2970 unsigned int length) 2971{ 2972 return __napi_alloc_skb(napi, length, GFP_ATOMIC); 2973} 2974void napi_consume_skb(struct sk_buff *skb, int budget); 2975 2976void napi_skb_free_stolen_head(struct sk_buff *skb); 2977void __kfree_skb_defer(struct sk_buff *skb); 2978 2979/** 2980 * __dev_alloc_pages - allocate page for network Rx 2981 * @gfp_mask: allocation priority. Set __GFP_NOMEMALLOC if not for network Rx 2982 * @order: size of the allocation 2983 * 2984 * Allocate a new page. 2985 * 2986 * %NULL is returned if there is no free memory. 2987*/ 2988static inline struct page *__dev_alloc_pages(gfp_t gfp_mask, 2989 unsigned int order) 2990{ 2991 /* This piece of code contains several assumptions. 2992 * 1. This is for device Rx, therefor a cold page is preferred. 2993 * 2. The expectation is the user wants a compound page. 2994 * 3. If requesting a order 0 page it will not be compound 2995 * due to the check to see if order has a value in prep_new_page 2996 * 4. __GFP_MEMALLOC is ignored if __GFP_NOMEMALLOC is set due to 2997 * code in gfp_to_alloc_flags that should be enforcing this. 2998 */ 2999 gfp_mask |= __GFP_COMP | __GFP_MEMALLOC; 3000 3001 return alloc_pages_node(NUMA_NO_NODE, gfp_mask, order); 3002} 3003 3004static inline struct page *dev_alloc_pages(unsigned int order) 3005{ 3006 return __dev_alloc_pages(GFP_ATOMIC | __GFP_NOWARN, order); 3007} 3008 3009/** 3010 * __dev_alloc_page - allocate a page for network Rx 3011 * @gfp_mask: allocation priority. Set __GFP_NOMEMALLOC if not for network Rx 3012 * 3013 * Allocate a new page. 3014 * 3015 * %NULL is returned if there is no free memory. 3016 */ 3017static inline struct page *__dev_alloc_page(gfp_t gfp_mask) 3018{ 3019 return __dev_alloc_pages(gfp_mask, 0); 3020} 3021 3022static inline struct page *dev_alloc_page(void) 3023{ 3024 return dev_alloc_pages(0); 3025} 3026 3027/** 3028 * dev_page_is_reusable - check whether a page can be reused for network Rx 3029 * @page: the page to test 3030 * 3031 * A page shouldn't be considered for reusing/recycling if it was allocated 3032 * under memory pressure or at a distant memory node. 3033 * 3034 * Returns false if this page should be returned to page allocator, true 3035 * otherwise. 3036 */ 3037static inline bool dev_page_is_reusable(const struct page *page) 3038{ 3039 return likely(page_to_nid(page) == numa_mem_id() && 3040 !page_is_pfmemalloc(page)); 3041} 3042 3043/** 3044 * skb_propagate_pfmemalloc - Propagate pfmemalloc if skb is allocated after RX page 3045 * @page: The page that was allocated from skb_alloc_page 3046 * @skb: The skb that may need pfmemalloc set 3047 */ 3048static inline void skb_propagate_pfmemalloc(const struct page *page, 3049 struct sk_buff *skb) 3050{ 3051 if (page_is_pfmemalloc(page)) 3052 skb->pfmemalloc = true; 3053} 3054 3055/** 3056 * skb_frag_off() - Returns the offset of a skb fragment 3057 * @frag: the paged fragment 3058 */ 3059static inline unsigned int skb_frag_off(const skb_frag_t *frag) 3060{ 3061 return frag->bv_offset; 3062} 3063 3064/** 3065 * skb_frag_off_add() - Increments the offset of a skb fragment by @delta 3066 * @frag: skb fragment 3067 * @delta: value to add 3068 */ 3069static inline void skb_frag_off_add(skb_frag_t *frag, int delta) 3070{ 3071 frag->bv_offset += delta; 3072} 3073 3074/** 3075 * skb_frag_off_set() - Sets the offset of a skb fragment 3076 * @frag: skb fragment 3077 * @offset: offset of fragment 3078 */ 3079static inline void skb_frag_off_set(skb_frag_t *frag, unsigned int offset) 3080{ 3081 frag->bv_offset = offset; 3082} 3083 3084/** 3085 * skb_frag_off_copy() - Sets the offset of a skb fragment from another fragment 3086 * @fragto: skb fragment where offset is set 3087 * @fragfrom: skb fragment offset is copied from 3088 */ 3089static inline void skb_frag_off_copy(skb_frag_t *fragto, 3090 const skb_frag_t *fragfrom) 3091{ 3092 fragto->bv_offset = fragfrom->bv_offset; 3093} 3094 3095/** 3096 * skb_frag_page - retrieve the page referred to by a paged fragment 3097 * @frag: the paged fragment 3098 * 3099 * Returns the &struct page associated with @frag. 3100 */ 3101static inline struct page *skb_frag_page(const skb_frag_t *frag) 3102{ 3103 return frag->bv_page; 3104} 3105 3106/** 3107 * __skb_frag_ref - take an addition reference on a paged fragment. 3108 * @frag: the paged fragment 3109 * 3110 * Takes an additional reference on the paged fragment @frag. 3111 */ 3112static inline void __skb_frag_ref(skb_frag_t *frag) 3113{ 3114 get_page(skb_frag_page(frag)); 3115} 3116 3117/** 3118 * skb_frag_ref - take an addition reference on a paged fragment of an skb. 3119 * @skb: the buffer 3120 * @f: the fragment offset. 3121 * 3122 * Takes an additional reference on the @f'th paged fragment of @skb. 3123 */ 3124static inline void skb_frag_ref(struct sk_buff *skb, int f) 3125{ 3126 __skb_frag_ref(&skb_shinfo(skb)->frags[f]); 3127} 3128 3129/** 3130 * __skb_frag_unref - release a reference on a paged fragment. 3131 * @frag: the paged fragment 3132 * @recycle: recycle the page if allocated via page_pool 3133 * 3134 * Releases a reference on the paged fragment @frag 3135 * or recycles the page via the page_pool API. 3136 */ 3137static inline void __skb_frag_unref(skb_frag_t *frag, bool recycle) 3138{ 3139 struct page *page = skb_frag_page(frag); 3140 3141#ifdef CONFIG_PAGE_POOL 3142 if (recycle && page_pool_return_skb_page(page)) 3143 return; 3144#endif 3145 put_page(page); 3146} 3147 3148/** 3149 * skb_frag_unref - release a reference on a paged fragment of an skb. 3150 * @skb: the buffer 3151 * @f: the fragment offset 3152 * 3153 * Releases a reference on the @f'th paged fragment of @skb. 3154 */ 3155static inline void skb_frag_unref(struct sk_buff *skb, int f) 3156{ 3157 __skb_frag_unref(&skb_shinfo(skb)->frags[f], skb->pp_recycle); 3158} 3159 3160/** 3161 * skb_frag_address - gets the address of the data contained in a paged fragment 3162 * @frag: the paged fragment buffer 3163 * 3164 * Returns the address of the data within @frag. The page must already 3165 * be mapped. 3166 */ 3167static inline void *skb_frag_address(const skb_frag_t *frag) 3168{ 3169 return page_address(skb_frag_page(frag)) + skb_frag_off(frag); 3170} 3171 3172/** 3173 * skb_frag_address_safe - gets the address of the data contained in a paged fragment 3174 * @frag: the paged fragment buffer 3175 * 3176 * Returns the address of the data within @frag. Checks that the page 3177 * is mapped and returns %NULL otherwise. 3178 */ 3179static inline void *skb_frag_address_safe(const skb_frag_t *frag) 3180{ 3181 void *ptr = page_address(skb_frag_page(frag)); 3182 if (unlikely(!ptr)) 3183 return NULL; 3184 3185 return ptr + skb_frag_off(frag); 3186} 3187 3188/** 3189 * skb_frag_page_copy() - sets the page in a fragment from another fragment 3190 * @fragto: skb fragment where page is set 3191 * @fragfrom: skb fragment page is copied from 3192 */ 3193static inline void skb_frag_page_copy(skb_frag_t *fragto, 3194 const skb_frag_t *fragfrom) 3195{ 3196 fragto->bv_page = fragfrom->bv_page; 3197} 3198 3199/** 3200 * __skb_frag_set_page - sets the page contained in a paged fragment 3201 * @frag: the paged fragment 3202 * @page: the page to set 3203 * 3204 * Sets the fragment @frag to contain @page. 3205 */ 3206static inline void __skb_frag_set_page(skb_frag_t *frag, struct page *page) 3207{ 3208 frag->bv_page = page; 3209} 3210 3211/** 3212 * skb_frag_set_page - sets the page contained in a paged fragment of an skb 3213 * @skb: the buffer 3214 * @f: the fragment offset 3215 * @page: the page to set 3216 * 3217 * Sets the @f'th fragment of @skb to contain @page. 3218 */ 3219static inline void skb_frag_set_page(struct sk_buff *skb, int f, 3220 struct page *page) 3221{ 3222 __skb_frag_set_page(&skb_shinfo(skb)->frags[f], page); 3223} 3224 3225bool skb_page_frag_refill(unsigned int sz, struct page_frag *pfrag, gfp_t prio); 3226 3227/** 3228 * skb_frag_dma_map - maps a paged fragment via the DMA API 3229 * @dev: the device to map the fragment to 3230 * @frag: the paged fragment to map 3231 * @offset: the offset within the fragment (starting at the 3232 * fragment's own offset) 3233 * @size: the number of bytes to map 3234 * @dir: the direction of the mapping (``PCI_DMA_*``) 3235 * 3236 * Maps the page associated with @frag to @device. 3237 */ 3238static inline dma_addr_t skb_frag_dma_map(struct device *dev, 3239 const skb_frag_t *frag, 3240 size_t offset, size_t size, 3241 enum dma_data_direction dir) 3242{ 3243 return dma_map_page(dev, skb_frag_page(frag), 3244 skb_frag_off(frag) + offset, size, dir); 3245} 3246 3247static inline struct sk_buff *pskb_copy(struct sk_buff *skb, 3248 gfp_t gfp_mask) 3249{ 3250 return __pskb_copy(skb, skb_headroom(skb), gfp_mask); 3251} 3252 3253 3254static inline struct sk_buff *pskb_copy_for_clone(struct sk_buff *skb, 3255 gfp_t gfp_mask) 3256{ 3257 return __pskb_copy_fclone(skb, skb_headroom(skb), gfp_mask, true); 3258} 3259 3260 3261/** 3262 * skb_clone_writable - is the header of a clone writable 3263 * @skb: buffer to check 3264 * @len: length up to which to write 3265 * 3266 * Returns true if modifying the header part of the cloned buffer 3267 * does not requires the data to be copied. 3268 */ 3269static inline int skb_clone_writable(const struct sk_buff *skb, unsigned int len) 3270{ 3271 return !skb_header_cloned(skb) && 3272 skb_headroom(skb) + len <= skb->hdr_len; 3273} 3274 3275static inline int skb_try_make_writable(struct sk_buff *skb, 3276 unsigned int write_len) 3277{ 3278 return skb_cloned(skb) && !skb_clone_writable(skb, write_len) && 3279 pskb_expand_head(skb, 0, 0, GFP_ATOMIC); 3280} 3281 3282static inline int __skb_cow(struct sk_buff *skb, unsigned int headroom, 3283 int cloned) 3284{ 3285 int delta = 0; 3286 3287 if (headroom > skb_headroom(skb)) 3288 delta = headroom - skb_headroom(skb); 3289 3290 if (delta || cloned) 3291 return pskb_expand_head(skb, ALIGN(delta, NET_SKB_PAD), 0, 3292 GFP_ATOMIC); 3293 return 0; 3294} 3295 3296/** 3297 * skb_cow - copy header of skb when it is required 3298 * @skb: buffer to cow 3299 * @headroom: needed headroom 3300 * 3301 * If the skb passed lacks sufficient headroom or its data part 3302 * is shared, data is reallocated. If reallocation fails, an error 3303 * is returned and original skb is not changed. 3304 * 3305 * The result is skb with writable area skb->head...skb->tail 3306 * and at least @headroom of space at head. 3307 */ 3308static inline int skb_cow(struct sk_buff *skb, unsigned int headroom) 3309{ 3310 return __skb_cow(skb, headroom, skb_cloned(skb)); 3311} 3312 3313/** 3314 * skb_cow_head - skb_cow but only making the head writable 3315 * @skb: buffer to cow 3316 * @headroom: needed headroom 3317 * 3318 * This function is identical to skb_cow except that we replace the 3319 * skb_cloned check by skb_header_cloned. It should be used when 3320 * you only need to push on some header and do not need to modify 3321 * the data. 3322 */ 3323static inline int skb_cow_head(struct sk_buff *skb, unsigned int headroom) 3324{ 3325 return __skb_cow(skb, headroom, skb_header_cloned(skb)); 3326} 3327 3328/** 3329 * skb_padto - pad an skbuff up to a minimal size 3330 * @skb: buffer to pad 3331 * @len: minimal length 3332 * 3333 * Pads up a buffer to ensure the trailing bytes exist and are 3334 * blanked. If the buffer already contains sufficient data it 3335 * is untouched. Otherwise it is extended. Returns zero on 3336 * success. The skb is freed on error. 3337 */ 3338static inline int skb_padto(struct sk_buff *skb, unsigned int len) 3339{ 3340 unsigned int size = skb->len; 3341 if (likely(size >= len)) 3342 return 0; 3343 return skb_pad(skb, len - size); 3344} 3345 3346/** 3347 * __skb_put_padto - increase size and pad an skbuff up to a minimal size 3348 * @skb: buffer to pad 3349 * @len: minimal length 3350 * @free_on_error: free buffer on error 3351 * 3352 * Pads up a buffer to ensure the trailing bytes exist and are 3353 * blanked. If the buffer already contains sufficient data it 3354 * is untouched. Otherwise it is extended. Returns zero on 3355 * success. The skb is freed on error if @free_on_error is true. 3356 */ 3357static inline int __must_check __skb_put_padto(struct sk_buff *skb, 3358 unsigned int len, 3359 bool free_on_error) 3360{ 3361 unsigned int size = skb->len; 3362 3363 if (unlikely(size < len)) { 3364 len -= size; 3365 if (__skb_pad(skb, len, free_on_error)) 3366 return -ENOMEM; 3367 __skb_put(skb, len); 3368 } 3369 return 0; 3370} 3371 3372/** 3373 * skb_put_padto - increase size and pad an skbuff up to a minimal size 3374 * @skb: buffer to pad 3375 * @len: minimal length 3376 * 3377 * Pads up a buffer to ensure the trailing bytes exist and are 3378 * blanked. If the buffer already contains sufficient data it 3379 * is untouched. Otherwise it is extended. Returns zero on 3380 * success. The skb is freed on error. 3381 */ 3382static inline int __must_check skb_put_padto(struct sk_buff *skb, unsigned int len) 3383{ 3384 return __skb_put_padto(skb, len, true); 3385} 3386 3387static inline int skb_add_data(struct sk_buff *skb, 3388 struct iov_iter *from, int copy) 3389{ 3390 const int off = skb->len; 3391 3392 if (skb->ip_summed == CHECKSUM_NONE) { 3393 __wsum csum = 0; 3394 if (csum_and_copy_from_iter_full(skb_put(skb, copy), copy, 3395 &csum, from)) { 3396 skb->csum = csum_block_add(skb->csum, csum, off); 3397 return 0; 3398 } 3399 } else if (copy_from_iter_full(skb_put(skb, copy), copy, from)) 3400 return 0; 3401 3402 __skb_trim(skb, off); 3403 return -EFAULT; 3404} 3405 3406static inline bool skb_can_coalesce(struct sk_buff *skb, int i, 3407 const struct page *page, int off) 3408{ 3409 if (skb_zcopy(skb)) 3410 return false; 3411 if (i) { 3412 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i - 1]; 3413 3414 return page == skb_frag_page(frag) && 3415 off == skb_frag_off(frag) + skb_frag_size(frag); 3416 } 3417 return false; 3418} 3419 3420static inline int __skb_linearize(struct sk_buff *skb) 3421{ 3422 return __pskb_pull_tail(skb, skb->data_len) ? 0 : -ENOMEM; 3423} 3424 3425/** 3426 * skb_linearize - convert paged skb to linear one 3427 * @skb: buffer to linarize 3428 * 3429 * If there is no free memory -ENOMEM is returned, otherwise zero 3430 * is returned and the old skb data released. 3431 */ 3432static inline int skb_linearize(struct sk_buff *skb) 3433{ 3434 return skb_is_nonlinear(skb) ? __skb_linearize(skb) : 0; 3435} 3436 3437/** 3438 * skb_has_shared_frag - can any frag be overwritten 3439 * @skb: buffer to test 3440 * 3441 * Return true if the skb has at least one frag that might be modified 3442 * by an external entity (as in vmsplice()/sendfile()) 3443 */ 3444static inline bool skb_has_shared_frag(const struct sk_buff *skb) 3445{ 3446 return skb_is_nonlinear(skb) && 3447 skb_shinfo(skb)->flags & SKBFL_SHARED_FRAG; 3448} 3449 3450/** 3451 * skb_linearize_cow - make sure skb is linear and writable 3452 * @skb: buffer to process 3453 * 3454 * If there is no free memory -ENOMEM is returned, otherwise zero 3455 * is returned and the old skb data released. 3456 */ 3457static inline int skb_linearize_cow(struct sk_buff *skb) 3458{ 3459 return skb_is_nonlinear(skb) || skb_cloned(skb) ? 3460 __skb_linearize(skb) : 0; 3461} 3462 3463static __always_inline void 3464__skb_postpull_rcsum(struct sk_buff *skb, const void *start, unsigned int len, 3465 unsigned int off) 3466{ 3467 if (skb->ip_summed == CHECKSUM_COMPLETE) 3468 skb->csum = csum_block_sub(skb->csum, 3469 csum_partial(start, len, 0), off); 3470 else if (skb->ip_summed == CHECKSUM_PARTIAL && 3471 skb_checksum_start_offset(skb) < 0) 3472 skb->ip_summed = CHECKSUM_NONE; 3473} 3474 3475/** 3476 * skb_postpull_rcsum - update checksum for received skb after pull 3477 * @skb: buffer to update 3478 * @start: start of data before pull 3479 * @len: length of data pulled 3480 * 3481 * After doing a pull on a received packet, you need to call this to 3482 * update the CHECKSUM_COMPLETE checksum, or set ip_summed to 3483 * CHECKSUM_NONE so that it can be recomputed from scratch. 3484 */ 3485static inline void skb_postpull_rcsum(struct sk_buff *skb, 3486 const void *start, unsigned int len) 3487{ 3488 __skb_postpull_rcsum(skb, start, len, 0); 3489} 3490 3491static __always_inline void 3492__skb_postpush_rcsum(struct sk_buff *skb, const void *start, unsigned int len, 3493 unsigned int off) 3494{ 3495 if (skb->ip_summed == CHECKSUM_COMPLETE) 3496 skb->csum = csum_block_add(skb->csum, 3497 csum_partial(start, len, 0), off); 3498} 3499 3500/** 3501 * skb_postpush_rcsum - update checksum for received skb after push 3502 * @skb: buffer to update 3503 * @start: start of data after push 3504 * @len: length of data pushed 3505 * 3506 * After doing a push on a received packet, you need to call this to 3507 * update the CHECKSUM_COMPLETE checksum. 3508 */ 3509static inline void skb_postpush_rcsum(struct sk_buff *skb, 3510 const void *start, unsigned int len) 3511{ 3512 __skb_postpush_rcsum(skb, start, len, 0); 3513} 3514 3515void *skb_pull_rcsum(struct sk_buff *skb, unsigned int len); 3516 3517/** 3518 * skb_push_rcsum - push skb and update receive checksum 3519 * @skb: buffer to update 3520 * @len: length of data pulled 3521 * 3522 * This function performs an skb_push on the packet and updates 3523 * the CHECKSUM_COMPLETE checksum. It should be used on 3524 * receive path processing instead of skb_push unless you know 3525 * that the checksum difference is zero (e.g., a valid IP header) 3526 * or you are setting ip_summed to CHECKSUM_NONE. 3527 */ 3528static inline void *skb_push_rcsum(struct sk_buff *skb, unsigned int len) 3529{ 3530 skb_push(skb, len); 3531 skb_postpush_rcsum(skb, skb->data, len); 3532 return skb->data; 3533} 3534 3535int pskb_trim_rcsum_slow(struct sk_buff *skb, unsigned int len); 3536/** 3537 * pskb_trim_rcsum - trim received skb and update checksum 3538 * @skb: buffer to trim 3539 * @len: new length 3540 * 3541 * This is exactly the same as pskb_trim except that it ensures the 3542 * checksum of received packets are still valid after the operation. 3543 * It can change skb pointers. 3544 */ 3545 3546static inline int pskb_trim_rcsum(struct sk_buff *skb, unsigned int len) 3547{ 3548 if (likely(len >= skb->len)) 3549 return 0; 3550 return pskb_trim_rcsum_slow(skb, len); 3551} 3552 3553static inline int __skb_trim_rcsum(struct sk_buff *skb, unsigned int len) 3554{ 3555 if (skb->ip_summed == CHECKSUM_COMPLETE) 3556 skb->ip_summed = CHECKSUM_NONE; 3557 __skb_trim(skb, len); 3558 return 0; 3559} 3560 3561static inline int __skb_grow_rcsum(struct sk_buff *skb, unsigned int len) 3562{ 3563 if (skb->ip_summed == CHECKSUM_COMPLETE) 3564 skb->ip_summed = CHECKSUM_NONE; 3565 return __skb_grow(skb, len); 3566} 3567 3568#define rb_to_skb(rb) rb_entry_safe(rb, struct sk_buff, rbnode) 3569#define skb_rb_first(root) rb_to_skb(rb_first(root)) 3570#define skb_rb_last(root) rb_to_skb(rb_last(root)) 3571#define skb_rb_next(skb) rb_to_skb(rb_next(&(skb)->rbnode)) 3572#define skb_rb_prev(skb) rb_to_skb(rb_prev(&(skb)->rbnode)) 3573 3574#define skb_queue_walk(queue, skb) \ 3575 for (skb = (queue)->next; \ 3576 skb != (struct sk_buff *)(queue); \ 3577 skb = skb->next) 3578 3579#define skb_queue_walk_safe(queue, skb, tmp) \ 3580 for (skb = (queue)->next, tmp = skb->next; \ 3581 skb != (struct sk_buff *)(queue); \ 3582 skb = tmp, tmp = skb->next) 3583 3584#define skb_queue_walk_from(queue, skb) \ 3585 for (; skb != (struct sk_buff *)(queue); \ 3586 skb = skb->next) 3587 3588#define skb_rbtree_walk(skb, root) \ 3589 for (skb = skb_rb_first(root); skb != NULL; \ 3590 skb = skb_rb_next(skb)) 3591 3592#define skb_rbtree_walk_from(skb) \ 3593 for (; skb != NULL; \ 3594 skb = skb_rb_next(skb)) 3595 3596#define skb_rbtree_walk_from_safe(skb, tmp) \ 3597 for (; tmp = skb ? skb_rb_next(skb) : NULL, (skb != NULL); \ 3598 skb = tmp) 3599 3600#define skb_queue_walk_from_safe(queue, skb, tmp) \ 3601 for (tmp = skb->next; \ 3602 skb != (struct sk_buff *)(queue); \ 3603 skb = tmp, tmp = skb->next) 3604 3605#define skb_queue_reverse_walk(queue, skb) \ 3606 for (skb = (queue)->prev; \ 3607 skb != (struct sk_buff *)(queue); \ 3608 skb = skb->prev) 3609 3610#define skb_queue_reverse_walk_safe(queue, skb, tmp) \ 3611 for (skb = (queue)->prev, tmp = skb->prev; \ 3612 skb != (struct sk_buff *)(queue); \ 3613 skb = tmp, tmp = skb->prev) 3614 3615#define skb_queue_reverse_walk_from_safe(queue, skb, tmp) \ 3616 for (tmp = skb->prev; \ 3617 skb != (struct sk_buff *)(queue); \ 3618 skb = tmp, tmp = skb->prev) 3619 3620static inline bool skb_has_frag_list(const struct sk_buff *skb) 3621{ 3622 return skb_shinfo(skb)->frag_list != NULL; 3623} 3624 3625static inline void skb_frag_list_init(struct sk_buff *skb) 3626{ 3627 skb_shinfo(skb)->frag_list = NULL; 3628} 3629 3630#define skb_walk_frags(skb, iter) \ 3631 for (iter = skb_shinfo(skb)->frag_list; iter; iter = iter->next) 3632 3633 3634int __skb_wait_for_more_packets(struct sock *sk, struct sk_buff_head *queue, 3635 int *err, long *timeo_p, 3636 const struct sk_buff *skb); 3637struct sk_buff *__skb_try_recv_from_queue(struct sock *sk, 3638 struct sk_buff_head *queue, 3639 unsigned int flags, 3640 int *off, int *err, 3641 struct sk_buff **last); 3642struct sk_buff *__skb_try_recv_datagram(struct sock *sk, 3643 struct sk_buff_head *queue, 3644 unsigned int flags, int *off, int *err, 3645 struct sk_buff **last); 3646struct sk_buff *__skb_recv_datagram(struct sock *sk, 3647 struct sk_buff_head *sk_queue, 3648 unsigned int flags, int *off, int *err); 3649struct sk_buff *skb_recv_datagram(struct sock *sk, unsigned flags, int noblock, 3650 int *err); 3651__poll_t datagram_poll(struct file *file, struct socket *sock, 3652 struct poll_table_struct *wait); 3653int skb_copy_datagram_iter(const struct sk_buff *from, int offset, 3654 struct iov_iter *to, int size); 3655static inline int skb_copy_datagram_msg(const struct sk_buff *from, int offset, 3656 struct msghdr *msg, int size) 3657{ 3658 return skb_copy_datagram_iter(from, offset, &msg->msg_iter, size); 3659} 3660int skb_copy_and_csum_datagram_msg(struct sk_buff *skb, int hlen, 3661 struct msghdr *msg); 3662int skb_copy_and_hash_datagram_iter(const struct sk_buff *skb, int offset, 3663 struct iov_iter *to, int len, 3664 struct ahash_request *hash); 3665int skb_copy_datagram_from_iter(struct sk_buff *skb, int offset, 3666 struct iov_iter *from, int len); 3667int zerocopy_sg_from_iter(struct sk_buff *skb, struct iov_iter *frm); 3668void skb_free_datagram(struct sock *sk, struct sk_buff *skb); 3669void __skb_free_datagram_locked(struct sock *sk, struct sk_buff *skb, int len); 3670static inline void skb_free_datagram_locked(struct sock *sk, 3671 struct sk_buff *skb) 3672{ 3673 __skb_free_datagram_locked(sk, skb, 0); 3674} 3675int skb_kill_datagram(struct sock *sk, struct sk_buff *skb, unsigned int flags); 3676int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len); 3677int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len); 3678__wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset, u8 *to, 3679 int len); 3680int skb_splice_bits(struct sk_buff *skb, struct sock *sk, unsigned int offset, 3681 struct pipe_inode_info *pipe, unsigned int len, 3682 unsigned int flags); 3683int skb_send_sock_locked(struct sock *sk, struct sk_buff *skb, int offset, 3684 int len); 3685int skb_send_sock(struct sock *sk, struct sk_buff *skb, int offset, int len); 3686void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to); 3687unsigned int skb_zerocopy_headlen(const struct sk_buff *from); 3688int skb_zerocopy(struct sk_buff *to, struct sk_buff *from, 3689 int len, int hlen); 3690void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len); 3691int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen); 3692void skb_scrub_packet(struct sk_buff *skb, bool xnet); 3693bool skb_gso_validate_network_len(const struct sk_buff *skb, unsigned int mtu); 3694bool skb_gso_validate_mac_len(const struct sk_buff *skb, unsigned int len); 3695struct sk_buff *skb_segment(struct sk_buff *skb, netdev_features_t features); 3696struct sk_buff *skb_segment_list(struct sk_buff *skb, netdev_features_t features, 3697 unsigned int offset); 3698struct sk_buff *skb_vlan_untag(struct sk_buff *skb); 3699int skb_ensure_writable(struct sk_buff *skb, int write_len); 3700int __skb_vlan_pop(struct sk_buff *skb, u16 *vlan_tci); 3701int skb_vlan_pop(struct sk_buff *skb); 3702int skb_vlan_push(struct sk_buff *skb, __be16 vlan_proto, u16 vlan_tci); 3703int skb_eth_pop(struct sk_buff *skb); 3704int skb_eth_push(struct sk_buff *skb, const unsigned char *dst, 3705 const unsigned char *src); 3706int skb_mpls_push(struct sk_buff *skb, __be32 mpls_lse, __be16 mpls_proto, 3707 int mac_len, bool ethernet); 3708int skb_mpls_pop(struct sk_buff *skb, __be16 next_proto, int mac_len, 3709 bool ethernet); 3710int skb_mpls_update_lse(struct sk_buff *skb, __be32 mpls_lse); 3711int skb_mpls_dec_ttl(struct sk_buff *skb); 3712struct sk_buff *pskb_extract(struct sk_buff *skb, int off, int to_copy, 3713 gfp_t gfp); 3714 3715static inline int memcpy_from_msg(void *data, struct msghdr *msg, int len) 3716{ 3717 return copy_from_iter_full(data, len, &msg->msg_iter) ? 0 : -EFAULT; 3718} 3719 3720static inline int memcpy_to_msg(struct msghdr *msg, void *data, int len) 3721{ 3722 return copy_to_iter(data, len, &msg->msg_iter) == len ? 0 : -EFAULT; 3723} 3724 3725struct skb_checksum_ops { 3726 __wsum (*update)(const void *mem, int len, __wsum wsum); 3727 __wsum (*combine)(__wsum csum, __wsum csum2, int offset, int len); 3728}; 3729 3730extern const struct skb_checksum_ops *crc32c_csum_stub __read_mostly; 3731 3732__wsum __skb_checksum(const struct sk_buff *skb, int offset, int len, 3733 __wsum csum, const struct skb_checksum_ops *ops); 3734__wsum skb_checksum(const struct sk_buff *skb, int offset, int len, 3735 __wsum csum); 3736 3737static inline void * __must_check 3738__skb_header_pointer(const struct sk_buff *skb, int offset, int len, 3739 const void *data, int hlen, void *buffer) 3740{ 3741 if (likely(hlen - offset >= len)) 3742 return (void *)data + offset; 3743 3744 if (!skb || unlikely(skb_copy_bits(skb, offset, buffer, len) < 0)) 3745 return NULL; 3746 3747 return buffer; 3748} 3749 3750static inline void * __must_check 3751skb_header_pointer(const struct sk_buff *skb, int offset, int len, void *buffer) 3752{ 3753 return __skb_header_pointer(skb, offset, len, skb->data, 3754 skb_headlen(skb), buffer); 3755} 3756 3757/** 3758 * skb_needs_linearize - check if we need to linearize a given skb 3759 * depending on the given device features. 3760 * @skb: socket buffer to check 3761 * @features: net device features 3762 * 3763 * Returns true if either: 3764 * 1. skb has frag_list and the device doesn't support FRAGLIST, or 3765 * 2. skb is fragmented and the device does not support SG. 3766 */ 3767static inline bool skb_needs_linearize(struct sk_buff *skb, 3768 netdev_features_t features) 3769{ 3770 return skb_is_nonlinear(skb) && 3771 ((skb_has_frag_list(skb) && !(features & NETIF_F_FRAGLIST)) || 3772 (skb_shinfo(skb)->nr_frags && !(features & NETIF_F_SG))); 3773} 3774 3775static inline void skb_copy_from_linear_data(const struct sk_buff *skb, 3776 void *to, 3777 const unsigned int len) 3778{ 3779 memcpy(to, skb->data, len); 3780} 3781 3782static inline void skb_copy_from_linear_data_offset(const struct sk_buff *skb, 3783 const int offset, void *to, 3784 const unsigned int len) 3785{ 3786 memcpy(to, skb->data + offset, len); 3787} 3788 3789static inline void skb_copy_to_linear_data(struct sk_buff *skb, 3790 const void *from, 3791 const unsigned int len) 3792{ 3793 memcpy(skb->data, from, len); 3794} 3795 3796static inline void skb_copy_to_linear_data_offset(struct sk_buff *skb, 3797 const int offset, 3798 const void *from, 3799 const unsigned int len) 3800{ 3801 memcpy(skb->data + offset, from, len); 3802} 3803 3804void skb_init(void); 3805 3806static inline ktime_t skb_get_ktime(const struct sk_buff *skb) 3807{ 3808 return skb->tstamp; 3809} 3810 3811/** 3812 * skb_get_timestamp - get timestamp from a skb 3813 * @skb: skb to get stamp from 3814 * @stamp: pointer to struct __kernel_old_timeval to store stamp in 3815 * 3816 * Timestamps are stored in the skb as offsets to a base timestamp. 3817 * This function converts the offset back to a struct timeval and stores 3818 * it in stamp. 3819 */ 3820static inline void skb_get_timestamp(const struct sk_buff *skb, 3821 struct __kernel_old_timeval *stamp) 3822{ 3823 *stamp = ns_to_kernel_old_timeval(skb->tstamp); 3824} 3825 3826static inline void skb_get_new_timestamp(const struct sk_buff *skb, 3827 struct __kernel_sock_timeval *stamp) 3828{ 3829 struct timespec64 ts = ktime_to_timespec64(skb->tstamp); 3830 3831 stamp->tv_sec = ts.tv_sec; 3832 stamp->tv_usec = ts.tv_nsec / 1000; 3833} 3834 3835static inline void skb_get_timestampns(const struct sk_buff *skb, 3836 struct __kernel_old_timespec *stamp) 3837{ 3838 struct timespec64 ts = ktime_to_timespec64(skb->tstamp); 3839 3840 stamp->tv_sec = ts.tv_sec; 3841 stamp->tv_nsec = ts.tv_nsec; 3842} 3843 3844static inline void skb_get_new_timestampns(const struct sk_buff *skb, 3845 struct __kernel_timespec *stamp) 3846{ 3847 struct timespec64 ts = ktime_to_timespec64(skb->tstamp); 3848 3849 stamp->tv_sec = ts.tv_sec; 3850 stamp->tv_nsec = ts.tv_nsec; 3851} 3852 3853static inline void __net_timestamp(struct sk_buff *skb) 3854{ 3855 skb->tstamp = ktime_get_real(); 3856} 3857 3858static inline ktime_t net_timedelta(ktime_t t) 3859{ 3860 return ktime_sub(ktime_get_real(), t); 3861} 3862 3863static inline ktime_t net_invalid_timestamp(void) 3864{ 3865 return 0; 3866} 3867 3868static inline u8 skb_metadata_len(const struct sk_buff *skb) 3869{ 3870 return skb_shinfo(skb)->meta_len; 3871} 3872 3873static inline void *skb_metadata_end(const struct sk_buff *skb) 3874{ 3875 return skb_mac_header(skb); 3876} 3877 3878static inline bool __skb_metadata_differs(const struct sk_buff *skb_a, 3879 const struct sk_buff *skb_b, 3880 u8 meta_len) 3881{ 3882 const void *a = skb_metadata_end(skb_a); 3883 const void *b = skb_metadata_end(skb_b); 3884 /* Using more efficient varaiant than plain call to memcmp(). */ 3885#if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64 3886 u64 diffs = 0; 3887 3888 switch (meta_len) { 3889#define __it(x, op) (x -= sizeof(u##op)) 3890#define __it_diff(a, b, op) (*(u##op *)__it(a, op)) ^ (*(u##op *)__it(b, op)) 3891 case 32: diffs |= __it_diff(a, b, 64); 3892 fallthrough; 3893 case 24: diffs |= __it_diff(a, b, 64); 3894 fallthrough; 3895 case 16: diffs |= __it_diff(a, b, 64); 3896 fallthrough; 3897 case 8: diffs |= __it_diff(a, b, 64); 3898 break; 3899 case 28: diffs |= __it_diff(a, b, 64); 3900 fallthrough; 3901 case 20: diffs |= __it_diff(a, b, 64); 3902 fallthrough; 3903 case 12: diffs |= __it_diff(a, b, 64); 3904 fallthrough; 3905 case 4: diffs |= __it_diff(a, b, 32); 3906 break; 3907 } 3908 return diffs; 3909#else 3910 return memcmp(a - meta_len, b - meta_len, meta_len); 3911#endif 3912} 3913 3914static inline bool skb_metadata_differs(const struct sk_buff *skb_a, 3915 const struct sk_buff *skb_b) 3916{ 3917 u8 len_a = skb_metadata_len(skb_a); 3918 u8 len_b = skb_metadata_len(skb_b); 3919 3920 if (!(len_a | len_b)) 3921 return false; 3922 3923 return len_a != len_b ? 3924 true : __skb_metadata_differs(skb_a, skb_b, len_a); 3925} 3926 3927static inline void skb_metadata_set(struct sk_buff *skb, u8 meta_len) 3928{ 3929 skb_shinfo(skb)->meta_len = meta_len; 3930} 3931 3932static inline void skb_metadata_clear(struct sk_buff *skb) 3933{ 3934 skb_metadata_set(skb, 0); 3935} 3936 3937struct sk_buff *skb_clone_sk(struct sk_buff *skb); 3938 3939#ifdef CONFIG_NETWORK_PHY_TIMESTAMPING 3940 3941void skb_clone_tx_timestamp(struct sk_buff *skb); 3942bool skb_defer_rx_timestamp(struct sk_buff *skb); 3943 3944#else /* CONFIG_NETWORK_PHY_TIMESTAMPING */ 3945 3946static inline void skb_clone_tx_timestamp(struct sk_buff *skb) 3947{ 3948} 3949 3950static inline bool skb_defer_rx_timestamp(struct sk_buff *skb) 3951{ 3952 return false; 3953} 3954 3955#endif /* !CONFIG_NETWORK_PHY_TIMESTAMPING */ 3956 3957/** 3958 * skb_complete_tx_timestamp() - deliver cloned skb with tx timestamps 3959 * 3960 * PHY drivers may accept clones of transmitted packets for 3961 * timestamping via their phy_driver.txtstamp method. These drivers 3962 * must call this function to return the skb back to the stack with a 3963 * timestamp. 3964 * 3965 * @skb: clone of the original outgoing packet 3966 * @hwtstamps: hardware time stamps 3967 * 3968 */ 3969void skb_complete_tx_timestamp(struct sk_buff *skb, 3970 struct skb_shared_hwtstamps *hwtstamps); 3971 3972void __skb_tstamp_tx(struct sk_buff *orig_skb, const struct sk_buff *ack_skb, 3973 struct skb_shared_hwtstamps *hwtstamps, 3974 struct sock *sk, int tstype); 3975 3976/** 3977 * skb_tstamp_tx - queue clone of skb with send time stamps 3978 * @orig_skb: the original outgoing packet 3979 * @hwtstamps: hardware time stamps, may be NULL if not available 3980 * 3981 * If the skb has a socket associated, then this function clones the 3982 * skb (thus sharing the actual data and optional structures), stores 3983 * the optional hardware time stamping information (if non NULL) or 3984 * generates a software time stamp (otherwise), then queues the clone 3985 * to the error queue of the socket. Errors are silently ignored. 3986 */ 3987void skb_tstamp_tx(struct sk_buff *orig_skb, 3988 struct skb_shared_hwtstamps *hwtstamps); 3989 3990/** 3991 * skb_tx_timestamp() - Driver hook for transmit timestamping 3992 * 3993 * Ethernet MAC Drivers should call this function in their hard_xmit() 3994 * function immediately before giving the sk_buff to the MAC hardware. 3995 * 3996 * Specifically, one should make absolutely sure that this function is 3997 * called before TX completion of this packet can trigger. Otherwise 3998 * the packet could potentially already be freed. 3999 * 4000 * @skb: A socket buffer. 4001 */ 4002static inline void skb_tx_timestamp(struct sk_buff *skb) 4003{ 4004 skb_clone_tx_timestamp(skb); 4005 if (skb_shinfo(skb)->tx_flags & SKBTX_SW_TSTAMP) 4006 skb_tstamp_tx(skb, NULL); 4007} 4008 4009/** 4010 * skb_complete_wifi_ack - deliver skb with wifi status 4011 * 4012 * @skb: the original outgoing packet 4013 * @acked: ack status 4014 * 4015 */ 4016void skb_complete_wifi_ack(struct sk_buff *skb, bool acked); 4017 4018__sum16 __skb_checksum_complete_head(struct sk_buff *skb, int len); 4019__sum16 __skb_checksum_complete(struct sk_buff *skb); 4020 4021static inline int skb_csum_unnecessary(const struct sk_buff *skb) 4022{ 4023 return ((skb->ip_summed == CHECKSUM_UNNECESSARY) || 4024 skb->csum_valid || 4025 (skb->ip_summed == CHECKSUM_PARTIAL && 4026 skb_checksum_start_offset(skb) >= 0)); 4027} 4028 4029/** 4030 * skb_checksum_complete - Calculate checksum of an entire packet 4031 * @skb: packet to process 4032 * 4033 * This function calculates the checksum over the entire packet plus 4034 * the value of skb->csum. The latter can be used to supply the 4035 * checksum of a pseudo header as used by TCP/UDP. It returns the 4036 * checksum. 4037 * 4038 * For protocols that contain complete checksums such as ICMP/TCP/UDP, 4039 * this function can be used to verify that checksum on received 4040 * packets. In that case the function should return zero if the 4041 * checksum is correct. In particular, this function will return zero 4042 * if skb->ip_summed is CHECKSUM_UNNECESSARY which indicates that the 4043 * hardware has already verified the correctness of the checksum. 4044 */ 4045static inline __sum16 skb_checksum_complete(struct sk_buff *skb) 4046{ 4047 return skb_csum_unnecessary(skb) ? 4048 0 : __skb_checksum_complete(skb); 4049} 4050 4051static inline void __skb_decr_checksum_unnecessary(struct sk_buff *skb) 4052{ 4053 if (skb->ip_summed == CHECKSUM_UNNECESSARY) { 4054 if (skb->csum_level == 0) 4055 skb->ip_summed = CHECKSUM_NONE; 4056 else 4057 skb->csum_level--; 4058 } 4059} 4060 4061static inline void __skb_incr_checksum_unnecessary(struct sk_buff *skb) 4062{ 4063 if (skb->ip_summed == CHECKSUM_UNNECESSARY) { 4064 if (skb->csum_level < SKB_MAX_CSUM_LEVEL) 4065 skb->csum_level++; 4066 } else if (skb->ip_summed == CHECKSUM_NONE) { 4067 skb->ip_summed = CHECKSUM_UNNECESSARY; 4068 skb->csum_level = 0; 4069 } 4070} 4071 4072static inline void __skb_reset_checksum_unnecessary(struct sk_buff *skb) 4073{ 4074 if (skb->ip_summed == CHECKSUM_UNNECESSARY) { 4075 skb->ip_summed = CHECKSUM_NONE; 4076 skb->csum_level = 0; 4077 } 4078} 4079 4080/* Check if we need to perform checksum complete validation. 4081 * 4082 * Returns true if checksum complete is needed, false otherwise 4083 * (either checksum is unnecessary or zero checksum is allowed). 4084 */ 4085static inline bool __skb_checksum_validate_needed(struct sk_buff *skb, 4086 bool zero_okay, 4087 __sum16 check) 4088{ 4089 if (skb_csum_unnecessary(skb) || (zero_okay && !check)) { 4090 skb->csum_valid = 1; 4091 __skb_decr_checksum_unnecessary(skb); 4092 return false; 4093 } 4094 4095 return true; 4096} 4097 4098/* For small packets <= CHECKSUM_BREAK perform checksum complete directly 4099 * in checksum_init. 4100 */ 4101#define CHECKSUM_BREAK 76 4102 4103/* Unset checksum-complete 4104 * 4105 * Unset checksum complete can be done when packet is being modified 4106 * (uncompressed for instance) and checksum-complete value is 4107 * invalidated. 4108 */ 4109static inline void skb_checksum_complete_unset(struct sk_buff *skb) 4110{ 4111 if (skb->ip_summed == CHECKSUM_COMPLETE) 4112 skb->ip_summed = CHECKSUM_NONE; 4113} 4114 4115/* Validate (init) checksum based on checksum complete. 4116 * 4117 * Return values: 4118 * 0: checksum is validated or try to in skb_checksum_complete. In the latter 4119 * case the ip_summed will not be CHECKSUM_UNNECESSARY and the pseudo 4120 * checksum is stored in skb->csum for use in __skb_checksum_complete 4121 * non-zero: value of invalid checksum 4122 * 4123 */ 4124static inline __sum16 __skb_checksum_validate_complete(struct sk_buff *skb, 4125 bool complete, 4126 __wsum psum) 4127{ 4128 if (skb->ip_summed == CHECKSUM_COMPLETE) { 4129 if (!csum_fold(csum_add(psum, skb->csum))) { 4130 skb->csum_valid = 1; 4131 return 0; 4132 } 4133 } 4134 4135 skb->csum = psum; 4136 4137 if (complete || skb->len <= CHECKSUM_BREAK) { 4138 __sum16 csum; 4139 4140 csum = __skb_checksum_complete(skb); 4141 skb->csum_valid = !csum; 4142 return csum; 4143 } 4144 4145 return 0; 4146} 4147 4148static inline __wsum null_compute_pseudo(struct sk_buff *skb, int proto) 4149{ 4150 return 0; 4151} 4152 4153/* Perform checksum validate (init). Note that this is a macro since we only 4154 * want to calculate the pseudo header which is an input function if necessary. 4155 * First we try to validate without any computation (checksum unnecessary) and 4156 * then calculate based on checksum complete calling the function to compute 4157 * pseudo header. 4158 * 4159 * Return values: 4160 * 0: checksum is validated or try to in skb_checksum_complete 4161 * non-zero: value of invalid checksum 4162 */ 4163#define __skb_checksum_validate(skb, proto, complete, \ 4164 zero_okay, check, compute_pseudo) \ 4165({ \ 4166 __sum16 __ret = 0; \ 4167 skb->csum_valid = 0; \ 4168 if (__skb_checksum_validate_needed(skb, zero_okay, check)) \ 4169 __ret = __skb_checksum_validate_complete(skb, \ 4170 complete, compute_pseudo(skb, proto)); \ 4171 __ret; \ 4172}) 4173 4174#define skb_checksum_init(skb, proto, compute_pseudo) \ 4175 __skb_checksum_validate(skb, proto, false, false, 0, compute_pseudo) 4176 4177#define skb_checksum_init_zero_check(skb, proto, check, compute_pseudo) \ 4178 __skb_checksum_validate(skb, proto, false, true, check, compute_pseudo) 4179 4180#define skb_checksum_validate(skb, proto, compute_pseudo) \ 4181 __skb_checksum_validate(skb, proto, true, false, 0, compute_pseudo) 4182 4183#define skb_checksum_validate_zero_check(skb, proto, check, \ 4184 compute_pseudo) \ 4185 __skb_checksum_validate(skb, proto, true, true, check, compute_pseudo) 4186 4187#define skb_checksum_simple_validate(skb) \ 4188 __skb_checksum_validate(skb, 0, true, false, 0, null_compute_pseudo) 4189 4190static inline bool __skb_checksum_convert_check(struct sk_buff *skb) 4191{ 4192 return (skb->ip_summed == CHECKSUM_NONE && skb->csum_valid); 4193} 4194 4195static inline void __skb_checksum_convert(struct sk_buff *skb, __wsum pseudo) 4196{ 4197 skb->csum = ~pseudo; 4198 skb->ip_summed = CHECKSUM_COMPLETE; 4199} 4200 4201#define skb_checksum_try_convert(skb, proto, compute_pseudo) \ 4202do { \ 4203 if (__skb_checksum_convert_check(skb)) \ 4204 __skb_checksum_convert(skb, compute_pseudo(skb, proto)); \ 4205} while (0) 4206 4207static inline void skb_remcsum_adjust_partial(struct sk_buff *skb, void *ptr, 4208 u16 start, u16 offset) 4209{ 4210 skb->ip_summed = CHECKSUM_PARTIAL; 4211 skb->csum_start = ((unsigned char *)ptr + start) - skb->head; 4212 skb->csum_offset = offset - start; 4213} 4214 4215/* Update skbuf and packet to reflect the remote checksum offload operation. 4216 * When called, ptr indicates the starting point for skb->csum when 4217 * ip_summed is CHECKSUM_COMPLETE. If we need create checksum complete 4218 * here, skb_postpull_rcsum is done so skb->csum start is ptr. 4219 */ 4220static inline void skb_remcsum_process(struct sk_buff *skb, void *ptr, 4221 int start, int offset, bool nopartial) 4222{ 4223 __wsum delta; 4224 4225 if (!nopartial) { 4226 skb_remcsum_adjust_partial(skb, ptr, start, offset); 4227 return; 4228 } 4229 4230 if (unlikely(skb->ip_summed != CHECKSUM_COMPLETE)) { 4231 __skb_checksum_complete(skb); 4232 skb_postpull_rcsum(skb, skb->data, ptr - (void *)skb->data); 4233 } 4234 4235 delta = remcsum_adjust(ptr, skb->csum, start, offset); 4236 4237 /* Adjust skb->csum since we changed the packet */ 4238 skb->csum = csum_add(skb->csum, delta); 4239} 4240 4241static inline struct nf_conntrack *skb_nfct(const struct sk_buff *skb) 4242{ 4243#if IS_ENABLED(CONFIG_NF_CONNTRACK) 4244 return (void *)(skb->_nfct & NFCT_PTRMASK); 4245#else 4246 return NULL; 4247#endif 4248} 4249 4250static inline unsigned long skb_get_nfct(const struct sk_buff *skb) 4251{ 4252#if IS_ENABLED(CONFIG_NF_CONNTRACK) 4253 return skb->_nfct; 4254#else 4255 return 0UL; 4256#endif 4257} 4258 4259static inline void skb_set_nfct(struct sk_buff *skb, unsigned long nfct) 4260{ 4261#if IS_ENABLED(CONFIG_NF_CONNTRACK) 4262 skb->slow_gro |= !!nfct; 4263 skb->_nfct = nfct; 4264#endif 4265} 4266 4267#ifdef CONFIG_SKB_EXTENSIONS 4268enum skb_ext_id { 4269#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER) 4270 SKB_EXT_BRIDGE_NF, 4271#endif 4272#ifdef CONFIG_XFRM 4273 SKB_EXT_SEC_PATH, 4274#endif 4275#if IS_ENABLED(CONFIG_NET_TC_SKB_EXT) 4276 TC_SKB_EXT, 4277#endif 4278#if IS_ENABLED(CONFIG_MPTCP) 4279 SKB_EXT_MPTCP, 4280#endif 4281#if IS_ENABLED(CONFIG_MCTP_FLOWS) 4282 SKB_EXT_MCTP, 4283#endif 4284 SKB_EXT_NUM, /* must be last */ 4285}; 4286 4287/** 4288 * struct skb_ext - sk_buff extensions 4289 * @refcnt: 1 on allocation, deallocated on 0 4290 * @offset: offset to add to @data to obtain extension address 4291 * @chunks: size currently allocated, stored in SKB_EXT_ALIGN_SHIFT units 4292 * @data: start of extension data, variable sized 4293 * 4294 * Note: offsets/lengths are stored in chunks of 8 bytes, this allows 4295 * to use 'u8' types while allowing up to 2kb worth of extension data. 4296 */ 4297struct skb_ext { 4298 refcount_t refcnt; 4299 u8 offset[SKB_EXT_NUM]; /* in chunks of 8 bytes */ 4300 u8 chunks; /* same */ 4301 char data[] __aligned(8); 4302}; 4303 4304struct skb_ext *__skb_ext_alloc(gfp_t flags); 4305void *__skb_ext_set(struct sk_buff *skb, enum skb_ext_id id, 4306 struct skb_ext *ext); 4307void *skb_ext_add(struct sk_buff *skb, enum skb_ext_id id); 4308void __skb_ext_del(struct sk_buff *skb, enum skb_ext_id id); 4309void __skb_ext_put(struct skb_ext *ext); 4310 4311static inline void skb_ext_put(struct sk_buff *skb) 4312{ 4313 if (skb->active_extensions) 4314 __skb_ext_put(skb->extensions); 4315} 4316 4317static inline void __skb_ext_copy(struct sk_buff *dst, 4318 const struct sk_buff *src) 4319{ 4320 dst->active_extensions = src->active_extensions; 4321 4322 if (src->active_extensions) { 4323 struct skb_ext *ext = src->extensions; 4324 4325 refcount_inc(&ext->refcnt); 4326 dst->extensions = ext; 4327 } 4328} 4329 4330static inline void skb_ext_copy(struct sk_buff *dst, const struct sk_buff *src) 4331{ 4332 skb_ext_put(dst); 4333 __skb_ext_copy(dst, src); 4334} 4335 4336static inline bool __skb_ext_exist(const struct skb_ext *ext, enum skb_ext_id i) 4337{ 4338 return !!ext->offset[i]; 4339} 4340 4341static inline bool skb_ext_exist(const struct sk_buff *skb, enum skb_ext_id id) 4342{ 4343 return skb->active_extensions & (1 << id); 4344} 4345 4346static inline void skb_ext_del(struct sk_buff *skb, enum skb_ext_id id) 4347{ 4348 if (skb_ext_exist(skb, id)) 4349 __skb_ext_del(skb, id); 4350} 4351 4352static inline void *skb_ext_find(const struct sk_buff *skb, enum skb_ext_id id) 4353{ 4354 if (skb_ext_exist(skb, id)) { 4355 struct skb_ext *ext = skb->extensions; 4356 4357 return (void *)ext + (ext->offset[id] << 3); 4358 } 4359 4360 return NULL; 4361} 4362 4363static inline void skb_ext_reset(struct sk_buff *skb) 4364{ 4365 if (unlikely(skb->active_extensions)) { 4366 __skb_ext_put(skb->extensions); 4367 skb->active_extensions = 0; 4368 } 4369} 4370 4371static inline bool skb_has_extensions(struct sk_buff *skb) 4372{ 4373 return unlikely(skb->active_extensions); 4374} 4375#else 4376static inline void skb_ext_put(struct sk_buff *skb) {} 4377static inline void skb_ext_reset(struct sk_buff *skb) {} 4378static inline void skb_ext_del(struct sk_buff *skb, int unused) {} 4379static inline void __skb_ext_copy(struct sk_buff *d, const struct sk_buff *s) {} 4380static inline void skb_ext_copy(struct sk_buff *dst, const struct sk_buff *s) {} 4381static inline bool skb_has_extensions(struct sk_buff *skb) { return false; } 4382#endif /* CONFIG_SKB_EXTENSIONS */ 4383 4384static inline void nf_reset_ct(struct sk_buff *skb) 4385{ 4386#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE) 4387 nf_conntrack_put(skb_nfct(skb)); 4388 skb->_nfct = 0; 4389#endif 4390} 4391 4392static inline void nf_reset_trace(struct sk_buff *skb) 4393{ 4394#if IS_ENABLED(CONFIG_NETFILTER_XT_TARGET_TRACE) || defined(CONFIG_NF_TABLES) 4395 skb->nf_trace = 0; 4396#endif 4397} 4398 4399static inline void ipvs_reset(struct sk_buff *skb) 4400{ 4401#if IS_ENABLED(CONFIG_IP_VS) 4402 skb->ipvs_property = 0; 4403#endif 4404} 4405 4406/* Note: This doesn't put any conntrack info in dst. */ 4407static inline void __nf_copy(struct sk_buff *dst, const struct sk_buff *src, 4408 bool copy) 4409{ 4410#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE) 4411 dst->_nfct = src->_nfct; 4412 nf_conntrack_get(skb_nfct(src)); 4413#endif 4414#if IS_ENABLED(CONFIG_NETFILTER_XT_TARGET_TRACE) || defined(CONFIG_NF_TABLES) 4415 if (copy) 4416 dst->nf_trace = src->nf_trace; 4417#endif 4418} 4419 4420static inline void nf_copy(struct sk_buff *dst, const struct sk_buff *src) 4421{ 4422#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE) 4423 nf_conntrack_put(skb_nfct(dst)); 4424#endif 4425 dst->slow_gro = src->slow_gro; 4426 __nf_copy(dst, src, true); 4427} 4428 4429#ifdef CONFIG_NETWORK_SECMARK 4430static inline void skb_copy_secmark(struct sk_buff *to, const struct sk_buff *from) 4431{ 4432 to->secmark = from->secmark; 4433} 4434 4435static inline void skb_init_secmark(struct sk_buff *skb) 4436{ 4437 skb->secmark = 0; 4438} 4439#else 4440static inline void skb_copy_secmark(struct sk_buff *to, const struct sk_buff *from) 4441{ } 4442 4443static inline void skb_init_secmark(struct sk_buff *skb) 4444{ } 4445#endif 4446 4447static inline int secpath_exists(const struct sk_buff *skb) 4448{ 4449#ifdef CONFIG_XFRM 4450 return skb_ext_exist(skb, SKB_EXT_SEC_PATH); 4451#else 4452 return 0; 4453#endif 4454} 4455 4456static inline bool skb_irq_freeable(const struct sk_buff *skb) 4457{ 4458 return !skb->destructor && 4459 !secpath_exists(skb) && 4460 !skb_nfct(skb) && 4461 !skb->_skb_refdst && 4462 !skb_has_frag_list(skb); 4463} 4464 4465static inline void skb_set_queue_mapping(struct sk_buff *skb, u16 queue_mapping) 4466{ 4467 skb->queue_mapping = queue_mapping; 4468} 4469 4470static inline u16 skb_get_queue_mapping(const struct sk_buff *skb) 4471{ 4472 return skb->queue_mapping; 4473} 4474 4475static inline void skb_copy_queue_mapping(struct sk_buff *to, const struct sk_buff *from) 4476{ 4477 to->queue_mapping = from->queue_mapping; 4478} 4479 4480static inline void skb_record_rx_queue(struct sk_buff *skb, u16 rx_queue) 4481{ 4482 skb->queue_mapping = rx_queue + 1; 4483} 4484 4485static inline u16 skb_get_rx_queue(const struct sk_buff *skb) 4486{ 4487 return skb->queue_mapping - 1; 4488} 4489 4490static inline bool skb_rx_queue_recorded(const struct sk_buff *skb) 4491{ 4492 return skb->queue_mapping != 0; 4493} 4494 4495static inline void skb_set_dst_pending_confirm(struct sk_buff *skb, u32 val) 4496{ 4497 skb->dst_pending_confirm = val; 4498} 4499 4500static inline bool skb_get_dst_pending_confirm(const struct sk_buff *skb) 4501{ 4502 return skb->dst_pending_confirm != 0; 4503} 4504 4505static inline struct sec_path *skb_sec_path(const struct sk_buff *skb) 4506{ 4507#ifdef CONFIG_XFRM 4508 return skb_ext_find(skb, SKB_EXT_SEC_PATH); 4509#else 4510 return NULL; 4511#endif 4512} 4513 4514/* Keeps track of mac header offset relative to skb->head. 4515 * It is useful for TSO of Tunneling protocol. e.g. GRE. 4516 * For non-tunnel skb it points to skb_mac_header() and for 4517 * tunnel skb it points to outer mac header. 4518 * Keeps track of level of encapsulation of network headers. 4519 */ 4520struct skb_gso_cb { 4521 union { 4522 int mac_offset; 4523 int data_offset; 4524 }; 4525 int encap_level; 4526 __wsum csum; 4527 __u16 csum_start; 4528}; 4529#define SKB_GSO_CB_OFFSET 32 4530#define SKB_GSO_CB(skb) ((struct skb_gso_cb *)((skb)->cb + SKB_GSO_CB_OFFSET)) 4531 4532static inline int skb_tnl_header_len(const struct sk_buff *inner_skb) 4533{ 4534 return (skb_mac_header(inner_skb) - inner_skb->head) - 4535 SKB_GSO_CB(inner_skb)->mac_offset; 4536} 4537 4538static inline int gso_pskb_expand_head(struct sk_buff *skb, int extra) 4539{ 4540 int new_headroom, headroom; 4541 int ret; 4542 4543 headroom = skb_headroom(skb); 4544 ret = pskb_expand_head(skb, extra, 0, GFP_ATOMIC); 4545 if (ret) 4546 return ret; 4547 4548 new_headroom = skb_headroom(skb); 4549 SKB_GSO_CB(skb)->mac_offset += (new_headroom - headroom); 4550 return 0; 4551} 4552 4553static inline void gso_reset_checksum(struct sk_buff *skb, __wsum res) 4554{ 4555 /* Do not update partial checksums if remote checksum is enabled. */ 4556 if (skb->remcsum_offload) 4557 return; 4558 4559 SKB_GSO_CB(skb)->csum = res; 4560 SKB_GSO_CB(skb)->csum_start = skb_checksum_start(skb) - skb->head; 4561} 4562 4563/* Compute the checksum for a gso segment. First compute the checksum value 4564 * from the start of transport header to SKB_GSO_CB(skb)->csum_start, and 4565 * then add in skb->csum (checksum from csum_start to end of packet). 4566 * skb->csum and csum_start are then updated to reflect the checksum of the 4567 * resultant packet starting from the transport header-- the resultant checksum 4568 * is in the res argument (i.e. normally zero or ~ of checksum of a pseudo 4569 * header. 4570 */ 4571static inline __sum16 gso_make_checksum(struct sk_buff *skb, __wsum res) 4572{ 4573 unsigned char *csum_start = skb_transport_header(skb); 4574 int plen = (skb->head + SKB_GSO_CB(skb)->csum_start) - csum_start; 4575 __wsum partial = SKB_GSO_CB(skb)->csum; 4576 4577 SKB_GSO_CB(skb)->csum = res; 4578 SKB_GSO_CB(skb)->csum_start = csum_start - skb->head; 4579 4580 return csum_fold(csum_partial(csum_start, plen, partial)); 4581} 4582 4583static inline bool skb_is_gso(const struct sk_buff *skb) 4584{ 4585 return skb_shinfo(skb)->gso_size; 4586} 4587 4588/* Note: Should be called only if skb_is_gso(skb) is true */ 4589static inline bool skb_is_gso_v6(const struct sk_buff *skb) 4590{ 4591 return skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6; 4592} 4593 4594/* Note: Should be called only if skb_is_gso(skb) is true */ 4595static inline bool skb_is_gso_sctp(const struct sk_buff *skb) 4596{ 4597 return skb_shinfo(skb)->gso_type & SKB_GSO_SCTP; 4598} 4599 4600/* Note: Should be called only if skb_is_gso(skb) is true */ 4601static inline bool skb_is_gso_tcp(const struct sk_buff *skb) 4602{ 4603 return skb_shinfo(skb)->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6); 4604} 4605 4606static inline void skb_gso_reset(struct sk_buff *skb) 4607{ 4608 skb_shinfo(skb)->gso_size = 0; 4609 skb_shinfo(skb)->gso_segs = 0; 4610 skb_shinfo(skb)->gso_type = 0; 4611} 4612 4613static inline void skb_increase_gso_size(struct skb_shared_info *shinfo, 4614 u16 increment) 4615{ 4616 if (WARN_ON_ONCE(shinfo->gso_size == GSO_BY_FRAGS)) 4617 return; 4618 shinfo->gso_size += increment; 4619} 4620 4621static inline void skb_decrease_gso_size(struct skb_shared_info *shinfo, 4622 u16 decrement) 4623{ 4624 if (WARN_ON_ONCE(shinfo->gso_size == GSO_BY_FRAGS)) 4625 return; 4626 shinfo->gso_size -= decrement; 4627} 4628 4629void __skb_warn_lro_forwarding(const struct sk_buff *skb); 4630 4631static inline bool skb_warn_if_lro(const struct sk_buff *skb) 4632{ 4633 /* LRO sets gso_size but not gso_type, whereas if GSO is really 4634 * wanted then gso_type will be set. */ 4635 const struct skb_shared_info *shinfo = skb_shinfo(skb); 4636 4637 if (skb_is_nonlinear(skb) && shinfo->gso_size != 0 && 4638 unlikely(shinfo->gso_type == 0)) { 4639 __skb_warn_lro_forwarding(skb); 4640 return true; 4641 } 4642 return false; 4643} 4644 4645static inline void skb_forward_csum(struct sk_buff *skb) 4646{ 4647 /* Unfortunately we don't support this one. Any brave souls? */ 4648 if (skb->ip_summed == CHECKSUM_COMPLETE) 4649 skb->ip_summed = CHECKSUM_NONE; 4650} 4651 4652/** 4653 * skb_checksum_none_assert - make sure skb ip_summed is CHECKSUM_NONE 4654 * @skb: skb to check 4655 * 4656 * fresh skbs have their ip_summed set to CHECKSUM_NONE. 4657 * Instead of forcing ip_summed to CHECKSUM_NONE, we can 4658 * use this helper, to document places where we make this assertion. 4659 */ 4660static inline void skb_checksum_none_assert(const struct sk_buff *skb) 4661{ 4662#ifdef DEBUG 4663 BUG_ON(skb->ip_summed != CHECKSUM_NONE); 4664#endif 4665} 4666 4667bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off); 4668 4669int skb_checksum_setup(struct sk_buff *skb, bool recalculate); 4670struct sk_buff *skb_checksum_trimmed(struct sk_buff *skb, 4671 unsigned int transport_len, 4672 __sum16(*skb_chkf)(struct sk_buff *skb)); 4673 4674/** 4675 * skb_head_is_locked - Determine if the skb->head is locked down 4676 * @skb: skb to check 4677 * 4678 * The head on skbs build around a head frag can be removed if they are 4679 * not cloned. This function returns true if the skb head is locked down 4680 * due to either being allocated via kmalloc, or by being a clone with 4681 * multiple references to the head. 4682 */ 4683static inline bool skb_head_is_locked(const struct sk_buff *skb) 4684{ 4685 return !skb->head_frag || skb_cloned(skb); 4686} 4687 4688/* Local Checksum Offload. 4689 * Compute outer checksum based on the assumption that the 4690 * inner checksum will be offloaded later. 4691 * See Documentation/networking/checksum-offloads.rst for 4692 * explanation of how this works. 4693 * Fill in outer checksum adjustment (e.g. with sum of outer 4694 * pseudo-header) before calling. 4695 * Also ensure that inner checksum is in linear data area. 4696 */ 4697static inline __wsum lco_csum(struct sk_buff *skb) 4698{ 4699 unsigned char *csum_start = skb_checksum_start(skb); 4700 unsigned char *l4_hdr = skb_transport_header(skb); 4701 __wsum partial; 4702 4703 /* Start with complement of inner checksum adjustment */ 4704 partial = ~csum_unfold(*(__force __sum16 *)(csum_start + 4705 skb->csum_offset)); 4706 4707 /* Add in checksum of our headers (incl. outer checksum 4708 * adjustment filled in by caller) and return result. 4709 */ 4710 return csum_partial(l4_hdr, csum_start - l4_hdr, partial); 4711} 4712 4713static inline bool skb_is_redirected(const struct sk_buff *skb) 4714{ 4715 return skb->redirected; 4716} 4717 4718static inline void skb_set_redirected(struct sk_buff *skb, bool from_ingress) 4719{ 4720 skb->redirected = 1; 4721#ifdef CONFIG_NET_REDIRECT 4722 skb->from_ingress = from_ingress; 4723 if (skb->from_ingress) 4724 skb->tstamp = 0; 4725#endif 4726} 4727 4728static inline void skb_reset_redirect(struct sk_buff *skb) 4729{ 4730 skb->redirected = 0; 4731} 4732 4733static inline bool skb_csum_is_sctp(struct sk_buff *skb) 4734{ 4735 return skb->csum_not_inet; 4736} 4737 4738static inline void skb_set_kcov_handle(struct sk_buff *skb, 4739 const u64 kcov_handle) 4740{ 4741#ifdef CONFIG_KCOV 4742 skb->kcov_handle = kcov_handle; 4743#endif 4744} 4745 4746static inline u64 skb_get_kcov_handle(struct sk_buff *skb) 4747{ 4748#ifdef CONFIG_KCOV 4749 return skb->kcov_handle; 4750#else 4751 return 0; 4752#endif 4753} 4754 4755#ifdef CONFIG_PAGE_POOL 4756static inline void skb_mark_for_recycle(struct sk_buff *skb) 4757{ 4758 skb->pp_recycle = 1; 4759} 4760#endif 4761 4762static inline bool skb_pp_recycle(struct sk_buff *skb, void *data) 4763{ 4764 if (!IS_ENABLED(CONFIG_PAGE_POOL) || !skb->pp_recycle) 4765 return false; 4766 return page_pool_return_skb_page(virt_to_page(data)); 4767} 4768 4769#endif /* __KERNEL__ */ 4770#endif /* _LINUX_SKBUFF_H */