ebt_limit.c 3.3 KB

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