xt_statistic.c 2.4 KB

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