ALSA: hda - Adding one more ALC255 pin definition for headset problem
[linux/fpc-iii.git] / net / netfilter / nf_log.c
blobaa5847a16713e9caddb74127dfd73dd50ee57b83
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 const struct nf_logger *log;
111 int i;
113 mutex_lock(&nf_log_mutex);
114 for (i = 0; i < NFPROTO_NUMPROTO; i++) {
115 log = nft_log_dereference(loggers[i][logger->type]);
116 if (log == logger)
117 RCU_INIT_POINTER(loggers[i][logger->type], NULL);
119 mutex_unlock(&nf_log_mutex);
120 synchronize_rcu();
122 EXPORT_SYMBOL(nf_log_unregister);
124 int nf_log_bind_pf(struct net *net, u_int8_t pf,
125 const struct nf_logger *logger)
127 if (pf >= ARRAY_SIZE(net->nf.nf_loggers))
128 return -EINVAL;
129 mutex_lock(&nf_log_mutex);
130 if (__find_logger(pf, logger->name) == NULL) {
131 mutex_unlock(&nf_log_mutex);
132 return -ENOENT;
134 rcu_assign_pointer(net->nf.nf_loggers[pf], logger);
135 mutex_unlock(&nf_log_mutex);
136 return 0;
138 EXPORT_SYMBOL(nf_log_bind_pf);
140 void nf_log_unbind_pf(struct net *net, u_int8_t pf)
142 if (pf >= ARRAY_SIZE(net->nf.nf_loggers))
143 return;
144 mutex_lock(&nf_log_mutex);
145 RCU_INIT_POINTER(net->nf.nf_loggers[pf], NULL);
146 mutex_unlock(&nf_log_mutex);
148 EXPORT_SYMBOL(nf_log_unbind_pf);
150 void nf_logger_request_module(int pf, enum nf_log_type type)
152 if (loggers[pf][type] == NULL)
153 request_module("nf-logger-%u-%u", pf, type);
155 EXPORT_SYMBOL_GPL(nf_logger_request_module);
157 int nf_logger_find_get(int pf, enum nf_log_type type)
159 struct nf_logger *logger;
160 int ret = -ENOENT;
162 if (pf == NFPROTO_INET) {
163 ret = nf_logger_find_get(NFPROTO_IPV4, type);
164 if (ret < 0)
165 return ret;
167 ret = nf_logger_find_get(NFPROTO_IPV6, type);
168 if (ret < 0) {
169 nf_logger_put(NFPROTO_IPV4, type);
170 return ret;
173 return 0;
176 if (rcu_access_pointer(loggers[pf][type]) == NULL)
177 request_module("nf-logger-%u-%u", pf, type);
179 rcu_read_lock();
180 logger = rcu_dereference(loggers[pf][type]);
181 if (logger == NULL)
182 goto out;
184 if (try_module_get(logger->me))
185 ret = 0;
186 out:
187 rcu_read_unlock();
188 return ret;
190 EXPORT_SYMBOL_GPL(nf_logger_find_get);
192 void nf_logger_put(int pf, enum nf_log_type type)
194 struct nf_logger *logger;
196 if (pf == NFPROTO_INET) {
197 nf_logger_put(NFPROTO_IPV4, type);
198 nf_logger_put(NFPROTO_IPV6, type);
199 return;
202 BUG_ON(loggers[pf][type] == NULL);
204 rcu_read_lock();
205 logger = rcu_dereference(loggers[pf][type]);
206 module_put(logger->me);
207 rcu_read_unlock();
209 EXPORT_SYMBOL_GPL(nf_logger_put);
211 void nf_log_packet(struct net *net,
212 u_int8_t pf,
213 unsigned int hooknum,
214 const struct sk_buff *skb,
215 const struct net_device *in,
216 const struct net_device *out,
217 const struct nf_loginfo *loginfo,
218 const char *fmt, ...)
220 va_list args;
221 char prefix[NF_LOG_PREFIXLEN];
222 const struct nf_logger *logger;
224 rcu_read_lock();
225 if (loginfo != NULL)
226 logger = rcu_dereference(loggers[pf][loginfo->type]);
227 else
228 logger = rcu_dereference(net->nf.nf_loggers[pf]);
230 if (logger) {
231 va_start(args, fmt);
232 vsnprintf(prefix, sizeof(prefix), fmt, args);
233 va_end(args);
234 logger->logfn(net, pf, hooknum, skb, in, out, loginfo, prefix);
236 rcu_read_unlock();
238 EXPORT_SYMBOL(nf_log_packet);
240 void nf_log_trace(struct net *net,
241 u_int8_t pf,
242 unsigned int hooknum,
243 const struct sk_buff *skb,
244 const struct net_device *in,
245 const struct net_device *out,
246 const struct nf_loginfo *loginfo, const char *fmt, ...)
248 va_list args;
249 char prefix[NF_LOG_PREFIXLEN];
250 const struct nf_logger *logger;
252 rcu_read_lock();
253 logger = rcu_dereference(net->nf.nf_loggers[pf]);
254 if (logger) {
255 va_start(args, fmt);
256 vsnprintf(prefix, sizeof(prefix), fmt, args);
257 va_end(args);
258 logger->logfn(net, pf, hooknum, skb, in, out, loginfo, prefix);
260 rcu_read_unlock();
262 EXPORT_SYMBOL(nf_log_trace);
264 #define S_SIZE (1024 - (sizeof(unsigned int) + 1))
266 struct nf_log_buf {
267 unsigned int count;
268 char buf[S_SIZE + 1];
270 static struct nf_log_buf emergency, *emergency_ptr = &emergency;
272 __printf(2, 3) int nf_log_buf_add(struct nf_log_buf *m, const char *f, ...)
274 va_list args;
275 int len;
277 if (likely(m->count < S_SIZE)) {
278 va_start(args, f);
279 len = vsnprintf(m->buf + m->count, S_SIZE - m->count, f, args);
280 va_end(args);
281 if (likely(m->count + len < S_SIZE)) {
282 m->count += len;
283 return 0;
286 m->count = S_SIZE;
287 printk_once(KERN_ERR KBUILD_MODNAME " please increase S_SIZE\n");
288 return -1;
290 EXPORT_SYMBOL_GPL(nf_log_buf_add);
292 struct nf_log_buf *nf_log_buf_open(void)
294 struct nf_log_buf *m = kmalloc(sizeof(*m), GFP_ATOMIC);
296 if (unlikely(!m)) {
297 local_bh_disable();
298 do {
299 m = xchg(&emergency_ptr, NULL);
300 } while (!m);
302 m->count = 0;
303 return m;
305 EXPORT_SYMBOL_GPL(nf_log_buf_open);
307 void nf_log_buf_close(struct nf_log_buf *m)
309 m->buf[m->count] = 0;
310 printk("%s\n", m->buf);
312 if (likely(m != &emergency))
313 kfree(m);
314 else {
315 emergency_ptr = m;
316 local_bh_enable();
319 EXPORT_SYMBOL_GPL(nf_log_buf_close);
321 #ifdef CONFIG_PROC_FS
322 static void *seq_start(struct seq_file *seq, loff_t *pos)
324 struct net *net = seq_file_net(seq);
326 mutex_lock(&nf_log_mutex);
328 if (*pos >= ARRAY_SIZE(net->nf.nf_loggers))
329 return NULL;
331 return pos;
334 static void *seq_next(struct seq_file *s, void *v, loff_t *pos)
336 struct net *net = seq_file_net(s);
338 (*pos)++;
340 if (*pos >= ARRAY_SIZE(net->nf.nf_loggers))
341 return NULL;
343 return pos;
346 static void seq_stop(struct seq_file *s, void *v)
348 mutex_unlock(&nf_log_mutex);
351 static int seq_show(struct seq_file *s, void *v)
353 loff_t *pos = v;
354 const struct nf_logger *logger;
355 int i;
356 struct net *net = seq_file_net(s);
358 logger = nft_log_dereference(net->nf.nf_loggers[*pos]);
360 if (!logger)
361 seq_printf(s, "%2lld NONE (", *pos);
362 else
363 seq_printf(s, "%2lld %s (", *pos, logger->name);
365 if (seq_has_overflowed(s))
366 return -ENOSPC;
368 for (i = 0; i < NF_LOG_TYPE_MAX; i++) {
369 if (loggers[*pos][i] == NULL)
370 continue;
372 logger = nft_log_dereference(loggers[*pos][i]);
373 seq_printf(s, "%s", logger->name);
374 if (i == 0 && loggers[*pos][i + 1] != NULL)
375 seq_printf(s, ",");
377 if (seq_has_overflowed(s))
378 return -ENOSPC;
381 seq_printf(s, ")\n");
383 if (seq_has_overflowed(s))
384 return -ENOSPC;
385 return 0;
388 static const struct seq_operations nflog_seq_ops = {
389 .start = seq_start,
390 .next = seq_next,
391 .stop = seq_stop,
392 .show = seq_show,
395 static int nflog_open(struct inode *inode, struct file *file)
397 return seq_open_net(inode, file, &nflog_seq_ops,
398 sizeof(struct seq_net_private));
401 static const struct file_operations nflog_file_ops = {
402 .owner = THIS_MODULE,
403 .open = nflog_open,
404 .read = seq_read,
405 .llseek = seq_lseek,
406 .release = seq_release_net,
410 #endif /* PROC_FS */
412 #ifdef CONFIG_SYSCTL
413 static char nf_log_sysctl_fnames[NFPROTO_NUMPROTO-NFPROTO_UNSPEC][3];
414 static struct ctl_table nf_log_sysctl_table[NFPROTO_NUMPROTO+1];
416 static int nf_log_proc_dostring(struct ctl_table *table, int write,
417 void __user *buffer, size_t *lenp, loff_t *ppos)
419 const struct nf_logger *logger;
420 char buf[NFLOGGER_NAME_LEN];
421 int r = 0;
422 int tindex = (unsigned long)table->extra1;
423 struct net *net = current->nsproxy->net_ns;
425 if (write) {
426 struct ctl_table tmp = *table;
428 tmp.data = buf;
429 r = proc_dostring(&tmp, write, buffer, lenp, ppos);
430 if (r)
431 return r;
433 if (!strcmp(buf, "NONE")) {
434 nf_log_unbind_pf(net, tindex);
435 return 0;
437 mutex_lock(&nf_log_mutex);
438 logger = __find_logger(tindex, buf);
439 if (logger == NULL) {
440 mutex_unlock(&nf_log_mutex);
441 return -ENOENT;
443 rcu_assign_pointer(net->nf.nf_loggers[tindex], logger);
444 mutex_unlock(&nf_log_mutex);
445 } else {
446 mutex_lock(&nf_log_mutex);
447 logger = nft_log_dereference(net->nf.nf_loggers[tindex]);
448 if (!logger)
449 table->data = "NONE";
450 else
451 table->data = logger->name;
452 r = proc_dostring(table, write, buffer, lenp, ppos);
453 mutex_unlock(&nf_log_mutex);
456 return r;
459 static int netfilter_log_sysctl_init(struct net *net)
461 int i;
462 struct ctl_table *table;
464 table = nf_log_sysctl_table;
465 if (!net_eq(net, &init_net)) {
466 table = kmemdup(nf_log_sysctl_table,
467 sizeof(nf_log_sysctl_table),
468 GFP_KERNEL);
469 if (!table)
470 goto err_alloc;
471 } else {
472 for (i = NFPROTO_UNSPEC; i < NFPROTO_NUMPROTO; i++) {
473 snprintf(nf_log_sysctl_fnames[i],
474 3, "%d", i);
475 nf_log_sysctl_table[i].procname =
476 nf_log_sysctl_fnames[i];
477 nf_log_sysctl_table[i].data = NULL;
478 nf_log_sysctl_table[i].maxlen = NFLOGGER_NAME_LEN;
479 nf_log_sysctl_table[i].mode = 0644;
480 nf_log_sysctl_table[i].proc_handler =
481 nf_log_proc_dostring;
482 nf_log_sysctl_table[i].extra1 =
483 (void *)(unsigned long) i;
487 net->nf.nf_log_dir_header = register_net_sysctl(net,
488 "net/netfilter/nf_log",
489 table);
490 if (!net->nf.nf_log_dir_header)
491 goto err_reg;
493 return 0;
495 err_reg:
496 if (!net_eq(net, &init_net))
497 kfree(table);
498 err_alloc:
499 return -ENOMEM;
502 static void netfilter_log_sysctl_exit(struct net *net)
504 struct ctl_table *table;
506 table = net->nf.nf_log_dir_header->ctl_table_arg;
507 unregister_net_sysctl_table(net->nf.nf_log_dir_header);
508 if (!net_eq(net, &init_net))
509 kfree(table);
511 #else
512 static int netfilter_log_sysctl_init(struct net *net)
514 return 0;
517 static void netfilter_log_sysctl_exit(struct net *net)
520 #endif /* CONFIG_SYSCTL */
522 static int __net_init nf_log_net_init(struct net *net)
524 int ret = -ENOMEM;
526 #ifdef CONFIG_PROC_FS
527 if (!proc_create("nf_log", S_IRUGO,
528 net->nf.proc_netfilter, &nflog_file_ops))
529 return ret;
530 #endif
531 ret = netfilter_log_sysctl_init(net);
532 if (ret < 0)
533 goto out_sysctl;
535 return 0;
537 out_sysctl:
538 #ifdef CONFIG_PROC_FS
539 remove_proc_entry("nf_log", net->nf.proc_netfilter);
540 #endif
541 return ret;
544 static void __net_exit nf_log_net_exit(struct net *net)
546 netfilter_log_sysctl_exit(net);
547 #ifdef CONFIG_PROC_FS
548 remove_proc_entry("nf_log", net->nf.proc_netfilter);
549 #endif
552 static struct pernet_operations nf_log_net_ops = {
553 .init = nf_log_net_init,
554 .exit = nf_log_net_exit,
557 int __init netfilter_log_init(void)
559 return register_pernet_subsys(&nf_log_net_ops);