nft_cmp.c 5.2 KB

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