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
));
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_HW_OFFLOAD_BIT
, &ct
->status
))
352 seq_puts(s
, "[HW_OFFLOAD] ");
353 else if (test_bit(IPS_OFFLOAD_BIT
, &ct
->status
))
354 seq_puts(s
, "[OFFLOAD] ");
355 else if (test_bit(IPS_ASSURED_BIT
, &ct
->status
))
356 seq_puts(s
, "[ASSURED] ");
358 if (seq_has_overflowed(s
))
361 #if defined(CONFIG_NF_CONNTRACK_MARK)
362 seq_printf(s
, "mark=%u ", ct
->mark
);
365 ct_show_secctx(s
, ct
);
366 ct_show_zone(s
, ct
, NF_CT_DEFAULT_ZONE_DIR
);
367 ct_show_delta_time(s
, ct
);
369 seq_printf(s
, "use=%u\n", atomic_read(&ct
->ct_general
.use
));
371 if (seq_has_overflowed(s
))
380 static const struct seq_operations ct_seq_ops
= {
381 .start
= ct_seq_start
,
387 static void *ct_cpu_seq_start(struct seq_file
*seq
, loff_t
*pos
)
389 struct net
*net
= seq_file_net(seq
);
393 return SEQ_START_TOKEN
;
395 for (cpu
= *pos
-1; cpu
< nr_cpu_ids
; ++cpu
) {
396 if (!cpu_possible(cpu
))
399 return per_cpu_ptr(net
->ct
.stat
, cpu
);
405 static void *ct_cpu_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
407 struct net
*net
= seq_file_net(seq
);
410 for (cpu
= *pos
; cpu
< nr_cpu_ids
; ++cpu
) {
411 if (!cpu_possible(cpu
))
414 return per_cpu_ptr(net
->ct
.stat
, cpu
);
420 static void ct_cpu_seq_stop(struct seq_file
*seq
, void *v
)
424 static int ct_cpu_seq_show(struct seq_file
*seq
, void *v
)
426 struct net
*net
= seq_file_net(seq
);
427 unsigned int nr_conntracks
= atomic_read(&net
->ct
.count
);
428 const struct ip_conntrack_stat
*st
= v
;
430 if (v
== SEQ_START_TOKEN
) {
431 seq_puts(seq
, "entries clashres found new invalid ignore delete delete_list insert insert_failed drop early_drop icmp_error expect_new expect_create expect_delete search_restart\n");
435 seq_printf(seq
, "%08x %08x %08x %08x %08x %08x %08x %08x "
436 "%08x %08x %08x %08x %08x %08x %08x %08x %08x\n",
459 static const struct seq_operations ct_cpu_seq_ops
= {
460 .start
= ct_cpu_seq_start
,
461 .next
= ct_cpu_seq_next
,
462 .stop
= ct_cpu_seq_stop
,
463 .show
= ct_cpu_seq_show
,
466 static int nf_conntrack_standalone_init_proc(struct net
*net
)
468 struct proc_dir_entry
*pde
;
472 pde
= proc_create_net("nf_conntrack", 0440, net
->proc_net
, &ct_seq_ops
,
473 sizeof(struct ct_iter_state
));
475 goto out_nf_conntrack
;
477 root_uid
= make_kuid(net
->user_ns
, 0);
478 root_gid
= make_kgid(net
->user_ns
, 0);
479 if (uid_valid(root_uid
) && gid_valid(root_gid
))
480 proc_set_user(pde
, root_uid
, root_gid
);
482 pde
= proc_create_net("nf_conntrack", 0444, net
->proc_net_stat
,
483 &ct_cpu_seq_ops
, sizeof(struct seq_net_private
));
485 goto out_stat_nf_conntrack
;
488 out_stat_nf_conntrack
:
489 remove_proc_entry("nf_conntrack", net
->proc_net
);
494 static void nf_conntrack_standalone_fini_proc(struct net
*net
)
496 remove_proc_entry("nf_conntrack", net
->proc_net_stat
);
497 remove_proc_entry("nf_conntrack", net
->proc_net
);
500 static int nf_conntrack_standalone_init_proc(struct net
*net
)
505 static void nf_conntrack_standalone_fini_proc(struct net
*net
)
508 #endif /* CONFIG_NF_CONNTRACK_PROCFS */
513 /* Log invalid packets of a given protocol */
514 static int log_invalid_proto_min __read_mostly
;
515 static int log_invalid_proto_max __read_mostly
= 255;
517 /* size the user *wants to set */
518 static unsigned int nf_conntrack_htable_size_user __read_mostly
;
521 nf_conntrack_hash_sysctl(struct ctl_table
*table
, int write
,
522 void *buffer
, size_t *lenp
, loff_t
*ppos
)
526 /* module_param hashsize could have changed value */
527 nf_conntrack_htable_size_user
= nf_conntrack_htable_size
;
529 ret
= proc_dointvec(table
, write
, buffer
, lenp
, ppos
);
530 if (ret
< 0 || !write
)
533 /* update ret, we might not be able to satisfy request */
534 ret
= nf_conntrack_hash_resize(nf_conntrack_htable_size_user
);
536 /* update it to the actual value used by conntrack */
537 nf_conntrack_htable_size_user
= nf_conntrack_htable_size
;
541 static struct ctl_table_header
*nf_ct_netfilter_header
;
543 enum nf_ct_sysctl_index
{
546 NF_SYSCTL_CT_BUCKETS
,
547 NF_SYSCTL_CT_CHECKSUM
,
548 NF_SYSCTL_CT_LOG_INVALID
,
549 NF_SYSCTL_CT_EXPECT_MAX
,
552 #ifdef CONFIG_NF_CONNTRACK_EVENTS
555 #ifdef CONFIG_NF_CONNTRACK_TIMESTAMP
556 NF_SYSCTL_CT_TIMESTAMP
,
558 NF_SYSCTL_CT_PROTO_TIMEOUT_GENERIC
,
559 NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_SYN_SENT
,
560 NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_SYN_RECV
,
561 NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_ESTABLISHED
,
562 NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_FIN_WAIT
,
563 NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_CLOSE_WAIT
,
564 NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_LAST_ACK
,
565 NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_TIME_WAIT
,
566 NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_CLOSE
,
567 NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_RETRANS
,
568 NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_UNACK
,
569 NF_SYSCTL_CT_PROTO_TCP_LOOSE
,
570 NF_SYSCTL_CT_PROTO_TCP_LIBERAL
,
571 NF_SYSCTL_CT_PROTO_TCP_MAX_RETRANS
,
572 NF_SYSCTL_CT_PROTO_TIMEOUT_UDP
,
573 NF_SYSCTL_CT_PROTO_TIMEOUT_UDP_STREAM
,
574 NF_SYSCTL_CT_PROTO_TIMEOUT_ICMP
,
575 NF_SYSCTL_CT_PROTO_TIMEOUT_ICMPV6
,
576 #ifdef CONFIG_NF_CT_PROTO_SCTP
577 NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_CLOSED
,
578 NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_COOKIE_WAIT
,
579 NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_COOKIE_ECHOED
,
580 NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_ESTABLISHED
,
581 NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_SHUTDOWN_SENT
,
582 NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_SHUTDOWN_RECD
,
583 NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_SHUTDOWN_ACK_SENT
,
584 NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_HEARTBEAT_SENT
,
585 NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_HEARTBEAT_ACKED
,
587 #ifdef CONFIG_NF_CT_PROTO_DCCP
588 NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_REQUEST
,
589 NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_RESPOND
,
590 NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_PARTOPEN
,
591 NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_OPEN
,
592 NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_CLOSEREQ
,
593 NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_CLOSING
,
594 NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_TIMEWAIT
,
595 NF_SYSCTL_CT_PROTO_DCCP_LOOSE
,
597 #ifdef CONFIG_NF_CT_PROTO_GRE
598 NF_SYSCTL_CT_PROTO_TIMEOUT_GRE
,
599 NF_SYSCTL_CT_PROTO_TIMEOUT_GRE_STREAM
,
602 __NF_SYSCTL_CT_LAST_SYSCTL
,
605 #define NF_SYSCTL_CT_LAST_SYSCTL (__NF_SYSCTL_CT_LAST_SYSCTL + 1)
607 static struct ctl_table nf_ct_sysctl_table
[] = {
608 [NF_SYSCTL_CT_MAX
] = {
609 .procname
= "nf_conntrack_max",
610 .data
= &nf_conntrack_max
,
611 .maxlen
= sizeof(int),
613 .proc_handler
= proc_dointvec
,
615 [NF_SYSCTL_CT_COUNT
] = {
616 .procname
= "nf_conntrack_count",
617 .data
= &init_net
.ct
.count
,
618 .maxlen
= sizeof(int),
620 .proc_handler
= proc_dointvec
,
622 [NF_SYSCTL_CT_BUCKETS
] = {
623 .procname
= "nf_conntrack_buckets",
624 .data
= &nf_conntrack_htable_size_user
,
625 .maxlen
= sizeof(unsigned int),
627 .proc_handler
= nf_conntrack_hash_sysctl
,
629 [NF_SYSCTL_CT_CHECKSUM
] = {
630 .procname
= "nf_conntrack_checksum",
631 .data
= &init_net
.ct
.sysctl_checksum
,
632 .maxlen
= sizeof(int),
634 .proc_handler
= proc_dointvec_minmax
,
635 .extra1
= SYSCTL_ZERO
,
636 .extra2
= SYSCTL_ONE
,
638 [NF_SYSCTL_CT_LOG_INVALID
] = {
639 .procname
= "nf_conntrack_log_invalid",
640 .data
= &init_net
.ct
.sysctl_log_invalid
,
641 .maxlen
= sizeof(unsigned int),
643 .proc_handler
= proc_dointvec_minmax
,
644 .extra1
= &log_invalid_proto_min
,
645 .extra2
= &log_invalid_proto_max
,
647 [NF_SYSCTL_CT_EXPECT_MAX
] = {
648 .procname
= "nf_conntrack_expect_max",
649 .data
= &nf_ct_expect_max
,
650 .maxlen
= sizeof(int),
652 .proc_handler
= proc_dointvec
,
654 [NF_SYSCTL_CT_ACCT
] = {
655 .procname
= "nf_conntrack_acct",
656 .data
= &init_net
.ct
.sysctl_acct
,
657 .maxlen
= sizeof(int),
659 .proc_handler
= proc_dointvec_minmax
,
660 .extra1
= SYSCTL_ZERO
,
661 .extra2
= SYSCTL_ONE
,
663 [NF_SYSCTL_CT_HELPER
] = {
664 .procname
= "nf_conntrack_helper",
665 .data
= &init_net
.ct
.sysctl_auto_assign_helper
,
666 .maxlen
= sizeof(int),
668 .proc_handler
= proc_dointvec_minmax
,
669 .extra1
= SYSCTL_ZERO
,
670 .extra2
= SYSCTL_ONE
,
672 #ifdef CONFIG_NF_CONNTRACK_EVENTS
673 [NF_SYSCTL_CT_EVENTS
] = {
674 .procname
= "nf_conntrack_events",
675 .data
= &init_net
.ct
.sysctl_events
,
676 .maxlen
= sizeof(int),
678 .proc_handler
= proc_dointvec_minmax
,
679 .extra1
= SYSCTL_ZERO
,
680 .extra2
= SYSCTL_ONE
,
683 #ifdef CONFIG_NF_CONNTRACK_TIMESTAMP
684 [NF_SYSCTL_CT_TIMESTAMP
] = {
685 .procname
= "nf_conntrack_timestamp",
686 .data
= &init_net
.ct
.sysctl_tstamp
,
687 .maxlen
= sizeof(int),
689 .proc_handler
= proc_dointvec_minmax
,
690 .extra1
= SYSCTL_ZERO
,
691 .extra2
= SYSCTL_ONE
,
694 [NF_SYSCTL_CT_PROTO_TIMEOUT_GENERIC
] = {
695 .procname
= "nf_conntrack_generic_timeout",
696 .maxlen
= sizeof(unsigned int),
698 .proc_handler
= proc_dointvec_jiffies
,
700 [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_SYN_SENT
] = {
701 .procname
= "nf_conntrack_tcp_timeout_syn_sent",
702 .maxlen
= sizeof(unsigned int),
704 .proc_handler
= proc_dointvec_jiffies
,
706 [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_SYN_RECV
] = {
707 .procname
= "nf_conntrack_tcp_timeout_syn_recv",
708 .maxlen
= sizeof(unsigned int),
710 .proc_handler
= proc_dointvec_jiffies
,
712 [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_ESTABLISHED
] = {
713 .procname
= "nf_conntrack_tcp_timeout_established",
714 .maxlen
= sizeof(unsigned int),
716 .proc_handler
= proc_dointvec_jiffies
,
718 [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_FIN_WAIT
] = {
719 .procname
= "nf_conntrack_tcp_timeout_fin_wait",
720 .maxlen
= sizeof(unsigned int),
722 .proc_handler
= proc_dointvec_jiffies
,
724 [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_CLOSE_WAIT
] = {
725 .procname
= "nf_conntrack_tcp_timeout_close_wait",
726 .maxlen
= sizeof(unsigned int),
728 .proc_handler
= proc_dointvec_jiffies
,
730 [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_LAST_ACK
] = {
731 .procname
= "nf_conntrack_tcp_timeout_last_ack",
732 .maxlen
= sizeof(unsigned int),
734 .proc_handler
= proc_dointvec_jiffies
,
736 [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_TIME_WAIT
] = {
737 .procname
= "nf_conntrack_tcp_timeout_time_wait",
738 .maxlen
= sizeof(unsigned int),
740 .proc_handler
= proc_dointvec_jiffies
,
742 [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_CLOSE
] = {
743 .procname
= "nf_conntrack_tcp_timeout_close",
744 .maxlen
= sizeof(unsigned int),
746 .proc_handler
= proc_dointvec_jiffies
,
748 [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_RETRANS
] = {
749 .procname
= "nf_conntrack_tcp_timeout_max_retrans",
750 .maxlen
= sizeof(unsigned int),
752 .proc_handler
= proc_dointvec_jiffies
,
754 [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_UNACK
] = {
755 .procname
= "nf_conntrack_tcp_timeout_unacknowledged",
756 .maxlen
= sizeof(unsigned int),
758 .proc_handler
= proc_dointvec_jiffies
,
760 [NF_SYSCTL_CT_PROTO_TCP_LOOSE
] = {
761 .procname
= "nf_conntrack_tcp_loose",
762 .maxlen
= sizeof(int),
764 .proc_handler
= proc_dointvec_minmax
,
765 .extra1
= SYSCTL_ZERO
,
766 .extra2
= SYSCTL_ONE
,
768 [NF_SYSCTL_CT_PROTO_TCP_LIBERAL
] = {
769 .procname
= "nf_conntrack_tcp_be_liberal",
770 .maxlen
= sizeof(int),
772 .proc_handler
= proc_dointvec_minmax
,
773 .extra1
= SYSCTL_ZERO
,
774 .extra2
= SYSCTL_ONE
,
776 [NF_SYSCTL_CT_PROTO_TCP_MAX_RETRANS
] = {
777 .procname
= "nf_conntrack_tcp_max_retrans",
778 .maxlen
= sizeof(unsigned int),
780 .proc_handler
= proc_dointvec
,
782 [NF_SYSCTL_CT_PROTO_TIMEOUT_UDP
] = {
783 .procname
= "nf_conntrack_udp_timeout",
784 .maxlen
= sizeof(unsigned int),
786 .proc_handler
= proc_dointvec_jiffies
,
788 [NF_SYSCTL_CT_PROTO_TIMEOUT_UDP_STREAM
] = {
789 .procname
= "nf_conntrack_udp_timeout_stream",
790 .maxlen
= sizeof(unsigned int),
792 .proc_handler
= proc_dointvec_jiffies
,
794 [NF_SYSCTL_CT_PROTO_TIMEOUT_ICMP
] = {
795 .procname
= "nf_conntrack_icmp_timeout",
796 .maxlen
= sizeof(unsigned int),
798 .proc_handler
= proc_dointvec_jiffies
,
800 [NF_SYSCTL_CT_PROTO_TIMEOUT_ICMPV6
] = {
801 .procname
= "nf_conntrack_icmpv6_timeout",
802 .maxlen
= sizeof(unsigned int),
804 .proc_handler
= proc_dointvec_jiffies
,
806 #ifdef CONFIG_NF_CT_PROTO_SCTP
807 [NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_CLOSED
] = {
808 .procname
= "nf_conntrack_sctp_timeout_closed",
809 .maxlen
= sizeof(unsigned int),
811 .proc_handler
= proc_dointvec_jiffies
,
813 [NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_COOKIE_WAIT
] = {
814 .procname
= "nf_conntrack_sctp_timeout_cookie_wait",
815 .maxlen
= sizeof(unsigned int),
817 .proc_handler
= proc_dointvec_jiffies
,
819 [NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_COOKIE_ECHOED
] = {
820 .procname
= "nf_conntrack_sctp_timeout_cookie_echoed",
821 .maxlen
= sizeof(unsigned int),
823 .proc_handler
= proc_dointvec_jiffies
,
825 [NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_ESTABLISHED
] = {
826 .procname
= "nf_conntrack_sctp_timeout_established",
827 .maxlen
= sizeof(unsigned int),
829 .proc_handler
= proc_dointvec_jiffies
,
831 [NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_SHUTDOWN_SENT
] = {
832 .procname
= "nf_conntrack_sctp_timeout_shutdown_sent",
833 .maxlen
= sizeof(unsigned int),
835 .proc_handler
= proc_dointvec_jiffies
,
837 [NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_SHUTDOWN_RECD
] = {
838 .procname
= "nf_conntrack_sctp_timeout_shutdown_recd",
839 .maxlen
= sizeof(unsigned int),
841 .proc_handler
= proc_dointvec_jiffies
,
843 [NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_SHUTDOWN_ACK_SENT
] = {
844 .procname
= "nf_conntrack_sctp_timeout_shutdown_ack_sent",
845 .maxlen
= sizeof(unsigned int),
847 .proc_handler
= proc_dointvec_jiffies
,
849 [NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_HEARTBEAT_SENT
] = {
850 .procname
= "nf_conntrack_sctp_timeout_heartbeat_sent",
851 .maxlen
= sizeof(unsigned int),
853 .proc_handler
= proc_dointvec_jiffies
,
855 [NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_HEARTBEAT_ACKED
] = {
856 .procname
= "nf_conntrack_sctp_timeout_heartbeat_acked",
857 .maxlen
= sizeof(unsigned int),
859 .proc_handler
= proc_dointvec_jiffies
,
862 #ifdef CONFIG_NF_CT_PROTO_DCCP
863 [NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_REQUEST
] = {
864 .procname
= "nf_conntrack_dccp_timeout_request",
865 .maxlen
= sizeof(unsigned int),
867 .proc_handler
= proc_dointvec_jiffies
,
869 [NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_RESPOND
] = {
870 .procname
= "nf_conntrack_dccp_timeout_respond",
871 .maxlen
= sizeof(unsigned int),
873 .proc_handler
= proc_dointvec_jiffies
,
875 [NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_PARTOPEN
] = {
876 .procname
= "nf_conntrack_dccp_timeout_partopen",
877 .maxlen
= sizeof(unsigned int),
879 .proc_handler
= proc_dointvec_jiffies
,
881 [NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_OPEN
] = {
882 .procname
= "nf_conntrack_dccp_timeout_open",
883 .maxlen
= sizeof(unsigned int),
885 .proc_handler
= proc_dointvec_jiffies
,
887 [NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_CLOSEREQ
] = {
888 .procname
= "nf_conntrack_dccp_timeout_closereq",
889 .maxlen
= sizeof(unsigned int),
891 .proc_handler
= proc_dointvec_jiffies
,
893 [NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_CLOSING
] = {
894 .procname
= "nf_conntrack_dccp_timeout_closing",
895 .maxlen
= sizeof(unsigned int),
897 .proc_handler
= proc_dointvec_jiffies
,
899 [NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_TIMEWAIT
] = {
900 .procname
= "nf_conntrack_dccp_timeout_timewait",
901 .maxlen
= sizeof(unsigned int),
903 .proc_handler
= proc_dointvec_jiffies
,
905 [NF_SYSCTL_CT_PROTO_DCCP_LOOSE
] = {
906 .procname
= "nf_conntrack_dccp_loose",
907 .maxlen
= sizeof(int),
909 .proc_handler
= proc_dointvec_minmax
,
910 .extra1
= SYSCTL_ZERO
,
911 .extra2
= SYSCTL_ONE
,
914 #ifdef CONFIG_NF_CT_PROTO_GRE
915 [NF_SYSCTL_CT_PROTO_TIMEOUT_GRE
] = {
916 .procname
= "nf_conntrack_gre_timeout",
917 .maxlen
= sizeof(unsigned int),
919 .proc_handler
= proc_dointvec_jiffies
,
921 [NF_SYSCTL_CT_PROTO_TIMEOUT_GRE_STREAM
] = {
922 .procname
= "nf_conntrack_gre_timeout_stream",
923 .maxlen
= sizeof(unsigned int),
925 .proc_handler
= proc_dointvec_jiffies
,
931 static struct ctl_table nf_ct_netfilter_table
[] = {
933 .procname
= "nf_conntrack_max",
934 .data
= &nf_conntrack_max
,
935 .maxlen
= sizeof(int),
937 .proc_handler
= proc_dointvec
,
942 static void nf_conntrack_standalone_init_tcp_sysctl(struct net
*net
,
943 struct ctl_table
*table
)
945 struct nf_tcp_net
*tn
= nf_tcp_pernet(net
);
947 #define XASSIGN(XNAME, tn) \
948 table[NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_ ## XNAME].data = \
949 &(tn)->timeouts[TCP_CONNTRACK_ ## XNAME]
951 XASSIGN(SYN_SENT
, tn
);
952 XASSIGN(SYN_RECV
, tn
);
953 XASSIGN(ESTABLISHED
, tn
);
954 XASSIGN(FIN_WAIT
, tn
);
955 XASSIGN(CLOSE_WAIT
, tn
);
956 XASSIGN(LAST_ACK
, tn
);
957 XASSIGN(TIME_WAIT
, tn
);
959 XASSIGN(RETRANS
, tn
);
962 #define XASSIGN(XNAME, rval) \
963 table[NF_SYSCTL_CT_PROTO_TCP_ ## XNAME].data = (rval)
965 XASSIGN(LOOSE
, &tn
->tcp_loose
);
966 XASSIGN(LIBERAL
, &tn
->tcp_be_liberal
);
967 XASSIGN(MAX_RETRANS
, &tn
->tcp_max_retrans
);
971 static void nf_conntrack_standalone_init_sctp_sysctl(struct net
*net
,
972 struct ctl_table
*table
)
974 #ifdef CONFIG_NF_CT_PROTO_SCTP
975 struct nf_sctp_net
*sn
= nf_sctp_pernet(net
);
977 #define XASSIGN(XNAME, sn) \
978 table[NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_ ## XNAME].data = \
979 &(sn)->timeouts[SCTP_CONNTRACK_ ## XNAME]
982 XASSIGN(COOKIE_WAIT
, sn
);
983 XASSIGN(COOKIE_ECHOED
, sn
);
984 XASSIGN(ESTABLISHED
, sn
);
985 XASSIGN(SHUTDOWN_SENT
, sn
);
986 XASSIGN(SHUTDOWN_RECD
, sn
);
987 XASSIGN(SHUTDOWN_ACK_SENT
, sn
);
988 XASSIGN(HEARTBEAT_SENT
, sn
);
989 XASSIGN(HEARTBEAT_ACKED
, sn
);
994 static void nf_conntrack_standalone_init_dccp_sysctl(struct net
*net
,
995 struct ctl_table
*table
)
997 #ifdef CONFIG_NF_CT_PROTO_DCCP
998 struct nf_dccp_net
*dn
= nf_dccp_pernet(net
);
1000 #define XASSIGN(XNAME, dn) \
1001 table[NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_ ## XNAME].data = \
1002 &(dn)->dccp_timeout[CT_DCCP_ ## XNAME]
1004 XASSIGN(REQUEST
, dn
);
1005 XASSIGN(RESPOND
, dn
);
1006 XASSIGN(PARTOPEN
, dn
);
1008 XASSIGN(CLOSEREQ
, dn
);
1009 XASSIGN(CLOSING
, dn
);
1010 XASSIGN(TIMEWAIT
, dn
);
1013 table
[NF_SYSCTL_CT_PROTO_DCCP_LOOSE
].data
= &dn
->dccp_loose
;
1017 static void nf_conntrack_standalone_init_gre_sysctl(struct net
*net
,
1018 struct ctl_table
*table
)
1020 #ifdef CONFIG_NF_CT_PROTO_GRE
1021 struct nf_gre_net
*gn
= nf_gre_pernet(net
);
1023 table
[NF_SYSCTL_CT_PROTO_TIMEOUT_GRE
].data
= &gn
->timeouts
[GRE_CT_UNREPLIED
];
1024 table
[NF_SYSCTL_CT_PROTO_TIMEOUT_GRE_STREAM
].data
= &gn
->timeouts
[GRE_CT_REPLIED
];
1028 static int nf_conntrack_standalone_init_sysctl(struct net
*net
)
1030 struct nf_udp_net
*un
= nf_udp_pernet(net
);
1031 struct ctl_table
*table
;
1033 BUILD_BUG_ON(ARRAY_SIZE(nf_ct_sysctl_table
) != NF_SYSCTL_CT_LAST_SYSCTL
);
1035 table
= kmemdup(nf_ct_sysctl_table
, sizeof(nf_ct_sysctl_table
),
1040 table
[NF_SYSCTL_CT_COUNT
].data
= &net
->ct
.count
;
1041 table
[NF_SYSCTL_CT_CHECKSUM
].data
= &net
->ct
.sysctl_checksum
;
1042 table
[NF_SYSCTL_CT_LOG_INVALID
].data
= &net
->ct
.sysctl_log_invalid
;
1043 table
[NF_SYSCTL_CT_ACCT
].data
= &net
->ct
.sysctl_acct
;
1044 table
[NF_SYSCTL_CT_HELPER
].data
= &net
->ct
.sysctl_auto_assign_helper
;
1045 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1046 table
[NF_SYSCTL_CT_EVENTS
].data
= &net
->ct
.sysctl_events
;
1048 #ifdef CONFIG_NF_CONNTRACK_TIMESTAMP
1049 table
[NF_SYSCTL_CT_TIMESTAMP
].data
= &net
->ct
.sysctl_tstamp
;
1051 table
[NF_SYSCTL_CT_PROTO_TIMEOUT_GENERIC
].data
= &nf_generic_pernet(net
)->timeout
;
1052 table
[NF_SYSCTL_CT_PROTO_TIMEOUT_ICMP
].data
= &nf_icmp_pernet(net
)->timeout
;
1053 table
[NF_SYSCTL_CT_PROTO_TIMEOUT_ICMPV6
].data
= &nf_icmpv6_pernet(net
)->timeout
;
1054 table
[NF_SYSCTL_CT_PROTO_TIMEOUT_UDP
].data
= &un
->timeouts
[UDP_CT_UNREPLIED
];
1055 table
[NF_SYSCTL_CT_PROTO_TIMEOUT_UDP_STREAM
].data
= &un
->timeouts
[UDP_CT_REPLIED
];
1057 nf_conntrack_standalone_init_tcp_sysctl(net
, table
);
1058 nf_conntrack_standalone_init_sctp_sysctl(net
, table
);
1059 nf_conntrack_standalone_init_dccp_sysctl(net
, table
);
1060 nf_conntrack_standalone_init_gre_sysctl(net
, table
);
1062 /* Don't allow unprivileged users to alter certain sysctls */
1063 if (net
->user_ns
!= &init_user_ns
) {
1064 table
[NF_SYSCTL_CT_MAX
].mode
= 0444;
1065 table
[NF_SYSCTL_CT_EXPECT_MAX
].mode
= 0444;
1066 table
[NF_SYSCTL_CT_HELPER
].mode
= 0444;
1067 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1068 table
[NF_SYSCTL_CT_EVENTS
].mode
= 0444;
1070 table
[NF_SYSCTL_CT_BUCKETS
].mode
= 0444;
1071 } else if (!net_eq(&init_net
, net
)) {
1072 table
[NF_SYSCTL_CT_BUCKETS
].mode
= 0444;
1075 net
->ct
.sysctl_header
= register_net_sysctl(net
, "net/netfilter", table
);
1076 if (!net
->ct
.sysctl_header
)
1077 goto out_unregister_netfilter
;
1081 out_unregister_netfilter
:
1086 static void nf_conntrack_standalone_fini_sysctl(struct net
*net
)
1088 struct ctl_table
*table
;
1090 table
= net
->ct
.sysctl_header
->ctl_table_arg
;
1091 unregister_net_sysctl_table(net
->ct
.sysctl_header
);
1095 static int nf_conntrack_standalone_init_sysctl(struct net
*net
)
1100 static void nf_conntrack_standalone_fini_sysctl(struct net
*net
)
1103 #endif /* CONFIG_SYSCTL */
1105 static void nf_conntrack_fini_net(struct net
*net
)
1108 nf_ct_netns_put(net
, NFPROTO_INET
);
1110 nf_conntrack_standalone_fini_proc(net
);
1111 nf_conntrack_standalone_fini_sysctl(net
);
1114 static int nf_conntrack_pernet_init(struct net
*net
)
1118 net
->ct
.sysctl_checksum
= 1;
1120 ret
= nf_conntrack_standalone_init_sysctl(net
);
1124 ret
= nf_conntrack_standalone_init_proc(net
);
1128 ret
= nf_conntrack_init_net(net
);
1133 ret
= nf_ct_netns_get(net
, NFPROTO_INET
);
1141 nf_conntrack_cleanup_net(net
);
1143 nf_conntrack_standalone_fini_proc(net
);
1145 nf_conntrack_standalone_fini_sysctl(net
);
1149 static void nf_conntrack_pernet_exit(struct list_head
*net_exit_list
)
1153 list_for_each_entry(net
, net_exit_list
, exit_list
)
1154 nf_conntrack_fini_net(net
);
1156 nf_conntrack_cleanup_net_list(net_exit_list
);
1159 static struct pernet_operations nf_conntrack_net_ops
= {
1160 .init
= nf_conntrack_pernet_init
,
1161 .exit_batch
= nf_conntrack_pernet_exit
,
1162 .id
= &nf_conntrack_net_id
,
1163 .size
= sizeof(struct nf_conntrack_net
),
1166 static int __init
nf_conntrack_standalone_init(void)
1168 int ret
= nf_conntrack_init_start();
1172 BUILD_BUG_ON(NFCT_INFOMASK
<= IP_CT_NUMBER
);
1174 #ifdef CONFIG_SYSCTL
1175 nf_ct_netfilter_header
=
1176 register_net_sysctl(&init_net
, "net", nf_ct_netfilter_table
);
1177 if (!nf_ct_netfilter_header
) {
1178 pr_err("nf_conntrack: can't register to sysctl.\n");
1183 nf_conntrack_htable_size_user
= nf_conntrack_htable_size
;
1186 ret
= register_pernet_subsys(&nf_conntrack_net_ops
);
1190 nf_conntrack_init_end();
1194 #ifdef CONFIG_SYSCTL
1195 unregister_net_sysctl_table(nf_ct_netfilter_header
);
1198 nf_conntrack_cleanup_end();
1203 static void __exit
nf_conntrack_standalone_fini(void)
1205 nf_conntrack_cleanup_start();
1206 unregister_pernet_subsys(&nf_conntrack_net_ops
);
1207 #ifdef CONFIG_SYSCTL
1208 unregister_net_sysctl_table(nf_ct_netfilter_header
);
1210 nf_conntrack_cleanup_end();
1213 module_init(nf_conntrack_standalone_init
);
1214 module_exit(nf_conntrack_standalone_fini
);