blob: ffe9ae062d23e48fe39f9136e8e01d3737a852af [file] [log] [blame]
Patrick McHardy20a69342013-10-11 12:06:22 +02001/*
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
Pablo Neira Ayuso76326672014-05-28 15:27:18 +020021static DEFINE_SPINLOCK(nft_rbtree_lock);
22
Patrick McHardy20a69342013-10-11 12:06:22 +020023struct nft_rbtree {
24 struct rb_root root;
25};
26
27struct nft_rbtree_elem {
28 struct rb_node node;
Patrick McHardyfe2811e2015-03-25 13:07:50 +000029 struct nft_set_ext ext;
Patrick McHardy20a69342013-10-11 12:06:22 +020030};
31
Pablo Neira Ayusoef1d20e2016-04-12 23:50:36 +020032static 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}
Patrick McHardycc02e452015-03-25 14:08:50 +000037
Pablo Neira Ayusoe7010012016-04-12 23:50:37 +020038static 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
Pablo Neira Ayuso42a55762016-07-08 14:41:49 +020044static bool nft_rbtree_lookup(const struct net *net, const struct nft_set *set,
45 const u32 *key, const struct nft_set_ext **ext)
Patrick McHardy20a69342013-10-11 12:06:22 +020046{
47 const struct nft_rbtree *priv = nft_set_priv(set);
48 const struct nft_rbtree_elem *rbe, *interval = NULL;
Pablo Neira Ayuso42a55762016-07-08 14:41:49 +020049 u8 genmask = nft_genmask_cur(net);
Patrick McHardy16c45ed2015-03-21 15:19:14 +000050 const struct rb_node *parent;
Pablo Neira Ayusoe7010012016-04-12 23:50:37 +020051 const void *this;
Patrick McHardy20a69342013-10-11 12:06:22 +020052 int d;
53
Pablo Neira Ayuso76326672014-05-28 15:27:18 +020054 spin_lock_bh(&nft_rbtree_lock);
Patrick McHardy16c45ed2015-03-21 15:19:14 +000055 parent = priv->root.rb_node;
Patrick McHardy20a69342013-10-11 12:06:22 +020056 while (parent != NULL) {
57 rbe = rb_entry(parent, struct nft_rbtree_elem, node);
58
Pablo Neira Ayusoe7010012016-04-12 23:50:37 +020059 this = nft_set_ext_key(&rbe->ext);
60 d = memcmp(this, key, set->klen);
Patrick McHardy20a69342013-10-11 12:06:22 +020061 if (d < 0) {
62 parent = parent->rb_left;
Pablo Neira Ayusoe7010012016-04-12 23:50:37 +020063 /* 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;
Patrick McHardy20a69342013-10-11 12:06:22 +020069 interval = rbe;
70 } else if (d > 0)
71 parent = parent->rb_right;
72 else {
Patrick McHardycc02e452015-03-25 14:08:50 +000073 if (!nft_set_elem_active(&rbe->ext, genmask)) {
74 parent = parent->rb_left;
75 continue;
76 }
Pablo Neira Ayusoef1d20e2016-04-12 23:50:36 +020077 if (nft_rbtree_interval_end(rbe))
Patrick McHardy20a69342013-10-11 12:06:22 +020078 goto out;
Pablo Neira Ayuso76326672014-05-28 15:27:18 +020079 spin_unlock_bh(&nft_rbtree_lock);
Patrick McHardyb2832dd2015-03-25 14:08:48 +000080
81 *ext = &rbe->ext;
Patrick McHardy20a69342013-10-11 12:06:22 +020082 return true;
83 }
84 }
85
Pablo Neira Ayusoc1eda3c2016-08-01 13:13:08 +020086 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;
Patrick McHardy20a69342013-10-11 12:06:22 +020092 }
93out:
Pablo Neira Ayuso76326672014-05-28 15:27:18 +020094 spin_unlock_bh(&nft_rbtree_lock);
Patrick McHardy20a69342013-10-11 12:06:22 +020095 return false;
96}
97
Pablo Neira Ayuso42a55762016-07-08 14:41:49 +020098static int __nft_rbtree_insert(const struct net *net, const struct nft_set *set,
Patrick McHardy20a69342013-10-11 12:06:22 +020099 struct nft_rbtree_elem *new)
100{
101 struct nft_rbtree *priv = nft_set_priv(set);
Pablo Neira Ayuso42a55762016-07-08 14:41:49 +0200102 u8 genmask = nft_genmask_next(net);
Patrick McHardy20a69342013-10-11 12:06:22 +0200103 struct nft_rbtree_elem *rbe;
104 struct rb_node *parent, **p;
105 int d;
106
107 parent = NULL;
108 p = &priv->root.rb_node;
109 while (*p != NULL) {
110 parent = *p;
111 rbe = rb_entry(parent, struct nft_rbtree_elem, node);
Patrick McHardye562d862015-04-11 02:27:34 +0100112 d = memcmp(nft_set_ext_key(&rbe->ext),
113 nft_set_ext_key(&new->ext),
114 set->klen);
Patrick McHardy20a69342013-10-11 12:06:22 +0200115 if (d < 0)
116 p = &parent->rb_left;
117 else if (d > 0)
118 p = &parent->rb_right;
Patrick McHardycc02e452015-03-25 14:08:50 +0000119 else {
Pablo Neira Ayusoe7010012016-04-12 23:50:37 +0200120 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;
127 else
128 return -EEXIST;
129 }
Patrick McHardycc02e452015-03-25 14:08:50 +0000130 }
Patrick McHardy20a69342013-10-11 12:06:22 +0200131 }
132 rb_link_node(&new->node, parent, p);
133 rb_insert_color(&new->node, &priv->root);
134 return 0;
135}
136
Pablo Neira Ayuso42a55762016-07-08 14:41:49 +0200137static int nft_rbtree_insert(const struct net *net, const struct nft_set *set,
Patrick McHardy20a69342013-10-11 12:06:22 +0200138 const struct nft_set_elem *elem)
139{
Patrick McHardyfe2811e2015-03-25 13:07:50 +0000140 struct nft_rbtree_elem *rbe = elem->priv;
Patrick McHardy20a69342013-10-11 12:06:22 +0200141 int err;
142
Pablo Neira Ayuso76326672014-05-28 15:27:18 +0200143 spin_lock_bh(&nft_rbtree_lock);
Pablo Neira Ayuso42a55762016-07-08 14:41:49 +0200144 err = __nft_rbtree_insert(net, set, rbe);
Pablo Neira Ayuso76326672014-05-28 15:27:18 +0200145 spin_unlock_bh(&nft_rbtree_lock);
Patrick McHardyfe2811e2015-03-25 13:07:50 +0000146
Patrick McHardy20a69342013-10-11 12:06:22 +0200147 return err;
148}
149
150static void nft_rbtree_remove(const struct nft_set *set,
151 const struct nft_set_elem *elem)
152{
153 struct nft_rbtree *priv = nft_set_priv(set);
Patrick McHardycc02e452015-03-25 14:08:50 +0000154 struct nft_rbtree_elem *rbe = elem->priv;
Patrick McHardy20a69342013-10-11 12:06:22 +0200155
Pablo Neira Ayuso76326672014-05-28 15:27:18 +0200156 spin_lock_bh(&nft_rbtree_lock);
Patrick McHardy20a69342013-10-11 12:06:22 +0200157 rb_erase(&rbe->node, &priv->root);
Pablo Neira Ayuso76326672014-05-28 15:27:18 +0200158 spin_unlock_bh(&nft_rbtree_lock);
Patrick McHardy20a69342013-10-11 12:06:22 +0200159}
160
Pablo Neira Ayuso42a55762016-07-08 14:41:49 +0200161static void nft_rbtree_activate(const struct net *net,
162 const struct nft_set *set,
Patrick McHardycc02e452015-03-25 14:08:50 +0000163 const struct nft_set_elem *elem)
164{
165 struct nft_rbtree_elem *rbe = elem->priv;
166
Pablo Neira Ayuso42a55762016-07-08 14:41:49 +0200167 nft_set_elem_change_active(net, set, &rbe->ext);
Patrick McHardycc02e452015-03-25 14:08:50 +0000168}
169
Pablo Neira Ayuso42a55762016-07-08 14:41:49 +0200170static void *nft_rbtree_deactivate(const struct net *net,
171 const struct nft_set *set,
Patrick McHardycc02e452015-03-25 14:08:50 +0000172 const struct nft_set_elem *elem)
Patrick McHardy20a69342013-10-11 12:06:22 +0200173{
174 const struct nft_rbtree *priv = nft_set_priv(set);
175 const struct rb_node *parent = priv->root.rb_node;
Pablo Neira Ayusoe7010012016-04-12 23:50:37 +0200176 struct nft_rbtree_elem *rbe, *this = elem->priv;
Pablo Neira Ayuso42a55762016-07-08 14:41:49 +0200177 u8 genmask = nft_genmask_next(net);
Patrick McHardy20a69342013-10-11 12:06:22 +0200178 int d;
179
180 while (parent != NULL) {
181 rbe = rb_entry(parent, struct nft_rbtree_elem, node);
182
Patrick McHardy7d740262015-04-11 02:27:39 +0100183 d = memcmp(nft_set_ext_key(&rbe->ext), &elem->key.val,
184 set->klen);
Patrick McHardy20a69342013-10-11 12:06:22 +0200185 if (d < 0)
186 parent = parent->rb_left;
187 else if (d > 0)
188 parent = parent->rb_right;
189 else {
Patrick McHardycc02e452015-03-25 14:08:50 +0000190 if (!nft_set_elem_active(&rbe->ext, genmask)) {
191 parent = parent->rb_left;
192 continue;
193 }
Pablo Neira Ayusoe7010012016-04-12 23:50:37 +0200194 if (nft_rbtree_interval_end(rbe) &&
195 !nft_rbtree_interval_end(this)) {
196 parent = parent->rb_left;
197 continue;
198 } else if (!nft_rbtree_interval_end(rbe) &&
199 nft_rbtree_interval_end(this)) {
200 parent = parent->rb_right;
201 continue;
202 }
Pablo Neira Ayuso42a55762016-07-08 14:41:49 +0200203 nft_set_elem_change_active(net, set, &rbe->ext);
Patrick McHardycc02e452015-03-25 14:08:50 +0000204 return rbe;
Patrick McHardy20a69342013-10-11 12:06:22 +0200205 }
206 }
Patrick McHardycc02e452015-03-25 14:08:50 +0000207 return NULL;
Patrick McHardy20a69342013-10-11 12:06:22 +0200208}
209
210static void nft_rbtree_walk(const struct nft_ctx *ctx,
211 const struct nft_set *set,
212 struct nft_set_iter *iter)
213{
214 const struct nft_rbtree *priv = nft_set_priv(set);
Patrick McHardyfe2811e2015-03-25 13:07:50 +0000215 struct nft_rbtree_elem *rbe;
Patrick McHardy20a69342013-10-11 12:06:22 +0200216 struct nft_set_elem elem;
217 struct rb_node *node;
218
Pablo Neira Ayuso76326672014-05-28 15:27:18 +0200219 spin_lock_bh(&nft_rbtree_lock);
Patrick McHardy20a69342013-10-11 12:06:22 +0200220 for (node = rb_first(&priv->root); node != NULL; node = rb_next(node)) {
Patrick McHardycc02e452015-03-25 14:08:50 +0000221 rbe = rb_entry(node, struct nft_rbtree_elem, node);
222
Patrick McHardy20a69342013-10-11 12:06:22 +0200223 if (iter->count < iter->skip)
224 goto cont;
Pablo Neira Ayuso8588ac02016-06-11 12:20:27 +0800225 if (!nft_set_elem_active(&rbe->ext, iter->genmask))
Patrick McHardycc02e452015-03-25 14:08:50 +0000226 goto cont;
Patrick McHardy20a69342013-10-11 12:06:22 +0200227
Patrick McHardyfe2811e2015-03-25 13:07:50 +0000228 elem.priv = rbe;
Patrick McHardy20a69342013-10-11 12:06:22 +0200229
230 iter->err = iter->fn(ctx, set, iter, &elem);
Pablo Neira Ayuso76326672014-05-28 15:27:18 +0200231 if (iter->err < 0) {
232 spin_unlock_bh(&nft_rbtree_lock);
Patrick McHardy20a69342013-10-11 12:06:22 +0200233 return;
Pablo Neira Ayuso76326672014-05-28 15:27:18 +0200234 }
Patrick McHardy20a69342013-10-11 12:06:22 +0200235cont:
236 iter->count++;
237 }
Pablo Neira Ayuso76326672014-05-28 15:27:18 +0200238 spin_unlock_bh(&nft_rbtree_lock);
Patrick McHardy20a69342013-10-11 12:06:22 +0200239}
240
241static unsigned int nft_rbtree_privsize(const struct nlattr * const nla[])
242{
243 return sizeof(struct nft_rbtree);
244}
245
246static int nft_rbtree_init(const struct nft_set *set,
Patrick McHardyc50b9602014-03-28 10:19:47 +0000247 const struct nft_set_desc *desc,
Patrick McHardy20a69342013-10-11 12:06:22 +0200248 const struct nlattr * const nla[])
249{
250 struct nft_rbtree *priv = nft_set_priv(set);
251
252 priv->root = RB_ROOT;
253 return 0;
254}
255
256static void nft_rbtree_destroy(const struct nft_set *set)
257{
258 struct nft_rbtree *priv = nft_set_priv(set);
259 struct nft_rbtree_elem *rbe;
260 struct rb_node *node;
261
262 while ((node = priv->root.rb_node) != NULL) {
263 rb_erase(node, &priv->root);
264 rbe = rb_entry(node, struct nft_rbtree_elem, node);
Patrick McHardy61edafb2015-03-25 14:08:47 +0000265 nft_set_elem_destroy(set, rbe);
Patrick McHardy20a69342013-10-11 12:06:22 +0200266 }
267}
268
Patrick McHardyc50b9602014-03-28 10:19:47 +0000269static bool nft_rbtree_estimate(const struct nft_set_desc *desc, u32 features,
270 struct nft_set_estimate *est)
271{
272 unsigned int nsize;
273
274 nsize = sizeof(struct nft_rbtree_elem);
Patrick McHardyc50b9602014-03-28 10:19:47 +0000275 if (desc->size)
276 est->size = sizeof(struct nft_rbtree) + desc->size * nsize;
277 else
278 est->size = nsize;
279
280 est->class = NFT_SET_CLASS_O_LOG_N;
281
282 return true;
283}
284
Patrick McHardy20a69342013-10-11 12:06:22 +0200285static struct nft_set_ops nft_rbtree_ops __read_mostly = {
286 .privsize = nft_rbtree_privsize,
Patrick McHardyfe2811e2015-03-25 13:07:50 +0000287 .elemsize = offsetof(struct nft_rbtree_elem, ext),
Patrick McHardyc50b9602014-03-28 10:19:47 +0000288 .estimate = nft_rbtree_estimate,
Patrick McHardy20a69342013-10-11 12:06:22 +0200289 .init = nft_rbtree_init,
290 .destroy = nft_rbtree_destroy,
291 .insert = nft_rbtree_insert,
292 .remove = nft_rbtree_remove,
Patrick McHardycc02e452015-03-25 14:08:50 +0000293 .deactivate = nft_rbtree_deactivate,
294 .activate = nft_rbtree_activate,
Patrick McHardy20a69342013-10-11 12:06:22 +0200295 .lookup = nft_rbtree_lookup,
296 .walk = nft_rbtree_walk,
297 .features = NFT_SET_INTERVAL | NFT_SET_MAP,
298 .owner = THIS_MODULE,
299};
300
301static int __init nft_rbtree_module_init(void)
302{
303 return nft_register_set(&nft_rbtree_ops);
304}
305
306static void __exit nft_rbtree_module_exit(void)
307{
308 nft_unregister_set(&nft_rbtree_ops);
309}
310
311module_init(nft_rbtree_module_init);
312module_exit(nft_rbtree_module_exit);
313
314MODULE_LICENSE("GPL");
315MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
316MODULE_ALIAS_NFT_SET();