nft_lookup.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. struct nft_set_binding binding;
  24. };
  25. static void nft_lookup_eval(const struct nft_expr *expr,
  26. struct nft_data data[NFT_REG_MAX + 1],
  27. const struct nft_pktinfo *pkt)
  28. {
  29. const struct nft_lookup *priv = nft_expr_priv(expr);
  30. const struct nft_set *set = priv->set;
  31. if (set->ops->lookup(set, &data[priv->sreg], &data[priv->dreg]))
  32. return;
  33. data[NFT_REG_VERDICT].verdict = NFT_BREAK;
  34. }
  35. static const struct nla_policy nft_lookup_policy[NFTA_LOOKUP_MAX + 1] = {
  36. [NFTA_LOOKUP_SET] = { .type = NLA_STRING },
  37. [NFTA_LOOKUP_SREG] = { .type = NLA_U32 },
  38. [NFTA_LOOKUP_DREG] = { .type = NLA_U32 },
  39. };
  40. static int nft_lookup_init(const struct nft_ctx *ctx,
  41. const struct nft_expr *expr,
  42. const struct nlattr * const tb[])
  43. {
  44. struct nft_lookup *priv = nft_expr_priv(expr);
  45. struct nft_set *set;
  46. int err;
  47. if (tb[NFTA_LOOKUP_SET] == NULL ||
  48. tb[NFTA_LOOKUP_SREG] == NULL)
  49. return -EINVAL;
  50. set = nf_tables_set_lookup(ctx->table, tb[NFTA_LOOKUP_SET]);
  51. if (IS_ERR(set)) {
  52. if (tb[NFTA_LOOKUP_SET_ID]) {
  53. set = nf_tables_set_lookup_byid(ctx->net,
  54. tb[NFTA_LOOKUP_SET_ID]);
  55. }
  56. if (IS_ERR(set))
  57. return PTR_ERR(set);
  58. }
  59. priv->sreg = ntohl(nla_get_be32(tb[NFTA_LOOKUP_SREG]));
  60. err = nft_validate_input_register(priv->sreg);
  61. if (err < 0)
  62. return err;
  63. if (tb[NFTA_LOOKUP_DREG] != NULL) {
  64. if (!(set->flags & NFT_SET_MAP))
  65. return -EINVAL;
  66. priv->dreg = ntohl(nla_get_be32(tb[NFTA_LOOKUP_DREG]));
  67. err = nft_validate_output_register(priv->dreg);
  68. if (err < 0)
  69. return err;
  70. if (priv->dreg == NFT_REG_VERDICT) {
  71. if (set->dtype != NFT_DATA_VERDICT)
  72. return -EINVAL;
  73. } else if (set->dtype == NFT_DATA_VERDICT)
  74. return -EINVAL;
  75. } else if (set->flags & NFT_SET_MAP)
  76. return -EINVAL;
  77. err = nf_tables_bind_set(ctx, set, &priv->binding);
  78. if (err < 0)
  79. return err;
  80. priv->set = set;
  81. return 0;
  82. }
  83. static void nft_lookup_destroy(const struct nft_ctx *ctx,
  84. const struct nft_expr *expr)
  85. {
  86. struct nft_lookup *priv = nft_expr_priv(expr);
  87. nf_tables_unbind_set(ctx, priv->set, &priv->binding);
  88. }
  89. static int nft_lookup_dump(struct sk_buff *skb, const struct nft_expr *expr)
  90. {
  91. const struct nft_lookup *priv = nft_expr_priv(expr);
  92. if (nla_put_string(skb, NFTA_LOOKUP_SET, priv->set->name))
  93. goto nla_put_failure;
  94. if (nla_put_be32(skb, NFTA_LOOKUP_SREG, htonl(priv->sreg)))
  95. goto nla_put_failure;
  96. if (priv->set->flags & NFT_SET_MAP)
  97. if (nla_put_be32(skb, NFTA_LOOKUP_DREG, htonl(priv->dreg)))
  98. goto nla_put_failure;
  99. return 0;
  100. nla_put_failure:
  101. return -1;
  102. }
  103. static struct nft_expr_type nft_lookup_type;
  104. static const struct nft_expr_ops nft_lookup_ops = {
  105. .type = &nft_lookup_type,
  106. .size = NFT_EXPR_SIZE(sizeof(struct nft_lookup)),
  107. .eval = nft_lookup_eval,
  108. .init = nft_lookup_init,
  109. .destroy = nft_lookup_destroy,
  110. .dump = nft_lookup_dump,
  111. };
  112. static struct nft_expr_type nft_lookup_type __read_mostly = {
  113. .name = "lookup",
  114. .ops = &nft_lookup_ops,
  115. .policy = nft_lookup_policy,
  116. .maxattr = NFTA_LOOKUP_MAX,
  117. .owner = THIS_MODULE,
  118. };
  119. int __init nft_lookup_module_init(void)
  120. {
  121. return nft_register_expr(&nft_lookup_type);
  122. }
  123. void nft_lookup_module_exit(void)
  124. {
  125. nft_unregister_expr(&nft_lookup_type);
  126. }