xt_RATEEST.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. struct hlist_node *n;
  40. unsigned int h;
  41. h = xt_rateest_hash(name);
  42. mutex_lock(&xt_rateest_mutex);
  43. hlist_for_each_entry(est, n, &rateest_hash[h], list) {
  44. if (strcmp(est->name, name) == 0) {
  45. est->refcnt++;
  46. mutex_unlock(&xt_rateest_mutex);
  47. return est;
  48. }
  49. }
  50. mutex_unlock(&xt_rateest_mutex);
  51. return NULL;
  52. }
  53. EXPORT_SYMBOL_GPL(xt_rateest_lookup);
  54. static void xt_rateest_free_rcu(struct rcu_head *head)
  55. {
  56. kfree(container_of(head, struct xt_rateest, rcu));
  57. }
  58. void xt_rateest_put(struct xt_rateest *est)
  59. {
  60. mutex_lock(&xt_rateest_mutex);
  61. if (--est->refcnt == 0) {
  62. hlist_del(&est->list);
  63. gen_kill_estimator(&est->bstats, &est->rstats);
  64. /*
  65. * gen_estimator est_timer() might access est->lock or bstats,
  66. * wait a RCU grace period before freeing 'est'
  67. */
  68. call_rcu(&est->rcu, xt_rateest_free_rcu);
  69. }
  70. mutex_unlock(&xt_rateest_mutex);
  71. }
  72. EXPORT_SYMBOL_GPL(xt_rateest_put);
  73. static unsigned int
  74. xt_rateest_tg(struct sk_buff *skb, const struct xt_action_param *par)
  75. {
  76. const struct xt_rateest_target_info *info = par->targinfo;
  77. struct gnet_stats_basic_packed *stats = &info->est->bstats;
  78. spin_lock_bh(&info->est->lock);
  79. stats->bytes += skb->len;
  80. stats->packets++;
  81. spin_unlock_bh(&info->est->lock);
  82. return XT_CONTINUE;
  83. }
  84. static int xt_rateest_tg_checkentry(const struct xt_tgchk_param *par)
  85. {
  86. struct xt_rateest_target_info *info = par->targinfo;
  87. struct xt_rateest *est;
  88. struct {
  89. struct nlattr opt;
  90. struct gnet_estimator est;
  91. } cfg;
  92. int ret;
  93. if (unlikely(!rnd_inited)) {
  94. get_random_bytes(&jhash_rnd, sizeof(jhash_rnd));
  95. rnd_inited = true;
  96. }
  97. est = xt_rateest_lookup(info->name);
  98. if (est) {
  99. /*
  100. * If estimator parameters are specified, they must match the
  101. * existing estimator.
  102. */
  103. if ((!info->interval && !info->ewma_log) ||
  104. (info->interval != est->params.interval ||
  105. info->ewma_log != est->params.ewma_log)) {
  106. xt_rateest_put(est);
  107. return -EINVAL;
  108. }
  109. info->est = est;
  110. return 0;
  111. }
  112. ret = -ENOMEM;
  113. est = kzalloc(sizeof(*est), GFP_KERNEL);
  114. if (!est)
  115. goto err1;
  116. strlcpy(est->name, info->name, sizeof(est->name));
  117. spin_lock_init(&est->lock);
  118. est->refcnt = 1;
  119. est->params.interval = info->interval;
  120. est->params.ewma_log = info->ewma_log;
  121. cfg.opt.nla_len = nla_attr_size(sizeof(cfg.est));
  122. cfg.opt.nla_type = TCA_STATS_RATE_EST;
  123. cfg.est.interval = info->interval;
  124. cfg.est.ewma_log = info->ewma_log;
  125. ret = gen_new_estimator(&est->bstats, &est->rstats,
  126. &est->lock, &cfg.opt);
  127. if (ret < 0)
  128. goto err2;
  129. info->est = est;
  130. xt_rateest_hash_insert(est);
  131. return 0;
  132. err2:
  133. kfree(est);
  134. err1:
  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. rcu_barrier(); /* Wait for completion of call_rcu()'s (xt_rateest_free_rcu) */
  163. }
  164. MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
  165. MODULE_LICENSE("GPL");
  166. MODULE_DESCRIPTION("Xtables: packet rate estimator");
  167. MODULE_ALIAS("ipt_RATEEST");
  168. MODULE_ALIAS("ip6t_RATEEST");
  169. module_init(xt_rateest_tg_init);
  170. module_exit(xt_rateest_tg_fini);