]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - net/netfilter/nf_conntrack_standalone.c
Merge branch 'for-linus' of master.kernel.org:/pub/scm/linux/kernel/git/jikos/hid
[mirror_ubuntu-artful-kernel.git] / net / netfilter / nf_conntrack_standalone.c
1 /* (C) 1999-2001 Paul `Rusty' Russell
2 * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 */
8
9 #include <linux/types.h>
10 #include <linux/netfilter.h>
11 #include <linux/module.h>
12 #include <linux/skbuff.h>
13 #include <linux/proc_fs.h>
14 #include <linux/seq_file.h>
15 #include <linux/percpu.h>
16 #include <linux/netdevice.h>
17 #ifdef CONFIG_SYSCTL
18 #include <linux/sysctl.h>
19 #endif
20
21 #include <net/netfilter/nf_conntrack.h>
22 #include <net/netfilter/nf_conntrack_core.h>
23 #include <net/netfilter/nf_conntrack_l3proto.h>
24 #include <net/netfilter/nf_conntrack_l4proto.h>
25 #include <net/netfilter/nf_conntrack_expect.h>
26 #include <net/netfilter/nf_conntrack_helper.h>
27
28 #if 0
29 #define DEBUGP printk
30 #else
31 #define DEBUGP(format, args...)
32 #endif
33
34 MODULE_LICENSE("GPL");
35
36 #ifdef CONFIG_PROC_FS
37 int
38 print_tuple(struct seq_file *s, const struct nf_conntrack_tuple *tuple,
39 struct nf_conntrack_l3proto *l3proto,
40 struct nf_conntrack_l4proto *l4proto)
41 {
42 return l3proto->print_tuple(s, tuple) || l4proto->print_tuple(s, tuple);
43 }
44 EXPORT_SYMBOL_GPL(print_tuple);
45
46 #ifdef CONFIG_NF_CT_ACCT
47 static unsigned int
48 seq_print_counters(struct seq_file *s,
49 const struct ip_conntrack_counter *counter)
50 {
51 return seq_printf(s, "packets=%llu bytes=%llu ",
52 (unsigned long long)counter->packets,
53 (unsigned long long)counter->bytes);
54 }
55 #else
56 #define seq_print_counters(x, y) 0
57 #endif
58
59 struct ct_iter_state {
60 unsigned int bucket;
61 };
62
63 static struct list_head *ct_get_first(struct seq_file *seq)
64 {
65 struct ct_iter_state *st = seq->private;
66
67 for (st->bucket = 0;
68 st->bucket < nf_conntrack_htable_size;
69 st->bucket++) {
70 if (!list_empty(&nf_conntrack_hash[st->bucket]))
71 return nf_conntrack_hash[st->bucket].next;
72 }
73 return NULL;
74 }
75
76 static struct list_head *ct_get_next(struct seq_file *seq, struct list_head *head)
77 {
78 struct ct_iter_state *st = seq->private;
79
80 head = head->next;
81 while (head == &nf_conntrack_hash[st->bucket]) {
82 if (++st->bucket >= nf_conntrack_htable_size)
83 return NULL;
84 head = nf_conntrack_hash[st->bucket].next;
85 }
86 return head;
87 }
88
89 static struct list_head *ct_get_idx(struct seq_file *seq, loff_t pos)
90 {
91 struct list_head *head = ct_get_first(seq);
92
93 if (head)
94 while (pos && (head = ct_get_next(seq, head)))
95 pos--;
96 return pos ? NULL : head;
97 }
98
99 static void *ct_seq_start(struct seq_file *seq, loff_t *pos)
100 {
101 read_lock_bh(&nf_conntrack_lock);
102 return ct_get_idx(seq, *pos);
103 }
104
105 static void *ct_seq_next(struct seq_file *s, void *v, loff_t *pos)
106 {
107 (*pos)++;
108 return ct_get_next(s, v);
109 }
110
111 static void ct_seq_stop(struct seq_file *s, void *v)
112 {
113 read_unlock_bh(&nf_conntrack_lock);
114 }
115
116 /* return 0 on success, 1 in case of error */
117 static int ct_seq_show(struct seq_file *s, void *v)
118 {
119 const struct nf_conntrack_tuple_hash *hash = v;
120 const struct nf_conn *conntrack = nf_ct_tuplehash_to_ctrack(hash);
121 struct nf_conntrack_l3proto *l3proto;
122 struct nf_conntrack_l4proto *l4proto;
123
124 NF_CT_ASSERT(conntrack);
125
126 /* we only want to print DIR_ORIGINAL */
127 if (NF_CT_DIRECTION(hash))
128 return 0;
129
130 l3proto = __nf_ct_l3proto_find(conntrack->tuplehash[IP_CT_DIR_ORIGINAL]
131 .tuple.src.l3num);
132
133 NF_CT_ASSERT(l3proto);
134 l4proto = __nf_ct_l4proto_find(conntrack->tuplehash[IP_CT_DIR_ORIGINAL]
135 .tuple.src.l3num,
136 conntrack->tuplehash[IP_CT_DIR_ORIGINAL]
137 .tuple.dst.protonum);
138 NF_CT_ASSERT(l4proto);
139
140 if (seq_printf(s, "%-8s %u %-8s %u %ld ",
141 l3proto->name,
142 conntrack->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num,
143 l4proto->name,
144 conntrack->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum,
145 timer_pending(&conntrack->timeout)
146 ? (long)(conntrack->timeout.expires - jiffies)/HZ : 0) != 0)
147 return -ENOSPC;
148
149 if (l3proto->print_conntrack(s, conntrack))
150 return -ENOSPC;
151
152 if (l4proto->print_conntrack(s, conntrack))
153 return -ENOSPC;
154
155 if (print_tuple(s, &conntrack->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
156 l3proto, l4proto))
157 return -ENOSPC;
158
159 if (seq_print_counters(s, &conntrack->counters[IP_CT_DIR_ORIGINAL]))
160 return -ENOSPC;
161
162 if (!(test_bit(IPS_SEEN_REPLY_BIT, &conntrack->status)))
163 if (seq_printf(s, "[UNREPLIED] "))
164 return -ENOSPC;
165
166 if (print_tuple(s, &conntrack->tuplehash[IP_CT_DIR_REPLY].tuple,
167 l3proto, l4proto))
168 return -ENOSPC;
169
170 if (seq_print_counters(s, &conntrack->counters[IP_CT_DIR_REPLY]))
171 return -ENOSPC;
172
173 if (test_bit(IPS_ASSURED_BIT, &conntrack->status))
174 if (seq_printf(s, "[ASSURED] "))
175 return -ENOSPC;
176
177 #if defined(CONFIG_NF_CONNTRACK_MARK)
178 if (seq_printf(s, "mark=%u ", conntrack->mark))
179 return -ENOSPC;
180 #endif
181
182 #ifdef CONFIG_NF_CONNTRACK_SECMARK
183 if (seq_printf(s, "secmark=%u ", conntrack->secmark))
184 return -ENOSPC;
185 #endif
186
187 if (seq_printf(s, "use=%u\n", atomic_read(&conntrack->ct_general.use)))
188 return -ENOSPC;
189
190 return 0;
191 }
192
193 static struct seq_operations ct_seq_ops = {
194 .start = ct_seq_start,
195 .next = ct_seq_next,
196 .stop = ct_seq_stop,
197 .show = ct_seq_show
198 };
199
200 static int ct_open(struct inode *inode, struct file *file)
201 {
202 struct seq_file *seq;
203 struct ct_iter_state *st;
204 int ret;
205
206 st = kmalloc(sizeof(struct ct_iter_state), GFP_KERNEL);
207 if (st == NULL)
208 return -ENOMEM;
209 ret = seq_open(file, &ct_seq_ops);
210 if (ret)
211 goto out_free;
212 seq = file->private_data;
213 seq->private = st;
214 memset(st, 0, sizeof(struct ct_iter_state));
215 return ret;
216 out_free:
217 kfree(st);
218 return ret;
219 }
220
221 static const struct file_operations ct_file_ops = {
222 .owner = THIS_MODULE,
223 .open = ct_open,
224 .read = seq_read,
225 .llseek = seq_lseek,
226 .release = seq_release_private,
227 };
228
229 static void *ct_cpu_seq_start(struct seq_file *seq, loff_t *pos)
230 {
231 int cpu;
232
233 if (*pos == 0)
234 return SEQ_START_TOKEN;
235
236 for (cpu = *pos-1; cpu < NR_CPUS; ++cpu) {
237 if (!cpu_possible(cpu))
238 continue;
239 *pos = cpu + 1;
240 return &per_cpu(nf_conntrack_stat, cpu);
241 }
242
243 return NULL;
244 }
245
246 static void *ct_cpu_seq_next(struct seq_file *seq, void *v, loff_t *pos)
247 {
248 int cpu;
249
250 for (cpu = *pos; cpu < NR_CPUS; ++cpu) {
251 if (!cpu_possible(cpu))
252 continue;
253 *pos = cpu + 1;
254 return &per_cpu(nf_conntrack_stat, cpu);
255 }
256
257 return NULL;
258 }
259
260 static void ct_cpu_seq_stop(struct seq_file *seq, void *v)
261 {
262 }
263
264 static int ct_cpu_seq_show(struct seq_file *seq, void *v)
265 {
266 unsigned int nr_conntracks = atomic_read(&nf_conntrack_count);
267 struct ip_conntrack_stat *st = v;
268
269 if (v == SEQ_START_TOKEN) {
270 seq_printf(seq, "entries searched found new invalid ignore delete delete_list insert insert_failed drop early_drop icmp_error expect_new expect_create expect_delete\n");
271 return 0;
272 }
273
274 seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x %08x "
275 "%08x %08x %08x %08x %08x %08x %08x %08x \n",
276 nr_conntracks,
277 st->searched,
278 st->found,
279 st->new,
280 st->invalid,
281 st->ignore,
282 st->delete,
283 st->delete_list,
284 st->insert,
285 st->insert_failed,
286 st->drop,
287 st->early_drop,
288 st->error,
289
290 st->expect_new,
291 st->expect_create,
292 st->expect_delete
293 );
294 return 0;
295 }
296
297 static struct seq_operations ct_cpu_seq_ops = {
298 .start = ct_cpu_seq_start,
299 .next = ct_cpu_seq_next,
300 .stop = ct_cpu_seq_stop,
301 .show = ct_cpu_seq_show,
302 };
303
304 static int ct_cpu_seq_open(struct inode *inode, struct file *file)
305 {
306 return seq_open(file, &ct_cpu_seq_ops);
307 }
308
309 static const struct file_operations ct_cpu_seq_fops = {
310 .owner = THIS_MODULE,
311 .open = ct_cpu_seq_open,
312 .read = seq_read,
313 .llseek = seq_lseek,
314 .release = seq_release_private,
315 };
316 #endif /* CONFIG_PROC_FS */
317
318 /* Sysctl support */
319
320 int nf_conntrack_checksum __read_mostly = 1;
321 EXPORT_SYMBOL_GPL(nf_conntrack_checksum);
322
323 #ifdef CONFIG_SYSCTL
324 /* Log invalid packets of a given protocol */
325 static int log_invalid_proto_min = 0;
326 static int log_invalid_proto_max = 255;
327
328 static struct ctl_table_header *nf_ct_sysctl_header;
329
330 static ctl_table nf_ct_sysctl_table[] = {
331 {
332 .ctl_name = NET_NF_CONNTRACK_MAX,
333 .procname = "nf_conntrack_max",
334 .data = &nf_conntrack_max,
335 .maxlen = sizeof(int),
336 .mode = 0644,
337 .proc_handler = &proc_dointvec,
338 },
339 {
340 .ctl_name = NET_NF_CONNTRACK_COUNT,
341 .procname = "nf_conntrack_count",
342 .data = &nf_conntrack_count,
343 .maxlen = sizeof(int),
344 .mode = 0444,
345 .proc_handler = &proc_dointvec,
346 },
347 {
348 .ctl_name = NET_NF_CONNTRACK_BUCKETS,
349 .procname = "nf_conntrack_buckets",
350 .data = &nf_conntrack_htable_size,
351 .maxlen = sizeof(unsigned int),
352 .mode = 0444,
353 .proc_handler = &proc_dointvec,
354 },
355 {
356 .ctl_name = NET_NF_CONNTRACK_CHECKSUM,
357 .procname = "nf_conntrack_checksum",
358 .data = &nf_conntrack_checksum,
359 .maxlen = sizeof(unsigned int),
360 .mode = 0644,
361 .proc_handler = &proc_dointvec,
362 },
363 {
364 .ctl_name = NET_NF_CONNTRACK_LOG_INVALID,
365 .procname = "nf_conntrack_log_invalid",
366 .data = &nf_ct_log_invalid,
367 .maxlen = sizeof(unsigned int),
368 .mode = 0644,
369 .proc_handler = &proc_dointvec_minmax,
370 .strategy = &sysctl_intvec,
371 .extra1 = &log_invalid_proto_min,
372 .extra2 = &log_invalid_proto_max,
373 },
374
375 { .ctl_name = 0 }
376 };
377
378 #define NET_NF_CONNTRACK_MAX 2089
379
380 static ctl_table nf_ct_netfilter_table[] = {
381 {
382 .ctl_name = NET_NETFILTER,
383 .procname = "netfilter",
384 .mode = 0555,
385 .child = nf_ct_sysctl_table,
386 },
387 {
388 .ctl_name = NET_NF_CONNTRACK_MAX,
389 .procname = "nf_conntrack_max",
390 .data = &nf_conntrack_max,
391 .maxlen = sizeof(int),
392 .mode = 0644,
393 .proc_handler = &proc_dointvec,
394 },
395 { .ctl_name = 0 }
396 };
397
398 static ctl_table nf_ct_net_table[] = {
399 {
400 .ctl_name = CTL_NET,
401 .procname = "net",
402 .mode = 0555,
403 .child = nf_ct_netfilter_table,
404 },
405 { .ctl_name = 0 }
406 };
407 EXPORT_SYMBOL_GPL(nf_ct_log_invalid);
408 #endif /* CONFIG_SYSCTL */
409
410 static int __init nf_conntrack_standalone_init(void)
411 {
412 #ifdef CONFIG_PROC_FS
413 struct proc_dir_entry *proc, *proc_exp, *proc_stat;
414 #endif
415 int ret = 0;
416
417 ret = nf_conntrack_init();
418 if (ret < 0)
419 return ret;
420
421 #ifdef CONFIG_PROC_FS
422 proc = proc_net_fops_create("nf_conntrack", 0440, &ct_file_ops);
423 if (!proc) goto cleanup_init;
424
425 proc_exp = proc_net_fops_create("nf_conntrack_expect", 0440,
426 &exp_file_ops);
427 if (!proc_exp) goto cleanup_proc;
428
429 proc_stat = create_proc_entry("nf_conntrack", S_IRUGO, proc_net_stat);
430 if (!proc_stat)
431 goto cleanup_proc_exp;
432
433 proc_stat->proc_fops = &ct_cpu_seq_fops;
434 proc_stat->owner = THIS_MODULE;
435 #endif
436 #ifdef CONFIG_SYSCTL
437 nf_ct_sysctl_header = register_sysctl_table(nf_ct_net_table);
438 if (nf_ct_sysctl_header == NULL) {
439 printk("nf_conntrack: can't register to sysctl.\n");
440 ret = -ENOMEM;
441 goto cleanup_proc_stat;
442 }
443 #endif
444 return ret;
445
446 #ifdef CONFIG_SYSCTL
447 cleanup_proc_stat:
448 #endif
449 #ifdef CONFIG_PROC_FS
450 remove_proc_entry("nf_conntrack", proc_net_stat);
451 cleanup_proc_exp:
452 proc_net_remove("nf_conntrack_expect");
453 cleanup_proc:
454 proc_net_remove("nf_conntrack");
455 cleanup_init:
456 #endif /* CNFIG_PROC_FS */
457 nf_conntrack_cleanup();
458 return ret;
459 }
460
461 static void __exit nf_conntrack_standalone_fini(void)
462 {
463 #ifdef CONFIG_SYSCTL
464 unregister_sysctl_table(nf_ct_sysctl_header);
465 #endif
466 #ifdef CONFIG_PROC_FS
467 remove_proc_entry("nf_conntrack", proc_net_stat);
468 proc_net_remove("nf_conntrack_expect");
469 proc_net_remove("nf_conntrack");
470 #endif /* CNFIG_PROC_FS */
471 nf_conntrack_cleanup();
472 }
473
474 module_init(nf_conntrack_standalone_init);
475 module_exit(nf_conntrack_standalone_fini);
476
477 /* Some modules need us, but don't depend directly on any symbol.
478 They should call this. */
479 void need_conntrack(void)
480 {
481 }
482 EXPORT_SYMBOL_GPL(need_conntrack);