2 * Definitions for the 'struct sk_buff' memory handlers.
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/config.h>
18 #include <linux/kernel.h>
19 #include <linux/compiler.h>
20 #include <linux/time.h>
21 #include <linux/cache.h>
23 #include <asm/atomic.h>
24 #include <asm/types.h>
25 #include <linux/spinlock.h>
27 #include <linux/highmem.h>
28 #include <linux/poll.h>
29 #include <linux/net.h>
31 #define HAVE_ALLOC_SKB /* For the drivers to know */
32 #define HAVE_ALIGNABLE_SKB /* Ditto 8) */
33 #define SLAB_SKB /* Slabified skbuffs */
35 #define CHECKSUM_NONE 0
37 #define CHECKSUM_UNNECESSARY 2
39 #define SKB_DATA_ALIGN(X) (((X) + (SMP_CACHE_BYTES - 1)) & \
40 ~(SMP_CACHE_BYTES - 1))
41 #define SKB_MAX_ORDER(X, ORDER) (((PAGE_SIZE << (ORDER)) - (X) - \
42 sizeof(struct skb_shared_info)) & \
43 ~(SMP_CACHE_BYTES - 1))
44 #define SKB_MAX_HEAD(X) (SKB_MAX_ORDER((X), 0))
45 #define SKB_MAX_ALLOC (SKB_MAX_ORDER(0, 2))
47 /* A. Checksumming of received packets by device.
49 * NONE: device failed to checksum this packet.
50 * skb->csum is undefined.
52 * UNNECESSARY: device parsed packet and wouldbe verified checksum.
53 * skb->csum is undefined.
54 * It is bad option, but, unfortunately, many of vendors do this.
55 * Apparently with secret goal to sell you new device, when you
56 * will add new protocol to your host. F.e. IPv6. 8)
58 * HW: the most generic way. Device supplied checksum of _all_
59 * the packet as seen by netif_rx in skb->csum.
60 * NOTE: Even if device supports only some protocols, but
61 * is able to produce some skb->csum, it MUST use HW,
64 * B. Checksumming on output.
66 * NONE: skb is checksummed by protocol or csum is not required.
68 * HW: device is required to csum packet as seen by hard_start_xmit
69 * from skb->h.raw to the end and to record the checksum
70 * at skb->h.raw+skb->csum.
72 * Device must show its capabilities in dev->features, set
73 * at device setup time.
74 * NETIF_F_HW_CSUM - it is clever device, it is able to checksum
76 * NETIF_F_NO_CSUM - loopback or reliable single hop media.
77 * NETIF_F_IP_CSUM - device is dumb. It is able to csum only
78 * TCP/UDP over IPv4. Sigh. Vendors like this
79 * way by an unknown reason. Though, see comment above
80 * about CHECKSUM_UNNECESSARY. 8)
82 * Any questions? No questions, good. --ANK
86 #define NET_CALLER(arg) (*(((void **)&arg) - 1))
88 #define NET_CALLER(arg) __builtin_return_address(0)
91 #ifdef CONFIG_NETFILTER
94 void (*destroy
)(struct nf_conntrack
*);
98 struct nf_conntrack
*master
;
101 #if defined(CONFIG_BRIDGE) || defined(CONFIG_BRIDGE_MODULE)
102 struct nf_bridge_info
{
104 struct net_device
*physindev
;
105 struct net_device
*physoutdev
;
107 unsigned long hh
[16 / sizeof(unsigned long)];
113 struct sk_buff_head
{
114 /* These two members must be first. */
115 struct sk_buff
*next
;
116 struct sk_buff
*prev
;
124 /* To allow 64K frame to be packed as single skb without frag_list */
125 #define MAX_SKB_FRAGS (65536/PAGE_SIZE + 2)
127 typedef struct skb_frag_struct skb_frag_t
;
129 struct skb_frag_struct
{
135 /* This data is invariant across clones and lives at
136 * the end of the header data, ie. at skb->end.
138 struct skb_shared_info
{
140 unsigned int nr_frags
;
141 unsigned short tso_size
;
142 unsigned short tso_segs
;
143 struct sk_buff
*frag_list
;
144 skb_frag_t frags
[MAX_SKB_FRAGS
];
148 * struct sk_buff - socket buffer
149 * @next: Next buffer in list
150 * @prev: Previous buffer in list
151 * @list: List we are on
152 * @sk: Socket we are owned by
153 * @stamp: Time we arrived
154 * @dev: Device we arrived on/are leaving by
155 * @h: Transport layer header
156 * @nh: Network layer header
157 * @mac: Link layer header
158 * @dst: FIXME: Describe this field
159 * @cb: Control buffer. Free for use by every layer. Put private vars here
160 * @len: Length of actual data
161 * @data_len: Data length
163 * @__unused: Dead field, may be reused
164 * @cloned: Head may be cloned (check refcnt to be sure)
165 * @pkt_type: Packet class
166 * @ip_summed: Driver fed us an IP checksum
167 * @priority: Packet queueing priority
168 * @users: User count - see {datagram,tcp}.c
169 * @protocol: Packet protocol from driver
170 * @security: Security level of packet
171 * @truesize: Buffer size
172 * @head: Head of buffer
173 * @data: Data head pointer
174 * @tail: Tail pointer
176 * @destructor: Destruct function
177 * @nfmark: Can be used for communication between hooks
178 * @nfcache: Cache info
179 * @nfct: Associated connection, if any
180 * @nf_debug: Netfilter debugging
181 * @nf_bridge: Saved data about a bridged frame - see br_netfilter.c
182 * @tc_index: Traffic control index
186 /* These two members must be first. */
187 struct sk_buff
*next
;
188 struct sk_buff
*prev
;
190 struct sk_buff_head
*list
;
192 struct timeval stamp
;
193 struct net_device
*dev
;
194 struct net_device
*real_dev
;
199 struct icmphdr
*icmph
;
200 struct igmphdr
*igmph
;
207 struct ipv6hdr
*ipv6h
;
213 struct ethhdr
*ethernet
;
217 struct dst_entry
*dst
;
221 * This is the control buffer. It is free to use for every
222 * layer. Please put your private variables there. If you
223 * want to keep them across layers you have to do a skb_clone()
224 * first. This is owned by whoever has the skb queued ATM.
231 unsigned char local_df
,
236 unsigned short protocol
,
239 void (*destructor
)(struct sk_buff
*skb
);
240 #ifdef CONFIG_NETFILTER
241 unsigned long nfmark
;
243 struct nf_ct_info
*nfct
;
244 #ifdef CONFIG_NETFILTER_DEBUG
245 unsigned int nf_debug
;
247 #if defined(CONFIG_BRIDGE) || defined(CONFIG_BRIDGE_MODULE)
248 struct nf_bridge_info
*nf_bridge
;
250 #endif /* CONFIG_NETFILTER */
251 #if defined(CONFIG_HIPPI)
256 #ifdef CONFIG_NET_SCHED
257 __u32 tc_index
; /* traffic control index */
260 /* These elements must be at the end, see alloc_skb() for details. */
261 unsigned int truesize
;
269 #define SK_WMEM_MAX 65535
270 #define SK_RMEM_MAX 65535
274 * Handling routines are only of interest to the kernel
276 #include <linux/slab.h>
278 #include <asm/system.h>
280 extern void __kfree_skb(struct sk_buff
*skb
);
281 extern struct sk_buff
*alloc_skb(unsigned int size
, int priority
);
282 extern void kfree_skbmem(struct sk_buff
*skb
);
283 extern struct sk_buff
*skb_clone(struct sk_buff
*skb
, int priority
);
284 extern struct sk_buff
*skb_copy(const struct sk_buff
*skb
, int priority
);
285 extern struct sk_buff
*pskb_copy(struct sk_buff
*skb
, int gfp_mask
);
286 extern int pskb_expand_head(struct sk_buff
*skb
,
287 int nhead
, int ntail
, int gfp_mask
);
288 extern struct sk_buff
*skb_realloc_headroom(struct sk_buff
*skb
,
289 unsigned int headroom
);
290 extern struct sk_buff
*skb_copy_expand(const struct sk_buff
*skb
,
291 int newheadroom
, int newtailroom
,
293 extern struct sk_buff
* skb_pad(struct sk_buff
*skb
, int pad
);
294 #define dev_kfree_skb(a) kfree_skb(a)
295 extern void skb_over_panic(struct sk_buff
*skb
, int len
,
297 extern void skb_under_panic(struct sk_buff
*skb
, int len
,
301 #define skb_shinfo(SKB) ((struct skb_shared_info *)((SKB)->end))
304 * skb_queue_empty - check if a queue is empty
307 * Returns true if the queue is empty, false otherwise.
309 static inline int skb_queue_empty(struct sk_buff_head
*list
)
311 return list
->next
== (struct sk_buff
*)list
;
315 * skb_get - reference buffer
316 * @skb: buffer to reference
318 * Makes another reference to a socket buffer and returns a pointer
321 static inline struct sk_buff
*skb_get(struct sk_buff
*skb
)
323 atomic_inc(&skb
->users
);
328 * If users == 1, we are the only owner and are can avoid redundant
333 * kfree_skb - free an sk_buff
334 * @skb: buffer to free
336 * Drop a reference to the buffer and free it if the usage count has
339 static inline void kfree_skb(struct sk_buff
*skb
)
341 if (atomic_read(&skb
->users
) == 1 || atomic_dec_and_test(&skb
->users
))
345 /* Use this if you didn't touch the skb state [for fast switching] */
346 static inline void kfree_skb_fast(struct sk_buff
*skb
)
348 if (atomic_read(&skb
->users
) == 1 || atomic_dec_and_test(&skb
->users
))
353 * skb_cloned - is the buffer a clone
354 * @skb: buffer to check
356 * Returns true if the buffer was generated with skb_clone() and is
357 * one of multiple shared copies of the buffer. Cloned buffers are
358 * shared data so must not be written to under normal circumstances.
360 static inline int skb_cloned(struct sk_buff
*skb
)
362 return skb
->cloned
&& atomic_read(&skb_shinfo(skb
)->dataref
) != 1;
366 * skb_shared - is the buffer shared
367 * @skb: buffer to check
369 * Returns true if more than one person has a reference to this
372 static inline int skb_shared(struct sk_buff
*skb
)
374 return atomic_read(&skb
->users
) != 1;
378 * skb_share_check - check if buffer is shared and if so clone it
379 * @skb: buffer to check
380 * @pri: priority for memory allocation
382 * If the buffer is shared the buffer is cloned and the old copy
383 * drops a reference. A new clone with a single reference is returned.
384 * If the buffer is not shared the original buffer is returned. When
385 * being called from interrupt status or with spinlocks held pri must
388 * NULL is returned on a memory allocation failure.
390 static inline struct sk_buff
*skb_share_check(struct sk_buff
*skb
, int pri
)
392 if (skb_shared(skb
)) {
393 struct sk_buff
*nskb
= skb_clone(skb
, pri
);
401 * Copy shared buffers into a new sk_buff. We effectively do COW on
402 * packets to handle cases where we have a local reader and forward
403 * and a couple of other messy ones. The normal one is tcpdumping
404 * a packet thats being forwarded.
408 * skb_unshare - make a copy of a shared buffer
409 * @skb: buffer to check
410 * @pri: priority for memory allocation
412 * If the socket buffer is a clone then this function creates a new
413 * copy of the data, drops a reference count on the old copy and returns
414 * the new copy with the reference count at 1. If the buffer is not a clone
415 * the original buffer is returned. When called with a spinlock held or
416 * from interrupt state @pri must be %GFP_ATOMIC
418 * %NULL is returned on a memory allocation failure.
420 static inline struct sk_buff
*skb_unshare(struct sk_buff
*skb
, int pri
)
422 if (skb_cloned(skb
)) {
423 struct sk_buff
*nskb
= skb_copy(skb
, pri
);
424 kfree_skb(skb
); /* Free our shared copy */
432 * @list_: list to peek at
434 * Peek an &sk_buff. Unlike most other operations you _MUST_
435 * be careful with this one. A peek leaves the buffer on the
436 * list and someone else may run off with it. You must hold
437 * the appropriate locks or have a private queue to do this.
439 * Returns %NULL for an empty list or a pointer to the head element.
440 * The reference count is not incremented and the reference is therefore
441 * volatile. Use with caution.
443 static inline struct sk_buff
*skb_peek(struct sk_buff_head
*list_
)
445 struct sk_buff
*list
= ((struct sk_buff
*)list_
)->next
;
446 if (list
== (struct sk_buff
*)list_
)
453 * @list_: list to peek at
455 * Peek an &sk_buff. Unlike most other operations you _MUST_
456 * be careful with this one. A peek leaves the buffer on the
457 * list and someone else may run off with it. You must hold
458 * the appropriate locks or have a private queue to do this.
460 * Returns %NULL for an empty list or a pointer to the tail element.
461 * The reference count is not incremented and the reference is therefore
462 * volatile. Use with caution.
464 static inline struct sk_buff
*skb_peek_tail(struct sk_buff_head
*list_
)
466 struct sk_buff
*list
= ((struct sk_buff
*)list_
)->prev
;
467 if (list
== (struct sk_buff
*)list_
)
473 * skb_queue_len - get queue length
474 * @list_: list to measure
476 * Return the length of an &sk_buff queue.
478 static inline __u32
skb_queue_len(struct sk_buff_head
*list_
)
483 static inline void skb_queue_head_init(struct sk_buff_head
*list
)
485 spin_lock_init(&list
->lock
);
486 list
->prev
= list
->next
= (struct sk_buff
*)list
;
491 * Insert an sk_buff at the start of a list.
493 * The "__skb_xxxx()" functions are the non-atomic ones that
494 * can only be called with interrupts disabled.
498 * __skb_queue_head - queue a buffer at the list head
500 * @newsk: buffer to queue
502 * Queue a buffer at the start of a list. This function takes no locks
503 * and you must therefore hold required locks before calling it.
505 * A buffer cannot be placed on two lists at the same time.
507 static inline void __skb_queue_head(struct sk_buff_head
*list
,
508 struct sk_buff
*newsk
)
510 struct sk_buff
*prev
, *next
;
514 prev
= (struct sk_buff
*)list
;
518 next
->prev
= prev
->next
= newsk
;
523 * skb_queue_head - queue a buffer at the list head
525 * @newsk: buffer to queue
527 * Queue a buffer at the start of the list. This function takes the
528 * list lock and can be used safely with other locking &sk_buff functions
531 * A buffer cannot be placed on two lists at the same time.
533 static inline void skb_queue_head(struct sk_buff_head
*list
,
534 struct sk_buff
*newsk
)
538 spin_lock_irqsave(&list
->lock
, flags
);
539 __skb_queue_head(list
, newsk
);
540 spin_unlock_irqrestore(&list
->lock
, flags
);
544 * __skb_queue_tail - queue a buffer at the list tail
546 * @newsk: buffer to queue
548 * Queue a buffer at the end of a list. This function takes no locks
549 * and you must therefore hold required locks before calling it.
551 * A buffer cannot be placed on two lists at the same time.
553 static inline void __skb_queue_tail(struct sk_buff_head
*list
,
554 struct sk_buff
*newsk
)
556 struct sk_buff
*prev
, *next
;
560 next
= (struct sk_buff
*)list
;
564 next
->prev
= prev
->next
= newsk
;
568 * skb_queue_tail - queue a buffer at the list tail
570 * @newsk: buffer to queue
572 * Queue a buffer at the tail of the list. This function takes the
573 * list lock and can be used safely with other locking &sk_buff functions
576 * A buffer cannot be placed on two lists at the same time.
578 static inline void skb_queue_tail(struct sk_buff_head
*list
,
579 struct sk_buff
*newsk
)
583 spin_lock_irqsave(&list
->lock
, flags
);
584 __skb_queue_tail(list
, newsk
);
585 spin_unlock_irqrestore(&list
->lock
, flags
);
589 * __skb_dequeue - remove from the head of the queue
590 * @list: list to dequeue from
592 * Remove the head of the list. This function does not take any locks
593 * so must be used with appropriate locks held only. The head item is
594 * returned or %NULL if the list is empty.
596 static inline struct sk_buff
*__skb_dequeue(struct sk_buff_head
*list
)
598 struct sk_buff
*next
, *prev
, *result
;
600 prev
= (struct sk_buff
*) list
;
609 result
->next
= result
->prev
= NULL
;
616 * skb_dequeue - remove from the head of the queue
617 * @list: list to dequeue from
619 * Remove the head of the list. The list lock is taken so the function
620 * may be used safely with other locking list functions. The head item is
621 * returned or %NULL if the list is empty.
624 static inline struct sk_buff
*skb_dequeue(struct sk_buff_head
*list
)
627 struct sk_buff
*result
;
629 spin_lock_irqsave(&list
->lock
, flags
);
630 result
= __skb_dequeue(list
);
631 spin_unlock_irqrestore(&list
->lock
, flags
);
636 * Insert a packet on a list.
639 static inline void __skb_insert(struct sk_buff
*newsk
,
640 struct sk_buff
*prev
, struct sk_buff
*next
,
641 struct sk_buff_head
*list
)
645 next
->prev
= prev
->next
= newsk
;
651 * skb_insert - insert a buffer
652 * @old: buffer to insert before
653 * @newsk: buffer to insert
655 * Place a packet before a given packet in a list. The list locks are taken
656 * and this function is atomic with respect to other list locked calls
657 * A buffer cannot be placed on two lists at the same time.
660 static inline void skb_insert(struct sk_buff
*old
, struct sk_buff
*newsk
)
664 spin_lock_irqsave(&old
->list
->lock
, flags
);
665 __skb_insert(newsk
, old
->prev
, old
, old
->list
);
666 spin_unlock_irqrestore(&old
->list
->lock
, flags
);
670 * Place a packet after a given packet in a list.
673 static inline void __skb_append(struct sk_buff
*old
, struct sk_buff
*newsk
)
675 __skb_insert(newsk
, old
, old
->next
, old
->list
);
679 * skb_append - append a buffer
680 * @old: buffer to insert after
681 * @newsk: buffer to insert
683 * Place a packet after a given packet in a list. The list locks are taken
684 * and this function is atomic with respect to other list locked calls.
685 * A buffer cannot be placed on two lists at the same time.
689 static inline void skb_append(struct sk_buff
*old
, struct sk_buff
*newsk
)
693 spin_lock_irqsave(&old
->list
->lock
, flags
);
694 __skb_append(old
, newsk
);
695 spin_unlock_irqrestore(&old
->list
->lock
, flags
);
699 * remove sk_buff from list. _Must_ be called atomically, and with
702 static inline void __skb_unlink(struct sk_buff
*skb
, struct sk_buff_head
*list
)
704 struct sk_buff
*next
, *prev
;
709 skb
->next
= skb
->prev
= NULL
;
716 * skb_unlink - remove a buffer from a list
717 * @skb: buffer to remove
719 * Place a packet after a given packet in a list. The list locks are taken
720 * and this function is atomic with respect to other list locked calls
722 * Works even without knowing the list it is sitting on, which can be
723 * handy at times. It also means that THE LIST MUST EXIST when you
724 * unlink. Thus a list must have its contents unlinked before it is
727 static inline void skb_unlink(struct sk_buff
*skb
)
729 struct sk_buff_head
*list
= skb
->list
;
734 spin_lock_irqsave(&list
->lock
, flags
);
735 if (skb
->list
== list
)
736 __skb_unlink(skb
, skb
->list
);
737 spin_unlock_irqrestore(&list
->lock
, flags
);
741 /* XXX: more streamlined implementation */
744 * __skb_dequeue_tail - remove from the tail of the queue
745 * @list: list to dequeue from
747 * Remove the tail of the list. This function does not take any locks
748 * so must be used with appropriate locks held only. The tail item is
749 * returned or %NULL if the list is empty.
751 static inline struct sk_buff
*__skb_dequeue_tail(struct sk_buff_head
*list
)
753 struct sk_buff
*skb
= skb_peek_tail(list
);
755 __skb_unlink(skb
, list
);
760 * skb_dequeue - remove from the head of the queue
761 * @list: list to dequeue from
763 * Remove the head of the list. The list lock is taken so the function
764 * may be used safely with other locking list functions. The tail item is
765 * returned or %NULL if the list is empty.
767 static inline struct sk_buff
*skb_dequeue_tail(struct sk_buff_head
*list
)
770 struct sk_buff
*result
;
772 spin_lock_irqsave(&list
->lock
, flags
);
773 result
= __skb_dequeue_tail(list
);
774 spin_unlock_irqrestore(&list
->lock
, flags
);
778 static inline int skb_is_nonlinear(const struct sk_buff
*skb
)
780 return skb
->data_len
;
783 static inline unsigned int skb_headlen(const struct sk_buff
*skb
)
785 return skb
->len
- skb
->data_len
;
788 static inline int skb_pagelen(const struct sk_buff
*skb
)
792 for (i
= (int)skb_shinfo(skb
)->nr_frags
- 1; i
>= 0; i
--)
793 len
+= skb_shinfo(skb
)->frags
[i
].size
;
794 return len
+ skb_headlen(skb
);
797 static inline void skb_fill_page_desc(struct sk_buff
*skb
, int i
, struct page
*page
, int off
, int size
)
799 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
801 frag
->page_offset
= off
;
803 skb_shinfo(skb
)->nr_frags
= i
+1;
806 #define SKB_PAGE_ASSERT(skb) BUG_ON(skb_shinfo(skb)->nr_frags)
807 #define SKB_FRAG_ASSERT(skb) BUG_ON(skb_shinfo(skb)->frag_list)
808 #define SKB_LINEAR_ASSERT(skb) BUG_ON(skb_is_nonlinear(skb))
811 * Add data to an sk_buff
813 static inline unsigned char *__skb_put(struct sk_buff
*skb
, unsigned int len
)
815 unsigned char *tmp
= skb
->tail
;
816 SKB_LINEAR_ASSERT(skb
);
823 * skb_put - add data to a buffer
824 * @skb: buffer to use
825 * @len: amount of data to add
827 * This function extends the used data area of the buffer. If this would
828 * exceed the total buffer size the kernel will panic. A pointer to the
829 * first byte of the extra data is returned.
831 static inline unsigned char *skb_put(struct sk_buff
*skb
, unsigned int len
)
833 unsigned char *tmp
= skb
->tail
;
834 SKB_LINEAR_ASSERT(skb
);
837 if (unlikely(skb
->tail
>skb
->end
))
838 skb_over_panic(skb
, len
, current_text_addr());
842 static inline unsigned char *__skb_push(struct sk_buff
*skb
, unsigned int len
)
850 * skb_push - add data to the start of a buffer
851 * @skb: buffer to use
852 * @len: amount of data to add
854 * This function extends the used data area of the buffer at the buffer
855 * start. If this would exceed the total buffer headroom the kernel will
856 * panic. A pointer to the first byte of the extra data is returned.
858 static inline unsigned char *skb_push(struct sk_buff
*skb
, unsigned int len
)
862 if (unlikely(skb
->data
<skb
->head
))
863 skb_under_panic(skb
, len
, current_text_addr());
867 static inline char *__skb_pull(struct sk_buff
*skb
, unsigned int len
)
870 BUG_ON(skb
->len
< skb
->data_len
);
871 return skb
->data
+= len
;
875 * skb_pull - remove data from the start of a buffer
876 * @skb: buffer to use
877 * @len: amount of data to remove
879 * This function removes data from the start of a buffer, returning
880 * the memory to the headroom. A pointer to the next data in the buffer
881 * is returned. Once the data has been pulled future pushes will overwrite
884 static inline unsigned char *skb_pull(struct sk_buff
*skb
, unsigned int len
)
886 return (len
> skb
->len
) ? NULL
: __skb_pull(skb
, len
);
889 extern unsigned char *__pskb_pull_tail(struct sk_buff
*skb
, int delta
);
891 static inline char *__pskb_pull(struct sk_buff
*skb
, unsigned int len
)
893 if (len
> skb_headlen(skb
) &&
894 !__pskb_pull_tail(skb
, len
-skb_headlen(skb
)))
897 return skb
->data
+= len
;
900 static inline unsigned char *pskb_pull(struct sk_buff
*skb
, unsigned int len
)
902 return (len
> skb
->len
) ? NULL
: __pskb_pull(skb
, len
);
905 static inline int pskb_may_pull(struct sk_buff
*skb
, unsigned int len
)
907 if (len
<= skb_headlen(skb
))
911 return __pskb_pull_tail(skb
, len
-skb_headlen(skb
)) != NULL
;
915 * skb_headroom - bytes at buffer head
916 * @skb: buffer to check
918 * Return the number of bytes of free space at the head of an &sk_buff.
920 static inline int skb_headroom(const struct sk_buff
*skb
)
922 return skb
->data
- skb
->head
;
926 * skb_tailroom - bytes at buffer end
927 * @skb: buffer to check
929 * Return the number of bytes of free space at the tail of an sk_buff
931 static inline int skb_tailroom(const struct sk_buff
*skb
)
933 return skb_is_nonlinear(skb
) ? 0 : skb
->end
- skb
->tail
;
937 * skb_reserve - adjust headroom
938 * @skb: buffer to alter
939 * @len: bytes to move
941 * Increase the headroom of an empty &sk_buff by reducing the tail
942 * room. This is only allowed for an empty buffer.
944 static inline void skb_reserve(struct sk_buff
*skb
, unsigned int len
)
950 extern int ___pskb_trim(struct sk_buff
*skb
, unsigned int len
, int realloc
);
952 static inline void __skb_trim(struct sk_buff
*skb
, unsigned int len
)
954 if (!skb
->data_len
) {
956 skb
->tail
= skb
->data
+ len
;
958 ___pskb_trim(skb
, len
, 0);
962 * skb_trim - remove end from a buffer
963 * @skb: buffer to alter
966 * Cut the length of a buffer down by removing data from the tail. If
967 * the buffer is already under the length specified it is not modified.
969 static inline void skb_trim(struct sk_buff
*skb
, unsigned int len
)
972 __skb_trim(skb
, len
);
976 static inline int __pskb_trim(struct sk_buff
*skb
, unsigned int len
)
978 if (!skb
->data_len
) {
980 skb
->tail
= skb
->data
+len
;
983 return ___pskb_trim(skb
, len
, 1);
986 static inline int pskb_trim(struct sk_buff
*skb
, unsigned int len
)
988 return (len
< skb
->len
) ? __pskb_trim(skb
, len
) : 0;
992 * skb_orphan - orphan a buffer
993 * @skb: buffer to orphan
995 * If a buffer currently has an owner then we call the owner's
996 * destructor function and make the @skb unowned. The buffer continues
997 * to exist but is no longer charged to its former owner.
999 static inline void skb_orphan(struct sk_buff
*skb
)
1001 if (skb
->destructor
)
1002 skb
->destructor(skb
);
1003 skb
->destructor
= NULL
;
1008 * skb_queue_purge - empty a list
1009 * @list: list to empty
1011 * Delete all buffers on an &sk_buff list. Each buffer is removed from
1012 * the list and one reference dropped. This function takes the list
1013 * lock and is atomic with respect to other list locking functions.
1015 static inline void skb_queue_purge(struct sk_buff_head
*list
)
1017 struct sk_buff
*skb
;
1018 while ((skb
= skb_dequeue(list
)) != NULL
)
1023 * __skb_queue_purge - empty a list
1024 * @list: list to empty
1026 * Delete all buffers on an &sk_buff list. Each buffer is removed from
1027 * the list and one reference dropped. This function does not take the
1028 * list lock and the caller must hold the relevant locks to use it.
1030 static inline void __skb_queue_purge(struct sk_buff_head
*list
)
1032 struct sk_buff
*skb
;
1033 while ((skb
= __skb_dequeue(list
)) != NULL
)
1038 * __dev_alloc_skb - allocate an skbuff for sending
1039 * @length: length to allocate
1040 * @gfp_mask: get_free_pages mask, passed to alloc_skb
1042 * Allocate a new &sk_buff and assign it a usage count of one. The
1043 * buffer has unspecified headroom built in. Users should allocate
1044 * the headroom they think they need without accounting for the
1045 * built in space. The built in space is used for optimisations.
1047 * %NULL is returned in there is no free memory.
1049 static inline struct sk_buff
*__dev_alloc_skb(unsigned int length
,
1052 struct sk_buff
*skb
= alloc_skb(length
+ 16, gfp_mask
);
1054 skb_reserve(skb
, 16);
1059 * dev_alloc_skb - allocate an skbuff for sending
1060 * @length: length to allocate
1062 * Allocate a new &sk_buff and assign it a usage count of one. The
1063 * buffer has unspecified headroom built in. Users should allocate
1064 * the headroom they think they need without accounting for the
1065 * built in space. The built in space is used for optimisations.
1067 * %NULL is returned in there is no free memory. Although this function
1068 * allocates memory it can be called from an interrupt.
1070 static inline struct sk_buff
*dev_alloc_skb(unsigned int length
)
1072 return __dev_alloc_skb(length
, GFP_ATOMIC
);
1076 * skb_cow - copy header of skb when it is required
1077 * @skb: buffer to cow
1078 * @headroom: needed headroom
1080 * If the skb passed lacks sufficient headroom or its data part
1081 * is shared, data is reallocated. If reallocation fails, an error
1082 * is returned and original skb is not changed.
1084 * The result is skb with writable area skb->head...skb->tail
1085 * and at least @headroom of space at head.
1087 static inline int skb_cow(struct sk_buff
*skb
, unsigned int headroom
)
1089 int delta
= (headroom
> 16 ? headroom
: 16) - skb_headroom(skb
);
1094 if (delta
|| skb_cloned(skb
))
1095 return pskb_expand_head(skb
, (delta
+ 15) & ~15, 0, GFP_ATOMIC
);
1100 * skb_padto - pad an skbuff up to a minimal size
1101 * @skb: buffer to pad
1102 * @len: minimal length
1104 * Pads up a buffer to ensure the trailing bytes exist and are
1105 * blanked. If the buffer already contains sufficient data it
1106 * is untouched. Returns the buffer, which may be a replacement
1107 * for the original, or NULL for out of memory - in which case
1108 * the original buffer is still freed.
1111 static inline struct sk_buff
*skb_padto(struct sk_buff
*skb
, unsigned int len
)
1113 unsigned int size
= skb
->len
;
1114 if (likely(size
>= len
))
1116 return skb_pad(skb
, len
-size
);
1120 * skb_linearize - convert paged skb to linear one
1121 * @skb: buffer to linarize
1122 * @gfp: allocation mode
1124 * If there is no free memory -ENOMEM is returned, otherwise zero
1125 * is returned and the old skb data released.
1127 extern int __skb_linearize(struct sk_buff
*skb
, int gfp
);
1128 static inline int __deprecated
skb_linearize(struct sk_buff
*skb
, int gfp
)
1130 return __skb_linearize(skb
, gfp
);
1133 static inline void *kmap_skb_frag(const skb_frag_t
*frag
)
1135 #ifdef CONFIG_HIGHMEM
1140 return kmap_atomic(frag
->page
, KM_SKB_DATA_SOFTIRQ
);
1143 static inline void kunmap_skb_frag(void *vaddr
)
1145 kunmap_atomic(vaddr
, KM_SKB_DATA_SOFTIRQ
);
1146 #ifdef CONFIG_HIGHMEM
1151 #define skb_queue_walk(queue, skb) \
1152 for (skb = (queue)->next, prefetch(skb->next); \
1153 (skb != (struct sk_buff *)(queue)); \
1154 skb = skb->next, prefetch(skb->next))
1157 extern struct sk_buff
*skb_recv_datagram(struct sock
*sk
, unsigned flags
,
1158 int noblock
, int *err
);
1159 extern unsigned int datagram_poll(struct file
*file
, struct socket
*sock
,
1160 struct poll_table_struct
*wait
);
1161 extern int skb_copy_datagram(const struct sk_buff
*from
,
1162 int offset
, char *to
, int size
);
1163 extern int skb_copy_datagram_iovec(const struct sk_buff
*from
,
1164 int offset
, struct iovec
*to
,
1166 extern int skb_copy_and_csum_datagram(const struct sk_buff
*skb
,
1167 int offset
, u8
*to
, int len
,
1168 unsigned int *csump
);
1169 extern int skb_copy_and_csum_datagram_iovec(const
1170 struct sk_buff
*skb
,
1173 extern void skb_free_datagram(struct sock
*sk
, struct sk_buff
*skb
);
1174 extern unsigned int skb_checksum(const struct sk_buff
*skb
, int offset
,
1175 int len
, unsigned int csum
);
1176 extern int skb_copy_bits(const struct sk_buff
*skb
, int offset
,
1178 extern unsigned int skb_copy_and_csum_bits(const struct sk_buff
*skb
,
1179 int offset
, u8
*to
, int len
,
1181 extern void skb_copy_and_csum_dev(const struct sk_buff
*skb
, u8
*to
);
1183 extern void skb_init(void);
1184 extern void skb_add_mtu(int mtu
);
1186 #ifdef CONFIG_NETFILTER
1187 static inline void nf_conntrack_put(struct nf_ct_info
*nfct
)
1189 if (nfct
&& atomic_dec_and_test(&nfct
->master
->use
))
1190 nfct
->master
->destroy(nfct
->master
);
1192 static inline void nf_conntrack_get(struct nf_ct_info
*nfct
)
1195 atomic_inc(&nfct
->master
->use
);
1198 #if defined(CONFIG_BRIDGE) || defined(CONFIG_BRIDGE_MODULE)
1199 static inline void nf_bridge_put(struct nf_bridge_info
*nf_bridge
)
1201 if (nf_bridge
&& atomic_dec_and_test(&nf_bridge
->use
))
1204 static inline void nf_bridge_get(struct nf_bridge_info
*nf_bridge
)
1207 atomic_inc(&nf_bridge
->use
);
1213 #endif /* __KERNEL__ */
1214 #endif /* _LINUX_SKBUFF_H */