skbuff.h 110 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 *	Definitions for the 'struct sk_buff' memory handlers.
 *
 *	Authors:
 *		Alan Cox, <gw4pts@gw4pts.ampr.org>
 *		Florian La Roche, <rzsfl@rz.uni-sb.de>
 *
 *	This program is free software; you can redistribute it and/or
 *	modify it under the terms of the GNU General Public License
 *	as published by the Free Software Foundation; either version
 *	2 of the License, or (at your option) any later version.
 */

#ifndef _LINUX_SKBUFF_H
#define _LINUX_SKBUFF_H

#include <linux/kernel.h>
18
#include <linux/kmemcheck.h>
Linus Torvalds's avatar
Linus Torvalds committed
19 20
#include <linux/compiler.h>
#include <linux/time.h>
21
#include <linux/bug.h>
Linus Torvalds's avatar
Linus Torvalds committed
22
#include <linux/cache.h>
23
#include <linux/rbtree.h>
24
#include <linux/socket.h>
Linus Torvalds's avatar
Linus Torvalds committed
25

26
#include <linux/atomic.h>
Linus Torvalds's avatar
Linus Torvalds committed
27 28 29
#include <asm/types.h>
#include <linux/spinlock.h>
#include <linux/net.h>
30
#include <linux/textsearch.h>
Linus Torvalds's avatar
Linus Torvalds committed
31
#include <net/checksum.h>
32
#include <linux/rcupdate.h>
33
#include <linux/hrtimer.h>
34
#include <linux/dma-mapping.h>
35
#include <linux/netdev_features.h>
36
#include <linux/sched.h>
37
#include <net/flow_dissector.h>
38
#include <linux/splice.h>
39
#include <linux/in6.h>
40
#include <linux/if_packet.h>
41
#include <net/flow.h>
Linus Torvalds's avatar
Linus Torvalds committed
42

43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
/* The interface for checksum offload between the stack and networking drivers
 * is as follows...
 *
 * A. IP checksum related features
 *
 * Drivers advertise checksum offload capabilities in the features of a device.
 * From the stack's point of view these are capabilities offered by the driver,
 * a driver typically only advertises features that it is capable of offloading
 * to its device.
 *
 * The checksum related features are:
 *
 *	NETIF_F_HW_CSUM	- The driver (or its device) is able to compute one
 *			  IP (one's complement) checksum for any combination
 *			  of protocols or protocol layering. The checksum is
 *			  computed and set in a packet per the CHECKSUM_PARTIAL
 *			  interface (see below).
 *
 *	NETIF_F_IP_CSUM - Driver (device) is only able to checksum plain
 *			  TCP or UDP packets over IPv4. These are specifically
 *			  unencapsulated packets of the form IPv4|TCP or
 *			  IPv4|UDP where the Protocol field in the IPv4 header
 *			  is TCP or UDP. The IPv4 header may contain IP options
 *			  This feature cannot be set in features for a device
 *			  with NETIF_F_HW_CSUM also set. This feature is being
 *			  DEPRECATED (see below).
 *
 *	NETIF_F_IPV6_CSUM - Driver (device) is only able to checksum plain
 *			  TCP or UDP packets over IPv6. These are specifically
 *			  unencapsulated packets of the form IPv6|TCP or
 *			  IPv4|UDP where the Next Header field in the IPv6
 *			  header is either TCP or UDP. IPv6 extension headers
 *			  are not supported with this feature. This feature
 *			  cannot be set in features for a device with
 *			  NETIF_F_HW_CSUM also set. This feature is being
 *			  DEPRECATED (see below).
 *
 *	NETIF_F_RXCSUM - Driver (device) performs receive checksum offload.
 *			 This flag is used only used to disable the RX checksum
 *			 feature for a device. The stack will accept receive
 *			 checksum indication in packets received on a device
 *			 regardless of whether NETIF_F_RXCSUM is set.
 *
 * B. Checksumming of received packets by device. Indication of checksum
 *    verification is in set skb->ip_summed. Possible values are:
88 89 90
 *
 * CHECKSUM_NONE:
 *
91
 *   Device did not checksum this packet e.g. due to lack of capabilities.
92 93 94 95 96 97 98
 *   The packet contains full (though not verified) checksum in packet but
 *   not in skb->csum. Thus, skb->csum is undefined in this case.
 *
 * CHECKSUM_UNNECESSARY:
 *
 *   The hardware you're dealing with doesn't calculate the full checksum
 *   (as in CHECKSUM_COMPLETE), but it does parse headers and verify checksums
99 100
 *   for specific protocols. For such packets it will set CHECKSUM_UNNECESSARY
 *   if their checksums are okay. skb->csum is still undefined in this case
101 102
 *   though. A driver or device must never modify the checksum field in the
 *   packet even if checksum is verified.
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
 *
 *   CHECKSUM_UNNECESSARY is applicable to following protocols:
 *     TCP: IPv6 and IPv4.
 *     UDP: IPv4 and IPv6. A device may apply CHECKSUM_UNNECESSARY to a
 *       zero UDP checksum for either IPv4 or IPv6, the networking stack
 *       may perform further validation in this case.
 *     GRE: only if the checksum is present in the header.
 *     SCTP: indicates the CRC in SCTP header has been validated.
 *
 *   skb->csum_level indicates the number of consecutive checksums found in
 *   the packet minus one that have been verified as CHECKSUM_UNNECESSARY.
 *   For instance if a device receives an IPv6->UDP->GRE->IPv4->TCP packet
 *   and a device is able to verify the checksums for UDP (possibly zero),
 *   GRE (checksum flag is set), and TCP-- skb->csum_level would be set to
 *   two. If the device were only able to verify the UDP checksum and not
 *   GRE, either because it doesn't support GRE checksum of because GRE
 *   checksum is bad, skb->csum_level would be set to zero (TCP checksum is
 *   not considered in this case).
121 122 123 124 125 126 127 128 129 130 131 132
 *
 * CHECKSUM_COMPLETE:
 *
 *   This is the most generic way. The device supplied checksum of the _whole_
 *   packet as seen by netif_rx() and fills out in skb->csum. Meaning, the
 *   hardware doesn't need to parse L3/L4 headers to implement this.
 *
 *   Note: Even if device supports only some protocols, but is able to produce
 *   skb->csum, it MUST use CHECKSUM_COMPLETE, not CHECKSUM_UNNECESSARY.
 *
 * CHECKSUM_PARTIAL:
 *
133 134
 *   A checksum is set up to be offloaded to a device as described in the
 *   output description for CHECKSUM_PARTIAL. This may occur on a packet
135
 *   received directly from another Linux OS, e.g., a virtualized Linux kernel
136 137 138 139 140 141
 *   on the same host, or it may be set in the input path in GRO or remote
 *   checksum offload. For the purposes of checksum verification, the checksum
 *   referred to by skb->csum_start + skb->csum_offset and any preceding
 *   checksums in the packet are considered verified. Any checksums in the
 *   packet that are after the checksum being offloaded are not considered to
 *   be verified.
142
 *
143 144
 * C. Checksumming on transmit for non-GSO. The stack requests checksum offload
 *    in the skb->ip_summed for a packet. Values are:
145 146 147
 *
 * CHECKSUM_PARTIAL:
 *
148
 *   The driver is required to checksum the packet as seen by hard_start_xmit()
149
 *   from skb->csum_start up to the end, and to record/write the checksum at
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
 *   offset skb->csum_start + skb->csum_offset. A driver may verify that the
 *   csum_start and csum_offset values are valid values given the length and
 *   offset of the packet, however they should not attempt to validate that the
 *   checksum refers to a legitimate transport layer checksum-- it is the
 *   purview of the stack to validate that csum_start and csum_offset are set
 *   correctly.
 *
 *   When the stack requests checksum offload for a packet, the driver MUST
 *   ensure that the checksum is set correctly. A driver can either offload the
 *   checksum calculation to the device, or call skb_checksum_help (in the case
 *   that the device does not support offload for a particular checksum).
 *
 *   NETIF_F_IP_CSUM and NETIF_F_IPV6_CSUM are being deprecated in favor of
 *   NETIF_F_HW_CSUM. New devices should use NETIF_F_HW_CSUM to indicate
 *   checksum offload capability. If a	device has limited checksum capabilities
 *   (for instance can only perform NETIF_F_IP_CSUM or NETIF_F_IPV6_CSUM as
 *   described above) a helper function can be called to resolve
 *   CHECKSUM_PARTIAL. The helper functions are skb_csum_off_chk*. The helper
 *   function takes a spec argument that describes the protocol layer that is
 *   supported for checksum offload and can be called for each packet. If a
 *   packet does not match the specification for offload, skb_checksum_help
 *   is called to resolve the checksum.
172
 *
173
 * CHECKSUM_NONE:
174
 *
175 176
 *   The skb was already checksummed by the protocol, or a checksum is not
 *   required.
177 178 179
 *
 * CHECKSUM_UNNECESSARY:
 *
180 181
 *   This has the same meaning on as CHECKSUM_NONE for checksum offload on
 *   output.
182
 *
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
 * CHECKSUM_COMPLETE:
 *   Not used in checksum output. If a driver observes a packet with this value
 *   set in skbuff, if should treat as CHECKSUM_NONE being set.
 *
 * D. Non-IP checksum (CRC) offloads
 *
 *   NETIF_F_SCTP_CRC - This feature indicates that a device is capable of
 *     offloading the SCTP CRC in a packet. To perform this offload the stack
 *     will set ip_summed to CHECKSUM_PARTIAL and set csum_start and csum_offset
 *     accordingly. Note the there is no indication in the skbuff that the
 *     CHECKSUM_PARTIAL refers to an SCTP checksum, a driver that supports
 *     both IP checksum offload and SCTP CRC offload must verify which offload
 *     is configured for a packet presumably by inspecting packet headers.
 *
 *   NETIF_F_FCOE_CRC - This feature indicates that a device is capable of
 *     offloading the FCOE CRC in a packet. To perform this offload the stack
 *     will set ip_summed to CHECKSUM_PARTIAL and set csum_start and csum_offset
 *     accordingly. Note the there is no indication in the skbuff that the
 *     CHECKSUM_PARTIAL refers to an FCOE checksum, a driver that supports
 *     both IP checksum offload and FCOE CRC offload must verify which offload
 *     is configured for a packet presumably by inspecting packet headers.
 *
 * E. Checksumming on output with GSO.
 *
 * In the case of a GSO packet (skb_is_gso(skb) is true), checksum offload
 * is implied by the SKB_GSO_* flags in gso_type. Most obviously, if the
 * gso_type is SKB_GSO_TCPV4 or SKB_GSO_TCPV6, TCP checksum offload as
 * part of the GSO operation is implied. If a checksum is being offloaded
 * with GSO then ip_summed is CHECKSUM_PARTIAL, csum_start and csum_offset
 * are set to refer to the outermost checksum being offload (two offloaded
 * checksums are possible with UDP encapsulation).
214 215
 */

