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 NFLOGGER_NAME_LEN 64
18 static struct list_head nf_loggers_l[NFPROTO_NUMPROTO] __read_mostly;
19 static DEFINE_MUTEX(nf_log_mutex);
21 static struct nf_logger *__find_logger(int pf, const char *str_logger)
25 list_for_each_entry(t, &nf_loggers_l[pf], list[pf]) {
26 if (!strnicmp(str_logger, t->name, strlen(t->name)))
33 void nf_log_set(struct net *net, u_int8_t pf, const struct nf_logger *logger)
35 const struct nf_logger *log;
37 if (pf == NFPROTO_UNSPEC)
40 mutex_lock(&nf_log_mutex);
41 log = rcu_dereference_protected(net->nf.nf_loggers[pf],
42 lockdep_is_held(&nf_log_mutex));
44 rcu_assign_pointer(net->nf.nf_loggers[pf], logger);
46 mutex_unlock(&nf_log_mutex);
48 EXPORT_SYMBOL(nf_log_set);
50 void nf_log_unset(struct net *net, const struct nf_logger *logger)
53 const struct nf_logger *log;
55 mutex_lock(&nf_log_mutex);
56 for (i = 0; i < NFPROTO_NUMPROTO; i++) {
57 log = rcu_dereference_protected(net->nf.nf_loggers[i],
58 lockdep_is_held(&nf_log_mutex));
60 RCU_INIT_POINTER(net->nf.nf_loggers[i], NULL);
62 mutex_unlock(&nf_log_mutex);
65 EXPORT_SYMBOL(nf_log_unset);
67 /* return EEXIST if the same logger is registered, 0 on success. */
68 int nf_log_register(u_int8_t pf, struct nf_logger *logger)
72 if (pf >= ARRAY_SIZE(init_net.nf.nf_loggers))
75 for (i = 0; i < ARRAY_SIZE(logger->list); i++)
76 INIT_LIST_HEAD(&logger->list[i]);
78 mutex_lock(&nf_log_mutex);
80 if (pf == NFPROTO_UNSPEC) {
81 for (i = NFPROTO_UNSPEC; i < NFPROTO_NUMPROTO; i++)
82 list_add_tail(&(logger->list[i]), &(nf_loggers_l[i]));
84 /* register at end of list to honor first register win */
85 list_add_tail(&logger->list[pf], &nf_loggers_l[pf]);
88 mutex_unlock(&nf_log_mutex);
92 EXPORT_SYMBOL(nf_log_register);
94 void nf_log_unregister(struct nf_logger *logger)
98 mutex_lock(&nf_log_mutex);
99 for (i = 0; i < NFPROTO_NUMPROTO; i++)
100 list_del(&logger->list[i]);
101 mutex_unlock(&nf_log_mutex);
103 EXPORT_SYMBOL(nf_log_unregister);
105 int nf_log_bind_pf(struct net *net, u_int8_t pf,
106 const struct nf_logger *logger)
108 if (pf >= ARRAY_SIZE(net->nf.nf_loggers))
110 mutex_lock(&nf_log_mutex);
111 if (__find_logger(pf, logger->name) == NULL) {
112 mutex_unlock(&nf_log_mutex);
115 rcu_assign_pointer(net->nf.nf_loggers[pf], logger);
116 mutex_unlock(&nf_log_mutex);
119 EXPORT_SYMBOL(nf_log_bind_pf);
121 void nf_log_unbind_pf(struct net *net, u_int8_t pf)
123 if (pf >= ARRAY_SIZE(net->nf.nf_loggers))
125 mutex_lock(&nf_log_mutex);
126 RCU_INIT_POINTER(net->nf.nf_loggers[pf], NULL);
127 mutex_unlock(&nf_log_mutex);
129 EXPORT_SYMBOL(nf_log_unbind_pf);
131 void nf_log_packet(struct net *net,
133 unsigned int hooknum,
134 const struct sk_buff *skb,
135 const struct net_device *in,
136 const struct net_device *out,
137 const struct nf_loginfo *loginfo,
138 const char *fmt, ...)
141 char prefix[NF_LOG_PREFIXLEN];
142 const struct nf_logger *logger;
145 logger = rcu_dereference(net->nf.nf_loggers[pf]);
148 vsnprintf(prefix, sizeof(prefix), fmt, args);
150 logger->logfn(net, pf, hooknum, skb, in, out, loginfo, prefix);
154 EXPORT_SYMBOL(nf_log_packet);
156 #ifdef CONFIG_PROC_FS
157 static void *seq_start(struct seq_file *seq, loff_t *pos)
159 struct net *net = seq_file_net(seq);
161 mutex_lock(&nf_log_mutex);
163 if (*pos >= ARRAY_SIZE(net->nf.nf_loggers))
169 static void *seq_next(struct seq_file *s, void *v, loff_t *pos)
171 struct net *net = seq_file_net(s);
175 if (*pos >= ARRAY_SIZE(net->nf.nf_loggers))
181 static void seq_stop(struct seq_file *s, void *v)
183 mutex_unlock(&nf_log_mutex);
186 static int seq_show(struct seq_file *s, void *v)
189 const struct nf_logger *logger;
192 struct net *net = seq_file_net(s);
194 logger = rcu_dereference_protected(net->nf.nf_loggers[*pos],
195 lockdep_is_held(&nf_log_mutex));
198 ret = seq_printf(s, "%2lld NONE (", *pos);
200 ret = seq_printf(s, "%2lld %s (", *pos, logger->name);
205 list_for_each_entry(t, &nf_loggers_l[*pos], list[*pos]) {
206 ret = seq_printf(s, "%s", t->name);
209 if (&t->list[*pos] != nf_loggers_l[*pos].prev) {
210 ret = seq_printf(s, ",");
216 return seq_printf(s, ")\n");
219 static const struct seq_operations nflog_seq_ops = {
226 static int nflog_open(struct inode *inode, struct file *file)
228 return seq_open_net(inode, file, &nflog_seq_ops,
229 sizeof(struct seq_net_private));
232 static const struct file_operations nflog_file_ops = {
233 .owner = THIS_MODULE,
237 .release = seq_release_net,
244 static char nf_log_sysctl_fnames[NFPROTO_NUMPROTO-NFPROTO_UNSPEC][3];
245 static struct ctl_table nf_log_sysctl_table[NFPROTO_NUMPROTO+1];
247 static int nf_log_proc_dostring(struct ctl_table *table, int write,
248 void __user *buffer, size_t *lenp, loff_t *ppos)
250 const struct nf_logger *logger;
251 char buf[NFLOGGER_NAME_LEN];
254 int tindex = (unsigned long)table->extra1;
255 struct net *net = current->nsproxy->net_ns;
258 if (size > sizeof(buf))
260 if (copy_from_user(buf, buffer, size))
263 if (!strcmp(buf, "NONE")) {
264 nf_log_unbind_pf(net, tindex);
267 mutex_lock(&nf_log_mutex);
268 logger = __find_logger(tindex, buf);
269 if (logger == NULL) {
270 mutex_unlock(&nf_log_mutex);
273 rcu_assign_pointer(net->nf.nf_loggers[tindex], logger);
274 mutex_unlock(&nf_log_mutex);
276 struct ctl_table tmp = *table;
279 mutex_lock(&nf_log_mutex);
280 logger = rcu_dereference_protected(net->nf.nf_loggers[tindex],
281 lockdep_is_held(&nf_log_mutex));
283 strlcpy(buf, "NONE", sizeof(buf));
285 strlcpy(buf, logger->name, sizeof(buf));
286 mutex_unlock(&nf_log_mutex);
287 r = proc_dostring(&tmp, write, buffer, lenp, ppos);
293 static int netfilter_log_sysctl_init(struct net *net)
296 struct ctl_table *table;
298 table = nf_log_sysctl_table;
299 if (!net_eq(net, &init_net)) {
300 table = kmemdup(nf_log_sysctl_table,
301 sizeof(nf_log_sysctl_table),
306 for (i = NFPROTO_UNSPEC; i < NFPROTO_NUMPROTO; i++) {
307 snprintf(nf_log_sysctl_fnames[i],
309 nf_log_sysctl_table[i].procname =
310 nf_log_sysctl_fnames[i];
311 nf_log_sysctl_table[i].data = NULL;
312 nf_log_sysctl_table[i].maxlen =
313 NFLOGGER_NAME_LEN * sizeof(char);
314 nf_log_sysctl_table[i].mode = 0644;
315 nf_log_sysctl_table[i].proc_handler =
316 nf_log_proc_dostring;
317 nf_log_sysctl_table[i].extra1 =
318 (void *)(unsigned long) i;
322 net->nf.nf_log_dir_header = register_net_sysctl(net,
323 "net/netfilter/nf_log",
325 if (!net->nf.nf_log_dir_header)
331 if (!net_eq(net, &init_net))
337 static void netfilter_log_sysctl_exit(struct net *net)
339 struct ctl_table *table;
341 table = net->nf.nf_log_dir_header->ctl_table_arg;
342 unregister_net_sysctl_table(net->nf.nf_log_dir_header);
343 if (!net_eq(net, &init_net))
347 static int netfilter_log_sysctl_init(struct net *net)
352 static void netfilter_log_sysctl_exit(struct net *net)
355 #endif /* CONFIG_SYSCTL */
357 static int __net_init nf_log_net_init(struct net *net)
361 #ifdef CONFIG_PROC_FS
362 if (!proc_create("nf_log", S_IRUGO,
363 net->nf.proc_netfilter, &nflog_file_ops))
366 ret = netfilter_log_sysctl_init(net);
373 #ifdef CONFIG_PROC_FS
374 remove_proc_entry("nf_log", net->nf.proc_netfilter);
379 static void __net_exit nf_log_net_exit(struct net *net)
381 netfilter_log_sysctl_exit(net);
382 #ifdef CONFIG_PROC_FS
383 remove_proc_entry("nf_log", net->nf.proc_netfilter);
387 static struct pernet_operations nf_log_net_ops = {
388 .init = nf_log_net_init,
389 .exit = nf_log_net_exit,
392 int __init netfilter_log_init(void)
396 ret = register_pernet_subsys(&nf_log_net_ops);
400 for (i = NFPROTO_UNSPEC; i < NFPROTO_NUMPROTO; i++)
401 INIT_LIST_HEAD(&(nf_loggers_l[i]));