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