Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 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 | |
Pablo Neira Ayuso | 7632667 | 2014-05-28 15:27:18 +0200 | [diff] [blame] | 21 | static DEFINE_SPINLOCK(nft_rbtree_lock); |
| 22 | |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 23 | struct nft_rbtree { |
| 24 | struct rb_root root; |
| 25 | }; |
| 26 | |
| 27 | struct nft_rbtree_elem { |
| 28 | struct rb_node node; |
Patrick McHardy | fe2811e | 2015-03-25 13:07:50 +0000 | [diff] [blame] | 29 | struct nft_set_ext ext; |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 30 | }; |
| 31 | |
| 32 | static bool nft_rbtree_lookup(const struct nft_set *set, |
| 33 | const struct nft_data *key, |
Patrick McHardy | b2832dd | 2015-03-25 14:08:48 +0000 | [diff] [blame^] | 34 | const struct nft_set_ext **ext) |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 35 | { |
| 36 | const struct nft_rbtree *priv = nft_set_priv(set); |
| 37 | const struct nft_rbtree_elem *rbe, *interval = NULL; |
Patrick McHardy | 16c45ed | 2015-03-21 15:19:14 +0000 | [diff] [blame] | 38 | const struct rb_node *parent; |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 39 | int d; |
| 40 | |
Pablo Neira Ayuso | 7632667 | 2014-05-28 15:27:18 +0200 | [diff] [blame] | 41 | spin_lock_bh(&nft_rbtree_lock); |
Patrick McHardy | 16c45ed | 2015-03-21 15:19:14 +0000 | [diff] [blame] | 42 | parent = priv->root.rb_node; |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 43 | while (parent != NULL) { |
| 44 | rbe = rb_entry(parent, struct nft_rbtree_elem, node); |
| 45 | |
Patrick McHardy | fe2811e | 2015-03-25 13:07:50 +0000 | [diff] [blame] | 46 | d = nft_data_cmp(nft_set_ext_key(&rbe->ext), key, set->klen); |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 47 | if (d < 0) { |
| 48 | parent = parent->rb_left; |
| 49 | interval = rbe; |
| 50 | } else if (d > 0) |
| 51 | parent = parent->rb_right; |
| 52 | else { |
| 53 | found: |
Patrick McHardy | fe2811e | 2015-03-25 13:07:50 +0000 | [diff] [blame] | 54 | 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 McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 57 | goto out; |
Pablo Neira Ayuso | 7632667 | 2014-05-28 15:27:18 +0200 | [diff] [blame] | 58 | spin_unlock_bh(&nft_rbtree_lock); |
Patrick McHardy | b2832dd | 2015-03-25 14:08:48 +0000 | [diff] [blame^] | 59 | |
| 60 | *ext = &rbe->ext; |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 61 | return true; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | if (set->flags & NFT_SET_INTERVAL && interval != NULL) { |
| 66 | rbe = interval; |
| 67 | goto found; |
| 68 | } |
| 69 | out: |
Pablo Neira Ayuso | 7632667 | 2014-05-28 15:27:18 +0200 | [diff] [blame] | 70 | spin_unlock_bh(&nft_rbtree_lock); |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 71 | return false; |
| 72 | } |
| 73 | |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 74 | static 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 McHardy | fe2811e | 2015-03-25 13:07:50 +0000 | [diff] [blame] | 87 | d = nft_data_cmp(nft_set_ext_key(&rbe->ext), |
| 88 | nft_set_ext_key(&new->ext), |
| 89 | set->klen); |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 90 | 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 | |
| 102 | static int nft_rbtree_insert(const struct nft_set *set, |
| 103 | const struct nft_set_elem *elem) |
| 104 | { |
Patrick McHardy | fe2811e | 2015-03-25 13:07:50 +0000 | [diff] [blame] | 105 | struct nft_rbtree_elem *rbe = elem->priv; |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 106 | int err; |
| 107 | |
Pablo Neira Ayuso | 7632667 | 2014-05-28 15:27:18 +0200 | [diff] [blame] | 108 | spin_lock_bh(&nft_rbtree_lock); |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 109 | err = __nft_rbtree_insert(set, rbe); |
Pablo Neira Ayuso | 7632667 | 2014-05-28 15:27:18 +0200 | [diff] [blame] | 110 | spin_unlock_bh(&nft_rbtree_lock); |
Patrick McHardy | fe2811e | 2015-03-25 13:07:50 +0000 | [diff] [blame] | 111 | |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 112 | return err; |
| 113 | } |
| 114 | |
| 115 | static 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 Ayuso | 7632667 | 2014-05-28 15:27:18 +0200 | [diff] [blame] | 121 | spin_lock_bh(&nft_rbtree_lock); |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 122 | rb_erase(&rbe->node, &priv->root); |
Pablo Neira Ayuso | 7632667 | 2014-05-28 15:27:18 +0200 | [diff] [blame] | 123 | spin_unlock_bh(&nft_rbtree_lock); |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | static 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 McHardy | fe2811e | 2015-03-25 13:07:50 +0000 | [diff] [blame] | 136 | d = nft_data_cmp(nft_set_ext_key(&rbe->ext), &elem->key, |
| 137 | set->klen); |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 138 | 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 McHardy | fe2811e | 2015-03-25 13:07:50 +0000 | [diff] [blame] | 144 | elem->priv = rbe; |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 145 | return 0; |
| 146 | } |
| 147 | } |
| 148 | return -ENOENT; |
| 149 | } |
| 150 | |
| 151 | static 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 McHardy | fe2811e | 2015-03-25 13:07:50 +0000 | [diff] [blame] | 156 | struct nft_rbtree_elem *rbe; |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 157 | struct nft_set_elem elem; |
| 158 | struct rb_node *node; |
| 159 | |
Pablo Neira Ayuso | 7632667 | 2014-05-28 15:27:18 +0200 | [diff] [blame] | 160 | spin_lock_bh(&nft_rbtree_lock); |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 161 | 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 McHardy | fe2811e | 2015-03-25 13:07:50 +0000 | [diff] [blame] | 166 | elem.priv = rbe; |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 167 | |
| 168 | iter->err = iter->fn(ctx, set, iter, &elem); |
Pablo Neira Ayuso | 7632667 | 2014-05-28 15:27:18 +0200 | [diff] [blame] | 169 | if (iter->err < 0) { |
| 170 | spin_unlock_bh(&nft_rbtree_lock); |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 171 | return; |
Pablo Neira Ayuso | 7632667 | 2014-05-28 15:27:18 +0200 | [diff] [blame] | 172 | } |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 173 | cont: |
| 174 | iter->count++; |
| 175 | } |
Pablo Neira Ayuso | 7632667 | 2014-05-28 15:27:18 +0200 | [diff] [blame] | 176 | spin_unlock_bh(&nft_rbtree_lock); |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | static unsigned int nft_rbtree_privsize(const struct nlattr * const nla[]) |
| 180 | { |
| 181 | return sizeof(struct nft_rbtree); |
| 182 | } |
| 183 | |
| 184 | static int nft_rbtree_init(const struct nft_set *set, |
Patrick McHardy | c50b960 | 2014-03-28 10:19:47 +0000 | [diff] [blame] | 185 | const struct nft_set_desc *desc, |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 186 | 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 | |
| 194 | static 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 McHardy | 61edafb | 2015-03-25 14:08:47 +0000 | [diff] [blame] | 203 | nft_set_elem_destroy(set, rbe); |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 204 | } |
| 205 | } |
| 206 | |
Patrick McHardy | c50b960 | 2014-03-28 10:19:47 +0000 | [diff] [blame] | 207 | static 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 McHardy | c50b960 | 2014-03-28 10:19:47 +0000 | [diff] [blame] | 213 | 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 McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 223 | static struct nft_set_ops nft_rbtree_ops __read_mostly = { |
| 224 | .privsize = nft_rbtree_privsize, |
Patrick McHardy | fe2811e | 2015-03-25 13:07:50 +0000 | [diff] [blame] | 225 | .elemsize = offsetof(struct nft_rbtree_elem, ext), |
Patrick McHardy | c50b960 | 2014-03-28 10:19:47 +0000 | [diff] [blame] | 226 | .estimate = nft_rbtree_estimate, |
Patrick McHardy | 20a6934 | 2013-10-11 12:06:22 +0200 | [diff] [blame] | 227 | .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 | |
| 238 | static int __init nft_rbtree_module_init(void) |
| 239 | { |
| 240 | return nft_register_set(&nft_rbtree_ops); |
| 241 | } |
| 242 | |
| 243 | static void __exit nft_rbtree_module_exit(void) |
| 244 | { |
| 245 | nft_unregister_set(&nft_rbtree_ops); |
| 246 | } |
| 247 | |
| 248 | module_init(nft_rbtree_module_init); |
| 249 | module_exit(nft_rbtree_module_exit); |
| 250 | |
| 251 | MODULE_LICENSE("GPL"); |
| 252 | MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>"); |
| 253 | MODULE_ALIAS_NFT_SET(); |