Main Page | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals

skbuff.h

Go to the documentation of this file.
00001 /*
00002  *      Definitions for the 'struct sk_buff' memory handlers.
00003  *
00004  *      Authors:
00005  *              Alan Cox, <gw4pts@gw4pts.ampr.org>
00006  *              Florian La Roche, <rzsfl@rz.uni-sb.de>
00007  *
00008  *      This program is free software; you can redistribute it and/or
00009  *      modify it under the terms of the GNU General Public License
00010  *      as published by the Free Software Foundation; either version
00011  *      2 of the License, or (at your option) any later version.
00012  */
00013  
00014 #ifndef _LINUX_SKBUFF_H
00015 #define _LINUX_SKBUFF_H
00016 
00017 #include <linux/config.h>
00018 #include <linux/time.h>
00019 
00020 #include <asm/atomic.h>
00021 #include <asm/types.h>
00022 #include <asm/spinlock.h>
00023 
00024 #define HAVE_ALLOC_SKB          /* For the drivers to know */
00025 #define HAVE_ALIGNABLE_SKB      /* Ditto 8)                */
00026 #define SLAB_SKB                /* Slabified skbuffs       */
00027 
00028 #define CHECKSUM_NONE 0
00029 #define CHECKSUM_HW 1
00030 #define CHECKSUM_UNNECESSARY 2
00031 
00032 struct sk_buff_head {
00033         struct sk_buff  * next;
00034         struct sk_buff  * prev;
00035         __u32           qlen;           /* Must be same length as a pointer
00036                                            for using debugging */
00037 };
00038 
00039 struct sk_buff {
00040         struct sk_buff  * next;                 /* Next buffer in list                          */
00041         struct sk_buff  * prev;                 /* Previous buffer in list                      */
00042         struct sk_buff_head * list;             /* List we are on                               */
00043         struct sock     *sk;                    /* Socket we are owned by                       */
00044         struct timeval  stamp;                  /* Time we arrived                              */
00045         struct device   *dev;                   /* Device we arrived on/are leaving by          */
00046 
00047         /* Transport layer header */
00048         union
00049         {
00050                 struct tcphdr   *th;
00051                 struct udphdr   *uh;
00052                 struct icmphdr  *icmph;
00053                 struct igmphdr  *igmph;
00054                 struct iphdr    *ipiph;
00055                 struct spxhdr   *spxh;
00056                 unsigned char   *raw;
00057         } h;
00058 
00059         /* Network layer header */
00060         union
00061         {
00062                 struct iphdr    *iph;
00063                 struct ipv6hdr  *ipv6h;
00064                 struct arphdr   *arph;
00065                 struct ipxhdr   *ipxh;
00066                 unsigned char   *raw;
00067         } nh;
00068   
00069         /* Link layer header */
00070         union 
00071         {       
00072                 struct ethhdr   *ethernet;
00073                 unsigned char   *raw;
00074         } mac;
00075 
00076         struct  dst_entry *dst;
00077 
00078         char            cb[48];  
00079 
00080         unsigned int    len;                    /* Length of actual data                        */
00081         unsigned int    csum;                   /* Checksum                                     */
00082         volatile char   used;                   /* Data moved to user and not MSG_PEEK          */
00083         unsigned char   is_clone,               /* We are a clone                               */
00084                         cloned,                 /* head may be cloned (check refcnt to be sure). */
00085                         pkt_type,               /* Packet class                                 */
00086                         pkt_bridged,            /* Tracker for bridging                         */
00087                         ip_summed;              /* Driver fed us an IP checksum                 */
00088         __u32           priority;               /* Packet queueing priority                     */
00089         atomic_t        users;                  /* User count - see datagram.c,tcp.c            */
00090         unsigned short  protocol;               /* Packet protocol from driver.                 */
00091         unsigned short  security;               /* Security level of packet                     */
00092         unsigned int    truesize;               /* Buffer size                                  */
00093 
00094         unsigned char   *head;                  /* Head of buffer                               */
00095         unsigned char   *data;                  /* Data head pointer                            */
00096         unsigned char   *tail;                  /* Tail pointer                                 */
00097         unsigned char   *end;                   /* End pointer                                  */
00098         void            (*destructor)(struct sk_buff *);        /* Destruct function            */
00099 #ifdef CONFIG_IP_FIREWALL
00100         __u32           fwmark;                 /* Label made by fwchains, used by pktsched     */
00101 #endif
00102 #if defined(CONFIG_SHAPER) || defined(CONFIG_SHAPER_MODULE)
00103         __u32           shapelatency;           /* Latency on frame */
00104         __u32           shapeclock;             /* Time it should go out */
00105         __u32           shapelen;               /* Frame length in clocks */
00106         __u32           shapestamp;             /* Stamp for shaper    */
00107         __u16           shapepend;              /* Pending */
00108 #endif
00109 
00110 #if defined(CONFIG_HIPPI)
00111         union{
00112                 __u32   ifield;
00113         } private;
00114 #endif
00115 };
00116 
00117 /* These are just the default values. This is run time configurable.
00118  * FIXME: Probably the config option should go away. -- erics
00119  */
00120 #ifdef CONFIG_SKB_LARGE
00121 #define SK_WMEM_MAX     65535
00122 #define SK_RMEM_MAX     65535
00123 #else
00124 #define SK_WMEM_MAX     32767
00125 #define SK_RMEM_MAX     32767
00126 #endif
00127 
00128 #ifdef __KERNEL__
00129 /*
00130  *      Handling routines are only of interest to the kernel
00131  */
00132 #include <linux/malloc.h>
00133 
00134 #include <asm/system.h>
00135 
00136 extern void                     __kfree_skb(struct sk_buff *skb);
00137 extern void                     skb_queue_head_init(struct sk_buff_head *list);
00138 extern void                     skb_queue_head(struct sk_buff_head *list,struct sk_buff *buf);
00139 extern void                     skb_queue_tail(struct sk_buff_head *list,struct sk_buff *buf);
00140 extern struct sk_buff *         skb_dequeue(struct sk_buff_head *list);
00141 extern void                     skb_insert(struct sk_buff *old,struct sk_buff *newsk);
00142 extern void                     skb_append(struct sk_buff *old,struct sk_buff *newsk);
00143 extern void                     skb_unlink(struct sk_buff *buf);
00144 extern __u32                    skb_queue_len(struct sk_buff_head *list);
00145 extern struct sk_buff *         skb_peek_copy(struct sk_buff_head *list);
00146 extern struct sk_buff *         alloc_skb(unsigned int size, int priority);
00147 extern struct sk_buff *         dev_alloc_skb(unsigned int size);
00148 extern void                     kfree_skbmem(struct sk_buff *skb);
00149 extern struct sk_buff *         skb_clone(struct sk_buff *skb, int priority);
00150 extern struct sk_buff *         skb_copy(struct sk_buff *skb, int priority);
00151 extern struct sk_buff *         skb_realloc_headroom(struct sk_buff *skb, int newheadroom);
00152 extern struct sk_buff *         skb_pad(struct sk_buff *skb, int pad);
00153 #define dev_kfree_skb(a)        kfree_skb(a)
00154 extern unsigned char *          skb_put(struct sk_buff *skb, unsigned int len);
00155 extern unsigned char *          skb_push(struct sk_buff *skb, unsigned int len);
00156 extern unsigned char *          skb_pull(struct sk_buff *skb, unsigned int len);
00157 extern int                      skb_headroom(struct sk_buff *skb);
00158 extern int                      skb_tailroom(struct sk_buff *skb);
00159 extern void                     skb_reserve(struct sk_buff *skb, unsigned int len);
00160 extern void                     skb_trim(struct sk_buff *skb, unsigned int len);
00161 extern void     skb_over_panic(struct sk_buff *skb, int len, void *here);
00162 extern void     skb_under_panic(struct sk_buff *skb, int len, void *here);
00163 
00164 /* Internal */
00165 extern __inline__ atomic_t *skb_datarefp(struct sk_buff *skb)
00166 {
00167         return (atomic_t *)(skb->end);
00168 }
00169 
00170 extern __inline__ int skb_queue_empty(struct sk_buff_head *list)
00171 {
00172         return (list->next == (struct sk_buff *) list);
00173 }
00174 
00175 extern __inline__ void kfree_skb(struct sk_buff *skb)
00176 {
00177         if (atomic_dec_and_test(&skb->users))
00178                 __kfree_skb(skb);
00179 }
00180 
00181 /* Use this if you didn't touch the skb state [for fast switching] */
00182 extern __inline__ void kfree_skb_fast(struct sk_buff *skb)
00183 {
00184         if (atomic_dec_and_test(&skb->users))
00185                 kfree_skbmem(skb);      
00186 }
00187 
00188 extern __inline__ int skb_cloned(struct sk_buff *skb)
00189 {
00190         return skb->cloned && atomic_read(skb_datarefp(skb)) != 1;
00191 }
00192 
00193 extern __inline__ int skb_shared(struct sk_buff *skb)
00194 {
00195         return (atomic_read(&skb->users) != 1);
00196 }
00197 
00198 /*
00199  *      Copy shared buffers into a new sk_buff. We effectively do COW on
00200  *      packets to handle cases where we have a local reader and forward
00201  *      and a couple of other messy ones. The normal one is tcpdumping
00202  *      a packet thats being forwarded.
00203  */
00204  
00205 extern __inline__ struct sk_buff *skb_unshare(struct sk_buff *skb, int pri)
00206 {
00207         struct sk_buff *nskb;
00208         if(!skb_cloned(skb))
00209                 return skb;
00210         nskb=skb_copy(skb, pri);
00211         kfree_skb(skb);         /* Free our shared copy */
00212         return nskb;
00213 }
00214 
00215 /*
00216  *      Peek an sk_buff. Unlike most other operations you _MUST_
00217  *      be careful with this one. A peek leaves the buffer on the
00218  *      list and someone else may run off with it. For an interrupt
00219  *      type system cli() peek the buffer copy the data and sti();
00220  */
00221  
00222 extern __inline__ struct sk_buff *skb_peek(struct sk_buff_head *list_)
00223 {
00224         struct sk_buff *list = ((struct sk_buff *)list_)->next;
00225         if (list == (struct sk_buff *)list_)
00226                 list = NULL;
00227         return list;
00228 }
00229 
00230 extern __inline__ struct sk_buff *skb_peek_tail(struct sk_buff_head *list_)
00231 {
00232         struct sk_buff *list = ((struct sk_buff *)list_)->prev;
00233         if (list == (struct sk_buff *)list_)
00234                 list = NULL;
00235         return list;
00236 }
00237 
00238 /*
00239  *      Return the length of an sk_buff queue
00240  */
00241  
00242 extern __inline__ __u32 skb_queue_len(struct sk_buff_head *list_)
00243 {
00244         return(list_->qlen);
00245 }
00246 
00247 extern __inline__ void skb_queue_head_init(struct sk_buff_head *list)
00248 {
00249         list->prev = (struct sk_buff *)list;
00250         list->next = (struct sk_buff *)list;
00251         list->qlen = 0;
00252 }
00253 
00254 /*
00255  *      Insert an sk_buff at the start of a list.
00256  *
00257  *      The "__skb_xxxx()" functions are the non-atomic ones that
00258  *      can only be called with interrupts disabled.
00259  */
00260 
00261 extern __inline__ void __skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
00262 {
00263         struct sk_buff *prev, *next;
00264 
00265         newsk->list = list;
00266         list->qlen++;
00267         prev = (struct sk_buff *)list;
00268         next = prev->next;
00269         newsk->next = next;
00270         newsk->prev = prev;
00271         next->prev = newsk;
00272         prev->next = newsk;
00273 }
00274 
00275 extern spinlock_t skb_queue_lock;
00276 
00277 extern __inline__ void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
00278 {
00279         unsigned long flags;
00280 
00281         spin_lock_irqsave(&skb_queue_lock, flags);
00282         __skb_queue_head(list, newsk);
00283         spin_unlock_irqrestore(&skb_queue_lock, flags);
00284 }
00285 
00286 /*
00287  *      Insert an sk_buff at the end of a list.
00288  */
00289 
00290 extern __inline__ void __skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
00291 {
00292         struct sk_buff *prev, *next;
00293 
00294         newsk->list = list;
00295         list->qlen++;
00296         next = (struct sk_buff *)list;
00297         prev = next->prev;
00298         newsk->next = next;
00299         newsk->prev = prev;
00300         next->prev = newsk;
00301         prev->next = newsk;
00302 }
00303 
00304 extern __inline__ void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
00305 {
00306         unsigned long flags;
00307 
00308         spin_lock_irqsave(&skb_queue_lock, flags);
00309         __skb_queue_tail(list, newsk);
00310         spin_unlock_irqrestore(&skb_queue_lock, flags);
00311 }
00312 
00313 /*
00314  *      Remove an sk_buff from a list.
00315  */
00316 
00317 extern __inline__ struct sk_buff *__skb_dequeue(struct sk_buff_head *list)
00318 {
00319         struct sk_buff *next, *prev, *result;
00320 
00321         prev = (struct sk_buff *) list;
00322         next = prev->next;
00323         result = NULL;
00324         if (next != prev) {
00325                 result = next;
00326                 next = next->next;
00327                 list->qlen--;
00328                 next->prev = prev;
00329                 prev->next = next;
00330                 result->next = NULL;
00331                 result->prev = NULL;
00332                 result->list = NULL;
00333         }
00334         return result;
00335 }
00336 
00337 extern __inline__ struct sk_buff *skb_dequeue(struct sk_buff_head *list)
00338 {
00339         long flags;
00340         struct sk_buff *result;
00341 
00342         spin_lock_irqsave(&skb_queue_lock, flags);
00343         result = __skb_dequeue(list);
00344         spin_unlock_irqrestore(&skb_queue_lock, flags);
00345         return result;
00346 }
00347 
00348 /*
00349  *      Insert a packet on a list.
00350  */
00351 
00352 extern __inline__ void __skb_insert(struct sk_buff *newsk,
00353         struct sk_buff * prev, struct sk_buff *next,
00354         struct sk_buff_head * list)
00355 {
00356         newsk->next = next;
00357         newsk->prev = prev;
00358         next->prev = newsk;
00359         prev->next = newsk;
00360         newsk->list = list;
00361         list->qlen++;
00362 }
00363 
00364 /*
00365  *      Place a packet before a given packet in a list
00366  */
00367 extern __inline__ void skb_insert(struct sk_buff *old, struct sk_buff *newsk)
00368 {
00369         unsigned long flags;
00370 
00371         spin_lock_irqsave(&skb_queue_lock, flags);
00372         __skb_insert(newsk, old->prev, old, old->list);
00373         spin_unlock_irqrestore(&skb_queue_lock, flags);
00374 }
00375 
00376 /*
00377  *      Place a packet after a given packet in a list.
00378  */
00379 
00380 extern __inline__ void __skb_append(struct sk_buff *old, struct sk_buff *newsk)
00381 {
00382         __skb_insert(newsk, old, old->next, old->list);
00383 }
00384 
00385 extern __inline__ void skb_append(struct sk_buff *old, struct sk_buff *newsk)
00386 {
00387         unsigned long flags;
00388 
00389         spin_lock_irqsave(&skb_queue_lock, flags);
00390         __skb_append(old, newsk);
00391         spin_unlock_irqrestore(&skb_queue_lock, flags);
00392 }
00393 
00394 /*
00395  * remove sk_buff from list. _Must_ be called atomically, and with
00396  * the list known..
00397  */
00398 extern __inline__ void __skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
00399 {
00400         struct sk_buff * next, * prev;
00401 
00402         list->qlen--;
00403         next = skb->next;
00404         prev = skb->prev;
00405         skb->next = NULL;
00406         skb->prev = NULL;
00407         skb->list = NULL;
00408         next->prev = prev;
00409         prev->next = next;
00410 }
00411 
00412 /*
00413  *      Remove an sk_buff from its list. Works even without knowing the list it
00414  *      is sitting on, which can be handy at times. It also means that THE LIST
00415  *      MUST EXIST when you unlink. Thus a list must have its contents unlinked
00416  *      _FIRST_.
00417  */
00418 
00419 extern __inline__ void skb_unlink(struct sk_buff *skb)
00420 {
00421         unsigned long flags;
00422 
00423         spin_lock_irqsave(&skb_queue_lock, flags);
00424         if(skb->list)
00425                 __skb_unlink(skb, skb->list);
00426         spin_unlock_irqrestore(&skb_queue_lock, flags);
00427 }
00428 
00429 /* XXX: more streamlined implementation */
00430 extern __inline__ struct sk_buff *__skb_dequeue_tail(struct sk_buff_head *list)
00431 {
00432         struct sk_buff *skb = skb_peek_tail(list); 
00433         if (skb)
00434                 __skb_unlink(skb, list);
00435         return skb;
00436 }
00437 
00438 extern __inline__ struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
00439 {
00440         long flags;
00441         struct sk_buff *result;
00442 
00443         spin_lock_irqsave(&skb_queue_lock, flags);
00444         result = __skb_dequeue_tail(list);
00445         spin_unlock_irqrestore(&skb_queue_lock, flags);
00446         return result;
00447 }
00448 
00449 /*
00450  *      Add data to an sk_buff
00451  */
00452  
00453 extern __inline__ unsigned char *__skb_put(struct sk_buff *skb, unsigned int len)
00454 {
00455         unsigned char *tmp=skb->tail;
00456         skb->tail+=len;
00457         skb->len+=len;
00458         return tmp;
00459 }
00460 
00461 extern __inline__ unsigned char *skb_put(struct sk_buff *skb, unsigned int len)
00462 {
00463         unsigned char *tmp=skb->tail;
00464         skb->tail+=len;
00465         skb->len+=len;
00466         if(skb->tail>skb->end)
00467         {
00468                 __label__ here; 
00469                 skb_over_panic(skb, len, &&here); 
00470 here:           ;
00471         }
00472         return tmp;
00473 }
00474 
00475 extern __inline__ unsigned char *__skb_push(struct sk_buff *skb, unsigned int len)
00476 {
00477         skb->data-=len;
00478         skb->len+=len;
00479         return skb->data;
00480 }
00481 
00482 extern __inline__ unsigned char *skb_push(struct sk_buff *skb, unsigned int len)
00483 {
00484         skb->data-=len;
00485         skb->len+=len;
00486         if(skb->data<skb->head)
00487         {
00488                 __label__ here;
00489                 skb_under_panic(skb, len, &&here);
00490 here:           ;
00491         }
00492         return skb->data;
00493 }
00494 
00495 extern __inline__ char *__skb_pull(struct sk_buff *skb, unsigned int len)
00496 {
00497         skb->len-=len;
00498         return  skb->data+=len;
00499 }
00500 
00501 extern __inline__ unsigned char * skb_pull(struct sk_buff *skb, unsigned int len)
00502 {       
00503         if (len > skb->len)
00504                 return NULL;
00505         return __skb_pull(skb,len);
00506 }
00507 
00508 extern __inline__ int skb_headroom(struct sk_buff *skb)
00509 {
00510         return skb->data-skb->head;
00511 }
00512 
00513 extern __inline__ int skb_tailroom(struct sk_buff *skb)
00514 {
00515         return skb->end-skb->tail;
00516 }
00517 
00518 extern __inline__ void skb_reserve(struct sk_buff *skb, unsigned int len)
00519 {
00520         skb->data+=len;
00521         skb->tail+=len;
00522 }
00523 
00524 extern __inline__ void __skb_trim(struct sk_buff *skb, unsigned int len)
00525 {
00526         skb->len = len;
00527         skb->tail = skb->data+len;
00528 }
00529 
00530 extern __inline__ void skb_trim(struct sk_buff *skb, unsigned int len)
00531 {
00532         if (skb->len > len) {
00533                 __skb_trim(skb, len);
00534         }
00535 }
00536 
00537 extern __inline__ void skb_orphan(struct sk_buff *skb)
00538 {
00539         if (skb->destructor)
00540                 skb->destructor(skb);
00541         skb->destructor = NULL;
00542         skb->sk = NULL;
00543 }
00544 
00545 extern __inline__ void skb_queue_purge(struct sk_buff_head *list)
00546 {
00547         struct sk_buff *skb;
00548         while ((skb=skb_dequeue(list))!=NULL)
00549                 kfree_skb(skb);
00550 }
00551 
00552 extern __inline__ struct sk_buff *dev_alloc_skb(unsigned int length)
00553 {
00554         struct sk_buff *skb;
00555 
00556         skb = alloc_skb(length+16, GFP_ATOMIC);
00557         if (skb)
00558                 skb_reserve(skb,16);
00559         return skb;
00560 }
00561 
00562 extern __inline__ struct sk_buff *
00563 skb_cow(struct sk_buff *skb, unsigned int headroom)
00564 {
00565         headroom = (headroom+15)&~15;
00566 
00567         if ((unsigned)skb_headroom(skb) < headroom || skb_cloned(skb)) {
00568                 struct sk_buff *skb2;
00569                 
00570                 if ((unsigned)skb_headroom(skb) < headroom)
00571                         skb2 = skb_realloc_headroom(skb, headroom);
00572                 else
00573                         skb2 = skb_copy(skb, GFP_ATOMIC);
00574                 kfree_skb(skb);
00575                 skb = skb2;
00576         }
00577         return skb;
00578 }
00579 
00580 /**
00581  *      skb_padto       - pad an skbuff up to a minimal size
00582  *      @skb: buffer to pad
00583  *      @len: minimal length
00584  *
00585  *      Pads up a buffer to ensure the trailing bytes exist and are
00586  *      blanked. If the buffer already contains sufficient data it
00587  *      is untouched. Returns the buffer, which may be a replacement
00588  *      for the original, or NULL for out of memory - in which case
00589  *      the original buffer is still freed.
00590  */
00591  
00592 static inline struct sk_buff *skb_padto(struct sk_buff *skb, unsigned int len)
00593 {
00594         unsigned int size = skb->len;
00595         if(size >= len)
00596                 return skb;
00597         return skb_pad(skb, len-size);
00598 }
00599 
00600 extern struct sk_buff *         skb_recv_datagram(struct sock *sk,unsigned flags,int noblock, int *err);
00601 extern unsigned int             datagram_poll(struct file *file, struct socket *sock, struct poll_table_struct *wait);
00602 extern int                      skb_copy_datagram(struct sk_buff *from, int offset, char *to,int size);
00603 extern int                      skb_copy_datagram_iovec(struct sk_buff *from, int offset, struct iovec *to,int size);
00604 extern void                     skb_free_datagram(struct sock * sk, struct sk_buff *skb);
00605 
00606 extern void skb_init(void);
00607 extern void skb_add_mtu(int mtu);
00608 
00609 #endif  /* __KERNEL__ */
00610 #endif  /* _LINUX_SKBUFF_H */