216
/* Don't change this without changing skb_csum_unnecessary! */
217 218 219 220
#define CHECKSUM_NONE		0
#define CHECKSUM_UNNECESSARY	1
#define CHECKSUM_COMPLETE	2
#define CHECKSUM_PARTIAL	3
Linus Torvalds's avatar
Linus Torvalds committed
221

222 223 224
/* Maximum value in skb->csum_level */
#define SKB_MAX_CSUM_LEVEL	3

225
#define SKB_DATA_ALIGN(X)	ALIGN(X, SMP_CACHE_BYTES)
226
#define SKB_WITH_OVERHEAD(X)	\
227
	((X) - SKB_DATA_ALIGN(sizeof(struct skb_shared_info)))
228 229
#define SKB_MAX_ORDER(X, ORDER) \
	SKB_WITH_OVERHEAD((PAGE_SIZE << (ORDER)) - (X))
Linus Torvalds's avatar
Linus Torvalds committed
230 231 232
#define SKB_MAX_HEAD(X)		(SKB_MAX_ORDER((X), 0))
#define SKB_MAX_ALLOC		(SKB_MAX_ORDER(0, 2))

233 234 235 236 237
/* return minimum truesize of one skb containing X bytes of data */
#define SKB_TRUESIZE(X) ((X) +						\
			 SKB_DATA_ALIGN(sizeof(struct sk_buff)) +	\
			 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)))

Linus Torvalds's avatar
Linus Torvalds committed
238
struct net_device;
239
struct scatterlist;
240
struct pipe_inode_info;
241
struct iov_iter;
242
struct napi_struct;
Linus Torvalds's avatar
Linus Torvalds committed
243

244
#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
Linus Torvalds's avatar
Linus Torvalds committed
245 246 247
struct nf_conntrack {
	atomic_t use;
};
248
#endif
Linus Torvalds's avatar
Linus Torvalds committed
249

250
#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
Linus Torvalds's avatar
Linus Torvalds committed
251
struct nf_bridge_info {
252
	atomic_t		use;
253 254 255 256
	enum {
		BRNF_PROTO_UNCHANGED,
		BRNF_PROTO_8021Q,
		BRNF_PROTO_PPPOE
257
	} orig_proto:8;
258 259 260
	u8			pkt_otherhost:1;
	u8			in_prerouting:1;
	u8			bridged_dnat:1;
261
	__u16			frag_max_size;
262
	struct net_device	*physindev;
263 264 265

	/* always valid & non-NULL from FORWARD on, for physdev match */
	struct net_device	*physoutdev;
266
	union {
267
		/* prerouting: detect dnat in orig/reply direction */
268 269
		__be32          ipv4_daddr;
		struct in6_addr ipv6_daddr;
270 271 272 273 274 275

		/* after prerouting + nat detected: store original source
		 * mac since neigh resolution overwrites it, only used while
		 * skb is out in neigh layer.
		 */
		char neigh_header[8];
276
	};
Linus Torvalds's avatar
Linus Torvalds committed
277 278 279 280 281 282 283 284 285 286 287 288 289 290
};
#endif

struct sk_buff_head {
	/* These two members must be first. */
	struct sk_buff	*next;
	struct sk_buff	*prev;

	__u32		qlen;
	spinlock_t	lock;
};

struct sk_buff;

291 292 293 294 295 296
/* To allow 64K frame to be packed as single skb without frag_list we
 * require 64K/PAGE_SIZE pages plus 1 additional page to allow for
 * buffers which do not start on a page boundary.
 *
 * Since GRO uses frags we allocate at least 16 regardless of page
 * size.
297
 */
298
#if (65536/PAGE_SIZE + 1) < 16
299
#define MAX_SKB_FRAGS 16UL
300
#else
301
#define MAX_SKB_FRAGS (65536/PAGE_SIZE + 1)
302
#endif
303
extern int sysctl_max_skb_frags;
Linus Torvalds's avatar
Linus Torvalds committed
304

305 306 307 308 309
/* Set skb_shinfo(skb)->gso_size to this in case you want skb_segment to
 * segment using its current segmentation instead.
 */
#define GSO_BY_FRAGS	0xFFFF

Linus Torvalds's avatar
Linus Torvalds committed
310 311 312
typedef struct skb_frag_struct skb_frag_t;

struct skb_frag_struct {
313 314 315
	struct {
		struct page *p;
	} page;
316
#if (BITS_PER_LONG > 32) || (PAGE_SIZE >= 65536)
317 318
	__u32 page_offset;
	__u32 size;
319 320 321 322
#else
	__u16 page_offset;
	__u16 size;
#endif
Linus Torvalds's avatar
Linus Torvalds committed
323 324
};

