nft_range.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * Copyright (c) 2016 Pablo Neira Ayuso <pablo@netfilter.org>
  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. #include <linux/kernel.h>
  9. #include <linux/init.h>
  10. #include <linux/module.h>
  11. #include <linux/netlink.h>
  12. #include <linux/netfilter.h>
  13. #include <linux/netfilter/nf_tables.h>
  14. #include <net/netfilter/nf_tables_core.h>
  15. #include <net/netfilter/nf_tables.h>
  16. struct nft_range_expr {
  17. struct nft_data data_from;
  18. struct nft_data data_to;
  19. enum nft_registers sreg:8;
  20. u8 len;
  21. enum nft_range_ops op:8;
  22. };
  23. static void nft_range_eval(const struct nft_expr *expr,
  24. struct nft_regs *regs,
  25. const struct nft_pktinfo *pkt)
  26. {
  27. const struct nft_range_expr *priv = nft_expr_priv(expr);
  28. int d1, d2;
  29. d1 = memcmp(&regs->data[priv->sreg], &priv->data_from, priv->len);
  30. d2 = memcmp(&regs->data[priv->sreg], &priv->data_to, priv->len);
  31. switch (priv->op) {
  32. case NFT_RANGE_EQ:
  33. if (d1 < 0 || d2 > 0)
  34. regs->verdict.code = NFT_BREAK;
  35. break;
  36. case NFT_RANGE_NEQ:
  37. if (d1 >= 0 && d2 <= 0)
  38. regs->verdict.code = NFT_BREAK;
  39. break;
  40. }
  41. }
  42. static const struct nla_policy nft_range_policy[NFTA_RANGE_MAX + 1] = {
  43. [NFTA_RANGE_SREG] = { .type = NLA_U32 },
  44. [NFTA_RANGE_OP] = { .type = NLA_U32 },
  45. [NFTA_RANGE_FROM_DATA] = { .type = NLA_NESTED },
  46. [NFTA_RANGE_TO_DATA] = { .type = NLA_NESTED },
  47. };
  48. static int nft_range_init(const struct nft_ctx *ctx, const struct nft_expr *expr,
  49. const struct nlattr * const tb[])
  50. {
  51. struct nft_range_expr *priv = nft_expr_priv(expr);
  52. struct nft_data_desc desc_from, desc_to;
  53. int err;
  54. u32 op;
  55. if (!tb[NFTA_RANGE_SREG] ||
  56. !tb[NFTA_RANGE_OP] ||
  57. !tb[NFTA_RANGE_FROM_DATA] ||
  58. !tb[NFTA_RANGE_TO_DATA])
  59. return -EINVAL;
  60. err = nft_data_init(NULL, &priv->data_from, sizeof(priv->data_from),
  61. &desc_from, tb[NFTA_RANGE_FROM_DATA]);
  62. if (err < 0)
  63. return err;
  64. if (desc_from.type != NFT_DATA_VALUE) {
  65. err = -EINVAL;
  66. goto err1;
  67. }
  68. err = nft_data_init(NULL, &priv->data_to, sizeof(priv->data_to),
  69. &desc_to, tb[NFTA_RANGE_TO_DATA]);
  70. if (err < 0)
  71. goto err1;
  72. if (desc_to.type != NFT_DATA_VALUE) {
  73. err = -EINVAL;
  74. goto err2;
  75. }
  76. if (desc_from.len != desc_to.len) {
  77. err = -EINVAL;
  78. goto err2;
  79. }
  80. priv->sreg = nft_parse_register(tb[NFTA_RANGE_SREG]);
  81. err = nft_validate_register_load(priv->sreg, desc_from.len);
  82. if (err < 0)
  83. goto err2;
  84. err = nft_parse_u32_check(tb[NFTA_RANGE_OP], U8_MAX, &op);
  85. if (err < 0)
  86. goto err2;
  87. switch (op) {
  88. case NFT_RANGE_EQ:
  89. case NFT_RANGE_NEQ:
  90. break;
  91. default:
  92. err = -EINVAL;
  93. goto err2;
  94. }
  95. priv->op = op;
  96. priv->len = desc_from.len;
  97. return 0;
  98. err2:
  99. nft_data_release(&priv->data_to, desc_to.type);
  100. err1:
  101. nft_data_release(&priv->data_from, desc_from.type);
  102. return err;
  103. }
  104. static int nft_range_dump(struct sk_buff *skb, const struct nft_expr *expr)
  105. {
  106. const struct nft_range_expr *priv = nft_expr_priv(expr);
  107. if (nft_dump_register(skb, NFTA_RANGE_SREG, priv->sreg))
  108. goto nla_put_failure;
  109. if (nla_put_be32(skb, NFTA_RANGE_OP, htonl(priv->op)))
  110. goto nla_put_failure;
  111. if (nft_data_dump(skb, NFTA_RANGE_FROM_DATA, &priv->data_from,
  112. NFT_DATA_VALUE, priv->len) < 0 ||
  113. nft_data_dump(skb, NFTA_RANGE_TO_DATA, &priv->data_to,
  114. NFT_DATA_VALUE, priv->len) < 0)
  115. goto nla_put_failure;
  116. return 0;
  117. nla_put_failure:
  118. return -1;
  119. }
  120. static const struct nft_expr_ops nft_range_ops = {
  121. .type = &nft_range_type,
  122. .size = NFT_EXPR_SIZE(sizeof(struct nft_range_expr)),
  123. .eval = nft_range_eval,
  124. .init = nft_range_init,
  125. .dump = nft_range_dump,
  126. };
  127. struct nft_expr_type nft_range_type __read_mostly = {
  128. .name = "range",
  129. .ops = &nft_range_ops,
  130. .policy = nft_range_policy,
  131. .maxattr = NFTA_RANGE_MAX,
  132. .owner = THIS_MODULE,
  133. };