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 ret
= proc_dointvec(table
, write
, buffer
, lenp
, ppos
);
527 if (ret
< 0 || !write
)
530 /* update ret, we might not be able to satisfy request */
531 ret
= nf_conntrack_hash_resize(nf_conntrack_htable_size_user
);
533 /* update it to the actual value used by conntrack */
534 nf_conntrack_htable_size_user
= nf_conntrack_htable_size
;
538 static struct ctl_table_header
*nf_ct_netfilter_header
;
540 enum nf_ct_sysctl_index
{
543 NF_SYSCTL_CT_BUCKETS
,
544 NF_SYSCTL_CT_CHECKSUM
,
545 NF_SYSCTL_CT_LOG_INVALID
,
546 NF_SYSCTL_CT_EXPECT_MAX
,
549 #ifdef CONFIG_NF_CONNTRACK_EVENTS
552 #ifdef CONFIG_NF_CONNTRACK_TIMESTAMP
553 NF_SYSCTL_CT_TIMESTAMP
,
555 NF_SYSCTL_CT_PROTO_TIMEOUT_GENERIC
,
556 NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_SYN_SENT
,
557 NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_SYN_RECV
,
558 NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_ESTABLISHED
,
559 NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_FIN_WAIT
,
560 NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_CLOSE_WAIT
,
561 NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_LAST_ACK
,
562 NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_TIME_WAIT
,
563 NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_CLOSE
,
564 NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_RETRANS
,
565 NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_UNACK
,
566 NF_SYSCTL_CT_PROTO_TCP_LOOSE
,
567 NF_SYSCTL_CT_PROTO_TCP_LIBERAL
,
568 NF_SYSCTL_CT_PROTO_TCP_MAX_RETRANS
,
569 NF_SYSCTL_CT_PROTO_TIMEOUT_UDP
,
570 NF_SYSCTL_CT_PROTO_TIMEOUT_UDP_STREAM
,
571 NF_SYSCTL_CT_PROTO_TIMEOUT_ICMP
,
572 NF_SYSCTL_CT_PROTO_TIMEOUT_ICMPV6
,
573 #ifdef CONFIG_NF_CT_PROTO_SCTP
574 NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_CLOSED
,
575 NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_COOKIE_WAIT
,
576 NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_COOKIE_ECHOED
,
577 NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_ESTABLISHED
,
578 NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_SHUTDOWN_SENT
,
579 NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_SHUTDOWN_RECD
,
580 NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_SHUTDOWN_ACK_SENT
,
581 NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_HEARTBEAT_SENT
,
582 NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_HEARTBEAT_ACKED
,
584 #ifdef CONFIG_NF_CT_PROTO_DCCP
585 NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_REQUEST
,
586 NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_RESPOND
,
587 NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_PARTOPEN
,
588 NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_OPEN
,
589 NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_CLOSEREQ
,
590 NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_CLOSING
,
591 NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_TIMEWAIT
,
592 NF_SYSCTL_CT_PROTO_DCCP_LOOSE
,
594 #ifdef CONFIG_NF_CT_PROTO_GRE
595 NF_SYSCTL_CT_PROTO_TIMEOUT_GRE
,
596 NF_SYSCTL_CT_PROTO_TIMEOUT_GRE_STREAM
,
599 __NF_SYSCTL_CT_LAST_SYSCTL
,
602 #define NF_SYSCTL_CT_LAST_SYSCTL (__NF_SYSCTL_CT_LAST_SYSCTL + 1)
604 static struct ctl_table nf_ct_sysctl_table
[] = {
605 [NF_SYSCTL_CT_MAX
] = {
606 .procname
= "nf_conntrack_max",
607 .data
= &nf_conntrack_max
,
608 .maxlen
= sizeof(int),
610 .proc_handler
= proc_dointvec
,
612 [NF_SYSCTL_CT_COUNT
] = {
613 .procname
= "nf_conntrack_count",
614 .data
= &init_net
.ct
.count
,
615 .maxlen
= sizeof(int),
617 .proc_handler
= proc_dointvec
,
619 [NF_SYSCTL_CT_BUCKETS
] = {
620 .procname
= "nf_conntrack_buckets",
621 .data
= &nf_conntrack_htable_size_user
,
622 .maxlen
= sizeof(unsigned int),
624 .proc_handler
= nf_conntrack_hash_sysctl
,
626 [NF_SYSCTL_CT_CHECKSUM
] = {
627 .procname
= "nf_conntrack_checksum",
628 .data
= &init_net
.ct
.sysctl_checksum
,
629 .maxlen
= sizeof(int),
631 .proc_handler
= proc_dointvec_minmax
,
632 .extra1
= SYSCTL_ZERO
,
633 .extra2
= SYSCTL_ONE
,
635 [NF_SYSCTL_CT_LOG_INVALID
] = {
636 .procname
= "nf_conntrack_log_invalid",
637 .data
= &init_net
.ct
.sysctl_log_invalid
,
638 .maxlen
= sizeof(unsigned int),
640 .proc_handler
= proc_dointvec_minmax
,
641 .extra1
= &log_invalid_proto_min
,
642 .extra2
= &log_invalid_proto_max
,
644 [NF_SYSCTL_CT_EXPECT_MAX
] = {
645 .procname
= "nf_conntrack_expect_max",
646 .data
= &nf_ct_expect_max
,
647 .maxlen
= sizeof(int),
649 .proc_handler
= proc_dointvec
,
651 [NF_SYSCTL_CT_ACCT
] = {
652 .procname
= "nf_conntrack_acct",
653 .data
= &init_net
.ct
.sysctl_acct
,
654 .maxlen
= sizeof(int),
656 .proc_handler
= proc_dointvec_minmax
,
657 .extra1
= SYSCTL_ZERO
,
658 .extra2
= SYSCTL_ONE
,
660 [NF_SYSCTL_CT_HELPER
] = {
661 .procname
= "nf_conntrack_helper",
662 .data
= &init_net
.ct
.sysctl_auto_assign_helper
,
663 .maxlen
= sizeof(int),
665 .proc_handler
= proc_dointvec_minmax
,
666 .extra1
= SYSCTL_ZERO
,
667 .extra2
= SYSCTL_ONE
,
669 #ifdef CONFIG_NF_CONNTRACK_EVENTS
670 [NF_SYSCTL_CT_EVENTS
] = {
671 .procname
= "nf_conntrack_events",
672 .data
= &init_net
.ct
.sysctl_events
,
673 .maxlen
= sizeof(int),
675 .proc_handler
= proc_dointvec_minmax
,
676 .extra1
= SYSCTL_ZERO
,
677 .extra2
= SYSCTL_ONE
,
680 #ifdef CONFIG_NF_CONNTRACK_TIMESTAMP
681 [NF_SYSCTL_CT_TIMESTAMP
] = {
682 .procname
= "nf_conntrack_timestamp",
683 .data
= &init_net
.ct
.sysctl_tstamp
,
684 .maxlen
= sizeof(int),
686 .proc_handler
= proc_dointvec_minmax
,
687 .extra1
= SYSCTL_ZERO
,
688 .extra2
= SYSCTL_ONE
,
691 [NF_SYSCTL_CT_PROTO_TIMEOUT_GENERIC
] = {
692 .procname
= "nf_conntrack_generic_timeout",
693 .maxlen
= sizeof(unsigned int),
695 .proc_handler
= proc_dointvec_jiffies
,
697 [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_SYN_SENT
] = {
698 .procname
= "nf_conntrack_tcp_timeout_syn_sent",
699 .maxlen
= sizeof(unsigned int),
701 .proc_handler
= proc_dointvec_jiffies
,
703 [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_SYN_RECV
] = {
704 .procname
= "nf_conntrack_tcp_timeout_syn_recv",
705 .maxlen
= sizeof(unsigned int),
707 .proc_handler
= proc_dointvec_jiffies
,
709 [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_ESTABLISHED
] = {
710 .procname
= "nf_conntrack_tcp_timeout_established",
711 .maxlen
= sizeof(unsigned int),
713 .proc_handler
= proc_dointvec_jiffies
,
715 [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_FIN_WAIT
] = {
716 .procname
= "nf_conntrack_tcp_timeout_fin_wait",
717 .maxlen
= sizeof(unsigned int),
719 .proc_handler
= proc_dointvec_jiffies
,
721 [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_CLOSE_WAIT
] = {
722 .procname
= "nf_conntrack_tcp_timeout_close_wait",
723 .maxlen
= sizeof(unsigned int),
725 .proc_handler
= proc_dointvec_jiffies
,
727 [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_LAST_ACK
] = {
728 .procname
= "nf_conntrack_tcp_timeout_last_ack",
729 .maxlen
= sizeof(unsigned int),
731 .proc_handler
= proc_dointvec_jiffies
,
733 [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_TIME_WAIT
] = {
734 .procname
= "nf_conntrack_tcp_timeout_time_wait",
735 .maxlen
= sizeof(unsigned int),
737 .proc_handler
= proc_dointvec_jiffies
,
739 [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_CLOSE
] = {
740 .procname
= "nf_conntrack_tcp_timeout_close",
741 .maxlen
= sizeof(unsigned int),
743 .proc_handler
= proc_dointvec_jiffies
,
745 [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_RETRANS
] = {
746 .procname
= "nf_conntrack_tcp_timeout_max_retrans",
747 .maxlen
= sizeof(unsigned int),
749 .proc_handler
= proc_dointvec_jiffies
,
751 [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_UNACK
] = {
752 .procname
= "nf_conntrack_tcp_timeout_unacknowledged",
753 .maxlen
= sizeof(unsigned int),
755 .proc_handler
= proc_dointvec_jiffies
,
757 [NF_SYSCTL_CT_PROTO_TCP_LOOSE
] = {
758 .procname
= "nf_conntrack_tcp_loose",
759 .maxlen
= sizeof(int),
761 .proc_handler
= proc_dointvec_minmax
,
762 .extra1
= SYSCTL_ZERO
,
763 .extra2
= SYSCTL_ONE
,
765 [NF_SYSCTL_CT_PROTO_TCP_LIBERAL
] = {
766 .procname
= "nf_conntrack_tcp_be_liberal",
767 .maxlen
= sizeof(int),
769 .proc_handler
= proc_dointvec_minmax
,
770 .extra1
= SYSCTL_ZERO
,
771 .extra2
= SYSCTL_ONE
,
773 [NF_SYSCTL_CT_PROTO_TCP_MAX_RETRANS
] = {
774 .procname
= "nf_conntrack_tcp_max_retrans",
775 .maxlen
= sizeof(unsigned int),
777 .proc_handler
= proc_dointvec
,
779 [NF_SYSCTL_CT_PROTO_TIMEOUT_UDP
] = {
780 .procname
= "nf_conntrack_udp_timeout",
781 .maxlen
= sizeof(unsigned int),
783 .proc_handler
= proc_dointvec_jiffies
,
785 [NF_SYSCTL_CT_PROTO_TIMEOUT_UDP_STREAM
] = {
786 .procname
= "nf_conntrack_udp_timeout_stream",
787 .maxlen
= sizeof(unsigned int),
789 .proc_handler
= proc_dointvec_jiffies
,
791 [NF_SYSCTL_CT_PROTO_TIMEOUT_ICMP
] = {
792 .procname
= "nf_conntrack_icmp_timeout",
793 .maxlen
= sizeof(unsigned int),
795 .proc_handler
= proc_dointvec_jiffies
,
797 [NF_SYSCTL_CT_PROTO_TIMEOUT_ICMPV6
] = {
798 .procname
= "nf_conntrack_icmpv6_timeout",
799 .maxlen
= sizeof(unsigned int),
801 .proc_handler
= proc_dointvec_jiffies
,
803 #ifdef CONFIG_NF_CT_PROTO_SCTP
804 [NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_CLOSED
] = {
805 .procname
= "nf_conntrack_sctp_timeout_closed",
806 .maxlen
= sizeof(unsigned int),
808 .proc_handler
= proc_dointvec_jiffies
,
810 [NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_COOKIE_WAIT
] = {
811 .procname
= "nf_conntrack_sctp_timeout_cookie_wait",
812 .maxlen
= sizeof(unsigned int),
814 .proc_handler
= proc_dointvec_jiffies
,
816 [NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_COOKIE_ECHOED
] = {
817 .procname
= "nf_conntrack_sctp_timeout_cookie_echoed",
818 .maxlen
= sizeof(unsigned int),
820 .proc_handler
= proc_dointvec_jiffies
,
822 [NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_ESTABLISHED
] = {
823 .procname
= "nf_conntrack_sctp_timeout_established",
824 .maxlen
= sizeof(unsigned int),
826 .proc_handler
= proc_dointvec_jiffies
,
828 [NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_SHUTDOWN_SENT
] = {
829 .procname
= "nf_conntrack_sctp_timeout_shutdown_sent",
830 .maxlen
= sizeof(unsigned int),
832 .proc_handler
= proc_dointvec_jiffies
,
834 [NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_SHUTDOWN_RECD
] = {
835 .procname
= "nf_conntrack_sctp_timeout_shutdown_recd",
836 .maxlen
= sizeof(unsigned int),
838 .proc_handler
= proc_dointvec_jiffies
,
840 [NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_SHUTDOWN_ACK_SENT
] = {
841 .procname
= "nf_conntrack_sctp_timeout_shutdown_ack_sent",
842 .maxlen
= sizeof(unsigned int),
844 .proc_handler
= proc_dointvec_jiffies
,
846 [NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_HEARTBEAT_SENT
] = {
847 .procname
= "nf_conntrack_sctp_timeout_heartbeat_sent",
848 .maxlen
= sizeof(unsigned int),
850 .proc_handler
= proc_dointvec_jiffies
,
852 [NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_HEARTBEAT_ACKED
] = {
853 .procname
= "nf_conntrack_sctp_timeout_heartbeat_acked",
854 .maxlen
= sizeof(unsigned int),
856 .proc_handler
= proc_dointvec_jiffies
,
859 #ifdef CONFIG_NF_CT_PROTO_DCCP
860 [NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_REQUEST
] = {
861 .procname
= "nf_conntrack_dccp_timeout_request",
862 .maxlen
= sizeof(unsigned int),
864 .proc_handler
= proc_dointvec_jiffies
,
866 [NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_RESPOND
] = {
867 .procname
= "nf_conntrack_dccp_timeout_respond",
868 .maxlen
= sizeof(unsigned int),
870 .proc_handler
= proc_dointvec_jiffies
,
872 [NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_PARTOPEN
] = {
873 .procname
= "nf_conntrack_dccp_timeout_partopen",
874 .maxlen
= sizeof(unsigned int),
876 .proc_handler
= proc_dointvec_jiffies
,
878 [NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_OPEN
] = {
879 .procname
= "nf_conntrack_dccp_timeout_open",
880 .maxlen
= sizeof(unsigned int),
882 .proc_handler
= proc_dointvec_jiffies
,
884 [NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_CLOSEREQ
] = {
885 .procname
= "nf_conntrack_dccp_timeout_closereq",
886 .maxlen
= sizeof(unsigned int),
888 .proc_handler
= proc_dointvec_jiffies
,
890 [NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_CLOSING
] = {
891 .procname
= "nf_conntrack_dccp_timeout_closing",
892 .maxlen
= sizeof(unsigned int),
894 .proc_handler
= proc_dointvec_jiffies
,
896 [NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_TIMEWAIT
] = {
897 .procname
= "nf_conntrack_dccp_timeout_timewait",
898 .maxlen
= sizeof(unsigned int),
900 .proc_handler
= proc_dointvec_jiffies
,
902 [NF_SYSCTL_CT_PROTO_DCCP_LOOSE
] = {
903 .procname
= "nf_conntrack_dccp_loose",
904 .maxlen
= sizeof(int),
906 .proc_handler
= proc_dointvec_minmax
,
907 .extra1
= SYSCTL_ZERO
,
908 .extra2
= SYSCTL_ONE
,
911 #ifdef CONFIG_NF_CT_PROTO_GRE
912 [NF_SYSCTL_CT_PROTO_TIMEOUT_GRE
] = {
913 .procname
= "nf_conntrack_gre_timeout",
914 .maxlen
= sizeof(unsigned int),
916 .proc_handler
= proc_dointvec_jiffies
,
918 [NF_SYSCTL_CT_PROTO_TIMEOUT_GRE_STREAM
] = {
919 .procname
= "nf_conntrack_gre_timeout_stream",
920 .maxlen
= sizeof(unsigned int),
922 .proc_handler
= proc_dointvec_jiffies
,
928 static struct ctl_table nf_ct_netfilter_table
[] = {
930 .procname
= "nf_conntrack_max",
931 .data
= &nf_conntrack_max
,
932 .maxlen
= sizeof(int),
934 .proc_handler
= proc_dointvec
,
939 static void nf_conntrack_standalone_init_tcp_sysctl(struct net
*net
,
940 struct ctl_table
*table
)
942 struct nf_tcp_net
*tn
= nf_tcp_pernet(net
);
944 #define XASSIGN(XNAME, tn) \
945 table[NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_ ## XNAME].data = \
946 &(tn)->timeouts[TCP_CONNTRACK_ ## XNAME]
948 XASSIGN(SYN_SENT
, tn
);
949 XASSIGN(SYN_RECV
, tn
);
950 XASSIGN(ESTABLISHED
, tn
);
951 XASSIGN(FIN_WAIT
, tn
);
952 XASSIGN(CLOSE_WAIT
, tn
);
953 XASSIGN(LAST_ACK
, tn
);
954 XASSIGN(TIME_WAIT
, tn
);
956 XASSIGN(RETRANS
, tn
);
959 #define XASSIGN(XNAME, rval) \
960 table[NF_SYSCTL_CT_PROTO_TCP_ ## XNAME].data = (rval)
962 XASSIGN(LOOSE
, &tn
->tcp_loose
);
963 XASSIGN(LIBERAL
, &tn
->tcp_be_liberal
);
964 XASSIGN(MAX_RETRANS
, &tn
->tcp_max_retrans
);
968 static void nf_conntrack_standalone_init_sctp_sysctl(struct net
*net
,
969 struct ctl_table
*table
)
971 #ifdef CONFIG_NF_CT_PROTO_SCTP
972 struct nf_sctp_net
*sn
= nf_sctp_pernet(net
);
974 #define XASSIGN(XNAME, sn) \
975 table[NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_ ## XNAME].data = \
976 &(sn)->timeouts[SCTP_CONNTRACK_ ## XNAME]
979 XASSIGN(COOKIE_WAIT
, sn
);
980 XASSIGN(COOKIE_ECHOED
, sn
);
981 XASSIGN(ESTABLISHED
, sn
);
982 XASSIGN(SHUTDOWN_SENT
, sn
);
983 XASSIGN(SHUTDOWN_RECD
, sn
);
984 XASSIGN(SHUTDOWN_ACK_SENT
, sn
);
985 XASSIGN(HEARTBEAT_SENT
, sn
);
986 XASSIGN(HEARTBEAT_ACKED
, sn
);
991 static void nf_conntrack_standalone_init_dccp_sysctl(struct net
*net
,
992 struct ctl_table
*table
)
994 #ifdef CONFIG_NF_CT_PROTO_DCCP
995 struct nf_dccp_net
*dn
= nf_dccp_pernet(net
);
997 #define XASSIGN(XNAME, dn) \
998 table[NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_ ## XNAME].data = \
999 &(dn)->dccp_timeout[CT_DCCP_ ## XNAME]
1001 XASSIGN(REQUEST
, dn
);
1002 XASSIGN(RESPOND
, dn
);
1003 XASSIGN(PARTOPEN
, dn
);
1005 XASSIGN(CLOSEREQ
, dn
);
1006 XASSIGN(CLOSING
, dn
);
1007 XASSIGN(TIMEWAIT
, dn
);
1010 table
[NF_SYSCTL_CT_PROTO_DCCP_LOOSE
].data
= &dn
->dccp_loose
;
1014 static void nf_conntrack_standalone_init_gre_sysctl(struct net
*net
,
1015 struct ctl_table
*table
)
1017 #ifdef CONFIG_NF_CT_PROTO_GRE
1018 struct nf_gre_net
*gn
= nf_gre_pernet(net
);
1020 table
[NF_SYSCTL_CT_PROTO_TIMEOUT_GRE
].data
= &gn
->timeouts
[GRE_CT_UNREPLIED
];
1021 table
[NF_SYSCTL_CT_PROTO_TIMEOUT_GRE_STREAM
].data
= &gn
->timeouts
[GRE_CT_REPLIED
];
1025 static int nf_conntrack_standalone_init_sysctl(struct net
*net
)
1027 struct nf_udp_net
*un
= nf_udp_pernet(net
);
1028 struct ctl_table
*table
;
1030 BUILD_BUG_ON(ARRAY_SIZE(nf_ct_sysctl_table
) != NF_SYSCTL_CT_LAST_SYSCTL
);
1032 table
= kmemdup(nf_ct_sysctl_table
, sizeof(nf_ct_sysctl_table
),
1037 table
[NF_SYSCTL_CT_COUNT
].data
= &net
->ct
.count
;
1038 table
[NF_SYSCTL_CT_CHECKSUM
].data
= &net
->ct
.sysctl_checksum
;
1039 table
[NF_SYSCTL_CT_LOG_INVALID
].data
= &net
->ct
.sysctl_log_invalid
;
1040 table
[NF_SYSCTL_CT_ACCT
].data
= &net
->ct
.sysctl_acct
;
1041 table
[NF_SYSCTL_CT_HELPER
].data
= &net
->ct
.sysctl_auto_assign_helper
;
1042 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1043 table
[NF_SYSCTL_CT_EVENTS
].data
= &net
->ct
.sysctl_events
;
1045 #ifdef CONFIG_NF_CONNTRACK_TIMESTAMP
1046 table
[NF_SYSCTL_CT_TIMESTAMP
].data
= &net
->ct
.sysctl_tstamp
;
1048 table
[NF_SYSCTL_CT_PROTO_TIMEOUT_GENERIC
].data
= &nf_generic_pernet(net
)->timeout
;
1049 table
[NF_SYSCTL_CT_PROTO_TIMEOUT_ICMP
].data
= &nf_icmp_pernet(net
)->timeout
;
1050 table
[NF_SYSCTL_CT_PROTO_TIMEOUT_ICMPV6
].data
= &nf_icmpv6_pernet(net
)->timeout
;
1051 table
[NF_SYSCTL_CT_PROTO_TIMEOUT_UDP
].data
= &un
->timeouts
[UDP_CT_UNREPLIED
];
1052 table
[NF_SYSCTL_CT_PROTO_TIMEOUT_UDP_STREAM
].data
= &un
->timeouts
[UDP_CT_REPLIED
];
1054 nf_conntrack_standalone_init_tcp_sysctl(net
, table
);
1055 nf_conntrack_standalone_init_sctp_sysctl(net
, table
);
1056 nf_conntrack_standalone_init_dccp_sysctl(net
, table
);
1057 nf_conntrack_standalone_init_gre_sysctl(net
, table
);
1059 /* Don't allow unprivileged users to alter certain sysctls */
1060 if (net
->user_ns
!= &init_user_ns
) {
1061 table
[NF_SYSCTL_CT_MAX
].mode
= 0444;
1062 table
[NF_SYSCTL_CT_EXPECT_MAX
].mode
= 0444;
1063 table
[NF_SYSCTL_CT_HELPER
].mode
= 0444;
1064 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1065 table
[NF_SYSCTL_CT_EVENTS
].mode
= 0444;
1067 table
[NF_SYSCTL_CT_BUCKETS
].mode
= 0444;
1068 } else if (!net_eq(&init_net
, net
)) {
1069 table
[NF_SYSCTL_CT_BUCKETS
].mode
= 0444;
1072 net
->ct
.sysctl_header
= register_net_sysctl(net
, "net/netfilter", table
);
1073 if (!net
->ct
.sysctl_header
)
1074 goto out_unregister_netfilter
;
1078 out_unregister_netfilter
:
1083 static void nf_conntrack_standalone_fini_sysctl(struct net
*net
)
1085 struct ctl_table
*table
;
1087 table
= net
->ct
.sysctl_header
->ctl_table_arg
;
1088 unregister_net_sysctl_table(net
->ct
.sysctl_header
);
1092 static int nf_conntrack_standalone_init_sysctl(struct net
*net
)
1097 static void nf_conntrack_standalone_fini_sysctl(struct net
*net
)
1100 #endif /* CONFIG_SYSCTL */
1102 static void nf_conntrack_fini_net(struct net
*net
)
1105 nf_ct_netns_put(net
, NFPROTO_INET
);
1107 nf_conntrack_standalone_fini_proc(net
);
1108 nf_conntrack_standalone_fini_sysctl(net
);
1111 static int nf_conntrack_pernet_init(struct net
*net
)
1115 net
->ct
.sysctl_checksum
= 1;
1117 ret
= nf_conntrack_standalone_init_sysctl(net
);
1121 ret
= nf_conntrack_standalone_init_proc(net
);
1125 ret
= nf_conntrack_init_net(net
);
1130 ret
= nf_ct_netns_get(net
, NFPROTO_INET
);
1138 nf_conntrack_cleanup_net(net
);
1140 nf_conntrack_standalone_fini_proc(net
);
1142 nf_conntrack_standalone_fini_sysctl(net
);
1146 static void nf_conntrack_pernet_exit(struct list_head
*net_exit_list
)
1150 list_for_each_entry(net
, net_exit_list
, exit_list
)
1151 nf_conntrack_fini_net(net
);
1153 nf_conntrack_cleanup_net_list(net_exit_list
);
1156 static struct pernet_operations nf_conntrack_net_ops
= {
1157 .init
= nf_conntrack_pernet_init
,
1158 .exit_batch
= nf_conntrack_pernet_exit
,
1159 .id
= &nf_conntrack_net_id
,
1160 .size
= sizeof(struct nf_conntrack_net
),
1163 static int __init
nf_conntrack_standalone_init(void)
1165 int ret
= nf_conntrack_init_start();
1169 BUILD_BUG_ON(NFCT_INFOMASK
<= IP_CT_NUMBER
);
1171 #ifdef CONFIG_SYSCTL
1172 nf_ct_netfilter_header
=
1173 register_net_sysctl(&init_net
, "net", nf_ct_netfilter_table
);
1174 if (!nf_ct_netfilter_header
) {
1175 pr_err("nf_conntrack: can't register to sysctl.\n");
1180 nf_conntrack_htable_size_user
= nf_conntrack_htable_size
;
1183 ret
= register_pernet_subsys(&nf_conntrack_net_ops
);
1187 nf_conntrack_init_end();
1191 #ifdef CONFIG_SYSCTL
1192 unregister_net_sysctl_table(nf_ct_netfilter_header
);
1195 nf_conntrack_cleanup_end();
1200 static void __exit
nf_conntrack_standalone_fini(void)
1202 nf_conntrack_cleanup_start();
1203 unregister_pernet_subsys(&nf_conntrack_net_ops
);
1204 #ifdef CONFIG_SYSCTL
1205 unregister_net_sysctl_table(nf_ct_netfilter_header
);
1207 nf_conntrack_cleanup_end();
1210 module_init(nf_conntrack_standalone_init
);
1211 module_exit(nf_conntrack_standalone_fini
);