skbuff.h 70.8 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
23
#include <linux/cache.h>

Arun Sharma's avatar
Arun Sharma committed
24
#include <linux/atomic.h>
Linus Torvalds's avatar
Linus Torvalds committed
25
26
27
#include <asm/types.h>
#include <linux/spinlock.h>
#include <linux/net.h>
28
#include <linux/textsearch.h>
Linus Torvalds's avatar
Linus Torvalds committed
29
#include <net/checksum.h>
30
#include <linux/rcupdate.h>
31
#include <linux/dmaengine.h>
32
#include <linux/hrtimer.h>
33
#include <linux/dma-mapping.h>
34
#include <linux/netdev_features.h>
Linus Torvalds's avatar
Linus Torvalds committed
35

36
/* Don't change this without changing skb_csum_unnecessary! */
Linus Torvalds's avatar
Linus Torvalds committed
37
#define CHECKSUM_NONE 0
38
39
40
#define CHECKSUM_UNNECESSARY 1
#define CHECKSUM_COMPLETE 2
#define CHECKSUM_PARTIAL 3
Linus Torvalds's avatar
Linus Torvalds committed
41
42
43

#define SKB_DATA_ALIGN(X)	(((X) + (SMP_CACHE_BYTES - 1)) & \
				 ~(SMP_CACHE_BYTES - 1))
44
#define SKB_WITH_OVERHEAD(X)	\
45
	((X) - SKB_DATA_ALIGN(sizeof(struct skb_shared_info)))
46
47
#define SKB_MAX_ORDER(X, ORDER) \
	SKB_WITH_OVERHEAD((PAGE_SIZE << (ORDER)) - (X))
Linus Torvalds's avatar
Linus Torvalds committed
48
49
50
#define SKB_MAX_HEAD(X)		(SKB_MAX_ORDER((X), 0))
#define SKB_MAX_ALLOC		(SKB_MAX_ORDER(0, 2))

Eric Dumazet's avatar
Eric Dumazet committed
51
52
53
54
55
/* 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
56
57
58
59
60
61
62
63
64
65
66
/* A. Checksumming of received packets by device.
 *
 *	NONE: device failed to checksum this packet.
 *		skb->csum is undefined.
 *
 *	UNNECESSARY: device parsed packet and wouldbe verified checksum.
 *		skb->csum is undefined.
 *	      It is bad option, but, unfortunately, many of vendors do this.
 *	      Apparently with secret goal to sell you new device, when you
 *	      will add new protocol to your host. F.e. IPv6. 8)
 *
67
 *	COMPLETE: the most generic way. Device supplied checksum of _all_
Linus Torvalds's avatar
Linus Torvalds committed
68
69
 *	    the packet as seen by netif_rx in skb->csum.
 *	    NOTE: Even if device supports only some protocols, but
70
 *	    is able to produce some skb->csum, it MUST use COMPLETE,
Linus Torvalds's avatar
Linus Torvalds committed
71
72
 *	    not UNNECESSARY.
 *
73
74
75
76
77
78
79
 *	PARTIAL: identical to the case for output below.  This may occur
 *	    on a packet received directly from another Linux OS, e.g.,
 *	    a virtualised Linux kernel on the same host.  The packet can
 *	    be treated in the same way as UNNECESSARY except that on
 *	    output (i.e., forwarding) the checksum must be filled in
 *	    by the OS or the hardware.
 *
Linus Torvalds's avatar
Linus Torvalds committed
80
81
82
83
 * B. Checksumming on output.
 *
 *	NONE: skb is checksummed by protocol or csum is not required.
 *
84
 *	PARTIAL: device is required to csum packet as seen by hard_start_xmit
85
86
 *	from skb->csum_start to the end and to record the checksum
 *	at skb->csum_start + skb->csum_offset.
Linus Torvalds's avatar
Linus Torvalds committed
87
88
89
90
91
92
93
94
95
 *
 *	Device must show its capabilities in dev->features, set
 *	at device setup time.
 *	NETIF_F_HW_CSUM	- it is clever device, it is able to checksum
 *			  everything.
 *	NETIF_F_IP_CSUM - device is dumb. It is able to csum only
 *			  TCP/UDP over IPv4. Sigh. Vendors like this
 *			  way by an unknown reason. Though, see comment above
 *			  about CHECKSUM_UNNECESSARY. 8)
96
 *	NETIF_F_IPV6_CSUM about as dumb as the last one but does IPv6 instead.
Linus Torvalds's avatar
Linus Torvalds committed
97
 *
98
99
100
101
102
103
104
 *	UNNECESSARY: device will do per protocol specific csum. Protocol drivers
 *	that do not want net to perform the checksum calculation should use
 *	this flag in their outgoing skbs.
 *	NETIF_F_FCOE_CRC  this indicates the device can do FCoE FC CRC
 *			  offload. Correspondingly, the FCoE protocol driver
 *			  stack should use CHECKSUM_UNNECESSARY.
 *
Linus Torvalds's avatar
Linus Torvalds committed
105
106
107
108
 *	Any questions? No questions, good. 		--ANK
 */

