Skip to content

Commit 5962815

Browse files
committed
netfilter: nf_log: use an array of loggers instead of list
Now that legacy ulog targets are not available anymore in the tree, we can have up to two possible loggers: 1) The plain text logging via kernel logging ring. 2) The nfnetlink_log infrastructure which delivers log messages to userspace. This patch replaces the list of loggers by an array of two pointers per family for each possible logger and it also introduces a new field to the nf_logger structure which indicates the position in the logger array (based on the logger type). This prepares a follow up patch that consolidates the nf_log_packet() interface by allowing to specify the logger as parameter. Signed-off-by: Pablo Neira Ayuso <[email protected]>
1 parent 7200135 commit 5962815

File tree

5 files changed

+37
-32
lines changed

5 files changed

+37
-32
lines changed

include/net/netfilter/nf_log.h

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,11 @@
1212
#define NF_LOG_UID 0x08 /* Log UID owning local socket */
1313
#define NF_LOG_MASK 0x0f
1414

15-
#define NF_LOG_TYPE_LOG 0x01
16-
#define NF_LOG_TYPE_ULOG 0x02
15+
enum nf_log_type {
16+
NF_LOG_TYPE_LOG = 0,
17+
NF_LOG_TYPE_ULOG,
18+
NF_LOG_TYPE_MAX
19+
};
1720

