nft_quota.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. bool invert;
  19. atomic64_t remain;
  20. };
  21. static inline bool nft_overquota(struct nft_quota *priv,
  22. const struct nft_pktinfo *pkt)
  23. {
  24. return atomic64_sub_return(pkt->skb->len, &priv->remain) < 0;
  25. }
  26. static void nft_quota_eval(const struct nft_expr *expr,
  27. struct nft_regs *regs,
  28. const struct nft_pktinfo *pkt)
  29. {
  30. struct nft_quota *priv = nft_expr_priv(expr);
  31. if (nft_overquota(priv, pkt) ^ priv->invert)
  32. regs->verdict.code = NFT_BREAK;
  33. }
  34. static const struct nla_policy nft_quota_policy[NFTA_QUOTA_MAX + 1] = {
  35. [NFTA_QUOTA_BYTES] = { .type = NLA_U64 },
  36. [NFTA_QUOTA_FLAGS] = { .type = NLA_U32 },
  37. };
  38. static int nft_quota_init(const struct nft_ctx *ctx,
  39. const struct nft_expr *expr,
  40. const struct nlattr * const tb[])
  41. {
  42. struct nft_quota *priv = nft_expr_priv(expr);
  43. u32 flags = 0;
  44. u64 quota;
  45. if (!tb[NFTA_QUOTA_BYTES])
  46. return -EINVAL;
  47. quota = be64_to_cpu(nla_get_be64(tb[NFTA_QUOTA_BYTES]));
  48. if (quota > S64_MAX)
  49. return -EOVERFLOW;
  50. if (tb[NFTA_QUOTA_FLAGS]) {
  51. flags = ntohl(nla_get_be32(tb[NFTA_QUOTA_FLAGS]));
  52. if (flags & ~NFT_QUOTA_F_INV)
  53. return -EINVAL;
  54. }
  55. priv->quota = quota;
  56. priv->invert = (flags & NFT_QUOTA_F_INV) ? true : false;
  57. atomic64_set(&priv->remain, quota);
  58. return 0;
  59. }
  60. static int nft_quota_dump(struct sk_buff *skb, const struct nft_expr *expr)
  61. {
  62. const struct nft_quota *priv = nft_expr_priv(expr);
  63. u32 flags = priv->invert ? NFT_QUOTA_F_INV : 0;
  64. if (nla_put_be64(skb, NFTA_QUOTA_BYTES, cpu_to_be64(priv->quota),
  65. NFTA_QUOTA_PAD) ||
  66. nla_put_be32(skb, NFTA_QUOTA_FLAGS, htonl(flags)))
  67. goto nla_put_failure;
  68. return 0;
  69. nla_put_failure:
  70. return -1;
  71. }
  72. static struct nft_expr_type nft_quota_type;
  73. static const struct nft_expr_ops nft_quota_ops = {
  74. .type = &nft_quota_type,
  75. .size = NFT_EXPR_SIZE(sizeof(struct nft_quota)),
  76. .eval = nft_quota_eval,
  77. .init = nft_quota_init,
  78. .dump = nft_quota_dump,
  79. };
  80. static struct nft_expr_type nft_quota_type __read_mostly = {
  81. .name = "quota",
  82. .ops = &nft_quota_ops,
  83. .policy = nft_quota_policy,
  84. .maxattr = NFTA_QUOTA_MAX,
  85. .flags = NFT_EXPR_STATEFUL,
  86. .owner = THIS_MODULE,
  87. };
  88. static int __init nft_quota_module_init(void)
  89. {
  90. return nft_register_expr(&nft_quota_type);
  91. }
  92. static void __exit nft_quota_module_exit(void)
  93. {
  94. nft_unregister_expr(&nft_quota_type);
  95. }
  96. module_init(nft_quota_module_init);
  97. module_exit(nft_quota_module_exit);
  98. MODULE_LICENSE("GPL");
  99. MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>");
  100. MODULE_ALIAS_NFT_EXPR("quota");