Don't include linux/config.h from anywhere else in include/
[linux-2.6/pdupreez.git] / include / linux / skbuff.h
blob4dc65b55812e6585498582da7d5f8fd76225cc86
1 /*
2 * Definitions for the 'struct sk_buff' memory handlers.
4 * Authors:
5 * Alan Cox, <gw4pts@gw4pts.ampr.org>
6 * Florian La Roche, <rzsfl@rz.uni-sb.de>
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
14 #ifndef _LINUX_SKBUFF_H
15 #define _LINUX_SKBUFF_H
17 #include <linux/kernel.h>
18 #include <linux/compiler.h>
19 #include <linux/time.h>
20 #include <linux/cache.h>
22 #include <asm/atomic.h>
23 #include <asm/types.h>
24 #include <linux/spinlock.h>
25 #include <linux/mm.h>
26 #include <linux/highmem.h>
27 #include <linux/poll.h>
28 #include <linux/net.h>
29 #include <linux/textsearch.h>
30 #include <net/checksum.h>
32 #define HAVE_ALLOC_SKB /* For the drivers to know */
33 #define HAVE_ALIGNABLE_SKB /* Ditto 8) */
35 #define CHECKSUM_NONE 0
36 #define CHECKSUM_HW 1
37 #define CHECKSUM_UNNECESSARY 2
39 #define SKB_DATA_ALIGN(X) (((X) + (SMP_CACHE_BYTES - 1)) & \
40 ~(SMP_CACHE_BYTES - 1))
41 #define SKB_MAX_ORDER(X, ORDER) (((PAGE_SIZE << (ORDER)) - (X) - \
42 sizeof(struct skb_shared_info)) & \
43 ~(SMP_CACHE_BYTES - 1))
44 #define SKB_MAX_HEAD(X) (SKB_MAX_ORDER((X), 0))
45 #define SKB_MAX_ALLOC (SKB_MAX_ORDER(0, 2))
47 /* A. Checksumming of received packets by device.
49 * NONE: device failed to checksum this packet.
50 * skb->csum is undefined.
52 * UNNECESSARY: device parsed packet and wouldbe verified checksum.
53 * skb->csum is undefined.
54 * It is bad option, but, unfortunately, many of vendors do this.
55 * Apparently with secret goal to sell you new device, when you
56 * will add new protocol to your host. F.e. IPv6. 8)
58 * HW: the most generic way. Device supplied checksum of _all_
59 * the packet as seen by netif_rx in skb->csum.
60 * NOTE: Even if device supports only some protocols, but
61 * is able to produce some skb->csum, it MUST use HW,
62 * not UNNECESSARY.
64 * B. Checksumming on output.
66 * NONE: skb is checksummed by protocol or csum is not required.
68 * HW: device is required to csum packet as seen by hard_start_xmit
69 * from skb->h.raw to the end and to record the checksum
70 * at skb->h.raw+skb->csum.
72 * Device must show its capabilities in dev->features, set
73 * at device setup time.
74 * NETIF_F_HW_CSUM - it is clever device, it is able to checksum
75 * everything.
76 * NETIF_F_NO_CSUM - loopback or reliable single hop media.
77 * NETIF_F_IP_CSUM - device is dumb. It is able to csum only
78 * TCP/UDP over IPv4. Sigh. Vendors like this
79 * way by an unknown reason. Though, see comment above
80 * about CHECKSUM_UNNECESSARY. 8)
82 * Any questions? No questions, good. --ANK
85 struct net_device;
87 #ifdef CONFIG_NETFILTER
88 struct nf_conntrack {
89 atomic_t use;
90 void (*destroy)(struct nf_conntrack *);
93 #ifdef CONFIG_BRIDGE_NETFILTER
94 struct nf_bridge_info {
95 atomic_t use;
96 struct net_device *physindev;
97 struct net_device *physoutdev;
98 #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
99 struct net_device *netoutdev;
100 #endif
101 unsigned int mask;
102 unsigned long data[32 / sizeof(unsigned long)];
104 #endif
106 #endif
108 struct sk_buff_head {
109 /* These two members must be first. */
110 struct sk_buff *next;
111 struct sk_buff *prev;
113 __u32 qlen;
114 spinlock_t lock;
117 struct sk_buff;
119 /* To allow 64K frame to be packed as single skb without frag_list */
120 #define MAX_SKB_FRAGS (65536/PAGE_SIZE + 2)
122 typedef struct skb_frag_struct skb_frag_t;
124 struct skb_frag_struct {
125 struct page *page;
126 __u16 page_offset;
127 __u16 size;
130 /* This data is invariant across clones and lives at
131 * the end of the header data, ie. at skb->end.
133 struct skb_shared_info {
134 atomic_t dataref;
135 unsigned short nr_frags;
136 unsigned short tso_size;
137 unsigned short tso_segs;
138 unsigned short ufo_size;
139 unsigned int ip6_frag_id;
140 struct sk_buff *frag_list;
141 skb_frag_t frags[MAX_SKB_FRAGS];
144 /* We divide dataref into two halves. The higher 16 bits hold references
145 * to the payload part of skb->data. The lower 16 bits hold references to
146 * the entire skb->data. It is up to the users of the skb to agree on
147 * where the payload starts.
149 * All users must obey the rule that the skb->data reference count must be
150 * greater than or equal to the payload reference count.
152 * Holding a reference to the payload part means that the user does not
153 * care about modifications to the header part of skb->data.
155 #define SKB_DATAREF_SHIFT 16
156 #define SKB_DATAREF_MASK ((1 << SKB_DATAREF_SHIFT) - 1)
158 struct skb_timeval {
159 u32 off_sec;
160 u32 off_usec;
164 enum {
165 SKB_FCLONE_UNAVAILABLE,
166 SKB_FCLONE_ORIG,
167 SKB_FCLONE_CLONE,
170 /**
171 * struct sk_buff - socket buffer
172 * @next: Next buffer in list
173 * @prev: Previous buffer in list
174 * @sk: Socket we are owned by
175 * @tstamp: Time we arrived
176 * @dev: Device we arrived on/are leaving by
177 * @input_dev: Device we arrived on
178 * @h: Transport layer header
179 * @nh: Network layer header
180 * @mac: Link layer header
181 * @dst: destination entry
182 * @sp: the security path, used for xfrm
183 * @cb: Control buffer. Free for use by every layer. Put private vars here
184 * @len: Length of actual data
185 * @data_len: Data length
186 * @mac_len: Length of link layer header
187 * @csum: Checksum
188 * @local_df: allow local fragmentation
189 * @cloned: Head may be cloned (check refcnt to be sure)
190 * @nohdr: Payload reference only, must not modify header
191 * @pkt_type: Packet class
192 * @fclone: skbuff clone status
193 * @ip_summed: Driver fed us an IP checksum
194 * @priority: Packet queueing priority
195 * @users: User count - see {datagram,tcp}.c
196 * @protocol: Packet protocol from driver
197 * @truesize: Buffer size
198 * @head: Head of buffer
199 * @data: Data head pointer
200 * @tail: Tail pointer
201 * @end: End pointer
202 * @destructor: Destruct function
203 * @nfmark: Can be used for communication between hooks
204 * @nfct: Associated connection, if any
205 * @ipvs_property: skbuff is owned by ipvs
206 * @nfctinfo: Relationship of this skb to the connection
207 * @nfct_reasm: netfilter conntrack re-assembly pointer
208 * @nf_bridge: Saved data about a bridged frame - see br_netfilter.c
209 * @tc_index: Traffic control index
210 * @tc_verd: traffic control verdict
213 struct sk_buff {
214 /* These two members must be first. */
215 struct sk_buff *next;
216 struct sk_buff *prev;
218 struct sock *sk;
219 struct skb_timeval tstamp;
220 struct net_device *dev;
221 struct net_device *input_dev;
223 union {
224 struct tcphdr *th;
225 struct udphdr *uh;
226 struct icmphdr *icmph;
227 struct igmphdr *igmph;
228 struct iphdr *ipiph;
229 struct ipv6hdr *ipv6h;
230 unsigned char *raw;
231 } h;
233 union {
234 struct iphdr *iph;
235 struct ipv6hdr *ipv6h;
236 struct arphdr *arph;
237 unsigned char *raw;
238 } nh;
240 union {
241 unsigned char *raw;
242 } mac;
244 struct dst_entry *dst;
245 struct sec_path *sp;
248 * This is the control buffer. It is free to use for every
249 * layer. Please put your private variables there. If you
250 * want to keep them across layers you have to do a skb_clone()
251 * first. This is owned by whoever has the skb queued ATM.
253 char cb[48];
255 unsigned int len,
256 data_len,
257 mac_len,
258 csum;
259 __u32 priority;
260 __u8 local_df:1,
261 cloned:1,
262 ip_summed:2,
263 nohdr:1,
264 nfctinfo:3;
265 __u8 pkt_type:3,
266 fclone:2,
267 ipvs_property:1;
268 __be16 protocol;
270 void (*destructor)(struct sk_buff *skb);
271 #ifdef CONFIG_NETFILTER
272 struct nf_conntrack *nfct;
273 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
274 struct sk_buff *nfct_reasm;
275 #endif
276 #ifdef CONFIG_BRIDGE_NETFILTER
277 struct nf_bridge_info *nf_bridge;
278 #endif
279 __u32 nfmark;
280 #endif /* CONFIG_NETFILTER */
281 #ifdef CONFIG_NET_SCHED
282 __u16 tc_index; /* traffic control index */
283 #ifdef CONFIG_NET_CLS_ACT
284 __u16 tc_verd; /* traffic control verdict */
285 #endif
286 #endif
289 /* These elements must be at the end, see alloc_skb() for details. */
290 unsigned int truesize;
291 atomic_t users;
292 unsigned char *head,
293 *data,
294 *tail,
295 *end;
298 #ifdef __KERNEL__
300 * Handling routines are only of interest to the kernel
302 #include <linux/slab.h>
304 #include <asm/system.h>
306 extern void kfree_skb(struct sk_buff *skb);
307 extern void __kfree_skb(struct sk_buff *skb);
308 extern struct sk_buff *__alloc_skb(unsigned int size,
309 gfp_t priority, int fclone);
310 static inline struct sk_buff *alloc_skb(unsigned int size,
311 gfp_t priority)
313 return __alloc_skb(size, priority, 0);
316 static inline struct sk_buff *alloc_skb_fclone(unsigned int size,
317 gfp_t priority)
319 return __alloc_skb(size, priority, 1);
322 extern struct sk_buff *alloc_skb_from_cache(kmem_cache_t *cp,
323 unsigned int size,
324 gfp_t priority);
325 extern void kfree_skbmem(struct sk_buff *skb);
326 extern struct sk_buff *skb_clone(struct sk_buff *skb,
327 gfp_t priority);
328 extern struct sk_buff *skb_copy(const struct sk_buff *skb,
329 gfp_t priority);
330 extern struct sk_buff *pskb_copy(struct sk_buff *skb,
331 gfp_t gfp_mask);
332 extern int pskb_expand_head(struct sk_buff *skb,
333 int nhead, int ntail,
334 gfp_t gfp_mask);
335 extern struct sk_buff *skb_realloc_headroom(struct sk_buff *skb,
336 unsigned int headroom);
337 extern struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
338 int newheadroom, int newtailroom,
339 gfp_t priority);
340 extern struct sk_buff * skb_pad(struct sk_buff *skb, int pad);
341 #define dev_kfree_skb(a) kfree_skb(a)
342 extern void skb_over_panic(struct sk_buff *skb, int len,
343 void *here);
344 extern void skb_under_panic(struct sk_buff *skb, int len,
345 void *here);
346 extern void skb_truesize_bug(struct sk_buff *skb);
348 static inline void skb_truesize_check(struct sk_buff *skb)
350 if (unlikely((int)skb->truesize < sizeof(struct sk_buff) + skb->len))
351 skb_truesize_bug(skb);
354 extern int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
355 int getfrag(void *from, char *to, int offset,
356 int len,int odd, struct sk_buff *skb),
357 void *from, int length);
359 struct skb_seq_state
361 __u32 lower_offset;
362 __u32 upper_offset;
363 __u32 frag_idx;
364 __u32 stepped_offset;
365 struct sk_buff *root_skb;
366 struct sk_buff *cur_skb;
367 __u8 *frag_data;
370 extern void skb_prepare_seq_read(struct sk_buff *skb,
371 unsigned int from, unsigned int to,
372 struct skb_seq_state *st);
373 extern unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
374 struct skb_seq_state *st);
375 extern void skb_abort_seq_read(struct skb_seq_state *st);
377 extern unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
378 unsigned int to, struct ts_config *config,
379 struct ts_state *state);
381 /* Internal */
382 #define skb_shinfo(SKB) ((struct skb_shared_info *)((SKB)->end))
385 * skb_queue_empty - check if a queue is empty
386 * @list: queue head
388 * Returns true if the queue is empty, false otherwise.
390 static inline int skb_queue_empty(const struct sk_buff_head *list)
392 return list->next == (struct sk_buff *)list;
396 * skb_get - reference buffer
397 * @skb: buffer to reference
399 * Makes another reference to a socket buffer and returns a pointer
400 * to the buffer.
402 static inline struct sk_buff *skb_get(struct sk_buff *skb)
404 atomic_inc(&skb->users);
405 return skb;
409 * If users == 1, we are the only owner and are can avoid redundant
410 * atomic change.
414 * skb_cloned - is the buffer a clone
415 * @skb: buffer to check
417 * Returns true if the buffer was generated with skb_clone() and is
418 * one of multiple shared copies of the buffer. Cloned buffers are
419 * shared data so must not be written to under normal circumstances.
421 static inline int skb_cloned(const struct sk_buff *skb)
423 return skb->cloned &&
424 (atomic_read(&skb_shinfo(skb)->dataref) & SKB_DATAREF_MASK) != 1;
428 * skb_header_cloned - is the header a clone
429 * @skb: buffer to check
431 * Returns true if modifying the header part of the buffer requires
432 * the data to be copied.
434 static inline int skb_header_cloned(const struct sk_buff *skb)
436 int dataref;
438 if (!skb->cloned)
439 return 0;
441 dataref = atomic_read(&skb_shinfo(skb)->dataref);
442 dataref = (dataref & SKB_DATAREF_MASK) - (dataref >> SKB_DATAREF_SHIFT);
443 return dataref != 1;
447 * skb_header_release - release reference to header
448 * @skb: buffer to operate on
450 * Drop a reference to the header part of the buffer. This is done
451 * by acquiring a payload reference. You must not read from the header
452 * part of skb->data after this.
454 static inline void skb_header_release(struct sk_buff *skb)
456 BUG_ON(skb->nohdr);
457 skb->nohdr = 1;
458 atomic_add(1 << SKB_DATAREF_SHIFT, &skb_shinfo(skb)->dataref);
462 * skb_shared - is the buffer shared
463 * @skb: buffer to check
465 * Returns true if more than one person has a reference to this
466 * buffer.
468 static inline int skb_shared(const struct sk_buff *skb)
470 return atomic_read(&skb->users) != 1;
474 * skb_share_check - check if buffer is shared and if so clone it
475 * @skb: buffer to check
476 * @pri: priority for memory allocation
478 * If the buffer is shared the buffer is cloned and the old copy
479 * drops a reference. A new clone with a single reference is returned.
480 * If the buffer is not shared the original buffer is returned. When
481 * being called from interrupt status or with spinlocks held pri must
482 * be GFP_ATOMIC.
484 * NULL is returned on a memory allocation failure.
486 static inline struct sk_buff *skb_share_check(struct sk_buff *skb,
487 gfp_t pri)
489 might_sleep_if(pri & __GFP_WAIT);
490 if (skb_shared(skb)) {
491 struct sk_buff *nskb = skb_clone(skb, pri);
492 kfree_skb(skb);
493 skb = nskb;
495 return skb;
499 * Copy shared buffers into a new sk_buff. We effectively do COW on
500 * packets to handle cases where we have a local reader and forward
501 * and a couple of other messy ones. The normal one is tcpdumping
502 * a packet thats being forwarded.
506 * skb_unshare - make a copy of a shared buffer
507 * @skb: buffer to check
508 * @pri: priority for memory allocation
510 * If the socket buffer is a clone then this function creates a new
511 * copy of the data, drops a reference count on the old copy and returns
512 * the new copy with the reference count at 1. If the buffer is not a clone
513 * the original buffer is returned. When called with a spinlock held or
514 * from interrupt state @pri must be %GFP_ATOMIC
516 * %NULL is returned on a memory allocation failure.
518 static inline struct sk_buff *skb_unshare(struct sk_buff *skb,
519 gfp_t pri)
521 might_sleep_if(pri & __GFP_WAIT);
522 if (skb_cloned(skb)) {
523 struct sk_buff *nskb = skb_copy(skb, pri);
524 kfree_skb(skb); /* Free our shared copy */
525 skb = nskb;
527 return skb;
531 * skb_peek
532 * @list_: list to peek at
534 * Peek an &sk_buff. Unlike most other operations you _MUST_
535 * be careful with this one. A peek leaves the buffer on the
536 * list and someone else may run off with it. You must hold
537 * the appropriate locks or have a private queue to do this.
539 * Returns %NULL for an empty list or a pointer to the head element.
540 * The reference count is not incremented and the reference is therefore
541 * volatile. Use with caution.
543 static inline struct sk_buff *skb_peek(struct sk_buff_head *list_)
545 struct sk_buff *list = ((struct sk_buff *)list_)->next;
546 if (list == (struct sk_buff *)list_)
547 list = NULL;
548 return list;
552 * skb_peek_tail
553 * @list_: list to peek at
555 * Peek an &sk_buff. Unlike most other operations you _MUST_
556 * be careful with this one. A peek leaves the buffer on the
557 * list and someone else may run off with it. You must hold
558 * the appropriate locks or have a private queue to do this.
560 * Returns %NULL for an empty list or a pointer to the tail element.
561 * The reference count is not incremented and the reference is therefore
562 * volatile. Use with caution.
564 static inline struct sk_buff *skb_peek_tail(struct sk_buff_head *list_)
566 struct sk_buff *list = ((struct sk_buff *)list_)->prev;
567 if (list == (struct sk_buff *)list_)
568 list = NULL;
569 return list;
573 * skb_queue_len - get queue length
574 * @list_: list to measure
576 * Return the length of an &sk_buff queue.
578 static inline __u32 skb_queue_len(const struct sk_buff_head *list_)
580 return list_->qlen;
583 static inline void skb_queue_head_init(struct sk_buff_head *list)
585 spin_lock_init(&list->lock);
586 list->prev = list->next = (struct sk_buff *)list;
587 list->qlen = 0;
591 * Insert an sk_buff at the start of a list.
593 * The "__skb_xxxx()" functions are the non-atomic ones that
594 * can only be called with interrupts disabled.
598 * __skb_queue_after - queue a buffer at the list head
599 * @list: list to use
600 * @prev: place after this buffer
601 * @newsk: buffer to queue
603 * Queue a buffer int the middle of a list. This function takes no locks
604 * and you must therefore hold required locks before calling it.
606 * A buffer cannot be placed on two lists at the same time.
608 static inline void __skb_queue_after(struct sk_buff_head *list,
609 struct sk_buff *prev,
610 struct sk_buff *newsk)
612 struct sk_buff *next;
613 list->qlen++;
615 next = prev->next;
616 newsk->next = next;
617 newsk->prev = prev;
618 next->prev = prev->next = newsk;
622 * __skb_queue_head - queue a buffer at the list head
623 * @list: list to use
624 * @newsk: buffer to queue
626 * Queue a buffer at the start of a list. This function takes no locks
627 * and you must therefore hold required locks before calling it.
629 * A buffer cannot be placed on two lists at the same time.
631 extern void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk);
632 static inline void __skb_queue_head(struct sk_buff_head *list,
633 struct sk_buff *newsk)
635 __skb_queue_after(list, (struct sk_buff *)list, newsk);
639 * __skb_queue_tail - queue a buffer at the list tail
640 * @list: list to use
641 * @newsk: buffer to queue
643 * Queue a buffer at the end of a list. This function takes no locks
644 * and you must therefore hold required locks before calling it.
646 * A buffer cannot be placed on two lists at the same time.
648 extern void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk);
649 static inline void __skb_queue_tail(struct sk_buff_head *list,
650 struct sk_buff *newsk)
652 struct sk_buff *prev, *next;
654 list->qlen++;
655 next = (struct sk_buff *)list;
656 prev = next->prev;
657 newsk->next = next;
658 newsk->prev = prev;
659 next->prev = prev->next = newsk;
664 * __skb_dequeue - remove from the head of the queue
665 * @list: list to dequeue from
667 * Remove the head of the list. This function does not take any locks
668 * so must be used with appropriate locks held only. The head item is
669 * returned or %NULL if the list is empty.
671 extern struct sk_buff *skb_dequeue(struct sk_buff_head *list);
672 static inline struct sk_buff *__skb_dequeue(struct sk_buff_head *list)
674 struct sk_buff *next, *prev, *result;
676 prev = (struct sk_buff *) list;
677 next = prev->next;
678 result = NULL;
679 if (next != prev) {
680 result = next;
681 next = next->next;
682 list->qlen--;
683 next->prev = prev;
684 prev->next = next;
685 result->next = result->prev = NULL;
687 return result;
692 * Insert a packet on a list.
694 extern void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list);
695 static inline void __skb_insert(struct sk_buff *newsk,
696 struct sk_buff *prev, struct sk_buff *next,
697 struct sk_buff_head *list)
699 newsk->next = next;
700 newsk->prev = prev;
701 next->prev = prev->next = newsk;
702 list->qlen++;
706 * Place a packet after a given packet in a list.
708 extern void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list);
709 static inline void __skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
711 __skb_insert(newsk, old, old->next, list);
715 * remove sk_buff from list. _Must_ be called atomically, and with
716 * the list known..
718 extern void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list);
719 static inline void __skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
721 struct sk_buff *next, *prev;
723 list->qlen--;
724 next = skb->next;
725 prev = skb->prev;
726 skb->next = skb->prev = NULL;
727 next->prev = prev;
728 prev->next = next;
732 /* XXX: more streamlined implementation */
735 * __skb_dequeue_tail - remove from the tail of the queue
736 * @list: list to dequeue from
738 * Remove the tail of the list. This function does not take any locks
739 * so must be used with appropriate locks held only. The tail item is
740 * returned or %NULL if the list is empty.
742 extern struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list);
743 static inline struct sk_buff *__skb_dequeue_tail(struct sk_buff_head *list)
745 struct sk_buff *skb = skb_peek_tail(list);
746 if (skb)
747 __skb_unlink(skb, list);
748 return skb;
752 static inline int skb_is_nonlinear(const struct sk_buff *skb)
754 return skb->data_len;
757 static inline unsigned int skb_headlen(const struct sk_buff *skb)
759 return skb->len - skb->data_len;
762 static inline int skb_pagelen(const struct sk_buff *skb)
764 int i, len = 0;
766 for (i = (int)skb_shinfo(skb)->nr_frags - 1; i >= 0; i--)
767 len += skb_shinfo(skb)->frags[i].size;
768 return len + skb_headlen(skb);
771 static inline void skb_fill_page_desc(struct sk_buff *skb, int i,
772 struct page *page, int off, int size)
774 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
776 frag->page = page;
777 frag->page_offset = off;
778 frag->size = size;
779 skb_shinfo(skb)->nr_frags = i + 1;
782 #define SKB_PAGE_ASSERT(skb) BUG_ON(skb_shinfo(skb)->nr_frags)
783 #define SKB_FRAG_ASSERT(skb) BUG_ON(skb_shinfo(skb)->frag_list)
784 #define SKB_LINEAR_ASSERT(skb) BUG_ON(skb_is_nonlinear(skb))
787 * Add data to an sk_buff
789 static inline unsigned char *__skb_put(struct sk_buff *skb, unsigned int len)
791 unsigned char *tmp = skb->tail;
792 SKB_LINEAR_ASSERT(skb);
793 skb->tail += len;
794 skb->len += len;
795 return tmp;
799 * skb_put - add data to a buffer
800 * @skb: buffer to use
801 * @len: amount of data to add
803 * This function extends the used data area of the buffer. If this would
804 * exceed the total buffer size the kernel will panic. A pointer to the
805 * first byte of the extra data is returned.
807 static inline unsigned char *skb_put(struct sk_buff *skb, unsigned int len)
809 unsigned char *tmp = skb->tail;
810 SKB_LINEAR_ASSERT(skb);
811 skb->tail += len;
812 skb->len += len;
813 if (unlikely(skb->tail>skb->end))
814 skb_over_panic(skb, len, current_text_addr());
815 return tmp;
818 static inline unsigned char *__skb_push(struct sk_buff *skb, unsigned int len)
820 skb->data -= len;
821 skb->len += len;
822 return skb->data;
826 * skb_push - add data to the start of a buffer
827 * @skb: buffer to use
828 * @len: amount of data to add
830 * This function extends the used data area of the buffer at the buffer
831 * start. If this would exceed the total buffer headroom the kernel will
832 * panic. A pointer to the first byte of the extra data is returned.
834 static inline unsigned char *skb_push(struct sk_buff *skb, unsigned int len)
836 skb->data -= len;
837 skb->len += len;
838 if (unlikely(skb->data<skb->head))
839 skb_under_panic(skb, len, current_text_addr());
840 return skb->data;
843 static inline unsigned char *__skb_pull(struct sk_buff *skb, unsigned int len)
845 skb->len -= len;
846 BUG_ON(skb->len < skb->data_len);
847 return skb->data += len;
851 * skb_pull - remove data from the start of a buffer
852 * @skb: buffer to use
853 * @len: amount of data to remove
855 * This function removes data from the start of a buffer, returning
856 * the memory to the headroom. A pointer to the next data in the buffer
857 * is returned. Once the data has been pulled future pushes will overwrite
858 * the old data.
860 static inline unsigned char *skb_pull(struct sk_buff *skb, unsigned int len)
862 return unlikely(len > skb->len) ? NULL : __skb_pull(skb, len);
865 extern unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta);
867 static inline unsigned char *__pskb_pull(struct sk_buff *skb, unsigned int len)
869 if (len > skb_headlen(skb) &&
870 !__pskb_pull_tail(skb, len-skb_headlen(skb)))
871 return NULL;
872 skb->len -= len;
873 return skb->data += len;
876 static inline unsigned char *pskb_pull(struct sk_buff *skb, unsigned int len)
878 return unlikely(len > skb->len) ? NULL : __pskb_pull(skb, len);
881 static inline int pskb_may_pull(struct sk_buff *skb, unsigned int len)
883 if (likely(len <= skb_headlen(skb)))
884 return 1;
885 if (unlikely(len > skb->len))
886 return 0;
887 return __pskb_pull_tail(skb, len-skb_headlen(skb)) != NULL;
891 * skb_headroom - bytes at buffer head
892 * @skb: buffer to check
894 * Return the number of bytes of free space at the head of an &sk_buff.
896 static inline int skb_headroom(const struct sk_buff *skb)
898 return skb->data - skb->head;
902 * skb_tailroom - bytes at buffer end
903 * @skb: buffer to check
905 * Return the number of bytes of free space at the tail of an sk_buff
907 static inline int skb_tailroom(const struct sk_buff *skb)
909 return skb_is_nonlinear(skb) ? 0 : skb->end - skb->tail;
913 * skb_reserve - adjust headroom
914 * @skb: buffer to alter
915 * @len: bytes to move
917 * Increase the headroom of an empty &sk_buff by reducing the tail
918 * room. This is only allowed for an empty buffer.
920 static inline void skb_reserve(struct sk_buff *skb, int len)
922 skb->data += len;
923 skb->tail += len;
927 * CPUs often take a performance hit when accessing unaligned memory
928 * locations. The actual performance hit varies, it can be small if the
929 * hardware handles it or large if we have to take an exception and fix it
930 * in software.
932 * Since an ethernet header is 14 bytes network drivers often end up with
933 * the IP header at an unaligned offset. The IP header can be aligned by
934 * shifting the start of the packet by 2 bytes. Drivers should do this
935 * with:
937 * skb_reserve(NET_IP_ALIGN);
939 * The downside to this alignment of the IP header is that the DMA is now
940 * unaligned. On some architectures the cost of an unaligned DMA is high
941 * and this cost outweighs the gains made by aligning the IP header.
943 * Since this trade off varies between architectures, we allow NET_IP_ALIGN
944 * to be overridden.
946 #ifndef NET_IP_ALIGN
947 #define NET_IP_ALIGN 2
948 #endif
951 * The networking layer reserves some headroom in skb data (via
952 * dev_alloc_skb). This is used to avoid having to reallocate skb data when
953 * the header has to grow. In the default case, if the header has to grow
954 * 16 bytes or less we avoid the reallocation.
956 * Unfortunately this headroom changes the DMA alignment of the resulting
957 * network packet. As for NET_IP_ALIGN, this unaligned DMA is expensive
958 * on some architectures. An architecture can override this value,
959 * perhaps setting it to a cacheline in size (since that will maintain
960 * cacheline alignment of the DMA). It must be a power of 2.
962 * Various parts of the networking layer expect at least 16 bytes of
963 * headroom, you should not reduce this.
965 #ifndef NET_SKB_PAD
966 #define NET_SKB_PAD 16
967 #endif
969 extern int ___pskb_trim(struct sk_buff *skb, unsigned int len, int realloc);
971 static inline void __skb_trim(struct sk_buff *skb, unsigned int len)
973 if (!skb->data_len) {
974 skb->len = len;
975 skb->tail = skb->data + len;
976 } else
977 ___pskb_trim(skb, len, 0);
981 * skb_trim - remove end from a buffer
982 * @skb: buffer to alter
983 * @len: new length
985 * Cut the length of a buffer down by removing data from the tail. If
986 * the buffer is already under the length specified it is not modified.
988 static inline void skb_trim(struct sk_buff *skb, unsigned int len)
990 if (skb->len > len)
991 __skb_trim(skb, len);
995 static inline int __pskb_trim(struct sk_buff *skb, unsigned int len)
997 if (!skb->data_len) {
998 skb->len = len;
999 skb->tail = skb->data+len;
1000 return 0;
1002 return ___pskb_trim(skb, len, 1);
1005 static inline int pskb_trim(struct sk_buff *skb, unsigned int len)
1007 return (len < skb->len) ? __pskb_trim(skb, len) : 0;
1011 * skb_orphan - orphan a buffer
1012 * @skb: buffer to orphan
1014 * If a buffer currently has an owner then we call the owner's
1015 * destructor function and make the @skb unowned. The buffer continues
1016 * to exist but is no longer charged to its former owner.
1018 static inline void skb_orphan(struct sk_buff *skb)
1020 if (skb->destructor)
1021 skb->destructor(skb);
1022 skb->destructor = NULL;
1023 skb->sk = NULL;
1027 * __skb_queue_purge - empty a list
1028 * @list: list to empty
1030 * Delete all buffers on an &sk_buff list. Each buffer is removed from
1031 * the list and one reference dropped. This function does not take the
1032 * list lock and the caller must hold the relevant locks to use it.
1034 extern void skb_queue_purge(struct sk_buff_head *list);
1035 static inline void __skb_queue_purge(struct sk_buff_head *list)
1037 struct sk_buff *skb;
1038 while ((skb = __skb_dequeue(list)) != NULL)
1039 kfree_skb(skb);
1042 #ifndef CONFIG_HAVE_ARCH_DEV_ALLOC_SKB
1044 * __dev_alloc_skb - allocate an skbuff for sending
1045 * @length: length to allocate
1046 * @gfp_mask: get_free_pages mask, passed to alloc_skb
1048 * Allocate a new &sk_buff and assign it a usage count of one. The
1049 * buffer has unspecified headroom built in. Users should allocate
1050 * the headroom they think they need without accounting for the
1051 * built in space. The built in space is used for optimisations.
1053 * %NULL is returned in there is no free memory.
1055 static inline struct sk_buff *__dev_alloc_skb(unsigned int length,
1056 gfp_t gfp_mask)
1058 struct sk_buff *skb = alloc_skb(length + NET_SKB_PAD, gfp_mask);
1059 if (likely(skb))
1060 skb_reserve(skb, NET_SKB_PAD);
1061 return skb;
1063 #else
1064 extern struct sk_buff *__dev_alloc_skb(unsigned int length, int gfp_mask);
1065 #endif
1068 * dev_alloc_skb - allocate an skbuff for sending
1069 * @length: length to allocate
1071 * Allocate a new &sk_buff and assign it a usage count of one. The
1072 * buffer has unspecified headroom built in. Users should allocate
1073 * the headroom they think they need without accounting for the
1074 * built in space. The built in space is used for optimisations.
1076 * %NULL is returned in there is no free memory. Although this function
1077 * allocates memory it can be called from an interrupt.
1079 static inline struct sk_buff *dev_alloc_skb(unsigned int length)
1081 return __dev_alloc_skb(length, GFP_ATOMIC);
1085 * skb_cow - copy header of skb when it is required
1086 * @skb: buffer to cow
1087 * @headroom: needed headroom
1089 * If the skb passed lacks sufficient headroom or its data part
1090 * is shared, data is reallocated. If reallocation fails, an error
1091 * is returned and original skb is not changed.
1093 * The result is skb with writable area skb->head...skb->tail
1094 * and at least @headroom of space at head.
1096 static inline int skb_cow(struct sk_buff *skb, unsigned int headroom)
1098 int delta = (headroom > NET_SKB_PAD ? headroom : NET_SKB_PAD) -
1099 skb_headroom(skb);
1101 if (delta < 0)
1102 delta = 0;
1104 if (delta || skb_cloned(skb))
1105 return pskb_expand_head(skb, (delta + (NET_SKB_PAD-1)) &
1106 ~(NET_SKB_PAD-1), 0, GFP_ATOMIC);
1107 return 0;
1111 * skb_padto - pad an skbuff up to a minimal size
1112 * @skb: buffer to pad
1113 * @len: minimal length
1115 * Pads up a buffer to ensure the trailing bytes exist and are
1116 * blanked. If the buffer already contains sufficient data it
1117 * is untouched. Returns the buffer, which may be a replacement
1118 * for the original, or NULL for out of memory - in which case
1119 * the original buffer is still freed.
1122 static inline struct sk_buff *skb_padto(struct sk_buff *skb, unsigned int len)
1124 unsigned int size = skb->len;
1125 if (likely(size >= len))
1126 return skb;
1127 return skb_pad(skb, len-size);
1130 static inline int skb_add_data(struct sk_buff *skb,
1131 char __user *from, int copy)
1133 const int off = skb->len;
1135 if (skb->ip_summed == CHECKSUM_NONE) {
1136 int err = 0;
1137 unsigned int csum = csum_and_copy_from_user(from,
1138 skb_put(skb, copy),
1139 copy, 0, &err);
1140 if (!err) {
1141 skb->csum = csum_block_add(skb->csum, csum, off);
1142 return 0;
1144 } else if (!copy_from_user(skb_put(skb, copy), from, copy))
1145 return 0;
1147 __skb_trim(skb, off);
1148 return -EFAULT;
1151 static inline int skb_can_coalesce(struct sk_buff *skb, int i,
1152 struct page *page, int off)
1154 if (i) {
1155 struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[i - 1];
1157 return page == frag->page &&
1158 off == frag->page_offset + frag->size;
1160 return 0;
1164 * skb_linearize - convert paged skb to linear one
1165 * @skb: buffer to linarize
1166 * @gfp: allocation mode
1168 * If there is no free memory -ENOMEM is returned, otherwise zero
1169 * is returned and the old skb data released.
1171 extern int __skb_linearize(struct sk_buff *skb, gfp_t gfp);
1172 static inline int skb_linearize(struct sk_buff *skb, gfp_t gfp)
1174 return __skb_linearize(skb, gfp);
1178 * skb_postpull_rcsum - update checksum for received skb after pull
1179 * @skb: buffer to update
1180 * @start: start of data before pull
1181 * @len: length of data pulled
1183 * After doing a pull on a received packet, you need to call this to
1184 * update the CHECKSUM_HW checksum, or set ip_summed to CHECKSUM_NONE
1185 * so that it can be recomputed from scratch.
1188 static inline void skb_postpull_rcsum(struct sk_buff *skb,
1189 const void *start, unsigned int len)
1191 if (skb->ip_summed == CHECKSUM_HW)
1192 skb->csum = csum_sub(skb->csum, csum_partial(start, len, 0));
1195 unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len);
1198 * pskb_trim_rcsum - trim received skb and update checksum
1199 * @skb: buffer to trim
1200 * @len: new length
1202 * This is exactly the same as pskb_trim except that it ensures the
1203 * checksum of received packets are still valid after the operation.
1206 static inline int pskb_trim_rcsum(struct sk_buff *skb, unsigned int len)
1208 if (likely(len >= skb->len))
1209 return 0;
1210 if (skb->ip_summed == CHECKSUM_HW)
1211 skb->ip_summed = CHECKSUM_NONE;
1212 return __pskb_trim(skb, len);
1215 static inline void *kmap_skb_frag(const skb_frag_t *frag)
1217 #ifdef CONFIG_HIGHMEM
1218 BUG_ON(in_irq());
1220 local_bh_disable();
1221 #endif
1222 return kmap_atomic(frag->page, KM_SKB_DATA_SOFTIRQ);
1225 static inline void kunmap_skb_frag(void *vaddr)
1227 kunmap_atomic(vaddr, KM_SKB_DATA_SOFTIRQ);
1228 #ifdef CONFIG_HIGHMEM
1229 local_bh_enable();
1230 #endif
1233 #define skb_queue_walk(queue, skb) \
1234 for (skb = (queue)->next; \
1235 prefetch(skb->next), (skb != (struct sk_buff *)(queue)); \
1236 skb = skb->next)
1238 #define skb_queue_reverse_walk(queue, skb) \
1239 for (skb = (queue)->prev; \
1240 prefetch(skb->prev), (skb != (struct sk_buff *)(queue)); \
1241 skb = skb->prev)
1244 extern struct sk_buff *skb_recv_datagram(struct sock *sk, unsigned flags,
1245 int noblock, int *err);
1246 extern unsigned int datagram_poll(struct file *file, struct socket *sock,
1247 struct poll_table_struct *wait);
1248 extern int skb_copy_datagram_iovec(const struct sk_buff *from,
1249 int offset, struct iovec *to,
1250 int size);
1251 extern int skb_copy_and_csum_datagram_iovec(struct sk_buff *skb,
1252 int hlen,
1253 struct iovec *iov);
1254 extern void skb_free_datagram(struct sock *sk, struct sk_buff *skb);
1255 extern void skb_kill_datagram(struct sock *sk, struct sk_buff *skb,
1256 unsigned int flags);
1257 extern unsigned int skb_checksum(const struct sk_buff *skb, int offset,
1258 int len, unsigned int csum);
1259 extern int skb_copy_bits(const struct sk_buff *skb, int offset,
1260 void *to, int len);
1261 extern int skb_store_bits(const struct sk_buff *skb, int offset,
1262 void *from, int len);
1263 extern unsigned int skb_copy_and_csum_bits(const struct sk_buff *skb,
1264 int offset, u8 *to, int len,
1265 unsigned int csum);
1266 extern void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to);
1267 extern void skb_split(struct sk_buff *skb,
1268 struct sk_buff *skb1, const u32 len);
1270 extern void skb_release_data(struct sk_buff *skb);
1272 static inline void *skb_header_pointer(const struct sk_buff *skb, int offset,
1273 int len, void *buffer)
1275 int hlen = skb_headlen(skb);
1277 if (hlen - offset >= len)
1278 return skb->data + offset;
1280 if (skb_copy_bits(skb, offset, buffer, len) < 0)
1281 return NULL;
1283 return buffer;
1286 extern void skb_init(void);
1287 extern void skb_add_mtu(int mtu);
1290 * skb_get_timestamp - get timestamp from a skb
1291 * @skb: skb to get stamp from
1292 * @stamp: pointer to struct timeval to store stamp in
1294 * Timestamps are stored in the skb as offsets to a base timestamp.
1295 * This function converts the offset back to a struct timeval and stores
1296 * it in stamp.
1298 static inline void skb_get_timestamp(const struct sk_buff *skb, struct timeval *stamp)
1300 stamp->tv_sec = skb->tstamp.off_sec;
1301 stamp->tv_usec = skb->tstamp.off_usec;
1305 * skb_set_timestamp - set timestamp of a skb
1306 * @skb: skb to set stamp of
1307 * @stamp: pointer to struct timeval to get stamp from
1309 * Timestamps are stored in the skb as offsets to a base timestamp.
1310 * This function converts a struct timeval to an offset and stores
1311 * it in the skb.
1313 static inline void skb_set_timestamp(struct sk_buff *skb, const struct timeval *stamp)
1315 skb->tstamp.off_sec = stamp->tv_sec;
1316 skb->tstamp.off_usec = stamp->tv_usec;
1319 extern void __net_timestamp(struct sk_buff *skb);
1321 extern unsigned int __skb_checksum_complete(struct sk_buff *skb);
1324 * skb_checksum_complete - Calculate checksum of an entire packet
1325 * @skb: packet to process
1327 * This function calculates the checksum over the entire packet plus
1328 * the value of skb->csum. The latter can be used to supply the
1329 * checksum of a pseudo header as used by TCP/UDP. It returns the
1330 * checksum.
1332 * For protocols that contain complete checksums such as ICMP/TCP/UDP,
1333 * this function can be used to verify that checksum on received
1334 * packets. In that case the function should return zero if the
1335 * checksum is correct. In particular, this function will return zero
1336 * if skb->ip_summed is CHECKSUM_UNNECESSARY which indicates that the
1337 * hardware has already verified the correctness of the checksum.
1339 static inline unsigned int skb_checksum_complete(struct sk_buff *skb)
1341 return skb->ip_summed != CHECKSUM_UNNECESSARY &&
1342 __skb_checksum_complete(skb);
1345 #ifdef CONFIG_NETFILTER
1346 static inline void nf_conntrack_put(struct nf_conntrack *nfct)
1348 if (nfct && atomic_dec_and_test(&nfct->use))
1349 nfct->destroy(nfct);
1351 static inline void nf_conntrack_get(struct nf_conntrack *nfct)
1353 if (nfct)
1354 atomic_inc(&nfct->use);
1356 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
1357 static inline void nf_conntrack_get_reasm(struct sk_buff *skb)
1359 if (skb)
1360 atomic_inc(&skb->users);
1362 static inline void nf_conntrack_put_reasm(struct sk_buff *skb)
1364 if (skb)
1365 kfree_skb(skb);
1367 #endif
1368 #ifdef CONFIG_BRIDGE_NETFILTER
1369 static inline void nf_bridge_put(struct nf_bridge_info *nf_bridge)
1371 if (nf_bridge && atomic_dec_and_test(&nf_bridge->use))
1372 kfree(nf_bridge);
1374 static inline void nf_bridge_get(struct nf_bridge_info *nf_bridge)
1376 if (nf_bridge)
1377 atomic_inc(&nf_bridge->use);
1379 #endif /* CONFIG_BRIDGE_NETFILTER */
1380 static inline void nf_reset(struct sk_buff *skb)
1382 nf_conntrack_put(skb->nfct);
1383 skb->nfct = NULL;
1384 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
1385 nf_conntrack_put_reasm(skb->nfct_reasm);
1386 skb->nfct_reasm = NULL;
1387 #endif
1388 #ifdef CONFIG_BRIDGE_NETFILTER
1389 nf_bridge_put(skb->nf_bridge);
1390 skb->nf_bridge = NULL;
1391 #endif
1394 #else /* CONFIG_NETFILTER */
1395 static inline void nf_reset(struct sk_buff *skb) {}
1396 #endif /* CONFIG_NETFILTER */
1398 #endif /* __KERNEL__ */
1399 #endif /* _LINUX_SKBUFF_H */