struct net_device;
109
struct scatterlist;
Jens Axboe's avatar
Jens Axboe committed
110
struct pipe_inode_info;
Linus Torvalds's avatar
Linus Torvalds committed
111

112
#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
Linus Torvalds's avatar
Linus Torvalds committed
113
114
115
struct nf_conntrack {
	atomic_t use;
};
116
#endif
Linus Torvalds's avatar
Linus Torvalds committed
117
118
119

#ifdef CONFIG_BRIDGE_NETFILTER
struct nf_bridge_info {
120
121
122
123
124
	atomic_t		use;
	unsigned int		mask;
	struct net_device	*physindev;
	struct net_device	*physoutdev;
	unsigned long		data[32 / sizeof(unsigned long)];
Linus Torvalds's avatar
Linus Torvalds committed
125
126
127
128
129
130
131
132
133
134
135
136
137
138
};
#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;

139
140
141
142
143
144
/* 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.
145
 */
146
#if (65536/PAGE_SIZE + 1) < 16
147
#define MAX_SKB_FRAGS 16UL
148
#else
149
#define MAX_SKB_FRAGS (65536/PAGE_SIZE + 1)
150
#endif
Linus Torvalds's avatar
Linus Torvalds committed
151
152
153
154

typedef struct skb_frag_struct skb_frag_t;

struct skb_frag_struct {
155
156
157
	struct {
		struct page *p;
	} page;
158
#if (BITS_PER_LONG > 32) || (PAGE_SIZE >= 65536)
159
160
	__u32 page_offset;
	__u32 size;
161
162
163
164
#else
	__u16 page_offset;
	__u16 size;
#endif
Linus Torvalds's avatar
Linus Torvalds committed
165
166
};

167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
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;
}

187
188
189
#define HAVE_HW_TIME_STAMP

/**
190
 * struct skb_shared_hwtstamps - hardware time stamps
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
 * @hwtstamp:	hardware time stamp transformed into duration
 *		since arbitrary point in time
 * @syststamp:	hwtstamp transformed to system time base
 *
 * Software time stamps generated by ktime_get_real() are stored in
 * skb->tstamp. The relation between the different kinds of time
 * stamps is as follows:
 *
 * syststamp and tstamp can be compared against each other in
 * arbitrary combinations.  The accuracy of a
 * syststamp/tstamp/"syststamp from other device" comparison is
 * limited by the accuracy of the transformation into system time
 * base. This depends on the device driver and its underlying
 * hardware.
 *
 * 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;
	ktime_t	syststamp;
};

217
218
219
220
221
222
223
224
225
226
227
228
229
/* Definitions for tx_flags in struct skb_shared_info */
enum {
	/* generate hardware time stamp */
	SKBTX_HW_TSTAMP = 1 << 0,

