net: more accurate skb truesize
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / include / linux / skbuff.h
blob64f86951ef74ebb7044dda18ed5bdecbe19c816c
1 /*
2 * Definitions for the 'struct sk_buff' memory handlers.
4 * Authors:
5 * Alan Cox, <gw4pts@gw4pts.ampr.org>
6 * Florian La Roche, <rzsfl@rz.uni-sb.de>
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
14 #ifndef _LINUX_SKBUFF_H
15 #define _LINUX_SKBUFF_H
17 #include <linux/kernel.h>
18 #include <linux/kmemcheck.h>
19 #include <linux/compiler.h>
20 #include <linux/time.h>
21 #include <linux/cache.h>
23 #include <linux/atomic.h>
24 #include <asm/types.h>
25 #include <linux/spinlock.h>
26 #include <linux/net.h>
27 #include <linux/textsearch.h>
28 #include <net/checksum.h>
29 #include <linux/rcupdate.h>
30 #include <linux/dmaengine.h>
31 #include <linux/hrtimer.h>
32 #include <linux/dma-mapping.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 /* return minimum truesize of one skb containing X bytes of data */
50 #define SKB_TRUESIZE(X) ((X) + \
51 SKB_DATA_ALIGN(sizeof(struct sk_buff)) + \
52 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)))
54 /* A. Checksumming of received packets by device.
56 * NONE: device failed to checksum this packet.
57 * skb->csum is undefined.
59 * UNNECESSARY: device parsed packet and wouldbe verified checksum.
60 * skb->csum is undefined.
61 * It is bad option, but, unfortunately, many of vendors do this.
62 * Apparently with secret goal to sell you new device, when you
63 * will add new protocol to your host. F.e. IPv6. 8)
65 * COMPLETE: the most generic way. Device supplied checksum of _all_
66 * the packet as seen by netif_rx in skb->csum.
67 * NOTE: Even if device supports only some protocols, but
68 * is able to produce some skb->csum, it MUST use COMPLETE,
69 * not UNNECESSARY.
71 * PARTIAL: identical to the case for output below. This may occur
72 * on a packet received directly from another Linux OS, e.g.,
73 * a virtualised Linux kernel on the same host. The packet can
74 * be treated in the same way as UNNECESSARY except that on
75 * output (i.e., forwarding) the checksum must be filled in
76 * by the OS or the hardware.
78 * B. Checksumming on output.
80 * NONE: skb is checksummed by protocol or csum is not required.
82 * PARTIAL: device is required to csum packet as seen by hard_start_xmit
83 * from skb->csum_start to the end and to record the checksum
84 * at skb->csum_start + skb->csum_offset.
86 * Device must show its capabilities in dev->features, set
87 * at device setup time.
88 * NETIF_F_HW_CSUM - it is clever device, it is able to checksum
89 * everything.
90 * NETIF_F_NO_CSUM - loopback or reliable single hop media.
91 * NETIF_F_IP_CSUM - device is dumb. It is able to csum only
92 * TCP/UDP over IPv4. Sigh. Vendors like this
93 * way by an unknown reason. Though, see comment above
94 * about CHECKSUM_UNNECESSARY. 8)
95 * NETIF_F_IPV6_CSUM about as dumb as the last one but does IPv6 instead.
97 * Any questions? No questions, good. --ANK
100 struct net_device;
101 struct scatterlist;
102 struct pipe_inode_info;
104 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
105 struct nf_conntrack {
106 atomic_t use;
108 #endif
110 #ifdef CONFIG_BRIDGE_NETFILTER
111 struct nf_bridge_info {
112 atomic_t use;
113 struct net_device *physindev;
114 struct net_device *physoutdev;
115 unsigned int mask;
116 unsigned long data[32 / sizeof(unsigned long)];
118 #endif
120 struct sk_buff_head {
121 /* These two members must be first. */
122 struct sk_buff *next;
123 struct sk_buff *prev;
125 __u32 qlen;
126 spinlock_t lock;
129 struct sk_buff;
131 /* To allow 64K frame to be packed as single skb without frag_list. Since
132 * GRO uses frags we allocate at least 16 regardless of page size.
134 #if (65536/PAGE_SIZE + 2) < 16
135 #define MAX_SKB_FRAGS 16UL
136 #else
137 #define MAX_SKB_FRAGS (65536/PAGE_SIZE + 2)
138 #endif
140 typedef struct skb_frag_struct skb_frag_t;
142 struct skb_frag_struct {
143 struct page *page;
144 #if (BITS_PER_LONG > 32) || (PAGE_SIZE >= 65536)
145 __u32 page_offset;
146 __u32 size;
147 #else
148 __u16 page_offset;
149 __u16 size;
150 #endif
153 #define HAVE_HW_TIME_STAMP
156 * struct skb_shared_hwtstamps - hardware time stamps
157 * @hwtstamp: hardware time stamp transformed into duration
158 * since arbitrary point in time
159 * @syststamp: hwtstamp transformed to system time base
161 * Software time stamps generated by ktime_get_real() are stored in
162 * skb->tstamp. The relation between the different kinds of time
163 * stamps is as follows:
165 * syststamp and tstamp can be compared against each other in
166 * arbitrary combinations. The accuracy of a
167 * syststamp/tstamp/"syststamp from other device" comparison is
168 * limited by the accuracy of the transformation into system time
169 * base. This depends on the device driver and its underlying
170 * hardware.
172 * hwtstamps can only be compared against other hwtstamps from
173 * the same device.
175 * This structure is attached to packets as part of the
176 * &skb_shared_info. Use skb_hwtstamps() to get a pointer.
178 struct skb_shared_hwtstamps {
179 ktime_t hwtstamp;
180 ktime_t syststamp;
183 /* Definitions for tx_flags in struct skb_shared_info */
184 enum {
185 /* generate hardware time stamp */
186 SKBTX_HW_TSTAMP = 1 << 0,
188 /* generate software time stamp */
189 SKBTX_SW_TSTAMP = 1 << 1,
191 /* device driver is going to provide hardware time stamp */
192 SKBTX_IN_PROGRESS = 1 << 2,
194 /* ensure the originating sk reference is available on driver level */
195 SKBTX_DRV_NEEDS_SK_REF = 1 << 3,
197 /* device driver supports TX zero-copy buffers */
198 SKBTX_DEV_ZEROCOPY = 1 << 4,
202 * The callback notifies userspace to release buffers when skb DMA is done in
203 * lower device, the skb last reference should be 0 when calling this.
204 * The desc is used to track userspace buffer index.
206 struct ubuf_info {
207 void (*callback)(void *);
208 void *arg;
209 unsigned long desc;
212 /* This data is invariant across clones and lives at
213 * the end of the header data, ie. at skb->end.
215 struct skb_shared_info {
216 unsigned short nr_frags;
217 unsigned short gso_size;
218 /* Warning: this field is not always filled in (UFO)! */
219 unsigned short gso_segs;
220 unsigned short gso_type;
221 __be32 ip6_frag_id;
222 __u8 tx_flags;
223 struct sk_buff *frag_list;
224 struct skb_shared_hwtstamps hwtstamps;
227 * Warning : all fields before dataref are cleared in __alloc_skb()
229 atomic_t dataref;
231 /* Intermediate layers must ensure that destructor_arg
232 * remains valid until skb destructor */
233 void * destructor_arg;
235 /* must be last field, see pskb_expand_head() */
236 skb_frag_t frags[MAX_SKB_FRAGS];
239 /* We divide dataref into two halves. The higher 16 bits hold references
240 * to the payload part of skb->data. The lower 16 bits hold references to
241 * the entire skb->data. A clone of a headerless skb holds the length of
242 * the header in skb->hdr_len.
244 * All users must obey the rule that the skb->data reference count must be
245 * greater than or equal to the payload reference count.
247 * Holding a reference to the payload part means that the user does not
248 * care about modifications to the header part of skb->data.
250 #define SKB_DATAREF_SHIFT 16
251 #define SKB_DATAREF_MASK ((1 << SKB_DATAREF_SHIFT) - 1)
254 enum {
255 SKB_FCLONE_UNAVAILABLE,
256 SKB_FCLONE_ORIG,
257 SKB_FCLONE_CLONE,
260 enum {
261 SKB_GSO_TCPV4 = 1 << 0,
262 SKB_GSO_UDP = 1 << 1,
264 /* This indicates the skb is from an untrusted source. */
265 SKB_GSO_DODGY = 1 << 2,
267 /* This indicates the tcp segment has CWR set. */
268 SKB_GSO_TCP_ECN = 1 << 3,
270 SKB_GSO_TCPV6 = 1 << 4,
272 SKB_GSO_FCOE = 1 << 5,
275 #if BITS_PER_LONG > 32
276 #define NET_SKBUFF_DATA_USES_OFFSET 1
277 #endif
279 #ifdef NET_SKBUFF_DATA_USES_OFFSET
280 typedef unsigned int sk_buff_data_t;
281 #else
282 typedef unsigned char *sk_buff_data_t;
283 #endif
285 #if defined(CONFIG_NF_DEFRAG_IPV4) || defined(CONFIG_NF_DEFRAG_IPV4_MODULE) || \
286 defined(CONFIG_NF_DEFRAG_IPV6) || defined(CONFIG_NF_DEFRAG_IPV6_MODULE)
287 #define NET_SKBUFF_NF_DEFRAG_NEEDED 1
288 #endif
290 /**
291 * struct sk_buff - socket buffer
292 * @next: Next buffer in list
293 * @prev: Previous buffer in list
294 * @tstamp: Time we arrived
295 * @sk: Socket we are owned by
296 * @dev: Device we arrived on/are leaving by
297 * @cb: Control buffer. Free for use by every layer. Put private vars here
298 * @_skb_refdst: destination entry (with norefcount bit)
299 * @sp: the security path, used for xfrm
300 * @len: Length of actual data
301 * @data_len: Data length
302 * @mac_len: Length of link layer header
303 * @hdr_len: writable header length of cloned skb
304 * @csum: Checksum (must include start/offset pair)
305 * @csum_start: Offset from skb->head where checksumming should start
306 * @csum_offset: Offset from csum_start where checksum should be stored
307 * @priority: Packet queueing priority
308 * @local_df: allow local fragmentation
309 * @cloned: Head may be cloned (check refcnt to be sure)
310 * @ip_summed: Driver fed us an IP checksum
311 * @nohdr: Payload reference only, must not modify header
312 * @nfctinfo: Relationship of this skb to the connection
313 * @pkt_type: Packet class
314 * @fclone: skbuff clone status
315 * @ipvs_property: skbuff is owned by ipvs
316 * @peeked: this packet has been seen already, so stats have been
317 * done for it, don't do them again
318 * @nf_trace: netfilter packet trace flag
319 * @protocol: Packet protocol from driver
320 * @destructor: Destruct function
321 * @nfct: Associated connection, if any
322 * @nfct_reasm: netfilter conntrack re-assembly pointer
323 * @nf_bridge: Saved data about a bridged frame - see br_netfilter.c
324 * @skb_iif: ifindex of device we arrived on
325 * @tc_index: Traffic control index
326 * @tc_verd: traffic control verdict
327 * @rxhash: the packet hash computed on receive
328 * @queue_mapping: Queue mapping for multiqueue devices
329 * @ndisc_nodetype: router type (from link layer)
330 * @ooo_okay: allow the mapping of a socket to a queue to be changed
331 * @l4_rxhash: indicate rxhash is a canonical 4-tuple hash over transport
332 * ports.
333 * @dma_cookie: a cookie to one of several possible DMA operations
334 * done by skb DMA functions
335 * @secmark: security marking
336 * @mark: Generic packet mark
337 * @dropcount: total number of sk_receive_queue overflows
338 * @vlan_tci: vlan tag control information
339 * @transport_header: Transport layer header
340 * @network_header: Network layer header
341 * @mac_header: Link layer header
342 * @tail: Tail pointer
343 * @end: End pointer
344 * @head: Head of buffer
345 * @data: Data head pointer
346 * @truesize: Buffer size
347 * @users: User count - see {datagram,tcp}.c
350 struct sk_buff {
351 /* These two members must be first. */
352 struct sk_buff *next;
353 struct sk_buff *prev;
355 ktime_t tstamp;
357 struct sock *sk;
358 struct net_device *dev;
361 * This is the control buffer. It is free to use for every
362 * layer. Please put your private variables there. If you
363 * want to keep them across layers you have to do a skb_clone()
364 * first. This is owned by whoever has the skb queued ATM.
366 char cb[48] __aligned(8);
368 unsigned long _skb_refdst;
369 #ifdef CONFIG_XFRM
370 struct sec_path *sp;
371 #endif
372 unsigned int len,
373 data_len;
374 __u16 mac_len,
375 hdr_len;
376 union {
377 __wsum csum;
378 struct {
379 __u16 csum_start;
380 __u16 csum_offset;
383 __u32 priority;
384 kmemcheck_bitfield_begin(flags1);
385 __u8 local_df:1,
386 cloned:1,
387 ip_summed:2,
388 nohdr:1,
389 nfctinfo:3;
390 __u8 pkt_type:3,
391 fclone:2,
392 ipvs_property:1,
393 peeked:1,
394 nf_trace:1;
395 kmemcheck_bitfield_end(flags1);
396 __be16 protocol;
398 void (*destructor)(struct sk_buff *skb);
399 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
400 struct nf_conntrack *nfct;
401 #endif
402 #ifdef NET_SKBUFF_NF_DEFRAG_NEEDED
403 struct sk_buff *nfct_reasm;
404 #endif
405 #ifdef CONFIG_BRIDGE_NETFILTER
406 struct nf_bridge_info *nf_bridge;
407 #endif
409 int skb_iif;
410 #ifdef CONFIG_NET_SCHED
411 __u16 tc_index; /* traffic control index */
412 #ifdef CONFIG_NET_CLS_ACT
413 __u16 tc_verd; /* traffic control verdict */
414 #endif
415 #endif
417 __u32 rxhash;
419 __u16 queue_mapping;
420 kmemcheck_bitfield_begin(flags2);
421 #ifdef CONFIG_IPV6_NDISC_NODETYPE
422 __u8 ndisc_nodetype:2;
423 #endif
424 __u8 ooo_okay:1;
425 __u8 l4_rxhash:1;
426 kmemcheck_bitfield_end(flags2);
428 /* 0/13 bit hole */
430 #ifdef CONFIG_NET_DMA
431 dma_cookie_t dma_cookie;
432 #endif
433 #ifdef CONFIG_NETWORK_SECMARK
434 __u32 secmark;
435 #endif
436 union {
437 __u32 mark;
438 __u32 dropcount;
441 __u16 vlan_tci;
443 sk_buff_data_t transport_header;
444 sk_buff_data_t network_header;
445 sk_buff_data_t mac_header;
446 /* These elements must be at the end, see alloc_skb() for details. */
447 sk_buff_data_t tail;
448 sk_buff_data_t end;
449 unsigned char *head,
450 *data;
451 unsigned int truesize;
452 atomic_t users;
455 #ifdef __KERNEL__
457 * Handling routines are only of interest to the kernel
459 #include <linux/slab.h>
461 #include <asm/system.h>
464 * skb might have a dst pointer attached, refcounted or not.
465 * _skb_refdst low order bit is set if refcount was _not_ taken
467 #define SKB_DST_NOREF 1UL
468 #define SKB_DST_PTRMASK ~(SKB_DST_NOREF)
471 * skb_dst - returns skb dst_entry
472 * @skb: buffer
474 * Returns skb dst_entry, regardless of reference taken or not.
476 static inline struct dst_entry *skb_dst(const struct sk_buff *skb)
478 /* If refdst was not refcounted, check we still are in a
479 * rcu_read_lock section
481 WARN_ON((skb->_skb_refdst & SKB_DST_NOREF) &&
482 !rcu_read_lock_held() &&
483 !rcu_read_lock_bh_held());
484 return (struct dst_entry *)(skb->_skb_refdst & SKB_DST_PTRMASK);
488 * skb_dst_set - sets skb dst
489 * @skb: buffer
490 * @dst: dst entry
492 * Sets skb dst, assuming a reference was taken on dst and should
493 * be released by skb_dst_drop()
495 static inline void skb_dst_set(struct sk_buff *skb, struct dst_entry *dst)
497 skb->_skb_refdst = (unsigned long)dst;
500 extern void skb_dst_set_noref(struct sk_buff *skb, struct dst_entry *dst);
503 * skb_dst_is_noref - Test if skb dst isn't refcounted
504 * @skb: buffer
506 static inline bool skb_dst_is_noref(const struct sk_buff *skb)
508 return (skb->_skb_refdst & SKB_DST_NOREF) && skb_dst(skb);
511 static inline struct rtable *skb_rtable(const struct sk_buff *skb)
513 return (struct rtable *)skb_dst(skb);
516 extern void kfree_skb(struct sk_buff *skb);
517 extern void consume_skb(struct sk_buff *skb);
518 extern void __kfree_skb(struct sk_buff *skb);
519 extern struct sk_buff *__alloc_skb(unsigned int size,
520 gfp_t priority, int fclone, int node);
521 static inline struct sk_buff *alloc_skb(unsigned int size,
522 gfp_t priority)
524 return __alloc_skb(size, priority, 0, NUMA_NO_NODE);
527 static inline struct sk_buff *alloc_skb_fclone(unsigned int size,
528 gfp_t priority)
530 return __alloc_skb(size, priority, 1, NUMA_NO_NODE);
533 extern bool skb_recycle_check(struct sk_buff *skb, int skb_size);
535 extern struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src);
536 extern int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask);
537 extern struct sk_buff *skb_clone(struct sk_buff *skb,
538 gfp_t priority);
539 extern struct sk_buff *skb_copy(const struct sk_buff *skb,
540 gfp_t priority);
541 extern struct sk_buff *pskb_copy(struct sk_buff *skb,
542 gfp_t gfp_mask);
543 extern int pskb_expand_head(struct sk_buff *skb,
544 int nhead, int ntail,
545 gfp_t gfp_mask);
546 extern struct sk_buff *skb_realloc_headroom(struct sk_buff *skb,
547 unsigned int headroom);
548 extern struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
549 int newheadroom, int newtailroom,
550 gfp_t priority);
551 extern int skb_to_sgvec(struct sk_buff *skb,
552 struct scatterlist *sg, int offset,
553 int len);
554 extern int skb_cow_data(struct sk_buff *skb, int tailbits,
555 struct sk_buff **trailer);
556 extern int skb_pad(struct sk_buff *skb, int pad);
557 #define dev_kfree_skb(a) consume_skb(a)
559 extern int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
560 int getfrag(void *from, char *to, int offset,
561 int len,int odd, struct sk_buff *skb),
562 void *from, int length);
564 struct skb_seq_state {
565 __u32 lower_offset;
566 __u32 upper_offset;
567 __u32 frag_idx;
568 __u32 stepped_offset;
569 struct sk_buff *root_skb;
570 struct sk_buff *cur_skb;
571 __u8 *frag_data;
574 extern void skb_prepare_seq_read(struct sk_buff *skb,
575 unsigned int from, unsigned int to,
576 struct skb_seq_state *st);
577 extern unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
578 struct skb_seq_state *st);
579 extern void skb_abort_seq_read(struct skb_seq_state *st);
581 extern unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
582 unsigned int to, struct ts_config *config,
583 struct ts_state *state);
585 extern void __skb_get_rxhash(struct sk_buff *skb);
586 static inline __u32 skb_get_rxhash(struct sk_buff *skb)
588 if (!skb->rxhash)
589 __skb_get_rxhash(skb);
591 return skb->rxhash;
594 #ifdef NET_SKBUFF_DATA_USES_OFFSET
595 static inline unsigned char *skb_end_pointer(const struct sk_buff *skb)
597 return skb->head + skb->end;
599 #else
600 static inline unsigned char *skb_end_pointer(const struct sk_buff *skb)
602 return skb->end;
604 #endif
606 /* Internal */
607 #define skb_shinfo(SKB) ((struct skb_shared_info *)(skb_end_pointer(SKB)))
609 static inline struct skb_shared_hwtstamps *skb_hwtstamps(struct sk_buff *skb)
611 return &skb_shinfo(skb)->hwtstamps;
615 * skb_queue_empty - check if a queue is empty
616 * @list: queue head
618 * Returns true if the queue is empty, false otherwise.
620 static inline int skb_queue_empty(const struct sk_buff_head *list)
622 return list->next == (struct sk_buff *)list;
626 * skb_queue_is_last - check if skb is the last entry in the queue
627 * @list: queue head
628 * @skb: buffer
630 * Returns true if @skb is the last buffer on the list.
632 static inline bool skb_queue_is_last(const struct sk_buff_head *list,
633 const struct sk_buff *skb)
635 return skb->next == (struct sk_buff *)list;
639 * skb_queue_is_first - check if skb is the first entry in the queue
640 * @list: queue head
641 * @skb: buffer
643 * Returns true if @skb is the first buffer on the list.
645 static inline bool skb_queue_is_first(const struct sk_buff_head *list,
646 const struct sk_buff *skb)
648 return skb->prev == (struct sk_buff *)list;
652 * skb_queue_next - return the next packet in the queue
653 * @list: queue head
654 * @skb: current buffer
656 * Return the next packet in @list after @skb. It is only valid to
657 * call this if skb_queue_is_last() evaluates to false.
659 static inline struct sk_buff *skb_queue_next(const struct sk_buff_head *list,
660 const struct sk_buff *skb)
662 /* This BUG_ON may seem severe, but if we just return then we
663 * are going to dereference garbage.
665 BUG_ON(skb_queue_is_last(list, skb));
666 return skb->next;
670 * skb_queue_prev - return the prev packet in the queue
671 * @list: queue head
672 * @skb: current buffer
674 * Return the prev packet in @list before @skb. It is only valid to
675 * call this if skb_queue_is_first() evaluates to false.
677 static inline struct sk_buff *skb_queue_prev(const struct sk_buff_head *list,
678 const struct sk_buff *skb)
680 /* This BUG_ON may seem severe, but if we just return then we
681 * are going to dereference garbage.
683 BUG_ON(skb_queue_is_first(list, skb));
684 return skb->prev;
688 * skb_get - reference buffer
689 * @skb: buffer to reference
691 * Makes another reference to a socket buffer and returns a pointer
692 * to the buffer.
694 static inline struct sk_buff *skb_get(struct sk_buff *skb)
696 atomic_inc(&skb->users);
697 return skb;
701 * If users == 1, we are the only owner and are can avoid redundant
702 * atomic change.
706 * skb_cloned - is the buffer a clone
707 * @skb: buffer to check
709 * Returns true if the buffer was generated with skb_clone() and is
710 * one of multiple shared copies of the buffer. Cloned buffers are
711 * shared data so must not be written to under normal circumstances.
713 static inline int skb_cloned(const struct sk_buff *skb)
715 return skb->cloned &&
716 (atomic_read(&skb_shinfo(skb)->dataref) & SKB_DATAREF_MASK) != 1;
720 * skb_header_cloned - is the header a clone
721 * @skb: buffer to check
723 * Returns true if modifying the header part of the buffer requires
724 * the data to be copied.
726 static inline int skb_header_cloned(const struct sk_buff *skb)
728 int dataref;
730 if (!skb->cloned)
731 return 0;
733 dataref = atomic_read(&skb_shinfo(skb)->dataref);
734 dataref = (dataref & SKB_DATAREF_MASK) - (dataref >> SKB_DATAREF_SHIFT);
735 return dataref != 1;
739 * skb_header_release - release reference to header
740 * @skb: buffer to operate on
742 * Drop a reference to the header part of the buffer. This is done
743 * by acquiring a payload reference. You must not read from the header
744 * part of skb->data after this.
746 static inline void skb_header_release(struct sk_buff *skb)
748 BUG_ON(skb->nohdr);
749 skb->nohdr = 1;
750 atomic_add(1 << SKB_DATAREF_SHIFT, &skb_shinfo(skb)->dataref);
754 * skb_shared - is the buffer shared
755 * @skb: buffer to check
757 * Returns true if more than one person has a reference to this
758 * buffer.
760 static inline int skb_shared(const struct sk_buff *skb)
762 return atomic_read(&skb->users) != 1;
766 * skb_share_check - check if buffer is shared and if so clone it
767 * @skb: buffer to check
768 * @pri: priority for memory allocation
770 * If the buffer is shared the buffer is cloned and the old copy
771 * drops a reference. A new clone with a single reference is returned.
772 * If the buffer is not shared the original buffer is returned. When
773 * being called from interrupt status or with spinlocks held pri must
774 * be GFP_ATOMIC.
776 * NULL is returned on a memory allocation failure.
778 static inline struct sk_buff *skb_share_check(struct sk_buff *skb,
779 gfp_t pri)
781 might_sleep_if(pri & __GFP_WAIT);
782 if (skb_shared(skb)) {
783 struct sk_buff *nskb = skb_clone(skb, pri);
784 kfree_skb(skb);
785 skb = nskb;
787 return skb;
791 * Copy shared buffers into a new sk_buff. We effectively do COW on
792 * packets to handle cases where we have a local reader and forward
793 * and a couple of other messy ones. The normal one is tcpdumping
794 * a packet thats being forwarded.
798 * skb_unshare - make a copy of a shared buffer
799 * @skb: buffer to check
800 * @pri: priority for memory allocation
802 * If the socket buffer is a clone then this function creates a new
803 * copy of the data, drops a reference count on the old copy and returns
804 * the new copy with the reference count at 1. If the buffer is not a clone
805 * the original buffer is returned. When called with a spinlock held or
806 * from interrupt state @pri must be %GFP_ATOMIC
808 * %NULL is returned on a memory allocation failure.
810 static inline struct sk_buff *skb_unshare(struct sk_buff *skb,
811 gfp_t pri)
813 might_sleep_if(pri & __GFP_WAIT);
814 if (skb_cloned(skb)) {
815 struct sk_buff *nskb = skb_copy(skb, pri);
816 kfree_skb(skb); /* Free our shared copy */
817 skb = nskb;
819 return skb;
823 * skb_peek - peek at the head of an &sk_buff_head
824 * @list_: list to peek at
826 * Peek an &sk_buff. Unlike most other operations you _MUST_
827 * be careful with this one. A peek leaves the buffer on the
828 * list and someone else may run off with it. You must hold
829 * the appropriate locks or have a private queue to do this.
831 * Returns %NULL for an empty list or a pointer to the head element.
832 * The reference count is not incremented and the reference is therefore
833 * volatile. Use with caution.
835 static inline struct sk_buff *skb_peek(struct sk_buff_head *list_)
837 struct sk_buff *list = ((struct sk_buff *)list_)->next;
838 if (list == (struct sk_buff *)list_)
839 list = NULL;
840 return list;
844 * skb_peek_tail - peek at the tail of an &sk_buff_head
845 * @list_: list to peek at
847 * Peek an &sk_buff. Unlike most other operations you _MUST_
848 * be careful with this one. A peek leaves the buffer on the
849 * list and someone else may run off with it. You must hold
850 * the appropriate locks or have a private queue to do this.
852 * Returns %NULL for an empty list or a pointer to the tail element.
853 * The reference count is not incremented and the reference is therefore
854 * volatile. Use with caution.
856 static inline struct sk_buff *skb_peek_tail(struct sk_buff_head *list_)
858 struct sk_buff *list = ((struct sk_buff *)list_)->prev;
859 if (list == (struct sk_buff *)list_)
860 list = NULL;
861 return list;
865 * skb_queue_len - get queue length
866 * @list_: list to measure
868 * Return the length of an &sk_buff queue.
870 static inline __u32 skb_queue_len(const struct sk_buff_head *list_)
872 return list_->qlen;
876 * __skb_queue_head_init - initialize non-spinlock portions of sk_buff_head
877 * @list: queue to initialize
879 * This initializes only the list and queue length aspects of
880 * an sk_buff_head object. This allows to initialize the list
881 * aspects of an sk_buff_head without reinitializing things like
882 * the spinlock. It can also be used for on-stack sk_buff_head
883 * objects where the spinlock is known to not be used.
885 static inline void __skb_queue_head_init(struct sk_buff_head *list)
887 list->prev = list->next = (struct sk_buff *)list;
888 list->qlen = 0;
892 * This function creates a split out lock class for each invocation;
893 * this is needed for now since a whole lot of users of the skb-queue
894 * infrastructure in drivers have different locking usage (in hardirq)
895 * than the networking core (in softirq only). In the long run either the
896 * network layer or drivers should need annotation to consolidate the
897 * main types of usage into 3 classes.
899 static inline void skb_queue_head_init(struct sk_buff_head *list)
901 spin_lock_init(&list->lock);
902 __skb_queue_head_init(list);
905 static inline void skb_queue_head_init_class(struct sk_buff_head *list,
906 struct lock_class_key *class)
908 skb_queue_head_init(list);
909 lockdep_set_class(&list->lock, class);
913 * Insert an sk_buff on a list.
915 * The "__skb_xxxx()" functions are the non-atomic ones that
916 * can only be called with interrupts disabled.
918 extern void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list);
919 static inline void __skb_insert(struct sk_buff *newsk,
920 struct sk_buff *prev, struct sk_buff *next,
921 struct sk_buff_head *list)
923 newsk->next = next;
924 newsk->prev = prev;
925 next->prev = prev->next = newsk;
926 list->qlen++;
929 static inline void __skb_queue_splice(const struct sk_buff_head *list,
930 struct sk_buff *prev,
931 struct sk_buff *next)
933 struct sk_buff *first = list->next;
934 struct sk_buff *last = list->prev;
936 first->prev = prev;
937 prev->next = first;
939 last->next = next;
940 next->prev = last;
944 * skb_queue_splice - join two skb lists, this is designed for stacks
945 * @list: the new list to add
946 * @head: the place to add it in the first list
948 static inline void skb_queue_splice(const struct sk_buff_head *list,
949 struct sk_buff_head *head)
951 if (!skb_queue_empty(list)) {
952 __skb_queue_splice(list, (struct sk_buff *) head, head->next);
953 head->qlen += list->qlen;
958 * skb_queue_splice - join two skb lists and reinitialise the emptied list
959 * @list: the new list to add
960 * @head: the place to add it in the first list
962 * The list at @list is reinitialised
964 static inline void skb_queue_splice_init(struct sk_buff_head *list,
965 struct sk_buff_head *head)
967 if (!skb_queue_empty(list)) {
968 __skb_queue_splice(list, (struct sk_buff *) head, head->next);
969 head->qlen += list->qlen;
970 __skb_queue_head_init(list);
975 * skb_queue_splice_tail - join two skb lists, each list being a queue
976 * @list: the new list to add
977 * @head: the place to add it in the first list
979 static inline void skb_queue_splice_tail(const struct sk_buff_head *list,
980 struct sk_buff_head *head)
982 if (!skb_queue_empty(list)) {
983 __skb_queue_splice(list, head->prev, (struct sk_buff *) head);
984 head->qlen += list->qlen;
989 * skb_queue_splice_tail - join two skb lists and reinitialise the emptied list
990 * @list: the new list to add
991 * @head: the place to add it in the first list
993 * Each of the lists is a queue.
994 * The list at @list is reinitialised
996 static inline void skb_queue_splice_tail_init(struct sk_buff_head *list,
997 struct sk_buff_head *head)
999 if (!skb_queue_empty(list)) {
1000 __skb_queue_splice(list, head->prev, (struct sk_buff *) head);
1001 head->qlen += list->qlen;
1002 __skb_queue_head_init(list);
1007 * __skb_queue_after - queue a buffer at the list head
1008 * @list: list to use
1009 * @prev: place after this buffer
1010 * @newsk: buffer to queue
1012 * Queue a buffer int the middle of a list. This function takes no locks
1013 * and you must therefore hold required locks before calling it.
1015 * A buffer cannot be placed on two lists at the same time.
1017 static inline void __skb_queue_after(struct sk_buff_head *list,
1018 struct sk_buff *prev,
1019 struct sk_buff *newsk)
1021 __skb_insert(newsk, prev, prev->next, list);
1024 extern void skb_append(struct sk_buff *old, struct sk_buff *newsk,
1025 struct sk_buff_head *list);
1027 static inline void __skb_queue_before(struct sk_buff_head *list,
1028 struct sk_buff *next,
1029 struct sk_buff *newsk)
1031 __skb_insert(newsk, next->prev, next, list);
1035 * __skb_queue_head - queue a buffer at the list head
1036 * @list: list to use
1037 * @newsk: buffer to queue
1039 * Queue a buffer at the start of a list. This function takes no locks
1040 * and you must therefore hold required locks before calling it.
1042 * A buffer cannot be placed on two lists at the same time.
1044 extern void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk);
1045 static inline void __skb_queue_head(struct sk_buff_head *list,
1046 struct sk_buff *newsk)
1048 __skb_queue_after(list, (struct sk_buff *)list, newsk);
1052 * __skb_queue_tail - queue a buffer at the list tail
1053 * @list: list to use
1054 * @newsk: buffer to queue
1056 * Queue a buffer at the end of a list. This function takes no locks
1057 * and you must therefore hold required locks before calling it.
1059 * A buffer cannot be placed on two lists at the same time.
1061 extern void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk);
1062 static inline void __skb_queue_tail(struct sk_buff_head *list,
1063 struct sk_buff *newsk)
1065 __skb_queue_before(list, (struct sk_buff *)list, newsk);
1069 * remove sk_buff from list. _Must_ be called atomically, and with
1070 * the list known..
1072 extern void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list);
1073 static inline void __skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
1075 struct sk_buff *next, *prev;
1077 list->qlen--;
1078 next = skb->next;
1079 prev = skb->prev;
1080 skb->next = skb->prev = NULL;
1081 next->prev = prev;
1082 prev->next = next;
1086 * __skb_dequeue - remove from the head of the queue
1087 * @list: list to dequeue from
1089 * Remove the head of the list. This function does not take any locks
1090 * so must be used with appropriate locks held only. The head item is
1091 * returned or %NULL if the list is empty.
1093 extern struct sk_buff *skb_dequeue(struct sk_buff_head *list);
1094 static inline struct sk_buff *__skb_dequeue(struct sk_buff_head *list)
1096 struct sk_buff *skb = skb_peek(list);
1097 if (skb)
1098 __skb_unlink(skb, list);
1099 return skb;
1103 * __skb_dequeue_tail - remove from the tail of the queue
1104 * @list: list to dequeue from
1106 * Remove the tail of the list. This function does not take any locks
1107 * so must be used with appropriate locks held only. The tail item is
1108 * returned or %NULL if the list is empty.
1110 extern struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list);
1111 static inline struct sk_buff *__skb_dequeue_tail(struct sk_buff_head *list)
1113 struct sk_buff *skb = skb_peek_tail(list);
1114 if (skb)
1115 __skb_unlink(skb, list);
1116 return skb;
1120 static inline int skb_is_nonlinear(const struct sk_buff *skb)
1122 return skb->data_len;
1125 static inline unsigned int skb_headlen(const struct sk_buff *skb)
1127 return skb->len - skb->data_len;
1130 static inline int skb_pagelen(const struct sk_buff *skb)
1132 int i, len = 0;
1134 for (i = (int)skb_shinfo(skb)->nr_frags - 1; i >= 0; i--)
1135 len += skb_shinfo(skb)->frags[i].size;
1136 return len + skb_headlen(skb);
1140 * __skb_fill_page_desc - initialise a paged fragment in an skb
1141 * @skb: buffer containing fragment to be initialised
1142 * @i: paged fragment index to initialise
1143 * @page: the page to use for this fragment
1144 * @off: the offset to the data with @page
1145 * @size: the length of the data
1147 * Initialises the @i'th fragment of @skb to point to &size bytes at
1148 * offset @off within @page.
1150 * Does not take any additional reference on the fragment.
1152 static inline void __skb_fill_page_desc(struct sk_buff *skb, int i,
1153 struct page *page, int off, int size)
1155 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1157 frag->page = page;
1158 frag->page_offset = off;
1159 frag->size = size;
1163 * skb_fill_page_desc - initialise a paged fragment in an skb
1164 * @skb: buffer containing fragment to be initialised
1165 * @i: paged fragment index to initialise
1166 * @page: the page to use for this fragment
1167 * @off: the offset to the data with @page
1168 * @size: the length of the data
1170 * As per __skb_fill_page_desc() -- initialises the @i'th fragment of
1171 * @skb to point to &size bytes at offset @off within @page. In
1172 * addition updates @skb such that @i is the last fragment.
1174 * Does not take any additional reference on the fragment.
1176 static inline void skb_fill_page_desc(struct sk_buff *skb, int i,
1177 struct page *page, int off, int size)
1179 __skb_fill_page_desc(skb, i, page, off, size);
1180 skb_shinfo(skb)->nr_frags = i + 1;
1183 extern void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page,
1184 int off, int size);
1186 #define SKB_PAGE_ASSERT(skb) BUG_ON(skb_shinfo(skb)->nr_frags)
1187 #define SKB_FRAG_ASSERT(skb) BUG_ON(skb_has_frag_list(skb))
1188 #define SKB_LINEAR_ASSERT(skb) BUG_ON(skb_is_nonlinear(skb))
1190 #ifdef NET_SKBUFF_DATA_USES_OFFSET
1191 static inline unsigned char *skb_tail_pointer(const struct sk_buff *skb)
1193 return skb->head + skb->tail;
1196 static inline void skb_reset_tail_pointer(struct sk_buff *skb)
1198 skb->tail = skb->data - skb->head;
1201 static inline void skb_set_tail_pointer(struct sk_buff *skb, const int offset)
1203 skb_reset_tail_pointer(skb);
1204 skb->tail += offset;
1206 #else /* NET_SKBUFF_DATA_USES_OFFSET */
1207 static inline unsigned char *skb_tail_pointer(const struct sk_buff *skb)
1209 return skb->tail;
1212 static inline void skb_reset_tail_pointer(struct sk_buff *skb)
1214 skb->tail = skb->data;
1217 static inline void skb_set_tail_pointer(struct sk_buff *skb, const int offset)
1219 skb->tail = skb->data + offset;
1222 #endif /* NET_SKBUFF_DATA_USES_OFFSET */
1225 * Add data to an sk_buff
1227 extern unsigned char *skb_put(struct sk_buff *skb, unsigned int len);
1228 static inline unsigned char *__skb_put(struct sk_buff *skb, unsigned int len)
1230 unsigned char *tmp = skb_tail_pointer(skb);
1231 SKB_LINEAR_ASSERT(skb);
1232 skb->tail += len;
1233 skb->len += len;
1234 return tmp;
1237 extern unsigned char *skb_push(struct sk_buff *skb, unsigned int len);
1238 static inline unsigned char *__skb_push(struct sk_buff *skb, unsigned int len)
1240 skb->data -= len;
1241 skb->len += len;
1242 return skb->data;
1245 extern unsigned char *skb_pull(struct sk_buff *skb, unsigned int len);
1246 static inline unsigned char *__skb_pull(struct sk_buff *skb, unsigned int len)
1248 skb->len -= len;
1249 BUG_ON(skb->len < skb->data_len);
1250 return skb->data += len;
1253 static inline unsigned char *skb_pull_inline(struct sk_buff *skb, unsigned int len)
1255 return unlikely(len > skb->len) ? NULL : __skb_pull(skb, len);
1258 extern unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta);
1260 static inline unsigned char *__pskb_pull(struct sk_buff *skb, unsigned int len)
1262 if (len > skb_headlen(skb) &&
1263 !__pskb_pull_tail(skb, len - skb_headlen(skb)))
1264 return NULL;
1265 skb->len -= len;
1266 return skb->data += len;
1269 static inline unsigned char *pskb_pull(struct sk_buff *skb, unsigned int len)
1271 return unlikely(len > skb->len) ? NULL : __pskb_pull(skb, len);
1274 static inline int pskb_may_pull(struct sk_buff *skb, unsigned int len)
1276 if (likely(len <= skb_headlen(skb)))
1277 return 1;
1278 if (unlikely(len > skb->len))
1279 return 0;
1280 return __pskb_pull_tail(skb, len - skb_headlen(skb)) != NULL;
1284 * skb_headroom - bytes at buffer head
1285 * @skb: buffer to check
1287 * Return the number of bytes of free space at the head of an &sk_buff.
1289 static inline unsigned int skb_headroom(const struct sk_buff *skb)
1291 return skb->data - skb->head;
1295 * skb_tailroom - bytes at buffer end
1296 * @skb: buffer to check
1298 * Return the number of bytes of free space at the tail of an sk_buff
1300 static inline int skb_tailroom(const struct sk_buff *skb)
1302 return skb_is_nonlinear(skb) ? 0 : skb->end - skb->tail;
1306 * skb_reserve - adjust headroom
1307 * @skb: buffer to alter
1308 * @len: bytes to move
1310 * Increase the headroom of an empty &sk_buff by reducing the tail
1311 * room. This is only allowed for an empty buffer.
1313 static inline void skb_reserve(struct sk_buff *skb, int len)
1315 skb->data += len;
1316 skb->tail += len;
1319 static inline void skb_reset_mac_len(struct sk_buff *skb)
1321 skb->mac_len = skb->network_header - skb->mac_header;
1324 #ifdef NET_SKBUFF_DATA_USES_OFFSET
1325 static inline unsigned char *skb_transport_header(const struct sk_buff *skb)
1327 return skb->head + skb->transport_header;
1330 static inline void skb_reset_transport_header(struct sk_buff *skb)
1332 skb->transport_header = skb->data - skb->head;
1335 static inline void skb_set_transport_header(struct sk_buff *skb,
1336 const int offset)
1338 skb_reset_transport_header(skb);
1339 skb->transport_header += offset;
1342 static inline unsigned char *skb_network_header(const struct sk_buff *skb)
1344 return skb->head + skb->network_header;
1347 static inline void skb_reset_network_header(struct sk_buff *skb)
1349 skb->network_header = skb->data - skb->head;
1352 static inline void skb_set_network_header(struct sk_buff *skb, const int offset)
1354 skb_reset_network_header(skb);
1355 skb->network_header += offset;
1358 static inline unsigned char *skb_mac_header(const struct sk_buff *skb)
1360 return skb->head + skb->mac_header;
1363 static inline int skb_mac_header_was_set(const struct sk_buff *skb)
1365 return skb->mac_header != ~0U;
1368 static inline void skb_reset_mac_header(struct sk_buff *skb)
1370 skb->mac_header = skb->data - skb->head;
1373 static inline void skb_set_mac_header(struct sk_buff *skb, const int offset)
1375 skb_reset_mac_header(skb);
1376 skb->mac_header += offset;
1379 #else /* NET_SKBUFF_DATA_USES_OFFSET */
1381 static inline unsigned char *skb_transport_header(const struct sk_buff *skb)
1383 return skb->transport_header;
1386 static inline void skb_reset_transport_header(struct sk_buff *skb)
1388 skb->transport_header = skb->data;
1391 static inline void skb_set_transport_header(struct sk_buff *skb,
1392 const int offset)
1394 skb->transport_header = skb->data + offset;
1397 static inline unsigned char *skb_network_header(const struct sk_buff *skb)
1399 return skb->network_header;
1402 static inline void skb_reset_network_header(struct sk_buff *skb)
1404 skb->network_header = skb->data;
1407 static inline void skb_set_network_header(struct sk_buff *skb, const int offset)
1409 skb->network_header = skb->data + offset;
1412 static inline unsigned char *skb_mac_header(const struct sk_buff *skb)
1414 return skb->mac_header;
1417 static inline int skb_mac_header_was_set(const struct sk_buff *skb)
1419 return skb->mac_header != NULL;
1422 static inline void skb_reset_mac_header(struct sk_buff *skb)
1424 skb->mac_header = skb->data;
1427 static inline void skb_set_mac_header(struct sk_buff *skb, const int offset)
1429 skb->mac_header = skb->data + offset;
1431 #endif /* NET_SKBUFF_DATA_USES_OFFSET */
1433 static inline int skb_checksum_start_offset(const struct sk_buff *skb)
1435 return skb->csum_start - skb_headroom(skb);
1438 static inline int skb_transport_offset(const struct sk_buff *skb)
1440 return skb_transport_header(skb) - skb->data;
1443 static inline u32 skb_network_header_len(const struct sk_buff *skb)
1445 return skb->transport_header - skb->network_header;
1448 static inline int skb_network_offset(const struct sk_buff *skb)
1450 return skb_network_header(skb) - skb->data;
1453 static inline int pskb_network_may_pull(struct sk_buff *skb, unsigned int len)
1455 return pskb_may_pull(skb, skb_network_offset(skb) + len);
1459 * CPUs often take a performance hit when accessing unaligned memory
1460 * locations. The actual performance hit varies, it can be small if the
1461 * hardware handles it or large if we have to take an exception and fix it
1462 * in software.
1464 * Since an ethernet header is 14 bytes network drivers often end up with
1465 * the IP header at an unaligned offset. The IP header can be aligned by
1466 * shifting the start of the packet by 2 bytes. Drivers should do this
1467 * with:
1469 * skb_reserve(skb, NET_IP_ALIGN);
1471 * The downside to this alignment of the IP header is that the DMA is now
1472 * unaligned. On some architectures the cost of an unaligned DMA is high
1473 * and this cost outweighs the gains made by aligning the IP header.
1475 * Since this trade off varies between architectures, we allow NET_IP_ALIGN
1476 * to be overridden.
1478 #ifndef NET_IP_ALIGN
1479 #define NET_IP_ALIGN 2
1480 #endif
1483 * The networking layer reserves some headroom in skb data (via
1484 * dev_alloc_skb). This is used to avoid having to reallocate skb data when
1485 * the header has to grow. In the default case, if the header has to grow
1486 * 32 bytes or less we avoid the reallocation.
1488 * Unfortunately this headroom changes the DMA alignment of the resulting
1489 * network packet. As for NET_IP_ALIGN, this unaligned DMA is expensive
1490 * on some architectures. An architecture can override this value,
1491 * perhaps setting it to a cacheline in size (since that will maintain
1492 * cacheline alignment of the DMA). It must be a power of 2.
1494 * Various parts of the networking layer expect at least 32 bytes of
1495 * headroom, you should not reduce this.
1497 * Using max(32, L1_CACHE_BYTES) makes sense (especially with RPS)
1498 * to reduce average number of cache lines per packet.
1499 * get_rps_cpus() for example only access one 64 bytes aligned block :
1500 * NET_IP_ALIGN(2) + ethernet_header(14) + IP_header(20/40) + ports(8)
1502 #ifndef NET_SKB_PAD
1503 #define NET_SKB_PAD max(32, L1_CACHE_BYTES)
1504 #endif
1506 extern int ___pskb_trim(struct sk_buff *skb, unsigned int len);
1508 static inline void __skb_trim(struct sk_buff *skb, unsigned int len)
1510 if (unlikely(skb_is_nonlinear(skb))) {
1511 WARN_ON(1);
1512 return;
1514 skb->len = len;
1515 skb_set_tail_pointer(skb, len);
1518 extern void skb_trim(struct sk_buff *skb, unsigned int len);
1520 static inline int __pskb_trim(struct sk_buff *skb, unsigned int len)
1522 if (skb->data_len)
1523 return ___pskb_trim(skb, len);
1524 __skb_trim(skb, len);
1525 return 0;
1528 static inline int pskb_trim(struct sk_buff *skb, unsigned int len)
1530 return (len < skb->len) ? __pskb_trim(skb, len) : 0;
1534 * pskb_trim_unique - remove end from a paged unique (not cloned) buffer
1535 * @skb: buffer to alter
1536 * @len: new length
1538 * This is identical to pskb_trim except that the caller knows that
1539 * the skb is not cloned so we should never get an error due to out-
1540 * of-memory.
1542 static inline void pskb_trim_unique(struct sk_buff *skb, unsigned int len)
1544 int err = pskb_trim(skb, len);
1545 BUG_ON(err);
1549 * skb_orphan - orphan a buffer
1550 * @skb: buffer to orphan
1552 * If a buffer currently has an owner then we call the owner's
1553 * destructor function and make the @skb unowned. The buffer continues
1554 * to exist but is no longer charged to its former owner.
1556 static inline void skb_orphan(struct sk_buff *skb)
1558 if (skb->destructor)
1559 skb->destructor(skb);
1560 skb->destructor = NULL;
1561 skb->sk = NULL;
1565 * __skb_queue_purge - empty a list
1566 * @list: list to empty
1568 * Delete all buffers on an &sk_buff list. Each buffer is removed from
1569 * the list and one reference dropped. This function does not take the
1570 * list lock and the caller must hold the relevant locks to use it.
1572 extern void skb_queue_purge(struct sk_buff_head *list);
1573 static inline void __skb_queue_purge(struct sk_buff_head *list)
1575 struct sk_buff *skb;
1576 while ((skb = __skb_dequeue(list)) != NULL)
1577 kfree_skb(skb);
1581 * __dev_alloc_skb - allocate an skbuff for receiving
1582 * @length: length to allocate
1583 * @gfp_mask: get_free_pages mask, passed to alloc_skb
1585 * Allocate a new &sk_buff and assign it a usage count of one. The
1586 * buffer has unspecified headroom built in. Users should allocate
1587 * the headroom they think they need without accounting for the
1588 * built in space. The built in space is used for optimisations.
1590 * %NULL is returned if there is no free memory.
1592 static inline struct sk_buff *__dev_alloc_skb(unsigned int length,
1593 gfp_t gfp_mask)
1595 struct sk_buff *skb = alloc_skb(length + NET_SKB_PAD, gfp_mask);
1596 if (likely(skb))
1597 skb_reserve(skb, NET_SKB_PAD);
1598 return skb;
1601 extern struct sk_buff *dev_alloc_skb(unsigned int length);
1603 extern struct sk_buff *__netdev_alloc_skb(struct net_device *dev,
1604 unsigned int length, gfp_t gfp_mask);
1607 * netdev_alloc_skb - allocate an skbuff for rx on a specific device
1608 * @dev: network device to receive on
1609 * @length: length to allocate
1611 * Allocate a new &sk_buff and assign it a usage count of one. The
1612 * buffer has unspecified headroom built in. Users should allocate
1613 * the headroom they think they need without accounting for the
1614 * built in space. The built in space is used for optimisations.
1616 * %NULL is returned if there is no free memory. Although this function
1617 * allocates memory it can be called from an interrupt.
1619 static inline struct sk_buff *netdev_alloc_skb(struct net_device *dev,
1620 unsigned int length)
1622 return __netdev_alloc_skb(dev, length, GFP_ATOMIC);
1625 static inline struct sk_buff *__netdev_alloc_skb_ip_align(struct net_device *dev,
1626 unsigned int length, gfp_t gfp)
1628 struct sk_buff *skb = __netdev_alloc_skb(dev, length + NET_IP_ALIGN, gfp);
1630 if (NET_IP_ALIGN && skb)
1631 skb_reserve(skb, NET_IP_ALIGN);
1632 return skb;
1635 static inline struct sk_buff *netdev_alloc_skb_ip_align(struct net_device *dev,
1636 unsigned int length)
1638 return __netdev_alloc_skb_ip_align(dev, length, GFP_ATOMIC);
1642 * __netdev_alloc_page - allocate a page for ps-rx on a specific device
1643 * @dev: network device to receive on
1644 * @gfp_mask: alloc_pages_node mask
1646 * Allocate a new page. dev currently unused.
1648 * %NULL is returned if there is no free memory.
1650 static inline struct page *__netdev_alloc_page(struct net_device *dev, gfp_t gfp_mask)
1652 return alloc_pages_node(NUMA_NO_NODE, gfp_mask, 0);
1656 * netdev_alloc_page - allocate a page for ps-rx on a specific device
1657 * @dev: network device to receive on
1659 * Allocate a new page. dev currently unused.
1661 * %NULL is returned if there is no free memory.
1663 static inline struct page *netdev_alloc_page(struct net_device *dev)
1665 return __netdev_alloc_page(dev, GFP_ATOMIC);
1668 static inline void netdev_free_page(struct net_device *dev, struct page *page)
1670 __free_page(page);
1674 * skb_frag_page - retrieve the page refered to by a paged fragment
1675 * @frag: the paged fragment
1677 * Returns the &struct page associated with @frag.
1679 static inline struct page *skb_frag_page(const skb_frag_t *frag)
1681 return frag->page;
1685 * __skb_frag_ref - take an addition reference on a paged fragment.
1686 * @frag: the paged fragment
1688 * Takes an additional reference on the paged fragment @frag.
1690 static inline void __skb_frag_ref(skb_frag_t *frag)
1692 get_page(skb_frag_page(frag));
1696 * skb_frag_ref - take an addition reference on a paged fragment of an skb.
1697 * @skb: the buffer
1698 * @f: the fragment offset.
1700 * Takes an additional reference on the @f'th paged fragment of @skb.
1702 static inline void skb_frag_ref(struct sk_buff *skb, int f)
1704 __skb_frag_ref(&skb_shinfo(skb)->frags[f]);
1708 * __skb_frag_unref - release a reference on a paged fragment.
1709 * @frag: the paged fragment
1711 * Releases a reference on the paged fragment @frag.
1713 static inline void __skb_frag_unref(skb_frag_t *frag)
1715 put_page(skb_frag_page(frag));
1719 * skb_frag_unref - release a reference on a paged fragment of an skb.
1720 * @skb: the buffer
1721 * @f: the fragment offset
1723 * Releases a reference on the @f'th paged fragment of @skb.
1725 static inline void skb_frag_unref(struct sk_buff *skb, int f)
1727 __skb_frag_unref(&skb_shinfo(skb)->frags[f]);
1731 * skb_frag_address - gets the address of the data contained in a paged fragment
1732 * @frag: the paged fragment buffer
1734 * Returns the address of the data within @frag. The page must already
1735 * be mapped.
1737 static inline void *skb_frag_address(const skb_frag_t *frag)
1739 return page_address(skb_frag_page(frag)) + frag->page_offset;
1743 * skb_frag_address_safe - gets the address of the data contained in a paged fragment
1744 * @frag: the paged fragment buffer
1746 * Returns the address of the data within @frag. Checks that the page
1747 * is mapped and returns %NULL otherwise.
1749 static inline void *skb_frag_address_safe(const skb_frag_t *frag)
1751 void *ptr = page_address(skb_frag_page(frag));
1752 if (unlikely(!ptr))
1753 return NULL;
1755 return ptr + frag->page_offset;
1759 * __skb_frag_set_page - sets the page contained in a paged fragment
1760 * @frag: the paged fragment
1761 * @page: the page to set
1763 * Sets the fragment @frag to contain @page.
1765 static inline void __skb_frag_set_page(skb_frag_t *frag, struct page *page)
1767 frag->page = page;
1768 __skb_frag_ref(frag);
1772 * skb_frag_set_page - sets the page contained in a paged fragment of an skb
1773 * @skb: the buffer
1774 * @f: the fragment offset
1775 * @page: the page to set
1777 * Sets the @f'th fragment of @skb to contain @page.
1779 static inline void skb_frag_set_page(struct sk_buff *skb, int f,
1780 struct page *page)
1782 __skb_frag_set_page(&skb_shinfo(skb)->frags[f], page);
1786 * skb_frag_dma_map - maps a paged fragment via the DMA API
1787 * @device: the device to map the fragment to
1788 * @frag: the paged fragment to map
1789 * @offset: the offset within the fragment (starting at the
1790 * fragment's own offset)
1791 * @size: the number of bytes to map
1792 * @direction: the direction of the mapping (%PCI_DMA_*)
1794 * Maps the page associated with @frag to @device.
1796 static inline dma_addr_t skb_frag_dma_map(struct device *dev,
1797 const skb_frag_t *frag,
1798 size_t offset, size_t size,
1799 enum dma_data_direction dir)
1801 return dma_map_page(dev, skb_frag_page(frag),
1802 frag->page_offset + offset, size, dir);
1806 * skb_clone_writable - is the header of a clone writable
1807 * @skb: buffer to check
1808 * @len: length up to which to write
1810 * Returns true if modifying the header part of the cloned buffer
1811 * does not requires the data to be copied.
1813 static inline int skb_clone_writable(struct sk_buff *skb, unsigned int len)
1815 return !skb_header_cloned(skb) &&
1816 skb_headroom(skb) + len <= skb->hdr_len;
1819 static inline int __skb_cow(struct sk_buff *skb, unsigned int headroom,
1820 int cloned)
1822 int delta = 0;
1824 if (headroom < NET_SKB_PAD)
1825 headroom = NET_SKB_PAD;
1826 if (headroom > skb_headroom(skb))
1827 delta = headroom - skb_headroom(skb);
1829 if (delta || cloned)
1830 return pskb_expand_head(skb, ALIGN(delta, NET_SKB_PAD), 0,
1831 GFP_ATOMIC);
1832 return 0;
1836 * skb_cow - copy header of skb when it is required
1837 * @skb: buffer to cow
1838 * @headroom: needed headroom
1840 * If the skb passed lacks sufficient headroom or its data part
1841 * is shared, data is reallocated. If reallocation fails, an error
1842 * is returned and original skb is not changed.
1844 * The result is skb with writable area skb->head...skb->tail
1845 * and at least @headroom of space at head.
1847 static inline int skb_cow(struct sk_buff *skb, unsigned int headroom)
1849 return __skb_cow(skb, headroom, skb_cloned(skb));
1853 * skb_cow_head - skb_cow but only making the head writable
1854 * @skb: buffer to cow
1855 * @headroom: needed headroom
1857 * This function is identical to skb_cow except that we replace the
1858 * skb_cloned check by skb_header_cloned. It should be used when
1859 * you only need to push on some header and do not need to modify
1860 * the data.
1862 static inline int skb_cow_head(struct sk_buff *skb, unsigned int headroom)
1864 return __skb_cow(skb, headroom, skb_header_cloned(skb));
1868 * skb_padto - pad an skbuff up to a minimal size
1869 * @skb: buffer to pad
1870 * @len: minimal length
1872 * Pads up a buffer to ensure the trailing bytes exist and are
1873 * blanked. If the buffer already contains sufficient data it
1874 * is untouched. Otherwise it is extended. Returns zero on
1875 * success. The skb is freed on error.
1878 static inline int skb_padto(struct sk_buff *skb, unsigned int len)
1880 unsigned int size = skb->len;
1881 if (likely(size >= len))
1882 return 0;
1883 return skb_pad(skb, len - size);
1886 static inline int skb_add_data(struct sk_buff *skb,
1887 char __user *from, int copy)
1889 const int off = skb->len;
1891 if (skb->ip_summed == CHECKSUM_NONE) {
1892 int err = 0;
1893 __wsum csum = csum_and_copy_from_user(from, skb_put(skb, copy),
1894 copy, 0, &err);
1895 if (!err) {
1896 skb->csum = csum_block_add(skb->csum, csum, off);
1897 return 0;
1899 } else if (!copy_from_user(skb_put(skb, copy), from, copy))
1900 return 0;
1902 __skb_trim(skb, off);
1903 return -EFAULT;
1906 static inline int skb_can_coalesce(struct sk_buff *skb, int i,
1907 const struct page *page, int off)
1909 if (i) {
1910 struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[i - 1];
1912 return page == skb_frag_page(frag) &&
1913 off == frag->page_offset + frag->size;
1915 return 0;
1918 static inline int __skb_linearize(struct sk_buff *skb)
1920 return __pskb_pull_tail(skb, skb->data_len) ? 0 : -ENOMEM;
1924 * skb_linearize - convert paged skb to linear one
1925 * @skb: buffer to linarize
1927 * If there is no free memory -ENOMEM is returned, otherwise zero
1928 * is returned and the old skb data released.
1930 static inline int skb_linearize(struct sk_buff *skb)
1932 return skb_is_nonlinear(skb) ? __skb_linearize(skb) : 0;
1936 * skb_linearize_cow - make sure skb is linear and writable
1937 * @skb: buffer to process
1939 * If there is no free memory -ENOMEM is returned, otherwise zero
1940 * is returned and the old skb data released.
1942 static inline int skb_linearize_cow(struct sk_buff *skb)
1944 return skb_is_nonlinear(skb) || skb_cloned(skb) ?
1945 __skb_linearize(skb) : 0;
1949 * skb_postpull_rcsum - update checksum for received skb after pull
1950 * @skb: buffer to update
1951 * @start: start of data before pull
1952 * @len: length of data pulled
1954 * After doing a pull on a received packet, you need to call this to
1955 * update the CHECKSUM_COMPLETE checksum, or set ip_summed to
1956 * CHECKSUM_NONE so that it can be recomputed from scratch.
1959 static inline void skb_postpull_rcsum(struct sk_buff *skb,
1960 const void *start, unsigned int len)
1962 if (skb->ip_summed == CHECKSUM_COMPLETE)
1963 skb->csum = csum_sub(skb->csum, csum_partial(start, len, 0));
1966 unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len);
1969 * pskb_trim_rcsum - trim received skb and update checksum
1970 * @skb: buffer to trim
1971 * @len: new length
1973 * This is exactly the same as pskb_trim except that it ensures the
1974 * checksum of received packets are still valid after the operation.
1977 static inline int pskb_trim_rcsum(struct sk_buff *skb, unsigned int len)
1979 if (likely(len >= skb->len))
1980 return 0;
1981 if (skb->ip_summed == CHECKSUM_COMPLETE)
1982 skb->ip_summed = CHECKSUM_NONE;
1983 return __pskb_trim(skb, len);
1986 #define skb_queue_walk(queue, skb) \
1987 for (skb = (queue)->next; \
1988 skb != (struct sk_buff *)(queue); \
1989 skb = skb->next)
1991 #define skb_queue_walk_safe(queue, skb, tmp) \
1992 for (skb = (queue)->next, tmp = skb->next; \
1993 skb != (struct sk_buff *)(queue); \
1994 skb = tmp, tmp = skb->next)
1996 #define skb_queue_walk_from(queue, skb) \
1997 for (; skb != (struct sk_buff *)(queue); \
1998 skb = skb->next)
2000 #define skb_queue_walk_from_safe(queue, skb, tmp) \
2001 for (tmp = skb->next; \
2002 skb != (struct sk_buff *)(queue); \
2003 skb = tmp, tmp = skb->next)
2005 #define skb_queue_reverse_walk(queue, skb) \
2006 for (skb = (queue)->prev; \
2007 skb != (struct sk_buff *)(queue); \
2008 skb = skb->prev)
2010 #define skb_queue_reverse_walk_safe(queue, skb, tmp) \
2011 for (skb = (queue)->prev, tmp = skb->prev; \
2012 skb != (struct sk_buff *)(queue); \
2013 skb = tmp, tmp = skb->prev)
2015 #define skb_queue_reverse_walk_from_safe(queue, skb, tmp) \
2016 for (tmp = skb->prev; \
2017 skb != (struct sk_buff *)(queue); \
2018 skb = tmp, tmp = skb->prev)
2020 static inline bool skb_has_frag_list(const struct sk_buff *skb)
2022 return skb_shinfo(skb)->frag_list != NULL;
2025 static inline void skb_frag_list_init(struct sk_buff *skb)
2027 skb_shinfo(skb)->frag_list = NULL;
2030 static inline void skb_frag_add_head(struct sk_buff *skb, struct sk_buff *frag)
2032 frag->next = skb_shinfo(skb)->frag_list;
2033 skb_shinfo(skb)->frag_list = frag;
2036 #define skb_walk_frags(skb, iter) \
2037 for (iter = skb_shinfo(skb)->frag_list; iter; iter = iter->next)
2039 extern struct sk_buff *__skb_recv_datagram(struct sock *sk, unsigned flags,
2040 int *peeked, int *err);
2041 extern struct sk_buff *skb_recv_datagram(struct sock *sk, unsigned flags,
2042 int noblock, int *err);
2043 extern unsigned int datagram_poll(struct file *file, struct socket *sock,
2044 struct poll_table_struct *wait);
2045 extern int skb_copy_datagram_iovec(const struct sk_buff *from,
2046 int offset, struct iovec *to,
2047 int size);
2048 extern int skb_copy_and_csum_datagram_iovec(struct sk_buff *skb,
2049 int hlen,
2050 struct iovec *iov);
2051 extern int skb_copy_datagram_from_iovec(struct sk_buff *skb,
2052 int offset,
2053 const struct iovec *from,
2054 int from_offset,
2055 int len);
2056 extern int skb_copy_datagram_const_iovec(const struct sk_buff *from,
2057 int offset,
2058 const struct iovec *to,
2059 int to_offset,
2060 int size);
2061 extern void skb_free_datagram(struct sock *sk, struct sk_buff *skb);
2062 extern void skb_free_datagram_locked(struct sock *sk,
2063 struct sk_buff *skb);
2064 extern int skb_kill_datagram(struct sock *sk, struct sk_buff *skb,
2065 unsigned int flags);
2066 extern __wsum skb_checksum(const struct sk_buff *skb, int offset,
2067 int len, __wsum csum);
2068 extern int skb_copy_bits(const struct sk_buff *skb, int offset,
2069 void *to, int len);
2070 extern int skb_store_bits(struct sk_buff *skb, int offset,
2071 const void *from, int len);
2072 extern __wsum skb_copy_and_csum_bits(const struct sk_buff *skb,
2073 int offset, u8 *to, int len,
2074 __wsum csum);
2075 extern int skb_splice_bits(struct sk_buff *skb,
2076 unsigned int offset,
2077 struct pipe_inode_info *pipe,
2078 unsigned int len,
2079 unsigned int flags);
2080 extern void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to);
2081 extern void skb_split(struct sk_buff *skb,
2082 struct sk_buff *skb1, const u32 len);
2083 extern int skb_shift(struct sk_buff *tgt, struct sk_buff *skb,
2084 int shiftlen);
2086 extern struct sk_buff *skb_segment(struct sk_buff *skb, u32 features);
2088 static inline void *skb_header_pointer(const struct sk_buff *skb, int offset,
2089 int len, void *buffer)
2091 int hlen = skb_headlen(skb);
2093 if (hlen - offset >= len)
2094 return skb->data + offset;
2096 if (skb_copy_bits(skb, offset, buffer, len) < 0)
2097 return NULL;
2099 return buffer;
2102 static inline void skb_copy_from_linear_data(const struct sk_buff *skb,
2103 void *to,
2104 const unsigned int len)
2106 memcpy(to, skb->data, len);
2109 static inline void skb_copy_from_linear_data_offset(const struct sk_buff *skb,
2110 const int offset, void *to,
2111 const unsigned int len)
2113 memcpy(to, skb->data + offset, len);
2116 static inline void skb_copy_to_linear_data(struct sk_buff *skb,
2117 const void *from,
2118 const unsigned int len)
2120 memcpy(skb->data, from, len);
2123 static inline void skb_copy_to_linear_data_offset(struct sk_buff *skb,
2124 const int offset,
2125 const void *from,
2126 const unsigned int len)
2128 memcpy(skb->data + offset, from, len);
2131 extern void skb_init(void);
2133 static inline ktime_t skb_get_ktime(const struct sk_buff *skb)
2135 return skb->tstamp;
2139 * skb_get_timestamp - get timestamp from a skb
2140 * @skb: skb to get stamp from
2141 * @stamp: pointer to struct timeval to store stamp in
2143 * Timestamps are stored in the skb as offsets to a base timestamp.
2144 * This function converts the offset back to a struct timeval and stores
2145 * it in stamp.
2147 static inline void skb_get_timestamp(const struct sk_buff *skb,
2148 struct timeval *stamp)
2150 *stamp = ktime_to_timeval(skb->tstamp);
2153 static inline void skb_get_timestampns(const struct sk_buff *skb,
2154 struct timespec *stamp)
2156 *stamp = ktime_to_timespec(skb->tstamp);
2159 static inline void __net_timestamp(struct sk_buff *skb)
2161 skb->tstamp = ktime_get_real();
2164 static inline ktime_t net_timedelta(ktime_t t)
2166 return ktime_sub(ktime_get_real(), t);
2169 static inline ktime_t net_invalid_timestamp(void)
2171 return ktime_set(0, 0);
2174 extern void skb_timestamping_init(void);
2176 #ifdef CONFIG_NETWORK_PHY_TIMESTAMPING
2178 extern void skb_clone_tx_timestamp(struct sk_buff *skb);
2179 extern bool skb_defer_rx_timestamp(struct sk_buff *skb);
2181 #else /* CONFIG_NETWORK_PHY_TIMESTAMPING */
2183 static inline void skb_clone_tx_timestamp(struct sk_buff *skb)
2187 static inline bool skb_defer_rx_timestamp(struct sk_buff *skb)
2189 return false;
2192 #endif /* !CONFIG_NETWORK_PHY_TIMESTAMPING */
2195 * skb_complete_tx_timestamp() - deliver cloned skb with tx timestamps
2197 * @skb: clone of the the original outgoing packet
2198 * @hwtstamps: hardware time stamps
2201 void skb_complete_tx_timestamp(struct sk_buff *skb,
2202 struct skb_shared_hwtstamps *hwtstamps);
2205 * skb_tstamp_tx - queue clone of skb with send time stamps
2206 * @orig_skb: the original outgoing packet
2207 * @hwtstamps: hardware time stamps, may be NULL if not available
2209 * If the skb has a socket associated, then this function clones the
2210 * skb (thus sharing the actual data and optional structures), stores
2211 * the optional hardware time stamping information (if non NULL) or
2212 * generates a software time stamp (otherwise), then queues the clone
2213 * to the error queue of the socket. Errors are silently ignored.
2215 extern void skb_tstamp_tx(struct sk_buff *orig_skb,
2216 struct skb_shared_hwtstamps *hwtstamps);
2218 static inline void sw_tx_timestamp(struct sk_buff *skb)
2220 if (skb_shinfo(skb)->tx_flags & SKBTX_SW_TSTAMP &&
2221 !(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS))
2222 skb_tstamp_tx(skb, NULL);
2226 * skb_tx_timestamp() - Driver hook for transmit timestamping
2228 * Ethernet MAC Drivers should call this function in their hard_xmit()
2229 * function immediately before giving the sk_buff to the MAC hardware.
2231 * @skb: A socket buffer.
2233 static inline void skb_tx_timestamp(struct sk_buff *skb)
2235 skb_clone_tx_timestamp(skb);
2236 sw_tx_timestamp(skb);
2239 extern __sum16 __skb_checksum_complete_head(struct sk_buff *skb, int len);
2240 extern __sum16 __skb_checksum_complete(struct sk_buff *skb);
2242 static inline int skb_csum_unnecessary(const struct sk_buff *skb)
2244 return skb->ip_summed & CHECKSUM_UNNECESSARY;
2248 * skb_checksum_complete - Calculate checksum of an entire packet
2249 * @skb: packet to process
2251 * This function calculates the checksum over the entire packet plus
2252 * the value of skb->csum. The latter can be used to supply the
2253 * checksum of a pseudo header as used by TCP/UDP. It returns the
2254 * checksum.
2256 * For protocols that contain complete checksums such as ICMP/TCP/UDP,
2257 * this function can be used to verify that checksum on received
2258 * packets. In that case the function should return zero if the
2259 * checksum is correct. In particular, this function will return zero
2260 * if skb->ip_summed is CHECKSUM_UNNECESSARY which indicates that the
2261 * hardware has already verified the correctness of the checksum.
2263 static inline __sum16 skb_checksum_complete(struct sk_buff *skb)
2265 return skb_csum_unnecessary(skb) ?
2266 0 : __skb_checksum_complete(skb);
2269 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
2270 extern void nf_conntrack_destroy(struct nf_conntrack *nfct);
2271 static inline void nf_conntrack_put(struct nf_conntrack *nfct)
2273 if (nfct && atomic_dec_and_test(&nfct->use))
2274 nf_conntrack_destroy(nfct);
2276 static inline void nf_conntrack_get(struct nf_conntrack *nfct)
2278 if (nfct)
2279 atomic_inc(&nfct->use);
2281 #endif
2282 #ifdef NET_SKBUFF_NF_DEFRAG_NEEDED
2283 static inline void nf_conntrack_get_reasm(struct sk_buff *skb)
2285 if (skb)
2286 atomic_inc(&skb->users);
2288 static inline void nf_conntrack_put_reasm(struct sk_buff *skb)
2290 if (skb)
2291 kfree_skb(skb);
2293 #endif
2294 #ifdef CONFIG_BRIDGE_NETFILTER
2295 static inline void nf_bridge_put(struct nf_bridge_info *nf_bridge)
2297 if (nf_bridge && atomic_dec_and_test(&nf_bridge->use))
2298 kfree(nf_bridge);
2300 static inline void nf_bridge_get(struct nf_bridge_info *nf_bridge)
2302 if (nf_bridge)
2303 atomic_inc(&nf_bridge->use);
2305 #endif /* CONFIG_BRIDGE_NETFILTER */
2306 static inline void nf_reset(struct sk_buff *skb)
2308 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
2309 nf_conntrack_put(skb->nfct);
2310 skb->nfct = NULL;
2311 #endif
2312 #ifdef NET_SKBUFF_NF_DEFRAG_NEEDED
2313 nf_conntrack_put_reasm(skb->nfct_reasm);
2314 skb->nfct_reasm = NULL;
2315 #endif
2316 #ifdef CONFIG_BRIDGE_NETFILTER
2317 nf_bridge_put(skb->nf_bridge);
2318 skb->nf_bridge = NULL;
2319 #endif
2322 /* Note: This doesn't put any conntrack and bridge info in dst. */
2323 static inline void __nf_copy(struct sk_buff *dst, const struct sk_buff *src)
2325 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
2326 dst->nfct = src->nfct;
2327 nf_conntrack_get(src->nfct);
2328 dst->nfctinfo = src->nfctinfo;
2329 #endif
2330 #ifdef NET_SKBUFF_NF_DEFRAG_NEEDED
2331 dst->nfct_reasm = src->nfct_reasm;
2332 nf_conntrack_get_reasm(src->nfct_reasm);
2333 #endif
2334 #ifdef CONFIG_BRIDGE_NETFILTER
2335 dst->nf_bridge = src->nf_bridge;
2336 nf_bridge_get(src->nf_bridge);
2337 #endif
2340 static inline void nf_copy(struct sk_buff *dst, const struct sk_buff *src)
2342 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
2343 nf_conntrack_put(dst->nfct);
2344 #endif
2345 #ifdef NET_SKBUFF_NF_DEFRAG_NEEDED
2346 nf_conntrack_put_reasm(dst->nfct_reasm);
2347 #endif
2348 #ifdef CONFIG_BRIDGE_NETFILTER
2349 nf_bridge_put(dst->nf_bridge);
2350 #endif
2351 __nf_copy(dst, src);
2354 #ifdef CONFIG_NETWORK_SECMARK
2355 static inline void skb_copy_secmark(struct sk_buff *to, const struct sk_buff *from)
2357 to->secmark = from->secmark;
2360 static inline void skb_init_secmark(struct sk_buff *skb)
2362 skb->secmark = 0;
2364 #else
2365 static inline void skb_copy_secmark(struct sk_buff *to, const struct sk_buff *from)
2368 static inline void skb_init_secmark(struct sk_buff *skb)
2370 #endif
2372 static inline void skb_set_queue_mapping(struct sk_buff *skb, u16 queue_mapping)
2374 skb->queue_mapping = queue_mapping;
2377 static inline u16 skb_get_queue_mapping(const struct sk_buff *skb)
2379 return skb->queue_mapping;
2382 static inline void skb_copy_queue_mapping(struct sk_buff *to, const struct sk_buff *from)
2384 to->queue_mapping = from->queue_mapping;
2387 static inline void skb_record_rx_queue(struct sk_buff *skb, u16 rx_queue)
2389 skb->queue_mapping = rx_queue + 1;
2392 static inline u16 skb_get_rx_queue(const struct sk_buff *skb)
2394 return skb->queue_mapping - 1;
2397 static inline bool skb_rx_queue_recorded(const struct sk_buff *skb)
2399 return skb->queue_mapping != 0;
2402 extern u16 __skb_tx_hash(const struct net_device *dev,
2403 const struct sk_buff *skb,
2404 unsigned int num_tx_queues);
2406 #ifdef CONFIG_XFRM
2407 static inline struct sec_path *skb_sec_path(struct sk_buff *skb)
2409 return skb->sp;
2411 #else
2412 static inline struct sec_path *skb_sec_path(struct sk_buff *skb)
2414 return NULL;
2416 #endif
2418 static inline int skb_is_gso(const struct sk_buff *skb)
2420 return skb_shinfo(skb)->gso_size;
2423 static inline int skb_is_gso_v6(const struct sk_buff *skb)
2425 return skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6;
2428 extern void __skb_warn_lro_forwarding(const struct sk_buff *skb);
2430 static inline bool skb_warn_if_lro(const struct sk_buff *skb)
2432 /* LRO sets gso_size but not gso_type, whereas if GSO is really
2433 * wanted then gso_type will be set. */
2434 struct skb_shared_info *shinfo = skb_shinfo(skb);
2435 if (skb_is_nonlinear(skb) && shinfo->gso_size != 0 &&
2436 unlikely(shinfo->gso_type == 0)) {
2437 __skb_warn_lro_forwarding(skb);
2438 return true;
2440 return false;
2443 static inline void skb_forward_csum(struct sk_buff *skb)
2445 /* Unfortunately we don't support this one. Any brave souls? */
2446 if (skb->ip_summed == CHECKSUM_COMPLETE)
2447 skb->ip_summed = CHECKSUM_NONE;
2451 * skb_checksum_none_assert - make sure skb ip_summed is CHECKSUM_NONE
2452 * @skb: skb to check
2454 * fresh skbs have their ip_summed set to CHECKSUM_NONE.
2455 * Instead of forcing ip_summed to CHECKSUM_NONE, we can
2456 * use this helper, to document places where we make this assertion.
2458 static inline void skb_checksum_none_assert(struct sk_buff *skb)
2460 #ifdef DEBUG
2461 BUG_ON(skb->ip_summed != CHECKSUM_NONE);
2462 #endif
2465 bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off);
2467 #endif /* __KERNEL__ */
2468 #endif /* _LINUX_SKBUFF_H */