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 list_head nf_loggers_l[NFPROTO_NUMPROTO] __read_mostly;
20 static DEFINE_MUTEX(nf_log_mutex);
22 static struct nf_logger *__find_logger(int pf, const char *str_logger)
26 list_for_each_entry(t, &nf_loggers_l[pf], list[pf]) {
27 if (!strnicmp(str_logger, t->name, strlen(t->name)))
34 void nf_log_set(struct net *net, u_int8_t pf, const struct nf_logger *logger)
36 const struct nf_logger *log;
38 if (!net_eq(net, &init_net))
41 if (pf == NFPROTO_UNSPEC)
44 mutex_lock(&nf_log_mutex);
45 log = rcu_dereference_protected(net->nf.nf_loggers[pf],
46 lockdep_is_held(&nf_log_mutex));
48 rcu_assign_pointer(net->nf.nf_loggers[pf], logger);
50 mutex_unlock(&nf_log_mutex);
52 EXPORT_SYMBOL(nf_log_set);
54 void nf_log_unset(struct net *net, const struct nf_logger *logger)
57 const struct nf_logger *log;
59 if (!net_eq(net, &init_net))
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 for (i = 0; i < ARRAY_SIZE(logger->list); i++)
83 INIT_LIST_HEAD(&logger->list[i]);
85 mutex_lock(&nf_log_mutex);
87 if (pf == NFPROTO_UNSPEC) {
88 for (i = NFPROTO_UNSPEC; i < NFPROTO_NUMPROTO; i++)
89 list_add_tail(&(logger->list[i]), &(nf_loggers_l[i]));
91 /* register at end of list to honor first register win */
92 list_add_tail(&logger->list[pf], &nf_loggers_l[pf]);
95 mutex_unlock(&nf_log_mutex);
97 nf_log_set(&init_net, pf, logger);
100 EXPORT_SYMBOL(nf_log_register);
102 void nf_log_unregister(struct nf_logger *logger)
106 mutex_lock(&nf_log_mutex);
107 for (i = 0; i < NFPROTO_NUMPROTO; i++)
108 list_del(&logger->list[i]);
109 mutex_unlock(&nf_log_mutex);
111 nf_log_unset(&init_net, logger);
113 EXPORT_SYMBOL(nf_log_unregister);
115 int nf_log_bind_pf(struct net *net, u_int8_t pf,
116 const struct nf_logger *logger)
118 if (!net_eq(net, &init_net))
121 if (pf >= ARRAY_SIZE(net->nf.nf_loggers))
123 mutex_lock(&nf_log_mutex);
124 if (__find_logger(pf, logger->name) == NULL) {
125 mutex_unlock(&nf_log_mutex);
128 rcu_assign_pointer(net->nf.nf_loggers[pf], logger);
129 mutex_unlock(&nf_log_mutex);
132 EXPORT_SYMBOL(nf_log_bind_pf);
134 void nf_log_unbind_pf(struct net *net, u_int8_t pf)
136 if (!net_eq(net, &init_net))
139 if (pf >= ARRAY_SIZE(net->nf.nf_loggers))
141 mutex_lock(&nf_log_mutex);
142 RCU_INIT_POINTER(net->nf.nf_loggers[pf], NULL);
143 mutex_unlock(&nf_log_mutex);
145 EXPORT_SYMBOL(nf_log_unbind_pf);
147 void nf_log_packet(struct net *net,
149 unsigned int hooknum,
150 const struct sk_buff *skb,
151 const struct net_device *in,
152 const struct net_device *out,
153 const struct nf_loginfo *loginfo,
154 const char *fmt, ...)
157 char prefix[NF_LOG_PREFIXLEN];
158 const struct nf_logger *logger;
160 if (!net_eq(net, &init_net))
164 logger = rcu_dereference(net->nf.nf_loggers[pf]);
167 vsnprintf(prefix, sizeof(prefix), fmt, args);
169 logger->logfn(pf, hooknum, skb, in, out, loginfo, prefix);
173 EXPORT_SYMBOL(nf_log_packet);
175 #ifdef CONFIG_PROC_FS
176 static void *seq_start(struct seq_file *seq, loff_t *pos)
178 struct net *net = seq_file_net(seq);
180 mutex_lock(&nf_log_mutex);
182 if (*pos >= ARRAY_SIZE(net->nf.nf_loggers))
188 static void *seq_next(struct seq_file *s, void *v, loff_t *pos)
190 struct net *net = seq_file_net(s);
194 if (*pos >= ARRAY_SIZE(net->nf.nf_loggers))
200 static void seq_stop(struct seq_file *s, void *v)
202 mutex_unlock(&nf_log_mutex);
205 static int seq_show(struct seq_file *s, void *v)
208 const struct nf_logger *logger;
211 struct net *net = seq_file_net(s);
213 logger = rcu_dereference_protected(net->nf.nf_loggers[*pos],
214 lockdep_is_held(&nf_log_mutex));
217 ret = seq_printf(s, "%2lld NONE (", *pos);
219 ret = seq_printf(s, "%2lld %s (", *pos, logger->name);
224 list_for_each_entry(t, &nf_loggers_l[*pos], list[*pos]) {
225 ret = seq_printf(s, "%s", t->name);
228 if (&t->list[*pos] != nf_loggers_l[*pos].prev) {
229 ret = seq_printf(s, ",");
235 return seq_printf(s, ")\n");
238 static const struct seq_operations nflog_seq_ops = {
245 static int nflog_open(struct inode *inode, struct file *file)
247 return seq_open_net(inode, file, &nflog_seq_ops,
248 sizeof(struct seq_net_private));
251 static const struct file_operations nflog_file_ops = {
252 .owner = THIS_MODULE,
256 .release = seq_release_net,
263 static char nf_log_sysctl_fnames[NFPROTO_NUMPROTO-NFPROTO_UNSPEC][3];
264 static struct ctl_table nf_log_sysctl_table[NFPROTO_NUMPROTO+1];
266 static int nf_log_proc_dostring(ctl_table *table, int write,
267 void __user *buffer, size_t *lenp, loff_t *ppos)
269 const struct nf_logger *logger;
270 char buf[NFLOGGER_NAME_LEN];
273 int tindex = (unsigned long)table->extra1;
274 struct net *net = current->nsproxy->net_ns;
277 if (!net_eq(net, &init_net))
280 if (size > sizeof(buf))
282 if (copy_from_user(buf, buffer, size))
285 if (!strcmp(buf, "NONE")) {
286 nf_log_unbind_pf(net, tindex);
289 mutex_lock(&nf_log_mutex);
290 logger = __find_logger(tindex, buf);
291 if (logger == NULL) {
292 mutex_unlock(&nf_log_mutex);
295 rcu_assign_pointer(net->nf.nf_loggers[tindex], logger);
296 mutex_unlock(&nf_log_mutex);
298 mutex_lock(&nf_log_mutex);
299 logger = rcu_dereference_protected(net->nf.nf_loggers[tindex],
300 lockdep_is_held(&nf_log_mutex));
302 table->data = "NONE";
304 table->data = logger->name;
305 r = proc_dostring(table, write, buffer, lenp, ppos);
306 mutex_unlock(&nf_log_mutex);
312 static int netfilter_log_sysctl_init(struct net *net)
315 struct ctl_table *table;
317 table = nf_log_sysctl_table;
318 if (!net_eq(net, &init_net)) {
319 table = kmemdup(nf_log_sysctl_table,
320 sizeof(nf_log_sysctl_table),
325 for (i = NFPROTO_UNSPEC; i < NFPROTO_NUMPROTO; i++) {
326 snprintf(nf_log_sysctl_fnames[i],
328 nf_log_sysctl_table[i].procname =
329 nf_log_sysctl_fnames[i];
330 nf_log_sysctl_table[i].data = NULL;
331 nf_log_sysctl_table[i].maxlen =
332 NFLOGGER_NAME_LEN * sizeof(char);
333 nf_log_sysctl_table[i].mode = 0644;
334 nf_log_sysctl_table[i].proc_handler =
335 nf_log_proc_dostring;
336 nf_log_sysctl_table[i].extra1 =
337 (void *)(unsigned long) i;
341 net->nf.nf_log_dir_header = register_net_sysctl(net,
342 "net/netfilter/nf_log",
344 if (!net->nf.nf_log_dir_header)
350 if (!net_eq(net, &init_net))
356 static void netfilter_log_sysctl_exit(struct net *net)
358 struct ctl_table *table;
360 table = net->nf.nf_log_dir_header->ctl_table_arg;
361 unregister_net_sysctl_table(net->nf.nf_log_dir_header);
362 if (!net_eq(net, &init_net))
366 static int netfilter_log_sysctl_init(struct net *net)
371 static void netfilter_log_sysctl_exit(struct net *net)
374 #endif /* CONFIG_SYSCTL */
376 static int __net_init nf_log_net_init(struct net *net)
380 #ifdef CONFIG_PROC_FS
381 if (!proc_create("nf_log", S_IRUGO,
382 net->nf.proc_netfilter, &nflog_file_ops))
385 ret = netfilter_log_sysctl_init(net);
392 /* For init_net: errors will trigger panic, don't unroll on error. */
393 if (!net_eq(net, &init_net))
394 remove_proc_entry("nf_log", net->nf.proc_netfilter);
399 static void __net_exit nf_log_net_exit(struct net *net)
401 netfilter_log_sysctl_exit(net);
402 remove_proc_entry("nf_log", net->nf.proc_netfilter);
405 static struct pernet_operations nf_log_net_ops = {
406 .init = nf_log_net_init,
407 .exit = nf_log_net_exit,
410 int __init netfilter_log_init(void)
414 ret = register_pernet_subsys(&nf_log_net_ops);
418 for (i = NFPROTO_UNSPEC; i < NFPROTO_NUMPROTO; i++)
419 INIT_LIST_HEAD(&(nf_loggers_l[i]));