325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
static inline unsigned int skb_frag_size(const skb_frag_t *frag)
{
	return frag->size;
}

static inline void skb_frag_size_set(skb_frag_t *frag, unsigned int size)
{
	frag->size = size;
}

static inline void skb_frag_size_add(skb_frag_t *frag, int delta)
{
	frag->size += delta;
}

static inline void skb_frag_size_sub(skb_frag_t *frag, int delta)
{
	frag->size -= delta;
}

345 346 347
#define HAVE_HW_TIME_STAMP

/**
348
 * struct skb_shared_hwtstamps - hardware time stamps
349 350 351 352
 * @hwtstamp:	hardware time stamp transformed into duration
 *		since arbitrary point in time
 *
 * Software time stamps generated by ktime_get_real() are stored in
353
 * skb->tstamp.
354 355 356 357 358 359 360 361 362 363 364
 *
 * hwtstamps can only be compared against other hwtstamps from
 * the same device.
 *
 * This structure is attached to packets as part of the
 * &skb_shared_info. Use skb_hwtstamps() to get a pointer.
 */
struct skb_shared_hwtstamps {
	ktime_t	hwtstamp;
};

365 366 367 368 369
/* Definitions for tx_flags in struct skb_shared_info */
enum {
	/* generate hardware time stamp */
	SKBTX_HW_TSTAMP = 1 << 0,

370
	/* generate software time stamp when queueing packet to NIC */
371 372 373 374 375
	SKBTX_SW_TSTAMP = 1 << 1,

	/* device driver is going to provide hardware time stamp */
	SKBTX_IN_PROGRESS = 1 << 2,

376
	/* device driver supports TX zero-copy buffers */
377
	SKBTX_DEV_ZEROCOPY = 1 << 3,
378 379

	/* generate wifi status information (where possible) */
380
	SKBTX_WIFI_STATUS = 1 << 4,
381 382 383 384 385 386 387

	/* This indicates at least one fragment might be overwritten
	 * (as in vmsplice(), sendfile() ...)
	 * If we need to compute a TX checksum, we'll need to copy
	 * all frags to avoid possible bad checksum
	 */
	SKBTX_SHARED_FRAG = 1 << 5,
388 389 390

	/* generate software time stamp when entering packet scheduling */
	SKBTX_SCHED_TSTAMP = 1 << 6,
391 392
};

393
#define SKBTX_ANY_SW_TSTAMP	(SKBTX_SW_TSTAMP    | \
394
				 SKBTX_SCHED_TSTAMP)
395 396
#define SKBTX_ANY_TSTAMP	(SKBTX_HW_TSTAMP | SKBTX_ANY_SW_TSTAMP)

397 398 399
/*
 * The callback notifies userspace to release buffers when skb DMA is done in
 * lower device, the skb last reference should be 0 when calling this.
400 401
 * The zerocopy_success argument is true if zero copy transmit occurred,
 * false on data copy or out of memory error caused by data copy attempt.
402 403
 * The ctx field is used to track device context.
 * The desc field is used to track userspace buffer index.
404 405
 */
struct ubuf_info {
406
	void (*callback)(struct ubuf_info *, bool zerocopy_success);
407
	void *ctx;
408
	unsigned long desc;
409 410
};

Linus Torvalds's avatar
Linus Torvalds committed
411 412 413 414
/* This data is invariant across clones and lives at
 * the end of the header data, ie. at skb->end.
 */
struct skb_shared_info {
415 416
	unsigned char	nr_frags;
	__u8		tx_flags;
417 418 419 420
	unsigned short	gso_size;
	/* Warning: this field is not always filled in (UFO)! */
	unsigned short	gso_segs;
	unsigned short  gso_type;
Linus Torvalds's avatar
Linus Torvalds committed
421
	struct sk_buff	*frag_list;
422
	struct skb_shared_hwtstamps hwtstamps;
423
	u32		tskey;
424
	__be32          ip6_frag_id;
425 426 427 428 429 430

	/*
	 * Warning : all fields before dataref are cleared in __alloc_skb()
	 */
	atomic_t	dataref;

431 432 433
	/* Intermediate layers must ensure that destructor_arg
	 * remains valid until skb destructor */
	void *		destructor_arg;
434

435 436
	/* must be last field, see pskb_expand_head() */
	skb_frag_t	frags[MAX_SKB_FRAGS];
Linus Torvalds's avatar
Linus Torvalds committed
437 438 439 440
};

/* We divide dataref into two halves.  The higher 16 bits hold references
 * to the payload part of skb->data.  The lower 16 bits hold references to
441 442
 * the entire skb->data.  A clone of a headerless skb holds the length of
 * the header in skb->hdr_len.
Linus Torvalds's avatar
Linus Torvalds committed
443 444 445 446 447 448 449 450 451 452
 *
 * All users must obey the rule that the skb->data reference count must be
 * greater than or equal to the payload reference count.
 *
 * Holding a reference to the payload part means that the user does not
 * care about modifications to the header part of skb->data.
 */
#define SKB_DATAREF_SHIFT 16
#define SKB_DATAREF_MASK ((1 << SKB_DATAREF_SHIFT) - 1)

453 454

enum {
455 456 457
	SKB_FCLONE_UNAVAILABLE,	/* skb has no fclone (from head_cache) */
	SKB_FCLONE_ORIG,	/* orig skb (from fclone_cache) */
	SKB_FCLONE_CLONE,	/* companion fclone skb (from fclone_cache) */
458 459
};

460 461
enum {
	SKB_GSO_TCPV4 = 1 << 0,
462
	SKB_GSO_UDP = 1 << 1,
463 464 465

	/* This indicates the skb is from an untrusted source. */
	SKB_GSO_DODGY = 1 << 2,
466 467

	/* This indicates the tcp segment has CWR set. */
468 469
	SKB_GSO_TCP_ECN = 1 << 3,

470
	SKB_GSO_TCP_FIXEDID = 1 << 4,
471

472
	SKB_GSO_TCPV6 = 1 << 5,
473

474
	SKB_GSO_FCOE = 1 << 6,
475

476
	SKB_GSO_GRE = 1 << 7,
477

478
	SKB_GSO_GRE_CSUM = 1 << 8,
Eric Dumazet's avatar
Eric Dumazet committed
479

480
	SKB_GSO_IPXIP4 = 1 << 9,
481

482
	SKB_GSO_IPXIP6 = 1 << 10,
483

484
	SKB_GSO_UDP_TUNNEL = 1 << 11,
485

486 487
	SKB_GSO_UDP_TUNNEL_CSUM = 1 << 12,

488 489 490
	SKB_GSO_PARTIAL = 1 << 13,

	SKB_GSO_TUNNEL_REMCSUM = 1 << 14,
491 492

	SKB_GSO_SCTP = 1 << 15,
493 494
};

495 496 497 498 499 500 501 502 503 504
#if BITS_PER_LONG > 32
#define NET_SKBUFF_DATA_USES_OFFSET 1
#endif

#ifdef NET_SKBUFF_DATA_USES_OFFSET
typedef unsigned int sk_buff_data_t;
#else
typedef unsigned char *sk_buff_data_t;
#endif

505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554
/**
 * struct skb_mstamp - multi resolution time stamps
 * @stamp_us: timestamp in us resolution
 * @stamp_jiffies: timestamp in jiffies
 */
struct skb_mstamp {
	union {
		u64		v64;
		struct {
			u32	stamp_us;
			u32	stamp_jiffies;
		};
	};
};

/**
 * skb_mstamp_get - get current timestamp
 * @cl: place to store timestamps
 */
