Skip to content

Commit e89a680

Browse files
ioworker0ummakynes
authored andcommitted
netfilter: load nf_log_syslog on enabling nf_conntrack_log_invalid
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> Acked-by: Florian Westphal <fw@strlen.de> Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
1 parent aa58401 commit e89a680

File tree

3 files changed

+54
-1
lines changed

3 files changed

+54
-1
lines changed

include/net/netfilter/nf_log.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ extern int sysctl_nf_log_all_netns;
5959
int nf_log_register(u_int8_t pf, struct nf_logger *logger);
6060
void nf_log_unregister(struct nf_logger *logger);
6161

62+
/* Check if any logger is registered for a given protocol family. */
63+
bool nf_log_is_registered(u_int8_t pf);
64+
6265
int nf_log_set(struct net *net, u_int8_t pf, const struct nf_logger *logger);
6366
void nf_log_unset(struct net *net, const struct nf_logger *logger);
6467

net/netfilter/nf_conntrack_standalone.c

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <linux/sysctl.h>
1515
#endif
1616

17+
#include <net/netfilter/nf_log.h>
1718
#include <net/netfilter/nf_conntrack.h>
1819
#include <net/netfilter/nf_conntrack_core.h>
1920
#include <net/netfilter/nf_conntrack_l4proto.h>
@@ -555,6 +556,29 @@ nf_conntrack_hash_sysctl(const struct ctl_table *table, int write,
555556
return ret;
556557
}
557558

559+
static int
560+
nf_conntrack_log_invalid_sysctl(const struct ctl_table *table, int write,
561+
void *buffer, size_t *lenp, loff_t *ppos)
562+
{
563+
int ret, i;
564+
565+
ret = proc_dou8vec_minmax(table, write, buffer, lenp, ppos);
566+
if (ret < 0 || !write)
567+
return ret;
568+
569+
if (*(u8 *)table->data == 0)
570+
return ret;
571+
572+
/* Load nf_log_syslog only if no logger is currently registered */
573+
for (i = 0; i < NFPROTO_NUMPROTO; i++) {
574+
if (nf_log_is_registered(i))
575+
return ret;
576+
}
577+
request_module("%s", "nf_log_syslog");
578+
579+
return ret;
580+
}
581+
558582
static struct ctl_table_header *nf_ct_netfilter_header;
559583

560584
enum nf_ct_sysctl_index {
@@ -651,7 +675,7 @@ static struct ctl_table nf_ct_sysctl_table[] = {
651675
.data = &init_net.ct.sysctl_log_invalid,
652676
.maxlen = sizeof(u8),
653677
.mode = 0644,
654-
.proc_handler = proc_dou8vec_minmax,
678+
.proc_handler = nf_conntrack_log_invalid_sysctl,
655679
},
656680
[NF_SYSCTL_CT_EXPECT_MAX] = {
657681
.procname = "nf_conntrack_expect_max",

net/netfilter/nf_log.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,32 @@ void nf_log_unregister(struct nf_logger *logger)
125125
}
126126
EXPORT_SYMBOL(nf_log_unregister);
127127

128+
/**
129+
* nf_log_is_registered - Check if any logger is registered for a given
130+
* protocol family.
131+
*
132+
* @pf: Protocol family
133+
*
134+
* Returns: true if at least one logger is active for @pf, false otherwise.
135+
*/
136+
bool nf_log_is_registered(u_int8_t pf)
137+
{
138+
int i;
139+
140+
if (pf >= NFPROTO_NUMPROTO) {
141+
WARN_ON_ONCE(1);
142+
return false;
143+
}
144+
145+
for (i = 0; i < NF_LOG_TYPE_MAX; i++) {
146+
if (rcu_access_pointer(loggers[pf][i]))
147+
return true;
148+
}
149+
150+
return false;
151+
}
152+
EXPORT_SYMBOL(nf_log_is_registered);
153+
128154
int nf_log_bind_pf(struct net *net, u_int8_t pf,
129155
const struct nf_logger *logger)
130156
{

0 commit comments

Comments
 (0)