[PATCH] some more av7110 dvb-driver updates
[linux-2.6/history.git] / include / linux / skbuff.h
blob2aeb35d62c2d2e4865db3ff272258e0cd23271fe
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/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>
26 #include <linux/mm.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
36 #define CHECKSUM_HW 1
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,
62 * not UNNECESSARY.
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
75 * everything.
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
85 #ifdef __i386__
86 #define NET_CALLER(arg) (*(((void **)&arg) - 1))
87 #else
88 #define NET_CALLER(arg) __builtin_return_address(0)
89 #endif
91 #ifdef CONFIG_NETFILTER
92 struct nf_conntrack {
93 atomic_t use;
94 void (*destroy)(struct nf_conntrack *);
97 struct nf_ct_info {
98 struct nf_conntrack *master;
101 #ifdef CONFIG_BRIDGE_NETFILTER
102 struct nf_bridge_info {
103 atomic_t use;
104 struct net_device *physindev;
105 struct net_device *physoutdev;
106 unsigned int mask;
107 unsigned long hh[16 / sizeof(unsigned long)];
109 #endif
111 #endif
113 struct sk_buff_head {
114 /* These two members must be first. */
115 struct sk_buff *next;
116 struct sk_buff *prev;
118 __u32 qlen;
119 spinlock_t lock;
122 struct sk_buff;
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 {
130 struct page *page;
131 __u16 page_offset;
132 __u16 size;
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 {
139 atomic_t dataref;
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];
147 /**
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 * @real_dev: The real device we are using
156 * @h: Transport layer header
157 * @nh: Network layer header
158 * @mac: Link layer header
159 * @dst: FIXME: Describe this field
160 * @cb: Control buffer. Free for use by every layer. Put private vars here
161 * @len: Length of actual data
162 * @data_len: Data length
163 * @csum: Checksum
164 * @__unused: Dead field, may be reused
165 * @cloned: Head may be cloned (check refcnt to be sure)
166 * @pkt_type: Packet class
167 * @ip_summed: Driver fed us an IP checksum
168 * @priority: Packet queueing priority
169 * @users: User count - see {datagram,tcp}.c
170 * @protocol: Packet protocol from driver
171 * @security: Security level of packet
172 * @truesize: Buffer size
173 * @head: Head of buffer
174 * @data: Data head pointer
175 * @tail: Tail pointer
176 * @end: End pointer
177 * @destructor: Destruct function
178 * @nfmark: Can be used for communication between hooks
179 * @nfcache: Cache info
180 * @nfct: Associated connection, if any
181 * @nf_debug: Netfilter debugging
182 * @nf_bridge: Saved data about a bridged frame - see br_netfilter.c
183 * @private: Data which is private to the HIPPI implementation
184 * @tc_index: Traffic control index
187 struct sk_buff {
188 /* These two members must be first. */
189 struct sk_buff *next;
190 struct sk_buff *prev;
192 struct sk_buff_head *list;
193 struct sock *sk;
194 struct timeval stamp;
195 struct net_device *dev;
196 struct net_device *real_dev;
198 union {
199 struct tcphdr *th;
200 struct udphdr *uh;
201 struct icmphdr *icmph;
202 struct igmphdr *igmph;
203 struct iphdr *ipiph;
204 unsigned char *raw;
205 } h;
207 union {
208 struct iphdr *iph;
209 struct ipv6hdr *ipv6h;
210 struct arphdr *arph;
211 unsigned char *raw;
212 } nh;
214 union {
215 struct ethhdr *ethernet;
216 unsigned char *raw;
217 } mac;
219 struct dst_entry *dst;
220 struct sec_path *sp;
223 * This is the control buffer. It is free to use for every
224 * layer. Please put your private variables there. If you
225 * want to keep them across layers you have to do a skb_clone()
226 * first. This is owned by whoever has the skb queued ATM.
228 char cb[48];
230 unsigned int len,
231 data_len,
232 csum;
233 unsigned char local_df,
234 cloned,
235 pkt_type,
236 ip_summed;
237 __u32 priority;
238 unsigned short protocol,
239 security;
241 void (*destructor)(struct sk_buff *skb);
242 #ifdef CONFIG_NETFILTER
243 unsigned long nfmark;
244 __u32 nfcache;
245 struct nf_ct_info *nfct;
246 #ifdef CONFIG_NETFILTER_DEBUG
247 unsigned int nf_debug;
248 #endif
249 #ifdef CONFIG_BRIDGE_NETFILTER
250 struct nf_bridge_info *nf_bridge;
251 #endif
252 #endif /* CONFIG_NETFILTER */
253 #if defined(CONFIG_HIPPI)
254 union {
255 __u32 ifield;
256 } private;
257 #endif
258 #ifdef CONFIG_NET_SCHED
259 __u32 tc_index; /* traffic control index */
260 #endif
262 /* These elements must be at the end, see alloc_skb() for details. */
263 unsigned int truesize;
264 atomic_t users;
265 unsigned char *head,
266 *data,
267 *tail,
268 *end;
271 #define SK_WMEM_MAX 65535
272 #define SK_RMEM_MAX 65535
274 #ifdef __KERNEL__
276 * Handling routines are only of interest to the kernel
278 #include <linux/slab.h>
280 #include <asm/system.h>
282 extern void __kfree_skb(struct sk_buff *skb);
283 extern struct sk_buff *alloc_skb(unsigned int size, int priority);
284 extern void kfree_skbmem(struct sk_buff *skb);
285 extern struct sk_buff *skb_clone(struct sk_buff *skb, int priority);
286 extern struct sk_buff *skb_copy(const struct sk_buff *skb, int priority);
287 extern struct sk_buff *pskb_copy(struct sk_buff *skb, int gfp_mask);
288 extern int pskb_expand_head(struct sk_buff *skb,
289 int nhead, int ntail, int gfp_mask);
290 extern struct sk_buff *skb_realloc_headroom(struct sk_buff *skb,
291 unsigned int headroom);
292 extern struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
293 int newheadroom, int newtailroom,
294 int priority);
295 extern struct sk_buff * skb_pad(struct sk_buff *skb, int pad);
296 #define dev_kfree_skb(a) kfree_skb(a)
297 extern void skb_over_panic(struct sk_buff *skb, int len,
298 void *here);
299 extern void skb_under_panic(struct sk_buff *skb, int len,
300 void *here);
302 /* Internal */
303 #define skb_shinfo(SKB) ((struct skb_shared_info *)((SKB)->end))
306 * skb_queue_empty - check if a queue is empty
307 * @list: queue head
309 * Returns true if the queue is empty, false otherwise.
311 static inline int skb_queue_empty(const struct sk_buff_head *list)
313 return list->next == (struct sk_buff *)list;
317 * skb_get - reference buffer
318 * @skb: buffer to reference
320 * Makes another reference to a socket buffer and returns a pointer
321 * to the buffer.
323 static inline struct sk_buff *skb_get(struct sk_buff *skb)
325 atomic_inc(&skb->users);
326 return skb;
330 * If users == 1, we are the only owner and are can avoid redundant
331 * atomic change.
335 * kfree_skb - free an sk_buff
336 * @skb: buffer to free
338 * Drop a reference to the buffer and free it if the usage count has
339 * hit zero.
341 static inline void kfree_skb(struct sk_buff *skb)
343 if (atomic_read(&skb->users) == 1 || atomic_dec_and_test(&skb->users))
344 __kfree_skb(skb);
347 /* Use this if you didn't touch the skb state [for fast switching] */
348 static inline void kfree_skb_fast(struct sk_buff *skb)
350 if (atomic_read(&skb->users) == 1 || atomic_dec_and_test(&skb->users))
351 kfree_skbmem(skb);
355 * skb_cloned - is the buffer a clone
356 * @skb: buffer to check
358 * Returns true if the buffer was generated with skb_clone() and is
359 * one of multiple shared copies of the buffer. Cloned buffers are
360 * shared data so must not be written to under normal circumstances.
362 static inline int skb_cloned(const struct sk_buff *skb)
364 return skb->cloned && atomic_read(&skb_shinfo(skb)->dataref) != 1;
368 * skb_shared - is the buffer shared
369 * @skb: buffer to check
371 * Returns true if more than one person has a reference to this
372 * buffer.
374 static inline int skb_shared(const struct sk_buff *skb)
376 return atomic_read(&skb->users) != 1;
380 * skb_share_check - check if buffer is shared and if so clone it
381 * @skb: buffer to check
382 * @pri: priority for memory allocation
384 * If the buffer is shared the buffer is cloned and the old copy
385 * drops a reference. A new clone with a single reference is returned.
386 * If the buffer is not shared the original buffer is returned. When
387 * being called from interrupt status or with spinlocks held pri must
388 * be GFP_ATOMIC.
390 * NULL is returned on a memory allocation failure.
392 static inline struct sk_buff *skb_share_check(struct sk_buff *skb, int pri)
394 might_sleep_if(pri & __GFP_WAIT);
395 if (skb_shared(skb)) {
396 struct sk_buff *nskb = skb_clone(skb, pri);
397 kfree_skb(skb);
398 skb = nskb;
400 return skb;
404 * Copy shared buffers into a new sk_buff. We effectively do COW on
405 * packets to handle cases where we have a local reader and forward
406 * and a couple of other messy ones. The normal one is tcpdumping
407 * a packet thats being forwarded.
411 * skb_unshare - make a copy of a shared buffer
412 * @skb: buffer to check
413 * @pri: priority for memory allocation
415 * If the socket buffer is a clone then this function creates a new
416 * copy of the data, drops a reference count on the old copy and returns
417 * the new copy with the reference count at 1. If the buffer is not a clone
418 * the original buffer is returned. When called with a spinlock held or
419 * from interrupt state @pri must be %GFP_ATOMIC
421 * %NULL is returned on a memory allocation failure.
423 static inline struct sk_buff *skb_unshare(struct sk_buff *skb, int pri)
425 might_sleep_if(pri & __GFP_WAIT);
426 if (skb_cloned(skb)) {
427 struct sk_buff *nskb = skb_copy(skb, pri);
428 kfree_skb(skb); /* Free our shared copy */
429 skb = nskb;
431 return skb;
435 * skb_peek
436 * @list_: list to peek at
438 * Peek an &sk_buff. Unlike most other operations you _MUST_
439 * be careful with this one. A peek leaves the buffer on the
440 * list and someone else may run off with it. You must hold
441 * the appropriate locks or have a private queue to do this.
443 * Returns %NULL for an empty list or a pointer to the head element.
444 * The reference count is not incremented and the reference is therefore
445 * volatile. Use with caution.
447 static inline struct sk_buff *skb_peek(struct sk_buff_head *list_)
449 struct sk_buff *list = ((struct sk_buff *)list_)->next;
450 if (list == (struct sk_buff *)list_)
451 list = NULL;
452 return list;
456 * skb_peek_tail
457 * @list_: list to peek at
459 * Peek an &sk_buff. Unlike most other operations you _MUST_
460 * be careful with this one. A peek leaves the buffer on the
461 * list and someone else may run off with it. You must hold
462 * the appropriate locks or have a private queue to do this.
464 * Returns %NULL for an empty list or a pointer to the tail element.
465 * The reference count is not incremented and the reference is therefore
466 * volatile. Use with caution.
468 static inline struct sk_buff *skb_peek_tail(struct sk_buff_head *list_)
470 struct sk_buff *list = ((struct sk_buff *)list_)->prev;
471 if (list == (struct sk_buff *)list_)
472 list = NULL;
473 return list;
477 * skb_queue_len - get queue length
478 * @list_: list to measure
480 * Return the length of an &sk_buff queue.
482 static inline __u32 skb_queue_len(const struct sk_buff_head *list_)
484 return list_->qlen;
487 static inline void skb_queue_head_init(struct sk_buff_head *list)
489 spin_lock_init(&list->lock);
490 list->prev = list->next = (struct sk_buff *)list;
491 list->qlen = 0;
495 * Insert an sk_buff at the start of a list.
497 * The "__skb_xxxx()" functions are the non-atomic ones that
498 * can only be called with interrupts disabled.
502 * __skb_queue_head - queue a buffer at the list head
503 * @list: list to use
504 * @newsk: buffer to queue
506 * Queue a buffer at the start of a list. This function takes no locks
507 * and you must therefore hold required locks before calling it.
509 * A buffer cannot be placed on two lists at the same time.
511 static inline void __skb_queue_head(struct sk_buff_head *list,
512 struct sk_buff *newsk)
514 struct sk_buff *prev, *next;
516 newsk->list = list;
517 list->qlen++;
518 prev = (struct sk_buff *)list;
519 next = prev->next;
520 newsk->next = next;
521 newsk->prev = prev;
522 next->prev = prev->next = newsk;
527 * skb_queue_head - queue a buffer at the list head
528 * @list: list to use
529 * @newsk: buffer to queue
531 * Queue a buffer at the start of the list. This function takes the
532 * list lock and can be used safely with other locking &sk_buff functions
533 * safely.
535 * A buffer cannot be placed on two lists at the same time.
537 static inline void skb_queue_head(struct sk_buff_head *list,
538 struct sk_buff *newsk)
540 unsigned long flags;
542 spin_lock_irqsave(&list->lock, flags);
543 __skb_queue_head(list, newsk);
544 spin_unlock_irqrestore(&list->lock, flags);
548 * __skb_queue_tail - queue a buffer at the list tail
549 * @list: list to use
550 * @newsk: buffer to queue
552 * Queue a buffer at the end of a list. This function takes no locks
553 * and you must therefore hold required locks before calling it.
555 * A buffer cannot be placed on two lists at the same time.
557 static inline void __skb_queue_tail(struct sk_buff_head *list,
558 struct sk_buff *newsk)
560 struct sk_buff *prev, *next;
562 newsk->list = list;
563 list->qlen++;
564 next = (struct sk_buff *)list;
565 prev = next->prev;
566 newsk->next = next;
567 newsk->prev = prev;
568 next->prev = prev->next = newsk;
572 * skb_queue_tail - queue a buffer at the list tail
573 * @list: list to use
574 * @newsk: buffer to queue
576 * Queue a buffer at the tail of the list. This function takes the
577 * list lock and can be used safely with other locking &sk_buff functions
578 * safely.
580 * A buffer cannot be placed on two lists at the same time.
582 static inline void skb_queue_tail(struct sk_buff_head *list,
583 struct sk_buff *newsk)
585 unsigned long flags;
587 spin_lock_irqsave(&list->lock, flags);
588 __skb_queue_tail(list, newsk);
589 spin_unlock_irqrestore(&list->lock, flags);
593 * __skb_dequeue - remove from the head of the queue
594 * @list: list to dequeue from
596 * Remove the head of the list. This function does not take any locks
597 * so must be used with appropriate locks held only. The head item is
598 * returned or %NULL if the list is empty.
600 static inline struct sk_buff *__skb_dequeue(struct sk_buff_head *list)
602 struct sk_buff *next, *prev, *result;
604 prev = (struct sk_buff *) list;
605 next = prev->next;
606 result = NULL;
607 if (next != prev) {
608 result = next;
609 next = next->next;
610 list->qlen--;
611 next->prev = prev;
612 prev->next = next;
613 result->next = result->prev = NULL;
614 result->list = NULL;
616 return result;
620 * skb_dequeue - remove from the head of the queue
621 * @list: list to dequeue from
623 * Remove the head of the list. The list lock is taken so the function
624 * may be used safely with other locking list functions. The head item is
625 * returned or %NULL if the list is empty.
628 static inline struct sk_buff *skb_dequeue(struct sk_buff_head *list)
630 unsigned long flags;
631 struct sk_buff *result;
633 spin_lock_irqsave(&list->lock, flags);
634 result = __skb_dequeue(list);
635 spin_unlock_irqrestore(&list->lock, flags);
636 return result;
640 * Insert a packet on a list.
643 static inline void __skb_insert(struct sk_buff *newsk,
644 struct sk_buff *prev, struct sk_buff *next,
645 struct sk_buff_head *list)
647 newsk->next = next;
648 newsk->prev = prev;
649 next->prev = prev->next = newsk;
650 newsk->list = list;
651 list->qlen++;
655 * skb_insert - insert a buffer
656 * @old: buffer to insert before
657 * @newsk: buffer to insert
659 * Place a packet before a given packet in a list. The list locks are taken
660 * and this function is atomic with respect to other list locked calls
661 * A buffer cannot be placed on two lists at the same time.
664 static inline void skb_insert(struct sk_buff *old, struct sk_buff *newsk)
666 unsigned long flags;
668 spin_lock_irqsave(&old->list->lock, flags);
669 __skb_insert(newsk, old->prev, old, old->list);
670 spin_unlock_irqrestore(&old->list->lock, flags);
674 * Place a packet after a given packet in a list.
677 static inline void __skb_append(struct sk_buff *old, struct sk_buff *newsk)
679 __skb_insert(newsk, old, old->next, old->list);
683 * skb_append - append a buffer
684 * @old: buffer to insert after
685 * @newsk: buffer to insert
687 * Place a packet after a given packet in a list. The list locks are taken
688 * and this function is atomic with respect to other list locked calls.
689 * A buffer cannot be placed on two lists at the same time.
693 static inline void skb_append(struct sk_buff *old, struct sk_buff *newsk)
695 unsigned long flags;
697 spin_lock_irqsave(&old->list->lock, flags);
698 __skb_append(old, newsk);
699 spin_unlock_irqrestore(&old->list->lock, flags);
703 * remove sk_buff from list. _Must_ be called atomically, and with
704 * the list known..
706 static inline void __skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
708 struct sk_buff *next, *prev;
710 list->qlen--;
711 next = skb->next;
712 prev = skb->prev;
713 skb->next = skb->prev = NULL;
714 skb->list = NULL;
715 next->prev = prev;
716 prev->next = next;
720 * skb_unlink - remove a buffer from a list
721 * @skb: buffer to remove
723 * Place a packet after a given packet in a list. The list locks are taken
724 * and this function is atomic with respect to other list locked calls
726 * Works even without knowing the list it is sitting on, which can be
727 * handy at times. It also means that THE LIST MUST EXIST when you
728 * unlink. Thus a list must have its contents unlinked before it is
729 * destroyed.
731 static inline void skb_unlink(struct sk_buff *skb)
733 struct sk_buff_head *list = skb->list;
735 if (list) {
736 unsigned long flags;
738 spin_lock_irqsave(&list->lock, flags);
739 if (skb->list == list)
740 __skb_unlink(skb, skb->list);
741 spin_unlock_irqrestore(&list->lock, flags);
745 /* XXX: more streamlined implementation */
748 * __skb_dequeue_tail - remove from the tail of the queue
749 * @list: list to dequeue from
751 * Remove the tail of the list. This function does not take any locks
752 * so must be used with appropriate locks held only. The tail item is
753 * returned or %NULL if the list is empty.
755 static inline struct sk_buff *__skb_dequeue_tail(struct sk_buff_head *list)
757 struct sk_buff *skb = skb_peek_tail(list);
758 if (skb)
759 __skb_unlink(skb, list);
760 return skb;
764 * skb_dequeue - remove from the head of the queue
765 * @list: list to dequeue from
767 * Remove the head of the list. The list lock is taken so the function
768 * may be used safely with other locking list functions. The tail item is
769 * returned or %NULL if the list is empty.
771 static inline struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
773 unsigned long flags;
774 struct sk_buff *result;
776 spin_lock_irqsave(&list->lock, flags);
777 result = __skb_dequeue_tail(list);
778 spin_unlock_irqrestore(&list->lock, flags);
779 return result;
782 static inline int skb_is_nonlinear(const struct sk_buff *skb)
784 return skb->data_len;
787 static inline unsigned int skb_headlen(const struct sk_buff *skb)
789 return skb->len - skb->data_len;
792 static inline int skb_pagelen(const struct sk_buff *skb)
794 int i, len = 0;
796 for (i = (int)skb_shinfo(skb)->nr_frags - 1; i >= 0; i--)
797 len += skb_shinfo(skb)->frags[i].size;
798 return len + skb_headlen(skb);
801 static inline void skb_fill_page_desc(struct sk_buff *skb, int i, struct page *page, int off, int size)
803 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
804 frag->page = page;
805 frag->page_offset = off;
806 frag->size = size;
807 skb_shinfo(skb)->nr_frags = i+1;
810 #define SKB_PAGE_ASSERT(skb) BUG_ON(skb_shinfo(skb)->nr_frags)
811 #define SKB_FRAG_ASSERT(skb) BUG_ON(skb_shinfo(skb)->frag_list)
812 #define SKB_LINEAR_ASSERT(skb) BUG_ON(skb_is_nonlinear(skb))
815 * Add data to an sk_buff
817 static inline unsigned char *__skb_put(struct sk_buff *skb, unsigned int len)
819 unsigned char *tmp = skb->tail;
820 SKB_LINEAR_ASSERT(skb);
821 skb->tail += len;
822 skb->len += len;
823 return tmp;
827 * skb_put - add data to a buffer
828 * @skb: buffer to use
829 * @len: amount of data to add
831 * This function extends the used data area of the buffer. If this would
832 * exceed the total buffer size the kernel will panic. A pointer to the
833 * first byte of the extra data is returned.
835 static inline unsigned char *skb_put(struct sk_buff *skb, unsigned int len)
837 unsigned char *tmp = skb->tail;
838 SKB_LINEAR_ASSERT(skb);
839 skb->tail += len;
840 skb->len += len;
841 if (unlikely(skb->tail>skb->end))
842 skb_over_panic(skb, len, current_text_addr());
843 return tmp;
846 static inline unsigned char *__skb_push(struct sk_buff *skb, unsigned int len)
848 skb->data -= len;
849 skb->len += len;
850 return skb->data;
854 * skb_push - add data to the start of a buffer
855 * @skb: buffer to use
856 * @len: amount of data to add
858 * This function extends the used data area of the buffer at the buffer
859 * start. If this would exceed the total buffer headroom the kernel will
860 * panic. A pointer to the first byte of the extra data is returned.
862 static inline unsigned char *skb_push(struct sk_buff *skb, unsigned int len)
864 skb->data -= len;
865 skb->len += len;
866 if (unlikely(skb->data<skb->head))
867 skb_under_panic(skb, len, current_text_addr());
868 return skb->data;
871 static inline char *__skb_pull(struct sk_buff *skb, unsigned int len)
873 skb->len -= len;
874 BUG_ON(skb->len < skb->data_len);
875 return skb->data += len;
879 * skb_pull - remove data from the start of a buffer
880 * @skb: buffer to use
881 * @len: amount of data to remove
883 * This function removes data from the start of a buffer, returning
884 * the memory to the headroom. A pointer to the next data in the buffer
885 * is returned. Once the data has been pulled future pushes will overwrite
886 * the old data.
888 static inline unsigned char *skb_pull(struct sk_buff *skb, unsigned int len)
890 return unlikely(len > skb->len) ? NULL : __skb_pull(skb, len);
893 extern unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta);
895 static inline char *__pskb_pull(struct sk_buff *skb, unsigned int len)
897 if (len > skb_headlen(skb) &&
898 !__pskb_pull_tail(skb, len-skb_headlen(skb)))
899 return NULL;
900 skb->len -= len;
901 return skb->data += len;
904 static inline unsigned char *pskb_pull(struct sk_buff *skb, unsigned int len)
906 return unlikely(len > skb->len) ? NULL : __pskb_pull(skb, len);
909 static inline int pskb_may_pull(struct sk_buff *skb, unsigned int len)
911 if (likely(len <= skb_headlen(skb)))
912 return 1;
913 if (unlikely(len > skb->len))
914 return 0;
915 return __pskb_pull_tail(skb, len-skb_headlen(skb)) != NULL;
919 * skb_headroom - bytes at buffer head
920 * @skb: buffer to check
922 * Return the number of bytes of free space at the head of an &sk_buff.
924 static inline int skb_headroom(const struct sk_buff *skb)
926 return skb->data - skb->head;
930 * skb_tailroom - bytes at buffer end
931 * @skb: buffer to check
933 * Return the number of bytes of free space at the tail of an sk_buff
935 static inline int skb_tailroom(const struct sk_buff *skb)
937 return skb_is_nonlinear(skb) ? 0 : skb->end - skb->tail;
941 * skb_reserve - adjust headroom
942 * @skb: buffer to alter
943 * @len: bytes to move
945 * Increase the headroom of an empty &sk_buff by reducing the tail
946 * room. This is only allowed for an empty buffer.
948 static inline void skb_reserve(struct sk_buff *skb, unsigned int len)
950 skb->data += len;
951 skb->tail += len;
954 extern int ___pskb_trim(struct sk_buff *skb, unsigned int len, int realloc);
956 static inline void __skb_trim(struct sk_buff *skb, unsigned int len)
958 if (!skb->data_len) {
959 skb->len = len;
960 skb->tail = skb->data + len;
961 } else
962 ___pskb_trim(skb, len, 0);
966 * skb_trim - remove end from a buffer
967 * @skb: buffer to alter
968 * @len: new length
970 * Cut the length of a buffer down by removing data from the tail. If
971 * the buffer is already under the length specified it is not modified.
973 static inline void skb_trim(struct sk_buff *skb, unsigned int len)
975 if (skb->len > len)
976 __skb_trim(skb, len);
980 static inline int __pskb_trim(struct sk_buff *skb, unsigned int len)
982 if (!skb->data_len) {
983 skb->len = len;
984 skb->tail = skb->data+len;
985 return 0;
987 return ___pskb_trim(skb, len, 1);
990 static inline int pskb_trim(struct sk_buff *skb, unsigned int len)
992 return (len < skb->len) ? __pskb_trim(skb, len) : 0;
996 * skb_orphan - orphan a buffer
997 * @skb: buffer to orphan
999 * If a buffer currently has an owner then we call the owner's
1000 * destructor function and make the @skb unowned. The buffer continues
1001 * to exist but is no longer charged to its former owner.
1003 static inline void skb_orphan(struct sk_buff *skb)
1005 if (skb->destructor)
1006 skb->destructor(skb);
1007 skb->destructor = NULL;
1008 skb->sk = NULL;
1012 * skb_queue_purge - empty a list
1013 * @list: list to empty
1015 * Delete all buffers on an &sk_buff list. Each buffer is removed from
1016 * the list and one reference dropped. This function takes the list
1017 * lock and is atomic with respect to other list locking functions.
1019 static inline void skb_queue_purge(struct sk_buff_head *list)
1021 struct sk_buff *skb;
1022 while ((skb = skb_dequeue(list)) != NULL)
1023 kfree_skb(skb);
1027 * __skb_queue_purge - empty a list
1028 * @list: list to empty
1030 * Delete all buffers on an &sk_buff list. Each buffer is removed from
1031 * the list and one reference dropped. This function does not take the
1032 * list lock and the caller must hold the relevant locks to use it.
1034 static inline void __skb_queue_purge(struct sk_buff_head *list)
1036 struct sk_buff *skb;
1037 while ((skb = __skb_dequeue(list)) != NULL)
1038 kfree_skb(skb);
1042 * __dev_alloc_skb - allocate an skbuff for sending
1043 * @length: length to allocate
1044 * @gfp_mask: get_free_pages mask, passed to alloc_skb
1046 * Allocate a new &sk_buff and assign it a usage count of one. The
1047 * buffer has unspecified headroom built in. Users should allocate
1048 * the headroom they think they need without accounting for the
1049 * built in space. The built in space is used for optimisations.
1051 * %NULL is returned in there is no free memory.
1053 static inline struct sk_buff *__dev_alloc_skb(unsigned int length,
1054 int gfp_mask)
1056 struct sk_buff *skb = alloc_skb(length + 16, gfp_mask);
1057 if (likely(skb))
1058 skb_reserve(skb, 16);
1059 return skb;
1063 * dev_alloc_skb - allocate an skbuff for sending
1064 * @length: length to allocate
1066 * Allocate a new &sk_buff and assign it a usage count of one. The
1067 * buffer has unspecified headroom built in. Users should allocate
1068 * the headroom they think they need without accounting for the
1069 * built in space. The built in space is used for optimisations.
1071 * %NULL is returned in there is no free memory. Although this function
1072 * allocates memory it can be called from an interrupt.
1074 static inline struct sk_buff *dev_alloc_skb(unsigned int length)
1076 return __dev_alloc_skb(length, GFP_ATOMIC);
1080 * skb_cow - copy header of skb when it is required
1081 * @skb: buffer to cow
1082 * @headroom: needed headroom
1084 * If the skb passed lacks sufficient headroom or its data part
1085 * is shared, data is reallocated. If reallocation fails, an error
1086 * is returned and original skb is not changed.
1088 * The result is skb with writable area skb->head...skb->tail
1089 * and at least @headroom of space at head.
1091 static inline int skb_cow(struct sk_buff *skb, unsigned int headroom)
1093 int delta = (headroom > 16 ? headroom : 16) - skb_headroom(skb);
1095 if (delta < 0)
1096 delta = 0;
1098 if (delta || skb_cloned(skb))
1099 return pskb_expand_head(skb, (delta + 15) & ~15, 0, GFP_ATOMIC);
1100 return 0;
1104 * skb_padto - pad an skbuff up to a minimal size
1105 * @skb: buffer to pad
1106 * @len: minimal length
1108 * Pads up a buffer to ensure the trailing bytes exist and are
1109 * blanked. If the buffer already contains sufficient data it
1110 * is untouched. Returns the buffer, which may be a replacement
1111 * for the original, or NULL for out of memory - in which case
1112 * the original buffer is still freed.
1115 static inline struct sk_buff *skb_padto(struct sk_buff *skb, unsigned int len)
1117 unsigned int size = skb->len;
1118 if (likely(size >= len))
1119 return skb;
1120 return skb_pad(skb, len-size);
1124 * skb_linearize - convert paged skb to linear one
1125 * @skb: buffer to linarize
1126 * @gfp: allocation mode
1128 * If there is no free memory -ENOMEM is returned, otherwise zero
1129 * is returned and the old skb data released.
1131 extern int __skb_linearize(struct sk_buff *skb, int gfp);
1132 static inline int __deprecated skb_linearize(struct sk_buff *skb, int gfp)
1134 return __skb_linearize(skb, gfp);
1137 static inline void *kmap_skb_frag(const skb_frag_t *frag)
1139 #ifdef CONFIG_HIGHMEM
1140 BUG_ON(in_irq());
1142 local_bh_disable();
1143 #endif
1144 return kmap_atomic(frag->page, KM_SKB_DATA_SOFTIRQ);
1147 static inline void kunmap_skb_frag(void *vaddr)
1149 kunmap_atomic(vaddr, KM_SKB_DATA_SOFTIRQ);
1150 #ifdef CONFIG_HIGHMEM
1151 local_bh_enable();
1152 #endif
1155 #define skb_queue_walk(queue, skb) \
1156 for (skb = (queue)->next, prefetch(skb->next); \
1157 (skb != (struct sk_buff *)(queue)); \
1158 skb = skb->next, prefetch(skb->next))
1161 extern struct sk_buff *skb_recv_datagram(struct sock *sk, unsigned flags,
1162 int noblock, int *err);
1163 extern unsigned int datagram_poll(struct file *file, struct socket *sock,
1164 struct poll_table_struct *wait);
1165 extern int skb_copy_datagram(const struct sk_buff *from,
1166 int offset, char *to, int size);
1167 extern int skb_copy_datagram_iovec(const struct sk_buff *from,
1168 int offset, struct iovec *to,
1169 int size);
1170 extern int skb_copy_and_csum_datagram(const struct sk_buff *skb,
1171 int offset, u8 *to, int len,
1172 unsigned int *csump);
1173 extern int skb_copy_and_csum_datagram_iovec(const
1174 struct sk_buff *skb,
1175 int hlen,
1176 struct iovec *iov);
1177 extern void skb_free_datagram(struct sock *sk, struct sk_buff *skb);
1178 extern unsigned int skb_checksum(const struct sk_buff *skb, int offset,
1179 int len, unsigned int csum);
1180 extern int skb_copy_bits(const struct sk_buff *skb, int offset,
1181 void *to, int len);
1182 extern unsigned int skb_copy_and_csum_bits(const struct sk_buff *skb,
1183 int offset, u8 *to, int len,
1184 unsigned int csum);
1185 extern void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to);
1187 extern void skb_init(void);
1188 extern void skb_add_mtu(int mtu);
1190 #ifdef CONFIG_NETFILTER
1191 static inline void nf_conntrack_put(struct nf_ct_info *nfct)
1193 if (nfct && atomic_dec_and_test(&nfct->master->use))
1194 nfct->master->destroy(nfct->master);
1196 static inline void nf_conntrack_get(struct nf_ct_info *nfct)
1198 if (nfct)
1199 atomic_inc(&nfct->master->use);
1202 #ifdef CONFIG_BRIDGE_NETFILTER
1203 static inline void nf_bridge_put(struct nf_bridge_info *nf_bridge)
1205 if (nf_bridge && atomic_dec_and_test(&nf_bridge->use))
1206 kfree(nf_bridge);
1208 static inline void nf_bridge_get(struct nf_bridge_info *nf_bridge)
1210 if (nf_bridge)
1211 atomic_inc(&nf_bridge->use);
1213 #endif
1215 #endif
1217 #endif /* __KERNEL__ */
1218 #endif /* _LINUX_SKBUFF_H */