static inline void skb_mstamp_get(struct skb_mstamp *cl)
{
	u64 val = local_clock();

	do_div(val, NSEC_PER_USEC);
	cl->stamp_us = (u32)val;
	cl->stamp_jiffies = (u32)jiffies;
}

/**
 * skb_mstamp_delta - compute the difference in usec between two skb_mstamp
 * @t1: pointer to newest sample
 * @t0: pointer to oldest sample
 */
static inline u32 skb_mstamp_us_delta(const struct skb_mstamp *t1,
				      const struct skb_mstamp *t0)
{
	s32 delta_us = t1->stamp_us - t0->stamp_us;
	u32 delta_jiffies = t1->stamp_jiffies - t0->stamp_jiffies;

	/* If delta_us is negative, this might be because interval is too big,
	 * or local_clock() drift is too big : fallback using jiffies.
	 */
	if (delta_us <= 0 ||
	    delta_jiffies >= (INT_MAX / (USEC_PER_SEC / HZ)))

		delta_us = jiffies_to_usecs(delta_jiffies);

	return delta_us;
}

555 556 557 558 559 560 561 562 563
static inline bool skb_mstamp_after(const struct skb_mstamp *t1,
				    const struct skb_mstamp *t0)
{
	s32 diff = t1->stamp_jiffies - t0->stamp_jiffies;

	if (!diff)
		diff = t1->stamp_us - t0->stamp_us;
	return diff > 0;
}
564

Linus Torvalds's avatar
Linus Torvalds committed
565 566 567 568
/** 
 *	struct sk_buff - socket buffer
 *	@next: Next buffer in list
 *	@prev: Previous buffer in list
569
 *	@tstamp: Time we arrived/left
570
 *	@rbnode: RB tree node, alternative to next/prev for netem/tcp
571
 *	@sk: Socket we are owned by
Linus Torvalds's avatar
Linus Torvalds committed
572
 *	@dev: Device we arrived on/are leaving by
573
 *	@cb: Control buffer. Free for use by every layer. Put private vars here
574
 *	@_skb_refdst: destination entry (with norefcount bit)
575
 *	@sp: the security path, used for xfrm
Linus Torvalds's avatar
Linus Torvalds committed
576 577 578
 *	@len: Length of actual data
 *	@data_len: Data length
 *	@mac_len: Length of link layer header
579
 *	@hdr_len: writable header length of cloned skb
580 581 582
 *	@csum: Checksum (must include start/offset pair)
 *	@csum_start: Offset from skb->head where checksumming should start
 *	@csum_offset: Offset from csum_start where checksum should be stored
583
 *	@priority: Packet queueing priority
584
 *	@ignore_df: allow local fragmentation
Linus Torvalds's avatar
Linus Torvalds committed
585
 *	@cloned: Head may be cloned (check refcnt to be sure)
586
 *	@ip_summed: Driver fed us an IP checksum
Linus Torvalds's avatar
Linus Torvalds committed
587
 *	@nohdr: Payload reference only, must not modify header
588
 *	@nfctinfo: Relationship of this skb to the connection
Linus Torvalds's avatar
Linus Torvalds committed
589
 *	@pkt_type: Packet class
590 591
 *	@fclone: skbuff clone status
 *	@ipvs_property: skbuff is owned by ipvs
592 593
 *	@peeked: this packet has been seen already, so stats have been
 *		done for it, don't do them again
594
 *	@nf_trace: netfilter packet trace flag
595 596 597
 *	@protocol: Packet protocol from driver
 *	@destructor: Destruct function
 *	@nfct: Associated connection, if any
Linus Torvalds's avatar
Linus Torvalds committed
598
 *	@nf_bridge: Saved data about a bridged frame - see br_netfilter.c
599
 *	@skb_iif: ifindex of device we arrived on
Linus Torvalds's avatar
Linus Torvalds committed
600 601
 *	@tc_index: Traffic control index
 *	@tc_verd: traffic control verdict
602
 *	@hash: the packet hash
603
 *	@queue_mapping: Queue mapping for multiqueue devices
604
 *	@xmit_more: More SKBs are pending for this queue
605
 *	@ndisc_nodetype: router type (from link layer)
606
 *	@ooo_okay: allow the mapping of a socket to a queue to be changed
607
 *	@l4_hash: indicate hash is a canonical 4-tuple hash over transport
608
 *		ports.
609
 *	@sw_hash: indicates hash was computed in software stack
610 611
 *	@wifi_acked_valid: wifi_acked was set
 *	@wifi_acked: whether frame was acked on wifi or not
612
 *	@no_fcs:  Request NIC to treat last 4 bytes as Ethernet FCS
613
  *	@napi_id: id of the NAPI struct this skb came from
614
 *	@secmark: security marking
615
 *	@offload_fwd_mark: fwding offload mark
616
 *	@mark: Generic packet mark
617
 *	@vlan_proto: vlan encapsulation protocol
618
 *	@vlan_tci: vlan tag control information
619
 *	@inner_protocol: Protocol (encapsulation)
620 621
 *	@inner_transport_header: Inner transport layer header (encapsulation)
 *	@inner_network_header: Network layer header (encapsulation)
622
 *	@inner_mac_header: Link layer header (encapsulation)
623 624 625 626 627 628 629 630 631
 *	@transport_header: Transport layer header
 *	@network_header: Network layer header
 *	@mac_header: Link layer header
 *	@tail: Tail pointer
 *	@end: End pointer
 *	@head: Head of buffer
 *	@data: Data head pointer
 *	@truesize: Buffer size
 *	@users: User count - see {datagram,tcp}.c
Linus Torvalds's avatar
Linus Torvalds committed
632 633 634
 */

struct sk_buff {
635
	union {
636 637 638 639 640 641 642 643 644 645 646
		struct {
			/* These two members must be first. */
			struct sk_buff		*next;
			struct sk_buff		*prev;

			union {
				ktime_t		tstamp;
				struct skb_mstamp skb_mstamp;
			};
		};
		struct rb_node	rbnode; /* used in netem & tcp stack */
647
	};
648
	struct sock		*sk;
Linus Torvalds's avatar
Linus Torvalds committed
649 650 651 652 653 654 655 656
	struct net_device	*dev;

	/*
	 * This is the control buffer. It is free to use for every
	 * layer. Please put your private variables there. If you
	 * want to keep them across layers you have to do a skb_clone()
	 * first. This is owned by whoever has the skb queued ATM.
	 */
657
	char			cb[48] __aligned(8);
Linus Torvalds's avatar
Linus Torvalds committed
658

659
	unsigned long		_skb_refdst;
660
	void			(*destructor)(struct sk_buff *skb);
661 662
#ifdef CONFIG_XFRM
	struct	sec_path	*sp;
663 664 665 666
#endif
#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
	struct nf_conntrack	*nfct;
#endif
667
#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
668
	struct nf_bridge_info	*nf_bridge;
669
#endif
Linus Torvalds's avatar
Linus Torvalds committed
670
	unsigned int		len,
671 672 673
				data_len;
	__u16			mac_len,
				hdr_len;
674 675 676 677

	/* Following fields are _not_ copied in __copy_skb_header()
	 * Note that queue_mapping is here mostly to fill a hole.
	 */
678
	kmemcheck_bitfield_begin(flags1);
679 680
	__u16			queue_mapping;
	__u8			cloned:1,
681
				nohdr:1,
682
				fclone:2,
683
				peeked:1,
684 685 686
				head_frag:1,
				xmit_more:1;
	/* one bit hole */
687
	kmemcheck_bitfield_end(flags1);
688

689 690 691
	/* fields enclosed in headers_start/headers_end are copied
	 * using a single memcpy() in __copy_skb_header()
	 */
692
	/* private: */
693
	__u32			headers_start[0];
694
	/* public: */
695

696 697 698 699 700
/* if you move pkt_type around you also must adapt those constants */
#ifdef __BIG_ENDIAN_BITFIELD
#define PKT_TYPE_MAX	(7 << 5)
#else
#define PKT_TYPE_MAX	7
Linus Torvalds's avatar
Linus Torvalds committed
701
#endif
702
#define PKT_TYPE_OFFSET()	offsetof(struct sk_buff, __pkt_type_offset)
703

704
	__u8			__pkt_type_offset[0];
705
	__u8			pkt_type:3;
706
	__u8			pfmemalloc:1;
707 708 709 710 711
	__u8			ignore_df:1;
	__u8			nfctinfo:3;

