patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / include / linux / skbuff.h
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  */
13
14 #ifndef _LINUX_SKBUFF_H
15 #define _LINUX_SKBUFF_H
16
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>
22
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>
30
31 #define HAVE_ALLOC_SKB          /* For the drivers to know */
32 #define HAVE_ALIGNABLE_SKB      /* Ditto 8)                */
33 #define SLAB_SKB                /* Slabified skbuffs       */
34
35 #define CHECKSUM_NONE 0
36 #define CHECKSUM_HW 1
37 #define CHECKSUM_UNNECESSARY 2
38
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))
46
47 /* A. Checksumming of received packets by device.
48  *
49  *      NONE: device failed to checksum this packet.
50  *              skb->csum is undefined.
51  *
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)
57  *
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.
63  *
64  * B. Checksumming on output.
65  *
66  *      NONE: skb is checksummed by protocol or csum is not required.
67  *
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.
71  *
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)
81  *
82  *      Any questions? No questions, good.              --ANK
83  */
84
85 #ifdef __i386__
86 #define NET_CALLER(arg) (*(((void **)&arg) - 1))
87 #else
88 #define NET_CALLER(arg) __builtin_return_address(0)
89 #endif
90
91 #ifdef CONFIG_NETFILTER
92 struct nf_conntrack {
93         atomic_t use;
94         void (*destroy)(struct nf_conntrack *);
95 };
96
97 struct nf_ct_info {
98         struct nf_conntrack *master;
99 };
100
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 #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
107         struct net_device *netoutdev;
108 #endif
109         unsigned int mask;
110         unsigned long data[32 / sizeof(unsigned long)];
111 };
112 #endif
113
114 #endif
115
116 struct sk_buff_head {
117         /* These two members must be first. */
118         struct sk_buff  *next;
119         struct sk_buff  *prev;
120
121         __u32           qlen;
122         spinlock_t      lock;
123 };
124
125 struct sk_buff;
126
127 /* To allow 64K frame to be packed as single skb without frag_list */
128 #define MAX_SKB_FRAGS (65536/PAGE_SIZE + 2)
129
130 typedef struct skb_frag_struct skb_frag_t;
131
132 struct skb_frag_struct {
133         struct page *page;
134         __u16 page_offset;
135         __u16 size;
136 };
137
138 /* This data is invariant across clones and lives at
139  * the end of the header data, ie. at skb->end.
140  */
141 struct skb_shared_info {
142         atomic_t        dataref;
143         unsigned int    nr_frags;
144         unsigned short  tso_size;
145         unsigned short  tso_segs;
146         struct sk_buff  *frag_list;
147         skb_frag_t      frags[MAX_SKB_FRAGS];
148 };
149
150 /** 
151  *      struct sk_buff - socket buffer
152  *      @next: Next buffer in list
153  *      @prev: Previous buffer in list
154  *      @list: List we are on
155  *      @sk: Socket we are owned by
156  *      @stamp: Time we arrived
157  *      @dev: Device we arrived on/are leaving by
158  *      @real_dev: The real device we are using
159  *      @h: Transport layer header
160  *      @nh: Network layer header
161  *      @mac: Link layer header
162  *      @dst: FIXME: Describe this field
163  *      @cb: Control buffer. Free for use by every layer. Put private vars here
164  *      @len: Length of actual data
165  *      @data_len: Data length
166  *      @mac_len: Length of link layer header
167  *      @csum: Checksum
168  *      @__unused: Dead field, may be reused
169  *      @cloned: Head may be cloned (check refcnt to be sure)
170  *      @pkt_type: Packet class
171  *      @ip_summed: Driver fed us an IP checksum
172  *      @priority: Packet queueing priority
173  *      @users: User count - see {datagram,tcp}.c
174  *      @protocol: Packet protocol from driver
175  *      @security: Security level of packet
176  *      @truesize: Buffer size 
177  *      @head: Head of buffer
178  *      @data: Data head pointer
179  *      @tail: Tail pointer
180  *      @end: End pointer
181  *      @destructor: Destruct function
182  *      @nfmark: Can be used for communication between hooks
183  *      @nfcache: Cache info
184  *      @nfct: Associated connection, if any
185  *      @nf_debug: Netfilter debugging
186  *      @nf_bridge: Saved data about a bridged frame - see br_netfilter.c
187  *      @private: Data which is private to the HIPPI implementation
188  *      @tc_index: Traffic control index
189  */
190
191 struct sk_buff {
192         /* These two members must be first. */
193         struct sk_buff          *next;
194         struct sk_buff          *prev;
195
196         struct sk_buff_head     *list;
197         struct sock             *sk;
198         struct timeval          stamp;
199         struct net_device       *dev;
200         struct net_device       *real_dev;
201
202         union {
203                 struct tcphdr   *th;
204                 struct udphdr   *uh;
205                 struct icmphdr  *icmph;
206                 struct igmphdr  *igmph;
207                 struct iphdr    *ipiph;
208                 struct ipv6hdr  *ipv6h;
209                 unsigned char   *raw;
210         } h;
211
212         union {
213                 struct iphdr    *iph;
214                 struct ipv6hdr  *ipv6h;
215                 struct arphdr   *arph;
216                 unsigned char   *raw;
217         } nh;
218
219         union {
220                 struct ethhdr   *ethernet;
221                 unsigned char   *raw;
222         } mac;
223
224         struct  dst_entry       *dst;
225         struct  sec_path        *sp;
226
227         /*
228          * This is the control buffer. It is free to use for every
229          * layer. Please put your private variables there. If you
230          * want to keep them across layers you have to do a skb_clone()
231          * first. This is owned by whoever has the skb queued ATM.
232          */
233         char                    cb[48];
234
235         unsigned int            len,
236                                 data_len,
237                                 mac_len,
238                                 csum;
239         unsigned char           local_df,
240                                 cloned,
241                                 pkt_type,
242                                 ip_summed;
243         __u32                   priority;
244         unsigned short          protocol,
245                                 security;
246
247         void                    (*destructor)(struct sk_buff *skb);
248 #ifdef CONFIG_NETFILTER
249         unsigned long           nfmark;
250         __u32                   nfcache;
251         struct nf_ct_info       *nfct;
252 #ifdef CONFIG_NETFILTER_DEBUG
253         unsigned int            nf_debug;
254 #endif
255 #ifdef CONFIG_BRIDGE_NETFILTER
256         struct nf_bridge_info   *nf_bridge;
257 #endif
258 #endif /* CONFIG_NETFILTER */
259 #if defined(CONFIG_HIPPI)
260         union {
261                 __u32           ifield;
262         } private;
263 #endif
264 #ifdef CONFIG_NET_SCHED
265        __u32                    tc_index;               /* traffic control index */
266 #endif
267
268         /* These elements must be at the end, see alloc_skb() for details.  */
269         unsigned int            truesize;
270         atomic_t                users;
271         unsigned char           *head,
272                                 *data,
273                                 *tail,
274                                 *end;
275 };
276
277 #ifdef __KERNEL__
278 /*
279  *      Handling routines are only of interest to the kernel
280  */
281 #include <linux/slab.h>
282
283 #include <asm/system.h>
284
285 extern void            __kfree_skb(struct sk_buff *skb);
286 extern struct sk_buff *alloc_skb(unsigned int size, int priority);
287 extern void            kfree_skbmem(struct sk_buff *skb);
288 extern struct sk_buff *skb_clone(struct sk_buff *skb, int priority);
289 extern struct sk_buff *skb_copy(const struct sk_buff *skb, int priority);
290 extern struct sk_buff *pskb_copy(struct sk_buff *skb, int gfp_mask);
291 extern int             pskb_expand_head(struct sk_buff *skb,
292                                         int nhead, int ntail, int gfp_mask);
293 extern struct sk_buff *skb_realloc_headroom(struct sk_buff *skb,
294                                             unsigned int headroom);
295 extern struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
296                                        int newheadroom, int newtailroom,
297                                        int priority);
298 extern struct sk_buff *         skb_pad(struct sk_buff *skb, int pad);
299 #define dev_kfree_skb(a)        kfree_skb(a)
300 extern void           skb_over_panic(struct sk_buff *skb, int len,
301                                      void *here);
302 extern void           skb_under_panic(struct sk_buff *skb, int len,
303                                       void *here);
304
305 /* Internal */
306 #define skb_shinfo(SKB)         ((struct skb_shared_info *)((SKB)->end))
307
308 /**
309  *      skb_queue_empty - check if a queue is empty
310  *      @list: queue head
311  *
312  *      Returns true if the queue is empty, false otherwise.
313  */
314 static inline int skb_queue_empty(const struct sk_buff_head *list)
315 {
316         return list->next == (struct sk_buff *)list;
317 }
318
319 /**
320  *      skb_get - reference buffer
321  *      @skb: buffer to reference
322  *
323  *      Makes another reference to a socket buffer and returns a pointer
324  *      to the buffer.
325  */
326 static inline struct sk_buff *skb_get(struct sk_buff *skb)
327 {
328         atomic_inc(&skb->users);
329         return skb;
330 }
331
332 /*
333  * If users == 1, we are the only owner and are can avoid redundant
334  * atomic change.
335  */
336
337 /**
338  *      kfree_skb - free an sk_buff
339  *      @skb: buffer to free
340  *
341  *      Drop a reference to the buffer and free it if the usage count has
342  *      hit zero.
343  */
344 static inline void kfree_skb(struct sk_buff *skb)
345 {
346         if (atomic_read(&skb->users) == 1 || atomic_dec_and_test(&skb->users))
347                 __kfree_skb(skb);
348 }
349
350 /* Use this if you didn't touch the skb state [for fast switching] */
351 static inline void kfree_skb_fast(struct sk_buff *skb)
352 {
353         if (atomic_read(&skb->users) == 1 || atomic_dec_and_test(&skb->users))
354                 kfree_skbmem(skb);
355 }
356
357 /**
358  *      skb_cloned - is the buffer a clone
359  *      @skb: buffer to check
360  *
361  *      Returns true if the buffer was generated with skb_clone() and is
362  *      one of multiple shared copies of the buffer. Cloned buffers are
363  *      shared data so must not be written to under normal circumstances.
364  */
365 static inline int skb_cloned(const struct sk_buff *skb)
366 {
367         return skb->cloned && atomic_read(&skb_shinfo(skb)->dataref) != 1;
368 }
369
370 /**
371  *      skb_shared - is the buffer shared
372  *      @skb: buffer to check
373  *
374  *      Returns true if more than one person has a reference to this
375  *      buffer.
376  */
377 static inline int skb_shared(const struct sk_buff *skb)
378 {
379         return atomic_read(&skb->users) != 1;
380 }
381
382 /**
383  *      skb_share_check - check if buffer is shared and if so clone it
384  *      @skb: buffer to check
385  *      @pri: priority for memory allocation
386  *
387  *      If the buffer is shared the buffer is cloned and the old copy
388  *      drops a reference. A new clone with a single reference is returned.
389  *      If the buffer is not shared the original buffer is returned. When
390  *      being called from interrupt status or with spinlocks held pri must
391  *      be GFP_ATOMIC.
392  *
393  *      NULL is returned on a memory allocation failure.
394  */
395 static inline struct sk_buff *skb_share_check(struct sk_buff *skb, int pri)
396 {
397         might_sleep_if(pri & __GFP_WAIT);
398         if (skb_shared(skb)) {
399                 struct sk_buff *nskb = skb_clone(skb, pri);
400                 kfree_skb(skb);
401                 skb = nskb;
402         }
403         return skb;
404 }
405
406 /*
407  *      Copy shared buffers into a new sk_buff. We effectively do COW on
408  *      packets to handle cases where we have a local reader and forward
409  *      and a couple of other messy ones. The normal one is tcpdumping
410  *      a packet thats being forwarded.
411  */
412
413 /**
414  *      skb_unshare - make a copy of a shared buffer
415  *      @skb: buffer to check
416  *      @pri: priority for memory allocation
417  *
418  *      If the socket buffer is a clone then this function creates a new
419  *      copy of the data, drops a reference count on the old copy and returns
420  *      the new copy with the reference count at 1. If the buffer is not a clone
421  *      the original buffer is returned. When called with a spinlock held or
422  *      from interrupt state @pri must be %GFP_ATOMIC
423  *
424  *      %NULL is returned on a memory allocation failure.
425  */
426 static inline struct sk_buff *skb_unshare(struct sk_buff *skb, int pri)
427 {
428         might_sleep_if(pri & __GFP_WAIT);
429         if (skb_cloned(skb)) {
430                 struct sk_buff *nskb = skb_copy(skb, pri);
431                 kfree_skb(skb); /* Free our shared copy */
432                 skb = nskb;
433         }
434         return skb;
435 }
436
437 /**
438  *      skb_peek
439  *      @list_: list to peek at
440  *
441  *      Peek an &sk_buff. Unlike most other operations you _MUST_
442  *      be careful with this one. A peek leaves the buffer on the
443  *      list and someone else may run off with it. You must hold
444  *      the appropriate locks or have a private queue to do this.
445  *
446  *      Returns %NULL for an empty list or a pointer to the head element.
447  *      The reference count is not incremented and the reference is therefore
448  *      volatile. Use with caution.
449  */
450 static inline struct sk_buff *skb_peek(struct sk_buff_head *list_)
451 {
452         struct sk_buff *list = ((struct sk_buff *)list_)->next;
453         if (list == (struct sk_buff *)list_)
454                 list = NULL;
455         return list;
456 }
457
458 /**
459  *      skb_peek_tail
460  *      @list_: list to peek at
461  *
462  *      Peek an &sk_buff. Unlike most other operations you _MUST_
463  *      be careful with this one. A peek leaves the buffer on the
464  *      list and someone else may run off with it. You must hold
465  *      the appropriate locks or have a private queue to do this.
466  *
467  *      Returns %NULL for an empty list or a pointer to the tail element.
468  *      The reference count is not incremented and the reference is therefore
469  *      volatile. Use with caution.
470  */
471 static inline struct sk_buff *skb_peek_tail(struct sk_buff_head *list_)
472 {
473         struct sk_buff *list = ((struct sk_buff *)list_)->prev;
474         if (list == (struct sk_buff *)list_)
475                 list = NULL;
476         return list;
477 }
478
479 /**
480  *      skb_queue_len   - get queue length
481  *      @list_: list to measure
482  *
483  *      Return the length of an &sk_buff queue.
484  */
485 static inline __u32 skb_queue_len(const struct sk_buff_head *list_)
486 {
487         return list_->qlen;
488 }
489
490 static inline void skb_queue_head_init(struct sk_buff_head *list)
491 {
492         spin_lock_init(&list->lock);
493         list->prev = list->next = (struct sk_buff *)list;
494         list->qlen = 0;
495 }
496
497 /*
498  *      Insert an sk_buff at the start of a list.
499  *
500  *      The "__skb_xxxx()" functions are the non-atomic ones that
501  *      can only be called with interrupts disabled.
502  */
503
504 /**
505  *      __skb_queue_head - queue a buffer at the list head
506  *      @list: list to use
507  *      @newsk: buffer to queue
508  *
509  *      Queue a buffer at the start of a list. This function takes no locks
510  *      and you must therefore hold required locks before calling it.
511  *
512  *      A buffer cannot be placed on two lists at the same time.
513  */
514 extern void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk);
515 static inline void __skb_queue_head(struct sk_buff_head *list,
516                                     struct sk_buff *newsk)
517 {
518         struct sk_buff *prev, *next;
519
520         newsk->list = list;
521         list->qlen++;
522         prev = (struct sk_buff *)list;
523         next = prev->next;
524         newsk->next = next;
525         newsk->prev = prev;
526         next->prev  = prev->next = newsk;
527 }
528
529 /**
530  *      __skb_queue_tail - queue a buffer at the list tail
531  *      @list: list to use
532  *      @newsk: buffer to queue
533  *
534  *      Queue a buffer at the end of a list. This function takes no locks
535  *      and you must therefore hold required locks before calling it.
536  *
537  *      A buffer cannot be placed on two lists at the same time.
538  */
539 extern void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk);
540 static inline void __skb_queue_tail(struct sk_buff_head *list,
541                                    struct sk_buff *newsk)
542 {
543         struct sk_buff *prev, *next;
544
545         newsk->list = list;
546         list->qlen++;
547         next = (struct sk_buff *)list;
548         prev = next->prev;
549         newsk->next = next;
550         newsk->prev = prev;
551         next->prev  = prev->next = newsk;
552 }
553
554
555 /**
556  *      __skb_dequeue - remove from the head of the queue
557  *      @list: list to dequeue from
558  *
559  *      Remove the head of the list. This function does not take any locks
560  *      so must be used with appropriate locks held only. The head item is
561  *      returned or %NULL if the list is empty.
562  */
563 extern struct sk_buff *skb_dequeue(struct sk_buff_head *list);
564 static inline struct sk_buff *__skb_dequeue(struct sk_buff_head *list)
565 {
566         struct sk_buff *next, *prev, *result;
567
568         prev = (struct sk_buff *) list;
569         next = prev->next;
570         result = NULL;
571         if (next != prev) {
572                 result       = next;
573                 next         = next->next;
574                 list->qlen--;
575                 next->prev   = prev;
576                 prev->next   = next;
577                 result->next = result->prev = NULL;
578                 result->list = NULL;
579         }
580         return result;
581 }
582
583
584 /*
585  *      Insert a packet on a list.
586  */
587 extern void        skb_insert(struct sk_buff *old, struct sk_buff *newsk);
588 static inline void __skb_insert(struct sk_buff *newsk,
589                                 struct sk_buff *prev, struct sk_buff *next,
590                                 struct sk_buff_head *list)
591 {
592         newsk->next = next;
593         newsk->prev = prev;
594         next->prev  = prev->next = newsk;
595         newsk->list = list;
596         list->qlen++;
597 }
598
599 /*
600  *      Place a packet after a given packet in a list.
601  */
602 extern void        skb_append(struct sk_buff *old, struct sk_buff *newsk);
603 static inline void __skb_append(struct sk_buff *old, struct sk_buff *newsk)
604 {
605         __skb_insert(newsk, old, old->next, old->list);
606 }
607
608 /*
609  * remove sk_buff from list. _Must_ be called atomically, and with
610  * the list known..
611  */
612 extern void        skb_unlink(struct sk_buff *skb);
613 static inline void __skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
614 {
615         struct sk_buff *next, *prev;
616
617         list->qlen--;
618         next       = skb->next;
619         prev       = skb->prev;
620         skb->next  = skb->prev = NULL;
621         skb->list  = NULL;
622         next->prev = prev;
623         prev->next = next;
624 }
625
626
627 /* XXX: more streamlined implementation */
628
629 /**
630  *      __skb_dequeue_tail - remove from the tail of the queue
631  *      @list: list to dequeue from
632  *
633  *      Remove the tail of the list. This function does not take any locks
634  *      so must be used with appropriate locks held only. The tail item is
635  *      returned or %NULL if the list is empty.
636  */
637 extern struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list);
638 static inline struct sk_buff *__skb_dequeue_tail(struct sk_buff_head *list)
639 {
640         struct sk_buff *skb = skb_peek_tail(list);
641         if (skb)
642                 __skb_unlink(skb, list);
643         return skb;
644 }
645
646
647 static inline int skb_is_nonlinear(const struct sk_buff *skb)
648 {
649         return skb->data_len;
650 }
651
652 static inline unsigned int skb_headlen(const struct sk_buff *skb)
653 {
654         return skb->len - skb->data_len;
655 }
656
657 static inline int skb_pagelen(const struct sk_buff *skb)
658 {
659         int i, len = 0;
660
661         for (i = (int)skb_shinfo(skb)->nr_frags - 1; i >= 0; i--)
662                 len += skb_shinfo(skb)->frags[i].size;
663         return len + skb_headlen(skb);
664 }
665
666 static inline void skb_fill_page_desc(struct sk_buff *skb, int i, struct page *page, int off, int size)
667 {
668         skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
669         frag->page = page;
670         frag->page_offset = off;
671         frag->size = size;
672         skb_shinfo(skb)->nr_frags = i+1;
673 }
674
675 #define SKB_PAGE_ASSERT(skb)    BUG_ON(skb_shinfo(skb)->nr_frags)
676 #define SKB_FRAG_ASSERT(skb)    BUG_ON(skb_shinfo(skb)->frag_list)
677 #define SKB_LINEAR_ASSERT(skb)  BUG_ON(skb_is_nonlinear(skb))
678
679 /*
680  *      Add data to an sk_buff
681  */
682 static inline unsigned char *__skb_put(struct sk_buff *skb, unsigned int len)
683 {
684         unsigned char *tmp = skb->tail;
685         SKB_LINEAR_ASSERT(skb);
686         skb->tail += len;
687         skb->len  += len;
688         return tmp;
689 }
690
691 /**
692  *      skb_put - add data to a buffer
693  *      @skb: buffer to use
694  *      @len: amount of data to add
695  *
696  *      This function extends the used data area of the buffer. If this would
697  *      exceed the total buffer size the kernel will panic. A pointer to the
698  *      first byte of the extra data is returned.
699  */
700 static inline unsigned char *skb_put(struct sk_buff *skb, unsigned int len)
701 {
702         unsigned char *tmp = skb->tail;
703         SKB_LINEAR_ASSERT(skb);
704         skb->tail += len;
705         skb->len  += len;
706         if (unlikely(skb->tail>skb->end))
707                 skb_over_panic(skb, len, current_text_addr());
708         return tmp;
709 }
710
711 static inline unsigned char *__skb_push(struct sk_buff *skb, unsigned int len)
712 {
713         skb->data -= len;
714         skb->len  += len;
715         return skb->data;
716 }
717
718 /**
719  *      skb_push - add data to the start of a buffer
720  *      @skb: buffer to use
721  *      @len: amount of data to add
722  *
723  *      This function extends the used data area of the buffer at the buffer
724  *      start. If this would exceed the total buffer headroom the kernel will
725  *      panic. A pointer to the first byte of the extra data is returned.
726  */
727 static inline unsigned char *skb_push(struct sk_buff *skb, unsigned int len)
728 {
729         skb->data -= len;
730         skb->len  += len;
731         if (unlikely(skb->data<skb->head))
732                 skb_under_panic(skb, len, current_text_addr());
733         return skb->data;
734 }
735
736 static inline unsigned char *__skb_pull(struct sk_buff *skb, unsigned int len)
737 {
738         skb->len -= len;
739         BUG_ON(skb->len < skb->data_len);
740         return skb->data += len;
741 }
742
743 /**
744  *      skb_pull - remove data from the start of a buffer
745  *      @skb: buffer to use
746  *      @len: amount of data to remove
747  *
748  *      This function removes data from the start of a buffer, returning
749  *      the memory to the headroom. A pointer to the next data in the buffer
750  *      is returned. Once the data has been pulled future pushes will overwrite
751  *      the old data.
752  */
753 static inline unsigned char *skb_pull(struct sk_buff *skb, unsigned int len)
754 {
755         return unlikely(len > skb->len) ? NULL : __skb_pull(skb, len);
756 }
757
758 extern unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta);
759
760 static inline unsigned char *__pskb_pull(struct sk_buff *skb, unsigned int len)
761 {
762         if (len > skb_headlen(skb) &&
763             !__pskb_pull_tail(skb, len-skb_headlen(skb)))
764                 return NULL;
765         skb->len -= len;
766         return skb->data += len;
767 }
768
769 static inline unsigned char *pskb_pull(struct sk_buff *skb, unsigned int len)
770 {
771         return unlikely(len > skb->len) ? NULL : __pskb_pull(skb, len);
772 }
773
774 static inline int pskb_may_pull(struct sk_buff *skb, unsigned int len)
775 {
776         if (likely(len <= skb_headlen(skb)))
777                 return 1;
778         if (unlikely(len > skb->len))
779                 return 0;
780         return __pskb_pull_tail(skb, len-skb_headlen(skb)) != NULL;
781 }
782
783 /**
784  *      skb_headroom - bytes at buffer head
785  *      @skb: buffer to check
786  *
787  *      Return the number of bytes of free space at the head of an &sk_buff.
788  */
789 static inline int skb_headroom(const struct sk_buff *skb)
790 {
791         return skb->data - skb->head;
792 }
793
794 /**
795  *      skb_tailroom - bytes at buffer end
796  *      @skb: buffer to check
797  *
798  *      Return the number of bytes of free space at the tail of an sk_buff
799  */
800 static inline int skb_tailroom(const struct sk_buff *skb)
801 {
802         return skb_is_nonlinear(skb) ? 0 : skb->end - skb->tail;
803 }
804
805 /**
806  *      skb_reserve - adjust headroom
807  *      @skb: buffer to alter
808  *      @len: bytes to move
809  *
810  *      Increase the headroom of an empty &sk_buff by reducing the tail
811  *      room. This is only allowed for an empty buffer.
812  */
813 static inline void skb_reserve(struct sk_buff *skb, unsigned int len)
814 {
815         skb->data += len;
816         skb->tail += len;
817 }
818
819 extern int ___pskb_trim(struct sk_buff *skb, unsigned int len, int realloc);
820
821 static inline void __skb_trim(struct sk_buff *skb, unsigned int len)
822 {
823         if (!skb->data_len) {
824                 skb->len  = len;
825                 skb->tail = skb->data + len;
826         } else
827                 ___pskb_trim(skb, len, 0);
828 }
829
830 /**
831  *      skb_trim - remove end from a buffer
832  *      @skb: buffer to alter
833  *      @len: new length
834  *
835  *      Cut the length of a buffer down by removing data from the tail. If
836  *      the buffer is already under the length specified it is not modified.
837  */
838 static inline void skb_trim(struct sk_buff *skb, unsigned int len)
839 {
840         if (skb->len > len)
841                 __skb_trim(skb, len);
842 }
843
844
845 static inline int __pskb_trim(struct sk_buff *skb, unsigned int len)
846 {
847         if (!skb->data_len) {
848                 skb->len  = len;
849                 skb->tail = skb->data+len;
850                 return 0;
851         }
852         return ___pskb_trim(skb, len, 1);
853 }
854
855 static inline int pskb_trim(struct sk_buff *skb, unsigned int len)
856 {
857         return (len < skb->len) ? __pskb_trim(skb, len) : 0;
858 }
859
860 /**
861  *      skb_orphan - orphan a buffer
862  *      @skb: buffer to orphan
863  *
864  *      If a buffer currently has an owner then we call the owner's
865  *      destructor function and make the @skb unowned. The buffer continues
866  *      to exist but is no longer charged to its former owner.
867  */
868 static inline void skb_orphan(struct sk_buff *skb)
869 {
870         if (skb->destructor)
871                 skb->destructor(skb);
872         skb->destructor = NULL;
873         skb->sk         = NULL;
874 }
875
876 /**
877  *      __skb_queue_purge - empty a list
878  *      @list: list to empty
879  *
880  *      Delete all buffers on an &sk_buff list. Each buffer is removed from
881  *      the list and one reference dropped. This function does not take the
882  *      list lock and the caller must hold the relevant locks to use it.
883  */
884 extern void skb_queue_purge(struct sk_buff_head *list);
885 static inline void __skb_queue_purge(struct sk_buff_head *list)
886 {
887         struct sk_buff *skb;
888         while ((skb = __skb_dequeue(list)) != NULL)
889                 kfree_skb(skb);
890 }
891
892 /**
893  *      __dev_alloc_skb - allocate an skbuff for sending
894  *      @length: length to allocate
895  *      @gfp_mask: get_free_pages mask, passed to alloc_skb
896  *
897  *      Allocate a new &sk_buff and assign it a usage count of one. The
898  *      buffer has unspecified headroom built in. Users should allocate
899  *      the headroom they think they need without accounting for the
900  *      built in space. The built in space is used for optimisations.
901  *
902  *      %NULL is returned in there is no free memory.
903  */
904 static inline struct sk_buff *__dev_alloc_skb(unsigned int length,
905                                               int gfp_mask)
906 {
907         struct sk_buff *skb = alloc_skb(length + 16, gfp_mask);
908         if (likely(skb))
909                 skb_reserve(skb, 16);
910         return skb;
911 }
912
913 /**
914  *      dev_alloc_skb - allocate an skbuff for sending
915  *      @length: length to allocate
916  *
917  *      Allocate a new &sk_buff and assign it a usage count of one. The
918  *      buffer has unspecified headroom built in. Users should allocate
919  *      the headroom they think they need without accounting for the
920  *      built in space. The built in space is used for optimisations.
921  *
922  *      %NULL is returned in there is no free memory. Although this function
923  *      allocates memory it can be called from an interrupt.
924  */
925 static inline struct sk_buff *dev_alloc_skb(unsigned int length)
926 {
927         return __dev_alloc_skb(length, GFP_ATOMIC);
928 }
929
930 /**
931  *      skb_cow - copy header of skb when it is required
932  *      @skb: buffer to cow
933  *      @headroom: needed headroom
934  *
935  *      If the skb passed lacks sufficient headroom or its data part
936  *      is shared, data is reallocated. If reallocation fails, an error
937  *      is returned and original skb is not changed.
938  *
939  *      The result is skb with writable area skb->head...skb->tail
940  *      and at least @headroom of space at head.
941  */
942 static inline int skb_cow(struct sk_buff *skb, unsigned int headroom)
943 {
944         int delta = (headroom > 16 ? headroom : 16) - skb_headroom(skb);
945
946         if (delta < 0)
947                 delta = 0;
948
949         if (delta || skb_cloned(skb))
950                 return pskb_expand_head(skb, (delta + 15) & ~15, 0, GFP_ATOMIC);
951         return 0;
952 }
953
954 /**
955  *      skb_padto       - pad an skbuff up to a minimal size
956  *      @skb: buffer to pad
957  *      @len: minimal length
958  *
959  *      Pads up a buffer to ensure the trailing bytes exist and are
960  *      blanked. If the buffer already contains sufficient data it
961  *      is untouched. Returns the buffer, which may be a replacement
962  *      for the original, or NULL for out of memory - in which case
963  *      the original buffer is still freed.
964  */
965  
966 static inline struct sk_buff *skb_padto(struct sk_buff *skb, unsigned int len)
967 {
968         unsigned int size = skb->len;
969         if (likely(size >= len))
970                 return skb;
971         return skb_pad(skb, len-size);
972 }
973
974 /**
975  *      skb_linearize - convert paged skb to linear one
976  *      @skb: buffer to linarize
977  *      @gfp: allocation mode
978  *
979  *      If there is no free memory -ENOMEM is returned, otherwise zero
980  *      is returned and the old skb data released.
981  */
982 extern int __skb_linearize(struct sk_buff *skb, int gfp);
983 static inline int skb_linearize(struct sk_buff *skb, int gfp)
984 {
985         return __skb_linearize(skb, gfp);
986 }
987
988 static inline void *kmap_skb_frag(const skb_frag_t *frag)
989 {
990 #ifdef CONFIG_HIGHMEM
991         BUG_ON(in_irq());
992
993         local_bh_disable();
994 #endif
995         return kmap_atomic(frag->page, KM_SKB_DATA_SOFTIRQ);
996 }
997
998 static inline void kunmap_skb_frag(void *vaddr)
999 {
1000         kunmap_atomic(vaddr, KM_SKB_DATA_SOFTIRQ);
1001 #ifdef CONFIG_HIGHMEM
1002         local_bh_enable();
1003 #endif
1004 }
1005
1006 #define skb_queue_walk(queue, skb) \
1007                 for (skb = (queue)->next, prefetch(skb->next);  \
1008                      (skb != (struct sk_buff *)(queue));        \
1009                      skb = skb->next, prefetch(skb->next))
1010
1011
1012 extern struct sk_buff *skb_recv_datagram(struct sock *sk, unsigned flags,
1013                                          int noblock, int *err);
1014 extern unsigned int    datagram_poll(struct file *file, struct socket *sock,
1015                                      struct poll_table_struct *wait);
1016 extern int             skb_copy_datagram(const struct sk_buff *from,
1017                                          int offset, char __user *to, int size);
1018 extern int             skb_copy_datagram_iovec(const struct sk_buff *from,
1019                                                int offset, struct iovec *to,
1020                                                int size);
1021 extern int             skb_copy_and_csum_datagram(const struct sk_buff *skb,
1022                                                   int offset, u8 __user *to,
1023                                                   int len, unsigned int *csump);
1024 extern int             skb_copy_and_csum_datagram_iovec(const
1025                                                         struct sk_buff *skb,
1026                                                         int hlen,
1027                                                         struct iovec *iov);
1028 extern void            skb_free_datagram(struct sock *sk, struct sk_buff *skb);
1029 extern unsigned int    skb_checksum(const struct sk_buff *skb, int offset,
1030                                     int len, unsigned int csum);
1031 extern int             skb_copy_bits(const struct sk_buff *skb, int offset,
1032                                      void *to, int len);
1033 extern unsigned int    skb_copy_and_csum_bits(const struct sk_buff *skb,
1034                                               int offset, u8 *to, int len,
1035                                               unsigned int csum);
1036 extern void            skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to);
1037
1038 extern void skb_init(void);
1039 extern void skb_add_mtu(int mtu);
1040
1041 #ifdef CONFIG_NETFILTER
1042 static inline void nf_conntrack_put(struct nf_ct_info *nfct)
1043 {
1044         if (nfct && atomic_dec_and_test(&nfct->master->use))
1045                 nfct->master->destroy(nfct->master);
1046 }
1047 static inline void nf_conntrack_get(struct nf_ct_info *nfct)
1048 {
1049         if (nfct)
1050                 atomic_inc(&nfct->master->use);
1051 }
1052
1053 #ifdef CONFIG_BRIDGE_NETFILTER
1054 static inline void nf_bridge_put(struct nf_bridge_info *nf_bridge)
1055 {
1056         if (nf_bridge && atomic_dec_and_test(&nf_bridge->use))
1057                 kfree(nf_bridge);
1058 }
1059 static inline void nf_bridge_get(struct nf_bridge_info *nf_bridge)
1060 {
1061         if (nf_bridge)
1062                 atomic_inc(&nf_bridge->use);
1063 }
1064 #endif
1065
1066 #endif
1067
1068 #endif  /* __KERNEL__ */
1069 #endif  /* _LINUX_SKBUFF_H */