netfilter: nf_log: Introduce nft_log_dereference() macro
[cascardo/linux.git] / net / netfilter / nf_log.c
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>
10
11 #include "nf_internals.h"
12
13 /* Internal logging interface, which relies on the real
14    LOG target modules */
15
16 #define NF_LOG_PREFIXLEN                128
17 #define NFLOGGER_NAME_LEN               64
18
19 static struct nf_logger __rcu *loggers[NFPROTO_NUMPROTO][NF_LOG_TYPE_MAX] __read_mostly;
20 static DEFINE_MUTEX(nf_log_mutex);
21
22 #define nft_log_dereference(logger) \
23         rcu_dereference_protected(logger, lockdep_is_held(&nf_log_mutex))
24
25 static struct nf_logger *__find_logger(int pf, const char *str_logger)
26 {
27         struct nf_logger *log;
28         int i;
29
30         for (i = 0; i < NF_LOG_TYPE_MAX; i++) {
31                 if (loggers[pf][i] == NULL)
32                         continue;
33
34                 log = nft_log_dereference(loggers[pf][i]);
35                 if (!strncasecmp(str_logger, log->name, strlen(log->name)))
36                         return log;
37         }
38
39         return NULL;
40 }
41
42 void nf_log_set(struct net *net, u_int8_t pf, const struct nf_logger *logger)
43 {
44         const struct nf_logger *log;
45
46         if (pf == NFPROTO_UNSPEC)
47                 return;
48
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);
53
54         mutex_unlock(&nf_log_mutex);
55 }
56 EXPORT_SYMBOL(nf_log_set);
57
58 void nf_log_unset(struct net *net, const struct nf_logger *logger)
59 {
60         int i;
61         const struct nf_logger *log;
62
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);
68         }
69         mutex_unlock(&nf_log_mutex);
70         synchronize_rcu();
71 }
72 EXPORT_SYMBOL(nf_log_unset);
73
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)
76 {
77         int i;
78
79         if (pf >= ARRAY_SIZE(init_net.nf.nf_loggers))
80                 return -EINVAL;
81
82         mutex_lock(&nf_log_mutex);
83
84         if (pf == NFPROTO_UNSPEC) {
85                 for (i = NFPROTO_UNSPEC; i < NFPROTO_NUMPROTO; i++)
86                         rcu_assign_pointer(loggers[i][logger->type], logger);
87         } else {
88                 /* register at end of list to honor first register win */
89                 rcu_assign_pointer(loggers[pf][logger->type], logger);
90         }
91
92         mutex_unlock(&nf_log_mutex);
93
94         return 0;
95 }
96 EXPORT_SYMBOL(nf_log_register);
97
98 void nf_log_unregister(struct nf_logger *logger)
99 {
100         int i;
101
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);
106 }
107 EXPORT_SYMBOL(nf_log_unregister);
108
109 int nf_log_bind_pf(struct net *net, u_int8_t pf,
110                    const struct nf_logger *logger)
111 {
112         if (pf >= ARRAY_SIZE(net->nf.nf_loggers))
113                 return -EINVAL;
114         mutex_lock(&nf_log_mutex);
115         if (__find_logger(pf, logger->name) == NULL) {
116                 mutex_unlock(&nf_log_mutex);
117                 return -ENOENT;
118         }
119         rcu_assign_pointer(net->nf.nf_loggers[pf], logger);
120         mutex_unlock(&nf_log_mutex);
121         return 0;
122 }
123 EXPORT_SYMBOL(nf_log_bind_pf);
124
125 void nf_log_unbind_pf(struct net *net, u_int8_t pf)
126 {
127         if (pf >= ARRAY_SIZE(net->nf.nf_loggers))
128                 return;
129         mutex_lock(&nf_log_mutex);
130         RCU_INIT_POINTER(net->nf.nf_loggers[pf], NULL);
131         mutex_unlock(&nf_log_mutex);
132 }
133 EXPORT_SYMBOL(nf_log_unbind_pf);
134
135 void nf_logger_request_module(int pf, enum nf_log_type type)
136 {
137         if (loggers[pf][type] == NULL)
138                 request_module("nf-logger-%u-%u", pf, type);
139 }
140 EXPORT_SYMBOL_GPL(nf_logger_request_module);
141
142 int nf_logger_find_get(int pf, enum nf_log_type type)
143 {
144         struct nf_logger *logger;
145         int ret = -ENOENT;
146
147         logger = loggers[pf][type];
148         if (logger == NULL)
149                 request_module("nf-logger-%u-%u", pf, type);
150
151         rcu_read_lock();
152         logger = rcu_dereference(loggers[pf][type]);
153         if (logger == NULL)
154                 goto out;
155
156         if (logger && try_module_get(logger->me))
157                 ret = 0;
158 out:
159         rcu_read_unlock();
160         return ret;
161 }
162 EXPORT_SYMBOL_GPL(nf_logger_find_get);
163
164 void nf_logger_put(int pf, enum nf_log_type type)
165 {
166         struct nf_logger *logger;
167
168         BUG_ON(loggers[pf][type] == NULL);
169
170         rcu_read_lock();
171         logger = rcu_dereference(loggers[pf][type]);
172         module_put(logger->me);
173         rcu_read_unlock();
174 }
175 EXPORT_SYMBOL_GPL(nf_logger_put);
176
177 void nf_log_packet(struct net *net,
178                    u_int8_t pf,
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, ...)
185 {
186         va_list args;
187         char prefix[NF_LOG_PREFIXLEN];
188         const struct nf_logger *logger;
189
190         rcu_read_lock();
191         if (loginfo != NULL)
192                 logger = rcu_dereference(loggers[pf][loginfo->type]);
193         else
194                 logger = rcu_dereference(net->nf.nf_loggers[pf]);
195
196         if (logger) {
197                 va_start(args, fmt);
198                 vsnprintf(prefix, sizeof(prefix), fmt, args);
199                 va_end(args);
200                 logger->logfn(net, pf, hooknum, skb, in, out, loginfo, prefix);
201         }
202         rcu_read_unlock();
203 }
204 EXPORT_SYMBOL(nf_log_packet);
205
206 #define S_SIZE (1024 - (sizeof(unsigned int) + 1))
207
208 struct nf_log_buf {
209         unsigned int    count;
210         char            buf[S_SIZE + 1];
211 };
212 static struct nf_log_buf emergency, *emergency_ptr = &emergency;
213
214 __printf(2, 3) int nf_log_buf_add(struct nf_log_buf *m, const char *f, ...)
215 {
216         va_list args;
217         int len;
218
219         if (likely(m->count < S_SIZE)) {
220                 va_start(args, f);
221                 len = vsnprintf(m->buf + m->count, S_SIZE - m->count, f, args);
222                 va_end(args);
223                 if (likely(m->count + len < S_SIZE)) {
224                         m->count += len;
225                         return 0;
226                 }
227         }
228         m->count = S_SIZE;
229         printk_once(KERN_ERR KBUILD_MODNAME " please increase S_SIZE\n");
230         return -1;
231 }
232 EXPORT_SYMBOL_GPL(nf_log_buf_add);
233
234 struct nf_log_buf *nf_log_buf_open(void)
235 {
236         struct nf_log_buf *m = kmalloc(sizeof(*m), GFP_ATOMIC);
237
238         if (unlikely(!m)) {
239                 local_bh_disable();
240                 do {
241                         m = xchg(&emergency_ptr, NULL);
242                 } while (!m);
243         }
244         m->count = 0;
245         return m;
246 }
247 EXPORT_SYMBOL_GPL(nf_log_buf_open);
248
249 void nf_log_buf_close(struct nf_log_buf *m)
250 {
251         m->buf[m->count] = 0;
252         printk("%s\n", m->buf);
253
254         if (likely(m != &emergency))
255                 kfree(m);
256         else {
257                 emergency_ptr = m;
258                 local_bh_enable();
259         }
260 }
261 EXPORT_SYMBOL_GPL(nf_log_buf_close);
262
263 #ifdef CONFIG_PROC_FS
264 static void *seq_start(struct seq_file *seq, loff_t *pos)
265 {
266         struct net *net = seq_file_net(seq);
267
268         mutex_lock(&nf_log_mutex);
269
270         if (*pos >= ARRAY_SIZE(net->nf.nf_loggers))
271                 return NULL;
272
273         return pos;
274 }
275
276 static void *seq_next(struct seq_file *s, void *v, loff_t *pos)
277 {
278         struct net *net = seq_file_net(s);
279
280         (*pos)++;
281
282         if (*pos >= ARRAY_SIZE(net->nf.nf_loggers))
283                 return NULL;
284
285         return pos;
286 }
287
288 static void seq_stop(struct seq_file *s, void *v)
289 {
290         mutex_unlock(&nf_log_mutex);
291 }
292
293 static int seq_show(struct seq_file *s, void *v)
294 {
295         loff_t *pos = v;
296         const struct nf_logger *logger;
297         int i, ret;
298         struct net *net = seq_file_net(s);
299
300         logger = nft_log_dereference(net->nf.nf_loggers[*pos]);
301
302         if (!logger)
303                 ret = seq_printf(s, "%2lld NONE (", *pos);
304         else
305                 ret = seq_printf(s, "%2lld %s (", *pos, logger->name);
306
307         if (ret < 0)
308                 return ret;
309
310         for (i = 0; i < NF_LOG_TYPE_MAX; i++) {
311                 if (loggers[*pos][i] == NULL)
312                         continue;
313
314                 logger = nft_log_dereference(loggers[*pos][i]);
315                 ret = seq_printf(s, "%s", logger->name);
316                 if (ret < 0)
317                         return ret;
318                 if (i == 0 && loggers[*pos][i + 1] != NULL) {
319                         ret = seq_printf(s, ",");
320                         if (ret < 0)
321                                 return ret;
322                 }
323         }
324
325         return seq_printf(s, ")\n");
326 }
327
328 static const struct seq_operations nflog_seq_ops = {
329         .start  = seq_start,
330         .next   = seq_next,
331         .stop   = seq_stop,
332         .show   = seq_show,
333 };
334
335 static int nflog_open(struct inode *inode, struct file *file)
336 {
337         return seq_open_net(inode, file, &nflog_seq_ops,
338                             sizeof(struct seq_net_private));
339 }
340
341 static const struct file_operations nflog_file_ops = {
342         .owner   = THIS_MODULE,
343         .open    = nflog_open,
344         .read    = seq_read,
345         .llseek  = seq_lseek,
346         .release = seq_release_net,
347 };
348
349
350 #endif /* PROC_FS */
351
352 #ifdef CONFIG_SYSCTL
353 static char nf_log_sysctl_fnames[NFPROTO_NUMPROTO-NFPROTO_UNSPEC][3];
354 static struct ctl_table nf_log_sysctl_table[NFPROTO_NUMPROTO+1];
355
356 static int nf_log_proc_dostring(struct ctl_table *table, int write,
357                          void __user *buffer, size_t *lenp, loff_t *ppos)
358 {
359         const struct nf_logger *logger;
360         char buf[NFLOGGER_NAME_LEN];
361         size_t size = *lenp;
362         int r = 0;
363         int tindex = (unsigned long)table->extra1;
364         struct net *net = current->nsproxy->net_ns;
365
366         if (write) {
367                 if (size > sizeof(buf))
368                         size = sizeof(buf);
369                 if (copy_from_user(buf, buffer, size))
370                         return -EFAULT;
371
372                 if (!strcmp(buf, "NONE")) {
373                         nf_log_unbind_pf(net, tindex);
374                         return 0;
375                 }
376                 mutex_lock(&nf_log_mutex);
377                 logger = __find_logger(tindex, buf);
378                 if (logger == NULL) {
379                         mutex_unlock(&nf_log_mutex);
380                         return -ENOENT;
381                 }
382                 rcu_assign_pointer(net->nf.nf_loggers[tindex], logger);
383                 mutex_unlock(&nf_log_mutex);
384         } else {
385                 mutex_lock(&nf_log_mutex);
386                 logger = nft_log_dereference(net->nf.nf_loggers[tindex]);
387                 if (!logger)
388                         table->data = "NONE";
389                 else
390                         table->data = logger->name;
391                 r = proc_dostring(table, write, buffer, lenp, ppos);
392                 mutex_unlock(&nf_log_mutex);
393         }
394
395         return r;
396 }
397
398 static int netfilter_log_sysctl_init(struct net *net)
399 {
400         int i;
401         struct ctl_table *table;
402
403         table = nf_log_sysctl_table;
404         if (!net_eq(net, &init_net)) {
405                 table = kmemdup(nf_log_sysctl_table,
406                                  sizeof(nf_log_sysctl_table),
407                                  GFP_KERNEL);
408                 if (!table)
409                         goto err_alloc;
410         } else {
411                 for (i = NFPROTO_UNSPEC; i < NFPROTO_NUMPROTO; i++) {
412                         snprintf(nf_log_sysctl_fnames[i],
413                                  3, "%d", i);
414                         nf_log_sysctl_table[i].procname =
415                                 nf_log_sysctl_fnames[i];
416                         nf_log_sysctl_table[i].data = NULL;
417                         nf_log_sysctl_table[i].maxlen =
418                                 NFLOGGER_NAME_LEN * sizeof(char);
419                         nf_log_sysctl_table[i].mode = 0644;
420                         nf_log_sysctl_table[i].proc_handler =
421                                 nf_log_proc_dostring;
422                         nf_log_sysctl_table[i].extra1 =
423                                 (void *)(unsigned long) i;
424                 }
425         }
426
427         net->nf.nf_log_dir_header = register_net_sysctl(net,
428                                                 "net/netfilter/nf_log",
429                                                 table);
430         if (!net->nf.nf_log_dir_header)
431                 goto err_reg;
432
433         return 0;
434
435 err_reg:
436         if (!net_eq(net, &init_net))
437                 kfree(table);
438 err_alloc:
439         return -ENOMEM;
440 }
441
442 static void netfilter_log_sysctl_exit(struct net *net)
443 {
444         struct ctl_table *table;
445
446         table = net->nf.nf_log_dir_header->ctl_table_arg;
447         unregister_net_sysctl_table(net->nf.nf_log_dir_header);
448         if (!net_eq(net, &init_net))
449                 kfree(table);
450 }
451 #else
452 static int netfilter_log_sysctl_init(struct net *net)
453 {
454         return 0;
455 }
456
457 static void netfilter_log_sysctl_exit(struct net *net)
458 {
459 }
460 #endif /* CONFIG_SYSCTL */
461
462 static int __net_init nf_log_net_init(struct net *net)
463 {
464         int ret = -ENOMEM;
465
466 #ifdef CONFIG_PROC_FS
467         if (!proc_create("nf_log", S_IRUGO,
468                          net->nf.proc_netfilter, &nflog_file_ops))
469                 return ret;
470 #endif
471         ret = netfilter_log_sysctl_init(net);
472         if (ret < 0)
473                 goto out_sysctl;
474
475         return 0;
476
477 out_sysctl:
478 #ifdef CONFIG_PROC_FS
479         remove_proc_entry("nf_log", net->nf.proc_netfilter);
480 #endif
481         return ret;
482 }
483
484 static void __net_exit nf_log_net_exit(struct net *net)
485 {
486         netfilter_log_sysctl_exit(net);
487 #ifdef CONFIG_PROC_FS
488         remove_proc_entry("nf_log", net->nf.proc_netfilter);
489 #endif
490 }
491
492 static struct pernet_operations nf_log_net_ops = {
493         .init = nf_log_net_init,
494         .exit = nf_log_net_exit,
495 };
496
497 int __init netfilter_log_init(void)
498 {
499         return register_pernet_subsys(&nf_log_net_ops);
500 }