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";
269 case IPPROTO_ICMPV6: return "icmpv6";
276 seq_print_acct(struct seq_file *s, const struct nf_conn *ct, int dir)
278 struct nf_conn_acct *acct;
279 struct nf_conn_counter *counter;
281 acct = nf_conn_acct_find(ct);
285 counter = acct->counter;
286 seq_printf(s, "packets=%llu bytes=%llu ",
287 (unsigned long long)atomic64_read(&counter[dir].packets),
288 (unsigned long long)atomic64_read(&counter[dir].bytes));
293 /* return 0 on success, 1 in case of error */
294 static int ct_seq_show(struct seq_file *s, void *v)
296 struct nf_conntrack_tuple_hash *hash = v;
297 struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(hash);
298 const struct nf_conntrack_l4proto *l4proto;
299 struct net *net = seq_file_net(s);
303 if (unlikely(!atomic_inc_not_zero(&ct->ct_general.use)))
306 if (nf_ct_should_gc(ct)) {
311 /* we only want to print DIR_ORIGINAL */
312 if (NF_CT_DIRECTION(hash))
315 if (!net_eq(nf_ct_net(ct), net))
318 l4proto = nf_ct_l4proto_find(nf_ct_protonum(ct));
321 seq_printf(s, "%-8s %u %-8s %u ",
322 l3proto_name(nf_ct_l3num(ct)), nf_ct_l3num(ct),
323 l4proto_name(l4proto->l4proto), nf_ct_protonum(ct));
325 if (!test_bit(IPS_OFFLOAD_BIT, &ct->status))
326 seq_printf(s, "%ld ", nf_ct_expires(ct) / HZ);
328 if (l4proto->print_conntrack)
329 l4proto->print_conntrack(s, ct);
331 print_tuple(s, &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
334 ct_show_zone(s, ct, NF_CT_ZONE_DIR_ORIG);
336 if (seq_has_overflowed(s))
339 if (seq_print_acct(s, ct, IP_CT_DIR_ORIGINAL))
342 if (!(test_bit(IPS_SEEN_REPLY_BIT, &ct->status)))
343 seq_puts(s, "[UNREPLIED] ");
345 print_tuple(s, &ct->tuplehash[IP_CT_DIR_REPLY].tuple, l4proto);
347 ct_show_zone(s, ct, NF_CT_ZONE_DIR_REPL);
349 if (seq_print_acct(s, ct, IP_CT_DIR_REPLY))
352 if (test_bit(IPS_HW_OFFLOAD_BIT, &ct->status))
353 seq_puts(s, "[HW_OFFLOAD] ");
354 else if (test_bit(IPS_OFFLOAD_BIT, &ct->status))
355 seq_puts(s, "[OFFLOAD] ");
356 else if (test_bit(IPS_ASSURED_BIT, &ct->status))
357 seq_puts(s, "[ASSURED] ");
359 if (seq_has_overflowed(s))
362 #if defined(CONFIG_NF_CONNTRACK_MARK)
363 seq_printf(s, "mark=%u ", ct->mark);
366 ct_show_secctx(s, ct);
367 ct_show_zone(s, ct, NF_CT_DEFAULT_ZONE_DIR);
368 ct_show_delta_time(s, ct);
370 seq_printf(s, "use=%u\n", atomic_read(&ct->ct_general.use));
372 if (seq_has_overflowed(s))
381 static const struct seq_operations ct_seq_ops = {
382 .start = ct_seq_start,
388 static void *ct_cpu_seq_start(struct seq_file *seq, loff_t *pos)
390 struct net *net = seq_file_net(seq);
394 return SEQ_START_TOKEN;
396 for (cpu = *pos-1; cpu < nr_cpu_ids; ++cpu) {
397 if (!cpu_possible(cpu))
400 return per_cpu_ptr(net->ct.stat, cpu);
406 static void *ct_cpu_seq_next(struct seq_file *seq, void *v, loff_t *pos)
408 struct net *net = seq_file_net(seq);
411 for (cpu = *pos; cpu < nr_cpu_ids; ++cpu) {
412 if (!cpu_possible(cpu))
415 return per_cpu_ptr(net->ct.stat, cpu);
421 static void ct_cpu_seq_stop(struct seq_file *seq, void *v)
425 static int ct_cpu_seq_show(struct seq_file *seq, void *v)
427 struct net *net = seq_file_net(seq);
428 const struct ip_conntrack_stat *st = v;
429 unsigned int nr_conntracks;
431 if (v == SEQ_START_TOKEN) {
432 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");
436 nr_conntracks = nf_conntrack_count(net);
438 seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x %08x "
439 "%08x %08x %08x %08x %08x %08x %08x %08x %08x\n",
462 static const struct seq_operations ct_cpu_seq_ops = {
463 .start = ct_cpu_seq_start,
464 .next = ct_cpu_seq_next,
465 .stop = ct_cpu_seq_stop,
466 .show = ct_cpu_seq_show,
469 static int nf_conntrack_standalone_init_proc(struct net *net)
471 struct proc_dir_entry *pde;
475 pde = proc_create_net("nf_conntrack", 0440, net->proc_net, &ct_seq_ops,
476 sizeof(struct ct_iter_state));
478 goto out_nf_conntrack;
480 root_uid = make_kuid(net->user_ns, 0);
481 root_gid = make_kgid(net->user_ns, 0);
482 if (uid_valid(root_uid) && gid_valid(root_gid))
483 proc_set_user(pde, root_uid, root_gid);
485 pde = proc_create_net("nf_conntrack", 0444, net->proc_net_stat,
486 &ct_cpu_seq_ops, sizeof(struct seq_net_private));
488 goto out_stat_nf_conntrack;
491 out_stat_nf_conntrack:
492 remove_proc_entry("nf_conntrack", net->proc_net);
497 static void nf_conntrack_standalone_fini_proc(struct net *net)
499 remove_proc_entry("nf_conntrack", net->proc_net_stat);
500 remove_proc_entry("nf_conntrack", net->proc_net);
503 static int nf_conntrack_standalone_init_proc(struct net *net)
508 static void nf_conntrack_standalone_fini_proc(struct net *net)
511 #endif /* CONFIG_NF_CONNTRACK_PROCFS */
513 u32 nf_conntrack_count(const struct net *net)
515 const struct nf_conntrack_net *cnet = nf_ct_pernet(net);
517 return atomic_read(&cnet->count);
519 EXPORT_SYMBOL_GPL(nf_conntrack_count);
524 /* size the user *wants to set */
525 static unsigned int nf_conntrack_htable_size_user __read_mostly;
528 nf_conntrack_hash_sysctl(struct ctl_table *table, int write,
529 void *buffer, size_t *lenp, loff_t *ppos)
533 /* module_param hashsize could have changed value */
534 nf_conntrack_htable_size_user = nf_conntrack_htable_size;
536 ret = proc_dointvec(table, write, buffer, lenp, ppos);
537 if (ret < 0 || !write)
540 /* update ret, we might not be able to satisfy request */
541 ret = nf_conntrack_hash_resize(nf_conntrack_htable_size_user);
543 /* update it to the actual value used by conntrack */
544 nf_conntrack_htable_size_user = nf_conntrack_htable_size;
548 static struct ctl_table_header *nf_ct_netfilter_header;
550 enum nf_ct_sysctl_index {
553 NF_SYSCTL_CT_BUCKETS,
554 NF_SYSCTL_CT_CHECKSUM,
555 NF_SYSCTL_CT_LOG_INVALID,
556 NF_SYSCTL_CT_EXPECT_MAX,
559 #ifdef CONFIG_NF_CONNTRACK_EVENTS
562 #ifdef CONFIG_NF_CONNTRACK_TIMESTAMP
563 NF_SYSCTL_CT_TIMESTAMP,
565 NF_SYSCTL_CT_PROTO_TIMEOUT_GENERIC,
566 NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_SYN_SENT,
567 NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_SYN_RECV,
568 NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_ESTABLISHED,
569 NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_FIN_WAIT,
570 NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_CLOSE_WAIT,
571 NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_LAST_ACK,
572 NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_TIME_WAIT,
573 NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_CLOSE,
574 NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_RETRANS,
575 NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_UNACK,
576 #if IS_ENABLED(CONFIG_NF_FLOW_TABLE)
577 NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_OFFLOAD,
578 NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_OFFLOAD_PICKUP,
580 NF_SYSCTL_CT_PROTO_TCP_LOOSE,
581 NF_SYSCTL_CT_PROTO_TCP_LIBERAL,
582 NF_SYSCTL_CT_PROTO_TCP_IGNORE_INVALID_RST,
583 NF_SYSCTL_CT_PROTO_TCP_MAX_RETRANS,
584 NF_SYSCTL_CT_PROTO_TIMEOUT_UDP,
585 NF_SYSCTL_CT_PROTO_TIMEOUT_UDP_STREAM,
586 #if IS_ENABLED(CONFIG_NF_FLOW_TABLE)
587 NF_SYSCTL_CT_PROTO_TIMEOUT_UDP_OFFLOAD,
588 NF_SYSCTL_CT_PROTO_TIMEOUT_UDP_OFFLOAD_PICKUP,
590 NF_SYSCTL_CT_PROTO_TIMEOUT_ICMP,
591 NF_SYSCTL_CT_PROTO_TIMEOUT_ICMPV6,
592 #ifdef CONFIG_NF_CT_PROTO_SCTP
593 NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_CLOSED,
594 NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_COOKIE_WAIT,
595 NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_COOKIE_ECHOED,
596 NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_ESTABLISHED,
597 NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_SHUTDOWN_SENT,
598 NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_SHUTDOWN_RECD,
599 NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_SHUTDOWN_ACK_SENT,
600 NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_HEARTBEAT_SENT,
601 NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_HEARTBEAT_ACKED,
603 #ifdef CONFIG_NF_CT_PROTO_DCCP
604 NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_REQUEST,
605 NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_RESPOND,
606 NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_PARTOPEN,
607 NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_OPEN,
608 NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_CLOSEREQ,
609 NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_CLOSING,
610 NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_TIMEWAIT,
611 NF_SYSCTL_CT_PROTO_DCCP_LOOSE,
613 #ifdef CONFIG_NF_CT_PROTO_GRE
614 NF_SYSCTL_CT_PROTO_TIMEOUT_GRE,
615 NF_SYSCTL_CT_PROTO_TIMEOUT_GRE_STREAM,
618 __NF_SYSCTL_CT_LAST_SYSCTL,
621 #define NF_SYSCTL_CT_LAST_SYSCTL (__NF_SYSCTL_CT_LAST_SYSCTL + 1)
623 static struct ctl_table nf_ct_sysctl_table[] = {
624 [NF_SYSCTL_CT_MAX] = {
625 .procname = "nf_conntrack_max",
626 .data = &nf_conntrack_max,
627 .maxlen = sizeof(int),
629 .proc_handler = proc_dointvec,
631 [NF_SYSCTL_CT_COUNT] = {
632 .procname = "nf_conntrack_count",
633 .maxlen = sizeof(int),
635 .proc_handler = proc_dointvec,
637 [NF_SYSCTL_CT_BUCKETS] = {
638 .procname = "nf_conntrack_buckets",
639 .data = &nf_conntrack_htable_size_user,
640 .maxlen = sizeof(unsigned int),
642 .proc_handler = nf_conntrack_hash_sysctl,
644 [NF_SYSCTL_CT_CHECKSUM] = {
645 .procname = "nf_conntrack_checksum",
646 .data = &init_net.ct.sysctl_checksum,
647 .maxlen = sizeof(u8),
649 .proc_handler = proc_dou8vec_minmax,
650 .extra1 = SYSCTL_ZERO,
651 .extra2 = SYSCTL_ONE,
653 [NF_SYSCTL_CT_LOG_INVALID] = {
654 .procname = "nf_conntrack_log_invalid",
655 .data = &init_net.ct.sysctl_log_invalid,
656 .maxlen = sizeof(u8),
658 .proc_handler = proc_dou8vec_minmax,
660 [NF_SYSCTL_CT_EXPECT_MAX] = {
661 .procname = "nf_conntrack_expect_max",
662 .data = &nf_ct_expect_max,
663 .maxlen = sizeof(int),
665 .proc_handler = proc_dointvec,
667 [NF_SYSCTL_CT_ACCT] = {
668 .procname = "nf_conntrack_acct",
669 .data = &init_net.ct.sysctl_acct,
670 .maxlen = sizeof(u8),
672 .proc_handler = proc_dou8vec_minmax,
673 .extra1 = SYSCTL_ZERO,
674 .extra2 = SYSCTL_ONE,
676 [NF_SYSCTL_CT_HELPER] = {
677 .procname = "nf_conntrack_helper",
678 .maxlen = sizeof(u8),
680 .proc_handler = proc_dou8vec_minmax,
681 .extra1 = SYSCTL_ZERO,
682 .extra2 = SYSCTL_ONE,
684 #ifdef CONFIG_NF_CONNTRACK_EVENTS
685 [NF_SYSCTL_CT_EVENTS] = {
686 .procname = "nf_conntrack_events",
687 .data = &init_net.ct.sysctl_events,
688 .maxlen = sizeof(u8),
690 .proc_handler = proc_dou8vec_minmax,
691 .extra1 = SYSCTL_ZERO,
692 .extra2 = SYSCTL_ONE,
695 #ifdef CONFIG_NF_CONNTRACK_TIMESTAMP
696 [NF_SYSCTL_CT_TIMESTAMP] = {
697 .procname = "nf_conntrack_timestamp",
698 .data = &init_net.ct.sysctl_tstamp,
699 .maxlen = sizeof(u8),
701 .proc_handler = proc_dou8vec_minmax,
702 .extra1 = SYSCTL_ZERO,
703 .extra2 = SYSCTL_ONE,
706 [NF_SYSCTL_CT_PROTO_TIMEOUT_GENERIC] = {
707 .procname = "nf_conntrack_generic_timeout",
708 .maxlen = sizeof(unsigned int),
710 .proc_handler = proc_dointvec_jiffies,
712 [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_SYN_SENT] = {
713 .procname = "nf_conntrack_tcp_timeout_syn_sent",
714 .maxlen = sizeof(unsigned int),
716 .proc_handler = proc_dointvec_jiffies,
718 [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_SYN_RECV] = {
719 .procname = "nf_conntrack_tcp_timeout_syn_recv",
720 .maxlen = sizeof(unsigned int),
722 .proc_handler = proc_dointvec_jiffies,
724 [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_ESTABLISHED] = {
725 .procname = "nf_conntrack_tcp_timeout_established",
726 .maxlen = sizeof(unsigned int),
728 .proc_handler = proc_dointvec_jiffies,
730 [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_FIN_WAIT] = {
731 .procname = "nf_conntrack_tcp_timeout_fin_wait",
732 .maxlen = sizeof(unsigned int),
734 .proc_handler = proc_dointvec_jiffies,
736 [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_CLOSE_WAIT] = {
737 .procname = "nf_conntrack_tcp_timeout_close_wait",
738 .maxlen = sizeof(unsigned int),
740 .proc_handler = proc_dointvec_jiffies,
742 [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_LAST_ACK] = {
743 .procname = "nf_conntrack_tcp_timeout_last_ack",
744 .maxlen = sizeof(unsigned int),
746 .proc_handler = proc_dointvec_jiffies,
748 [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_TIME_WAIT] = {
749 .procname = "nf_conntrack_tcp_timeout_time_wait",
750 .maxlen = sizeof(unsigned int),
752 .proc_handler = proc_dointvec_jiffies,
754 [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_CLOSE] = {
755 .procname = "nf_conntrack_tcp_timeout_close",
756 .maxlen = sizeof(unsigned int),
758 .proc_handler = proc_dointvec_jiffies,
760 [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_RETRANS] = {
761 .procname = "nf_conntrack_tcp_timeout_max_retrans",
762 .maxlen = sizeof(unsigned int),
764 .proc_handler = proc_dointvec_jiffies,
766 [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_UNACK] = {
767 .procname = "nf_conntrack_tcp_timeout_unacknowledged",
768 .maxlen = sizeof(unsigned int),
770 .proc_handler = proc_dointvec_jiffies,
772 #if IS_ENABLED(CONFIG_NF_FLOW_TABLE)
773 [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_OFFLOAD] = {
774 .procname = "nf_flowtable_tcp_timeout",
775 .maxlen = sizeof(unsigned int),
777 .proc_handler = proc_dointvec_jiffies,
779 [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_OFFLOAD_PICKUP] = {
780 .procname = "nf_flowtable_tcp_pickup",
781 .maxlen = sizeof(unsigned int),
783 .proc_handler = proc_dointvec_jiffies,
786 [NF_SYSCTL_CT_PROTO_TCP_LOOSE] = {
787 .procname = "nf_conntrack_tcp_loose",
788 .maxlen = sizeof(u8),
790 .proc_handler = proc_dou8vec_minmax,
791 .extra1 = SYSCTL_ZERO,
792 .extra2 = SYSCTL_ONE,
794 [NF_SYSCTL_CT_PROTO_TCP_LIBERAL] = {
795 .procname = "nf_conntrack_tcp_be_liberal",
796 .maxlen = sizeof(u8),
798 .proc_handler = proc_dou8vec_minmax,
799 .extra1 = SYSCTL_ZERO,
800 .extra2 = SYSCTL_ONE,
802 [NF_SYSCTL_CT_PROTO_TCP_IGNORE_INVALID_RST] = {
803 .procname = "nf_conntrack_tcp_ignore_invalid_rst",
804 .maxlen = sizeof(u8),
806 .proc_handler = proc_dou8vec_minmax,
807 .extra1 = SYSCTL_ZERO,
808 .extra2 = SYSCTL_ONE,
810 [NF_SYSCTL_CT_PROTO_TCP_MAX_RETRANS] = {
811 .procname = "nf_conntrack_tcp_max_retrans",
812 .maxlen = sizeof(u8),
814 .proc_handler = proc_dou8vec_minmax,
816 [NF_SYSCTL_CT_PROTO_TIMEOUT_UDP] = {
817 .procname = "nf_conntrack_udp_timeout",
818 .maxlen = sizeof(unsigned int),
820 .proc_handler = proc_dointvec_jiffies,
822 [NF_SYSCTL_CT_PROTO_TIMEOUT_UDP_STREAM] = {
823 .procname = "nf_conntrack_udp_timeout_stream",
824 .maxlen = sizeof(unsigned int),
826 .proc_handler = proc_dointvec_jiffies,
828 #if IS_ENABLED(CONFIG_NFT_FLOW_OFFLOAD)
829 [NF_SYSCTL_CT_PROTO_TIMEOUT_UDP_OFFLOAD] = {
830 .procname = "nf_flowtable_udp_timeout",
831 .maxlen = sizeof(unsigned int),
833 .proc_handler = proc_dointvec_jiffies,
835 [NF_SYSCTL_CT_PROTO_TIMEOUT_UDP_OFFLOAD_PICKUP] = {
836 .procname = "nf_flowtable_udp_pickup",
837 .maxlen = sizeof(unsigned int),
839 .proc_handler = proc_dointvec_jiffies,
842 [NF_SYSCTL_CT_PROTO_TIMEOUT_ICMP] = {
843 .procname = "nf_conntrack_icmp_timeout",
844 .maxlen = sizeof(unsigned int),
846 .proc_handler = proc_dointvec_jiffies,
848 [NF_SYSCTL_CT_PROTO_TIMEOUT_ICMPV6] = {
849 .procname = "nf_conntrack_icmpv6_timeout",
850 .maxlen = sizeof(unsigned int),
852 .proc_handler = proc_dointvec_jiffies,
854 #ifdef CONFIG_NF_CT_PROTO_SCTP
855 [NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_CLOSED] = {
856 .procname = "nf_conntrack_sctp_timeout_closed",
857 .maxlen = sizeof(unsigned int),
859 .proc_handler = proc_dointvec_jiffies,
861 [NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_COOKIE_WAIT] = {
862 .procname = "nf_conntrack_sctp_timeout_cookie_wait",
863 .maxlen = sizeof(unsigned int),
865 .proc_handler = proc_dointvec_jiffies,
867 [NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_COOKIE_ECHOED] = {
868 .procname = "nf_conntrack_sctp_timeout_cookie_echoed",
869 .maxlen = sizeof(unsigned int),
871 .proc_handler = proc_dointvec_jiffies,
873 [NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_ESTABLISHED] = {
874 .procname = "nf_conntrack_sctp_timeout_established",
875 .maxlen = sizeof(unsigned int),
877 .proc_handler = proc_dointvec_jiffies,
879 [NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_SHUTDOWN_SENT] = {
880 .procname = "nf_conntrack_sctp_timeout_shutdown_sent",
881 .maxlen = sizeof(unsigned int),
883 .proc_handler = proc_dointvec_jiffies,
885 [NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_SHUTDOWN_RECD] = {
886 .procname = "nf_conntrack_sctp_timeout_shutdown_recd",
887 .maxlen = sizeof(unsigned int),
889 .proc_handler = proc_dointvec_jiffies,
891 [NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_SHUTDOWN_ACK_SENT] = {
892 .procname = "nf_conntrack_sctp_timeout_shutdown_ack_sent",
893 .maxlen = sizeof(unsigned int),
895 .proc_handler = proc_dointvec_jiffies,
897 [NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_HEARTBEAT_SENT] = {
898 .procname = "nf_conntrack_sctp_timeout_heartbeat_sent",
899 .maxlen = sizeof(unsigned int),
901 .proc_handler = proc_dointvec_jiffies,
903 [NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_HEARTBEAT_ACKED] = {
904 .procname = "nf_conntrack_sctp_timeout_heartbeat_acked",
905 .maxlen = sizeof(unsigned int),
907 .proc_handler = proc_dointvec_jiffies,
910 #ifdef CONFIG_NF_CT_PROTO_DCCP
911 [NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_REQUEST] = {
912 .procname = "nf_conntrack_dccp_timeout_request",
913 .maxlen = sizeof(unsigned int),
915 .proc_handler = proc_dointvec_jiffies,
917 [NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_RESPOND] = {
918 .procname = "nf_conntrack_dccp_timeout_respond",
919 .maxlen = sizeof(unsigned int),
921 .proc_handler = proc_dointvec_jiffies,
923 [NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_PARTOPEN] = {
924 .procname = "nf_conntrack_dccp_timeout_partopen",
925 .maxlen = sizeof(unsigned int),
927 .proc_handler = proc_dointvec_jiffies,
929 [NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_OPEN] = {
930 .procname = "nf_conntrack_dccp_timeout_open",
931 .maxlen = sizeof(unsigned int),
933 .proc_handler = proc_dointvec_jiffies,
935 [NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_CLOSEREQ] = {
936 .procname = "nf_conntrack_dccp_timeout_closereq",
937 .maxlen = sizeof(unsigned int),
939 .proc_handler = proc_dointvec_jiffies,
941 [NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_CLOSING] = {
942 .procname = "nf_conntrack_dccp_timeout_closing",
943 .maxlen = sizeof(unsigned int),
945 .proc_handler = proc_dointvec_jiffies,
947 [NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_TIMEWAIT] = {
948 .procname = "nf_conntrack_dccp_timeout_timewait",
949 .maxlen = sizeof(unsigned int),
951 .proc_handler = proc_dointvec_jiffies,
953 [NF_SYSCTL_CT_PROTO_DCCP_LOOSE] = {
954 .procname = "nf_conntrack_dccp_loose",
955 .maxlen = sizeof(u8),
957 .proc_handler = proc_dou8vec_minmax,
958 .extra1 = SYSCTL_ZERO,
959 .extra2 = SYSCTL_ONE,
962 #ifdef CONFIG_NF_CT_PROTO_GRE
963 [NF_SYSCTL_CT_PROTO_TIMEOUT_GRE] = {
964 .procname = "nf_conntrack_gre_timeout",
965 .maxlen = sizeof(unsigned int),
967 .proc_handler = proc_dointvec_jiffies,
969 [NF_SYSCTL_CT_PROTO_TIMEOUT_GRE_STREAM] = {
970 .procname = "nf_conntrack_gre_timeout_stream",
971 .maxlen = sizeof(unsigned int),
973 .proc_handler = proc_dointvec_jiffies,
979 static struct ctl_table nf_ct_netfilter_table[] = {
981 .procname = "nf_conntrack_max",
982 .data = &nf_conntrack_max,
983 .maxlen = sizeof(int),
985 .proc_handler = proc_dointvec,
990 static void nf_conntrack_standalone_init_tcp_sysctl(struct net *net,
991 struct ctl_table *table)
993 struct nf_tcp_net *tn = nf_tcp_pernet(net);
995 #define XASSIGN(XNAME, tn) \
996 table[NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_ ## XNAME].data = \
997 &(tn)->timeouts[TCP_CONNTRACK_ ## XNAME]
999 XASSIGN(SYN_SENT, tn);
1000 XASSIGN(SYN_RECV, tn);
1001 XASSIGN(ESTABLISHED, tn);
1002 XASSIGN(FIN_WAIT, tn);
1003 XASSIGN(CLOSE_WAIT, tn);
1004 XASSIGN(LAST_ACK, tn);
1005 XASSIGN(TIME_WAIT, tn);
1007 XASSIGN(RETRANS, tn);
1010 #define XASSIGN(XNAME, rval) \
1011 table[NF_SYSCTL_CT_PROTO_TCP_ ## XNAME].data = (rval)
1013 XASSIGN(LOOSE, &tn->tcp_loose);
1014 XASSIGN(LIBERAL, &tn->tcp_be_liberal);
1015 XASSIGN(MAX_RETRANS, &tn->tcp_max_retrans);
1016 XASSIGN(IGNORE_INVALID_RST, &tn->tcp_ignore_invalid_rst);
1019 #if IS_ENABLED(CONFIG_NF_FLOW_TABLE)
1020 table[NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_OFFLOAD].data = &tn->offload_timeout;
1021 table[NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_OFFLOAD_PICKUP].data = &tn->offload_pickup;
1026 static void nf_conntrack_standalone_init_sctp_sysctl(struct net *net,
1027 struct ctl_table *table)
1029 #ifdef CONFIG_NF_CT_PROTO_SCTP
1030 struct nf_sctp_net *sn = nf_sctp_pernet(net);
1032 #define XASSIGN(XNAME, sn) \
1033 table[NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_ ## XNAME].data = \
1034 &(sn)->timeouts[SCTP_CONNTRACK_ ## XNAME]
1036 XASSIGN(CLOSED, sn);
1037 XASSIGN(COOKIE_WAIT, sn);
1038 XASSIGN(COOKIE_ECHOED, sn);
1039 XASSIGN(ESTABLISHED, sn);
1040 XASSIGN(SHUTDOWN_SENT, sn);
1041 XASSIGN(SHUTDOWN_RECD, sn);
1042 XASSIGN(SHUTDOWN_ACK_SENT, sn);
1043 XASSIGN(HEARTBEAT_SENT, sn);
1044 XASSIGN(HEARTBEAT_ACKED, sn);
1049 static void nf_conntrack_standalone_init_dccp_sysctl(struct net *net,
1050 struct ctl_table *table)
1052 #ifdef CONFIG_NF_CT_PROTO_DCCP
1053 struct nf_dccp_net *dn = nf_dccp_pernet(net);
1055 #define XASSIGN(XNAME, dn) \
1056 table[NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_ ## XNAME].data = \
1057 &(dn)->dccp_timeout[CT_DCCP_ ## XNAME]
1059 XASSIGN(REQUEST, dn);
1060 XASSIGN(RESPOND, dn);
1061 XASSIGN(PARTOPEN, dn);
1063 XASSIGN(CLOSEREQ, dn);
1064 XASSIGN(CLOSING, dn);
1065 XASSIGN(TIMEWAIT, dn);
1068 table[NF_SYSCTL_CT_PROTO_DCCP_LOOSE].data = &dn->dccp_loose;
1072 static void nf_conntrack_standalone_init_gre_sysctl(struct net *net,
1073 struct ctl_table *table)
1075 #ifdef CONFIG_NF_CT_PROTO_GRE
1076 struct nf_gre_net *gn = nf_gre_pernet(net);
1078 table[NF_SYSCTL_CT_PROTO_TIMEOUT_GRE].data = &gn->timeouts[GRE_CT_UNREPLIED];
1079 table[NF_SYSCTL_CT_PROTO_TIMEOUT_GRE_STREAM].data = &gn->timeouts[GRE_CT_REPLIED];
1083 static int nf_conntrack_standalone_init_sysctl(struct net *net)
1085 struct nf_conntrack_net *cnet = nf_ct_pernet(net);
1086 struct nf_udp_net *un = nf_udp_pernet(net);
1087 struct ctl_table *table;
1089 BUILD_BUG_ON(ARRAY_SIZE(nf_ct_sysctl_table) != NF_SYSCTL_CT_LAST_SYSCTL);
1091 table = kmemdup(nf_ct_sysctl_table, sizeof(nf_ct_sysctl_table),
1096 table[NF_SYSCTL_CT_COUNT].data = &cnet->count;
1097 table[NF_SYSCTL_CT_CHECKSUM].data = &net->ct.sysctl_checksum;
1098 table[NF_SYSCTL_CT_LOG_INVALID].data = &net->ct.sysctl_log_invalid;
1099 table[NF_SYSCTL_CT_ACCT].data = &net->ct.sysctl_acct;
1100 table[NF_SYSCTL_CT_HELPER].data = &cnet->sysctl_auto_assign_helper;
1101 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1102 table[NF_SYSCTL_CT_EVENTS].data = &net->ct.sysctl_events;
1104 #ifdef CONFIG_NF_CONNTRACK_TIMESTAMP
1105 table[NF_SYSCTL_CT_TIMESTAMP].data = &net->ct.sysctl_tstamp;
1107 table[NF_SYSCTL_CT_PROTO_TIMEOUT_GENERIC].data = &nf_generic_pernet(net)->timeout;
1108 table[NF_SYSCTL_CT_PROTO_TIMEOUT_ICMP].data = &nf_icmp_pernet(net)->timeout;
1109 table[NF_SYSCTL_CT_PROTO_TIMEOUT_ICMPV6].data = &nf_icmpv6_pernet(net)->timeout;
1110 table[NF_SYSCTL_CT_PROTO_TIMEOUT_UDP].data = &un->timeouts[UDP_CT_UNREPLIED];
1111 table[NF_SYSCTL_CT_PROTO_TIMEOUT_UDP_STREAM].data = &un->timeouts[UDP_CT_REPLIED];
1112 #if IS_ENABLED(CONFIG_NF_FLOW_TABLE)
1113 table[NF_SYSCTL_CT_PROTO_TIMEOUT_UDP_OFFLOAD].data = &un->offload_timeout;
1114 table[NF_SYSCTL_CT_PROTO_TIMEOUT_UDP_OFFLOAD_PICKUP].data = &un->offload_pickup;
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);