ia64/xen-unstable

view linux-2.6-xen-sparse/include/linux/skbuff.h @ 10714:a4041ac6f152

[NET] net-gso.patch: Fix up GSO packets with broken checksums

Here is the original changelog:

[NET] gso: Fix up GSO packets with broken checksums

Certain subsystems in the stack (e.g., netfilter) can break the
partial
checksum on GSO packets. Until they're fixed, this patch allows
this to
work by recomputing the partial checksums through the GSO
mechanism.

Once they've all been converted to update the partial checksum
instead of
clearing it, this workaround can be removed.

Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
author kfraser@localhost.localdomain
date Mon Jul 10 15:36:04 2006 +0100 (2006-07-10)
parents 6e7027a2abca
children aaaa249e6f3b
line source
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 */
14 #ifndef _LINUX_SKBUFF_H
15 #define _LINUX_SKBUFF_H
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>
23 #include <asm/atomic.h>
24 #include <asm/types.h>
25 #include <linux/spinlock.h>
26 #include <linux/mm.h>
27 #include <linux/highmem.h>
28 #include <linux/poll.h>
29 #include <linux/net.h>
30 #include <linux/textsearch.h>
31 #include <net/checksum.h>
33 #define HAVE_ALLOC_SKB /* For the drivers to know */
34 #define HAVE_ALIGNABLE_SKB /* Ditto 8) */
36 #define CHECKSUM_NONE 0
37 #define CHECKSUM_HW 1
38 #define CHECKSUM_UNNECESSARY 2
40 #define SKB_DATA_ALIGN(X) (((X) + (SMP_CACHE_BYTES - 1)) & \
41 ~(SMP_CACHE_BYTES - 1))
42 #define SKB_MAX_ORDER(X, ORDER) (((PAGE_SIZE << (ORDER)) - (X) - \
43 sizeof(struct skb_shared_info)) & \
44 ~(SMP_CACHE_BYTES - 1))
45 #define SKB_MAX_HEAD(X) (SKB_MAX_ORDER((X), 0))
46 #define SKB_MAX_ALLOC (SKB_MAX_ORDER(0, 2))
48 /* A. Checksumming of received packets by device.
49 *
50 * NONE: device failed to checksum this packet.
51 * skb->csum is undefined.
52 *
53 * UNNECESSARY: device parsed packet and wouldbe verified checksum.
54 * skb->csum is undefined.
55 * It is bad option, but, unfortunately, many of vendors do this.
56 * Apparently with secret goal to sell you new device, when you
57 * will add new protocol to your host. F.e. IPv6. 8)
58 *
59 * HW: the most generic way. Device supplied checksum of _all_
60 * the packet as seen by netif_rx in skb->csum.
61 * NOTE: Even if device supports only some protocols, but
62 * is able to produce some skb->csum, it MUST use HW,
63 * not UNNECESSARY.
64 *
65 * B. Checksumming on output.
66 *
67 * NONE: skb is checksummed by protocol or csum is not required.
68 *
69 * HW: device is required to csum packet as seen by hard_start_xmit
70 * from skb->h.raw to the end and to record the checksum
71 * at skb->h.raw+skb->csum.
72 *
73 * Device must show its capabilities in dev->features, set
74 * at device setup time.
75 * NETIF_F_HW_CSUM - it is clever device, it is able to checksum
76 * everything.
77 * NETIF_F_NO_CSUM - loopback or reliable single hop media.
78 * NETIF_F_IP_CSUM - device is dumb. It is able to csum only
79 * TCP/UDP over IPv4. Sigh. Vendors like this
80 * way by an unknown reason. Though, see comment above
81 * about CHECKSUM_UNNECESSARY. 8)
82 *
83 * Any questions? No questions, good. --ANK
84 */
86 struct net_device;
88 #ifdef CONFIG_NETFILTER
89 struct nf_conntrack {
90 atomic_t use;
91 void (*destroy)(struct nf_conntrack *);
92 };
94 #ifdef CONFIG_BRIDGE_NETFILTER
95 struct nf_bridge_info {
96 atomic_t use;
97 struct net_device *physindev;
98 struct net_device *physoutdev;
99 #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
100 struct net_device *netoutdev;
101 #endif
102 unsigned int mask;
103 unsigned long data[32 / sizeof(unsigned long)];
104 };
105 #endif
107 #endif
109 struct sk_buff_head {
110 /* These two members must be first. */
111 struct sk_buff *next;
112 struct sk_buff *prev;
114 __u32 qlen;
115 spinlock_t lock;
116 };
118 struct sk_buff;
120 /* To allow 64K frame to be packed as single skb without frag_list */
121 #define MAX_SKB_FRAGS (65536/PAGE_SIZE + 2)
123 typedef struct skb_frag_struct skb_frag_t;
125 struct skb_frag_struct {
126 struct page *page;
127 __u16 page_offset;
128 __u16 size;
129 };
131 /* This data is invariant across clones and lives at
132 * the end of the header data, ie. at skb->end.
133 */
134 struct skb_shared_info {
135 atomic_t dataref;
136 unsigned short nr_frags;
137 unsigned short gso_size;
138 /* Warning: this field is not always filled in (UFO)! */
139 unsigned short gso_segs;
140 unsigned short gso_type;
141 unsigned int ip6_frag_id;
142 struct sk_buff *frag_list;
143 skb_frag_t frags[MAX_SKB_FRAGS];
144 };
146 /* We divide dataref into two halves. The higher 16 bits hold references
147 * to the payload part of skb->data. The lower 16 bits hold references to
148 * the entire skb->data. It is up to the users of the skb to agree on
149 * where the payload starts.
150 *
151 * All users must obey the rule that the skb->data reference count must be
152 * greater than or equal to the payload reference count.
153 *
154 * Holding a reference to the payload part means that the user does not
155 * care about modifications to the header part of skb->data.
156 */
157 #define SKB_DATAREF_SHIFT 16
158 #define SKB_DATAREF_MASK ((1 << SKB_DATAREF_SHIFT) - 1)
160 struct skb_timeval {
161 u32 off_sec;
162 u32 off_usec;
163 };
166 enum {
167 SKB_FCLONE_UNAVAILABLE,
168 SKB_FCLONE_ORIG,
169 SKB_FCLONE_CLONE,
170 };
172 enum {
173 SKB_GSO_TCPV4 = 1 << 0,
174 SKB_GSO_UDPV4 = 1 << 1,
176 /* This indicates the skb is from an untrusted source. */
177 SKB_GSO_DODGY = 1 << 2,
178 };
180 /**
181 * struct sk_buff - socket buffer
182 * @next: Next buffer in list
183 * @prev: Previous buffer in list
184 * @sk: Socket we are owned by
185 * @tstamp: Time we arrived
186 * @dev: Device we arrived on/are leaving by
187 * @input_dev: Device we arrived on
188 * @h: Transport layer header
189 * @nh: Network layer header
190 * @mac: Link layer header
191 * @dst: destination entry
192 * @sp: the security path, used for xfrm
193 * @cb: Control buffer. Free for use by every layer. Put private vars here
194 * @len: Length of actual data
195 * @data_len: Data length
196 * @mac_len: Length of link layer header
197 * @csum: Checksum
198 * @local_df: allow local fragmentation
199 * @cloned: Head may be cloned (check refcnt to be sure)
200 * @nohdr: Payload reference only, must not modify header
201 * @proto_data_valid: Protocol data validated since arriving at localhost
202 * @proto_csum_blank: Protocol csum must be added before leaving localhost
203 * @pkt_type: Packet class
204 * @fclone: skbuff clone status
205 * @ip_summed: Driver fed us an IP checksum
206 * @priority: Packet queueing priority
207 * @users: User count - see {datagram,tcp}.c
208 * @protocol: Packet protocol from driver
209 * @truesize: Buffer size
210 * @head: Head of buffer
211 * @data: Data head pointer
212 * @tail: Tail pointer
213 * @end: End pointer
214 * @destructor: Destruct function
215 * @nfmark: Can be used for communication between hooks
216 * @nfct: Associated connection, if any
217 * @ipvs_property: skbuff is owned by ipvs
218 * @nfctinfo: Relationship of this skb to the connection
219 * @nfct_reasm: netfilter conntrack re-assembly pointer
220 * @nf_bridge: Saved data about a bridged frame - see br_netfilter.c
221 * @tc_index: Traffic control index
222 * @tc_verd: traffic control verdict
223 */
225 struct sk_buff {
226 /* These two members must be first. */
227 struct sk_buff *next;
228 struct sk_buff *prev;
230 struct sock *sk;
231 struct skb_timeval tstamp;
232 struct net_device *dev;
233 struct net_device *input_dev;
235 union {
236 struct tcphdr *th;
237 struct udphdr *uh;
238 struct icmphdr *icmph;
239 struct igmphdr *igmph;
240 struct iphdr *ipiph;
241 struct ipv6hdr *ipv6h;
242 unsigned char *raw;
243 } h;
245 union {
246 struct iphdr *iph;
247 struct ipv6hdr *ipv6h;
248 struct arphdr *arph;
249 unsigned char *raw;
250 } nh;
252 union {
253 unsigned char *raw;
254 } mac;
256 struct dst_entry *dst;
257 struct sec_path *sp;
259 /*
260 * This is the control buffer. It is free to use for every
261 * layer. Please put your private variables there. If you
262 * want to keep them across layers you have to do a skb_clone()
263 * first. This is owned by whoever has the skb queued ATM.
264 */
265 char cb[48];
267 unsigned int len,
268 data_len,
269 mac_len,
270 csum;
271 __u32 priority;
272 __u8 local_df:1,
273 cloned:1,
274 ip_summed:2,
275 nohdr:1,
276 nfctinfo:3;
277 __u8 pkt_type:3,
278 fclone:2,
279 #ifndef CONFIG_XEN
280 ipvs_property:1;
281 #else
282 ipvs_property:1,
283 proto_data_valid:1,
284 proto_csum_blank:1;
285 #endif
286 __be16 protocol;
288 void (*destructor)(struct sk_buff *skb);
289 #ifdef CONFIG_NETFILTER
290 __u32 nfmark;
291 struct nf_conntrack *nfct;
292 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
293 struct sk_buff *nfct_reasm;
294 #endif
295 #ifdef CONFIG_BRIDGE_NETFILTER
296 struct nf_bridge_info *nf_bridge;
297 #endif
298 #endif /* CONFIG_NETFILTER */
299 #ifdef CONFIG_NET_SCHED
300 __u16 tc_index; /* traffic control index */
301 #ifdef CONFIG_NET_CLS_ACT
302 __u16 tc_verd; /* traffic control verdict */
303 #endif
304 #endif
307 /* These elements must be at the end, see alloc_skb() for details. */
308 unsigned int truesize;
309 atomic_t users;
310 unsigned char *head,
311 *data,
312 *tail,
313 *end;
314 };
316 #ifdef __KERNEL__
317 /*
318 * Handling routines are only of interest to the kernel
319 */
320 #include <linux/slab.h>
322 #include <asm/system.h>
324 extern void __kfree_skb(struct sk_buff *skb);
325 extern struct sk_buff *__alloc_skb(unsigned int size,
326 gfp_t priority, int fclone);
327 static inline struct sk_buff *alloc_skb(unsigned int size,
328 gfp_t priority)
329 {
330 return __alloc_skb(size, priority, 0);
331 }
333 static inline struct sk_buff *alloc_skb_fclone(unsigned int size,
334 gfp_t priority)
335 {
336 return __alloc_skb(size, priority, 1);
337 }
339 extern struct sk_buff *alloc_skb_from_cache(kmem_cache_t *cp,
340 unsigned int size,
341 gfp_t priority,
342 int fclone);
343 extern void kfree_skbmem(struct sk_buff *skb);
344 extern struct sk_buff *skb_clone(struct sk_buff *skb,
345 gfp_t priority);
346 extern struct sk_buff *skb_copy(const struct sk_buff *skb,
347 gfp_t priority);
348 extern struct sk_buff *pskb_copy(struct sk_buff *skb,
349 gfp_t gfp_mask);
350 extern int pskb_expand_head(struct sk_buff *skb,
351 int nhead, int ntail,
352 gfp_t gfp_mask);
353 extern struct sk_buff *skb_realloc_headroom(struct sk_buff *skb,
354 unsigned int headroom);
355 extern struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
356 int newheadroom, int newtailroom,
357 gfp_t priority);
358 extern struct sk_buff * skb_pad(struct sk_buff *skb, int pad);
359 #define dev_kfree_skb(a) kfree_skb(a)
360 extern void skb_over_panic(struct sk_buff *skb, int len,
361 void *here);
362 extern void skb_under_panic(struct sk_buff *skb, int len,
363 void *here);
365 extern int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
366 int getfrag(void *from, char *to, int offset,
367 int len,int odd, struct sk_buff *skb),
368 void *from, int length);
370 struct skb_seq_state
371 {
372 __u32 lower_offset;
373 __u32 upper_offset;
374 __u32 frag_idx;
375 __u32 stepped_offset;
376 struct sk_buff *root_skb;
377 struct sk_buff *cur_skb;
378 __u8 *frag_data;
379 };
381 extern void skb_prepare_seq_read(struct sk_buff *skb,
382 unsigned int from, unsigned int to,
383 struct skb_seq_state *st);
384 extern unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
385 struct skb_seq_state *st);
386 extern void skb_abort_seq_read(struct skb_seq_state *st);
388 extern unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
389 unsigned int to, struct ts_config *config,
390 struct ts_state *state);
392 /* Internal */
393 #define skb_shinfo(SKB) ((struct skb_shared_info *)((SKB)->end))
395 /**
396 * skb_queue_empty - check if a queue is empty
397 * @list: queue head
398 *
399 * Returns true if the queue is empty, false otherwise.
400 */
401 static inline int skb_queue_empty(const struct sk_buff_head *list)
402 {
403 return list->next == (struct sk_buff *)list;
404 }
406 /**
407 * skb_get - reference buffer
408 * @skb: buffer to reference
409 *
410 * Makes another reference to a socket buffer and returns a pointer
411 * to the buffer.
412 */
413 static inline struct sk_buff *skb_get(struct sk_buff *skb)
414 {
415 atomic_inc(&skb->users);
416 return skb;
417 }
419 /*
420 * If users == 1, we are the only owner and are can avoid redundant
421 * atomic change.
422 */
424 /**
425 * kfree_skb - free an sk_buff
426 * @skb: buffer to free
427 *
428 * Drop a reference to the buffer and free it if the usage count has
429 * hit zero.
430 */
431 static inline void kfree_skb(struct sk_buff *skb)
432 {
433 if (likely(atomic_read(&skb->users) == 1))
434 smp_rmb();
435 else if (likely(!atomic_dec_and_test(&skb->users)))
436 return;
437 __kfree_skb(skb);
438 }
440 /**
441 * skb_cloned - is the buffer a clone
442 * @skb: buffer to check
443 *
444 * Returns true if the buffer was generated with skb_clone() and is
445 * one of multiple shared copies of the buffer. Cloned buffers are
446 * shared data so must not be written to under normal circumstances.
447 */
448 static inline int skb_cloned(const struct sk_buff *skb)
449 {
450 return skb->cloned &&
451 (atomic_read(&skb_shinfo(skb)->dataref) & SKB_DATAREF_MASK) != 1;
452 }
454 /**
455 * skb_header_cloned - is the header a clone
456 * @skb: buffer to check
457 *
458 * Returns true if modifying the header part of the buffer requires
459 * the data to be copied.
460 */
461 static inline int skb_header_cloned(const struct sk_buff *skb)
462 {
463 int dataref;
465 if (!skb->cloned)
466 return 0;
468 dataref = atomic_read(&skb_shinfo(skb)->dataref);
469 dataref = (dataref & SKB_DATAREF_MASK) - (dataref >> SKB_DATAREF_SHIFT);
470 return dataref != 1;
471 }
473 /**
474 * skb_header_release - release reference to header
475 * @skb: buffer to operate on
476 *
477 * Drop a reference to the header part of the buffer. This is done
478 * by acquiring a payload reference. You must not read from the header
479 * part of skb->data after this.
480 */
481 static inline void skb_header_release(struct sk_buff *skb)
482 {
483 BUG_ON(skb->nohdr);
484 skb->nohdr = 1;
485 atomic_add(1 << SKB_DATAREF_SHIFT, &skb_shinfo(skb)->dataref);
486 }
488 /**
489 * skb_shared - is the buffer shared
490 * @skb: buffer to check
491 *
492 * Returns true if more than one person has a reference to this
493 * buffer.
494 */
495 static inline int skb_shared(const struct sk_buff *skb)
496 {
497 return atomic_read(&skb->users) != 1;
498 }
500 /**
501 * skb_share_check - check if buffer is shared and if so clone it
502 * @skb: buffer to check
503 * @pri: priority for memory allocation
504 *
505 * If the buffer is shared the buffer is cloned and the old copy
506 * drops a reference. A new clone with a single reference is returned.
507 * If the buffer is not shared the original buffer is returned. When
508 * being called from interrupt status or with spinlocks held pri must
509 * be GFP_ATOMIC.
510 *
511 * NULL is returned on a memory allocation failure.
512 */
513 static inline struct sk_buff *skb_share_check(struct sk_buff *skb,
514 gfp_t pri)
515 {
516 might_sleep_if(pri & __GFP_WAIT);
517 if (skb_shared(skb)) {
518 struct sk_buff *nskb = skb_clone(skb, pri);
519 kfree_skb(skb);
520 skb = nskb;
521 }
522 return skb;
523 }
525 /*
526 * Copy shared buffers into a new sk_buff. We effectively do COW on
527 * packets to handle cases where we have a local reader and forward
528 * and a couple of other messy ones. The normal one is tcpdumping
529 * a packet thats being forwarded.
530 */
532 /**
533 * skb_unshare - make a copy of a shared buffer
534 * @skb: buffer to check
535 * @pri: priority for memory allocation
536 *
537 * If the socket buffer is a clone then this function creates a new
538 * copy of the data, drops a reference count on the old copy and returns
539 * the new copy with the reference count at 1. If the buffer is not a clone
540 * the original buffer is returned. When called with a spinlock held or
541 * from interrupt state @pri must be %GFP_ATOMIC
542 *
543 * %NULL is returned on a memory allocation failure.
544 */
545 static inline struct sk_buff *skb_unshare(struct sk_buff *skb,
546 gfp_t pri)
547 {
548 might_sleep_if(pri & __GFP_WAIT);
549 if (skb_cloned(skb)) {
550 struct sk_buff *nskb = skb_copy(skb, pri);
551 kfree_skb(skb); /* Free our shared copy */
552 skb = nskb;
553 }
554 return skb;
555 }
557 /**
558 * skb_peek
559 * @list_: list to peek at
560 *
561 * Peek an &sk_buff. Unlike most other operations you _MUST_
562 * be careful with this one. A peek leaves the buffer on the
563 * list and someone else may run off with it. You must hold
564 * the appropriate locks or have a private queue to do this.
565 *
566 * Returns %NULL for an empty list or a pointer to the head element.
567 * The reference count is not incremented and the reference is therefore
568 * volatile. Use with caution.
569 */
570 static inline struct sk_buff *skb_peek(struct sk_buff_head *list_)
571 {
572 struct sk_buff *list = ((struct sk_buff *)list_)->next;
573 if (list == (struct sk_buff *)list_)
574 list = NULL;
575 return list;
576 }
578 /**
579 * skb_peek_tail
580 * @list_: list to peek at
581 *
582 * Peek an &sk_buff. Unlike most other operations you _MUST_
583 * be careful with this one. A peek leaves the buffer on the
584 * list and someone else may run off with it. You must hold
585 * the appropriate locks or have a private queue to do this.
586 *
587 * Returns %NULL for an empty list or a pointer to the tail element.
588 * The reference count is not incremented and the reference is therefore
589 * volatile. Use with caution.
590 */
591 static inline struct sk_buff *skb_peek_tail(struct sk_buff_head *list_)
592 {
593 struct sk_buff *list = ((struct sk_buff *)list_)->prev;
594 if (list == (struct sk_buff *)list_)
595 list = NULL;
596 return list;
597 }
599 /**
600 * skb_queue_len - get queue length
601 * @list_: list to measure
602 *
603 * Return the length of an &sk_buff queue.
604 */
605 static inline __u32 skb_queue_len(const struct sk_buff_head *list_)
606 {
607 return list_->qlen;
608 }
610 static inline void skb_queue_head_init(struct sk_buff_head *list)
611 {
612 spin_lock_init(&list->lock);
613 list->prev = list->next = (struct sk_buff *)list;
614 list->qlen = 0;
615 }
617 /*
618 * Insert an sk_buff at the start of a list.
619 *
620 * The "__skb_xxxx()" functions are the non-atomic ones that
621 * can only be called with interrupts disabled.
622 */
624 /**
625 * __skb_queue_after - queue a buffer at the list head
626 * @list: list to use
627 * @prev: place after this buffer
628 * @newsk: buffer to queue
629 *
630 * Queue a buffer int the middle of a list. This function takes no locks
631 * and you must therefore hold required locks before calling it.
632 *
633 * A buffer cannot be placed on two lists at the same time.
634 */
635 static inline void __skb_queue_after(struct sk_buff_head *list,
636 struct sk_buff *prev,
637 struct sk_buff *newsk)
638 {
639 struct sk_buff *next;
640 list->qlen++;
642 next = prev->next;
643 newsk->next = next;
644 newsk->prev = prev;
645 next->prev = prev->next = newsk;
646 }
648 /**
649 * __skb_queue_head - queue a buffer at the list head
650 * @list: list to use
651 * @newsk: buffer to queue
652 *
653 * Queue a buffer at the start of a list. This function takes no locks
654 * and you must therefore hold required locks before calling it.
655 *
656 * A buffer cannot be placed on two lists at the same time.
657 */
658 extern void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk);
659 static inline void __skb_queue_head(struct sk_buff_head *list,
660 struct sk_buff *newsk)
661 {
662 __skb_queue_after(list, (struct sk_buff *)list, newsk);
663 }
665 /**
666 * __skb_queue_tail - queue a buffer at the list tail
667 * @list: list to use
668 * @newsk: buffer to queue
669 *
670 * Queue a buffer at the end of a list. This function takes no locks
671 * and you must therefore hold required locks before calling it.
672 *
673 * A buffer cannot be placed on two lists at the same time.
674 */
675 extern void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk);
676 static inline void __skb_queue_tail(struct sk_buff_head *list,
677 struct sk_buff *newsk)
678 {
679 struct sk_buff *prev, *next;
681 list->qlen++;
682 next = (struct sk_buff *)list;
683 prev = next->prev;
684 newsk->next = next;
685 newsk->prev = prev;
686 next->prev = prev->next = newsk;
687 }
690 /**
691 * __skb_dequeue - remove from the head of the queue
692 * @list: list to dequeue from
693 *
694 * Remove the head of the list. This function does not take any locks
695 * so must be used with appropriate locks held only. The head item is
696 * returned or %NULL if the list is empty.
697 */
698 extern struct sk_buff *skb_dequeue(struct sk_buff_head *list);
699 static inline struct sk_buff *__skb_dequeue(struct sk_buff_head *list)
700 {
701 struct sk_buff *next, *prev, *result;
703 prev = (struct sk_buff *) list;
704 next = prev->next;
705 result = NULL;
706 if (next != prev) {
707 result = next;
708 next = next->next;
709 list->qlen--;
710 next->prev = prev;
711 prev->next = next;
712 result->next = result->prev = NULL;
713 }
714 return result;
715 }
718 /*
719 * Insert a packet on a list.
720 */
721 extern void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list);
722 static inline void __skb_insert(struct sk_buff *newsk,
723 struct sk_buff *prev, struct sk_buff *next,
724 struct sk_buff_head *list)
725 {
726 newsk->next = next;
727 newsk->prev = prev;
728 next->prev = prev->next = newsk;
729 list->qlen++;
730 }
732 /*
733 * Place a packet after a given packet in a list.
734 */
735 extern void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list);
736 static inline void __skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
737 {
738 __skb_insert(newsk, old, old->next, list);
739 }
741 /*
742 * remove sk_buff from list. _Must_ be called atomically, and with
743 * the list known..
744 */
745 extern void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list);
746 static inline void __skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
747 {
748 struct sk_buff *next, *prev;
750 list->qlen--;
751 next = skb->next;
752 prev = skb->prev;
753 skb->next = skb->prev = NULL;
754 next->prev = prev;
755 prev->next = next;
756 }
759 /* XXX: more streamlined implementation */
761 /**
762 * __skb_dequeue_tail - remove from the tail of the queue
763 * @list: list to dequeue from
764 *
765 * Remove the tail of the list. This function does not take any locks
766 * so must be used with appropriate locks held only. The tail item is
767 * returned or %NULL if the list is empty.
768 */
769 extern struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list);
770 static inline struct sk_buff *__skb_dequeue_tail(struct sk_buff_head *list)
771 {
772 struct sk_buff *skb = skb_peek_tail(list);
773 if (skb)
774 __skb_unlink(skb, list);
775 return skb;
776 }
779 static inline int skb_is_nonlinear(const struct sk_buff *skb)
780 {
781 return skb->data_len;
782 }
784 static inline unsigned int skb_headlen(const struct sk_buff *skb)
785 {
786 return skb->len - skb->data_len;
787 }
789 static inline int skb_pagelen(const struct sk_buff *skb)
790 {
791 int i, len = 0;
793 for (i = (int)skb_shinfo(skb)->nr_frags - 1; i >= 0; i--)
794 len += skb_shinfo(skb)->frags[i].size;
795 return len + skb_headlen(skb);
796 }
798 static inline void skb_fill_page_desc(struct sk_buff *skb, int i,
799 struct page *page, int off, int size)
800 {
801 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
803 frag->page = page;
804 frag->page_offset = off;
805 frag->size = size;
806 skb_shinfo(skb)->nr_frags = i + 1;
807 }
809 #define SKB_PAGE_ASSERT(skb) BUG_ON(skb_shinfo(skb)->nr_frags)
810 #define SKB_FRAG_ASSERT(skb) BUG_ON(skb_shinfo(skb)->frag_list)
811 #define SKB_LINEAR_ASSERT(skb) BUG_ON(skb_is_nonlinear(skb))
813 /*
814 * Add data to an sk_buff
815 */
816 static inline unsigned char *__skb_put(struct sk_buff *skb, unsigned int len)
817 {
818 unsigned char *tmp = skb->tail;
819 SKB_LINEAR_ASSERT(skb);
820 skb->tail += len;
821 skb->len += len;
822 return tmp;
823 }
825 /**
826 * skb_put - add data to a buffer
827 * @skb: buffer to use
828 * @len: amount of data to add
829 *
830 * This function extends the used data area of the buffer. If this would
831 * exceed the total buffer size the kernel will panic. A pointer to the
832 * first byte of the extra data is returned.
833 */
834 static inline unsigned char *skb_put(struct sk_buff *skb, unsigned int len)
835 {
836 unsigned char *tmp = skb->tail;
837 SKB_LINEAR_ASSERT(skb);
838 skb->tail += len;
839 skb->len += len;
840 if (unlikely(skb->tail>skb->end))
841 skb_over_panic(skb, len, current_text_addr());
842 return tmp;
843 }
845 static inline unsigned char *__skb_push(struct sk_buff *skb, unsigned int len)
846 {
847 skb->data -= len;
848 skb->len += len;
849 return skb->data;
850 }
852 /**
853 * skb_push - add data to the start of a buffer
854 * @skb: buffer to use
855 * @len: amount of data to add
856 *
857 * This function extends the used data area of the buffer at the buffer
858 * start. If this would exceed the total buffer headroom the kernel will
859 * panic. A pointer to the first byte of the extra data is returned.
860 */
861 static inline unsigned char *skb_push(struct sk_buff *skb, unsigned int len)
862 {
863 skb->data -= len;
864 skb->len += len;
865 if (unlikely(skb->data<skb->head))
866 skb_under_panic(skb, len, current_text_addr());
867 return skb->data;
868 }
870 static inline unsigned char *__skb_pull(struct sk_buff *skb, unsigned int len)
871 {
872 skb->len -= len;
873 BUG_ON(skb->len < skb->data_len);
874 return skb->data += len;
875 }
877 /**
878 * skb_pull - remove data from the start of a buffer
879 * @skb: buffer to use
880 * @len: amount of data to remove
881 *
882 * This function removes data from the start of a buffer, returning
883 * the memory to the headroom. A pointer to the next data in the buffer
884 * is returned. Once the data has been pulled future pushes will overwrite
885 * the old data.
886 */
887 static inline unsigned char *skb_pull(struct sk_buff *skb, unsigned int len)
888 {
889 return unlikely(len > skb->len) ? NULL : __skb_pull(skb, len);
890 }
892 extern unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta);
894 static inline unsigned char *__pskb_pull(struct sk_buff *skb, unsigned int len)
895 {
896 if (len > skb_headlen(skb) &&
897 !__pskb_pull_tail(skb, len-skb_headlen(skb)))
898 return NULL;
899 skb->len -= len;
900 return skb->data += len;
901 }
903 static inline unsigned char *pskb_pull(struct sk_buff *skb, unsigned int len)
904 {
905 return unlikely(len > skb->len) ? NULL : __pskb_pull(skb, len);
906 }
908 static inline int pskb_may_pull(struct sk_buff *skb, unsigned int len)
909 {
910 if (likely(len <= skb_headlen(skb)))
911 return 1;
912 if (unlikely(len > skb->len))
913 return 0;
914 return __pskb_pull_tail(skb, len-skb_headlen(skb)) != NULL;
915 }
917 /**
918 * skb_headroom - bytes at buffer head
919 * @skb: buffer to check
920 *
921 * Return the number of bytes of free space at the head of an &sk_buff.
922 */
923 static inline int skb_headroom(const struct sk_buff *skb)
924 {
925 return skb->data - skb->head;
926 }
928 /**
929 * skb_tailroom - bytes at buffer end
930 * @skb: buffer to check
931 *
932 * Return the number of bytes of free space at the tail of an sk_buff
933 */
934 static inline int skb_tailroom(const struct sk_buff *skb)
935 {
936 return skb_is_nonlinear(skb) ? 0 : skb->end - skb->tail;
937 }
939 /**
940 * skb_reserve - adjust headroom
941 * @skb: buffer to alter
942 * @len: bytes to move
943 *
944 * Increase the headroom of an empty &sk_buff by reducing the tail
945 * room. This is only allowed for an empty buffer.
946 */
947 static inline void skb_reserve(struct sk_buff *skb, int len)
948 {
949 skb->data += len;
950 skb->tail += len;
951 }
953 /*
954 * CPUs often take a performance hit when accessing unaligned memory
955 * locations. The actual performance hit varies, it can be small if the
956 * hardware handles it or large if we have to take an exception and fix it
957 * in software.
958 *
959 * Since an ethernet header is 14 bytes network drivers often end up with
960 * the IP header at an unaligned offset. The IP header can be aligned by
961 * shifting the start of the packet by 2 bytes. Drivers should do this
962 * with:
963 *
964 * skb_reserve(NET_IP_ALIGN);
965 *
966 * The downside to this alignment of the IP header is that the DMA is now
967 * unaligned. On some architectures the cost of an unaligned DMA is high
968 * and this cost outweighs the gains made by aligning the IP header.
969 *
970 * Since this trade off varies between architectures, we allow NET_IP_ALIGN
971 * to be overridden.
972 */
973 #ifndef NET_IP_ALIGN
974 #define NET_IP_ALIGN 2
975 #endif
977 extern int ___pskb_trim(struct sk_buff *skb, unsigned int len, int realloc);
979 static inline void __skb_trim(struct sk_buff *skb, unsigned int len)
980 {
981 if (!skb->data_len) {
982 skb->len = len;
983 skb->tail = skb->data + len;
984 } else
985 ___pskb_trim(skb, len, 0);
986 }
988 /**
989 * skb_trim - remove end from a buffer
990 * @skb: buffer to alter
991 * @len: new length
992 *
993 * Cut the length of a buffer down by removing data from the tail. If
994 * the buffer is already under the length specified it is not modified.
995 */
996 static inline void skb_trim(struct sk_buff *skb, unsigned int len)
997 {
998 if (skb->len > len)
999 __skb_trim(skb, len);
1003 static inline int __pskb_trim(struct sk_buff *skb, unsigned int len)
1005 if (!skb->data_len) {
1006 skb->len = len;
1007 skb->tail = skb->data+len;
1008 return 0;
1010 return ___pskb_trim(skb, len, 1);
1013 static inline int pskb_trim(struct sk_buff *skb, unsigned int len)
1015 return (len < skb->len) ? __pskb_trim(skb, len) : 0;
1018 /**
1019 * skb_orphan - orphan a buffer
1020 * @skb: buffer to orphan
1022 * If a buffer currently has an owner then we call the owner's
1023 * destructor function and make the @skb unowned. The buffer continues
1024 * to exist but is no longer charged to its former owner.
1025 */
1026 static inline void skb_orphan(struct sk_buff *skb)
1028 if (skb->destructor)
1029 skb->destructor(skb);
1030 skb->destructor = NULL;
1031 skb->sk = NULL;
1034 /**
1035 * __skb_queue_purge - empty a list
1036 * @list: list to empty
1038 * Delete all buffers on an &sk_buff list. Each buffer is removed from
1039 * the list and one reference dropped. This function does not take the
1040 * list lock and the caller must hold the relevant locks to use it.
1041 */
1042 extern void skb_queue_purge(struct sk_buff_head *list);
1043 static inline void __skb_queue_purge(struct sk_buff_head *list)
1045 struct sk_buff *skb;
1046 while ((skb = __skb_dequeue(list)) != NULL)
1047 kfree_skb(skb);
1050 #ifndef CONFIG_HAVE_ARCH_DEV_ALLOC_SKB
1051 /**
1052 * __dev_alloc_skb - allocate an skbuff for sending
1053 * @length: length to allocate
1054 * @gfp_mask: get_free_pages mask, passed to alloc_skb
1056 * Allocate a new &sk_buff and assign it a usage count of one. The
1057 * buffer has unspecified headroom built in. Users should allocate
1058 * the headroom they think they need without accounting for the
1059 * built in space. The built in space is used for optimisations.
1061 * %NULL is returned in there is no free memory.
1062 */
1063 static inline struct sk_buff *__dev_alloc_skb(unsigned int length,
1064 gfp_t gfp_mask)
1066 struct sk_buff *skb = alloc_skb(length + 16, gfp_mask);
1067 if (likely(skb))
1068 skb_reserve(skb, 16);
1069 return skb;
1071 #else
1072 extern struct sk_buff *__dev_alloc_skb(unsigned int length, gfp_t gfp_mask);
1073 #endif
1075 /**
1076 * dev_alloc_skb - allocate an skbuff for sending
1077 * @length: length to allocate
1079 * Allocate a new &sk_buff and assign it a usage count of one. The
1080 * buffer has unspecified headroom built in. Users should allocate
1081 * the headroom they think they need without accounting for the
1082 * built in space. The built in space is used for optimisations.
1084 * %NULL is returned in there is no free memory. Although this function
1085 * allocates memory it can be called from an interrupt.
1086 */
1087 static inline struct sk_buff *dev_alloc_skb(unsigned int length)
1089 return __dev_alloc_skb(length, GFP_ATOMIC);
1092 /**
1093 * skb_cow - copy header of skb when it is required
1094 * @skb: buffer to cow
1095 * @headroom: needed headroom
1097 * If the skb passed lacks sufficient headroom or its data part
1098 * is shared, data is reallocated. If reallocation fails, an error
1099 * is returned and original skb is not changed.
1101 * The result is skb with writable area skb->head...skb->tail
1102 * and at least @headroom of space at head.
1103 */
1104 static inline int skb_cow(struct sk_buff *skb, unsigned int headroom)
1106 int delta = (headroom > 16 ? headroom : 16) - skb_headroom(skb);
1108 if (delta < 0)
1109 delta = 0;
1111 if (delta || skb_cloned(skb))
1112 return pskb_expand_head(skb, (delta + 15) & ~15, 0, GFP_ATOMIC);
1113 return 0;
1116 /**
1117 * skb_padto - pad an skbuff up to a minimal size
1118 * @skb: buffer to pad
1119 * @len: minimal length
1121 * Pads up a buffer to ensure the trailing bytes exist and are
1122 * blanked. If the buffer already contains sufficient data it
1123 * is untouched. Returns the buffer, which may be a replacement
1124 * for the original, or NULL for out of memory - in which case
1125 * the original buffer is still freed.
1126 */
1128 static inline struct sk_buff *skb_padto(struct sk_buff *skb, unsigned int len)
1130 unsigned int size = skb->len;
1131 if (likely(size >= len))
1132 return skb;
1133 return skb_pad(skb, len-size);
1136 static inline int skb_add_data(struct sk_buff *skb,
1137 char __user *from, int copy)
1139 const int off = skb->len;
1141 if (skb->ip_summed == CHECKSUM_NONE) {
1142 int err = 0;
1143 unsigned int csum = csum_and_copy_from_user(from,
1144 skb_put(skb, copy),
1145 copy, 0, &err);
1146 if (!err) {
1147 skb->csum = csum_block_add(skb->csum, csum, off);
1148 return 0;
1150 } else if (!copy_from_user(skb_put(skb, copy), from, copy))
1151 return 0;
1153 __skb_trim(skb, off);
1154 return -EFAULT;
1157 static inline int skb_can_coalesce(struct sk_buff *skb, int i,
1158 struct page *page, int off)
1160 if (i) {
1161 struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[i - 1];
1163 return page == frag->page &&
1164 off == frag->page_offset + frag->size;
1166 return 0;
1169 static inline int __skb_linearize(struct sk_buff *skb)
1171 return __pskb_pull_tail(skb, skb->data_len) ? 0 : -ENOMEM;
1174 /**
1175 * skb_linearize - convert paged skb to linear one
1176 * @skb: buffer to linarize
1178 * If there is no free memory -ENOMEM is returned, otherwise zero
1179 * is returned and the old skb data released.
1180 */
1181 static inline int skb_linearize(struct sk_buff *skb)
1183 return skb_is_nonlinear(skb) ? __skb_linearize(skb) : 0;
1186 /**
1187 * skb_linearize_cow - make sure skb is linear and writable
1188 * @skb: buffer to process
1190 * If there is no free memory -ENOMEM is returned, otherwise zero
1191 * is returned and the old skb data released.
1192 */
1193 static inline int skb_linearize_cow(struct sk_buff *skb)
1195 return skb_is_nonlinear(skb) || skb_cloned(skb) ?
1196 __skb_linearize(skb) : 0;
1199 /**
1200 * skb_postpull_rcsum - update checksum for received skb after pull
1201 * @skb: buffer to update
1202 * @start: start of data before pull
1203 * @len: length of data pulled
1205 * After doing a pull on a received packet, you need to call this to
1206 * update the CHECKSUM_HW checksum, or set ip_summed to CHECKSUM_NONE
1207 * so that it can be recomputed from scratch.
1208 */
1210 static inline void skb_postpull_rcsum(struct sk_buff *skb,
1211 const void *start, int len)
1213 if (skb->ip_summed == CHECKSUM_HW)
1214 skb->csum = csum_sub(skb->csum, csum_partial(start, len, 0));
1217 /**
1218 * pskb_trim_rcsum - trim received skb and update checksum
1219 * @skb: buffer to trim
1220 * @len: new length
1222 * This is exactly the same as pskb_trim except that it ensures the
1223 * checksum of received packets are still valid after the operation.
1224 */
1226 static inline int pskb_trim_rcsum(struct sk_buff *skb, unsigned int len)
1228 if (likely(len >= skb->len))
1229 return 0;
1230 if (skb->ip_summed == CHECKSUM_HW)
1231 skb->ip_summed = CHECKSUM_NONE;
1232 return __pskb_trim(skb, len);
1235 static inline void *kmap_skb_frag(const skb_frag_t *frag)
1237 #ifdef CONFIG_HIGHMEM
1238 BUG_ON(in_irq());
1240 local_bh_disable();
1241 #endif
1242 return kmap_atomic(frag->page, KM_SKB_DATA_SOFTIRQ);
1245 static inline void kunmap_skb_frag(void *vaddr)
1247 kunmap_atomic(vaddr, KM_SKB_DATA_SOFTIRQ);
1248 #ifdef CONFIG_HIGHMEM
1249 local_bh_enable();
1250 #endif
1253 #define skb_queue_walk(queue, skb) \
1254 for (skb = (queue)->next; \
1255 prefetch(skb->next), (skb != (struct sk_buff *)(queue)); \
1256 skb = skb->next)
1258 #define skb_queue_reverse_walk(queue, skb) \
1259 for (skb = (queue)->prev; \
1260 prefetch(skb->prev), (skb != (struct sk_buff *)(queue)); \
1261 skb = skb->prev)
1264 extern struct sk_buff *skb_recv_datagram(struct sock *sk, unsigned flags,
1265 int noblock, int *err);
1266 extern unsigned int datagram_poll(struct file *file, struct socket *sock,
1267 struct poll_table_struct *wait);
1268 extern int skb_copy_datagram_iovec(const struct sk_buff *from,
1269 int offset, struct iovec *to,
1270 int size);
1271 extern int skb_copy_and_csum_datagram_iovec(struct sk_buff *skb,
1272 int hlen,
1273 struct iovec *iov);
1274 extern void skb_free_datagram(struct sock *sk, struct sk_buff *skb);
1275 extern void skb_kill_datagram(struct sock *sk, struct sk_buff *skb,
1276 unsigned int flags);
1277 extern unsigned int skb_checksum(const struct sk_buff *skb, int offset,
1278 int len, unsigned int csum);
1279 extern int skb_copy_bits(const struct sk_buff *skb, int offset,
1280 void *to, int len);
1281 extern int skb_store_bits(const struct sk_buff *skb, int offset,
1282 void *from, int len);
1283 extern unsigned int skb_copy_and_csum_bits(const struct sk_buff *skb,
1284 int offset, u8 *to, int len,
1285 unsigned int csum);
1286 extern void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to);
1287 extern void skb_split(struct sk_buff *skb,
1288 struct sk_buff *skb1, const u32 len);
1290 extern void skb_release_data(struct sk_buff *skb);
1291 extern struct sk_buff *skb_segment(struct sk_buff *skb, int features);
1293 static inline void *skb_header_pointer(const struct sk_buff *skb, int offset,
1294 int len, void *buffer)
1296 int hlen = skb_headlen(skb);
1298 if (hlen - offset >= len)
1299 return skb->data + offset;
1301 if (skb_copy_bits(skb, offset, buffer, len) < 0)
1302 return NULL;
1304 return buffer;
1307 extern void skb_init(void);
1308 extern void skb_add_mtu(int mtu);
1310 /**
1311 * skb_get_timestamp - get timestamp from a skb
1312 * @skb: skb to get stamp from
1313 * @stamp: pointer to struct timeval to store stamp in
1315 * Timestamps are stored in the skb as offsets to a base timestamp.
1316 * This function converts the offset back to a struct timeval and stores
1317 * it in stamp.
1318 */
1319 static inline void skb_get_timestamp(const struct sk_buff *skb, struct timeval *stamp)
1321 stamp->tv_sec = skb->tstamp.off_sec;
1322 stamp->tv_usec = skb->tstamp.off_usec;
1325 /**
1326 * skb_set_timestamp - set timestamp of a skb
1327 * @skb: skb to set stamp of
1328 * @stamp: pointer to struct timeval to get stamp from
1330 * Timestamps are stored in the skb as offsets to a base timestamp.
1331 * This function converts a struct timeval to an offset and stores
1332 * it in the skb.
1333 */
1334 static inline void skb_set_timestamp(struct sk_buff *skb, const struct timeval *stamp)
1336 skb->tstamp.off_sec = stamp->tv_sec;
1337 skb->tstamp.off_usec = stamp->tv_usec;
1340 extern void __net_timestamp(struct sk_buff *skb);
1342 extern unsigned int __skb_checksum_complete(struct sk_buff *skb);
1344 /**
1345 * skb_checksum_complete - Calculate checksum of an entire packet
1346 * @skb: packet to process
1348 * This function calculates the checksum over the entire packet plus
1349 * the value of skb->csum. The latter can be used to supply the
1350 * checksum of a pseudo header as used by TCP/UDP. It returns the
1351 * checksum.
1353 * For protocols that contain complete checksums such as ICMP/TCP/UDP,
1354 * this function can be used to verify that checksum on received
1355 * packets. In that case the function should return zero if the
1356 * checksum is correct. In particular, this function will return zero
1357 * if skb->ip_summed is CHECKSUM_UNNECESSARY which indicates that the
1358 * hardware has already verified the correctness of the checksum.
1359 */
1360 static inline unsigned int skb_checksum_complete(struct sk_buff *skb)
1362 return skb->ip_summed != CHECKSUM_UNNECESSARY &&
1363 __skb_checksum_complete(skb);
1366 #ifdef CONFIG_NETFILTER
1367 static inline void nf_conntrack_put(struct nf_conntrack *nfct)
1369 if (nfct && atomic_dec_and_test(&nfct->use))
1370 nfct->destroy(nfct);
1372 static inline void nf_conntrack_get(struct nf_conntrack *nfct)
1374 if (nfct)
1375 atomic_inc(&nfct->use);
1377 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
1378 static inline void nf_conntrack_get_reasm(struct sk_buff *skb)
1380 if (skb)
1381 atomic_inc(&skb->users);
1383 static inline void nf_conntrack_put_reasm(struct sk_buff *skb)
1385 if (skb)
1386 kfree_skb(skb);
1388 #endif
1389 static inline void nf_reset(struct sk_buff *skb)
1391 nf_conntrack_put(skb->nfct);
1392 skb->nfct = NULL;
1393 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
1394 nf_conntrack_put_reasm(skb->nfct_reasm);
1395 skb->nfct_reasm = NULL;
1396 #endif
1399 #ifdef CONFIG_BRIDGE_NETFILTER
1400 static inline void nf_bridge_put(struct nf_bridge_info *nf_bridge)
1402 if (nf_bridge && atomic_dec_and_test(&nf_bridge->use))
1403 kfree(nf_bridge);
1405 static inline void nf_bridge_get(struct nf_bridge_info *nf_bridge)
1407 if (nf_bridge)
1408 atomic_inc(&nf_bridge->use);
1410 #endif /* CONFIG_BRIDGE_NETFILTER */
1411 #else /* CONFIG_NETFILTER */
1412 static inline void nf_reset(struct sk_buff *skb) {}
1413 #endif /* CONFIG_NETFILTER */
1415 static inline int skb_is_gso(const struct sk_buff *skb)
1417 return skb_shinfo(skb)->gso_size;
1420 #endif /* __KERNEL__ */
1421 #endif /* _LINUX_SKBUFF_H */