nft_cmp.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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_cmp_expr {
  19. struct nft_data data;
  20. enum nft_registers sreg:8;
  21. u8 len;
  22. enum nft_cmp_ops op:8;
  23. };
  24. static void nft_cmp_eval(const struct nft_expr *expr,
  25. struct nft_data data[NFT_REG_MAX + 1],
  26. const struct nft_pktinfo *pkt)
  27. {
  28. const struct nft_cmp_expr *priv = nft_expr_priv(expr);
  29. int d;
  30. d = nft_data_cmp(&data[priv->sreg], &priv->data, priv->len);
  31. switch (priv->op) {
  32. case NFT_CMP_EQ:
  33. if (d != 0)
  34. goto mismatch;
  35. break;
  36. case NFT_CMP_NEQ:
  37. if (d == 0)
  38. goto mismatch;
  39. break;
  40. case NFT_CMP_LT:
  41. if (d == 0)
  42. goto mismatch;
  43. case NFT_CMP_LTE:
  44. if (d > 0)
  45. goto mismatch;
  46. break;
  47. case NFT_CMP_GT:
  48. if (d == 0)
  49. goto mismatch;
  50. case NFT_CMP_GTE:
  51. if (d < 0)
  52. goto mismatch;
  53. break;
  54. }
  55. return;
  56. mismatch:
  57. data[NFT_REG_VERDICT].verdict = NFT_BREAK;
  58. }
  59. static const struct nla_policy nft_cmp_policy[NFTA_CMP_MAX + 1] = {
  60. [NFTA_CMP_SREG] = { .type = NLA_U32 },
  61. [NFTA_CMP_OP] = { .type = NLA_U32 },
  62. [NFTA_CMP_DATA] = { .type = NLA_NESTED },
  63. };
  64. static int nft_cmp_init(const struct nft_ctx *ctx, const struct nft_expr *expr,
  65. const struct nlattr * const tb[])
  66. {
  67. struct nft_cmp_expr *priv = nft_expr_priv(expr);
  68. struct nft_data_desc desc;
  69. int err;
  70. priv->sreg = ntohl(nla_get_be32(tb[NFTA_CMP_SREG]));
  71. priv->op = ntohl(nla_get_be32(tb[NFTA_CMP_OP]));
  72. err = nft_data_init(NULL, &priv->data, &desc, tb[NFTA_CMP_DATA]);
  73. BUG_ON(err < 0);
  74. priv->len = desc.len;
  75. return 0;
  76. }
  77. static int nft_cmp_dump(struct sk_buff *skb, const struct nft_expr *expr)
  78. {
  79. const struct nft_cmp_expr *priv = nft_expr_priv(expr);
  80. if (nla_put_be32(skb, NFTA_CMP_SREG, htonl(priv->sreg)))
  81. goto nla_put_failure;
  82. if (nla_put_be32(skb, NFTA_CMP_OP, htonl(priv->op)))
  83. goto nla_put_failure;
  84. if (nft_data_dump(skb, NFTA_CMP_DATA, &priv->data,
  85. NFT_DATA_VALUE, priv->len) < 0)
  86. goto nla_put_failure;
  87. return 0;
  88. nla_put_failure:
  89. return -1;
  90. }
  91. static struct nft_expr_type nft_cmp_type;
  92. static const struct nft_expr_ops nft_cmp_ops = {
  93. .type = &nft_cmp_type,
  94. .size = NFT_EXPR_SIZE(sizeof(struct nft_cmp_expr)),
  95. .eval = nft_cmp_eval,
  96. .init = nft_cmp_init,
  97. .dump = nft_cmp_dump,
  98. };
  99. static int nft_cmp_fast_init(const struct nft_ctx *ctx,
  100. const struct nft_expr *expr,
  101. const struct nlattr * const tb[])
  102. {
  103. struct nft_cmp_fast_expr *priv = nft_expr_priv(expr);
  104. struct nft_data_desc desc;
  105. struct nft_data data;
  106. u32 mask;
  107. int err;
  108. priv->sreg = ntohl(nla_get_be32(tb[NFTA_CMP_SREG]));
  109. err = nft_data_init(NULL, &data, &desc, tb[NFTA_CMP_DATA]);
  110. BUG_ON(err < 0);
  111. desc.len *= BITS_PER_BYTE;
  112. mask = nft_cmp_fast_mask(desc.len);
  113. priv->data = data.data[0] & mask;
  114. priv->len = desc.len;
  115. return 0;
  116. }
  117. static int nft_cmp_fast_dump(struct sk_buff *skb, const struct nft_expr *expr)
  118. {
  119. const struct nft_cmp_fast_expr *priv = nft_expr_priv(expr);
  120. struct nft_data data;
  121. if (nla_put_be32(skb, NFTA_CMP_SREG, htonl(priv->sreg)))
  122. goto nla_put_failure;
  123. if (nla_put_be32(skb, NFTA_CMP_OP, htonl(NFT_CMP_EQ)))
  124. goto nla_put_failure;
  125. data.data[0] = priv->data;
  126. if (nft_data_dump(skb, NFTA_CMP_DATA, &data,
  127. NFT_DATA_VALUE, priv->len / BITS_PER_BYTE) < 0)
  128. goto nla_put_failure;
  129. return 0;
  130. nla_put_failure:
  131. return -1;
  132. }
  133. const struct nft_expr_ops nft_cmp_fast_ops = {
  134. .type = &nft_cmp_type,
  135. .size = NFT_EXPR_SIZE(sizeof(struct nft_cmp_fast_expr)),
  136. .eval = NULL, /* inlined */
  137. .init = nft_cmp_fast_init,
  138. .dump = nft_cmp_fast_dump,
  139. };
  140. static const struct nft_expr_ops *
  141. nft_cmp_select_ops(const struct nft_ctx *ctx, const struct nlattr * const tb[])
  142. {
  143. struct nft_data_desc desc;
  144. struct nft_data data;
  145. enum nft_registers sreg;
  146. enum nft_cmp_ops op;
  147. int err;
  148. if (tb[NFTA_CMP_SREG] == NULL ||
  149. tb[NFTA_CMP_OP] == NULL ||
  150. tb[NFTA_CMP_DATA] == NULL)
  151. return ERR_PTR(-EINVAL);
  152. sreg = ntohl(nla_get_be32(tb[NFTA_CMP_SREG]));
  153. err = nft_validate_input_register(sreg);
  154. if (err < 0)
  155. return ERR_PTR(err);
  156. op = ntohl(nla_get_be32(tb[NFTA_CMP_OP]));
  157. switch (op) {
  158. case NFT_CMP_EQ:
  159. case NFT_CMP_NEQ:
  160. case NFT_CMP_LT:
  161. case NFT_CMP_LTE:
  162. case NFT_CMP_GT:
  163. case NFT_CMP_GTE:
  164. break;
  165. default:
  166. return ERR_PTR(-EINVAL);
  167. }
  168. err = nft_data_init(NULL, &data, &desc, tb[NFTA_CMP_DATA]);
  169. if (err < 0)
  170. return ERR_PTR(err);
  171. if (desc.len <= sizeof(u32) && op == NFT_CMP_EQ)
  172. return &nft_cmp_fast_ops;
  173. else
  174. return &nft_cmp_ops;
  175. }
  176. static struct nft_expr_type nft_cmp_type __read_mostly = {
  177. .name = "cmp",
  178. .select_ops = nft_cmp_select_ops,
  179. .policy = nft_cmp_policy,
  180. .maxattr = NFTA_CMP_MAX,
  181. .owner = THIS_MODULE,
  182. };
  183. int __init nft_cmp_module_init(void)
  184. {
  185. return nft_register_expr(&nft_cmp_type);
  186. }
  187. void nft_cmp_module_exit(void)
  188. {
  189. nft_unregister_expr(&nft_cmp_type);
  190. }