1821
struct nf_loginfo {
1922
u_int8_t type;
@@ -40,10 +43,10 @@ typedef void nf_logfn(struct net *net,
4043
const char *prefix);
4144

4245
struct nf_logger {
43-
struct module *me;
44-
nf_logfn *logfn;
45-
char *name;
46-
struct list_head list[NFPROTO_NUMPROTO];
46+
char *name;
47+
enum nf_log_type type;
48+
nf_logfn *logfn;
49+
struct module *me;
4750
};
4851

4952
/* Function to register/unregister log function. */

net/bridge/netfilter/ebt_log.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ static struct xt_target ebt_log_tg_reg __read_mostly = {
207207

208208
static struct nf_logger ebt_log_logger __read_mostly = {
209209
.name = "ebt_log",
210+
.type = NF_LOG_TYPE_LOG,
210211
.logfn = &ebt_log_packet,
211212
.me = THIS_MODULE,
212213
};

net/netfilter/nf_log.c

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,22 @@
1616
#define NF_LOG_PREFIXLEN 128
1717
#define NFLOGGER_NAME_LEN 64
1818

19-
static struct list_head nf_loggers_l[NFPROTO_NUMPROTO] __read_mostly;
19+
static struct nf_logger __rcu *loggers[NFPROTO_NUMPROTO][NF_LOG_TYPE_MAX] __read_mostly;
2020
static DEFINE_MUTEX(nf_log_mutex);
2121

2222
static struct nf_logger *__find_logger(int pf, const char *str_logger)
2323
{
24-
struct nf_logger *t;
24+
struct nf_logger *log;
25+
int i;
26+
27+
for (i = 0; i < NF_LOG_TYPE_MAX; i++) {
28+
if (loggers[pf][i] == NULL)
29+
continue;
2530

26-
list_for_each_entry(t, &nf_loggers_l[pf], list[pf]) {
27-
if (!strnicmp(str_logger, t->name, strlen(t->name)))
28-
return t;
31+
log = rcu_dereference_protected(loggers[pf][i],
32+
lockdep_is_held(&nf_log_mutex));
33+
if (!strnicmp(str_logger, log->name, strlen(log->name)))
34+
return log;
2935
}
3036

3137
return NULL;
@@ -73,17 +79,14 @@ int nf_log_register(u_int8_t pf, struct nf_logger *logger)
7379
if (pf >= ARRAY_SIZE(init_net.nf.nf_loggers))
7480
return -EINVAL;
7581

76-
for (i = 0; i < ARRAY_SIZE(logger->list); i++)
77-
INIT_LIST_HEAD(&logger->list[i]);
78-
7982
mutex_lock(&nf_log_mutex);
8083

8184
if (pf == NFPROTO_UNSPEC) {
8285
for (i = NFPROTO_UNSPEC; i < NFPROTO_NUMPROTO; i++)
83-
list_add_tail(&(logger->list[i]), &(nf_loggers_l[i]));
86+
rcu_assign_pointer(loggers[i][logger->type], logger);
8487
} else {
8588
/* register at end of list to honor first register win */
86-
list_add_tail(&logger->list[pf], &nf_loggers_l[pf]);
89+
rcu_assign_pointer(loggers[pf][logger->type], logger);
8790
}
8891

8992
mutex_unlock(&nf_log_mutex);
@@ -98,7 +101,7 @@ void nf_log_unregister(struct nf_logger *logger)
98101

99102
mutex_lock(&nf_log_mutex);
100103
for (i = 0; i < NFPROTO_NUMPROTO; i++)
101-
list_del(&logger->list[i]);
104+
RCU_INIT_POINTER(loggers[i][logger->type], NULL);
102105
mutex_unlock(&nf_log_mutex);
103106
}
104107
EXPORT_SYMBOL(nf_log_unregister);
@@ -188,8 +191,7 @@ static int seq_show(struct seq_file *s, void *v)
188191
{
189192
loff_t *pos = v;
190193
const struct nf_logger *logger;
191-
struct nf_logger *t;
192-
int ret;
194+
int i, ret;
193195
struct net *net = seq_file_net(s);
194196

195197
logger = rcu_dereference_protected(net->nf.nf_loggers[*pos],
@@ -203,11 +205,16 @@ static int seq_show(struct seq_file *s, void *v)
203205
if (ret < 0)
204206
return ret;
205207

206-
list_for_each_entry(t, &nf_loggers_l[*pos], list[*pos]) {
207-
ret = seq_printf(s, "%s", t->name);
208+
for (i = 0; i < NF_LOG_TYPE_MAX; i++) {
209+
if (loggers[*pos][i] == NULL)
210+
continue;
211+
212+
logger = rcu_dereference_protected(loggers[*pos][i],
213+
lockdep_is_held(&nf_log_mutex));
214+
ret = seq_printf(s, "%s", logger->name);
208215
if (ret < 0)
209216
return ret;
210-
if (&t->list[*pos] != nf_loggers_l[*pos].prev) {
217+
if (i == 0 && loggers[*pos][i + 1] != NULL) {
211218
ret = seq_printf(s, ",");
212219
if (ret < 0)
213220
return ret;
@@ -389,14 +396,5 @@ static struct pernet_operations nf_log_net_ops = {
389396

390397
int __init netfilter_log_init(void)
391398
{
392-
int i, ret;
393-
394-
ret = register_pernet_subsys(&nf_log_net_ops);
395-
if (ret < 0)
396-
return ret;
397-
398-
for (i = NFPROTO_UNSPEC; i < NFPROTO_NUMPROTO; i++)
399-
INIT_LIST_HEAD(&(nf_loggers_l[i]));
400-
401-
return 0;
399+
return register_pernet_subsys(&nf_log_net_ops);
402400
}

net/netfilter/nfnetlink_log.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -773,6 +773,7 @@ nfulnl_recv_unsupp(struct sock *ctnl, struct sk_buff *skb,
773773

774774
static struct nf_logger nfulnl_logger __read_mostly = {
775775
.name = "nfnetlink_log",
776+
.type = NF_LOG_TYPE_ULOG,
776777
.logfn = &nfulnl_log_packet,
777778
.me = THIS_MODULE,
778779
};

net/netfilter/xt_LOG.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -896,13 +896,15 @@ static struct xt_target log_tg_regs[] __read_mostly = {
896896

897897
static struct nf_logger ipt_log_logger __read_mostly = {
898898
.name = "ipt_LOG",
899+
.type = NF_LOG_TYPE_LOG,
899900
.logfn = &ipt_log_packet,
900901
.me = THIS_MODULE,
901902
};
902903

903904
#if IS_ENABLED(CONFIG_IP6_NF_IPTABLES)
904905
static struct nf_logger ip6t_log_logger __read_mostly = {
905906
.name = "ip6t_LOG",
907+
.type = NF_LOG_TYPE_LOG,
906908
.logfn = &ip6t_log_packet,
907909
.me = THIS_MODULE,
908910
};

0 commit comments

Comments
 (0)