linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2 1/1] netfilter: load nf_log_syslog on enabling nf_conntrack_log_invalid
@ 2025-05-26  8:59 Lance Yang
  2025-05-28 11:05 ` Florian Westphal
  2025-07-24 12:24 ` Pablo Neira Ayuso
  0 siblings, 2 replies; 7+ messages in thread
From: Lance Yang @ 2025-05-26  8:59 UTC (permalink / raw)
  To: fw, pablo
  Cc: coreteam, davem, edumazet, horms, kadlec, kuba, linux-kernel,
	netfilter-devel, pabeni, zi.li, Lance Yang

From: Lance Yang <lance.yang@linux.dev>

When no logger is registered, nf_conntrack_log_invalid fails to log invalid
packets, leaving users unaware of actual invalid traffic. Improve this by
loading nf_log_syslog, similar to how 'iptables -I FORWARD 1 -m conntrack
--ctstate INVALID -j LOG' triggers it.

Suggested-by: Florian Westphal <fw@strlen.de>
Signed-off-by: Zi Li <zi.li@linux.dev>
Signed-off-by: Lance Yang <lance.yang@linux.dev>
---
v1 -> v2:
 - Add a new, simpler helper (per Florian)
 - Load the module only when no logger is registered (per Florian)
 - https://lore.kernel.org/all/20250514053751.2271-1-lance.yang@linux.dev/

 include/net/netfilter/nf_log.h          |  3 +++
 net/netfilter/nf_conntrack_standalone.c | 26 +++++++++++++++++++++++-
 net/netfilter/nf_log.c                  | 27 +++++++++++++++++++++++++
 3 files changed, 55 insertions(+), 1 deletion(-)

diff --git a/include/net/netfilter/nf_log.h b/include/net/netfilter/nf_log.h
index e55eedc84ed7..00506792a06d 100644
--- a/include/net/netfilter/nf_log.h
+++ b/include/net/netfilter/nf_log.h
@@ -59,6 +59,9 @@ extern int sysctl_nf_log_all_netns;
 int nf_log_register(u_int8_t pf, struct nf_logger *logger);
 void nf_log_unregister(struct nf_logger *logger);
 
+/* Check if any logger is registered for a given protocol family. */
+bool nf_log_is_registered(u_int8_t pf);
+
 int nf_log_set(struct net *net, u_int8_t pf, const struct nf_logger *logger);
 void nf_log_unset(struct net *net, const struct nf_logger *logger);
 
diff --git a/net/netfilter/nf_conntrack_standalone.c b/net/netfilter/nf_conntrack_standalone.c
index 2f666751c7e7..cdc27424f84a 100644
--- a/net/netfilter/nf_conntrack_standalone.c
+++ b/net/netfilter/nf_conntrack_standalone.c
@@ -14,6 +14,7 @@
 #include <linux/sysctl.h>
 #endif
 
+#include <net/netfilter/nf_log.h>
 #include <net/netfilter/nf_conntrack.h>
 #include <net/netfilter/nf_conntrack_core.h>
 #include <net/netfilter/nf_conntrack_l4proto.h>
@@ -543,6 +544,29 @@ nf_conntrack_hash_sysctl(const struct ctl_table *table, int write,
 	return ret;
 }
 
+static int
+nf_conntrack_log_invalid_sysctl(const struct ctl_table *table, int write,
+				void *buffer, size_t *lenp, loff_t *ppos)
+{
+	int ret, i;
+
+	ret = proc_dou8vec_minmax(table, write, buffer, lenp, ppos);
+	if (ret < 0 || !write)
+		return ret;
+
+	if (*(u8 *)table->data == 0)
+		return ret;
+
+	/* Load nf_log_syslog only if no logger is currently registered */
+	for (i = 0; i < NFPROTO_NUMPROTO; i++) {
+		if (nf_log_is_registered(i))
+			return ret;
+	}
+	request_module("%s", "nf_log_syslog");
+
+	return ret;
+}
+
 static struct ctl_table_header *nf_ct_netfilter_header;
 
 enum nf_ct_sysctl_index {
@@ -649,7 +673,7 @@ static struct ctl_table nf_ct_sysctl_table[] = {
 		.data		= &init_net.ct.sysctl_log_invalid,
 		.maxlen		= sizeof(u8),
 		.mode		= 0644,
-		.proc_handler	= proc_dou8vec_minmax,
+		.proc_handler	= nf_conntrack_log_invalid_sysctl,
 	},
 	[NF_SYSCTL_CT_EXPECT_MAX] = {
 		.procname	= "nf_conntrack_expect_max",
diff --git a/net/netfilter/nf_log.c b/net/netfilter/nf_log.c
index 6dd0de33eebd..c7dd5019a89d 100644
--- a/net/netfilter/nf_log.c
+++ b/net/netfilter/nf_log.c
@@ -125,6 +125,33 @@ void nf_log_unregister(struct nf_logger *logger)
 }
 EXPORT_SYMBOL(nf_log_unregister);
 
+/**
+ * nf_log_is_registered - Check if any logger is registered for a given
+ * protocol family.
+ *
+ * @pf: Protocol family
+ *
+ * Returns: true if at least one logger is active for @pf, false otherwise.
+ */
+bool nf_log_is_registered(u_int8_t pf)
+{
+	int i;
+
+	/* Out of bounds. */
+	if (pf >= NFPROTO_NUMPROTO) {
+		WARN_ON_ONCE(1);
+		return false;
+	}
+
+	for (i = 0; i < NF_LOG_TYPE_MAX; i++) {
+		if (rcu_access_pointer(loggers[pf][i]))
+			return true;
+	}
+
+	return false;
+}
+EXPORT_SYMBOL(nf_log_is_registered);
+
 int nf_log_bind_pf(struct net *net, u_int8_t pf,
 		   const struct nf_logger *logger)
 {
-- 
2.49.0


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH v2 1/1] netfilter: load nf_log_syslog on enabling nf_conntrack_log_invalid
  2025-05-26  8:59 [PATCH v2 1/1] netfilter: load nf_log_syslog on enabling nf_conntrack_log_invalid Lance Yang
@ 2025-05-28 11:05 ` Florian Westphal
  2025-05-28 11:42   ` Lance Yang
  2025-07-14 12:05   ` Lance Yang
  2025-07-24 12:24 ` Pablo Neira Ayuso
  1 sibling, 2 replies; 7+ messages in thread
From: Florian Westphal @ 2025-05-28 11:05 UTC (permalink / raw)
  To: Lance Yang
  Cc: pablo, coreteam, davem, edumazet, horms, kadlec, kuba,
	linux-kernel, netfilter-devel, pabeni, zi.li, Lance Yang

Lance Yang <ioworker0@gmail.com> wrote:
> From: Lance Yang <lance.yang@linux.dev>
> 
> When no logger is registered, nf_conntrack_log_invalid fails to log invalid
> packets, leaving users unaware of actual invalid traffic. Improve this by
> loading nf_log_syslog, similar to how 'iptables -I FORWARD 1 -m conntrack
> --ctstate INVALID -j LOG' triggers it.

Acked-by: Florian Westphal <fw@strlen.de>

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v2 1/1] netfilter: load nf_log_syslog on enabling nf_conntrack_log_invalid
  2025-05-28 11:05 ` Florian Westphal
@ 2025-05-28 11:42   ` Lance Yang
  2025-05-30  3:10     ` Lance Yang
  2025-07-14 12:05   ` Lance Yang
  1 sibling, 1 reply; 7+ messages in thread
From: Lance Yang @ 2025-05-28 11:42 UTC (permalink / raw)
  To: Florian Westphal
  Cc: pablo, coreteam, davem, Lance Yang, edumazet, horms, kadlec, kuba,
	linux-kernel, netfilter-devel, pabeni, zi.li


Thanks for taking the time to review!

On 2025/5/28 19:05, Florian Westphal wrote:
> Lance Yang <ioworker0@gmail.com> wrote:
>> From: Lance Yang <lance.yang@linux.dev>
>>
>> When no logger is registered, nf_conntrack_log_invalid fails to log invalid
>> packets, leaving users unaware of actual invalid traffic. Improve this by
>> loading nf_log_syslog, similar to how 'iptables -I FORWARD 1 -m conntrack
>> --ctstate INVALID -j LOG' triggers it.
> 
> Acked-by: Florian Westphal <fw@strlen.de>

