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