nft_lookup.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2009 Patrick McHardy <kaber@trash.net>
  4. *
  5. * Development of this code funded by Astaro AG (http://www.astaro.com/)
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/init.h>
  9. #include <linux/list.h>
  10. #include <linux/rbtree.h>
  11. #include <linux/netlink.h>
  12. #include <linux/netfilter.h>
  13. #include <linux/netfilter/nf_tables.h>
  14. #include <net/netfilter/nf_tables.h>
  15. #include <net/netfilter/nf_tables_core.h>
  16. struct nft_lookup {
  17. struct nft_set *set;
  18. enum nft_registers sreg:8;
  19. enum nft_registers dreg:8;
  20. bool invert;
  21. struct nft_set_binding binding;
  22. };
  23. void nft_lookup_eval(const struct nft_expr *expr,
  24. struct nft_regs *regs,
  25. const struct nft_pktinfo *pkt)
  26. {
  27. const struct nft_lookup *priv = nft_expr_priv(expr);
  28. const struct nft_set *set = priv->set;
  29. const struct nft_set_ext *ext = NULL;
  30. bool found;
  31. found = set->ops->lookup(nft_net(pkt), set, &regs->data[priv->sreg],
  32. &ext) ^ priv->invert;
  33. if (!found) {
  34. regs->verdict.code = NFT_BREAK;
  35. return;
  36. }
  37. if (ext) {
  38. if (set->flags & NFT_SET_MAP)
  39. nft_data_copy(&regs->data[priv->dreg],
  40. nft_set_ext_data(ext), set->dlen);
  41. nft_set_elem_update_expr(ext, regs, pkt);
  42. }
  43. }
  44. static const struct nla_policy nft_lookup_policy[NFTA_LOOKUP_MAX + 1] = {
  45. [NFTA_LOOKUP_SET] = { .type = NLA_STRING,
  46. .len = NFT_SET_MAXNAMELEN - 1 },
  47. [NFTA_LOOKUP_SET_ID] = { .type = NLA_U32 },
  48. [NFTA_LOOKUP_SREG] = { .type = NLA_U32 },
  49. [NFTA_LOOKUP_DREG] = { .type = NLA_U32 },
  50. [NFTA_LOOKUP_FLAGS] = { .type = NLA_U32 },
  51. };
  52. static int nft_lookup_init(const struct nft_ctx *ctx,
  53. const struct nft_expr *expr,
  54. const struct nlattr * const tb[])
  55. {
  56. struct nft_lookup *priv = nft_expr_priv(expr);
  57. u8 genmask = nft_genmask_next(ctx->net);
  58. struct nft_set *set;
  59. u32 flags;
  60. int err;
  61. if (tb[NFTA_LOOKUP_SET] == NULL ||
  62. tb[NFTA_LOOKUP_SREG] == NULL)
  63. return -EINVAL;
  64. set = nft_set_lookup_global(ctx->net, ctx->table, tb[NFTA_LOOKUP_SET],
  65. tb[NFTA_LOOKUP_SET_ID], genmask);
  66. if (IS_ERR(set))
  67. return PTR_ERR(set);
  68. priv->sreg = nft_parse_register(tb[NFTA_LOOKUP_SREG]);
  69. err = nft_validate_register_load(priv->sreg, set->klen);
  70. if (err < 0)
  71. return err;
  72. if (tb[NFTA_LOOKUP_FLAGS]) {
  73. flags = ntohl(nla_get_be32(tb[NFTA_LOOKUP_FLAGS]));
  74. if (flags & ~NFT_LOOKUP_F_INV)
  75. return -EINVAL;
  76. if (flags & NFT_LOOKUP_F_INV) {
  77. if (set->flags & NFT_SET_MAP)
  78. return -EINVAL;
  79. priv->invert = true;
  80. }
  81. }
  82. if (tb[NFTA_LOOKUP_DREG] != NULL) {
  83. if (priv->invert)
  84. return -EINVAL;
  85. if (!(set->flags & NFT_SET_MAP))
  86. return -EINVAL;
  87. priv->dreg = nft_parse_register(tb[NFTA_LOOKUP_DREG]);
  88. err = nft_validate_register_store(ctx, priv->dreg, NULL,
  89. set->dtype, set->dlen);
  90. if (err < 0)
  91. return err;
  92. } else if (set->flags & NFT_SET_MAP)
  93. return -EINVAL;
  94. priv->binding.flags = set->flags & NFT_SET_MAP;
  95. err = nf_tables_bind_set(ctx, set, &priv->binding);
  96. if (err < 0)
  97. return err;
  98. priv->set = set;
  99. return 0;
  100. }
  101. static void nft_lookup_deactivate(const struct nft_ctx *ctx,
  102. const struct nft_expr *expr,
  103. enum nft_trans_phase phase)
  104. {
  105. struct nft_lookup *priv = nft_expr_priv(expr);
  106. nf_tables_deactivate_set(ctx, priv->set, &priv->binding, phase);
  107. }
  108. static void nft_lookup_activate(const struct nft_ctx *ctx,
  109. const struct nft_expr *expr)
  110. {
  111. struct nft_lookup *priv = nft_expr_priv(expr);
  112. priv->set->use++;
  113. }
  114. static void nft_lookup_destroy(const struct nft_ctx *ctx,
  115. const struct nft_expr *expr)
  116. {
  117. struct nft_lookup *priv = nft_expr_priv(expr);
  118. nf_tables_destroy_set(ctx, priv->set);
  119. }
  120. static int nft_lookup_dump(struct sk_buff *skb, const struct nft_expr *expr)
  121. {
  122. const struct nft_lookup *priv = nft_expr_priv(expr);
  123. u32 flags = priv->invert ? NFT_LOOKUP_F_INV : 0;
  124. if (nla_put_string(skb, NFTA_LOOKUP_SET, priv->set->name))
  125. goto nla_put_failure;
  126. if (nft_dump_register(skb, NFTA_LOOKUP_SREG, priv->sreg))
  127. goto nla_put_failure;
  128. if (priv->set->flags & NFT_SET_MAP)
  129. if (nft_dump_register(skb, NFTA_LOOKUP_DREG, priv->dreg))
  130. goto nla_put_failure;
  131. if (nla_put_be32(skb, NFTA_LOOKUP_FLAGS, htonl(flags)))
  132. goto nla_put_failure;
  133. return 0;
  134. nla_put_failure:
  135. return -1;
  136. }
  137. static int nft_lookup_validate_setelem(const struct nft_ctx *ctx,
  138. struct nft_set *set,
  139. const struct nft_set_iter *iter,
  140. struct nft_set_elem *elem)
  141. {
  142. const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv);
  143. struct nft_ctx *pctx = (struct nft_ctx *)ctx;
  144. const struct nft_data *data;
  145. int err;
  146. if (nft_set_ext_exists(ext, NFT_SET_EXT_FLAGS) &&
  147. *nft_set_ext_flags(ext) & NFT_SET_ELEM_INTERVAL_END)
  148. return 0;
  149. data = nft_set_ext_data(ext);
  150. switch (data->verdict.code) {
  151. case NFT_JUMP:
  152. case NFT_GOTO:
  153. pctx->level++;
  154. err = nft_chain_validate(ctx, data->verdict.chain);
  155. if (err < 0)
  156. return err;
  157. pctx->level--;
  158. break;
  159. default:
  160. break;
  161. }
  162. return 0;
  163. }
  164. static int nft_lookup_validate(const struct nft_ctx *ctx,
  165. const struct nft_expr *expr,
  166. const struct nft_data **d)
  167. {
  168. const struct nft_lookup *priv = nft_expr_priv(expr);
  169. struct nft_set_iter iter;
  170. if (!(priv->set->flags & NFT_SET_MAP) ||
  171. priv->set->dtype != NFT_DATA_VERDICT)
  172. return 0;
  173. iter.genmask = nft_genmask_next(ctx->net);
  174. iter.skip = 0;
  175. iter.count = 0;
  176. iter.err = 0;
  177. iter.fn = nft_lookup_validate_setelem;
  178. priv->set->ops->walk(ctx, priv->set, &iter);
  179. if (iter.err < 0)
  180. return iter.err;
  181. return 0;
  182. }
  183. static const struct nft_expr_ops nft_lookup_ops = {
  184. .type = &nft_lookup_type,
  185. .size = NFT_EXPR_SIZE(sizeof(struct nft_lookup)),
  186. .eval = nft_lookup_eval,
  187. .init = nft_lookup_init,
  188. .activate = nft_lookup_activate,
  189. .deactivate = nft_lookup_deactivate,
  190. .destroy = nft_lookup_destroy,
  191. .dump = nft_lookup_dump,
  192. .validate = nft_lookup_validate,
  193. };
  194. struct nft_expr_type nft_lookup_type __read_mostly = {
  195. .name = "lookup",
  196. .ops = &nft_lookup_ops,
  197. .policy = nft_lookup_policy,
  198. .maxattr = NFTA_LOOKUP_MAX,
  199. .owner = THIS_MODULE,
  200. };