ocfs2: Make the left masklogs compat.
[taoma-kernel.git] / net / netfilter / nf_conntrack_standalone.c
blobb4d7f0f24b27e9534a97851e7830714c8135a7bf
1 /* (C) 1999-2001 Paul `Rusty' Russell
2 * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
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 */
9 #include <linux/types.h>
10 #include <linux/netfilter.h>
11 #include <linux/slab.h>
12 #include <linux/module.h>
13 #include <linux/skbuff.h>
14 #include <linux/proc_fs.h>
15 #include <linux/seq_file.h>
16 #include <linux/percpu.h>
17 #include <linux/netdevice.h>
18 #include <linux/security.h>
19 #include <net/net_namespace.h>
20 #ifdef CONFIG_SYSCTL
21 #include <linux/sysctl.h>
22 #endif
24 #include <net/netfilter/nf_conntrack.h>
25 #include <net/netfilter/nf_conntrack_core.h>
26 #include <net/netfilter/nf_conntrack_l3proto.h>
27 #include <net/netfilter/nf_conntrack_l4proto.h>
28 #include <net/netfilter/nf_conntrack_expect.h>
29 #include <net/netfilter/nf_conntrack_helper.h>
30 #include <net/netfilter/nf_conntrack_acct.h>
31 #include <net/netfilter/nf_conntrack_zones.h>
33 MODULE_LICENSE("GPL");
35 #ifdef CONFIG_PROC_FS
36 int
37 print_tuple(struct seq_file *s, const struct nf_conntrack_tuple *tuple,
38 const struct nf_conntrack_l3proto *l3proto,
39 const struct nf_conntrack_l4proto *l4proto)
41 return l3proto->print_tuple(s, tuple) || l4proto->print_tuple(s, tuple);
43 EXPORT_SYMBOL_GPL(print_tuple);
45 struct ct_iter_state {
46 struct seq_net_private p;
47 unsigned int bucket;
50 static struct hlist_nulls_node *ct_get_first(struct seq_file *seq)
52 struct net *net = seq_file_net(seq);
53 struct ct_iter_state *st = seq->private;
54 struct hlist_nulls_node *n;
56 for (st->bucket = 0;
57 st->bucket < net->ct.htable_size;
58 st->bucket++) {
59 n = rcu_dereference(net->ct.hash[st->bucket].first);
60 if (!is_a_nulls(n))
61 return n;
63 return NULL;
66 static struct hlist_nulls_node *ct_get_next(struct seq_file *seq,
67 struct hlist_nulls_node *head)
69 struct net *net = seq_file_net(seq);
70 struct ct_iter_state *st = seq->private;
72 head = rcu_dereference(head->next);
73 while (is_a_nulls(head)) {
74 if (likely(get_nulls_value(head) == st->bucket)) {
75 if (++st->bucket >= net->ct.htable_size)
76 return NULL;
78 head = rcu_dereference(net->ct.hash[st->bucket].first);
80 return head;
83 static struct hlist_nulls_node *ct_get_idx(struct seq_file *seq, loff_t pos)
85 struct hlist_nulls_node *head = ct_get_first(seq);
87 if (head)
88 while (pos && (head = ct_get_next(seq, head)))
89 pos--;
90 return pos ? NULL : head;
93 static void *ct_seq_start(struct seq_file *seq, loff_t *pos)
94 __acquires(RCU)
96 rcu_read_lock();
97 return ct_get_idx(seq, *pos);
100 static void *ct_seq_next(struct seq_file *s, void *v, loff_t *pos)
102 (*pos)++;
103 return ct_get_next(s, v);
106 static void ct_seq_stop(struct seq_file *s, void *v)
107 __releases(RCU)
109 rcu_read_unlock();
112 #ifdef CONFIG_NF_CONNTRACK_SECMARK
113 static int ct_show_secctx(struct seq_file *s, const struct nf_conn *ct)
115 int ret;
116 u32 len;
117 char *secctx;
119 ret = security_secid_to_secctx(ct->secmark, &secctx, &len);
120 if (ret)
121 return 0;
123 ret = seq_printf(s, "secctx=%s ", secctx);
125 security_release_secctx(secctx, len);
126 return ret;
128 #else
129 static inline int ct_show_secctx(struct seq_file *s, const struct nf_conn *ct)
131 return 0;
133 #endif
135 /* return 0 on success, 1 in case of error */
136 static int ct_seq_show(struct seq_file *s, void *v)
138 struct nf_conntrack_tuple_hash *hash = v;
139 struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(hash);
140 const struct nf_conntrack_l3proto *l3proto;
141 const struct nf_conntrack_l4proto *l4proto;
142 int ret = 0;
144 NF_CT_ASSERT(ct);
145 if (unlikely(!atomic_inc_not_zero(&ct->ct_general.use)))
146 return 0;
148 /* we only want to print DIR_ORIGINAL */
149 if (NF_CT_DIRECTION(hash))
150 goto release;
152 l3proto = __nf_ct_l3proto_find(nf_ct_l3num(ct));
153 NF_CT_ASSERT(l3proto);
154 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
155 NF_CT_ASSERT(l4proto);
157 ret = -ENOSPC;
158 if (seq_printf(s, "%-8s %u %-8s %u %ld ",
159 l3proto->name, nf_ct_l3num(ct),
160 l4proto->name, nf_ct_protonum(ct),
161 timer_pending(&ct->timeout)
162 ? (long)(ct->timeout.expires - jiffies)/HZ : 0) != 0)
163 goto release;
165 if (l4proto->print_conntrack && l4proto->print_conntrack(s, ct))
166 goto release;
168 if (print_tuple(s, &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
169 l3proto, l4proto))
170 goto release;
172 if (seq_print_acct(s, ct, IP_CT_DIR_ORIGINAL))
173 goto release;
175 if (!(test_bit(IPS_SEEN_REPLY_BIT, &ct->status)))
176 if (seq_printf(s, "[UNREPLIED] "))
177 goto release;
179 if (print_tuple(s, &ct->tuplehash[IP_CT_DIR_REPLY].tuple,
180 l3proto, l4proto))
181 goto release;
183 if (seq_print_acct(s, ct, IP_CT_DIR_REPLY))
184 goto release;
186 if (test_bit(IPS_ASSURED_BIT, &ct->status))
187 if (seq_printf(s, "[ASSURED] "))
188 goto release;
190 #if defined(CONFIG_NF_CONNTRACK_MARK)
191 if (seq_printf(s, "mark=%u ", ct->mark))
192 goto release;
193 #endif
195 if (ct_show_secctx(s, ct))
196 goto release;
198 #ifdef CONFIG_NF_CONNTRACK_ZONES
199 if (seq_printf(s, "zone=%u ", nf_ct_zone(ct)))
200 goto release;
201 #endif
203 if (seq_printf(s, "use=%u\n", atomic_read(&ct->ct_general.use)))
204 goto release;
206 ret = 0;
207 release:
208 nf_ct_put(ct);
209 return 0;
212 static const struct seq_operations ct_seq_ops = {
213 .start = ct_seq_start,
214 .next = ct_seq_next,
215 .stop = ct_seq_stop,
216 .show = ct_seq_show
219 static int ct_open(struct inode *inode, struct file *file)
221 return seq_open_net(inode, file, &ct_seq_ops,
222 sizeof(struct ct_iter_state));
225 static const struct file_operations ct_file_ops = {
226 .owner = THIS_MODULE,
227 .open = ct_open,
228 .read = seq_read,
229 .llseek = seq_lseek,
230 .release = seq_release_net,
233 static void *ct_cpu_seq_start(struct seq_file *seq, loff_t *pos)
235 struct net *net = seq_file_net(seq);
236 int cpu;
238 if (*pos == 0)
239 return SEQ_START_TOKEN;
241 for (cpu = *pos-1; cpu < nr_cpu_ids; ++cpu) {
242 if (!cpu_possible(cpu))
243 continue;
244 *pos = cpu + 1;
245 return per_cpu_ptr(net->ct.stat, cpu);
248 return NULL;
251 static void *ct_cpu_seq_next(struct seq_file *seq, void *v, loff_t *pos)
253 struct net *net = seq_file_net(seq);
254 int cpu;
256 for (cpu = *pos; cpu < nr_cpu_ids; ++cpu) {
257 if (!cpu_possible(cpu))
258 continue;
259 *pos = cpu + 1;
260 return per_cpu_ptr(net->ct.stat, cpu);
263 return NULL;
266 static void ct_cpu_seq_stop(struct seq_file *seq, void *v)
270 static int ct_cpu_seq_show(struct seq_file *seq, void *v)
272 struct net *net = seq_file_net(seq);
273 unsigned int nr_conntracks = atomic_read(&net->ct.count);
274 const struct ip_conntrack_stat *st = v;
276 if (v == SEQ_START_TOKEN) {
277 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 search_restart\n");
278 return 0;
281 seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x %08x "
282 "%08x %08x %08x %08x %08x %08x %08x %08x %08x\n",
283 nr_conntracks,
284 st->searched,
285 st->found,
286 st->new,
287 st->invalid,
288 st->ignore,
289 st->delete,
290 st->delete_list,
291 st->insert,
292 st->insert_failed,
293 st->drop,
294 st->early_drop,
295 st->error,
297 st->expect_new,
298 st->expect_create,
299 st->expect_delete,
300 st->search_restart
302 return 0;
305 static const struct seq_operations ct_cpu_seq_ops = {
306 .start = ct_cpu_seq_start,
307 .next = ct_cpu_seq_next,
308 .stop = ct_cpu_seq_stop,
309 .show = ct_cpu_seq_show,
312 static int ct_cpu_seq_open(struct inode *inode, struct file *file)
314 return seq_open_net(inode, file, &ct_cpu_seq_ops,
315 sizeof(struct seq_net_private));
318 static const struct file_operations ct_cpu_seq_fops = {
319 .owner = THIS_MODULE,
320 .open = ct_cpu_seq_open,
321 .read = seq_read,
322 .llseek = seq_lseek,
323 .release = seq_release_net,
326 static int nf_conntrack_standalone_init_proc(struct net *net)
328 struct proc_dir_entry *pde;
330 pde = proc_net_fops_create(net, "nf_conntrack", 0440, &ct_file_ops);
331 if (!pde)
332 goto out_nf_conntrack;
334 pde = proc_create("nf_conntrack", S_IRUGO, net->proc_net_stat,
335 &ct_cpu_seq_fops);
336 if (!pde)
337 goto out_stat_nf_conntrack;
338 return 0;
340 out_stat_nf_conntrack:
341 proc_net_remove(net, "nf_conntrack");
342 out_nf_conntrack:
343 return -ENOMEM;
346 static void nf_conntrack_standalone_fini_proc(struct net *net)
348 remove_proc_entry("nf_conntrack", net->proc_net_stat);
349 proc_net_remove(net, "nf_conntrack");
351 #else
352 static int nf_conntrack_standalone_init_proc(struct net *net)
354 return 0;
357 static void nf_conntrack_standalone_fini_proc(struct net *net)
360 #endif /* CONFIG_PROC_FS */
362 /* Sysctl support */
364 #ifdef CONFIG_SYSCTL
365 /* Log invalid packets of a given protocol */
366 static int log_invalid_proto_min = 0;
367 static int log_invalid_proto_max = 255;
369 static struct ctl_table_header *nf_ct_netfilter_header;
371 static ctl_table nf_ct_sysctl_table[] = {
373 .procname = "nf_conntrack_max",
374 .data = &nf_conntrack_max,
375 .maxlen = sizeof(int),
376 .mode = 0644,
377 .proc_handler = proc_dointvec,
380 .procname = "nf_conntrack_count",
381 .data = &init_net.ct.count,
382 .maxlen = sizeof(int),
383 .mode = 0444,
384 .proc_handler = proc_dointvec,
387 .procname = "nf_conntrack_buckets",
388 .data = &init_net.ct.htable_size,
389 .maxlen = sizeof(unsigned int),
390 .mode = 0444,
391 .proc_handler = proc_dointvec,
394 .procname = "nf_conntrack_checksum",
395 .data = &init_net.ct.sysctl_checksum,
396 .maxlen = sizeof(unsigned int),
397 .mode = 0644,
398 .proc_handler = proc_dointvec,
401 .procname = "nf_conntrack_log_invalid",
402 .data = &init_net.ct.sysctl_log_invalid,
403 .maxlen = sizeof(unsigned int),
404 .mode = 0644,
405 .proc_handler = proc_dointvec_minmax,
406 .extra1 = &log_invalid_proto_min,
407 .extra2 = &log_invalid_proto_max,
410 .procname = "nf_conntrack_expect_max",
411 .data = &nf_ct_expect_max,
412 .maxlen = sizeof(int),
413 .mode = 0644,
414 .proc_handler = proc_dointvec,
419 #define NET_NF_CONNTRACK_MAX 2089
421 static ctl_table nf_ct_netfilter_table[] = {
423 .procname = "nf_conntrack_max",
424 .data = &nf_conntrack_max,
425 .maxlen = sizeof(int),
426 .mode = 0644,
427 .proc_handler = proc_dointvec,
432 static struct ctl_path nf_ct_path[] = {
433 { .procname = "net", },
437 static int nf_conntrack_standalone_init_sysctl(struct net *net)
439 struct ctl_table *table;
441 if (net_eq(net, &init_net)) {
442 nf_ct_netfilter_header =
443 register_sysctl_paths(nf_ct_path, nf_ct_netfilter_table);
444 if (!nf_ct_netfilter_header)
445 goto out;
448 table = kmemdup(nf_ct_sysctl_table, sizeof(nf_ct_sysctl_table),
449 GFP_KERNEL);
450 if (!table)
451 goto out_kmemdup;
453 table[1].data = &net->ct.count;
454 table[2].data = &net->ct.htable_size;
455 table[3].data = &net->ct.sysctl_checksum;
456 table[4].data = &net->ct.sysctl_log_invalid;
458 net->ct.sysctl_header = register_net_sysctl_table(net,
459 nf_net_netfilter_sysctl_path, table);
460 if (!net->ct.sysctl_header)
461 goto out_unregister_netfilter;
463 return 0;
465 out_unregister_netfilter:
466 kfree(table);
467 out_kmemdup:
468 if (net_eq(net, &init_net))
469 unregister_sysctl_table(nf_ct_netfilter_header);
470 out:
471 printk(KERN_ERR "nf_conntrack: can't register to sysctl.\n");
472 return -ENOMEM;
475 static void nf_conntrack_standalone_fini_sysctl(struct net *net)
477 struct ctl_table *table;
479 if (net_eq(net, &init_net))
480 unregister_sysctl_table(nf_ct_netfilter_header);
481 table = net->ct.sysctl_header->ctl_table_arg;
482 unregister_net_sysctl_table(net->ct.sysctl_header);
483 kfree(table);
485 #else
486 static int nf_conntrack_standalone_init_sysctl(struct net *net)
488 return 0;
491 static void nf_conntrack_standalone_fini_sysctl(struct net *net)
494 #endif /* CONFIG_SYSCTL */
496 static int nf_conntrack_net_init(struct net *net)
498 int ret;
500 ret = nf_conntrack_init(net);
501 if (ret < 0)
502 goto out_init;
503 ret = nf_conntrack_standalone_init_proc(net);
504 if (ret < 0)
505 goto out_proc;
506 net->ct.sysctl_checksum = 1;
507 net->ct.sysctl_log_invalid = 0;
508 ret = nf_conntrack_standalone_init_sysctl(net);
509 if (ret < 0)
510 goto out_sysctl;
511 return 0;
513 out_sysctl:
514 nf_conntrack_standalone_fini_proc(net);
515 out_proc:
516 nf_conntrack_cleanup(net);
517 out_init:
518 return ret;
521 static void nf_conntrack_net_exit(struct net *net)
523 nf_conntrack_standalone_fini_sysctl(net);
524 nf_conntrack_standalone_fini_proc(net);
525 nf_conntrack_cleanup(net);
528 static struct pernet_operations nf_conntrack_net_ops = {
529 .init = nf_conntrack_net_init,
530 .exit = nf_conntrack_net_exit,
533 static int __init nf_conntrack_standalone_init(void)
535 return register_pernet_subsys(&nf_conntrack_net_ops);
538 static void __exit nf_conntrack_standalone_fini(void)
540 unregister_pernet_subsys(&nf_conntrack_net_ops);
543 module_init(nf_conntrack_standalone_init);
544 module_exit(nf_conntrack_standalone_fini);
546 /* Some modules need us, but don't depend directly on any symbol.
547 They should call this. */
548 void need_conntrack(void)
551 EXPORT_SYMBOL_GPL(need_conntrack);