nft_quota.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. * Copyright (c) 2016 Pablo Neira Ayuso <pablo@netfilter.org>
  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. #include <linux/kernel.h>
  9. #include <linux/init.h>
  10. #include <linux/module.h>
  11. #include <linux/atomic.h>
  12. #include <linux/netlink.h>
  13. #include <linux/netfilter.h>
  14. #include <linux/netfilter/nf_tables.h>
  15. #include <net/netfilter/nf_tables.h>
  16. struct nft_quota {
  17. u64 quota;
  18. unsigned long flags;
  19. atomic64_t consumed;
  20. };
  21. static inline bool nft_overquota(struct nft_quota *priv,
  22. const struct sk_buff *skb)
  23. {
  24. return atomic64_add_return(skb->len, &priv->consumed) >= priv->quota;
  25. }
  26. static inline bool nft_quota_invert(struct nft_quota *priv)
  27. {
  28. return priv->flags & NFT_QUOTA_F_INV;
  29. }
  30. static inline void nft_quota_do_eval(struct nft_quota *priv,
  31. struct nft_regs *regs,
  32. const struct nft_pktinfo *pkt)
  33. {
  34. if (nft_overquota(priv, pkt->skb) ^ nft_quota_invert(priv))
  35. regs->verdict.code = NFT_BREAK;
  36. }
  37. static const struct nla_policy nft_quota_policy[NFTA_QUOTA_MAX + 1] = {
  38. [NFTA_QUOTA_BYTES] = { .type = NLA_U64 },
  39. [NFTA_QUOTA_FLAGS] = { .type = NLA_U32 },
  40. [NFTA_QUOTA_CONSUMED] = { .type = NLA_U64 },
  41. };
  42. #define NFT_QUOTA_DEPLETED_BIT 1 /* From NFT_QUOTA_F_DEPLETED. */
  43. static void nft_quota_obj_eval(struct nft_object *obj,
  44. struct nft_regs *regs,
  45. const struct nft_pktinfo *pkt)
  46. {
  47. struct nft_quota *priv = nft_obj_data(obj);
  48. bool overquota;
  49. overquota = nft_overquota(priv, pkt->skb);
  50. if (overquota ^ nft_quota_invert(priv))
  51. regs->verdict.code = NFT_BREAK;
  52. if (overquota &&
  53. !test_and_set_bit(NFT_QUOTA_DEPLETED_BIT, &priv->flags))
  54. nft_obj_notify(nft_net(pkt), obj->table, obj, 0, 0,
  55. NFT_MSG_NEWOBJ, nft_pf(pkt), 0, GFP_ATOMIC);
  56. }
  57. static int nft_quota_do_init(const struct nlattr * const tb[],
  58. struct nft_quota *priv)
  59. {
  60. unsigned long flags = 0;
  61. u64 quota, consumed = 0;
  62. if (!tb[NFTA_QUOTA_BYTES])
  63. return -EINVAL;
  64. quota = be64_to_cpu(nla_get_be64(tb[NFTA_QUOTA_BYTES]));
  65. if (quota > S64_MAX)
  66. return -EOVERFLOW;
  67. if (tb[NFTA_QUOTA_CONSUMED]) {
  68. consumed = be64_to_cpu(nla_get_be64(tb[NFTA_QUOTA_CONSUMED]));
  69. if (consumed > quota)
  70. return -EINVAL;
  71. }
  72. if (tb[NFTA_QUOTA_FLAGS]) {
  73. flags = ntohl(nla_get_be32(tb[NFTA_QUOTA_FLAGS]));
  74. if (flags & ~NFT_QUOTA_F_INV)
  75. return -EINVAL;
  76. if (flags & NFT_QUOTA_F_DEPLETED)
  77. return -EOPNOTSUPP;
  78. }
  79. priv->quota = quota;
  80. priv->flags = flags;
  81. atomic64_set(&priv->consumed, consumed);
  82. return 0;
  83. }
  84. static int nft_quota_obj_init(const struct nft_ctx *ctx,
  85. const struct nlattr * const tb[],
  86. struct nft_object *obj)
  87. {
  88. struct nft_quota *priv = nft_obj_data(obj);
  89. return nft_quota_do_init(tb, priv);
  90. }
  91. static int nft_quota_do_dump(struct sk_buff *skb, struct nft_quota *priv,
  92. bool reset)
  93. {
  94. u64 consumed, consumed_cap;
  95. u32 flags = priv->flags;
  96. /* Since we inconditionally increment consumed quota for each packet
  97. * that we see, don't go over the quota boundary in what we send to
  98. * userspace.
  99. */
  100. consumed = atomic64_read(&priv->consumed);
  101. if (consumed >= priv->quota) {
  102. consumed_cap = priv->quota;
  103. flags |= NFT_QUOTA_F_DEPLETED;
  104. } else {
  105. consumed_cap = consumed;
  106. }
  107. if (nla_put_be64(skb, NFTA_QUOTA_BYTES, cpu_to_be64(priv->quota),
  108. NFTA_QUOTA_PAD) ||
  109. nla_put_be64(skb, NFTA_QUOTA_CONSUMED, cpu_to_be64(consumed_cap),
  110. NFTA_QUOTA_PAD) ||
  111. nla_put_be32(skb, NFTA_QUOTA_FLAGS, htonl(flags)))
  112. goto nla_put_failure;
  113. if (reset) {
  114. atomic64_sub(consumed, &priv->consumed);
  115. clear_bit(NFT_QUOTA_DEPLETED_BIT, &priv->flags);
  116. }
  117. return 0;
  118. nla_put_failure:
  119. return -1;
  120. }
  121. static int nft_quota_obj_dump(struct sk_buff *skb, struct nft_object *obj,
  122. bool reset)
  123. {
  124. struct nft_quota *priv = nft_obj_data(obj);
  125. return nft_quota_do_dump(skb, priv, reset);
  126. }
  127. static struct nft_object_type nft_quota_obj_type;
  128. static const struct nft_object_ops nft_quota_obj_ops = {
  129. .type = &nft_quota_obj_type,
  130. .size = sizeof(struct nft_quota),
  131. .init = nft_quota_obj_init,
  132. .eval = nft_quota_obj_eval,
  133. .dump = nft_quota_obj_dump,
  134. };
  135. static struct nft_object_type nft_quota_obj_type __read_mostly = {
  136. .type = NFT_OBJECT_QUOTA,
  137. .ops = &nft_quota_obj_ops,
  138. .maxattr = NFTA_QUOTA_MAX,
  139. .policy = nft_quota_policy,
  140. .owner = THIS_MODULE,
  141. };
  142. static void nft_quota_eval(const struct nft_expr *expr,
  143. struct nft_regs *regs,
  144. const struct nft_pktinfo *pkt)
  145. {
  146. struct nft_quota *priv = nft_expr_priv(expr);
  147. nft_quota_do_eval(priv, regs, pkt);
  148. }
  149. static int nft_quota_init(const struct nft_ctx *ctx,
  150. const struct nft_expr *expr,
  151. const struct nlattr * const tb[])
  152. {
  153. struct nft_quota *priv = nft_expr_priv(expr);
  154. return nft_quota_do_init(tb, priv);
  155. }
  156. static int nft_quota_dump(struct sk_buff *skb, const struct nft_expr *expr)
  157. {
  158. struct nft_quota *priv = nft_expr_priv(expr);
  159. return nft_quota_do_dump(skb, priv, false);
  160. }
  161. static struct nft_expr_type nft_quota_type;
  162. static const struct nft_expr_ops nft_quota_ops = {
  163. .type = &nft_quota_type,
  164. .size = NFT_EXPR_SIZE(sizeof(struct nft_quota)),
  165. .eval = nft_quota_eval,
  166. .init = nft_quota_init,
  167. .dump = nft_quota_dump,
  168. };
  169. static struct nft_expr_type nft_quota_type __read_mostly = {
  170. .name = "quota",
  171. .ops = &nft_quota_ops,
  172. .policy = nft_quota_policy,
  173. .maxattr = NFTA_QUOTA_MAX,
  174. .flags = NFT_EXPR_STATEFUL,
  175. .owner = THIS_MODULE,
  176. };
  177. static int __init nft_quota_module_init(void)
  178. {
  179. int err;
  180. err = nft_register_obj(&nft_quota_obj_type);
  181. if (err < 0)
  182. return err;
  183. err = nft_register_expr(&nft_quota_type);
  184. if (err < 0)
  185. goto err1;
  186. return 0;
  187. err1:
  188. nft_unregister_obj(&nft_quota_obj_type);
  189. return err;
  190. }
  191. static void __exit nft_quota_module_exit(void)
  192. {
  193. nft_unregister_expr(&nft_quota_type);
  194. nft_unregister_obj(&nft_quota_obj_type);
  195. }
  196. module_init(nft_quota_module_init);
  197. module_exit(nft_quota_module_exit);
  198. MODULE_LICENSE("GPL");
  199. MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>");
  200. MODULE_ALIAS_NFT_EXPR("quota");
  201. MODULE_ALIAS_NFT_OBJ(NFT_OBJECT_QUOTA);