nft_bitwise.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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_bitwise {
  19. enum nft_registers sreg:8;
  20. enum nft_registers dreg:8;
  21. u8 len;
  22. struct nft_data mask;
  23. struct nft_data xor;
  24. };
  25. static void nft_bitwise_eval(const struct nft_expr *expr,
  26. struct nft_regs *regs,
  27. const struct nft_pktinfo *pkt)
  28. {
  29. const struct nft_bitwise *priv = nft_expr_priv(expr);
  30. const u32 *src = &regs->data[priv->sreg];
  31. u32 *dst = &regs->data[priv->dreg];
  32. unsigned int i;
  33. for (i = 0; i < DIV_ROUND_UP(priv->len, 4); i++)
  34. dst[i] = (src[i] & priv->mask.data[i]) ^ priv->xor.data[i];
  35. }
  36. static const struct nla_policy nft_bitwise_policy[NFTA_BITWISE_MAX + 1] = {
  37. [NFTA_BITWISE_SREG] = { .type = NLA_U32 },
  38. [NFTA_BITWISE_DREG] = { .type = NLA_U32 },
  39. [NFTA_BITWISE_LEN] = { .type = NLA_U32 },
  40. [NFTA_BITWISE_MASK] = { .type = NLA_NESTED },
  41. [NFTA_BITWISE_XOR] = { .type = NLA_NESTED },
  42. };
  43. static int nft_bitwise_init(const struct nft_ctx *ctx,
  44. const struct nft_expr *expr,
  45. const struct nlattr * const tb[])
  46. {
  47. struct nft_bitwise *priv = nft_expr_priv(expr);
  48. struct nft_data_desc d1, d2;
  49. int err;
  50. if (tb[NFTA_BITWISE_SREG] == NULL ||
  51. tb[NFTA_BITWISE_DREG] == NULL ||
  52. tb[NFTA_BITWISE_LEN] == NULL ||
  53. tb[NFTA_BITWISE_MASK] == NULL ||
  54. tb[NFTA_BITWISE_XOR] == NULL)
  55. return -EINVAL;
  56. priv->len = ntohl(nla_get_be32(tb[NFTA_BITWISE_LEN]));
  57. priv->sreg = nft_parse_register(tb[NFTA_BITWISE_SREG]);
  58. err = nft_validate_register_load(priv->sreg, priv->len);
  59. if (err < 0)
  60. return err;
  61. priv->dreg = nft_parse_register(tb[NFTA_BITWISE_DREG]);
  62. err = nft_validate_register_store(ctx, priv->dreg, NULL,
  63. NFT_DATA_VALUE, priv->len);
  64. if (err < 0)
  65. return err;
  66. err = nft_data_init(NULL, &priv->mask, sizeof(priv->mask), &d1,
  67. tb[NFTA_BITWISE_MASK]);
  68. if (err < 0)
  69. return err;
  70. if (d1.len != priv->len)
  71. return -EINVAL;
  72. err = nft_data_init(NULL, &priv->xor, sizeof(priv->xor), &d2,
  73. tb[NFTA_BITWISE_XOR]);
  74. if (err < 0)
  75. return err;
  76. if (d2.len != priv->len)
  77. return -EINVAL;
  78. return 0;
  79. }
  80. static int nft_bitwise_dump(struct sk_buff *skb, const struct nft_expr *expr)
  81. {
  82. const struct nft_bitwise *priv = nft_expr_priv(expr);
  83. if (nft_dump_register(skb, NFTA_BITWISE_SREG, priv->sreg))
  84. goto nla_put_failure;
  85. if (nft_dump_register(skb, NFTA_BITWISE_DREG, priv->dreg))
  86. goto nla_put_failure;
  87. if (nla_put_be32(skb, NFTA_BITWISE_LEN, htonl(priv->len)))
  88. goto nla_put_failure;
  89. if (nft_data_dump(skb, NFTA_BITWISE_MASK, &priv->mask,
  90. NFT_DATA_VALUE, priv->len) < 0)
  91. goto nla_put_failure;
  92. if (nft_data_dump(skb, NFTA_BITWISE_XOR, &priv->xor,
  93. NFT_DATA_VALUE, priv->len) < 0)
  94. goto nla_put_failure;
  95. return 0;
  96. nla_put_failure:
  97. return -1;
  98. }
  99. static struct nft_expr_type nft_bitwise_type;
  100. static const struct nft_expr_ops nft_bitwise_ops = {
  101. .type = &nft_bitwise_type,
  102. .size = NFT_EXPR_SIZE(sizeof(struct nft_bitwise)),
  103. .eval = nft_bitwise_eval,
  104. .init = nft_bitwise_init,
  105. .dump = nft_bitwise_dump,
  106. };
  107. static struct nft_expr_type nft_bitwise_type __read_mostly = {
  108. .name = "bitwise",
  109. .ops = &nft_bitwise_ops,
  110. .policy = nft_bitwise_policy,
  111. .maxattr = NFTA_BITWISE_MAX,
  112. .owner = THIS_MODULE,
  113. };
  114. int __init nft_bitwise_module_init(void)
  115. {
  116. return nft_register_expr(&nft_bitwise_type);
  117. }
  118. void nft_bitwise_module_exit(void)
  119. {
  120. nft_unregister_expr(&nft_bitwise_type);
  121. }