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