Skip to content

Commit fab4085

Browse files
committed
netfilter: log: nf_log_packet() as real unified interface
Before this patch, the nf_loginfo parameter specified the logging configuration in case the specified default logger was loaded. This patch updates the semantics of the nf_loginfo parameter in nf_log_packet() which now indicates the logger that you explicitly want to use. Thus, nf_log_packet() is exposed as an unified interface which internally routes the log message to the corresponding logger type by family. The module dependencies are expressed by the new nf_logger_find_get() and nf_logger_put() functions which bump the logger module refcount. Thus, you can not remove logger modules that are used by rules anymore. Another important effect of this change is that the family specific module is only loaded when required. Therefore, xt_LOG and nft_log will just trigger the autoload of the nf_log_{ip,ip6} modules according to the family. Signed-off-by: Pablo Neira Ayuso <[email protected]>
1 parent 83e96d4 commit fab4085

File tree

6 files changed

+73
-41
lines changed

6 files changed

+73
-41
lines changed

include/net/netfilter/nf_log.h

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ int nf_log_bind_pf(struct net *net, u_int8_t pf,
6161
const struct nf_logger *logger);
6262
void nf_log_unbind_pf(struct net *net, u_int8_t pf);
6363

64+
int nf_logger_find_get(int pf, enum nf_log_type type);
65+
void nf_logger_put(int pf, enum nf_log_type type);
66+
67+
#define MODULE_ALIAS_NF_LOGGER(family, type) \
68+
MODULE_ALIAS("nf-logger-" __stringify(family) "-" __stringify(type))
69+
6470
/* Calls the registered backend logging function */
6571
__printf(8, 9)
6672
void nf_log_packet(struct net *net,
@@ -78,20 +84,6 @@ struct nf_log_buf *nf_log_buf_open(void);
7884
__printf(2, 3) int nf_log_buf_add(struct nf_log_buf *m, const char *f, ...);
7985
void nf_log_buf_close(struct nf_log_buf *m);
8086

81-
void nf_log_ip_packet(struct net *net, u_int8_t pf,
82-
unsigned int hooknum, const struct sk_buff *skb,
83-
const struct net_device *in,
84-
const struct net_device *out,
85-
const struct nf_loginfo *loginfo,
86-
const char *prefix);
87-
88-
void nf_log_ip6_packet(struct net *net, u_int8_t pf,
89-
unsigned int hooknum, const struct sk_buff *skb,
90-
const struct net_device *in,
91-
const struct net_device *out,
92-
const struct nf_loginfo *loginfo,
93-
const char *prefix);
94-
9587
/* common logging functions */
9688
int nf_log_dump_udp_header(struct nf_log_buf *m, const struct sk_buff *skb,
9789
u8 proto, int fragment, unsigned int offset);

net/ipv4/netfilter/nf_log_ipv4.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -306,12 +306,12 @@ static void dump_ipv4_mac_header(struct nf_log_buf *m,
306306
nf_log_buf_add(m, " ");
307307
}
308308

309-
void nf_log_ip_packet(struct net *net, u_int8_t pf,
310-
unsigned int hooknum, const struct sk_buff *skb,
311-
const struct net_device *in,
312-
const struct net_device *out,
313-
const struct nf_loginfo *loginfo,
314-
const char *prefix)
309+
static void nf_log_ip_packet(struct net *net, u_int8_t pf,
310+
unsigned int hooknum, const struct sk_buff *skb,
311+
const struct net_device *in,
312+
const struct net_device *out,
313+
const struct nf_loginfo *loginfo,
314+
const char *prefix)
315315
{
316316
struct nf_log_buf *m;
317317

@@ -334,7 +334,6 @@ void nf_log_ip_packet(struct net *net, u_int8_t pf,
334334

335335
nf_log_buf_close(m);
336336
}
337-
EXPORT_SYMBOL_GPL(nf_log_ip_packet);
338337

339338
static struct nf_logger nf_ip_logger __read_mostly = {
340339
.name = "nf_log_ipv4",
@@ -383,3 +382,4 @@ module_exit(nf_log_ipv4_exit);
383382
MODULE_AUTHOR("Netfilter Core Team <[email protected]>");
384383
MODULE_DESCRIPTION("Netfilter IPv4 packet logging");
385384
MODULE_LICENSE("GPL");
385+
MODULE_ALIAS_NF_LOGGER(AF_INET, 0);

net/ipv6/netfilter/nf_log_ipv6.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -338,12 +338,12 @@ static void dump_ipv6_mac_header(struct nf_log_buf *m,
338338
}
339339
}
340340

341-
void nf_log_ip6_packet(struct net *net, u_int8_t pf,
342-
unsigned int hooknum, const struct sk_buff *skb,
343-
const struct net_device *in,
344-
const struct net_device *out,
345-
const struct nf_loginfo *loginfo,
346-
const char *prefix)
341+
static void nf_log_ip6_packet(struct net *net, u_int8_t pf,
342+
unsigned int hooknum, const struct sk_buff *skb,
343+
const struct net_device *in,
344+
const struct net_device *out,
345+
const struct nf_loginfo *loginfo,
346+
const char *prefix)
347347
{
348348
struct nf_log_buf *m;
349349

@@ -366,7 +366,6 @@ void nf_log_ip6_packet(struct net *net, u_int8_t pf,
366366

367367
nf_log_buf_close(m);
368368
}
369-
EXPORT_SYMBOL_GPL(nf_log_ip6_packet);
370369

371370
static struct nf_logger nf_ip6_logger __read_mostly = {
372371
.name = "nf_log_ipv6",
@@ -415,3 +414,4 @@ module_exit(nf_log_ipv6_exit);
415414
MODULE_AUTHOR("Netfilter Core Team <[email protected]>");
416415
MODULE_DESCRIPTION("Netfilter IPv4 packet logging");
417416
MODULE_LICENSE("GPL");
417+
MODULE_ALIAS_NF_LOGGER(AF_INET6, 0);

net/netfilter/nf_log.c

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,41 @@ void nf_log_unbind_pf(struct net *net, u_int8_t pf)
132132
}
133133
EXPORT_SYMBOL(nf_log_unbind_pf);
134134

135+
int nf_logger_find_get(int pf, enum nf_log_type type)
136+
{
137+
struct nf_logger *logger;
138+
int ret = -ENOENT;
139+
140+
logger = loggers[pf][type];
141+
if (logger == NULL)
142+
request_module("nf-logger-%u-%u", pf, type);
143+
144+
rcu_read_lock();
145+
logger = rcu_dereference(loggers[pf][type]);
146+
if (logger == NULL)
147+
goto out;
148+
149+
if (logger && try_module_get(logger->me))
150+
ret = 0;
151+
out:
152+
rcu_read_unlock();
153+
return ret;
154+
}
155+
EXPORT_SYMBOL_GPL(nf_logger_find_get);
156+
157+
void nf_logger_put(int pf, enum nf_log_type type)
158+
{
159+
struct nf_logger *logger;
160+
161+
BUG_ON(loggers[pf][type] == NULL);
162+
163+
rcu_read_lock();
164+
logger = rcu_dereference(loggers[pf][type]);
165+
module_put(logger->me);
166+
rcu_read_unlock();
167+
}
168+
EXPORT_SYMBOL_GPL(nf_logger_put);
169+
135170
void nf_log_packet(struct net *net,
136171
u_int8_t pf,
137172
unsigned int hooknum,
@@ -146,7 +181,11 @@ void nf_log_packet(struct net *net,
146181
const struct nf_logger *logger;
147182

148183
rcu_read_lock();
149-
logger = rcu_dereference(net->nf.nf_loggers[pf]);
184+
if (loginfo != NULL)
185+
logger = rcu_dereference(loggers[pf][loginfo->type]);
186+
else
187+
logger = rcu_dereference(net->nf.nf_loggers[pf]);
188+
150189
if (logger) {
151190
va_start(args, fmt);
152191
vsnprintf(prefix, sizeof(prefix), fmt, args);

net/netfilter/nfnetlink_log.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1106,6 +1106,9 @@ MODULE_DESCRIPTION("netfilter userspace logging");
11061106
MODULE_AUTHOR("Harald Welte <[email protected]>");
11071107
MODULE_LICENSE("GPL");
11081108
MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_ULOG);
1109+
MODULE_ALIAS_NF_LOGGER(AF_INET, 1);
1110+
MODULE_ALIAS_NF_LOGGER(AF_INET6, 1);
1111+
MODULE_ALIAS_NF_LOGGER(AF_BRIDGE, 1);
11091112

11101113
module_init(nfnetlink_log_init);
11111114
module_exit(nfnetlink_log_fini);

net/netfilter/xt_LOG.c

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,8 @@ log_tg(struct sk_buff *skb, const struct xt_action_param *par)
3939
li.u.log.level = loginfo->level;
4040
li.u.log.logflags = loginfo->logflags;
4141

42-
if (par->family == NFPROTO_IPV4)
43-
nf_log_ip_packet(net, NFPROTO_IPV4, par->hooknum, skb, par->in,
44-
par->out, &li, loginfo->prefix);
45-
#if IS_ENABLED(CONFIG_IP6_NF_IPTABLES)
46-
else if (par->family == NFPROTO_IPV6)
47-
nf_log_ip6_packet(net, NFPROTO_IPV6, par->hooknum, skb, par->in,
48-
par->out, &li, loginfo->prefix);
49-
#endif
50-
else
51-
WARN_ON_ONCE(1);
52-
42+
nf_log_packet(net, par->family, par->hooknum, skb, par->in, par->out,
43+
&li, loginfo->prefix);
5344
return XT_CONTINUE;
5445
}
5546

@@ -70,7 +61,12 @@ static int log_tg_check(const struct xt_tgchk_param *par)
7061
return -EINVAL;
7162
}
7263

73-
return 0;
64+
return nf_logger_find_get(par->family, NF_LOG_TYPE_LOG);
65+
}
66+
67+
static void log_tg_destroy(const struct xt_tgdtor_param *par)
68+
{
69+
nf_logger_put(par->family, NF_LOG_TYPE_LOG);
7470
}
7571

7672
static struct xt_target log_tg_regs[] __read_mostly = {
@@ -80,6 +76,7 @@ static struct xt_target log_tg_regs[] __read_mostly = {
8076
.target = log_tg,
8177
.targetsize = sizeof(struct xt_log_info),
8278
.checkentry = log_tg_check,
79+
.destroy = log_tg_destroy,
8380
.me = THIS_MODULE,
8481
},
8582
#if IS_ENABLED(CONFIG_IP6_NF_IPTABLES)
@@ -89,6 +86,7 @@ static struct xt_target log_tg_regs[] __read_mostly = {
8986
.target = log_tg,
9087
.targetsize = sizeof(struct xt_log_info),
9188
.checkentry = log_tg_check,
89+
.destroy = log_tg_destroy,
9290
.me = THIS_MODULE,
9391
},
9492
#endif

0 commit comments

Comments
 (0)