xref: /linux/include/net/netfilter/nf_conntrack_zones.h (revision 976e3645923bdd2fe7893aae33fd7a21098bfb28)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _NF_CONNTRACK_ZONES_H
3 #define _NF_CONNTRACK_ZONES_H
4 
5 #include <linux/netfilter/nf_conntrack_zones_common.h>
6 #include <net/netfilter/nf_conntrack.h>
7 
8 static inline const struct nf_conntrack_zone *
nf_ct_zone(const struct nf_conn * ct)9 nf_ct_zone(const struct nf_conn *ct)
10 {
11 #ifdef CONFIG_NF_CONNTRACK_ZONES
12 	return &ct->zone;
13 #else
14 	return &nf_ct_zone_dflt;
15 #endif
16 }
17 
18 static inline const struct nf_conntrack_zone *
nf_ct_zone_init(struct nf_conntrack_zone * zone,u16 id,u8 dir,u8 flags)19 nf_ct_zone_init(struct nf_conntrack_zone *zone, u16 id, u8 dir, u8 flags)
20 {
21 	zone->id = id;
22 	zone->flags = flags;
23 	zone->dir = dir;
24 
25 	return zone;
26 }
27 
28 static inline const struct nf_conntrack_zone *
nf_ct_zone_tmpl(const struct nf_conn * tmpl,const struct sk_buff * skb,struct nf_conntrack_zone * tmp)29 nf_ct_zone_tmpl(const struct nf_conn *tmpl, const struct sk_buff *skb,
30 		struct nf_conntrack_zone *tmp)
31 {
32 #ifdef CONFIG_NF_CONNTRACK_ZONES
33 	if (!tmpl)
34 		return &nf_ct_zone_dflt;
35 
36 	if (tmpl->zone.flags & NF_CT_FLAG_MARK)
37 		return nf_ct_zone_init(tmp, skb->mark, tmpl->zone.dir, 0);
38 #endif
39 	return nf_ct_zone(tmpl);
40 }
41 
nf_ct_zone_add(struct nf_conn * ct,const struct nf_conntrack_zone * zone)42 static inline void nf_ct_zone_add(struct nf_conn *ct,
43 				  const struct nf_conntrack_zone *zone)
44 {
45 #ifdef CONFIG_NF_CONNTRACK_ZONES
46 	ct->zone = *zone;
47 #endif
48 }
49 
nf_ct_zone_matches_dir(const struct nf_conntrack_zone * zone,enum ip_conntrack_dir dir)50 static inline bool nf_ct_zone_matches_dir(const struct nf_conntrack_zone *zone,
51 					  enum ip_conntrack_dir dir)
52 {
53 	return zone->dir & (1 << dir);
54 }
55 
nf_ct_zone_id(const struct nf_conntrack_zone * zone,enum ip_conntrack_dir dir)56 static inline u16 nf_ct_zone_id(const struct nf_conntrack_zone *zone,
57 				enum ip_conntrack_dir dir)
58 {
59 #ifdef CONFIG_NF_CONNTRACK_ZONES
60 	return nf_ct_zone_matches_dir(zone, dir) ?
61 	       zone->id : NF_CT_DEFAULT_ZONE_ID;
62 #else
63 	return NF_CT_DEFAULT_ZONE_ID;
64 #endif
65 }
66 
nf_ct_zone_equal(const struct nf_conn * a,const struct nf_conntrack_zone * b,enum ip_conntrack_dir dir)67 static inline bool nf_ct_zone_equal(const struct nf_conn *a,
68 				    const struct nf_conntrack_zone *b,
69 				    enum ip_conntrack_dir dir)
70 {
71 #ifdef CONFIG_NF_CONNTRACK_ZONES
72 	return nf_ct_zone_id(nf_ct_zone(a), dir) ==
73 	       nf_ct_zone_id(b, dir);
74 #else
75 	return true;
76 #endif
77 }
78 
nf_ct_zone_equal_any(const struct nf_conn * a,const struct nf_conntrack_zone * b)79 static inline bool nf_ct_zone_equal_any(const struct nf_conn *a,
80 					const struct nf_conntrack_zone *b)
81 {
82 #ifdef CONFIG_NF_CONNTRACK_ZONES
83 	return nf_ct_zone(a)->id == b->id;
84 #else
85 	return true;
86 #endif
87 }
88 
89 #endif /* _NF_CONNTRACK_ZONES_H */
90