	/* generate software time stamp */
	SKBTX_SW_TSTAMP = 1 << 1,

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

	/* ensure the originating sk reference is available on driver level */
	SKBTX_DRV_NEEDS_SK_REF = 1 << 3,
230
231
232

	/* device driver supports TX zero-copy buffers */
	SKBTX_DEV_ZEROCOPY = 1 << 4,
233
234
235

	/* generate wifi status information (where possible) */
	SKBTX_WIFI_STATUS = 1 << 5,
236
237
238
239
240
};

/*
 * 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.
241
242
 * The ctx field is used to track device context.
 * The desc field is used to track userspace buffer index.
243
244
 */
struct ubuf_info {
245
246
	void (*callback)(struct ubuf_info *);
	void *ctx;
247
	unsigned long desc;
248
249
};

Linus Torvalds's avatar
Linus Torvalds committed
250
251
252
253
/* This data is invariant across clones and lives at
 * the end of the header data, ie. at skb->end.
 */
struct skb_shared_info {
254
255
	unsigned char	nr_frags;
	__u8		tx_flags;
256
257
258
259
	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
260
	struct sk_buff	*frag_list;
261
	struct skb_shared_hwtstamps hwtstamps;
262
	__be32          ip6_frag_id;
Eric Dumazet's avatar
Eric Dumazet committed
263
264
265
266
267
268

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

Johann Baudy's avatar
Johann Baudy committed
269
270
271
	/* Intermediate layers must ensure that destructor_arg
	 * remains valid until skb destructor */
	void *		destructor_arg;
272

273
274
	/* must be last field, see pskb_expand_head() */
	skb_frag_t	frags[MAX_SKB_FRAGS];
Linus Torvalds's avatar
Linus Torvalds committed
275
276
277
278
};

/* 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
279
280
 * 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
281
282
283
284
285
286
287
288
289
290
 *
 * 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)

291
292
293
294
295
296
297

enum {
	SKB_FCLONE_UNAVAILABLE,
	SKB_FCLONE_ORIG,
	SKB_FCLONE_CLONE,
};

298
299
enum {
	SKB_GSO_TCPV4 = 1 << 0,
300
	SKB_GSO_UDP = 1 << 1,
301
302
303

	/* This indicates the skb is from an untrusted source. */
	SKB_GSO_DODGY = 1 << 2,
Michael Chan's avatar
Michael Chan committed
304
305

	/* This indicates the tcp segment has CWR set. */
306
307
308
	SKB_GSO_TCP_ECN = 1 << 3,

	SKB_GSO_TCPV6 = 1 << 4,
309
310

	SKB_GSO_FCOE = 1 << 5,
311
312
};

313
314
315
316
317
318
319
320
321
322
#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

323
324
325
326
327
#if defined(CONFIG_NF_DEFRAG_IPV4) || defined(CONFIG_NF_DEFRAG_IPV4_MODULE) || \
    defined(CONFIG_NF_DEFRAG_IPV6) || defined(CONFIG_NF_DEFRAG_IPV6_MODULE)
#define NET_SKBUFF_NF_DEFRAG_NEEDED 1
#endif

