Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Richard Weinberger | 6939c33 | 2012-02-10 23:10:52 +0100 | [diff] [blame] | 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> |
Richard Weinberger | 6939c33 | 2012-02-10 23:10:52 +0100 | [diff] [blame] | 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> |
Richard Weinberger | 6939c33 | 2012-02-10 23:10:52 +0100 | [diff] [blame] | 27 | |
Richard Weinberger | 6939c33 | 2012-02-10 23:10:52 +0100 | [diff] [blame] | 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; |
Pablo Neira Ayuso | 613dbd9 | 2016-11-03 10:56:21 +0100 | [diff] [blame] | 32 | struct net *net = xt_net(par); |
Richard Weinberger | 6939c33 | 2012-02-10 23:10:52 +0100 | [diff] [blame] | 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 | |
Pablo Neira Ayuso | 613dbd9 | 2016-11-03 10:56:21 +0100 | [diff] [blame] | 39 | nf_log_packet(net, xt_family(par), xt_hooknum(par), skb, xt_in(par), |
| 40 | xt_out(par), &li, "%s", loginfo->prefix); |
Richard Weinberger | 6939c33 | 2012-02-10 23:10:52 +0100 | [diff] [blame] | 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 | |
Pablo Neira Ayuso | fab4085 | 2014-06-18 19:38:25 +0200 | [diff] [blame] | 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); |
Richard Weinberger | 6939c33 | 2012-02-10 23:10:52 +0100 | [diff] [blame] | 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, |
Pablo Neira Ayuso | fab4085 | 2014-06-18 19:38:25 +0200 | [diff] [blame] | 76 | .destroy = log_tg_destroy, |
Richard Weinberger | 6939c33 | 2012-02-10 23:10:52 +0100 | [diff] [blame] | 77 | .me = THIS_MODULE, |
| 78 | }, |
Pablo Neira Ayuso | a0f65a26 | 2012-03-10 12:15:15 +0100 | [diff] [blame] | 79 | #if IS_ENABLED(CONFIG_IP6_NF_IPTABLES) |
Richard Weinberger | 6939c33 | 2012-02-10 23:10:52 +0100 | [diff] [blame] | 80 | { |
| 81 | .name = "LOG", |
| 82 | .family = NFPROTO_IPV6, |
| 83 | .target = log_tg, |
| 84 | .targetsize = sizeof(struct xt_log_info), |
| 85 | .checkentry = log_tg_check, |
Pablo Neira Ayuso | fab4085 | 2014-06-18 19:38:25 +0200 | [diff] [blame] | 86 | .destroy = log_tg_destroy, |
Richard Weinberger | 6939c33 | 2012-02-10 23:10:52 +0100 | [diff] [blame] | 87 | .me = THIS_MODULE, |
| 88 | }, |
| 89 | #endif |
| 90 | }; |
| 91 | |
Richard Weinberger | 6939c33 | 2012-02-10 23:10:52 +0100 | [diff] [blame] | 92 | static int __init log_tg_init(void) |
| 93 | { |
Pablo Neira Ayuso | 83e96d4 | 2014-06-19 20:47:14 +0200 | [diff] [blame] | 94 | return xt_register_targets(log_tg_regs, ARRAY_SIZE(log_tg_regs)); |
Richard Weinberger | 6939c33 | 2012-02-10 23:10:52 +0100 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | static void __exit log_tg_exit(void) |
| 98 | { |
Richard Weinberger | 6939c33 | 2012-02-10 23:10:52 +0100 | [diff] [blame] | 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"); |