nft_immediate.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * Copyright (c) 2008-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/module.h>
  13. #include <linux/netlink.h>
  14. #include <linux/netfilter.h>
  15. #include <linux/netfilter/nf_tables.h>
  16. #include <net/netfilter/nf_tables_core.h>
  17. #include <net/netfilter/nf_tables.h>
  18. struct nft_immediate_expr {
  19. struct nft_data data;
  20. enum nft_registers dreg:8;
  21. u8 dlen;
  22. };
  23. static void nft_immediate_eval(const struct nft_expr *expr,
  24. struct nft_data data[NFT_REG_MAX + 1],
  25. const struct nft_pktinfo *pkt)
  26. {
  27. const struct nft_immediate_expr *priv = nft_expr_priv(expr);
  28. nft_data_copy(&data[priv->dreg], &priv->data);
  29. }
  30. static const struct nla_policy nft_immediate_policy[NFTA_IMMEDIATE_MAX + 1] = {
  31. [NFTA_IMMEDIATE_DREG] = { .type = NLA_U32 },
  32. [NFTA_IMMEDIATE_DATA] = { .type = NLA_NESTED },
  33. };
  34. static int nft_immediate_init(const struct nft_ctx *ctx,
  35. const struct nft_expr *expr,
  36. const struct nlattr * const tb[])
  37. {
  38. struct nft_immediate_expr *priv = nft_expr_priv(expr);
  39. struct nft_data_desc desc;
  40. int err;
  41. if (tb[NFTA_IMMEDIATE_DREG] == NULL ||
  42. tb[NFTA_IMMEDIATE_DATA] == NULL)
  43. return -EINVAL;
  44. priv->dreg = ntohl(nla_get_be32(tb[NFTA_IMMEDIATE_DREG]));
  45. err = nft_validate_output_register(priv->dreg);
  46. if (err < 0)
  47. return err;
  48. err = nft_data_init(ctx, &priv->data, &desc, tb[NFTA_IMMEDIATE_DATA]);
  49. if (err < 0)
  50. return err;
  51. priv->dlen = desc.len;
  52. err = nft_validate_data_load(ctx, priv->dreg, &priv->data, desc.type);
  53. if (err < 0)
  54. goto err1;
  55. return 0;
  56. err1:
  57. nft_data_uninit(&priv->data, desc.type);
  58. return err;
  59. }
  60. static void nft_immediate_destroy(const struct nft_ctx *ctx,
  61. const struct nft_expr *expr)
  62. {
  63. const struct nft_immediate_expr *priv = nft_expr_priv(expr);
  64. return nft_data_uninit(&priv->data, nft_dreg_to_type(priv->dreg));
  65. }
  66. static int nft_immediate_dump(struct sk_buff *skb, const struct nft_expr *expr)
  67. {
  68. const struct nft_immediate_expr *priv = nft_expr_priv(expr);
  69. if (nla_put_be32(skb, NFTA_IMMEDIATE_DREG, htonl(priv->dreg)))
  70. goto nla_put_failure;
  71. return nft_data_dump(skb, NFTA_IMMEDIATE_DATA, &priv->data,
  72. nft_dreg_to_type(priv->dreg), priv->dlen);
  73. nla_put_failure:
  74. return -1;
  75. }
  76. static int nft_immediate_validate(const struct nft_ctx *ctx,
  77. const struct nft_expr *expr,
  78. const struct nft_data **data)
  79. {
  80. const struct nft_immediate_expr *priv = nft_expr_priv(expr);
  81. if (priv->dreg == NFT_REG_VERDICT)
  82. *data = &priv->data;
  83. return 0;
  84. }
  85. static struct nft_expr_type nft_imm_type;
  86. static const struct nft_expr_ops nft_imm_ops = {
  87. .type = &nft_imm_type,
  88. .size = NFT_EXPR_SIZE(sizeof(struct nft_immediate_expr)),
  89. .eval = nft_immediate_eval,
  90. .init = nft_immediate_init,
  91. .destroy = nft_immediate_destroy,
  92. .dump = nft_immediate_dump,
  93. .validate = nft_immediate_validate,
  94. };
  95. static struct nft_expr_type nft_imm_type __read_mostly = {
  96. .name = "immediate",
  97. .ops = &nft_imm_ops,
  98. .policy = nft_immediate_policy,
  99. .maxattr = NFTA_IMMEDIATE_MAX,
  100. .owner = THIS_MODULE,
  101. };
  102. int __init nft_immediate_module_init(void)
  103. {
  104. return nft_register_expr(&nft_imm_type);
  105. }
  106. void nft_immediate_module_exit(void)
  107. {
  108. nft_unregister_expr(&nft_imm_type);
  109. }