Linus Torvalds's avatar
Linus Torvalds committed
328
329
330
331
/** 
 *	struct sk_buff - socket buffer
 *	@next: Next buffer in list
 *	@prev: Previous buffer in list
Herbert Xu's avatar
Herbert Xu committed
332
 *	@tstamp: Time we arrived
333
 *	@sk: Socket we are owned by
Linus Torvalds's avatar
Linus Torvalds committed
334
 *	@dev: Device we arrived on/are leaving by
335
 *	@cb: Control buffer. Free for use by every layer. Put private vars here
Eric Dumazet's avatar
Eric Dumazet committed
336
 *	@_skb_refdst: destination entry (with norefcount bit)
337
 *	@sp: the security path, used for xfrm
Linus Torvalds's avatar
Linus Torvalds committed
338
339
340
 *	@len: Length of actual data
 *	@data_len: Data length
 *	@mac_len: Length of link layer header
341
 *	@hdr_len: writable header length of cloned skb
342
343
344
 *	@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
345
 *	@priority: Packet queueing priority
346
 *	@local_df: allow local fragmentation
Linus Torvalds's avatar
Linus Torvalds committed
347
 *	@cloned: Head may be cloned (check refcnt to be sure)
348
 *	@ip_summed: Driver fed us an IP checksum
Linus Torvalds's avatar
Linus Torvalds committed
349
 *	@nohdr: Payload reference only, must not modify header
350
 *	@nfctinfo: Relationship of this skb to the connection
Linus Torvalds's avatar
Linus Torvalds committed
351
 *	@pkt_type: Packet class
352
353
 *	@fclone: skbuff clone status
 *	@ipvs_property: skbuff is owned by ipvs
354
355
 *	@peeked: this packet has been seen already, so stats have been
 *		done for it, don't do them again
356
 *	@nf_trace: netfilter packet trace flag
357
358
359
 *	@protocol: Packet protocol from driver
 *	@destructor: Destruct function
 *	@nfct: Associated connection, if any
Randy Dunlap's avatar
Randy Dunlap committed
360
 *	@nfct_reasm: netfilter conntrack re-assembly pointer
Linus Torvalds's avatar
Linus Torvalds committed
361
 *	@nf_bridge: Saved data about a bridged frame - see br_netfilter.c
362
 *	@skb_iif: ifindex of device we arrived on
Linus Torvalds's avatar
Linus Torvalds committed
363
364
 *	@tc_index: Traffic control index
 *	@tc_verd: traffic control verdict
365
366
 *	@rxhash: the packet hash computed on receive
 *	@queue_mapping: Queue mapping for multiqueue devices
367
 *	@ndisc_nodetype: router type (from link layer)
368
 *	@ooo_okay: allow the mapping of a socket to a queue to be changed
369
370
 *	@l4_rxhash: indicate rxhash is a canonical 4-tuple hash over transport
 *		ports.
371
372
 *	@wifi_acked_valid: wifi_acked was set
 *	@wifi_acked: whether frame was acked on wifi or not
373
 *	@no_fcs:  Request NIC to treat last 4 bytes as Ethernet FCS
Randy Dunlap's avatar
Randy Dunlap committed
374
375
 *	@dma_cookie: a cookie to one of several possible DMA operations
 *		done by skb DMA functions
376
 *	@secmark: security marking
377
378
 *	@mark: Generic packet mark
 *	@dropcount: total number of sk_receive_queue overflows
379
 *	@vlan_tci: vlan tag control information
380
381
382
383
384
385
386
387
388
 *	@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
389
390
391
392
393
394
395
 */

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

396
	ktime_t			tstamp;
397
398

	struct sock		*sk;
Linus Torvalds's avatar
Linus Torvalds committed
399
400
401
402
403
404
405
406
	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.
	 */
407
	char			cb[48] __aligned(8);
Linus Torvalds's avatar
Linus Torvalds committed
408

Eric Dumazet's avatar
Eric Dumazet committed
409
	unsigned long		_skb_refdst;
410
411
412
#ifdef CONFIG_XFRM
	struct	sec_path	*sp;
#endif
Linus Torvalds's avatar
Linus Torvalds committed
413
	unsigned int		len,
414
415
416
				data_len;
	__u16			mac_len,
				hdr_len;
Al Viro's avatar
Al Viro committed
417
418
	union {
		__wsum		csum;
419
420
421
422
		struct {
			__u16	csum_start;
			__u16	csum_offset;
		};
Al Viro's avatar
Al Viro committed
423
	};
Linus Torvalds's avatar
Linus Torvalds committed
424
	__u32			priority;
