1
2
3
4
5
6
7
8
9
10
11
12
13
14#ifndef _LINUX_SKBUFF_H
15#define _LINUX_SKBUFF_H
16
17#include <linux/config.h>
18#include <linux/kernel.h>
19#include <linux/sched.h>
20#include <linux/time.h>
21#include <linux/cache.h>
22
23#include <asm/atomic.h>
24#include <asm/types.h>
25#include <linux/spinlock.h>
26#include <linux/mm.h>
27#include <linux/highmem.h>
28
29#define HAVE_ALLOC_SKB
30#define HAVE_ALIGNABLE_SKB
31#define SLAB_SKB
32
33#define CHECKSUM_NONE 0
34#define CHECKSUM_HW 1
35#define CHECKSUM_UNNECESSARY 2
36
37#define SKB_DATA_ALIGN(X) (((X) + (SMP_CACHE_BYTES-1)) & ~(SMP_CACHE_BYTES-1))
38#define SKB_MAX_ORDER(X,ORDER) (((PAGE_SIZE<<(ORDER)) - (X) - sizeof(struct skb_shared_info))&~(SMP_CACHE_BYTES-1))
39#define SKB_MAX_HEAD(X) (SKB_MAX_ORDER((X),0))
40#define SKB_MAX_ALLOC (SKB_MAX_ORDER(0,2))
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80#ifdef __i386__
81#define NET_CALLER(arg) (*(((void**)&arg)-1))
82#else
83#define NET_CALLER(arg) __builtin_return_address(0)
84#endif
85
86#ifdef CONFIG_NETFILTER
87struct nf_conntrack {
88 atomic_t use;
89 void (*destroy)(struct nf_conntrack *);
90};
91
92struct nf_ct_info {
93 struct nf_conntrack *master;
94};
95#endif
96
97struct sk_buff_head {
98
99 struct sk_buff * next;
100 struct sk_buff * prev;
101
102 __u32 qlen;
103 spinlock_t lock;
104};
105
106struct sk_buff;
107
108#define MAX_SKB_FRAGS 6
109
110typedef struct skb_frag_struct skb_frag_t;
111
112struct skb_frag_struct
113{
114 struct page *page;
115 __u16 page_offset;
116 __u16 size;
117};
118
119
120
121
122struct skb_shared_info {
123 atomic_t dataref;
124 unsigned int nr_frags;
125 struct sk_buff *frag_list;
126 skb_frag_t frags[MAX_SKB_FRAGS];
127};
128
129struct sk_buff {
130
131 struct sk_buff * next;
132 struct sk_buff * prev;
133
134 struct sk_buff_head * list;
135 struct sock *sk;
136 struct timeval stamp;
137 struct net_device *dev;
138 struct net_device *real_dev;
139
140
141
142
143
144 union
145 {
146 struct tcphdr *th;
147 struct udphdr *uh;
148 struct icmphdr *icmph;
149 struct igmphdr *igmph;
150 struct iphdr *ipiph;
151 struct spxhdr *spxh;
152 unsigned char *raw;
153 } h;
154
155
156 union
157 {
158 struct iphdr *iph;
159 struct ipv6hdr *ipv6h;
160 struct arphdr *arph;
161 struct ipxhdr *ipxh;
162 unsigned char *raw;
163 } nh;
164
165
166 union
167 {
168 struct ethhdr *ethernet;
169 unsigned char *raw;
170 } mac;
171
172 struct dst_entry *dst;
173
174
175
176
177
178
179
180 char cb[48];
181
182 unsigned int len;
183 unsigned int data_len;
184 unsigned int csum;
185 unsigned char __unused,
186 cloned,
187 pkt_type,
188 ip_summed;
189 __u32 priority;
190 atomic_t users;
191 unsigned short protocol;
192 unsigned short security;
193 unsigned int truesize;
194
195 unsigned char *head;
196 unsigned char *data;
197 unsigned char *tail;
198 unsigned char *end;
199
200 void (*destructor)(struct sk_buff *);
201#ifdef CONFIG_NETFILTER
202
203 unsigned long nfmark;
204
205 __u32 nfcache;
206
207 struct nf_ct_info *nfct;
208#ifdef CONFIG_NETFILTER_DEBUG
209 unsigned int nf_debug;
210#endif
211#endif
212
213#if defined(CONFIG_HIPPI)
214 union{
215 __u32 ifield;
216 } private;
217#endif
218
219#ifdef CONFIG_NET_SCHED
220 __u32 tc_index;
221#endif
222};
223
224#ifdef __KERNEL__
225
226
227
228#include <linux/slab.h>
229
230#include <asm/system.h>
231
232extern void __kfree_skb(struct sk_buff *skb);
233extern struct sk_buff * alloc_skb(unsigned int size, int priority);
234extern void kfree_skbmem(struct sk_buff *skb);
235extern struct sk_buff * skb_clone(struct sk_buff *skb, int priority);
236extern struct sk_buff * skb_copy(const struct sk_buff *skb, int priority);
237extern struct sk_buff * pskb_copy(struct sk_buff *skb, int gfp_mask);
238extern int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail, int gfp_mask);
239extern struct sk_buff * skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom);
240extern struct sk_buff * skb_copy_expand(const struct sk_buff *skb,
241 int newheadroom,
242 int newtailroom,
243 int priority);
244extern struct sk_buff * skb_pad(struct sk_buff *skb, int pad);
245#define dev_kfree_skb(a) kfree_skb(a)
246extern void skb_over_panic(struct sk_buff *skb, int len, void *here);
247extern void skb_under_panic(struct sk_buff *skb, int len, void *here);
248
249
250#define skb_shinfo(SKB) ((struct skb_shared_info *)((SKB)->end))
251
252
253
254
255
256
257
258
259static inline int skb_queue_empty(struct sk_buff_head *list)
260{
261 return (list->next == (struct sk_buff *) list);
262}
263
264
265
266
267
268
269
270
271
272static inline struct sk_buff *skb_get(struct sk_buff *skb)
273{
274 atomic_inc(&skb->users);
275 return skb;
276}
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291static inline void kfree_skb(struct sk_buff *skb)
292{
293 if (atomic_read(&skb->users) == 1 || atomic_dec_and_test(&skb->users))
294 __kfree_skb(skb);
295}
296
297
298static inline void kfree_skb_fast(struct sk_buff *skb)
299{
300 if (atomic_read(&skb->users) == 1 || atomic_dec_and_test(&skb->users))
301 kfree_skbmem(skb);
302}
303
304
305
306
307
308
309
310
311
312
313static inline int skb_cloned(struct sk_buff *skb)
314{
315 return skb->cloned && atomic_read(&skb_shinfo(skb)->dataref) != 1;
316}
317
318
319
320
321
322
323
324
325
326static inline int skb_shared(struct sk_buff *skb)
327{
328 return (atomic_read(&skb->users) != 1);
329}
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345static inline struct sk_buff *skb_share_check(struct sk_buff *skb, int pri)
346{
347 if (skb_shared(skb)) {
348 struct sk_buff *nskb;
349 nskb = skb_clone(skb, pri);
350 kfree_skb(skb);
351 return nskb;
352 }
353 return skb;
354}
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378static inline struct sk_buff *skb_unshare(struct sk_buff *skb, int pri)
379{
380 struct sk_buff *nskb;
381 if(!skb_cloned(skb))
382 return skb;
383 nskb=skb_copy(skb, pri);
384 kfree_skb(skb);
385 return nskb;
386}
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402static inline struct sk_buff *skb_peek(struct sk_buff_head *list_)
403{
404 struct sk_buff *list = ((struct sk_buff *)list_)->next;
405 if (list == (struct sk_buff *)list_)
406 list = NULL;
407 return list;
408}
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424static inline struct sk_buff *skb_peek_tail(struct sk_buff_head *list_)
425{
426 struct sk_buff *list = ((struct sk_buff *)list_)->prev;
427 if (list == (struct sk_buff *)list_)
428 list = NULL;
429 return list;
430}
431
432
433
434
435
436
437
438
439static inline __u32 skb_queue_len(struct sk_buff_head *list_)
440{
441 return(list_->qlen);
442}
443
444static inline void skb_queue_head_init(struct sk_buff_head *list)
445{
446 spin_lock_init(&list->lock);
447 list->prev = (struct sk_buff *)list;
448 list->next = (struct sk_buff *)list;
449 list->qlen = 0;
450}
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470static inline void __skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
471{
472 struct sk_buff *prev, *next;
473
474 newsk->list = list;
475 list->qlen++;
476 prev = (struct sk_buff *)list;
477 next = prev->next;
478 newsk->next = next;
479 newsk->prev = prev;
480 next->prev = newsk;
481 prev->next = newsk;
482}
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497static inline void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
498{
499 unsigned long flags;
500
501 spin_lock_irqsave(&list->lock, flags);
502 __skb_queue_head(list, newsk);
503 spin_unlock_irqrestore(&list->lock, flags);
504}
505
506
507
508
509
510
511
512
513
514
515
516
517
518static inline void __skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
519{
520 struct sk_buff *prev, *next;
521
522 newsk->list = list;
523 list->qlen++;
524 next = (struct sk_buff *)list;
525 prev = next->prev;
526 newsk->next = next;
527 newsk->prev = prev;
528 next->prev = newsk;
529 prev->next = newsk;
530}
531
532
533
534
535
536
537
538
539
540
541
542
543
544static inline void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
545{
546 unsigned long flags;
547
548 spin_lock_irqsave(&list->lock, flags);
549 __skb_queue_tail(list, newsk);
550 spin_unlock_irqrestore(&list->lock, flags);
551}
552
553
554
555
556
557
558
559
560
561
562static inline struct sk_buff *__skb_dequeue(struct sk_buff_head *list)
563{
564 struct sk_buff *next, *prev, *result;
565
566 prev = (struct sk_buff *) list;
567 next = prev->next;
568 result = NULL;
569 if (next != prev) {
570 result = next;
571 next = next->next;
572 list->qlen--;
573 next->prev = prev;
574 prev->next = next;
575 result->next = NULL;
576 result->prev = NULL;
577 result->list = NULL;
578 }
579 return result;
580}
581
582
583
584
585
586
587
588
589
590
591static inline struct sk_buff *skb_dequeue(struct sk_buff_head *list)
592{
593 unsigned long flags;
594 struct sk_buff *result;
595
596 spin_lock_irqsave(&list->lock, flags);
597 result = __skb_dequeue(list);
598 spin_unlock_irqrestore(&list->lock, flags);
599 return result;
600}
601
602
603
604
605
606static inline void __skb_insert(struct sk_buff *newsk,
607 struct sk_buff * prev, struct sk_buff *next,
608 struct sk_buff_head * list)
609{
610 newsk->next = next;
611 newsk->prev = prev;
612 next->prev = newsk;
613 prev->next = newsk;
614 newsk->list = list;
615 list->qlen++;
616}
617
618
619
620
621
622
623
624
625
626
627
628static inline void skb_insert(struct sk_buff *old, struct sk_buff *newsk)
629{
630 unsigned long flags;
631
632 spin_lock_irqsave(&old->list->lock, flags);
633 __skb_insert(newsk, old->prev, old, old->list);
634 spin_unlock_irqrestore(&old->list->lock, flags);
635}
636
637
638
639
640
641static inline void __skb_append(struct sk_buff *old, struct sk_buff *newsk)
642{
643 __skb_insert(newsk, old, old->next, old->list);
644}
645
646
647
648
649
650
651
652
653
654
655
656
657static inline void skb_append(struct sk_buff *old, struct sk_buff *newsk)
658{
659 unsigned long flags;
660
661 spin_lock_irqsave(&old->list->lock, flags);
662 __skb_append(old, newsk);
663 spin_unlock_irqrestore(&old->list->lock, flags);
664}
665
666
667
668
669
670
671static inline void __skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
672{
673 struct sk_buff * next, * prev;
674
675 list->qlen--;
676 next = skb->next;
677 prev = skb->prev;
678 skb->next = NULL;
679 skb->prev = NULL;
680 skb->list = NULL;
681 next->prev = prev;
682 prev->next = next;
683}
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698static inline void skb_unlink(struct sk_buff *skb)
699{
700 struct sk_buff_head *list = skb->list;
701
702 if(list) {
703 unsigned long flags;
704
705 spin_lock_irqsave(&list->lock, flags);
706 if(skb->list == list)
707 __skb_unlink(skb, skb->list);
708 spin_unlock_irqrestore(&list->lock, flags);
709 }
710}
711
712
713
714
715
716
717
718
719
720
721
722
723static inline struct sk_buff *__skb_dequeue_tail(struct sk_buff_head *list)
724{
725 struct sk_buff *skb = skb_peek_tail(list);
726 if (skb)
727 __skb_unlink(skb, list);
728 return skb;
729}
730
731
732
733
734
735
736
737
738
739
740static inline struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
741{
742 unsigned long flags;
743 struct sk_buff *result;
744
745 spin_lock_irqsave(&list->lock, flags);
746 result = __skb_dequeue_tail(list);
747 spin_unlock_irqrestore(&list->lock, flags);
748 return result;
749}
750
751static inline int skb_is_nonlinear(const struct sk_buff *skb)
752{
753 return skb->data_len;
754}
755
756static inline unsigned int skb_headlen(const struct sk_buff *skb)
757{
758 return skb->len - skb->data_len;
759}
760
761#define SKB_PAGE_ASSERT(skb) do { if (skb_shinfo(skb)->nr_frags) out_of_line_bug(); } while (0)
762#define SKB_FRAG_ASSERT(skb) do { if (skb_shinfo(skb)->frag_list) out_of_line_bug(); } while (0)
763#define SKB_LINEAR_ASSERT(skb) do { if (skb_is_nonlinear(skb)) out_of_line_bug(); } while (0)
764
765
766
767
768
769static inline unsigned char *__skb_put(struct sk_buff *skb, unsigned int len)
770{
771 unsigned char *tmp=skb->tail;
772 SKB_LINEAR_ASSERT(skb);
773 skb->tail+=len;
774 skb->len+=len;
775 return tmp;
776}
777
778
779
780
781
782
783
784
785
786
787
788static inline unsigned char *skb_put(struct sk_buff *skb, unsigned int len)
789{
790 unsigned char *tmp=skb->tail;
791 SKB_LINEAR_ASSERT(skb);
792 skb->tail+=len;
793 skb->len+=len;
794 if(skb->tail>skb->end) {
795 skb_over_panic(skb, len, current_text_addr());
796 }
797 return tmp;
798}
799
800static inline unsigned char *__skb_push(struct sk_buff *skb, unsigned int len)
801{
802 skb->data-=len;
803 skb->len+=len;
804 return skb->data;
805}
806
807
808
809
810
811
812
813
814
815
816
817static inline unsigned char *skb_push(struct sk_buff *skb, unsigned int len)
818{
819 skb->data-=len;
820 skb->len+=len;
821 if(skb->data<skb->head) {
822 skb_under_panic(skb, len, current_text_addr());
823 }
824 return skb->data;
825}
826
827static inline char *__skb_pull(struct sk_buff *skb, unsigned int len)
828{
829 skb->len-=len;
830 if (skb->len < skb->data_len)
831 out_of_line_bug();
832 return skb->data+=len;
833}
834
835
836
837
838
839
840
841
842
843
844
845
846static inline unsigned char * skb_pull(struct sk_buff *skb, unsigned int len)
847{
848 if (len > skb->len)
849 return NULL;
850 return __skb_pull(skb,len);
851}
852
853extern unsigned char * __pskb_pull_tail(struct sk_buff *skb, int delta);
854
855static inline char *__pskb_pull(struct sk_buff *skb, unsigned int len)
856{
857 if (len > skb_headlen(skb) &&
858 __pskb_pull_tail(skb, len-skb_headlen(skb)) == NULL)
859 return NULL;
860 skb->len -= len;
861 return skb->data += len;
862}
863
864static inline unsigned char * pskb_pull(struct sk_buff *skb, unsigned int len)
865{
866 if (len > skb->len)
867 return NULL;
868 return __pskb_pull(skb,len);
869}
870
871static inline int pskb_may_pull(struct sk_buff *skb, unsigned int len)
872{
873 if (len <= skb_headlen(skb))
874 return 1;
875 if (len > skb->len)
876 return 0;
877 return (__pskb_pull_tail(skb, len-skb_headlen(skb)) != NULL);
878}
879
880
881
882
883
884
885
886
887static inline int skb_headroom(const struct sk_buff *skb)
888{
889 return skb->data-skb->head;
890}
891
892
893
894
895
896
897
898
899static inline int skb_tailroom(const struct sk_buff *skb)
900{
901 return skb_is_nonlinear(skb) ? 0 : skb->end-skb->tail;
902}
903
904
905
906
907
908
909
910
911
912
913static inline void skb_reserve(struct sk_buff *skb, unsigned int len)
914{
915 skb->data+=len;
916 skb->tail+=len;
917}
918
919extern int ___pskb_trim(struct sk_buff *skb, unsigned int len, int realloc);
920
921static inline void __skb_trim(struct sk_buff *skb, unsigned int len)
922{
923 if (!skb->data_len) {
924 skb->len = len;
925 skb->tail = skb->data+len;
926 } else {
927 ___pskb_trim(skb, len, 0);
928 }
929}
930
931
932
933
934
935
936
937
938
939
940static inline void skb_trim(struct sk_buff *skb, unsigned int len)
941{
942 if (skb->len > len) {
943 __skb_trim(skb, len);
944 }
945}
946
947
948static inline int __pskb_trim(struct sk_buff *skb, unsigned int len)
949{
950 if (!skb->data_len) {
951 skb->len = len;
952 skb->tail = skb->data+len;
953 return 0;
954 } else {
955 return ___pskb_trim(skb, len, 1);
956 }
957}
958
959static inline int pskb_trim(struct sk_buff *skb, unsigned int len)
960{
961 if (len < skb->len)
962 return __pskb_trim(skb, len);
963 return 0;
964}
965
966
967
968
969
970
971
972
973
974
975
976static inline void skb_orphan(struct sk_buff *skb)
977{
978 if (skb->destructor)
979 skb->destructor(skb);
980 skb->destructor = NULL;
981 skb->sk = NULL;
982}
983
984
985
986
987
988
989
990
991
992
993
994static inline void skb_queue_purge(struct sk_buff_head *list)
995{
996 struct sk_buff *skb;
997 while ((skb=skb_dequeue(list))!=NULL)
998 kfree_skb(skb);
999}
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011static inline void __skb_queue_purge(struct sk_buff_head *list)
1012{
1013 struct sk_buff *skb;
1014 while ((skb=__skb_dequeue(list))!=NULL)
1015 kfree_skb(skb);
1016}
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031static inline struct sk_buff *__dev_alloc_skb(unsigned int length,
1032 int gfp_mask)
1033{
1034 struct sk_buff *skb;
1035
1036 skb = alloc_skb(length+16, gfp_mask);
1037 if (skb)
1038 skb_reserve(skb,16);
1039 return skb;
1040}
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055static inline struct sk_buff *dev_alloc_skb(unsigned int length)
1056{
1057 return __dev_alloc_skb(length, GFP_ATOMIC);
1058}
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073static inline int
1074skb_cow(struct sk_buff *skb, unsigned int headroom)
1075{
1076 int delta = (headroom > 16 ? headroom : 16) - skb_headroom(skb);
1077
1078 if (delta < 0)
1079 delta = 0;
1080
1081 if (delta || skb_cloned(skb))
1082 return pskb_expand_head(skb, (delta+15)&~15, 0, GFP_ATOMIC);
1083 return 0;
1084}
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098static inline struct sk_buff *skb_padto(struct sk_buff *skb, unsigned int len)
1099{
1100 unsigned int size = skb->len;
1101 if(likely(size >= len))
1102 return skb;
1103 return skb_pad(skb, len-size);
1104}
1105
1106
1107
1108
1109
1110
1111
1112
1113int skb_linearize(struct sk_buff *skb, int gfp);
1114
1115static inline void *kmap_skb_frag(const skb_frag_t *frag)
1116{
1117#ifdef CONFIG_HIGHMEM
1118 if (in_irq())
1119 out_of_line_bug();
1120
1121 local_bh_disable();
1122#endif
1123 return kmap_atomic(frag->page, KM_SKB_DATA_SOFTIRQ);
1124}
1125
1126static inline void kunmap_skb_frag(void *vaddr)
1127{
1128 kunmap_atomic(vaddr, KM_SKB_DATA_SOFTIRQ);
1129#ifdef CONFIG_HIGHMEM
1130 local_bh_enable();
1131#endif
1132}
1133
1134#define skb_queue_walk(queue, skb) \
1135 for (skb = (queue)->next; \
1136 (skb != (struct sk_buff *)(queue)); \
1137 skb=skb->next)
1138
1139
1140extern struct sk_buff * skb_recv_datagram(struct sock *sk,unsigned flags,int noblock, int *err);
1141extern unsigned int datagram_poll(struct file *file, struct socket *sock, struct poll_table_struct *wait);
1142extern int skb_copy_datagram(const struct sk_buff *from, int offset, char *to,int size);
1143extern int skb_copy_datagram_iovec(const struct sk_buff *from, int offset, struct iovec *to,int size);
1144extern int skb_copy_and_csum_datagram(const struct sk_buff *skb, int offset, u8 *to, int len, unsigned int *csump);
1145extern int skb_copy_and_csum_datagram_iovec(const struct sk_buff *skb, int hlen, struct iovec *iov);
1146extern void skb_free_datagram(struct sock * sk, struct sk_buff *skb);
1147
1148extern unsigned int skb_checksum(const struct sk_buff *skb, int offset, int len, unsigned int csum);
1149extern int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len);
1150extern unsigned int skb_copy_and_csum_bits(const struct sk_buff *skb, int offset, u8 *to, int len, unsigned int csum);
1151extern void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to);
1152
1153extern void skb_init(void);
1154extern void skb_add_mtu(int mtu);
1155
1156#ifdef CONFIG_NETFILTER
1157static inline void
1158nf_conntrack_put(struct nf_ct_info *nfct)
1159{
1160 if (nfct && atomic_dec_and_test(&nfct->master->use))
1161 nfct->master->destroy(nfct->master);
1162}
1163static inline void
1164nf_conntrack_get(struct nf_ct_info *nfct)
1165{
1166 if (nfct)
1167 atomic_inc(&nfct->master->use);
1168}
1169static inline void
1170nf_reset(struct sk_buff *skb)
1171{
1172 nf_conntrack_put(skb->nfct);
1173 skb->nfct = NULL;
1174#ifdef CONFIG_NETFILTER_DEBUG
1175 skb->nf_debug = 0;
1176#endif
1177}
1178#else
1179static inline void nf_reset(struct sk_buff *skb) {}
1180#endif
1181
1182#endif
1183#endif
1184