]> bbs.cooldavid.org Git - net-next-2.6.git/blame - include/linux/skbuff.h
Merge master.kernel.org:/pub/scm/linux/kernel/git/holtmann/bluetooth-2.6
[net-next-2.6.git] / include / linux / skbuff.h
CommitLineData
1da177e4
LT
1/*
2 * Definitions for the 'struct sk_buff' memory handlers.
3 *
4 * Authors:
5 * Alan Cox, <gw4pts@gw4pts.ampr.org>
6 * Florian La Roche, <rzsfl@rz.uni-sb.de>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
12 */
13
14#ifndef _LINUX_SKBUFF_H
15#define _LINUX_SKBUFF_H
16
17#include <linux/config.h>
18#include <linux/kernel.h>
19#include <linux/compiler.h>
20#include <linux/time.h>
21#include <linux/cache.h>
22
23#include <asm/atomic.h>
24#include <asm/types.h>
25#include <linux/spinlock.h>
26#include <linux/mm.h>
27#include <linux/highmem.h>
28#include <linux/poll.h>
29#include <linux/net.h>
3fc7e8a6 30#include <linux/textsearch.h>
1da177e4
LT
31#include <net/checksum.h>
32
33#define HAVE_ALLOC_SKB /* For the drivers to know */
34#define HAVE_ALIGNABLE_SKB /* Ditto 8) */
35#define SLAB_SKB /* Slabified skbuffs */
36
37#define CHECKSUM_NONE 0
38#define CHECKSUM_HW 1
39#define CHECKSUM_UNNECESSARY 2
40
41#define SKB_DATA_ALIGN(X) (((X) + (SMP_CACHE_BYTES - 1)) & \
42 ~(SMP_CACHE_BYTES - 1))
43#define SKB_MAX_ORDER(X, ORDER) (((PAGE_SIZE << (ORDER)) - (X) - \
44 sizeof(struct skb_shared_info)) & \
45 ~(SMP_CACHE_BYTES - 1))
46#define SKB_MAX_HEAD(X) (SKB_MAX_ORDER((X), 0))
47#define SKB_MAX_ALLOC (SKB_MAX_ORDER(0, 2))
48
49/* A. Checksumming of received packets by device.
50 *
51 * NONE: device failed to checksum this packet.
52 * skb->csum is undefined.
53 *
54 * UNNECESSARY: device parsed packet and wouldbe verified checksum.
55 * skb->csum is undefined.
56 * It is bad option, but, unfortunately, many of vendors do this.
57 * Apparently with secret goal to sell you new device, when you
58 * will add new protocol to your host. F.e. IPv6. 8)
59 *
60 * HW: the most generic way. Device supplied checksum of _all_
61 * the packet as seen by netif_rx in skb->csum.
62 * NOTE: Even if device supports only some protocols, but
63 * is able to produce some skb->csum, it MUST use HW,
64 * not UNNECESSARY.
65 *
66 * B. Checksumming on output.
67 *
68 * NONE: skb is checksummed by protocol or csum is not required.
69 *
70 * HW: device is required to csum packet as seen by hard_start_xmit
71 * from skb->h.raw to the end and to record the checksum
72 * at skb->h.raw+skb->csum.
73 *
74 * Device must show its capabilities in dev->features, set
75 * at device setup time.
76 * NETIF_F_HW_CSUM - it is clever device, it is able to checksum
77 * everything.
78 * NETIF_F_NO_CSUM - loopback or reliable single hop media.
79 * NETIF_F_IP_CSUM - device is dumb. It is able to csum only
80 * TCP/UDP over IPv4. Sigh. Vendors like this
81 * way by an unknown reason. Though, see comment above
82 * about CHECKSUM_UNNECESSARY. 8)
83 *
84 * Any questions? No questions, good. --ANK
85 */
86
1da177e4
LT
87struct net_device;
88
89#ifdef CONFIG_NETFILTER
90struct nf_conntrack {
91 atomic_t use;
92 void (*destroy)(struct nf_conntrack *);
93};
94
95#ifdef CONFIG_BRIDGE_NETFILTER
96struct nf_bridge_info {
97 atomic_t use;
98 struct net_device *physindev;
99 struct net_device *physoutdev;
100#if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
101 struct net_device *netoutdev;
102#endif
103 unsigned int mask;
104 unsigned long data[32 / sizeof(unsigned long)];
105};
106#endif
107
108#endif
109
110struct sk_buff_head {
111 /* These two members must be first. */
112 struct sk_buff *next;
113 struct sk_buff *prev;
114
115 __u32 qlen;
116 spinlock_t lock;
117};
118
119struct sk_buff;
120
121/* To allow 64K frame to be packed as single skb without frag_list */
122#define MAX_SKB_FRAGS (65536/PAGE_SIZE + 2)
123
124typedef struct skb_frag_struct skb_frag_t;
125
126struct skb_frag_struct {
127 struct page *page;
128 __u16 page_offset;
129 __u16 size;
130};
131
132/* This data is invariant across clones and lives at
133 * the end of the header data, ie. at skb->end.
134 */
135struct skb_shared_info {
136 atomic_t dataref;
137 unsigned int nr_frags;
138 unsigned short tso_size;
139 unsigned short tso_segs;
140 struct sk_buff *frag_list;
141 skb_frag_t frags[MAX_SKB_FRAGS];
142};
143
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.
148 *
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.
151 *
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.
154 */
155#define SKB_DATAREF_SHIFT 16
156#define SKB_DATAREF_MASK ((1 << SKB_DATAREF_SHIFT) - 1)
157
a61bbcf2
PM
158struct skb_timeval {
159 u32 off_sec;
160 u32 off_usec;
161};
162
d179cd12
DM
163
164enum {
165 SKB_FCLONE_UNAVAILABLE,
166 SKB_FCLONE_ORIG,
167 SKB_FCLONE_CLONE,
168};
169
1da177e4
LT
170/**
171 * struct sk_buff - socket buffer
172 * @next: Next buffer in list
173 * @prev: Previous buffer in list
1da177e4 174 * @sk: Socket we are owned by
325ed823 175 * @tstamp: Time we arrived
1da177e4
LT
176 * @dev: Device we arrived on/are leaving by
177 * @input_dev: Device we arrived on
1da177e4
LT
178 * @h: Transport layer header
179 * @nh: Network layer header
180 * @mac: Link layer header
67be2dd1
MW
181 * @dst: destination entry
182 * @sp: the security path, used for xfrm
1da177e4
LT
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
67be2dd1 188 * @local_df: allow local fragmentation
1da177e4
LT
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
c83c2486 192 * @fclone: skbuff clone status
1da177e4
LT
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
1da177e4
LT
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
1da177e4 204 * @nfct: Associated connection, if any
c83c2486 205 * @ipvs_property: skbuff is owned by ipvs
1da177e4 206 * @nfctinfo: Relationship of this skb to the connection
1da177e4 207 * @nf_bridge: Saved data about a bridged frame - see br_netfilter.c
1da177e4
LT
208 * @tc_index: Traffic control index
209 * @tc_verd: traffic control verdict
1da177e4
LT
210 */
211
212struct sk_buff {
213 /* These two members must be first. */
214 struct sk_buff *next;
215 struct sk_buff *prev;
216
1da177e4 217 struct sock *sk;
a61bbcf2 218 struct skb_timeval tstamp;
1da177e4
LT
219 struct net_device *dev;
220 struct net_device *input_dev;
1da177e4
LT
221
222 union {
223 struct tcphdr *th;
224 struct udphdr *uh;
225 struct icmphdr *icmph;
226 struct igmphdr *igmph;
227 struct iphdr *ipiph;
228 struct ipv6hdr *ipv6h;
229 unsigned char *raw;
230 } h;
231
232 union {
233 struct iphdr *iph;
234 struct ipv6hdr *ipv6h;
235 struct arphdr *arph;
236 unsigned char *raw;
237 } nh;
238
239 union {
240 unsigned char *raw;
241 } mac;
242
243 struct dst_entry *dst;
244 struct sec_path *sp;
245
246 /*
247 * This is the control buffer. It is free to use for every
248 * layer. Please put your private variables there. If you
249 * want to keep them across layers you have to do a skb_clone()
250 * first. This is owned by whoever has the skb queued ATM.
251 */
252 char cb[40];
253
254 unsigned int len,
255 data_len,
256 mac_len,
257 csum;
1da177e4 258 __u32 priority;
1cbb3380
TG
259 __u8 local_df:1,
260 cloned:1,
261 ip_summed:2,
6869c4d8
HW
262 nohdr:1,
263 nfctinfo:3;
d179cd12
DM
264 __u8 pkt_type:3,
265 fclone:2;
a0d3bea3 266 __be16 protocol;
1da177e4
LT
267
268 void (*destructor)(struct sk_buff *skb);
269#ifdef CONFIG_NETFILTER
bf3a46aa 270 __u32 nfmark;
1da177e4 271 struct nf_conntrack *nfct;
6869c4d8
HW
272#if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE)
273 __u8 ipvs_property:1;
274#endif
1da177e4
LT
275#ifdef CONFIG_BRIDGE_NETFILTER
276 struct nf_bridge_info *nf_bridge;
277#endif
278#endif /* CONFIG_NETFILTER */
1da177e4 279#ifdef CONFIG_NET_SCHED
b6b99eb5 280 __u16 tc_index; /* traffic control index */
1da177e4 281#ifdef CONFIG_NET_CLS_ACT
b6b99eb5 282 __u16 tc_verd; /* traffic control verdict */
1da177e4 283#endif
1da177e4
LT
284#endif
285
286
287 /* These elements must be at the end, see alloc_skb() for details. */
288 unsigned int truesize;
289 atomic_t users;
290 unsigned char *head,
291 *data,
292 *tail,
293 *end;
294};
295
296#ifdef __KERNEL__
297/*
298 * Handling routines are only of interest to the kernel
299 */
300#include <linux/slab.h>
301
302#include <asm/system.h>
303
304extern void __kfree_skb(struct sk_buff *skb);
d179cd12 305extern struct sk_buff *__alloc_skb(unsigned int size,
dd0fc66f 306 gfp_t priority, int fclone);
d179cd12 307static inline struct sk_buff *alloc_skb(unsigned int size,
dd0fc66f 308 gfp_t priority)
d179cd12
DM
309{
310 return __alloc_skb(size, priority, 0);
311}
312
313static inline struct sk_buff *alloc_skb_fclone(unsigned int size,
dd0fc66f 314 gfp_t priority)
d179cd12
DM
315{
316 return __alloc_skb(size, priority, 1);
317}
318
1da177e4 319extern struct sk_buff *alloc_skb_from_cache(kmem_cache_t *cp,
86a76caf 320 unsigned int size,
dd0fc66f 321 gfp_t priority);
1da177e4 322extern void kfree_skbmem(struct sk_buff *skb);
86a76caf 323extern struct sk_buff *skb_clone(struct sk_buff *skb,
dd0fc66f 324 gfp_t priority);
86a76caf 325extern struct sk_buff *skb_copy(const struct sk_buff *skb,
dd0fc66f 326 gfp_t priority);
86a76caf 327extern struct sk_buff *pskb_copy(struct sk_buff *skb,
dd0fc66f 328 gfp_t gfp_mask);
1da177e4 329extern int pskb_expand_head(struct sk_buff *skb,
86a76caf 330 int nhead, int ntail,
dd0fc66f 331 gfp_t gfp_mask);
1da177e4
LT
332extern struct sk_buff *skb_realloc_headroom(struct sk_buff *skb,
333 unsigned int headroom);
334extern struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
335 int newheadroom, int newtailroom,
dd0fc66f 336 gfp_t priority);
1da177e4
LT
337extern struct sk_buff * skb_pad(struct sk_buff *skb, int pad);
338#define dev_kfree_skb(a) kfree_skb(a)
339extern void skb_over_panic(struct sk_buff *skb, int len,
340 void *here);
341extern void skb_under_panic(struct sk_buff *skb, int len,
342 void *here);
343
677e90ed
TG
344struct skb_seq_state
345{
346 __u32 lower_offset;
347 __u32 upper_offset;
348 __u32 frag_idx;
349 __u32 stepped_offset;
350 struct sk_buff *root_skb;
351 struct sk_buff *cur_skb;
352 __u8 *frag_data;
353};
354
355extern void skb_prepare_seq_read(struct sk_buff *skb,
356 unsigned int from, unsigned int to,
357 struct skb_seq_state *st);
358extern unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
359 struct skb_seq_state *st);
360extern void skb_abort_seq_read(struct skb_seq_state *st);
361
3fc7e8a6
TG
362extern unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
363 unsigned int to, struct ts_config *config,
364 struct ts_state *state);
365
1da177e4
LT
366/* Internal */
367#define skb_shinfo(SKB) ((struct skb_shared_info *)((SKB)->end))
368
369/**
370 * skb_queue_empty - check if a queue is empty
371 * @list: queue head
372 *
373 * Returns true if the queue is empty, false otherwise.
374 */
375static inline int skb_queue_empty(const struct sk_buff_head *list)
376{
377 return list->next == (struct sk_buff *)list;
378}
379
380/**
381 * skb_get - reference buffer
382 * @skb: buffer to reference
383 *
384 * Makes another reference to a socket buffer and returns a pointer
385 * to the buffer.
386 */
387static inline struct sk_buff *skb_get(struct sk_buff *skb)
388{
389 atomic_inc(&skb->users);
390 return skb;
391}
392
393/*
394 * If users == 1, we are the only owner and are can avoid redundant
395 * atomic change.
396 */
397
398/**
399 * kfree_skb - free an sk_buff
400 * @skb: buffer to free
401 *
402 * Drop a reference to the buffer and free it if the usage count has
403 * hit zero.
404 */
405static inline void kfree_skb(struct sk_buff *skb)
406{
407 if (likely(atomic_read(&skb->users) == 1))
408 smp_rmb();
409 else if (likely(!atomic_dec_and_test(&skb->users)))
410 return;
411 __kfree_skb(skb);
412}
413
414/**
415 * skb_cloned - is the buffer a clone
416 * @skb: buffer to check
417 *
418 * Returns true if the buffer was generated with skb_clone() and is
419 * one of multiple shared copies of the buffer. Cloned buffers are
420 * shared data so must not be written to under normal circumstances.
421 */
422static inline int skb_cloned(const struct sk_buff *skb)
423{
424 return skb->cloned &&
425 (atomic_read(&skb_shinfo(skb)->dataref) & SKB_DATAREF_MASK) != 1;
426}
427
428/**
429 * skb_header_cloned - is the header a clone
430 * @skb: buffer to check
431 *
432 * Returns true if modifying the header part of the buffer requires
433 * the data to be copied.
434 */
435static inline int skb_header_cloned(const struct sk_buff *skb)
436{
437 int dataref;
438
439 if (!skb->cloned)
440 return 0;
441
442 dataref = atomic_read(&skb_shinfo(skb)->dataref);
443 dataref = (dataref & SKB_DATAREF_MASK) - (dataref >> SKB_DATAREF_SHIFT);
444 return dataref != 1;
445}
446
447/**
448 * skb_header_release - release reference to header
449 * @skb: buffer to operate on
450 *
451 * Drop a reference to the header part of the buffer. This is done
452 * by acquiring a payload reference. You must not read from the header
453 * part of skb->data after this.
454 */
455static inline void skb_header_release(struct sk_buff *skb)
456{
457 BUG_ON(skb->nohdr);
458 skb->nohdr = 1;
459 atomic_add(1 << SKB_DATAREF_SHIFT, &skb_shinfo(skb)->dataref);
460}
461
462/**
463 * skb_shared - is the buffer shared
464 * @skb: buffer to check
465 *
466 * Returns true if more than one person has a reference to this
467 * buffer.
468 */
469static inline int skb_shared(const struct sk_buff *skb)
470{
471 return atomic_read(&skb->users) != 1;
472}
473
474/**
475 * skb_share_check - check if buffer is shared and if so clone it
476 * @skb: buffer to check
477 * @pri: priority for memory allocation
478 *
479 * If the buffer is shared the buffer is cloned and the old copy
480 * drops a reference. A new clone with a single reference is returned.
481 * If the buffer is not shared the original buffer is returned. When
482 * being called from interrupt status or with spinlocks held pri must
483 * be GFP_ATOMIC.
484 *
485 * NULL is returned on a memory allocation failure.
486 */
86a76caf 487static inline struct sk_buff *skb_share_check(struct sk_buff *skb,
dd0fc66f 488 gfp_t pri)
1da177e4
LT
489{
490 might_sleep_if(pri & __GFP_WAIT);
491 if (skb_shared(skb)) {
492 struct sk_buff *nskb = skb_clone(skb, pri);
493 kfree_skb(skb);
494 skb = nskb;
495 }
496 return skb;
497}
498
499/*
500 * Copy shared buffers into a new sk_buff. We effectively do COW on
501 * packets to handle cases where we have a local reader and forward
502 * and a couple of other messy ones. The normal one is tcpdumping
503 * a packet thats being forwarded.
504 */
505
506/**
507 * skb_unshare - make a copy of a shared buffer
508 * @skb: buffer to check
509 * @pri: priority for memory allocation
510 *
511 * If the socket buffer is a clone then this function creates a new
512 * copy of the data, drops a reference count on the old copy and returns
513 * the new copy with the reference count at 1. If the buffer is not a clone
514 * the original buffer is returned. When called with a spinlock held or
515 * from interrupt state @pri must be %GFP_ATOMIC
516 *
517 * %NULL is returned on a memory allocation failure.
518 */
e2bf521d 519static inline struct sk_buff *skb_unshare(struct sk_buff *skb,
dd0fc66f 520 gfp_t pri)
1da177e4
LT
521{
522 might_sleep_if(pri & __GFP_WAIT);
523 if (skb_cloned(skb)) {
524 struct sk_buff *nskb = skb_copy(skb, pri);
525 kfree_skb(skb); /* Free our shared copy */
526 skb = nskb;
527 }
528 return skb;
529}
530
531/**
532 * skb_peek
533 * @list_: list to peek at
534 *
535 * Peek an &sk_buff. Unlike most other operations you _MUST_
536 * be careful with this one. A peek leaves the buffer on the
537 * list and someone else may run off with it. You must hold
538 * the appropriate locks or have a private queue to do this.
539 *
540 * Returns %NULL for an empty list or a pointer to the head element.
541 * The reference count is not incremented and the reference is therefore
542 * volatile. Use with caution.
543 */
544static inline struct sk_buff *skb_peek(struct sk_buff_head *list_)
545{
546 struct sk_buff *list = ((struct sk_buff *)list_)->next;
547 if (list == (struct sk_buff *)list_)
548 list = NULL;
549 return list;
550}
551
552/**
553 * skb_peek_tail
554 * @list_: list to peek at
555 *
556 * Peek an &sk_buff. Unlike most other operations you _MUST_
557 * be careful with this one. A peek leaves the buffer on the
558 * list and someone else may run off with it. You must hold
559 * the appropriate locks or have a private queue to do this.
560 *
561 * Returns %NULL for an empty list or a pointer to the tail element.
562 * The reference count is not incremented and the reference is therefore
563 * volatile. Use with caution.
564 */
565static inline struct sk_buff *skb_peek_tail(struct sk_buff_head *list_)
566{
567 struct sk_buff *list = ((struct sk_buff *)list_)->prev;
568 if (list == (struct sk_buff *)list_)
569 list = NULL;
570 return list;
571}
572
573/**
574 * skb_queue_len - get queue length
575 * @list_: list to measure
576 *
577 * Return the length of an &sk_buff queue.
578 */
579static inline __u32 skb_queue_len(const struct sk_buff_head *list_)
580{
581 return list_->qlen;
582}
583
584static inline void skb_queue_head_init(struct sk_buff_head *list)
585{
586 spin_lock_init(&list->lock);
587 list->prev = list->next = (struct sk_buff *)list;
588 list->qlen = 0;
589}
590
591/*
592 * Insert an sk_buff at the start of a list.
593 *
594 * The "__skb_xxxx()" functions are the non-atomic ones that
595 * can only be called with interrupts disabled.
596 */
597
598/**
599 * __skb_queue_head - queue a buffer at the list head
600 * @list: list to use
601 * @newsk: buffer to queue
602 *
603 * Queue a buffer at the start of a list. This function takes no locks
604 * and you must therefore hold required locks before calling it.
605 *
606 * A buffer cannot be placed on two lists at the same time.
607 */
608extern void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk);
609static inline void __skb_queue_head(struct sk_buff_head *list,
610 struct sk_buff *newsk)
611{
612 struct sk_buff *prev, *next;
613
1da177e4
LT
614 list->qlen++;
615 prev = (struct sk_buff *)list;
616 next = prev->next;
617 newsk->next = next;
618 newsk->prev = prev;
619 next->prev = prev->next = newsk;
620}
621
622/**
623 * __skb_queue_tail - queue a buffer at the list tail
624 * @list: list to use
625 * @newsk: buffer to queue
626 *
627 * Queue a buffer at the end of a list. This function takes no locks
628 * and you must therefore hold required locks before calling it.
629 *
630 * A buffer cannot be placed on two lists at the same time.
631 */
632extern void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk);
633static inline void __skb_queue_tail(struct sk_buff_head *list,
634 struct sk_buff *newsk)
635{
636 struct sk_buff *prev, *next;
637
1da177e4
LT
638 list->qlen++;
639 next = (struct sk_buff *)list;
640 prev = next->prev;
641 newsk->next = next;
642 newsk->prev = prev;
643 next->prev = prev->next = newsk;
644}
645
646
647/**
648 * __skb_dequeue - remove from the head of the queue
649 * @list: list to dequeue from
650 *
651 * Remove the head of the list. This function does not take any locks
652 * so must be used with appropriate locks held only. The head item is
653 * returned or %NULL if the list is empty.
654 */
655extern struct sk_buff *skb_dequeue(struct sk_buff_head *list);
656static inline struct sk_buff *__skb_dequeue(struct sk_buff_head *list)
657{
658 struct sk_buff *next, *prev, *result;
659
660 prev = (struct sk_buff *) list;
661 next = prev->next;
662 result = NULL;
663 if (next != prev) {
664 result = next;
665 next = next->next;
666 list->qlen--;
667 next->prev = prev;
668 prev->next = next;
669 result->next = result->prev = NULL;
1da177e4
LT
670 }
671 return result;
672}
673
674
675/*
676 * Insert a packet on a list.
677 */
8728b834 678extern void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list);
1da177e4
LT
679static inline void __skb_insert(struct sk_buff *newsk,
680 struct sk_buff *prev, struct sk_buff *next,
681 struct sk_buff_head *list)
682{
683 newsk->next = next;
684 newsk->prev = prev;
685 next->prev = prev->next = newsk;
1da177e4
LT
686 list->qlen++;
687}
688
689/*
690 * Place a packet after a given packet in a list.
691 */
8728b834
DM
692extern void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list);
693static inline void __skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
1da177e4 694{
8728b834 695 __skb_insert(newsk, old, old->next, list);
1da177e4
LT
696}
697
698/*
699 * remove sk_buff from list. _Must_ be called atomically, and with
700 * the list known..
701 */
8728b834 702extern void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list);
1da177e4
LT
703static inline void __skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
704{
705 struct sk_buff *next, *prev;
706
707 list->qlen--;
708 next = skb->next;
709 prev = skb->prev;
710 skb->next = skb->prev = NULL;
1da177e4
LT
711 next->prev = prev;
712 prev->next = next;
713}
714
715
716/* XXX: more streamlined implementation */
717
718/**
719 * __skb_dequeue_tail - remove from the tail of the queue
720 * @list: list to dequeue from
721 *
722 * Remove the tail of the list. This function does not take any locks
723 * so must be used with appropriate locks held only. The tail item is
724 * returned or %NULL if the list is empty.
725 */
726extern struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list);
727static inline struct sk_buff *__skb_dequeue_tail(struct sk_buff_head *list)
728{
729 struct sk_buff *skb = skb_peek_tail(list);
730 if (skb)
731 __skb_unlink(skb, list);
732 return skb;
733}
734
735
736static inline int skb_is_nonlinear(const struct sk_buff *skb)
737{
738 return skb->data_len;
739}
740
741static inline unsigned int skb_headlen(const struct sk_buff *skb)
742{
743 return skb->len - skb->data_len;
744}
745
746static inline int skb_pagelen(const struct sk_buff *skb)
747{
748 int i, len = 0;
749
750 for (i = (int)skb_shinfo(skb)->nr_frags - 1; i >= 0; i--)
751 len += skb_shinfo(skb)->frags[i].size;
752 return len + skb_headlen(skb);
753}
754
755static inline void skb_fill_page_desc(struct sk_buff *skb, int i,
756 struct page *page, int off, int size)
757{
758 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
759
760 frag->page = page;
761 frag->page_offset = off;
762 frag->size = size;
763 skb_shinfo(skb)->nr_frags = i + 1;
764}
765
766#define SKB_PAGE_ASSERT(skb) BUG_ON(skb_shinfo(skb)->nr_frags)
767#define SKB_FRAG_ASSERT(skb) BUG_ON(skb_shinfo(skb)->frag_list)
768#define SKB_LINEAR_ASSERT(skb) BUG_ON(skb_is_nonlinear(skb))
769
770/*
771 * Add data to an sk_buff
772 */
773static inline unsigned char *__skb_put(struct sk_buff *skb, unsigned int len)
774{
775 unsigned char *tmp = skb->tail;
776 SKB_LINEAR_ASSERT(skb);
777 skb->tail += len;
778 skb->len += len;
779 return tmp;
780}
781
782/**
783 * skb_put - add data to a buffer
784 * @skb: buffer to use
785 * @len: amount of data to add
786 *
787 * This function extends the used data area of the buffer. If this would
788 * exceed the total buffer size the kernel will panic. A pointer to the
789 * first byte of the extra data is returned.
790 */
791static inline unsigned char *skb_put(struct sk_buff *skb, unsigned int len)
792{
793 unsigned char *tmp = skb->tail;
794 SKB_LINEAR_ASSERT(skb);
795 skb->tail += len;
796 skb->len += len;
797 if (unlikely(skb->tail>skb->end))
798 skb_over_panic(skb, len, current_text_addr());
799 return tmp;
800}
801
802static inline unsigned char *__skb_push(struct sk_buff *skb, unsigned int len)
803{
804 skb->data -= len;
805 skb->len += len;
806 return skb->data;
807}
808
809/**
810 * skb_push - add data to the start of a buffer
811 * @skb: buffer to use
812 * @len: amount of data to add
813 *
814 * This function extends the used data area of the buffer at the buffer
815 * start. If this would exceed the total buffer headroom the kernel will
816 * panic. A pointer to the first byte of the extra data is returned.
817 */
818static inline unsigned char *skb_push(struct sk_buff *skb, unsigned int len)
819{
820 skb->data -= len;
821 skb->len += len;
822 if (unlikely(skb->data<skb->head))
823 skb_under_panic(skb, len, current_text_addr());
824 return skb->data;
825}
826
827static inline unsigned char *__skb_pull(struct sk_buff *skb, unsigned int len)
828{
829 skb->len -= len;
830 BUG_ON(skb->len < skb->data_len);
831 return skb->data += len;
832}
833
834/**
835 * skb_pull - remove data from the start of a buffer
836 * @skb: buffer to use
837 * @len: amount of data to remove
838 *
839 * This function removes data from the start of a buffer, returning
840 * the memory to the headroom. A pointer to the next data in the buffer
841 * is returned. Once the data has been pulled future pushes will overwrite
842 * the old data.
843 */
844static inline unsigned char *skb_pull(struct sk_buff *skb, unsigned int len)
845{
846 return unlikely(len > skb->len) ? NULL : __skb_pull(skb, len);
847}
848
849extern unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta);
850
851static inline unsigned char *__pskb_pull(struct sk_buff *skb, unsigned int len)
852{
853 if (len > skb_headlen(skb) &&
854 !__pskb_pull_tail(skb, len-skb_headlen(skb)))
855 return NULL;
856 skb->len -= len;
857 return skb->data += len;
858}
859
860static inline unsigned char *pskb_pull(struct sk_buff *skb, unsigned int len)
861{
862 return unlikely(len > skb->len) ? NULL : __pskb_pull(skb, len);
863}
864
865static inline int pskb_may_pull(struct sk_buff *skb, unsigned int len)
866{
867 if (likely(len <= skb_headlen(skb)))
868 return 1;
869 if (unlikely(len > skb->len))
870 return 0;
871 return __pskb_pull_tail(skb, len-skb_headlen(skb)) != NULL;
872}
873
874/**
875 * skb_headroom - bytes at buffer head
876 * @skb: buffer to check
877 *
878 * Return the number of bytes of free space at the head of an &sk_buff.
879 */
880static inline int skb_headroom(const struct sk_buff *skb)
881{
882 return skb->data - skb->head;
883}
884
885/**
886 * skb_tailroom - bytes at buffer end
887 * @skb: buffer to check
888 *
889 * Return the number of bytes of free space at the tail of an sk_buff
890 */
891static inline int skb_tailroom(const struct sk_buff *skb)
892{
893 return skb_is_nonlinear(skb) ? 0 : skb->end - skb->tail;
894}
895
896/**
897 * skb_reserve - adjust headroom
898 * @skb: buffer to alter
899 * @len: bytes to move
900 *
901 * Increase the headroom of an empty &sk_buff by reducing the tail
902 * room. This is only allowed for an empty buffer.
903 */
904static inline void skb_reserve(struct sk_buff *skb, unsigned int len)
905{
906 skb->data += len;
907 skb->tail += len;
908}
909
910/*
911 * CPUs often take a performance hit when accessing unaligned memory
912 * locations. The actual performance hit varies, it can be small if the
913 * hardware handles it or large if we have to take an exception and fix it
914 * in software.
915 *
916 * Since an ethernet header is 14 bytes network drivers often end up with
917 * the IP header at an unaligned offset. The IP header can be aligned by
918 * shifting the start of the packet by 2 bytes. Drivers should do this
919 * with:
920 *
921 * skb_reserve(NET_IP_ALIGN);
922 *
923 * The downside to this alignment of the IP header is that the DMA is now
924 * unaligned. On some architectures the cost of an unaligned DMA is high
925 * and this cost outweighs the gains made by aligning the IP header.
926 *
927 * Since this trade off varies between architectures, we allow NET_IP_ALIGN
928 * to be overridden.
929 */
930#ifndef NET_IP_ALIGN
931#define NET_IP_ALIGN 2
932#endif
933
934extern int ___pskb_trim(struct sk_buff *skb, unsigned int len, int realloc);
935
936static inline void __skb_trim(struct sk_buff *skb, unsigned int len)
937{
938 if (!skb->data_len) {
939 skb->len = len;
940 skb->tail = skb->data + len;
941 } else
942 ___pskb_trim(skb, len, 0);
943}
944
945/**
946 * skb_trim - remove end from a buffer
947 * @skb: buffer to alter
948 * @len: new length
949 *
950 * Cut the length of a buffer down by removing data from the tail. If
951 * the buffer is already under the length specified it is not modified.
952 */
953static inline void skb_trim(struct sk_buff *skb, unsigned int len)
954{
955 if (skb->len > len)
956 __skb_trim(skb, len);
957}
958
959
960static inline int __pskb_trim(struct sk_buff *skb, unsigned int len)
961{
962 if (!skb->data_len) {
963 skb->len = len;
964 skb->tail = skb->data+len;
965 return 0;
966 }
967 return ___pskb_trim(skb, len, 1);
968}
969
970static inline int pskb_trim(struct sk_buff *skb, unsigned int len)
971{
972 return (len < skb->len) ? __pskb_trim(skb, len) : 0;
973}
974
975/**
976 * skb_orphan - orphan a buffer
977 * @skb: buffer to orphan
978 *
979 * If a buffer currently has an owner then we call the owner's
980 * destructor function and make the @skb unowned. The buffer continues
981 * to exist but is no longer charged to its former owner.
982 */
983static inline void skb_orphan(struct sk_buff *skb)
984{
985 if (skb->destructor)
986 skb->destructor(skb);
987 skb->destructor = NULL;
988 skb->sk = NULL;
989}
990
991/**
992 * __skb_queue_purge - empty a list
993 * @list: list to empty
994 *
995 * Delete all buffers on an &sk_buff list. Each buffer is removed from
996 * the list and one reference dropped. This function does not take the
997 * list lock and the caller must hold the relevant locks to use it.
998 */
999extern void skb_queue_purge(struct sk_buff_head *list);
1000static inline void __skb_queue_purge(struct sk_buff_head *list)
1001{
1002 struct sk_buff *skb;
1003 while ((skb = __skb_dequeue(list)) != NULL)
1004 kfree_skb(skb);
1005}
1006
4dc3b16b 1007#ifndef CONFIG_HAVE_ARCH_DEV_ALLOC_SKB
1da177e4
LT
1008/**
1009 * __dev_alloc_skb - allocate an skbuff for sending
1010 * @length: length to allocate
1011 * @gfp_mask: get_free_pages mask, passed to alloc_skb
1012 *
1013 * Allocate a new &sk_buff and assign it a usage count of one. The
1014 * buffer has unspecified headroom built in. Users should allocate
1015 * the headroom they think they need without accounting for the
1016 * built in space. The built in space is used for optimisations.
1017 *
1018 * %NULL is returned in there is no free memory.
1019 */
1da177e4 1020static inline struct sk_buff *__dev_alloc_skb(unsigned int length,
dd0fc66f 1021 gfp_t gfp_mask)
1da177e4
LT
1022{
1023 struct sk_buff *skb = alloc_skb(length + 16, gfp_mask);
1024 if (likely(skb))
1025 skb_reserve(skb, 16);
1026 return skb;
1027}
1028#else
1029extern struct sk_buff *__dev_alloc_skb(unsigned int length, int gfp_mask);
1030#endif
1031
1032/**
1033 * dev_alloc_skb - allocate an skbuff for sending
1034 * @length: length to allocate
1035 *
1036 * Allocate a new &sk_buff and assign it a usage count of one. The
1037 * buffer has unspecified headroom built in. Users should allocate
1038 * the headroom they think they need without accounting for the
1039 * built in space. The built in space is used for optimisations.
1040 *
1041 * %NULL is returned in there is no free memory. Although this function
1042 * allocates memory it can be called from an interrupt.
1043 */
1044static inline struct sk_buff *dev_alloc_skb(unsigned int length)
1045{
1046 return __dev_alloc_skb(length, GFP_ATOMIC);
1047}
1048
1049/**
1050 * skb_cow - copy header of skb when it is required
1051 * @skb: buffer to cow
1052 * @headroom: needed headroom
1053 *
1054 * If the skb passed lacks sufficient headroom or its data part
1055 * is shared, data is reallocated. If reallocation fails, an error
1056 * is returned and original skb is not changed.
1057 *
1058 * The result is skb with writable area skb->head...skb->tail
1059 * and at least @headroom of space at head.
1060 */
1061static inline int skb_cow(struct sk_buff *skb, unsigned int headroom)
1062{
1063 int delta = (headroom > 16 ? headroom : 16) - skb_headroom(skb);
1064
1065 if (delta < 0)
1066 delta = 0;
1067
1068 if (delta || skb_cloned(skb))
1069 return pskb_expand_head(skb, (delta + 15) & ~15, 0, GFP_ATOMIC);
1070 return 0;
1071}
1072
1073/**
1074 * skb_padto - pad an skbuff up to a minimal size
1075 * @skb: buffer to pad
1076 * @len: minimal length
1077 *
1078 * Pads up a buffer to ensure the trailing bytes exist and are
1079 * blanked. If the buffer already contains sufficient data it
1080 * is untouched. Returns the buffer, which may be a replacement
1081 * for the original, or NULL for out of memory - in which case
1082 * the original buffer is still freed.
1083 */
1084
1085static inline struct sk_buff *skb_padto(struct sk_buff *skb, unsigned int len)
1086{
1087 unsigned int size = skb->len;
1088 if (likely(size >= len))
1089 return skb;
1090 return skb_pad(skb, len-size);
1091}
1092
1093static inline int skb_add_data(struct sk_buff *skb,
1094 char __user *from, int copy)
1095{
1096 const int off = skb->len;
1097
1098 if (skb->ip_summed == CHECKSUM_NONE) {
1099 int err = 0;
1100 unsigned int csum = csum_and_copy_from_user(from,
1101 skb_put(skb, copy),
1102 copy, 0, &err);
1103 if (!err) {
1104 skb->csum = csum_block_add(skb->csum, csum, off);
1105 return 0;
1106 }
1107 } else if (!copy_from_user(skb_put(skb, copy), from, copy))
1108 return 0;
1109
1110 __skb_trim(skb, off);
1111 return -EFAULT;
1112}
1113
1114static inline int skb_can_coalesce(struct sk_buff *skb, int i,
1115 struct page *page, int off)
1116{
1117 if (i) {
1118 struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[i - 1];
1119
1120 return page == frag->page &&
1121 off == frag->page_offset + frag->size;
1122 }
1123 return 0;
1124}
1125
1126/**
1127 * skb_linearize - convert paged skb to linear one
1128 * @skb: buffer to linarize
1129 * @gfp: allocation mode
1130 *
1131 * If there is no free memory -ENOMEM is returned, otherwise zero
1132 * is returned and the old skb data released.
1133 */
dd0fc66f
AV
1134extern int __skb_linearize(struct sk_buff *skb, gfp_t gfp);
1135static inline int skb_linearize(struct sk_buff *skb, gfp_t gfp)
1da177e4
LT
1136{
1137 return __skb_linearize(skb, gfp);
1138}
1139
1140/**
1141 * skb_postpull_rcsum - update checksum for received skb after pull
1142 * @skb: buffer to update
1143 * @start: start of data before pull
1144 * @len: length of data pulled
1145 *
1146 * After doing a pull on a received packet, you need to call this to
1147 * update the CHECKSUM_HW checksum, or set ip_summed to CHECKSUM_NONE
1148 * so that it can be recomputed from scratch.
1149 */
1150
1151static inline void skb_postpull_rcsum(struct sk_buff *skb,
1152 const void *start, int len)
1153{
1154 if (skb->ip_summed == CHECKSUM_HW)
1155 skb->csum = csum_sub(skb->csum, csum_partial(start, len, 0));
1156}
1157
1158/**
1159 * pskb_trim_rcsum - trim received skb and update checksum
1160 * @skb: buffer to trim
1161 * @len: new length
1162 *
1163 * This is exactly the same as pskb_trim except that it ensures the
1164 * checksum of received packets are still valid after the operation.
1165 */
1166
1167static inline int pskb_trim_rcsum(struct sk_buff *skb, unsigned int len)
1168{
0e4e4220 1169 if (likely(len >= skb->len))
1da177e4
LT
1170 return 0;
1171 if (skb->ip_summed == CHECKSUM_HW)
1172 skb->ip_summed = CHECKSUM_NONE;
1173 return __pskb_trim(skb, len);
1174}
1175
1176static inline void *kmap_skb_frag(const skb_frag_t *frag)
1177{
1178#ifdef CONFIG_HIGHMEM
1179 BUG_ON(in_irq());
1180
1181 local_bh_disable();
1182#endif
1183 return kmap_atomic(frag->page, KM_SKB_DATA_SOFTIRQ);
1184}
1185
1186static inline void kunmap_skb_frag(void *vaddr)
1187{
1188 kunmap_atomic(vaddr, KM_SKB_DATA_SOFTIRQ);
1189#ifdef CONFIG_HIGHMEM
1190 local_bh_enable();
1191#endif
1192}
1193
1194#define skb_queue_walk(queue, skb) \
1195 for (skb = (queue)->next; \
1196 prefetch(skb->next), (skb != (struct sk_buff *)(queue)); \
1197 skb = skb->next)
1198
1199
1200extern struct sk_buff *skb_recv_datagram(struct sock *sk, unsigned flags,
1201 int noblock, int *err);
1202extern unsigned int datagram_poll(struct file *file, struct socket *sock,
1203 struct poll_table_struct *wait);
1204extern int skb_copy_datagram_iovec(const struct sk_buff *from,
1205 int offset, struct iovec *to,
1206 int size);
1207extern int skb_copy_and_csum_datagram_iovec(const
1208 struct sk_buff *skb,
1209 int hlen,
1210 struct iovec *iov);
1211extern void skb_free_datagram(struct sock *sk, struct sk_buff *skb);
1212extern unsigned int skb_checksum(const struct sk_buff *skb, int offset,
1213 int len, unsigned int csum);
1214extern int skb_copy_bits(const struct sk_buff *skb, int offset,
1215 void *to, int len);
357b40a1
HX
1216extern int skb_store_bits(const struct sk_buff *skb, int offset,
1217 void *from, int len);
1da177e4
LT
1218extern unsigned int skb_copy_and_csum_bits(const struct sk_buff *skb,
1219 int offset, u8 *to, int len,
1220 unsigned int csum);
1221extern void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to);
1222extern void skb_split(struct sk_buff *skb,
1223 struct sk_buff *skb1, const u32 len);
1224
20380731
ACM
1225extern void skb_release_data(struct sk_buff *skb);
1226
1da177e4
LT
1227static inline void *skb_header_pointer(const struct sk_buff *skb, int offset,
1228 int len, void *buffer)
1229{
1230 int hlen = skb_headlen(skb);
1231
55820ee2 1232 if (hlen - offset >= len)
1da177e4
LT
1233 return skb->data + offset;
1234
1235 if (skb_copy_bits(skb, offset, buffer, len) < 0)
1236 return NULL;
1237
1238 return buffer;
1239}
1240
1241extern void skb_init(void);
1242extern void skb_add_mtu(int mtu);
1243
a61bbcf2
PM
1244/**
1245 * skb_get_timestamp - get timestamp from a skb
1246 * @skb: skb to get stamp from
1247 * @stamp: pointer to struct timeval to store stamp in
1248 *
1249 * Timestamps are stored in the skb as offsets to a base timestamp.
1250 * This function converts the offset back to a struct timeval and stores
1251 * it in stamp.
1252 */
f2c38398 1253static inline void skb_get_timestamp(const struct sk_buff *skb, struct timeval *stamp)
a61bbcf2
PM
1254{
1255 stamp->tv_sec = skb->tstamp.off_sec;
1256 stamp->tv_usec = skb->tstamp.off_usec;
a61bbcf2
PM
1257}
1258
1259/**
1260 * skb_set_timestamp - set timestamp of a skb
1261 * @skb: skb to set stamp of
1262 * @stamp: pointer to struct timeval to get stamp from
1263 *
1264 * Timestamps are stored in the skb as offsets to a base timestamp.
1265 * This function converts a struct timeval to an offset and stores
1266 * it in the skb.
1267 */
f2c38398 1268static inline void skb_set_timestamp(struct sk_buff *skb, const struct timeval *stamp)
a61bbcf2 1269{
325ed823
HX
1270 skb->tstamp.off_sec = stamp->tv_sec;
1271 skb->tstamp.off_usec = stamp->tv_usec;
a61bbcf2
PM
1272}
1273
1274extern void __net_timestamp(struct sk_buff *skb);
1275
1da177e4
LT
1276#ifdef CONFIG_NETFILTER
1277static inline void nf_conntrack_put(struct nf_conntrack *nfct)
1278{
1279 if (nfct && atomic_dec_and_test(&nfct->use))
1280 nfct->destroy(nfct);
1281}
1282static inline void nf_conntrack_get(struct nf_conntrack *nfct)
1283{
1284 if (nfct)
1285 atomic_inc(&nfct->use);
1286}
1287static inline void nf_reset(struct sk_buff *skb)
1288{
1289 nf_conntrack_put(skb->nfct);
1290 skb->nfct = NULL;
1da177e4
LT
1291}
1292
1293#ifdef CONFIG_BRIDGE_NETFILTER
1294static inline void nf_bridge_put(struct nf_bridge_info *nf_bridge)
1295{
1296 if (nf_bridge && atomic_dec_and_test(&nf_bridge->use))
1297 kfree(nf_bridge);
1298}
1299static inline void nf_bridge_get(struct nf_bridge_info *nf_bridge)
1300{
1301 if (nf_bridge)
1302 atomic_inc(&nf_bridge->use);
1303}
1304#endif /* CONFIG_BRIDGE_NETFILTER */
1305#else /* CONFIG_NETFILTER */
1306static inline void nf_reset(struct sk_buff *skb) {}
1307#endif /* CONFIG_NETFILTER */
1308
1309#endif /* __KERNEL__ */
1310#endif /* _LINUX_SKBUFF_H */