425
	kmemcheck_bitfield_begin(flags1);
426
427
428
	__u8			local_df:1,
				cloned:1,
				ip_summed:2,
429
430
				nohdr:1,
				nfctinfo:3;
431
	__u8			pkt_type:3,
432
				fclone:2,
433
				ipvs_property:1,
434
				peeked:1,
435
				nf_trace:1;
436
	kmemcheck_bitfield_end(flags1);
Eric Dumazet's avatar
Eric Dumazet committed
437
	__be16			protocol;
Linus Torvalds's avatar
Linus Torvalds committed
438
439

	void			(*destructor)(struct sk_buff *skb);
440
#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
441
	struct nf_conntrack	*nfct;
442
443
#endif
#ifdef NET_SKBUFF_NF_DEFRAG_NEEDED
444
445
	struct sk_buff		*nfct_reasm;
#endif
Linus Torvalds's avatar
Linus Torvalds committed
446
447
448
#ifdef CONFIG_BRIDGE_NETFILTER
	struct nf_bridge_info	*nf_bridge;
#endif
449

450
	int			skb_iif;
451
452
453
454
455

	__u32			rxhash;

	__u16			vlan_tci;

Linus Torvalds's avatar
Linus Torvalds committed
456
#ifdef CONFIG_NET_SCHED
457
	__u16			tc_index;	/* traffic control index */
Linus Torvalds's avatar
Linus Torvalds committed
458
#ifdef CONFIG_NET_CLS_ACT
459
	__u16			tc_verd;	/* traffic control verdict */
Linus Torvalds's avatar
Linus Torvalds committed
460
461
#endif
#endif
462

463
	__u16			queue_mapping;
464
	kmemcheck_bitfield_begin(flags2);
465
#ifdef CONFIG_IPV6_NDISC_NODETYPE
466
	__u8			ndisc_nodetype:2;
467
#endif
468
	__u8			ooo_okay:1;
469
	__u8			l4_rxhash:1;
470
471
	__u8			wifi_acked_valid:1;
	__u8			wifi_acked:1;
472
	__u8			no_fcs:1;
473
474
	__u8			head_frag:1;
	/* 8/10 bit hole (depending on ndisc_nodetype presence) */
475
476
	kmemcheck_bitfield_end(flags2);

477
478
479
#ifdef CONFIG_NET_DMA
	dma_cookie_t		dma_cookie;
#endif
480
481
482
#ifdef CONFIG_NETWORK_SECMARK
	__u32			secmark;
#endif
483
484
485
	union {
		__u32		mark;
		__u32		dropcount;
486
		__u32		avail_size;
487
	};
Linus Torvalds's avatar
Linus Torvalds committed
488

489
490
491
	sk_buff_data_t		transport_header;
	sk_buff_data_t		network_header;
	sk_buff_data_t		mac_header;
Linus Torvalds's avatar
Linus Torvalds committed
492
	/* These elements must be at the end, see alloc_skb() for details.  */
493
	sk_buff_data_t		tail;
494
	sk_buff_data_t		end;
Linus Torvalds's avatar
Linus Torvalds committed
495
	unsigned char		*head,
496
				*data;
497
498
	unsigned int		truesize;
	atomic_t		users;
Linus Torvalds's avatar
Linus Torvalds committed
499
500
501
502
503
504
505
506
507
};

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


