nft_lookup.c 4.0 KB

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