xt_limit.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /* (C) 1999 Jérôme de Vivie <devivie@info.enserb.u-bordeaux.fr>
  2. * (C) 1999 Hervé Eychenne <eychenne@info.enserb.u-bordeaux.fr>
  3. * (C) 2006-2012 Patrick McHardy <kaber@trash.net>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. */
  9. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  10. #include <linux/slab.h>
  11. #include <linux/module.h>
  12. #include <linux/skbuff.h>
  13. #include <linux/spinlock.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/netfilter/x_tables.h>
  16. #include <linux/netfilter/xt_limit.h>
  17. struct xt_limit_priv {
  18. spinlock_t lock;
  19. unsigned long prev;
  20. uint32_t credit;
  21. };
  22. MODULE_LICENSE("GPL");
  23. MODULE_AUTHOR("Herve Eychenne <rv@wallfire.org>");
  24. MODULE_DESCRIPTION("Xtables: rate-limit match");
  25. MODULE_ALIAS("ipt_limit");
  26. MODULE_ALIAS("ip6t_limit");
  27. /* The algorithm used is the Simple Token Bucket Filter (TBF)
  28. * see net/sched/sch_tbf.c in the linux source tree
  29. */
  30. /* Rusty: This is my (non-mathematically-inclined) understanding of
  31. this algorithm. The `average rate' in jiffies becomes your initial
  32. amount of credit `credit' and the most credit you can ever have
  33. `credit_cap'. The `peak rate' becomes the cost of passing the
  34. test, `cost'.
  35. `prev' tracks the last packet hit: you gain one credit per jiffy.
  36. If you get credit balance more than this, the extra credit is
  37. discarded. Every time the match passes, you lose `cost' credits;
  38. if you don't have that many, the test fails.
  39. See Alexey's formal explanation in net/sched/sch_tbf.c.
  40. To get the maximum range, we multiply by this factor (ie. you get N
  41. credits per jiffy). We want to allow a rate as low as 1 per day
  42. (slowest userspace tool allows), which means
  43. CREDITS_PER_JIFFY*HZ*60*60*24 < 2^32. ie. */
  44. #define MAX_CPJ (0xFFFFFFFF / (HZ*60*60*24))
  45. /* Repeated shift and or gives us all 1s, final shift and add 1 gives
  46. * us the power of 2 below the theoretical max, so GCC simply does a
  47. * shift. */
  48. #define _POW2_BELOW2(x) ((x)|((x)>>1))
  49. #define _POW2_BELOW4(x) (_POW2_BELOW2(x)|_POW2_BELOW2((x)>>2))
  50. #define _POW2_BELOW8(x) (_POW2_BELOW4(x)|_POW2_BELOW4((x)>>4))
  51. #define _POW2_BELOW16(x) (_POW2_BELOW8(x)|_POW2_BELOW8((x)>>8))
  52. #define _POW2_BELOW32(x) (_POW2_BELOW16(x)|_POW2_BELOW16((x)>>16))
  53. #define POW2_BELOW32(x) ((_POW2_BELOW32(x)>>1) + 1)
  54. #define CREDITS_PER_JIFFY POW2_BELOW32(MAX_CPJ)
  55. static bool
  56. limit_mt(const struct sk_buff *skb, struct xt_action_param *par)
  57. {
  58. const struct xt_rateinfo *r = par->matchinfo;
  59. struct xt_limit_priv *priv = r->master;
  60. unsigned long now = jiffies;
  61. spin_lock_bh(&priv->lock);
  62. priv->credit += (now - xchg(&priv->prev, now)) * CREDITS_PER_JIFFY;
  63. if (priv->credit > r->credit_cap)
  64. priv->credit = r->credit_cap;
  65. if (priv->credit >= r->cost) {
  66. /* We're not limited. */
  67. priv->credit -= r->cost;
  68. spin_unlock_bh(&priv->lock);
  69. return true;
  70. }
  71. spin_unlock_bh(&priv->lock);
  72. return false;
  73. }
  74. /* Precision saver. */
  75. static u32 user2credits(u32 user)
  76. {
  77. /* If multiplying would overflow... */
  78. if (user > 0xFFFFFFFF / (HZ*CREDITS_PER_JIFFY))
  79. /* Divide first. */
  80. return (user / XT_LIMIT_SCALE) * HZ * CREDITS_PER_JIFFY;
  81. return (user * HZ * CREDITS_PER_JIFFY) / XT_LIMIT_SCALE;
  82. }
  83. static int limit_mt_check(const struct xt_mtchk_param *par)
  84. {
  85. struct xt_rateinfo *r = par->matchinfo;
  86. struct xt_limit_priv *priv;
  87. /* Check for overflow. */
  88. if (r->burst == 0
  89. || user2credits(r->avg * r->burst) < user2credits(r->avg)) {
  90. pr_info_ratelimited("Overflow, try lower: %u/%u\n",
  91. r->avg, r->burst);
  92. return -ERANGE;
  93. }
  94. priv = kmalloc(sizeof(*priv), GFP_KERNEL);
  95. if (priv == NULL)
  96. return -ENOMEM;
  97. /* For SMP, we only want to use one set of state. */
  98. r->master = priv;
  99. /* User avg in seconds * XT_LIMIT_SCALE: convert to jiffies *
  100. 128. */
  101. priv->prev = jiffies;
  102. priv->credit = user2credits(r->avg * r->burst); /* Credits full. */
  103. if (r->cost == 0) {
  104. r->credit_cap = priv->credit; /* Credits full. */
  105. r->cost = user2credits(r->avg);
  106. }
  107. spin_lock_init(&priv->lock);
  108. return 0;
  109. }
  110. static void limit_mt_destroy(const struct xt_mtdtor_param *par)
  111. {
  112. const struct xt_rateinfo *info = par->matchinfo;
  113. kfree(info->master);
  114. }
  115. #ifdef CONFIG_COMPAT
  116. struct compat_xt_rateinfo {
  117. u_int32_t avg;
  118. u_int32_t burst;
  119. compat_ulong_t prev;
  120. u_int32_t credit;
  121. u_int32_t credit_cap, cost;
  122. u_int32_t master;
  123. };
  124. /* To keep the full "prev" timestamp, the upper 32 bits are stored in the
  125. * master pointer, which does not need to be preserved. */
  126. static void limit_mt_compat_from_user(void *dst, const void *src)
  127. {
  128. const struct compat_xt_rateinfo *cm = src;
  129. struct xt_rateinfo m = {
  130. .avg = cm->avg,
  131. .burst = cm->burst,
  132. .prev = cm->prev | (unsigned long)cm->master << 32,
  133. .credit = cm->credit,
  134. .credit_cap = cm->credit_cap,
  135. .cost = cm->cost,
  136. };
  137. memcpy(dst, &m, sizeof(m));
  138. }
  139. static int limit_mt_compat_to_user(void __user *dst, const void *src)
  140. {
  141. const struct xt_rateinfo *m = src;
  142. struct compat_xt_rateinfo cm = {
  143. .avg = m->avg,
  144. .burst = m->burst,
  145. .prev = m->prev,
  146. .credit = m->credit,
  147. .credit_cap = m->credit_cap,
  148. .cost = m->cost,
  149. .master = m->prev >> 32,
  150. };
  151. return copy_to_user(dst, &cm, sizeof(cm)) ? -EFAULT : 0;
  152. }
  153. #endif /* CONFIG_COMPAT */
  154. static struct xt_match limit_mt_reg __read_mostly = {
  155. .name = "limit",
  156. .revision = 0,
  157. .family = NFPROTO_UNSPEC,
  158. .match = limit_mt,
  159. .checkentry = limit_mt_check,
  160. .destroy = limit_mt_destroy,
  161. .matchsize = sizeof(struct xt_rateinfo),
  162. #ifdef CONFIG_COMPAT
  163. .compatsize = sizeof(struct compat_xt_rateinfo),
  164. .compat_from_user = limit_mt_compat_from_user,
  165. .compat_to_user = limit_mt_compat_to_user,
  166. #endif
  167. .usersize = offsetof(struct xt_rateinfo, prev),
  168. .me = THIS_MODULE,
  169. };
  170. static int __init limit_mt_init(void)
  171. {
  172. return xt_register_match(&limit_mt_reg);
  173. }
  174. static void __exit limit_mt_exit(void)
  175. {
  176. xt_unregister_match(&limit_mt_reg);
  177. }
  178. module_init(limit_mt_init);
  179. module_exit(limit_mt_exit);