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/kernel.h>
18#include <linux/kmemcheck.h>
19#include <linux/compiler.h>
20#include <linux/time.h>
21#include <linux/bug.h>
22#include <linux/cache.h>
23
24#include <linux/atomic.h>
25#include <asm/types.h>
26#include <linux/spinlock.h>
27#include <linux/net.h>
28#include <linux/textsearch.h>
29#include <net/checksum.h>
30#include <linux/rcupdate.h>
31#include <linux/dmaengine.h>
32#include <linux/hrtimer.h>
33#include <linux/dma-mapping.h>
34#include <linux/netdev_features.h>
35
36
37#define CHECKSUM_NONE 0
38#define CHECKSUM_UNNECESSARY 1
39#define CHECKSUM_COMPLETE 2
40#define CHECKSUM_PARTIAL 3
41
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))
50
51
52#define SKB_TRUESIZE(X) ((X) + \
53 SKB_DATA_ALIGN(sizeof(struct sk_buff)) + \
54 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)))
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108struct net_device;
109struct scatterlist;
110struct pipe_inode_info;
111
112#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
113struct nf_conntrack {
114 atomic_t use;
115};
116#endif
117
118#ifdef CONFIG_BRIDGE_NETFILTER
119struct nf_bridge_info {
120 atomic_t use;
121 unsigned int mask;
122 struct net_device *physindev;
123 struct net_device *physoutdev;
124 unsigned long data[32 / sizeof(unsigned long)];
125};
126#endif
127
128struct sk_buff_head {
129
130 struct sk_buff *next;
131 struct sk_buff *prev;
132
133 __u32 qlen;
134 spinlock_t lock;
135};
136
137struct sk_buff;
138
139
140
141
142
143
144
145
146#if (65536/PAGE_SIZE + 1) < 16
147#define MAX_SKB_FRAGS 16UL
148#else
149#define MAX_SKB_FRAGS (65536/PAGE_SIZE + 1)
150#endif
151
152typedef struct skb_frag_struct skb_frag_t;
153
154struct skb_frag_struct {
155 struct {
156 struct page *p;
157 } page;
158#if (BITS_PER_LONG > 32) || (PAGE_SIZE >= 65536)
159 __u32 page_offset;
160 __u32 size;
161#else
162 __u16 page_offset;
163 __u16 size;
164#endif
165};
166
167static inline unsigned int skb_frag_size(const skb_frag_t *frag)
168{
169 return frag->size;
170}
171
172static inline void skb_frag_size_set(skb_frag_t *frag, unsigned int size)
173{
174 frag->size = size;
175}
176
177static inline void skb_frag_size_add(skb_frag_t *frag, int delta)
178{
179 frag->size += delta;
180}
181
182static inline void skb_frag_size_sub(skb_frag_t *frag, int delta)
183{
184 frag->size -= delta;
185}
186
187#define HAVE_HW_TIME_STAMP
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212struct skb_shared_hwtstamps {
213 ktime_t hwtstamp;
214 ktime_t syststamp;
215};
216
217
218enum {
219
220 SKBTX_HW_TSTAMP = 1 << 0,
221
222
223 SKBTX_SW_TSTAMP = 1 << 1,
224
225
226 SKBTX_IN_PROGRESS = 1 << 2,
227
228
229 SKBTX_DEV_ZEROCOPY = 1 << 3,
230
231
232 SKBTX_WIFI_STATUS = 1 << 4,
233
234
235
236
237
238
239 SKBTX_SHARED_FRAG = 1 << 5,
240};
241
242
243
244
245
246
247
248
249
250struct ubuf_info {
251 void (*callback)(struct ubuf_info *, bool zerocopy_success);
252 void *ctx;
253 unsigned long desc;
254};
255
256
257
258
259struct skb_shared_info {
260 unsigned char nr_frags;
261 __u8 tx_flags;
262 unsigned short gso_size;
263
264 unsigned short gso_segs;
265 unsigned short gso_type;
266 struct sk_buff *frag_list;
267 struct skb_shared_hwtstamps hwtstamps;
268 __be32 ip6_frag_id;
269
270
271
272
273 atomic_t dataref;
274
275
276
277 void * destructor_arg;
278
279
280 skb_frag_t frags[MAX_SKB_FRAGS];
281};
282
283
284
285
286
287
288
289
290
291
292
293
294#define SKB_DATAREF_SHIFT 16
295#define SKB_DATAREF_MASK ((1 << SKB_DATAREF_SHIFT) - 1)
296
297
298enum {
299 SKB_FCLONE_UNAVAILABLE,
300 SKB_FCLONE_ORIG,
301 SKB_FCLONE_CLONE,
302};
303
304enum {
305 SKB_GSO_TCPV4 = 1 << 0,
306 SKB_GSO_UDP = 1 << 1,
307
308
309 SKB_GSO_DODGY = 1 << 2,
310
311
312 SKB_GSO_TCP_ECN = 1 << 3,
313
314 SKB_GSO_TCPV6 = 1 << 4,
315
316 SKB_GSO_FCOE = 1 << 5,
317
318 SKB_GSO_GRE = 1 << 6,
319};
320
321#if BITS_PER_LONG > 32
322#define NET_SKBUFF_DATA_USES_OFFSET 1
323#endif
324
325#ifdef NET_SKBUFF_DATA_USES_OFFSET
326typedef unsigned int sk_buff_data_t;
327#else
328typedef unsigned char *sk_buff_data_t;
329#endif
330
331#if defined(CONFIG_NF_DEFRAG_IPV4) || defined(CONFIG_NF_DEFRAG_IPV4_MODULE) || \
332 defined(CONFIG_NF_DEFRAG_IPV6) || defined(CONFIG_NF_DEFRAG_IPV6_MODULE)
333#define NET_SKBUFF_NF_DEFRAG_NEEDED 1
334#endif
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401struct sk_buff {
402
403 struct sk_buff *next;
404 struct sk_buff *prev;
405
406 ktime_t tstamp;
407
408 struct sock *sk;
409 struct net_device *dev;
410
411
412
413
414
415
416
417 char cb[48] __aligned(8);
418
419 unsigned long _skb_refdst;
420#ifdef CONFIG_XFRM
421 struct sec_path *sp;
422#endif
423 unsigned int len,
424 data_len;
425 __u16 mac_len,
426 hdr_len;
427 union {
428 __wsum csum;
429 struct {
430 __u16 csum_start;
431 __u16 csum_offset;
432 };
433 };
434 __u32 priority;
435 kmemcheck_bitfield_begin(flags1);
436 __u8 local_df:1,
437 cloned:1,
438 ip_summed:2,
439 nohdr:1,
440 nfctinfo:3;
441 __u8 pkt_type:3,
442 fclone:2,
443 ipvs_property:1,
444 peeked:1,
445 nf_trace:1;
446 kmemcheck_bitfield_end(flags1);
447 __be16 protocol;
448
449 void (*destructor)(struct sk_buff *skb);
450#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
451 struct nf_conntrack *nfct;
452#endif
453#ifdef NET_SKBUFF_NF_DEFRAG_NEEDED
454 struct sk_buff *nfct_reasm;
455#endif
456#ifdef CONFIG_BRIDGE_NETFILTER
457 struct nf_bridge_info *nf_bridge;
458#endif
459
460 int skb_iif;
461
462 __u32 rxhash;
463
464 __u16 vlan_tci;
465
466#ifdef CONFIG_NET_SCHED
467 __u16 tc_index;
468#ifdef CONFIG_NET_CLS_ACT
469 __u16 tc_verd;
470#endif
471#endif
472
473 __u16 queue_mapping;
474 kmemcheck_bitfield_begin(flags2);
475#ifdef CONFIG_IPV6_NDISC_NODETYPE
476 __u8 ndisc_nodetype:2;
477#endif
478 __u8 pfmemalloc:1;
479 __u8 ooo_okay:1;
480 __u8 l4_rxhash:1;
481 __u8 wifi_acked_valid:1;
482 __u8 wifi_acked:1;
483 __u8 no_fcs:1;
484 __u8 head_frag:1;
485
486
487
488
489
490 __u8 encapsulation:1;
491
492 kmemcheck_bitfield_end(flags2);
493
494#ifdef CONFIG_NET_DMA
495 dma_cookie_t dma_cookie;
496#endif
497#ifdef CONFIG_NETWORK_SECMARK
498 __u32 secmark;
499#endif
500 union {
501 __u32 mark;
502 __u32 dropcount;
503 __u32 reserved_tailroom;
504 };
505
506 sk_buff_data_t inner_transport_header;
507 sk_buff_data_t inner_network_header;
508 sk_buff_data_t transport_header;
509 sk_buff_data_t network_header;
510 sk_buff_data_t mac_header;
511
512 sk_buff_data_t tail;
513 sk_buff_data_t end;
514 unsigned char *head,
515 *data;
516 unsigned int truesize;
517 atomic_t users;
518};
519
520#ifdef __KERNEL__
521
522
523
524#include <linux/slab.h>
525
526
527#define SKB_ALLOC_FCLONE 0x01
528#define SKB_ALLOC_RX 0x02
529
530
531static inline bool skb_pfmemalloc(const struct sk_buff *skb)
532{
533 return unlikely(skb->pfmemalloc);
534}
535
536
537
538
539
540#define SKB_DST_NOREF 1UL
541#define SKB_DST_PTRMASK ~(SKB_DST_NOREF)
542
543
544
545
546
547
548
549static inline struct dst_entry *skb_dst(const struct sk_buff *skb)
550{
551
552
553
554 WARN_ON((skb->_skb_refdst & SKB_DST_NOREF) &&
555 !rcu_read_lock_held() &&
556 !rcu_read_lock_bh_held());
557 return (struct dst_entry *)(skb->_skb_refdst & SKB_DST_PTRMASK);
558}
559
560
561
562
563
564
565
566
567
568static inline void skb_dst_set(struct sk_buff *skb, struct dst_entry *dst)
569{
570 skb->_skb_refdst = (unsigned long)dst;
571}
572
573extern void skb_dst_set_noref(struct sk_buff *skb, struct dst_entry *dst);
574
575
576
577
578
579static inline bool skb_dst_is_noref(const struct sk_buff *skb)
580{
581 return (skb->_skb_refdst & SKB_DST_NOREF) && skb_dst(skb);
582}
583
584static inline struct rtable *skb_rtable(const struct sk_buff *skb)
585{
586 return (struct rtable *)skb_dst(skb);
587}
588
589extern void kfree_skb(struct sk_buff *skb);
590extern void skb_tx_error(struct sk_buff *skb);
591extern void consume_skb(struct sk_buff *skb);
592extern void __kfree_skb(struct sk_buff *skb);
593extern struct kmem_cache *skbuff_head_cache;
594
595extern void kfree_skb_partial(struct sk_buff *skb, bool head_stolen);
596extern bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
597 bool *fragstolen, int *delta_truesize);
598
599extern struct sk_buff *__alloc_skb(unsigned int size,
600 gfp_t priority, int flags, int node);
601extern struct sk_buff *build_skb(void *data, unsigned int frag_size);
602static inline struct sk_buff *alloc_skb(unsigned int size,
603 gfp_t priority)
604{
605 return __alloc_skb(size, priority, 0, NUMA_NO_NODE);
606}
607
608static inline struct sk_buff *alloc_skb_fclone(unsigned int size,
609 gfp_t priority)
610{
611 return __alloc_skb(size, priority, SKB_ALLOC_FCLONE, NUMA_NO_NODE);
612}
613
614extern struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src);
615extern int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask);
616extern struct sk_buff *skb_clone(struct sk_buff *skb,
617 gfp_t priority);
618extern struct sk_buff *skb_copy(const struct sk_buff *skb,
619 gfp_t priority);
620extern struct sk_buff *__pskb_copy(struct sk_buff *skb,
621 int headroom, gfp_t gfp_mask);
622
623extern int pskb_expand_head(struct sk_buff *skb,
624 int nhead, int ntail,
625 gfp_t gfp_mask);
626extern struct sk_buff *skb_realloc_headroom(struct sk_buff *skb,
627 unsigned int headroom);
628extern struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
629 int newheadroom, int newtailroom,
630 gfp_t priority);
631extern int skb_to_sgvec(struct sk_buff *skb,
632 struct scatterlist *sg, int offset,
633 int len);
634extern int skb_cow_data(struct sk_buff *skb, int tailbits,
635 struct sk_buff **trailer);
636extern int skb_pad(struct sk_buff *skb, int pad);
637#define dev_kfree_skb(a) consume_skb(a)
638
639extern int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
640 int getfrag(void *from, char *to, int offset,
641 int len,int odd, struct sk_buff *skb),
642 void *from, int length);
643
644struct skb_seq_state {
645 __u32 lower_offset;
646 __u32 upper_offset;
647 __u32 frag_idx;
648 __u32 stepped_offset;
649 struct sk_buff *root_skb;
650 struct sk_buff *cur_skb;
651 __u8 *frag_data;
652};
653
654extern void skb_prepare_seq_read(struct sk_buff *skb,
655 unsigned int from, unsigned int to,
656 struct skb_seq_state *st);
657extern unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
658 struct skb_seq_state *st);
659extern void skb_abort_seq_read(struct skb_seq_state *st);
660
661extern unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
662 unsigned int to, struct ts_config *config,
663 struct ts_state *state);
664
665extern void __skb_get_rxhash(struct sk_buff *skb);
666static inline __u32 skb_get_rxhash(struct sk_buff *skb)
667{
668 if (!skb->l4_rxhash)
669 __skb_get_rxhash(skb);
670
671 return skb->rxhash;
672}
673
674#ifdef NET_SKBUFF_DATA_USES_OFFSET
675static inline unsigned char *skb_end_pointer(const struct sk_buff *skb)
676{
677 return skb->head + skb->end;
678}
679
680static inline unsigned int skb_end_offset(const struct sk_buff *skb)
681{
682 return skb->end;
683}
684#else
685static inline unsigned char *skb_end_pointer(const struct sk_buff *skb)
686{
687 return skb->end;
688}
689
690static inline unsigned int skb_end_offset(const struct sk_buff *skb)
691{
692 return skb->end - skb->head;
693}
694#endif
695
696
697#define skb_shinfo(SKB) ((struct skb_shared_info *)(skb_end_pointer(SKB)))
698
699static inline struct skb_shared_hwtstamps *skb_hwtstamps(struct sk_buff *skb)
700{
701 return &skb_shinfo(skb)->hwtstamps;
702}
703
704
705
706
707
708
709
710static inline int skb_queue_empty(const struct sk_buff_head *list)
711{
712 return list->next == (struct sk_buff *)list;
713}
714
715
716
717
718
719
720
721
722static inline bool skb_queue_is_last(const struct sk_buff_head *list,
723 const struct sk_buff *skb)
724{
725 return skb->next == (struct sk_buff *)list;
726}
727
728
729
730
731
732
733
734
735static inline bool skb_queue_is_first(const struct sk_buff_head *list,
736 const struct sk_buff *skb)
737{
738 return skb->prev == (struct sk_buff *)list;
739}
740
741
742
743
744
745
746
747
748
749static inline struct sk_buff *skb_queue_next(const struct sk_buff_head *list,
750 const struct sk_buff *skb)
751{
752
753
754
755 BUG_ON(skb_queue_is_last(list, skb));
756 return skb->next;
757}
758
759
760
761
762
763
764
765
766
767static inline struct sk_buff *skb_queue_prev(const struct sk_buff_head *list,
768 const struct sk_buff *skb)
769{
770
771
772
773 BUG_ON(skb_queue_is_first(list, skb));
774 return skb->prev;
775}
776
777
778
779
780
781
782
783
784static inline struct sk_buff *skb_get(struct sk_buff *skb)
785{
786 atomic_inc(&skb->users);
787 return skb;
788}
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803static inline int skb_cloned(const struct sk_buff *skb)
804{
805 return skb->cloned &&
806 (atomic_read(&skb_shinfo(skb)->dataref) & SKB_DATAREF_MASK) != 1;
807}
808
809static inline int skb_unclone(struct sk_buff *skb, gfp_t pri)
810{
811 might_sleep_if(pri & __GFP_WAIT);
812
813 if (skb_cloned(skb))
814 return pskb_expand_head(skb, 0, 0, pri);
815
816 return 0;
817}
818
819
820
821
822
823
824
825
826static inline int skb_header_cloned(const struct sk_buff *skb)
827{
828 int dataref;
829
830 if (!skb->cloned)
831 return 0;
832
833 dataref = atomic_read(&skb_shinfo(skb)->dataref);
834 dataref = (dataref & SKB_DATAREF_MASK) - (dataref >> SKB_DATAREF_SHIFT);
835 return dataref != 1;
836}
837
838
839
840
841
842
843
844
845
846static inline void skb_header_release(struct sk_buff *skb)
847{
848 BUG_ON(skb->nohdr);
849 skb->nohdr = 1;
850 atomic_add(1 << SKB_DATAREF_SHIFT, &skb_shinfo(skb)->dataref);
851}
852
853
854
855
856
857
858
859
860static inline int skb_shared(const struct sk_buff *skb)
861{
862 return atomic_read(&skb->users) != 1;
863}
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878static inline struct sk_buff *skb_share_check(struct sk_buff *skb, gfp_t pri)
879{
880 might_sleep_if(pri & __GFP_WAIT);
881 if (skb_shared(skb)) {
882 struct sk_buff *nskb = skb_clone(skb, pri);
883
884 if (likely(nskb))
885 consume_skb(skb);
886 else
887 kfree_skb(skb);
888 skb = nskb;
889 }
890 return skb;
891}
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913static inline struct sk_buff *skb_unshare(struct sk_buff *skb,
914 gfp_t pri)
915{
916 might_sleep_if(pri & __GFP_WAIT);
917 if (skb_cloned(skb)) {
918 struct sk_buff *nskb = skb_copy(skb, pri);
919 kfree_skb(skb);
920 skb = nskb;
921 }
922 return skb;
923}
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938static inline struct sk_buff *skb_peek(const struct sk_buff_head *list_)
939{
940 struct sk_buff *skb = list_->next;
941
942 if (skb == (struct sk_buff *)list_)
943 skb = NULL;
944 return skb;
945}
946
947
948
949
950
951
952
953
954
955
956static inline struct sk_buff *skb_peek_next(struct sk_buff *skb,
957 const struct sk_buff_head *list_)
958{
959 struct sk_buff *next = skb->next;
960
961 if (next == (struct sk_buff *)list_)
962 next = NULL;
963 return next;
964}
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979static inline struct sk_buff *skb_peek_tail(const struct sk_buff_head *list_)
980{
981 struct sk_buff *skb = list_->prev;
982
983 if (skb == (struct sk_buff *)list_)
984 skb = NULL;
985 return skb;
986
987}
988
989
990
991
992
993
994
995static inline __u32 skb_queue_len(const struct sk_buff_head *list_)
996{
997 return list_->qlen;
998}
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010static inline void __skb_queue_head_init(struct sk_buff_head *list)
1011{
1012 list->prev = list->next = (struct sk_buff *)list;
1013 list->qlen = 0;
1014}
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024static inline void skb_queue_head_init(struct sk_buff_head *list)
1025{
1026 spin_lock_init(&list->lock);
1027 __skb_queue_head_init(list);
1028}
1029
1030static inline void skb_queue_head_init_class(struct sk_buff_head *list,
1031 struct lock_class_key *class)
1032{
1033 skb_queue_head_init(list);
1034 lockdep_set_class(&list->lock, class);
1035}
1036
1037
1038
1039
1040
1041
1042
1043extern void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list);
1044static inline void __skb_insert(struct sk_buff *newsk,
1045 struct sk_buff *prev, struct sk_buff *next,
1046 struct sk_buff_head *list)
1047{
1048 newsk->next = next;
1049 newsk->prev = prev;
1050 next->prev = prev->next = newsk;
1051 list->qlen++;
1052}
1053
1054static inline void __skb_queue_splice(const struct sk_buff_head *list,
1055 struct sk_buff *prev,
1056 struct sk_buff *next)
1057{
1058 struct sk_buff *first = list->next;
1059 struct sk_buff *last = list->prev;
1060
1061 first->prev = prev;
1062 prev->next = first;
1063
1064 last->next = next;
1065 next->prev = last;
1066}
1067
1068
1069
1070
1071
1072
1073static inline void skb_queue_splice(const struct sk_buff_head *list,
1074 struct sk_buff_head *head)
1075{
1076 if (!skb_queue_empty(list)) {
1077 __skb_queue_splice(list, (struct sk_buff *) head, head->next);
1078 head->qlen += list->qlen;
1079 }
1080}
1081
1082
1083
1084
1085
1086
1087
1088
1089static inline void skb_queue_splice_init(struct sk_buff_head *list,
1090 struct sk_buff_head *head)
1091{
1092 if (!skb_queue_empty(list)) {
1093 __skb_queue_splice(list, (struct sk_buff *) head, head->next);
1094 head->qlen += list->qlen;
1095 __skb_queue_head_init(list);
1096 }
1097}
1098
1099
1100
1101
1102
1103
1104static inline void skb_queue_splice_tail(const struct sk_buff_head *list,
1105 struct sk_buff_head *head)
1106{
1107 if (!skb_queue_empty(list)) {
1108 __skb_queue_splice(list, head->prev, (struct sk_buff *) head);
1109 head->qlen += list->qlen;
1110 }
1111}
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121static inline void skb_queue_splice_tail_init(struct sk_buff_head *list,
1122 struct sk_buff_head *head)
1123{
1124 if (!skb_queue_empty(list)) {
1125 __skb_queue_splice(list, head->prev, (struct sk_buff *) head);
1126 head->qlen += list->qlen;
1127 __skb_queue_head_init(list);
1128 }
1129}
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142static inline void __skb_queue_after(struct sk_buff_head *list,
1143 struct sk_buff *prev,
1144 struct sk_buff *newsk)
1145{
1146 __skb_insert(newsk, prev, prev->next, list);
1147}
1148
1149extern void skb_append(struct sk_buff *old, struct sk_buff *newsk,
1150 struct sk_buff_head *list);
1151
1152static inline void __skb_queue_before(struct sk_buff_head *list,
1153 struct sk_buff *next,
1154 struct sk_buff *newsk)
1155{
1156 __skb_insert(newsk, next->prev, next, list);
1157}
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169extern void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk);
1170static inline void __skb_queue_head(struct sk_buff_head *list,
1171 struct sk_buff *newsk)
1172{
1173 __skb_queue_after(list, (struct sk_buff *)list, newsk);
1174}
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186extern void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk);
1187static inline void __skb_queue_tail(struct sk_buff_head *list,
1188 struct sk_buff *newsk)
1189{
1190 __skb_queue_before(list, (struct sk_buff *)list, newsk);
1191}
1192
1193
1194
1195
1196
1197extern void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list);
1198static inline void __skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
1199{
1200 struct sk_buff *next, *prev;
1201
1202 list->qlen--;
1203 next = skb->next;
1204 prev = skb->prev;
1205 skb->next = skb->prev = NULL;
1206 next->prev = prev;
1207 prev->next = next;
1208}
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218extern struct sk_buff *skb_dequeue(struct sk_buff_head *list);
1219static inline struct sk_buff *__skb_dequeue(struct sk_buff_head *list)
1220{
1221 struct sk_buff *skb = skb_peek(list);
1222 if (skb)
1223 __skb_unlink(skb, list);
1224 return skb;
1225}
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235extern struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list);
1236static inline struct sk_buff *__skb_dequeue_tail(struct sk_buff_head *list)
1237{
1238 struct sk_buff *skb = skb_peek_tail(list);
1239 if (skb)
1240 __skb_unlink(skb, list);
1241 return skb;
1242}
1243
1244
1245static inline bool skb_is_nonlinear(const struct sk_buff *skb)
1246{
1247 return skb->data_len;
1248}
1249
1250static inline unsigned int skb_headlen(const struct sk_buff *skb)
1251{
1252 return skb->len - skb->data_len;
1253}
1254
1255static inline int skb_pagelen(const struct sk_buff *skb)
1256{
1257 int i, len = 0;
1258
1259 for (i = (int)skb_shinfo(skb)->nr_frags - 1; i >= 0; i--)
1260 len += skb_frag_size(&skb_shinfo(skb)->frags[i]);
1261 return len + skb_headlen(skb);
1262}
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277static inline void __skb_fill_page_desc(struct sk_buff *skb, int i,
1278 struct page *page, int off, int size)
1279{
1280 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291 frag->page.p = page;
1292 frag->page_offset = off;
1293 skb_frag_size_set(frag, size);
1294
1295 page = compound_head(page);
1296 if (page->pfmemalloc && !page->mapping)
1297 skb->pfmemalloc = true;
1298}
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314static inline void skb_fill_page_desc(struct sk_buff *skb, int i,
1315 struct page *page, int off, int size)
1316{
1317 __skb_fill_page_desc(skb, i, page, off, size);
1318 skb_shinfo(skb)->nr_frags = i + 1;
1319}
1320
1321extern void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page,
1322 int off, int size, unsigned int truesize);
1323
1324#define SKB_PAGE_ASSERT(skb) BUG_ON(skb_shinfo(skb)->nr_frags)
1325#define SKB_FRAG_ASSERT(skb) BUG_ON(skb_has_frag_list(skb))
1326#define SKB_LINEAR_ASSERT(skb) BUG_ON(skb_is_nonlinear(skb))
1327
1328#ifdef NET_SKBUFF_DATA_USES_OFFSET
1329static inline unsigned char *skb_tail_pointer(const struct sk_buff *skb)
1330{
1331 return skb->head + skb->tail;
1332}
1333
1334static inline void skb_reset_tail_pointer(struct sk_buff *skb)
1335{
1336 skb->tail = skb->data - skb->head;
1337}
1338
1339static inline void skb_set_tail_pointer(struct sk_buff *skb, const int offset)
1340{
1341 skb_reset_tail_pointer(skb);
1342 skb->tail += offset;
1343}
1344#else
1345static inline unsigned char *skb_tail_pointer(const struct sk_buff *skb)
1346{
1347 return skb->tail;
1348}
1349
1350static inline void skb_reset_tail_pointer(struct sk_buff *skb)
1351{
1352 skb->tail = skb->data;
1353}
1354
1355static inline void skb_set_tail_pointer(struct sk_buff *skb, const int offset)
1356{
1357 skb->tail = skb->data + offset;
1358}
1359
1360#endif
1361
1362
1363
1364
1365extern unsigned char *skb_put(struct sk_buff *skb, unsigned int len);
1366static inline unsigned char *__skb_put(struct sk_buff *skb, unsigned int len)
1367{
1368 unsigned char *tmp = skb_tail_pointer(skb);
1369 SKB_LINEAR_ASSERT(skb);
1370 skb->tail += len;
1371 skb->len += len;
1372 return tmp;
1373}
1374
1375extern unsigned char *skb_push(struct sk_buff *skb, unsigned int len);
1376static inline unsigned char *__skb_push(struct sk_buff *skb, unsigned int len)
1377{
1378 skb->data -= len;
1379 skb->len += len;
1380 return skb->data;
1381}
1382
1383extern unsigned char *skb_pull(struct sk_buff *skb, unsigned int len);
1384static inline unsigned char *__skb_pull(struct sk_buff *skb, unsigned int len)
1385{
1386 skb->len -= len;
1387 BUG_ON(skb->len < skb->data_len);
1388 return skb->data += len;
1389}
1390
1391static inline unsigned char *skb_pull_inline(struct sk_buff *skb, unsigned int len)
1392{
1393 return unlikely(len > skb->len) ? NULL : __skb_pull(skb, len);
1394}
1395
1396extern unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta);
1397
1398static inline unsigned char *__pskb_pull(struct sk_buff *skb, unsigned int len)
1399{
1400 if (len > skb_headlen(skb) &&
1401 !__pskb_pull_tail(skb, len - skb_headlen(skb)))
1402 return NULL;
1403 skb->len -= len;
1404 return skb->data += len;
1405}
1406
1407static inline unsigned char *pskb_pull(struct sk_buff *skb, unsigned int len)
1408{
1409 return unlikely(len > skb->len) ? NULL : __pskb_pull(skb, len);
1410}
1411
1412static inline int pskb_may_pull(struct sk_buff *skb, unsigned int len)
1413{
1414 if (likely(len <= skb_headlen(skb)))
1415 return 1;
1416 if (unlikely(len > skb->len))
1417 return 0;
1418 return __pskb_pull_tail(skb, len - skb_headlen(skb)) != NULL;
1419}
1420
1421
1422
1423
1424
1425
1426
1427static inline unsigned int skb_headroom(const struct sk_buff *skb)
1428{
1429 return skb->data - skb->head;
1430}
1431
1432
1433
1434
1435
1436
1437
1438static inline int skb_tailroom(const struct sk_buff *skb)
1439{
1440 return skb_is_nonlinear(skb) ? 0 : skb->end - skb->tail;
1441}
1442
1443
1444
1445
1446
1447
1448
1449
1450static inline int skb_availroom(const struct sk_buff *skb)
1451{
1452 if (skb_is_nonlinear(skb))
1453 return 0;
1454
1455 return skb->end - skb->tail - skb->reserved_tailroom;
1456}
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466static inline void skb_reserve(struct sk_buff *skb, int len)
1467{
1468 skb->data += len;
1469 skb->tail += len;
1470}
1471
1472static inline void skb_reset_inner_headers(struct sk_buff *skb)
1473{
1474 skb->inner_network_header = skb->network_header;
1475 skb->inner_transport_header = skb->transport_header;
1476}
1477
1478static inline void skb_reset_mac_len(struct sk_buff *skb)
1479{
1480 skb->mac_len = skb->network_header - skb->mac_header;
1481}
1482
1483#ifdef NET_SKBUFF_DATA_USES_OFFSET
1484static inline unsigned char *skb_inner_transport_header(const struct sk_buff
1485 *skb)
1486{
1487 return skb->head + skb->inner_transport_header;
1488}
1489
1490static inline void skb_reset_inner_transport_header(struct sk_buff *skb)
1491{
1492 skb->inner_transport_header = skb->data - skb->head;
1493}
1494
1495static inline void skb_set_inner_transport_header(struct sk_buff *skb,
1496 const int offset)
1497{
1498 skb_reset_inner_transport_header(skb);
1499 skb->inner_transport_header += offset;
1500}
1501
1502static inline unsigned char *skb_inner_network_header(const struct sk_buff *skb)
1503{
1504 return skb->head + skb->inner_network_header;
1505}
1506
1507static inline void skb_reset_inner_network_header(struct sk_buff *skb)
1508{
1509 skb->inner_network_header = skb->data - skb->head;
1510}
1511
1512static inline void skb_set_inner_network_header(struct sk_buff *skb,
1513 const int offset)
1514{
1515 skb_reset_inner_network_header(skb);
1516 skb->inner_network_header += offset;
1517}
1518
1519static inline bool skb_transport_header_was_set(const struct sk_buff *skb)
1520{
1521 return skb->transport_header != ~0U;
1522}
1523
1524static inline unsigned char *skb_transport_header(const struct sk_buff *skb)
1525{
1526 return skb->head + skb->transport_header;
1527}
1528
1529static inline void skb_reset_transport_header(struct sk_buff *skb)
1530{
1531 skb->transport_header = skb->data - skb->head;
1532}
1533
1534static inline void skb_set_transport_header(struct sk_buff *skb,
1535 const int offset)
1536{
1537 skb_reset_transport_header(skb);
1538 skb->transport_header += offset;
1539}
1540
1541static inline unsigned char *skb_network_header(const struct sk_buff *skb)
1542{
1543 return skb->head + skb->network_header;
1544}
1545
1546static inline void skb_reset_network_header(struct sk_buff *skb)
1547{
1548 skb->network_header = skb->data - skb->head;
1549}
1550
1551static inline void skb_set_network_header(struct sk_buff *skb, const int offset)
1552{
1553 skb_reset_network_header(skb);
1554 skb->network_header += offset;
1555}
1556
1557static inline unsigned char *skb_mac_header(const struct sk_buff *skb)
1558{
1559 return skb->head + skb->mac_header;
1560}
1561
1562static inline int skb_mac_header_was_set(const struct sk_buff *skb)
1563{
1564 return skb->mac_header != ~0U;
1565}
1566
1567static inline void skb_reset_mac_header(struct sk_buff *skb)
1568{
1569 skb->mac_header = skb->data - skb->head;
1570}
1571
1572static inline void skb_set_mac_header(struct sk_buff *skb, const int offset)
1573{
1574 skb_reset_mac_header(skb);
1575 skb->mac_header += offset;
1576}
1577
1578#else
1579static inline unsigned char *skb_inner_transport_header(const struct sk_buff
1580 *skb)
1581{
1582 return skb->inner_transport_header;
1583}
1584
1585static inline void skb_reset_inner_transport_header(struct sk_buff *skb)
1586{
1587 skb->inner_transport_header = skb->data;
1588}
1589
1590static inline void skb_set_inner_transport_header(struct sk_buff *skb,
1591 const int offset)
1592{
1593 skb->inner_transport_header = skb->data + offset;
1594}
1595
1596static inline unsigned char *skb_inner_network_header(const struct sk_buff *skb)
1597{
1598 return skb->inner_network_header;
1599}
1600
1601static inline void skb_reset_inner_network_header(struct sk_buff *skb)
1602{
1603 skb->inner_network_header = skb->data;
1604}
1605
1606static inline void skb_set_inner_network_header(struct sk_buff *skb,
1607 const int offset)
1608{
1609 skb->inner_network_header = skb->data + offset;
1610}
1611
1612static inline bool skb_transport_header_was_set(const struct sk_buff *skb)
1613{
1614 return skb->transport_header != NULL;
1615}
1616
1617static inline unsigned char *skb_transport_header(const struct sk_buff *skb)
1618{
1619 return skb->transport_header;
1620}
1621
1622static inline void skb_reset_transport_header(struct sk_buff *skb)
1623{
1624 skb->transport_header = skb->data;
1625}
1626
1627static inline void skb_set_transport_header(struct sk_buff *skb,
1628 const int offset)
1629{
1630 skb->transport_header = skb->data + offset;
1631}
1632
1633static inline unsigned char *skb_network_header(const struct sk_buff *skb)
1634{
1635 return skb->network_header;
1636}
1637
1638static inline void skb_reset_network_header(struct sk_buff *skb)
1639{
1640 skb->network_header = skb->data;
1641}
1642
1643static inline void skb_set_network_header(struct sk_buff *skb, const int offset)
1644{
1645 skb->network_header = skb->data + offset;
1646}
1647
1648static inline unsigned char *skb_mac_header(const struct sk_buff *skb)
1649{
1650 return skb->mac_header;
1651}
1652
1653static inline int skb_mac_header_was_set(const struct sk_buff *skb)
1654{
1655 return skb->mac_header != NULL;
1656}
1657
1658static inline void skb_reset_mac_header(struct sk_buff *skb)
1659{
1660 skb->mac_header = skb->data;
1661}
1662
1663static inline void skb_set_mac_header(struct sk_buff *skb, const int offset)
1664{
1665 skb->mac_header = skb->data + offset;
1666}
1667#endif
1668
1669static inline void skb_mac_header_rebuild(struct sk_buff *skb)
1670{
1671 if (skb_mac_header_was_set(skb)) {
1672 const unsigned char *old_mac = skb_mac_header(skb);
1673
1674 skb_set_mac_header(skb, -skb->mac_len);
1675 memmove(skb_mac_header(skb), old_mac, skb->mac_len);
1676 }
1677}
1678
1679static inline int skb_checksum_start_offset(const struct sk_buff *skb)
1680{
1681 return skb->csum_start - skb_headroom(skb);
1682}
1683
1684static inline int skb_transport_offset(const struct sk_buff *skb)
1685{
1686 return skb_transport_header(skb) - skb->data;
1687}
1688
1689static inline u32 skb_network_header_len(const struct sk_buff *skb)
1690{
1691 return skb->transport_header - skb->network_header;
1692}
1693
1694static inline u32 skb_inner_network_header_len(const struct sk_buff *skb)
1695{
1696 return skb->inner_transport_header - skb->inner_network_header;
1697}
1698
1699static inline int skb_network_offset(const struct sk_buff *skb)
1700{
1701 return skb_network_header(skb) - skb->data;
1702}
1703
1704static inline int skb_inner_network_offset(const struct sk_buff *skb)
1705{
1706 return skb_inner_network_header(skb) - skb->data;
1707}
1708
1709static inline int pskb_network_may_pull(struct sk_buff *skb, unsigned int len)
1710{
1711 return pskb_may_pull(skb, skb_network_offset(skb) + len);
1712}
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734#ifndef NET_IP_ALIGN
1735#define NET_IP_ALIGN 2
1736#endif
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758#ifndef NET_SKB_PAD
1759#define NET_SKB_PAD max(32, L1_CACHE_BYTES)
1760#endif
1761
1762extern int ___pskb_trim(struct sk_buff *skb, unsigned int len);
1763
1764static inline void __skb_trim(struct sk_buff *skb, unsigned int len)
1765{
1766 if (unlikely(skb_is_nonlinear(skb))) {
1767 WARN_ON(1);
1768 return;
1769 }
1770 skb->len = len;
1771 skb_set_tail_pointer(skb, len);
1772}
1773
1774extern void skb_trim(struct sk_buff *skb, unsigned int len);
1775
1776static inline int __pskb_trim(struct sk_buff *skb, unsigned int len)
1777{
1778 if (skb->data_len)
1779 return ___pskb_trim(skb, len);
1780 __skb_trim(skb, len);
1781 return 0;
1782}
1783
1784static inline int pskb_trim(struct sk_buff *skb, unsigned int len)
1785{
1786 return (len < skb->len) ? __pskb_trim(skb, len) : 0;
1787}
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798static inline void pskb_trim_unique(struct sk_buff *skb, unsigned int len)
1799{
1800 int err = pskb_trim(skb, len);
1801 BUG_ON(err);
1802}
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812static inline void skb_orphan(struct sk_buff *skb)
1813{
1814 if (skb->destructor)
1815 skb->destructor(skb);
1816 skb->destructor = NULL;
1817 skb->sk = NULL;
1818}
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829static inline int skb_orphan_frags(struct sk_buff *skb, gfp_t gfp_mask)
1830{
1831 if (likely(!(skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY)))
1832 return 0;
1833 return skb_copy_ubufs(skb, gfp_mask);
1834}
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844extern void skb_queue_purge(struct sk_buff_head *list);
1845static inline void __skb_queue_purge(struct sk_buff_head *list)
1846{
1847 struct sk_buff *skb;
1848 while ((skb = __skb_dequeue(list)) != NULL)
1849 kfree_skb(skb);
1850}
1851
1852#define NETDEV_FRAG_PAGE_MAX_ORDER get_order(32768)
1853#define NETDEV_FRAG_PAGE_MAX_SIZE (PAGE_SIZE << NETDEV_FRAG_PAGE_MAX_ORDER)
1854#define NETDEV_PAGECNT_MAX_BIAS NETDEV_FRAG_PAGE_MAX_SIZE
1855
1856extern void *netdev_alloc_frag(unsigned int fragsz);
1857
1858extern struct sk_buff *__netdev_alloc_skb(struct net_device *dev,
1859 unsigned int length,
1860 gfp_t gfp_mask);
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875static inline struct sk_buff *netdev_alloc_skb(struct net_device *dev,
1876 unsigned int length)
1877{
1878 return __netdev_alloc_skb(dev, length, GFP_ATOMIC);
1879}
1880
1881
1882static inline struct sk_buff *__dev_alloc_skb(unsigned int length,
1883 gfp_t gfp_mask)
1884{
1885 return __netdev_alloc_skb(NULL, length, gfp_mask);
1886}
1887
1888
1889static inline struct sk_buff *dev_alloc_skb(unsigned int length)
1890{
1891 return netdev_alloc_skb(NULL, length);
1892}
1893
1894
1895static inline struct sk_buff *__netdev_alloc_skb_ip_align(struct net_device *dev,
1896 unsigned int length, gfp_t gfp)
1897{
1898 struct sk_buff *skb = __netdev_alloc_skb(dev, length + NET_IP_ALIGN, gfp);
1899
1900 if (NET_IP_ALIGN && skb)
1901 skb_reserve(skb, NET_IP_ALIGN);
1902 return skb;
1903}
1904
1905static inline struct sk_buff *netdev_alloc_skb_ip_align(struct net_device *dev,
1906 unsigned int length)
1907{
1908 return __netdev_alloc_skb_ip_align(dev, length, GFP_ATOMIC);
1909}
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921static inline struct page *__skb_alloc_pages(gfp_t gfp_mask,
1922 struct sk_buff *skb,
1923 unsigned int order)
1924{
1925 struct page *page;
1926
1927 gfp_mask |= __GFP_COLD;
1928
1929 if (!(gfp_mask & __GFP_NOMEMALLOC))
1930 gfp_mask |= __GFP_MEMALLOC;
1931
1932 page = alloc_pages_node(NUMA_NO_NODE, gfp_mask, order);
1933 if (skb && page && page->pfmemalloc)
1934 skb->pfmemalloc = true;
1935
1936 return page;
1937}
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948static inline struct page *__skb_alloc_page(gfp_t gfp_mask,
1949 struct sk_buff *skb)
1950{
1951 return __skb_alloc_pages(gfp_mask, skb, 0);
1952}
1953
1954
1955
1956
1957
1958
1959static inline void skb_propagate_pfmemalloc(struct page *page,
1960 struct sk_buff *skb)
1961{
1962 if (page && page->pfmemalloc)
1963 skb->pfmemalloc = true;
1964}
1965
1966
1967
1968
1969
1970
1971
1972static inline struct page *skb_frag_page(const skb_frag_t *frag)
1973{
1974 return frag->page.p;
1975}
1976
1977
1978
1979
1980
1981
1982
1983static inline void __skb_frag_ref(skb_frag_t *frag)
1984{
1985 get_page(skb_frag_page(frag));
1986}
1987
1988
1989
1990
1991
1992
1993
1994
1995static inline void skb_frag_ref(struct sk_buff *skb, int f)
1996{
1997 __skb_frag_ref(&skb_shinfo(skb)->frags[f]);
1998}
1999
2000
2001
2002
2003
2004
2005
2006static inline void __skb_frag_unref(skb_frag_t *frag)
2007{
2008 put_page(skb_frag_page(frag));
2009}
2010
2011
2012
2013
2014
2015
2016
2017
2018static inline void skb_frag_unref(struct sk_buff *skb, int f)
2019{
2020 __skb_frag_unref(&skb_shinfo(skb)->frags[f]);
2021}
2022
2023
2024
2025
2026
2027
2028
2029
2030static inline void *skb_frag_address(const skb_frag_t *frag)
2031{
2032 return page_address(skb_frag_page(frag)) + frag->page_offset;
2033}
2034
2035
2036
2037
2038
2039
2040
2041
2042static inline void *skb_frag_address_safe(const skb_frag_t *frag)
2043{
2044 void *ptr = page_address(skb_frag_page(frag));
2045 if (unlikely(!ptr))
2046 return NULL;
2047
2048 return ptr + frag->page_offset;
2049}
2050
2051
2052
2053
2054
2055
2056
2057
2058static inline void __skb_frag_set_page(skb_frag_t *frag, struct page *page)
2059{
2060 frag->page.p = page;
2061}
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071static inline void skb_frag_set_page(struct sk_buff *skb, int f,
2072 struct page *page)
2073{
2074 __skb_frag_set_page(&skb_shinfo(skb)->frags[f], page);
2075}
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088static inline dma_addr_t skb_frag_dma_map(struct device *dev,
2089 const skb_frag_t *frag,
2090 size_t offset, size_t size,
2091 enum dma_data_direction dir)
2092{
2093 return dma_map_page(dev, skb_frag_page(frag),
2094 frag->page_offset + offset, size, dir);
2095}
2096
2097static inline struct sk_buff *pskb_copy(struct sk_buff *skb,
2098 gfp_t gfp_mask)
2099{
2100 return __pskb_copy(skb, skb_headroom(skb), gfp_mask);
2101}
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111static inline int skb_clone_writable(const struct sk_buff *skb, unsigned int len)
2112{
2113 return !skb_header_cloned(skb) &&
2114 skb_headroom(skb) + len <= skb->hdr_len;
2115}
2116
2117static inline int __skb_cow(struct sk_buff *skb, unsigned int headroom,
2118 int cloned)
2119{
2120 int delta = 0;
2121
2122 if (headroom > skb_headroom(skb))
2123 delta = headroom - skb_headroom(skb);
2124
2125 if (delta || cloned)
2126 return pskb_expand_head(skb, ALIGN(delta, NET_SKB_PAD), 0,
2127 GFP_ATOMIC);
2128 return 0;
2129}
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143static inline int skb_cow(struct sk_buff *skb, unsigned int headroom)
2144{
2145 return __skb_cow(skb, headroom, skb_cloned(skb));
2146}
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158static inline int skb_cow_head(struct sk_buff *skb, unsigned int headroom)
2159{
2160 return __skb_cow(skb, headroom, skb_header_cloned(skb));
2161}
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174static inline int skb_padto(struct sk_buff *skb, unsigned int len)
2175{
2176 unsigned int size = skb->len;
2177 if (likely(size >= len))
2178 return 0;
2179 return skb_pad(skb, len - size);
2180}
2181
2182static inline int skb_add_data(struct sk_buff *skb,
2183 char __user *from, int copy)
2184{
2185 const int off = skb->len;
2186
2187 if (skb->ip_summed == CHECKSUM_NONE) {
2188 int err = 0;
2189 __wsum csum = csum_and_copy_from_user(from, skb_put(skb, copy),
2190 copy, 0, &err);
2191 if (!err) {
2192 skb->csum = csum_block_add(skb->csum, csum, off);
2193 return 0;
2194 }
2195 } else if (!copy_from_user(skb_put(skb, copy), from, copy))
2196 return 0;
2197
2198 __skb_trim(skb, off);
2199 return -EFAULT;
2200}
2201
2202static inline bool skb_can_coalesce(struct sk_buff *skb, int i,
2203 const struct page *page, int off)
2204{
2205 if (i) {
2206 const struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[i - 1];
2207
2208 return page == skb_frag_page(frag) &&
2209 off == frag->page_offset + skb_frag_size(frag);
2210 }
2211 return false;
2212}
2213
2214static inline int __skb_linearize(struct sk_buff *skb)
2215{
2216 return __pskb_pull_tail(skb, skb->data_len) ? 0 : -ENOMEM;
2217}
2218
2219
2220
2221
2222
2223
2224
2225
2226static inline int skb_linearize(struct sk_buff *skb)
2227{
2228 return skb_is_nonlinear(skb) ? __skb_linearize(skb) : 0;
2229}
2230
2231
2232
2233
2234
2235
2236
2237
2238static inline bool skb_has_shared_frag(const struct sk_buff *skb)
2239{
2240 return skb_is_nonlinear(skb) &&
2241 skb_shinfo(skb)->tx_flags & SKBTX_SHARED_FRAG;
2242}
2243
2244
2245
2246
2247
2248
2249
2250
2251static inline int skb_linearize_cow(struct sk_buff *skb)
2252{
2253 return skb_is_nonlinear(skb) || skb_cloned(skb) ?
2254 __skb_linearize(skb) : 0;
2255}
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268static inline void skb_postpull_rcsum(struct sk_buff *skb,
2269 const void *start, unsigned int len)
2270{
2271 if (skb->ip_summed == CHECKSUM_COMPLETE)
2272 skb->csum = csum_sub(skb->csum, csum_partial(start, len, 0));
2273}
2274
2275unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len);
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286static inline int pskb_trim_rcsum(struct sk_buff *skb, unsigned int len)
2287{
2288 if (likely(len >= skb->len))
2289 return 0;
2290 if (skb->ip_summed == CHECKSUM_COMPLETE)
2291 skb->ip_summed = CHECKSUM_NONE;
2292 return __pskb_trim(skb, len);
2293}
2294
2295#define skb_queue_walk(queue, skb) \
2296 for (skb = (queue)->next; \
2297 skb != (struct sk_buff *)(queue); \
2298 skb = skb->next)
2299
2300#define skb_queue_walk_safe(queue, skb, tmp) \
2301 for (skb = (queue)->next, tmp = skb->next; \
2302 skb != (struct sk_buff *)(queue); \
2303 skb = tmp, tmp = skb->next)
2304
2305#define skb_queue_walk_from(queue, skb) \
2306 for (; skb != (struct sk_buff *)(queue); \
2307 skb = skb->next)
2308
2309#define skb_queue_walk_from_safe(queue, skb, tmp) \
2310 for (tmp = skb->next; \
2311 skb != (struct sk_buff *)(queue); \
2312 skb = tmp, tmp = skb->next)
2313
2314#define skb_queue_reverse_walk(queue, skb) \
2315 for (skb = (queue)->prev; \
2316 skb != (struct sk_buff *)(queue); \
2317 skb = skb->prev)
2318
2319#define skb_queue_reverse_walk_safe(queue, skb, tmp) \
2320 for (skb = (queue)->prev, tmp = skb->prev; \
2321 skb != (struct sk_buff *)(queue); \
2322 skb = tmp, tmp = skb->prev)
2323
2324#define skb_queue_reverse_walk_from_safe(queue, skb, tmp) \
2325 for (tmp = skb->prev; \
2326 skb != (struct sk_buff *)(queue); \
2327 skb = tmp, tmp = skb->prev)
2328
2329static inline bool skb_has_frag_list(const struct sk_buff *skb)
2330{
2331 return skb_shinfo(skb)->frag_list != NULL;
2332}
2333
2334static inline void skb_frag_list_init(struct sk_buff *skb)
2335{
2336 skb_shinfo(skb)->frag_list = NULL;
2337}
2338
2339static inline void skb_frag_add_head(struct sk_buff *skb, struct sk_buff *frag)
2340{
2341 frag->next = skb_shinfo(skb)->frag_list;
2342 skb_shinfo(skb)->frag_list = frag;
2343}
2344
2345#define skb_walk_frags(skb, iter) \
2346 for (iter = skb_shinfo(skb)->frag_list; iter; iter = iter->next)
2347
2348extern struct sk_buff *__skb_recv_datagram(struct sock *sk, unsigned flags,
2349 int *peeked, int *off, int *err);
2350extern struct sk_buff *skb_recv_datagram(struct sock *sk, unsigned flags,
2351 int noblock, int *err);
2352extern unsigned int datagram_poll(struct file *file, struct socket *sock,
2353 struct poll_table_struct *wait);
2354extern int skb_copy_datagram_iovec(const struct sk_buff *from,
2355 int offset, struct iovec *to,
2356 int size);
2357extern int skb_copy_and_csum_datagram_iovec(struct sk_buff *skb,
2358 int hlen,
2359 struct iovec *iov);
2360extern int skb_copy_datagram_from_iovec(struct sk_buff *skb,
2361 int offset,
2362 const struct iovec *from,
2363 int from_offset,
2364 int len);
2365extern int skb_copy_datagram_const_iovec(const struct sk_buff *from,
2366 int offset,
2367 const struct iovec *to,
2368 int to_offset,
2369 int size);
2370extern void skb_free_datagram(struct sock *sk, struct sk_buff *skb);
2371extern void skb_free_datagram_locked(struct sock *sk,
2372 struct sk_buff *skb);
2373extern int skb_kill_datagram(struct sock *sk, struct sk_buff *skb,
2374 unsigned int flags);
2375extern __wsum skb_checksum(const struct sk_buff *skb, int offset,
2376 int len, __wsum csum);
2377extern int skb_copy_bits(const struct sk_buff *skb, int offset,
2378 void *to, int len);
2379extern int skb_store_bits(struct sk_buff *skb, int offset,
2380 const void *from, int len);
2381extern __wsum skb_copy_and_csum_bits(const struct sk_buff *skb,
2382 int offset, u8 *to, int len,
2383 __wsum csum);
2384extern int skb_splice_bits(struct sk_buff *skb,
2385 unsigned int offset,
2386 struct pipe_inode_info *pipe,
2387 unsigned int len,
2388 unsigned int flags);
2389extern void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to);
2390extern void skb_split(struct sk_buff *skb,
2391 struct sk_buff *skb1, const u32 len);
2392extern int skb_shift(struct sk_buff *tgt, struct sk_buff *skb,
2393 int shiftlen);
2394
2395extern struct sk_buff *skb_segment(struct sk_buff *skb,
2396 netdev_features_t features);
2397
2398static inline void *skb_header_pointer(const struct sk_buff *skb, int offset,
2399 int len, void *buffer)
2400{
2401 int hlen = skb_headlen(skb);
2402
2403 if (hlen - offset >= len)
2404 return skb->data + offset;
2405
2406 if (skb_copy_bits(skb, offset, buffer, len) < 0)
2407 return NULL;
2408
2409 return buffer;
2410}
2411
2412static inline void skb_copy_from_linear_data(const struct sk_buff *skb,
2413 void *to,
2414 const unsigned int len)
2415{
2416 memcpy(to, skb->data, len);
2417}
2418
2419static inline void skb_copy_from_linear_data_offset(const struct sk_buff *skb,
2420 const int offset, void *to,
2421 const unsigned int len)
2422{
2423 memcpy(to, skb->data + offset, len);
2424}
2425
2426static inline void skb_copy_to_linear_data(struct sk_buff *skb,
2427 const void *from,
2428 const unsigned int len)
2429{
2430 memcpy(skb->data, from, len);
2431}
2432
2433static inline void skb_copy_to_linear_data_offset(struct sk_buff *skb,
2434 const int offset,
2435 const void *from,
2436 const unsigned int len)
2437{
2438 memcpy(skb->data + offset, from, len);
2439}
2440
2441extern void skb_init(void);
2442
2443static inline ktime_t skb_get_ktime(const struct sk_buff *skb)
2444{
2445 return skb->tstamp;
2446}
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457static inline void skb_get_timestamp(const struct sk_buff *skb,
2458 struct timeval *stamp)
2459{
2460 *stamp = ktime_to_timeval(skb->tstamp);
2461}
2462
2463static inline void skb_get_timestampns(const struct sk_buff *skb,
2464 struct timespec *stamp)
2465{
2466 *stamp = ktime_to_timespec(skb->tstamp);
2467}
2468
2469static inline void __net_timestamp(struct sk_buff *skb)
2470{
2471 skb->tstamp = ktime_get_real();
2472}
2473
2474static inline ktime_t net_timedelta(ktime_t t)
2475{
2476 return ktime_sub(ktime_get_real(), t);
2477}
2478
2479static inline ktime_t net_invalid_timestamp(void)
2480{
2481 return ktime_set(0, 0);
2482}
2483
2484extern void skb_timestamping_init(void);
2485
2486#ifdef CONFIG_NETWORK_PHY_TIMESTAMPING
2487
2488extern void skb_clone_tx_timestamp(struct sk_buff *skb);
2489extern bool skb_defer_rx_timestamp(struct sk_buff *skb);
2490
2491#else
2492
2493static inline void skb_clone_tx_timestamp(struct sk_buff *skb)
2494{
2495}
2496
2497static inline bool skb_defer_rx_timestamp(struct sk_buff *skb)
2498{
2499 return false;
2500}
2501
2502#endif
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516void skb_complete_tx_timestamp(struct sk_buff *skb,
2517 struct skb_shared_hwtstamps *hwtstamps);
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530extern void skb_tstamp_tx(struct sk_buff *orig_skb,
2531 struct skb_shared_hwtstamps *hwtstamps);
2532
2533static inline void sw_tx_timestamp(struct sk_buff *skb)
2534{
2535 if (skb_shinfo(skb)->tx_flags & SKBTX_SW_TSTAMP &&
2536 !(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS))
2537 skb_tstamp_tx(skb, NULL);
2538}
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548static inline void skb_tx_timestamp(struct sk_buff *skb)
2549{
2550 skb_clone_tx_timestamp(skb);
2551 sw_tx_timestamp(skb);
2552}
2553
2554
2555
2556
2557
2558
2559
2560
2561void skb_complete_wifi_ack(struct sk_buff *skb, bool acked);
2562
2563extern __sum16 __skb_checksum_complete_head(struct sk_buff *skb, int len);
2564extern __sum16 __skb_checksum_complete(struct sk_buff *skb);
2565
2566static inline int skb_csum_unnecessary(const struct sk_buff *skb)
2567{
2568 return skb->ip_summed & CHECKSUM_UNNECESSARY;
2569}
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587static inline __sum16 skb_checksum_complete(struct sk_buff *skb)
2588{
2589 return skb_csum_unnecessary(skb) ?
2590 0 : __skb_checksum_complete(skb);
2591}
2592
2593#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
2594extern void nf_conntrack_destroy(struct nf_conntrack *nfct);
2595static inline void nf_conntrack_put(struct nf_conntrack *nfct)
2596{
2597 if (nfct && atomic_dec_and_test(&nfct->use))
2598 nf_conntrack_destroy(nfct);
2599}
2600static inline void nf_conntrack_get(struct nf_conntrack *nfct)
2601{
2602 if (nfct)
2603 atomic_inc(&nfct->use);
2604}
2605#endif
2606#ifdef NET_SKBUFF_NF_DEFRAG_NEEDED
2607static inline void nf_conntrack_get_reasm(struct sk_buff *skb)
2608{
2609 if (skb)
2610 atomic_inc(&skb->users);
2611}
2612static inline void nf_conntrack_put_reasm(struct sk_buff *skb)
2613{
2614 if (skb)
2615 kfree_skb(skb);
2616}
2617#endif
2618#ifdef CONFIG_BRIDGE_NETFILTER
2619static inline void nf_bridge_put(struct nf_bridge_info *nf_bridge)
2620{
2621 if (nf_bridge && atomic_dec_and_test(&nf_bridge->use))
2622 kfree(nf_bridge);
2623}
2624static inline void nf_bridge_get(struct nf_bridge_info *nf_bridge)
2625{
2626 if (nf_bridge)
2627 atomic_inc(&nf_bridge->use);
2628}
2629#endif
2630static inline void nf_reset(struct sk_buff *skb)
2631{
2632#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
2633 nf_conntrack_put(skb->nfct);
2634 skb->nfct = NULL;
2635#endif
2636#ifdef NET_SKBUFF_NF_DEFRAG_NEEDED
2637 nf_conntrack_put_reasm(skb->nfct_reasm);
2638 skb->nfct_reasm = NULL;
2639#endif
2640#ifdef CONFIG_BRIDGE_NETFILTER
2641 nf_bridge_put(skb->nf_bridge);
2642 skb->nf_bridge = NULL;
2643#endif
2644}
2645
2646static inline void nf_reset_trace(struct sk_buff *skb)
2647{
2648#if IS_ENABLED(CONFIG_NETFILTER_XT_TARGET_TRACE)
2649 skb->nf_trace = 0;
2650#endif
2651}
2652
2653
2654static inline void __nf_copy(struct sk_buff *dst, const struct sk_buff *src)
2655{
2656#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
2657 dst->nfct = src->nfct;
2658 nf_conntrack_get(src->nfct);
2659 dst->nfctinfo = src->nfctinfo;
2660#endif
2661#ifdef NET_SKBUFF_NF_DEFRAG_NEEDED
2662 dst->nfct_reasm = src->nfct_reasm;
2663 nf_conntrack_get_reasm(src->nfct_reasm);
2664#endif
2665#ifdef CONFIG_BRIDGE_NETFILTER
2666 dst->nf_bridge = src->nf_bridge;
2667 nf_bridge_get(src->nf_bridge);
2668#endif
2669}
2670
2671static inline void nf_copy(struct sk_buff *dst, const struct sk_buff *src)
2672{
2673#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
2674 nf_conntrack_put(dst->nfct);
2675#endif
2676#ifdef NET_SKBUFF_NF_DEFRAG_NEEDED
2677 nf_conntrack_put_reasm(dst->nfct_reasm);
2678#endif
2679#ifdef CONFIG_BRIDGE_NETFILTER
2680 nf_bridge_put(dst->nf_bridge);
2681#endif
2682 __nf_copy(dst, src);
2683}
2684
2685#ifdef CONFIG_NETWORK_SECMARK
2686static inline void skb_copy_secmark(struct sk_buff *to, const struct sk_buff *from)
2687{
2688 to->secmark = from->secmark;
2689}
2690
2691static inline void skb_init_secmark(struct sk_buff *skb)
2692{
2693 skb->secmark = 0;
2694}
2695#else
2696static inline void skb_copy_secmark(struct sk_buff *to, const struct sk_buff *from)
2697{ }
2698
2699static inline void skb_init_secmark(struct sk_buff *skb)
2700{ }
2701#endif
2702
2703static inline void skb_set_queue_mapping(struct sk_buff *skb, u16 queue_mapping)
2704{
2705 skb->queue_mapping = queue_mapping;
2706}
2707
2708static inline u16 skb_get_queue_mapping(const struct sk_buff *skb)
2709{
2710 return skb->queue_mapping;
2711}
2712
2713static inline void skb_copy_queue_mapping(struct sk_buff *to, const struct sk_buff *from)
2714{
2715 to->queue_mapping = from->queue_mapping;
2716}
2717
2718static inline void skb_record_rx_queue(struct sk_buff *skb, u16 rx_queue)
2719{
2720 skb->queue_mapping = rx_queue + 1;
2721}
2722
2723static inline u16 skb_get_rx_queue(const struct sk_buff *skb)
2724{
2725 return skb->queue_mapping - 1;
2726}
2727
2728static inline bool skb_rx_queue_recorded(const struct sk_buff *skb)
2729{
2730 return skb->queue_mapping != 0;
2731}
2732
2733extern u16 __skb_tx_hash(const struct net_device *dev,
2734 const struct sk_buff *skb,
2735 unsigned int num_tx_queues);
2736
2737#ifdef CONFIG_XFRM
2738static inline struct sec_path *skb_sec_path(struct sk_buff *skb)
2739{
2740 return skb->sp;
2741}
2742#else
2743static inline struct sec_path *skb_sec_path(struct sk_buff *skb)
2744{
2745 return NULL;
2746}
2747#endif
2748
2749
2750
2751
2752
2753struct skb_gso_cb {
2754 int mac_offset;
2755};
2756#define SKB_GSO_CB(skb) ((struct skb_gso_cb *)(skb)->cb)
2757
2758static inline int skb_tnl_header_len(const struct sk_buff *inner_skb)
2759{
2760 return (skb_mac_header(inner_skb) - inner_skb->head) -
2761 SKB_GSO_CB(inner_skb)->mac_offset;
2762}
2763
2764static inline bool skb_is_gso(const struct sk_buff *skb)
2765{
2766 return skb_shinfo(skb)->gso_size;
2767}
2768
2769static inline bool skb_is_gso_v6(const struct sk_buff *skb)
2770{
2771 return skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6;
2772}
2773
2774extern void __skb_warn_lro_forwarding(const struct sk_buff *skb);
2775
2776static inline bool skb_warn_if_lro(const struct sk_buff *skb)
2777{
2778
2779
2780 const struct skb_shared_info *shinfo = skb_shinfo(skb);
2781
2782 if (skb_is_nonlinear(skb) && shinfo->gso_size != 0 &&
2783 unlikely(shinfo->gso_type == 0)) {
2784 __skb_warn_lro_forwarding(skb);
2785 return true;
2786 }
2787 return false;
2788}
2789
2790static inline void skb_forward_csum(struct sk_buff *skb)
2791{
2792
2793 if (skb->ip_summed == CHECKSUM_COMPLETE)
2794 skb->ip_summed = CHECKSUM_NONE;
2795}
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805static inline void skb_checksum_none_assert(const struct sk_buff *skb)
2806{
2807#ifdef DEBUG
2808 BUG_ON(skb->ip_summed != CHECKSUM_NONE);
2809#endif
2810}
2811
2812bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off);
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823static inline bool skb_head_is_locked(const struct sk_buff *skb)
2824{
2825 return !skb->head_frag || skb_cloned(skb);
2826}
2827#endif
2828#endif
2829