1 /* Modified by Broadcom Corp. Portions Copyright (c) Broadcom Corp, 2012. */
3 * Definitions for the 'struct sk_buff' memory handlers.
6 * Alan Cox, <gw4pts@gw4pts.ampr.org>
7 * Florian La Roche, <rzsfl@rz.uni-sb.de>
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version
12 * 2 of the License, or (at your option) any later version.
15 #ifndef _LINUX_SKBUFF_H
16 #define _LINUX_SKBUFF_H
18 #include <linux/kernel.h>
19 #include <linux/kmemcheck.h>
20 #include <linux/compiler.h>
21 #include <linux/time.h>
22 #include <linux/cache.h>
24 #include <asm/atomic.h>
25 #include <asm/types.h>
26 #include <linux/spinlock.h>
27 #include <linux/net.h>
28 #include <linux/textsearch.h>
29 #include <net/checksum.h>
30 #include <linux/rcupdate.h>
31 #include <linux/dmaengine.h>
32 #include <linux/hrtimer.h>
34 /* Don't change this without changing skb_csum_unnecessary! */
35 #define CHECKSUM_NONE 0
36 #define CHECKSUM_UNNECESSARY 1
37 #define CHECKSUM_COMPLETE 2
38 #define CHECKSUM_PARTIAL 3
40 #define SKB_DATA_ALIGN(X) (((X) + (SMP_CACHE_BYTES - 1)) & \
41 ~(SMP_CACHE_BYTES - 1))
42 #define SKB_WITH_OVERHEAD(X) \
43 ((X) - SKB_DATA_ALIGN(sizeof(struct skb_shared_info)))
44 #define SKB_MAX_ORDER(X, ORDER) \
45 SKB_WITH_OVERHEAD((PAGE_SIZE << (ORDER)) - (X))
46 #define SKB_MAX_HEAD(X) (SKB_MAX_ORDER((X), 0))
47 #define SKB_MAX_ALLOC (SKB_MAX_ORDER(0, 2))
49 /* A. Checksumming of received packets by device.
51 * NONE: device failed to checksum this packet.
52 * skb->csum is undefined.
54 * UNNECESSARY: device parsed packet and wouldbe verified checksum.
55 * skb->csum is undefined.
56 * It is bad option, but, unfortunately, many of vendors do this.
57 * Apparently with secret goal to sell you new device, when you
58 * will add new protocol to your host. F.e. IPv6. 8)
60 * COMPLETE: the most generic way. Device supplied checksum of _all_
61 * the packet as seen by netif_rx in skb->csum.
62 * NOTE: Even if device supports only some protocols, but
63 * is able to produce some skb->csum, it MUST use COMPLETE,
66 * PARTIAL: identical to the case for output below. This may occur
67 * on a packet received directly from another Linux OS, e.g.,
68 * a virtualised Linux kernel on the same host. The packet can
69 * be treated in the same way as UNNECESSARY except that on
70 * output (i.e., forwarding) the checksum must be filled in
71 * by the OS or the hardware.
73 * B. Checksumming on output.
75 * NONE: skb is checksummed by protocol or csum is not required.
77 * PARTIAL: device is required to csum packet as seen by hard_start_xmit
78 * from skb->csum_start to the end and to record the checksum
79 * at skb->csum_start + skb->csum_offset.
81 * Device must show its capabilities in dev->features, set
82 * at device setup time.
83 * NETIF_F_HW_CSUM - it is clever device, it is able to checksum
85 * NETIF_F_NO_CSUM - loopback or reliable single hop media.
86 * NETIF_F_IP_CSUM - device is dumb. It is able to csum only
87 * TCP/UDP over IPv4. Sigh. Vendors like this
88 * way by an unknown reason. Though, see comment above
89 * about CHECKSUM_UNNECESSARY. 8)
90 * NETIF_F_IPV6_CSUM about as dumb as the last one but does IPv6 instead.
92 * Any questions? No questions, good. --ANK
97 struct pipe_inode_info
;
99 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
100 struct nf_conntrack
{
105 #ifdef CONFIG_BRIDGE_NETFILTER
106 struct nf_bridge_info
{
108 struct net_device
*physindev
;
109 struct net_device
*physoutdev
;
111 unsigned long data
[32 / sizeof(unsigned long)];
115 struct sk_buff_head
{
116 /* These two members must be first. */
117 struct sk_buff
*next
;
118 struct sk_buff
*prev
;
126 /* To allow 64K frame to be packed as single skb without frag_list */
127 #define MAX_SKB_FRAGS (65536/PAGE_SIZE + 2)
129 typedef struct skb_frag_struct skb_frag_t
;
131 struct skb_frag_struct
{
137 #define HAVE_HW_TIME_STAMP
140 * struct skb_shared_hwtstamps - hardware time stamps
141 * @hwtstamp: hardware time stamp transformed into duration
142 * since arbitrary point in time
143 * @syststamp: hwtstamp transformed to system time base
145 * Software time stamps generated by ktime_get_real() are stored in
146 * skb->tstamp. The relation between the different kinds of time
147 * stamps is as follows:
149 * syststamp and tstamp can be compared against each other in
150 * arbitrary combinations. The accuracy of a
151 * syststamp/tstamp/"syststamp from other device" comparison is
152 * limited by the accuracy of the transformation into system time
153 * base. This depends on the device driver and its underlying
156 * hwtstamps can only be compared against other hwtstamps from
159 * This structure is attached to packets as part of the
160 * &skb_shared_info. Use skb_hwtstamps() to get a pointer.
162 struct skb_shared_hwtstamps
{
168 * struct skb_shared_tx - instructions for time stamping of outgoing packets
169 * @hardware: generate hardware time stamp
170 * @software: generate software time stamp
171 * @in_progress: device driver is going to provide
172 * hardware time stamp
173 * @prevent_sk_orphan: make sk reference available on driver level
174 * @flags: all shared_tx flags
176 * These flags are attached to packets as part of the
177 * &skb_shared_info. Use skb_tx() to get a pointer.
179 union skb_shared_tx
{
189 /* This data is invariant across clones and lives at
190 * the end of the header data, ie. at skb->end.
192 struct skb_shared_info
{
193 unsigned short nr_frags
;
194 unsigned short gso_size
;
195 /* Warning: this field is not always filled in (UFO)! */
196 unsigned short gso_segs
;
197 unsigned short gso_type
;
199 union skb_shared_tx tx_flags
;
200 struct sk_buff
*frag_list
;
201 struct skb_shared_hwtstamps hwtstamps
;
204 * Warning : all fields before dataref are cleared in __alloc_skb()
208 /* Intermediate layers must ensure that destructor_arg
209 * remains valid until skb destructor */
210 void * destructor_arg
;
211 /* must be last field, see pskb_expand_head() */
212 skb_frag_t frags
[MAX_SKB_FRAGS
];
215 /* We divide dataref into two halves. The higher 16 bits hold references
216 * to the payload part of skb->data. The lower 16 bits hold references to
217 * the entire skb->data. A clone of a headerless skb holds the length of
218 * the header in skb->hdr_len.
220 * All users must obey the rule that the skb->data reference count must be
221 * greater than or equal to the payload reference count.
223 * Holding a reference to the payload part means that the user does not
224 * care about modifications to the header part of skb->data.
226 #define SKB_DATAREF_SHIFT 16
227 #define SKB_DATAREF_MASK ((1 << SKB_DATAREF_SHIFT) - 1)
231 SKB_FCLONE_UNAVAILABLE
,
237 SKB_GSO_TCPV4
= 1 << 0,
238 SKB_GSO_UDP
= 1 << 1,
240 /* This indicates the skb is from an untrusted source. */
241 SKB_GSO_DODGY
= 1 << 2,
243 /* This indicates the tcp segment has CWR set. */
244 SKB_GSO_TCP_ECN
= 1 << 3,
246 SKB_GSO_TCPV6
= 1 << 4,
248 SKB_GSO_FCOE
= 1 << 5,
251 #if BITS_PER_LONG > 32
252 #define NET_SKBUFF_DATA_USES_OFFSET 1
255 #ifdef NET_SKBUFF_DATA_USES_OFFSET
256 typedef unsigned int sk_buff_data_t
;
258 typedef unsigned char *sk_buff_data_t
;
262 * struct sk_buff - socket buffer
263 * @next: Next buffer in list
264 * @prev: Previous buffer in list
265 * @sk: Socket we are owned by
266 * @tstamp: Time we arrived
267 * @dev: Device we arrived on/are leaving by
268 * @transport_header: Transport layer header
269 * @network_header: Network layer header
270 * @mac_header: Link layer header
271 * @_skb_refdst: destination entry (with norefcount bit)
272 * @sp: the security path, used for xfrm
273 * @cb: Control buffer. Free for use by every layer. Put private vars here
274 * @len: Length of actual data
275 * @data_len: Data length
276 * @mac_len: Length of link layer header
277 * @hdr_len: writable header length of cloned skb
278 * @csum: Checksum (must include start/offset pair)
279 * @csum_start: Offset from skb->head where checksumming should start
280 * @csum_offset: Offset from csum_start where checksum should be stored
281 * @local_df: allow local fragmentation
282 * @cloned: Head may be cloned (check refcnt to be sure)
283 * @nohdr: Payload reference only, must not modify header
284 * @pkt_type: Packet class
285 * @fclone: skbuff clone status
286 * @ip_summed: Driver fed us an IP checksum
287 * @priority: Packet queueing priority
288 * @users: User count - see {datagram,tcp}.c
289 * @protocol: Packet protocol from driver
290 * @truesize: Buffer size
291 * @head: Head of buffer
292 * @data: Data head pointer
293 * @tail: Tail pointer
295 * @destructor: Destruct function
296 * @mark: Generic packet mark
297 * @nfct: Associated connection, if any
298 * @ipvs_property: skbuff is owned by ipvs
299 * @peeked: this packet has been seen already, so stats have been
300 * done for it, don't do them again
301 * @nf_trace: netfilter packet trace flag
302 * @nfctinfo: Relationship of this skb to the connection
303 * @nfct_reasm: netfilter conntrack re-assembly pointer
304 * @nf_bridge: Saved data about a bridged frame - see br_netfilter.c
305 * @skb_iif: ifindex of device we arrived on
306 * @rxhash: the packet hash computed on receive
307 * @queue_mapping: Queue mapping for multiqueue devices
308 * @tc_index: Traffic control index
309 * @tc_verd: traffic control verdict
310 * @ndisc_nodetype: router type (from link layer)
311 * @dma_cookie: a cookie to one of several possible DMA operations
312 * done by skb DMA functions
313 * @secmark: security marking
314 * @vlan_tci: vlan tag control information
318 /* These two members must be first. */
319 struct sk_buff
*next
;
320 struct sk_buff
*prev
;
322 unsigned char pktc_cb
[8];
327 struct net_device
*dev
;
330 * This is the control buffer. It is free to use for every
331 * layer. Please put your private variables there. If you
332 * want to keep them across layers you have to do a skb_clone()
333 * first. This is owned by whoever has the skb queued ATM.
335 char cb
[48] __aligned(8);
337 unsigned long _skb_refdst
;
338 #if defined(CONFIG_XFRM) || defined(CTFMAP)
345 struct list_head ctrace_list
;
346 #define CTRACE_NUM 16
347 char *func
[CTRACE_NUM
];
348 int line
[CTRACE_NUM
];
351 #endif /* BCMDBG_CTRACE */
352 #if defined(HNDCTF) || defined(CTFPOOL)
371 kmemcheck_bitfield_begin(flags1
);
382 kmemcheck_bitfield_end(flags1
);
385 void (*destructor
)(struct sk_buff
*skb
);
399 sk_buff_data_t transport_header
;
400 sk_buff_data_t network_header
;
401 sk_buff_data_t mac_header
;
402 /* These elements must be at the end, see alloc_skb() for details. */
407 unsigned int truesize
;
409 kmemcheck_bitfield_begin(flags2
);
410 __u16 queue_mapping
:16;
411 #ifdef CONFIG_IPV6_NDISC_NODETYPE
412 __u8 ndisc_nodetype
:2,
415 __u8 deliver_no_wcard
:1;
417 kmemcheck_bitfield_end(flags2
);
418 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
419 struct nf_conntrack
*nfct
;
420 struct sk_buff
*nfct_reasm
;
424 #ifdef CONFIG_BRIDGE_NETFILTER
425 struct nf_bridge_info
*nf_bridge
;
427 #ifdef CONFIG_NET_SCHED
428 __u16 tc_index
; /* traffic control index */
429 #ifdef CONFIG_NET_CLS_ACT
430 __u16 tc_verd
; /* traffic control verdict */
433 #ifdef CONFIG_NET_DMA
434 dma_cookie_t dma_cookie
;
436 #ifdef CONFIG_NETWORK_SECMARK
443 * Handling routines are only of interest to the kernel
445 #include <linux/slab.h>
447 #include <asm/system.h>
450 * skb might have a dst pointer attached, refcounted or not.
451 * _skb_refdst low order bit is set if refcount was _not_ taken
453 #define SKB_DST_NOREF 1UL
454 #define SKB_DST_PTRMASK ~(SKB_DST_NOREF)
457 * skb_dst - returns skb dst_entry
460 * Returns skb dst_entry, regardless of reference taken or not.
462 static inline struct dst_entry
*skb_dst(const struct sk_buff
*skb
)
464 /* If refdst was not refcounted, check we still are in a
465 * rcu_read_lock section
467 WARN_ON((skb
->_skb_refdst
& SKB_DST_NOREF
) &&
468 !rcu_read_lock_held() &&
469 !rcu_read_lock_bh_held());
470 return (struct dst_entry
*)(skb
->_skb_refdst
& SKB_DST_PTRMASK
);
474 * skb_dst_set - sets skb dst
478 * Sets skb dst, assuming a reference was taken on dst and should
479 * be released by skb_dst_drop()
481 static inline void skb_dst_set(struct sk_buff
*skb
, struct dst_entry
*dst
)
483 skb
->_skb_refdst
= (unsigned long)dst
;
487 * skb_dst_set_noref - sets skb dst, without a reference
491 * Sets skb dst, assuming a reference was not taken on dst
492 * skb_dst_drop() should not dst_release() this dst
494 static inline void skb_dst_set_noref(struct sk_buff
*skb
, struct dst_entry
*dst
)
496 WARN_ON(!rcu_read_lock_held() && !rcu_read_lock_bh_held());
497 skb
->_skb_refdst
= (unsigned long)dst
| SKB_DST_NOREF
;
501 * skb_dst_is_noref - Test if skb dst isnt refcounted
504 static inline bool skb_dst_is_noref(const struct sk_buff
*skb
)
506 return (skb
->_skb_refdst
& SKB_DST_NOREF
) && skb_dst(skb
);
509 static inline struct rtable
*skb_rtable(const struct sk_buff
*skb
)
511 return (struct rtable
*)skb_dst(skb
);
514 extern void kfree_skb(struct sk_buff
*skb
);
515 extern void consume_skb(struct sk_buff
*skb
);
516 extern void __kfree_skb(struct sk_buff
*skb
);
517 extern struct sk_buff
*__alloc_skb(unsigned int size
,
518 gfp_t priority
, int fclone
, int node
);
519 static inline struct sk_buff
*alloc_skb(unsigned int size
,
522 return __alloc_skb(size
, priority
, 0, -1);
525 static inline struct sk_buff
*alloc_skb_fclone(unsigned int size
,
528 return __alloc_skb(size
, priority
, 1, -1);
531 extern bool skb_recycle_check(struct sk_buff
*skb
, int skb_size
);
533 extern struct sk_buff
*skb_morph(struct sk_buff
*dst
, struct sk_buff
*src
);
534 extern struct sk_buff
*skb_clone(struct sk_buff
*skb
,
536 extern struct sk_buff
*skb_copy(const struct sk_buff
*skb
,
538 extern struct sk_buff
*pskb_copy(struct sk_buff
*skb
,
540 extern int pskb_expand_head(struct sk_buff
*skb
,
541 int nhead
, int ntail
,
543 extern struct sk_buff
*skb_realloc_headroom(struct sk_buff
*skb
,
544 unsigned int headroom
);
545 extern struct sk_buff
*skb_copy_expand(const struct sk_buff
*skb
,
546 int newheadroom
, int newtailroom
,
548 extern int skb_to_sgvec(struct sk_buff
*skb
,
549 struct scatterlist
*sg
, int offset
,
551 extern int skb_cow_data(struct sk_buff
*skb
, int tailbits
,
552 struct sk_buff
**trailer
);
553 extern int skb_pad(struct sk_buff
*skb
, int pad
);
554 #define dev_kfree_skb(a) consume_skb(a)
556 extern int skb_append_datato_frags(struct sock
*sk
, struct sk_buff
*skb
,
557 int getfrag(void *from
, char *to
, int offset
,
558 int len
,int odd
, struct sk_buff
*skb
),
559 void *from
, int length
);
561 struct skb_seq_state
{
565 __u32 stepped_offset
;
566 struct sk_buff
*root_skb
;
567 struct sk_buff
*cur_skb
;
571 extern void skb_prepare_seq_read(struct sk_buff
*skb
,
572 unsigned int from
, unsigned int to
,
573 struct skb_seq_state
*st
);
574 extern unsigned int skb_seq_read(unsigned int consumed
, const u8
**data
,
575 struct skb_seq_state
*st
);
576 extern void skb_abort_seq_read(struct skb_seq_state
*st
);
578 extern unsigned int skb_find_text(struct sk_buff
*skb
, unsigned int from
,
579 unsigned int to
, struct ts_config
*config
,
580 struct ts_state
*state
);
582 #ifdef NET_SKBUFF_DATA_USES_OFFSET
583 static inline unsigned char *skb_end_pointer(const struct sk_buff
*skb
)
585 return skb
->head
+ skb
->end
;
588 static inline unsigned char *skb_end_pointer(const struct sk_buff
*skb
)
595 #define skb_shinfo(SKB) ((struct skb_shared_info *)(skb_end_pointer(SKB)))
597 static inline struct skb_shared_hwtstamps
*skb_hwtstamps(struct sk_buff
*skb
)
599 return &skb_shinfo(skb
)->hwtstamps
;
602 static inline union skb_shared_tx
*skb_tx(struct sk_buff
*skb
)
604 return &skb_shinfo(skb
)->tx_flags
;
608 * skb_queue_empty - check if a queue is empty
611 * Returns true if the queue is empty, false otherwise.
613 static inline int skb_queue_empty(const struct sk_buff_head
*list
)
615 return list
->next
== (struct sk_buff
*)list
;
619 * skb_queue_is_last - check if skb is the last entry in the queue
623 * Returns true if @skb is the last buffer on the list.
625 static inline bool skb_queue_is_last(const struct sk_buff_head
*list
,
626 const struct sk_buff
*skb
)
628 return (skb
->next
== (struct sk_buff
*) list
);
632 * skb_queue_is_first - check if skb is the first entry in the queue
636 * Returns true if @skb is the first buffer on the list.
638 static inline bool skb_queue_is_first(const struct sk_buff_head
*list
,
639 const struct sk_buff
*skb
)
641 return (skb
->prev
== (struct sk_buff
*) list
);
645 * skb_queue_next - return the next packet in the queue
647 * @skb: current buffer
649 * Return the next packet in @list after @skb. It is only valid to
650 * call this if skb_queue_is_last() evaluates to false.
652 static inline struct sk_buff
*skb_queue_next(const struct sk_buff_head
*list
,
653 const struct sk_buff
*skb
)
655 /* This BUG_ON may seem severe, but if we just return then we
656 * are going to dereference garbage.
658 BUG_ON(skb_queue_is_last(list
, skb
));
663 * skb_queue_prev - return the prev packet in the queue
665 * @skb: current buffer
667 * Return the prev packet in @list before @skb. It is only valid to
668 * call this if skb_queue_is_first() evaluates to false.
670 static inline struct sk_buff
*skb_queue_prev(const struct sk_buff_head
*list
,
671 const struct sk_buff
*skb
)
673 /* This BUG_ON may seem severe, but if we just return then we
674 * are going to dereference garbage.
676 BUG_ON(skb_queue_is_first(list
, skb
));
681 * skb_get - reference buffer
682 * @skb: buffer to reference
684 * Makes another reference to a socket buffer and returns a pointer
687 static inline struct sk_buff
*skb_get(struct sk_buff
*skb
)
689 atomic_inc(&skb
->users
);
694 * If users == 1, we are the only owner and are can avoid redundant
699 * skb_cloned - is the buffer a clone
700 * @skb: buffer to check
702 * Returns true if the buffer was generated with skb_clone() and is
703 * one of multiple shared copies of the buffer. Cloned buffers are
704 * shared data so must not be written to under normal circumstances.
706 static inline int skb_cloned(const struct sk_buff
*skb
)
708 return skb
->cloned
&&
709 (atomic_read(&skb_shinfo(skb
)->dataref
) & SKB_DATAREF_MASK
) != 1;
713 * skb_header_cloned - is the header a clone
714 * @skb: buffer to check
716 * Returns true if modifying the header part of the buffer requires
717 * the data to be copied.
719 static inline int skb_header_cloned(const struct sk_buff
*skb
)
726 dataref
= atomic_read(&skb_shinfo(skb
)->dataref
);
727 dataref
= (dataref
& SKB_DATAREF_MASK
) - (dataref
>> SKB_DATAREF_SHIFT
);
732 * skb_header_release - release reference to header
733 * @skb: buffer to operate on
735 * Drop a reference to the header part of the buffer. This is done
736 * by acquiring a payload reference. You must not read from the header
737 * part of skb->data after this.
739 static inline void skb_header_release(struct sk_buff
*skb
)
743 atomic_add(1 << SKB_DATAREF_SHIFT
, &skb_shinfo(skb
)->dataref
);
747 * skb_shared - is the buffer shared
748 * @skb: buffer to check
750 * Returns true if more than one person has a reference to this
753 static inline int skb_shared(const struct sk_buff
*skb
)
755 return atomic_read(&skb
->users
) != 1;
759 * skb_share_check - check if buffer is shared and if so clone it
760 * @skb: buffer to check
761 * @pri: priority for memory allocation
763 * If the buffer is shared the buffer is cloned and the old copy
764 * drops a reference. A new clone with a single reference is returned.
765 * If the buffer is not shared the original buffer is returned. When
766 * being called from interrupt status or with spinlocks held pri must
769 * NULL is returned on a memory allocation failure.
771 static inline struct sk_buff
*skb_share_check(struct sk_buff
*skb
,
774 might_sleep_if(pri
& __GFP_WAIT
);
775 if (skb_shared(skb
)) {
776 struct sk_buff
*nskb
= skb_clone(skb
, pri
);
784 * Copy shared buffers into a new sk_buff. We effectively do COW on
785 * packets to handle cases where we have a local reader and forward
786 * and a couple of other messy ones. The normal one is tcpdumping
787 * a packet thats being forwarded.
791 * skb_unshare - make a copy of a shared buffer
792 * @skb: buffer to check
793 * @pri: priority for memory allocation
795 * If the socket buffer is a clone then this function creates a new
796 * copy of the data, drops a reference count on the old copy and returns
797 * the new copy with the reference count at 1. If the buffer is not a clone
798 * the original buffer is returned. When called with a spinlock held or
799 * from interrupt state @pri must be %GFP_ATOMIC
801 * %NULL is returned on a memory allocation failure.
803 static inline struct sk_buff
*skb_unshare(struct sk_buff
*skb
,
806 might_sleep_if(pri
& __GFP_WAIT
);
807 if (skb_cloned(skb
)) {
808 struct sk_buff
*nskb
= skb_copy(skb
, pri
);
809 kfree_skb(skb
); /* Free our shared copy */
816 * skb_peek - peek at the head of an &sk_buff_head
817 * @list_: list to peek at
819 * Peek an &sk_buff. Unlike most other operations you _MUST_
820 * be careful with this one. A peek leaves the buffer on the
821 * list and someone else may run off with it. You must hold
822 * the appropriate locks or have a private queue to do this.
824 * Returns %NULL for an empty list or a pointer to the head element.
825 * The reference count is not incremented and the reference is therefore
826 * volatile. Use with caution.
828 static inline struct sk_buff
*skb_peek(struct sk_buff_head
*list_
)
830 struct sk_buff
*list
= ((struct sk_buff
*)list_
)->next
;
831 if (list
== (struct sk_buff
*)list_
)
837 * skb_peek_tail - peek at the tail of an &sk_buff_head
838 * @list_: list to peek at
840 * Peek an &sk_buff. Unlike most other operations you _MUST_
841 * be careful with this one. A peek leaves the buffer on the
842 * list and someone else may run off with it. You must hold
843 * the appropriate locks or have a private queue to do this.
845 * Returns %NULL for an empty list or a pointer to the tail element.
846 * The reference count is not incremented and the reference is therefore
847 * volatile. Use with caution.
849 static inline struct sk_buff
*skb_peek_tail(struct sk_buff_head
*list_
)
851 struct sk_buff
*list
= ((struct sk_buff
*)list_
)->prev
;
852 if (list
== (struct sk_buff
*)list_
)
858 * skb_queue_len - get queue length
859 * @list_: list to measure
861 * Return the length of an &sk_buff queue.
863 static inline __u32
skb_queue_len(const struct sk_buff_head
*list_
)
869 * __skb_queue_head_init - initialize non-spinlock portions of sk_buff_head
870 * @list: queue to initialize
872 * This initializes only the list and queue length aspects of
873 * an sk_buff_head object. This allows to initialize the list
874 * aspects of an sk_buff_head without reinitializing things like
875 * the spinlock. It can also be used for on-stack sk_buff_head
876 * objects where the spinlock is known to not be used.
878 static inline void __skb_queue_head_init(struct sk_buff_head
*list
)
880 list
->prev
= list
->next
= (struct sk_buff
*)list
;
885 * This function creates a split out lock class for each invocation;
886 * this is needed for now since a whole lot of users of the skb-queue
887 * infrastructure in drivers have different locking usage (in hardirq)
888 * than the networking core (in softirq only). In the long run either the
889 * network layer or drivers should need annotation to consolidate the
890 * main types of usage into 3 classes.
892 static inline void skb_queue_head_init(struct sk_buff_head
*list
)
894 spin_lock_init(&list
->lock
);
895 __skb_queue_head_init(list
);
898 static inline void skb_queue_head_init_class(struct sk_buff_head
*list
,
899 struct lock_class_key
*class)
901 skb_queue_head_init(list
);
902 lockdep_set_class(&list
->lock
, class);
906 * Insert an sk_buff on a list.
908 * The "__skb_xxxx()" functions are the non-atomic ones that
909 * can only be called with interrupts disabled.
911 extern void skb_insert(struct sk_buff
*old
, struct sk_buff
*newsk
, struct sk_buff_head
*list
);
912 static inline void __skb_insert(struct sk_buff
*newsk
,
913 struct sk_buff
*prev
, struct sk_buff
*next
,
914 struct sk_buff_head
*list
)
918 next
->prev
= prev
->next
= newsk
;
922 static inline void __skb_queue_splice(const struct sk_buff_head
*list
,
923 struct sk_buff
*prev
,
924 struct sk_buff
*next
)
926 struct sk_buff
*first
= list
->next
;
927 struct sk_buff
*last
= list
->prev
;
937 * skb_queue_splice - join two skb lists, this is designed for stacks
938 * @list: the new list to add
939 * @head: the place to add it in the first list
941 static inline void skb_queue_splice(const struct sk_buff_head
*list
,
942 struct sk_buff_head
*head
)
944 if (!skb_queue_empty(list
)) {
945 __skb_queue_splice(list
, (struct sk_buff
*) head
, head
->next
);
946 head
->qlen
+= list
->qlen
;
951 * skb_queue_splice - join two skb lists and reinitialise the emptied list
952 * @list: the new list to add
953 * @head: the place to add it in the first list
955 * The list at @list is reinitialised
957 static inline void skb_queue_splice_init(struct sk_buff_head
*list
,
958 struct sk_buff_head
*head
)
960 if (!skb_queue_empty(list
)) {
961 __skb_queue_splice(list
, (struct sk_buff
*) head
, head
->next
);
962 head
->qlen
+= list
->qlen
;
963 __skb_queue_head_init(list
);
968 * skb_queue_splice_tail - join two skb lists, each list being a queue
969 * @list: the new list to add
970 * @head: the place to add it in the first list
972 static inline void skb_queue_splice_tail(const struct sk_buff_head
*list
,
973 struct sk_buff_head
*head
)
975 if (!skb_queue_empty(list
)) {
976 __skb_queue_splice(list
, head
->prev
, (struct sk_buff
*) head
);
977 head
->qlen
+= list
->qlen
;
982 * skb_queue_splice_tail - join two skb lists and reinitialise the emptied list
983 * @list: the new list to add
984 * @head: the place to add it in the first list
986 * Each of the lists is a queue.
987 * The list at @list is reinitialised
989 static inline void skb_queue_splice_tail_init(struct sk_buff_head
*list
,
990 struct sk_buff_head
*head
)
992 if (!skb_queue_empty(list
)) {
993 __skb_queue_splice(list
, head
->prev
, (struct sk_buff
*) head
);
994 head
->qlen
+= list
->qlen
;
995 __skb_queue_head_init(list
);
1000 * __skb_queue_after - queue a buffer at the list head
1001 * @list: list to use
1002 * @prev: place after this buffer
1003 * @newsk: buffer to queue
1005 * Queue a buffer int the middle of a list. This function takes no locks
1006 * and you must therefore hold required locks before calling it.
1008 * A buffer cannot be placed on two lists at the same time.
1010 static inline void __skb_queue_after(struct sk_buff_head
*list
,
1011 struct sk_buff
*prev
,
1012 struct sk_buff
*newsk
)
1014 __skb_insert(newsk
, prev
, prev
->next
, list
);
1017 extern void skb_append(struct sk_buff
*old
, struct sk_buff
*newsk
,
1018 struct sk_buff_head
*list
);
1020 static inline void __skb_queue_before(struct sk_buff_head
*list
,
1021 struct sk_buff
*next
,
1022 struct sk_buff
*newsk
)
1024 __skb_insert(newsk
, next
->prev
, next
, list
);
1028 * __skb_queue_head - queue a buffer at the list head
1029 * @list: list to use
1030 * @newsk: buffer to queue
1032 * Queue a buffer at the start of a list. This function takes no locks
1033 * and you must therefore hold required locks before calling it.
1035 * A buffer cannot be placed on two lists at the same time.
1037 extern void skb_queue_head(struct sk_buff_head
*list
, struct sk_buff
*newsk
);
1038 static inline void __skb_queue_head(struct sk_buff_head
*list
,
1039 struct sk_buff
*newsk
)
1041 __skb_queue_after(list
, (struct sk_buff
*)list
, newsk
);
1045 * __skb_queue_tail - queue a buffer at the list tail
1046 * @list: list to use
1047 * @newsk: buffer to queue
1049 * Queue a buffer at the end of a list. This function takes no locks
1050 * and you must therefore hold required locks before calling it.
1052 * A buffer cannot be placed on two lists at the same time.
1054 extern void skb_queue_tail(struct sk_buff_head
*list
, struct sk_buff
*newsk
);
1055 static inline void __skb_queue_tail(struct sk_buff_head
*list
,
1056 struct sk_buff
*newsk
)
1058 __skb_queue_before(list
, (struct sk_buff
*)list
, newsk
);
1062 * remove sk_buff from list. _Must_ be called atomically, and with
1065 extern void skb_unlink(struct sk_buff
*skb
, struct sk_buff_head
*list
);
1066 static inline void __skb_unlink(struct sk_buff
*skb
, struct sk_buff_head
*list
)
1068 struct sk_buff
*next
, *prev
;
1073 skb
->next
= skb
->prev
= NULL
;
1079 * __skb_dequeue - remove from the head of the queue
1080 * @list: list to dequeue from
1082 * Remove the head of the list. This function does not take any locks
1083 * so must be used with appropriate locks held only. The head item is
1084 * returned or %NULL if the list is empty.
1086 extern struct sk_buff
*skb_dequeue(struct sk_buff_head
*list
);
1087 static inline struct sk_buff
*__skb_dequeue(struct sk_buff_head
*list
)
1089 struct sk_buff
*skb
= skb_peek(list
);
1091 __skb_unlink(skb
, list
);
1096 * __skb_dequeue_tail - remove from the tail of the queue
1097 * @list: list to dequeue from
1099 * Remove the tail of the list. This function does not take any locks
1100 * so must be used with appropriate locks held only. The tail item is
1101 * returned or %NULL if the list is empty.
1103 extern struct sk_buff
*skb_dequeue_tail(struct sk_buff_head
*list
);
1104 static inline struct sk_buff
*__skb_dequeue_tail(struct sk_buff_head
*list
)
1106 struct sk_buff
*skb
= skb_peek_tail(list
);
1108 __skb_unlink(skb
, list
);
1113 static inline int skb_is_nonlinear(const struct sk_buff
*skb
)
1115 return skb
->data_len
;
1118 static inline unsigned int skb_headlen(const struct sk_buff
*skb
)
1120 return skb
->len
- skb
->data_len
;
1123 static inline int skb_pagelen(const struct sk_buff
*skb
)
1127 for (i
= (int)skb_shinfo(skb
)->nr_frags
- 1; i
>= 0; i
--)
1128 len
+= skb_shinfo(skb
)->frags
[i
].size
;
1129 return len
+ skb_headlen(skb
);
1132 static inline void skb_fill_page_desc(struct sk_buff
*skb
, int i
,
1133 struct page
*page
, int off
, int size
)
1135 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
1138 frag
->page_offset
= off
;
1140 skb_shinfo(skb
)->nr_frags
= i
+ 1;
1143 extern void skb_add_rx_frag(struct sk_buff
*skb
, int i
, struct page
*page
,
1146 #define SKB_PAGE_ASSERT(skb) BUG_ON(skb_shinfo(skb)->nr_frags)
1147 #define SKB_FRAG_ASSERT(skb) BUG_ON(skb_has_frags(skb))
1148 #define SKB_LINEAR_ASSERT(skb) BUG_ON(skb_is_nonlinear(skb))
1150 #ifdef NET_SKBUFF_DATA_USES_OFFSET
1151 static inline unsigned char *skb_tail_pointer(const struct sk_buff
*skb
)
1153 return skb
->head
+ skb
->tail
;
1156 static inline void skb_reset_tail_pointer(struct sk_buff
*skb
)
1158 skb
->tail
= skb
->data
- skb
->head
;
1161 static inline void skb_set_tail_pointer(struct sk_buff
*skb
, const int offset
)
1163 skb_reset_tail_pointer(skb
);
1164 skb
->tail
+= offset
;
1166 #else /* NET_SKBUFF_DATA_USES_OFFSET */
1167 static inline unsigned char *skb_tail_pointer(const struct sk_buff
*skb
)
1172 static inline void skb_reset_tail_pointer(struct sk_buff
*skb
)
1174 skb
->tail
= skb
->data
;
1177 static inline void skb_set_tail_pointer(struct sk_buff
*skb
, const int offset
)
1179 skb
->tail
= skb
->data
+ offset
;
1182 #endif /* NET_SKBUFF_DATA_USES_OFFSET */
1185 * Add data to an sk_buff
1187 extern unsigned char *skb_put(struct sk_buff
*skb
, unsigned int len
);
1188 static inline unsigned char *__skb_put(struct sk_buff
*skb
, unsigned int len
)
1190 unsigned char *tmp
= skb_tail_pointer(skb
);
1191 SKB_LINEAR_ASSERT(skb
);
1197 extern unsigned char *skb_push(struct sk_buff
*skb
, unsigned int len
);
1198 static inline unsigned char *__skb_push(struct sk_buff
*skb
, unsigned int len
)
1205 extern unsigned char *skb_pull(struct sk_buff
*skb
, unsigned int len
);
1206 static inline unsigned char *__skb_pull(struct sk_buff
*skb
, unsigned int len
)
1209 BUG_ON(skb
->len
< skb
->data_len
);
1210 return skb
->data
+= len
;
1213 static inline unsigned char *skb_pull_inline(struct sk_buff
*skb
, unsigned int len
)
1215 return unlikely(len
> skb
->len
) ? NULL
: __skb_pull(skb
, len
);
1218 extern unsigned char *__pskb_pull_tail(struct sk_buff
*skb
, int delta
);
1220 static inline unsigned char *__pskb_pull(struct sk_buff
*skb
, unsigned int len
)
1222 if (len
> skb_headlen(skb
) &&
1223 !__pskb_pull_tail(skb
, len
- skb_headlen(skb
)))
1226 return skb
->data
+= len
;
1229 static inline unsigned char *pskb_pull(struct sk_buff
*skb
, unsigned int len
)
1231 return unlikely(len
> skb
->len
) ? NULL
: __pskb_pull(skb
, len
);
1234 static inline int pskb_may_pull(struct sk_buff
*skb
, unsigned int len
)
1236 if (likely(len
<= skb_headlen(skb
)))
1238 if (unlikely(len
> skb
->len
))
1240 return __pskb_pull_tail(skb
, len
- skb_headlen(skb
)) != NULL
;
1244 * skb_headroom - bytes at buffer head
1245 * @skb: buffer to check
1247 * Return the number of bytes of free space at the head of an &sk_buff.
1249 static inline unsigned int skb_headroom(const struct sk_buff
*skb
)
1251 return skb
->data
- skb
->head
;
1255 * skb_tailroom - bytes at buffer end
1256 * @skb: buffer to check
1258 * Return the number of bytes of free space at the tail of an sk_buff
1260 static inline int skb_tailroom(const struct sk_buff
*skb
)
1262 return skb_is_nonlinear(skb
) ? 0 : skb
->end
- skb
->tail
;
1266 * skb_reserve - adjust headroom
1267 * @skb: buffer to alter
1268 * @len: bytes to move
1270 * Increase the headroom of an empty &sk_buff by reducing the tail
1271 * room. This is only allowed for an empty buffer.
1273 static inline void skb_reserve(struct sk_buff
*skb
, int len
)
1279 #ifdef NET_SKBUFF_DATA_USES_OFFSET
1280 static inline unsigned char *skb_transport_header(const struct sk_buff
*skb
)
1282 return skb
->head
+ skb
->transport_header
;
1285 static inline void skb_reset_transport_header(struct sk_buff
*skb
)
1287 skb
->transport_header
= skb
->data
- skb
->head
;
1290 static inline void skb_set_transport_header(struct sk_buff
*skb
,
1293 skb_reset_transport_header(skb
);
1294 skb
->transport_header
+= offset
;
1297 static inline unsigned char *skb_network_header(const struct sk_buff
*skb
)
1299 return skb
->head
+ skb
->network_header
;
1302 static inline void skb_reset_network_header(struct sk_buff
*skb
)
1304 skb
->network_header
= skb
->data
- skb
->head
;
1307 static inline void skb_set_network_header(struct sk_buff
*skb
, const int offset
)
1309 skb_reset_network_header(skb
);
1310 skb
->network_header
+= offset
;
1313 static inline unsigned char *skb_mac_header(const struct sk_buff
*skb
)
1315 return skb
->head
+ skb
->mac_header
;
1318 static inline int skb_mac_header_was_set(const struct sk_buff
*skb
)
1320 return skb
->mac_header
!= ~0U;
1323 static inline void skb_reset_mac_header(struct sk_buff
*skb
)
1325 skb
->mac_header
= skb
->data
- skb
->head
;
1328 static inline void skb_set_mac_header(struct sk_buff
*skb
, const int offset
)
1330 skb_reset_mac_header(skb
);
1331 skb
->mac_header
+= offset
;
1334 #else /* NET_SKBUFF_DATA_USES_OFFSET */
1336 static inline unsigned char *skb_transport_header(const struct sk_buff
*skb
)
1338 return skb
->transport_header
;
1341 static inline void skb_reset_transport_header(struct sk_buff
*skb
)
1343 skb
->transport_header
= skb
->data
;
1346 static inline void skb_set_transport_header(struct sk_buff
*skb
,
1349 skb
->transport_header
= skb
->data
+ offset
;
1352 static inline unsigned char *skb_network_header(const struct sk_buff
*skb
)
1354 return skb
->network_header
;
1357 static inline void skb_reset_network_header(struct sk_buff
*skb
)
1359 skb
->network_header
= skb
->data
;
1362 static inline void skb_set_network_header(struct sk_buff
*skb
, const int offset
)
1364 skb
->network_header
= skb
->data
+ offset
;
1367 static inline unsigned char *skb_mac_header(const struct sk_buff
*skb
)
1369 return skb
->mac_header
;
1372 static inline int skb_mac_header_was_set(const struct sk_buff
*skb
)
1374 return skb
->mac_header
!= NULL
;
1377 static inline void skb_reset_mac_header(struct sk_buff
*skb
)
1379 skb
->mac_header
= skb
->data
;
1382 static inline void skb_set_mac_header(struct sk_buff
*skb
, const int offset
)
1384 skb
->mac_header
= skb
->data
+ offset
;
1386 #endif /* NET_SKBUFF_DATA_USES_OFFSET */
1388 static inline int skb_transport_offset(const struct sk_buff
*skb
)
1390 return skb_transport_header(skb
) - skb
->data
;
1393 static inline u32
skb_network_header_len(const struct sk_buff
*skb
)
1395 return skb
->transport_header
- skb
->network_header
;
1398 static inline int skb_network_offset(const struct sk_buff
*skb
)
1400 return skb_network_header(skb
) - skb
->data
;
1403 static inline int pskb_network_may_pull(struct sk_buff
*skb
, unsigned int len
)
1405 return pskb_may_pull(skb
, skb_network_offset(skb
) + len
);
1409 * CPUs often take a performance hit when accessing unaligned memory
1410 * locations. The actual performance hit varies, it can be small if the
1411 * hardware handles it or large if we have to take an exception and fix it
1414 * Since an ethernet header is 14 bytes network drivers often end up with
1415 * the IP header at an unaligned offset. The IP header can be aligned by
1416 * shifting the start of the packet by 2 bytes. Drivers should do this
1419 * skb_reserve(skb, NET_IP_ALIGN);
1421 * The downside to this alignment of the IP header is that the DMA is now
1422 * unaligned. On some architectures the cost of an unaligned DMA is high
1423 * and this cost outweighs the gains made by aligning the IP header.
1425 * Since this trade off varies between architectures, we allow NET_IP_ALIGN
1428 #ifndef NET_IP_ALIGN
1429 #define NET_IP_ALIGN 2
1433 * The networking layer reserves some headroom in skb data (via
1434 * dev_alloc_skb). This is used to avoid having to reallocate skb data when
1435 * the header has to grow. In the default case, if the header has to grow
1436 * 32 bytes or less we avoid the reallocation.
1438 * Unfortunately this headroom changes the DMA alignment of the resulting
1439 * network packet. As for NET_IP_ALIGN, this unaligned DMA is expensive
1440 * on some architectures. An architecture can override this value,
1441 * perhaps setting it to a cacheline in size (since that will maintain
1442 * cacheline alignment of the DMA). It must be a power of 2.
1444 * Various parts of the networking layer expect at least 32 bytes of
1445 * headroom, you should not reduce this.
1447 * Using max(32, L1_CACHE_BYTES) makes sense (especially with RPS)
1448 * to reduce average number of cache lines per packet.
1449 * get_rps_cpus() for example only access one 64 bytes aligned block :
1450 * NET_IP_ALIGN(2) + ethernet_header(14) + IP_header(20/40) + ports(8)
1453 #define NET_SKB_PAD max(32, L1_CACHE_BYTES)
1456 extern int ___pskb_trim(struct sk_buff
*skb
, unsigned int len
);
1458 static inline void __skb_trim(struct sk_buff
*skb
, unsigned int len
)
1460 if (unlikely(skb
->data_len
)) {
1465 skb_set_tail_pointer(skb
, len
);
1468 extern void skb_trim(struct sk_buff
*skb
, unsigned int len
);
1470 static inline int __pskb_trim(struct sk_buff
*skb
, unsigned int len
)
1473 return ___pskb_trim(skb
, len
);
1474 __skb_trim(skb
, len
);
1478 static inline int pskb_trim(struct sk_buff
*skb
, unsigned int len
)
1480 return (len
< skb
->len
) ? __pskb_trim(skb
, len
) : 0;
1484 * pskb_trim_unique - remove end from a paged unique (not cloned) buffer
1485 * @skb: buffer to alter
1488 * This is identical to pskb_trim except that the caller knows that
1489 * the skb is not cloned so we should never get an error due to out-
1492 static inline void pskb_trim_unique(struct sk_buff
*skb
, unsigned int len
)
1494 int err
= pskb_trim(skb
, len
);
1499 * skb_orphan - orphan a buffer
1500 * @skb: buffer to orphan
1502 * If a buffer currently has an owner then we call the owner's
1503 * destructor function and make the @skb unowned. The buffer continues
1504 * to exist but is no longer charged to its former owner.
1506 static inline void skb_orphan(struct sk_buff
*skb
)
1508 if (skb
->destructor
)
1509 skb
->destructor(skb
);
1510 skb
->destructor
= NULL
;
1515 * __skb_queue_purge - empty a list
1516 * @list: list to empty
1518 * Delete all buffers on an &sk_buff list. Each buffer is removed from
1519 * the list and one reference dropped. This function does not take the
1520 * list lock and the caller must hold the relevant locks to use it.
1522 extern void skb_queue_purge(struct sk_buff_head
*list
);
1523 static inline void __skb_queue_purge(struct sk_buff_head
*list
)
1525 struct sk_buff
*skb
;
1526 while ((skb
= __skb_dequeue(list
)) != NULL
)
1531 * __dev_alloc_skb - allocate an skbuff for receiving
1532 * @length: length to allocate
1533 * @gfp_mask: get_free_pages mask, passed to alloc_skb
1535 * Allocate a new &sk_buff and assign it a usage count of one. The
1536 * buffer has unspecified headroom built in. Users should allocate
1537 * the headroom they think they need without accounting for the
1538 * built in space. The built in space is used for optimisations.
1540 * %NULL is returned if there is no free memory.
1542 static inline struct sk_buff
*__dev_alloc_skb(unsigned int length
,
1545 struct sk_buff
*skb
= alloc_skb(length
+ NET_SKB_PAD
, gfp_mask
);
1547 skb_reserve(skb
, NET_SKB_PAD
);
1551 extern struct sk_buff
*dev_alloc_skb(unsigned int length
);
1553 extern struct sk_buff
*__netdev_alloc_skb(struct net_device
*dev
,
1554 unsigned int length
, gfp_t gfp_mask
);
1557 * netdev_alloc_skb - allocate an skbuff for rx on a specific device
1558 * @dev: network device to receive on
1559 * @length: length to allocate
1561 * Allocate a new &sk_buff and assign it a usage count of one. The
1562 * buffer has unspecified headroom built in. Users should allocate
1563 * the headroom they think they need without accounting for the
1564 * built in space. The built in space is used for optimisations.
1566 * %NULL is returned if there is no free memory. Although this function
1567 * allocates memory it can be called from an interrupt.
1569 static inline struct sk_buff
*netdev_alloc_skb(struct net_device
*dev
,
1570 unsigned int length
)
1572 return __netdev_alloc_skb(dev
, length
, GFP_ATOMIC
);
1575 static inline struct sk_buff
*netdev_alloc_skb_ip_align(struct net_device
*dev
,
1576 unsigned int length
)
1578 struct sk_buff
*skb
= netdev_alloc_skb(dev
, length
+ NET_IP_ALIGN
);
1580 if (NET_IP_ALIGN
&& skb
)
1581 skb_reserve(skb
, NET_IP_ALIGN
);
1585 extern struct page
*__netdev_alloc_page(struct net_device
*dev
, gfp_t gfp_mask
);
1588 * netdev_alloc_page - allocate a page for ps-rx on a specific device
1589 * @dev: network device to receive on
1591 * Allocate a new page node local to the specified device.
1593 * %NULL is returned if there is no free memory.
1595 static inline struct page
*netdev_alloc_page(struct net_device
*dev
)
1597 return __netdev_alloc_page(dev
, GFP_ATOMIC
);
1600 static inline void netdev_free_page(struct net_device
*dev
, struct page
*page
)
1606 * skb_clone_writable - is the header of a clone writable
1607 * @skb: buffer to check
1608 * @len: length up to which to write
1610 * Returns true if modifying the header part of the cloned buffer
1611 * does not requires the data to be copied.
1613 static inline int skb_clone_writable(struct sk_buff
*skb
, unsigned int len
)
1615 return !skb_header_cloned(skb
) &&
1616 skb_headroom(skb
) + len
<= skb
->hdr_len
;
1619 static inline int __skb_cow(struct sk_buff
*skb
, unsigned int headroom
,
1624 if (headroom
< NET_SKB_PAD
)
1625 headroom
= NET_SKB_PAD
;
1626 if (headroom
> skb_headroom(skb
))
1627 delta
= headroom
- skb_headroom(skb
);
1629 if (delta
|| cloned
)
1630 return pskb_expand_head(skb
, ALIGN(delta
, NET_SKB_PAD
), 0,
1636 * skb_cow - copy header of skb when it is required
1637 * @skb: buffer to cow
1638 * @headroom: needed headroom
1640 * If the skb passed lacks sufficient headroom or its data part
1641 * is shared, data is reallocated. If reallocation fails, an error
1642 * is returned and original skb is not changed.
1644 * The result is skb with writable area skb->head...skb->tail
1645 * and at least @headroom of space at head.
1647 static inline int skb_cow(struct sk_buff
*skb
, unsigned int headroom
)
1649 return __skb_cow(skb
, headroom
, skb_cloned(skb
));
1653 * skb_cow_head - skb_cow but only making the head writable
1654 * @skb: buffer to cow
1655 * @headroom: needed headroom
1657 * This function is identical to skb_cow except that we replace the
1658 * skb_cloned check by skb_header_cloned. It should be used when
1659 * you only need to push on some header and do not need to modify
1662 static inline int skb_cow_head(struct sk_buff
*skb
, unsigned int headroom
)
1664 return __skb_cow(skb
, headroom
, skb_header_cloned(skb
));
1668 * skb_padto - pad an skbuff up to a minimal size
1669 * @skb: buffer to pad
1670 * @len: minimal length
1672 * Pads up a buffer to ensure the trailing bytes exist and are
1673 * blanked. If the buffer already contains sufficient data it
1674 * is untouched. Otherwise it is extended. Returns zero on
1675 * success. The skb is freed on error.
1678 static inline int skb_padto(struct sk_buff
*skb
, unsigned int len
)
1680 unsigned int size
= skb
->len
;
1681 if (likely(size
>= len
))
1683 return skb_pad(skb
, len
- size
);
1686 static inline int skb_add_data(struct sk_buff
*skb
,
1687 char __user
*from
, int copy
)
1689 const int off
= skb
->len
;
1691 if (skb
->ip_summed
== CHECKSUM_NONE
) {
1693 __wsum csum
= csum_and_copy_from_user(from
, skb_put(skb
, copy
),
1696 skb
->csum
= csum_block_add(skb
->csum
, csum
, off
);
1699 } else if (!copy_from_user(skb_put(skb
, copy
), from
, copy
))
1702 __skb_trim(skb
, off
);
1706 static inline int skb_can_coalesce(struct sk_buff
*skb
, int i
,
1707 struct page
*page
, int off
)
1710 struct skb_frag_struct
*frag
= &skb_shinfo(skb
)->frags
[i
- 1];
1712 return page
== frag
->page
&&
1713 off
== frag
->page_offset
+ frag
->size
;
1718 static inline int __skb_linearize(struct sk_buff
*skb
)
1720 return __pskb_pull_tail(skb
, skb
->data_len
) ? 0 : -ENOMEM
;
1724 * skb_linearize - convert paged skb to linear one
1725 * @skb: buffer to linarize
1727 * If there is no free memory -ENOMEM is returned, otherwise zero
1728 * is returned and the old skb data released.
1730 static inline int skb_linearize(struct sk_buff
*skb
)
1732 return skb_is_nonlinear(skb
) ? __skb_linearize(skb
) : 0;
1736 * skb_linearize_cow - make sure skb is linear and writable
1737 * @skb: buffer to process
1739 * If there is no free memory -ENOMEM is returned, otherwise zero
1740 * is returned and the old skb data released.
1742 static inline int skb_linearize_cow(struct sk_buff
*skb
)
1744 return skb_is_nonlinear(skb
) || skb_cloned(skb
) ?
1745 __skb_linearize(skb
) : 0;
1749 * skb_postpull_rcsum - update checksum for received skb after pull
1750 * @skb: buffer to update
1751 * @start: start of data before pull
1752 * @len: length of data pulled
1754 * After doing a pull on a received packet, you need to call this to
1755 * update the CHECKSUM_COMPLETE checksum, or set ip_summed to
1756 * CHECKSUM_NONE so that it can be recomputed from scratch.
1759 static inline void skb_postpull_rcsum(struct sk_buff
*skb
,
1760 const void *start
, unsigned int len
)
1762 if (skb
->ip_summed
== CHECKSUM_COMPLETE
)
1763 skb
->csum
= csum_sub(skb
->csum
, csum_partial(start
, len
, 0));
1766 unsigned char *skb_pull_rcsum(struct sk_buff
*skb
, unsigned int len
);
1769 * pskb_trim_rcsum - trim received skb and update checksum
1770 * @skb: buffer to trim
1773 * This is exactly the same as pskb_trim except that it ensures the
1774 * checksum of received packets are still valid after the operation.
1777 static inline int pskb_trim_rcsum(struct sk_buff
*skb
, unsigned int len
)
1779 if (likely(len
>= skb
->len
))
1781 if (skb
->ip_summed
== CHECKSUM_COMPLETE
)
1782 skb
->ip_summed
= CHECKSUM_NONE
;
1783 return __pskb_trim(skb
, len
);
1786 #define skb_queue_walk(queue, skb) \
1787 for (skb = (queue)->next; \
1788 prefetch(skb->next), (skb != (struct sk_buff *)(queue)); \
1791 #define skb_queue_walk_safe(queue, skb, tmp) \
1792 for (skb = (queue)->next, tmp = skb->next; \
1793 skb != (struct sk_buff *)(queue); \
1794 skb = tmp, tmp = skb->next)
1796 #define skb_queue_walk_from(queue, skb) \
1797 for (; prefetch(skb->next), (skb != (struct sk_buff *)(queue)); \
1800 #define skb_queue_walk_from_safe(queue, skb, tmp) \
1801 for (tmp = skb->next; \
1802 skb != (struct sk_buff *)(queue); \
1803 skb = tmp, tmp = skb->next)
1805 #define skb_queue_reverse_walk(queue, skb) \
1806 for (skb = (queue)->prev; \
1807 prefetch(skb->prev), (skb != (struct sk_buff *)(queue)); \
1811 static inline bool skb_has_frags(const struct sk_buff
*skb
)
1813 return skb_shinfo(skb
)->frag_list
!= NULL
;
1816 static inline void skb_frag_list_init(struct sk_buff
*skb
)
1818 skb_shinfo(skb
)->frag_list
= NULL
;
1821 static inline void skb_frag_add_head(struct sk_buff
*skb
, struct sk_buff
*frag
)
1823 frag
->next
= skb_shinfo(skb
)->frag_list
;
1824 skb_shinfo(skb
)->frag_list
= frag
;
1827 #define skb_walk_frags(skb, iter) \
1828 for (iter = skb_shinfo(skb)->frag_list; iter; iter = iter->next)
1830 extern struct sk_buff
*__skb_recv_datagram(struct sock
*sk
, unsigned flags
,
1831 int *peeked
, int *err
);
1832 extern struct sk_buff
*skb_recv_datagram(struct sock
*sk
, unsigned flags
,
1833 int noblock
, int *err
);
1834 extern unsigned int datagram_poll(struct file
*file
, struct socket
*sock
,
1835 struct poll_table_struct
*wait
);
1836 extern int skb_copy_datagram_iovec(const struct sk_buff
*from
,
1837 int offset
, struct iovec
*to
,
1839 extern int skb_copy_and_csum_datagram_iovec(struct sk_buff
*skb
,
1842 extern int skb_copy_datagram_from_iovec(struct sk_buff
*skb
,
1844 const struct iovec
*from
,
1847 extern int skb_copy_datagram_const_iovec(const struct sk_buff
*from
,
1849 const struct iovec
*to
,
1852 extern void skb_free_datagram(struct sock
*sk
, struct sk_buff
*skb
);
1853 extern void skb_free_datagram_locked(struct sock
*sk
,
1854 struct sk_buff
*skb
);
1855 extern int skb_kill_datagram(struct sock
*sk
, struct sk_buff
*skb
,
1856 unsigned int flags
);
1857 extern __wsum
skb_checksum(const struct sk_buff
*skb
, int offset
,
1858 int len
, __wsum csum
);
1859 extern int skb_copy_bits(const struct sk_buff
*skb
, int offset
,
1861 extern int skb_store_bits(struct sk_buff
*skb
, int offset
,
1862 const void *from
, int len
);
1863 extern __wsum
skb_copy_and_csum_bits(const struct sk_buff
*skb
,
1864 int offset
, u8
*to
, int len
,
1866 extern int skb_splice_bits(struct sk_buff
*skb
,
1867 unsigned int offset
,
1868 struct pipe_inode_info
*pipe
,
1870 unsigned int flags
);
1871 extern void skb_copy_and_csum_dev(const struct sk_buff
*skb
, u8
*to
);
1872 extern void skb_split(struct sk_buff
*skb
,
1873 struct sk_buff
*skb1
, const u32 len
);
1874 extern int skb_shift(struct sk_buff
*tgt
, struct sk_buff
*skb
,
1877 extern struct sk_buff
*skb_segment(struct sk_buff
*skb
, int features
);
1878 extern struct sk_buff
*skb_tcp_segment(struct sk_buff
*skb
, int features
,
1879 unsigned int oldlen
, unsigned thlen
);
1881 static inline void *skb_header_pointer(const struct sk_buff
*skb
, int offset
,
1882 int len
, void *buffer
)
1884 int hlen
= skb_headlen(skb
);
1886 if (hlen
- offset
>= len
)
1887 return skb
->data
+ offset
;
1889 if (skb_copy_bits(skb
, offset
, buffer
, len
) < 0)
1895 static inline void skb_copy_from_linear_data(const struct sk_buff
*skb
,
1897 const unsigned int len
)
1899 memcpy(to
, skb
->data
, len
);
1902 static inline void skb_copy_from_linear_data_offset(const struct sk_buff
*skb
,
1903 const int offset
, void *to
,
1904 const unsigned int len
)
1906 memcpy(to
, skb
->data
+ offset
, len
);
1909 static inline void skb_copy_to_linear_data(struct sk_buff
*skb
,
1911 const unsigned int len
)
1913 memcpy(skb
->data
, from
, len
);
1916 static inline void skb_copy_to_linear_data_offset(struct sk_buff
*skb
,
1919 const unsigned int len
)
1921 memcpy(skb
->data
+ offset
, from
, len
);
1924 extern void skb_init(void);
1926 static inline ktime_t
skb_get_ktime(const struct sk_buff
*skb
)
1932 * skb_get_timestamp - get timestamp from a skb
1933 * @skb: skb to get stamp from
1934 * @stamp: pointer to struct timeval to store stamp in
1936 * Timestamps are stored in the skb as offsets to a base timestamp.
1937 * This function converts the offset back to a struct timeval and stores
1940 static inline void skb_get_timestamp(const struct sk_buff
*skb
,
1941 struct timeval
*stamp
)
1943 *stamp
= ktime_to_timeval(skb
->tstamp
);
1946 static inline void skb_get_timestampns(const struct sk_buff
*skb
,
1947 struct timespec
*stamp
)
1949 *stamp
= ktime_to_timespec(skb
->tstamp
);
1952 static inline void __net_timestamp(struct sk_buff
*skb
)
1954 skb
->tstamp
= ktime_get_real();
1957 static inline ktime_t
net_timedelta(ktime_t t
)
1959 return ktime_sub(ktime_get_real(), t
);
1962 static inline ktime_t
net_invalid_timestamp(void)
1964 return ktime_set(0, 0);
1967 extern void skb_timestamping_init(void);
1969 #ifdef CONFIG_NETWORK_PHY_TIMESTAMPING
1971 extern void skb_clone_tx_timestamp(struct sk_buff
*skb
);
1972 extern bool skb_defer_rx_timestamp(struct sk_buff
*skb
);
1974 #else /* CONFIG_NETWORK_PHY_TIMESTAMPING */
1976 static inline void skb_clone_tx_timestamp(struct sk_buff
*skb
)
1980 static inline bool skb_defer_rx_timestamp(struct sk_buff
*skb
)
1985 #endif /* !CONFIG_NETWORK_PHY_TIMESTAMPING */
1988 * skb_complete_tx_timestamp() - deliver cloned skb with tx timestamps
1990 * @skb: clone of the the original outgoing packet
1991 * @hwtstamps: hardware time stamps
1994 void skb_complete_tx_timestamp(struct sk_buff
*skb
,
1995 struct skb_shared_hwtstamps
*hwtstamps
);
1998 * skb_tstamp_tx - queue clone of skb with send time stamps
1999 * @orig_skb: the original outgoing packet
2000 * @hwtstamps: hardware time stamps, may be NULL if not available
2002 * If the skb has a socket associated, then this function clones the
2003 * skb (thus sharing the actual data and optional structures), stores
2004 * the optional hardware time stamping information (if non NULL) or
2005 * generates a software time stamp (otherwise), then queues the clone
2006 * to the error queue of the socket. Errors are silently ignored.
2008 extern void skb_tstamp_tx(struct sk_buff
*orig_skb
,
2009 struct skb_shared_hwtstamps
*hwtstamps
);
2011 static inline void sw_tx_timestamp(struct sk_buff
*skb
)
2013 union skb_shared_tx
*shtx
= skb_tx(skb
);
2014 if (shtx
->software
&& !shtx
->in_progress
)
2015 skb_tstamp_tx(skb
, NULL
);
2019 * skb_tx_timestamp() - Driver hook for transmit timestamping
2021 * Ethernet MAC Drivers should call this function in their hard_xmit()
2022 * function as soon as possible after giving the sk_buff to the MAC
2023 * hardware, but before freeing the sk_buff.
2025 * @skb: A socket buffer.
2027 static inline void skb_tx_timestamp(struct sk_buff
*skb
)
2029 skb_clone_tx_timestamp(skb
);
2030 sw_tx_timestamp(skb
);
2033 extern __sum16
__skb_checksum_complete_head(struct sk_buff
*skb
, int len
);
2034 extern __sum16
__skb_checksum_complete(struct sk_buff
*skb
);
2036 static inline int skb_csum_unnecessary(const struct sk_buff
*skb
)
2038 return skb
->ip_summed
& CHECKSUM_UNNECESSARY
;
2042 * skb_checksum_complete - Calculate checksum of an entire packet
2043 * @skb: packet to process
2045 * This function calculates the checksum over the entire packet plus
2046 * the value of skb->csum. The latter can be used to supply the
2047 * checksum of a pseudo header as used by TCP/UDP. It returns the
2050 * For protocols that contain complete checksums such as ICMP/TCP/UDP,
2051 * this function can be used to verify that checksum on received
2052 * packets. In that case the function should return zero if the
2053 * checksum is correct. In particular, this function will return zero
2054 * if skb->ip_summed is CHECKSUM_UNNECESSARY which indicates that the
2055 * hardware has already verified the correctness of the checksum.
2057 static inline __sum16
skb_checksum_complete(struct sk_buff
*skb
)
2059 return skb_csum_unnecessary(skb
) ?
2060 0 : __skb_checksum_complete(skb
);
2063 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
2064 extern void nf_conntrack_destroy(struct nf_conntrack
*nfct
);
2065 static inline void nf_conntrack_put(struct nf_conntrack
*nfct
)
2067 if (nfct
&& atomic_dec_and_test(&nfct
->use
))
2068 nf_conntrack_destroy(nfct
);
2070 static inline void nf_conntrack_get(struct nf_conntrack
*nfct
)
2073 atomic_inc(&nfct
->use
);
2075 static inline void nf_conntrack_get_reasm(struct sk_buff
*skb
)
2078 atomic_inc(&skb
->users
);
2080 static inline void nf_conntrack_put_reasm(struct sk_buff
*skb
)
2086 #ifdef CONFIG_BRIDGE_NETFILTER
2087 static inline void nf_bridge_put(struct nf_bridge_info
*nf_bridge
)
2089 if (nf_bridge
&& atomic_dec_and_test(&nf_bridge
->use
))
2092 static inline void nf_bridge_get(struct nf_bridge_info
*nf_bridge
)
2095 atomic_inc(&nf_bridge
->use
);
2097 #endif /* CONFIG_BRIDGE_NETFILTER */
2098 static inline void nf_reset(struct sk_buff
*skb
)
2100 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
2101 nf_conntrack_put(skb
->nfct
);
2103 nf_conntrack_put_reasm(skb
->nfct_reasm
);
2104 skb
->nfct_reasm
= NULL
;
2106 #ifdef CONFIG_BRIDGE_NETFILTER
2107 nf_bridge_put(skb
->nf_bridge
);
2108 skb
->nf_bridge
= NULL
;
2112 /* Note: This doesn't put any conntrack and bridge info in dst. */
2113 static inline void __nf_copy(struct sk_buff
*dst
, const struct sk_buff
*src
)
2115 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
2116 dst
->nfct
= src
->nfct
;
2117 nf_conntrack_get(src
->nfct
);
2118 dst
->nfctinfo
= src
->nfctinfo
;
2119 dst
->nfct_reasm
= src
->nfct_reasm
;
2120 nf_conntrack_get_reasm(src
->nfct_reasm
);
2122 #ifdef CONFIG_BRIDGE_NETFILTER
2123 dst
->nf_bridge
= src
->nf_bridge
;
2124 nf_bridge_get(src
->nf_bridge
);
2127 #if defined(CONFIG_IP_NF_LFP)
2128 dst
->nfcache
= src
->nfcache
;
2132 static inline void nf_copy(struct sk_buff
*dst
, const struct sk_buff
*src
)
2134 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
2135 nf_conntrack_put(dst
->nfct
);
2136 nf_conntrack_put_reasm(dst
->nfct_reasm
);
2138 #ifdef CONFIG_BRIDGE_NETFILTER
2139 nf_bridge_put(dst
->nf_bridge
);
2141 __nf_copy(dst
, src
);
2144 #ifdef CONFIG_NETWORK_SECMARK
2145 static inline void skb_copy_secmark(struct sk_buff
*to
, const struct sk_buff
*from
)
2147 to
->secmark
= from
->secmark
;
2150 static inline void skb_init_secmark(struct sk_buff
*skb
)
2155 static inline void skb_copy_secmark(struct sk_buff
*to
, const struct sk_buff
*from
)
2158 static inline void skb_init_secmark(struct sk_buff
*skb
)
2162 static inline void skb_set_queue_mapping(struct sk_buff
*skb
, u16 queue_mapping
)
2164 skb
->queue_mapping
= queue_mapping
;
2167 static inline u16
skb_get_queue_mapping(const struct sk_buff
*skb
)
2169 return skb
->queue_mapping
;
2172 static inline void skb_copy_queue_mapping(struct sk_buff
*to
, const struct sk_buff
*from
)
2174 to
->queue_mapping
= from
->queue_mapping
;
2177 static inline void skb_record_rx_queue(struct sk_buff
*skb
, u16 rx_queue
)
2179 skb
->queue_mapping
= rx_queue
+ 1;
2182 static inline u16
skb_get_rx_queue(const struct sk_buff
*skb
)
2184 return skb
->queue_mapping
- 1;
2187 static inline bool skb_rx_queue_recorded(const struct sk_buff
*skb
)
2189 return (skb
->queue_mapping
!= 0);
2192 extern u16
skb_tx_hash(const struct net_device
*dev
,
2193 const struct sk_buff
*skb
);
2196 static inline struct sec_path
*skb_sec_path(struct sk_buff
*skb
)
2201 static inline struct sec_path
*skb_sec_path(struct sk_buff
*skb
)
2207 static inline int skb_is_gso(const struct sk_buff
*skb
)
2209 return skb_shinfo(skb
)->gso_size
;
2212 static inline int skb_is_gso_v6(const struct sk_buff
*skb
)
2214 return skb_shinfo(skb
)->gso_type
& SKB_GSO_TCPV6
;
2217 extern void __skb_warn_lro_forwarding(const struct sk_buff
*skb
);
2219 static inline bool skb_warn_if_lro(const struct sk_buff
*skb
)
2221 /* LRO sets gso_size but not gso_type, whereas if GSO is really
2222 * wanted then gso_type will be set. */
2223 struct skb_shared_info
*shinfo
= skb_shinfo(skb
);
2224 if (skb_is_nonlinear(skb
) && shinfo
->gso_size
!= 0 &&
2225 unlikely(shinfo
->gso_type
== 0)) {
2226 __skb_warn_lro_forwarding(skb
);
2232 static inline void skb_forward_csum(struct sk_buff
*skb
)
2234 /* Unfortunately we don't support this one. Any brave souls? */
2235 if (skb
->ip_summed
== CHECKSUM_COMPLETE
)
2236 skb
->ip_summed
= CHECKSUM_NONE
;
2239 bool skb_partial_csum_set(struct sk_buff
*skb
, u16 start
, u16 off
);
2240 #endif /* __KERNEL__ */
2241 #endif /* _LINUX_SKBUFF_H */