xt_rateest.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * (C) 2007 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. #include <linux/module.h>
  9. #include <linux/skbuff.h>
  10. #include <linux/gen_stats.h>
  11. #include <linux/netfilter/x_tables.h>
  12. #include <linux/netfilter/xt_rateest.h>
  13. #include <net/netfilter/xt_rateest.h>
  14. static bool
  15. xt_rateest_mt(const struct sk_buff *skb, struct xt_action_param *par)
  16. {
  17. const struct xt_rateest_match_info *info = par->matchinfo;
  18. struct gnet_stats_rate_est64 *r;
  19. u_int32_t bps1, bps2, pps1, pps2;
  20. bool ret = true;
  21. spin_lock_bh(&info->est1->lock);
  22. r = &info->est1->rstats;
  23. if (info->flags & XT_RATEEST_MATCH_DELTA) {
  24. bps1 = info->bps1 >= r->bps ? info->bps1 - r->bps : 0;
  25. pps1 = info->pps1 >= r->pps ? info->pps1 - r->pps : 0;
  26. } else {
  27. bps1 = r->bps;
  28. pps1 = r->pps;
  29. }
  30. spin_unlock_bh(&info->est1->lock);
  31. if (info->flags & XT_RATEEST_MATCH_ABS) {
  32. bps2 = info->bps2;
  33. pps2 = info->pps2;
  34. } else {
  35. spin_lock_bh(&info->est2->lock);
  36. r = &info->est2->rstats;
  37. if (info->flags & XT_RATEEST_MATCH_DELTA) {
  38. bps2 = info->bps2 >= r->bps ? info->bps2 - r->bps : 0;
  39. pps2 = info->pps2 >= r->pps ? info->pps2 - r->pps : 0;
  40. } else {
  41. bps2 = r->bps;
  42. pps2 = r->pps;
  43. }
  44. spin_unlock_bh(&info->est2->lock);
  45. }
  46. switch (info->mode) {
  47. case XT_RATEEST_MATCH_LT:
  48. if (info->flags & XT_RATEEST_MATCH_BPS)
  49. ret &= bps1 < bps2;
  50. if (info->flags & XT_RATEEST_MATCH_PPS)
  51. ret &= pps1 < pps2;
  52. break;
  53. case XT_RATEEST_MATCH_GT:
  54. if (info->flags & XT_RATEEST_MATCH_BPS)
  55. ret &= bps1 > bps2;
  56. if (info->flags & XT_RATEEST_MATCH_PPS)
  57. ret &= pps1 > pps2;
  58. break;
  59. case XT_RATEEST_MATCH_EQ:
  60. if (info->flags & XT_RATEEST_MATCH_BPS)
  61. ret &= bps1 == bps2;
  62. if (info->flags & XT_RATEEST_MATCH_PPS)
  63. ret &= pps1 == pps2;
  64. break;
  65. }
  66. ret ^= info->flags & XT_RATEEST_MATCH_INVERT ? true : false;
  67. return ret;
  68. }
  69. static int xt_rateest_mt_checkentry(const struct xt_mtchk_param *par)
  70. {
  71. struct xt_rateest_match_info *info = par->matchinfo;
  72. struct xt_rateest *est1, *est2;
  73. int ret = -EINVAL;
  74. if (hweight32(info->flags & (XT_RATEEST_MATCH_ABS |
  75. XT_RATEEST_MATCH_REL)) != 1)
  76. goto err1;
  77. if (!(info->flags & (XT_RATEEST_MATCH_BPS | XT_RATEEST_MATCH_PPS)))
  78. goto err1;
  79. switch (info->mode) {
  80. case XT_RATEEST_MATCH_EQ:
  81. case XT_RATEEST_MATCH_LT:
  82. case XT_RATEEST_MATCH_GT:
  83. break;
  84. default:
  85. goto err1;
  86. }
  87. ret = -ENOENT;
  88. est1 = xt_rateest_lookup(info->name1);
  89. if (!est1)
  90. goto err1;
  91. est2 = NULL;
  92. if (info->flags & XT_RATEEST_MATCH_REL) {
  93. est2 = xt_rateest_lookup(info->name2);
  94. if (!est2)
  95. goto err2;
  96. }
  97. info->est1 = est1;
  98. info->est2 = est2;
  99. return 0;
  100. err2:
  101. xt_rateest_put(est1);
  102. err1:
  103. return ret;
  104. }
  105. static void xt_rateest_mt_destroy(const struct xt_mtdtor_param *par)
  106. {
  107. struct xt_rateest_match_info *info = par->matchinfo;
  108. xt_rateest_put(info->est1);
  109. if (info->est2)
  110. xt_rateest_put(info->est2);
  111. }
  112. static struct xt_match xt_rateest_mt_reg __read_mostly = {
  113. .name = "rateest",
  114. .revision = 0,
  115. .family = NFPROTO_UNSPEC,
  116. .match = xt_rateest_mt,
  117. .checkentry = xt_rateest_mt_checkentry,
  118. .destroy = xt_rateest_mt_destroy,
  119. .matchsize = sizeof(struct xt_rateest_match_info),
  120. .me = THIS_MODULE,
  121. };
  122. static int __init xt_rateest_mt_init(void)
  123. {
  124. return xt_register_match(&xt_rateest_mt_reg);
  125. }
  126. static void __exit xt_rateest_mt_fini(void)
  127. {
  128. xt_unregister_match(&xt_rateest_mt_reg);
  129. }
  130. MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
  131. MODULE_LICENSE("GPL");
  132. MODULE_DESCRIPTION("xtables rate estimator match");
  133. MODULE_ALIAS("ipt_rateest");
  134. MODULE_ALIAS("ip6t_rateest");
  135. module_init(xt_rateest_mt_init);
  136. module_exit(xt_rateest_mt_fini);