nft_numgen.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2016 Laura Garcia <nevola@gmail.com>
  4. */
  5. #include <linux/kernel.h>
  6. #include <linux/init.h>
  7. #include <linux/module.h>
  8. #include <linux/netlink.h>
  9. #include <linux/netfilter.h>
  10. #include <linux/netfilter/nf_tables.h>
  11. #include <linux/static_key.h>
  12. #include <net/netfilter/nf_tables.h>
  13. #include <net/netfilter/nf_tables_core.h>
  14. static DEFINE_PER_CPU(struct rnd_state, nft_numgen_prandom_state);
  15. struct nft_ng_inc {
  16. enum nft_registers dreg:8;
  17. u32 modulus;
  18. atomic_t counter;
  19. u32 offset;
  20. };
  21. static u32 nft_ng_inc_gen(struct nft_ng_inc *priv)
  22. {
  23. u32 nval, oval;
  24. do {
  25. oval = atomic_read(&priv->counter);
  26. nval = (oval + 1 < priv->modulus) ? oval + 1 : 0;
  27. } while (atomic_cmpxchg(&priv->counter, oval, nval) != oval);
  28. return nval + priv->offset;
  29. }
  30. static void nft_ng_inc_eval(const struct nft_expr *expr,
  31. struct nft_regs *regs,
  32. const struct nft_pktinfo *pkt)
  33. {
  34. struct nft_ng_inc *priv = nft_expr_priv(expr);
  35. regs->data[priv->dreg] = nft_ng_inc_gen(priv);
  36. }
  37. static const struct nla_policy nft_ng_policy[NFTA_NG_MAX + 1] = {
  38. [NFTA_NG_DREG] = { .type = NLA_U32 },
  39. [NFTA_NG_MODULUS] = { .type = NLA_U32 },
  40. [NFTA_NG_TYPE] = { .type = NLA_U32 },
  41. [NFTA_NG_OFFSET] = { .type = NLA_U32 },
  42. };
  43. static int nft_ng_inc_init(const struct nft_ctx *ctx,
  44. const struct nft_expr *expr,
  45. const struct nlattr * const tb[])
  46. {
  47. struct nft_ng_inc *priv = nft_expr_priv(expr);
  48. if (tb[NFTA_NG_OFFSET])
  49. priv->offset = ntohl(nla_get_be32(tb[NFTA_NG_OFFSET]));
  50. priv->modulus = ntohl(nla_get_be32(tb[NFTA_NG_MODULUS]));
  51. if (priv->modulus == 0)
  52. return -ERANGE;
  53. if (priv->offset + priv->modulus - 1 < priv->offset)
  54. return -EOVERFLOW;
  55. priv->dreg = nft_parse_register(tb[NFTA_NG_DREG]);
  56. atomic_set(&priv->counter, priv->modulus - 1);
  57. return nft_validate_register_store(ctx, priv->dreg, NULL,
  58. NFT_DATA_VALUE, sizeof(u32));
  59. }
  60. static int nft_ng_dump(struct sk_buff *skb, enum nft_registers dreg,
  61. u32 modulus, enum nft_ng_types type, u32 offset)
  62. {
  63. if (nft_dump_register(skb, NFTA_NG_DREG, dreg))
  64. goto nla_put_failure;
  65. if (nla_put_be32(skb, NFTA_NG_MODULUS, htonl(modulus)))
  66. goto nla_put_failure;
  67. if (nla_put_be32(skb, NFTA_NG_TYPE, htonl(type)))
  68. goto nla_put_failure;
  69. if (nla_put_be32(skb, NFTA_NG_OFFSET, htonl(offset)))
  70. goto nla_put_failure;
  71. return 0;
  72. nla_put_failure:
  73. return -1;
  74. }
  75. static int nft_ng_inc_dump(struct sk_buff *skb, const struct nft_expr *expr)
  76. {
  77. const struct nft_ng_inc *priv = nft_expr_priv(expr);
  78. return nft_ng_dump(skb, priv->dreg, priv->modulus, NFT_NG_INCREMENTAL,
  79. priv->offset);
  80. }
  81. struct nft_ng_random {
  82. enum nft_registers dreg:8;
  83. u32 modulus;
  84. u32 offset;
  85. };
  86. static u32 nft_ng_random_gen(struct nft_ng_random *priv)
  87. {
  88. struct rnd_state *state = this_cpu_ptr(&nft_numgen_prandom_state);
  89. return reciprocal_scale(prandom_u32_state(state), priv->modulus) +
  90. priv->offset;
  91. }
  92. static void nft_ng_random_eval(const struct nft_expr *expr,
  93. struct nft_regs *regs,
  94. const struct nft_pktinfo *pkt)
  95. {
  96. struct nft_ng_random *priv = nft_expr_priv(expr);
  97. regs->data[priv->dreg] = nft_ng_random_gen(priv);
  98. }
  99. static int nft_ng_random_init(const struct nft_ctx *ctx,
  100. const struct nft_expr *expr,
  101. const struct nlattr * const tb[])
  102. {
  103. struct nft_ng_random *priv = nft_expr_priv(expr);
  104. if (tb[NFTA_NG_OFFSET])
  105. priv->offset = ntohl(nla_get_be32(tb[NFTA_NG_OFFSET]));
  106. priv->modulus = ntohl(nla_get_be32(tb[NFTA_NG_MODULUS]));
  107. if (priv->modulus == 0)
  108. return -ERANGE;
  109. if (priv->offset + priv->modulus - 1 < priv->offset)
  110. return -EOVERFLOW;
  111. prandom_init_once(&nft_numgen_prandom_state);
  112. priv->dreg = nft_parse_register(tb[NFTA_NG_DREG]);
  113. return nft_validate_register_store(ctx, priv->dreg, NULL,
  114. NFT_DATA_VALUE, sizeof(u32));
  115. }
  116. static int nft_ng_random_dump(struct sk_buff *skb, const struct nft_expr *expr)
  117. {
  118. const struct nft_ng_random *priv = nft_expr_priv(expr);
  119. return nft_ng_dump(skb, priv->dreg, priv->modulus, NFT_NG_RANDOM,
  120. priv->offset);
  121. }
  122. static struct nft_expr_type nft_ng_type;
  123. static const struct nft_expr_ops nft_ng_inc_ops = {
  124. .type = &nft_ng_type,
  125. .size = NFT_EXPR_SIZE(sizeof(struct nft_ng_inc)),
  126. .eval = nft_ng_inc_eval,
  127. .init = nft_ng_inc_init,
  128. .dump = nft_ng_inc_dump,
  129. };
  130. static const struct nft_expr_ops nft_ng_random_ops = {
  131. .type = &nft_ng_type,
  132. .size = NFT_EXPR_SIZE(sizeof(struct nft_ng_random)),
  133. .eval = nft_ng_random_eval,
  134. .init = nft_ng_random_init,
  135. .dump = nft_ng_random_dump,
  136. };
  137. static const struct nft_expr_ops *
  138. nft_ng_select_ops(const struct nft_ctx *ctx, const struct nlattr * const tb[])
  139. {
  140. u32 type;
  141. if (!tb[NFTA_NG_DREG] ||
  142. !tb[NFTA_NG_MODULUS] ||
  143. !tb[NFTA_NG_TYPE])
  144. return ERR_PTR(-EINVAL);
  145. type = ntohl(nla_get_be32(tb[NFTA_NG_TYPE]));
  146. switch (type) {
  147. case NFT_NG_INCREMENTAL:
  148. return &nft_ng_inc_ops;
  149. case NFT_NG_RANDOM:
  150. return &nft_ng_random_ops;
  151. }
  152. return ERR_PTR(-EINVAL);
  153. }
  154. static struct nft_expr_type nft_ng_type __read_mostly = {
  155. .name = "numgen",
  156. .select_ops = nft_ng_select_ops,
  157. .policy = nft_ng_policy,
  158. .maxattr = NFTA_NG_MAX,
  159. .owner = THIS_MODULE,
  160. };
  161. static int __init nft_ng_module_init(void)
  162. {
  163. return nft_register_expr(&nft_ng_type);
  164. }
  165. static void __exit nft_ng_module_exit(void)
  166. {
  167. nft_unregister_expr(&nft_ng_type);
  168. }
  169. module_init(nft_ng_module_init);
  170. module_exit(nft_ng_module_exit);
  171. MODULE_LICENSE("GPL");
  172. MODULE_AUTHOR("Laura Garcia <nevola@gmail.com>");
  173. MODULE_ALIAS_NFT_EXPR("numgen");
  174. MODULE_DESCRIPTION("nftables number generator module");