compat: Detect and use struct nf_conntrack_zone.
[cascardo/ovs.git] / datapath / linux / compat / include / net / netfilter / nf_conntrack_zones.h
1 #ifndef _NF_CONNTRACK_ZONES_WRAPPER_H
2 #define _NF_CONNTRACK_ZONES_WRAPPER_H
3
4 #include <linux/version.h>
5
6 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,34)
7 #include_next <net/netfilter/nf_conntrack_zones.h>
8 #endif
9
10 #ifndef HAVE_NF_CT_ZONE_INIT
11
12 #include <linux/kconfig.h>
13 #include <linux/types.h>
14 #include <linux/netfilter/nf_conntrack_tuple_common.h>
15
16 #define NF_CT_DEFAULT_ZONE_ID   0
17
18 #define NF_CT_ZONE_DIR_ORIG     (1 << IP_CT_DIR_ORIGINAL)
19 #define NF_CT_ZONE_DIR_REPL     (1 << IP_CT_DIR_REPLY)
20
21 #define NF_CT_DEFAULT_ZONE_DIR  (NF_CT_ZONE_DIR_ORIG | NF_CT_ZONE_DIR_REPL)
22
23 #define NF_CT_FLAG_MARK  1
24
25 struct rpl_nf_conntrack_zone {
26         u16     id;
27         u8      flags;
28         u8      dir;
29 };
30 #define nf_conntrack_zone rpl_nf_conntrack_zone
31
32 extern const struct nf_conntrack_zone nf_ct_zone_dflt;
33
34 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
35 #include <net/netfilter/nf_conntrack_extend.h>
36
37 static inline const struct nf_conntrack_zone *
38 rpl_nf_ct_zone(const struct nf_conn *ct)
39 {
40         const struct nf_conntrack_zone *nf_ct_zone = NULL;
41
42 #ifdef CONFIG_NF_CONNTRACK_ZONES
43         nf_ct_zone = nf_ct_ext_find(ct, NF_CT_EXT_ZONE);
44 #endif
45         return nf_ct_zone ? nf_ct_zone : &nf_ct_zone_dflt;
46 }
47 #define nf_ct_zone rpl_nf_ct_zone
48
49 static inline const struct nf_conntrack_zone *
50 nf_ct_zone_init(struct nf_conntrack_zone *zone, u16 id, u8 dir, u8 flags)
51 {
52         zone->id = id;
53         zone->flags = flags;
54         zone->dir = dir;
55
56         return zone;
57 }
58
59 static inline int nf_ct_zone_add(struct nf_conn *ct, gfp_t flags,
60                                  const struct nf_conntrack_zone *info)
61 {
62 #ifdef CONFIG_NF_CONNTRACK_ZONES
63         struct nf_conntrack_zone *nf_ct_zone;
64
65         nf_ct_zone = nf_ct_ext_add(ct, NF_CT_EXT_ZONE, flags);
66         if (!nf_ct_zone)
67                 return -ENOMEM;
68
69         nf_ct_zone_init(nf_ct_zone, info->id, info->dir,
70                         info->flags);
71 #endif
72         return 0;
73 }
74
75 static inline bool nf_ct_zone_matches_dir(const struct nf_conntrack_zone *zone,
76                                           enum ip_conntrack_dir dir)
77 {
78         return zone->dir & (1 << dir);
79 }
80
81 static inline u16 nf_ct_zone_id(const struct nf_conntrack_zone *zone,
82                                 enum ip_conntrack_dir dir)
83 {
84         return nf_ct_zone_matches_dir(zone, dir) ?
85                zone->id : NF_CT_DEFAULT_ZONE_ID;
86 }
87
88 static inline bool nf_ct_zone_equal(const struct nf_conn *a,
89                                     const struct nf_conntrack_zone *b,
90                                     enum ip_conntrack_dir dir)
91 {
92         return nf_ct_zone_id(nf_ct_zone(a), dir) ==
93                nf_ct_zone_id(b, dir);
94 }
95
96 static inline bool nf_ct_zone_equal_any(const struct nf_conn *a,
97                                         const struct nf_conntrack_zone *b)
98 {
99         return nf_ct_zone(a)->id == b->id;
100 }
101 #endif /* IS_ENABLED(CONFIG_NF_CONNTRACK) */
102 #endif /* HAVE_NF_CT_ZONE_INIT */
103 #endif /* _NF_CONNTRACK_ZONES_WRAPPER_H */