nft_cmp.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. /* fall through */
  44. case NFT_CMP_LTE:
  45. if (d > 0)
  46. goto mismatch;
  47. break;
  48. case NFT_CMP_GT:
  49. if (d == 0)
  50. goto mismatch;
  51. /* fall through */
  52. case NFT_CMP_GTE:
  53. if (d < 0)
  54. goto mismatch;
  55. break;
  56. }
  57. return;
  58. mismatch:
  59. regs->verdict.code = NFT_BREAK;
  60. }
  61. static const struct nla_policy nft_cmp_policy[NFTA_CMP_MAX + 1] = {
  62. [NFTA_CMP_SREG] = { .type = NLA_U32 },
  63. [NFTA_CMP_OP] = { .type = NLA_U32 },
  64. [NFTA_CMP_DATA] = { .type = NLA_NESTED },
  65. };
  66. static int nft_cmp_init(const struct nft_ctx *ctx, const struct nft_expr *expr,
  67. const struct nlattr * const tb[])
  68. {
  69. struct nft_cmp_expr *priv = nft_expr_priv(expr);
  70. struct nft_data_desc desc;
  71. int err;
  72. err = nft_data_init(NULL, &priv->data, sizeof(priv->data), &desc,
  73. tb[NFTA_CMP_DATA]);
  74. if (err < 0)
  75. return err;
  76. if (desc.type != NFT_DATA_VALUE) {
  77. err = -EINVAL;
  78. nft_data_release(&priv->data, desc.type);
  79. return err;
  80. }
  81. priv->sreg = nft_parse_register(tb[NFTA_CMP_SREG]);
  82. err = nft_validate_register_load(priv->sreg, desc.len);
  83. if (err < 0)
  84. return err;
  85. priv->op = ntohl(nla_get_be32(tb[NFTA_CMP_OP]));
  86. priv->len = desc.len;
  87. return 0;
  88. }
  89. static int nft_cmp_dump(struct sk_buff *skb, const struct nft_expr *expr)
  90. {
  91. const struct nft_cmp_expr *priv = nft_expr_priv(expr);
  92. if (nft_dump_register(skb, NFTA_CMP_SREG, priv->sreg))
  93. goto nla_put_failure;
  94. if (nla_put_be32(skb, NFTA_CMP_OP, htonl(priv->op)))
  95. goto nla_put_failure;
  96. if (nft_data_dump(skb, NFTA_CMP_DATA, &priv->data,
  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 const struct nft_expr_ops nft_cmp_ops = {
  104. .type = &nft_cmp_type,
  105. .size = NFT_EXPR_SIZE(sizeof(struct nft_cmp_expr)),
  106. .eval = nft_cmp_eval,
  107. .init = nft_cmp_init,
  108. .dump = nft_cmp_dump,
  109. };
  110. static int nft_cmp_fast_init(const struct nft_ctx *ctx,
  111. const struct nft_expr *expr,
  112. const struct nlattr * const tb[])
  113. {
  114. struct nft_cmp_fast_expr *priv = nft_expr_priv(expr);
  115. struct nft_data_desc desc;
  116. struct nft_data data;
  117. u32 mask;
  118. int err;
  119. err = nft_data_init(NULL, &data, sizeof(data), &desc,
  120. tb[NFTA_CMP_DATA]);
  121. if (err < 0)
  122. return err;
  123. priv->sreg = nft_parse_register(tb[NFTA_CMP_SREG]);
  124. err = nft_validate_register_load(priv->sreg, desc.len);
  125. if (err < 0)
  126. return err;
  127. desc.len *= BITS_PER_BYTE;
  128. mask = nft_cmp_fast_mask(desc.len);
  129. priv->data = data.data[0] & mask;
  130. priv->len = desc.len;
  131. return 0;
  132. }
  133. static int nft_cmp_fast_dump(struct sk_buff *skb, const struct nft_expr *expr)
  134. {
  135. const struct nft_cmp_fast_expr *priv = nft_expr_priv(expr);
  136. struct nft_data data;
  137. if (nft_dump_register(skb, NFTA_CMP_SREG, priv->sreg))
  138. goto nla_put_failure;
  139. if (nla_put_be32(skb, NFTA_CMP_OP, htonl(NFT_CMP_EQ)))
  140. goto nla_put_failure;
  141. data.data[0] = priv->data;
  142. if (nft_data_dump(skb, NFTA_CMP_DATA, &data,
  143. NFT_DATA_VALUE, priv->len / BITS_PER_BYTE) < 0)
  144. goto nla_put_failure;
  145. return 0;
  146. nla_put_failure:
  147. return -1;
  148. }
  149. const struct nft_expr_ops nft_cmp_fast_ops = {
  150. .type = &nft_cmp_type,
  151. .size = NFT_EXPR_SIZE(sizeof(struct nft_cmp_fast_expr)),
  152. .eval = NULL, /* inlined */
  153. .init = nft_cmp_fast_init,
  154. .dump = nft_cmp_fast_dump,
  155. };
  156. static const struct nft_expr_ops *
  157. nft_cmp_select_ops(const struct nft_ctx *ctx, const struct nlattr * const tb[])
  158. {
  159. struct nft_data_desc desc;
  160. struct nft_data data;
  161. enum nft_cmp_ops op;
  162. int err;
  163. if (tb[NFTA_CMP_SREG] == NULL ||
  164. tb[NFTA_CMP_OP] == NULL ||
  165. tb[NFTA_CMP_DATA] == NULL)
  166. return ERR_PTR(-EINVAL);
  167. op = ntohl(nla_get_be32(tb[NFTA_CMP_OP]));
  168. switch (op) {
  169. case NFT_CMP_EQ:
  170. case NFT_CMP_NEQ:
  171. case NFT_CMP_LT:
  172. case NFT_CMP_LTE:
  173. case NFT_CMP_GT:
  174. case NFT_CMP_GTE:
  175. break;
  176. default:
  177. return ERR_PTR(-EINVAL);
  178. }
  179. err = nft_data_init(NULL, &data, sizeof(data), &desc,
  180. tb[NFTA_CMP_DATA]);
  181. if (err < 0)
  182. return ERR_PTR(err);
  183. if (desc.type != NFT_DATA_VALUE) {
  184. err = -EINVAL;
  185. goto err1;
  186. }
  187. if (desc.len <= sizeof(u32) && op == NFT_CMP_EQ)
  188. return &nft_cmp_fast_ops;
  189. return &nft_cmp_ops;
  190. err1:
  191. nft_data_release(&data, desc.type);
  192. return ERR_PTR(-EINVAL);
  193. }
  194. struct nft_expr_type nft_cmp_type __read_mostly = {
  195. .name = "cmp",
  196. .select_ops = nft_cmp_select_ops,
  197. .policy = nft_cmp_policy,
  198. .maxattr = NFTA_CMP_MAX,
  199. .owner = THIS_MODULE,
  200. };