ia64/xen-unstable

view linux-2.4.29-xen-sparse/include/linux/skbuff.h @ 3516:1a4f61d36171

bitkeeper revision 1.1159.223.31 (41f599bcklevTYwPtWQUZ7QK-azDbg)

Fix recent patch to change the way the version string is generated.
Signed-off-by: ian.pratt@cl.cam.ac.uk
author iap10@freefall.cl.cam.ac.uk
date Tue Jan 25 00:58:36 2005 +0000 (2005-01-25)
parents ed0d4ce83995
children d126cac32f08
line source
1 /*
2 * Definitions for the 'struct sk_buff' memory handlers.
3 *
4 * Authors:
5 * Alan Cox, <gw4pts@gw4pts.ampr.org>
6 * Florian La Roche, <rzsfl@rz.uni-sb.de>
7 *
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.
12 */
14 #ifndef _LINUX_SKBUFF_H
15 #define _LINUX_SKBUFF_H
17 #include <linux/config.h>
18 #include <linux/kernel.h>
19 #include <linux/sched.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>
29 #define HAVE_ALLOC_SKB /* For the drivers to know */
30 #define HAVE_ALIGNABLE_SKB /* Ditto 8) */
31 #define SLAB_SKB /* Slabified skbuffs */
33 #define CHECKSUM_NONE 0
34 #define CHECKSUM_HW 1
35 #define CHECKSUM_UNNECESSARY 2
37 #define SKB_DATA_ALIGN(X) (((X) + (SMP_CACHE_BYTES-1)) & ~(SMP_CACHE_BYTES-1))
38 #define SKB_MAX_ORDER(X,ORDER) (((PAGE_SIZE<<(ORDER)) - (X) - sizeof(struct skb_shared_info))&~(SMP_CACHE_BYTES-1))
39 #define SKB_MAX_HEAD(X) (SKB_MAX_ORDER((X),0))
40 #define SKB_MAX_ALLOC (SKB_MAX_ORDER(0,2))
42 /* A. Checksumming of received packets by device.
43 *
44 * NONE: device failed to checksum this packet.
45 * skb->csum is undefined.
46 *
47 * UNNECESSARY: device parsed packet and wouldbe verified checksum.
48 * skb->csum is undefined.
49 * It is bad option, but, unfortunately, many of vendors do this.
50 * Apparently with secret goal to sell you new device, when you
51 * will add new protocol to your host. F.e. IPv6. 8)
52 *
53 * HW: the most generic way. Device supplied checksum of _all_
54 * the packet as seen by netif_rx in skb->csum.
55 * NOTE: Even if device supports only some protocols, but
56 * is able to produce some skb->csum, it MUST use HW,
57 * not UNNECESSARY.
58 *
59 * B. Checksumming on output.
60 *
61 * NONE: skb is checksummed by protocol or csum is not required.
62 *
63 * HW: device is required to csum packet as seen by hard_start_xmit
64 * from skb->h.raw to the end and to record the checksum
65 * at skb->h.raw+skb->csum.
66 *
67 * Device must show its capabilities in dev->features, set
68 * at device setup time.
69 * NETIF_F_HW_CSUM - it is clever device, it is able to checksum
70 * everything.
71 * NETIF_F_NO_CSUM - loopback or reliable single hop media.
72 * NETIF_F_IP_CSUM - device is dumb. It is able to csum only
73 * TCP/UDP over IPv4. Sigh. Vendors like this
74 * way by an unknown reason. Though, see comment above
75 * about CHECKSUM_UNNECESSARY. 8)
76 *
77 * Any questions? No questions, good. --ANK
78 */
80 #ifdef __i386__
81 #define NET_CALLER(arg) (*(((void**)&arg)-1))
82 #else
83 #define NET_CALLER(arg) __builtin_return_address(0)
84 #endif
86 #ifdef CONFIG_NETFILTER
87 struct nf_conntrack {
88 atomic_t use;
89 void (*destroy)(struct nf_conntrack *);
90 };
92 struct nf_ct_info {
93 struct nf_conntrack *master;
94 };
95 #endif
97 struct sk_buff_head {
98 /* These two members must be first. */
99 struct sk_buff * next;
100 struct sk_buff * prev;
102 __u32 qlen;
103 spinlock_t lock;
104 };
106 struct sk_buff;
108 #define MAX_SKB_FRAGS 6
110 typedef struct skb_frag_struct skb_frag_t;
112 struct skb_frag_struct
113 {
114 struct page *page;
115 __u16 page_offset;
116 __u16 size;
117 };
119 /* This data is invariant across clones and lives at
120 * the end of the header data, ie. at skb->end.
121 */
122 struct skb_shared_info {
123 atomic_t dataref;
124 unsigned int nr_frags;
125 struct sk_buff *frag_list;
126 skb_frag_t frags[MAX_SKB_FRAGS];
127 };
129 struct sk_buff {
130 /* These two members must be first. */
131 struct sk_buff * next; /* Next buffer in list */
132 struct sk_buff * prev; /* Previous buffer in list */
134 struct sk_buff_head * list; /* List we are on */
135 struct sock *sk; /* Socket we are owned by */
136 struct timeval stamp; /* Time we arrived */
137 struct net_device *dev; /* Device we arrived on/are leaving by */
138 struct net_device *real_dev; /* For support of point to point protocols
139 (e.g. 802.3ad) over bonding, we must save the
140 physical device that got the packet before
141 replacing skb->dev with the virtual device. */
143 /* Transport layer header */
144 union
145 {
146 struct tcphdr *th;
147 struct udphdr *uh;
148 struct icmphdr *icmph;
149 struct igmphdr *igmph;
150 struct iphdr *ipiph;
151 struct spxhdr *spxh;
152 unsigned char *raw;
153 } h;
155 /* Network layer header */
156 union
157 {
158 struct iphdr *iph;
159 struct ipv6hdr *ipv6h;
160 struct arphdr *arph;
161 struct ipxhdr *ipxh;
162 unsigned char *raw;
163 } nh;
165 /* Link layer header */
166 union
167 {
168 struct ethhdr *ethernet;
169 unsigned char *raw;
170 } mac;
172 struct dst_entry *dst;
174 /*
175 * This is the control buffer. It is free to use for every
176 * layer. Please put your private variables there. If you
177 * want to keep them across layers you have to do a skb_clone()
178 * first. This is owned by whoever has the skb queued ATM.
179 */
180 char cb[48];
182 unsigned int len; /* Length of actual data */
183 unsigned int data_len;
184 unsigned int csum; /* Checksum */
185 unsigned char __unused, /* Dead field, may be reused */
186 cloned, /* head may be cloned (check refcnt to be sure). */
187 pkt_type, /* Packet class */
188 ip_summed; /* Driver fed us an IP checksum */
189 __u32 priority; /* Packet queueing priority */
190 atomic_t users; /* User count - see datagram.c,tcp.c */
191 unsigned short protocol; /* Packet protocol from driver. */
192 unsigned short security; /* Security level of packet */
193 unsigned int truesize; /* Buffer size */
195 unsigned char *head; /* Head of buffer */
196 unsigned char *data; /* Data head pointer */
197 unsigned char *tail; /* Tail pointer */
198 unsigned char *end; /* End pointer */
200 void (*destructor)(struct sk_buff *); /* Destruct function */
201 #ifdef CONFIG_NETFILTER
202 /* Can be used for communication between hooks. */
203 unsigned long nfmark;
204 /* Cache info */
205 __u32 nfcache;
206 /* Associated connection, if any */
207 struct nf_ct_info *nfct;
208 #ifdef CONFIG_NETFILTER_DEBUG
209 unsigned int nf_debug;
210 #endif
211 #endif /*CONFIG_NETFILTER*/
213 #if defined(CONFIG_HIPPI)
214 union{
215 __u32 ifield;
216 } private;
217 #endif
219 #ifdef CONFIG_NET_SCHED
220 __u32 tc_index; /* traffic control index */
221 #endif
222 };
224 #ifdef __KERNEL__
225 /*
226 * Handling routines are only of interest to the kernel
227 */
228 #include <linux/slab.h>
230 #include <asm/system.h>
232 extern void __kfree_skb(struct sk_buff *skb);
233 extern struct sk_buff * alloc_skb(unsigned int size, int priority);
234 extern struct sk_buff * alloc_skb_from_cache(kmem_cache_t *cp, unsigned int size, int priority);
235 extern void kfree_skbmem(struct sk_buff *skb);
236 extern struct sk_buff * skb_clone(struct sk_buff *skb, int priority);
237 extern struct sk_buff * skb_copy(const struct sk_buff *skb, int priority);
238 extern struct sk_buff * pskb_copy(struct sk_buff *skb, int gfp_mask);
239 extern int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail, int gfp_mask);
240 extern struct sk_buff * skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom);
241 extern struct sk_buff * skb_copy_expand(const struct sk_buff *skb,
242 int newheadroom,
243 int newtailroom,
244 int priority);
245 extern struct sk_buff * skb_pad(struct sk_buff *skb, int pad);
246 #define dev_kfree_skb(a) kfree_skb(a)
247 extern void skb_over_panic(struct sk_buff *skb, int len, void *here);
248 extern void skb_under_panic(struct sk_buff *skb, int len, void *here);
250 /* Internal */
251 #define skb_shinfo(SKB) ((struct skb_shared_info *)((SKB)->end))
253 /**
254 * skb_queue_empty - check if a queue is empty
255 * @list: queue head
256 *
257 * Returns true if the queue is empty, false otherwise.
258 */
260 static inline int skb_queue_empty(struct sk_buff_head *list)
261 {
262 return (list->next == (struct sk_buff *) list);
263 }
265 /**
266 * skb_get - reference buffer
267 * @skb: buffer to reference
268 *
269 * Makes another reference to a socket buffer and returns a pointer
270 * to the buffer.
271 */
273 static inline struct sk_buff *skb_get(struct sk_buff *skb)
274 {
275 atomic_inc(&skb->users);
276 return skb;
277 }
279 /*
280 * If users==1, we are the only owner and are can avoid redundant
281 * atomic change.
282 */
284 /**
285 * kfree_skb - free an sk_buff
286 * @skb: buffer to free
287 *
288 * Drop a reference to the buffer and free it if the usage count has
289 * hit zero.
290 */
292 static inline void kfree_skb(struct sk_buff *skb)
293 {
294 if (atomic_read(&skb->users) == 1 || atomic_dec_and_test(&skb->users))
295 __kfree_skb(skb);
296 }
298 /* Use this if you didn't touch the skb state [for fast switching] */
299 static inline void kfree_skb_fast(struct sk_buff *skb)
300 {
301 if (atomic_read(&skb->users) == 1 || atomic_dec_and_test(&skb->users))
302 kfree_skbmem(skb);
303 }
305 /**
306 * skb_cloned - is the buffer a clone
307 * @skb: buffer to check
308 *
309 * Returns true if the buffer was generated with skb_clone() and is
310 * one of multiple shared copies of the buffer. Cloned buffers are
311 * shared data so must not be written to under normal circumstances.
312 */
314 static inline int skb_cloned(struct sk_buff *skb)
315 {
316 return skb->cloned && atomic_read(&skb_shinfo(skb)->dataref) != 1;
317 }
319 /**
320 * skb_shared - is the buffer shared
321 * @skb: buffer to check
322 *
323 * Returns true if more than one person has a reference to this
324 * buffer.
325 */
327 static inline int skb_shared(struct sk_buff *skb)
328 {
329 return (atomic_read(&skb->users) != 1);
330 }
332 /**
333 * skb_share_check - check if buffer is shared and if so clone it
334 * @skb: buffer to check
335 * @pri: priority for memory allocation
336 *
337 * If the buffer is shared the buffer is cloned and the old copy
338 * drops a reference. A new clone with a single reference is returned.
339 * If the buffer is not shared the original buffer is returned. When
340 * being called from interrupt status or with spinlocks held pri must
341 * be GFP_ATOMIC.
342 *
343 * NULL is returned on a memory allocation failure.
344 */
346 static inline struct sk_buff *skb_share_check(struct sk_buff *skb, int pri)
347 {
348 if (skb_shared(skb)) {
349 struct sk_buff *nskb;
350 nskb = skb_clone(skb, pri);
351 kfree_skb(skb);
352 return nskb;
353 }
354 return skb;
355 }
358 /*
359 * Copy shared buffers into a new sk_buff. We effectively do COW on
360 * packets to handle cases where we have a local reader and forward
361 * and a couple of other messy ones. The normal one is tcpdumping
362 * a packet thats being forwarded.
363 */
365 /**
366 * skb_unshare - make a copy of a shared buffer
367 * @skb: buffer to check
368 * @pri: priority for memory allocation
369 *
370 * If the socket buffer is a clone then this function creates a new
371 * copy of the data, drops a reference count on the old copy and returns
372 * the new copy with the reference count at 1. If the buffer is not a clone
373 * the original buffer is returned. When called with a spinlock held or
374 * from interrupt state @pri must be %GFP_ATOMIC
375 *
376 * %NULL is returned on a memory allocation failure.
377 */
379 static inline struct sk_buff *skb_unshare(struct sk_buff *skb, int pri)
380 {
381 struct sk_buff *nskb;
382 if(!skb_cloned(skb))
383 return skb;
384 nskb=skb_copy(skb, pri);
385 kfree_skb(skb); /* Free our shared copy */
386 return nskb;
387 }
389 /**
390 * skb_peek
391 * @list_: list to peek at
392 *
393 * Peek an &sk_buff. Unlike most other operations you _MUST_
394 * be careful with this one. A peek leaves the buffer on the
395 * list and someone else may run off with it. You must hold
396 * the appropriate locks or have a private queue to do this.
397 *
398 * Returns %NULL for an empty list or a pointer to the head element.
399 * The reference count is not incremented and the reference is therefore
400 * volatile. Use with caution.
401 */
403 static inline struct sk_buff *skb_peek(struct sk_buff_head *list_)
404 {
405 struct sk_buff *list = ((struct sk_buff *)list_)->next;
406 if (list == (struct sk_buff *)list_)
407 list = NULL;
408 return list;
409 }
411 /**
412 * skb_peek_tail
413 * @list_: list to peek at
414 *
415 * Peek an &sk_buff. Unlike most other operations you _MUST_
416 * be careful with this one. A peek leaves the buffer on the
417 * list and someone else may run off with it. You must hold
418 * the appropriate locks or have a private queue to do this.
419 *
420 * Returns %NULL for an empty list or a pointer to the tail element.
421 * The reference count is not incremented and the reference is therefore
422 * volatile. Use with caution.
423 */
425 static inline struct sk_buff *skb_peek_tail(struct sk_buff_head *list_)
426 {
427 struct sk_buff *list = ((struct sk_buff *)list_)->prev;
428 if (list == (struct sk_buff *)list_)
429 list = NULL;
430 return list;
431 }
433 /**
434 * skb_queue_len - get queue length
435 * @list_: list to measure
436 *
437 * Return the length of an &sk_buff queue.
438 */
440 static inline __u32 skb_queue_len(struct sk_buff_head *list_)
441 {
442 return(list_->qlen);
443 }
445 static inline void skb_queue_head_init(struct sk_buff_head *list)
446 {
447 spin_lock_init(&list->lock);
448 list->prev = (struct sk_buff *)list;
449 list->next = (struct sk_buff *)list;
450 list->qlen = 0;
451 }
453 /*
454 * Insert an sk_buff at the start of a list.
455 *
456 * The "__skb_xxxx()" functions are the non-atomic ones that
457 * can only be called with interrupts disabled.
458 */
460 /**
461 * __skb_queue_head - queue a buffer at the list head
462 * @list: list to use
463 * @newsk: buffer to queue
464 *
465 * Queue a buffer at the start of a list. This function takes no locks
466 * and you must therefore hold required locks before calling it.
467 *
468 * A buffer cannot be placed on two lists at the same time.
469 */
471 static inline void __skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
472 {
473 struct sk_buff *prev, *next;
475 newsk->list = list;
476 list->qlen++;
477 prev = (struct sk_buff *)list;
478 next = prev->next;
479 newsk->next = next;
480 newsk->prev = prev;
481 next->prev = newsk;
482 prev->next = newsk;
483 }
486 /**
487 * skb_queue_head - queue a buffer at the list head
488 * @list: list to use
489 * @newsk: buffer to queue
490 *
491 * Queue a buffer at the start of the list. This function takes the
492 * list lock and can be used safely with other locking &sk_buff functions
493 * safely.
494 *
495 * A buffer cannot be placed on two lists at the same time.
496 */
498 static inline void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
499 {
500 unsigned long flags;
502 spin_lock_irqsave(&list->lock, flags);
503 __skb_queue_head(list, newsk);
504 spin_unlock_irqrestore(&list->lock, flags);
505 }
507 /**
508 * __skb_queue_tail - queue a buffer at the list tail
509 * @list: list to use
510 * @newsk: buffer to queue
511 *
512 * Queue a buffer at the end of a list. This function takes no locks
513 * and you must therefore hold required locks before calling it.
514 *
515 * A buffer cannot be placed on two lists at the same time.
516 */
519 static inline void __skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
520 {
521 struct sk_buff *prev, *next;
523 newsk->list = list;
524 list->qlen++;
525 next = (struct sk_buff *)list;
526 prev = next->prev;
527 newsk->next = next;
528 newsk->prev = prev;
529 next->prev = newsk;
530 prev->next = newsk;
531 }
533 /**
534 * skb_queue_tail - queue a buffer at the list tail
535 * @list: list to use
536 * @newsk: buffer to queue
537 *
538 * Queue a buffer at the tail of the list. This function takes the
539 * list lock and can be used safely with other locking &sk_buff functions
540 * safely.
541 *
542 * A buffer cannot be placed on two lists at the same time.
543 */
545 static inline void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
546 {
547 unsigned long flags;
549 spin_lock_irqsave(&list->lock, flags);
550 __skb_queue_tail(list, newsk);
551 spin_unlock_irqrestore(&list->lock, flags);
552 }
554 /**
555 * __skb_dequeue - remove from the head of the queue
556 * @list: list to dequeue from
557 *
558 * Remove the head of the list. This function does not take any locks
559 * so must be used with appropriate locks held only. The head item is
560 * returned or %NULL if the list is empty.
561 */
563 static inline struct sk_buff *__skb_dequeue(struct sk_buff_head *list)
564 {
565 struct sk_buff *next, *prev, *result;
567 prev = (struct sk_buff *) list;
568 next = prev->next;
569 result = NULL;
570 if (next != prev) {
571 result = next;
572 next = next->next;
573 list->qlen--;
574 next->prev = prev;
575 prev->next = next;
576 result->next = NULL;
577 result->prev = NULL;
578 result->list = NULL;
579 }
580 return result;
581 }
583 /**
584 * skb_dequeue - remove from the head of the queue
585 * @list: list to dequeue from
586 *
587 * Remove the head of the list. The list lock is taken so the function
588 * may be used safely with other locking list functions. The head item is
589 * returned or %NULL if the list is empty.
590 */
592 static inline struct sk_buff *skb_dequeue(struct sk_buff_head *list)
593 {
594 unsigned long flags;
595 struct sk_buff *result;
597 spin_lock_irqsave(&list->lock, flags);
598 result = __skb_dequeue(list);
599 spin_unlock_irqrestore(&list->lock, flags);
600 return result;
601 }
603 /*
604 * Insert a packet on a list.
605 */
607 static inline void __skb_insert(struct sk_buff *newsk,
608 struct sk_buff * prev, struct sk_buff *next,
609 struct sk_buff_head * list)
610 {
611 newsk->next = next;
612 newsk->prev = prev;
613 next->prev = newsk;
614 prev->next = newsk;
615 newsk->list = list;
616 list->qlen++;
617 }
619 /**
620 * skb_insert - insert a buffer
621 * @old: buffer to insert before
622 * @newsk: buffer to insert
623 *
624 * Place a packet before a given packet in a list. The list locks are taken
625 * and this function is atomic with respect to other list locked calls
626 * A buffer cannot be placed on two lists at the same time.
627 */
629 static inline void skb_insert(struct sk_buff *old, struct sk_buff *newsk)
630 {
631 unsigned long flags;
633 spin_lock_irqsave(&old->list->lock, flags);
634 __skb_insert(newsk, old->prev, old, old->list);
635 spin_unlock_irqrestore(&old->list->lock, flags);
636 }
638 /*
639 * Place a packet after a given packet in a list.
640 */
642 static inline void __skb_append(struct sk_buff *old, struct sk_buff *newsk)
643 {
644 __skb_insert(newsk, old, old->next, old->list);
645 }
647 /**
648 * skb_append - append a buffer
649 * @old: buffer to insert after
650 * @newsk: buffer to insert
651 *
652 * Place a packet after a given packet in a list. The list locks are taken
653 * and this function is atomic with respect to other list locked calls.
654 * A buffer cannot be placed on two lists at the same time.
655 */
658 static inline void skb_append(struct sk_buff *old, struct sk_buff *newsk)
659 {
660 unsigned long flags;
662 spin_lock_irqsave(&old->list->lock, flags);
663 __skb_append(old, newsk);
664 spin_unlock_irqrestore(&old->list->lock, flags);
665 }
667 /*
668 * remove sk_buff from list. _Must_ be called atomically, and with
669 * the list known..
670 */
672 static inline void __skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
673 {
674 struct sk_buff * next, * prev;
676 list->qlen--;
677 next = skb->next;
678 prev = skb->prev;
679 skb->next = NULL;
680 skb->prev = NULL;
681 skb->list = NULL;
682 next->prev = prev;
683 prev->next = next;
684 }
686 /**
687 * skb_unlink - remove a buffer from a list
688 * @skb: buffer to remove
689 *
690 * Place a packet after a given packet in a list. The list locks are taken
691 * and this function is atomic with respect to other list locked calls
692 *
693 * Works even without knowing the list it is sitting on, which can be
694 * handy at times. It also means that THE LIST MUST EXIST when you
695 * unlink. Thus a list must have its contents unlinked before it is
696 * destroyed.
697 */
699 static inline void skb_unlink(struct sk_buff *skb)
700 {
701 struct sk_buff_head *list = skb->list;
703 if(list) {
704 unsigned long flags;
706 spin_lock_irqsave(&list->lock, flags);
707 if(skb->list == list)
708 __skb_unlink(skb, skb->list);
709 spin_unlock_irqrestore(&list->lock, flags);
710 }
711 }
713 /* XXX: more streamlined implementation */
715 /**
716 * __skb_dequeue_tail - remove from the tail of the queue
717 * @list: list to dequeue from
718 *
719 * Remove the tail of the list. This function does not take any locks
720 * so must be used with appropriate locks held only. The tail item is
721 * returned or %NULL if the list is empty.
722 */
724 static inline struct sk_buff *__skb_dequeue_tail(struct sk_buff_head *list)
725 {
726 struct sk_buff *skb = skb_peek_tail(list);
727 if (skb)
728 __skb_unlink(skb, list);
729 return skb;
730 }
732 /**
733 * skb_dequeue - remove from the head of the queue
734 * @list: list to dequeue from
735 *
736 * Remove the head of the list. The list lock is taken so the function
737 * may be used safely with other locking list functions. The tail item is
738 * returned or %NULL if the list is empty.
739 */
741 static inline struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
742 {
743 unsigned long flags;
744 struct sk_buff *result;
746 spin_lock_irqsave(&list->lock, flags);
747 result = __skb_dequeue_tail(list);
748 spin_unlock_irqrestore(&list->lock, flags);
749 return result;
750 }
752 static inline int skb_is_nonlinear(const struct sk_buff *skb)
753 {
754 return skb->data_len;
755 }
757 static inline unsigned int skb_headlen(const struct sk_buff *skb)
758 {
759 return skb->len - skb->data_len;
760 }
762 #define SKB_PAGE_ASSERT(skb) do { if (skb_shinfo(skb)->nr_frags) out_of_line_bug(); } while (0)
763 #define SKB_FRAG_ASSERT(skb) do { if (skb_shinfo(skb)->frag_list) out_of_line_bug(); } while (0)
764 #define SKB_LINEAR_ASSERT(skb) do { if (skb_is_nonlinear(skb)) out_of_line_bug(); } while (0)
766 /*
767 * Add data to an sk_buff
768 */
770 static inline unsigned char *__skb_put(struct sk_buff *skb, unsigned int len)
771 {
772 unsigned char *tmp=skb->tail;
773 SKB_LINEAR_ASSERT(skb);
774 skb->tail+=len;
775 skb->len+=len;
776 return tmp;
777 }
779 /**
780 * skb_put - add data to a buffer
781 * @skb: buffer to use
782 * @len: amount of data to add
783 *
784 * This function extends the used data area of the buffer. If this would
785 * exceed the total buffer size the kernel will panic. A pointer to the
786 * first byte of the extra data is returned.
787 */
789 static inline unsigned char *skb_put(struct sk_buff *skb, unsigned int len)
790 {
791 unsigned char *tmp=skb->tail;
792 SKB_LINEAR_ASSERT(skb);
793 skb->tail+=len;
794 skb->len+=len;
795 if(skb->tail>skb->end) {
796 skb_over_panic(skb, len, current_text_addr());
797 }
798 return tmp;
799 }
801 static inline unsigned char *__skb_push(struct sk_buff *skb, unsigned int len)
802 {
803 skb->data-=len;
804 skb->len+=len;
805 return skb->data;
806 }
808 /**
809 * skb_push - add data to the start of a buffer
810 * @skb: buffer to use
811 * @len: amount of data to add
812 *
813 * This function extends the used data area of the buffer at the buffer
814 * start. If this would exceed the total buffer headroom the kernel will
815 * panic. A pointer to the first byte of the extra data is returned.
816 */
818 static inline unsigned char *skb_push(struct sk_buff *skb, unsigned int len)
819 {
820 skb->data-=len;
821 skb->len+=len;
822 if(skb->data<skb->head) {
823 skb_under_panic(skb, len, current_text_addr());
824 }
825 return skb->data;
826 }
828 static inline char *__skb_pull(struct sk_buff *skb, unsigned int len)
829 {
830 skb->len-=len;
831 if (skb->len < skb->data_len)
832 out_of_line_bug();
833 return skb->data+=len;
834 }
836 /**
837 * skb_pull - remove data from the start of a buffer
838 * @skb: buffer to use
839 * @len: amount of data to remove
840 *
841 * This function removes data from the start of a buffer, returning
842 * the memory to the headroom. A pointer to the next data in the buffer
843 * is returned. Once the data has been pulled future pushes will overwrite
844 * the old data.
845 */
847 static inline unsigned char * skb_pull(struct sk_buff *skb, unsigned int len)
848 {
849 if (len > skb->len)
850 return NULL;
851 return __skb_pull(skb,len);
852 }
854 extern unsigned char * __pskb_pull_tail(struct sk_buff *skb, int delta);
856 static inline char *__pskb_pull(struct sk_buff *skb, unsigned int len)
857 {
858 if (len > skb_headlen(skb) &&
859 __pskb_pull_tail(skb, len-skb_headlen(skb)) == NULL)
860 return NULL;
861 skb->len -= len;
862 return skb->data += len;
863 }
865 static inline unsigned char * pskb_pull(struct sk_buff *skb, unsigned int len)
866 {
867 if (len > skb->len)
868 return NULL;
869 return __pskb_pull(skb,len);
870 }
872 static inline int pskb_may_pull(struct sk_buff *skb, unsigned int len)
873 {
874 if (len <= skb_headlen(skb))
875 return 1;
876 if (len > skb->len)
877 return 0;
878 return (__pskb_pull_tail(skb, len-skb_headlen(skb)) != NULL);
879 }
881 /**
882 * skb_headroom - bytes at buffer head
883 * @skb: buffer to check
884 *
885 * Return the number of bytes of free space at the head of an &sk_buff.
886 */
888 static inline int skb_headroom(const struct sk_buff *skb)
889 {
890 return skb->data-skb->head;
891 }
893 /**
894 * skb_tailroom - bytes at buffer end
895 * @skb: buffer to check
896 *
897 * Return the number of bytes of free space at the tail of an sk_buff
898 */
900 static inline int skb_tailroom(const struct sk_buff *skb)
901 {
902 return skb_is_nonlinear(skb) ? 0 : skb->end-skb->tail;
903 }
905 /**
906 * skb_reserve - adjust headroom
907 * @skb: buffer to alter
908 * @len: bytes to move
909 *
910 * Increase the headroom of an empty &sk_buff by reducing the tail
911 * room. This is only allowed for an empty buffer.
912 */
914 static inline void skb_reserve(struct sk_buff *skb, unsigned int len)
915 {
916 skb->data+=len;
917 skb->tail+=len;
918 }
920 extern int ___pskb_trim(struct sk_buff *skb, unsigned int len, int realloc);
922 static inline void __skb_trim(struct sk_buff *skb, unsigned int len)
923 {
924 if (!skb->data_len) {
925 skb->len = len;
926 skb->tail = skb->data+len;
927 } else {
928 ___pskb_trim(skb, len, 0);
929 }
930 }
932 /**
933 * skb_trim - remove end from a buffer
934 * @skb: buffer to alter
935 * @len: new length
936 *
937 * Cut the length of a buffer down by removing data from the tail. If
938 * the buffer is already under the length specified it is not modified.
939 */
941 static inline void skb_trim(struct sk_buff *skb, unsigned int len)
942 {
943 if (skb->len > len) {
944 __skb_trim(skb, len);
945 }
946 }
949 static inline int __pskb_trim(struct sk_buff *skb, unsigned int len)
950 {
951 if (!skb->data_len) {
952 skb->len = len;
953 skb->tail = skb->data+len;
954 return 0;
955 } else {
956 return ___pskb_trim(skb, len, 1);
957 }
958 }
960 static inline int pskb_trim(struct sk_buff *skb, unsigned int len)
961 {
962 if (len < skb->len)
963 return __pskb_trim(skb, len);
964 return 0;
965 }
967 /**
968 * skb_orphan - orphan a buffer
969 * @skb: buffer to orphan
970 *
971 * If a buffer currently has an owner then we call the owner's
972 * destructor function and make the @skb unowned. The buffer continues
973 * to exist but is no longer charged to its former owner.
974 */
977 static inline void skb_orphan(struct sk_buff *skb)
978 {
979 if (skb->destructor)
980 skb->destructor(skb);
981 skb->destructor = NULL;
982 skb->sk = NULL;
983 }
985 /**
986 * skb_purge - empty a list
987 * @list: list to empty
988 *
989 * Delete all buffers on an &sk_buff list. Each buffer is removed from
990 * the list and one reference dropped. This function takes the list
991 * lock and is atomic with respect to other list locking functions.
992 */
995 static inline void skb_queue_purge(struct sk_buff_head *list)
996 {
997 struct sk_buff *skb;
998 while ((skb=skb_dequeue(list))!=NULL)
999 kfree_skb(skb);
1002 /**
1003 * __skb_purge - empty a list
1004 * @list: list to empty
1006 * Delete all buffers on an &sk_buff list. Each buffer is removed from
1007 * the list and one reference dropped. This function does not take the
1008 * list lock and the caller must hold the relevant locks to use it.
1009 */
1012 static inline void __skb_queue_purge(struct sk_buff_head *list)
1014 struct sk_buff *skb;
1015 while ((skb=__skb_dequeue(list))!=NULL)
1016 kfree_skb(skb);
1019 /**
1020 * __dev_alloc_skb - allocate an skbuff for sending
1021 * @length: length to allocate
1022 * @gfp_mask: get_free_pages mask, passed to alloc_skb
1024 * Allocate a new &sk_buff and assign it a usage count of one. The
1025 * buffer has unspecified headroom built in. Users should allocate
1026 * the headroom they think they need without accounting for the
1027 * built in space. The built in space is used for optimisations.
1029 * %NULL is returned in there is no free memory.
1030 */
1031 #ifndef CONFIG_XEN
1032 static inline struct sk_buff *__dev_alloc_skb(unsigned int length,
1033 int gfp_mask)
1035 struct sk_buff *skb = alloc_skb(length+16, gfp_mask);
1036 if (skb)
1037 skb_reserve(skb,16);
1038 return skb;
1040 #else
1041 extern struct sk_buff *__dev_alloc_skb(unsigned int length, int gfp_mask);
1042 #endif
1044 /**
1045 * dev_alloc_skb - allocate an skbuff for sending
1046 * @length: length to allocate
1048 * Allocate a new &sk_buff and assign it a usage count of one. The
1049 * buffer has unspecified headroom built in. Users should allocate
1050 * the headroom they think they need without accounting for the
1051 * built in space. The built in space is used for optimisations.
1053 * %NULL is returned in there is no free memory. Although this function
1054 * allocates memory it can be called from an interrupt.
1055 */
1057 static inline struct sk_buff *dev_alloc_skb(unsigned int length)
1059 return __dev_alloc_skb(length, GFP_ATOMIC);
1062 /**
1063 * skb_cow - copy header of skb when it is required
1064 * @skb: buffer to cow
1065 * @headroom: needed headroom
1067 * If the skb passed lacks sufficient headroom or its data part
1068 * is shared, data is reallocated. If reallocation fails, an error
1069 * is returned and original skb is not changed.
1071 * The result is skb with writable area skb->head...skb->tail
1072 * and at least @headroom of space at head.
1073 */
1075 static inline int
1076 skb_cow(struct sk_buff *skb, unsigned int headroom)
1078 int delta = (headroom > 16 ? headroom : 16) - skb_headroom(skb);
1080 if (delta < 0)
1081 delta = 0;
1083 if (delta || skb_cloned(skb))
1084 return pskb_expand_head(skb, (delta+15)&~15, 0, GFP_ATOMIC);
1085 return 0;
1088 /**
1089 * skb_padto - pad an skbuff up to a minimal size
1090 * @skb: buffer to pad
1091 * @len: minimal length
1093 * Pads up a buffer to ensure the trailing bytes exist and are
1094 * blanked. If the buffer already contains sufficient data it
1095 * is untouched. Returns the buffer, which may be a replacement
1096 * for the original, or NULL for out of memory - in which case
1097 * the original buffer is still freed.
1098 */
1100 static inline struct sk_buff *skb_padto(struct sk_buff *skb, unsigned int len)
1102 unsigned int size = skb->len;
1103 if(likely(size >= len))
1104 return skb;
1105 return skb_pad(skb, len-size);
1108 /**
1109 * skb_linearize - convert paged skb to linear one
1110 * @skb: buffer to linarize
1111 * @gfp: allocation mode
1113 * If there is no free memory -ENOMEM is returned, otherwise zero
1114 * is returned and the old skb data released. */
1115 int skb_linearize(struct sk_buff *skb, int gfp);
1117 static inline void *kmap_skb_frag(const skb_frag_t *frag)
1119 #ifdef CONFIG_HIGHMEM
1120 if (in_irq())
1121 out_of_line_bug();
1123 local_bh_disable();
1124 #endif
1125 return kmap_atomic(frag->page, KM_SKB_DATA_SOFTIRQ);
1128 static inline void kunmap_skb_frag(void *vaddr)
1130 kunmap_atomic(vaddr, KM_SKB_DATA_SOFTIRQ);
1131 #ifdef CONFIG_HIGHMEM
1132 local_bh_enable();
1133 #endif
1136 #define skb_queue_walk(queue, skb) \
1137 for (skb = (queue)->next; \
1138 (skb != (struct sk_buff *)(queue)); \
1139 skb=skb->next)
1142 extern struct sk_buff * skb_recv_datagram(struct sock *sk,unsigned flags,int noblock, int *err);
1143 extern unsigned int datagram_poll(struct file *file, struct socket *sock, struct poll_table_struct *wait);
1144 extern int skb_copy_datagram(const struct sk_buff *from, int offset, char *to,int size);
1145 extern int skb_copy_datagram_iovec(const struct sk_buff *from, int offset, struct iovec *to,int size);
1146 extern int skb_copy_and_csum_datagram(const struct sk_buff *skb, int offset, u8 *to, int len, unsigned int *csump);
1147 extern int skb_copy_and_csum_datagram_iovec(const struct sk_buff *skb, int hlen, struct iovec *iov);
1148 extern void skb_free_datagram(struct sock * sk, struct sk_buff *skb);
1150 extern unsigned int skb_checksum(const struct sk_buff *skb, int offset, int len, unsigned int csum);
1151 extern int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len);
1152 extern unsigned int skb_copy_and_csum_bits(const struct sk_buff *skb, int offset, u8 *to, int len, unsigned int csum);
1153 extern void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to);
1155 extern void skb_init(void);
1156 extern void skb_add_mtu(int mtu);
1158 #ifdef CONFIG_NETFILTER
1159 static inline void
1160 nf_conntrack_put(struct nf_ct_info *nfct)
1162 if (nfct && atomic_dec_and_test(&nfct->master->use))
1163 nfct->master->destroy(nfct->master);
1165 static inline void
1166 nf_conntrack_get(struct nf_ct_info *nfct)
1168 if (nfct)
1169 atomic_inc(&nfct->master->use);
1171 static inline void
1172 nf_reset(struct sk_buff *skb)
1174 nf_conntrack_put(skb->nfct);
1175 skb->nfct = NULL;
1176 #ifdef CONFIG_NETFILTER_DEBUG
1177 skb->nf_debug = 0;
1178 #endif
1180 #else /* CONFIG_NETFILTER */
1181 static inline void nf_reset(struct sk_buff *skb) {}
1182 #endif /* CONFIG_NETFILTER */
1184 #endif /* __KERNEL__ */
1185 #endif /* _LINUX_SKBUFF_H */