1 #include <linux/kernel.h>
2 #include <linux/init.h>
3 #include <linux/module.h>
4 #include <linux/proc_fs.h>
5 #include <linux/skbuff.h>
6 #include <linux/netfilter.h>
7 #include <linux/seq_file.h>
8 #include <net/protocol.h>
9 #include <net/netfilter/nf_log.h>
11 #include "nf_internals.h"
13 /* Internal logging interface, which relies on the real
16 #define NF_LOG_PREFIXLEN 128
17 #define NFLOGGER_NAME_LEN 64
19 static struct nf_logger __rcu *loggers[NFPROTO_NUMPROTO][NF_LOG_TYPE_MAX] __read_mostly;
20 static DEFINE_MUTEX(nf_log_mutex);
22 static struct nf_logger *__find_logger(int pf, const char *str_logger)
24 struct nf_logger *log;
27 for (i = 0; i < NF_LOG_TYPE_MAX; i++) {
28 if (loggers[pf][i] == NULL)
31 log = rcu_dereference_protected(loggers[pf][i],
32 lockdep_is_held(&nf_log_mutex));
33 if (!strncasecmp(str_logger, log->name, strlen(log->name)))
40 void nf_log_set(struct net *net, u_int8_t pf, const struct nf_logger *logger)
42 const struct nf_logger *log;
44 if (pf == NFPROTO_UNSPEC)
47 mutex_lock(&nf_log_mutex);
48 log = rcu_dereference_protected(net->nf.nf_loggers[pf],
49 lockdep_is_held(&nf_log_mutex));
51 rcu_assign_pointer(net->nf.nf_loggers[pf], logger);
53 mutex_unlock(&nf_log_mutex);
55 EXPORT_SYMBOL(nf_log_set);
57 void nf_log_unset(struct net *net, const struct nf_logger *logger)
60 const struct nf_logger *log;
62 mutex_lock(&nf_log_mutex);
63 for (i = 0; i < NFPROTO_NUMPROTO; i++) {
64 log = rcu_dereference_protected(net->nf.nf_loggers[i],
65 lockdep_is_held(&nf_log_mutex));
67 RCU_INIT_POINTER(net->nf.nf_loggers[i], NULL);
69 mutex_unlock(&nf_log_mutex);
72 EXPORT_SYMBOL(nf_log_unset);
74 /* return EEXIST if the same logger is registered, 0 on success. */
75 int nf_log_register(u_int8_t pf, struct nf_logger *logger)
79 if (pf >= ARRAY_SIZE(init_net.nf.nf_loggers))
82 mutex_lock(&nf_log_mutex);
84 if (pf == NFPROTO_UNSPEC) {
85 for (i = NFPROTO_UNSPEC; i < NFPROTO_NUMPROTO; i++)
86 rcu_assign_pointer(loggers[i][logger->type], logger);
88 /* register at end of list to honor first register win */
89 rcu_assign_pointer(loggers[pf][logger->type], logger);
92 mutex_unlock(&nf_log_mutex);
96 EXPORT_SYMBOL(nf_log_register);
98 void nf_log_unregister(struct nf_logger *logger)
102 mutex_lock(&nf_log_mutex);
103 for (i = 0; i < NFPROTO_NUMPROTO; i++)
104 RCU_INIT_POINTER(loggers[i][logger->type], NULL);
105 mutex_unlock(&nf_log_mutex);
107 EXPORT_SYMBOL(nf_log_unregister);
109 int nf_log_bind_pf(struct net *net, u_int8_t pf,
110 const struct nf_logger *logger)
112 if (pf >= ARRAY_SIZE(net->nf.nf_loggers))
114 mutex_lock(&nf_log_mutex);
115 if (__find_logger(pf, logger->name) == NULL) {
116 mutex_unlock(&nf_log_mutex);
119 rcu_assign_pointer(net->nf.nf_loggers[pf], logger);
120 mutex_unlock(&nf_log_mutex);
123 EXPORT_SYMBOL(nf_log_bind_pf);
125 void nf_log_unbind_pf(struct net *net, u_int8_t pf)
127 if (pf >= ARRAY_SIZE(net->nf.nf_loggers))
129 mutex_lock(&nf_log_mutex);
130 RCU_INIT_POINTER(net->nf.nf_loggers[pf], NULL);
131 mutex_unlock(&nf_log_mutex);
133 EXPORT_SYMBOL(nf_log_unbind_pf);
135 void nf_logger_request_module(int pf, enum nf_log_type type)
137 if (loggers[pf][type] == NULL)
138 request_module("nf-logger-%u-%u", pf, type);
140 EXPORT_SYMBOL_GPL(nf_logger_request_module);
142 int nf_logger_find_get(int pf, enum nf_log_type type)
144 struct nf_logger *logger;
147 logger = loggers[pf][type];
149 request_module("nf-logger-%u-%u", pf, type);
152 logger = rcu_dereference(loggers[pf][type]);
156 if (logger && try_module_get(logger->me))
162 EXPORT_SYMBOL_GPL(nf_logger_find_get);
164 void nf_logger_put(int pf, enum nf_log_type type)
166 struct nf_logger *logger;
168 BUG_ON(loggers[pf][type] == NULL);
171 logger = rcu_dereference(loggers[pf][type]);
172 module_put(logger->me);
175 EXPORT_SYMBOL_GPL(nf_logger_put);
177 void nf_log_packet(struct net *net,
179 unsigned int hooknum,
180 const struct sk_buff *skb,
181 const struct net_device *in,
182 const struct net_device *out,
183 const struct nf_loginfo *loginfo,
184 const char *fmt, ...)
187 char prefix[NF_LOG_PREFIXLEN];
188 const struct nf_logger *logger;
192 logger = rcu_dereference(loggers[pf][loginfo->type]);
194 logger = rcu_dereference(net->nf.nf_loggers[pf]);
198 vsnprintf(prefix, sizeof(prefix), fmt, args);
200 logger->logfn(net, pf, hooknum, skb, in, out, loginfo, prefix);
204 EXPORT_SYMBOL(nf_log_packet);
206 #define S_SIZE (1024 - (sizeof(unsigned int) + 1))
210 char buf[S_SIZE + 1];
212 static struct nf_log_buf emergency, *emergency_ptr = &emergency;
214 __printf(2, 3) int nf_log_buf_add(struct nf_log_buf *m, const char *f, ...)
219 if (likely(m->count < S_SIZE)) {
221 len = vsnprintf(m->buf + m->count, S_SIZE - m->count, f, args);
223 if (likely(m->count + len < S_SIZE)) {
229 printk_once(KERN_ERR KBUILD_MODNAME " please increase S_SIZE\n");
232 EXPORT_SYMBOL_GPL(nf_log_buf_add);
234 struct nf_log_buf *nf_log_buf_open(void)
236 struct nf_log_buf *m = kmalloc(sizeof(*m), GFP_ATOMIC);
241 m = xchg(&emergency_ptr, NULL);
247 EXPORT_SYMBOL_GPL(nf_log_buf_open);
249 void nf_log_buf_close(struct nf_log_buf *m)
251 m->buf[m->count] = 0;
252 printk("%s\n", m->buf);
254 if (likely(m != &emergency))
261 EXPORT_SYMBOL_GPL(nf_log_buf_close);
263 #ifdef CONFIG_PROC_FS
264 static void *seq_start(struct seq_file *seq, loff_t *pos)
266 struct net *net = seq_file_net(seq);
268 mutex_lock(&nf_log_mutex);
270 if (*pos >= ARRAY_SIZE(net->nf.nf_loggers))
276 static void *seq_next(struct seq_file *s, void *v, loff_t *pos)
278 struct net *net = seq_file_net(s);
282 if (*pos >= ARRAY_SIZE(net->nf.nf_loggers))
288 static void seq_stop(struct seq_file *s, void *v)
290 mutex_unlock(&nf_log_mutex);
293 static int seq_show(struct seq_file *s, void *v)
296 const struct nf_logger *logger;
298 struct net *net = seq_file_net(s);
300 logger = rcu_dereference_protected(net->nf.nf_loggers[*pos],
301 lockdep_is_held(&nf_log_mutex));
304 seq_printf(s, "%2lld NONE (", *pos);
306 seq_printf(s, "%2lld %s (", *pos, logger->name);
308 if (seq_has_overflowed(s))
311 for (i = 0; i < NF_LOG_TYPE_MAX; i++) {
312 if (loggers[*pos][i] == NULL)
315 logger = rcu_dereference_protected(loggers[*pos][i],
316 lockdep_is_held(&nf_log_mutex));
317 seq_printf(s, "%s", logger->name);
318 if (i == 0 && loggers[*pos][i + 1] != NULL)
321 if (seq_has_overflowed(s))
325 seq_printf(s, ")\n");
327 if (seq_has_overflowed(s))
332 static const struct seq_operations nflog_seq_ops = {
339 static int nflog_open(struct inode *inode, struct file *file)
341 return seq_open_net(inode, file, &nflog_seq_ops,
342 sizeof(struct seq_net_private));
345 static const struct file_operations nflog_file_ops = {
346 .owner = THIS_MODULE,
350 .release = seq_release_net,
357 static char nf_log_sysctl_fnames[NFPROTO_NUMPROTO-NFPROTO_UNSPEC][3];
358 static struct ctl_table nf_log_sysctl_table[NFPROTO_NUMPROTO+1];
360 static int nf_log_proc_dostring(struct ctl_table *table, int write,
361 void __user *buffer, size_t *lenp, loff_t *ppos)
363 const struct nf_logger *logger;
364 char buf[NFLOGGER_NAME_LEN];
367 int tindex = (unsigned long)table->extra1;
368 struct net *net = current->nsproxy->net_ns;
371 if (size > sizeof(buf))
373 if (copy_from_user(buf, buffer, size))
376 if (!strcmp(buf, "NONE")) {
377 nf_log_unbind_pf(net, tindex);
380 mutex_lock(&nf_log_mutex);
381 logger = __find_logger(tindex, buf);
382 if (logger == NULL) {
383 mutex_unlock(&nf_log_mutex);
386 rcu_assign_pointer(net->nf.nf_loggers[tindex], logger);
387 mutex_unlock(&nf_log_mutex);
389 mutex_lock(&nf_log_mutex);
390 logger = rcu_dereference_protected(net->nf.nf_loggers[tindex],
391 lockdep_is_held(&nf_log_mutex));
393 table->data = "NONE";
395 table->data = logger->name;
396 r = proc_dostring(table, write, buffer, lenp, ppos);
397 mutex_unlock(&nf_log_mutex);
403 static int netfilter_log_sysctl_init(struct net *net)
406 struct ctl_table *table;
408 table = nf_log_sysctl_table;
409 if (!net_eq(net, &init_net)) {
410 table = kmemdup(nf_log_sysctl_table,
411 sizeof(nf_log_sysctl_table),
416 for (i = NFPROTO_UNSPEC; i < NFPROTO_NUMPROTO; i++) {
417 snprintf(nf_log_sysctl_fnames[i],
419 nf_log_sysctl_table[i].procname =
420 nf_log_sysctl_fnames[i];
421 nf_log_sysctl_table[i].data = NULL;
422 nf_log_sysctl_table[i].maxlen =
423 NFLOGGER_NAME_LEN * sizeof(char);
424 nf_log_sysctl_table[i].mode = 0644;
425 nf_log_sysctl_table[i].proc_handler =
426 nf_log_proc_dostring;
427 nf_log_sysctl_table[i].extra1 =
428 (void *)(unsigned long) i;
432 net->nf.nf_log_dir_header = register_net_sysctl(net,
433 "net/netfilter/nf_log",
435 if (!net->nf.nf_log_dir_header)
441 if (!net_eq(net, &init_net))
447 static void netfilter_log_sysctl_exit(struct net *net)
449 struct ctl_table *table;
451 table = net->nf.nf_log_dir_header->ctl_table_arg;
452 unregister_net_sysctl_table(net->nf.nf_log_dir_header);
453 if (!net_eq(net, &init_net))
457 static int netfilter_log_sysctl_init(struct net *net)
462 static void netfilter_log_sysctl_exit(struct net *net)
465 #endif /* CONFIG_SYSCTL */
467 static int __net_init nf_log_net_init(struct net *net)
471 #ifdef CONFIG_PROC_FS
472 if (!proc_create("nf_log", S_IRUGO,
473 net->nf.proc_netfilter, &nflog_file_ops))
476 ret = netfilter_log_sysctl_init(net);
483 #ifdef CONFIG_PROC_FS
484 remove_proc_entry("nf_log", net->nf.proc_netfilter);
489 static void __net_exit nf_log_net_exit(struct net *net)
491 netfilter_log_sysctl_exit(net);
492 #ifdef CONFIG_PROC_FS
493 remove_proc_entry("nf_log", net->nf.proc_netfilter);
497 static struct pernet_operations nf_log_net_ops = {
498 .init = nf_log_net_init,
499 .exit = nf_log_net_exit,
502 int __init netfilter_log_init(void)
504 return register_pernet_subsys(&nf_log_net_ops);