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.
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>
22 #include <linux/sysctl.h>
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
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 switch (l3proto
->l3proto
) {
46 seq_printf(s
, "src=%pI4 dst=%pI4 ",
47 &tuple
->src
.u3
.ip
, &tuple
->dst
.u3
.ip
);
50 seq_printf(s
, "src=%pI6 dst=%pI6 ",
51 tuple
->src
.u3
.ip6
, tuple
->dst
.u3
.ip6
);
57 switch (l4proto
->l4proto
) {
59 seq_printf(s
, "type=%u code=%u id=%u ",
60 tuple
->dst
.u
.icmp
.type
,
61 tuple
->dst
.u
.icmp
.code
,
62 ntohs(tuple
->src
.u
.icmp
.id
));
65 seq_printf(s
, "sport=%hu dport=%hu ",
66 ntohs(tuple
->src
.u
.tcp
.port
),
67 ntohs(tuple
->dst
.u
.tcp
.port
));
69 case IPPROTO_UDPLITE
: /* fallthrough */
71 seq_printf(s
, "sport=%hu dport=%hu ",
72 ntohs(tuple
->src
.u
.udp
.port
),
73 ntohs(tuple
->dst
.u
.udp
.port
));
77 seq_printf(s
, "sport=%hu dport=%hu ",
78 ntohs(tuple
->src
.u
.dccp
.port
),
79 ntohs(tuple
->dst
.u
.dccp
.port
));
82 seq_printf(s
, "sport=%hu dport=%hu ",
83 ntohs(tuple
->src
.u
.sctp
.port
),
84 ntohs(tuple
->dst
.u
.sctp
.port
));
87 seq_printf(s
, "type=%u code=%u id=%u ",
88 tuple
->dst
.u
.icmp
.type
,
89 tuple
->dst
.u
.icmp
.code
,
90 ntohs(tuple
->src
.u
.icmp
.id
));
93 seq_printf(s
, "srckey=0x%x dstkey=0x%x ",
94 ntohs(tuple
->src
.u
.gre
.key
),
95 ntohs(tuple
->dst
.u
.gre
.key
));
101 EXPORT_SYMBOL_GPL(print_tuple
);
103 struct ct_iter_state
{
104 struct seq_net_private p
;
105 struct hlist_nulls_head
*hash
;
106 unsigned int htable_size
;
111 static struct hlist_nulls_node
*ct_get_first(struct seq_file
*seq
)
113 struct ct_iter_state
*st
= seq
->private;
114 struct hlist_nulls_node
*n
;
117 st
->bucket
< st
->htable_size
;
120 hlist_nulls_first_rcu(&st
->hash
[st
->bucket
]));
127 static struct hlist_nulls_node
*ct_get_next(struct seq_file
*seq
,
128 struct hlist_nulls_node
*head
)
130 struct ct_iter_state
*st
= seq
->private;
132 head
= rcu_dereference(hlist_nulls_next_rcu(head
));
133 while (is_a_nulls(head
)) {
134 if (likely(get_nulls_value(head
) == st
->bucket
)) {
135 if (++st
->bucket
>= st
->htable_size
)
138 head
= rcu_dereference(
139 hlist_nulls_first_rcu(&st
->hash
[st
->bucket
]));
144 static struct hlist_nulls_node
*ct_get_idx(struct seq_file
*seq
, loff_t pos
)
146 struct hlist_nulls_node
*head
= ct_get_first(seq
);
149 while (pos
&& (head
= ct_get_next(seq
, head
)))
151 return pos
? NULL
: head
;
154 static void *ct_seq_start(struct seq_file
*seq
, loff_t
*pos
)
157 struct ct_iter_state
*st
= seq
->private;
159 st
->time_now
= ktime_get_real_ns();
162 nf_conntrack_get_ht(&st
->hash
, &st
->htable_size
);
163 return ct_get_idx(seq
, *pos
);
166 static void *ct_seq_next(struct seq_file
*s
, void *v
, loff_t
*pos
)
169 return ct_get_next(s
, v
);
172 static void ct_seq_stop(struct seq_file
*s
, void *v
)
178 #ifdef CONFIG_NF_CONNTRACK_SECMARK
179 static void ct_show_secctx(struct seq_file
*s
, const struct nf_conn
*ct
)
185 ret
= security_secid_to_secctx(ct
->secmark
, &secctx
, &len
);
189 seq_printf(s
, "secctx=%s ", secctx
);
191 security_release_secctx(secctx
, len
);
194 static inline void ct_show_secctx(struct seq_file
*s
, const struct nf_conn
*ct
)
199 #ifdef CONFIG_NF_CONNTRACK_ZONES
200 static void ct_show_zone(struct seq_file
*s
, const struct nf_conn
*ct
,
203 const struct nf_conntrack_zone
*zone
= nf_ct_zone(ct
);
205 if (zone
->dir
!= dir
)
208 case NF_CT_DEFAULT_ZONE_DIR
:
209 seq_printf(s
, "zone=%u ", zone
->id
);
211 case NF_CT_ZONE_DIR_ORIG
:
212 seq_printf(s
, "zone-orig=%u ", zone
->id
);
214 case NF_CT_ZONE_DIR_REPL
:
215 seq_printf(s
, "zone-reply=%u ", zone
->id
);
222 static inline void ct_show_zone(struct seq_file
*s
, const struct nf_conn
*ct
,
228 #ifdef CONFIG_NF_CONNTRACK_TIMESTAMP
229 static void ct_show_delta_time(struct seq_file
*s
, const struct nf_conn
*ct
)
231 struct ct_iter_state
*st
= s
->private;
232 struct nf_conn_tstamp
*tstamp
;
235 tstamp
= nf_conn_tstamp_find(ct
);
237 delta_time
= st
->time_now
- tstamp
->start
;
239 delta_time
= div_s64(delta_time
, NSEC_PER_SEC
);
243 seq_printf(s
, "delta-time=%llu ",
244 (unsigned long long)delta_time
);
250 ct_show_delta_time(struct seq_file
*s
, const struct nf_conn
*ct
)
255 static const char* l3proto_name(u16 proto
)
258 case AF_INET
: return "ipv4";
259 case AF_INET6
: return "ipv6";
265 static const char* l4proto_name(u16 proto
)
268 case IPPROTO_ICMP
: return "icmp";
269 case IPPROTO_TCP
: return "tcp";
270 case IPPROTO_UDP
: return "udp";
271 case IPPROTO_DCCP
: return "dccp";
272 case IPPROTO_GRE
: return "gre";
273 case IPPROTO_SCTP
: return "sctp";
274 case IPPROTO_UDPLITE
: return "udplite";
280 /* return 0 on success, 1 in case of error */
281 static int ct_seq_show(struct seq_file
*s
, void *v
)
283 struct nf_conntrack_tuple_hash
*hash
= v
;
284 struct nf_conn
*ct
= nf_ct_tuplehash_to_ctrack(hash
);
285 const struct nf_conntrack_l3proto
*l3proto
;
286 const struct nf_conntrack_l4proto
*l4proto
;
287 struct net
*net
= seq_file_net(s
);
291 if (unlikely(!atomic_inc_not_zero(&ct
->ct_general
.use
)))
294 if (nf_ct_should_gc(ct
)) {
299 /* we only want to print DIR_ORIGINAL */
300 if (NF_CT_DIRECTION(hash
))
303 if (!net_eq(nf_ct_net(ct
), net
))
306 l3proto
= __nf_ct_l3proto_find(nf_ct_l3num(ct
));
308 l4proto
= __nf_ct_l4proto_find(nf_ct_l3num(ct
), nf_ct_protonum(ct
));
312 seq_printf(s
, "%-8s %u %-8s %u %ld ",
313 l3proto_name(l3proto
->l3proto
), nf_ct_l3num(ct
),
314 l4proto_name(l4proto
->l4proto
), nf_ct_protonum(ct
),
315 nf_ct_expires(ct
) / HZ
);
317 if (l4proto
->print_conntrack
)
318 l4proto
->print_conntrack(s
, ct
);
320 print_tuple(s
, &ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
,
323 ct_show_zone(s
, ct
, NF_CT_ZONE_DIR_ORIG
);
325 if (seq_has_overflowed(s
))
328 if (seq_print_acct(s
, ct
, IP_CT_DIR_ORIGINAL
))
331 if (!(test_bit(IPS_SEEN_REPLY_BIT
, &ct
->status
)))
332 seq_puts(s
, "[UNREPLIED] ");
334 print_tuple(s
, &ct
->tuplehash
[IP_CT_DIR_REPLY
].tuple
,
337 ct_show_zone(s
, ct
, NF_CT_ZONE_DIR_REPL
);
339 if (seq_print_acct(s
, ct
, IP_CT_DIR_REPLY
))
342 if (test_bit(IPS_ASSURED_BIT
, &ct
->status
))
343 seq_puts(s
, "[ASSURED] ");
345 if (seq_has_overflowed(s
))
348 #if defined(CONFIG_NF_CONNTRACK_MARK)
349 seq_printf(s
, "mark=%u ", ct
->mark
);
352 ct_show_secctx(s
, ct
);
353 ct_show_zone(s
, ct
, NF_CT_DEFAULT_ZONE_DIR
);
354 ct_show_delta_time(s
, ct
);
356 seq_printf(s
, "use=%u\n", atomic_read(&ct
->ct_general
.use
));
358 if (seq_has_overflowed(s
))
367 static const struct seq_operations ct_seq_ops
= {
368 .start
= ct_seq_start
,
374 static int ct_open(struct inode
*inode
, struct file
*file
)
376 return seq_open_net(inode
, file
, &ct_seq_ops
,
377 sizeof(struct ct_iter_state
));
380 static const struct file_operations ct_file_ops
= {
381 .owner
= THIS_MODULE
,
385 .release
= seq_release_net
,
388 static void *ct_cpu_seq_start(struct seq_file
*seq
, loff_t
*pos
)
390 struct net
*net
= seq_file_net(seq
);
394 return SEQ_START_TOKEN
;
396 for (cpu
= *pos
-1; cpu
< nr_cpu_ids
; ++cpu
) {
397 if (!cpu_possible(cpu
))
400 return per_cpu_ptr(net
->ct
.stat
, cpu
);
406 static void *ct_cpu_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
408 struct net
*net
= seq_file_net(seq
);
411 for (cpu
= *pos
; cpu
< nr_cpu_ids
; ++cpu
) {
412 if (!cpu_possible(cpu
))
415 return per_cpu_ptr(net
->ct
.stat
, cpu
);
421 static void ct_cpu_seq_stop(struct seq_file
*seq
, void *v
)
425 static int ct_cpu_seq_show(struct seq_file
*seq
, void *v
)
427 struct net
*net
= seq_file_net(seq
);
428 unsigned int nr_conntracks
= atomic_read(&net
->ct
.count
);
429 const struct ip_conntrack_stat
*st
= v
;
431 if (v
== SEQ_START_TOKEN
) {
432 seq_puts(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");
436 seq_printf(seq
, "%08x %08x %08x %08x %08x %08x %08x %08x "
437 "%08x %08x %08x %08x %08x %08x %08x %08x %08x\n",
460 static const struct seq_operations ct_cpu_seq_ops
= {
461 .start
= ct_cpu_seq_start
,
462 .next
= ct_cpu_seq_next
,
463 .stop
= ct_cpu_seq_stop
,
464 .show
= ct_cpu_seq_show
,
467 static int ct_cpu_seq_open(struct inode
*inode
, struct file
*file
)
469 return seq_open_net(inode
, file
, &ct_cpu_seq_ops
,
470 sizeof(struct seq_net_private
));
473 static const struct file_operations ct_cpu_seq_fops
= {
474 .owner
= THIS_MODULE
,
475 .open
= ct_cpu_seq_open
,
478 .release
= seq_release_net
,
481 static int nf_conntrack_standalone_init_proc(struct net
*net
)
483 struct proc_dir_entry
*pde
;
487 pde
= proc_create("nf_conntrack", 0440, net
->proc_net
, &ct_file_ops
);
489 goto out_nf_conntrack
;
491 root_uid
= make_kuid(net
->user_ns
, 0);
492 root_gid
= make_kgid(net
->user_ns
, 0);
493 if (uid_valid(root_uid
) && gid_valid(root_gid
))
494 proc_set_user(pde
, root_uid
, root_gid
);
496 pde
= proc_create("nf_conntrack", S_IRUGO
, net
->proc_net_stat
,
499 goto out_stat_nf_conntrack
;
502 out_stat_nf_conntrack
:
503 remove_proc_entry("nf_conntrack", net
->proc_net
);
508 static void nf_conntrack_standalone_fini_proc(struct net
*net
)
510 remove_proc_entry("nf_conntrack", net
->proc_net_stat
);
511 remove_proc_entry("nf_conntrack", net
->proc_net
);
514 static int nf_conntrack_standalone_init_proc(struct net
*net
)
519 static void nf_conntrack_standalone_fini_proc(struct net
*net
)
522 #endif /* CONFIG_NF_CONNTRACK_PROCFS */
527 /* Log invalid packets of a given protocol */
528 static int log_invalid_proto_min __read_mostly
;
529 static int log_invalid_proto_max __read_mostly
= 255;
531 /* size the user *wants to set */
532 static unsigned int nf_conntrack_htable_size_user __read_mostly
;
535 nf_conntrack_hash_sysctl(struct ctl_table
*table
, int write
,
536 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
540 ret
= proc_dointvec(table
, write
, buffer
, lenp
, ppos
);
541 if (ret
< 0 || !write
)
544 /* update ret, we might not be able to satisfy request */
545 ret
= nf_conntrack_hash_resize(nf_conntrack_htable_size_user
);
547 /* update it to the actual value used by conntrack */
548 nf_conntrack_htable_size_user
= nf_conntrack_htable_size
;
552 static struct ctl_table_header
*nf_ct_netfilter_header
;
554 static struct ctl_table nf_ct_sysctl_table
[] = {
556 .procname
= "nf_conntrack_max",
557 .data
= &nf_conntrack_max
,
558 .maxlen
= sizeof(int),
560 .proc_handler
= proc_dointvec
,
563 .procname
= "nf_conntrack_count",
564 .data
= &init_net
.ct
.count
,
565 .maxlen
= sizeof(int),
567 .proc_handler
= proc_dointvec
,
570 .procname
= "nf_conntrack_buckets",
571 .data
= &nf_conntrack_htable_size_user
,
572 .maxlen
= sizeof(unsigned int),
574 .proc_handler
= nf_conntrack_hash_sysctl
,
577 .procname
= "nf_conntrack_checksum",
578 .data
= &init_net
.ct
.sysctl_checksum
,
579 .maxlen
= sizeof(unsigned int),
581 .proc_handler
= proc_dointvec
,
584 .procname
= "nf_conntrack_log_invalid",
585 .data
= &init_net
.ct
.sysctl_log_invalid
,
586 .maxlen
= sizeof(unsigned int),
588 .proc_handler
= proc_dointvec_minmax
,
589 .extra1
= &log_invalid_proto_min
,
590 .extra2
= &log_invalid_proto_max
,
593 .procname
= "nf_conntrack_expect_max",
594 .data
= &nf_ct_expect_max
,
595 .maxlen
= sizeof(int),
597 .proc_handler
= proc_dointvec
,
602 static struct ctl_table nf_ct_netfilter_table
[] = {
604 .procname
= "nf_conntrack_max",
605 .data
= &nf_conntrack_max
,
606 .maxlen
= sizeof(int),
608 .proc_handler
= proc_dointvec
,
613 static int nf_conntrack_standalone_init_sysctl(struct net
*net
)
615 struct ctl_table
*table
;
617 table
= kmemdup(nf_ct_sysctl_table
, sizeof(nf_ct_sysctl_table
),
622 table
[1].data
= &net
->ct
.count
;
623 table
[3].data
= &net
->ct
.sysctl_checksum
;
624 table
[4].data
= &net
->ct
.sysctl_log_invalid
;
626 /* Don't export sysctls to unprivileged users */
627 if (net
->user_ns
!= &init_user_ns
)
628 table
[0].procname
= NULL
;
630 if (!net_eq(&init_net
, net
))
631 table
[2].mode
= 0444;
633 net
->ct
.sysctl_header
= register_net_sysctl(net
, "net/netfilter", table
);
634 if (!net
->ct
.sysctl_header
)
635 goto out_unregister_netfilter
;
639 out_unregister_netfilter
:
645 static void nf_conntrack_standalone_fini_sysctl(struct net
*net
)
647 struct ctl_table
*table
;
649 table
= net
->ct
.sysctl_header
->ctl_table_arg
;
650 unregister_net_sysctl_table(net
->ct
.sysctl_header
);
654 static int nf_conntrack_standalone_init_sysctl(struct net
*net
)
659 static void nf_conntrack_standalone_fini_sysctl(struct net
*net
)
662 #endif /* CONFIG_SYSCTL */
664 static int nf_conntrack_pernet_init(struct net
*net
)
668 ret
= nf_conntrack_init_net(net
);
672 ret
= nf_conntrack_standalone_init_proc(net
);
676 net
->ct
.sysctl_checksum
= 1;
677 net
->ct
.sysctl_log_invalid
= 0;
678 ret
= nf_conntrack_standalone_init_sysctl(net
);
685 nf_conntrack_standalone_fini_proc(net
);
687 nf_conntrack_cleanup_net(net
);
692 static void nf_conntrack_pernet_exit(struct list_head
*net_exit_list
)
696 list_for_each_entry(net
, net_exit_list
, exit_list
) {
697 nf_conntrack_standalone_fini_sysctl(net
);
698 nf_conntrack_standalone_fini_proc(net
);
700 nf_conntrack_cleanup_net_list(net_exit_list
);
703 static struct pernet_operations nf_conntrack_net_ops
= {
704 .init
= nf_conntrack_pernet_init
,
705 .exit_batch
= nf_conntrack_pernet_exit
,
708 static int __init
nf_conntrack_standalone_init(void)
710 int ret
= nf_conntrack_init_start();
714 BUILD_BUG_ON(SKB_NFCT_PTRMASK
!= NFCT_PTRMASK
);
715 BUILD_BUG_ON(NFCT_INFOMASK
<= IP_CT_NUMBER
);
718 nf_ct_netfilter_header
=
719 register_net_sysctl(&init_net
, "net", nf_ct_netfilter_table
);
720 if (!nf_ct_netfilter_header
) {
721 pr_err("nf_conntrack: can't register to sysctl.\n");
726 nf_conntrack_htable_size_user
= nf_conntrack_htable_size
;
729 ret
= register_pernet_subsys(&nf_conntrack_net_ops
);
733 nf_conntrack_init_end();
738 unregister_net_sysctl_table(nf_ct_netfilter_header
);
741 nf_conntrack_cleanup_end();
746 static void __exit
nf_conntrack_standalone_fini(void)
748 nf_conntrack_cleanup_start();
749 unregister_pernet_subsys(&nf_conntrack_net_ops
);
751 unregister_net_sysctl_table(nf_ct_netfilter_header
);
753 nf_conntrack_cleanup_end();
756 module_init(nf_conntrack_standalone_init
);
757 module_exit(nf_conntrack_standalone_fini
);
759 /* Some modules need us, but don't depend directly on any symbol.
760 They should call this. */
761 void need_conntrack(void)
764 EXPORT_SYMBOL_GPL(need_conntrack
);