2 * Copyright (c) 2008-2009 Patrick McHardy <kaber@trash.net>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
8 * Development of this code funded by Astaro AG (http://www.astaro.com/)
11 #include <linux/kernel.h>
12 #include <linux/init.h>
13 #include <linux/module.h>
14 #include <linux/list.h>
15 #include <linux/rbtree.h>
16 #include <linux/netlink.h>
17 #include <linux/netfilter.h>
18 #include <linux/netfilter/nf_tables.h>
19 #include <net/netfilter/nf_tables.h>
21 static DEFINE_SPINLOCK(nft_rbtree_lock
);
27 struct nft_rbtree_elem
{
29 struct nft_set_ext ext
;
32 static bool nft_rbtree_interval_end(const struct nft_rbtree_elem
*rbe
)
34 return nft_set_ext_exists(&rbe
->ext
, NFT_SET_EXT_FLAGS
) &&
35 (*nft_set_ext_flags(&rbe
->ext
) & NFT_SET_ELEM_INTERVAL_END
);
38 static bool nft_rbtree_equal(const struct nft_set
*set
, const void *this,
39 const struct nft_rbtree_elem
*interval
)
41 return memcmp(this, nft_set_ext_key(&interval
->ext
), set
->klen
) == 0;
44 static bool nft_rbtree_lookup(const struct net
*net
, const struct nft_set
*set
,
45 const u32
*key
, const struct nft_set_ext
**ext
)
47 const struct nft_rbtree
*priv
= nft_set_priv(set
);
48 const struct nft_rbtree_elem
*rbe
, *interval
= NULL
;
49 u8 genmask
= nft_genmask_cur(net
);
50 const struct rb_node
*parent
;
54 spin_lock_bh(&nft_rbtree_lock
);
55 parent
= priv
->root
.rb_node
;
56 while (parent
!= NULL
) {
57 rbe
= rb_entry(parent
, struct nft_rbtree_elem
, node
);
59 this = nft_set_ext_key(&rbe
->ext
);
60 d
= memcmp(this, key
, set
->klen
);
62 parent
= parent
->rb_left
;
63 /* In case of adjacent ranges, we always see the high
64 * part of the range in first place, before the low one.
65 * So don't update interval if the keys are equal.
67 if (interval
&& nft_rbtree_equal(set
, this, interval
))
71 parent
= parent
->rb_right
;
73 if (!nft_set_elem_active(&rbe
->ext
, genmask
)) {
74 parent
= parent
->rb_left
;
77 if (nft_rbtree_interval_end(rbe
))
79 spin_unlock_bh(&nft_rbtree_lock
);
86 if (set
->flags
& NFT_SET_INTERVAL
&& interval
!= NULL
&&
87 nft_set_elem_active(&interval
->ext
, genmask
) &&
88 !nft_rbtree_interval_end(interval
)) {
89 spin_unlock_bh(&nft_rbtree_lock
);
90 *ext
= &interval
->ext
;
94 spin_unlock_bh(&nft_rbtree_lock
);
98 static int __nft_rbtree_insert(const struct net
*net
, const struct nft_set
*set
,
99 struct nft_rbtree_elem
*new)
101 struct nft_rbtree
*priv
= nft_set_priv(set
);
102 u8 genmask
= nft_genmask_next(net
);
103 struct nft_rbtree_elem
*rbe
;
104 struct rb_node
*parent
, **p
;
108 p
= &priv
->root
.rb_node
;
111 rbe
= rb_entry(parent
, struct nft_rbtree_elem
, node
);
112 d
= memcmp(nft_set_ext_key(&rbe
->ext
),
113 nft_set_ext_key(&new->ext
),
116 p
= &parent
->rb_left
;
118 p
= &parent
->rb_right
;
120 if (nft_set_elem_active(&rbe
->ext
, genmask
)) {
121 if (nft_rbtree_interval_end(rbe
) &&
122 !nft_rbtree_interval_end(new))
123 p
= &parent
->rb_left
;
124 else if (!nft_rbtree_interval_end(rbe
) &&
125 nft_rbtree_interval_end(new))
126 p
= &parent
->rb_right
;
132 rb_link_node(&new->node
, parent
, p
);
133 rb_insert_color(&new->node
, &priv
->root
);
137 static int nft_rbtree_insert(const struct net
*net
, const struct nft_set
*set
,
138 const struct nft_set_elem
*elem
)
140 struct nft_rbtree_elem
*rbe
= elem
->priv
;
143 spin_lock_bh(&nft_rbtree_lock
);
144 err
= __nft_rbtree_insert(net
, set
, rbe
);
145 spin_unlock_bh(&nft_rbtree_lock
);
150 static void nft_rbtree_remove(const struct nft_set
*set
,
151 const struct nft_set_elem
*elem
)
153 struct nft_rbtree
*priv
= nft_set_priv(set
);
154 struct nft_rbtree_elem
*rbe
= elem
->priv
;
156 spin_lock_bh(&nft_rbtree_lock
);
157 rb_erase(&rbe
->node
, &priv
->root
);
158 spin_unlock_bh(&nft_rbtree_lock
);
161 static void nft_rbtree_activate(const struct net
*net
,
162 const struct nft_set
*set
,
163 const struct nft_set_elem
*elem
)
165 struct nft_rbtree_elem
*rbe
= elem
->priv
;
167 nft_set_elem_change_active(net
, set
, &rbe
->ext
);
170 static void *nft_rbtree_deactivate(const struct net
*net
,
171 const struct nft_set
*set
,
172 const struct nft_set_elem
*elem
)
174 const struct nft_rbtree
*priv
= nft_set_priv(set
);
175 const struct rb_node
*parent
= priv
->root
.rb_node
;
176 struct nft_rbtree_elem
*rbe
, *this = elem
->priv
;
177 u8 genmask
= nft_genmask_next(net
);
180 while (parent
!= NULL
) {
181 rbe
= rb_entry(parent
, struct nft_rbtree_elem
, node
);
183 d
= memcmp(nft_set_ext_key(&rbe
->ext
), &elem
->key
.val
,
186 parent
= parent
->rb_left
;
188 parent
= parent
->rb_right
;
190 if (!nft_set_elem_active(&rbe
->ext
, genmask
)) {
191 parent
= parent
->rb_left
;
194 if (nft_rbtree_interval_end(rbe
) &&
195 !nft_rbtree_interval_end(this)) {
196 parent
= parent
->rb_left
;
198 } else if (!nft_rbtree_interval_end(rbe
) &&
199 nft_rbtree_interval_end(this)) {
200 parent
= parent
->rb_right
;
203 nft_set_elem_change_active(net
, set
, &rbe
->ext
);
210 static void nft_rbtree_walk(const struct nft_ctx
*ctx
,
211 const struct nft_set
*set
,
212 struct nft_set_iter
*iter
)
214 const struct nft_rbtree
*priv
= nft_set_priv(set
);
215 struct nft_rbtree_elem
*rbe
;
216 struct nft_set_elem elem
;
217 struct rb_node
*node
;
219 spin_lock_bh(&nft_rbtree_lock
);
220 for (node
= rb_first(&priv
->root
); node
!= NULL
; node
= rb_next(node
)) {
221 rbe
= rb_entry(node
, struct nft_rbtree_elem
, node
);
223 if (iter
->count
< iter
->skip
)
225 if (!nft_set_elem_active(&rbe
->ext
, iter
->genmask
))
230 iter
->err
= iter
->fn(ctx
, set
, iter
, &elem
);
232 spin_unlock_bh(&nft_rbtree_lock
);
238 spin_unlock_bh(&nft_rbtree_lock
);
241 static unsigned int nft_rbtree_privsize(const struct nlattr
* const nla
[])
243 return sizeof(struct nft_rbtree
);
246 static int nft_rbtree_init(const struct nft_set
*set
,
247 const struct nft_set_desc
*desc
,
248 const struct nlattr
* const nla
[])
250 struct nft_rbtree
*priv
= nft_set_priv(set
);
252 priv
->root
= RB_ROOT
;
256 static void nft_rbtree_destroy(const struct nft_set
*set
)
258 struct nft_rbtree
*priv
= nft_set_priv(set
);
259 struct nft_rbtree_elem
*rbe
;
260 struct rb_node
*node
;
262 while ((node
= priv
->root
.rb_node
) != NULL
) {
263 rb_erase(node
, &priv
->root
);
264 rbe
= rb_entry(node
, struct nft_rbtree_elem
, node
);
265 nft_set_elem_destroy(set
, rbe
);
269 static bool nft_rbtree_estimate(const struct nft_set_desc
*desc
, u32 features
,
270 struct nft_set_estimate
*est
)
274 nsize
= sizeof(struct nft_rbtree_elem
);
276 est
->size
= sizeof(struct nft_rbtree
) + desc
->size
* nsize
;
280 est
->class = NFT_SET_CLASS_O_LOG_N
;
285 static struct nft_set_ops nft_rbtree_ops __read_mostly
= {
286 .privsize
= nft_rbtree_privsize
,
287 .elemsize
= offsetof(struct nft_rbtree_elem
, ext
),
288 .estimate
= nft_rbtree_estimate
,
289 .init
= nft_rbtree_init
,
290 .destroy
= nft_rbtree_destroy
,
291 .insert
= nft_rbtree_insert
,
292 .remove
= nft_rbtree_remove
,
293 .deactivate
= nft_rbtree_deactivate
,
294 .activate
= nft_rbtree_activate
,
295 .lookup
= nft_rbtree_lookup
,
296 .walk
= nft_rbtree_walk
,
297 .features
= NFT_SET_INTERVAL
| NFT_SET_MAP
,
298 .owner
= THIS_MODULE
,
301 static int __init
nft_rbtree_module_init(void)
303 return nft_register_set(&nft_rbtree_ops
);
306 static void __exit
nft_rbtree_module_exit(void)
308 nft_unregister_set(&nft_rbtree_ops
);
311 module_init(nft_rbtree_module_init
);
312 module_exit(nft_rbtree_module_exit
);
314 MODULE_LICENSE("GPL");
315 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
316 MODULE_ALIAS_NFT_SET();