Hmm... should this patch be backported to stable kernels? Without it,
nf_conntrack_log_invalid won't log invalid packets when no logger is
registered, causing unnecessary debugging effort ;)

Back then, I actually thought my machine wasn't seeing any invalid
packets... turns out they just weren't logged in dmesg :(

Thanks,
Lance

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v2 1/1] netfilter: load nf_log_syslog on enabling nf_conntrack_log_invalid
  2025-05-28 11:42   ` Lance Yang
@ 2025-05-30  3:10     ` Lance Yang
  0 siblings, 0 replies; 7+ messages in thread
From: Lance Yang @ 2025-05-30  3:10 UTC (permalink / raw)
  To: Florian Westphal
  Cc: pablo, coreteam, davem, Lance Yang, edumazet, horms, kadlec, kuba,
	linux-kernel, netfilter-devel, pabeni, zi.li, stable

Cc: stable

On 2025/5/28 19:42, Lance Yang wrote:
> 
> Thanks for taking the time to review!
> 
> On 2025/5/28 19:05, Florian Westphal wrote:
>> Lance Yang <ioworker0@gmail.com> wrote:
>>> From: Lance Yang <lance.yang@linux.dev>
>>>
>>> When no logger is registered, nf_conntrack_log_invalid fails to log 
>>> invalid
>>> packets, leaving users unaware of actual invalid traffic. Improve 
>>> this by
>>> loading nf_log_syslog, similar to how 'iptables -I FORWARD 1 -m 
>>> conntrack
>>> --ctstate INVALID -j LOG' triggers it.
>>
>> Acked-by: Florian Westphal <fw@strlen.de>
> 
> Hmm... should this patch be backported to stable kernels? Without it,
> nf_conntrack_log_invalid won't log invalid packets when no logger is
> registered, causing unnecessary debugging effort ;)
> 
> Back then, I actually thought my machine wasn't seeing any invalid
> packets... turns out they just weren't logged in dmesg :(
> 
> Thanks,
> Lance


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v2 1/1] netfilter: load nf_log_syslog on enabling nf_conntrack_log_invalid
  2025-05-28 11:05 ` Florian Westphal
  2025-05-28 11:42   ` Lance Yang
@ 2025-07-14 12:05   ` Lance Yang
  1 sibling, 0 replies; 7+ messages in thread
From: Lance Yang @ 2025-07-14 12:05 UTC (permalink / raw)
  To: Florian Westphal
  Cc: pablo, coreteam, davem, edumazet, horms, kadlec, kuba,
	linux-kernel, netfilter-devel, pabeni, zi.li, Lance Yang



On 2025/5/28 19:05, Florian Westphal wrote:
> Lance Yang <ioworker0@gmail.com> wrote:
>> From: Lance Yang <lance.yang@linux.dev>
>>
>> When no logger is registered, nf_conntrack_log_invalid fails to log invalid
>> packets, leaving users unaware of actual invalid traffic. Improve this by
>> loading nf_log_syslog, similar to how 'iptables -I FORWARD 1 -m conntrack
>> --ctstate INVALID -j LOG' triggers it.
> 
> Acked-by: Florian Westphal <fw@strlen.de>

A gentle reminder for this patch, as the merge window is approaching ;)
Please let me know if any changes are needed.

Thanks,
Lance


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v2 1/1] netfilter: load nf_log_syslog on enabling nf_conntrack_log_invalid
  2025-05-26  8:59 [PATCH v2 1/1] netfilter: load nf_log_syslog on enabling nf_conntrack_log_invalid Lance Yang
  2025-05-28 11:05 ` Florian Westphal
@ 2025-07-24 12:24 ` Pablo Neira Ayuso
  2025-07-24 12:48   ` Lance Yang
  1 sibling, 1 reply; 7+ messages in thread
From: Pablo Neira Ayuso @ 2025-07-24 12:24 UTC (permalink / raw)
  To: Lance Yang
  Cc: fw, coreteam, davem, edumazet, horms, kadlec, kuba, linux-kernel,
	netfilter-devel, pabeni, zi.li, Lance Yang

Hi,

