xt_RATEEST.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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/jhash.h>
  12. #include <linux/rtnetlink.h>
  13. #include <linux/random.h>
  14. #include <linux/slab.h>
  15. #include <net/gen_stats.h>
  16. #include <net/netlink.h>
  17. #include <linux/netfilter/x_tables.h>
  18. #include <linux/netfilter/xt_RATEEST.h>
  19. #include <net/netfilter/xt_rateest.h>
  20. static DEFINE_MUTEX(xt_rateest_mutex);
  21. #define RATEEST_HSIZE 16
  22. static struct hlist_head rateest_hash[RATEEST_HSIZE] __read_mostly;
  23. static unsigned int jhash_rnd __read_mostly;
  24. static unsigned int xt_rateest_hash(const char *name)
  25. {
  26. return jhash(name, FIELD_SIZEOF(struct xt_rateest, name), jhash_rnd) &
  27. (RATEEST_HSIZE - 1);
  28. }
  29. static void xt_rateest_hash_insert(struct xt_rateest *est)
  30. {
  31. unsigned int h;
  32. h = xt_rateest_hash(est->name);
  33. hlist_add_head(&est->list, &rateest_hash[h]);
  34. }
  35. static struct xt_rateest *__xt_rateest_lookup(const char *name)
  36. {
  37. struct xt_rateest *est;
  38. unsigned int h;
  39. h = xt_rateest_hash(name);
  40. hlist_for_each_entry(est, &rateest_hash[h], list) {
  41. if (strcmp(est->name, name) == 0) {
  42. est->refcnt++;
  43. return est;
  44. }
  45. }
  46. return NULL;
  47. }
  48. struct xt_rateest *xt_rateest_lookup(const char *name)
  49. {
  50. struct xt_rateest *est;
  51. mutex_lock(&xt_rateest_mutex);
  52. est = __xt_rateest_lookup(name);
  53. mutex_unlock(&xt_rateest_mutex);
  54. return est;
  55. }
  56. EXPORT_SYMBOL_GPL(xt_rateest_lookup);
  57. void xt_rateest_put(struct xt_rateest *est)
  58. {
  59. mutex_lock(&xt_rateest_mutex);
  60. if (--est->refcnt == 0) {
  61. hlist_del(&est->list);
  62. gen_kill_estimator(&est->bstats, &est->rstats);
  63. /*
  64. * gen_estimator est_timer() might access est->lock or bstats,
  65. * wait a RCU grace period before freeing 'est'
  66. */
  67. kfree_rcu(est, rcu);
  68. }
  69. mutex_unlock(&xt_rateest_mutex);
  70. }
  71. EXPORT_SYMBOL_GPL(xt_rateest_put);
  72. static unsigned int
  73. xt_rateest_tg(struct sk_buff *skb, const struct xt_action_param *par)
  74. {
  75. const struct xt_rateest_target_info *info = par->targinfo;
  76. struct gnet_stats_basic_packed *stats = &info->est->bstats;
  77. spin_lock_bh(&info->est->lock);
  78. stats->bytes += skb->len;
  79. stats->packets++;
  80. spin_unlock_bh(&info->est->lock);
  81. return XT_CONTINUE;
  82. }
  83. static int xt_rateest_tg_checkentry(const struct xt_tgchk_param *par)
  84. {
  85. struct xt_rateest_target_info *info = par->targinfo;
  86. struct xt_rateest *est;
  87. struct {
  88. struct nlattr opt;
  89. struct gnet_estimator est;
  90. } cfg;
  91. int ret;
  92. net_get_random_once(&jhash_rnd, sizeof(jhash_rnd));
  93. mutex_lock(&xt_rateest_mutex);
  94. est = __xt_rateest_lookup(info->name);
  95. if (est) {
  96. mutex_unlock(&xt_rateest_mutex);
  97. /*
  98. * If estimator parameters are specified, they must match the
  99. * existing estimator.
  100. */
  101. if ((!info->interval && !info->ewma_log) ||
  102. (info->interval != est->params.interval ||
  103. info->ewma_log != est->params.ewma_log)) {
  104. xt_rateest_put(est);
  105. return -EINVAL;
  106. }
  107. info->est = est;
  108. return 0;
  109. }
  110. ret = -ENOMEM;
  111. est = kzalloc(sizeof(*est), GFP_KERNEL);
  112. if (!est)
  113. goto err1;
  114. strlcpy(est->name, info->name, sizeof(est->name));
  115. spin_lock_init(&est->lock);
  116. est->refcnt = 1;
  117. est->params.interval = info->interval;
  118. est->params.ewma_log = info->ewma_log;
  119. cfg.opt.nla_len = nla_attr_size(sizeof(cfg.est));
  120. cfg.opt.nla_type = TCA_STATS_RATE_EST;
  121. cfg.est.interval = info->interval;
  122. cfg.est.ewma_log = info->ewma_log;
  123. ret = gen_new_estimator(&est->bstats, NULL, &est->rstats,
  124. &est->lock, NULL, &cfg.opt);
  125. if (ret < 0)
  126. goto err2;
  127. info->est = est;
  128. xt_rateest_hash_insert(est);
  129. mutex_unlock(&xt_rateest_mutex);
  130. return 0;
  131. err2:
  132. kfree(est);
  133. err1:
  134. mutex_unlock(&xt_rateest_mutex);
  135. return ret;
  136. }
  137. static void xt_rateest_tg_destroy(const struct xt_tgdtor_param *par)
  138. {
  139. struct xt_rateest_target_info *info = par->targinfo;
  140. xt_rateest_put(info->est);
  141. }
  142. static struct xt_target xt_rateest_tg_reg __read_mostly = {
  143. .name = "RATEEST",
  144. .revision = 0,
  145. .family = NFPROTO_UNSPEC,
  146. .target = xt_rateest_tg,
  147. .checkentry = xt_rateest_tg_checkentry,
  148. .destroy = xt_rateest_tg_destroy,
  149. .targetsize = sizeof(struct xt_rateest_target_info),
  150. .me = THIS_MODULE,
  151. };
  152. static int __init xt_rateest_tg_init(void)
  153. {
  154. unsigned int i;
  155. for (i = 0; i < ARRAY_SIZE(rateest_hash); i++)
  156. INIT_HLIST_HEAD(&rateest_hash[i]);
  157. return xt_register_target(&xt_rateest_tg_reg);
  158. }
  159. static void __exit xt_rateest_tg_fini(void)
  160. {
  161. xt_unregister_target(&xt_rateest_tg_reg);
  162. }
  163. MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
  164. MODULE_LICENSE("GPL");
  165. MODULE_DESCRIPTION("Xtables: packet rate estimator");
  166. MODULE_ALIAS("ipt_RATEEST");
  167. MODULE_ALIAS("ip6t_RATEEST");
  168. module_init(xt_rateest_tg_init);
  169. module_exit(xt_rateest_tg_fini);