xt_nfacct.c 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * (C) 2011 Pablo Neira Ayuso <pablo@netfilter.org>
  3. * (C) 2011 Intra2net AG <http://www.intra2net.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 (or any
  7. * later at your option) as published by the Free Software Foundation.
  8. */
  9. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  10. #include <linux/module.h>
  11. #include <linux/skbuff.h>
  12. #include <linux/netfilter/x_tables.h>
  13. #include <linux/netfilter/nfnetlink_acct.h>
  14. #include <linux/netfilter/xt_nfacct.h>
  15. MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>");
  16. MODULE_DESCRIPTION("Xtables: match for the extended accounting infrastructure");
  17. MODULE_LICENSE("GPL");
  18. MODULE_ALIAS("ipt_nfacct");
  19. MODULE_ALIAS("ip6t_nfacct");
  20. static bool nfacct_mt(const struct sk_buff *skb, struct xt_action_param *par)
  21. {
  22. int overquota;
  23. const struct xt_nfacct_match_info *info = par->targinfo;
  24. nfnl_acct_update(skb, info->nfacct);
  25. overquota = nfnl_acct_overquota(xt_net(par), info->nfacct);
  26. return overquota == NFACCT_UNDERQUOTA ? false : true;
  27. }
  28. static int
  29. nfacct_mt_checkentry(const struct xt_mtchk_param *par)
  30. {
  31. struct xt_nfacct_match_info *info = par->matchinfo;
  32. struct nf_acct *nfacct;
  33. nfacct = nfnl_acct_find_get(par->net, info->name);
  34. if (nfacct == NULL) {
  35. pr_info_ratelimited("accounting object `%s' does not exists\n",
  36. info->name);
  37. return -ENOENT;
  38. }
  39. info->nfacct = nfacct;
  40. return 0;
  41. }
  42. static void
  43. nfacct_mt_destroy(const struct xt_mtdtor_param *par)
  44. {
  45. const struct xt_nfacct_match_info *info = par->matchinfo;
  46. nfnl_acct_put(info->nfacct);
  47. }
  48. static struct xt_match nfacct_mt_reg[] __read_mostly = {
  49. {
  50. .name = "nfacct",
  51. .revision = 0,
  52. .family = NFPROTO_UNSPEC,
  53. .checkentry = nfacct_mt_checkentry,
  54. .match = nfacct_mt,
  55. .destroy = nfacct_mt_destroy,
  56. .matchsize = sizeof(struct xt_nfacct_match_info),
  57. .usersize = offsetof(struct xt_nfacct_match_info, nfacct),
  58. .me = THIS_MODULE,
  59. },
  60. {
  61. .name = "nfacct",
  62. .revision = 1,
  63. .family = NFPROTO_UNSPEC,
  64. .checkentry = nfacct_mt_checkentry,
  65. .match = nfacct_mt,
  66. .destroy = nfacct_mt_destroy,
  67. .matchsize = sizeof(struct xt_nfacct_match_info_v1),
  68. .usersize = offsetof(struct xt_nfacct_match_info_v1, nfacct),
  69. .me = THIS_MODULE,
  70. },
  71. };
  72. static int __init nfacct_mt_init(void)
  73. {
  74. return xt_register_matches(nfacct_mt_reg, ARRAY_SIZE(nfacct_mt_reg));
  75. }
  76. static void __exit nfacct_mt_exit(void)
  77. {
  78. xt_unregister_matches(nfacct_mt_reg, ARRAY_SIZE(nfacct_mt_reg));
  79. }
  80. module_init(nfacct_mt_init);
  81. module_exit(nfacct_mt_exit);