Commit 5962815a authored by Pablo Neira Ayuso's avatar Pablo Neira Ayuso

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: default avatarPablo Neira Ayuso <pablo@netfilter.org>
parent 7200135b
...@@ -12,8 +12,11 @@ ...@@ -12,8 +12,11 @@
#define NF_LOG_UID 0x08 /* Log UID owning local socket */ #define NF_LOG_UID 0x08 /* Log UID owning local socket */
#define NF_LOG_MASK 0x0f #define NF_LOG_MASK 0x0f
#define NF_LOG_TYPE_LOG 0x01 enum nf_log_type {
#define NF_LOG_TYPE_ULOG 0x02 NF_LOG_TYPE_LOG = 0,
NF_LOG_TYPE_ULOG,
NF_LOG_TYPE_MAX
};
struct nf_loginfo { struct nf_loginfo {
u_int8_t type; u_int8_t type;
...@@ -40,10 +43,10 @@ typedef void nf_logfn(struct net *net, ...@@ -40,10 +43,10 @@ typedef void nf_logfn(struct net *net,
const char *prefix); const char *prefix);
struct nf_logger { struct nf_logger {
struct module *me;
nf_logfn *logfn;
char *name; char *name;
struct list_head list[NFPROTO_NUMPROTO]; enum nf_log_type type;
nf_logfn *logfn;
struct module *me;
}; };
/* Function to register/unregister log function. */ /* Function to register/unregister log function. */
......
...@@ -207,6 +207,7 @@ static struct xt_target ebt_log_tg_reg __read_mostly = { ...@@ -207,6 +207,7 @@ static struct xt_target ebt_log_tg_reg __read_mostly = {
static struct nf_logger ebt_log_logger __read_mostly = { static struct nf_logger ebt_log_logger __read_mostly = {
.name = "ebt_log", .name = "ebt_log",
.type = NF_LOG_TYPE_LOG,
.logfn = &ebt_log_packet, .logfn = &ebt_log_packet,
.me = THIS_MODULE, .me = THIS_MODULE,
}; };
......
...@@ -16,16 +16,22 @@ ...@@ -16,16 +16,22 @@
#define NF_LOG_PREFIXLEN 128 #define NF_LOG_PREFIXLEN 128
#define NFLOGGER_NAME_LEN 64 #define NFLOGGER_NAME_LEN 64
static struct list_head nf_loggers_l[NFPROTO_NUMPROTO] __read_mostly; static struct nf_logger __rcu *loggers[NFPROTO_NUMPROTO][NF_LOG_TYPE_MAX] __read_mostly;
static DEFINE_MUTEX(nf_log_mutex); static DEFINE_MUTEX(nf_log_mutex);
static struct nf_logger *__find_logger(int pf, const char *str_logger) static struct nf_logger *__find_logger(int pf, const char *str_logger)
{ {
struct nf_logger *t; struct nf_logger *log;
int i;
for (i = 0; i < NF_LOG_TYPE_MAX; i++) {
if (loggers[pf][i] == NULL)
continue;
list_for_each_entry(t, &nf_loggers_l[pf], list[pf]) { log = rcu_dereference_protected(loggers[pf][i],
if (!strnicmp(str_logger, t->name, strlen(t->name))) lockdep_is_held(&nf_log_mutex));
return t; if (!strnicmp(str_logger, log->name, strlen(log->name)))
return log;
} }
return NULL; return NULL;
...@@ -73,17 +79,14 @@ int nf_log_register(u_int8_t pf, struct nf_logger *logger) ...@@ -73,17 +79,14 @@ int nf_log_register(u_int8_t pf, struct nf_logger *logger)
if (pf >= ARRAY_SIZE(init_net.nf.nf_loggers)) if (pf >= ARRAY_SIZE(init_net.nf.nf_loggers))
return -EINVAL; return -EINVAL;
for (i = 0; i < ARRAY_SIZE(logger->list); i++)
INIT_LIST_HEAD(&logger->list[i]);
mutex_lock(&nf_log_mutex); mutex_lock(&nf_log_mutex);
if (pf == NFPROTO_UNSPEC) { if (pf == NFPROTO_UNSPEC) {
for (i = NFPROTO_UNSPEC; i < NFPROTO_NUMPROTO; i++) for (i = NFPROTO_UNSPEC; i < NFPROTO_NUMPROTO; i++)
list_add_tail(&(logger->list[i]), &(nf_loggers_l[i])); rcu_assign_pointer(loggers[i][logger->type], logger);
} else { } else {
/* register at end of list to honor first register win */ /* register at end of list to honor first register win */
list_add_tail(&logger->list[pf], &nf_loggers_l[pf]); rcu_assign_pointer(loggers[pf][logger->type], logger);
} }
mutex_unlock(&nf_log_mutex); mutex_unlock(&nf_log_mutex);
...@@ -98,7 +101,7 @@ void nf_log_unregister(struct nf_logger *logger) ...@@ -98,7 +101,7 @@ void nf_log_unregister(struct nf_logger *logger)
mutex_lock(&nf_log_mutex); mutex_lock(&nf_log_mutex);
for (i = 0; i < NFPROTO_NUMPROTO; i++) for (i = 0; i < NFPROTO_NUMPROTO; i++)
list_del(&logger->list[i]); RCU_INIT_POINTER(loggers[i][logger->type], NULL);
mutex_unlock(&nf_log_mutex); mutex_unlock(&nf_log_mutex);
} }
EXPORT_SYMBOL(nf_log_unregister); EXPORT_SYMBOL(nf_log_unregister);
...@@ -188,8 +191,7 @@ static int seq_show(struct seq_file *s, void *v) ...@@ -188,8 +191,7 @@ static int seq_show(struct seq_file *s, void *v)
{ {
loff_t *pos = v; loff_t *pos = v;
const struct nf_logger *logger; const struct nf_logger *logger;
struct nf_logger *t; int i, ret;
int ret;
struct net *net = seq_file_net(s); struct net *net = seq_file_net(s);
logger = rcu_dereference_protected(net->nf.nf_loggers[*pos], logger = rcu_dereference_protected(net->nf.nf_loggers[*pos],
...@@ -203,11 +205,16 @@ static int seq_show(struct seq_file *s, void *v) ...@@ -203,11 +205,16 @@ static int seq_show(struct seq_file *s, void *v)
if (ret < 0) if (ret < 0)
return ret; return ret;
list_for_each_entry(t, &nf_loggers_l[*pos], list[*pos]) { for (i = 0; i < NF_LOG_TYPE_MAX; i++) {
ret = seq_printf(s, "%s", t->name); if (loggers[*pos][i] == NULL)
continue;
logger = rcu_dereference_protected(loggers[*pos][i],
lockdep_is_held(&nf_log_mutex));
ret = seq_printf(s, "%s", logger->name);
if (ret < 0) if (ret < 0)
return ret; return ret;
if (&t->list[*pos] != nf_loggers_l[*pos].prev) { if (i == 0 && loggers[*pos][i + 1] != NULL) {
ret = seq_printf(s, ","); ret = seq_printf(s, ",");
if (ret < 0) if (ret < 0)
return ret; return ret;
...@@ -389,14 +396,5 @@ static struct pernet_operations nf_log_net_ops = { ...@@ -389,14 +396,5 @@ static struct pernet_operations nf_log_net_ops = {
int __init netfilter_log_init(void) int __init netfilter_log_init(void)
{ {
int i, ret; return register_pernet_subsys(&nf_log_net_ops);
ret = register_pernet_subsys(&nf_log_net_ops);
if (ret < 0)
return ret;
for (i = NFPROTO_UNSPEC; i < NFPROTO_NUMPROTO; i++)
INIT_LIST_HEAD(&(nf_loggers_l[i]));
return 0;
} }
...@@ -773,6 +773,7 @@ nfulnl_recv_unsupp(struct sock *ctnl, struct sk_buff *skb, ...@@ -773,6 +773,7 @@ nfulnl_recv_unsupp(struct sock *ctnl, struct sk_buff *skb,
static struct nf_logger nfulnl_logger __read_mostly = { static struct nf_logger nfulnl_logger __read_mostly = {
.name = "nfnetlink_log", .name = "nfnetlink_log",
.type = NF_LOG_TYPE_ULOG,
.logfn = &nfulnl_log_packet, .logfn = &nfulnl_log_packet,
.me = THIS_MODULE, .me = THIS_MODULE,
}; };
......
...@@ -896,6 +896,7 @@ static struct xt_target log_tg_regs[] __read_mostly = { ...@@ -896,6 +896,7 @@ static struct xt_target log_tg_regs[] __read_mostly = {
static struct nf_logger ipt_log_logger __read_mostly = { static struct nf_logger ipt_log_logger __read_mostly = {
.name = "ipt_LOG", .name = "ipt_LOG",
.type = NF_LOG_TYPE_LOG,
.logfn = &ipt_log_packet, .logfn = &ipt_log_packet,
.me = THIS_MODULE, .me = THIS_MODULE,
}; };
...@@ -903,6 +904,7 @@ static struct nf_logger ipt_log_logger __read_mostly = { ...@@ -903,6 +904,7 @@ static struct nf_logger ipt_log_logger __read_mostly = {
#if IS_ENABLED(CONFIG_IP6_NF_IPTABLES) #if IS_ENABLED(CONFIG_IP6_NF_IPTABLES)
static struct nf_logger ip6t_log_logger __read_mostly = { static struct nf_logger ip6t_log_logger __read_mostly = {
.name = "ip6t_LOG", .name = "ip6t_LOG",
.type = NF_LOG_TYPE_LOG,
.logfn = &ip6t_log_packet, .logfn = &ip6t_log_packet,
.me = THIS_MODULE, .me = THIS_MODULE,
}; };
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment