]>
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 | ||
5cb82a38 PNA |
154 | static void nft_rbtree_remove(const struct net *net, |
155 | const struct nft_set *set, | |
20a69341 PM |
156 | const struct nft_set_elem *elem) |
157 | { | |
158 | struct nft_rbtree *priv = nft_set_priv(set); | |
cc02e457 | 159 | struct nft_rbtree_elem *rbe = elem->priv; |
20a69341 | 160 | |
7632667d | 161 | spin_lock_bh(&nft_rbtree_lock); |
20a69341 | 162 | rb_erase(&rbe->node, &priv->root); |
7632667d | 163 | spin_unlock_bh(&nft_rbtree_lock); |
20a69341 PM |
164 | } |
165 | ||
42a55769 PNA |
166 | static void nft_rbtree_activate(const struct net *net, |
167 | const struct nft_set *set, | |
cc02e457 PM |
168 | const struct nft_set_elem *elem) |
169 | { | |
170 | struct nft_rbtree_elem *rbe = elem->priv; | |
171 | ||
42a55769 | 172 | nft_set_elem_change_active(net, set, &rbe->ext); |
cc02e457 PM |
173 | } |
174 | ||
1ba1c414 PNA |
175 | static bool nft_rbtree_flush(const struct net *net, |
176 | const struct nft_set *set, void *priv) | |
37df5301 PNA |
177 | { |
178 | struct nft_rbtree_elem *rbe = priv; | |
179 | ||
180 | nft_set_elem_change_active(net, set, &rbe->ext); | |
181 | return true; | |
182 | } | |
183 | ||
42a55769 PNA |
184 | static void *nft_rbtree_deactivate(const struct net *net, |
185 | const struct nft_set *set, | |
cc02e457 | 186 | const struct nft_set_elem *elem) |
20a69341 PM |
187 | { |
188 | const struct nft_rbtree *priv = nft_set_priv(set); | |
189 | const struct rb_node *parent = priv->root.rb_node; | |
e701001e | 190 | struct nft_rbtree_elem *rbe, *this = elem->priv; |
42a55769 | 191 | u8 genmask = nft_genmask_next(net); |
20a69341 PM |
192 | int d; |
193 | ||
194 | while (parent != NULL) { | |
195 | rbe = rb_entry(parent, struct nft_rbtree_elem, node); | |
196 | ||
7d740264 PM |
197 | d = memcmp(nft_set_ext_key(&rbe->ext), &elem->key.val, |
198 | set->klen); | |
20a69341 PM |
199 | if (d < 0) |
200 | parent = parent->rb_left; | |
201 | else if (d > 0) | |
202 | parent = parent->rb_right; | |
203 | else { | |
cc02e457 PM |
204 | if (!nft_set_elem_active(&rbe->ext, genmask)) { |
205 | parent = parent->rb_left; | |
206 | continue; | |
207 | } | |
e701001e PNA |
208 | if (nft_rbtree_interval_end(rbe) && |
209 | !nft_rbtree_interval_end(this)) { | |
210 | parent = parent->rb_left; | |
211 | continue; | |
212 | } else if (!nft_rbtree_interval_end(rbe) && | |
213 | nft_rbtree_interval_end(this)) { | |
214 | parent = parent->rb_right; | |
215 | continue; | |
216 | } | |
1ba1c414 | 217 | nft_rbtree_flush(net, set, rbe); |
cc02e457 | 218 | return rbe; |
20a69341 PM |
219 | } |
220 | } | |
cc02e457 | 221 | return NULL; |
20a69341 PM |
222 | } |
223 | ||
224 | static void nft_rbtree_walk(const struct nft_ctx *ctx, | |
de70185d | 225 | struct nft_set *set, |
20a69341 PM |
226 | struct nft_set_iter *iter) |
227 | { | |
228 | const struct nft_rbtree *priv = nft_set_priv(set); | |
fe2811eb | 229 | struct nft_rbtree_elem *rbe; |
20a69341 PM |
230 | struct nft_set_elem elem; |
231 | struct rb_node *node; | |
232 | ||
7632667d | 233 | spin_lock_bh(&nft_rbtree_lock); |
20a69341 | 234 | for (node = rb_first(&priv->root); node != NULL; node = rb_next(node)) { |
cc02e457 PM |
235 | rbe = rb_entry(node, struct nft_rbtree_elem, node); |
236 | ||
20a69341 PM |
237 | if (iter->count < iter->skip) |
238 | goto cont; | |
8588ac09 | 239 | if (!nft_set_elem_active(&rbe->ext, iter->genmask)) |
cc02e457 | 240 | goto cont; |
20a69341 | 241 | |
fe2811eb | 242 | elem.priv = rbe; |
20a69341 PM |
243 | |
244 | iter->err = iter->fn(ctx, set, iter, &elem); | |
7632667d PNA |
245 | if (iter->err < 0) { |
246 | spin_unlock_bh(&nft_rbtree_lock); | |
20a69341 | 247 | return; |
7632667d | 248 | } |
20a69341 PM |
249 | cont: |
250 | iter->count++; | |
251 | } | |
7632667d | 252 | spin_unlock_bh(&nft_rbtree_lock); |
20a69341 PM |
253 | } |
254 | ||
255 | static unsigned int nft_rbtree_privsize(const struct nlattr * const nla[]) | |
256 | { | |
257 | return sizeof(struct nft_rbtree); | |
258 | } | |
259 | ||
260 | static int nft_rbtree_init(const struct nft_set *set, | |
c50b960c | 261 | const struct nft_set_desc *desc, |
20a69341 PM |
262 | const struct nlattr * const nla[]) |
263 | { | |
264 | struct nft_rbtree *priv = nft_set_priv(set); | |
265 | ||
266 | priv->root = RB_ROOT; | |
267 | return 0; | |
268 | } | |
269 | ||
270 | static void nft_rbtree_destroy(const struct nft_set *set) | |
271 | { | |
272 | struct nft_rbtree *priv = nft_set_priv(set); | |
273 | struct nft_rbtree_elem *rbe; | |
274 | struct rb_node *node; | |
275 | ||
276 | while ((node = priv->root.rb_node) != NULL) { | |
277 | rb_erase(node, &priv->root); | |
278 | rbe = rb_entry(node, struct nft_rbtree_elem, node); | |
61f9e292 | 279 | nft_set_elem_destroy(set, rbe, true); |
20a69341 PM |
280 | } |
281 | } | |
282 | ||
c50b960c PM |
283 | static bool nft_rbtree_estimate(const struct nft_set_desc *desc, u32 features, |
284 | struct nft_set_estimate *est) | |
285 | { | |
286 | unsigned int nsize; | |
287 | ||
288 | nsize = sizeof(struct nft_rbtree_elem); | |
c50b960c PM |
289 | if (desc->size) |
290 | est->size = sizeof(struct nft_rbtree) + desc->size * nsize; | |
291 | else | |
292 | est->size = nsize; | |
293 | ||
294 | est->class = NFT_SET_CLASS_O_LOG_N; | |
295 | ||
296 | return true; | |
297 | } | |
298 | ||
20a69341 PM |
299 | static struct nft_set_ops nft_rbtree_ops __read_mostly = { |
300 | .privsize = nft_rbtree_privsize, | |
fe2811eb | 301 | .elemsize = offsetof(struct nft_rbtree_elem, ext), |
c50b960c | 302 | .estimate = nft_rbtree_estimate, |
20a69341 PM |
303 | .init = nft_rbtree_init, |
304 | .destroy = nft_rbtree_destroy, | |
305 | .insert = nft_rbtree_insert, | |
306 | .remove = nft_rbtree_remove, | |
cc02e457 | 307 | .deactivate = nft_rbtree_deactivate, |
1ba1c414 | 308 | .flush = nft_rbtree_flush, |
cc02e457 | 309 | .activate = nft_rbtree_activate, |
20a69341 PM |
310 | .lookup = nft_rbtree_lookup, |
311 | .walk = nft_rbtree_walk, | |
312 | .features = NFT_SET_INTERVAL | NFT_SET_MAP, | |
313 | .owner = THIS_MODULE, | |
314 | }; | |
315 | ||
316 | static int __init nft_rbtree_module_init(void) | |
317 | { | |
318 | return nft_register_set(&nft_rbtree_ops); | |
319 | } | |
320 | ||
321 | static void __exit nft_rbtree_module_exit(void) | |
322 | { | |
323 | nft_unregister_set(&nft_rbtree_ops); | |
324 | } | |
325 | ||
326 | module_init(nft_rbtree_module_init); | |
327 | module_exit(nft_rbtree_module_exit); | |
328 | ||
329 | MODULE_LICENSE("GPL"); | |
330 | MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>"); | |
331 | MODULE_ALIAS_NFT_SET(); |