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.
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>
21 #include <linux/sysctl.h>
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");
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
;
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
;
57 st
->bucket
< net
->ct
.htable_size
;
59 n
= rcu_dereference(net
->ct
.hash
[st
->bucket
].first
);
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
)
78 head
= rcu_dereference(net
->ct
.hash
[st
->bucket
].first
);
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
);
88 while (pos
&& (head
= ct_get_next(seq
, head
)))
90 return pos
? NULL
: head
;
93 static void *ct_seq_start(struct seq_file
*seq
, loff_t
*pos
)
97 return ct_get_idx(seq
, *pos
);
100 static void *ct_seq_next(struct seq_file
*s
, void *v
, loff_t
*pos
)
103 return ct_get_next(s
, v
);
106 static void ct_seq_stop(struct seq_file
*s
, void *v
)
112 #ifdef CONFIG_NF_CONNTRACK_SECMARK
113 static int ct_show_secctx(struct seq_file
*s
, const struct nf_conn
*ct
)
119 ret
= security_secid_to_secctx(ct
->secmark
, &secctx
, &len
);
123 ret
= seq_printf(s
, "secctx=%s ", secctx
);
125 security_release_secctx(secctx
, len
);
129 static inline int ct_show_secctx(struct seq_file
*s
, const struct nf_conn
*ct
)
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
;
145 if (unlikely(!atomic_inc_not_zero(&ct
->ct_general
.use
)))
148 /* we only want to print DIR_ORIGINAL */
149 if (NF_CT_DIRECTION(hash
))
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
);
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)
165 if (l4proto
->print_conntrack
&& l4proto
->print_conntrack(s
, ct
))
168 if (print_tuple(s
, &ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
,
172 if (seq_print_acct(s
, ct
, IP_CT_DIR_ORIGINAL
))
175 if (!(test_bit(IPS_SEEN_REPLY_BIT
, &ct
->status
)))
176 if (seq_printf(s
, "[UNREPLIED] "))
179 if (print_tuple(s
, &ct
->tuplehash
[IP_CT_DIR_REPLY
].tuple
,
183 if (seq_print_acct(s
, ct
, IP_CT_DIR_REPLY
))
186 if (test_bit(IPS_ASSURED_BIT
, &ct
->status
))
187 if (seq_printf(s
, "[ASSURED] "))
190 #if defined(CONFIG_NF_CONNTRACK_MARK)
191 if (seq_printf(s
, "mark=%u ", ct
->mark
))
195 if (ct_show_secctx(s
, ct
))
198 #ifdef CONFIG_NF_CONNTRACK_ZONES
199 if (seq_printf(s
, "zone=%u ", nf_ct_zone(ct
)))
203 if (seq_printf(s
, "use=%u\n", atomic_read(&ct
->ct_general
.use
)))
212 static const struct seq_operations ct_seq_ops
= {
213 .start
= ct_seq_start
,
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
,
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
);
239 return SEQ_START_TOKEN
;
241 for (cpu
= *pos
-1; cpu
< nr_cpu_ids
; ++cpu
) {
242 if (!cpu_possible(cpu
))
245 return per_cpu_ptr(net
->ct
.stat
, cpu
);
251 static void *ct_cpu_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
253 struct net
*net
= seq_file_net(seq
);
256 for (cpu
= *pos
; cpu
< nr_cpu_ids
; ++cpu
) {
257 if (!cpu_possible(cpu
))
260 return per_cpu_ptr(net
->ct
.stat
, cpu
);
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");
281 seq_printf(seq
, "%08x %08x %08x %08x %08x %08x %08x %08x "
282 "%08x %08x %08x %08x %08x %08x %08x %08x %08x\n",
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
,
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
);
332 goto out_nf_conntrack
;
334 pde
= proc_create("nf_conntrack", S_IRUGO
, net
->proc_net_stat
,
337 goto out_stat_nf_conntrack
;
340 out_stat_nf_conntrack
:
341 proc_net_remove(net
, "nf_conntrack");
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");
352 static int nf_conntrack_standalone_init_proc(struct net
*net
)
357 static void nf_conntrack_standalone_fini_proc(struct net
*net
)
360 #endif /* CONFIG_PROC_FS */
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),
377 .proc_handler
= proc_dointvec
,
380 .procname
= "nf_conntrack_count",
381 .data
= &init_net
.ct
.count
,
382 .maxlen
= sizeof(int),
384 .proc_handler
= proc_dointvec
,
387 .procname
= "nf_conntrack_buckets",
388 .data
= &init_net
.ct
.htable_size
,
389 .maxlen
= sizeof(unsigned int),
391 .proc_handler
= proc_dointvec
,
394 .procname
= "nf_conntrack_checksum",
395 .data
= &init_net
.ct
.sysctl_checksum
,
396 .maxlen
= sizeof(unsigned int),
398 .proc_handler
= proc_dointvec
,
401 .procname
= "nf_conntrack_log_invalid",
402 .data
= &init_net
.ct
.sysctl_log_invalid
,
403 .maxlen
= sizeof(unsigned int),
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),
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),
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
)
448 table
= kmemdup(nf_ct_sysctl_table
, sizeof(nf_ct_sysctl_table
),
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
;
465 out_unregister_netfilter
:
468 if (net_eq(net
, &init_net
))
469 unregister_sysctl_table(nf_ct_netfilter_header
);
471 printk(KERN_ERR
"nf_conntrack: can't register to sysctl.\n");
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
);
486 static int nf_conntrack_standalone_init_sysctl(struct net
*net
)
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
)
500 ret
= nf_conntrack_init(net
);
503 ret
= nf_conntrack_standalone_init_proc(net
);
506 net
->ct
.sysctl_checksum
= 1;
507 net
->ct
.sysctl_log_invalid
= 0;
508 ret
= nf_conntrack_standalone_init_sysctl(net
);
514 nf_conntrack_standalone_fini_proc(net
);
516 nf_conntrack_cleanup(net
);
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
);