ebt_limit.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * ebt_limit
  3. *
  4. * Authors:
  5. * Tom Marshall <tommy@home.tig-grr.com>
  6. *
  7. * Mostly copied from netfilter's ipt_limit.c, see that file for
  8. * more explanation
  9. *
  10. * September, 2003
  11. *
  12. */
  13. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  14. #include <linux/module.h>
  15. #include <linux/netdevice.h>
  16. #include <linux/spinlock.h>
  17. #include <linux/netfilter/x_tables.h>
  18. #include <linux/netfilter_bridge/ebtables.h>
  19. #include <linux/netfilter_bridge/ebt_limit.h>
  20. static DEFINE_SPINLOCK(limit_lock);
  21. #define MAX_CPJ (0xFFFFFFFF / (HZ*60*60*24))
  22. #define _POW2_BELOW2(x) ((x)|((x)>>1))
  23. #define _POW2_BELOW4(x) (_POW2_BELOW2(x)|_POW2_BELOW2((x)>>2))
  24. #define _POW2_BELOW8(x) (_POW2_BELOW4(x)|_POW2_BELOW4((x)>>4))
  25. #define _POW2_BELOW16(x) (_POW2_BELOW8(x)|_POW2_BELOW8((x)>>8))
  26. #define _POW2_BELOW32(x) (_POW2_BELOW16(x)|_POW2_BELOW16((x)>>16))
  27. #define POW2_BELOW32(x) ((_POW2_BELOW32(x)>>1) + 1)
  28. #define CREDITS_PER_JIFFY POW2_BELOW32(MAX_CPJ)
  29. static bool
  30. ebt_limit_mt(const struct sk_buff *skb, struct xt_action_param *par)
  31. {
  32. struct ebt_limit_info *info = (void *)par->matchinfo;
  33. unsigned long now = jiffies;
  34. spin_lock_bh(&limit_lock);
  35. info->credit += (now - xchg(&info->prev, now)) * CREDITS_PER_JIFFY;
  36. if (info->credit > info->credit_cap)
  37. info->credit = info->credit_cap;
  38. if (info->credit >= info->cost) {
  39. /* We're not limited. */
  40. info->credit -= info->cost;
  41. spin_unlock_bh(&limit_lock);
  42. return true;
  43. }
  44. spin_unlock_bh(&limit_lock);
  45. return false;
  46. }
  47. /* Precision saver. */
  48. static u_int32_t
  49. user2credits(u_int32_t user)
  50. {
  51. /* If multiplying would overflow... */
  52. if (user > 0xFFFFFFFF / (HZ*CREDITS_PER_JIFFY))
  53. /* Divide first. */
  54. return (user / EBT_LIMIT_SCALE) * HZ * CREDITS_PER_JIFFY;
  55. return (user * HZ * CREDITS_PER_JIFFY) / EBT_LIMIT_SCALE;
  56. }
  57. static int ebt_limit_mt_check(const struct xt_mtchk_param *par)
  58. {
  59. struct ebt_limit_info *info = par->matchinfo;
  60. /* Check for overflow. */
  61. if (info->burst == 0 ||
  62. user2credits(info->avg * info->burst) < user2credits(info->avg)) {
  63. pr_info("overflow, try lower: %u/%u\n",
  64. info->avg, info->burst);
  65. return -EINVAL;
  66. }
  67. /* User avg in seconds * EBT_LIMIT_SCALE: convert to jiffies * 128. */
  68. info->prev = jiffies;
  69. info->credit = user2credits(info->avg * info->burst);
  70. info->credit_cap = user2credits(info->avg * info->burst);
  71. info->cost = user2credits(info->avg);
  72. return 0;
  73. }
  74. #ifdef CONFIG_COMPAT
  75. /*
  76. * no conversion function needed --
  77. * only avg/burst have meaningful values in userspace.
  78. */
  79. struct ebt_compat_limit_info {
  80. compat_uint_t avg, burst;
  81. compat_ulong_t prev;
  82. compat_uint_t credit, credit_cap, cost;
  83. };
  84. #endif
  85. static struct xt_match ebt_limit_mt_reg __read_mostly = {
  86. .name = "limit",
  87. .revision = 0,
  88. .family = NFPROTO_BRIDGE,
  89. .match = ebt_limit_mt,
  90. .checkentry = ebt_limit_mt_check,
  91. .matchsize = sizeof(struct ebt_limit_info),
  92. #ifdef CONFIG_COMPAT
  93. .compatsize = sizeof(struct ebt_compat_limit_info),
  94. #endif
  95. .me = THIS_MODULE,
  96. };
  97. static int __init ebt_limit_init(void)
  98. {
  99. return xt_register_match(&ebt_limit_mt_reg);
  100. }
  101. static void __exit ebt_limit_fini(void)
  102. {
  103. xt_unregister_match(&ebt_limit_mt_reg);
  104. }
  105. module_init(ebt_limit_init);
  106. module_exit(ebt_limit_fini);
  107. MODULE_DESCRIPTION("Ebtables: Rate-limit match");
  108. MODULE_LICENSE("GPL");