Merge tag 'powerpc-4.1-3' of git://git.kernel.org/pub/scm/linux/kernel/git/mpe/linux
[linux-2.6/btrfs-unstable.git] / net / netfilter / nf_conntrack_standalone.c
blobfc823fa5dcf53794bc8977cb5502d1dac92938e2
1 /* (C) 1999-2001 Paul `Rusty' Russell
2 * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
3 * (C) 2005-2012 Patrick McHardy <kaber@trash.net>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 */
10 #include <linux/types.h>
11 #include <linux/netfilter.h>
12 #include <linux/slab.h>
13 #include <linux/module.h>
14 #include <linux/skbuff.h>
15 #include <linux/proc_fs.h>
16 #include <linux/seq_file.h>
17 #include <linux/percpu.h>
18 #include <linux/netdevice.h>
19 #include <linux/security.h>
20 #include <net/net_namespace.h>
21 #ifdef CONFIG_SYSCTL
22 #include <linux/sysctl.h>
23 #endif
25 #include <net/netfilter/nf_conntrack.h>
26 #include <net/netfilter/nf_conntrack_core.h>
27 #include <net/netfilter/nf_conntrack_l3proto.h>
28 #include <net/netfilter/nf_conntrack_l4proto.h>
29 #include <net/netfilter/nf_conntrack_expect.h>
30 #include <net/netfilter/nf_conntrack_helper.h>
31 #include <net/netfilter/nf_conntrack_acct.h>
32 #include <net/netfilter/nf_conntrack_zones.h>
33 #include <net/netfilter/nf_conntrack_timestamp.h>
34 #include <linux/rculist_nulls.h>
36 MODULE_LICENSE("GPL");
38 #ifdef CONFIG_NF_CONNTRACK_PROCFS
39 void
40 print_tuple(struct seq_file *s, const struct nf_conntrack_tuple *tuple,
41 const struct nf_conntrack_l3proto *l3proto,
42 const struct nf_conntrack_l4proto *l4proto)
44 l3proto->print_tuple(s, tuple);
45 l4proto->print_tuple(s, tuple);
47 EXPORT_SYMBOL_GPL(print_tuple);
49 struct ct_iter_state {
50 struct seq_net_private p;
51 unsigned int bucket;
52 u_int64_t time_now;
55 static struct hlist_nulls_node *ct_get_first(struct seq_file *seq)
57 struct net *net = seq_file_net(seq);
58 struct ct_iter_state *st = seq->private;
59 struct hlist_nulls_node *n;
61 for (st->bucket = 0;
62 st->bucket < net->ct.htable_size;
63 st->bucket++) {
64 n = rcu_dereference(hlist_nulls_first_rcu(&net->ct.hash[st->bucket]));
65 if (!is_a_nulls(n))
66 return n;
68 return NULL;
71 static struct hlist_nulls_node *ct_get_next(struct seq_file *seq,
72 struct hlist_nulls_node *head)
74 struct net *net = seq_file_net(seq);
75 struct ct_iter_state *st = seq->private;
77 head = rcu_dereference(hlist_nulls_next_rcu(head));
78 while (is_a_nulls(head)) {
79 if (likely(get_nulls_value(head) == st->bucket)) {
80 if (++st->bucket >= net->ct.htable_size)
81 return NULL;
83 head = rcu_dereference(
84 hlist_nulls_first_rcu(
85 &net->ct.hash[st->bucket]));
87 return head;
90 static struct hlist_nulls_node *ct_get_idx(struct seq_file *seq, loff_t pos)
92 struct hlist_nulls_node *head = ct_get_first(seq);
94 if (head)
95 while (pos && (head = ct_get_next(seq, head)))
96 pos--;
97 return pos ? NULL : head;
100 static void *ct_seq_start(struct seq_file *seq, loff_t *pos)
101 __acquires(RCU)
103 struct ct_iter_state *st = seq->private;
105 st->time_now = ktime_get_real_ns();
106 rcu_read_lock();
107 return ct_get_idx(seq, *pos);
110 static void *ct_seq_next(struct seq_file *s, void *v, loff_t *pos)
112 (*pos)++;
113 return ct_get_next(s, v);
116 static void ct_seq_stop(struct seq_file *s, void *v)
117 __releases(RCU)
119 rcu_read_unlock();
122 #ifdef CONFIG_NF_CONNTRACK_SECMARK
123 static void ct_show_secctx(struct seq_file *s, const struct nf_conn *ct)
125 int ret;
126 u32 len;
127 char *secctx;
129 ret = security_secid_to_secctx(ct->secmark, &secctx, &len);
130 if (ret)
131 return;
133 seq_printf(s, "secctx=%s ", secctx);
135 security_release_secctx(secctx, len);
137 #else
138 static inline void ct_show_secctx(struct seq_file *s, const struct nf_conn *ct)
141 #endif
143 #ifdef CONFIG_NF_CONNTRACK_TIMESTAMP
144 static void ct_show_delta_time(struct seq_file *s, const struct nf_conn *ct)
146 struct ct_iter_state *st = s->private;
147 struct nf_conn_tstamp *tstamp;
148 s64 delta_time;
150 tstamp = nf_conn_tstamp_find(ct);
151 if (tstamp) {
152 delta_time = st->time_now - tstamp->start;
153 if (delta_time > 0)
154 delta_time = div_s64(delta_time, NSEC_PER_SEC);
155 else
156 delta_time = 0;
158 seq_printf(s, "delta-time=%llu ",
159 (unsigned long long)delta_time);
161 return;
163 #else
164 static inline void
165 ct_show_delta_time(struct seq_file *s, const struct nf_conn *ct)
168 #endif
170 /* return 0 on success, 1 in case of error */
171 static int ct_seq_show(struct seq_file *s, void *v)
173 struct nf_conntrack_tuple_hash *hash = v;
174 struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(hash);
175 const struct nf_conntrack_l3proto *l3proto;
176 const struct nf_conntrack_l4proto *l4proto;
177 int ret = 0;
179 NF_CT_ASSERT(ct);
180 if (unlikely(!atomic_inc_not_zero(&ct->ct_general.use)))
181 return 0;
183 /* we only want to print DIR_ORIGINAL */
184 if (NF_CT_DIRECTION(hash))
185 goto release;
187 l3proto = __nf_ct_l3proto_find(nf_ct_l3num(ct));
188 NF_CT_ASSERT(l3proto);
189 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
190 NF_CT_ASSERT(l4proto);
192 ret = -ENOSPC;
193 seq_printf(s, "%-8s %u %-8s %u %ld ",
194 l3proto->name, nf_ct_l3num(ct),
195 l4proto->name, nf_ct_protonum(ct),
196 timer_pending(&ct->timeout)
197 ? (long)(ct->timeout.expires - jiffies)/HZ : 0);
199 if (l4proto->print_conntrack)
200 l4proto->print_conntrack(s, ct);
202 print_tuple(s, &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
203 l3proto, l4proto);
205 if (seq_has_overflowed(s))
206 goto release;
208 if (seq_print_acct(s, ct, IP_CT_DIR_ORIGINAL))
209 goto release;
211 if (!(test_bit(IPS_SEEN_REPLY_BIT, &ct->status)))
212 seq_printf(s, "[UNREPLIED] ");
214 print_tuple(s, &ct->tuplehash[IP_CT_DIR_REPLY].tuple,
215 l3proto, l4proto);
217 if (seq_print_acct(s, ct, IP_CT_DIR_REPLY))
218 goto release;
220 if (test_bit(IPS_ASSURED_BIT, &ct->status))
221 seq_printf(s, "[ASSURED] ");
223 if (seq_has_overflowed(s))
224 goto release;
226 #if defined(CONFIG_NF_CONNTRACK_MARK)
227 seq_printf(s, "mark=%u ", ct->mark);
228 #endif
230 ct_show_secctx(s, ct);
232 #ifdef CONFIG_NF_CONNTRACK_ZONES
233 seq_printf(s, "zone=%u ", nf_ct_zone(ct));
234 #endif
236 ct_show_delta_time(s, ct);
238 seq_printf(s, "use=%u\n", atomic_read(&ct->ct_general.use));
240 if (seq_has_overflowed(s))
241 goto release;
243 ret = 0;
244 release:
245 nf_ct_put(ct);
246 return ret;
249 static const struct seq_operations ct_seq_ops = {
250 .start = ct_seq_start,
251 .next = ct_seq_next,
252 .stop = ct_seq_stop,
253 .show = ct_seq_show
256 static int ct_open(struct inode *inode, struct file *file)
258 return seq_open_net(inode, file, &ct_seq_ops,
259 sizeof(struct ct_iter_state));
262 static const struct file_operations ct_file_ops = {
263 .owner = THIS_MODULE,
264 .open = ct_open,
265 .read = seq_read,
266 .llseek = seq_lseek,
267 .release = seq_release_net,
270 static void *ct_cpu_seq_start(struct seq_file *seq, loff_t *pos)
272 struct net *net = seq_file_net(seq);
273 int cpu;
275 if (*pos == 0)
276 return SEQ_START_TOKEN;
278 for (cpu = *pos-1; cpu < nr_cpu_ids; ++cpu) {
279 if (!cpu_possible(cpu))
280 continue;
281 *pos = cpu + 1;
282 return per_cpu_ptr(net->ct.stat, cpu);
285 return NULL;
288 static void *ct_cpu_seq_next(struct seq_file *seq, void *v, loff_t *pos)
290 struct net *net = seq_file_net(seq);
291 int cpu;
293 for (cpu = *pos; cpu < nr_cpu_ids; ++cpu) {
294 if (!cpu_possible(cpu))
295 continue;
296 *pos = cpu + 1;
297 return per_cpu_ptr(net->ct.stat, cpu);
300 return NULL;
303 static void ct_cpu_seq_stop(struct seq_file *seq, void *v)
307 static int ct_cpu_seq_show(struct seq_file *seq, void *v)
309 struct net *net = seq_file_net(seq);
310 unsigned int nr_conntracks = atomic_read(&net->ct.count);
311 const struct ip_conntrack_stat *st = v;
313 if (v == SEQ_START_TOKEN) {
314 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");
315 return 0;
318 seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x %08x "
319 "%08x %08x %08x %08x %08x %08x %08x %08x %08x\n",
320 nr_conntracks,
321 st->searched,
322 st->found,
323 st->new,
324 st->invalid,
325 st->ignore,
326 st->delete,
327 st->delete_list,
328 st->insert,
329 st->insert_failed,
330 st->drop,
331 st->early_drop,
332 st->error,
334 st->expect_new,
335 st->expect_create,
336 st->expect_delete,
337 st->search_restart
339 return 0;
342 static const struct seq_operations ct_cpu_seq_ops = {
343 .start = ct_cpu_seq_start,
344 .next = ct_cpu_seq_next,
345 .stop = ct_cpu_seq_stop,
346 .show = ct_cpu_seq_show,
349 static int ct_cpu_seq_open(struct inode *inode, struct file *file)
351 return seq_open_net(inode, file, &ct_cpu_seq_ops,
352 sizeof(struct seq_net_private));
355 static const struct file_operations ct_cpu_seq_fops = {
356 .owner = THIS_MODULE,
357 .open = ct_cpu_seq_open,
358 .read = seq_read,
359 .llseek = seq_lseek,
360 .release = seq_release_net,
363 static int nf_conntrack_standalone_init_proc(struct net *net)
365 struct proc_dir_entry *pde;
367 pde = proc_create("nf_conntrack", 0440, net->proc_net, &ct_file_ops);
368 if (!pde)
369 goto out_nf_conntrack;
371 pde = proc_create("nf_conntrack", S_IRUGO, net->proc_net_stat,
372 &ct_cpu_seq_fops);
373 if (!pde)
374 goto out_stat_nf_conntrack;
375 return 0;
377 out_stat_nf_conntrack:
378 remove_proc_entry("nf_conntrack", net->proc_net);
379 out_nf_conntrack:
380 return -ENOMEM;
383 static void nf_conntrack_standalone_fini_proc(struct net *net)
385 remove_proc_entry("nf_conntrack", net->proc_net_stat);
386 remove_proc_entry("nf_conntrack", net->proc_net);
388 #else
389 static int nf_conntrack_standalone_init_proc(struct net *net)
391 return 0;
394 static void nf_conntrack_standalone_fini_proc(struct net *net)
397 #endif /* CONFIG_NF_CONNTRACK_PROCFS */
399 /* Sysctl support */
401 #ifdef CONFIG_SYSCTL
402 /* Log invalid packets of a given protocol */
403 static int log_invalid_proto_min = 0;
404 static int log_invalid_proto_max = 255;
406 static struct ctl_table_header *nf_ct_netfilter_header;
408 static struct ctl_table nf_ct_sysctl_table[] = {
410 .procname = "nf_conntrack_max",
411 .data = &nf_conntrack_max,
412 .maxlen = sizeof(int),
413 .mode = 0644,
414 .proc_handler = proc_dointvec,
417 .procname = "nf_conntrack_count",
418 .data = &init_net.ct.count,
419 .maxlen = sizeof(int),
420 .mode = 0444,
421 .proc_handler = proc_dointvec,
424 .procname = "nf_conntrack_buckets",
425 .data = &init_net.ct.htable_size,
426 .maxlen = sizeof(unsigned int),
427 .mode = 0444,
428 .proc_handler = proc_dointvec,
431 .procname = "nf_conntrack_checksum",
432 .data = &init_net.ct.sysctl_checksum,
433 .maxlen = sizeof(unsigned int),
434 .mode = 0644,
435 .proc_handler = proc_dointvec,
438 .procname = "nf_conntrack_log_invalid",
439 .data = &init_net.ct.sysctl_log_invalid,
440 .maxlen = sizeof(unsigned int),
441 .mode = 0644,
442 .proc_handler = proc_dointvec_minmax,
443 .extra1 = &log_invalid_proto_min,
444 .extra2 = &log_invalid_proto_max,
447 .procname = "nf_conntrack_expect_max",
448 .data = &nf_ct_expect_max,
449 .maxlen = sizeof(int),
450 .mode = 0644,
451 .proc_handler = proc_dointvec,
456 #define NET_NF_CONNTRACK_MAX 2089
458 static struct ctl_table nf_ct_netfilter_table[] = {
460 .procname = "nf_conntrack_max",
461 .data = &nf_conntrack_max,
462 .maxlen = sizeof(int),
463 .mode = 0644,
464 .proc_handler = proc_dointvec,
469 static int nf_conntrack_standalone_init_sysctl(struct net *net)
471 struct ctl_table *table;
473 table = kmemdup(nf_ct_sysctl_table, sizeof(nf_ct_sysctl_table),
474 GFP_KERNEL);
475 if (!table)
476 goto out_kmemdup;
478 table[1].data = &net->ct.count;
479 table[2].data = &net->ct.htable_size;
480 table[3].data = &net->ct.sysctl_checksum;
481 table[4].data = &net->ct.sysctl_log_invalid;
483 /* Don't export sysctls to unprivileged users */
484 if (net->user_ns != &init_user_ns)
485 table[0].procname = NULL;
487 net->ct.sysctl_header = register_net_sysctl(net, "net/netfilter", table);
488 if (!net->ct.sysctl_header)
489 goto out_unregister_netfilter;
491 return 0;
493 out_unregister_netfilter:
494 kfree(table);
495 out_kmemdup:
496 return -ENOMEM;
499 static void nf_conntrack_standalone_fini_sysctl(struct net *net)
501 struct ctl_table *table;
503 table = net->ct.sysctl_header->ctl_table_arg;
504 unregister_net_sysctl_table(net->ct.sysctl_header);
505 kfree(table);
507 #else
508 static int nf_conntrack_standalone_init_sysctl(struct net *net)
510 return 0;
513 static void nf_conntrack_standalone_fini_sysctl(struct net *net)
516 #endif /* CONFIG_SYSCTL */
518 static int nf_conntrack_pernet_init(struct net *net)
520 int ret;
522 ret = nf_conntrack_init_net(net);
523 if (ret < 0)
524 goto out_init;
526 ret = nf_conntrack_standalone_init_proc(net);
527 if (ret < 0)
528 goto out_proc;
530 net->ct.sysctl_checksum = 1;
531 net->ct.sysctl_log_invalid = 0;
532 ret = nf_conntrack_standalone_init_sysctl(net);
533 if (ret < 0)
534 goto out_sysctl;
536 return 0;
538 out_sysctl:
539 nf_conntrack_standalone_fini_proc(net);
540 out_proc:
541 nf_conntrack_cleanup_net(net);
542 out_init:
543 return ret;
546 static void nf_conntrack_pernet_exit(struct list_head *net_exit_list)
548 struct net *net;
550 list_for_each_entry(net, net_exit_list, exit_list) {
551 nf_conntrack_standalone_fini_sysctl(net);
552 nf_conntrack_standalone_fini_proc(net);
554 nf_conntrack_cleanup_net_list(net_exit_list);
557 static struct pernet_operations nf_conntrack_net_ops = {
558 .init = nf_conntrack_pernet_init,
559 .exit_batch = nf_conntrack_pernet_exit,
562 static int __init nf_conntrack_standalone_init(void)
564 int ret = nf_conntrack_init_start();
565 if (ret < 0)
566 goto out_start;
568 #ifdef CONFIG_SYSCTL
569 nf_ct_netfilter_header =
570 register_net_sysctl(&init_net, "net", nf_ct_netfilter_table);
571 if (!nf_ct_netfilter_header) {
572 pr_err("nf_conntrack: can't register to sysctl.\n");
573 ret = -ENOMEM;
574 goto out_sysctl;
576 #endif
578 ret = register_pernet_subsys(&nf_conntrack_net_ops);
579 if (ret < 0)
580 goto out_pernet;
582 nf_conntrack_init_end();
583 return 0;
585 out_pernet:
586 #ifdef CONFIG_SYSCTL
587 unregister_net_sysctl_table(nf_ct_netfilter_header);
588 out_sysctl:
589 #endif
590 nf_conntrack_cleanup_end();
591 out_start:
592 return ret;
595 static void __exit nf_conntrack_standalone_fini(void)
597 nf_conntrack_cleanup_start();
598 unregister_pernet_subsys(&nf_conntrack_net_ops);
599 #ifdef CONFIG_SYSCTL
600 unregister_net_sysctl_table(nf_ct_netfilter_header);
601 #endif
602 nf_conntrack_cleanup_end();
605 module_init(nf_conntrack_standalone_init);
606 module_exit(nf_conntrack_standalone_fini);
608 /* Some modules need us, but don't depend directly on any symbol.
609 They should call this. */
610 void need_conntrack(void)
613 EXPORT_SYMBOL_GPL(need_conntrack);