xt_nfacct.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. #include <linux/module.h>
  10. #include <linux/skbuff.h>
  11. #include <linux/netfilter/x_tables.h>
  12. #include <linux/netfilter/nfnetlink_acct.h>
  13. #include <linux/netfilter/xt_nfacct.h>
  14. MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>");
  15. MODULE_DESCRIPTION("Xtables: match for the extended accounting infrastructure");
  16. MODULE_LICENSE("GPL");
  17. MODULE_ALIAS("ipt_nfacct");
  18. MODULE_ALIAS("ip6t_nfacct");
  19. static bool nfacct_mt(const struct sk_buff *skb, struct xt_action_param *par)
  20. {
  21. int overquota;
  22. const struct xt_nfacct_match_info *info = par->targinfo;
  23. nfnl_acct_update(skb, info->nfacct);
  24. overquota = nfnl_acct_overquota(skb, info->nfacct);
  25. return overquota == NFACCT_UNDERQUOTA ? false : true;
  26. }
  27. static int
  28. nfacct_mt_checkentry(const struct xt_mtchk_param *par)
  29. {
  30. struct xt_nfacct_match_info *info = par->matchinfo;
  31. struct nf_acct *nfacct;
  32. nfacct = nfnl_acct_find_get(info->name);
  33. if (nfacct == NULL) {
  34. pr_info("xt_nfacct: accounting object with name `%s' "
  35. "does not exists\n", info->name);
  36. return -ENOENT;
  37. }
  38. info->nfacct = nfacct;
  39. return 0;
  40. }
  41. static void
  42. nfacct_mt_destroy(const struct xt_mtdtor_param *par)
  43. {
  44. const struct xt_nfacct_match_info *info = par->matchinfo;
  45. nfnl_acct_put(info->nfacct);
  46. }
  47. static struct xt_match nfacct_mt_reg __read_mostly = {
  48. .name = "nfacct",
  49. .family = NFPROTO_UNSPEC,
  50. .checkentry = nfacct_mt_checkentry,
  51. .match = nfacct_mt,
  52. .destroy = nfacct_mt_destroy,
  53. .matchsize = sizeof(struct xt_nfacct_match_info),
  54. .me = THIS_MODULE,
  55. };
  56. static int __init nfacct_mt_init(void)
  57. {
  58. return xt_register_match(&nfacct_mt_reg);
  59. }
  60. static void __exit nfacct_mt_exit(void)
  61. {
  62. xt_unregister_match(&nfacct_mt_reg);
  63. }
  64. module_init(nfacct_mt_init);
  65. module_exit(nfacct_mt_exit);