blob: 85296d4eac0e56c69052bbdab75269f3c7fd333e [file] [log] [blame]
Harald Weltef6ebe772005-08-09 20:21:49 -07001#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>
Harald Weltebbd86b9f2005-08-09 20:23:11 -07007#include <linux/seq_file.h>
Harald Weltef6ebe772005-08-09 20:21:49 -07008#include <net/protocol.h>
Patrick McHardyf01ffbd2007-12-17 22:38:49 -08009#include <net/netfilter/nf_log.h>
Harald Weltef6ebe772005-08-09 20:21:49 -070010
11#include "nf_internals.h"
12
YOSHIFUJI Hideakia5d29262007-07-19 10:44:21 +090013/* Internal logging interface, which relies on the real
Harald Weltef6ebe772005-08-09 20:21:49 -070014 LOG target modules */
15
16#define NF_LOG_PREFIXLEN 128
Eric Leblond17625272009-03-23 13:16:53 +010017#define NFLOGGER_NAME_LEN 64
Harald Weltef6ebe772005-08-09 20:21:49 -070018
Eric Leblondca735b32009-03-16 14:54:21 +010019static struct list_head nf_loggers_l[NFPROTO_NUMPROTO] __read_mostly;
Patrick McHardy9b735342007-02-12 11:11:39 -080020static DEFINE_MUTEX(nf_log_mutex);
Harald Weltef6ebe772005-08-09 20:21:49 -070021
Eric Leblondca735b32009-03-16 14:54:21 +010022static struct nf_logger *__find_logger(int pf, const char *str_logger)
Harald Weltef6ebe772005-08-09 20:21:49 -070023{
Eric Leblondca735b32009-03-16 14:54:21 +010024 struct nf_logger *t;
25
26 list_for_each_entry(t, &nf_loggers_l[pf], list[pf]) {
27 if (!strnicmp(str_logger, t->name, strlen(t->name)))
28 return t;
29 }
30
31 return NULL;
32}
33
Gao feng30e0c6a2013-03-24 23:50:40 +000034void nf_log_set(struct net *net, u_int8_t pf, const struct nf_logger *logger)
35{
36 const struct nf_logger *log;
37
Gao feng30e0c6a2013-03-24 23:50:40 +000038 if (pf == NFPROTO_UNSPEC)
39 return;
40
41 mutex_lock(&nf_log_mutex);
42 log = rcu_dereference_protected(net->nf.nf_loggers[pf],
43 lockdep_is_held(&nf_log_mutex));
44 if (log == NULL)
45 rcu_assign_pointer(net->nf.nf_loggers[pf], logger);
46
47 mutex_unlock(&nf_log_mutex);
48}
49EXPORT_SYMBOL(nf_log_set);
50
51void nf_log_unset(struct net *net, const struct nf_logger *logger)
52{
53 int i;
54 const struct nf_logger *log;
55
Gao feng30e0c6a2013-03-24 23:50:40 +000056 mutex_lock(&nf_log_mutex);
57 for (i = 0; i < NFPROTO_NUMPROTO; i++) {
58 log = rcu_dereference_protected(net->nf.nf_loggers[i],
59 lockdep_is_held(&nf_log_mutex));
60 if (log == logger)
61 RCU_INIT_POINTER(net->nf.nf_loggers[i], NULL);
62 }
63 mutex_unlock(&nf_log_mutex);
64 synchronize_rcu();
65}
66EXPORT_SYMBOL(nf_log_unset);
67
Adam Buchbinderd93cf062012-09-19 21:47:58 -040068/* return EEXIST if the same logger is registered, 0 on success. */
Eric Leblondca735b32009-03-16 14:54:21 +010069int nf_log_register(u_int8_t pf, struct nf_logger *logger)
70{
Eric Dumazetb6f0a362009-04-15 12:16:19 +020071 int i;
Harald Weltef6ebe772005-08-09 20:21:49 -070072
Gao feng30e0c6a2013-03-24 23:50:40 +000073 if (pf >= ARRAY_SIZE(init_net.nf.nf_loggers))
Harald Welte8a61fad2005-08-09 20:23:53 -070074 return -EINVAL;
75
Eric Dumazetb6f0a362009-04-15 12:16:19 +020076 for (i = 0; i < ARRAY_SIZE(logger->list); i++)
77 INIT_LIST_HEAD(&logger->list[i]);
78
Eric Leblondca735b32009-03-16 14:54:21 +010079 mutex_lock(&nf_log_mutex);
Harald Welted72367b2005-08-09 20:23:36 -070080
Eric Leblondca735b32009-03-16 14:54:21 +010081 if (pf == NFPROTO_UNSPEC) {
Eric Leblondca735b32009-03-16 14:54:21 +010082 for (i = NFPROTO_UNSPEC; i < NFPROTO_NUMPROTO; i++)
83 list_add_tail(&(logger->list[i]), &(nf_loggers_l[i]));
84 } else {
85 /* register at end of list to honor first register win */
86 list_add_tail(&logger->list[pf], &nf_loggers_l[pf]);
Eric Leblondca735b32009-03-16 14:54:21 +010087 }
Patrick McHardy9b735342007-02-12 11:11:39 -080088
89 mutex_unlock(&nf_log_mutex);
Eric Leblondca735b32009-03-16 14:54:21 +010090
91 return 0;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080092}
Harald Weltef6ebe772005-08-09 20:21:49 -070093EXPORT_SYMBOL(nf_log_register);
94
Eric Leblondca735b32009-03-16 14:54:21 +010095void nf_log_unregister(struct nf_logger *logger)
Harald Weltef6ebe772005-08-09 20:21:49 -070096{
Harald Weltef6ebe772005-08-09 20:21:49 -070097 int i;
98
Patrick McHardy9b735342007-02-12 11:11:39 -080099 mutex_lock(&nf_log_mutex);
Gao feng30e0c6a2013-03-24 23:50:40 +0000100 for (i = 0; i < NFPROTO_NUMPROTO; i++)
Eric Leblondca735b32009-03-16 14:54:21 +0100101 list_del(&logger->list[i]);
Patrick McHardy9b735342007-02-12 11:11:39 -0800102 mutex_unlock(&nf_log_mutex);
Harald Weltef6ebe772005-08-09 20:21:49 -0700103}
Patrick McHardye92ad992007-02-12 11:11:55 -0800104EXPORT_SYMBOL(nf_log_unregister);
Harald Weltef6ebe772005-08-09 20:21:49 -0700105
Gao feng30e0c6a2013-03-24 23:50:40 +0000106int nf_log_bind_pf(struct net *net, u_int8_t pf,
107 const struct nf_logger *logger)
Eric Leblondca735b32009-03-16 14:54:21 +0100108{
Gao feng30e0c6a2013-03-24 23:50:40 +0000109 if (pf >= ARRAY_SIZE(net->nf.nf_loggers))
Jan Engelhardt9ef02982011-03-02 12:10:13 +0100110 return -EINVAL;
Eric Leblondca735b32009-03-16 14:54:21 +0100111 mutex_lock(&nf_log_mutex);
112 if (__find_logger(pf, logger->name) == NULL) {
113 mutex_unlock(&nf_log_mutex);
114 return -ENOENT;
115 }
Gao feng30e0c6a2013-03-24 23:50:40 +0000116 rcu_assign_pointer(net->nf.nf_loggers[pf], logger);
Eric Leblondca735b32009-03-16 14:54:21 +0100117 mutex_unlock(&nf_log_mutex);
118 return 0;
119}
120EXPORT_SYMBOL(nf_log_bind_pf);
121
Gao feng30e0c6a2013-03-24 23:50:40 +0000122void nf_log_unbind_pf(struct net *net, u_int8_t pf)
Eric Leblondca735b32009-03-16 14:54:21 +0100123{
Gao feng30e0c6a2013-03-24 23:50:40 +0000124 if (pf >= ARRAY_SIZE(net->nf.nf_loggers))
Jan Engelhardt9ef02982011-03-02 12:10:13 +0100125 return;
Eric Leblondca735b32009-03-16 14:54:21 +0100126 mutex_lock(&nf_log_mutex);
Gao feng30e0c6a2013-03-24 23:50:40 +0000127 RCU_INIT_POINTER(net->nf.nf_loggers[pf], NULL);
Eric Leblondca735b32009-03-16 14:54:21 +0100128 mutex_unlock(&nf_log_mutex);
129}
130EXPORT_SYMBOL(nf_log_unbind_pf);
131
Gao feng30e0c6a2013-03-24 23:50:40 +0000132void nf_log_packet(struct net *net,
133 u_int8_t pf,
Harald Weltef6ebe772005-08-09 20:21:49 -0700134 unsigned int hooknum,
135 const struct sk_buff *skb,
136 const struct net_device *in,
137 const struct net_device *out,
Patrick McHardy7b2f9632007-12-17 22:39:08 -0800138 const struct nf_loginfo *loginfo,
Harald Weltef6ebe772005-08-09 20:21:49 -0700139 const char *fmt, ...)
140{
141 va_list args;
142 char prefix[NF_LOG_PREFIXLEN];
Patrick McHardy7b2f9632007-12-17 22:39:08 -0800143 const struct nf_logger *logger;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800144
Harald Weltef6ebe772005-08-09 20:21:49 -0700145 rcu_read_lock();
Gao feng30e0c6a2013-03-24 23:50:40 +0000146 logger = rcu_dereference(net->nf.nf_loggers[pf]);
Harald Weltef6ebe772005-08-09 20:21:49 -0700147 if (logger) {
148 va_start(args, fmt);
149 vsnprintf(prefix, sizeof(prefix), fmt, args);
150 va_end(args);
Hans Schillstrom8cdb46d2013-05-15 01:23:45 +0000151 logger->logfn(net, pf, hooknum, skb, in, out, loginfo, prefix);
Harald Weltef6ebe772005-08-09 20:21:49 -0700152 }
153 rcu_read_unlock();
154}
155EXPORT_SYMBOL(nf_log_packet);
156
157#ifdef CONFIG_PROC_FS
158static void *seq_start(struct seq_file *seq, loff_t *pos)
159{
Gao feng30e0c6a2013-03-24 23:50:40 +0000160 struct net *net = seq_file_net(seq);
161
Patrick McHardy6440fe02009-11-19 04:59:05 +0000162 mutex_lock(&nf_log_mutex);
Harald Weltef6ebe772005-08-09 20:21:49 -0700163
Gao feng30e0c6a2013-03-24 23:50:40 +0000164 if (*pos >= ARRAY_SIZE(net->nf.nf_loggers))
Harald Weltef6ebe772005-08-09 20:21:49 -0700165 return NULL;
166
167 return pos;
168}
169
170static void *seq_next(struct seq_file *s, void *v, loff_t *pos)
171{
Gao feng30e0c6a2013-03-24 23:50:40 +0000172 struct net *net = seq_file_net(s);
173
Harald Weltef6ebe772005-08-09 20:21:49 -0700174 (*pos)++;
175
Gao feng30e0c6a2013-03-24 23:50:40 +0000176 if (*pos >= ARRAY_SIZE(net->nf.nf_loggers))
Harald Weltef6ebe772005-08-09 20:21:49 -0700177 return NULL;
178
179 return pos;
180}
181
182static void seq_stop(struct seq_file *s, void *v)
183{
Patrick McHardy6440fe02009-11-19 04:59:05 +0000184 mutex_unlock(&nf_log_mutex);
Harald Weltef6ebe772005-08-09 20:21:49 -0700185}
186
187static int seq_show(struct seq_file *s, void *v)
188{
189 loff_t *pos = v;
190 const struct nf_logger *logger;
Eric Leblondc7a913c2009-03-16 14:55:27 +0100191 struct nf_logger *t;
192 int ret;
Gao feng30e0c6a2013-03-24 23:50:40 +0000193 struct net *net = seq_file_net(s);
Harald Weltef6ebe772005-08-09 20:21:49 -0700194
Gao feng30e0c6a2013-03-24 23:50:40 +0000195 logger = rcu_dereference_protected(net->nf.nf_loggers[*pos],
Eric Dumazet0e60ebe2010-11-15 18:17:21 +0100196 lockdep_is_held(&nf_log_mutex));
Harald Weltef6ebe772005-08-09 20:21:49 -0700197
198 if (!logger)
Eric Leblondc7a913c2009-03-16 14:55:27 +0100199 ret = seq_printf(s, "%2lld NONE (", *pos);
200 else
201 ret = seq_printf(s, "%2lld %s (", *pos, logger->name);
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800202
Eric Leblondc7a913c2009-03-16 14:55:27 +0100203 if (ret < 0)
204 return ret;
205
Eric Leblondc7a913c2009-03-16 14:55:27 +0100206 list_for_each_entry(t, &nf_loggers_l[*pos], list[*pos]) {
207 ret = seq_printf(s, "%s", t->name);
Patrick McHardy6440fe02009-11-19 04:59:05 +0000208 if (ret < 0)
Eric Leblondc7a913c2009-03-16 14:55:27 +0100209 return ret;
Eric Leblondc7a913c2009-03-16 14:55:27 +0100210 if (&t->list[*pos] != nf_loggers_l[*pos].prev) {
211 ret = seq_printf(s, ",");
Patrick McHardy6440fe02009-11-19 04:59:05 +0000212 if (ret < 0)
Eric Leblondc7a913c2009-03-16 14:55:27 +0100213 return ret;
Eric Leblondc7a913c2009-03-16 14:55:27 +0100214 }
215 }
Eric Leblondc7a913c2009-03-16 14:55:27 +0100216
217 return seq_printf(s, ")\n");
Harald Weltef6ebe772005-08-09 20:21:49 -0700218}
219
Philippe De Muyter56b3d972007-07-10 23:07:31 -0700220static const struct seq_operations nflog_seq_ops = {
Harald Weltef6ebe772005-08-09 20:21:49 -0700221 .start = seq_start,
222 .next = seq_next,
223 .stop = seq_stop,
224 .show = seq_show,
225};
226
227static int nflog_open(struct inode *inode, struct file *file)
228{
Gao feng30e0c6a2013-03-24 23:50:40 +0000229 return seq_open_net(inode, file, &nflog_seq_ops,
230 sizeof(struct seq_net_private));
Harald Weltef6ebe772005-08-09 20:21:49 -0700231}
232
Arjan van de Venda7071d2007-02-12 00:55:36 -0800233static const struct file_operations nflog_file_ops = {
Harald Weltef6ebe772005-08-09 20:21:49 -0700234 .owner = THIS_MODULE,
235 .open = nflog_open,
236 .read = seq_read,
237 .llseek = seq_lseek,
Gao feng30e0c6a2013-03-24 23:50:40 +0000238 .release = seq_release_net,
Harald Weltef6ebe772005-08-09 20:21:49 -0700239};
240
Eric Leblond17625272009-03-23 13:16:53 +0100241
Harald Weltef6ebe772005-08-09 20:21:49 -0700242#endif /* PROC_FS */
243
Eric Leblond17625272009-03-23 13:16:53 +0100244#ifdef CONFIG_SYSCTL
Eric Leblond17625272009-03-23 13:16:53 +0100245static char nf_log_sysctl_fnames[NFPROTO_NUMPROTO-NFPROTO_UNSPEC][3];
246static struct ctl_table nf_log_sysctl_table[NFPROTO_NUMPROTO+1];
Eric Leblond17625272009-03-23 13:16:53 +0100247
Joe Perchesfe2c6332013-06-11 23:04:25 -0700248static int nf_log_proc_dostring(struct ctl_table *table, int write,
Patrick McHardy24955612009-06-22 14:15:30 +0200249 void __user *buffer, size_t *lenp, loff_t *ppos)
Eric Leblond17625272009-03-23 13:16:53 +0100250{
251 const struct nf_logger *logger;
Patrick McHardy24955612009-06-22 14:15:30 +0200252 char buf[NFLOGGER_NAME_LEN];
253 size_t size = *lenp;
Eric Leblond17625272009-03-23 13:16:53 +0100254 int r = 0;
255 int tindex = (unsigned long)table->extra1;
Gao feng30e0c6a2013-03-24 23:50:40 +0000256 struct net *net = current->nsproxy->net_ns;
Eric Leblond17625272009-03-23 13:16:53 +0100257
258 if (write) {
Patrick McHardy24955612009-06-22 14:15:30 +0200259 if (size > sizeof(buf))
260 size = sizeof(buf);
261 if (copy_from_user(buf, buffer, size))
262 return -EFAULT;
263
264 if (!strcmp(buf, "NONE")) {
Gao feng30e0c6a2013-03-24 23:50:40 +0000265 nf_log_unbind_pf(net, tindex);
Eric Leblond17625272009-03-23 13:16:53 +0100266 return 0;
267 }
268 mutex_lock(&nf_log_mutex);
Patrick McHardy24955612009-06-22 14:15:30 +0200269 logger = __find_logger(tindex, buf);
Eric Leblond17625272009-03-23 13:16:53 +0100270 if (logger == NULL) {
271 mutex_unlock(&nf_log_mutex);
272 return -ENOENT;
273 }
Gao feng30e0c6a2013-03-24 23:50:40 +0000274 rcu_assign_pointer(net->nf.nf_loggers[tindex], logger);
Eric Leblond17625272009-03-23 13:16:53 +0100275 mutex_unlock(&nf_log_mutex);
276 } else {
Patrick McHardy266d07c2009-06-13 12:21:10 +0200277 mutex_lock(&nf_log_mutex);
Gao feng30e0c6a2013-03-24 23:50:40 +0000278 logger = rcu_dereference_protected(net->nf.nf_loggers[tindex],
Eric Dumazet0e60ebe2010-11-15 18:17:21 +0100279 lockdep_is_held(&nf_log_mutex));
Eric Leblond17625272009-03-23 13:16:53 +0100280 if (!logger)
281 table->data = "NONE";
282 else
283 table->data = logger->name;
Alexey Dobriyan8d65af72009-09-23 15:57:19 -0700284 r = proc_dostring(table, write, buffer, lenp, ppos);
Patrick McHardy266d07c2009-06-13 12:21:10 +0200285 mutex_unlock(&nf_log_mutex);
Eric Leblond17625272009-03-23 13:16:53 +0100286 }
287
288 return r;
289}
290
Gao feng30e0c6a2013-03-24 23:50:40 +0000291static int netfilter_log_sysctl_init(struct net *net)
Eric Leblond17625272009-03-23 13:16:53 +0100292{
293 int i;
Gao feng30e0c6a2013-03-24 23:50:40 +0000294 struct ctl_table *table;
Eric Leblond17625272009-03-23 13:16:53 +0100295
Gao feng30e0c6a2013-03-24 23:50:40 +0000296 table = nf_log_sysctl_table;
297 if (!net_eq(net, &init_net)) {
298 table = kmemdup(nf_log_sysctl_table,
299 sizeof(nf_log_sysctl_table),
300 GFP_KERNEL);
301 if (!table)
302 goto err_alloc;
303 } else {
304 for (i = NFPROTO_UNSPEC; i < NFPROTO_NUMPROTO; i++) {
305 snprintf(nf_log_sysctl_fnames[i],
306 3, "%d", i);
307 nf_log_sysctl_table[i].procname =
308 nf_log_sysctl_fnames[i];
309 nf_log_sysctl_table[i].data = NULL;
310 nf_log_sysctl_table[i].maxlen =
311 NFLOGGER_NAME_LEN * sizeof(char);
312 nf_log_sysctl_table[i].mode = 0644;
313 nf_log_sysctl_table[i].proc_handler =
314 nf_log_proc_dostring;
315 nf_log_sysctl_table[i].extra1 =
316 (void *)(unsigned long) i;
317 }
Eric Leblond17625272009-03-23 13:16:53 +0100318 }
319
Gao feng30e0c6a2013-03-24 23:50:40 +0000320 net->nf.nf_log_dir_header = register_net_sysctl(net,
321 "net/netfilter/nf_log",
322 table);
323 if (!net->nf.nf_log_dir_header)
324 goto err_reg;
Eric Leblond17625272009-03-23 13:16:53 +0100325
326 return 0;
Gao feng30e0c6a2013-03-24 23:50:40 +0000327
328err_reg:
329 if (!net_eq(net, &init_net))
330 kfree(table);
331err_alloc:
332 return -ENOMEM;
333}
334
335static void netfilter_log_sysctl_exit(struct net *net)
336{
337 struct ctl_table *table;
338
339 table = net->nf.nf_log_dir_header->ctl_table_arg;
340 unregister_net_sysctl_table(net->nf.nf_log_dir_header);
341 if (!net_eq(net, &init_net))
342 kfree(table);
Eric Leblond17625272009-03-23 13:16:53 +0100343}
344#else
Gao feng30e0c6a2013-03-24 23:50:40 +0000345static int netfilter_log_sysctl_init(struct net *net)
Eric Leblond17625272009-03-23 13:16:53 +0100346{
347 return 0;
348}
Gao feng30e0c6a2013-03-24 23:50:40 +0000349
350static void netfilter_log_sysctl_exit(struct net *net)
351{
352}
Eric Leblond17625272009-03-23 13:16:53 +0100353#endif /* CONFIG_SYSCTL */
Harald Weltef6ebe772005-08-09 20:21:49 -0700354
Gao feng30e0c6a2013-03-24 23:50:40 +0000355static int __net_init nf_log_net_init(struct net *net)
Harald Weltef6ebe772005-08-09 20:21:49 -0700356{
Gao feng30e0c6a2013-03-24 23:50:40 +0000357 int ret = -ENOMEM;
358
Harald Weltef6ebe772005-08-09 20:21:49 -0700359#ifdef CONFIG_PROC_FS
Denis V. Lunev8eeee8b2008-03-27 16:55:53 -0700360 if (!proc_create("nf_log", S_IRUGO,
Gao feng30e0c6a2013-03-24 23:50:40 +0000361 net->nf.proc_netfilter, &nflog_file_ops))
362 return ret;
Harald Welte62243922005-08-11 15:30:45 -0700363#endif
Gao feng30e0c6a2013-03-24 23:50:40 +0000364 ret = netfilter_log_sysctl_init(net);
365 if (ret < 0)
366 goto out_sysctl;
Eric Leblondca735b32009-03-16 14:54:21 +0100367
Gao feng30e0c6a2013-03-24 23:50:40 +0000368 return 0;
369
370out_sysctl:
Pablo Neira Ayusoe778f562013-04-30 08:01:18 +0000371#ifdef CONFIG_PROC_FS
Pablo Neira Ayuso6d11cfd2013-05-22 22:42:36 +0000372 remove_proc_entry("nf_log", net->nf.proc_netfilter);
Pablo Neira Ayusoe778f562013-04-30 08:01:18 +0000373#endif
Gao feng30e0c6a2013-03-24 23:50:40 +0000374 return ret;
375}
376
377static void __net_exit nf_log_net_exit(struct net *net)
378{
379 netfilter_log_sysctl_exit(net);
Pablo Neira Ayusoe778f562013-04-30 08:01:18 +0000380#ifdef CONFIG_PROC_FS
Gao feng30e0c6a2013-03-24 23:50:40 +0000381 remove_proc_entry("nf_log", net->nf.proc_netfilter);
Pablo Neira Ayusoe778f562013-04-30 08:01:18 +0000382#endif
Gao feng30e0c6a2013-03-24 23:50:40 +0000383}
384
385static struct pernet_operations nf_log_net_ops = {
386 .init = nf_log_net_init,
387 .exit = nf_log_net_exit,
388};
389
390int __init netfilter_log_init(void)
391{
392 int i, ret;
393
394 ret = register_pernet_subsys(&nf_log_net_ops);
395 if (ret < 0)
396 return ret;
Eric Leblond17625272009-03-23 13:16:53 +0100397
Eric Leblondca735b32009-03-16 14:54:21 +0100398 for (i = NFPROTO_UNSPEC; i < NFPROTO_NUMPROTO; i++)
399 INIT_LIST_HEAD(&(nf_loggers_l[i]));
400
Harald Weltef6ebe772005-08-09 20:21:49 -0700401 return 0;
402}