	__u8			nf_trace:1;
	__u8			ip_summed:2;
712
	__u8			ooo_okay:1;
713
	__u8			l4_hash:1;
714
	__u8			sw_hash:1;
715 716
	__u8			wifi_acked_valid:1;
	__u8			wifi_acked:1;
717

718
	__u8			no_fcs:1;
719
	/* Indicates the inner headers are valid in the skbuff. */
720
	__u8			encapsulation:1;
721
	__u8			encap_hdr_csum:1;
722
	__u8			csum_valid:1;
723
	__u8			csum_complete_sw:1;
724 725
	__u8			csum_level:2;
	__u8			csum_bad:1;
726

727 728 729 730
#ifdef CONFIG_IPV6_NDISC_NODETYPE
	__u8			ndisc_nodetype:2;
#endif
	__u8			ipvs_property:1;
731
	__u8			inner_protocol_type:1;
732 733
	__u8			remcsum_offload:1;
	/* 3 or 5 bit hole */
734 735 736 737 738 739 740

#ifdef CONFIG_NET_SCHED
	__u16			tc_index;	/* traffic control index */
#ifdef CONFIG_NET_CLS_ACT
	__u16			tc_verd;	/* traffic control verdict */
#endif
#endif
741

742 743 744 745 746 747 748 749 750 751 752 753
	union {
		__wsum		csum;
		struct {
			__u16	csum_start;
			__u16	csum_offset;
		};
	};
	__u32			priority;
	int			skb_iif;
	__u32			hash;
	__be16			vlan_proto;
	__u16			vlan_tci;
754 755 756 757 758
#if defined(CONFIG_NET_RX_BUSY_POLL) || defined(CONFIG_XPS)
	union {
		unsigned int	napi_id;
		unsigned int	sender_cpu;
	};
759
#endif
760
	union {
761
#ifdef CONFIG_NETWORK_SECMARK
762 763 764 765
		__u32		secmark;
#endif
#ifdef CONFIG_NET_SWITCHDEV
		__u32		offload_fwd_mark;
766
#endif
767 768
	};

769 770
	union {
		__u32		mark;
Eric Dumazet's avatar
Eric Dumazet committed
771
		__u32		reserved_tailroom;
772
	};
Linus Torvalds's avatar
Linus Torvalds committed
773

774 775 776 777 778
	union {
		__be16		inner_protocol;
		__u8		inner_ipproto;
	};

779 780 781
	__u16			inner_transport_header;
	__u16			inner_network_header;
	__u16			inner_mac_header;
782 783

	__be16			protocol;
784 785 786
	__u16			transport_header;
	__u16			network_header;
	__u16			mac_header;
787

788
	/* private: */
789
	__u32			headers_end[0];
790
	/* public: */
791

Linus Torvalds's avatar
Linus Torvalds committed
792
	/* These elements must be at the end, see alloc_skb() for details.  */
793
	sk_buff_data_t		tail;
794
	sk_buff_data_t		end;
Linus Torvalds's avatar
Linus Torvalds committed
795
	unsigned char		*head,
796
				*data;
797 798
	unsigned int		truesize;
	atomic_t		users;
Linus Torvalds's avatar
Linus Torvalds committed
799 800 801 802 803 804 805 806 807
};

#ifdef __KERNEL__
/*
 *	Handling routines are only of interest to the kernel
 */
#include <linux/slab.h>


808 809
#define SKB_ALLOC_FCLONE	0x01
#define SKB_ALLOC_RX		0x02
810
#define SKB_ALLOC_NAPI		0x04
811 812 813 814 815 816 817

/* Returns true if the skb was allocated from PFMEMALLOC reserves */
static inline bool skb_pfmemalloc(const struct sk_buff *skb)
{
	return unlikely(skb->pfmemalloc);
}

818 819 820 821 822 823 824 825 826 827 828 829 830
/*
 * skb might have a dst pointer attached, refcounted or not.
 * _skb_refdst low order bit is set if refcount was _not_ taken
 */
#define SKB_DST_NOREF	1UL
#define SKB_DST_PTRMASK	~(SKB_DST_NOREF)

/**
 * skb_dst - returns skb dst_entry
 * @skb: buffer
 *
 * Returns skb dst_entry, regardless of reference taken or not.
 */
Eric Dumazet's avatar
Eric Dumazet committed
831 832
static inline struct dst_entry *skb_dst(const struct sk_buff *skb)
{
833 834 835 836 837 838 839
	/* If refdst was not refcounted, check we still are in a 
	 * rcu_read_lock section
	 */
	WARN_ON((skb->_skb_refdst & SKB_DST_NOREF) &&
		!rcu_read_lock_held() &&
		!rcu_read_lock_bh_held());
	return (struct dst_entry *)(skb->_skb_refdst & SKB_DST_PTRMASK);
Eric Dumazet's avatar
Eric Dumazet committed
840 841
}

842 843 844 845 846 847 848 849
/**
 * skb_dst_set - sets skb dst
 * @skb: buffer
 * @dst: dst entry
 *
 * Sets skb dst, assuming a reference was taken on dst and should
 * be released by skb_dst_drop()
 */
Eric Dumazet's avatar
Eric Dumazet committed
850 851
static inline void skb_dst_set(struct sk_buff *skb, struct dst_entry *dst)
{
852 853 854
	skb->_skb_refdst = (unsigned long)dst;
}

855 856 857 858 859 860 861 862 863 864 865 866
/**
 * skb_dst_set_noref - sets skb dst, hopefully, without taking reference
 * @skb: buffer
 * @dst: dst entry
 *
 * Sets skb dst, assuming a reference was not taken on dst.
 * If dst entry is cached, we do not take reference and dst_release
 * will be avoided by refdst_drop. If dst entry is not cached, we take
 * reference, so that last dst_release can destroy the dst immediately.
 */
static inline void skb_dst_set_noref(struct sk_buff *skb, struct dst_entry *dst)
{
867 868
	WARN_ON(!rcu_read_lock_held() && !rcu_read_lock_bh_held());
	skb->_skb_refdst = (unsigned long)dst | SKB_DST_NOREF;
869
}
870 871

/**
872
 * skb_dst_is_noref - Test if skb dst isn't refcounted
873 874 875 876 877
 * @skb: buffer
 */
static inline bool skb_dst_is_noref(const struct sk_buff *skb)
{
	return (skb->_skb_refdst & SKB_DST_NOREF) && skb_dst(skb);
Eric Dumazet's avatar
Eric Dumazet committed
878 879
}

Eric Dumazet's avatar
Eric Dumazet committed
880 881
static inline struct rtable *skb_rtable(const struct sk_buff *skb)
{
Eric Dumazet's avatar
Eric Dumazet committed
882
	return (struct rtable *)skb_dst(skb);
Eric Dumazet's avatar
Eric Dumazet committed
883 884
}

885 886 887 888 889 890 891 892 893
/* For mangling skb->pkt_type from user space side from applications
 * such as nft, tc, etc, we only allow a conservative subset of
 * possible pkt_types to be set.
*/
static inline bool skb_pkt_type_ok(u32 ptype)
{
	return ptype <= PACKET_OTHERHOST;
}