On Mon, May 26, 2025 at 04:59:02PM +0800, Lance Yang wrote:
> diff --git a/net/netfilter/nf_conntrack_standalone.c b/net/netfilter/nf_conntrack_standalone.c
> index 2f666751c7e7..cdc27424f84a 100644
> --- a/net/netfilter/nf_conntrack_standalone.c
> +++ b/net/netfilter/nf_conntrack_standalone.c
> @@ -14,6 +14,7 @@
>  #include <linux/sysctl.h>
>  #endif
>  
> +#include <net/netfilter/nf_log.h>
>  #include <net/netfilter/nf_conntrack.h>
>  #include <net/netfilter/nf_conntrack_core.h>
>  #include <net/netfilter/nf_conntrack_l4proto.h>
> @@ -543,6 +544,29 @@ nf_conntrack_hash_sysctl(const struct ctl_table *table, int write,
>  	return ret;
>  }
>  
> +static int
> +nf_conntrack_log_invalid_sysctl(const struct ctl_table *table, int write,
> +				void *buffer, size_t *lenp, loff_t *ppos)
> +{
> +	int ret, i;
> +
> +	ret = proc_dou8vec_minmax(table, write, buffer, lenp, ppos);
> +	if (ret < 0 || !write)
> +		return ret;
> +
> +	if (*(u8 *)table->data == 0)
> +		return ret;

What is this table->data check for? I don't find any similar idiom
like this in the existing proc_dou8vec_minmax() callers.

> +
> +	/* Load nf_log_syslog only if no logger is currently registered */
> +	for (i = 0; i < NFPROTO_NUMPROTO; i++) {
> +		if (nf_log_is_registered(i))
> +			return ret;
> +	}
> +	request_module("%s", "nf_log_syslog");
> +
> +	return ret;
> +}
> +
>  static struct ctl_table_header *nf_ct_netfilter_header;
>  
>  enum nf_ct_sysctl_index {
> @@ -649,7 +673,7 @@ static struct ctl_table nf_ct_sysctl_table[] = {
>  		.data		= &init_net.ct.sysctl_log_invalid,
>  		.maxlen		= sizeof(u8),
>  		.mode		= 0644,
> -		.proc_handler	= proc_dou8vec_minmax,
> +		.proc_handler	= nf_conntrack_log_invalid_sysctl,
>  	},
>  	[NF_SYSCTL_CT_EXPECT_MAX] = {
>  		.procname	= "nf_conntrack_expect_max",
> diff --git a/net/netfilter/nf_log.c b/net/netfilter/nf_log.c
> index 6dd0de33eebd..c7dd5019a89d 100644
> --- a/net/netfilter/nf_log.c
> +++ b/net/netfilter/nf_log.c
> @@ -125,6 +125,33 @@ void nf_log_unregister(struct nf_logger *logger)
>  }
>  EXPORT_SYMBOL(nf_log_unregister);
>  
> +/**
> + * nf_log_is_registered - Check if any logger is registered for a given
> + * protocol family.
> + *
> + * @pf: Protocol family
> + *
> + * Returns: true if at least one logger is active for @pf, false otherwise.
> + */
> +bool nf_log_is_registered(u_int8_t pf)
> +{
> +	int i;
> +
> +	/* Out of bounds. */

No need for this comment, please remove it.

> +	if (pf >= NFPROTO_NUMPROTO) {
> +		WARN_ON_ONCE(1);
> +		return false;
> +	}

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v2 1/1] netfilter: load nf_log_syslog on enabling nf_conntrack_log_invalid
  2025-07-24 12:24 ` Pablo Neira Ayuso
@ 2025-07-24 12:48   ` Lance Yang
  0 siblings, 0 replies; 7+ messages in thread
From: Lance Yang @ 2025-07-24 12:48 UTC (permalink / raw)
  To: Pablo Neira Ayuso
  Cc: fw, coreteam, davem, edumazet, horms, kadlec, kuba, linux-kernel,
	netfilter-devel, pabeni, zi.li, Lance Yang



On 2025/7/24 20:24, Pablo Neira Ayuso wrote:
> Hi,
> 
> On Mon, May 26, 2025 at 04:59:02PM +0800, Lance Yang wrote:
>> diff --git a/net/netfilter/nf_conntrack_standalone.c b/net/netfilter/nf_conntrack_standalone.c
>> index 2f666751c7e7..cdc27424f84a 100644
>> --- a/net/netfilter/nf_conntrack_standalone.c
>> +++ b/net/netfilter/nf_conntrack_standalone.c
>> @@ -14,6 +14,7 @@
>>   #include <linux/sysctl.h>
>>   #endif
>>   
>> +#include <net/netfilter/nf_log.h>
>>   #include <net/netfilter/nf_conntrack.h>
>>   #include <net/netfilter/nf_conntrack_core.h>
>>   #include <net/netfilter/nf_conntrack_l4proto.h>
>> @@ -543,6 +544,29 @@ nf_conntrack_hash_sysctl(const struct ctl_table *table, int write,
>>   	return ret;
>>   }
>>   
>> +static int
>> +nf_conntrack_log_invalid_sysctl(const struct ctl_table *table, int write,
>> +				void *buffer, size_t *lenp, loff_t *ppos)
>> +{
>> +	int ret, i;
>> +
>> +	ret = proc_dou8vec_minmax(table, write, buffer, lenp, ppos);
>> +	if (ret < 0 || !write)
>> +		return ret;
>> +
>> +	if (*(u8 *)table->data == 0)
>> +		return ret;
> 
> What is this table->data check for? I don't find any similar idiom
> like this in the existing proc_dou8vec_minmax() callers.

My intention was to avoid the unnecessary nf_log_is_registered()
calls when a user disables the feature by writing '0' to the sysctl.

But it's not a big deal. I will remove this check in v3 ;)

>> +
>> +	/* Load nf_log_syslog only if no logger is currently registered */
>> +	for (i = 0; i < NFPROTO_NUMPROTO; i++) {
>> +		if (nf_log_is_registered(i))
>> +			return ret;
>> +	}
>> +	request_module("%s", "nf_log_syslog");
>> +
>> +	return ret;
>> +}
>> +
>>   static struct ctl_table_header *nf_ct_netfilter_header;
>>   
>>   enum nf_ct_sysctl_index {
>> @@ -649,7 +673,7 @@ static struct ctl_table nf_ct_sysctl_table[] = {
>>   		.data		= &init_net.ct.sysctl_log_invalid,
>>   		.maxlen		= sizeof(u8),
>>   		.mode		= 0644,
>> -		.proc_handler	= proc_dou8vec_minmax,
>> +		.proc_handler	= nf_conntrack_log_invalid_sysctl,
>>   	},
>>   	[NF_SYSCTL_CT_EXPECT_MAX] = {
>>   		.procname	= "nf_conntrack_expect_max",
>> diff --git a/net/netfilter/nf_log.c b/net/netfilter/nf_log.c
>> index 6dd0de33eebd..c7dd5019a89d 100644
>> --- a/net/netfilter/nf_log.c
>> +++ b/net/netfilter/nf_log.c
>> @@ -125,6 +125,33 @@ void nf_log_unregister(struct nf_logger *logger)
>>   }
>>   EXPORT_SYMBOL(nf_log_unregister);
>>   
>> +/**
>> + * nf_log_is_registered - Check if any logger is registered for a given
>> + * protocol family.
>> + *
>> + * @pf: Protocol family
>> + *
>> + * Returns: true if at least one logger is active for @pf, false otherwise.
>> + */
>> +bool nf_log_is_registered(u_int8_t pf)
>> +{
>> +	int i;
>> +
>> +	/* Out of bounds. */
> 
> No need for this comment, please remove it.

OK, will remove it.

Thanks,
Lance

> 
>> +	if (pf >= NFPROTO_NUMPROTO) {
>> +		WARN_ON_ONCE(1);
>> +		return false;
>> +	}


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2025-07-24 12:48 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-05-26  8:59 [PATCH v2 1/1] netfilter: load nf_log_syslog on enabling nf_conntrack_log_invalid Lance Yang
2025-05-28 11:05 ` Florian Westphal
2025-05-28 11:42   ` Lance Yang
2025-05-30  3:10     ` Lance Yang
2025-07-14 12:05   ` Lance Yang
2025-07-24 12:24 ` Pablo Neira Ayuso
2025-07-24 12:48   ` Lance Yang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).