Eric Dumazet's avatar
Eric Dumazet committed
508
509
510
511
512
513
514
515
516
517
518
519
520
/*
 * 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
521
522
static inline struct dst_entry *skb_dst(const struct sk_buff *skb)
{
Eric Dumazet's avatar
Eric Dumazet committed
523
524
525
526
527
528
529
	/* 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
530
531
}

Eric Dumazet's avatar
Eric Dumazet committed
532
533
534
535
536
537
538
539
/**
 * 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
540
541
static inline void skb_dst_set(struct sk_buff *skb, struct dst_entry *dst)
{
Eric Dumazet's avatar
Eric Dumazet committed
542
543
544
	skb->_skb_refdst = (unsigned long)dst;
}

Eric Dumazet's avatar
Eric Dumazet committed
545
extern void skb_dst_set_noref(struct sk_buff *skb, struct dst_entry *dst);
Eric Dumazet's avatar
Eric Dumazet committed
546
547

/**
Lucas De Marchi's avatar
Lucas De Marchi committed
548
 * skb_dst_is_noref - Test if skb dst isn't refcounted
Eric Dumazet's avatar
Eric Dumazet committed
549
550
551
552
553
 * @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
554
555
}

Eric Dumazet's avatar
Eric Dumazet committed
556
557
static inline struct rtable *skb_rtable(const struct sk_buff *skb)
{
Eric Dumazet's avatar
Eric Dumazet committed
558
	return (struct rtable *)skb_dst(skb);
Eric Dumazet's avatar
Eric Dumazet committed
559
560
}

561
extern void kfree_skb(struct sk_buff *skb);
562
extern void consume_skb(struct sk_buff *skb);
Linus Torvalds's avatar
Linus Torvalds committed
563
extern void	       __kfree_skb(struct sk_buff *skb);
564
extern struct kmem_cache *skbuff_head_cache;
565
extern struct sk_buff *__alloc_skb(unsigned int size,
566
				   gfp_t priority, int fclone, int node);
567
extern struct sk_buff *build_skb(void *data, unsigned int frag_size);
568
static inline struct sk_buff *alloc_skb(unsigned int size,
569
					gfp_t priority)
570
{
571
	return __alloc_skb(size, priority, 0, NUMA_NO_NODE);
572
573
574
}

static inline struct sk_buff *alloc_skb_fclone(unsigned int size,
575
					       gfp_t priority)
576
{
577
	return __alloc_skb(size, priority, 1, NUMA_NO_NODE);
578
579
}

580
extern void skb_recycle(struct sk_buff *skb);
581
extern bool skb_recycle_check(struct sk_buff *skb, int skb_size);
582

Herbert Xu's avatar
Herbert Xu committed
583
extern struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src);
584
extern int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask);
Victor Fusco's avatar
Victor Fusco committed
585
extern struct sk_buff *skb_clone(struct sk_buff *skb,
586
				 gfp_t priority);
Victor Fusco's avatar
Victor Fusco committed
587
extern struct sk_buff *skb_copy(const struct sk_buff *skb,
588
				gfp_t priority);
Eric Dumazet's avatar
Eric Dumazet committed
589
590
591
extern struct sk_buff *__pskb_copy(struct sk_buff *skb,
				 int headroom, gfp_t gfp_mask);

Linus Torvalds's avatar
Linus Torvalds committed
592
extern int	       pskb_expand_head(struct sk_buff *skb,
Victor Fusco's avatar
Victor Fusco committed
593
					int nhead, int ntail,
594
					gfp_t gfp_mask);
Linus Torvalds's avatar
Linus Torvalds committed
595
596
597
598
extern struct sk_buff *skb_realloc_headroom(struct sk_buff *skb,
					    unsigned int headroom);
extern struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
				       int newheadroom, int newtailroom,
599
				       gfp_t priority);
600
601
602
603
604
extern int	       skb_to_sgvec(struct sk_buff *skb,
				    struct scatterlist *sg, int offset,
				    int len);
extern int	       skb_cow_data(struct sk_buff *skb, int tailbits,
				    struct sk_buff **trailer);
605
extern int	       skb_pad(struct sk_buff *skb, int pad);
606
#define dev_kfree_skb(a)	consume_skb(a)
Linus Torvalds's avatar
Linus Torvalds committed
607

608
609
610
611
612
extern 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);

Eric Dumazet's avatar
Eric Dumazet committed
613
struct skb_seq_state {
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
	__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;
};

extern void	      skb_prepare_seq_read(struct sk_buff *skb,
					   unsigned int from, unsigned int to,
					   struct skb_seq_state *st);
extern unsigned int   skb_seq_read(unsigned int consumed, const u8 **data,
				   struct skb_seq_state *st);
extern void	      skb_abort_seq_read(struct skb_seq_state *st);

630
631
632
633
extern unsigned int   skb_find_text(struct sk_buff *skb, unsigned int from,
				    unsigned int to, struct ts_config *config,
				    struct ts_state *state);

634
extern void __skb_get_rxhash(struct sk_buff *skb);
635
636
637
static inline __u32 skb_get_rxhash(struct sk_buff *skb)
{
	if (!skb->rxhash)
638
		__skb_get_rxhash(skb);
639
640
641
642

	return skb->rxhash;
}

643
644
645
646
647
648
649
650
651
652
653
654
#ifdef NET_SKBUFF_DATA_USES_OFFSET
static inline unsigned char *skb_end_pointer(const struct sk_buff *skb)
{
	return skb->head + skb->end;
}
#else
static inline unsigned char *skb_end_pointer(const struct sk_buff *skb)
{
	return skb->end;
}
#endif

Linus Torvalds's avatar
Linus Torvalds committed
655
/* Internal */
656
#define skb_shinfo(SKB)	((struct skb_shared_info *)(skb_end_pointer(SKB)))
Linus Torvalds's avatar
Linus Torvalds committed
657

