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 #ifdef CONFIG_LWTUNNEL
26 #include <net/netfilter/nf_hooks_lwtunnel.h>
28 #include <linux/rculist_nulls.h>
30 static bool enable_hooks __read_mostly;
31 MODULE_PARM_DESC(enable_hooks, "Always enable conntrack hooks");
32 module_param(enable_hooks, bool, 0000);
34 unsigned int nf_conntrack_net_id __read_mostly;
36 #ifdef CONFIG_NF_CONNTRACK_PROCFS
38 print_tuple(struct seq_file *s, const struct nf_conntrack_tuple *tuple,
39 const struct nf_conntrack_l4proto *l4proto)
41 switch (tuple->src.l3num) {
43 seq_printf(s, "src=%pI4 dst=%pI4 ",
44 &tuple->src.u3.ip, &tuple->dst.u3.ip);
47 seq_printf(s, "src=%pI6 dst=%pI6 ",
48 tuple->src.u3.ip6, tuple->dst.u3.ip6);
54 switch (l4proto->l4proto) {
56 seq_printf(s, "type=%u code=%u id=%u ",
57 tuple->dst.u.icmp.type,
58 tuple->dst.u.icmp.code,
59 ntohs(tuple->src.u.icmp.id));
62 seq_printf(s, "sport=%hu dport=%hu ",
63 ntohs(tuple->src.u.tcp.port),
64 ntohs(tuple->dst.u.tcp.port));
68 seq_printf(s, "sport=%hu dport=%hu ",
69 ntohs(tuple->src.u.udp.port),
70 ntohs(tuple->dst.u.udp.port));
74 seq_printf(s, "sport=%hu dport=%hu ",
75 ntohs(tuple->src.u.dccp.port),
76 ntohs(tuple->dst.u.dccp.port));
79 seq_printf(s, "sport=%hu dport=%hu ",
80 ntohs(tuple->src.u.sctp.port),
81 ntohs(tuple->dst.u.sctp.port));
84 seq_printf(s, "type=%u code=%u id=%u ",
85 tuple->dst.u.icmp.type,
86 tuple->dst.u.icmp.code,
87 ntohs(tuple->src.u.icmp.id));
90 seq_printf(s, "srckey=0x%x dstkey=0x%x ",
91 ntohs(tuple->src.u.gre.key),
92 ntohs(tuple->dst.u.gre.key));
98 EXPORT_SYMBOL_GPL(print_tuple);
100 struct ct_iter_state {
101 struct seq_net_private p;
102 struct hlist_nulls_head *hash;
103 unsigned int htable_size;
108 static struct hlist_nulls_node *ct_get_first(struct seq_file *seq)
110 struct ct_iter_state *st = seq->private;
111 struct hlist_nulls_node *n;
114 st->bucket < st->htable_size;
117 hlist_nulls_first_rcu(&st->hash[st->bucket]));
124 static struct hlist_nulls_node *ct_get_next(struct seq_file *seq,
125 struct hlist_nulls_node *head)
127 struct ct_iter_state *st = seq->private;
129 head = rcu_dereference(hlist_nulls_next_rcu(head));
130 while (is_a_nulls(head)) {
131 if (likely(get_nulls_value(head) == st->bucket)) {
132 if (++st->bucket >= st->htable_size)
135 head = rcu_dereference(
136 hlist_nulls_first_rcu(&st->hash[st->bucket]));
141 static struct hlist_nulls_node *ct_get_idx(struct seq_file *seq, loff_t pos)
143 struct hlist_nulls_node *head = ct_get_first(seq);
146 while (pos && (head = ct_get_next(seq, head)))
148 return pos ? NULL : head;
151 static void *ct_seq_start(struct seq_file *seq, loff_t *pos)
154 struct ct_iter_state *st = seq->private;
156 st->time_now = ktime_get_real_ns();
159 nf_conntrack_get_ht(&st->hash, &st->htable_size);
160 return ct_get_idx(seq, *pos);
163 static void *ct_seq_next(struct seq_file *s, void *v, loff_t *pos)
166 return ct_get_next(s, v);
169 static void ct_seq_stop(struct seq_file *s, void *v)
175 #ifdef CONFIG_NF_CONNTRACK_SECMARK
176 static void ct_show_secctx(struct seq_file *s, const struct nf_conn *ct)
182 ret = security_secid_to_secctx(ct->secmark, &secctx, &len);
186 seq_printf(s, "secctx=%s ", secctx);
188 security_release_secctx(secctx, len);
191 static inline void ct_show_secctx(struct seq_file *s, const struct nf_conn *ct)
196 #ifdef CONFIG_NF_CONNTRACK_ZONES
197 static void ct_show_zone(struct seq_file *s, const struct nf_conn *ct,
200 const struct nf_conntrack_zone *zone = nf_ct_zone(ct);
202 if (zone->dir != dir)
205 case NF_CT_DEFAULT_ZONE_DIR:
206 seq_printf(s, "zone=%u ", zone->id);
208 case NF_CT_ZONE_DIR_ORIG:
209 seq_printf(s, "zone-orig=%u ", zone->id);
211 case NF_CT_ZONE_DIR_REPL:
212 seq_printf(s, "zone-reply=%u ", zone->id);
219 static inline void ct_show_zone(struct seq_file *s, const struct nf_conn *ct,
225 #ifdef CONFIG_NF_CONNTRACK_TIMESTAMP
226 static void ct_show_delta_time(struct seq_file *s, const struct nf_conn *ct)
228 struct ct_iter_state *st = s->private;
229 struct nf_conn_tstamp *tstamp;
232 tstamp = nf_conn_tstamp_find(ct);
234 delta_time = st->time_now - tstamp->start;
236 delta_time = div_s64(delta_time, NSEC_PER_SEC);
240 seq_printf(s, "delta-time=%llu ",
241 (unsigned long long)delta_time);
247 ct_show_delta_time(struct seq_file *s, const struct nf_conn *ct)
252 static const char* l3proto_name(u16 proto)
255 case AF_INET: return "ipv4";
256 case AF_INET6: return "ipv6";
262 static const char* l4proto_name(u16 proto)
265 case IPPROTO_ICMP: return "icmp";
266 case IPPROTO_TCP: return "tcp";
267 case IPPROTO_UDP: return "udp";
268 case IPPROTO_DCCP: return "dccp";
269 case IPPROTO_GRE: return "gre";
270 case IPPROTO_SCTP: return "sctp";
271 case IPPROTO_UDPLITE: return "udplite";
272 case IPPROTO_ICMPV6: return "icmpv6";
279 seq_print_acct(struct seq_file *s, const struct nf_conn *ct, int dir)
281 struct nf_conn_acct *acct;
282 struct nf_conn_counter *counter;
284 acct = nf_conn_acct_find(ct);
288 counter = acct->counter;
289 seq_printf(s, "packets=%llu bytes=%llu ",
290 (unsigned long long)atomic64_read(&counter[dir].packets),
291 (unsigned long long)atomic64_read(&counter[dir].bytes));
296 /* return 0 on success, 1 in case of error */
297 static int ct_seq_show(struct seq_file *s, void *v)
299 struct nf_conntrack_tuple_hash *hash = v;
300 struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(hash);
301 const struct nf_conntrack_l4proto *l4proto;
302 struct net *net = seq_file_net(s);
306 if (unlikely(!refcount_inc_not_zero(&ct->ct_general.use)))
309 /* load ->status after refcount increase */
310 smp_acquire__after_ctrl_dep();
312 if (nf_ct_should_gc(ct)) {
317 /* we only want to print DIR_ORIGINAL */
318 if (NF_CT_DIRECTION(hash))
321 if (!net_eq(nf_ct_net(ct), net))
324 l4proto = nf_ct_l4proto_find(nf_ct_protonum(ct));
327 seq_printf(s, "%-8s %u %-8s %u ",
328 l3proto_name(nf_ct_l3num(ct)), nf_ct_l3num(ct),
329 l4proto_name(l4proto->l4proto), nf_ct_protonum(ct));
331 if (!test_bit(IPS_OFFLOAD_BIT, &ct->status))
332 seq_printf(s, "%ld ", nf_ct_expires(ct) / HZ);
334 if (l4proto->print_conntrack)
335 l4proto->print_conntrack(s, ct);
337 print_tuple(s, &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
340 ct_show_zone(s, ct, NF_CT_ZONE_DIR_ORIG);
342 if (seq_has_overflowed(s))
345 if (seq_print_acct(s, ct, IP_CT_DIR_ORIGINAL))
348 if (!(test_bit(IPS_SEEN_REPLY_BIT, &ct->status)))
349 seq_puts(s, "[UNREPLIED] ");
351 print_tuple(s, &ct->tuplehash[IP_CT_DIR_REPLY].tuple, l4proto);
353 ct_show_zone(s, ct, NF_CT_ZONE_DIR_REPL);
355 if (seq_print_acct(s, ct, IP_CT_DIR_REPLY))
358 if (test_bit(IPS_HW_OFFLOAD_BIT, &ct->status))
359 seq_puts(s, "[HW_OFFLOAD] ");
360 else if (test_bit(IPS_OFFLOAD_BIT, &ct->status))
361 seq_puts(s, "[OFFLOAD] ");
362 else if (test_bit(IPS_ASSURED_BIT, &ct->status))
363 seq_puts(s, "[ASSURED] ");
365 if (seq_has_overflowed(s))
368 #if defined(CONFIG_NF_CONNTRACK_MARK)
369 seq_printf(s, "mark=%u ", READ_ONCE(ct->mark));
372 ct_show_secctx(s, ct);
373 ct_show_zone(s, ct, NF_CT_DEFAULT_ZONE_DIR);
374 ct_show_delta_time(s, ct);
376 seq_printf(s, "use=%u\n", refcount_read(&ct->ct_general.use));
378 if (seq_has_overflowed(s))
387 static const struct seq_operations ct_seq_ops = {
388 .start = ct_seq_start,
394 static void *ct_cpu_seq_start(struct seq_file *seq, loff_t *pos)
396 struct net *net = seq_file_net(seq);
400 return SEQ_START_TOKEN;
402 for (cpu = *pos-1; cpu < nr_cpu_ids; ++cpu) {
403 if (!cpu_possible(cpu))
406 return per_cpu_ptr(net->ct.stat, cpu);
412 static void *ct_cpu_seq_next(struct seq_file *seq, void *v, loff_t *pos)
414 struct net *net = seq_file_net(seq);
417 for (cpu = *pos; cpu < nr_cpu_ids; ++cpu) {
418 if (!cpu_possible(cpu))
421 return per_cpu_ptr(net->ct.stat, cpu);
427 static void ct_cpu_seq_stop(struct seq_file *seq, void *v)
431 static int ct_cpu_seq_show(struct seq_file *seq, void *v)
433 struct net *net = seq_file_net(seq);
434 const struct ip_conntrack_stat *st = v;
435 unsigned int nr_conntracks;
437 if (v == SEQ_START_TOKEN) {
438 seq_puts(seq, "entries clashres found new invalid ignore delete chainlength insert insert_failed drop early_drop icmp_error expect_new expect_create expect_delete search_restart\n");
442 nr_conntracks = nf_conntrack_count(net);
444 seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x %08x "
445 "%08x %08x %08x %08x %08x %08x %08x %08x %08x\n",
468 static const struct seq_operations ct_cpu_seq_ops = {
469 .start = ct_cpu_seq_start,
470 .next = ct_cpu_seq_next,
471 .stop = ct_cpu_seq_stop,
472 .show = ct_cpu_seq_show,
475 static int nf_conntrack_standalone_init_proc(struct net *net)
477 struct proc_dir_entry *pde;
481 pde = proc_create_net("nf_conntrack", 0440, net->proc_net, &ct_seq_ops,
482 sizeof(struct ct_iter_state));
484 goto out_nf_conntrack;
486 root_uid = make_kuid(net->user_ns, 0);
487 root_gid = make_kgid(net->user_ns, 0);
488 if (uid_valid(root_uid) && gid_valid(root_gid))
489 proc_set_user(pde, root_uid, root_gid);
491 pde = proc_create_net("nf_conntrack", 0444, net->proc_net_stat,
492 &ct_cpu_seq_ops, sizeof(struct seq_net_private));
494 goto out_stat_nf_conntrack;
497 out_stat_nf_conntrack:
498 remove_proc_entry("nf_conntrack", net->proc_net);
503 static void nf_conntrack_standalone_fini_proc(struct net *net)
505 remove_proc_entry("nf_conntrack", net->proc_net_stat);
506 remove_proc_entry("nf_conntrack", net->proc_net);
509 static int nf_conntrack_standalone_init_proc(struct net *net)
514 static void nf_conntrack_standalone_fini_proc(struct net *net)
517 #endif /* CONFIG_NF_CONNTRACK_PROCFS */
519 u32 nf_conntrack_count(const struct net *net)
521 const struct nf_conntrack_net *cnet = nf_ct_pernet(net);
523 return atomic_read(&cnet->count);
525 EXPORT_SYMBOL_GPL(nf_conntrack_count);
530 /* size the user *wants to set */
531 static unsigned int nf_conntrack_htable_size_user __read_mostly;
534 nf_conntrack_hash_sysctl(struct ctl_table *table, int write,
535 void *buffer, size_t *lenp, loff_t *ppos)
539 /* module_param hashsize could have changed value */
540 nf_conntrack_htable_size_user = nf_conntrack_htable_size;
542 ret = proc_dointvec(table, write, buffer, lenp, ppos);
543 if (ret < 0 || !write)
546 /* update ret, we might not be able to satisfy request */
547 ret = nf_conntrack_hash_resize(nf_conntrack_htable_size_user);
549 /* update it to the actual value used by conntrack */
550 nf_conntrack_htable_size_user = nf_conntrack_htable_size;
554 static struct ctl_table_header *nf_ct_netfilter_header;
556 enum nf_ct_sysctl_index {
559 NF_SYSCTL_CT_BUCKETS,
560 NF_SYSCTL_CT_CHECKSUM,
561 NF_SYSCTL_CT_LOG_INVALID,
562 NF_SYSCTL_CT_EXPECT_MAX,
564 #ifdef CONFIG_NF_CONNTRACK_EVENTS
567 #ifdef CONFIG_NF_CONNTRACK_TIMESTAMP
568 NF_SYSCTL_CT_TIMESTAMP,
570 NF_SYSCTL_CT_PROTO_TIMEOUT_GENERIC,
571 NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_SYN_SENT,
572 NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_SYN_RECV,
573 NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_ESTABLISHED,
574 NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_FIN_WAIT,
575 NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_CLOSE_WAIT,
576 NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_LAST_ACK,
577 NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_TIME_WAIT,
578 NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_CLOSE,
579 NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_RETRANS,
580 NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_UNACK,
581 #if IS_ENABLED(CONFIG_NF_FLOW_TABLE)
582 NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_OFFLOAD,
584 NF_SYSCTL_CT_PROTO_TCP_LOOSE,
585 NF_SYSCTL_CT_PROTO_TCP_LIBERAL,
586 NF_SYSCTL_CT_PROTO_TCP_IGNORE_INVALID_RST,
587 NF_SYSCTL_CT_PROTO_TCP_MAX_RETRANS,
588 NF_SYSCTL_CT_PROTO_TIMEOUT_UDP,
589 NF_SYSCTL_CT_PROTO_TIMEOUT_UDP_STREAM,
590 #if IS_ENABLED(CONFIG_NF_FLOW_TABLE)
591 NF_SYSCTL_CT_PROTO_TIMEOUT_UDP_OFFLOAD,
593 NF_SYSCTL_CT_PROTO_TIMEOUT_ICMP,
594 NF_SYSCTL_CT_PROTO_TIMEOUT_ICMPV6,
595 #ifdef CONFIG_NF_CT_PROTO_SCTP
596 NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_CLOSED,
597 NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_COOKIE_WAIT,
598 NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_COOKIE_ECHOED,
599 NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_ESTABLISHED,
600 NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_SHUTDOWN_SENT,
601 NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_SHUTDOWN_RECD,
602 NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_SHUTDOWN_ACK_SENT,
603 NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_HEARTBEAT_SENT,
604 NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_HEARTBEAT_ACKED,
605 NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_DATA_SENT,
607 #ifdef CONFIG_NF_CT_PROTO_DCCP
608 NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_REQUEST,
609 NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_RESPOND,
610 NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_PARTOPEN,
611 NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_OPEN,
612 NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_CLOSEREQ,
613 NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_CLOSING,
614 NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_TIMEWAIT,
615 NF_SYSCTL_CT_PROTO_DCCP_LOOSE,
617 #ifdef CONFIG_NF_CT_PROTO_GRE
618 NF_SYSCTL_CT_PROTO_TIMEOUT_GRE,
619 NF_SYSCTL_CT_PROTO_TIMEOUT_GRE_STREAM,
621 #ifdef CONFIG_LWTUNNEL
622 NF_SYSCTL_CT_LWTUNNEL,
625 __NF_SYSCTL_CT_LAST_SYSCTL,
628 #define NF_SYSCTL_CT_LAST_SYSCTL (__NF_SYSCTL_CT_LAST_SYSCTL + 1)
630 static struct ctl_table nf_ct_sysctl_table[] = {
631 [NF_SYSCTL_CT_MAX] = {
632 .procname = "nf_conntrack_max",
633 .data = &nf_conntrack_max,
634 .maxlen = sizeof(int),
636 .proc_handler = proc_dointvec,
638 [NF_SYSCTL_CT_COUNT] = {
639 .procname = "nf_conntrack_count",
640 .maxlen = sizeof(int),
642 .proc_handler = proc_dointvec,
644 [NF_SYSCTL_CT_BUCKETS] = {
645 .procname = "nf_conntrack_buckets",
646 .data = &nf_conntrack_htable_size_user,
647 .maxlen = sizeof(unsigned int),
649 .proc_handler = nf_conntrack_hash_sysctl,
651 [NF_SYSCTL_CT_CHECKSUM] = {
652 .procname = "nf_conntrack_checksum",
653 .data = &init_net.ct.sysctl_checksum,
654 .maxlen = sizeof(u8),
656 .proc_handler = proc_dou8vec_minmax,
657 .extra1 = SYSCTL_ZERO,
658 .extra2 = SYSCTL_ONE,
660 [NF_SYSCTL_CT_LOG_INVALID] = {
661 .procname = "nf_conntrack_log_invalid",
662 .data = &init_net.ct.sysctl_log_invalid,
663 .maxlen = sizeof(u8),
665 .proc_handler = proc_dou8vec_minmax,
667 [NF_SYSCTL_CT_EXPECT_MAX] = {
668 .procname = "nf_conntrack_expect_max",
669 .data = &nf_ct_expect_max,
670 .maxlen = sizeof(int),
672 .proc_handler = proc_dointvec,
674 [NF_SYSCTL_CT_ACCT] = {
675 .procname = "nf_conntrack_acct",
676 .data = &init_net.ct.sysctl_acct,
677 .maxlen = sizeof(u8),
679 .proc_handler = proc_dou8vec_minmax,
680 .extra1 = SYSCTL_ZERO,
681 .extra2 = SYSCTL_ONE,
683 #ifdef CONFIG_NF_CONNTRACK_EVENTS
684 [NF_SYSCTL_CT_EVENTS] = {
685 .procname = "nf_conntrack_events",
686 .data = &init_net.ct.sysctl_events,
687 .maxlen = sizeof(u8),
689 .proc_handler = proc_dou8vec_minmax,
690 .extra1 = SYSCTL_ZERO,
691 .extra2 = SYSCTL_TWO,
694 #ifdef CONFIG_NF_CONNTRACK_TIMESTAMP
695 [NF_SYSCTL_CT_TIMESTAMP] = {
696 .procname = "nf_conntrack_timestamp",
697 .data = &init_net.ct.sysctl_tstamp,
698 .maxlen = sizeof(u8),
700 .proc_handler = proc_dou8vec_minmax,
701 .extra1 = SYSCTL_ZERO,
702 .extra2 = SYSCTL_ONE,
705 [NF_SYSCTL_CT_PROTO_TIMEOUT_GENERIC] = {
706 .procname = "nf_conntrack_generic_timeout",
707 .maxlen = sizeof(unsigned int),
709 .proc_handler = proc_dointvec_jiffies,
711 [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_SYN_SENT] = {
712 .procname = "nf_conntrack_tcp_timeout_syn_sent",
713 .maxlen = sizeof(unsigned int),
715 .proc_handler = proc_dointvec_jiffies,
717 [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_SYN_RECV] = {
718 .procname = "nf_conntrack_tcp_timeout_syn_recv",
719 .maxlen = sizeof(unsigned int),
721 .proc_handler = proc_dointvec_jiffies,
723 [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_ESTABLISHED] = {
724 .procname = "nf_conntrack_tcp_timeout_established",
725 .maxlen = sizeof(unsigned int),
727 .proc_handler = proc_dointvec_jiffies,
729 [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_FIN_WAIT] = {
730 .procname = "nf_conntrack_tcp_timeout_fin_wait",
731 .maxlen = sizeof(unsigned int),
733 .proc_handler = proc_dointvec_jiffies,
735 [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_CLOSE_WAIT] = {
736 .procname = "nf_conntrack_tcp_timeout_close_wait",
737 .maxlen = sizeof(unsigned int),
739 .proc_handler = proc_dointvec_jiffies,
741 [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_LAST_ACK] = {
742 .procname = "nf_conntrack_tcp_timeout_last_ack",
743 .maxlen = sizeof(unsigned int),
745 .proc_handler = proc_dointvec_jiffies,
747 [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_TIME_WAIT] = {
748 .procname = "nf_conntrack_tcp_timeout_time_wait",
749 .maxlen = sizeof(unsigned int),
751 .proc_handler = proc_dointvec_jiffies,
753 [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_CLOSE] = {
754 .procname = "nf_conntrack_tcp_timeout_close",
755 .maxlen = sizeof(unsigned int),
757 .proc_handler = proc_dointvec_jiffies,
759 [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_RETRANS] = {
760 .procname = "nf_conntrack_tcp_timeout_max_retrans",
761 .maxlen = sizeof(unsigned int),
763 .proc_handler = proc_dointvec_jiffies,
765 [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_UNACK] = {
766 .procname = "nf_conntrack_tcp_timeout_unacknowledged",
767 .maxlen = sizeof(unsigned int),
769 .proc_handler = proc_dointvec_jiffies,
771 #if IS_ENABLED(CONFIG_NF_FLOW_TABLE)
772 [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_OFFLOAD] = {
773 .procname = "nf_flowtable_tcp_timeout",
774 .maxlen = sizeof(unsigned int),
776 .proc_handler = proc_dointvec_jiffies,
779 [NF_SYSCTL_CT_PROTO_TCP_LOOSE] = {
780 .procname = "nf_conntrack_tcp_loose",
781 .maxlen = sizeof(u8),
783 .proc_handler = proc_dou8vec_minmax,
784 .extra1 = SYSCTL_ZERO,
785 .extra2 = SYSCTL_ONE,
787 [NF_SYSCTL_CT_PROTO_TCP_LIBERAL] = {
788 .procname = "nf_conntrack_tcp_be_liberal",
789 .maxlen = sizeof(u8),
791 .proc_handler = proc_dou8vec_minmax,
792 .extra1 = SYSCTL_ZERO,
793 .extra2 = SYSCTL_ONE,
795 [NF_SYSCTL_CT_PROTO_TCP_IGNORE_INVALID_RST] = {
796 .procname = "nf_conntrack_tcp_ignore_invalid_rst",
797 .maxlen = sizeof(u8),
799 .proc_handler = proc_dou8vec_minmax,
800 .extra1 = SYSCTL_ZERO,
801 .extra2 = SYSCTL_ONE,
803 [NF_SYSCTL_CT_PROTO_TCP_MAX_RETRANS] = {
804 .procname = "nf_conntrack_tcp_max_retrans",
805 .maxlen = sizeof(u8),
807 .proc_handler = proc_dou8vec_minmax,
809 [NF_SYSCTL_CT_PROTO_TIMEOUT_UDP] = {
810 .procname = "nf_conntrack_udp_timeout",
811 .maxlen = sizeof(unsigned int),
813 .proc_handler = proc_dointvec_jiffies,
815 [NF_SYSCTL_CT_PROTO_TIMEOUT_UDP_STREAM] = {
816 .procname = "nf_conntrack_udp_timeout_stream",
817 .maxlen = sizeof(unsigned int),
819 .proc_handler = proc_dointvec_jiffies,
821 #if IS_ENABLED(CONFIG_NF_FLOW_TABLE)
822 [NF_SYSCTL_CT_PROTO_TIMEOUT_UDP_OFFLOAD] = {
823 .procname = "nf_flowtable_udp_timeout",
824 .maxlen = sizeof(unsigned int),
826 .proc_handler = proc_dointvec_jiffies,
829 [NF_SYSCTL_CT_PROTO_TIMEOUT_ICMP] = {
830 .procname = "nf_conntrack_icmp_timeout",
831 .maxlen = sizeof(unsigned int),
833 .proc_handler = proc_dointvec_jiffies,
835 [NF_SYSCTL_CT_PROTO_TIMEOUT_ICMPV6] = {
836 .procname = "nf_conntrack_icmpv6_timeout",
837 .maxlen = sizeof(unsigned int),
839 .proc_handler = proc_dointvec_jiffies,
841 #ifdef CONFIG_NF_CT_PROTO_SCTP
842 [NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_CLOSED] = {
843 .procname = "nf_conntrack_sctp_timeout_closed",
844 .maxlen = sizeof(unsigned int),
846 .proc_handler = proc_dointvec_jiffies,
848 [NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_COOKIE_WAIT] = {
849 .procname = "nf_conntrack_sctp_timeout_cookie_wait",
850 .maxlen = sizeof(unsigned int),
852 .proc_handler = proc_dointvec_jiffies,
854 [NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_COOKIE_ECHOED] = {
855 .procname = "nf_conntrack_sctp_timeout_cookie_echoed",
856 .maxlen = sizeof(unsigned int),
858 .proc_handler = proc_dointvec_jiffies,
860 [NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_ESTABLISHED] = {
861 .procname = "nf_conntrack_sctp_timeout_established",
862 .maxlen = sizeof(unsigned int),
864 .proc_handler = proc_dointvec_jiffies,
866 [NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_SHUTDOWN_SENT] = {
867 .procname = "nf_conntrack_sctp_timeout_shutdown_sent",
868 .maxlen = sizeof(unsigned int),
870 .proc_handler = proc_dointvec_jiffies,
872 [NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_SHUTDOWN_RECD] = {
873 .procname = "nf_conntrack_sctp_timeout_shutdown_recd",
874 .maxlen = sizeof(unsigned int),
876 .proc_handler = proc_dointvec_jiffies,
878 [NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_SHUTDOWN_ACK_SENT] = {
879 .procname = "nf_conntrack_sctp_timeout_shutdown_ack_sent",
880 .maxlen = sizeof(unsigned int),
882 .proc_handler = proc_dointvec_jiffies,
884 [NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_HEARTBEAT_SENT] = {
885 .procname = "nf_conntrack_sctp_timeout_heartbeat_sent",
886 .maxlen = sizeof(unsigned int),
888 .proc_handler = proc_dointvec_jiffies,
890 [NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_HEARTBEAT_ACKED] = {
891 .procname = "nf_conntrack_sctp_timeout_heartbeat_acked",
892 .maxlen = sizeof(unsigned int),
894 .proc_handler = proc_dointvec_jiffies,
896 [NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_DATA_SENT] = {
897 .procname = "nf_conntrack_sctp_timeout_data_sent",
898 .maxlen = sizeof(unsigned int),
900 .proc_handler = proc_dointvec_jiffies,
903 #ifdef CONFIG_NF_CT_PROTO_DCCP
904 [NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_REQUEST] = {
905 .procname = "nf_conntrack_dccp_timeout_request",
906 .maxlen = sizeof(unsigned int),
908 .proc_handler = proc_dointvec_jiffies,
910 [NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_RESPOND] = {
911 .procname = "nf_conntrack_dccp_timeout_respond",
912 .maxlen = sizeof(unsigned int),
914 .proc_handler = proc_dointvec_jiffies,
916 [NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_PARTOPEN] = {
917 .procname = "nf_conntrack_dccp_timeout_partopen",
918 .maxlen = sizeof(unsigned int),
920 .proc_handler = proc_dointvec_jiffies,
922 [NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_OPEN] = {
923 .procname = "nf_conntrack_dccp_timeout_open",
924 .maxlen = sizeof(unsigned int),
926 .proc_handler = proc_dointvec_jiffies,
928 [NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_CLOSEREQ] = {
929 .procname = "nf_conntrack_dccp_timeout_closereq",
930 .maxlen = sizeof(unsigned int),
932 .proc_handler = proc_dointvec_jiffies,
934 [NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_CLOSING] = {
935 .procname = "nf_conntrack_dccp_timeout_closing",
936 .maxlen = sizeof(unsigned int),
938 .proc_handler = proc_dointvec_jiffies,
940 [NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_TIMEWAIT] = {
941 .procname = "nf_conntrack_dccp_timeout_timewait",
942 .maxlen = sizeof(unsigned int),
944 .proc_handler = proc_dointvec_jiffies,
946 [NF_SYSCTL_CT_PROTO_DCCP_LOOSE] = {
947 .procname = "nf_conntrack_dccp_loose",
948 .maxlen = sizeof(u8),
950 .proc_handler = proc_dou8vec_minmax,
951 .extra1 = SYSCTL_ZERO,
952 .extra2 = SYSCTL_ONE,
955 #ifdef CONFIG_NF_CT_PROTO_GRE
956 [NF_SYSCTL_CT_PROTO_TIMEOUT_GRE] = {
957 .procname = "nf_conntrack_gre_timeout",
958 .maxlen = sizeof(unsigned int),
960 .proc_handler = proc_dointvec_jiffies,
962 [NF_SYSCTL_CT_PROTO_TIMEOUT_GRE_STREAM] = {
963 .procname = "nf_conntrack_gre_timeout_stream",
964 .maxlen = sizeof(unsigned int),
966 .proc_handler = proc_dointvec_jiffies,
969 #ifdef CONFIG_LWTUNNEL
970 [NF_SYSCTL_CT_LWTUNNEL] = {
971 .procname = "nf_hooks_lwtunnel",
973 .maxlen = sizeof(int),
975 .proc_handler = nf_hooks_lwtunnel_sysctl_handler,
981 static struct ctl_table nf_ct_netfilter_table[] = {
983 .procname = "nf_conntrack_max",
984 .data = &nf_conntrack_max,
985 .maxlen = sizeof(int),
987 .proc_handler = proc_dointvec,
992 static void nf_conntrack_standalone_init_tcp_sysctl(struct net *net,
993 struct ctl_table *table)
995 struct nf_tcp_net *tn = nf_tcp_pernet(net);
997 #define XASSIGN(XNAME, tn) \
998 table[NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_ ## XNAME].data = \
999 &(tn)->timeouts[TCP_CONNTRACK_ ## XNAME]
1001 XASSIGN(SYN_SENT, tn);
1002 XASSIGN(SYN_RECV, tn);
1003 XASSIGN(ESTABLISHED, tn);
1004 XASSIGN(FIN_WAIT, tn);
1005 XASSIGN(CLOSE_WAIT, tn);
1006 XASSIGN(LAST_ACK, tn);
1007 XASSIGN(TIME_WAIT, tn);
1009 XASSIGN(RETRANS, tn);
1012 #define XASSIGN(XNAME, rval) \
1013 table[NF_SYSCTL_CT_PROTO_TCP_ ## XNAME].data = (rval)
1015 XASSIGN(LOOSE, &tn->tcp_loose);
1016 XASSIGN(LIBERAL, &tn->tcp_be_liberal);
1017 XASSIGN(MAX_RETRANS, &tn->tcp_max_retrans);
1018 XASSIGN(IGNORE_INVALID_RST, &tn->tcp_ignore_invalid_rst);
1021 #if IS_ENABLED(CONFIG_NF_FLOW_TABLE)
1022 table[NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_OFFLOAD].data = &tn->offload_timeout;
1027 static void nf_conntrack_standalone_init_sctp_sysctl(struct net *net,
1028 struct ctl_table *table)
1030 #ifdef CONFIG_NF_CT_PROTO_SCTP
1031 struct nf_sctp_net *sn = nf_sctp_pernet(net);
1033 #define XASSIGN(XNAME, sn) \
1034 table[NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_ ## XNAME].data = \
1035 &(sn)->timeouts[SCTP_CONNTRACK_ ## XNAME]
1037 XASSIGN(CLOSED, sn);
1038 XASSIGN(COOKIE_WAIT, sn);
1039 XASSIGN(COOKIE_ECHOED, sn);
1040 XASSIGN(ESTABLISHED, sn);
1041 XASSIGN(SHUTDOWN_SENT, sn);
1042 XASSIGN(SHUTDOWN_RECD, sn);
1043 XASSIGN(SHUTDOWN_ACK_SENT, sn);
1044 XASSIGN(HEARTBEAT_SENT, sn);
1045 XASSIGN(HEARTBEAT_ACKED, sn);
1046 XASSIGN(DATA_SENT, sn);
1051 static void nf_conntrack_standalone_init_dccp_sysctl(struct net *net,
1052 struct ctl_table *table)
1054 #ifdef CONFIG_NF_CT_PROTO_DCCP
1055 struct nf_dccp_net *dn = nf_dccp_pernet(net);
1057 #define XASSIGN(XNAME, dn) \
1058 table[NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_ ## XNAME].data = \
1059 &(dn)->dccp_timeout[CT_DCCP_ ## XNAME]
1061 XASSIGN(REQUEST, dn);
1062 XASSIGN(RESPOND, dn);
1063 XASSIGN(PARTOPEN, dn);
1065 XASSIGN(CLOSEREQ, dn);
1066 XASSIGN(CLOSING, dn);
1067 XASSIGN(TIMEWAIT, dn);
1070 table[NF_SYSCTL_CT_PROTO_DCCP_LOOSE].data = &dn->dccp_loose;
1074 static void nf_conntrack_standalone_init_gre_sysctl(struct net *net,
1075 struct ctl_table *table)
1077 #ifdef CONFIG_NF_CT_PROTO_GRE
1078 struct nf_gre_net *gn = nf_gre_pernet(net);
1080 table[NF_SYSCTL_CT_PROTO_TIMEOUT_GRE].data = &gn->timeouts[GRE_CT_UNREPLIED];
1081 table[NF_SYSCTL_CT_PROTO_TIMEOUT_GRE_STREAM].data = &gn->timeouts[GRE_CT_REPLIED];
1085 static int nf_conntrack_standalone_init_sysctl(struct net *net)
1087 struct nf_conntrack_net *cnet = nf_ct_pernet(net);
1088 struct nf_udp_net *un = nf_udp_pernet(net);
1089 struct ctl_table *table;
1091 BUILD_BUG_ON(ARRAY_SIZE(nf_ct_sysctl_table) != NF_SYSCTL_CT_LAST_SYSCTL);
1093 table = kmemdup(nf_ct_sysctl_table, sizeof(nf_ct_sysctl_table),
1098 table[NF_SYSCTL_CT_COUNT].data = &cnet->count;
1099 table[NF_SYSCTL_CT_CHECKSUM].data = &net->ct.sysctl_checksum;
1100 table[NF_SYSCTL_CT_LOG_INVALID].data = &net->ct.sysctl_log_invalid;
1101 table[NF_SYSCTL_CT_ACCT].data = &net->ct.sysctl_acct;
1102 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1103 table[NF_SYSCTL_CT_EVENTS].data = &net->ct.sysctl_events;
1105 #ifdef CONFIG_NF_CONNTRACK_TIMESTAMP
1106 table[NF_SYSCTL_CT_TIMESTAMP].data = &net->ct.sysctl_tstamp;
1108 table[NF_SYSCTL_CT_PROTO_TIMEOUT_GENERIC].data = &nf_generic_pernet(net)->timeout;
1109 table[NF_SYSCTL_CT_PROTO_TIMEOUT_ICMP].data = &nf_icmp_pernet(net)->timeout;
1110 table[NF_SYSCTL_CT_PROTO_TIMEOUT_ICMPV6].data = &nf_icmpv6_pernet(net)->timeout;
1111 table[NF_SYSCTL_CT_PROTO_TIMEOUT_UDP].data = &un->timeouts[UDP_CT_UNREPLIED];
1112 table[NF_SYSCTL_CT_PROTO_TIMEOUT_UDP_STREAM].data = &un->timeouts[UDP_CT_REPLIED];
1113 #if IS_ENABLED(CONFIG_NF_FLOW_TABLE)
1114 table[NF_SYSCTL_CT_PROTO_TIMEOUT_UDP_OFFLOAD].data = &un->offload_timeout;
1117 nf_conntrack_standalone_init_tcp_sysctl(net, table);
1118 nf_conntrack_standalone_init_sctp_sysctl(net, table);
1119 nf_conntrack_standalone_init_dccp_sysctl(net, table);
1120 nf_conntrack_standalone_init_gre_sysctl(net, table);
1122 /* Don't allow non-init_net ns to alter global sysctls */
1123 if (!net_eq(&init_net, net)) {
1124 table[NF_SYSCTL_CT_MAX].mode = 0444;
1125 table[NF_SYSCTL_CT_EXPECT_MAX].mode = 0444;
1126 table[NF_SYSCTL_CT_BUCKETS].mode = 0444;
1129 cnet->sysctl_header = register_net_sysctl(net, "net/netfilter", table);
1130 if (!cnet->sysctl_header)
1131 goto out_unregister_netfilter;
1135 out_unregister_netfilter:
1140 static void nf_conntrack_standalone_fini_sysctl(struct net *net)
1142 struct nf_conntrack_net *cnet = nf_ct_pernet(net);
1143 struct ctl_table *table;
1145 table = cnet->sysctl_header->ctl_table_arg;
1146 unregister_net_sysctl_table(cnet->sysctl_header);
1150 static int nf_conntrack_standalone_init_sysctl(struct net *net)
1155 static void nf_conntrack_standalone_fini_sysctl(struct net *net)
1158 #endif /* CONFIG_SYSCTL */
1160 static void nf_conntrack_fini_net(struct net *net)
1163 nf_ct_netns_put(net, NFPROTO_INET);
1165 nf_conntrack_standalone_fini_proc(net);
1166 nf_conntrack_standalone_fini_sysctl(net);
1169 static int nf_conntrack_pernet_init(struct net *net)
1173 net->ct.sysctl_checksum = 1;
1175 ret = nf_conntrack_standalone_init_sysctl(net);
1179 ret = nf_conntrack_standalone_init_proc(net);
1183 ret = nf_conntrack_init_net(net);
1188 ret = nf_ct_netns_get(net, NFPROTO_INET);
1196 nf_conntrack_cleanup_net(net);
1198 nf_conntrack_standalone_fini_proc(net);
1200 nf_conntrack_standalone_fini_sysctl(net);
1204 static void nf_conntrack_pernet_exit(struct list_head *net_exit_list)
1208 list_for_each_entry(net, net_exit_list, exit_list)
1209 nf_conntrack_fini_net(net);
1211 nf_conntrack_cleanup_net_list(net_exit_list);
1214 static struct pernet_operations nf_conntrack_net_ops = {
1215 .init = nf_conntrack_pernet_init,
1216 .exit_batch = nf_conntrack_pernet_exit,
1217 .id = &nf_conntrack_net_id,
1218 .size = sizeof(struct nf_conntrack_net),
1221 static int __init nf_conntrack_standalone_init(void)
1223 int ret = nf_conntrack_init_start();
1227 BUILD_BUG_ON(NFCT_INFOMASK <= IP_CT_NUMBER);
1229 #ifdef CONFIG_SYSCTL
1230 nf_ct_netfilter_header =
1231 register_net_sysctl(&init_net, "net", nf_ct_netfilter_table);
1232 if (!nf_ct_netfilter_header) {
1233 pr_err("nf_conntrack: can't register to sysctl.\n");
1238 nf_conntrack_htable_size_user = nf_conntrack_htable_size;
1241 ret = register_pernet_subsys(&nf_conntrack_net_ops);
1245 nf_conntrack_init_end();
1249 #ifdef CONFIG_SYSCTL
1250 unregister_net_sysctl_table(nf_ct_netfilter_header);
1253 nf_conntrack_cleanup_end();
1258 static void __exit nf_conntrack_standalone_fini(void)
1260 nf_conntrack_cleanup_start();
1261 unregister_pernet_subsys(&nf_conntrack_net_ops);
1262 #ifdef CONFIG_SYSCTL
1263 unregister_net_sysctl_table(nf_ct_netfilter_header);
1265 nf_conntrack_cleanup_end();
1268 module_init(nf_conntrack_standalone_init);
1269 module_exit(nf_conntrack_standalone_fini);