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_MAX_RETRANS,
583 NF_SYSCTL_CT_PROTO_TIMEOUT_UDP,
584 NF_SYSCTL_CT_PROTO_TIMEOUT_UDP_STREAM,
585 #if IS_ENABLED(CONFIG_NF_FLOW_TABLE)
586 NF_SYSCTL_CT_PROTO_TIMEOUT_UDP_OFFLOAD,
587 NF_SYSCTL_CT_PROTO_TIMEOUT_UDP_OFFLOAD_PICKUP,
589 NF_SYSCTL_CT_PROTO_TIMEOUT_ICMP,
590 NF_SYSCTL_CT_PROTO_TIMEOUT_ICMPV6,
591 #ifdef CONFIG_NF_CT_PROTO_SCTP
592 NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_CLOSED,
593 NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_COOKIE_WAIT,
594 NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_COOKIE_ECHOED,
595 NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_ESTABLISHED,
596 NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_SHUTDOWN_SENT,
597 NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_SHUTDOWN_RECD,
598 NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_SHUTDOWN_ACK_SENT,
599 NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_HEARTBEAT_SENT,
600 NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_HEARTBEAT_ACKED,
602 #ifdef CONFIG_NF_CT_PROTO_DCCP
603 NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_REQUEST,
604 NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_RESPOND,
605 NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_PARTOPEN,
606 NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_OPEN,
607 NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_CLOSEREQ,
608 NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_CLOSING,
609 NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_TIMEWAIT,
610 NF_SYSCTL_CT_PROTO_DCCP_LOOSE,
612 #ifdef CONFIG_NF_CT_PROTO_GRE
613 NF_SYSCTL_CT_PROTO_TIMEOUT_GRE,
614 NF_SYSCTL_CT_PROTO_TIMEOUT_GRE_STREAM,
617 __NF_SYSCTL_CT_LAST_SYSCTL,
620 #define NF_SYSCTL_CT_LAST_SYSCTL (__NF_SYSCTL_CT_LAST_SYSCTL + 1)
622 static struct ctl_table nf_ct_sysctl_table[] = {
623 [NF_SYSCTL_CT_MAX] = {
624 .procname = "nf_conntrack_max",
625 .data = &nf_conntrack_max,
626 .maxlen = sizeof(int),
628 .proc_handler = proc_dointvec,
630 [NF_SYSCTL_CT_COUNT] = {
631 .procname = "nf_conntrack_count",
632 .maxlen = sizeof(int),
634 .proc_handler = proc_dointvec,
636 [NF_SYSCTL_CT_BUCKETS] = {
637 .procname = "nf_conntrack_buckets",
638 .data = &nf_conntrack_htable_size_user,
639 .maxlen = sizeof(unsigned int),
641 .proc_handler = nf_conntrack_hash_sysctl,
643 [NF_SYSCTL_CT_CHECKSUM] = {
644 .procname = "nf_conntrack_checksum",
645 .data = &init_net.ct.sysctl_checksum,
646 .maxlen = sizeof(u8),
648 .proc_handler = proc_dou8vec_minmax,
649 .extra1 = SYSCTL_ZERO,
650 .extra2 = SYSCTL_ONE,
652 [NF_SYSCTL_CT_LOG_INVALID] = {
653 .procname = "nf_conntrack_log_invalid",
654 .data = &init_net.ct.sysctl_log_invalid,
655 .maxlen = sizeof(u8),
657 .proc_handler = proc_dou8vec_minmax,
659 [NF_SYSCTL_CT_EXPECT_MAX] = {
660 .procname = "nf_conntrack_expect_max",
661 .data = &nf_ct_expect_max,
662 .maxlen = sizeof(int),
664 .proc_handler = proc_dointvec,
666 [NF_SYSCTL_CT_ACCT] = {
667 .procname = "nf_conntrack_acct",
668 .data = &init_net.ct.sysctl_acct,
669 .maxlen = sizeof(u8),
671 .proc_handler = proc_dou8vec_minmax,
672 .extra1 = SYSCTL_ZERO,
673 .extra2 = SYSCTL_ONE,
675 [NF_SYSCTL_CT_HELPER] = {
676 .procname = "nf_conntrack_helper",
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_ONE,
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,
778 [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_OFFLOAD_PICKUP] = {
779 .procname = "nf_flowtable_tcp_pickup",
780 .maxlen = sizeof(unsigned int),
782 .proc_handler = proc_dointvec_jiffies,
785 [NF_SYSCTL_CT_PROTO_TCP_LOOSE] = {
786 .procname = "nf_conntrack_tcp_loose",
787 .maxlen = sizeof(u8),
789 .proc_handler = proc_dou8vec_minmax,
790 .extra1 = SYSCTL_ZERO,
791 .extra2 = SYSCTL_ONE,
793 [NF_SYSCTL_CT_PROTO_TCP_LIBERAL] = {
794 .procname = "nf_conntrack_tcp_be_liberal",
795 .maxlen = sizeof(u8),
797 .proc_handler = proc_dou8vec_minmax,
798 .extra1 = SYSCTL_ZERO,
799 .extra2 = SYSCTL_ONE,
801 [NF_SYSCTL_CT_PROTO_TCP_MAX_RETRANS] = {
802 .procname = "nf_conntrack_tcp_max_retrans",
803 .maxlen = sizeof(u8),
805 .proc_handler = proc_dou8vec_minmax,
807 [NF_SYSCTL_CT_PROTO_TIMEOUT_UDP] = {
808 .procname = "nf_conntrack_udp_timeout",
809 .maxlen = sizeof(unsigned int),
811 .proc_handler = proc_dointvec_jiffies,
813 [NF_SYSCTL_CT_PROTO_TIMEOUT_UDP_STREAM] = {
814 .procname = "nf_conntrack_udp_timeout_stream",
815 .maxlen = sizeof(unsigned int),
817 .proc_handler = proc_dointvec_jiffies,
819 #if IS_ENABLED(CONFIG_NFT_FLOW_OFFLOAD)
820 [NF_SYSCTL_CT_PROTO_TIMEOUT_UDP_OFFLOAD] = {
821 .procname = "nf_flowtable_udp_timeout",
822 .maxlen = sizeof(unsigned int),
824 .proc_handler = proc_dointvec_jiffies,
826 [NF_SYSCTL_CT_PROTO_TIMEOUT_UDP_OFFLOAD_PICKUP] = {
827 .procname = "nf_flowtable_udp_pickup",
828 .maxlen = sizeof(unsigned int),
830 .proc_handler = proc_dointvec_jiffies,
833 [NF_SYSCTL_CT_PROTO_TIMEOUT_ICMP] = {
834 .procname = "nf_conntrack_icmp_timeout",
835 .maxlen = sizeof(unsigned int),
837 .proc_handler = proc_dointvec_jiffies,
839 [NF_SYSCTL_CT_PROTO_TIMEOUT_ICMPV6] = {
840 .procname = "nf_conntrack_icmpv6_timeout",
841 .maxlen = sizeof(unsigned int),
843 .proc_handler = proc_dointvec_jiffies,
845 #ifdef CONFIG_NF_CT_PROTO_SCTP
846 [NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_CLOSED] = {
847 .procname = "nf_conntrack_sctp_timeout_closed",
848 .maxlen = sizeof(unsigned int),
850 .proc_handler = proc_dointvec_jiffies,
852 [NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_COOKIE_WAIT] = {
853 .procname = "nf_conntrack_sctp_timeout_cookie_wait",
854 .maxlen = sizeof(unsigned int),
856 .proc_handler = proc_dointvec_jiffies,
858 [NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_COOKIE_ECHOED] = {
859 .procname = "nf_conntrack_sctp_timeout_cookie_echoed",
860 .maxlen = sizeof(unsigned int),
862 .proc_handler = proc_dointvec_jiffies,
864 [NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_ESTABLISHED] = {
865 .procname = "nf_conntrack_sctp_timeout_established",
866 .maxlen = sizeof(unsigned int),
868 .proc_handler = proc_dointvec_jiffies,
870 [NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_SHUTDOWN_SENT] = {
871 .procname = "nf_conntrack_sctp_timeout_shutdown_sent",
872 .maxlen = sizeof(unsigned int),
874 .proc_handler = proc_dointvec_jiffies,
876 [NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_SHUTDOWN_RECD] = {
877 .procname = "nf_conntrack_sctp_timeout_shutdown_recd",
878 .maxlen = sizeof(unsigned int),
880 .proc_handler = proc_dointvec_jiffies,
882 [NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_SHUTDOWN_ACK_SENT] = {
883 .procname = "nf_conntrack_sctp_timeout_shutdown_ack_sent",
884 .maxlen = sizeof(unsigned int),
886 .proc_handler = proc_dointvec_jiffies,
888 [NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_HEARTBEAT_SENT] = {
889 .procname = "nf_conntrack_sctp_timeout_heartbeat_sent",
890 .maxlen = sizeof(unsigned int),
892 .proc_handler = proc_dointvec_jiffies,
894 [NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_HEARTBEAT_ACKED] = {
895 .procname = "nf_conntrack_sctp_timeout_heartbeat_acked",
896 .maxlen = sizeof(unsigned int),
898 .proc_handler = proc_dointvec_jiffies,
901 #ifdef CONFIG_NF_CT_PROTO_DCCP
902 [NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_REQUEST] = {
903 .procname = "nf_conntrack_dccp_timeout_request",
904 .maxlen = sizeof(unsigned int),
906 .proc_handler = proc_dointvec_jiffies,
908 [NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_RESPOND] = {
909 .procname = "nf_conntrack_dccp_timeout_respond",
910 .maxlen = sizeof(unsigned int),
912 .proc_handler = proc_dointvec_jiffies,
914 [NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_PARTOPEN] = {
915 .procname = "nf_conntrack_dccp_timeout_partopen",
916 .maxlen = sizeof(unsigned int),
918 .proc_handler = proc_dointvec_jiffies,
920 [NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_OPEN] = {
921 .procname = "nf_conntrack_dccp_timeout_open",
922 .maxlen = sizeof(unsigned int),
924 .proc_handler = proc_dointvec_jiffies,
926 [NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_CLOSEREQ] = {
927 .procname = "nf_conntrack_dccp_timeout_closereq",
928 .maxlen = sizeof(unsigned int),
930 .proc_handler = proc_dointvec_jiffies,
932 [NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_CLOSING] = {
933 .procname = "nf_conntrack_dccp_timeout_closing",
934 .maxlen = sizeof(unsigned int),
936 .proc_handler = proc_dointvec_jiffies,
938 [NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_TIMEWAIT] = {
939 .procname = "nf_conntrack_dccp_timeout_timewait",
940 .maxlen = sizeof(unsigned int),
942 .proc_handler = proc_dointvec_jiffies,
944 [NF_SYSCTL_CT_PROTO_DCCP_LOOSE] = {
945 .procname = "nf_conntrack_dccp_loose",
946 .maxlen = sizeof(u8),
948 .proc_handler = proc_dou8vec_minmax,
949 .extra1 = SYSCTL_ZERO,
950 .extra2 = SYSCTL_ONE,
953 #ifdef CONFIG_NF_CT_PROTO_GRE
954 [NF_SYSCTL_CT_PROTO_TIMEOUT_GRE] = {
955 .procname = "nf_conntrack_gre_timeout",
956 .maxlen = sizeof(unsigned int),
958 .proc_handler = proc_dointvec_jiffies,
960 [NF_SYSCTL_CT_PROTO_TIMEOUT_GRE_STREAM] = {
961 .procname = "nf_conntrack_gre_timeout_stream",
962 .maxlen = sizeof(unsigned int),
964 .proc_handler = proc_dointvec_jiffies,
970 static struct ctl_table nf_ct_netfilter_table[] = {
972 .procname = "nf_conntrack_max",
973 .data = &nf_conntrack_max,
974 .maxlen = sizeof(int),
976 .proc_handler = proc_dointvec,
981 static void nf_conntrack_standalone_init_tcp_sysctl(struct net *net,
982 struct ctl_table *table)
984 struct nf_tcp_net *tn = nf_tcp_pernet(net);
986 #define XASSIGN(XNAME, tn) \
987 table[NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_ ## XNAME].data = \
988 &(tn)->timeouts[TCP_CONNTRACK_ ## XNAME]
990 XASSIGN(SYN_SENT, tn);
991 XASSIGN(SYN_RECV, tn);
992 XASSIGN(ESTABLISHED, tn);
993 XASSIGN(FIN_WAIT, tn);
994 XASSIGN(CLOSE_WAIT, tn);
995 XASSIGN(LAST_ACK, tn);
996 XASSIGN(TIME_WAIT, tn);
998 XASSIGN(RETRANS, tn);
1001 #define XASSIGN(XNAME, rval) \
1002 table[NF_SYSCTL_CT_PROTO_TCP_ ## XNAME].data = (rval)
1004 XASSIGN(LOOSE, &tn->tcp_loose);
1005 XASSIGN(LIBERAL, &tn->tcp_be_liberal);
1006 XASSIGN(MAX_RETRANS, &tn->tcp_max_retrans);
1009 #if IS_ENABLED(CONFIG_NF_FLOW_TABLE)
1010 table[NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_OFFLOAD].data = &tn->offload_timeout;
1011 table[NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_OFFLOAD_PICKUP].data = &tn->offload_pickup;
1016 static void nf_conntrack_standalone_init_sctp_sysctl(struct net *net,
1017 struct ctl_table *table)
1019 #ifdef CONFIG_NF_CT_PROTO_SCTP
1020 struct nf_sctp_net *sn = nf_sctp_pernet(net);
1022 #define XASSIGN(XNAME, sn) \
1023 table[NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_ ## XNAME].data = \
1024 &(sn)->timeouts[SCTP_CONNTRACK_ ## XNAME]
1026 XASSIGN(CLOSED, sn);
1027 XASSIGN(COOKIE_WAIT, sn);
1028 XASSIGN(COOKIE_ECHOED, sn);
1029 XASSIGN(ESTABLISHED, sn);
1030 XASSIGN(SHUTDOWN_SENT, sn);
1031 XASSIGN(SHUTDOWN_RECD, sn);
1032 XASSIGN(SHUTDOWN_ACK_SENT, sn);
1033 XASSIGN(HEARTBEAT_SENT, sn);
1034 XASSIGN(HEARTBEAT_ACKED, sn);
1039 static void nf_conntrack_standalone_init_dccp_sysctl(struct net *net,
1040 struct ctl_table *table)
1042 #ifdef CONFIG_NF_CT_PROTO_DCCP
1043 struct nf_dccp_net *dn = nf_dccp_pernet(net);
1045 #define XASSIGN(XNAME, dn) \
1046 table[NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_ ## XNAME].data = \
1047 &(dn)->dccp_timeout[CT_DCCP_ ## XNAME]
1049 XASSIGN(REQUEST, dn);
1050 XASSIGN(RESPOND, dn);
1051 XASSIGN(PARTOPEN, dn);
1053 XASSIGN(CLOSEREQ, dn);
1054 XASSIGN(CLOSING, dn);
1055 XASSIGN(TIMEWAIT, dn);
1058 table[NF_SYSCTL_CT_PROTO_DCCP_LOOSE].data = &dn->dccp_loose;
1062 static void nf_conntrack_standalone_init_gre_sysctl(struct net *net,
1063 struct ctl_table *table)
1065 #ifdef CONFIG_NF_CT_PROTO_GRE
1066 struct nf_gre_net *gn = nf_gre_pernet(net);
1068 table[NF_SYSCTL_CT_PROTO_TIMEOUT_GRE].data = &gn->timeouts[GRE_CT_UNREPLIED];
1069 table[NF_SYSCTL_CT_PROTO_TIMEOUT_GRE_STREAM].data = &gn->timeouts[GRE_CT_REPLIED];
1073 static int nf_conntrack_standalone_init_sysctl(struct net *net)
1075 struct nf_conntrack_net *cnet = nf_ct_pernet(net);
1076 struct nf_udp_net *un = nf_udp_pernet(net);
1077 struct ctl_table *table;
1079 BUILD_BUG_ON(ARRAY_SIZE(nf_ct_sysctl_table) != NF_SYSCTL_CT_LAST_SYSCTL);
1081 table = kmemdup(nf_ct_sysctl_table, sizeof(nf_ct_sysctl_table),
1086 table[NF_SYSCTL_CT_COUNT].data = &cnet->count;
1087 table[NF_SYSCTL_CT_CHECKSUM].data = &net->ct.sysctl_checksum;
1088 table[NF_SYSCTL_CT_LOG_INVALID].data = &net->ct.sysctl_log_invalid;
1089 table[NF_SYSCTL_CT_ACCT].data = &net->ct.sysctl_acct;
1090 table[NF_SYSCTL_CT_HELPER].data = &cnet->sysctl_auto_assign_helper;
1091 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1092 table[NF_SYSCTL_CT_EVENTS].data = &net->ct.sysctl_events;
1094 #ifdef CONFIG_NF_CONNTRACK_TIMESTAMP
1095 table[NF_SYSCTL_CT_TIMESTAMP].data = &net->ct.sysctl_tstamp;
1097 table[NF_SYSCTL_CT_PROTO_TIMEOUT_GENERIC].data = &nf_generic_pernet(net)->timeout;
1098 table[NF_SYSCTL_CT_PROTO_TIMEOUT_ICMP].data = &nf_icmp_pernet(net)->timeout;
1099 table[NF_SYSCTL_CT_PROTO_TIMEOUT_ICMPV6].data = &nf_icmpv6_pernet(net)->timeout;
1100 table[NF_SYSCTL_CT_PROTO_TIMEOUT_UDP].data = &un->timeouts[UDP_CT_UNREPLIED];
1101 table[NF_SYSCTL_CT_PROTO_TIMEOUT_UDP_STREAM].data = &un->timeouts[UDP_CT_REPLIED];
1102 #if IS_ENABLED(CONFIG_NF_FLOW_TABLE)
1103 table[NF_SYSCTL_CT_PROTO_TIMEOUT_UDP_OFFLOAD].data = &un->offload_timeout;
1104 table[NF_SYSCTL_CT_PROTO_TIMEOUT_UDP_OFFLOAD_PICKUP].data = &un->offload_pickup;
1107 nf_conntrack_standalone_init_tcp_sysctl(net, table);
1108 nf_conntrack_standalone_init_sctp_sysctl(net, table);
1109 nf_conntrack_standalone_init_dccp_sysctl(net, table);
1110 nf_conntrack_standalone_init_gre_sysctl(net, table);
1112 /* Don't allow non-init_net ns to alter global sysctls */
1113 if (!net_eq(&init_net, net)) {
1114 table[NF_SYSCTL_CT_MAX].mode = 0444;
1115 table[NF_SYSCTL_CT_EXPECT_MAX].mode = 0444;
1116 table[NF_SYSCTL_CT_BUCKETS].mode = 0444;
1119 cnet->sysctl_header = register_net_sysctl(net, "net/netfilter", table);
1120 if (!cnet->sysctl_header)
1121 goto out_unregister_netfilter;
1125 out_unregister_netfilter:
1130 static void nf_conntrack_standalone_fini_sysctl(struct net *net)
1132 struct nf_conntrack_net *cnet = nf_ct_pernet(net);
1133 struct ctl_table *table;
1135 table = cnet->sysctl_header->ctl_table_arg;
1136 unregister_net_sysctl_table(cnet->sysctl_header);
1140 static int nf_conntrack_standalone_init_sysctl(struct net *net)
1145 static void nf_conntrack_standalone_fini_sysctl(struct net *net)
1148 #endif /* CONFIG_SYSCTL */
1150 static void nf_conntrack_fini_net(struct net *net)
1153 nf_ct_netns_put(net, NFPROTO_INET);
1155 nf_conntrack_standalone_fini_proc(net);
1156 nf_conntrack_standalone_fini_sysctl(net);
1159 static int nf_conntrack_pernet_init(struct net *net)
1163 net->ct.sysctl_checksum = 1;
1165 ret = nf_conntrack_standalone_init_sysctl(net);
1169 ret = nf_conntrack_standalone_init_proc(net);
1173 ret = nf_conntrack_init_net(net);
1178 ret = nf_ct_netns_get(net, NFPROTO_INET);
1186 nf_conntrack_cleanup_net(net);
1188 nf_conntrack_standalone_fini_proc(net);
1190 nf_conntrack_standalone_fini_sysctl(net);
1194 static void nf_conntrack_pernet_exit(struct list_head *net_exit_list)
1198 list_for_each_entry(net, net_exit_list, exit_list)
1199 nf_conntrack_fini_net(net);
1201 nf_conntrack_cleanup_net_list(net_exit_list);
1204 static struct pernet_operations nf_conntrack_net_ops = {
1205 .init = nf_conntrack_pernet_init,
1206 .exit_batch = nf_conntrack_pernet_exit,
1207 .id = &nf_conntrack_net_id,
1208 .size = sizeof(struct nf_conntrack_net),
1211 static int __init nf_conntrack_standalone_init(void)
1213 int ret = nf_conntrack_init_start();
1217 BUILD_BUG_ON(NFCT_INFOMASK <= IP_CT_NUMBER);
1219 #ifdef CONFIG_SYSCTL
1220 nf_ct_netfilter_header =
1221 register_net_sysctl(&init_net, "net", nf_ct_netfilter_table);
1222 if (!nf_ct_netfilter_header) {
1223 pr_err("nf_conntrack: can't register to sysctl.\n");
1228 nf_conntrack_htable_size_user = nf_conntrack_htable_size;
1231 ret = register_pernet_subsys(&nf_conntrack_net_ops);
1235 nf_conntrack_init_end();
1239 #ifdef CONFIG_SYSCTL
1240 unregister_net_sysctl_table(nf_ct_netfilter_header);
1243 nf_conntrack_cleanup_end();
1248 static void __exit nf_conntrack_standalone_fini(void)
1250 nf_conntrack_cleanup_start();
1251 unregister_pernet_subsys(&nf_conntrack_net_ops);
1252 #ifdef CONFIG_SYSCTL
1253 unregister_net_sysctl_table(nf_ct_netfilter_header);
1255 nf_conntrack_cleanup_end();
1258 module_init(nf_conntrack_standalone_init);
1259 module_exit(nf_conntrack_standalone_fini);