nft_limit.c 6.1 KB

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