Linux Audio

Check our new training course

Linux kernel drivers training

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