894 895 896 897 898
void kfree_skb(struct sk_buff *skb);
void kfree_skb_list(struct sk_buff *segs);
void skb_tx_error(struct sk_buff *skb);
void consume_skb(struct sk_buff *skb);
void  __kfree_skb(struct sk_buff *skb);
899
extern struct kmem_cache *skbuff_head_cache;
900

901 902 903
void kfree_skb_partial(struct sk_buff *skb, bool head_stolen);
bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
		      bool *fragstolen, int *delta_truesize);
904

905 906
struct sk_buff *__alloc_skb(unsigned int size, gfp_t priority, int flags,
			    int node);
907
struct sk_buff *__build_skb(void *data, unsigned int frag_size);
908
struct sk_buff *build_skb(void *data, unsigned int frag_size);
909
static inline struct sk_buff *alloc_skb(unsigned int size,
910
					gfp_t priority)
911
{
912
	return __alloc_skb(size, priority, 0, NUMA_NO_NODE);
913 914
}

915 916 917 918 919 920
struct sk_buff *alloc_skb_with_frags(unsigned long header_len,
				     unsigned long data_len,
				     int max_page_order,
				     int *errcode,
				     gfp_t gfp_mask);

921 922 923 924 925 926 927 928 929 930 931 932 933
/* Layout of fast clones : [skb1][skb2][fclone_ref] */
struct sk_buff_fclones {
	struct sk_buff	skb1;

	struct sk_buff	skb2;

	atomic_t	fclone_ref;
};

/**
 *	skb_fclone_busy - check if fclone is busy
 *	@skb: buffer
 *
934
 * Returns true if skb is a fast clone, and its clone is not freed.
935 936
 * Some drivers call skb_orphan() in their ndo_start_xmit(),
 * so we also check that this didnt happen.
937
 */
938 939
static inline bool skb_fclone_busy(const struct sock *sk,
				   const struct sk_buff *skb)
940 941 942 943 944 945
{
	const struct sk_buff_fclones *fclones;

	fclones = container_of(skb, struct sk_buff_fclones, skb1);

	return skb->fclone == SKB_FCLONE_ORIG &&
946
	       atomic_read(&fclones->fclone_ref) > 1 &&
947
	       fclones->skb2.sk == sk;
948 949
}

950
static inline struct sk_buff *alloc_skb_fclone(unsigned int size,
951
					       gfp_t priority)
952
{
953
	return __alloc_skb(size, priority, SKB_ALLOC_FCLONE, NUMA_NO_NODE);
954 955
}

956
struct sk_buff *__alloc_skb_head(gfp_t priority, int node);
957 958 959 960 961
static inline struct sk_buff *alloc_skb_head(gfp_t priority)
{
	return __alloc_skb_head(priority, -1);
}

962 963 964 965
struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src);
int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask);
struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t priority);
struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t priority);
966 967 968 969 970 971 972
struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom,
				   gfp_t gfp_mask, bool fclone);
static inline struct sk_buff *__pskb_copy(struct sk_buff *skb, int headroom,
					  gfp_t gfp_mask)
{
	return __pskb_copy_fclone(skb, headroom, gfp_mask, false);
}
973 974 975 976 977 978

int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail, gfp_t gfp_mask);
struct sk_buff *skb_realloc_headroom(struct sk_buff *skb,
				     unsigned int headroom);
struct sk_buff *skb_copy_expand(const struct sk_buff *skb, int newheadroom,
				int newtailroom, gfp_t priority);
979 980
int skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg,
			int offset, int len);
981 982 983 984
int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset,
		 int len);
int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer);
int skb_pad(struct sk_buff *skb, int pad);
985
#define dev_kfree_skb(a)	consume_skb(a)
Linus Torvalds's avatar
Linus Torvalds committed
986

987 988 989 990
int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
			    int getfrag(void *from, char *to, int offset,
					int len, int odd, struct sk_buff *skb),
			    void *from, int length);
991

992 993 994
int skb_append_pagefrags(struct sk_buff *skb, struct page *page,
			 int offset, size_t size);

995
struct skb_seq_state {
996 997 998 999 1000 1001 1002 1003 1004
	__u32		lower_offset;
	__u32		upper_offset;
	__u32		frag_idx;
	__u32		stepped_offset;
	struct sk_buff	*root_skb;
	struct sk_buff	*cur_skb;
	__u8		*frag_data;
};

1005 1006 1007 1008 1009
void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
			  unsigned int to, struct skb_seq_state *st);
unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
			  struct skb_seq_state *st);
void skb_abort_seq_read(struct skb_seq_state *st);
1010

1011
unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
1012
			   unsigned int to, struct ts_config *config);
1013

1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046
/*
 * Packet hash types specify the type of hash in skb_set_hash.
 *
 * Hash types refer to the protocol layer addresses which are used to
 * construct a packet's hash. The hashes are used to differentiate or identify
 * flows of the protocol layer for the hash type. Hash types are either
 * layer-2 (L2), layer-3 (L3), or layer-4 (L4).
 *
 * Properties of hashes:
 *
 * 1) Two packets in different flows have different hash values
 * 2) Two packets in the same flow should have the same hash value
 *
 * A hash at a higher layer is considered to be more specific. A driver should
 * set the most specific hash possible.
 *
 * A driver cannot indicate a more specific hash than the layer at which a hash
 * was computed. For instance an L3 hash cannot be set as an L4 hash.
 *
 * A driver may indicate a hash level which is less specific than the
 * actual layer the hash was computed on. For instance, a hash computed
 * at L4 may be considered an L3 hash. This should only be done if the
 * driver can't unambiguously determine that the HW computed the hash at
 * the higher layer. Note that the "should" in the second property above
 * permits this.
 */
enum pkt_hash_types {
	PKT_HASH_TYPE_NONE,	/* Undefined type */
	PKT_HASH_TYPE_L2,	/* Input: src_MAC, dest_MAC */
	PKT_HASH_TYPE_L3,	/* Input: src_IP, dst_IP */
	PKT_HASH_TYPE_L4,	/* Input: src_IP, dst_IP, src_port, dst_port */
};

1047
static inline void skb_clear_hash(struct sk_buff *skb)
1048
{
1049
	skb->hash = 0;
1050
	skb->sw_hash = 0;
1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064
	skb->l4_hash = 0;
}

static inline void skb_clear_hash_if_not_l4(struct sk_buff *skb)
{
	if (!skb->l4_hash)
		skb_clear_hash(skb);
}

static inline void
__skb_set_hash(struct sk_buff *skb, __u32 hash, bool is_sw, bool is_l4)
{
	skb->l4_hash = is_l4;
	skb->sw_hash = is_sw;
1065
	skb->hash = hash;
1066 1067
}

1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080
static inline void
skb_set_hash(struct sk_buff *skb, __u32 hash, enum pkt_hash_types type)
{
	/* Used by drivers to set hash from HW */
	__skb_set_hash(skb, hash, false, type == PKT_HASH_TYPE_L4);
}

static inline void
__skb_set_sw_hash(struct sk_buff *skb, __u32 hash, bool is_l4)
{
	__skb_set_hash(skb, hash, true, is_l4);
}

1081
void __skb_get_hash(struct sk_buff *skb);
1082
u32 __skb_get_hash_symmetric(struct sk_buff *skb);
1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101
u32 skb_get_poff(const struct sk_buff *skb);
u32 __skb_get_poff(const struct sk_buff *skb, void *data,
		   const struct flow_keys *keys, int hlen);
__be32 __skb_flow_get_ports(const struct sk_buff *skb, int thoff, u8 ip_proto,
			    void *data, int hlen_proto);

static inline __be32 skb_flow_get_ports(const struct sk_buff *skb,
					int thoff, u8 ip_proto)
{
	return __skb_flow_get_ports(skb, thoff, ip_proto, NULL, 0);
}

