xt_NFLOG.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2006 Patrick McHardy <kaber@trash.net>
  4. */
  5. #include <linux/module.h>
  6. #include <linux/init.h>
  7. #include <linux/skbuff.h>
  8. #include <linux/netfilter/x_tables.h>
  9. #include <linux/netfilter/xt_NFLOG.h>
  10. #include <net/netfilter/nf_log.h>
  11. MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
  12. MODULE_DESCRIPTION("Xtables: packet logging to netlink using NFLOG");
  13. MODULE_LICENSE("GPL");
  14. MODULE_ALIAS("ipt_NFLOG");
  15. MODULE_ALIAS("ip6t_NFLOG");
  16. static unsigned int
  17. nflog_tg(struct sk_buff *skb, const struct xt_action_param *par)
  18. {
  19. const struct xt_nflog_info *info = par->targinfo;
  20. struct net *net = xt_net(par);
  21. struct nf_loginfo li;
  22. li.type = NF_LOG_TYPE_ULOG;
  23. li.u.ulog.copy_len = info->len;
  24. li.u.ulog.group = info->group;
  25. li.u.ulog.qthreshold = info->threshold;
  26. li.u.ulog.flags = 0;
  27. if (info->flags & XT_NFLOG_F_COPY_LEN)
  28. li.u.ulog.flags |= NF_LOG_F_COPY_LEN;
  29. nf_log_packet(net, xt_family(par), xt_hooknum(par), skb, xt_in(par),
  30. xt_out(par), &li, "%s", info->prefix);
  31. return XT_CONTINUE;
  32. }
  33. static int nflog_tg_check(const struct xt_tgchk_param *par)
  34. {
  35. const struct xt_nflog_info *info = par->targinfo;
  36. if (info->flags & ~XT_NFLOG_MASK)
  37. return -EINVAL;
  38. if (info->prefix[sizeof(info->prefix) - 1] != '\0')
  39. return -EINVAL;
  40. return nf_logger_find_get(par->family, NF_LOG_TYPE_ULOG);
  41. }
  42. static void nflog_tg_destroy(const struct xt_tgdtor_param *par)
  43. {
  44. nf_logger_put(par->family, NF_LOG_TYPE_ULOG);
  45. }
  46. static struct xt_target nflog_tg_reg __read_mostly = {
  47. .name = "NFLOG",
  48. .revision = 0,
  49. .family = NFPROTO_UNSPEC,
  50. .checkentry = nflog_tg_check,
  51. .destroy = nflog_tg_destroy,
  52. .target = nflog_tg,
  53. .targetsize = sizeof(struct xt_nflog_info),
  54. .me = THIS_MODULE,
  55. };
  56. static int __init nflog_tg_init(void)
  57. {
  58. return xt_register_target(&nflog_tg_reg);
  59. }
  60. static void __exit nflog_tg_exit(void)
  61. {
  62. xt_unregister_target(&nflog_tg_reg);
  63. }
  64. module_init(nflog_tg_init);
  65. module_exit(nflog_tg_exit);