nft_limit.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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/spinlock.h>
  14. #include <linux/netlink.h>
  15. #include <linux/netfilter.h>
  16. #include <linux/netfilter/nf_tables.h>
  17. #include <net/netfilter/nf_tables.h>
  18. static DEFINE_SPINLOCK(limit_lock);
  19. struct nft_limit {
  20. u64 tokens;
  21. u64 rate;
  22. u64 unit;
  23. unsigned long stamp;
  24. };
  25. static void nft_limit_eval(const struct nft_expr *expr,
  26. struct nft_data data[NFT_REG_MAX + 1],
  27. const struct nft_pktinfo *pkt)
  28. {
  29. struct nft_limit *priv = nft_expr_priv(expr);
  30. spin_lock_bh(&limit_lock);
  31. if (time_after_eq(jiffies, priv->stamp)) {
  32. priv->tokens = priv->rate;
  33. priv->stamp = jiffies + priv->unit * HZ;
  34. }
  35. if (priv->tokens >= 1) {
  36. priv->tokens--;
  37. spin_unlock_bh(&limit_lock);
  38. return;
  39. }
  40. spin_unlock_bh(&limit_lock);
  41. data[NFT_REG_VERDICT].verdict = NFT_BREAK;
  42. }
  43. static const struct nla_policy nft_limit_policy[NFTA_LIMIT_MAX + 1] = {
  44. [NFTA_LIMIT_RATE] = { .type = NLA_U64 },
  45. [NFTA_LIMIT_UNIT] = { .type = NLA_U64 },
  46. };
  47. static int nft_limit_init(const struct nft_ctx *ctx,
  48. const struct nft_expr *expr,
  49. const struct nlattr * const tb[])
  50. {
  51. struct nft_limit *priv = nft_expr_priv(expr);
  52. if (tb[NFTA_LIMIT_RATE] == NULL ||
  53. tb[NFTA_LIMIT_UNIT] == NULL)
  54. return -EINVAL;
  55. priv->rate = be64_to_cpu(nla_get_be64(tb[NFTA_LIMIT_RATE]));
  56. priv->unit = be64_to_cpu(nla_get_be64(tb[NFTA_LIMIT_UNIT]));
  57. priv->stamp = jiffies + priv->unit * HZ;
  58. priv->tokens = priv->rate;
  59. return 0;
  60. }
  61. static int nft_limit_dump(struct sk_buff *skb, const struct nft_expr *expr)
  62. {
  63. const struct nft_limit *priv = nft_expr_priv(expr);
  64. if (nla_put_be64(skb, NFTA_LIMIT_RATE, cpu_to_be64(priv->rate)))
  65. goto nla_put_failure;
  66. if (nla_put_be64(skb, NFTA_LIMIT_UNIT, cpu_to_be64(priv->unit)))
  67. goto nla_put_failure;
  68. return 0;
  69. nla_put_failure:
  70. return -1;
  71. }
  72. static struct nft_expr_type nft_limit_type;
  73. static const struct nft_expr_ops nft_limit_ops = {
  74. .type = &nft_limit_type,
  75. .size = NFT_EXPR_SIZE(sizeof(struct nft_limit)),
  76. .eval = nft_limit_eval,
  77. .init = nft_limit_init,
  78. .dump = nft_limit_dump,
  79. };
  80. static struct nft_expr_type nft_limit_type __read_mostly = {
  81. .name = "limit",
  82. .ops = &nft_limit_ops,
  83. .policy = nft_limit_policy,
  84. .maxattr = NFTA_LIMIT_MAX,
  85. .owner = THIS_MODULE,
  86. };
  87. static int __init nft_limit_module_init(void)
  88. {
  89. return nft_register_expr(&nft_limit_type);
  90. }
  91. static void __exit nft_limit_module_exit(void)
  92. {
  93. nft_unregister_expr(&nft_limit_type);
  94. }
  95. module_init(nft_limit_module_init);
  96. module_exit(nft_limit_module_exit);
  97. MODULE_LICENSE("GPL");
  98. MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
  99. MODULE_ALIAS_NFT_EXPR("limit");