xt_RATEEST.c 4.6 KB

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