Main Page | Class List | File List | Class Members | File Members

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/kernel.h> 00019 #include <linux/sched.h> 00020 #include <linux/time.h> 00021 #include <linux/cache.h> 00022 00023 #include <asm/atomic.h> 00024 #include <asm/types.h> 00025 #include <linux/spinlock.h> 00026 #include <linux/mm.h> 00027 #include <linux/highmem.h> 00028 00029 #define HAVE_ALLOC_SKB /* For the drivers to know */ 00030 #define HAVE_ALIGNABLE_SKB /* Ditto 8) */ 00031 #define SLAB_SKB /* Slabified skbuffs */ 00032 00033 #define CHECKSUM_NONE 0 00034 #define CHECKSUM_HW 1 00035 #define CHECKSUM_UNNECESSARY 2 00036 00037 #define SKB_DATA_ALIGN(X) (((X) + (SMP_CACHE_BYTES-1)) & ~(SMP_CACHE_BYTES-1)) 00038 #define SKB_MAX_ORDER(X,ORDER) (((PAGE_SIZE<<(ORDER)) - (X) - sizeof(struct skb_shared_info))&~(SMP_CACHE_BYTES-1)) 00039 #define SKB_MAX_HEAD(X) (SKB_MAX_ORDER((X),0)) 00040 #define SKB_MAX_ALLOC (SKB_MAX_ORDER(0,2)) 00041 00042 /* A. Checksumming of received packets by device. 00043 * 00044 * NONE: device failed to checksum this packet. 00045 * skb->csum is undefined. 00046 * 00047 * UNNECESSARY: device parsed packet and wouldbe verified checksum. 00048 * skb->csum is undefined. 00049 * It is bad option, but, unfortunately, many of vendors do this. 00050 * Apparently with secret goal to sell you new device, when you 00051 * will add new protocol to your host. F.e. IPv6. 8) 00052 * 00053 * HW: the most generic way. Device supplied checksum of _all_ 00054 * the packet as seen by netif_rx in skb->csum. 00055 * NOTE: Even if device supports only some protocols, but 00056 * is able to produce some skb->csum, it MUST use HW, 00057 * not UNNECESSARY. 00058 * 00059 * B. Checksumming on output. 00060 * 00061 * NONE: skb is checksummed by protocol or csum is not required. 00062 * 00063 * HW: device is required to csum packet as seen by hard_start_xmit 00064 * from skb->h.raw to the end and to record the checksum 00065 * at skb->h.raw+skb->csum. 00066 * 00067 * Device must show its capabilities in dev->features, set 00068 * at device setup time. 00069 * NETIF_F_HW_CSUM - it is clever device, it is able to checksum 00070 * everything. 00071 * NETIF_F_NO_CSUM - loopback or reliable single hop media. 00072 * NETIF_F_IP_CSUM - device is dumb. It is able to csum only 00073 * TCP/UDP over IPv4. Sigh. Vendors like this 00074 * way by an unknown reason. Though, see comment above 00075 * about CHECKSUM_UNNECESSARY. 8) 00076 * 00077 * Any questions? No questions, good. --ANK 00078 */ 00079 00080 #ifdef __i386__ 00081 #define NET_CALLER(arg) (*(((void**)&arg)-1)) 00082 #else 00083 #define NET_CALLER(arg) __builtin_return_address(0) 00084 #endif 00085 00086 #ifdef CONFIG_NETFILTER 00087 struct nf_conntrack { 00088 atomic_t use; 00089 void (*destroy)(struct nf_conntrack *); 00090 }; 00091 00092 struct nf_ct_info { 00093 struct nf_conntrack *master; 00094 }; 00095 #endif 00096 00097 struct sk_buff_head { 00098 /* These two members must be first. */ 00099 struct sk_buff * next; 00100 struct sk_buff * prev; 00101 00102 __u32 qlen; 00103 spinlock_t lock; 00104 }; 00105 00106 struct sk_buff; 00107 00108 #define MAX_SKB_FRAGS 6 00109 00110 typedef struct skb_frag_struct skb_frag_t; 00111 00112 struct skb_frag_struct 00113 { 00114 struct page *page; 00115 __u16 page_offset; 00116 __u16 size; 00117 }; 00118 00119 /* This data is invariant across clones and lives at 00120 * the end of the header data, ie. at skb->end. 00121 */ 00122 struct skb_shared_info { 00123 atomic_t dataref; 00124 unsigned int nr_frags; 00125 struct sk_buff *frag_list; 00126 skb_frag_t frags[MAX_SKB_FRAGS]; 00127 }; 00128 00129 struct sk_buff { 00130 /* These two members must be first. */ 00131 struct sk_buff * next; /* Next buffer in list */ 00132 struct sk_buff * prev; /* Previous buffer in list */ 00133 00134 struct sk_buff_head * list; /* List we are on */ 00135 struct sock *sk; /* Socket we are owned by */ 00136 struct timeval stamp; /* Time we arrived */ 00137 struct net_device *dev; /* Device we arrived on/are leaving by */ 00138 struct net_device *real_dev; /* For support of point to point protocols 00139 (e.g. 802.3ad) over bonding, we must save the 00140 physical device that got the packet before 00141 replacing skb->dev with the virtual device. */ 00142 00143 /* Transport layer header */ 00144 union 00145 { 00146 struct tcphdr *th; 00147 struct udphdr *uh; 00148 struct icmphdr *icmph; 00149 struct igmphdr *igmph; 00150 struct iphdr *ipiph; 00151 struct spxhdr *spxh; 00152 unsigned char *raw; 00153 } h; 00154 00155 /* Network layer header */ 00156 union 00157 { 00158 struct iphdr *iph; 00159 struct ipv6hdr *ipv6h; 00160 struct arphdr *arph; 00161 struct ipxhdr *ipxh; 00162 unsigned char *raw; 00163 } nh; 00164 00165 /* Link layer header */ 00166 union 00167 { 00168 struct ethhdr *ethernet; 00169 unsigned char *raw; 00170 } mac; 00171 00172 struct dst_entry *dst; 00173 00174 /* 00175 * This is the control buffer. It is free to use for every 00176 * layer. Please put your private variables there. If you 00177 * want to keep them across layers you have to do a skb_clone() 00178 * first. This is owned by whoever has the skb queued ATM. 00179 */ 00180 char cb[48]; 00181 00182 unsigned int len; /* Length of actual data */ 00183 unsigned int data_len; 00184 unsigned int csum; /* Checksum */ 00185 unsigned char __unused, /* Dead field, may be reused */ 00186 cloned, /* head may be cloned (check refcnt to be sure). */ 00187 pkt_type, /* Packet class */ 00188 ip_summed; /* Driver fed us an IP checksum */ 00189 __u32 priority; /* Packet queueing priority */ 00190 atomic_t users; /* User count - see datagram.c,tcp.c */ 00191 unsigned short protocol; /* Packet protocol from driver. */ 00192 unsigned short security; /* Security level of packet */ 00193 unsigned int truesize; /* Buffer size */ 00194 00195 unsigned char *head; /* Head of buffer */ 00196 unsigned char *data; /* Data head pointer */ 00197 unsigned char *tail; /* Tail pointer */ 00198 unsigned char *end; /* End pointer */ 00199 00200 void (*destructor)(struct sk_buff *); /* Destruct function */ 00201 #ifdef CONFIG_NETFILTER 00202 /* Can be used for communication between hooks. */ 00203 unsigned long nfmark; 00204 /* Cache info */ 00205 __u32 nfcache; 00206 /* Associated connection, if any */ 00207 struct nf_ct_info *nfct; 00208 #ifdef CONFIG_NETFILTER_DEBUG 00209 unsigned int nf_debug; 00210 #endif 00211 #endif /*CONFIG_NETFILTER*/ 00212 00213 #if defined(CONFIG_HIPPI) 00214 union{ 00215 __u32 ifield; 00216 } private; 00217 #endif 00218 00219 #ifdef CONFIG_NET_SCHED 00220 __u32 tc_index; /* traffic control index */ 00221 #endif 00222 }; 00223 00224 #define SK_WMEM_MAX 65535 00225 #define SK_RMEM_MAX 65535 00226 00227 #ifdef __KERNEL__ 00228 /* 00229 * Handling routines are only of interest to the kernel 00230 */ 00231 #include <linux/slab.h> 00232 00233 #include <asm/system.h> 00234 00235 extern void __kfree_skb(struct sk_buff *skb); 00236 extern struct sk_buff * alloc_skb(unsigned int size, int priority); 00237 extern void kfree_skbmem(struct sk_buff *skb); 00238 extern struct sk_buff * skb_clone(struct sk_buff *skb, int priority); 00239 extern struct sk_buff * skb_copy(const struct sk_buff *skb, int priority); 00240 extern struct sk_buff * pskb_copy(struct sk_buff *skb, int gfp_mask); 00241 extern int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail, int gfp_mask); 00242 extern struct sk_buff * skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom); 00243 extern struct sk_buff * skb_copy_expand(const struct sk_buff *skb, 00244 int newheadroom, 00245 int newtailroom, 00246 int priority); 00247 extern struct sk_buff * skb_pad(struct sk_buff *skb, int pad); 00248 #define dev_kfree_skb(a) kfree_skb(a) 00249 extern void skb_over_panic(struct sk_buff *skb, int len, void *here); 00250 extern void skb_under_panic(struct sk_buff *skb, int len, void *here); 00251 00252 /* Internal */ 00253 #define skb_shinfo(SKB) ((struct skb_shared_info *)((SKB)->end)) 00254 00255 /* 00256 * skb_queue_empty - check if a queue is empty 00257 * @list: queue head 00258 * 00259 * Returns true if the queue is empty, false otherwise. 00260 */ 00261 00262 static inline int skb_queue_empty(struct sk_buff_head *list) 00263 { 00264 return (list->next == (struct sk_buff *) list); 00265 } 00266 00267 /* 00268 * skb_get - reference buffer 00269 * @skb: buffer to reference 00270 * 00271 * Makes another reference to a socket buffer and returns a pointer 00272 * to the buffer. 00273 */ 00274 00275 static inline struct sk_buff *skb_get(struct sk_buff *skb) 00276 { 00277 atomic_inc(&skb->users); 00278 return skb; 00279 } 00280 00281 /* 00282 * If users==1, we are the only owner and are can avoid redundant 00283 * atomic change. 00284 */ 00285 00286 /* 00287 * kfree_skb - free an sk_buff 00288 * @skb: buffer to free 00289 * 00290 * Drop a reference to the buffer and free it if the usage count has 00291 * hit zero. 00292 */ 00293 00294 static inline void kfree_skb(struct sk_buff *skb) 00295 { 00296 if (atomic_read(&skb->users) == 1 || atomic_dec_and_test(&skb->users)) 00297 __kfree_skb(skb); 00298 } 00299 00300 /* Use this if you didn't touch the skb state [for fast switching] */ 00301 static inline void kfree_skb_fast(struct sk_buff *skb) 00302 { 00303 if (atomic_read(&skb->users) == 1 || atomic_dec_and_test(&skb->users)) 00304 kfree_skbmem(skb); 00305 } 00306 00307 /* 00308 * skb_cloned - is the buffer a clone 00309 * @skb: buffer to check 00310 * 00311 * Returns true if the buffer was generated with skb_clone() and is 00312 * one of multiple shared copies of the buffer. Cloned buffers are 00313 * shared data so must not be written to under normal circumstances. 00314 */ 00315 00316 static inline int skb_cloned(struct sk_buff *skb) 00317 { 00318 return skb->cloned && atomic_read(&skb_shinfo(skb)->dataref) != 1; 00319 } 00320 00321 /* 00322 * skb_shared - is the buffer shared 00323 * @skb: buffer to check 00324 * 00325 * Returns true if more than one person has a reference to this 00326 * buffer. 00327 */ 00328 00329 static inline int skb_shared(struct sk_buff *skb) 00330 { 00331 return (atomic_read(&skb->users) != 1); 00332 } 00333 00334 /* 00335 * skb_share_check - check if buffer is shared and if so clone it 00336 * @skb: buffer to check 00337 * @pri: priority for memory allocation 00338 * 00339 * If the buffer is shared the buffer is cloned and the old copy 00340 * drops a reference. A new clone with a single reference is returned. 00341 * If the buffer is not shared the original buffer is returned. When 00342 * being called from interrupt status or with spinlocks held pri must 00343 * be GFP_ATOMIC. 00344 * 00345 * NULL is returned on a memory allocation failure. 00346 */ 00347 00348 static inline struct sk_buff *skb_share_check(struct sk_buff *skb, int pri) 00349 { 00350 if (skb_shared(skb)) { 00351 struct sk_buff *nskb; 00352 nskb = skb_clone(skb, pri); 00353 kfree_skb(skb); 00354 return nskb; 00355 } 00356 return skb; 00357 } 00358 00359 00360 /* 00361 * Copy shared buffers into a new sk_buff. We effectively do COW on 00362 * packets to handle cases where we have a local reader and forward 00363 * and a couple of other messy ones. The normal one is tcpdumping 00364 * a packet thats being forwarded. 00365 */ 00366 00367 /* 00368 * skb_unshare - make a copy of a shared buffer 00369 * @skb: buffer to check 00370 * @pri: priority for memory allocation 00371 * 00372 * If the socket buffer is a clone then this function creates a new 00373 * copy of the data, drops a reference count on the old copy and returns 00374 * the new copy with the reference count at 1. If the buffer is not a clone 00375 * the original buffer is returned. When called with a spinlock held or 00376 * from interrupt state @pri must be %GFP_ATOMIC 00377 * 00378 * %NULL is returned on a memory allocation failure. 00379 */ 00380 00381 static inline struct sk_buff *skb_unshare(struct sk_buff *skb, int pri) 00382 { 00383 struct sk_buff *nskb; 00384 if(!skb_cloned(skb)) 00385 return skb; 00386 nskb=skb_copy(skb, pri); 00387 kfree_skb(skb); /* Free our shared copy */ 00388 return nskb; 00389 } 00390 00391 /* 00392 * skb_peek 00393 * @list_: list to peek at 00394 * 00395 * Peek an &sk_buff. Unlike most other operations you _MUST_ 00396 * be careful with this one. A peek leaves the buffer on the 00397 * list and someone else may run off with it. You must hold 00398 * the appropriate locks or have a private queue to do this. 00399 * 00400 * Returns %NULL for an empty list or a pointer to the head element. 00401 * The reference count is not incremented and the reference is therefore 00402 * volatile. Use with caution. 00403 */ 00404 00405 static inline struct sk_buff *skb_peek(struct sk_buff_head *list_) 00406 { 00407 struct sk_buff *list = ((struct sk_buff *)list_)->next; 00408 if (list == (struct sk_buff *)list_) 00409 list = NULL; 00410 return list; 00411 } 00412 00413 /* 00414 * skb_peek_tail 00415 * @list_: list to peek at 00416 * 00417 * Peek an &sk_buff. Unlike most other operations you _MUST_ 00418 * be careful with this one. A peek leaves the buffer on the 00419 * list and someone else may run off with it. You must hold 00420 * the appropriate locks or have a private queue to do this. 00421 * 00422 * Returns %NULL for an empty list or a pointer to the tail element. 00423 * The reference count is not incremented and the reference is therefore 00424 * volatile. Use with caution. 00425 */ 00426 00427 static inline struct sk_buff *skb_peek_tail(struct sk_buff_head *list_) 00428 { 00429 struct sk_buff *list = ((struct sk_buff *)list_)->prev; 00430 if (list == (struct sk_buff *)list_) 00431 list = NULL; 00432 return list; 00433 } 00434 00435 /* 00436 * skb_queue_len - get queue length 00437 * @list_: list to measure 00438 * 00439 * Return the length of an &sk_buff queue. 00440 */ 00441 00442 static inline __u32 skb_queue_len(struct sk_buff_head *list_) 00443 { 00444 return(list_->qlen); 00445 } 00446 00447 static inline void skb_queue_head_init(struct sk_buff_head *list) 00448 { 00449 spin_lock_init(&list->lock); 00450 list->prev = (struct sk_buff *)list; 00451 list->next = (struct sk_buff *)list; 00452 list->qlen = 0; 00453 } 00454 00455 /* 00456 * Insert an sk_buff at the start of a list. 00457 * 00458 * The "__skb_xxxx()" functions are the non-atomic ones that 00459 * can only be called with interrupts disabled. 00460 */ 00461 00462 /* 00463 * __skb_queue_head - queue a buffer at the list head 00464 * @list: list to use 00465 * @newsk: buffer to queue 00466 * 00467 * Queue a buffer at the start of a list. This function takes no locks 00468 * and you must therefore hold required locks before calling it. 00469 * 00470 * A buffer cannot be placed on two lists at the same time. 00471 */ 00472 00473 static inline void __skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk) 00474 { 00475 struct sk_buff *prev, *next; 00476 00477 newsk->list = list; 00478 list->qlen++; 00479 prev = (struct sk_buff *)list; 00480 next = prev->next; 00481 newsk->next = next; 00482 newsk->prev = prev; 00483 next->prev = newsk; 00484 prev->next = newsk; 00485 } 00486 00487 00488 /* 00489 * skb_queue_head - queue a buffer at the list head 00490 * @list: list to use 00491 * @newsk: buffer to queue 00492 * 00493 * Queue a buffer at the start of the list. This function takes the 00494 * list lock and can be used safely with other locking &sk_buff functions 00495 * safely. 00496 * 00497 * A buffer cannot be placed on two lists at the same time. 00498 */ 00499 00500 static inline void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk) 00501 { 00502 unsigned long flags; 00503 00504 spin_lock_irqsave(&list->lock, flags); 00505 __skb_queue_head(list, newsk); 00506 spin_unlock_irqrestore(&list->lock, flags); 00507 } 00508 00509 /* 00510 * __skb_queue_tail - queue a buffer at the list tail 00511 * @list: list to use 00512 * @newsk: buffer to queue 00513 * 00514 * Queue a buffer at the end of a list. This function takes no locks 00515 * and you must therefore hold required locks before calling it. 00516 * 00517 * A buffer cannot be placed on two lists at the same time. 00518 */ 00519 00520 00521 static inline void __skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk) 00522 { 00523 struct sk_buff *prev, *next; 00524 00525 newsk->list = list; 00526 list->qlen++; 00527 next = (struct sk_buff *)list; 00528 prev = next->prev; 00529 newsk->next = next; 00530 newsk->prev = prev; 00531 next->prev = newsk; 00532 prev->next = newsk; 00533 } 00534 00535 /* 00536 * skb_queue_tail - queue a buffer at the list tail 00537 * @list: list to use 00538 * @newsk: buffer to queue 00539 * 00540 * Queue a buffer at the tail of the list. This function takes the 00541 * list lock and can be used safely with other locking &sk_buff functions 00542 * safely. 00543 * 00544 * A buffer cannot be placed on two lists at the same time. 00545 */ 00546 00547 static inline void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk) 00548 { 00549 unsigned long flags; 00550 00551 spin_lock_irqsave(&list->lock, flags); 00552 __skb_queue_tail(list, newsk); 00553 spin_unlock_irqrestore(&list->lock, flags); 00554 } 00555 00556 /* 00557 * __skb_dequeue - remove from the head of the queue 00558 * @list: list to dequeue from 00559 * 00560 * Remove the head of the list. This function does not take any locks 00561 * so must be used with appropriate locks held only. The head item is 00562 * returned or %NULL if the list is empty. 00563 */ 00564 00565 static inline struct sk_buff *__skb_dequeue(struct sk_buff_head *list) 00566 { 00567 struct sk_buff *next, *prev, *result; 00568 00569 prev = (struct sk_buff *) list; 00570 next = prev->next; 00571 result = NULL; 00572 if (next != prev) { 00573 result = next; 00574 next = next->next; 00575 list->qlen--; 00576 next->prev = prev; 00577 prev->next = next; 00578 result->next = NULL; 00579 result->prev = NULL; 00580 result->list = NULL; 00581 } 00582 return result; 00583 } 00584 00585 /* 00586 * skb_dequeue - remove from the head of the queue 00587 * @list: list to dequeue from 00588 * 00589 * Remove the head of the list. The list lock is taken so the function 00590 * may be used safely with other locking list functions. The head item is 00591 * returned or %NULL if the list is empty. 00592 */ 00593 00594 static inline struct sk_buff *skb_dequeue(struct sk_buff_head *list) 00595 { 00596 unsigned long flags; 00597 struct sk_buff *result; 00598 00599 spin_lock_irqsave(&list->lock, flags); 00600 result = __skb_dequeue(list); 00601 spin_unlock_irqrestore(&list->lock, flags); 00602 return result; 00603 } 00604 00605 /* 00606 * Insert a packet on a list. 00607 */ 00608 00609 static inline void __skb_insert(struct sk_buff *newsk, 00610 struct sk_buff * prev, struct sk_buff *next, 00611 struct sk_buff_head * list) 00612 { 00613 newsk->next = next; 00614 newsk->prev = prev; 00615 next->prev = newsk; 00616 prev->next = newsk; 00617 newsk->list = list; 00618 list->qlen++; 00619 } 00620 00621 /* 00622 * skb_insert - insert a buffer 00623 * @old: buffer to insert before 00624 * @newsk: buffer to insert 00625 * 00626 * Place a packet before a given packet in a list. The list locks are taken 00627 * and this function is atomic with respect to other list locked calls 00628 * A buffer cannot be placed on two lists at the same time. 00629 */ 00630 00631 static inline void skb_insert(struct sk_buff *old, struct sk_buff *newsk) 00632 { 00633 unsigned long flags; 00634 00635 spin_lock_irqsave(&old->list->lock, flags); 00636 __skb_insert(newsk, old->prev, old, old->list); 00637 spin_unlock_irqrestore(&old->list->lock, flags); 00638 } 00639 00640 /* 00641 * Place a packet after a given packet in a list. 00642 */ 00643 00644 static inline void __skb_append(struct sk_buff *old, struct sk_buff *newsk) 00645 { 00646 __skb_insert(newsk, old, old->next, old->list); 00647 } 00648 00649 /* 00650 * skb_append - append a buffer 00651 * @old: buffer to insert after 00652 * @newsk: buffer to insert 00653 * 00654 * Place a packet after a given packet in a list. The list locks are taken 00655 * and this function is atomic with respect to other list locked calls. 00656 * A buffer cannot be placed on two lists at the same time. 00657 */ 00658 00659 00660 static inline void skb_append(struct sk_buff *old, struct sk_buff *newsk) 00661 { 00662 unsigned long flags; 00663 00664 spin_lock_irqsave(&old->list->lock, flags); 00665 __skb_append(old, newsk); 00666 spin_unlock_irqrestore(&old->list->lock, flags); 00667 } 00668 00669 /* 00670 * remove sk_buff from list. _Must_ be called atomically, and with 00671 * the list known.. 00672 */ 00673 00674 static inline void __skb_unlink(struct sk_buff *skb, struct sk_buff_head *list) 00675 { 00676 struct sk_buff * next, * prev; 00677 00678 list->qlen--; 00679 next = skb->next; 00680 prev = skb->prev; 00681 skb->next = NULL; 00682 skb->prev = NULL; 00683 skb->list = NULL; 00684 next->prev = prev; 00685 prev->next = next; 00686 } 00687 00688 /* 00689 * skb_unlink - remove a buffer from a list 00690 * @skb: buffer to remove 00691 * 00692 * Place a packet after a given packet in a list. The list locks are taken 00693 * and this function is atomic with respect to other list locked calls 00694 * 00695 * Works even without knowing the list it is sitting on, which can be 00696 * handy at times. It also means that THE LIST MUST EXIST when you 00697 * unlink. Thus a list must have its contents unlinked before it is 00698 * destroyed. 00699 */ 00700 00701 static inline void skb_unlink(struct sk_buff *skb) 00702 { 00703 struct sk_buff_head *list = skb->list; 00704 00705 if(list) { 00706 unsigned long flags; 00707 00708 spin_lock_irqsave(&list->lock, flags); 00709 if(skb->list == list) 00710 __skb_unlink(skb, skb->list); 00711 spin_unlock_irqrestore(&list->lock, flags); 00712 } 00713 } 00714 00715 /* XXX: more streamlined implementation */ 00716 00717 /* 00718 * __skb_dequeue_tail - remove from the tail of the queue 00719 * @list: list to dequeue from 00720 * 00721 * Remove the tail of the list. This function does not take any locks 00722 * so must be used with appropriate locks held only. The tail item is 00723 * returned or %NULL if the list is empty. 00724 */ 00725 00726 static inline struct sk_buff *__skb_dequeue_tail(struct sk_buff_head *list) 00727 { 00728 struct sk_buff *skb = skb_peek_tail(list); 00729 if (skb) 00730 __skb_unlink(skb, list); 00731 return skb; 00732 } 00733 00734 /* 00735 * skb_dequeue - remove from the head of the queue 00736 * @list: list to dequeue from 00737 * 00738 * Remove the head of the list. The list lock is taken so the function 00739 * may be used safely with other locking list functions. The tail item is 00740 * returned or %NULL if the list is empty. 00741 */ 00742 00743 static inline struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list) 00744 { 00745 unsigned long flags; 00746 struct sk_buff *result; 00747 00748 spin_lock_irqsave(&list->lock, flags); 00749 result = __skb_dequeue_tail(list); 00750 spin_unlock_irqrestore(&list->lock, flags); 00751 return result; 00752 } 00753 00754 static inline int skb_is_nonlinear(const struct sk_buff *skb) 00755 { 00756 return skb->data_len; 00757 } 00758 00759 static inline unsigned int skb_headlen(const struct sk_buff *skb) 00760 { 00761 return skb->len - skb->data_len; 00762 } 00763 00764 #define SKB_PAGE_ASSERT(skb) do { if (skb_shinfo(skb)->nr_frags) out_of_line_bug(); } while (0) 00765 #define SKB_FRAG_ASSERT(skb) do { if (skb_shinfo(skb)->frag_list) out_of_line_bug(); } while (0) 00766 #define SKB_LINEAR_ASSERT(skb) do { if (skb_is_nonlinear(skb)) out_of_line_bug(); } while (0) 00767 00768 /* 00769 * Add data to an sk_buff 00770 */ 00771 00772 static inline unsigned char *__skb_put(struct sk_buff *skb, unsigned int len) 00773 { 00774 unsigned char *tmp=skb->tail; 00775 SKB_LINEAR_ASSERT(skb); 00776 skb->tail+=len; 00777 skb->len+=len; 00778 return tmp; 00779 } 00780 00781 /* 00782 * skb_put - add data to a buffer 00783 * @skb: buffer to use 00784 * @len: amount of data to add 00785 * 00786 * This function extends the used data area of the buffer. If this would 00787 * exceed the total buffer size the kernel will panic. A pointer to the 00788 * first byte of the extra data is returned. 00789 */ 00790 00791 static inline unsigned char *skb_put(struct sk_buff *skb, unsigned int len) 00792 { 00793 unsigned char *tmp=skb->tail; 00794 SKB_LINEAR_ASSERT(skb); 00795 skb->tail+=len; 00796 skb->len+=len; 00797 if(skb->tail>skb->end) { 00798 skb_over_panic(skb, len, current_text_addr()); 00799 } 00800 return tmp; 00801 } 00802 00803 static inline unsigned char *__skb_push(struct sk_buff *skb, unsigned int len) 00804 { 00805 skb->data-=len; 00806 skb->len+=len; 00807 return skb->data; 00808 } 00809 00810 /* 00811 * skb_push - add data to the start of a buffer 00812 * @skb: buffer to use 00813 * @len: amount of data to add 00814 * 00815 * This function extends the used data area of the buffer at the buffer 00816 * start. If this would exceed the total buffer headroom the kernel will 00817 * panic. A pointer to the first byte of the extra data is returned. 00818 */ 00819 00820 static inline unsigned char *skb_push(struct sk_buff *skb, unsigned int len) 00821 { 00822 skb->data-=len; 00823 skb->len+=len; 00824 if(skb->data<skb->head) { 00825 skb_under_panic(skb, len, current_text_addr()); 00826 } 00827 return skb->data; 00828 } 00829 00830 static inline char *__skb_pull(struct sk_buff *skb, unsigned int len) 00831 { 00832 skb->len-=len; 00833 if (skb->len < skb->data_len) 00834 out_of_line_bug(); 00835 return skb->data+=len; 00836 } 00837 00838 /* 00839 * skb_pull - remove data from the start of a buffer 00840 * @skb: buffer to use 00841 * @len: amount of data to remove 00842 * 00843 * This function removes data from the start of a buffer, returning 00844 * the memory to the headroom. A pointer to the next data in the buffer 00845 * is returned. Once the data has been pulled future pushes will overwrite 00846 * the old data. 00847 */ 00848 00849 static inline unsigned char * skb_pull(struct sk_buff *skb, unsigned int len) 00850 { 00851 if (len > skb->len) 00852 return NULL; 00853 return __skb_pull(skb,len); 00854 } 00855 00856 extern unsigned char * __pskb_pull_tail(struct sk_buff *skb, int delta); 00857 00858 static inline char *__pskb_pull(struct sk_buff *skb, unsigned int len) 00859 { 00860 if (len > skb_headlen(skb) && 00861 __pskb_pull_tail(skb, len-skb_headlen(skb)) == NULL) 00862 return NULL; 00863 skb->len -= len; 00864 return skb->data += len; 00865 } 00866 00867 static inline unsigned char * pskb_pull(struct sk_buff *skb, unsigned int len) 00868 { 00869 if (len > skb->len) 00870 return NULL; 00871 return __pskb_pull(skb,len); 00872 } 00873 00874 static inline int pskb_may_pull(struct sk_buff *skb, unsigned int len) 00875 { 00876 if (len <= skb_headlen(skb)) 00877 return 1; 00878 if (len > skb->len) 00879 return 0; 00880 return (__pskb_pull_tail(skb, len-skb_headlen(skb)) != NULL); 00881 } 00882 00883 /* 00884 * skb_headroom - bytes at buffer head 00885 * @skb: buffer to check 00886 * 00887 * Return the number of bytes of free space at the head of an &sk_buff. 00888 */ 00889 00890 static inline int skb_headroom(const struct sk_buff *skb) 00891 { 00892 return skb->data-skb->head; 00893 } 00894 00895 /* 00896 * skb_tailroom - bytes at buffer end 00897 * @skb: buffer to check 00898 * 00899 * Return the number of bytes of free space at the tail of an sk_buff 00900 */ 00901 00902 static inline int skb_tailroom(const struct sk_buff *skb) 00903 { 00904 return skb_is_nonlinear(skb) ? 0 : skb->end-skb->tail; 00905 } 00906 00907 /* 00908 * skb_reserve - adjust headroom 00909 * @skb: buffer to alter 00910 * @len: bytes to move 00911 * 00912 * Increase the headroom of an empty &sk_buff by reducing the tail 00913 * room. This is only allowed for an empty buffer. 00914 */ 00915 00916 static inline void skb_reserve(struct sk_buff *skb, unsigned int len) 00917 { 00918 skb->data+=len; 00919 skb->tail+=len; 00920 } 00921 00922 extern int ___pskb_trim(struct sk_buff *skb, unsigned int len, int realloc); 00923 00924 static inline void __skb_trim(struct sk_buff *skb, unsigned int len) 00925 { 00926 if (!skb->data_len) { 00927 skb->len = len; 00928 skb->tail = skb->data+len; 00929 } else { 00930 ___pskb_trim(skb, len, 0); 00931 } 00932 } 00933 00934 /* 00935 * skb_trim - remove end from a buffer 00936 * @skb: buffer to alter 00937 * @len: new length 00938 * 00939 * Cut the length of a buffer down by removing data from the tail. If 00940 * the buffer is already under the length specified it is not modified. 00941 */ 00942 00943 static inline void skb_trim(struct sk_buff *skb, unsigned int len) 00944 { 00945 if (skb->len > len) { 00946 __skb_trim(skb, len); 00947 } 00948 } 00949 00950 00951 static inline int __pskb_trim(struct sk_buff *skb, unsigned int len) 00952 { 00953 if (!skb->data_len) { 00954 skb->len = len; 00955 skb->tail = skb->data+len; 00956 return 0; 00957 } else { 00958 return ___pskb_trim(skb, len, 1); 00959 } 00960 } 00961 00962 static inline int pskb_trim(struct sk_buff *skb, unsigned int len) 00963 { 00964 if (len < skb->len) 00965 return __pskb_trim(skb, len); 00966 return 0; 00967 } 00968 00969 /* 00970 * skb_orphan - orphan a buffer 00971 * @skb: buffer to orphan 00972 * 00973 * If a buffer currently has an owner then we call the owner's 00974 * destructor function and make the @skb unowned. The buffer continues 00975 * to exist but is no longer charged to its former owner. 00976 */ 00977 00978 00979 static inline void skb_orphan(struct sk_buff *skb) 00980 { 00981 if (skb->destructor) 00982 skb->destructor(skb); 00983 skb->destructor = NULL; 00984 skb->sk = NULL; 00985 } 00986 00987 /* 00988 * skb_purge - empty a list 00989 * @list: list to empty 00990 * 00991 * Delete all buffers on an &sk_buff list. Each buffer is removed from 00992 * the list and one reference dropped. This function takes the list 00993 * lock and is atomic with respect to other list locking functions. 00994 */ 00995 00996 00997 static inline void skb_queue_purge(struct sk_buff_head *list) 00998 { 00999 struct sk_buff *skb; 01000 while ((skb=skb_dequeue(list))!=NULL) 01001 kfree_skb(skb); 01002 } 01003 01004 /* 01005 * __skb_purge - empty a list 01006 * @list: list to empty 01007 * 01008 * Delete all buffers on an &sk_buff list. Each buffer is removed from 01009 * the list and one reference dropped. This function does not take the 01010 * list lock and the caller must hold the relevant locks to use it. 01011 */ 01012 01013 01014 static inline void __skb_queue_purge(struct sk_buff_head *list) 01015 { 01016 struct sk_buff *skb; 01017 while ((skb=__skb_dequeue(list))!=NULL) 01018 kfree_skb(skb); 01019 } 01020 01021 /* 01022 * __dev_alloc_skb - allocate an skbuff for sending 01023 * @length: length to allocate 01024 * @gfp_mask: get_free_pages mask, passed to alloc_skb 01025 * 01026 * Allocate a new &sk_buff and assign it a usage count of one. The 01027 * buffer has unspecified headroom built in. Users should allocate 01028 * the headroom they think they need without accounting for the 01029 * built in space. The built in space is used for optimisations. 01030 * 01031 * %NULL is returned in there is no free memory. 01032 */ 01033 01034 static inline struct sk_buff *__dev_alloc_skb(unsigned int length, 01035 int gfp_mask) 01036 { 01037 struct sk_buff *skb; 01038 01039 skb = alloc_skb(length+16, gfp_mask); 01040 if (skb) 01041 skb_reserve(skb,16); 01042 return skb; 01043 } 01044 01045 /* 01046 * dev_alloc_skb - allocate an skbuff for sending 01047 * @length: length to allocate 01048 * 01049 * Allocate a new &sk_buff and assign it a usage count of one. The 01050 * buffer has unspecified headroom built in. Users should allocate 01051 * the headroom they think they need without accounting for the 01052 * built in space. The built in space is used for optimisations. 01053 * 01054 * %NULL is returned in there is no free memory. Although this function 01055 * allocates memory it can be called from an interrupt. 01056 */ 01057 01058 static inline struct sk_buff *dev_alloc_skb(unsigned int length) 01059 { 01060 return __dev_alloc_skb(length, GFP_ATOMIC); 01061 } 01062 01063 /* 01064 * skb_cow - copy header of skb when it is required 01065 * @skb: buffer to cow 01066 * @headroom: needed headroom 01067 * 01068 * If the skb passed lacks sufficient headroom or its data part 01069 * is shared, data is reallocated. If reallocation fails, an error 01070 * is returned and original skb is not changed. 01071 * 01072 * The result is skb with writable area skb->head...skb->tail 01073 * and at least @headroom of space at head. 01074 */ 01075 01076 static inline int 01077 skb_cow(struct sk_buff *skb, unsigned int headroom) 01078 { 01079 int delta = (headroom > 16 ? headroom : 16) - skb_headroom(skb); 01080 01081 if (delta < 0) 01082 delta = 0; 01083 01084 if (delta || skb_cloned(skb)) 01085 return pskb_expand_head(skb, (delta+15)&~15, 0, GFP_ATOMIC); 01086 return 0; 01087 } 01088 01089 /* 01090 * skb_padto - pad an skbuff up to a minimal size 01091 * @skb: buffer to pad 01092 * @len: minimal length 01093 * 01094 * Pads up a buffer to ensure the trailing bytes exist and are 01095 * blanked. If the buffer already contains sufficient data it 01096 * is untouched. Returns the buffer, which may be a replacement 01097 * for the original, or NULL for out of memory - in which case 01098 * the original buffer is still freed. 01099 */ 01100 01101 static inline struct sk_buff *skb_padto(struct sk_buff *skb, unsigned int len) 01102 { 01103 unsigned int size = skb->len; 01104 if(likely(size >= len)) 01105 return skb; 01106 return skb_pad(skb, len-size); 01107 } 01108 01109 /* 01110 * skb_linearize - convert paged skb to linear one 01111 * @skb: buffer to linarize 01112 * @gfp: allocation mode 01113 * 01114 * If there is no free memory -ENOMEM is returned, otherwise zero 01115 * is returned and the old skb data released. */ 01116 int skb_linearize(struct sk_buff *skb, int gfp); 01117 01118 static inline void *kmap_skb_frag(const skb_frag_t *frag) 01119 { 01120 #ifdef CONFIG_HIGHMEM 01121 if (in_irq()) 01122 out_of_line_bug(); 01123 01124 local_bh_disable(); 01125 #endif 01126 return kmap_atomic(frag->page, KM_SKB_DATA_SOFTIRQ); 01127 } 01128 01129 static inline void kunmap_skb_frag(void *vaddr) 01130 { 01131 kunmap_atomic(vaddr, KM_SKB_DATA_SOFTIRQ); 01132 #ifdef CONFIG_HIGHMEM 01133 local_bh_enable(); 01134 #endif 01135 } 01136 01137 #define skb_queue_walk(queue, skb) \ 01138 for (skb = (queue)->next; \ 01139 (skb != (struct sk_buff *)(queue)); \ 01140 skb=skb->next) 01141 01142 01143 extern struct sk_buff * skb_recv_datagram(struct sock *sk,unsigned flags,int noblock, int *err); 01144 extern unsigned int datagram_poll(struct file *file, struct socket *sock, struct poll_table_struct *wait); 01145 extern int skb_copy_datagram(const struct sk_buff *from, int offset, char *to,int size); 01146 extern int skb_copy_datagram_iovec(const struct sk_buff *from, int offset, struct iovec *to,int size); 01147 extern int skb_copy_and_csum_datagram(const struct sk_buff *skb, int offset, u8 *to, int len, unsigned int *csump); 01148 extern int skb_copy_and_csum_datagram_iovec(const struct sk_buff *skb, int hlen, struct iovec *iov); 01149 extern void skb_free_datagram(struct sock * sk, struct sk_buff *skb); 01150 01151 extern unsigned int skb_checksum(const struct sk_buff *skb, int offset, int len, unsigned int csum); 01152 extern int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len); 01153 extern unsigned int skb_copy_and_csum_bits(const struct sk_buff *skb, int offset, u8 *to, int len, unsigned int csum); 01154 extern void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to); 01155 01156 extern void skb_init(void); 01157 extern void skb_add_mtu(int mtu); 01158 01159 #ifdef CONFIG_NETFILTER 01160 static inline void 01161 nf_conntrack_put(struct nf_ct_info *nfct) 01162 { 01163 if (nfct && atomic_dec_and_test(&nfct->master->use)) 01164 nfct->master->destroy(nfct->master); 01165 } 01166 static inline void 01167 nf_conntrack_get(struct nf_ct_info *nfct) 01168 { 01169 if (nfct) 01170 atomic_inc(&nfct->master->use); 01171 } 01172 #endif 01173 01174 #endif /* __KERNEL__ */ 01175 #endif /* _LINUX_SKBUFF_H */

Generated on Wed Dec 1 21:25:32 2004 for Linux 2.4.23 Networking by doxygen 1.3.8