nft_immediate.c 3.4 KB

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