nft_bitwise.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. u32 len;
  50. int err;
  51. if (tb[NFTA_BITWISE_SREG] == NULL ||
  52. tb[NFTA_BITWISE_DREG] == NULL ||
  53. tb[NFTA_BITWISE_LEN] == NULL ||
  54. tb[NFTA_BITWISE_MASK] == NULL ||
  55. tb[NFTA_BITWISE_XOR] == NULL)
  56. return -EINVAL;
  57. err = nft_parse_u32_check(tb[NFTA_BITWISE_LEN], U8_MAX, &len);
  58. if (err < 0)
  59. return err;
  60. priv->len = len;
  61. priv->sreg = nft_parse_register(tb[NFTA_BITWISE_SREG]);
  62. err = nft_validate_register_load(priv->sreg, priv->len);
  63. if (err < 0)
  64. return err;
  65. priv->dreg = nft_parse_register(tb[NFTA_BITWISE_DREG]);
  66. err = nft_validate_register_store(ctx, priv->dreg, NULL,
  67. NFT_DATA_VALUE, priv->len);
  68. if (err < 0)
  69. return err;
  70. err = nft_data_init(NULL, &priv->mask, sizeof(priv->mask), &d1,
  71. tb[NFTA_BITWISE_MASK]);
  72. if (err < 0)
  73. return err;
  74. if (d1.len != priv->len)
  75. return -EINVAL;
  76. err = nft_data_init(NULL, &priv->xor, sizeof(priv->xor), &d2,
  77. tb[NFTA_BITWISE_XOR]);
  78. if (err < 0)
  79. return err;
  80. if (d2.len != priv->len)
  81. return -EINVAL;
  82. return 0;
  83. }
  84. static int nft_bitwise_dump(struct sk_buff *skb, const struct nft_expr *expr)
  85. {
  86. const struct nft_bitwise *priv = nft_expr_priv(expr);
  87. if (nft_dump_register(skb, NFTA_BITWISE_SREG, priv->sreg))
  88. goto nla_put_failure;
  89. if (nft_dump_register(skb, NFTA_BITWISE_DREG, priv->dreg))
  90. goto nla_put_failure;
  91. if (nla_put_be32(skb, NFTA_BITWISE_LEN, htonl(priv->len)))
  92. goto nla_put_failure;
  93. if (nft_data_dump(skb, NFTA_BITWISE_MASK, &priv->mask,
  94. NFT_DATA_VALUE, priv->len) < 0)
  95. goto nla_put_failure;
  96. if (nft_data_dump(skb, NFTA_BITWISE_XOR, &priv->xor,
  97. NFT_DATA_VALUE, priv->len) < 0)
  98. goto nla_put_failure;
  99. return 0;
  100. nla_put_failure:
  101. return -1;
  102. }
  103. static struct nft_expr_type nft_bitwise_type;
  104. static const struct nft_expr_ops nft_bitwise_ops = {
  105. .type = &nft_bitwise_type,
  106. .size = NFT_EXPR_SIZE(sizeof(struct nft_bitwise)),
  107. .eval = nft_bitwise_eval,
  108. .init = nft_bitwise_init,
  109. .dump = nft_bitwise_dump,
  110. };
  111. static struct nft_expr_type nft_bitwise_type __read_mostly = {
  112. .name = "bitwise",
  113. .ops = &nft_bitwise_ops,
  114. .policy = nft_bitwise_policy,
  115. .maxattr = NFTA_BITWISE_MAX,
  116. .owner = THIS_MODULE,
  117. };
  118. int __init nft_bitwise_module_init(void)
  119. {
  120. return nft_register_expr(&nft_bitwise_type);
  121. }
  122. void nft_bitwise_module_exit(void)
  123. {
  124. nft_unregister_expr(&nft_bitwise_type);
  125. }