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