void skb_flow_dissector_init(struct flow_dissector *flow_dissector,
			     const struct flow_dissector_key *key,
			     unsigned int key_count);

bool __skb_flow_dissect(const struct sk_buff *skb,
			struct flow_dissector *flow_dissector,
			void *target_container,
1102 1103
			void *data, __be16 proto, int nhoff, int hlen,
			unsigned int flags);
1104 1105 1106

static inline bool skb_flow_dissect(const struct sk_buff *skb,
				    struct flow_dissector *flow_dissector,
1107
				    void *target_container, unsigned int flags)
1108 1109
{
	return __skb_flow_dissect(skb, flow_dissector, target_container,
1110
				  NULL, 0, 0, 0, flags);
1111 1112 1113
}

static inline bool skb_flow_dissect_flow_keys(const struct sk_buff *skb,
1114 1115
					      struct flow_keys *flow,
					      unsigned int flags)
1116 1117 1118
{
	memset(flow, 0, sizeof(*flow));
	return __skb_flow_dissect(skb, &flow_keys_dissector, flow,
1119
				  NULL, 0, 0, 0, flags);
1120 1121 1122 1123
}

static inline bool skb_flow_dissect_flow_keys_buf(struct flow_keys *flow,
						  void *data, __be16 proto,
1124 1125
						  int nhoff, int hlen,
						  unsigned int flags)
1126 1127 1128
{
	memset(flow, 0, sizeof(*flow));
	return __skb_flow_dissect(NULL, &flow_keys_buf_dissector, flow,
1129
				  data, proto, nhoff, hlen, flags);
1130 1131
}

1132
static inline __u32 skb_get_hash(struct sk_buff *skb)
1133
{
1134
	if (!skb->l4_hash && !skb->sw_hash)
1135
		__skb_get_hash(skb);
1136

1137
	return skb->hash;
1138 1139
}

1140
__u32 __skb_get_hash_flowi6(struct sk_buff *skb, const struct flowi6 *fl6);
1141

1142
static inline __u32 skb_get_hash_flowi6(struct sk_buff *skb, const struct flowi6 *fl6)
1143
{
1144 1145
	if (!skb->l4_hash && !skb->sw_hash) {
		struct flow_keys keys;
1146
		__u32 hash = __get_hash_from_flowi6(fl6, &keys);
1147

1148
		__skb_set_sw_hash(skb, hash, flow_keys_have_l4(&keys));
1149
	}
1150 1151 1152 1153

	return skb->hash;
}

1154
__u32 __skb_get_hash_flowi4(struct sk_buff *skb, const struct flowi4 *fl);
1155

1156
static inline __u32 skb_get_hash_flowi4(struct sk_buff *skb, const struct flowi4 *fl4)
1157
{
1158 1159
	if (!skb->l4_hash && !skb->sw_hash) {
		struct flow_keys keys;
1160
		__u32 hash = __get_hash_from_flowi4(fl4, &keys);
1161

1162
		__skb_set_sw_hash(skb, hash, flow_keys_have_l4(&keys));
1163
	}
1164 1165 1166 1167

	return skb->hash;
}

1168 1169
__u32 skb_get_hash_perturb(const struct sk_buff *skb, u32 perturb);

Tom Herbert's avatar
Tom Herbert committed
1170 1171
static inline __u32 skb_get_hash_raw(const struct sk_buff *skb)
{
1172
	return skb->hash;
Tom Herbert's avatar
Tom Herbert committed
1173 1174
}

1175 1176
static inline void skb_copy_hash(struct sk_buff *to, const struct sk_buff *from)
{
1177
	to->hash = from->hash;
1178
	to->sw_hash = from->sw_hash;
1179
	to->l4_hash = from->l4_hash;
1180 1181
};

1182 1183 1184 1185 1186
#ifdef NET_SKBUFF_DATA_USES_OFFSET
static inline unsigned char *skb_end_pointer(const struct sk_buff *skb)
{
	return skb->head + skb->end;
}
1187 1188 1189 1190 1191

static inline unsigned int skb_end_offset(const struct sk_buff *skb)
{
	return skb->end;
}
1192 1193 1194 1195 1196
#else
static inline unsigned char *skb_end_pointer(const struct sk_buff *skb)
{
	return skb->end;
}
1197 1198 1199 1200 1201

static inline unsigned int skb_end_offset(const struct sk_buff *skb)
{
	return skb->end - skb->head;
}
1202 1203
#endif

Linus Torvalds's avatar
Linus Torvalds committed
1204
/* Internal */
1205
#define skb_shinfo(SKB)	((struct skb_shared_info *)(skb_end_pointer(SKB)))
Linus Torvalds's avatar
Linus Torvalds committed
1206

1207 1208 1209 1210 1211
static inline struct skb_shared_hwtstamps *skb_hwtstamps(struct sk_buff *skb)
{
	return &skb_shinfo(skb)->hwtstamps;
}

Linus Torvalds's avatar
Linus Torvalds committed
1212 1213 1214 1215 1216 1217 1218 1219
/**
 *	skb_queue_empty - check if a queue is empty
 *	@list: queue head
 *
 *	Returns true if the queue is empty, false otherwise.
 */
static inline int skb_queue_empty(const struct sk_buff_head *list)
{
1220
	return list->next == (const struct sk_buff *) list;
Linus Torvalds's avatar
Linus Torvalds committed
1221 1222
}

1223 1224 1225 1226 1227 1228 1229 1230 1231 1232
/**
 *	skb_queue_is_last - check if skb is the last entry in the queue
 *	@list: queue head
 *	@skb: buffer
 *
 *	Returns true if @skb is the last buffer on the list.
 */
static inline bool skb_queue_is_last(const struct sk_buff_head *list,
				     const struct sk_buff *skb)
{
1233
	return skb->next == (const struct sk_buff *) list;
1234 1235
}

1236 1237 1238 1239 1240 1241 1242 1243 1244 1245
/**
 *	skb_queue_is_first - check if skb is the first entry in the queue
 *	@list: queue head
 *	@skb: buffer
 *
 *	Returns true if @skb is the first buffer on the list.
 */
static inline bool skb_queue_is_first(const struct sk_buff_head *list,
				      const struct sk_buff *skb)
{
1246
	return skb->prev == (const struct sk_buff *) list;
1247 1248
}

1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266
/**
 *	skb_queue_next - return the next packet in the queue
 *	@list: queue head
 *	@skb: current buffer
 *
 *	Return the next packet in @list after @skb.  It is only valid to
 *	call this if skb_queue_is_last() evaluates to false.
 */
static inline struct sk_buff *skb_queue_next(const struct sk_buff_head *list,
					     const struct sk_buff *skb)
{
	/* This BUG_ON may seem severe, but if we just return then we
	 * are going to dereference garbage.
	 */
	BUG_ON(skb_queue_is_last(list, skb));
	return skb->next;
}

1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284
/**
 *	skb_queue_prev - return the prev packet in the queue
 *	@list: queue head
 *	@skb: current buffer
 *
 *	Return the prev packet in @list before @skb.  It is only valid to
 *	call this if skb_queue_is_first() evaluates to false.
 */
static inline struct sk_buff *skb_queue_prev(const struct sk_buff_head *list,
					     const struct sk_buff *skb)
{
	/* This BUG_ON may seem severe, but if we just return then we
	 * are going to dereference garbage.
	 */
	BUG_ON(skb_queue_is_first(list, skb));
	return skb->prev;
}

Linus Torvalds's avatar
Linus Torvalds committed
1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316
/**
 *	skb_get - reference buffer
 *	@skb: buffer to reference
 *
 *	Makes another reference to a socket buffer and returns a pointer
 *	to the buffer.
 */
