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