Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * This is a module which is used for logging packets.
0004  */
0005 
0006 /* (C) 1999-2001 Paul `Rusty' Russell
0007  * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
0008  */
0009 
0010 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0011 #include <linux/module.h>
0012 #include <linux/spinlock.h>
0013 #include <linux/skbuff.h>
0014 #include <linux/if_arp.h>
0015 #include <linux/ip.h>
0016 #include <net/ipv6.h>
0017 #include <net/icmp.h>
0018 #include <net/udp.h>
0019 #include <net/tcp.h>
0020 #include <net/route.h>
0021 
0022 #include <linux/netfilter.h>
0023 #include <linux/netfilter/x_tables.h>
0024 #include <linux/netfilter/xt_LOG.h>
0025 #include <linux/netfilter_ipv6/ip6_tables.h>
0026 #include <net/netfilter/nf_log.h>
0027 
0028 static unsigned int
0029 log_tg(struct sk_buff *skb, const struct xt_action_param *par)
0030 {
0031     const struct xt_log_info *loginfo = par->targinfo;
0032     struct net *net = xt_net(par);
0033     struct nf_loginfo li;
0034 
0035     li.type = NF_LOG_TYPE_LOG;
0036     li.u.log.level = loginfo->level;
0037     li.u.log.logflags = loginfo->logflags;
0038 
0039     nf_log_packet(net, xt_family(par), xt_hooknum(par), skb, xt_in(par),
0040               xt_out(par), &li, "%s", loginfo->prefix);
0041     return XT_CONTINUE;
0042 }
0043 
0044 static int log_tg_check(const struct xt_tgchk_param *par)
0045 {
0046     const struct xt_log_info *loginfo = par->targinfo;
0047     int ret;
0048 
0049     if (par->family != NFPROTO_IPV4 && par->family != NFPROTO_IPV6)
0050         return -EINVAL;
0051 
0052     if (loginfo->level >= 8) {
0053         pr_debug("level %u >= 8\n", loginfo->level);
0054         return -EINVAL;
0055     }
0056 
0057     if (loginfo->prefix[sizeof(loginfo->prefix)-1] != '\0') {
0058         pr_debug("prefix is not null-terminated\n");
0059         return -EINVAL;
0060     }
0061 
0062     ret = nf_logger_find_get(par->family, NF_LOG_TYPE_LOG);
0063     if (ret != 0 && !par->nft_compat) {
0064         request_module("%s", "nf_log_syslog");
0065 
0066         ret = nf_logger_find_get(par->family, NF_LOG_TYPE_LOG);
0067     }
0068 
0069     return ret;
0070 }
0071 
0072 static void log_tg_destroy(const struct xt_tgdtor_param *par)
0073 {
0074     nf_logger_put(par->family, NF_LOG_TYPE_LOG);
0075 }
0076 
0077 static struct xt_target log_tg_regs[] __read_mostly = {
0078     {
0079         .name       = "LOG",
0080         .family     = NFPROTO_IPV4,
0081         .target     = log_tg,
0082         .targetsize = sizeof(struct xt_log_info),
0083         .checkentry = log_tg_check,
0084         .destroy    = log_tg_destroy,
0085         .me     = THIS_MODULE,
0086     },
0087 #if IS_ENABLED(CONFIG_IP6_NF_IPTABLES)
0088     {
0089         .name       = "LOG",
0090         .family     = NFPROTO_IPV6,
0091         .target     = log_tg,
0092         .targetsize = sizeof(struct xt_log_info),
0093         .checkentry = log_tg_check,
0094         .destroy    = log_tg_destroy,
0095         .me     = THIS_MODULE,
0096     },
0097 #endif
0098 };
0099 
0100 static int __init log_tg_init(void)
0101 {
0102     return xt_register_targets(log_tg_regs, ARRAY_SIZE(log_tg_regs));
0103 }
0104 
0105 static void __exit log_tg_exit(void)
0106 {
0107     xt_unregister_targets(log_tg_regs, ARRAY_SIZE(log_tg_regs));
0108 }
0109 
0110 module_init(log_tg_init);
0111 module_exit(log_tg_exit);
0112 
0113 MODULE_LICENSE("GPL");
0114 MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
0115 MODULE_AUTHOR("Jan Rekorajski <baggins@pld.org.pl>");
0116 MODULE_DESCRIPTION("Xtables: IPv4/IPv6 packet logging");
0117 MODULE_ALIAS("ipt_LOG");
0118 MODULE_ALIAS("ip6t_LOG");
0119 MODULE_SOFTDEP("pre: nf_log_syslog");