Merge branch 'for-3.13' of git://git.kernel.org/pub/scm/linux/kernel/git/tj/libata
[linux-2.6.git] / net / netfilter / nf_conntrack_standalone.c
blobf641751dba9dc467b4207716bdb0ac69208747f8
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 int
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 return l3proto->print_tuple(s, tuple) || l4proto->print_tuple(s, tuple);
46 EXPORT_SYMBOL_GPL(print_tuple);
48 struct ct_iter_state {
49 struct seq_net_private p;
50 unsigned int bucket;
51 u_int64_t time_now;
54 static struct hlist_nulls_node *ct_get_first(struct seq_file *seq)
56 struct net *net = seq_file_net(seq);
57 struct ct_iter_state *st = seq->private;
58 struct hlist_nulls_node *n;
60 for (st->bucket = 0;
61 st->bucket < net->ct.htable_size;
62 st->bucket++) {
63 n = rcu_dereference(hlist_nulls_first_rcu(&net->ct.hash[st->bucket]));
64 if (!is_a_nulls(n))
65 return n;
67 return NULL;
70 static struct hlist_nulls_node *ct_get_next(struct seq_file *seq,
71 struct hlist_nulls_node *head)
73 struct net *net = seq_file_net(seq);
74 struct ct_iter_state *st = seq->private;
76 head = rcu_dereference(hlist_nulls_next_rcu(head));
77 while (is_a_nulls(head)) {
78 if (likely(get_nulls_value(head) == st->bucket)) {
79 if (++st->bucket >= net->ct.htable_size)
80 return NULL;
82 head = rcu_dereference(
83 hlist_nulls_first_rcu(
84 &net->ct.hash[st->bucket]));
86 return head;
89 static struct hlist_nulls_node *ct_get_idx(struct seq_file *seq, loff_t pos)
91 struct hlist_nulls_node *head = ct_get_first(seq);
93 if (head)
94 while (pos && (head = ct_get_next(seq, head)))
95 pos--;
96 return pos ? NULL : head;
99 static void *ct_seq_start(struct seq_file *seq, loff_t *pos)
100 __acquires(RCU)
102 struct ct_iter_state *st = seq->private;
104 st->time_now = ktime_to_ns(ktime_get_real());
105 rcu_read_lock();
106 return ct_get_idx(seq, *pos);
109 static void *ct_seq_next(struct seq_file *s, void *v, loff_t *pos)
111 (*pos)++;
112 return ct_get_next(s, v);
115 static void ct_seq_stop(struct seq_file *s, void *v)
116 __releases(RCU)
118 rcu_read_unlock();
121 #ifdef CONFIG_NF_CONNTRACK_SECMARK
122 static int ct_show_secctx(struct seq_file *s, const struct nf_conn *ct)
124 int ret;
125 u32 len;
126 char *secctx;
128 ret = security_secid_to_secctx(ct->secmark, &secctx, &len);
129 if (ret)
130 return 0;
132 ret = seq_printf(s, "secctx=%s ", secctx);
134 security_release_secctx(secctx, len);
135 return ret;
137 #else
138 static inline int ct_show_secctx(struct seq_file *s, const struct nf_conn *ct)
140 return 0;
142 #endif
144 #ifdef CONFIG_NF_CONNTRACK_TIMESTAMP
145 static int ct_show_delta_time(struct seq_file *s, const struct nf_conn *ct)
147 struct ct_iter_state *st = s->private;
148 struct nf_conn_tstamp *tstamp;
149 s64 delta_time;
151 tstamp = nf_conn_tstamp_find(ct);
152 if (tstamp) {
153 delta_time = st->time_now - tstamp->start;
154 if (delta_time > 0)
155 delta_time = div_s64(delta_time, NSEC_PER_SEC);
156 else
157 delta_time = 0;
159 return seq_printf(s, "delta-time=%llu ",
160 (unsigned long long)delta_time);
162 return 0;
164 #else
165 static inline int
166 ct_show_delta_time(struct seq_file *s, const struct nf_conn *ct)
168 return 0;
170 #endif
172 /* return 0 on success, 1 in case of error */
173 static int ct_seq_show(struct seq_file *s, void *v)
175 struct nf_conntrack_tuple_hash *hash = v;
176 struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(hash);
177 const struct nf_conntrack_l3proto *l3proto;
178 const struct nf_conntrack_l4proto *l4proto;
179 int ret = 0;
181 NF_CT_ASSERT(ct);
182 if (unlikely(!atomic_inc_not_zero(&ct->ct_general.use)))
183 return 0;
185 /* we only want to print DIR_ORIGINAL */
186 if (NF_CT_DIRECTION(hash))
187 goto release;
189 l3proto = __nf_ct_l3proto_find(nf_ct_l3num(ct));
190 NF_CT_ASSERT(l3proto);
191 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
192 NF_CT_ASSERT(l4proto);
194 ret = -ENOSPC;
195 if (seq_printf(s, "%-8s %u %-8s %u %ld ",
196 l3proto->name, nf_ct_l3num(ct),
197 l4proto->name, nf_ct_protonum(ct),
198 timer_pending(&ct->timeout)
199 ? (long)(ct->timeout.expires - jiffies)/HZ : 0) != 0)
200 goto release;
202 if (l4proto->print_conntrack && l4proto->print_conntrack(s, ct))
203 goto release;
205 if (print_tuple(s, &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
206 l3proto, l4proto))
207 goto release;
209 if (seq_print_acct(s, ct, IP_CT_DIR_ORIGINAL))
210 goto release;
212 if (!(test_bit(IPS_SEEN_REPLY_BIT, &ct->status)))
213 if (seq_printf(s, "[UNREPLIED] "))
214 goto release;
216 if (print_tuple(s, &ct->tuplehash[IP_CT_DIR_REPLY].tuple,
217 l3proto, l4proto))
218 goto release;
220 if (seq_print_acct(s, ct, IP_CT_DIR_REPLY))
221 goto release;
223 if (test_bit(IPS_ASSURED_BIT, &ct->status))
224 if (seq_printf(s, "[ASSURED] "))
225 goto release;
227 #if defined(CONFIG_NF_CONNTRACK_MARK)
228 if (seq_printf(s, "mark=%u ", ct->mark))
229 goto release;
230 #endif
232 if (ct_show_secctx(s, ct))
233 goto release;
235 #ifdef CONFIG_NF_CONNTRACK_ZONES
236 if (seq_printf(s, "zone=%u ", nf_ct_zone(ct)))
237 goto release;
238 #endif
240 if (ct_show_delta_time(s, ct))
241 goto release;
243 if (seq_printf(s, "use=%u\n", atomic_read(&ct->ct_general.use)))
244 goto release;
246 ret = 0;
247 release:
248 nf_ct_put(ct);
249 return ret;
252 static const struct seq_operations ct_seq_ops = {
253 .start = ct_seq_start,
254 .next = ct_seq_next,
255 .stop = ct_seq_stop,
256 .show = ct_seq_show
259 static int ct_open(struct inode *inode, struct file *file)
261 return seq_open_net(inode, file, &ct_seq_ops,
262 sizeof(struct ct_iter_state));
265 static const struct file_operations ct_file_ops = {
266 .owner = THIS_MODULE,
267 .open = ct_open,
268 .read = seq_read,
269 .llseek = seq_lseek,
270 .release = seq_release_net,
273 static void *ct_cpu_seq_start(struct seq_file *seq, loff_t *pos)
275 struct net *net = seq_file_net(seq);
276 int cpu;
278 if (*pos == 0)
279 return SEQ_START_TOKEN;
281 for (cpu = *pos-1; cpu < nr_cpu_ids; ++cpu) {
282 if (!cpu_possible(cpu))
283 continue;
284 *pos = cpu + 1;
285 return per_cpu_ptr(net->ct.stat, cpu);
288 return NULL;
291 static void *ct_cpu_seq_next(struct seq_file *seq, void *v, loff_t *pos)
293 struct net *net = seq_file_net(seq);
294 int cpu;
296 for (cpu = *pos; cpu < nr_cpu_ids; ++cpu) {
297 if (!cpu_possible(cpu))
298 continue;
299 *pos = cpu + 1;
300 return per_cpu_ptr(net->ct.stat, cpu);
303 return NULL;
306 static void ct_cpu_seq_stop(struct seq_file *seq, void *v)
310 static int ct_cpu_seq_show(struct seq_file *seq, void *v)
312 struct net *net = seq_file_net(seq);
313 unsigned int nr_conntracks = atomic_read(&net->ct.count);
314 const struct ip_conntrack_stat *st = v;
316 if (v == SEQ_START_TOKEN) {
317 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");
318 return 0;
321 seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x %08x "
322 "%08x %08x %08x %08x %08x %08x %08x %08x %08x\n",
323 nr_conntracks,
324 st->searched,
325 st->found,
326 st->new,
327 st->invalid,
328 st->ignore,
329 st->delete,
330 st->delete_list,
331 st->insert,
332 st->insert_failed,
333 st->drop,
334 st->early_drop,
335 st->error,
337 st->expect_new,
338 st->expect_create,
339 st->expect_delete,
340 st->search_restart
342 return 0;
345 static const struct seq_operations ct_cpu_seq_ops = {
346 .start = ct_cpu_seq_start,
347 .next = ct_cpu_seq_next,
348 .stop = ct_cpu_seq_stop,
349 .show = ct_cpu_seq_show,
352 static int ct_cpu_seq_open(struct inode *inode, struct file *file)
354 return seq_open_net(inode, file, &ct_cpu_seq_ops,
355 sizeof(struct seq_net_private));
358 static const struct file_operations ct_cpu_seq_fops = {
359 .owner = THIS_MODULE,
360 .open = ct_cpu_seq_open,
361 .read = seq_read,
362 .llseek = seq_lseek,
363 .release = seq_release_net,
366 static int nf_conntrack_standalone_init_proc(struct net *net)
368 struct proc_dir_entry *pde;
370 pde = proc_create("nf_conntrack", 0440, net->proc_net, &ct_file_ops);
371 if (!pde)
372 goto out_nf_conntrack;
374 pde = proc_create("nf_conntrack", S_IRUGO, net->proc_net_stat,
375 &ct_cpu_seq_fops);
376 if (!pde)
377 goto out_stat_nf_conntrack;
378 return 0;
380 out_stat_nf_conntrack:
381 remove_proc_entry("nf_conntrack", net->proc_net);
382 out_nf_conntrack:
383 return -ENOMEM;
386 static void nf_conntrack_standalone_fini_proc(struct net *net)
388 remove_proc_entry("nf_conntrack", net->proc_net_stat);
389 remove_proc_entry("nf_conntrack", net->proc_net);
391 #else
392 static int nf_conntrack_standalone_init_proc(struct net *net)
394 return 0;
397 static void nf_conntrack_standalone_fini_proc(struct net *net)
400 #endif /* CONFIG_NF_CONNTRACK_PROCFS */
402 /* Sysctl support */
404 #ifdef CONFIG_SYSCTL
405 /* Log invalid packets of a given protocol */
406 static int log_invalid_proto_min = 0;
407 static int log_invalid_proto_max = 255;
409 static struct ctl_table_header *nf_ct_netfilter_header;
411 static struct ctl_table nf_ct_sysctl_table[] = {
413 .procname = "nf_conntrack_max",
414 .data = &nf_conntrack_max,
415 .maxlen = sizeof(int),
416 .mode = 0644,
417 .proc_handler = proc_dointvec,
420 .procname = "nf_conntrack_count",
421 .data = &init_net.ct.count,
422 .maxlen = sizeof(int),
423 .mode = 0444,
424 .proc_handler = proc_dointvec,
427 .procname = "nf_conntrack_buckets",
428 .data = &init_net.ct.htable_size,
429 .maxlen = sizeof(unsigned int),
430 .mode = 0444,
431 .proc_handler = proc_dointvec,
434 .procname = "nf_conntrack_checksum",
435 .data = &init_net.ct.sysctl_checksum,
436 .maxlen = sizeof(unsigned int),
437 .mode = 0644,
438 .proc_handler = proc_dointvec,
441 .procname = "nf_conntrack_log_invalid",
442 .data = &init_net.ct.sysctl_log_invalid,
443 .maxlen = sizeof(unsigned int),
444 .mode = 0644,
445 .proc_handler = proc_dointvec_minmax,
446 .extra1 = &log_invalid_proto_min,
447 .extra2 = &log_invalid_proto_max,
450 .procname = "nf_conntrack_expect_max",
451 .data = &nf_ct_expect_max,
452 .maxlen = sizeof(int),
453 .mode = 0644,
454 .proc_handler = proc_dointvec,
459 #define NET_NF_CONNTRACK_MAX 2089
461 static struct ctl_table nf_ct_netfilter_table[] = {
463 .procname = "nf_conntrack_max",
464 .data = &nf_conntrack_max,
465 .maxlen = sizeof(int),
466 .mode = 0644,
467 .proc_handler = proc_dointvec,
472 static int nf_conntrack_standalone_init_sysctl(struct net *net)
474 struct ctl_table *table;
476 table = kmemdup(nf_ct_sysctl_table, sizeof(nf_ct_sysctl_table),
477 GFP_KERNEL);
478 if (!table)
479 goto out_kmemdup;
481 table[1].data = &net->ct.count;
482 table[2].data = &net->ct.htable_size;
483 table[3].data = &net->ct.sysctl_checksum;
484 table[4].data = &net->ct.sysctl_log_invalid;
486 /* Don't export sysctls to unprivileged users */
487 if (net->user_ns != &init_user_ns)
488 table[0].procname = NULL;
490 net->ct.sysctl_header = register_net_sysctl(net, "net/netfilter", table);
491 if (!net->ct.sysctl_header)
492 goto out_unregister_netfilter;
494 return 0;
496 out_unregister_netfilter:
497 kfree(table);
498 out_kmemdup:
499 return -ENOMEM;
502 static void nf_conntrack_standalone_fini_sysctl(struct net *net)
504 struct ctl_table *table;
506 table = net->ct.sysctl_header->ctl_table_arg;
507 unregister_net_sysctl_table(net->ct.sysctl_header);
508 kfree(table);
510 #else
511 static int nf_conntrack_standalone_init_sysctl(struct net *net)
513 return 0;
516 static void nf_conntrack_standalone_fini_sysctl(struct net *net)
519 #endif /* CONFIG_SYSCTL */
521 static int nf_conntrack_pernet_init(struct net *net)
523 int ret;
525 ret = nf_conntrack_init_net(net);
526 if (ret < 0)
527 goto out_init;
529 ret = nf_conntrack_standalone_init_proc(net);
530 if (ret < 0)
531 goto out_proc;
533 net->ct.sysctl_checksum = 1;
534 net->ct.sysctl_log_invalid = 0;
535 ret = nf_conntrack_standalone_init_sysctl(net);
536 if (ret < 0)
537 goto out_sysctl;
539 return 0;
541 out_sysctl:
542 nf_conntrack_standalone_fini_proc(net);
543 out_proc:
544 nf_conntrack_cleanup_net(net);
545 out_init:
546 return ret;
549 static void nf_conntrack_pernet_exit(struct list_head *net_exit_list)
551 struct net *net;
553 list_for_each_entry(net, net_exit_list, exit_list) {
554 nf_conntrack_standalone_fini_sysctl(net);
555 nf_conntrack_standalone_fini_proc(net);
557 nf_conntrack_cleanup_net_list(net_exit_list);
560 static struct pernet_operations nf_conntrack_net_ops = {
561 .init = nf_conntrack_pernet_init,
562 .exit_batch = nf_conntrack_pernet_exit,
565 static int __init nf_conntrack_standalone_init(void)
567 int ret = nf_conntrack_init_start();
568 if (ret < 0)
569 goto out_start;
571 #ifdef CONFIG_SYSCTL
572 nf_ct_netfilter_header =
573 register_net_sysctl(&init_net, "net", nf_ct_netfilter_table);
574 if (!nf_ct_netfilter_header) {
575 pr_err("nf_conntrack: can't register to sysctl.\n");
576 ret = -ENOMEM;
577 goto out_sysctl;
579 #endif
581 ret = register_pernet_subsys(&nf_conntrack_net_ops);
582 if (ret < 0)
583 goto out_pernet;
585 nf_conntrack_init_end();
586 return 0;
588 out_pernet:
589 #ifdef CONFIG_SYSCTL
590 unregister_net_sysctl_table(nf_ct_netfilter_header);
591 out_sysctl:
592 #endif
593 nf_conntrack_cleanup_end();
594 out_start:
595 return ret;
598 static void __exit nf_conntrack_standalone_fini(void)
600 nf_conntrack_cleanup_start();
601 unregister_pernet_subsys(&nf_conntrack_net_ops);
602 #ifdef CONFIG_SYSCTL
603 unregister_net_sysctl_table(nf_ct_netfilter_header);
604 #endif
605 nf_conntrack_cleanup_end();
608 module_init(nf_conntrack_standalone_init);
609 module_exit(nf_conntrack_standalone_fini);
611 /* Some modules need us, but don't depend directly on any symbol.
612 They should call this. */
613 void need_conntrack(void)
616 EXPORT_SYMBOL_GPL(need_conntrack);