nft_lookup.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. * Copyright (c) 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/list.h>
  13. #include <linux/rbtree.h>
  14. #include <linux/netlink.h>
  15. #include <linux/netfilter.h>
  16. #include <linux/netfilter/nf_tables.h>
  17. #include <net/netfilter/nf_tables.h>
  18. #include <net/netfilter/nf_tables_core.h>
  19. struct nft_lookup {
  20. struct nft_set *set;
  21. enum nft_registers sreg:8;
  22. enum nft_registers dreg:8;
  23. bool invert;
  24. struct nft_set_binding binding;
  25. };
  26. static void nft_lookup_eval(const struct nft_expr *expr,
  27. struct nft_regs *regs,
  28. const struct nft_pktinfo *pkt)
  29. {
  30. const struct nft_lookup *priv = nft_expr_priv(expr);
  31. const struct nft_set *set = priv->set;
  32. const struct nft_set_ext *ext;
  33. bool found;
  34. found = set->ops->lookup(pkt->net, set, &regs->data[priv->sreg], &ext) ^
  35. priv->invert;
  36. if (!found) {
  37. regs->verdict.code = NFT_BREAK;
  38. return;
  39. }
  40. if (set->flags & NFT_SET_MAP)
  41. nft_data_copy(&regs->data[priv->dreg],
  42. nft_set_ext_data(ext), set->dlen);
  43. }
  44. static const struct nla_policy nft_lookup_policy[NFTA_LOOKUP_MAX + 1] = {
  45. [NFTA_LOOKUP_SET] = { .type = NLA_STRING },
  46. [NFTA_LOOKUP_SET_ID] = { .type = NLA_U32 },
  47. [NFTA_LOOKUP_SREG] = { .type = NLA_U32 },
  48. [NFTA_LOOKUP_DREG] = { .type = NLA_U32 },
  49. [NFTA_LOOKUP_FLAGS] = { .type = NLA_U32 },
  50. };
  51. static int nft_lookup_init(const struct nft_ctx *ctx,
  52. const struct nft_expr *expr,
  53. const struct nlattr * const tb[])
  54. {
  55. struct nft_lookup *priv = nft_expr_priv(expr);
  56. u8 genmask = nft_genmask_next(ctx->net);
  57. struct nft_set *set;
  58. u32 flags;
  59. int err;
  60. if (tb[NFTA_LOOKUP_SET] == NULL ||
  61. tb[NFTA_LOOKUP_SREG] == NULL)
  62. return -EINVAL;
  63. set = nf_tables_set_lookup(ctx->table, tb[NFTA_LOOKUP_SET], genmask);
  64. if (IS_ERR(set)) {
  65. if (tb[NFTA_LOOKUP_SET_ID]) {
  66. set = nf_tables_set_lookup_byid(ctx->net,
  67. tb[NFTA_LOOKUP_SET_ID],
  68. genmask);
  69. }
  70. if (IS_ERR(set))
  71. return PTR_ERR(set);
  72. }
  73. if (set->flags & NFT_SET_EVAL)
  74. return -EOPNOTSUPP;
  75. priv->sreg = nft_parse_register(tb[NFTA_LOOKUP_SREG]);
  76. err = nft_validate_register_load(priv->sreg, set->klen);
  77. if (err < 0)
  78. return err;
  79. if (tb[NFTA_LOOKUP_FLAGS]) {
  80. flags = ntohl(nla_get_be32(tb[NFTA_LOOKUP_FLAGS]));
  81. if (flags & ~NFT_LOOKUP_F_INV)
  82. return -EINVAL;
  83. if (flags & NFT_LOOKUP_F_INV) {
  84. if (set->flags & NFT_SET_MAP)
  85. return -EINVAL;
  86. priv->invert = true;
  87. }
  88. }
  89. if (tb[NFTA_LOOKUP_DREG] != NULL) {
  90. if (priv->invert)
  91. return -EINVAL;
  92. if (!(set->flags & NFT_SET_MAP))
  93. return -EINVAL;
  94. priv->dreg = nft_parse_register(tb[NFTA_LOOKUP_DREG]);
  95. err = nft_validate_register_store(ctx, priv->dreg, NULL,
  96. set->dtype, set->dlen);
  97. if (err < 0)
  98. return err;
  99. } else if (set->flags & NFT_SET_MAP)
  100. return -EINVAL;
  101. priv->binding.flags = set->flags & NFT_SET_MAP;
  102. err = nf_tables_bind_set(ctx, set, &priv->binding);
  103. if (err < 0)
  104. return err;
  105. priv->set = set;
  106. return 0;
  107. }
  108. static void nft_lookup_destroy(const struct nft_ctx *ctx,
  109. const struct nft_expr *expr)
  110. {
  111. struct nft_lookup *priv = nft_expr_priv(expr);
  112. nf_tables_unbind_set(ctx, priv->set, &priv->binding);
  113. }
  114. static int nft_lookup_dump(struct sk_buff *skb, const struct nft_expr *expr)
  115. {
  116. const struct nft_lookup *priv = nft_expr_priv(expr);
  117. u32 flags = priv->invert ? NFT_LOOKUP_F_INV : 0;
  118. if (nla_put_string(skb, NFTA_LOOKUP_SET, priv->set->name))
  119. goto nla_put_failure;
  120. if (nft_dump_register(skb, NFTA_LOOKUP_SREG, priv->sreg))
  121. goto nla_put_failure;
  122. if (priv->set->flags & NFT_SET_MAP)
  123. if (nft_dump_register(skb, NFTA_LOOKUP_DREG, priv->dreg))
  124. goto nla_put_failure;
  125. if (nla_put_be32(skb, NFTA_LOOKUP_FLAGS, htonl(flags)))
  126. goto nla_put_failure;
  127. return 0;
  128. nla_put_failure:
  129. return -1;
  130. }
  131. static struct nft_expr_type nft_lookup_type;
  132. static const struct nft_expr_ops nft_lookup_ops = {
  133. .type = &nft_lookup_type,
  134. .size = NFT_EXPR_SIZE(sizeof(struct nft_lookup)),
  135. .eval = nft_lookup_eval,
  136. .init = nft_lookup_init,
  137. .destroy = nft_lookup_destroy,
  138. .dump = nft_lookup_dump,
  139. };
  140. static struct nft_expr_type nft_lookup_type __read_mostly = {
  141. .name = "lookup",
  142. .ops = &nft_lookup_ops,
  143. .policy = nft_lookup_policy,
  144. .maxattr = NFTA_LOOKUP_MAX,
  145. .owner = THIS_MODULE,
  146. };
  147. int __init nft_lookup_module_init(void)
  148. {
  149. return nft_register_expr(&nft_lookup_type);
  150. }
  151. void nft_lookup_module_exit(void)
  152. {
  153. nft_unregister_expr(&nft_lookup_type);
  154. }