ARM: dts: add 'dr_mode' property to hsotg devices for exynos boards
[linux/fpc-iii.git] / net / netfilter / nf_log.c
blob43c926cae9c00de1306144abcddcf64f510c2864
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
14 LOG target modules */
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 #define nft_log_dereference(logger) \
23 rcu_dereference_protected(logger, lockdep_is_held(&nf_log_mutex))
25 static struct nf_logger *__find_logger(int pf, const char *str_logger)
27 struct nf_logger *log;
28 int i;
30 for (i = 0; i < NF_LOG_TYPE_MAX; i++) {
31 if (loggers[pf][i] == NULL)
32 continue;
34 log = nft_log_dereference(loggers[pf][i]);
35 if (!strncasecmp(str_logger, log->name, strlen(log->name)))
36 return log;
39 return NULL;
42 void nf_log_set(struct net *net, u_int8_t pf, const struct nf_logger *logger)
44 const struct nf_logger *log;
46 if (pf == NFPROTO_UNSPEC)
47 return;
49 mutex_lock(&nf_log_mutex);
50 log = nft_log_dereference(net->nf.nf_loggers[pf]);
51 if (log == NULL)
52 rcu_assign_pointer(net->nf.nf_loggers[pf], logger);
54 mutex_unlock(&nf_log_mutex);
56 EXPORT_SYMBOL(nf_log_set);
58 void nf_log_unset(struct net *net, const struct nf_logger *logger)
60 int i;
61 const struct nf_logger *log;
63 mutex_lock(&nf_log_mutex);
64 for (i = 0; i < NFPROTO_NUMPROTO; i++) {
65 log = nft_log_dereference(net->nf.nf_loggers[i]);
66 if (log == logger)
67 RCU_INIT_POINTER(net->nf.nf_loggers[i], NULL);
69 mutex_unlock(&nf_log_mutex);
70 synchronize_rcu();
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)
77 int i;
78 int ret = 0;
80 if (pf >= ARRAY_SIZE(init_net.nf.nf_loggers))
81 return -EINVAL;
83 mutex_lock(&nf_log_mutex);
85 if (pf == NFPROTO_UNSPEC) {
86 for (i = NFPROTO_UNSPEC; i < NFPROTO_NUMPROTO; i++) {
87 if (rcu_access_pointer(loggers[i][logger->type])) {
88 ret = -EEXIST;
89 goto unlock;
92 for (i = NFPROTO_UNSPEC; i < NFPROTO_NUMPROTO; i++)
93 rcu_assign_pointer(loggers[i][logger->type], logger);
94 } else {
95 if (rcu_access_pointer(loggers[pf][logger->type])) {
96 ret = -EEXIST;
97 goto unlock;
99 rcu_assign_pointer(loggers[pf][logger->type], logger);
102 unlock:
103 mutex_unlock(&nf_log_mutex);
104 return ret;
106 EXPORT_SYMBOL(nf_log_register);
108 void nf_log_unregister(struct nf_logger *logger)
110 int i;
112 mutex_lock(&nf_log_mutex);
113 for (i = 0; i < NFPROTO_NUMPROTO; i++)
114 RCU_INIT_POINTER(loggers[i][logger->type], NULL);
115 mutex_unlock(&nf_log_mutex);
117 EXPORT_SYMBOL(nf_log_unregister);
119 int nf_log_bind_pf(struct net *net, u_int8_t pf,
120 const struct nf_logger *logger)
122 if (pf >= ARRAY_SIZE(net->nf.nf_loggers))
123 return -EINVAL;
124 mutex_lock(&nf_log_mutex);
125 if (__find_logger(pf, logger->name) == NULL) {
126 mutex_unlock(&nf_log_mutex);
127 return -ENOENT;
129 rcu_assign_pointer(net->nf.nf_loggers[pf], logger);
130 mutex_unlock(&nf_log_mutex);
131 return 0;
133 EXPORT_SYMBOL(nf_log_bind_pf);
135 void nf_log_unbind_pf(struct net *net, u_int8_t pf)
137 if (pf >= ARRAY_SIZE(net->nf.nf_loggers))
138 return;
139 mutex_lock(&nf_log_mutex);
140 RCU_INIT_POINTER(net->nf.nf_loggers[pf], NULL);
141 mutex_unlock(&nf_log_mutex);
143 EXPORT_SYMBOL(nf_log_unbind_pf);
145 void nf_logger_request_module(int pf, enum nf_log_type type)
147 if (loggers[pf][type] == NULL)
148 request_module("nf-logger-%u-%u", pf, type);
150 EXPORT_SYMBOL_GPL(nf_logger_request_module);
152 int nf_logger_find_get(int pf, enum nf_log_type type)
154 struct nf_logger *logger;
155 int ret = -ENOENT;
157 if (rcu_access_pointer(loggers[pf][type]) == NULL)
158 request_module("nf-logger-%u-%u", pf, type);
160 rcu_read_lock();
161 logger = rcu_dereference(loggers[pf][type]);
162 if (logger == NULL)
163 goto out;
165 if (logger && try_module_get(logger->me))
166 ret = 0;
167 out:
168 rcu_read_unlock();
169 return ret;
171 EXPORT_SYMBOL_GPL(nf_logger_find_get);
173 void nf_logger_put(int pf, enum nf_log_type type)
175 struct nf_logger *logger;
177 BUG_ON(loggers[pf][type] == NULL);
179 rcu_read_lock();
180 logger = rcu_dereference(loggers[pf][type]);
181 module_put(logger->me);
182 rcu_read_unlock();
184 EXPORT_SYMBOL_GPL(nf_logger_put);
186 void nf_log_packet(struct net *net,
187 u_int8_t pf,
188 unsigned int hooknum,
189 const struct sk_buff *skb,
190 const struct net_device *in,
191 const struct net_device *out,
192 const struct nf_loginfo *loginfo,
193 const char *fmt, ...)
195 va_list args;
196 char prefix[NF_LOG_PREFIXLEN];
197 const struct nf_logger *logger;
199 rcu_read_lock();
200 if (loginfo != NULL)
201 logger = rcu_dereference(loggers[pf][loginfo->type]);
202 else
203 logger = rcu_dereference(net->nf.nf_loggers[pf]);
205 if (logger) {
206 va_start(args, fmt);
207 vsnprintf(prefix, sizeof(prefix), fmt, args);
208 va_end(args);
209 logger->logfn(net, pf, hooknum, skb, in, out, loginfo, prefix);
211 rcu_read_unlock();
213 EXPORT_SYMBOL(nf_log_packet);
215 #define S_SIZE (1024 - (sizeof(unsigned int) + 1))
217 struct nf_log_buf {
218 unsigned int count;
219 char buf[S_SIZE + 1];
221 static struct nf_log_buf emergency, *emergency_ptr = &emergency;
223 __printf(2, 3) int nf_log_buf_add(struct nf_log_buf *m, const char *f, ...)
225 va_list args;
226 int len;
228 if (likely(m->count < S_SIZE)) {
229 va_start(args, f);
230 len = vsnprintf(m->buf + m->count, S_SIZE - m->count, f, args);
231 va_end(args);
232 if (likely(m->count + len < S_SIZE)) {
233 m->count += len;
234 return 0;
237 m->count = S_SIZE;
238 printk_once(KERN_ERR KBUILD_MODNAME " please increase S_SIZE\n");
239 return -1;
241 EXPORT_SYMBOL_GPL(nf_log_buf_add);
243 struct nf_log_buf *nf_log_buf_open(void)
245 struct nf_log_buf *m = kmalloc(sizeof(*m), GFP_ATOMIC);
247 if (unlikely(!m)) {
248 local_bh_disable();
249 do {
250 m = xchg(&emergency_ptr, NULL);
251 } while (!m);
253 m->count = 0;
254 return m;
256 EXPORT_SYMBOL_GPL(nf_log_buf_open);
258 void nf_log_buf_close(struct nf_log_buf *m)
260 m->buf[m->count] = 0;
261 printk("%s\n", m->buf);
263 if (likely(m != &emergency))
264 kfree(m);
265 else {
266 emergency_ptr = m;
267 local_bh_enable();
270 EXPORT_SYMBOL_GPL(nf_log_buf_close);
272 #ifdef CONFIG_PROC_FS
273 static void *seq_start(struct seq_file *seq, loff_t *pos)
275 struct net *net = seq_file_net(seq);
277 mutex_lock(&nf_log_mutex);
279 if (*pos >= ARRAY_SIZE(net->nf.nf_loggers))
280 return NULL;
282 return pos;
285 static void *seq_next(struct seq_file *s, void *v, loff_t *pos)
287 struct net *net = seq_file_net(s);
289 (*pos)++;
291 if (*pos >= ARRAY_SIZE(net->nf.nf_loggers))
292 return NULL;
294 return pos;
297 static void seq_stop(struct seq_file *s, void *v)
299 mutex_unlock(&nf_log_mutex);
302 static int seq_show(struct seq_file *s, void *v)
304 loff_t *pos = v;
305 const struct nf_logger *logger;
306 int i;
307 struct net *net = seq_file_net(s);
309 logger = nft_log_dereference(net->nf.nf_loggers[*pos]);
311 if (!logger)
312 seq_printf(s, "%2lld NONE (", *pos);
313 else
314 seq_printf(s, "%2lld %s (", *pos, logger->name);
316 if (seq_has_overflowed(s))
317 return -ENOSPC;
319 for (i = 0; i < NF_LOG_TYPE_MAX; i++) {
320 if (loggers[*pos][i] == NULL)
321 continue;
323 logger = nft_log_dereference(loggers[*pos][i]);
324 seq_printf(s, "%s", logger->name);
325 if (i == 0 && loggers[*pos][i + 1] != NULL)
326 seq_printf(s, ",");
328 if (seq_has_overflowed(s))
329 return -ENOSPC;
332 seq_printf(s, ")\n");
334 if (seq_has_overflowed(s))
335 return -ENOSPC;
336 return 0;
339 static const struct seq_operations nflog_seq_ops = {
340 .start = seq_start,
341 .next = seq_next,
342 .stop = seq_stop,
343 .show = seq_show,
346 static int nflog_open(struct inode *inode, struct file *file)
348 return seq_open_net(inode, file, &nflog_seq_ops,
349 sizeof(struct seq_net_private));
352 static const struct file_operations nflog_file_ops = {
353 .owner = THIS_MODULE,
354 .open = nflog_open,
355 .read = seq_read,
356 .llseek = seq_lseek,
357 .release = seq_release_net,
361 #endif /* PROC_FS */
363 #ifdef CONFIG_SYSCTL
364 static char nf_log_sysctl_fnames[NFPROTO_NUMPROTO-NFPROTO_UNSPEC][3];
365 static struct ctl_table nf_log_sysctl_table[NFPROTO_NUMPROTO+1];
367 static int nf_log_proc_dostring(struct ctl_table *table, int write,
368 void __user *buffer, size_t *lenp, loff_t *ppos)
370 const struct nf_logger *logger;
371 char buf[NFLOGGER_NAME_LEN];
372 size_t size = *lenp;
373 int r = 0;
374 int tindex = (unsigned long)table->extra1;
375 struct net *net = current->nsproxy->net_ns;
377 if (write) {
378 if (size > sizeof(buf))
379 size = sizeof(buf);
380 if (copy_from_user(buf, buffer, size))
381 return -EFAULT;
383 if (!strcmp(buf, "NONE")) {
384 nf_log_unbind_pf(net, tindex);
385 return 0;
387 mutex_lock(&nf_log_mutex);
388 logger = __find_logger(tindex, buf);
389 if (logger == NULL) {
390 mutex_unlock(&nf_log_mutex);
391 return -ENOENT;
393 rcu_assign_pointer(net->nf.nf_loggers[tindex], logger);
394 mutex_unlock(&nf_log_mutex);
395 } else {
396 mutex_lock(&nf_log_mutex);
397 logger = nft_log_dereference(net->nf.nf_loggers[tindex]);
398 if (!logger)
399 table->data = "NONE";
400 else
401 table->data = logger->name;
402 r = proc_dostring(table, write, buffer, lenp, ppos);
403 mutex_unlock(&nf_log_mutex);
406 return r;
409 static int netfilter_log_sysctl_init(struct net *net)
411 int i;
412 struct ctl_table *table;
414 table = nf_log_sysctl_table;
415 if (!net_eq(net, &init_net)) {
416 table = kmemdup(nf_log_sysctl_table,
417 sizeof(nf_log_sysctl_table),
418 GFP_KERNEL);
419 if (!table)
420 goto err_alloc;
421 } else {
422 for (i = NFPROTO_UNSPEC; i < NFPROTO_NUMPROTO; i++) {
423 snprintf(nf_log_sysctl_fnames[i],
424 3, "%d", i);
425 nf_log_sysctl_table[i].procname =
426 nf_log_sysctl_fnames[i];
427 nf_log_sysctl_table[i].data = NULL;
428 nf_log_sysctl_table[i].maxlen =
429 NFLOGGER_NAME_LEN * sizeof(char);
430 nf_log_sysctl_table[i].mode = 0644;
431 nf_log_sysctl_table[i].proc_handler =
432 nf_log_proc_dostring;
433 nf_log_sysctl_table[i].extra1 =
434 (void *)(unsigned long) i;
438 net->nf.nf_log_dir_header = register_net_sysctl(net,
439 "net/netfilter/nf_log",
440 table);
441 if (!net->nf.nf_log_dir_header)
442 goto err_reg;
444 return 0;
446 err_reg:
447 if (!net_eq(net, &init_net))
448 kfree(table);
449 err_alloc:
450 return -ENOMEM;
453 static void netfilter_log_sysctl_exit(struct net *net)
455 struct ctl_table *table;
457 table = net->nf.nf_log_dir_header->ctl_table_arg;
458 unregister_net_sysctl_table(net->nf.nf_log_dir_header);
459 if (!net_eq(net, &init_net))
460 kfree(table);
462 #else
463 static int netfilter_log_sysctl_init(struct net *net)
465 return 0;
468 static void netfilter_log_sysctl_exit(struct net *net)
471 #endif /* CONFIG_SYSCTL */
473 static int __net_init nf_log_net_init(struct net *net)
475 int ret = -ENOMEM;
477 #ifdef CONFIG_PROC_FS
478 if (!proc_create("nf_log", S_IRUGO,
479 net->nf.proc_netfilter, &nflog_file_ops))
480 return ret;
481 #endif
482 ret = netfilter_log_sysctl_init(net);
483 if (ret < 0)
484 goto out_sysctl;
486 return 0;
488 out_sysctl:
489 #ifdef CONFIG_PROC_FS
490 remove_proc_entry("nf_log", net->nf.proc_netfilter);
491 #endif
492 return ret;
495 static void __net_exit nf_log_net_exit(struct net *net)
497 netfilter_log_sysctl_exit(net);
498 #ifdef CONFIG_PROC_FS
499 remove_proc_entry("nf_log", net->nf.proc_netfilter);
500 #endif
503 static struct pernet_operations nf_log_net_ops = {
504 .init = nf_log_net_init,
505 .exit = nf_log_net_exit,
508 int __init netfilter_log_init(void)
510 return register_pernet_subsys(&nf_log_net_ops);