nft_cmp.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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_regs *regs,
  26. const struct nft_pktinfo *pkt)
  27. {
  28. const struct nft_cmp_expr *priv = nft_expr_priv(expr);
  29. int d;
  30. d = memcmp(&regs->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. regs->verdict.code = 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. err = nft_data_init(NULL, &priv->data, sizeof(priv->data), &desc,
  71. tb[NFTA_CMP_DATA]);
  72. BUG_ON(err < 0);
  73. priv->sreg = nft_parse_register(tb[NFTA_CMP_SREG]);
  74. err = nft_validate_register_load(priv->sreg, desc.len);
  75. if (err < 0)
  76. return err;
  77. priv->op = ntohl(nla_get_be32(tb[NFTA_CMP_OP]));
  78. priv->len = desc.len;
  79. return 0;
  80. }
  81. static int nft_cmp_dump(struct sk_buff *skb, const struct nft_expr *expr)
  82. {
  83. const struct nft_cmp_expr *priv = nft_expr_priv(expr);
  84. if (nft_dump_register(skb, NFTA_CMP_SREG, priv->sreg))
  85. goto nla_put_failure;
  86. if (nla_put_be32(skb, NFTA_CMP_OP, htonl(priv->op)))
  87. goto nla_put_failure;
  88. if (nft_data_dump(skb, NFTA_CMP_DATA, &priv->data,
  89. NFT_DATA_VALUE, priv->len) < 0)
  90. goto nla_put_failure;
  91. return 0;
  92. nla_put_failure:
  93. return -1;
  94. }
  95. static struct nft_expr_type nft_cmp_type;
  96. static const struct nft_expr_ops nft_cmp_ops = {
  97. .type = &nft_cmp_type,
  98. .size = NFT_EXPR_SIZE(sizeof(struct nft_cmp_expr)),
  99. .eval = nft_cmp_eval,
  100. .init = nft_cmp_init,
  101. .dump = nft_cmp_dump,
  102. };
  103. static int nft_cmp_fast_init(const struct nft_ctx *ctx,
  104. const struct nft_expr *expr,
  105. const struct nlattr * const tb[])
  106. {
  107. struct nft_cmp_fast_expr *priv = nft_expr_priv(expr);
  108. struct nft_data_desc desc;
  109. struct nft_data data;
  110. u32 mask;
  111. int err;
  112. err = nft_data_init(NULL, &data, sizeof(data), &desc,
  113. tb[NFTA_CMP_DATA]);
  114. BUG_ON(err < 0);
  115. priv->sreg = nft_parse_register(tb[NFTA_CMP_SREG]);
  116. err = nft_validate_register_load(priv->sreg, desc.len);
  117. if (err < 0)
  118. return err;
  119. desc.len *= BITS_PER_BYTE;
  120. mask = nft_cmp_fast_mask(desc.len);
  121. priv->data = data.data[0] & mask;
  122. priv->len = desc.len;
  123. return 0;
  124. }
  125. static int nft_cmp_fast_dump(struct sk_buff *skb, const struct nft_expr *expr)
  126. {
  127. const struct nft_cmp_fast_expr *priv = nft_expr_priv(expr);
  128. struct nft_data data;
  129. if (nft_dump_register(skb, NFTA_CMP_SREG, priv->sreg))
  130. goto nla_put_failure;
  131. if (nla_put_be32(skb, NFTA_CMP_OP, htonl(NFT_CMP_EQ)))
  132. goto nla_put_failure;
  133. data.data[0] = priv->data;
  134. if (nft_data_dump(skb, NFTA_CMP_DATA, &data,
  135. NFT_DATA_VALUE, priv->len / BITS_PER_BYTE) < 0)
  136. goto nla_put_failure;
  137. return 0;
  138. nla_put_failure:
  139. return -1;
  140. }
  141. const struct nft_expr_ops nft_cmp_fast_ops = {
  142. .type = &nft_cmp_type,
  143. .size = NFT_EXPR_SIZE(sizeof(struct nft_cmp_fast_expr)),
  144. .eval = NULL, /* inlined */
  145. .init = nft_cmp_fast_init,
  146. .dump = nft_cmp_fast_dump,
  147. };
  148. static const struct nft_expr_ops *
  149. nft_cmp_select_ops(const struct nft_ctx *ctx, const struct nlattr * const tb[])
  150. {
  151. struct nft_data_desc desc;
  152. struct nft_data data;
  153. enum nft_cmp_ops op;
  154. int err;
  155. if (tb[NFTA_CMP_SREG] == NULL ||
  156. tb[NFTA_CMP_OP] == NULL ||
  157. tb[NFTA_CMP_DATA] == NULL)
  158. return ERR_PTR(-EINVAL);
  159. op = ntohl(nla_get_be32(tb[NFTA_CMP_OP]));
  160. switch (op) {
  161. case NFT_CMP_EQ:
  162. case NFT_CMP_NEQ:
  163. case NFT_CMP_LT:
  164. case NFT_CMP_LTE:
  165. case NFT_CMP_GT:
  166. case NFT_CMP_GTE:
  167. break;
  168. default:
  169. return ERR_PTR(-EINVAL);
  170. }
  171. err = nft_data_init(NULL, &data, sizeof(data), &desc,
  172. tb[NFTA_CMP_DATA]);
  173. if (err < 0)
  174. return ERR_PTR(err);
  175. if (desc.len <= sizeof(u32) && op == NFT_CMP_EQ)
  176. return &nft_cmp_fast_ops;
  177. else
  178. return &nft_cmp_ops;
  179. }
  180. static struct nft_expr_type nft_cmp_type __read_mostly = {
  181. .name = "cmp",
  182. .select_ops = nft_cmp_select_ops,
  183. .policy = nft_cmp_policy,
  184. .maxattr = NFTA_CMP_MAX,
  185. .owner = THIS_MODULE,
  186. };
  187. int __init nft_cmp_module_init(void)
  188. {
  189. return nft_register_expr(&nft_cmp_type);
  190. }
  191. void nft_cmp_module_exit(void)
  192. {
  193. nft_unregister_expr(&nft_cmp_type);
  194. }