1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/types.h>
3 #include <linux/netfilter.h>
4 #include <linux/slab.h>
5 #include <linux/module.h>
6 #include <linux/skbuff.h>
7 #include <linux/proc_fs.h>
8 #include <linux/seq_file.h>
9 #include <linux/percpu.h>
10 #include <linux/netdevice.h>
11 #include <linux/security.h>
12 #include <net/net_namespace.h>
14 #include <linux/sysctl.h>
17 #include <net/netfilter/nf_conntrack.h>
18 #include <net/netfilter/nf_conntrack_core.h>
19 #include <net/netfilter/nf_conntrack_l4proto.h>
20 #include <net/netfilter/nf_conntrack_expect.h>
21 #include <net/netfilter/nf_conntrack_helper.h>
22 #include <net/netfilter/nf_conntrack_acct.h>
23 #include <net/netfilter/nf_conntrack_zones.h>
24 #include <net/netfilter/nf_conntrack_timestamp.h>
25 #include <linux/rculist_nulls.h>
27 static bool enable_hooks __read_mostly
;
28 MODULE_PARM_DESC(enable_hooks
, "Always enable conntrack hooks");
29 module_param(enable_hooks
, bool, 0000);
31 unsigned int nf_conntrack_net_id __read_mostly
;
33 #ifdef CONFIG_NF_CONNTRACK_PROCFS
35 print_tuple(struct seq_file
*s
, const struct nf_conntrack_tuple
*tuple
,
36 const struct nf_conntrack_l4proto
*l4proto
)
38 switch (tuple
->src
.l3num
) {
40 seq_printf(s
, "src=%pI4 dst=%pI4 ",
41 &tuple
->src
.u3
.ip
, &tuple
->dst
.u3
.ip
);
44 seq_printf(s
, "src=%pI6 dst=%pI6 ",
45 tuple
->src
.u3
.ip6
, tuple
->dst
.u3
.ip6
);
51 switch (l4proto
->l4proto
) {
53 seq_printf(s
, "type=%u code=%u id=%u ",
54 tuple
->dst
.u
.icmp
.type
,
55 tuple
->dst
.u
.icmp
.code
,
56 ntohs(tuple
->src
.u
.icmp
.id
));
59 seq_printf(s
, "sport=%hu dport=%hu ",
60 ntohs(tuple
->src
.u
.tcp
.port
),
61 ntohs(tuple
->dst
.u
.tcp
.port
));
63 case IPPROTO_UDPLITE
: /* fallthrough */
65 seq_printf(s
, "sport=%hu dport=%hu ",
66 ntohs(tuple
->src
.u
.udp
.port
),
67 ntohs(tuple
->dst
.u
.udp
.port
));
71 seq_printf(s
, "sport=%hu dport=%hu ",
72 ntohs(tuple
->src
.u
.dccp
.port
),
73 ntohs(tuple
->dst
.u
.dccp
.port
));
76 seq_printf(s
, "sport=%hu dport=%hu ",
77 ntohs(tuple
->src
.u
.sctp
.port
),
78 ntohs(tuple
->dst
.u
.sctp
.port
));
81 seq_printf(s
, "type=%u code=%u id=%u ",
82 tuple
->dst
.u
.icmp
.type
,
83 tuple
->dst
.u
.icmp
.code
,
84 ntohs(tuple
->src
.u
.icmp
.id
));
87 seq_printf(s
, "srckey=0x%x dstkey=0x%x ",
88 ntohs(tuple
->src
.u
.gre
.key
),
89 ntohs(tuple
->dst
.u
.gre
.key
));
95 EXPORT_SYMBOL_GPL(print_tuple
);
97 struct ct_iter_state
{
98 struct seq_net_private p
;
99 struct hlist_nulls_head
*hash
;
100 unsigned int htable_size
;
105 static struct hlist_nulls_node
*ct_get_first(struct seq_file
*seq
)
107 struct ct_iter_state
*st
= seq
->private;
108 struct hlist_nulls_node
*n
;
111 st
->bucket
< st
->htable_size
;
114 hlist_nulls_first_rcu(&st
->hash
[st
->bucket
]));
121 static struct hlist_nulls_node
*ct_get_next(struct seq_file
*seq
,
122 struct hlist_nulls_node
*head
)
124 struct ct_iter_state
*st
= seq
->private;
126 head
= rcu_dereference(hlist_nulls_next_rcu(head
));
127 while (is_a_nulls(head
)) {
128 if (likely(get_nulls_value(head
) == st
->bucket
)) {
129 if (++st
->bucket
>= st
->htable_size
)
132 head
= rcu_dereference(
133 hlist_nulls_first_rcu(&st
->hash
[st
->bucket
]));
138 static struct hlist_nulls_node
*ct_get_idx(struct seq_file
*seq
, loff_t pos
)
140 struct hlist_nulls_node
*head
= ct_get_first(seq
);
143 while (pos
&& (head
= ct_get_next(seq
, head
)))
145 return pos
? NULL
: head
;
148 static void *ct_seq_start(struct seq_file
*seq
, loff_t
*pos
)
151 struct ct_iter_state
*st
= seq
->private;
153 st
->time_now
= ktime_get_real_ns();
156 nf_conntrack_get_ht(&st
->hash
, &st
->htable_size
);
157 return ct_get_idx(seq
, *pos
);
160 static void *ct_seq_next(struct seq_file
*s
, void *v
, loff_t
*pos
)
163 return ct_get_next(s
, v
);
166 static void ct_seq_stop(struct seq_file
*s
, void *v
)
172 #ifdef CONFIG_NF_CONNTRACK_SECMARK
173 static void ct_show_secctx(struct seq_file
*s
, const struct nf_conn
*ct
)
179 ret
= security_secid_to_secctx(ct
->secmark
, &secctx
, &len
);
183 seq_printf(s
, "secctx=%s ", secctx
);
185 security_release_secctx(secctx
, len
);
188 static inline void ct_show_secctx(struct seq_file
*s
, const struct nf_conn
*ct
)
193 #ifdef CONFIG_NF_CONNTRACK_ZONES
194 static void ct_show_zone(struct seq_file
*s
, const struct nf_conn
*ct
,
197 const struct nf_conntrack_zone
*zone
= nf_ct_zone(ct
);
199 if (zone
->dir
!= dir
)
202 case NF_CT_DEFAULT_ZONE_DIR
:
203 seq_printf(s
, "zone=%u ", zone
->id
);
205 case NF_CT_ZONE_DIR_ORIG
:
206 seq_printf(s
, "zone-orig=%u ", zone
->id
);
208 case NF_CT_ZONE_DIR_REPL
:
209 seq_printf(s
, "zone-reply=%u ", zone
->id
);
216 static inline void ct_show_zone(struct seq_file
*s
, const struct nf_conn
*ct
,
222 #ifdef CONFIG_NF_CONNTRACK_TIMESTAMP
223 static void ct_show_delta_time(struct seq_file
*s
, const struct nf_conn
*ct
)
225 struct ct_iter_state
*st
= s
->private;
226 struct nf_conn_tstamp
*tstamp
;
229 tstamp
= nf_conn_tstamp_find(ct
);
231 delta_time
= st
->time_now
- tstamp
->start
;
233 delta_time
= div_s64(delta_time
, NSEC_PER_SEC
);
237 seq_printf(s
, "delta-time=%llu ",
238 (unsigned long long)delta_time
);
244 ct_show_delta_time(struct seq_file
*s
, const struct nf_conn
*ct
)
249 static const char* l3proto_name(u16 proto
)
252 case AF_INET
: return "ipv4";
253 case AF_INET6
: return "ipv6";
259 static const char* l4proto_name(u16 proto
)
262 case IPPROTO_ICMP
: return "icmp";
263 case IPPROTO_TCP
: return "tcp";
264 case IPPROTO_UDP
: return "udp";
265 case IPPROTO_DCCP
: return "dccp";
266 case IPPROTO_GRE
: return "gre";
267 case IPPROTO_SCTP
: return "sctp";
268 case IPPROTO_UDPLITE
: return "udplite";
275 seq_print_acct(struct seq_file
*s
, const struct nf_conn
*ct
, int dir
)
277 struct nf_conn_acct
*acct
;
278 struct nf_conn_counter
*counter
;
280 acct
= nf_conn_acct_find(ct
);
284 counter
= acct
->counter
;
285 seq_printf(s
, "packets=%llu bytes=%llu ",
286 (unsigned long long)atomic64_read(&counter
[dir
].packets
),
287 (unsigned long long)atomic64_read(&counter
[dir
].bytes
));
292 /* return 0 on success, 1 in case of error */
293 static int ct_seq_show(struct seq_file
*s
, void *v
)
295 struct nf_conntrack_tuple_hash
*hash
= v
;
296 struct nf_conn
*ct
= nf_ct_tuplehash_to_ctrack(hash
);
297 const struct nf_conntrack_l4proto
*l4proto
;
298 struct net
*net
= seq_file_net(s
);
302 if (unlikely(!atomic_inc_not_zero(&ct
->ct_general
.use
)))
305 if (nf_ct_should_gc(ct
)) {
310 /* we only want to print DIR_ORIGINAL */
311 if (NF_CT_DIRECTION(hash
))
314 if (!net_eq(nf_ct_net(ct
), net
))
317 l4proto
= nf_ct_l4proto_find(nf_ct_protonum(ct
));
320 seq_printf(s
, "%-8s %u %-8s %u ",
321 l3proto_name(nf_ct_l3num(ct
)), nf_ct_l3num(ct
),
322 l4proto_name(l4proto
->l4proto
), nf_ct_protonum(ct
));
324 if (!test_bit(IPS_OFFLOAD_BIT
, &ct
->status
))
325 seq_printf(s
, "%ld ", nf_ct_expires(ct
) / HZ
);
327 if (l4proto
->print_conntrack
)
328 l4proto
->print_conntrack(s
, ct
);
330 print_tuple(s
, &ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
,
333 ct_show_zone(s
, ct
, NF_CT_ZONE_DIR_ORIG
);
335 if (seq_has_overflowed(s
))
338 if (seq_print_acct(s
, ct
, IP_CT_DIR_ORIGINAL
))
341 if (!(test_bit(IPS_SEEN_REPLY_BIT
, &ct
->status
)))
342 seq_puts(s
, "[UNREPLIED] ");
344 print_tuple(s
, &ct
->tuplehash
[IP_CT_DIR_REPLY
].tuple
, l4proto
);
346 ct_show_zone(s
, ct
, NF_CT_ZONE_DIR_REPL
);
348 if (seq_print_acct(s
, ct
, IP_CT_DIR_REPLY
))
351 if (test_bit(IPS_OFFLOAD_BIT
, &ct
->status
))
352 seq_puts(s
, "[OFFLOAD] ");
353 else if (test_bit(IPS_ASSURED_BIT
, &ct
->status
))
354 seq_puts(s
, "[ASSURED] ");
356 if (seq_has_overflowed(s
))
359 #if defined(CONFIG_NF_CONNTRACK_MARK)
360 seq_printf(s
, "mark=%u ", ct
->mark
);
363 ct_show_secctx(s
, ct
);
364 ct_show_zone(s
, ct
, NF_CT_DEFAULT_ZONE_DIR
);
365 ct_show_delta_time(s
, ct
);
367 seq_printf(s
, "use=%u\n", atomic_read(&ct
->ct_general
.use
));
369 if (seq_has_overflowed(s
))
378 static const struct seq_operations ct_seq_ops
= {
379 .start
= ct_seq_start
,
385 static void *ct_cpu_seq_start(struct seq_file
*seq
, loff_t
*pos
)
387 struct net
*net
= seq_file_net(seq
);
391 return SEQ_START_TOKEN
;
393 for (cpu
= *pos
-1; cpu
< nr_cpu_ids
; ++cpu
) {
394 if (!cpu_possible(cpu
))
397 return per_cpu_ptr(net
->ct
.stat
, cpu
);
403 static void *ct_cpu_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
405 struct net
*net
= seq_file_net(seq
);
408 for (cpu
= *pos
; cpu
< nr_cpu_ids
; ++cpu
) {
409 if (!cpu_possible(cpu
))
412 return per_cpu_ptr(net
->ct
.stat
, cpu
);
418 static void ct_cpu_seq_stop(struct seq_file
*seq
, void *v
)
422 static int ct_cpu_seq_show(struct seq_file
*seq
, void *v
)
424 struct net
*net
= seq_file_net(seq
);
425 unsigned int nr_conntracks
= atomic_read(&net
->ct
.count
);
426 const struct ip_conntrack_stat
*st
= v
;
428 if (v
== SEQ_START_TOKEN
) {
429 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");
433 seq_printf(seq
, "%08x %08x %08x %08x %08x %08x %08x %08x "
434 "%08x %08x %08x %08x %08x %08x %08x %08x %08x\n",
457 static const struct seq_operations ct_cpu_seq_ops
= {
458 .start
= ct_cpu_seq_start
,
459 .next
= ct_cpu_seq_next
,
460 .stop
= ct_cpu_seq_stop
,
461 .show
= ct_cpu_seq_show
,
464 static int nf_conntrack_standalone_init_proc(struct net
*net
)
466 struct proc_dir_entry
*pde
;
470 pde
= proc_create_net("nf_conntrack", 0440, net
->proc_net
, &ct_seq_ops
,
471 sizeof(struct ct_iter_state
));
473 goto out_nf_conntrack
;
475 root_uid
= make_kuid(net
->user_ns
, 0);
476 root_gid
= make_kgid(net
->user_ns
, 0);
477 if (uid_valid(root_uid
) && gid_valid(root_gid
))
478 proc_set_user(pde
, root_uid
, root_gid
);
480 pde
= proc_create_net("nf_conntrack", 0444, net
->proc_net_stat
,
481 &ct_cpu_seq_ops
, sizeof(struct seq_net_private
));
483 goto out_stat_nf_conntrack
;
486 out_stat_nf_conntrack
:
487 remove_proc_entry("nf_conntrack", net
->proc_net
);
492 static void nf_conntrack_standalone_fini_proc(struct net
*net
)
494 remove_proc_entry("nf_conntrack", net
->proc_net_stat
);
495 remove_proc_entry("nf_conntrack", net
->proc_net
);
498 static int nf_conntrack_standalone_init_proc(struct net
*net
)
503 static void nf_conntrack_standalone_fini_proc(struct net
*net
)
506 #endif /* CONFIG_NF_CONNTRACK_PROCFS */
511 /* Log invalid packets of a given protocol */
512 static int log_invalid_proto_min __read_mostly
;
513 static int log_invalid_proto_max __read_mostly
= 255;
515 /* size the user *wants to set */
516 static unsigned int nf_conntrack_htable_size_user __read_mostly
;
519 nf_conntrack_hash_sysctl(struct ctl_table
*table
, int write
,
520 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
524 ret
= proc_dointvec(table
, write
, buffer
, lenp
, ppos
);
525 if (ret
< 0 || !write
)
528 /* update ret, we might not be able to satisfy request */
529 ret
= nf_conntrack_hash_resize(nf_conntrack_htable_size_user
);
531 /* update it to the actual value used by conntrack */
532 nf_conntrack_htable_size_user
= nf_conntrack_htable_size
;
536 static struct ctl_table_header
*nf_ct_netfilter_header
;
538 enum nf_ct_sysctl_index
{
541 NF_SYSCTL_CT_BUCKETS
,
542 NF_SYSCTL_CT_CHECKSUM
,
543 NF_SYSCTL_CT_LOG_INVALID
,
544 NF_SYSCTL_CT_EXPECT_MAX
,
547 #ifdef CONFIG_NF_CONNTRACK_EVENTS
550 #ifdef CONFIG_NF_CONNTRACK_TIMESTAMP
551 NF_SYSCTL_CT_TIMESTAMP
,
553 NF_SYSCTL_CT_PROTO_TIMEOUT_GENERIC
,
554 NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_SYN_SENT
,
555 NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_SYN_RECV
,
556 NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_ESTABLISHED
,
557 NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_FIN_WAIT
,
558 NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_CLOSE_WAIT
,
559 NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_LAST_ACK
,
560 NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_TIME_WAIT
,
561 NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_CLOSE
,
562 NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_RETRANS
,
563 NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_UNACK
,
564 NF_SYSCTL_CT_PROTO_TCP_LOOSE
,
565 NF_SYSCTL_CT_PROTO_TCP_LIBERAL
,
566 NF_SYSCTL_CT_PROTO_TCP_MAX_RETRANS
,
567 NF_SYSCTL_CT_PROTO_TIMEOUT_UDP
,
568 NF_SYSCTL_CT_PROTO_TIMEOUT_UDP_STREAM
,
569 NF_SYSCTL_CT_PROTO_TIMEOUT_ICMP
,
570 NF_SYSCTL_CT_PROTO_TIMEOUT_ICMPV6
,
571 #ifdef CONFIG_NF_CT_PROTO_SCTP
572 NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_CLOSED
,
573 NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_COOKIE_WAIT
,
574 NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_COOKIE_ECHOED
,
575 NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_ESTABLISHED
,
576 NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_SHUTDOWN_SENT
,
577 NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_SHUTDOWN_RECD
,
578 NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_SHUTDOWN_ACK_SENT
,
579 NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_HEARTBEAT_SENT
,
580 NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_HEARTBEAT_ACKED
,
582 #ifdef CONFIG_NF_CT_PROTO_DCCP
583 NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_REQUEST
,
584 NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_RESPOND
,
585 NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_PARTOPEN
,
586 NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_OPEN
,
587 NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_CLOSEREQ
,
588 NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_CLOSING
,
589 NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_TIMEWAIT
,
590 NF_SYSCTL_CT_PROTO_DCCP_LOOSE
,
592 #ifdef CONFIG_NF_CT_PROTO_GRE
593 NF_SYSCTL_CT_PROTO_TIMEOUT_GRE
,
594 NF_SYSCTL_CT_PROTO_TIMEOUT_GRE_STREAM
,
597 __NF_SYSCTL_CT_LAST_SYSCTL
,
600 #define NF_SYSCTL_CT_LAST_SYSCTL (__NF_SYSCTL_CT_LAST_SYSCTL + 1)
602 static struct ctl_table nf_ct_sysctl_table
[] = {
603 [NF_SYSCTL_CT_MAX
] = {
604 .procname
= "nf_conntrack_max",
605 .data
= &nf_conntrack_max
,
606 .maxlen
= sizeof(int),
608 .proc_handler
= proc_dointvec
,
610 [NF_SYSCTL_CT_COUNT
] = {
611 .procname
= "nf_conntrack_count",
612 .data
= &init_net
.ct
.count
,
613 .maxlen
= sizeof(int),
615 .proc_handler
= proc_dointvec
,
617 [NF_SYSCTL_CT_BUCKETS
] = {
618 .procname
= "nf_conntrack_buckets",
619 .data
= &nf_conntrack_htable_size_user
,
620 .maxlen
= sizeof(unsigned int),
622 .proc_handler
= nf_conntrack_hash_sysctl
,
624 [NF_SYSCTL_CT_CHECKSUM
] = {
625 .procname
= "nf_conntrack_checksum",
626 .data
= &init_net
.ct
.sysctl_checksum
,
627 .maxlen
= sizeof(int),
629 .proc_handler
= proc_dointvec_minmax
,
630 .extra1
= SYSCTL_ZERO
,
631 .extra2
= SYSCTL_ONE
,
633 [NF_SYSCTL_CT_LOG_INVALID
] = {
634 .procname
= "nf_conntrack_log_invalid",
635 .data
= &init_net
.ct
.sysctl_log_invalid
,
636 .maxlen
= sizeof(unsigned int),
638 .proc_handler
= proc_dointvec_minmax
,
639 .extra1
= &log_invalid_proto_min
,
640 .extra2
= &log_invalid_proto_max
,
642 [NF_SYSCTL_CT_EXPECT_MAX
] = {
643 .procname
= "nf_conntrack_expect_max",
644 .data
= &nf_ct_expect_max
,
645 .maxlen
= sizeof(int),
647 .proc_handler
= proc_dointvec
,
649 [NF_SYSCTL_CT_ACCT
] = {
650 .procname
= "nf_conntrack_acct",
651 .data
= &init_net
.ct
.sysctl_acct
,
652 .maxlen
= sizeof(int),
654 .proc_handler
= proc_dointvec_minmax
,
655 .extra1
= SYSCTL_ZERO
,
656 .extra2
= SYSCTL_ONE
,
658 [NF_SYSCTL_CT_HELPER
] = {
659 .procname
= "nf_conntrack_helper",
660 .data
= &init_net
.ct
.sysctl_auto_assign_helper
,
661 .maxlen
= sizeof(int),
663 .proc_handler
= proc_dointvec_minmax
,
664 .extra1
= SYSCTL_ZERO
,
665 .extra2
= SYSCTL_ONE
,
667 #ifdef CONFIG_NF_CONNTRACK_EVENTS
668 [NF_SYSCTL_CT_EVENTS
] = {
669 .procname
= "nf_conntrack_events",
670 .data
= &init_net
.ct
.sysctl_events
,
671 .maxlen
= sizeof(int),
673 .proc_handler
= proc_dointvec_minmax
,
674 .extra1
= SYSCTL_ZERO
,
675 .extra2
= SYSCTL_ONE
,
678 #ifdef CONFIG_NF_CONNTRACK_TIMESTAMP
679 [NF_SYSCTL_CT_TIMESTAMP
] = {
680 .procname
= "nf_conntrack_timestamp",
681 .data
= &init_net
.ct
.sysctl_tstamp
,
682 .maxlen
= sizeof(int),
684 .proc_handler
= proc_dointvec_minmax
,
685 .extra1
= SYSCTL_ZERO
,
686 .extra2
= SYSCTL_ONE
,
689 [NF_SYSCTL_CT_PROTO_TIMEOUT_GENERIC
] = {
690 .procname
= "nf_conntrack_generic_timeout",
691 .maxlen
= sizeof(unsigned int),
693 .proc_handler
= proc_dointvec_jiffies
,
695 [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_SYN_SENT
] = {
696 .procname
= "nf_conntrack_tcp_timeout_syn_sent",
697 .maxlen
= sizeof(unsigned int),
699 .proc_handler
= proc_dointvec_jiffies
,
701 [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_SYN_RECV
] = {
702 .procname
= "nf_conntrack_tcp_timeout_syn_recv",
703 .maxlen
= sizeof(unsigned int),
705 .proc_handler
= proc_dointvec_jiffies
,
707 [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_ESTABLISHED
] = {
708 .procname
= "nf_conntrack_tcp_timeout_established",
709 .maxlen
= sizeof(unsigned int),
711 .proc_handler
= proc_dointvec_jiffies
,
713 [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_FIN_WAIT
] = {
714 .procname
= "nf_conntrack_tcp_timeout_fin_wait",
715 .maxlen
= sizeof(unsigned int),
717 .proc_handler
= proc_dointvec_jiffies
,
719 [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_CLOSE_WAIT
] = {
720 .procname
= "nf_conntrack_tcp_timeout_close_wait",
721 .maxlen
= sizeof(unsigned int),
723 .proc_handler
= proc_dointvec_jiffies
,
725 [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_LAST_ACK
] = {
726 .procname
= "nf_conntrack_tcp_timeout_last_ack",
727 .maxlen
= sizeof(unsigned int),
729 .proc_handler
= proc_dointvec_jiffies
,
731 [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_TIME_WAIT
] = {
732 .procname
= "nf_conntrack_tcp_timeout_time_wait",
733 .maxlen
= sizeof(unsigned int),
735 .proc_handler
= proc_dointvec_jiffies
,
737 [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_CLOSE
] = {
738 .procname
= "nf_conntrack_tcp_timeout_close",
739 .maxlen
= sizeof(unsigned int),
741 .proc_handler
= proc_dointvec_jiffies
,
743 [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_RETRANS
] = {
744 .procname
= "nf_conntrack_tcp_timeout_max_retrans",
745 .maxlen
= sizeof(unsigned int),
747 .proc_handler
= proc_dointvec_jiffies
,
749 [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_UNACK
] = {
750 .procname
= "nf_conntrack_tcp_timeout_unacknowledged",
751 .maxlen
= sizeof(unsigned int),
753 .proc_handler
= proc_dointvec_jiffies
,
755 [NF_SYSCTL_CT_PROTO_TCP_LOOSE
] = {
756 .procname
= "nf_conntrack_tcp_loose",
757 .maxlen
= sizeof(int),
759 .proc_handler
= proc_dointvec_minmax
,
760 .extra1
= SYSCTL_ZERO
,
761 .extra2
= SYSCTL_ONE
,
763 [NF_SYSCTL_CT_PROTO_TCP_LIBERAL
] = {
764 .procname
= "nf_conntrack_tcp_be_liberal",
765 .maxlen
= sizeof(int),
767 .proc_handler
= proc_dointvec_minmax
,
768 .extra1
= SYSCTL_ZERO
,
769 .extra2
= SYSCTL_ONE
,
771 [NF_SYSCTL_CT_PROTO_TCP_MAX_RETRANS
] = {
772 .procname
= "nf_conntrack_tcp_max_retrans",
773 .maxlen
= sizeof(unsigned int),
775 .proc_handler
= proc_dointvec
,
777 [NF_SYSCTL_CT_PROTO_TIMEOUT_UDP
] = {
778 .procname
= "nf_conntrack_udp_timeout",
779 .maxlen
= sizeof(unsigned int),
781 .proc_handler
= proc_dointvec_jiffies
,
783 [NF_SYSCTL_CT_PROTO_TIMEOUT_UDP_STREAM
] = {
784 .procname
= "nf_conntrack_udp_timeout_stream",
785 .maxlen
= sizeof(unsigned int),
787 .proc_handler
= proc_dointvec_jiffies
,
789 [NF_SYSCTL_CT_PROTO_TIMEOUT_ICMP
] = {
790 .procname
= "nf_conntrack_icmp_timeout",
791 .maxlen
= sizeof(unsigned int),
793 .proc_handler
= proc_dointvec_jiffies
,
795 [NF_SYSCTL_CT_PROTO_TIMEOUT_ICMPV6
] = {
796 .procname
= "nf_conntrack_icmpv6_timeout",
797 .maxlen
= sizeof(unsigned int),
799 .proc_handler
= proc_dointvec_jiffies
,
801 #ifdef CONFIG_NF_CT_PROTO_SCTP
802 [NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_CLOSED
] = {
803 .procname
= "nf_conntrack_sctp_timeout_closed",
804 .maxlen
= sizeof(unsigned int),
806 .proc_handler
= proc_dointvec_jiffies
,
808 [NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_COOKIE_WAIT
] = {
809 .procname
= "nf_conntrack_sctp_timeout_cookie_wait",
810 .maxlen
= sizeof(unsigned int),
812 .proc_handler
= proc_dointvec_jiffies
,
814 [NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_COOKIE_ECHOED
] = {
815 .procname
= "nf_conntrack_sctp_timeout_cookie_echoed",
816 .maxlen
= sizeof(unsigned int),
818 .proc_handler
= proc_dointvec_jiffies
,
820 [NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_ESTABLISHED
] = {
821 .procname
= "nf_conntrack_sctp_timeout_established",
822 .maxlen
= sizeof(unsigned int),
824 .proc_handler
= proc_dointvec_jiffies
,
826 [NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_SHUTDOWN_SENT
] = {
827 .procname
= "nf_conntrack_sctp_timeout_shutdown_sent",
828 .maxlen
= sizeof(unsigned int),
830 .proc_handler
= proc_dointvec_jiffies
,
832 [NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_SHUTDOWN_RECD
] = {
833 .procname
= "nf_conntrack_sctp_timeout_shutdown_recd",
834 .maxlen
= sizeof(unsigned int),
836 .proc_handler
= proc_dointvec_jiffies
,
838 [NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_SHUTDOWN_ACK_SENT
] = {
839 .procname
= "nf_conntrack_sctp_timeout_shutdown_ack_sent",
840 .maxlen
= sizeof(unsigned int),
842 .proc_handler
= proc_dointvec_jiffies
,
844 [NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_HEARTBEAT_SENT
] = {
845 .procname
= "nf_conntrack_sctp_timeout_heartbeat_sent",
846 .maxlen
= sizeof(unsigned int),
848 .proc_handler
= proc_dointvec_jiffies
,
850 [NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_HEARTBEAT_ACKED
] = {
851 .procname
= "nf_conntrack_sctp_timeout_heartbeat_acked",
852 .maxlen
= sizeof(unsigned int),
854 .proc_handler
= proc_dointvec_jiffies
,
857 #ifdef CONFIG_NF_CT_PROTO_DCCP
858 [NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_REQUEST
] = {
859 .procname
= "nf_conntrack_dccp_timeout_request",
860 .maxlen
= sizeof(unsigned int),
862 .proc_handler
= proc_dointvec_jiffies
,
864 [NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_RESPOND
] = {
865 .procname
= "nf_conntrack_dccp_timeout_respond",
866 .maxlen
= sizeof(unsigned int),
868 .proc_handler
= proc_dointvec_jiffies
,
870 [NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_PARTOPEN
] = {
871 .procname
= "nf_conntrack_dccp_timeout_partopen",
872 .maxlen
= sizeof(unsigned int),
874 .proc_handler
= proc_dointvec_jiffies
,
876 [NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_OPEN
] = {
877 .procname
= "nf_conntrack_dccp_timeout_open",
878 .maxlen
= sizeof(unsigned int),
880 .proc_handler
= proc_dointvec_jiffies
,
882 [NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_CLOSEREQ
] = {
883 .procname
= "nf_conntrack_dccp_timeout_closereq",
884 .maxlen
= sizeof(unsigned int),
886 .proc_handler
= proc_dointvec_jiffies
,
888 [NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_CLOSING
] = {
889 .procname
= "nf_conntrack_dccp_timeout_closing",
890 .maxlen
= sizeof(unsigned int),
892 .proc_handler
= proc_dointvec_jiffies
,
894 [NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_TIMEWAIT
] = {
895 .procname
= "nf_conntrack_dccp_timeout_timewait",
896 .maxlen
= sizeof(unsigned int),
898 .proc_handler
= proc_dointvec_jiffies
,
900 [NF_SYSCTL_CT_PROTO_DCCP_LOOSE
] = {
901 .procname
= "nf_conntrack_dccp_loose",
902 .maxlen
= sizeof(int),
904 .proc_handler
= proc_dointvec_minmax
,
905 .extra1
= SYSCTL_ZERO
,
906 .extra2
= SYSCTL_ONE
,
909 #ifdef CONFIG_NF_CT_PROTO_GRE
910 [NF_SYSCTL_CT_PROTO_TIMEOUT_GRE
] = {
911 .procname
= "nf_conntrack_gre_timeout",
912 .maxlen
= sizeof(unsigned int),
914 .proc_handler
= proc_dointvec_jiffies
,
916 [NF_SYSCTL_CT_PROTO_TIMEOUT_GRE_STREAM
] = {
917 .procname
= "nf_conntrack_gre_timeout_stream",
918 .maxlen
= sizeof(unsigned int),
920 .proc_handler
= proc_dointvec_jiffies
,
926 static struct ctl_table nf_ct_netfilter_table
[] = {
928 .procname
= "nf_conntrack_max",
929 .data
= &nf_conntrack_max
,
930 .maxlen
= sizeof(int),
932 .proc_handler
= proc_dointvec
,
937 static void nf_conntrack_standalone_init_tcp_sysctl(struct net
*net
,
938 struct ctl_table
*table
)
940 struct nf_tcp_net
*tn
= nf_tcp_pernet(net
);
942 #define XASSIGN(XNAME, tn) \
943 table[NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_ ## XNAME].data = \
944 &(tn)->timeouts[TCP_CONNTRACK_ ## XNAME]
946 XASSIGN(SYN_SENT
, tn
);
947 XASSIGN(SYN_RECV
, tn
);
948 XASSIGN(ESTABLISHED
, tn
);
949 XASSIGN(FIN_WAIT
, tn
);
950 XASSIGN(CLOSE_WAIT
, tn
);
951 XASSIGN(LAST_ACK
, tn
);
952 XASSIGN(TIME_WAIT
, tn
);
954 XASSIGN(RETRANS
, tn
);
957 #define XASSIGN(XNAME, rval) \
958 table[NF_SYSCTL_CT_PROTO_TCP_ ## XNAME].data = (rval)
960 XASSIGN(LOOSE
, &tn
->tcp_loose
);
961 XASSIGN(LIBERAL
, &tn
->tcp_be_liberal
);
962 XASSIGN(MAX_RETRANS
, &tn
->tcp_max_retrans
);
966 static void nf_conntrack_standalone_init_sctp_sysctl(struct net
*net
,
967 struct ctl_table
*table
)
969 #ifdef CONFIG_NF_CT_PROTO_SCTP
970 struct nf_sctp_net
*sn
= nf_sctp_pernet(net
);
972 #define XASSIGN(XNAME, sn) \
973 table[NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_ ## XNAME].data = \
974 &(sn)->timeouts[SCTP_CONNTRACK_ ## XNAME]
977 XASSIGN(COOKIE_WAIT
, sn
);
978 XASSIGN(COOKIE_ECHOED
, sn
);
979 XASSIGN(ESTABLISHED
, sn
);
980 XASSIGN(SHUTDOWN_SENT
, sn
);
981 XASSIGN(SHUTDOWN_RECD
, sn
);
982 XASSIGN(SHUTDOWN_ACK_SENT
, sn
);
983 XASSIGN(HEARTBEAT_SENT
, sn
);
984 XASSIGN(HEARTBEAT_ACKED
, sn
);
989 static void nf_conntrack_standalone_init_dccp_sysctl(struct net
*net
,
990 struct ctl_table
*table
)
992 #ifdef CONFIG_NF_CT_PROTO_DCCP
993 struct nf_dccp_net
*dn
= nf_dccp_pernet(net
);
995 #define XASSIGN(XNAME, dn) \
996 table[NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_ ## XNAME].data = \
997 &(dn)->dccp_timeout[CT_DCCP_ ## XNAME]
999 XASSIGN(REQUEST
, dn
);
1000 XASSIGN(RESPOND
, dn
);
1001 XASSIGN(PARTOPEN
, dn
);
1003 XASSIGN(CLOSEREQ
, dn
);
1004 XASSIGN(CLOSING
, dn
);
1005 XASSIGN(TIMEWAIT
, dn
);
1008 table
[NF_SYSCTL_CT_PROTO_DCCP_LOOSE
].data
= &dn
->dccp_loose
;
1012 static void nf_conntrack_standalone_init_gre_sysctl(struct net
*net
,
1013 struct ctl_table
*table
)
1015 #ifdef CONFIG_NF_CT_PROTO_GRE
1016 struct nf_gre_net
*gn
= nf_gre_pernet(net
);
1018 table
[NF_SYSCTL_CT_PROTO_TIMEOUT_GRE
].data
= &gn
->timeouts
[GRE_CT_UNREPLIED
];
1019 table
[NF_SYSCTL_CT_PROTO_TIMEOUT_GRE_STREAM
].data
= &gn
->timeouts
[GRE_CT_REPLIED
];
1023 static int nf_conntrack_standalone_init_sysctl(struct net
*net
)
1025 struct nf_udp_net
*un
= nf_udp_pernet(net
);
1026 struct ctl_table
*table
;
1028 BUILD_BUG_ON(ARRAY_SIZE(nf_ct_sysctl_table
) != NF_SYSCTL_CT_LAST_SYSCTL
);
1030 table
= kmemdup(nf_ct_sysctl_table
, sizeof(nf_ct_sysctl_table
),
1035 table
[NF_SYSCTL_CT_COUNT
].data
= &net
->ct
.count
;
1036 table
[NF_SYSCTL_CT_CHECKSUM
].data
= &net
->ct
.sysctl_checksum
;
1037 table
[NF_SYSCTL_CT_LOG_INVALID
].data
= &net
->ct
.sysctl_log_invalid
;
1038 table
[NF_SYSCTL_CT_ACCT
].data
= &net
->ct
.sysctl_acct
;
1039 table
[NF_SYSCTL_CT_HELPER
].data
= &net
->ct
.sysctl_auto_assign_helper
;
1040 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1041 table
[NF_SYSCTL_CT_EVENTS
].data
= &net
->ct
.sysctl_events
;
1043 #ifdef CONFIG_NF_CONNTRACK_TIMESTAMP
1044 table
[NF_SYSCTL_CT_TIMESTAMP
].data
= &net
->ct
.sysctl_tstamp
;
1046 table
[NF_SYSCTL_CT_PROTO_TIMEOUT_GENERIC
].data
= &nf_generic_pernet(net
)->timeout
;
1047 table
[NF_SYSCTL_CT_PROTO_TIMEOUT_ICMP
].data
= &nf_icmp_pernet(net
)->timeout
;
1048 table
[NF_SYSCTL_CT_PROTO_TIMEOUT_ICMPV6
].data
= &nf_icmpv6_pernet(net
)->timeout
;
1049 table
[NF_SYSCTL_CT_PROTO_TIMEOUT_UDP
].data
= &un
->timeouts
[UDP_CT_UNREPLIED
];
1050 table
[NF_SYSCTL_CT_PROTO_TIMEOUT_UDP_STREAM
].data
= &un
->timeouts
[UDP_CT_REPLIED
];
1052 nf_conntrack_standalone_init_tcp_sysctl(net
, table
);
1053 nf_conntrack_standalone_init_sctp_sysctl(net
, table
);
1054 nf_conntrack_standalone_init_dccp_sysctl(net
, table
);
1055 nf_conntrack_standalone_init_gre_sysctl(net
, table
);
1057 /* Don't export sysctls to unprivileged users */
1058 if (net
->user_ns
!= &init_user_ns
) {
1059 table
[NF_SYSCTL_CT_MAX
].procname
= NULL
;
1060 table
[NF_SYSCTL_CT_ACCT
].procname
= NULL
;
1061 table
[NF_SYSCTL_CT_HELPER
].procname
= NULL
;
1062 #ifdef CONFIG_NF_CONNTRACK_TIMESTAMP
1063 table
[NF_SYSCTL_CT_TIMESTAMP
].procname
= NULL
;
1065 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1066 table
[NF_SYSCTL_CT_EVENTS
].procname
= NULL
;
1070 if (!net_eq(&init_net
, net
))
1071 table
[NF_SYSCTL_CT_BUCKETS
].mode
= 0444;
1073 net
->ct
.sysctl_header
= register_net_sysctl(net
, "net/netfilter", table
);
1074 if (!net
->ct
.sysctl_header
)
1075 goto out_unregister_netfilter
;
1079 out_unregister_netfilter
:
1084 static void nf_conntrack_standalone_fini_sysctl(struct net
*net
)
1086 struct ctl_table
*table
;
1088 table
= net
->ct
.sysctl_header
->ctl_table_arg
;
1089 unregister_net_sysctl_table(net
->ct
.sysctl_header
);
1093 static int nf_conntrack_standalone_init_sysctl(struct net
*net
)
1098 static void nf_conntrack_standalone_fini_sysctl(struct net
*net
)
1101 #endif /* CONFIG_SYSCTL */
1103 static void nf_conntrack_fini_net(struct net
*net
)
1106 nf_ct_netns_put(net
, NFPROTO_INET
);
1108 nf_conntrack_standalone_fini_proc(net
);
1109 nf_conntrack_standalone_fini_sysctl(net
);
1112 static int nf_conntrack_pernet_init(struct net
*net
)
1116 net
->ct
.sysctl_checksum
= 1;
1118 ret
= nf_conntrack_standalone_init_sysctl(net
);
1122 ret
= nf_conntrack_standalone_init_proc(net
);
1126 ret
= nf_conntrack_init_net(net
);
1131 ret
= nf_ct_netns_get(net
, NFPROTO_INET
);
1139 nf_conntrack_cleanup_net(net
);
1141 nf_conntrack_standalone_fini_proc(net
);
1143 nf_conntrack_standalone_fini_sysctl(net
);
1147 static void nf_conntrack_pernet_exit(struct list_head
*net_exit_list
)
1151 list_for_each_entry(net
, net_exit_list
, exit_list
)
1152 nf_conntrack_fini_net(net
);
1154 nf_conntrack_cleanup_net_list(net_exit_list
);
1157 static struct pernet_operations nf_conntrack_net_ops
= {
1158 .init
= nf_conntrack_pernet_init
,
1159 .exit_batch
= nf_conntrack_pernet_exit
,
1160 .id
= &nf_conntrack_net_id
,
1161 .size
= sizeof(struct nf_conntrack_net
),
1164 static int __init
nf_conntrack_standalone_init(void)
1166 int ret
= nf_conntrack_init_start();
1170 BUILD_BUG_ON(NFCT_INFOMASK
<= IP_CT_NUMBER
);
1172 #ifdef CONFIG_SYSCTL
1173 nf_ct_netfilter_header
=
1174 register_net_sysctl(&init_net
, "net", nf_ct_netfilter_table
);
1175 if (!nf_ct_netfilter_header
) {
1176 pr_err("nf_conntrack: can't register to sysctl.\n");
1181 nf_conntrack_htable_size_user
= nf_conntrack_htable_size
;
1184 ret
= register_pernet_subsys(&nf_conntrack_net_ops
);
1188 nf_conntrack_init_end();
1192 #ifdef CONFIG_SYSCTL
1193 unregister_net_sysctl_table(nf_ct_netfilter_header
);
1196 nf_conntrack_cleanup_end();
1201 static void __exit
nf_conntrack_standalone_fini(void)
1203 nf_conntrack_cleanup_start();
1204 unregister_pernet_subsys(&nf_conntrack_net_ops
);
1205 #ifdef CONFIG_SYSCTL
1206 unregister_net_sysctl_table(nf_ct_netfilter_header
);
1208 nf_conntrack_cleanup_end();
1211 module_init(nf_conntrack_standalone_init
);
1212 module_exit(nf_conntrack_standalone_fini
);