blob: cbba755ebebc4cc47bd9930aa4205e7d0d7e0de0 [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
32static bool nft_rbtree_lookup(const struct nft_set *set,
33 const struct nft_data *key,
Patrick McHardyb2832dd2015-03-25 14:08:48 +000034 const struct nft_set_ext **ext)
Patrick McHardy20a69342013-10-11 12:06:22 +020035{
36 const struct nft_rbtree *priv = nft_set_priv(set);
37 const struct nft_rbtree_elem *rbe, *interval = NULL;
Patrick McHardy16c45ed2015-03-21 15:19:14 +000038 const struct rb_node *parent;
Patrick McHardy20a69342013-10-11 12:06:22 +020039 int d;
40
Pablo Neira Ayuso76326672014-05-28 15:27:18 +020041 spin_lock_bh(&nft_rbtree_lock);
Patrick McHardy16c45ed2015-03-21 15:19:14 +000042 parent = priv->root.rb_node;
Patrick McHardy20a69342013-10-11 12:06:22 +020043 while (parent != NULL) {
44 rbe = rb_entry(parent, struct nft_rbtree_elem, node);
45
Patrick McHardyfe2811e2015-03-25 13:07:50 +000046 d = nft_data_cmp(nft_set_ext_key(&rbe->ext), key, set->klen);
Patrick McHardy20a69342013-10-11 12:06:22 +020047 if (d < 0) {
48 parent = parent->rb_left;
49 interval = rbe;
50 } else if (d > 0)
51 parent = parent->rb_right;
52 else {
53found:
Patrick McHardyfe2811e2015-03-25 13:07:50 +000054 if (nft_set_ext_exists(&rbe->ext, NFT_SET_EXT_FLAGS) &&
55 *nft_set_ext_flags(&rbe->ext) &
56 NFT_SET_ELEM_INTERVAL_END)
Patrick McHardy20a69342013-10-11 12:06:22 +020057 goto out;
Pablo Neira Ayuso76326672014-05-28 15:27:18 +020058 spin_unlock_bh(&nft_rbtree_lock);
Patrick McHardyb2832dd2015-03-25 14:08:48 +000059
60 *ext = &rbe->ext;
Patrick McHardy20a69342013-10-11 12:06:22 +020061 return true;
62 }
63 }
64
65 if (set->flags & NFT_SET_INTERVAL && interval != NULL) {
66 rbe = interval;
67 goto found;
68 }
69out:
Pablo Neira Ayuso76326672014-05-28 15:27:18 +020070 spin_unlock_bh(&nft_rbtree_lock);
Patrick McHardy20a69342013-10-11 12:06:22 +020071 return false;
72}
73
Patrick McHardy20a69342013-10-11 12:06:22 +020074static int __nft_rbtree_insert(const struct nft_set *set,
75 struct nft_rbtree_elem *new)
76{
77 struct nft_rbtree *priv = nft_set_priv(set);
78 struct nft_rbtree_elem *rbe;
79 struct rb_node *parent, **p;
80 int d;
81
82 parent = NULL;
83 p = &priv->root.rb_node;
84 while (*p != NULL) {
85 parent = *p;
86 rbe = rb_entry(parent, struct nft_rbtree_elem, node);
Patrick McHardyfe2811e2015-03-25 13:07:50 +000087 d = nft_data_cmp(nft_set_ext_key(&rbe->ext),
88 nft_set_ext_key(&new->ext),
89 set->klen);
Patrick McHardy20a69342013-10-11 12:06:22 +020090 if (d < 0)
91 p = &parent->rb_left;
92 else if (d > 0)
93 p = &parent->rb_right;
94 else
95 return -EEXIST;
96 }
97 rb_link_node(&new->node, parent, p);
98 rb_insert_color(&new->node, &priv->root);
99 return 0;
100}
101
102static int nft_rbtree_insert(const struct nft_set *set,
103 const struct nft_set_elem *elem)
104{
Patrick McHardyfe2811e2015-03-25 13:07:50 +0000105 struct nft_rbtree_elem *rbe = elem->priv;
Patrick McHardy20a69342013-10-11 12:06:22 +0200106 int err;
107
Pablo Neira Ayuso76326672014-05-28 15:27:18 +0200108 spin_lock_bh(&nft_rbtree_lock);
Patrick McHardy20a69342013-10-11 12:06:22 +0200109 err = __nft_rbtree_insert(set, rbe);
Pablo Neira Ayuso76326672014-05-28 15:27:18 +0200110 spin_unlock_bh(&nft_rbtree_lock);
Patrick McHardyfe2811e2015-03-25 13:07:50 +0000111
Patrick McHardy20a69342013-10-11 12:06:22 +0200112 return err;
113}
114
115static void nft_rbtree_remove(const struct nft_set *set,
116 const struct nft_set_elem *elem)
117{
118 struct nft_rbtree *priv = nft_set_priv(set);
119 struct nft_rbtree_elem *rbe = elem->cookie;
120
Pablo Neira Ayuso76326672014-05-28 15:27:18 +0200121 spin_lock_bh(&nft_rbtree_lock);
Patrick McHardy20a69342013-10-11 12:06:22 +0200122 rb_erase(&rbe->node, &priv->root);
Pablo Neira Ayuso76326672014-05-28 15:27:18 +0200123 spin_unlock_bh(&nft_rbtree_lock);
Patrick McHardy20a69342013-10-11 12:06:22 +0200124}
125
126static int nft_rbtree_get(const struct nft_set *set, struct nft_set_elem *elem)
127{
128 const struct nft_rbtree *priv = nft_set_priv(set);
129 const struct rb_node *parent = priv->root.rb_node;
130 struct nft_rbtree_elem *rbe;
131 int d;
132
133 while (parent != NULL) {
134 rbe = rb_entry(parent, struct nft_rbtree_elem, node);
135
Patrick McHardyfe2811e2015-03-25 13:07:50 +0000136 d = nft_data_cmp(nft_set_ext_key(&rbe->ext), &elem->key,
137 set->klen);
Patrick McHardy20a69342013-10-11 12:06:22 +0200138 if (d < 0)
139 parent = parent->rb_left;
140 else if (d > 0)
141 parent = parent->rb_right;
142 else {
143 elem->cookie = rbe;
Patrick McHardyfe2811e2015-03-25 13:07:50 +0000144 elem->priv = rbe;
Patrick McHardy20a69342013-10-11 12:06:22 +0200145 return 0;
146 }
147 }
148 return -ENOENT;
149}
150
151static void nft_rbtree_walk(const struct nft_ctx *ctx,
152 const struct nft_set *set,
153 struct nft_set_iter *iter)
154{
155 const struct nft_rbtree *priv = nft_set_priv(set);
Patrick McHardyfe2811e2015-03-25 13:07:50 +0000156 struct nft_rbtree_elem *rbe;
Patrick McHardy20a69342013-10-11 12:06:22 +0200157 struct nft_set_elem elem;
158 struct rb_node *node;
159
Pablo Neira Ayuso76326672014-05-28 15:27:18 +0200160 spin_lock_bh(&nft_rbtree_lock);
Patrick McHardy20a69342013-10-11 12:06:22 +0200161 for (node = rb_first(&priv->root); node != NULL; node = rb_next(node)) {
162 if (iter->count < iter->skip)
163 goto cont;
164
165 rbe = rb_entry(node, struct nft_rbtree_elem, node);
Patrick McHardyfe2811e2015-03-25 13:07:50 +0000166 elem.priv = rbe;
Patrick McHardy20a69342013-10-11 12:06:22 +0200167
168 iter->err = iter->fn(ctx, set, iter, &elem);
Pablo Neira Ayuso76326672014-05-28 15:27:18 +0200169 if (iter->err < 0) {
170 spin_unlock_bh(&nft_rbtree_lock);
Patrick McHardy20a69342013-10-11 12:06:22 +0200171 return;
Pablo Neira Ayuso76326672014-05-28 15:27:18 +0200172 }
Patrick McHardy20a69342013-10-11 12:06:22 +0200173cont:
174 iter->count++;
175 }
Pablo Neira Ayuso76326672014-05-28 15:27:18 +0200176 spin_unlock_bh(&nft_rbtree_lock);
Patrick McHardy20a69342013-10-11 12:06:22 +0200177}
178
179static unsigned int nft_rbtree_privsize(const struct nlattr * const nla[])
180{
181 return sizeof(struct nft_rbtree);
182}
183
184static int nft_rbtree_init(const struct nft_set *set,
Patrick McHardyc50b9602014-03-28 10:19:47 +0000185 const struct nft_set_desc *desc,
Patrick McHardy20a69342013-10-11 12:06:22 +0200186 const struct nlattr * const nla[])
187{
188 struct nft_rbtree *priv = nft_set_priv(set);
189
190 priv->root = RB_ROOT;
191 return 0;
192}
193
194static void nft_rbtree_destroy(const struct nft_set *set)
195{
196 struct nft_rbtree *priv = nft_set_priv(set);
197 struct nft_rbtree_elem *rbe;
198 struct rb_node *node;
199
200 while ((node = priv->root.rb_node) != NULL) {
201 rb_erase(node, &priv->root);
202 rbe = rb_entry(node, struct nft_rbtree_elem, node);
Patrick McHardy61edafb2015-03-25 14:08:47 +0000203 nft_set_elem_destroy(set, rbe);
Patrick McHardy20a69342013-10-11 12:06:22 +0200204 }
205}
206
Patrick McHardyc50b9602014-03-28 10:19:47 +0000207static bool nft_rbtree_estimate(const struct nft_set_desc *desc, u32 features,
208 struct nft_set_estimate *est)
209{
210 unsigned int nsize;
211
212 nsize = sizeof(struct nft_rbtree_elem);
Patrick McHardyc50b9602014-03-28 10:19:47 +0000213 if (desc->size)
214 est->size = sizeof(struct nft_rbtree) + desc->size * nsize;
215 else
216 est->size = nsize;
217
218 est->class = NFT_SET_CLASS_O_LOG_N;
219
220 return true;
221}
222
Patrick McHardy20a69342013-10-11 12:06:22 +0200223static struct nft_set_ops nft_rbtree_ops __read_mostly = {
224 .privsize = nft_rbtree_privsize,
Patrick McHardyfe2811e2015-03-25 13:07:50 +0000225 .elemsize = offsetof(struct nft_rbtree_elem, ext),
Patrick McHardyc50b9602014-03-28 10:19:47 +0000226 .estimate = nft_rbtree_estimate,
Patrick McHardy20a69342013-10-11 12:06:22 +0200227 .init = nft_rbtree_init,
228 .destroy = nft_rbtree_destroy,
229 .insert = nft_rbtree_insert,
230 .remove = nft_rbtree_remove,
231 .get = nft_rbtree_get,
232 .lookup = nft_rbtree_lookup,
233 .walk = nft_rbtree_walk,
234 .features = NFT_SET_INTERVAL | NFT_SET_MAP,
235 .owner = THIS_MODULE,
236};
237
238static int __init nft_rbtree_module_init(void)
239{
240 return nft_register_set(&nft_rbtree_ops);
241}
242
243static void __exit nft_rbtree_module_exit(void)
244{
245 nft_unregister_set(&nft_rbtree_ops);
246}
247
248module_init(nft_rbtree_module_init);
249module_exit(nft_rbtree_module_exit);
250
251MODULE_LICENSE("GPL");
252MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
253MODULE_ALIAS_NFT_SET();