658
659
660
661
662
static inline struct skb_shared_hwtstamps *skb_hwtstamps(struct sk_buff *skb)
{
	return &skb_shinfo(skb)->hwtstamps;
}

Linus Torvalds's avatar
Linus Torvalds committed
663
664
665
666
667
668
669
670
671
672
673
/**
 *	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)
{
	return list->next == (struct sk_buff *)list;
}

674
675
676
677
678
679
680
681
682
683
/**
 *	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)
{
Eric Dumazet's avatar
Eric Dumazet committed
684
	return skb->next == (struct sk_buff *)list;
685
686
}

687
688
689
690
691
692
693
694
695
696
/**
 *	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)
{
Eric Dumazet's avatar
Eric Dumazet committed
697
	return skb->prev == (struct sk_buff *)list;
698
699
}

David S. Miller's avatar
David S. Miller committed
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
/**
 *	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;
}

718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
/**
 *	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
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
/**
 *	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;
}

/**
 *	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;
}

/**
 *	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.
 */
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);
}

/**
 *	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.
 */
Victor Fusco's avatar
Victor Fusco committed
827
static inline struct sk_buff *skb_share_check(struct sk_buff *skb,
828
					      gfp_t pri)
Linus Torvalds's avatar
Linus Torvalds committed
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
{
	might_sleep_if(pri & __GFP_WAIT);
	if (skb_shared(skb)) {
		struct sk_buff *nskb = skb_clone(skb, pri);
		kfree_skb(skb);
		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.
 */
859
static inline struct sk_buff *skb_unshare(struct sk_buff *skb,
860
					  gfp_t pri)
Linus Torvalds's avatar
Linus Torvalds committed
861
862
863
864
865
866
867
868
869
870
871
{
	might_sleep_if(pri & __GFP_WAIT);
	if (skb_cloned(skb)) {
		struct sk_buff *nskb = skb_copy(skb, pri);
		kfree_skb(skb);	/* Free our shared copy */
		skb = nskb;
	}
	return skb;
}

/**
872
 *	skb_peek - peek at the head of an &sk_buff_head
Linus Torvalds's avatar
Linus Torvalds committed
873
874
875
876
877
878
879
880
881
882
883
 *	@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.
 */
884
static inline struct sk_buff *skb_peek(const struct sk_buff_head *list_)
Linus Torvalds's avatar
Linus Torvalds committed
885
{
886
	struct sk_buff *list = ((const struct sk_buff *)list_)->next;
Linus Torvalds's avatar
Linus Torvalds committed
887
888
889
890
891
	if (list == (struct sk_buff *)list_)
		list = NULL;
	return list;
}

892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
/**
 *	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;
	if (next == (struct sk_buff *)list_)
		next = NULL;
	return next;
}

Linus Torvalds's avatar
Linus Torvalds committed
910
/**
911
 *	skb_peek_tail - peek at the tail of an &sk_buff_head
Linus Torvalds's avatar
Linus Torvalds committed
912
913
914
915
916
917
918
919
920
921
922
 *	@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 tail element.
 *	The reference count is not incremented and the reference is therefore
 *	volatile. Use with caution.
 */
923
static inline struct sk_buff *skb_peek_tail(const struct sk_buff_head *list_)
Linus Torvalds's avatar
Linus Torvalds committed
924
{
925
	struct sk_buff *list = ((const struct sk_buff *)list_)->prev;
Linus Torvalds's avatar
Linus Torvalds committed
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
	if (list == (struct sk_buff *)list_)
		list = NULL;
	return list;
}

/**
 *	skb_queue_len	- get queue length
 *	@list_: list to measure
 *
 *	Return the length of an &sk_buff queue.
 */
static inline __u32 skb_queue_len(const struct sk_buff_head *list_)
{
	return list_->qlen;
}

942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
/**
 *	__skb_queue_head_init - initialize non-spinlock portions of sk_buff_head
 *	@list: queue to initialize
 *
 *	This initializes only the list and queue length aspects of
 *	an sk_buff_head object.  This allows to initialize the list
 *	aspects of an sk_buff_head without reinitializing things like
 *	the spinlock.  It can also be used for on-stack sk_buff_head
 *	objects where the spinlock is known to not be used.
 */
static inline void __skb_queue_head_init(struct sk_buff_head *list)
{
	list->prev = list->next = (struct sk_buff *)list;
	list->qlen = 0;
}

958
959
960
961
962
963
964
965
/*
 * This function creates a split out lock class for each invocation;
 * this is needed for now since a whole lot of users of the skb-queue
 * infrastructure in drivers have different locking usage (in hardirq)
 * than the networking core (in softirq only). In the long run either the
 * network layer or drivers should need annotation to consolidate the
 * main types of usage into 3 classes.
 */
Linus Torvalds's avatar
Linus Torvalds committed
966
967
968
static inline void skb_queue_head_init(struct sk_buff_head *list)
{
	spin_lock_init(&list->lock);
969
	__skb_queue_head_init(list);
Linus Torvalds's avatar
Linus Torvalds committed
970
971
}

972
973
974
975
976
977
978
static inline void skb_queue_head_init_class(struct sk_buff_head *list,
		struct lock_class_key *class)
{
	skb_queue_head_init(list);
	lockdep_set_class(&list->lock, class);
}

Linus Torvalds's avatar
Linus Torvalds committed
979
/*
980
 *	Insert an sk_buff on a list.
Linus Torvalds's avatar
Linus Torvalds committed
981
982
983
984
 *
 *	The "__skb_xxxx()" functions are the non-atomic ones that
 *	can only be called with interrupts disabled.
 */
985
986
987
988
989
990
991
992
993
994
extern void        skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list);
static inline void __skb_insert(struct sk_buff *newsk,
				struct sk_buff *prev, struct sk_buff *next,
				struct sk_buff_head *list)
{
	newsk->next = next;
	newsk->prev = prev;
	next->prev  = prev->next = newsk;
	list->qlen++;
}
Linus Torvalds's avatar
Linus Torvalds committed
995

996
997
998
999
1000
static inline void __skb_queue_splice(const struct sk_buff_head *list,
				      struct sk_buff *prev,
				      struct sk_buff *next)
{
	struct sk_buff *first = list->next;
For faster browsing, not all history is shown. View entire blame