static inline struct sk_buff *skb_get(struct sk_buff *skb)
{
	atomic_inc(&skb->users);
	return skb;
}

/*
 * If users == 1, we are the only owner and are can avoid redundant
 * atomic change.
 */

/**
 *	skb_cloned - is the buffer a clone
 *	@skb: buffer to check
 *
 *	Returns true if the buffer was generated with skb_clone() and is
 *	one of multiple shared copies of the buffer. Cloned buffers are
 *	shared data so must not be written to under normal circumstances.
 */
static inline int skb_cloned(const struct sk_buff *skb)
{
	return skb->cloned &&
	       (atomic_read(&skb_shinfo(skb)->dataref) & SKB_DATAREF_MASK) != 1;
}

1317 1318
static inline int skb_unclone(struct sk_buff *skb, gfp_t pri)
{
1319
	might_sleep_if(gfpflags_allow_blocking(pri));
1320 1321 1322 1323 1324 1325 1326

	if (skb_cloned(skb))
		return pskb_expand_head(skb, 0, 0, pri);

	return 0;
}

Linus Torvalds's avatar
Linus Torvalds committed
1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345
/**
 *	skb_header_cloned - is the header a clone
 *	@skb: buffer to check
 *
 *	Returns true if modifying the header part of the buffer requires
 *	the data to be copied.
 */
static inline int skb_header_cloned(const struct sk_buff *skb)
{
	int dataref;

	if (!skb->cloned)
		return 0;

	dataref = atomic_read(&skb_shinfo(skb)->dataref);
	dataref = (dataref & SKB_DATAREF_MASK) - (dataref >> SKB_DATAREF_SHIFT);
	return dataref != 1;
}

1346 1347 1348 1349 1350 1351 1352 1353 1354 1355
static inline int skb_header_unclone(struct sk_buff *skb, gfp_t pri)
{
	might_sleep_if(gfpflags_allow_blocking(pri));

	if (skb_header_cloned(skb))
		return pskb_expand_head(skb, 0, 0, pri);

	return 0;
}

Linus Torvalds's avatar
Linus Torvalds committed
1356 1357 1358 1359 1360 1361 1362
/**
 *	skb_header_release - release reference to header
 *	@skb: buffer to operate on
 *
 *	Drop a reference to the header part of the buffer.  This is done
 *	by acquiring a payload reference.  You must not read from the header
 *	part of skb->data after this.
1363
 *	Note : Check if you can use __skb_header_release() instead.
Linus Torvalds's avatar
Linus Torvalds committed
1364 1365 1366 1367 1368 1369 1370 1371
 */
static inline void skb_header_release(struct sk_buff *skb)
{
	BUG_ON(skb->nohdr);
	skb->nohdr = 1;
	atomic_add(1 << SKB_DATAREF_SHIFT, &skb_shinfo(skb)->dataref);
}

1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385
/**
 *	__skb_header_release - release reference to header
 *	@skb: buffer to operate on
 *
 *	Variant of skb_header_release() assuming skb is private to caller.
 *	We can avoid one atomic operation.
 */
static inline void __skb_header_release(struct sk_buff *skb)
{
	skb->nohdr = 1;
	atomic_set(&skb_shinfo(skb)->dataref, 1 + (1 << SKB_DATAREF_SHIFT));
}


Linus Torvalds's avatar
Linus Torvalds committed
1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410
/**
 *	skb_shared - is the buffer shared
 *	@skb: buffer to check
 *
 *	Returns true if more than one person has a reference to this
 *	buffer.
 */
static inline int skb_shared(const struct sk_buff *skb)
{
	return atomic_read(&skb->users) != 1;
}

/**
 *	skb_share_check - check if buffer is shared and if so clone it
 *	@skb: buffer to check
 *	@pri: priority for memory allocation
 *
 *	If the buffer is shared the buffer is cloned and the old copy
 *	drops a reference. A new clone with a single reference is returned.
 *	If the buffer is not shared the original buffer is returned. When
 *	being called from interrupt status or with spinlocks held pri must
 *	be GFP_ATOMIC.
 *
 *	NULL is returned on a memory allocation failure.
 */
1411
static inline struct sk_buff *skb_share_check(struct sk_buff *skb, gfp_t pri)
Linus Torvalds's avatar
Linus Torvalds committed
1412
{
1413
	might_sleep_if(gfpflags_allow_blocking(pri));
Linus Torvalds's avatar
Linus Torvalds committed
1414 1415
	if (skb_shared(skb)) {
		struct sk_buff *nskb = skb_clone(skb, pri);
1416 1417 1418 1419 1420

		if (likely(nskb))
			consume_skb(skb);
		else
			kfree_skb(skb);
Linus Torvalds's avatar
Linus Torvalds committed
1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445
		skb = nskb;
	}
	return skb;
}

/*
 *	Copy shared buffers into a new sk_buff. We effectively do COW on
 *	packets to handle cases where we have a local reader and forward
 *	and a couple of other messy ones. The normal one is tcpdumping
 *	a packet thats being forwarded.
 */

/**
 *	skb_unshare - make a copy of a shared buffer
 *	@skb: buffer to check
 *	@pri: priority for memory allocation
 *
 *	If the socket buffer is a clone then this function creates a new
 *	copy of the data, drops a reference count on the old copy and returns
 *	the new copy with the reference count at 1. If the buffer is not a clone
 *	the original buffer is returned. When called with a spinlock held or
 *	from interrupt state @pri must be %GFP_ATOMIC
 *
 *	%NULL is returned on a memory allocation failure.
 */
1446
static inline struct sk_buff *skb_unshare(struct sk_buff *skb,
1447
					  gfp_t pri)
Linus Torvalds's avatar
Linus Torvalds committed
1448
{
1449
	might_sleep_if(gfpflags_allow_blocking(pri));
Linus Torvalds's avatar
Linus Torvalds committed
1450 1451
	if (skb_cloned(skb)) {
		struct sk_buff *nskb = skb_copy(skb, pri);
1452 1453 1454 1455 1456 1457

		/* Free our shared copy */
		if (likely(nskb))
			consume_skb(skb);
		else
			kfree_skb(skb);
Linus Torvalds's avatar
Linus Torvalds committed
1458 1459 1460 1461 1462 1463
		skb = nskb;
	}
	return skb;
}

/**
1464
 *	skb_peek - peek at the head of an &sk_buff_head
Linus Torvalds's avatar
Linus Torvalds committed
1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475
 *	@list_: list to peek at
 *
 *	Peek an &sk_buff. Unlike most other operations you _MUST_
 *	be careful with this one. A peek leaves the buffer on the
 *	list and someone else may run off with it. You must hold
 *	the appropriate locks or have a private queue to do this.
 *
 *	Returns %NULL for an empty list or a pointer to the head element.
 *	The reference count is not incremented and the reference is therefore
 *	volatile. Use with caution.
 */
1476
static inline struct sk_buff *skb_peek(const struct sk_buff_head *list_)
Linus Torvalds's avatar
Linus Torvalds committed
1477
{
1478 1479 1480 1481 1482
	struct sk_buff *skb = list_->next;

	if (skb == (struct sk_buff *)list_)
		skb = NULL;
	return skb;
Linus Torvalds's avatar
Linus Torvalds committed
1483 1484
}

1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497
/**
 *	skb_peek_next - peek skb following the given one from a queue
 *	@skb: skb to start from
 *	@list_: list to peek at
 *
 *	Returns %NULL when the end of the list is met or a pointer to the
 *	next element. The reference count is not incremented and the
 *	reference is therefore volatile. Use with caution.
 */
static inline struct sk_buff *skb_peek_next(struct sk_buff *skb,
		const struct sk_buff_head *list_)
{
	struct sk_buff *next = skb->next;
1498

1499