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 unsigned int nr_conntracks = atomic_read(&net->ct.count);
429 const struct ip_conntrack_stat *st = v;
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 seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x %08x "
437 "%08x %08x %08x %08x %08x %08x %08x %08x %08x\n",
460 static const struct seq_operations ct_cpu_seq_ops = {
461 .start = ct_cpu_seq_start,
462 .next = ct_cpu_seq_next,
463 .stop = ct_cpu_seq_stop,
464 .show = ct_cpu_seq_show,
467 static int nf_conntrack_standalone_init_proc(struct net *net)
469 struct proc_dir_entry *pde;
473 pde = proc_create_net("nf_conntrack", 0440, net->proc_net, &ct_seq_ops,
474 sizeof(struct ct_iter_state));
476 goto out_nf_conntrack;
478 root_uid = make_kuid(net->user_ns, 0);
479 root_gid = make_kgid(net->user_ns, 0);
480 if (uid_valid(root_uid) && gid_valid(root_gid))
481 proc_set_user(pde, root_uid, root_gid);
483 pde = proc_create_net("nf_conntrack", 0444, net->proc_net_stat,
484 &ct_cpu_seq_ops, sizeof(struct seq_net_private));
486 goto out_stat_nf_conntrack;
489 out_stat_nf_conntrack:
490 remove_proc_entry("nf_conntrack", net->proc_net);
495 static void nf_conntrack_standalone_fini_proc(struct net *net)
497 remove_proc_entry("nf_conntrack", net->proc_net_stat);
498 remove_proc_entry("nf_conntrack", net->proc_net);
501 static int nf_conntrack_standalone_init_proc(struct net *net)
506 static void nf_conntrack_standalone_fini_proc(struct net *net)
509 #endif /* CONFIG_NF_CONNTRACK_PROCFS */
514 /* Log invalid packets of a given protocol */
515 static int log_invalid_proto_min __read_mostly;
516 static int log_invalid_proto_max __read_mostly = 255;
518 /* size the user *wants to set */
519 static unsigned int nf_conntrack_htable_size_user __read_mostly;
522 nf_conntrack_hash_sysctl(struct ctl_table *table, int write,
523 void *buffer, size_t *lenp, loff_t *ppos)
527 /* module_param hashsize could have changed value */
528 nf_conntrack_htable_size_user = nf_conntrack_htable_size;
530 ret = proc_dointvec(table, write, buffer, lenp, ppos);
531 if (ret < 0 || !write)
534 /* update ret, we might not be able to satisfy request */
535 ret = nf_conntrack_hash_resize(nf_conntrack_htable_size_user);
537 /* update it to the actual value used by conntrack */
538 nf_conntrack_htable_size_user = nf_conntrack_htable_size;
542 static struct ctl_table_header *nf_ct_netfilter_header;
544 enum nf_ct_sysctl_index {
547 NF_SYSCTL_CT_BUCKETS,
548 NF_SYSCTL_CT_CHECKSUM,
549 NF_SYSCTL_CT_LOG_INVALID,
550 NF_SYSCTL_CT_EXPECT_MAX,
553 #ifdef CONFIG_NF_CONNTRACK_EVENTS
556 #ifdef CONFIG_NF_CONNTRACK_TIMESTAMP
557 NF_SYSCTL_CT_TIMESTAMP,
559 NF_SYSCTL_CT_PROTO_TIMEOUT_GENERIC,
560 NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_SYN_SENT,
561 NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_SYN_RECV,
562 NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_ESTABLISHED,
563 NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_FIN_WAIT,
564 NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_CLOSE_WAIT,
565 NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_LAST_ACK,
566 NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_TIME_WAIT,
567 NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_CLOSE,
568 NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_RETRANS,
569 NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_UNACK,
570 NF_SYSCTL_CT_PROTO_TCP_LOOSE,
571 NF_SYSCTL_CT_PROTO_TCP_LIBERAL,
572 NF_SYSCTL_CT_PROTO_TCP_MAX_RETRANS,
573 NF_SYSCTL_CT_PROTO_TIMEOUT_UDP,
574 NF_SYSCTL_CT_PROTO_TIMEOUT_UDP_STREAM,
575 NF_SYSCTL_CT_PROTO_TIMEOUT_ICMP,
576 NF_SYSCTL_CT_PROTO_TIMEOUT_ICMPV6,
577 #ifdef CONFIG_NF_CT_PROTO_SCTP
578 NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_CLOSED,
579 NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_COOKIE_WAIT,
580 NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_COOKIE_ECHOED,
581 NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_ESTABLISHED,
582 NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_SHUTDOWN_SENT,
583 NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_SHUTDOWN_RECD,
584 NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_SHUTDOWN_ACK_SENT,
585 NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_HEARTBEAT_SENT,
586 NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_HEARTBEAT_ACKED,
588 #ifdef CONFIG_NF_CT_PROTO_DCCP
589 NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_REQUEST,
590 NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_RESPOND,
591 NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_PARTOPEN,
592 NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_OPEN,
593 NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_CLOSEREQ,
594 NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_CLOSING,
595 NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_TIMEWAIT,
596 NF_SYSCTL_CT_PROTO_DCCP_LOOSE,
598 #ifdef CONFIG_NF_CT_PROTO_GRE
599 NF_SYSCTL_CT_PROTO_TIMEOUT_GRE,
600 NF_SYSCTL_CT_PROTO_TIMEOUT_GRE_STREAM,
603 __NF_SYSCTL_CT_LAST_SYSCTL,
606 #define NF_SYSCTL_CT_LAST_SYSCTL (__NF_SYSCTL_CT_LAST_SYSCTL + 1)
608 static struct ctl_table nf_ct_sysctl_table[] = {
609 [NF_SYSCTL_CT_MAX] = {
610 .procname = "nf_conntrack_max",
611 .data = &nf_conntrack_max,
612 .maxlen = sizeof(int),
614 .proc_handler = proc_dointvec,
616 [NF_SYSCTL_CT_COUNT] = {
617 .procname = "nf_conntrack_count",
618 .data = &init_net.ct.count,
619 .maxlen = sizeof(int),
621 .proc_handler = proc_dointvec,
623 [NF_SYSCTL_CT_BUCKETS] = {
624 .procname = "nf_conntrack_buckets",
625 .data = &nf_conntrack_htable_size_user,
626 .maxlen = sizeof(unsigned int),
628 .proc_handler = nf_conntrack_hash_sysctl,
630 [NF_SYSCTL_CT_CHECKSUM] = {
631 .procname = "nf_conntrack_checksum",
632 .data = &init_net.ct.sysctl_checksum,
633 .maxlen = sizeof(int),
635 .proc_handler = proc_dointvec_minmax,
636 .extra1 = SYSCTL_ZERO,
637 .extra2 = SYSCTL_ONE,
639 [NF_SYSCTL_CT_LOG_INVALID] = {
640 .procname = "nf_conntrack_log_invalid",
641 .data = &init_net.ct.sysctl_log_invalid,
642 .maxlen = sizeof(unsigned int),
644 .proc_handler = proc_dointvec_minmax,
645 .extra1 = &log_invalid_proto_min,
646 .extra2 = &log_invalid_proto_max,
648 [NF_SYSCTL_CT_EXPECT_MAX] = {
649 .procname = "nf_conntrack_expect_max",
650 .data = &nf_ct_expect_max,
651 .maxlen = sizeof(int),
653 .proc_handler = proc_dointvec,
655 [NF_SYSCTL_CT_ACCT] = {
656 .procname = "nf_conntrack_acct",
657 .data = &init_net.ct.sysctl_acct,
658 .maxlen = sizeof(int),
660 .proc_handler = proc_dointvec_minmax,
661 .extra1 = SYSCTL_ZERO,
662 .extra2 = SYSCTL_ONE,
664 [NF_SYSCTL_CT_HELPER] = {
665 .procname = "nf_conntrack_helper",
666 .data = &init_net.ct.sysctl_auto_assign_helper,
667 .maxlen = sizeof(int),
669 .proc_handler = proc_dointvec_minmax,
670 .extra1 = SYSCTL_ZERO,
671 .extra2 = SYSCTL_ONE,
673 #ifdef CONFIG_NF_CONNTRACK_EVENTS
674 [NF_SYSCTL_CT_EVENTS] = {
675 .procname = "nf_conntrack_events",
676 .data = &init_net.ct.sysctl_events,
677 .maxlen = sizeof(int),
679 .proc_handler = proc_dointvec_minmax,
680 .extra1 = SYSCTL_ZERO,
681 .extra2 = SYSCTL_ONE,
684 #ifdef CONFIG_NF_CONNTRACK_TIMESTAMP
685 [NF_SYSCTL_CT_TIMESTAMP] = {
686 .procname = "nf_conntrack_timestamp",
687 .data = &init_net.ct.sysctl_tstamp,
688 .maxlen = sizeof(int),
690 .proc_handler = proc_dointvec_minmax,
691 .extra1 = SYSCTL_ZERO,
692 .extra2 = SYSCTL_ONE,
695 [NF_SYSCTL_CT_PROTO_TIMEOUT_GENERIC] = {
696 .procname = "nf_conntrack_generic_timeout",
697 .maxlen = sizeof(unsigned int),
699 .proc_handler = proc_dointvec_jiffies,
701 [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_SYN_SENT] = {
702 .procname = "nf_conntrack_tcp_timeout_syn_sent",
703 .maxlen = sizeof(unsigned int),
705 .proc_handler = proc_dointvec_jiffies,
707 [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_SYN_RECV] = {
708 .procname = "nf_conntrack_tcp_timeout_syn_recv",
709 .maxlen = sizeof(unsigned int),
711 .proc_handler = proc_dointvec_jiffies,
713 [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_ESTABLISHED] = {
714 .procname = "nf_conntrack_tcp_timeout_established",
715 .maxlen = sizeof(unsigned int),
717 .proc_handler = proc_dointvec_jiffies,
719 [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_FIN_WAIT] = {
720 .procname = "nf_conntrack_tcp_timeout_fin_wait",
721 .maxlen = sizeof(unsigned int),
723 .proc_handler = proc_dointvec_jiffies,
725 [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_CLOSE_WAIT] = {
726 .procname = "nf_conntrack_tcp_timeout_close_wait",
727 .maxlen = sizeof(unsigned int),
729 .proc_handler = proc_dointvec_jiffies,
731 [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_LAST_ACK] = {
732 .procname = "nf_conntrack_tcp_timeout_last_ack",
733 .maxlen = sizeof(unsigned int),
735 .proc_handler = proc_dointvec_jiffies,
737 [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_TIME_WAIT] = {
738 .procname = "nf_conntrack_tcp_timeout_time_wait",
739 .maxlen = sizeof(unsigned int),
741 .proc_handler = proc_dointvec_jiffies,
743 [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_CLOSE] = {
744 .procname = "nf_conntrack_tcp_timeout_close",
745 .maxlen = sizeof(unsigned int),
747 .proc_handler = proc_dointvec_jiffies,
749 [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_RETRANS] = {
750 .procname = "nf_conntrack_tcp_timeout_max_retrans",
751 .maxlen = sizeof(unsigned int),
753 .proc_handler = proc_dointvec_jiffies,
755 [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_UNACK] = {
756 .procname = "nf_conntrack_tcp_timeout_unacknowledged",
757 .maxlen = sizeof(unsigned int),
759 .proc_handler = proc_dointvec_jiffies,
761 [NF_SYSCTL_CT_PROTO_TCP_LOOSE] = {
762 .procname = "nf_conntrack_tcp_loose",
763 .maxlen = sizeof(int),
765 .proc_handler = proc_dointvec_minmax,
766 .extra1 = SYSCTL_ZERO,
767 .extra2 = SYSCTL_ONE,
769 [NF_SYSCTL_CT_PROTO_TCP_LIBERAL] = {
770 .procname = "nf_conntrack_tcp_be_liberal",
771 .maxlen = sizeof(int),
773 .proc_handler = proc_dointvec_minmax,
774 .extra1 = SYSCTL_ZERO,
775 .extra2 = SYSCTL_ONE,
777 [NF_SYSCTL_CT_PROTO_TCP_MAX_RETRANS] = {
778 .procname = "nf_conntrack_tcp_max_retrans",
779 .maxlen = sizeof(unsigned int),
781 .proc_handler = proc_dointvec,
783 [NF_SYSCTL_CT_PROTO_TIMEOUT_UDP] = {
784 .procname = "nf_conntrack_udp_timeout",
785 .maxlen = sizeof(unsigned int),
787 .proc_handler = proc_dointvec_jiffies,
789 [NF_SYSCTL_CT_PROTO_TIMEOUT_UDP_STREAM] = {
790 .procname = "nf_conntrack_udp_timeout_stream",
791 .maxlen = sizeof(unsigned int),
793 .proc_handler = proc_dointvec_jiffies,
795 [NF_SYSCTL_CT_PROTO_TIMEOUT_ICMP] = {
796 .procname = "nf_conntrack_icmp_timeout",
797 .maxlen = sizeof(unsigned int),
799 .proc_handler = proc_dointvec_jiffies,
801 [NF_SYSCTL_CT_PROTO_TIMEOUT_ICMPV6] = {
802 .procname = "nf_conntrack_icmpv6_timeout",
803 .maxlen = sizeof(unsigned int),
805 .proc_handler = proc_dointvec_jiffies,
807 #ifdef CONFIG_NF_CT_PROTO_SCTP
808 [NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_CLOSED] = {
809 .procname = "nf_conntrack_sctp_timeout_closed",
810 .maxlen = sizeof(unsigned int),
812 .proc_handler = proc_dointvec_jiffies,
814 [NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_COOKIE_WAIT] = {
815 .procname = "nf_conntrack_sctp_timeout_cookie_wait",
816 .maxlen = sizeof(unsigned int),
818 .proc_handler = proc_dointvec_jiffies,
820 [NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_COOKIE_ECHOED] = {
821 .procname = "nf_conntrack_sctp_timeout_cookie_echoed",
822 .maxlen = sizeof(unsigned int),
824 .proc_handler = proc_dointvec_jiffies,
826 [NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_ESTABLISHED] = {
827 .procname = "nf_conntrack_sctp_timeout_established",
828 .maxlen = sizeof(unsigned int),
830 .proc_handler = proc_dointvec_jiffies,
832 [NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_SHUTDOWN_SENT] = {
833 .procname = "nf_conntrack_sctp_timeout_shutdown_sent",
834 .maxlen = sizeof(unsigned int),
836 .proc_handler = proc_dointvec_jiffies,
838 [NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_SHUTDOWN_RECD] = {
839 .procname = "nf_conntrack_sctp_timeout_shutdown_recd",
840 .maxlen = sizeof(unsigned int),
842 .proc_handler = proc_dointvec_jiffies,
844 [NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_SHUTDOWN_ACK_SENT] = {
845 .procname = "nf_conntrack_sctp_timeout_shutdown_ack_sent",
846 .maxlen = sizeof(unsigned int),
848 .proc_handler = proc_dointvec_jiffies,
850 [NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_HEARTBEAT_SENT] = {
851 .procname = "nf_conntrack_sctp_timeout_heartbeat_sent",
852 .maxlen = sizeof(unsigned int),
854 .proc_handler = proc_dointvec_jiffies,
856 [NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_HEARTBEAT_ACKED] = {
857 .procname = "nf_conntrack_sctp_timeout_heartbeat_acked",
858 .maxlen = sizeof(unsigned int),
860 .proc_handler = proc_dointvec_jiffies,
863 #ifdef CONFIG_NF_CT_PROTO_DCCP
864 [NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_REQUEST] = {
865 .procname = "nf_conntrack_dccp_timeout_request",
866 .maxlen = sizeof(unsigned int),
868 .proc_handler = proc_dointvec_jiffies,
870 [NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_RESPOND] = {
871 .procname = "nf_conntrack_dccp_timeout_respond",
872 .maxlen = sizeof(unsigned int),
874 .proc_handler = proc_dointvec_jiffies,
876 [NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_PARTOPEN] = {
877 .procname = "nf_conntrack_dccp_timeout_partopen",
878 .maxlen = sizeof(unsigned int),
880 .proc_handler = proc_dointvec_jiffies,
882 [NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_OPEN] = {
883 .procname = "nf_conntrack_dccp_timeout_open",
884 .maxlen = sizeof(unsigned int),
886 .proc_handler = proc_dointvec_jiffies,
888 [NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_CLOSEREQ] = {
889 .procname = "nf_conntrack_dccp_timeout_closereq",
890 .maxlen = sizeof(unsigned int),
892 .proc_handler = proc_dointvec_jiffies,
894 [NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_CLOSING] = {
895 .procname = "nf_conntrack_dccp_timeout_closing",
896 .maxlen = sizeof(unsigned int),
898 .proc_handler = proc_dointvec_jiffies,
900 [NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_TIMEWAIT] = {
901 .procname = "nf_conntrack_dccp_timeout_timewait",
902 .maxlen = sizeof(unsigned int),
904 .proc_handler = proc_dointvec_jiffies,
906 [NF_SYSCTL_CT_PROTO_DCCP_LOOSE] = {
907 .procname = "nf_conntrack_dccp_loose",
908 .maxlen = sizeof(int),
910 .proc_handler = proc_dointvec_minmax,
911 .extra1 = SYSCTL_ZERO,
912 .extra2 = SYSCTL_ONE,
915 #ifdef CONFIG_NF_CT_PROTO_GRE
916 [NF_SYSCTL_CT_PROTO_TIMEOUT_GRE] = {
917 .procname = "nf_conntrack_gre_timeout",
918 .maxlen = sizeof(unsigned int),
920 .proc_handler = proc_dointvec_jiffies,
922 [NF_SYSCTL_CT_PROTO_TIMEOUT_GRE_STREAM] = {
923 .procname = "nf_conntrack_gre_timeout_stream",
924 .maxlen = sizeof(unsigned int),
926 .proc_handler = proc_dointvec_jiffies,
932 static struct ctl_table nf_ct_netfilter_table[] = {
934 .procname = "nf_conntrack_max",
935 .data = &nf_conntrack_max,
936 .maxlen = sizeof(int),
938 .proc_handler = proc_dointvec,
943 static void nf_conntrack_standalone_init_tcp_sysctl(struct net *net,
944 struct ctl_table *table)
946 struct nf_tcp_net *tn = nf_tcp_pernet(net);
948 #define XASSIGN(XNAME, tn) \
949 table[NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_ ## XNAME].data = \
950 &(tn)->timeouts[TCP_CONNTRACK_ ## XNAME]
952 XASSIGN(SYN_SENT, tn);
953 XASSIGN(SYN_RECV, tn);
954 XASSIGN(ESTABLISHED, tn);
955 XASSIGN(FIN_WAIT, tn);
956 XASSIGN(CLOSE_WAIT, tn);
957 XASSIGN(LAST_ACK, tn);
958 XASSIGN(TIME_WAIT, tn);
960 XASSIGN(RETRANS, tn);
963 #define XASSIGN(XNAME, rval) \
964 table[NF_SYSCTL_CT_PROTO_TCP_ ## XNAME].data = (rval)
966 XASSIGN(LOOSE, &tn->tcp_loose);
967 XASSIGN(LIBERAL, &tn->tcp_be_liberal);
968 XASSIGN(MAX_RETRANS, &tn->tcp_max_retrans);
972 static void nf_conntrack_standalone_init_sctp_sysctl(struct net *net,
973 struct ctl_table *table)
975 #ifdef CONFIG_NF_CT_PROTO_SCTP
976 struct nf_sctp_net *sn = nf_sctp_pernet(net);
978 #define XASSIGN(XNAME, sn) \
979 table[NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_ ## XNAME].data = \
980 &(sn)->timeouts[SCTP_CONNTRACK_ ## XNAME]
983 XASSIGN(COOKIE_WAIT, sn);
984 XASSIGN(COOKIE_ECHOED, sn);
985 XASSIGN(ESTABLISHED, sn);
986 XASSIGN(SHUTDOWN_SENT, sn);
987 XASSIGN(SHUTDOWN_RECD, sn);
988 XASSIGN(SHUTDOWN_ACK_SENT, sn);
989 XASSIGN(HEARTBEAT_SENT, sn);
990 XASSIGN(HEARTBEAT_ACKED, sn);
995 static void nf_conntrack_standalone_init_dccp_sysctl(struct net *net,
996 struct ctl_table *table)
998 #ifdef CONFIG_NF_CT_PROTO_DCCP
999 struct nf_dccp_net *dn = nf_dccp_pernet(net);
1001 #define XASSIGN(XNAME, dn) \
1002 table[NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_ ## XNAME].data = \
1003 &(dn)->dccp_timeout[CT_DCCP_ ## XNAME]
1005 XASSIGN(REQUEST, dn);
1006 XASSIGN(RESPOND, dn);
1007 XASSIGN(PARTOPEN, dn);
1009 XASSIGN(CLOSEREQ, dn);
1010 XASSIGN(CLOSING, dn);
1011 XASSIGN(TIMEWAIT, dn);
1014 table[NF_SYSCTL_CT_PROTO_DCCP_LOOSE].data = &dn->dccp_loose;
1018 static void nf_conntrack_standalone_init_gre_sysctl(struct net *net,
1019 struct ctl_table *table)
1021 #ifdef CONFIG_NF_CT_PROTO_GRE
1022 struct nf_gre_net *gn = nf_gre_pernet(net);
1024 table[NF_SYSCTL_CT_PROTO_TIMEOUT_GRE].data = &gn->timeouts[GRE_CT_UNREPLIED];
1025 table[NF_SYSCTL_CT_PROTO_TIMEOUT_GRE_STREAM].data = &gn->timeouts[GRE_CT_REPLIED];
1029 static int nf_conntrack_standalone_init_sysctl(struct net *net)
1031 struct nf_udp_net *un = nf_udp_pernet(net);
1032 struct ctl_table *table;
1034 BUILD_BUG_ON(ARRAY_SIZE(nf_ct_sysctl_table) != NF_SYSCTL_CT_LAST_SYSCTL);
1036 table = kmemdup(nf_ct_sysctl_table, sizeof(nf_ct_sysctl_table),
1041 table[NF_SYSCTL_CT_COUNT].data = &net->ct.count;
1042 table[NF_SYSCTL_CT_CHECKSUM].data = &net->ct.sysctl_checksum;
1043 table[NF_SYSCTL_CT_LOG_INVALID].data = &net->ct.sysctl_log_invalid;
1044 table[NF_SYSCTL_CT_ACCT].data = &net->ct.sysctl_acct;
1045 table[NF_SYSCTL_CT_HELPER].data = &net->ct.sysctl_auto_assign_helper;
1046 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1047 table[NF_SYSCTL_CT_EVENTS].data = &net->ct.sysctl_events;
1049 #ifdef CONFIG_NF_CONNTRACK_TIMESTAMP
1050 table[NF_SYSCTL_CT_TIMESTAMP].data = &net->ct.sysctl_tstamp;
1052 table[NF_SYSCTL_CT_PROTO_TIMEOUT_GENERIC].data = &nf_generic_pernet(net)->timeout;
1053 table[NF_SYSCTL_CT_PROTO_TIMEOUT_ICMP].data = &nf_icmp_pernet(net)->timeout;
1054 table[NF_SYSCTL_CT_PROTO_TIMEOUT_ICMPV6].data = &nf_icmpv6_pernet(net)->timeout;
1055 table[NF_SYSCTL_CT_PROTO_TIMEOUT_UDP].data = &un->timeouts[UDP_CT_UNREPLIED];
1056 table[NF_SYSCTL_CT_PROTO_TIMEOUT_UDP_STREAM].data = &un->timeouts[UDP_CT_REPLIED];
1058 nf_conntrack_standalone_init_tcp_sysctl(net, table);
1059 nf_conntrack_standalone_init_sctp_sysctl(net, table);
1060 nf_conntrack_standalone_init_dccp_sysctl(net, table);
1061 nf_conntrack_standalone_init_gre_sysctl(net, table);
1063 /* Don't allow unprivileged users to alter certain sysctls */
1064 if (net->user_ns != &init_user_ns) {
1065 table[NF_SYSCTL_CT_MAX].mode = 0444;
1066 table[NF_SYSCTL_CT_EXPECT_MAX].mode = 0444;
1067 table[NF_SYSCTL_CT_HELPER].mode = 0444;
1068 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1069 table[NF_SYSCTL_CT_EVENTS].mode = 0444;
1071 table[NF_SYSCTL_CT_BUCKETS].mode = 0444;
1072 } else if (!net_eq(&init_net, net)) {
1073 table[NF_SYSCTL_CT_BUCKETS].mode = 0444;
1076 net->ct.sysctl_header = register_net_sysctl(net, "net/netfilter", table);
1077 if (!net->ct.sysctl_header)
1078 goto out_unregister_netfilter;
1082 out_unregister_netfilter:
1087 static void nf_conntrack_standalone_fini_sysctl(struct net *net)
1089 struct ctl_table *table;
1091 table = net->ct.sysctl_header->ctl_table_arg;
1092 unregister_net_sysctl_table(net->ct.sysctl_header);
1096 static int nf_conntrack_standalone_init_sysctl(struct net *net)
1101 static void nf_conntrack_standalone_fini_sysctl(struct net *net)
1104 #endif /* CONFIG_SYSCTL */
1106 static void nf_conntrack_fini_net(struct net *net)
1109 nf_ct_netns_put(net, NFPROTO_INET);
1111 nf_conntrack_standalone_fini_proc(net);
1112 nf_conntrack_standalone_fini_sysctl(net);
1115 static int nf_conntrack_pernet_init(struct net *net)
1119 net->ct.sysctl_checksum = 1;
1121 ret = nf_conntrack_standalone_init_sysctl(net);
1125 ret = nf_conntrack_standalone_init_proc(net);
1129 ret = nf_conntrack_init_net(net);
1134 ret = nf_ct_netns_get(net, NFPROTO_INET);
1142 nf_conntrack_cleanup_net(net);
1144 nf_conntrack_standalone_fini_proc(net);
1146 nf_conntrack_standalone_fini_sysctl(net);
1150 static void nf_conntrack_pernet_exit(struct list_head *net_exit_list)
1154 list_for_each_entry(net, net_exit_list, exit_list)
1155 nf_conntrack_fini_net(net);
1157 nf_conntrack_cleanup_net_list(net_exit_list);
1160 static struct pernet_operations nf_conntrack_net_ops = {
1161 .init = nf_conntrack_pernet_init,
1162 .exit_batch = nf_conntrack_pernet_exit,
1163 .id = &nf_conntrack_net_id,
1164 .size = sizeof(struct nf_conntrack_net),
1167 static int __init nf_conntrack_standalone_init(void)
1169 int ret = nf_conntrack_init_start();
1173 BUILD_BUG_ON(NFCT_INFOMASK <= IP_CT_NUMBER);
1175 #ifdef CONFIG_SYSCTL
1176 nf_ct_netfilter_header =
1177 register_net_sysctl(&init_net, "net", nf_ct_netfilter_table);
1178 if (!nf_ct_netfilter_header) {
1179 pr_err("nf_conntrack: can't register to sysctl.\n");
1184 nf_conntrack_htable_size_user = nf_conntrack_htable_size;
1187 ret = register_pernet_subsys(&nf_conntrack_net_ops);
1191 nf_conntrack_init_end();
1195 #ifdef CONFIG_SYSCTL
1196 unregister_net_sysctl_table(nf_ct_netfilter_header);
1199 nf_conntrack_cleanup_end();
1204 static void __exit nf_conntrack_standalone_fini(void)
1206 nf_conntrack_cleanup_start();
1207 unregister_pernet_subsys(&nf_conntrack_net_ops);
1208 #ifdef CONFIG_SYSCTL
1209 unregister_net_sysctl_table(nf_ct_netfilter_header);
1211 nf_conntrack_cleanup_end();
1214 module_init(nf_conntrack_standalone_init);
1215 module_exit(nf_conntrack_standalone_fini);