nft_immediate.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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_regs *regs,
  25. const struct nft_pktinfo *pkt)
  26. {
  27. const struct nft_immediate_expr *priv = nft_expr_priv(expr);
  28. nft_data_copy(&regs->data[priv->dreg], &priv->data, priv->dlen);
  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. err = nft_data_init(ctx, &priv->data, sizeof(priv->data), &desc,
  45. tb[NFTA_IMMEDIATE_DATA]);
  46. if (err < 0)
  47. return err;
  48. if (desc.len > U8_MAX)
  49. return -ERANGE;
  50. priv->dlen = desc.len;
  51. priv->dreg = nft_parse_register(tb[NFTA_IMMEDIATE_DREG]);
  52. err = nft_validate_register_store(ctx, priv->dreg, &priv->data,
  53. desc.type, desc.len);
  54. if (err < 0)
  55. goto err1;
  56. return 0;
  57. err1:
  58. nft_data_uninit(&priv->data, desc.type);
  59. return err;
  60. }
  61. static void nft_immediate_destroy(const struct nft_ctx *ctx,
  62. const struct nft_expr *expr)
  63. {
  64. const struct nft_immediate_expr *priv = nft_expr_priv(expr);
  65. return nft_data_uninit(&priv->data, nft_dreg_to_type(priv->dreg));
  66. }
  67. static int nft_immediate_dump(struct sk_buff *skb, const struct nft_expr *expr)
  68. {
  69. const struct nft_immediate_expr *priv = nft_expr_priv(expr);
  70. if (nft_dump_register(skb, NFTA_IMMEDIATE_DREG, priv->dreg))
  71. goto nla_put_failure;
  72. return nft_data_dump(skb, NFTA_IMMEDIATE_DATA, &priv->data,
  73. nft_dreg_to_type(priv->dreg), priv->dlen);
  74. nla_put_failure:
  75. return -1;
  76. }
  77. static int nft_immediate_validate(const struct nft_ctx *ctx,
  78. const struct nft_expr *expr,
  79. const struct nft_data **data)
  80. {
  81. const struct nft_immediate_expr *priv = nft_expr_priv(expr);
  82. if (priv->dreg == NFT_REG_VERDICT)
  83. *data = &priv->data;
  84. return 0;
  85. }
  86. static struct nft_expr_type nft_imm_type;
  87. static const struct nft_expr_ops nft_imm_ops = {
  88. .type = &nft_imm_type,
  89. .size = NFT_EXPR_SIZE(sizeof(struct nft_immediate_expr)),
  90. .eval = nft_immediate_eval,
  91. .init = nft_immediate_init,
  92. .destroy = nft_immediate_destroy,
  93. .dump = nft_immediate_dump,
  94. .validate = nft_immediate_validate,
  95. };
  96. static struct nft_expr_type nft_imm_type __read_mostly = {
  97. .name = "immediate",
  98. .ops = &nft_imm_ops,
  99. .policy = nft_immediate_policy,
  100. .maxattr = NFTA_IMMEDIATE_MAX,
  101. .owner = THIS_MODULE,
  102. };
  103. int __init nft_immediate_module_init(void)
  104. {
  105. return nft_register_expr(&nft_imm_type);
  106. }
  107. void nft_immediate_module_exit(void)
  108. {
  109. nft_unregister_expr(&nft_imm_type);
  110. }