Merge branch 'irq-fixes-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[deliverable/linux.git] / net / netfilter / nf_log.c
CommitLineData
f6ebe77f
HW
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>
bbd86b9f 7#include <linux/seq_file.h>
f6ebe77f 8#include <net/protocol.h>
f01ffbd6 9#include <net/netfilter/nf_log.h>
f6ebe77f
HW
10
11#include "nf_internals.h"
12
a5d29264 13/* Internal logging interface, which relies on the real
f6ebe77f
HW
14 LOG target modules */
15
16#define NF_LOG_PREFIXLEN 128
17625274 17#define NFLOGGER_NAME_LEN 64
f6ebe77f 18
0906a372 19static const struct nf_logger __rcu *nf_loggers[NFPROTO_NUMPROTO] __read_mostly;
ca735b3a 20static struct list_head nf_loggers_l[NFPROTO_NUMPROTO] __read_mostly;
9b73534d 21static DEFINE_MUTEX(nf_log_mutex);
f6ebe77f 22
ca735b3a 23static struct nf_logger *__find_logger(int pf, const char *str_logger)
f6ebe77f 24{
ca735b3a 25 struct nf_logger *t;
f6ebe77f 26
ca735b3a
EL
27 list_for_each_entry(t, &nf_loggers_l[pf], list[pf]) {
28 if (!strnicmp(str_logger, t->name, strlen(t->name)))
29 return t;
30 }
d72367b6 31
ca735b3a 32 return NULL;
601e68e1 33}
f6ebe77f 34
ca735b3a
EL
35/* return EEXIST if the same logger is registred, 0 on success. */
36int nf_log_register(u_int8_t pf, struct nf_logger *logger)
f6ebe77f 37{
b56f2d55 38 const struct nf_logger *llog;
b6f0a365 39 int i;
ca735b3a 40
7e9c6eeb 41 if (pf >= ARRAY_SIZE(nf_loggers))
ca735b3a
EL
42 return -EINVAL;
43
b6f0a365
ED
44 for (i = 0; i < ARRAY_SIZE(logger->list); i++)
45 INIT_LIST_HEAD(&logger->list[i]);
46
9b73534d 47 mutex_lock(&nf_log_mutex);
ca735b3a
EL
48
49 if (pf == NFPROTO_UNSPEC) {
ca735b3a
EL
50 for (i = NFPROTO_UNSPEC; i < NFPROTO_NUMPROTO; i++)
51 list_add_tail(&(logger->list[i]), &(nf_loggers_l[i]));
52 } else {
53 /* register at end of list to honor first register win */
54 list_add_tail(&logger->list[pf], &nf_loggers_l[pf]);
b56f2d55
PM
55 llog = rcu_dereference_protected(nf_loggers[pf],
56 lockdep_is_held(&nf_log_mutex));
57 if (llog == NULL)
ca735b3a
EL
58 rcu_assign_pointer(nf_loggers[pf], logger);
59 }
60
9b73534d 61 mutex_unlock(&nf_log_mutex);
f6ebe77f 62
ca735b3a 63 return 0;
f6ebe77f 64}
ca735b3a 65EXPORT_SYMBOL(nf_log_register);
f6ebe77f 66
ca735b3a 67void nf_log_unregister(struct nf_logger *logger)
f6ebe77f 68{
b56f2d55 69 const struct nf_logger *c_logger;
f6ebe77f
HW
70 int i;
71
9b73534d 72 mutex_lock(&nf_log_mutex);
7e9c6eeb 73 for (i = 0; i < ARRAY_SIZE(nf_loggers); i++) {
b56f2d55
PM
74 c_logger = rcu_dereference_protected(nf_loggers[i],
75 lockdep_is_held(&nf_log_mutex));
76 if (c_logger == logger)
e92ad99c 77 rcu_assign_pointer(nf_loggers[i], NULL);
ca735b3a 78 list_del(&logger->list[i]);
f6ebe77f 79 }
9b73534d 80 mutex_unlock(&nf_log_mutex);
f6ebe77f 81
a5ea6169 82 synchronize_rcu();
f6ebe77f 83}
e92ad99c 84EXPORT_SYMBOL(nf_log_unregister);
f6ebe77f 85
ca735b3a
EL
86int nf_log_bind_pf(u_int8_t pf, const struct nf_logger *logger)
87{
9ef0298a
JE
88 if (pf >= ARRAY_SIZE(nf_loggers))
89 return -EINVAL;
ca735b3a
EL
90 mutex_lock(&nf_log_mutex);
91 if (__find_logger(pf, logger->name) == NULL) {
92 mutex_unlock(&nf_log_mutex);
93 return -ENOENT;
94 }
95 rcu_assign_pointer(nf_loggers[pf], logger);
96 mutex_unlock(&nf_log_mutex);
97 return 0;
98}
99EXPORT_SYMBOL(nf_log_bind_pf);
100
101void nf_log_unbind_pf(u_int8_t pf)
102{
9ef0298a
JE
103 if (pf >= ARRAY_SIZE(nf_loggers))
104 return;
ca735b3a
EL
105 mutex_lock(&nf_log_mutex);
106 rcu_assign_pointer(nf_loggers[pf], NULL);
107 mutex_unlock(&nf_log_mutex);
108}
109EXPORT_SYMBOL(nf_log_unbind_pf);
110
76108cea 111void nf_log_packet(u_int8_t pf,
f6ebe77f
HW
112 unsigned int hooknum,
113 const struct sk_buff *skb,
114 const struct net_device *in,
115 const struct net_device *out,
7b2f9631 116 const struct nf_loginfo *loginfo,
f6ebe77f
HW
117 const char *fmt, ...)
118{
119 va_list args;
120 char prefix[NF_LOG_PREFIXLEN];
7b2f9631 121 const struct nf_logger *logger;
601e68e1 122
f6ebe77f 123 rcu_read_lock();
e92ad99c 124 logger = rcu_dereference(nf_loggers[pf]);
f6ebe77f
HW
125 if (logger) {
126 va_start(args, fmt);
127 vsnprintf(prefix, sizeof(prefix), fmt, args);
128 va_end(args);
f6ebe77f 129 logger->logfn(pf, hooknum, skb, in, out, loginfo, prefix);
f6ebe77f
HW
130 }
131 rcu_read_unlock();
132}
133EXPORT_SYMBOL(nf_log_packet);
134
135#ifdef CONFIG_PROC_FS
136static void *seq_start(struct seq_file *seq, loff_t *pos)
137{
6440fe05 138 mutex_lock(&nf_log_mutex);
f6ebe77f 139
7e9c6eeb 140 if (*pos >= ARRAY_SIZE(nf_loggers))
f6ebe77f
HW
141 return NULL;
142
143 return pos;
144}
145
146static void *seq_next(struct seq_file *s, void *v, loff_t *pos)
147{
148 (*pos)++;
149
7e9c6eeb 150 if (*pos >= ARRAY_SIZE(nf_loggers))
f6ebe77f
HW
151 return NULL;
152
153 return pos;
154}
155
156static void seq_stop(struct seq_file *s, void *v)
157{
6440fe05 158 mutex_unlock(&nf_log_mutex);
f6ebe77f
HW
159}
160
161static int seq_show(struct seq_file *s, void *v)
162{
163 loff_t *pos = v;
164 const struct nf_logger *logger;
c7a913cd
EL
165 struct nf_logger *t;
166 int ret;
f6ebe77f 167
0e60ebe0
ED
168 logger = rcu_dereference_protected(nf_loggers[*pos],
169 lockdep_is_held(&nf_log_mutex));
f6ebe77f
HW
170
171 if (!logger)
c7a913cd
EL
172 ret = seq_printf(s, "%2lld NONE (", *pos);
173 else
174 ret = seq_printf(s, "%2lld %s (", *pos, logger->name);
175
176 if (ret < 0)
177 return ret;
178
c7a913cd
EL
179 list_for_each_entry(t, &nf_loggers_l[*pos], list[*pos]) {
180 ret = seq_printf(s, "%s", t->name);
6440fe05 181 if (ret < 0)
c7a913cd 182 return ret;
c7a913cd
EL
183 if (&t->list[*pos] != nf_loggers_l[*pos].prev) {
184 ret = seq_printf(s, ",");
6440fe05 185 if (ret < 0)
c7a913cd 186 return ret;
c7a913cd
EL
187 }
188 }
601e68e1 189
c7a913cd 190 return seq_printf(s, ")\n");
f6ebe77f
HW
191}
192
56b3d975 193static const struct seq_operations nflog_seq_ops = {
f6ebe77f
HW
194 .start = seq_start,
195 .next = seq_next,
196 .stop = seq_stop,
197 .show = seq_show,
198};
199
200static int nflog_open(struct inode *inode, struct file *file)
201{
202 return seq_open(file, &nflog_seq_ops);
203}
204
da7071d7 205static const struct file_operations nflog_file_ops = {
f6ebe77f
HW
206 .owner = THIS_MODULE,
207 .open = nflog_open,
208 .read = seq_read,
209 .llseek = seq_lseek,
210 .release = seq_release,
211};
212
17625274 213
f6ebe77f
HW
214#endif /* PROC_FS */
215
17625274 216#ifdef CONFIG_SYSCTL
24955619 217static struct ctl_path nf_log_sysctl_path[] = {
f8572d8f
EB
218 { .procname = "net", },
219 { .procname = "netfilter", },
220 { .procname = "nf_log", },
17625274
EL
221 { }
222};
223
224static char nf_log_sysctl_fnames[NFPROTO_NUMPROTO-NFPROTO_UNSPEC][3];
225static struct ctl_table nf_log_sysctl_table[NFPROTO_NUMPROTO+1];
226static struct ctl_table_header *nf_log_dir_header;
f6ebe77f 227
8d65af78 228static int nf_log_proc_dostring(ctl_table *table, int write,
24955619 229 void __user *buffer, size_t *lenp, loff_t *ppos)
17625274
EL
230{
231 const struct nf_logger *logger;
24955619
PM
232 char buf[NFLOGGER_NAME_LEN];
233 size_t size = *lenp;
17625274
EL
234 int r = 0;
235 int tindex = (unsigned long)table->extra1;
236
237 if (write) {
24955619
PM
238 if (size > sizeof(buf))
239 size = sizeof(buf);
240 if (copy_from_user(buf, buffer, size))
241 return -EFAULT;
242
243 if (!strcmp(buf, "NONE")) {
17625274
EL
244 nf_log_unbind_pf(tindex);
245 return 0;
246 }
247 mutex_lock(&nf_log_mutex);
24955619 248 logger = __find_logger(tindex, buf);
17625274
EL
249 if (logger == NULL) {
250 mutex_unlock(&nf_log_mutex);
251 return -ENOENT;
252 }
253 rcu_assign_pointer(nf_loggers[tindex], logger);
254 mutex_unlock(&nf_log_mutex);
255 } else {
266d07cb 256 mutex_lock(&nf_log_mutex);
0e60ebe0
ED
257 logger = rcu_dereference_protected(nf_loggers[tindex],
258 lockdep_is_held(&nf_log_mutex));
17625274
EL
259 if (!logger)
260 table->data = "NONE";
261 else
262 table->data = logger->name;
8d65af78 263 r = proc_dostring(table, write, buffer, lenp, ppos);
266d07cb 264 mutex_unlock(&nf_log_mutex);
17625274
EL
265 }
266
267 return r;
268}
269
270static __init int netfilter_log_sysctl_init(void)
f6ebe77f 271{
ca735b3a 272 int i;
17625274
EL
273
274 for (i = NFPROTO_UNSPEC; i < NFPROTO_NUMPROTO; i++) {
275 snprintf(nf_log_sysctl_fnames[i-NFPROTO_UNSPEC], 3, "%d", i);
17625274
EL
276 nf_log_sysctl_table[i].procname =
277 nf_log_sysctl_fnames[i-NFPROTO_UNSPEC];
278 nf_log_sysctl_table[i].data = NULL;
279 nf_log_sysctl_table[i].maxlen =
280 NFLOGGER_NAME_LEN * sizeof(char);
281 nf_log_sysctl_table[i].mode = 0644;
282 nf_log_sysctl_table[i].proc_handler = nf_log_proc_dostring;
283 nf_log_sysctl_table[i].extra1 = (void *)(unsigned long) i;
284 }
285
286 nf_log_dir_header = register_sysctl_paths(nf_log_sysctl_path,
287 nf_log_sysctl_table);
288 if (!nf_log_dir_header)
289 return -ENOMEM;
290
291 return 0;
292}
293#else
294static __init int netfilter_log_sysctl_init(void)
295{
296 return 0;
297}
298#endif /* CONFIG_SYSCTL */
299
300int __init netfilter_log_init(void)
301{
302 int i, r;
f6ebe77f 303#ifdef CONFIG_PROC_FS
8eeee8b1
DL
304 if (!proc_create("nf_log", S_IRUGO,
305 proc_net_netfilter, &nflog_file_ops))
f6ebe77f 306 return -1;
62243927 307#endif
ca735b3a 308
17625274
EL
309 /* Errors will trigger panic, unroll on error is unnecessary. */
310 r = netfilter_log_sysctl_init();
311 if (r < 0)
312 return r;
313
ca735b3a
EL
314 for (i = NFPROTO_UNSPEC; i < NFPROTO_NUMPROTO; i++)
315 INIT_LIST_HEAD(&(nf_loggers_l[i]));
316
f6ebe77f
HW
317 return 0;
318}
This page took 0.465708 seconds and 5 git commands to generate.