Luca's patch ported
[cbs-scheduler.git] / include / linux / skbuff.h
blob336a46093a5b555c73329192bb81415bdb744c55
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/kmemcheck.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/net.h>
27 #include <linux/textsearch.h>
28 #include <net/checksum.h>
29 #include <linux/rcupdate.h>
30 #include <linux/dmaengine.h>
31 #include <linux/hrtimer.h>
33 #define HAVE_ALLOC_SKB /* For the drivers to know */
34 #define HAVE_ALIGNABLE_SKB /* Ditto 8) */
36 /* Don't change this without changing skb_csum_unnecessary! */
37 #define CHECKSUM_NONE 0
38 #define CHECKSUM_UNNECESSARY 1
39 #define CHECKSUM_COMPLETE 2
40 #define CHECKSUM_PARTIAL 3
42 #define SKB_DATA_ALIGN(X) (((X) + (SMP_CACHE_BYTES - 1)) & \
43 ~(SMP_CACHE_BYTES - 1))
44 #define SKB_WITH_OVERHEAD(X) \
45 ((X) - SKB_DATA_ALIGN(sizeof(struct skb_shared_info)))
46 #define SKB_MAX_ORDER(X, ORDER) \
47 SKB_WITH_OVERHEAD((PAGE_SIZE << (ORDER)) - (X))
48 #define SKB_MAX_HEAD(X) (SKB_MAX_ORDER((X), 0))
49 #define SKB_MAX_ALLOC (SKB_MAX_ORDER(0, 2))
51 /* A. Checksumming of received packets by device.
53 * NONE: device failed to checksum this packet.
54 * skb->csum is undefined.
56 * UNNECESSARY: device parsed packet and wouldbe verified checksum.
57 * skb->csum is undefined.
58 * It is bad option, but, unfortunately, many of vendors do this.
59 * Apparently with secret goal to sell you new device, when you
60 * will add new protocol to your host. F.e. IPv6. 8)
62 * COMPLETE: the most generic way. Device supplied checksum of _all_
63 * the packet as seen by netif_rx in skb->csum.
64 * NOTE: Even if device supports only some protocols, but
65 * is able to produce some skb->csum, it MUST use COMPLETE,
66 * not UNNECESSARY.
68 * PARTIAL: identical to the case for output below. This may occur
69 * on a packet received directly from another Linux OS, e.g.,
70 * a virtualised Linux kernel on the same host. The packet can
71 * be treated in the same way as UNNECESSARY except that on
72 * output (i.e., forwarding) the checksum must be filled in
73 * by the OS or the hardware.
75 * B. Checksumming on output.
77 * NONE: skb is checksummed by protocol or csum is not required.
79 * PARTIAL: device is required to csum packet as seen by hard_start_xmit
80 * from skb->csum_start to the end and to record the checksum
81 * at skb->csum_start + skb->csum_offset.
83 * Device must show its capabilities in dev->features, set
84 * at device setup time.
85 * NETIF_F_HW_CSUM - it is clever device, it is able to checksum
86 * everything.
87 * NETIF_F_NO_CSUM - loopback or reliable single hop media.
88 * NETIF_F_IP_CSUM - device is dumb. It is able to csum only
89 * TCP/UDP over IPv4. Sigh. Vendors like this
90 * way by an unknown reason. Though, see comment above
91 * about CHECKSUM_UNNECESSARY. 8)
92 * NETIF_F_IPV6_CSUM about as dumb as the last one but does IPv6 instead.
94 * Any questions? No questions, good. --ANK
97 struct net_device;
98 struct scatterlist;
99 struct pipe_inode_info;
101 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
102 struct nf_conntrack {
103 atomic_t use;
104 #ifdef CONFIG_PREEMPT_RT
105 struct rcu_head rcu;
106 #endif
108 #endif
110 #ifdef CONFIG_BRIDGE_NETFILTER
111 struct nf_bridge_info {
112 atomic_t use;
113 struct net_device *physindev;
114 struct net_device *physoutdev;
115 unsigned int mask;
116 unsigned long data[32 / sizeof(unsigned long)];
118 #endif
120 struct sk_buff_head {
121 /* These two members must be first. */
122 struct sk_buff *next;
123 struct sk_buff *prev;
125 __u32 qlen;
126 spinlock_t lock;
129 struct sk_buff;
131 /* To allow 64K frame to be packed as single skb without frag_list */
132 #define MAX_SKB_FRAGS (65536/PAGE_SIZE + 2)
134 typedef struct skb_frag_struct skb_frag_t;
136 struct skb_frag_struct {
137 struct page *page;
138 __u32 page_offset;
139 __u32 size;
142 /* This data is invariant across clones and lives at
143 * the end of the header data, ie. at skb->end.
145 struct skb_shared_info {
146 atomic_t dataref;
147 unsigned short nr_frags;
148 unsigned short gso_size;
149 /* Warning: this field is not always filled in (UFO)! */
150 unsigned short gso_segs;
151 unsigned short gso_type;
152 __be32 ip6_frag_id;
153 #ifdef CONFIG_HAS_DMA
154 unsigned int num_dma_maps;
155 #endif
156 struct sk_buff *frag_list;
157 skb_frag_t frags[MAX_SKB_FRAGS];
158 #ifdef CONFIG_HAS_DMA
159 dma_addr_t dma_maps[MAX_SKB_FRAGS + 1];
160 #endif
163 /* We divide dataref into two halves. The higher 16 bits hold references
164 * to the payload part of skb->data. The lower 16 bits hold references to
165 * the entire skb->data. A clone of a headerless skb holds the length of
166 * the header in skb->hdr_len.
168 * All users must obey the rule that the skb->data reference count must be
169 * greater than or equal to the payload reference count.
171 * Holding a reference to the payload part means that the user does not
172 * care about modifications to the header part of skb->data.
174 #define SKB_DATAREF_SHIFT 16
175 #define SKB_DATAREF_MASK ((1 << SKB_DATAREF_SHIFT) - 1)
178 enum {
179 SKB_FCLONE_UNAVAILABLE,
180 SKB_FCLONE_ORIG,
181 SKB_FCLONE_CLONE,
184 enum {
185 SKB_GSO_TCPV4 = 1 << 0,
186 SKB_GSO_UDP = 1 << 1,
188 /* This indicates the skb is from an untrusted source. */
189 SKB_GSO_DODGY = 1 << 2,
191 /* This indicates the tcp segment has CWR set. */
192 SKB_GSO_TCP_ECN = 1 << 3,
194 SKB_GSO_TCPV6 = 1 << 4,
197 #if BITS_PER_LONG > 32
198 #define NET_SKBUFF_DATA_USES_OFFSET 1
199 #endif
201 #ifdef NET_SKBUFF_DATA_USES_OFFSET
202 typedef unsigned int sk_buff_data_t;
203 #else
204 typedef unsigned char *sk_buff_data_t;
205 #endif
207 /**
208 * struct sk_buff - socket buffer
209 * @next: Next buffer in list
210 * @prev: Previous buffer in list
211 * @sk: Socket we are owned by
212 * @tstamp: Time we arrived
213 * @dev: Device we arrived on/are leaving by
214 * @transport_header: Transport layer header
215 * @network_header: Network layer header
216 * @mac_header: Link layer header
217 * @dst: destination entry
218 * @sp: the security path, used for xfrm
219 * @cb: Control buffer. Free for use by every layer. Put private vars here
220 * @len: Length of actual data
221 * @data_len: Data length
222 * @mac_len: Length of link layer header
223 * @hdr_len: writable header length of cloned skb
224 * @csum: Checksum (must include start/offset pair)
225 * @csum_start: Offset from skb->head where checksumming should start
226 * @csum_offset: Offset from csum_start where checksum should be stored
227 * @local_df: allow local fragmentation
228 * @cloned: Head may be cloned (check refcnt to be sure)
229 * @nohdr: Payload reference only, must not modify header
230 * @pkt_type: Packet class
231 * @fclone: skbuff clone status
232 * @ip_summed: Driver fed us an IP checksum
233 * @priority: Packet queueing priority
234 * @users: User count - see {datagram,tcp}.c
235 * @protocol: Packet protocol from driver
236 * @truesize: Buffer size
237 * @head: Head of buffer
238 * @data: Data head pointer
239 * @tail: Tail pointer
240 * @end: End pointer
241 * @destructor: Destruct function
242 * @mark: Generic packet mark
243 * @nfct: Associated connection, if any
244 * @ipvs_property: skbuff is owned by ipvs
245 * @peeked: this packet has been seen already, so stats have been
246 * done for it, don't do them again
247 * @nf_trace: netfilter packet trace flag
248 * @nfctinfo: Relationship of this skb to the connection
249 * @nfct_reasm: netfilter conntrack re-assembly pointer
250 * @nf_bridge: Saved data about a bridged frame - see br_netfilter.c
251 * @iif: ifindex of device we arrived on
252 * @queue_mapping: Queue mapping for multiqueue devices
253 * @tc_index: Traffic control index
254 * @tc_verd: traffic control verdict
255 * @ndisc_nodetype: router type (from link layer)
256 * @do_not_encrypt: set to prevent encryption of this frame
257 * @requeue: set to indicate that the wireless core should attempt
258 * a software retry on this frame if we failed to
259 * receive an ACK for it
260 * @dma_cookie: a cookie to one of several possible DMA operations
261 * done by skb DMA functions
262 * @secmark: security marking
263 * @vlan_tci: vlan tag control information
266 struct sk_buff {
267 /* These two members must be first. */
268 struct sk_buff *next;
269 struct sk_buff *prev;
271 struct sock *sk;
272 ktime_t tstamp;
273 struct net_device *dev;
275 union {
276 struct dst_entry *dst;
277 struct rtable *rtable;
279 #ifdef CONFIG_XFRM
280 struct sec_path *sp;
281 #endif
283 * This is the control buffer. It is free to use for every
284 * layer. Please put your private variables there. If you
285 * want to keep them across layers you have to do a skb_clone()
286 * first. This is owned by whoever has the skb queued ATM.
288 char cb[48];
290 unsigned int len,
291 data_len;
292 __u16 mac_len,
293 hdr_len;
294 union {
295 __wsum csum;
296 struct {
297 __u16 csum_start;
298 __u16 csum_offset;
301 __u32 priority;
302 kmemcheck_define_bitfield(flags1, {
303 __u8 local_df:1,
304 cloned:1,
305 ip_summed:2,
306 nohdr:1,
307 nfctinfo:3;
308 __u8 pkt_type:3,
309 fclone:2,
310 ipvs_property:1,
311 peeked:1,
312 nf_trace:1;
314 __be16 protocol;
316 void (*destructor)(struct sk_buff *skb);
317 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
318 struct nf_conntrack *nfct;
319 struct sk_buff *nfct_reasm;
320 #endif
321 #ifdef CONFIG_BRIDGE_NETFILTER
322 struct nf_bridge_info *nf_bridge;
323 #endif
325 int iif;
326 __u16 queue_mapping;
327 #ifdef CONFIG_NET_SCHED
328 __u16 tc_index; /* traffic control index */
329 #ifdef CONFIG_NET_CLS_ACT
330 __u16 tc_verd; /* traffic control verdict */
331 #endif
332 #endif
334 kmemcheck_define_bitfield(flags2, {
335 #ifdef CONFIG_IPV6_NDISC_NODETYPE
336 __u8 ndisc_nodetype:2;
337 #endif
338 #if defined(CONFIG_MAC80211) || defined(CONFIG_MAC80211_MODULE)
339 __u8 do_not_encrypt:1;
340 __u8 requeue:1;
341 #endif
344 /* 0/13/14 bit hole */
346 #ifdef CONFIG_NET_DMA
347 dma_cookie_t dma_cookie;
348 #endif
349 #ifdef CONFIG_NETWORK_SECMARK
350 __u32 secmark;
351 #endif
353 __u32 mark;
355 __u16 vlan_tci;
357 sk_buff_data_t transport_header;
358 sk_buff_data_t network_header;
359 sk_buff_data_t mac_header;
360 /* These elements must be at the end, see alloc_skb() for details. */
361 sk_buff_data_t tail;
362 sk_buff_data_t end;
363 unsigned char *head,
364 *data;
365 unsigned int truesize;
366 atomic_t users;
369 #ifdef __KERNEL__
371 * Handling routines are only of interest to the kernel
373 #include <linux/slab.h>
375 #include <asm/system.h>
377 #ifdef CONFIG_HAS_DMA
378 #include <linux/dma-mapping.h>
379 extern int skb_dma_map(struct device *dev, struct sk_buff *skb,
380 enum dma_data_direction dir);
381 extern void skb_dma_unmap(struct device *dev, struct sk_buff *skb,
382 enum dma_data_direction dir);
383 #endif
385 extern void kfree_skb(struct sk_buff *skb);
386 extern void __kfree_skb(struct sk_buff *skb);
387 extern struct sk_buff *__alloc_skb(unsigned int size,
388 gfp_t priority, int fclone, int node);
389 static inline struct sk_buff *alloc_skb(unsigned int size,
390 gfp_t priority)
392 return __alloc_skb(size, priority, 0, -1);
395 static inline struct sk_buff *alloc_skb_fclone(unsigned int size,
396 gfp_t priority)
398 return __alloc_skb(size, priority, 1, -1);
401 extern int skb_recycle_check(struct sk_buff *skb, int skb_size);
403 extern struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src);
404 extern struct sk_buff *skb_clone(struct sk_buff *skb,
405 gfp_t priority);
406 extern struct sk_buff *skb_copy(const struct sk_buff *skb,
407 gfp_t priority);
408 extern struct sk_buff *pskb_copy(struct sk_buff *skb,
409 gfp_t gfp_mask);
410 extern int pskb_expand_head(struct sk_buff *skb,
411 int nhead, int ntail,
412 gfp_t gfp_mask);
413 extern struct sk_buff *skb_realloc_headroom(struct sk_buff *skb,
414 unsigned int headroom);
415 extern struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
416 int newheadroom, int newtailroom,
417 gfp_t priority);
418 extern int skb_to_sgvec(struct sk_buff *skb,
419 struct scatterlist *sg, int offset,
420 int len);
421 extern int skb_cow_data(struct sk_buff *skb, int tailbits,
422 struct sk_buff **trailer);
423 extern int skb_pad(struct sk_buff *skb, int pad);
424 #define dev_kfree_skb(a) kfree_skb(a)
425 extern void skb_over_panic(struct sk_buff *skb, int len,
426 void *here);
427 extern void skb_under_panic(struct sk_buff *skb, int len,
428 void *here);
430 extern int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
431 int getfrag(void *from, char *to, int offset,
432 int len,int odd, struct sk_buff *skb),
433 void *from, int length);
435 struct skb_seq_state
437 __u32 lower_offset;
438 __u32 upper_offset;
439 __u32 frag_idx;
440 __u32 stepped_offset;
441 struct sk_buff *root_skb;
442 struct sk_buff *cur_skb;
443 __u8 *frag_data;
446 extern void skb_prepare_seq_read(struct sk_buff *skb,
447 unsigned int from, unsigned int to,
448 struct skb_seq_state *st);
449 extern unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
450 struct skb_seq_state *st);
451 extern void skb_abort_seq_read(struct skb_seq_state *st);
453 extern unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
454 unsigned int to, struct ts_config *config,
455 struct ts_state *state);
457 #ifdef NET_SKBUFF_DATA_USES_OFFSET
458 static inline unsigned char *skb_end_pointer(const struct sk_buff *skb)
460 return skb->head + skb->end;
462 #else
463 static inline unsigned char *skb_end_pointer(const struct sk_buff *skb)
465 return skb->end;
467 #endif
469 /* Internal */
470 #define skb_shinfo(SKB) ((struct skb_shared_info *)(skb_end_pointer(SKB)))
473 * skb_queue_empty - check if a queue is empty
474 * @list: queue head
476 * Returns true if the queue is empty, false otherwise.
478 static inline int skb_queue_empty(const struct sk_buff_head *list)
480 return list->next == (struct sk_buff *)list;
484 * skb_queue_is_last - check if skb is the last entry in the queue
485 * @list: queue head
486 * @skb: buffer
488 * Returns true if @skb is the last buffer on the list.
490 static inline bool skb_queue_is_last(const struct sk_buff_head *list,
491 const struct sk_buff *skb)
493 return (skb->next == (struct sk_buff *) list);
497 * skb_queue_is_first - check if skb is the first entry in the queue
498 * @list: queue head
499 * @skb: buffer
501 * Returns true if @skb is the first buffer on the list.
503 static inline bool skb_queue_is_first(const struct sk_buff_head *list,
504 const struct sk_buff *skb)
506 return (skb->prev == (struct sk_buff *) list);
510 * skb_queue_next - return the next packet in the queue
511 * @list: queue head
512 * @skb: current buffer
514 * Return the next packet in @list after @skb. It is only valid to
515 * call this if skb_queue_is_last() evaluates to false.
517 static inline struct sk_buff *skb_queue_next(const struct sk_buff_head *list,
518 const struct sk_buff *skb)
520 /* This BUG_ON may seem severe, but if we just return then we
521 * are going to dereference garbage.
523 BUG_ON(skb_queue_is_last(list, skb));
524 return skb->next;
528 * skb_queue_prev - return the prev packet in the queue
529 * @list: queue head
530 * @skb: current buffer
532 * Return the prev packet in @list before @skb. It is only valid to
533 * call this if skb_queue_is_first() evaluates to false.
535 static inline struct sk_buff *skb_queue_prev(const struct sk_buff_head *list,
536 const struct sk_buff *skb)
538 /* This BUG_ON may seem severe, but if we just return then we
539 * are going to dereference garbage.
541 BUG_ON(skb_queue_is_first(list, skb));
542 return skb->prev;
546 * skb_get - reference buffer
547 * @skb: buffer to reference
549 * Makes another reference to a socket buffer and returns a pointer
550 * to the buffer.
552 static inline struct sk_buff *skb_get(struct sk_buff *skb)
554 atomic_inc(&skb->users);
555 return skb;
559 * If users == 1, we are the only owner and are can avoid redundant
560 * atomic change.
564 * skb_cloned - is the buffer a clone
565 * @skb: buffer to check
567 * Returns true if the buffer was generated with skb_clone() and is
568 * one of multiple shared copies of the buffer. Cloned buffers are
569 * shared data so must not be written to under normal circumstances.
571 static inline int skb_cloned(const struct sk_buff *skb)
573 return skb->cloned &&
574 (atomic_read(&skb_shinfo(skb)->dataref) & SKB_DATAREF_MASK) != 1;
578 * skb_header_cloned - is the header a clone
579 * @skb: buffer to check
581 * Returns true if modifying the header part of the buffer requires
582 * the data to be copied.
584 static inline int skb_header_cloned(const struct sk_buff *skb)
586 int dataref;
588 if (!skb->cloned)
589 return 0;
591 dataref = atomic_read(&skb_shinfo(skb)->dataref);
592 dataref = (dataref & SKB_DATAREF_MASK) - (dataref >> SKB_DATAREF_SHIFT);
593 return dataref != 1;
597 * skb_header_release - release reference to header
598 * @skb: buffer to operate on
600 * Drop a reference to the header part of the buffer. This is done
601 * by acquiring a payload reference. You must not read from the header
602 * part of skb->data after this.
604 static inline void skb_header_release(struct sk_buff *skb)
606 BUG_ON(skb->nohdr);
607 skb->nohdr = 1;
608 atomic_add(1 << SKB_DATAREF_SHIFT, &skb_shinfo(skb)->dataref);
612 * skb_shared - is the buffer shared
613 * @skb: buffer to check
615 * Returns true if more than one person has a reference to this
616 * buffer.
618 static inline int skb_shared(const struct sk_buff *skb)
620 return atomic_read(&skb->users) != 1;
624 * skb_share_check - check if buffer is shared and if so clone it
625 * @skb: buffer to check
626 * @pri: priority for memory allocation
628 * If the buffer is shared the buffer is cloned and the old copy
629 * drops a reference. A new clone with a single reference is returned.
630 * If the buffer is not shared the original buffer is returned. When
631 * being called from interrupt status or with spinlocks held pri must
632 * be GFP_ATOMIC.
634 * NULL is returned on a memory allocation failure.
636 static inline struct sk_buff *skb_share_check(struct sk_buff *skb,
637 gfp_t pri)
639 might_sleep_if(pri & __GFP_WAIT);
640 if (skb_shared(skb)) {
641 struct sk_buff *nskb = skb_clone(skb, pri);
642 kfree_skb(skb);
643 skb = nskb;
645 return skb;
649 * Copy shared buffers into a new sk_buff. We effectively do COW on
650 * packets to handle cases where we have a local reader and forward
651 * and a couple of other messy ones. The normal one is tcpdumping
652 * a packet thats being forwarded.
656 * skb_unshare - make a copy of a shared buffer
657 * @skb: buffer to check
658 * @pri: priority for memory allocation
660 * If the socket buffer is a clone then this function creates a new
661 * copy of the data, drops a reference count on the old copy and returns
662 * the new copy with the reference count at 1. If the buffer is not a clone
663 * the original buffer is returned. When called with a spinlock held or
664 * from interrupt state @pri must be %GFP_ATOMIC
666 * %NULL is returned on a memory allocation failure.
668 static inline struct sk_buff *skb_unshare(struct sk_buff *skb,
669 gfp_t pri)
671 might_sleep_if(pri & __GFP_WAIT);
672 if (skb_cloned(skb)) {
673 struct sk_buff *nskb = skb_copy(skb, pri);
674 kfree_skb(skb); /* Free our shared copy */
675 skb = nskb;
677 return skb;
681 * skb_peek
682 * @list_: list to peek at
684 * Peek an &sk_buff. Unlike most other operations you _MUST_
685 * be careful with this one. A peek leaves the buffer on the
686 * list and someone else may run off with it. You must hold
687 * the appropriate locks or have a private queue to do this.
689 * Returns %NULL for an empty list or a pointer to the head element.
690 * The reference count is not incremented and the reference is therefore
691 * volatile. Use with caution.
693 static inline struct sk_buff *skb_peek(struct sk_buff_head *list_)
695 struct sk_buff *list = ((struct sk_buff *)list_)->next;
696 if (list == (struct sk_buff *)list_)
697 list = NULL;
698 return list;
702 * skb_peek_tail
703 * @list_: list to peek at
705 * Peek an &sk_buff. Unlike most other operations you _MUST_
706 * be careful with this one. A peek leaves the buffer on the
707 * list and someone else may run off with it. You must hold
708 * the appropriate locks or have a private queue to do this.
710 * Returns %NULL for an empty list or a pointer to the tail element.
711 * The reference count is not incremented and the reference is therefore
712 * volatile. Use with caution.
714 static inline struct sk_buff *skb_peek_tail(struct sk_buff_head *list_)
716 struct sk_buff *list = ((struct sk_buff *)list_)->prev;
717 if (list == (struct sk_buff *)list_)
718 list = NULL;
719 return list;
723 * skb_queue_len - get queue length
724 * @list_: list to measure
726 * Return the length of an &sk_buff queue.
728 static inline __u32 skb_queue_len(const struct sk_buff_head *list_)
730 return list_->qlen;
734 * __skb_queue_head_init - initialize non-spinlock portions of sk_buff_head
735 * @list: queue to initialize
737 * This initializes only the list and queue length aspects of
738 * an sk_buff_head object. This allows to initialize the list
739 * aspects of an sk_buff_head without reinitializing things like
740 * the spinlock. It can also be used for on-stack sk_buff_head
741 * objects where the spinlock is known to not be used.
743 static inline void __skb_queue_head_init(struct sk_buff_head *list)
745 list->prev = list->next = (struct sk_buff *)list;
746 list->qlen = 0;
750 * This function creates a split out lock class for each invocation;
751 * this is needed for now since a whole lot of users of the skb-queue
752 * infrastructure in drivers have different locking usage (in hardirq)
753 * than the networking core (in softirq only). In the long run either the
754 * network layer or drivers should need annotation to consolidate the
755 * main types of usage into 3 classes.
757 static inline void skb_queue_head_init(struct sk_buff_head *list)
759 spin_lock_init(&list->lock);
760 __skb_queue_head_init(list);
763 static inline void skb_queue_head_init_class(struct sk_buff_head *list,
764 struct lock_class_key *class)
766 skb_queue_head_init(list);
767 lockdep_set_class(&list->lock, class);
771 * Insert an sk_buff on a list.
773 * The "__skb_xxxx()" functions are the non-atomic ones that
774 * can only be called with interrupts disabled.
776 extern void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list);
777 static inline void __skb_insert(struct sk_buff *newsk,
778 struct sk_buff *prev, struct sk_buff *next,
779 struct sk_buff_head *list)
781 newsk->next = next;
782 newsk->prev = prev;
783 next->prev = prev->next = newsk;
784 list->qlen++;
787 static inline void __skb_queue_splice(const struct sk_buff_head *list,
788 struct sk_buff *prev,
789 struct sk_buff *next)
791 struct sk_buff *first = list->next;
792 struct sk_buff *last = list->prev;
794 first->prev = prev;
795 prev->next = first;
797 last->next = next;
798 next->prev = last;
802 * skb_queue_splice - join two skb lists, this is designed for stacks
803 * @list: the new list to add
804 * @head: the place to add it in the first list
806 static inline void skb_queue_splice(const struct sk_buff_head *list,
807 struct sk_buff_head *head)
809 if (!skb_queue_empty(list)) {
810 __skb_queue_splice(list, (struct sk_buff *) head, head->next);
811 head->qlen += list->qlen;
816 * skb_queue_splice - join two skb lists and reinitialise the emptied list
817 * @list: the new list to add
818 * @head: the place to add it in the first list
820 * The list at @list is reinitialised
822 static inline void skb_queue_splice_init(struct sk_buff_head *list,
823 struct sk_buff_head *head)
825 if (!skb_queue_empty(list)) {
826 __skb_queue_splice(list, (struct sk_buff *) head, head->next);
827 head->qlen += list->qlen;
828 __skb_queue_head_init(list);
833 * skb_queue_splice_tail - join two skb lists, each list being a queue
834 * @list: the new list to add
835 * @head: the place to add it in the first list
837 static inline void skb_queue_splice_tail(const struct sk_buff_head *list,
838 struct sk_buff_head *head)
840 if (!skb_queue_empty(list)) {
841 __skb_queue_splice(list, head->prev, (struct sk_buff *) head);
842 head->qlen += list->qlen;
847 * skb_queue_splice_tail - join two skb lists and reinitialise the emptied list
848 * @list: the new list to add
849 * @head: the place to add it in the first list
851 * Each of the lists is a queue.
852 * The list at @list is reinitialised
854 static inline void skb_queue_splice_tail_init(struct sk_buff_head *list,
855 struct sk_buff_head *head)
857 if (!skb_queue_empty(list)) {
858 __skb_queue_splice(list, head->prev, (struct sk_buff *) head);
859 head->qlen += list->qlen;
860 __skb_queue_head_init(list);
865 * __skb_queue_after - queue a buffer at the list head
866 * @list: list to use
867 * @prev: place after this buffer
868 * @newsk: buffer to queue
870 * Queue a buffer int the middle of a list. This function takes no locks
871 * and you must therefore hold required locks before calling it.
873 * A buffer cannot be placed on two lists at the same time.
875 static inline void __skb_queue_after(struct sk_buff_head *list,
876 struct sk_buff *prev,
877 struct sk_buff *newsk)
879 __skb_insert(newsk, prev, prev->next, list);
882 extern void skb_append(struct sk_buff *old, struct sk_buff *newsk,
883 struct sk_buff_head *list);
885 static inline void __skb_queue_before(struct sk_buff_head *list,
886 struct sk_buff *next,
887 struct sk_buff *newsk)
889 __skb_insert(newsk, next->prev, next, list);
893 * __skb_queue_head - queue a buffer at the list head
894 * @list: list to use
895 * @newsk: buffer to queue
897 * Queue a buffer at the start of a list. This function takes no locks
898 * and you must therefore hold required locks before calling it.
900 * A buffer cannot be placed on two lists at the same time.
902 extern void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk);
903 static inline void __skb_queue_head(struct sk_buff_head *list,
904 struct sk_buff *newsk)
906 __skb_queue_after(list, (struct sk_buff *)list, newsk);
910 * __skb_queue_tail - queue a buffer at the list tail
911 * @list: list to use
912 * @newsk: buffer to queue
914 * Queue a buffer at the end of a list. This function takes no locks
915 * and you must therefore hold required locks before calling it.
917 * A buffer cannot be placed on two lists at the same time.
919 extern void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk);
920 static inline void __skb_queue_tail(struct sk_buff_head *list,
921 struct sk_buff *newsk)
923 __skb_queue_before(list, (struct sk_buff *)list, newsk);
927 * remove sk_buff from list. _Must_ be called atomically, and with
928 * the list known..
930 extern void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list);
931 static inline void __skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
933 struct sk_buff *next, *prev;
935 list->qlen--;
936 next = skb->next;
937 prev = skb->prev;
938 skb->next = skb->prev = NULL;
939 next->prev = prev;
940 prev->next = next;
944 * __skb_dequeue - remove from the head of the queue
945 * @list: list to dequeue from
947 * Remove the head of the list. This function does not take any locks
948 * so must be used with appropriate locks held only. The head item is
949 * returned or %NULL if the list is empty.
951 extern struct sk_buff *skb_dequeue(struct sk_buff_head *list);
952 static inline struct sk_buff *__skb_dequeue(struct sk_buff_head *list)
954 struct sk_buff *skb = skb_peek(list);
955 if (skb)
956 __skb_unlink(skb, list);
957 return skb;
961 * __skb_dequeue_tail - remove from the tail of the queue
962 * @list: list to dequeue from
964 * Remove the tail of the list. This function does not take any locks
965 * so must be used with appropriate locks held only. The tail item is
966 * returned or %NULL if the list is empty.
968 extern struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list);
969 static inline struct sk_buff *__skb_dequeue_tail(struct sk_buff_head *list)
971 struct sk_buff *skb = skb_peek_tail(list);
972 if (skb)
973 __skb_unlink(skb, list);
974 return skb;
978 static inline int skb_is_nonlinear(const struct sk_buff *skb)
980 return skb->data_len;
983 static inline unsigned int skb_headlen(const struct sk_buff *skb)
985 return skb->len - skb->data_len;
988 static inline int skb_pagelen(const struct sk_buff *skb)
990 int i, len = 0;
992 for (i = (int)skb_shinfo(skb)->nr_frags - 1; i >= 0; i--)
993 len += skb_shinfo(skb)->frags[i].size;
994 return len + skb_headlen(skb);
997 static inline void skb_fill_page_desc(struct sk_buff *skb, int i,
998 struct page *page, int off, int size)
1000 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1002 frag->page = page;
1003 frag->page_offset = off;
1004 frag->size = size;
1005 skb_shinfo(skb)->nr_frags = i + 1;
1008 extern void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page,
1009 int off, int size);
1011 #define SKB_PAGE_ASSERT(skb) BUG_ON(skb_shinfo(skb)->nr_frags)
1012 #define SKB_FRAG_ASSERT(skb) BUG_ON(skb_shinfo(skb)->frag_list)
1013 #define SKB_LINEAR_ASSERT(skb) BUG_ON(skb_is_nonlinear(skb))
1015 #ifdef NET_SKBUFF_DATA_USES_OFFSET
1016 static inline unsigned char *skb_tail_pointer(const struct sk_buff *skb)
1018 return skb->head + skb->tail;
1021 static inline void skb_reset_tail_pointer(struct sk_buff *skb)
1023 skb->tail = skb->data - skb->head;
1026 static inline void skb_set_tail_pointer(struct sk_buff *skb, const int offset)
1028 skb_reset_tail_pointer(skb);
1029 skb->tail += offset;
1031 #else /* NET_SKBUFF_DATA_USES_OFFSET */
1032 static inline unsigned char *skb_tail_pointer(const struct sk_buff *skb)
1034 return skb->tail;
1037 static inline void skb_reset_tail_pointer(struct sk_buff *skb)
1039 skb->tail = skb->data;
1042 static inline void skb_set_tail_pointer(struct sk_buff *skb, const int offset)
1044 skb->tail = skb->data + offset;
1047 #endif /* NET_SKBUFF_DATA_USES_OFFSET */
1050 * Add data to an sk_buff
1052 extern unsigned char *skb_put(struct sk_buff *skb, unsigned int len);
1053 static inline unsigned char *__skb_put(struct sk_buff *skb, unsigned int len)
1055 unsigned char *tmp = skb_tail_pointer(skb);
1056 SKB_LINEAR_ASSERT(skb);
1057 skb->tail += len;
1058 skb->len += len;
1059 return tmp;
1062 extern unsigned char *skb_push(struct sk_buff *skb, unsigned int len);
1063 static inline unsigned char *__skb_push(struct sk_buff *skb, unsigned int len)
1065 skb->data -= len;
1066 skb->len += len;
1067 return skb->data;
1070 extern unsigned char *skb_pull(struct sk_buff *skb, unsigned int len);
1071 static inline unsigned char *__skb_pull(struct sk_buff *skb, unsigned int len)
1073 skb->len -= len;
1074 BUG_ON(skb->len < skb->data_len);
1075 return skb->data += len;
1078 extern unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta);
1080 static inline unsigned char *__pskb_pull(struct sk_buff *skb, unsigned int len)
1082 if (len > skb_headlen(skb) &&
1083 !__pskb_pull_tail(skb, len - skb_headlen(skb)))
1084 return NULL;
1085 skb->len -= len;
1086 return skb->data += len;
1089 static inline unsigned char *pskb_pull(struct sk_buff *skb, unsigned int len)
1091 return unlikely(len > skb->len) ? NULL : __pskb_pull(skb, len);
1094 static inline int pskb_may_pull(struct sk_buff *skb, unsigned int len)
1096 if (likely(len <= skb_headlen(skb)))
1097 return 1;
1098 if (unlikely(len > skb->len))
1099 return 0;
1100 return __pskb_pull_tail(skb, len - skb_headlen(skb)) != NULL;
1104 * skb_headroom - bytes at buffer head
1105 * @skb: buffer to check
1107 * Return the number of bytes of free space at the head of an &sk_buff.
1109 static inline unsigned int skb_headroom(const struct sk_buff *skb)
1111 return skb->data - skb->head;
1115 * skb_tailroom - bytes at buffer end
1116 * @skb: buffer to check
1118 * Return the number of bytes of free space at the tail of an sk_buff
1120 static inline int skb_tailroom(const struct sk_buff *skb)
1122 return skb_is_nonlinear(skb) ? 0 : skb->end - skb->tail;
1126 * skb_reserve - adjust headroom
1127 * @skb: buffer to alter
1128 * @len: bytes to move
1130 * Increase the headroom of an empty &sk_buff by reducing the tail
1131 * room. This is only allowed for an empty buffer.
1133 static inline void skb_reserve(struct sk_buff *skb, int len)
1135 skb->data += len;
1136 skb->tail += len;
1139 #ifdef NET_SKBUFF_DATA_USES_OFFSET
1140 static inline unsigned char *skb_transport_header(const struct sk_buff *skb)
1142 return skb->head + skb->transport_header;
1145 static inline void skb_reset_transport_header(struct sk_buff *skb)
1147 skb->transport_header = skb->data - skb->head;
1150 static inline void skb_set_transport_header(struct sk_buff *skb,
1151 const int offset)
1153 skb_reset_transport_header(skb);
1154 skb->transport_header += offset;
1157 static inline unsigned char *skb_network_header(const struct sk_buff *skb)
1159 return skb->head + skb->network_header;
1162 static inline void skb_reset_network_header(struct sk_buff *skb)
1164 skb->network_header = skb->data - skb->head;
1167 static inline void skb_set_network_header(struct sk_buff *skb, const int offset)
1169 skb_reset_network_header(skb);
1170 skb->network_header += offset;
1173 static inline unsigned char *skb_mac_header(const struct sk_buff *skb)
1175 return skb->head + skb->mac_header;
1178 static inline int skb_mac_header_was_set(const struct sk_buff *skb)
1180 return skb->mac_header != ~0U;
1183 static inline void skb_reset_mac_header(struct sk_buff *skb)
1185 skb->mac_header = skb->data - skb->head;
1188 static inline void skb_set_mac_header(struct sk_buff *skb, const int offset)
1190 skb_reset_mac_header(skb);
1191 skb->mac_header += offset;
1194 #else /* NET_SKBUFF_DATA_USES_OFFSET */
1196 static inline unsigned char *skb_transport_header(const struct sk_buff *skb)
1198 return skb->transport_header;
1201 static inline void skb_reset_transport_header(struct sk_buff *skb)
1203 skb->transport_header = skb->data;
1206 static inline void skb_set_transport_header(struct sk_buff *skb,
1207 const int offset)
1209 skb->transport_header = skb->data + offset;
1212 static inline unsigned char *skb_network_header(const struct sk_buff *skb)
1214 return skb->network_header;
1217 static inline void skb_reset_network_header(struct sk_buff *skb)
1219 skb->network_header = skb->data;
1222 static inline void skb_set_network_header(struct sk_buff *skb, const int offset)
1224 skb->network_header = skb->data + offset;
1227 static inline unsigned char *skb_mac_header(const struct sk_buff *skb)
1229 return skb->mac_header;
1232 static inline int skb_mac_header_was_set(const struct sk_buff *skb)
1234 return skb->mac_header != NULL;
1237 static inline void skb_reset_mac_header(struct sk_buff *skb)
1239 skb->mac_header = skb->data;
1242 static inline void skb_set_mac_header(struct sk_buff *skb, const int offset)
1244 skb->mac_header = skb->data + offset;
1246 #endif /* NET_SKBUFF_DATA_USES_OFFSET */
1248 static inline int skb_transport_offset(const struct sk_buff *skb)
1250 return skb_transport_header(skb) - skb->data;
1253 static inline u32 skb_network_header_len(const struct sk_buff *skb)
1255 return skb->transport_header - skb->network_header;
1258 static inline int skb_network_offset(const struct sk_buff *skb)
1260 return skb_network_header(skb) - skb->data;
1264 * CPUs often take a performance hit when accessing unaligned memory
1265 * locations. The actual performance hit varies, it can be small if the
1266 * hardware handles it or large if we have to take an exception and fix it
1267 * in software.
1269 * Since an ethernet header is 14 bytes network drivers often end up with
1270 * the IP header at an unaligned offset. The IP header can be aligned by
1271 * shifting the start of the packet by 2 bytes. Drivers should do this
1272 * with:
1274 * skb_reserve(NET_IP_ALIGN);
1276 * The downside to this alignment of the IP header is that the DMA is now
1277 * unaligned. On some architectures the cost of an unaligned DMA is high
1278 * and this cost outweighs the gains made by aligning the IP header.
1280 * Since this trade off varies between architectures, we allow NET_IP_ALIGN
1281 * to be overridden.
1283 #ifndef NET_IP_ALIGN
1284 #define NET_IP_ALIGN 2
1285 #endif
1288 * The networking layer reserves some headroom in skb data (via
1289 * dev_alloc_skb). This is used to avoid having to reallocate skb data when
1290 * the header has to grow. In the default case, if the header has to grow
1291 * 16 bytes or less we avoid the reallocation.
1293 * Unfortunately this headroom changes the DMA alignment of the resulting
1294 * network packet. As for NET_IP_ALIGN, this unaligned DMA is expensive
1295 * on some architectures. An architecture can override this value,
1296 * perhaps setting it to a cacheline in size (since that will maintain
1297 * cacheline alignment of the DMA). It must be a power of 2.
1299 * Various parts of the networking layer expect at least 16 bytes of
1300 * headroom, you should not reduce this.
1302 #ifndef NET_SKB_PAD
1303 #define NET_SKB_PAD 16
1304 #endif
1306 extern int ___pskb_trim(struct sk_buff *skb, unsigned int len);
1308 static inline void __skb_trim(struct sk_buff *skb, unsigned int len)
1310 if (unlikely(skb->data_len)) {
1311 WARN_ON(1);
1312 return;
1314 skb->len = len;
1315 skb_set_tail_pointer(skb, len);
1318 extern void skb_trim(struct sk_buff *skb, unsigned int len);
1320 static inline int __pskb_trim(struct sk_buff *skb, unsigned int len)
1322 if (skb->data_len)
1323 return ___pskb_trim(skb, len);
1324 __skb_trim(skb, len);
1325 return 0;
1328 static inline int pskb_trim(struct sk_buff *skb, unsigned int len)
1330 return (len < skb->len) ? __pskb_trim(skb, len) : 0;
1334 * pskb_trim_unique - remove end from a paged unique (not cloned) buffer
1335 * @skb: buffer to alter
1336 * @len: new length
1338 * This is identical to pskb_trim except that the caller knows that
1339 * the skb is not cloned so we should never get an error due to out-
1340 * of-memory.
1342 static inline void pskb_trim_unique(struct sk_buff *skb, unsigned int len)
1344 int err = pskb_trim(skb, len);
1345 BUG_ON(err);
1349 * skb_orphan - orphan a buffer
1350 * @skb: buffer to orphan
1352 * If a buffer currently has an owner then we call the owner's
1353 * destructor function and make the @skb unowned. The buffer continues
1354 * to exist but is no longer charged to its former owner.
1356 static inline void skb_orphan(struct sk_buff *skb)
1358 if (skb->destructor)
1359 skb->destructor(skb);
1360 skb->destructor = NULL;
1361 skb->sk = NULL;
1365 * __skb_queue_purge - empty a list
1366 * @list: list to empty
1368 * Delete all buffers on an &sk_buff list. Each buffer is removed from
1369 * the list and one reference dropped. This function does not take the
1370 * list lock and the caller must hold the relevant locks to use it.
1372 extern void skb_queue_purge(struct sk_buff_head *list);
1373 static inline void __skb_queue_purge(struct sk_buff_head *list)
1375 struct sk_buff *skb;
1376 while ((skb = __skb_dequeue(list)) != NULL)
1377 kfree_skb(skb);
1381 * __dev_alloc_skb - allocate an skbuff for receiving
1382 * @length: length to allocate
1383 * @gfp_mask: get_free_pages mask, passed to alloc_skb
1385 * Allocate a new &sk_buff and assign it a usage count of one. The
1386 * buffer has unspecified headroom built in. Users should allocate
1387 * the headroom they think they need without accounting for the
1388 * built in space. The built in space is used for optimisations.
1390 * %NULL is returned if there is no free memory.
1392 static inline struct sk_buff *__dev_alloc_skb(unsigned int length,
1393 gfp_t gfp_mask)
1395 struct sk_buff *skb = alloc_skb(length + NET_SKB_PAD, gfp_mask);
1396 if (likely(skb))
1397 skb_reserve(skb, NET_SKB_PAD);
1398 return skb;
1401 extern struct sk_buff *dev_alloc_skb(unsigned int length);
1403 extern struct sk_buff *__netdev_alloc_skb(struct net_device *dev,
1404 unsigned int length, gfp_t gfp_mask);
1407 * netdev_alloc_skb - allocate an skbuff for rx on a specific device
1408 * @dev: network device to receive on
1409 * @length: length to allocate
1411 * Allocate a new &sk_buff and assign it a usage count of one. The
1412 * buffer has unspecified headroom built in. Users should allocate
1413 * the headroom they think they need without accounting for the
1414 * built in space. The built in space is used for optimisations.
1416 * %NULL is returned if there is no free memory. Although this function
1417 * allocates memory it can be called from an interrupt.
1419 static inline struct sk_buff *netdev_alloc_skb(struct net_device *dev,
1420 unsigned int length)
1422 return __netdev_alloc_skb(dev, length, GFP_ATOMIC);
1425 extern struct page *__netdev_alloc_page(struct net_device *dev, gfp_t gfp_mask);
1428 * netdev_alloc_page - allocate a page for ps-rx on a specific device
1429 * @dev: network device to receive on
1431 * Allocate a new page node local to the specified device.
1433 * %NULL is returned if there is no free memory.
1435 static inline struct page *netdev_alloc_page(struct net_device *dev)
1437 return __netdev_alloc_page(dev, GFP_ATOMIC);
1440 static inline void netdev_free_page(struct net_device *dev, struct page *page)
1442 __free_page(page);
1446 * skb_clone_writable - is the header of a clone writable
1447 * @skb: buffer to check
1448 * @len: length up to which to write
1450 * Returns true if modifying the header part of the cloned buffer
1451 * does not requires the data to be copied.
1453 static inline int skb_clone_writable(struct sk_buff *skb, unsigned int len)
1455 return !skb_header_cloned(skb) &&
1456 skb_headroom(skb) + len <= skb->hdr_len;
1459 static inline int __skb_cow(struct sk_buff *skb, unsigned int headroom,
1460 int cloned)
1462 int delta = 0;
1464 if (headroom < NET_SKB_PAD)
1465 headroom = NET_SKB_PAD;
1466 if (headroom > skb_headroom(skb))
1467 delta = headroom - skb_headroom(skb);
1469 if (delta || cloned)
1470 return pskb_expand_head(skb, ALIGN(delta, NET_SKB_PAD), 0,
1471 GFP_ATOMIC);
1472 return 0;
1476 * skb_cow - copy header of skb when it is required
1477 * @skb: buffer to cow
1478 * @headroom: needed headroom
1480 * If the skb passed lacks sufficient headroom or its data part
1481 * is shared, data is reallocated. If reallocation fails, an error
1482 * is returned and original skb is not changed.
1484 * The result is skb with writable area skb->head...skb->tail
1485 * and at least @headroom of space at head.
1487 static inline int skb_cow(struct sk_buff *skb, unsigned int headroom)
1489 return __skb_cow(skb, headroom, skb_cloned(skb));
1493 * skb_cow_head - skb_cow but only making the head writable
1494 * @skb: buffer to cow
1495 * @headroom: needed headroom
1497 * This function is identical to skb_cow except that we replace the
1498 * skb_cloned check by skb_header_cloned. It should be used when
1499 * you only need to push on some header and do not need to modify
1500 * the data.
1502 static inline int skb_cow_head(struct sk_buff *skb, unsigned int headroom)
1504 return __skb_cow(skb, headroom, skb_header_cloned(skb));
1508 * skb_padto - pad an skbuff up to a minimal size
1509 * @skb: buffer to pad
1510 * @len: minimal length
1512 * Pads up a buffer to ensure the trailing bytes exist and are
1513 * blanked. If the buffer already contains sufficient data it
1514 * is untouched. Otherwise it is extended. Returns zero on
1515 * success. The skb is freed on error.
1518 static inline int skb_padto(struct sk_buff *skb, unsigned int len)
1520 unsigned int size = skb->len;
1521 if (likely(size >= len))
1522 return 0;
1523 return skb_pad(skb, len - size);
1526 static inline int skb_add_data(struct sk_buff *skb,
1527 char __user *from, int copy)
1529 const int off = skb->len;
1531 if (skb->ip_summed == CHECKSUM_NONE) {
1532 int err = 0;
1533 __wsum csum = csum_and_copy_from_user(from, skb_put(skb, copy),
1534 copy, 0, &err);
1535 if (!err) {
1536 skb->csum = csum_block_add(skb->csum, csum, off);
1537 return 0;
1539 } else if (!copy_from_user(skb_put(skb, copy), from, copy))
1540 return 0;
1542 __skb_trim(skb, off);
1543 return -EFAULT;
1546 static inline int skb_can_coalesce(struct sk_buff *skb, int i,
1547 struct page *page, int off)
1549 if (i) {
1550 struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[i - 1];
1552 return page == frag->page &&
1553 off == frag->page_offset + frag->size;
1555 return 0;
1558 static inline int __skb_linearize(struct sk_buff *skb)
1560 return __pskb_pull_tail(skb, skb->data_len) ? 0 : -ENOMEM;
1564 * skb_linearize - convert paged skb to linear one
1565 * @skb: buffer to linarize
1567 * If there is no free memory -ENOMEM is returned, otherwise zero
1568 * is returned and the old skb data released.
1570 static inline int skb_linearize(struct sk_buff *skb)
1572 return skb_is_nonlinear(skb) ? __skb_linearize(skb) : 0;
1576 * skb_linearize_cow - make sure skb is linear and writable
1577 * @skb: buffer to process
1579 * If there is no free memory -ENOMEM is returned, otherwise zero
1580 * is returned and the old skb data released.
1582 static inline int skb_linearize_cow(struct sk_buff *skb)
1584 return skb_is_nonlinear(skb) || skb_cloned(skb) ?
1585 __skb_linearize(skb) : 0;
1589 * skb_postpull_rcsum - update checksum for received skb after pull
1590 * @skb: buffer to update
1591 * @start: start of data before pull
1592 * @len: length of data pulled
1594 * After doing a pull on a received packet, you need to call this to
1595 * update the CHECKSUM_COMPLETE checksum, or set ip_summed to
1596 * CHECKSUM_NONE so that it can be recomputed from scratch.
1599 static inline void skb_postpull_rcsum(struct sk_buff *skb,
1600 const void *start, unsigned int len)
1602 if (skb->ip_summed == CHECKSUM_COMPLETE)
1603 skb->csum = csum_sub(skb->csum, csum_partial(start, len, 0));
1606 unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len);
1609 * pskb_trim_rcsum - trim received skb and update checksum
1610 * @skb: buffer to trim
1611 * @len: new length
1613 * This is exactly the same as pskb_trim except that it ensures the
1614 * checksum of received packets are still valid after the operation.
1617 static inline int pskb_trim_rcsum(struct sk_buff *skb, unsigned int len)
1619 if (likely(len >= skb->len))
1620 return 0;
1621 if (skb->ip_summed == CHECKSUM_COMPLETE)
1622 skb->ip_summed = CHECKSUM_NONE;
1623 return __pskb_trim(skb, len);
1626 #define skb_queue_walk(queue, skb) \
1627 for (skb = (queue)->next; \
1628 prefetch(skb->next), (skb != (struct sk_buff *)(queue)); \
1629 skb = skb->next)
1631 #define skb_queue_walk_safe(queue, skb, tmp) \
1632 for (skb = (queue)->next, tmp = skb->next; \
1633 skb != (struct sk_buff *)(queue); \
1634 skb = tmp, tmp = skb->next)
1636 #define skb_queue_walk_from(queue, skb) \
1637 for (; prefetch(skb->next), (skb != (struct sk_buff *)(queue)); \
1638 skb = skb->next)
1640 #define skb_queue_walk_from_safe(queue, skb, tmp) \
1641 for (tmp = skb->next; \
1642 skb != (struct sk_buff *)(queue); \
1643 skb = tmp, tmp = skb->next)
1645 #define skb_queue_reverse_walk(queue, skb) \
1646 for (skb = (queue)->prev; \
1647 prefetch(skb->prev), (skb != (struct sk_buff *)(queue)); \
1648 skb = skb->prev)
1651 extern struct sk_buff *__skb_recv_datagram(struct sock *sk, unsigned flags,
1652 int *peeked, int *err);
1653 extern struct sk_buff *skb_recv_datagram(struct sock *sk, unsigned flags,
1654 int noblock, int *err);
1655 extern unsigned int datagram_poll(struct file *file, struct socket *sock,
1656 struct poll_table_struct *wait);
1657 extern int skb_copy_datagram_iovec(const struct sk_buff *from,
1658 int offset, struct iovec *to,
1659 int size);
1660 extern int skb_copy_and_csum_datagram_iovec(struct sk_buff *skb,
1661 int hlen,
1662 struct iovec *iov);
1663 extern int skb_copy_datagram_from_iovec(struct sk_buff *skb,
1664 int offset,
1665 struct iovec *from,
1666 int len);
1667 extern void skb_free_datagram(struct sock *sk, struct sk_buff *skb);
1668 extern int skb_kill_datagram(struct sock *sk, struct sk_buff *skb,
1669 unsigned int flags);
1670 extern __wsum skb_checksum(const struct sk_buff *skb, int offset,
1671 int len, __wsum csum);
1672 extern int skb_copy_bits(const struct sk_buff *skb, int offset,
1673 void *to, int len);
1674 extern int skb_store_bits(struct sk_buff *skb, int offset,
1675 const void *from, int len);
1676 extern __wsum skb_copy_and_csum_bits(const struct sk_buff *skb,
1677 int offset, u8 *to, int len,
1678 __wsum csum);
1679 extern int skb_splice_bits(struct sk_buff *skb,
1680 unsigned int offset,
1681 struct pipe_inode_info *pipe,
1682 unsigned int len,
1683 unsigned int flags);
1684 extern void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to);
1685 extern void skb_split(struct sk_buff *skb,
1686 struct sk_buff *skb1, const u32 len);
1687 extern int skb_shift(struct sk_buff *tgt, struct sk_buff *skb,
1688 int shiftlen);
1690 extern struct sk_buff *skb_segment(struct sk_buff *skb, int features);
1691 extern int skb_gro_receive(struct sk_buff **head,
1692 struct sk_buff *skb);
1694 static inline void *skb_header_pointer(const struct sk_buff *skb, int offset,
1695 int len, void *buffer)
1697 int hlen = skb_headlen(skb);
1699 if (hlen - offset >= len)
1700 return skb->data + offset;
1702 if (skb_copy_bits(skb, offset, buffer, len) < 0)
1703 return NULL;
1705 return buffer;
1708 static inline void skb_copy_from_linear_data(const struct sk_buff *skb,
1709 void *to,
1710 const unsigned int len)
1712 memcpy(to, skb->data, len);
1715 static inline void skb_copy_from_linear_data_offset(const struct sk_buff *skb,
1716 const int offset, void *to,
1717 const unsigned int len)
1719 memcpy(to, skb->data + offset, len);
1722 static inline void skb_copy_to_linear_data(struct sk_buff *skb,
1723 const void *from,
1724 const unsigned int len)
1726 memcpy(skb->data, from, len);
1729 static inline void skb_copy_to_linear_data_offset(struct sk_buff *skb,
1730 const int offset,
1731 const void *from,
1732 const unsigned int len)
1734 memcpy(skb->data + offset, from, len);
1737 extern void skb_init(void);
1740 * skb_get_timestamp - get timestamp from a skb
1741 * @skb: skb to get stamp from
1742 * @stamp: pointer to struct timeval to store stamp in
1744 * Timestamps are stored in the skb as offsets to a base timestamp.
1745 * This function converts the offset back to a struct timeval and stores
1746 * it in stamp.
1748 static inline void skb_get_timestamp(const struct sk_buff *skb, struct timeval *stamp)
1750 *stamp = ktime_to_timeval(skb->tstamp);
1753 static inline void __net_timestamp(struct sk_buff *skb)
1755 skb->tstamp = ktime_get_real();
1758 static inline ktime_t net_timedelta(ktime_t t)
1760 return ktime_sub(ktime_get_real(), t);
1763 static inline ktime_t net_invalid_timestamp(void)
1765 return ktime_set(0, 0);
1768 extern __sum16 __skb_checksum_complete_head(struct sk_buff *skb, int len);
1769 extern __sum16 __skb_checksum_complete(struct sk_buff *skb);
1771 static inline int skb_csum_unnecessary(const struct sk_buff *skb)
1773 return skb->ip_summed & CHECKSUM_UNNECESSARY;
1777 * skb_checksum_complete - Calculate checksum of an entire packet
1778 * @skb: packet to process
1780 * This function calculates the checksum over the entire packet plus
1781 * the value of skb->csum. The latter can be used to supply the
1782 * checksum of a pseudo header as used by TCP/UDP. It returns the
1783 * checksum.
1785 * For protocols that contain complete checksums such as ICMP/TCP/UDP,
1786 * this function can be used to verify that checksum on received
1787 * packets. In that case the function should return zero if the
1788 * checksum is correct. In particular, this function will return zero
1789 * if skb->ip_summed is CHECKSUM_UNNECESSARY which indicates that the
1790 * hardware has already verified the correctness of the checksum.
1792 static inline __sum16 skb_checksum_complete(struct sk_buff *skb)
1794 return skb_csum_unnecessary(skb) ?
1795 0 : __skb_checksum_complete(skb);
1798 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
1799 extern void nf_conntrack_destroy(struct nf_conntrack *nfct);
1800 static inline void nf_conntrack_put(struct nf_conntrack *nfct)
1802 if (nfct && atomic_dec_and_test(&nfct->use))
1803 nf_conntrack_destroy(nfct);
1805 static inline void nf_conntrack_get(struct nf_conntrack *nfct)
1807 if (nfct)
1808 atomic_inc(&nfct->use);
1810 static inline void nf_conntrack_get_reasm(struct sk_buff *skb)
1812 if (skb)
1813 atomic_inc(&skb->users);
1815 static inline void nf_conntrack_put_reasm(struct sk_buff *skb)
1817 if (skb)
1818 kfree_skb(skb);
1820 #endif
1821 #ifdef CONFIG_BRIDGE_NETFILTER
1822 static inline void nf_bridge_put(struct nf_bridge_info *nf_bridge)
1824 if (nf_bridge && atomic_dec_and_test(&nf_bridge->use))
1825 kfree(nf_bridge);
1827 static inline void nf_bridge_get(struct nf_bridge_info *nf_bridge)
1829 if (nf_bridge)
1830 atomic_inc(&nf_bridge->use);
1832 #endif /* CONFIG_BRIDGE_NETFILTER */
1833 static inline void nf_reset(struct sk_buff *skb)
1835 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
1836 nf_conntrack_put(skb->nfct);
1837 skb->nfct = NULL;
1838 nf_conntrack_put_reasm(skb->nfct_reasm);
1839 skb->nfct_reasm = NULL;
1840 #endif
1841 #ifdef CONFIG_BRIDGE_NETFILTER
1842 nf_bridge_put(skb->nf_bridge);
1843 skb->nf_bridge = NULL;
1844 #endif
1847 /* Note: This doesn't put any conntrack and bridge info in dst. */
1848 static inline void __nf_copy(struct sk_buff *dst, const struct sk_buff *src)
1850 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
1851 dst->nfct = src->nfct;
1852 nf_conntrack_get(src->nfct);
1853 dst->nfctinfo = src->nfctinfo;
1854 dst->nfct_reasm = src->nfct_reasm;
1855 nf_conntrack_get_reasm(src->nfct_reasm);
1856 #endif
1857 #ifdef CONFIG_BRIDGE_NETFILTER
1858 dst->nf_bridge = src->nf_bridge;
1859 nf_bridge_get(src->nf_bridge);
1860 #endif
1863 static inline void nf_copy(struct sk_buff *dst, const struct sk_buff *src)
1865 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
1866 nf_conntrack_put(dst->nfct);
1867 nf_conntrack_put_reasm(dst->nfct_reasm);
1868 #endif
1869 #ifdef CONFIG_BRIDGE_NETFILTER
1870 nf_bridge_put(dst->nf_bridge);
1871 #endif
1872 __nf_copy(dst, src);
1875 #ifdef CONFIG_NETWORK_SECMARK
1876 static inline void skb_copy_secmark(struct sk_buff *to, const struct sk_buff *from)
1878 to->secmark = from->secmark;
1881 static inline void skb_init_secmark(struct sk_buff *skb)
1883 skb->secmark = 0;
1885 #else
1886 static inline void skb_copy_secmark(struct sk_buff *to, const struct sk_buff *from)
1889 static inline void skb_init_secmark(struct sk_buff *skb)
1891 #endif
1893 static inline void skb_set_queue_mapping(struct sk_buff *skb, u16 queue_mapping)
1895 skb->queue_mapping = queue_mapping;
1898 static inline u16 skb_get_queue_mapping(struct sk_buff *skb)
1900 return skb->queue_mapping;
1903 static inline void skb_copy_queue_mapping(struct sk_buff *to, const struct sk_buff *from)
1905 to->queue_mapping = from->queue_mapping;
1908 #ifdef CONFIG_XFRM
1909 static inline struct sec_path *skb_sec_path(struct sk_buff *skb)
1911 return skb->sp;
1913 #else
1914 static inline struct sec_path *skb_sec_path(struct sk_buff *skb)
1916 return NULL;
1918 #endif
1920 static inline int skb_is_gso(const struct sk_buff *skb)
1922 return skb_shinfo(skb)->gso_size;
1925 static inline int skb_is_gso_v6(const struct sk_buff *skb)
1927 return skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6;
1930 extern void __skb_warn_lro_forwarding(const struct sk_buff *skb);
1932 static inline bool skb_warn_if_lro(const struct sk_buff *skb)
1934 /* LRO sets gso_size but not gso_type, whereas if GSO is really
1935 * wanted then gso_type will be set. */
1936 struct skb_shared_info *shinfo = skb_shinfo(skb);
1937 if (shinfo->gso_size != 0 && unlikely(shinfo->gso_type == 0)) {
1938 __skb_warn_lro_forwarding(skb);
1939 return true;
1941 return false;
1944 static inline void skb_forward_csum(struct sk_buff *skb)
1946 /* Unfortunately we don't support this one. Any brave souls? */
1947 if (skb->ip_summed == CHECKSUM_COMPLETE)
1948 skb->ip_summed = CHECKSUM_NONE;
1951 bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off);
1952 #endif /* __KERNEL__ */
1953 #endif /* _LINUX_SKBUFF_H */