nft_rbtree.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. #include <linux/kernel.h>
  11. #include <linux/init.h>
  12. #include <linux/module.h>
  13. #include <linux/list.h>
  14. #include <linux/rbtree.h>
  15. #include <linux/netlink.h>
  16. #include <linux/netfilter.h>
  17. #include <linux/netfilter/nf_tables.h>
  18. #include <net/netfilter/nf_tables.h>
  19. static DEFINE_SPINLOCK(nft_rbtree_lock);
  20. struct nft_rbtree {
  21. struct rb_root root;
  22. };
  23. struct nft_rbtree_elem {
  24. struct rb_node node;
  25. struct nft_set_ext ext;
  26. };
  27. static bool nft_rbtree_lookup(const struct nft_set *set, const u32 *key,
  28. const struct nft_set_ext **ext)
  29. {
  30. const struct nft_rbtree *priv = nft_set_priv(set);
  31. const struct nft_rbtree_elem *rbe, *interval = NULL;
  32. const struct rb_node *parent;
  33. u8 genmask = nft_genmask_cur(read_pnet(&set->pnet));
  34. int d;
  35. spin_lock_bh(&nft_rbtree_lock);
  36. parent = priv->root.rb_node;
  37. while (parent != NULL) {
  38. rbe = rb_entry(parent, struct nft_rbtree_elem, node);
  39. d = memcmp(nft_set_ext_key(&rbe->ext), key, set->klen);
  40. if (d < 0) {
  41. parent = parent->rb_left;
  42. interval = rbe;
  43. } else if (d > 0)
  44. parent = parent->rb_right;
  45. else {
  46. found:
  47. if (!nft_set_elem_active(&rbe->ext, genmask)) {
  48. parent = parent->rb_left;
  49. continue;
  50. }
  51. if (nft_set_ext_exists(&rbe->ext, NFT_SET_EXT_FLAGS) &&
  52. *nft_set_ext_flags(&rbe->ext) &
  53. NFT_SET_ELEM_INTERVAL_END)
  54. goto out;
  55. spin_unlock_bh(&nft_rbtree_lock);
  56. *ext = &rbe->ext;
  57. return true;
  58. }
  59. }
  60. if (set->flags & NFT_SET_INTERVAL && interval != NULL) {
  61. rbe = interval;
  62. goto found;
  63. }
  64. out:
  65. spin_unlock_bh(&nft_rbtree_lock);
  66. return false;
  67. }
  68. static int __nft_rbtree_insert(const struct nft_set *set,
  69. struct nft_rbtree_elem *new)
  70. {
  71. struct nft_rbtree *priv = nft_set_priv(set);
  72. struct nft_rbtree_elem *rbe;
  73. struct rb_node *parent, **p;
  74. u8 genmask = nft_genmask_next(read_pnet(&set->pnet));
  75. int d;
  76. parent = NULL;
  77. p = &priv->root.rb_node;
  78. while (*p != NULL) {
  79. parent = *p;
  80. rbe = rb_entry(parent, struct nft_rbtree_elem, node);
  81. d = memcmp(nft_set_ext_key(&rbe->ext),
  82. nft_set_ext_key(&new->ext),
  83. set->klen);
  84. if (d < 0)
  85. p = &parent->rb_left;
  86. else if (d > 0)
  87. p = &parent->rb_right;
  88. else {
  89. if (nft_set_elem_active(&rbe->ext, genmask))
  90. return -EEXIST;
  91. p = &parent->rb_left;
  92. }
  93. }
  94. rb_link_node(&new->node, parent, p);
  95. rb_insert_color(&new->node, &priv->root);
  96. return 0;
  97. }
  98. static int nft_rbtree_insert(const struct nft_set *set,
  99. const struct nft_set_elem *elem)
  100. {
  101. struct nft_rbtree_elem *rbe = elem->priv;
  102. int err;
  103. spin_lock_bh(&nft_rbtree_lock);
  104. err = __nft_rbtree_insert(set, rbe);
  105. spin_unlock_bh(&nft_rbtree_lock);
  106. return err;
  107. }
  108. static void nft_rbtree_remove(const struct nft_set *set,
  109. const struct nft_set_elem *elem)
  110. {
  111. struct nft_rbtree *priv = nft_set_priv(set);
  112. struct nft_rbtree_elem *rbe = elem->priv;
  113. spin_lock_bh(&nft_rbtree_lock);
  114. rb_erase(&rbe->node, &priv->root);
  115. spin_unlock_bh(&nft_rbtree_lock);
  116. }
  117. static void nft_rbtree_activate(const struct nft_set *set,
  118. const struct nft_set_elem *elem)
  119. {
  120. struct nft_rbtree_elem *rbe = elem->priv;
  121. nft_set_elem_change_active(set, &rbe->ext);
  122. }
  123. static void *nft_rbtree_deactivate(const struct nft_set *set,
  124. const struct nft_set_elem *elem)
  125. {
  126. const struct nft_rbtree *priv = nft_set_priv(set);
  127. const struct rb_node *parent = priv->root.rb_node;
  128. struct nft_rbtree_elem *rbe;
  129. u8 genmask = nft_genmask_cur(read_pnet(&set->pnet));
  130. int d;
  131. while (parent != NULL) {
  132. rbe = rb_entry(parent, struct nft_rbtree_elem, node);
  133. d = memcmp(nft_set_ext_key(&rbe->ext), &elem->key.val,
  134. set->klen);
  135. if (d < 0)
  136. parent = parent->rb_left;
  137. else if (d > 0)
  138. parent = parent->rb_right;
  139. else {
  140. if (!nft_set_elem_active(&rbe->ext, genmask)) {
  141. parent = parent->rb_left;
  142. continue;
  143. }
  144. nft_set_elem_change_active(set, &rbe->ext);
  145. return rbe;
  146. }
  147. }
  148. return NULL;
  149. }
  150. static void nft_rbtree_walk(const struct nft_ctx *ctx,
  151. const struct nft_set *set,
  152. struct nft_set_iter *iter)
  153. {
  154. const struct nft_rbtree *priv = nft_set_priv(set);
  155. struct nft_rbtree_elem *rbe;
  156. struct nft_set_elem elem;
  157. struct rb_node *node;
  158. u8 genmask = nft_genmask_cur(read_pnet(&set->pnet));
  159. spin_lock_bh(&nft_rbtree_lock);
  160. for (node = rb_first(&priv->root); node != NULL; node = rb_next(node)) {
  161. rbe = rb_entry(node, struct nft_rbtree_elem, node);
  162. if (iter->count < iter->skip)
  163. goto cont;
  164. if (!nft_set_elem_active(&rbe->ext, genmask))
  165. goto cont;
  166. elem.priv = rbe;
  167. iter->err = iter->fn(ctx, set, iter, &elem);
  168. if (iter->err < 0) {
  169. spin_unlock_bh(&nft_rbtree_lock);
  170. return;
  171. }
  172. cont:
  173. iter->count++;
  174. }
  175. spin_unlock_bh(&nft_rbtree_lock);
  176. }
  177. static unsigned int nft_rbtree_privsize(const struct nlattr * const nla[])
  178. {
  179. return sizeof(struct nft_rbtree);
  180. }
  181. static int nft_rbtree_init(const struct nft_set *set,
  182. const struct nft_set_desc *desc,
  183. const struct nlattr * const nla[])
  184. {
  185. struct nft_rbtree *priv = nft_set_priv(set);
  186. priv->root = RB_ROOT;
  187. return 0;
  188. }
  189. static void nft_rbtree_destroy(const struct nft_set *set)
  190. {
  191. struct nft_rbtree *priv = nft_set_priv(set);
  192. struct nft_rbtree_elem *rbe;
  193. struct rb_node *node;
  194. while ((node = priv->root.rb_node) != NULL) {
  195. rb_erase(node, &priv->root);
  196. rbe = rb_entry(node, struct nft_rbtree_elem, node);
  197. nft_set_elem_destroy(set, rbe);
  198. }
  199. }
  200. static bool nft_rbtree_estimate(const struct nft_set_desc *desc, u32 features,
  201. struct nft_set_estimate *est)
  202. {
  203. unsigned int nsize;
  204. nsize = sizeof(struct nft_rbtree_elem);
  205. if (desc->size)
  206. est->size = sizeof(struct nft_rbtree) + desc->size * nsize;
  207. else
  208. est->size = nsize;
  209. est->class = NFT_SET_CLASS_O_LOG_N;
  210. return true;
  211. }
  212. static struct nft_set_ops nft_rbtree_ops __read_mostly = {
  213. .privsize = nft_rbtree_privsize,
  214. .elemsize = offsetof(struct nft_rbtree_elem, ext),
  215. .estimate = nft_rbtree_estimate,
  216. .init = nft_rbtree_init,
  217. .destroy = nft_rbtree_destroy,
  218. .insert = nft_rbtree_insert,
  219. .remove = nft_rbtree_remove,
  220. .deactivate = nft_rbtree_deactivate,
  221. .activate = nft_rbtree_activate,
  222. .lookup = nft_rbtree_lookup,
  223. .walk = nft_rbtree_walk,
  224. .features = NFT_SET_INTERVAL | NFT_SET_MAP,
  225. .owner = THIS_MODULE,
  226. };
  227. static int __init nft_rbtree_module_init(void)
  228. {
  229. return nft_register_set(&nft_rbtree_ops);
  230. }
  231. static void __exit nft_rbtree_module_exit(void)
  232. {
  233. nft_unregister_set(&nft_rbtree_ops);
  234. }
  235. module_init(nft_rbtree_module_init);
  236. module_exit(nft_rbtree_module_exit);
  237. MODULE_LICENSE("GPL");
  238. MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
  239. MODULE_ALIAS_NFT_SET();