Commit | Line | Data |
---|---|---|
20a69341 PM |
1 | /* |
2 | * Copyright (c) 2008-2009 Patrick McHardy <kaber@trash.net> | |
3 | * | |
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. | |
7 | * | |
8 | * Development of this code funded by Astaro AG (http://www.astaro.com/) | |
9 | */ | |
10 | ||
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> | |
20 | ||
7632667d PNA |
21 | static DEFINE_SPINLOCK(nft_rbtree_lock); |
22 | ||
20a69341 PM |
23 | struct nft_rbtree { |
24 | struct rb_root root; | |
25 | }; | |
26 | ||
27 | struct nft_rbtree_elem { | |
28 | struct rb_node node; | |
fe2811eb | 29 | struct nft_set_ext ext; |
20a69341 PM |
30 | }; |
31 | ||
ef1d20e0 PNA |
32 | static bool nft_rbtree_interval_end(const struct nft_rbtree_elem *rbe) |
33 | { | |
34 | return nft_set_ext_exists(&rbe->ext, NFT_SET_EXT_FLAGS) && | |
35 | (*nft_set_ext_flags(&rbe->ext) & NFT_SET_ELEM_INTERVAL_END); | |
36 | } | |
cc02e457 | 37 | |
e701001e PNA |
38 | static bool nft_rbtree_equal(const struct nft_set *set, const void *this, |
39 | const struct nft_rbtree_elem *interval) | |
40 | { | |
41 | return memcmp(this, nft_set_ext_key(&interval->ext), set->klen) == 0; | |
42 | } | |
43 | ||
8cd8937a | 44 | static bool nft_rbtree_lookup(const struct nft_set *set, const u32 *key, |
b2832dd6 | 45 | const struct nft_set_ext **ext) |
20a69341 PM |
46 | { |
47 | const struct nft_rbtree *priv = nft_set_priv(set); | |
48 | const struct nft_rbtree_elem *rbe, *interval = NULL; | |
16c45eda | 49 | const struct rb_node *parent; |
cc02e457 | 50 | u8 genmask = nft_genmask_cur(read_pnet(&set->pnet)); |
e701001e | 51 | const void *this; |
20a69341 PM |
52 | int d; |
53 | ||
7632667d | 54 | spin_lock_bh(&nft_rbtree_lock); |
16c45eda | 55 | parent = priv->root.rb_node; |
20a69341 PM |
56 | while (parent != NULL) { |
57 | rbe = rb_entry(parent, struct nft_rbtree_elem, node); | |
58 | ||
e701001e PNA |
59 | this = nft_set_ext_key(&rbe->ext); |
60 | d = memcmp(this, key, set->klen); | |
20a69341 PM |
61 | if (d < 0) { |
62 | parent = parent->rb_left; | |
e701001e PNA |
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. | |
66 | */ | |
67 | if (interval && nft_rbtree_equal(set, this, interval)) | |
68 | continue; | |
20a69341 PM |
69 | interval = rbe; |
70 | } else if (d > 0) | |
71 | parent = parent->rb_right; | |
72 | else { | |
73 | found: | |
cc02e457 PM |
74 | if (!nft_set_elem_active(&rbe->ext, genmask)) { |
75 | parent = parent->rb_left; | |
76 | continue; | |
77 | } | |
ef1d20e0 | 78 | if (nft_rbtree_interval_end(rbe)) |
20a69341 | 79 | goto out; |
7632667d | 80 | spin_unlock_bh(&nft_rbtree_lock); |
b2832dd6 PM |
81 | |
82 | *ext = &rbe->ext; | |
20a69341 PM |
83 | return true; |
84 | } | |
85 | } | |
86 | ||
87 | if (set->flags & NFT_SET_INTERVAL && interval != NULL) { | |
88 | rbe = interval; | |
89 | goto found; | |
90 | } | |
91 | out: | |
7632667d | 92 | spin_unlock_bh(&nft_rbtree_lock); |
20a69341 PM |
93 | return false; |
94 | } | |
95 | ||
20a69341 PM |
96 | static int __nft_rbtree_insert(const struct nft_set *set, |
97 | struct nft_rbtree_elem *new) | |
98 | { | |
99 | struct nft_rbtree *priv = nft_set_priv(set); | |
100 | struct nft_rbtree_elem *rbe; | |
101 | struct rb_node *parent, **p; | |
cc02e457 | 102 | u8 genmask = nft_genmask_next(read_pnet(&set->pnet)); |
20a69341 PM |
103 | int d; |
104 | ||
105 | parent = NULL; | |
106 | p = &priv->root.rb_node; | |
107 | while (*p != NULL) { | |
108 | parent = *p; | |
109 | rbe = rb_entry(parent, struct nft_rbtree_elem, node); | |
e562d860 PM |
110 | d = memcmp(nft_set_ext_key(&rbe->ext), |
111 | nft_set_ext_key(&new->ext), | |
112 | set->klen); | |
20a69341 PM |
113 | if (d < 0) |
114 | p = &parent->rb_left; | |
115 | else if (d > 0) | |
116 | p = &parent->rb_right; | |
cc02e457 | 117 | else { |
e701001e PNA |
118 | if (nft_set_elem_active(&rbe->ext, genmask)) { |
119 | if (nft_rbtree_interval_end(rbe) && | |
120 | !nft_rbtree_interval_end(new)) | |
121 | p = &parent->rb_left; | |
122 | else if (!nft_rbtree_interval_end(rbe) && | |
123 | nft_rbtree_interval_end(new)) | |
124 | p = &parent->rb_right; | |
125 | else | |
126 | return -EEXIST; | |
127 | } | |
cc02e457 | 128 | } |
20a69341 PM |
129 | } |
130 | rb_link_node(&new->node, parent, p); | |
131 | rb_insert_color(&new->node, &priv->root); | |
132 | return 0; | |
133 | } | |
134 | ||
135 | static int nft_rbtree_insert(const struct nft_set *set, | |
136 | const struct nft_set_elem *elem) | |
137 | { | |
fe2811eb | 138 | struct nft_rbtree_elem *rbe = elem->priv; |
20a69341 PM |
139 | int err; |
140 | ||
7632667d | 141 | spin_lock_bh(&nft_rbtree_lock); |
20a69341 | 142 | err = __nft_rbtree_insert(set, rbe); |
7632667d | 143 | spin_unlock_bh(&nft_rbtree_lock); |
fe2811eb | 144 | |
20a69341 PM |
145 | return err; |
146 | } | |
147 | ||
148 | static void nft_rbtree_remove(const struct nft_set *set, | |
149 | const struct nft_set_elem *elem) | |
150 | { | |
151 | struct nft_rbtree *priv = nft_set_priv(set); | |
cc02e457 | 152 | struct nft_rbtree_elem *rbe = elem->priv; |
20a69341 | 153 | |
7632667d | 154 | spin_lock_bh(&nft_rbtree_lock); |
20a69341 | 155 | rb_erase(&rbe->node, &priv->root); |
7632667d | 156 | spin_unlock_bh(&nft_rbtree_lock); |
20a69341 PM |
157 | } |
158 | ||
cc02e457 PM |
159 | static void nft_rbtree_activate(const struct nft_set *set, |
160 | const struct nft_set_elem *elem) | |
161 | { | |
162 | struct nft_rbtree_elem *rbe = elem->priv; | |
163 | ||
164 | nft_set_elem_change_active(set, &rbe->ext); | |
165 | } | |
166 | ||
167 | static void *nft_rbtree_deactivate(const struct nft_set *set, | |
168 | const struct nft_set_elem *elem) | |
20a69341 PM |
169 | { |
170 | const struct nft_rbtree *priv = nft_set_priv(set); | |
171 | const struct rb_node *parent = priv->root.rb_node; | |
e701001e | 172 | struct nft_rbtree_elem *rbe, *this = elem->priv; |
cc02e457 | 173 | u8 genmask = nft_genmask_cur(read_pnet(&set->pnet)); |
20a69341 PM |
174 | int d; |
175 | ||
176 | while (parent != NULL) { | |
177 | rbe = rb_entry(parent, struct nft_rbtree_elem, node); | |
178 | ||
7d740264 PM |
179 | d = memcmp(nft_set_ext_key(&rbe->ext), &elem->key.val, |
180 | set->klen); | |
20a69341 PM |
181 | if (d < 0) |
182 | parent = parent->rb_left; | |
183 | else if (d > 0) | |
184 | parent = parent->rb_right; | |
185 | else { | |
cc02e457 PM |
186 | if (!nft_set_elem_active(&rbe->ext, genmask)) { |
187 | parent = parent->rb_left; | |
188 | continue; | |
189 | } | |
e701001e PNA |
190 | if (nft_rbtree_interval_end(rbe) && |
191 | !nft_rbtree_interval_end(this)) { | |
192 | parent = parent->rb_left; | |
193 | continue; | |
194 | } else if (!nft_rbtree_interval_end(rbe) && | |
195 | nft_rbtree_interval_end(this)) { | |
196 | parent = parent->rb_right; | |
197 | continue; | |
198 | } | |
cc02e457 PM |
199 | nft_set_elem_change_active(set, &rbe->ext); |
200 | return rbe; | |
20a69341 PM |
201 | } |
202 | } | |
cc02e457 | 203 | return NULL; |
20a69341 PM |
204 | } |
205 | ||
206 | static void nft_rbtree_walk(const struct nft_ctx *ctx, | |
207 | const struct nft_set *set, | |
208 | struct nft_set_iter *iter) | |
209 | { | |
210 | const struct nft_rbtree *priv = nft_set_priv(set); | |
fe2811eb | 211 | struct nft_rbtree_elem *rbe; |
20a69341 PM |
212 | struct nft_set_elem elem; |
213 | struct rb_node *node; | |
214 | ||
7632667d | 215 | spin_lock_bh(&nft_rbtree_lock); |
20a69341 | 216 | for (node = rb_first(&priv->root); node != NULL; node = rb_next(node)) { |
cc02e457 PM |
217 | rbe = rb_entry(node, struct nft_rbtree_elem, node); |
218 | ||
20a69341 PM |
219 | if (iter->count < iter->skip) |
220 | goto cont; | |
8588ac09 | 221 | if (!nft_set_elem_active(&rbe->ext, iter->genmask)) |
cc02e457 | 222 | goto cont; |
20a69341 | 223 | |
fe2811eb | 224 | elem.priv = rbe; |
20a69341 PM |
225 | |
226 | iter->err = iter->fn(ctx, set, iter, &elem); | |
7632667d PNA |
227 | if (iter->err < 0) { |
228 | spin_unlock_bh(&nft_rbtree_lock); | |
20a69341 | 229 | return; |
7632667d | 230 | } |
20a69341 PM |
231 | cont: |
232 | iter->count++; | |
233 | } | |
7632667d | 234 | spin_unlock_bh(&nft_rbtree_lock); |
20a69341 PM |
235 | } |
236 | ||
237 | static unsigned int nft_rbtree_privsize(const struct nlattr * const nla[]) | |
238 | { | |
239 | return sizeof(struct nft_rbtree); | |
240 | } | |
241 | ||
242 | static int nft_rbtree_init(const struct nft_set *set, | |
c50b960c | 243 | const struct nft_set_desc *desc, |
20a69341 PM |
244 | const struct nlattr * const nla[]) |
245 | { | |
246 | struct nft_rbtree *priv = nft_set_priv(set); | |
247 | ||
248 | priv->root = RB_ROOT; | |
249 | return 0; | |
250 | } | |
251 | ||
252 | static void nft_rbtree_destroy(const struct nft_set *set) | |
253 | { | |
254 | struct nft_rbtree *priv = nft_set_priv(set); | |
255 | struct nft_rbtree_elem *rbe; | |
256 | struct rb_node *node; | |
257 | ||
258 | while ((node = priv->root.rb_node) != NULL) { | |
259 | rb_erase(node, &priv->root); | |
260 | rbe = rb_entry(node, struct nft_rbtree_elem, node); | |
61edafbb | 261 | nft_set_elem_destroy(set, rbe); |
20a69341 PM |
262 | } |
263 | } | |
264 | ||
c50b960c PM |
265 | static bool nft_rbtree_estimate(const struct nft_set_desc *desc, u32 features, |
266 | struct nft_set_estimate *est) | |
267 | { | |
268 | unsigned int nsize; | |
269 | ||
270 | nsize = sizeof(struct nft_rbtree_elem); | |
c50b960c PM |
271 | if (desc->size) |
272 | est->size = sizeof(struct nft_rbtree) + desc->size * nsize; | |
273 | else | |
274 | est->size = nsize; | |
275 | ||
276 | est->class = NFT_SET_CLASS_O_LOG_N; | |
277 | ||
278 | return true; | |
279 | } | |
280 | ||
20a69341 PM |
281 | static struct nft_set_ops nft_rbtree_ops __read_mostly = { |
282 | .privsize = nft_rbtree_privsize, | |
fe2811eb | 283 | .elemsize = offsetof(struct nft_rbtree_elem, ext), |
c50b960c | 284 | .estimate = nft_rbtree_estimate, |
20a69341 PM |
285 | .init = nft_rbtree_init, |
286 | .destroy = nft_rbtree_destroy, | |
287 | .insert = nft_rbtree_insert, | |
288 | .remove = nft_rbtree_remove, | |
cc02e457 PM |
289 | .deactivate = nft_rbtree_deactivate, |
290 | .activate = nft_rbtree_activate, | |
20a69341 PM |
291 | .lookup = nft_rbtree_lookup, |
292 | .walk = nft_rbtree_walk, | |
293 | .features = NFT_SET_INTERVAL | NFT_SET_MAP, | |
294 | .owner = THIS_MODULE, | |
295 | }; | |
296 | ||
297 | static int __init nft_rbtree_module_init(void) | |
298 | { | |
299 | return nft_register_set(&nft_rbtree_ops); | |
300 | } | |
301 | ||
302 | static void __exit nft_rbtree_module_exit(void) | |
303 | { | |
304 | nft_unregister_set(&nft_rbtree_ops); | |
305 | } | |
306 | ||
307 | module_init(nft_rbtree_module_init); | |
308 | module_exit(nft_rbtree_module_exit); | |
309 | ||
310 | MODULE_LICENSE("GPL"); | |
311 | MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>"); | |
312 | MODULE_ALIAS_NFT_SET(); |