nft_limit.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 last;
  21. u64 tokens;
  22. u64 tokens_max;
  23. u64 rate;
  24. u64 nsecs;
  25. u32 burst;
  26. };
  27. static inline bool nft_limit_eval(struct nft_limit *limit, u64 cost)
  28. {
  29. u64 now, tokens;
  30. s64 delta;
  31. spin_lock_bh(&limit_lock);
  32. now = ktime_get_ns();
  33. tokens = limit->tokens + now - limit->last;
  34. if (tokens > limit->tokens_max)
  35. tokens = limit->tokens_max;
  36. limit->last = now;
  37. delta = tokens - cost;
  38. if (delta >= 0) {
  39. limit->tokens = delta;
  40. spin_unlock_bh(&limit_lock);
  41. return false;
  42. }
  43. limit->tokens = tokens;
  44. spin_unlock_bh(&limit_lock);
  45. return true;
  46. }
  47. static int nft_limit_init(struct nft_limit *limit,
  48. const struct nlattr * const tb[])
  49. {
  50. u64 unit;
  51. if (tb[NFTA_LIMIT_RATE] == NULL ||
  52. tb[NFTA_LIMIT_UNIT] == NULL)
  53. return -EINVAL;
  54. limit->rate = be64_to_cpu(nla_get_be64(tb[NFTA_LIMIT_RATE]));
  55. unit = be64_to_cpu(nla_get_be64(tb[NFTA_LIMIT_UNIT]));
  56. limit->nsecs = unit * NSEC_PER_SEC;
  57. if (limit->rate == 0 || limit->nsecs < unit)
  58. return -EOVERFLOW;
  59. limit->tokens = limit->tokens_max = limit->nsecs;
  60. if (tb[NFTA_LIMIT_BURST]) {
  61. u64 rate;
  62. limit->burst = ntohl(nla_get_be32(tb[NFTA_LIMIT_BURST]));
  63. rate = limit->rate + limit->burst;
  64. if (rate < limit->rate)
  65. return -EOVERFLOW;
  66. limit->rate = rate;
  67. }
  68. limit->last = ktime_get_ns();
  69. return 0;
  70. }
  71. static int nft_limit_dump(struct sk_buff *skb, const struct nft_limit *limit,
  72. enum nft_limit_type type)
  73. {
  74. u64 secs = div_u64(limit->nsecs, NSEC_PER_SEC);
  75. u64 rate = limit->rate - limit->burst;
  76. if (nla_put_be64(skb, NFTA_LIMIT_RATE, cpu_to_be64(rate)) ||
  77. nla_put_be64(skb, NFTA_LIMIT_UNIT, cpu_to_be64(secs)) ||
  78. nla_put_be32(skb, NFTA_LIMIT_BURST, htonl(limit->burst)) ||
  79. nla_put_be32(skb, NFTA_LIMIT_TYPE, htonl(type)))
  80. goto nla_put_failure;
  81. return 0;
  82. nla_put_failure:
  83. return -1;
  84. }
  85. struct nft_limit_pkts {
  86. struct nft_limit limit;
  87. u64 cost;
  88. };
  89. static void nft_limit_pkts_eval(const struct nft_expr *expr,
  90. struct nft_regs *regs,
  91. const struct nft_pktinfo *pkt)
  92. {
  93. struct nft_limit_pkts *priv = nft_expr_priv(expr);
  94. if (nft_limit_eval(&priv->limit, priv->cost))
  95. regs->verdict.code = NFT_BREAK;
  96. }
  97. static const struct nla_policy nft_limit_policy[NFTA_LIMIT_MAX + 1] = {
  98. [NFTA_LIMIT_RATE] = { .type = NLA_U64 },
  99. [NFTA_LIMIT_UNIT] = { .type = NLA_U64 },
  100. [NFTA_LIMIT_BURST] = { .type = NLA_U32 },
  101. [NFTA_LIMIT_TYPE] = { .type = NLA_U32 },
  102. };
  103. static int nft_limit_pkts_init(const struct nft_ctx *ctx,
  104. const struct nft_expr *expr,
  105. const struct nlattr * const tb[])
  106. {
  107. struct nft_limit_pkts *priv = nft_expr_priv(expr);
  108. int err;
  109. err = nft_limit_init(&priv->limit, tb);
  110. if (err < 0)
  111. return err;
  112. priv->cost = div_u64(priv->limit.nsecs, priv->limit.rate);
  113. return 0;
  114. }
  115. static int nft_limit_pkts_dump(struct sk_buff *skb, const struct nft_expr *expr)
  116. {
  117. const struct nft_limit_pkts *priv = nft_expr_priv(expr);
  118. return nft_limit_dump(skb, &priv->limit, NFT_LIMIT_PKTS);
  119. }
  120. static struct nft_expr_type nft_limit_type;
  121. static const struct nft_expr_ops nft_limit_pkts_ops = {
  122. .type = &nft_limit_type,
  123. .size = NFT_EXPR_SIZE(sizeof(struct nft_limit_pkts)),
  124. .eval = nft_limit_pkts_eval,
  125. .init = nft_limit_pkts_init,
  126. .dump = nft_limit_pkts_dump,
  127. };
  128. static void nft_limit_pkt_bytes_eval(const struct nft_expr *expr,
  129. struct nft_regs *regs,
  130. const struct nft_pktinfo *pkt)
  131. {
  132. struct nft_limit *priv = nft_expr_priv(expr);
  133. u64 cost = div_u64(priv->nsecs * pkt->skb->len, priv->rate);
  134. if (nft_limit_eval(priv, cost))
  135. regs->verdict.code = NFT_BREAK;
  136. }
  137. static int nft_limit_pkt_bytes_init(const struct nft_ctx *ctx,
  138. const struct nft_expr *expr,
  139. const struct nlattr * const tb[])
  140. {
  141. struct nft_limit *priv = nft_expr_priv(expr);
  142. return nft_limit_init(priv, tb);
  143. }
  144. static int nft_limit_pkt_bytes_dump(struct sk_buff *skb,
  145. const struct nft_expr *expr)
  146. {
  147. const struct nft_limit *priv = nft_expr_priv(expr);
  148. return nft_limit_dump(skb, priv, NFT_LIMIT_PKT_BYTES);
  149. }
  150. static const struct nft_expr_ops nft_limit_pkt_bytes_ops = {
  151. .type = &nft_limit_type,
  152. .size = NFT_EXPR_SIZE(sizeof(struct nft_limit)),
  153. .eval = nft_limit_pkt_bytes_eval,
  154. .init = nft_limit_pkt_bytes_init,
  155. .dump = nft_limit_pkt_bytes_dump,
  156. };
  157. static const struct nft_expr_ops *
  158. nft_limit_select_ops(const struct nft_ctx *ctx,
  159. const struct nlattr * const tb[])
  160. {
  161. if (tb[NFTA_LIMIT_TYPE] == NULL)
  162. return &nft_limit_pkts_ops;
  163. switch (ntohl(nla_get_be32(tb[NFTA_LIMIT_TYPE]))) {
  164. case NFT_LIMIT_PKTS:
  165. return &nft_limit_pkts_ops;
  166. case NFT_LIMIT_PKT_BYTES:
  167. return &nft_limit_pkt_bytes_ops;
  168. }
  169. return ERR_PTR(-EOPNOTSUPP);
  170. }
  171. static struct nft_expr_type nft_limit_type __read_mostly = {
  172. .name = "limit",
  173. .select_ops = nft_limit_select_ops,
  174. .policy = nft_limit_policy,
  175. .maxattr = NFTA_LIMIT_MAX,
  176. .flags = NFT_EXPR_STATEFUL,
  177. .owner = THIS_MODULE,
  178. };
  179. static int __init nft_limit_module_init(void)
  180. {
  181. return nft_register_expr(&nft_limit_type);
  182. }
  183. static void __exit nft_limit_module_exit(void)
  184. {
  185. nft_unregister_expr(&nft_limit_type);
  186. }
  187. module_init(nft_limit_module_init);
  188. module_exit(nft_limit_module_exit);
  189. MODULE_LICENSE("GPL");
  190. MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
  191. MODULE_ALIAS_NFT_EXPR("limit");