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/netfilter/xt_statistic.h>
  15. #include <linux/netfilter/x_tables.h>
  16. MODULE_LICENSE("GPL");
  17. MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
  18. MODULE_DESCRIPTION("xtables statistical match module");
  19. MODULE_ALIAS("ipt_statistic");
  20. MODULE_ALIAS("ip6t_statistic");
  21. static DEFINE_SPINLOCK(nth_lock);
  22. static int
  23. match(const struct sk_buff *skb,
  24. const struct net_device *in, const struct net_device *out,
  25. const struct xt_match *match, const void *matchinfo,
  26. int offset, unsigned int protoff, int *hotdrop)
  27. {
  28. struct xt_statistic_info *info = (struct xt_statistic_info *)matchinfo;
  29. int ret = info->flags & XT_STATISTIC_INVERT ? 1 : 0;
  30. switch (info->mode) {
  31. case XT_STATISTIC_MODE_RANDOM:
  32. if ((net_random() & 0x7FFFFFFF) < info->u.random.probability)
  33. ret ^= 1;
  34. break;
  35. case XT_STATISTIC_MODE_NTH:
  36. info = info->master;
  37. spin_lock_bh(&nth_lock);
  38. if (info->u.nth.count++ == info->u.nth.every) {
  39. info->u.nth.count = 0;
  40. ret ^= 1;
  41. }
  42. spin_unlock_bh(&nth_lock);
  43. break;
  44. }
  45. return ret;
  46. }
  47. static int
  48. checkentry(const char *tablename, const void *entry,
  49. const struct xt_match *match, void *matchinfo,
  50. unsigned int hook_mask)
  51. {
  52. struct xt_statistic_info *info = (struct xt_statistic_info *)matchinfo;
  53. if (info->mode > XT_STATISTIC_MODE_MAX ||
  54. info->flags & ~XT_STATISTIC_MASK)
  55. return 0;
  56. info->master = info;
  57. return 1;
  58. }
  59. static struct xt_match xt_statistic_match[] = {
  60. {
  61. .name = "statistic",
  62. .family = AF_INET,
  63. .checkentry = checkentry,
  64. .match = match,
  65. .matchsize = sizeof(struct xt_statistic_info),
  66. .me = THIS_MODULE,
  67. },
  68. {
  69. .name = "statistic",
  70. .family = AF_INET6,
  71. .checkentry = checkentry,
  72. .match = match,
  73. .matchsize = sizeof(struct xt_statistic_info),
  74. .me = THIS_MODULE,
  75. },
  76. };
  77. static int __init xt_statistic_init(void)
  78. {
  79. return xt_register_matches(xt_statistic_match,
  80. ARRAY_SIZE(xt_statistic_match));
  81. }
  82. static void __exit xt_statistic_fini(void)
  83. {
  84. xt_unregister_matches(xt_statistic_match,
  85. ARRAY_SIZE(xt_statistic_match));
  86. }
  87. module_init(xt_statistic_init);
  88. module_exit(xt_statistic_fini);