xt_LOG.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * This is a module which is used for logging packets.
  4. */
  5. /* (C) 1999-2001 Paul `Rusty' Russell
  6. * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
  7. */
  8. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  9. #include <linux/module.h>
  10. #include <linux/spinlock.h>
  11. #include <linux/skbuff.h>
  12. #include <linux/if_arp.h>
  13. #include <linux/ip.h>
  14. #include <net/ipv6.h>
  15. #include <net/icmp.h>
  16. #include <net/udp.h>
  17. #include <net/tcp.h>
  18. #include <net/route.h>
  19. #include <linux/netfilter.h>
  20. #include <linux/netfilter/x_tables.h>
  21. #include <linux/netfilter/xt_LOG.h>
  22. #include <linux/netfilter_ipv6/ip6_tables.h>
  23. #include <net/netfilter/nf_log.h>
  24. static unsigned int
  25. log_tg(struct sk_buff *skb, const struct xt_action_param *par)
  26. {
  27. const struct xt_log_info *loginfo = par->targinfo;
  28. struct net *net = xt_net(par);
  29. struct nf_loginfo li;
  30. li.type = NF_LOG_TYPE_LOG;
  31. li.u.log.level = loginfo->level;
  32. li.u.log.logflags = loginfo->logflags;
  33. nf_log_packet(net, xt_family(par), xt_hooknum(par), skb, xt_in(par),
  34. xt_out(par), &li, "%s", loginfo->prefix);
  35. return XT_CONTINUE;
  36. }
  37. static int log_tg_check(const struct xt_tgchk_param *par)
  38. {
  39. const struct xt_log_info *loginfo = par->targinfo;
  40. if (par->family != NFPROTO_IPV4 && par->family != NFPROTO_IPV6)
  41. return -EINVAL;
  42. if (loginfo->level >= 8) {
  43. pr_debug("level %u >= 8\n", loginfo->level);
  44. return -EINVAL;
  45. }
  46. if (loginfo->prefix[sizeof(loginfo->prefix)-1] != '\0') {
  47. pr_debug("prefix is not null-terminated\n");
  48. return -EINVAL;
  49. }
  50. return nf_logger_find_get(par->family, NF_LOG_TYPE_LOG);
  51. }
  52. static void log_tg_destroy(const struct xt_tgdtor_param *par)
  53. {
  54. nf_logger_put(par->family, NF_LOG_TYPE_LOG);
  55. }
  56. static struct xt_target log_tg_regs[] __read_mostly = {
  57. {
  58. .name = "LOG",
  59. .family = NFPROTO_IPV4,
  60. .target = log_tg,
  61. .targetsize = sizeof(struct xt_log_info),
  62. .checkentry = log_tg_check,
  63. .destroy = log_tg_destroy,
  64. .me = THIS_MODULE,
  65. },
  66. #if IS_ENABLED(CONFIG_IP6_NF_IPTABLES)
  67. {
  68. .name = "LOG",
  69. .family = NFPROTO_IPV6,
  70. .target = log_tg,
  71. .targetsize = sizeof(struct xt_log_info),
  72. .checkentry = log_tg_check,
  73. .destroy = log_tg_destroy,
  74. .me = THIS_MODULE,
  75. },
  76. #endif
  77. };
  78. static int __init log_tg_init(void)
  79. {
  80. return xt_register_targets(log_tg_regs, ARRAY_SIZE(log_tg_regs));
  81. }
  82. static void __exit log_tg_exit(void)
  83. {
  84. xt_unregister_targets(log_tg_regs, ARRAY_SIZE(log_tg_regs));
  85. }
  86. module_init(log_tg_init);
  87. module_exit(log_tg_exit);
  88. MODULE_LICENSE("GPL");
  89. MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
  90. MODULE_AUTHOR("Jan Rekorajski <baggins@pld.org.pl>");
  91. MODULE_DESCRIPTION("Xtables: IPv4/IPv6 packet logging");
  92. MODULE_ALIAS("ipt_LOG");
  93. MODULE_ALIAS("ip6t_LOG");