nft_dynset.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /*
  2. * Copyright (c) 2015 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. */
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/init.h>
  12. #include <linux/netlink.h>
  13. #include <linux/netfilter.h>
  14. #include <linux/netfilter/nf_tables.h>
  15. #include <net/netfilter/nf_tables.h>
  16. #include <net/netfilter/nf_tables_core.h>
  17. struct nft_dynset {
  18. struct nft_set *set;
  19. struct nft_set_ext_tmpl tmpl;
  20. enum nft_dynset_ops op:8;
  21. enum nft_registers sreg_key:8;
  22. enum nft_registers sreg_data:8;
  23. bool invert;
  24. u64 timeout;
  25. struct nft_expr *expr;
  26. struct nft_set_binding binding;
  27. };
  28. static void *nft_dynset_new(struct nft_set *set, const struct nft_expr *expr,
  29. struct nft_regs *regs)
  30. {
  31. const struct nft_dynset *priv = nft_expr_priv(expr);
  32. struct nft_set_ext *ext;
  33. u64 timeout;
  34. void *elem;
  35. if (set->size && !atomic_add_unless(&set->nelems, 1, set->size))
  36. return NULL;
  37. timeout = priv->timeout ? : set->timeout;
  38. elem = nft_set_elem_init(set, &priv->tmpl,
  39. &regs->data[priv->sreg_key],
  40. &regs->data[priv->sreg_data],
  41. timeout, GFP_ATOMIC);
  42. if (elem == NULL)
  43. goto err1;
  44. ext = nft_set_elem_ext(set, elem);
  45. if (priv->expr != NULL &&
  46. nft_expr_clone(nft_set_ext_expr(ext), priv->expr) < 0)
  47. goto err2;
  48. return elem;
  49. err2:
  50. nft_set_elem_destroy(set, elem, false);
  51. err1:
  52. if (set->size)
  53. atomic_dec(&set->nelems);
  54. return NULL;
  55. }
  56. static void nft_dynset_eval(const struct nft_expr *expr,
  57. struct nft_regs *regs,
  58. const struct nft_pktinfo *pkt)
  59. {
  60. const struct nft_dynset *priv = nft_expr_priv(expr);
  61. struct nft_set *set = priv->set;
  62. const struct nft_set_ext *ext;
  63. const struct nft_expr *sexpr;
  64. u64 timeout;
  65. if (set->ops->update(set, &regs->data[priv->sreg_key], nft_dynset_new,
  66. expr, regs, &ext)) {
  67. sexpr = NULL;
  68. if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPR))
  69. sexpr = nft_set_ext_expr(ext);
  70. if (priv->op == NFT_DYNSET_OP_UPDATE &&
  71. nft_set_ext_exists(ext, NFT_SET_EXT_EXPIRATION)) {
  72. timeout = priv->timeout ? : set->timeout;
  73. *nft_set_ext_expiration(ext) = jiffies + timeout;
  74. }
  75. if (sexpr != NULL)
  76. sexpr->ops->eval(sexpr, regs, pkt);
  77. if (priv->invert)
  78. regs->verdict.code = NFT_BREAK;
  79. return;
  80. }
  81. if (!priv->invert)
  82. regs->verdict.code = NFT_BREAK;
  83. }
  84. static const struct nla_policy nft_dynset_policy[NFTA_DYNSET_MAX + 1] = {
  85. [NFTA_DYNSET_SET_NAME] = { .type = NLA_STRING },
  86. [NFTA_DYNSET_SET_ID] = { .type = NLA_U32 },
  87. [NFTA_DYNSET_OP] = { .type = NLA_U32 },
  88. [NFTA_DYNSET_SREG_KEY] = { .type = NLA_U32 },
  89. [NFTA_DYNSET_SREG_DATA] = { .type = NLA_U32 },
  90. [NFTA_DYNSET_TIMEOUT] = { .type = NLA_U64 },
  91. [NFTA_DYNSET_EXPR] = { .type = NLA_NESTED },
  92. [NFTA_DYNSET_FLAGS] = { .type = NLA_U32 },
  93. };
  94. static int nft_dynset_init(const struct nft_ctx *ctx,
  95. const struct nft_expr *expr,
  96. const struct nlattr * const tb[])
  97. {
  98. struct nft_dynset *priv = nft_expr_priv(expr);
  99. u8 genmask = nft_genmask_next(ctx->net);
  100. struct nft_set *set;
  101. u64 timeout;
  102. int err;
  103. if (tb[NFTA_DYNSET_SET_NAME] == NULL ||
  104. tb[NFTA_DYNSET_OP] == NULL ||
  105. tb[NFTA_DYNSET_SREG_KEY] == NULL)
  106. return -EINVAL;
  107. if (tb[NFTA_DYNSET_FLAGS]) {
  108. u32 flags = ntohl(nla_get_be32(tb[NFTA_DYNSET_FLAGS]));
  109. if (flags & ~NFT_DYNSET_F_INV)
  110. return -EINVAL;
  111. if (flags & NFT_DYNSET_F_INV)
  112. priv->invert = true;
  113. }
  114. set = nf_tables_set_lookup(ctx->table, tb[NFTA_DYNSET_SET_NAME],
  115. genmask);
  116. if (IS_ERR(set)) {
  117. if (tb[NFTA_DYNSET_SET_ID])
  118. set = nf_tables_set_lookup_byid(ctx->net,
  119. tb[NFTA_DYNSET_SET_ID],
  120. genmask);
  121. if (IS_ERR(set))
  122. return PTR_ERR(set);
  123. }
  124. if (set->ops->update == NULL)
  125. return -EOPNOTSUPP;
  126. if (set->flags & NFT_SET_CONSTANT)
  127. return -EBUSY;
  128. priv->op = ntohl(nla_get_be32(tb[NFTA_DYNSET_OP]));
  129. switch (priv->op) {
  130. case NFT_DYNSET_OP_ADD:
  131. break;
  132. case NFT_DYNSET_OP_UPDATE:
  133. if (!(set->flags & NFT_SET_TIMEOUT))
  134. return -EOPNOTSUPP;
  135. break;
  136. default:
  137. return -EOPNOTSUPP;
  138. }
  139. timeout = 0;
  140. if (tb[NFTA_DYNSET_TIMEOUT] != NULL) {
  141. if (!(set->flags & NFT_SET_TIMEOUT))
  142. return -EINVAL;
  143. timeout = msecs_to_jiffies(be64_to_cpu(nla_get_be64(
  144. tb[NFTA_DYNSET_TIMEOUT])));
  145. }
  146. priv->sreg_key = nft_parse_register(tb[NFTA_DYNSET_SREG_KEY]);
  147. err = nft_validate_register_load(priv->sreg_key, set->klen);;
  148. if (err < 0)
  149. return err;
  150. if (tb[NFTA_DYNSET_SREG_DATA] != NULL) {
  151. if (!(set->flags & NFT_SET_MAP))
  152. return -EINVAL;
  153. if (set->dtype == NFT_DATA_VERDICT)
  154. return -EOPNOTSUPP;
  155. priv->sreg_data = nft_parse_register(tb[NFTA_DYNSET_SREG_DATA]);
  156. err = nft_validate_register_load(priv->sreg_data, set->dlen);
  157. if (err < 0)
  158. return err;
  159. } else if (set->flags & NFT_SET_MAP)
  160. return -EINVAL;
  161. if (tb[NFTA_DYNSET_EXPR] != NULL) {
  162. if (!(set->flags & NFT_SET_EVAL))
  163. return -EINVAL;
  164. if (!(set->flags & NFT_SET_ANONYMOUS))
  165. return -EOPNOTSUPP;
  166. priv->expr = nft_expr_init(ctx, tb[NFTA_DYNSET_EXPR]);
  167. if (IS_ERR(priv->expr))
  168. return PTR_ERR(priv->expr);
  169. err = -EOPNOTSUPP;
  170. if (!(priv->expr->ops->type->flags & NFT_EXPR_STATEFUL))
  171. goto err1;
  172. } else if (set->flags & NFT_SET_EVAL)
  173. return -EINVAL;
  174. nft_set_ext_prepare(&priv->tmpl);
  175. nft_set_ext_add_length(&priv->tmpl, NFT_SET_EXT_KEY, set->klen);
  176. if (set->flags & NFT_SET_MAP)
  177. nft_set_ext_add_length(&priv->tmpl, NFT_SET_EXT_DATA, set->dlen);
  178. if (priv->expr != NULL)
  179. nft_set_ext_add_length(&priv->tmpl, NFT_SET_EXT_EXPR,
  180. priv->expr->ops->size);
  181. if (set->flags & NFT_SET_TIMEOUT) {
  182. if (timeout || set->timeout)
  183. nft_set_ext_add(&priv->tmpl, NFT_SET_EXT_EXPIRATION);
  184. }
  185. priv->timeout = timeout;
  186. err = nf_tables_bind_set(ctx, set, &priv->binding);
  187. if (err < 0)
  188. goto err1;
  189. priv->set = set;
  190. return 0;
  191. err1:
  192. if (priv->expr != NULL)
  193. nft_expr_destroy(ctx, priv->expr);
  194. return err;
  195. }
  196. static void nft_dynset_destroy(const struct nft_ctx *ctx,
  197. const struct nft_expr *expr)
  198. {
  199. struct nft_dynset *priv = nft_expr_priv(expr);
  200. nf_tables_unbind_set(ctx, priv->set, &priv->binding);
  201. if (priv->expr != NULL)
  202. nft_expr_destroy(ctx, priv->expr);
  203. }
  204. static int nft_dynset_dump(struct sk_buff *skb, const struct nft_expr *expr)
  205. {
  206. const struct nft_dynset *priv = nft_expr_priv(expr);
  207. u32 flags = priv->invert ? NFT_DYNSET_F_INV : 0;
  208. if (nft_dump_register(skb, NFTA_DYNSET_SREG_KEY, priv->sreg_key))
  209. goto nla_put_failure;
  210. if (priv->set->flags & NFT_SET_MAP &&
  211. nft_dump_register(skb, NFTA_DYNSET_SREG_DATA, priv->sreg_data))
  212. goto nla_put_failure;
  213. if (nla_put_be32(skb, NFTA_DYNSET_OP, htonl(priv->op)))
  214. goto nla_put_failure;
  215. if (nla_put_string(skb, NFTA_DYNSET_SET_NAME, priv->set->name))
  216. goto nla_put_failure;
  217. if (nla_put_be64(skb, NFTA_DYNSET_TIMEOUT,
  218. cpu_to_be64(jiffies_to_msecs(priv->timeout)),
  219. NFTA_DYNSET_PAD))
  220. goto nla_put_failure;
  221. if (priv->expr && nft_expr_dump(skb, NFTA_DYNSET_EXPR, priv->expr))
  222. goto nla_put_failure;
  223. if (nla_put_be32(skb, NFTA_DYNSET_FLAGS, htonl(flags)))
  224. goto nla_put_failure;
  225. return 0;
  226. nla_put_failure:
  227. return -1;
  228. }
  229. static struct nft_expr_type nft_dynset_type;
  230. static const struct nft_expr_ops nft_dynset_ops = {
  231. .type = &nft_dynset_type,
  232. .size = NFT_EXPR_SIZE(sizeof(struct nft_dynset)),
  233. .eval = nft_dynset_eval,
  234. .init = nft_dynset_init,
  235. .destroy = nft_dynset_destroy,
  236. .dump = nft_dynset_dump,
  237. };
  238. static struct nft_expr_type nft_dynset_type __read_mostly = {
  239. .name = "dynset",
  240. .ops = &nft_dynset_ops,
  241. .policy = nft_dynset_policy,
  242. .maxattr = NFTA_DYNSET_MAX,
  243. .owner = THIS_MODULE,
  244. };
  245. int __init nft_dynset_module_init(void)
  246. {
  247. return nft_register_expr(&nft_dynset_type);
  248. }
  249. void nft_dynset_module_exit(void)
  250. {
  251. nft_unregister_expr(&nft_dynset_type);
  252. }