xt_limit.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. unsigned long prev;
  19. uint32_t credit;
  20. };
  21. MODULE_LICENSE("GPL");
  22. MODULE_AUTHOR("Herve Eychenne <rv@wallfire.org>");
  23. MODULE_DESCRIPTION("Xtables: rate-limit match");
  24. MODULE_ALIAS("ipt_limit");
  25. MODULE_ALIAS("ip6t_limit");
  26. /* The algorithm used is the Simple Token Bucket Filter (TBF)
  27. * see net/sched/sch_tbf.c in the linux source tree
  28. */
  29. static DEFINE_SPINLOCK(limit_lock);
  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 maxmum 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(&limit_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(&limit_lock);
  69. return true;
  70. }
  71. spin_unlock_bh(&limit_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("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. return 0;
  108. }
  109. static void limit_mt_destroy(const struct xt_mtdtor_param *par)
  110. {
  111. const struct xt_rateinfo *info = par->matchinfo;
  112. kfree(info->master);
  113. }
  114. #ifdef CONFIG_COMPAT
  115. struct compat_xt_rateinfo {
  116. u_int32_t avg;
  117. u_int32_t burst;
  118. compat_ulong_t prev;
  119. u_int32_t credit;
  120. u_int32_t credit_cap, cost;
  121. u_int32_t master;
  122. };
  123. /* To keep the full "prev" timestamp, the upper 32 bits are stored in the
  124. * master pointer, which does not need to be preserved. */
  125. static void limit_mt_compat_from_user(void *dst, const void *src)
  126. {
  127. const struct compat_xt_rateinfo *cm = src;
  128. struct xt_rateinfo m = {
  129. .avg = cm->avg,
  130. .burst = cm->burst,
  131. .prev = cm->prev | (unsigned long)cm->master << 32,
  132. .credit = cm->credit,
  133. .credit_cap = cm->credit_cap,
  134. .cost = cm->cost,
  135. };
  136. memcpy(dst, &m, sizeof(m));
  137. }
  138. static int limit_mt_compat_to_user(void __user *dst, const void *src)
  139. {
  140. const struct xt_rateinfo *m = src;
  141. struct compat_xt_rateinfo cm = {
  142. .avg = m->avg,
  143. .burst = m->burst,
  144. .prev = m->prev,
  145. .credit = m->credit,
  146. .credit_cap = m->credit_cap,
  147. .cost = m->cost,
  148. .master = m->prev >> 32,
  149. };
  150. return copy_to_user(dst, &cm, sizeof(cm)) ? -EFAULT : 0;
  151. }
  152. #endif /* CONFIG_COMPAT */
  153. static struct xt_match limit_mt_reg __read_mostly = {
  154. .name = "limit",
  155. .revision = 0,
  156. .family = NFPROTO_UNSPEC,
  157. .match = limit_mt,
  158. .checkentry = limit_mt_check,
  159. .destroy = limit_mt_destroy,
  160. .matchsize = sizeof(struct xt_rateinfo),
  161. #ifdef CONFIG_COMPAT
  162. .compatsize = sizeof(struct compat_xt_rateinfo),
  163. .compat_from_user = limit_mt_compat_from_user,
  164. .compat_to_user = limit_mt_compat_to_user,
  165. #endif
  166. .me = THIS_MODULE,
  167. };
  168. static int __init limit_mt_init(void)
  169. {
  170. return xt_register_match(&limit_mt_reg);
  171. }
  172. static void __exit limit_mt_exit(void)
  173. {
  174. xt_unregister_match(&limit_mt_reg);
  175. }
  176. module_init(limit_mt_init);
  177. module_exit(limit_mt_exit);