xt_statistic.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * Copyright (c) 2006 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. * Based on ipt_random and ipt_nth by Fabrice MARIE <fabrice@netfilter.org>.
  9. */
  10. #include <linux/init.h>
  11. #include <linux/spinlock.h>
  12. #include <linux/skbuff.h>
  13. #include <linux/net.h>
  14. #include <linux/slab.h>
  15. #include <linux/netfilter/xt_statistic.h>
  16. #include <linux/netfilter/x_tables.h>
  17. struct xt_statistic_priv {
  18. atomic_t count;
  19. } ____cacheline_aligned_in_smp;
  20. MODULE_LICENSE("GPL");
  21. MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
  22. MODULE_DESCRIPTION("Xtables: statistics-based matching (\"Nth\", random)");
  23. MODULE_ALIAS("ipt_statistic");
  24. MODULE_ALIAS("ip6t_statistic");
  25. static bool
  26. statistic_mt(const struct sk_buff *skb, struct xt_action_param *par)
  27. {
  28. const struct xt_statistic_info *info = par->matchinfo;
  29. bool ret = info->flags & XT_STATISTIC_INVERT;
  30. int nval, oval;
  31. switch (info->mode) {
  32. case XT_STATISTIC_MODE_RANDOM:
  33. if ((net_random() & 0x7FFFFFFF) < info->u.random.probability)
  34. ret = !ret;
  35. break;
  36. case XT_STATISTIC_MODE_NTH:
  37. do {
  38. oval = atomic_read(&info->master->count);
  39. nval = (oval == info->u.nth.every) ? 0 : oval + 1;
  40. } while (atomic_cmpxchg(&info->master->count, oval, nval) != oval);
  41. if (nval == 0)
  42. ret = !ret;
  43. break;
  44. }
  45. return ret;
  46. }
  47. static int statistic_mt_check(const struct xt_mtchk_param *par)
  48. {
  49. struct xt_statistic_info *info = par->matchinfo;
  50. if (info->mode > XT_STATISTIC_MODE_MAX ||
  51. info->flags & ~XT_STATISTIC_MASK)
  52. return -EINVAL;
  53. info->master = kzalloc(sizeof(*info->master), GFP_KERNEL);
  54. if (info->master == NULL)
  55. return -ENOMEM;
  56. atomic_set(&info->master->count, info->u.nth.count);
  57. return 0;
  58. }
  59. static void statistic_mt_destroy(const struct xt_mtdtor_param *par)
  60. {
  61. const struct xt_statistic_info *info = par->matchinfo;
  62. kfree(info->master);
  63. }
  64. static struct xt_match xt_statistic_mt_reg __read_mostly = {
  65. .name = "statistic",
  66. .revision = 0,
  67. .family = NFPROTO_UNSPEC,
  68. .match = statistic_mt,
  69. .checkentry = statistic_mt_check,
  70. .destroy = statistic_mt_destroy,
  71. .matchsize = sizeof(struct xt_statistic_info),
  72. .me = THIS_MODULE,
  73. };
  74. static int __init statistic_mt_init(void)
  75. {
  76. return xt_register_match(&xt_statistic_mt_reg);
  77. }
  78. static void __exit statistic_mt_exit(void)
  79. {
  80. xt_unregister_match(&xt_statistic_mt_reg);
  81. }
  82. module_init(statistic_mt_init);
  83. module_exit(statistic_mt_exit);