More cleaning ...
[linux-2.6/linux-mips.git] / include / linux / skbuff.h
blob57af37850a4e7a542005c0a62e0431d25b0f12bf
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 #if defined(CONFIG_BRIDGE) || defined(CONFIG_BRIDGE_MODULE)
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 * @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
162 * @csum: Checksum
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
175 * @end: End 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
185 struct sk_buff {
186 /* These two members must be first. */
187 struct sk_buff *next;
188 struct sk_buff *prev;
190 struct sk_buff_head *list;
191 struct sock *sk;
192 struct timeval stamp;
193 struct net_device *dev;
194 struct net_device *real_dev;
196 union {
197 struct tcphdr *th;
198 struct udphdr *uh;
199 struct icmphdr *icmph;
200 struct igmphdr *igmph;
201 struct iphdr *ipiph;
202 unsigned char *raw;
203 } h;
205 union {
206 struct iphdr *iph;
207 struct ipv6hdr *ipv6h;
208 struct arphdr *arph;
209 unsigned char *raw;
210 } nh;
212 union {
213 struct ethhdr *ethernet;
214 unsigned char *raw;
215 } mac;
217 struct dst_entry *dst;
218 struct sec_path *sp;
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.
226 char cb[48];
228 unsigned int len,
229 data_len,
230 csum;
231 unsigned char local_df,
232 cloned,
233 pkt_type,
234 ip_summed;
235 __u32 priority;
236 unsigned short protocol,
237 security;
239 void (*destructor)(struct sk_buff *skb);
240 #ifdef CONFIG_NETFILTER
241 unsigned long nfmark;
242 __u32 nfcache;
243 struct nf_ct_info *nfct;
244 #ifdef CONFIG_NETFILTER_DEBUG
245 unsigned int nf_debug;
246 #endif
247 #if defined(CONFIG_BRIDGE) || defined(CONFIG_BRIDGE_MODULE)
248 struct nf_bridge_info *nf_bridge;
249 #endif
250 #endif /* CONFIG_NETFILTER */
251 #if defined(CONFIG_HIPPI)
252 union {
253 __u32 ifield;
254 } private;
255 #endif
256 #ifdef CONFIG_NET_SCHED
257 __u32 tc_index; /* traffic control index */
258 #endif
260 /* These elements must be at the end, see alloc_skb() for details. */
261 unsigned int truesize;
262 atomic_t users;
263 unsigned char *head,
264 *data,
265 *tail,
266 *end;
269 #define SK_WMEM_MAX 65535
270 #define SK_RMEM_MAX 65535
272 #ifdef __KERNEL__
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,
292 int priority);
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,
296 void *here);
297 extern void skb_under_panic(struct sk_buff *skb, int len,
298 void *here);
300 /* Internal */
301 #define skb_shinfo(SKB) ((struct skb_shared_info *)((SKB)->end))
304 * skb_queue_empty - check if a queue is empty
305 * @list: queue head
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
319 * to the buffer.
321 static inline struct sk_buff *skb_get(struct sk_buff *skb)
323 atomic_inc(&skb->users);
324 return skb;
328 * If users == 1, we are the only owner and are can avoid redundant
329 * atomic change.
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
337 * hit zero.
339 static inline void kfree_skb(struct sk_buff *skb)
341 if (atomic_read(&skb->users) == 1 || atomic_dec_and_test(&skb->users))
342 __kfree_skb(skb);
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))
349 kfree_skbmem(skb);
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
370 * buffer.
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
386 * be GFP_ATOMIC.
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);
394 kfree_skb(skb);
395 skb = nskb;
397 return skb;
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 */
425 skb = nskb;
427 return skb;
431 * skb_peek
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_)
447 list = NULL;
448 return list;
452 * skb_peek_tail
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_)
468 list = NULL;
469 return 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_)
480 return list_->qlen;
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;
487 list->qlen = 0;
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
499 * @list: list to use
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;
512 newsk->list = list;
513 list->qlen++;
514 prev = (struct sk_buff *)list;
515 next = prev->next;
516 newsk->next = next;
517 newsk->prev = prev;
518 next->prev = prev->next = newsk;
523 * skb_queue_head - queue a buffer at the list head
524 * @list: list to use
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
529 * safely.
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)
536 unsigned long flags;
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
545 * @list: list to use
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;
558 newsk->list = list;
559 list->qlen++;
560 next = (struct sk_buff *)list;
561 prev = next->prev;
562 newsk->next = next;
563 newsk->prev = prev;
564 next->prev = prev->next = newsk;
568 * skb_queue_tail - queue a buffer at the list tail
569 * @list: list to use
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
574 * safely.
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)
581 unsigned long flags;
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;
601 next = prev->next;
602 result = NULL;
603 if (next != prev) {
604 result = next;
605 next = next->next;
606 list->qlen--;
607 next->prev = prev;
608 prev->next = next;
609 result->next = result->prev = NULL;
610 result->list = NULL;
612 return result;
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)
626 unsigned long flags;
627 struct sk_buff *result;
629 spin_lock_irqsave(&list->lock, flags);
630 result = __skb_dequeue(list);
631 spin_unlock_irqrestore(&list->lock, flags);
632 return result;
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)
643 newsk->next = next;
644 newsk->prev = prev;
645 next->prev = prev->next = newsk;
646 newsk->list = list;
647 list->qlen++;
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)
662 unsigned long flags;
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)
691 unsigned long flags;
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
700 * the list known..
702 static inline void __skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
704 struct sk_buff *next, *prev;
706 list->qlen--;
707 next = skb->next;
708 prev = skb->prev;
709 skb->next = skb->prev = NULL;
710 skb->list = NULL;
711 next->prev = prev;
712 prev->next = next;
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
725 * destroyed.
727 static inline void skb_unlink(struct sk_buff *skb)
729 struct sk_buff_head *list = skb->list;
731 if (list) {
732 unsigned long flags;
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);
754 if (skb)
755 __skb_unlink(skb, list);
756 return skb;
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)
769 unsigned long flags;
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);
775 return result;
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)
790 int i, len = 0;
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];
800 frag->page = page;
801 frag->page_offset = off;
802 frag->size = size;
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);
817 skb->tail += len;
818 skb->len += len;
819 return tmp;
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);
835 skb->tail += len;
836 skb->len += len;
837 if (unlikely(skb->tail>skb->end))
838 skb_over_panic(skb, len, current_text_addr());
839 return tmp;
842 static inline unsigned char *__skb_push(struct sk_buff *skb, unsigned int len)
844 skb->data -= len;
845 skb->len += len;
846 return skb->data;
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)
860 skb->data -= len;
861 skb->len += len;
862 if (unlikely(skb->data<skb->head))
863 skb_under_panic(skb, len, current_text_addr());
864 return skb->data;
867 static inline char *__skb_pull(struct sk_buff *skb, unsigned int len)
869 skb->len -= 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
882 * the old data.
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)))
895 return NULL;
896 skb->len -= len;
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))
908 return 1;
909 if (len > skb->len)
910 return 0;
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)
946 skb->data += len;
947 skb->tail += 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) {
955 skb->len = len;
956 skb->tail = skb->data + len;
957 } else
958 ___pskb_trim(skb, len, 0);
962 * skb_trim - remove end from a buffer
963 * @skb: buffer to alter
964 * @len: new length
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)
971 if (skb->len > len)
972 __skb_trim(skb, len);
976 static inline int __pskb_trim(struct sk_buff *skb, unsigned int len)
978 if (!skb->data_len) {
979 skb->len = len;
980 skb->tail = skb->data+len;
981 return 0;
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;
1004 skb->sk = 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)
1019 kfree_skb(skb);
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)
1034 kfree_skb(skb);
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,
1050 int gfp_mask)
1052 struct sk_buff *skb = alloc_skb(length + 16, gfp_mask);
1053 if (skb)
1054 skb_reserve(skb, 16);
1055 return skb;
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);
1091 if (delta < 0)
1092 delta = 0;
1094 if (delta || skb_cloned(skb))
1095 return pskb_expand_head(skb, (delta + 15) & ~15, 0, GFP_ATOMIC);
1096 return 0;
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))
1115 return skb;
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
1136 BUG_ON(in_irq());
1138 local_bh_disable();
1139 #endif
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
1147 local_bh_enable();
1148 #endif
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,
1165 int size);
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,
1171 int hlen,
1172 struct iovec *iov);
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,
1177 void *to, int len);
1178 extern unsigned int skb_copy_and_csum_bits(const struct sk_buff *skb,
1179 int offset, u8 *to, int len,
1180 unsigned int csum);
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)
1194 if (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))
1202 kfree(nf_bridge);
1204 static inline void nf_bridge_get(struct nf_bridge_info *nf_bridge)
1206 if (nf_bridge)
1207 atomic_inc(&nf_bridge->use);
1209 #endif
1211 #endif
1213 #endif /* __KERNEL__ */
1214 #endif /* _LINUX_SKBUFF_H */