• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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>
13 #ifdef CONFIG_SYSCTL
14 #include <linux/sysctl.h>
15 #endif
16 
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>
26 
27 static bool enable_hooks __read_mostly;
28 MODULE_PARM_DESC(enable_hooks, "Always enable conntrack hooks");
29 module_param(enable_hooks, bool, 0000);
30 
31 unsigned int nf_conntrack_net_id __read_mostly;
32 
33 #ifdef CONFIG_NF_CONNTRACK_PROCFS
34 void
print_tuple(struct seq_file * s,const struct nf_conntrack_tuple * tuple,const struct nf_conntrack_l4proto * l4proto)35 print_tuple(struct seq_file *s, const struct nf_conntrack_tuple *tuple,
36             const struct nf_conntrack_l4proto *l4proto)
37 {
38 	switch (tuple->src.l3num) {
39 	case NFPROTO_IPV4:
40 		seq_printf(s, "src=%pI4 dst=%pI4 ",
41 			   &tuple->src.u3.ip, &tuple->dst.u3.ip);
42 		break;
43 	case NFPROTO_IPV6:
44 		seq_printf(s, "src=%pI6 dst=%pI6 ",
45 			   tuple->src.u3.ip6, tuple->dst.u3.ip6);
46 		break;
47 	default:
48 		break;
49 	}
50 
51 	switch (l4proto->l4proto) {
52 	case IPPROTO_ICMP:
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));
57 		break;
58 	case IPPROTO_TCP:
59 		seq_printf(s, "sport=%hu dport=%hu ",
60 			   ntohs(tuple->src.u.tcp.port),
61 			   ntohs(tuple->dst.u.tcp.port));
62 		break;
63 	case IPPROTO_UDPLITE:
64 	case IPPROTO_UDP:
65 		seq_printf(s, "sport=%hu dport=%hu ",
66 			   ntohs(tuple->src.u.udp.port),
67 			   ntohs(tuple->dst.u.udp.port));
68 
69 		break;
70 	case IPPROTO_DCCP:
71 		seq_printf(s, "sport=%hu dport=%hu ",
72 			   ntohs(tuple->src.u.dccp.port),
73 			   ntohs(tuple->dst.u.dccp.port));
74 		break;
75 	case IPPROTO_SCTP:
76 		seq_printf(s, "sport=%hu dport=%hu ",
77 			   ntohs(tuple->src.u.sctp.port),
78 			   ntohs(tuple->dst.u.sctp.port));
79 		break;
80 	case IPPROTO_ICMPV6:
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));
85 		break;
86 	case IPPROTO_GRE:
87 		seq_printf(s, "srckey=0x%x dstkey=0x%x ",
88 			   ntohs(tuple->src.u.gre.key),
89 			   ntohs(tuple->dst.u.gre.key));
90 		break;
91 	default:
92 		break;
93 	}
94 }
95 EXPORT_SYMBOL_GPL(print_tuple);
96 
97 struct ct_iter_state {
98 	struct seq_net_private p;
99 	struct hlist_nulls_head *hash;
100 	unsigned int htable_size;
101 	unsigned int bucket;
102 	u_int64_t time_now;
103 };
104 
ct_get_first(struct seq_file * seq)105 static struct hlist_nulls_node *ct_get_first(struct seq_file *seq)
106 {
107 	struct ct_iter_state *st = seq->private;
108 	struct hlist_nulls_node *n;
109 
110 	for (st->bucket = 0;
111 	     st->bucket < st->htable_size;
112 	     st->bucket++) {
113 		n = rcu_dereference(
114 			hlist_nulls_first_rcu(&st->hash[st->bucket]));
115 		if (!is_a_nulls(n))
116 			return n;
117 	}
118 	return NULL;
119 }
120 
ct_get_next(struct seq_file * seq,struct hlist_nulls_node * head)121 static struct hlist_nulls_node *ct_get_next(struct seq_file *seq,
122 				      struct hlist_nulls_node *head)
123 {
124 	struct ct_iter_state *st = seq->private;
125 
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)
130 				return NULL;
131 		}
132 		head = rcu_dereference(
133 			hlist_nulls_first_rcu(&st->hash[st->bucket]));
134 	}
135 	return head;
136 }
137 
ct_get_idx(struct seq_file * seq,loff_t pos)138 static struct hlist_nulls_node *ct_get_idx(struct seq_file *seq, loff_t pos)
139 {
140 	struct hlist_nulls_node *head = ct_get_first(seq);
141 
142 	if (head)
143 		while (pos && (head = ct_get_next(seq, head)))
144 			pos--;
145 	return pos ? NULL : head;
146 }
147 
ct_seq_start(struct seq_file * seq,loff_t * pos)148 static void *ct_seq_start(struct seq_file *seq, loff_t *pos)
149 	__acquires(RCU)
150 {
151 	struct ct_iter_state *st = seq->private;
152 
153 	st->time_now = ktime_get_real_ns();
154 	rcu_read_lock();
155 
156 	nf_conntrack_get_ht(&st->hash, &st->htable_size);
157 	return ct_get_idx(seq, *pos);
158 }
159 
ct_seq_next(struct seq_file * s,void * v,loff_t * pos)160 static void *ct_seq_next(struct seq_file *s, void *v, loff_t *pos)
161 {
162 	(*pos)++;
163 	return ct_get_next(s, v);
164 }
165 
ct_seq_stop(struct seq_file * s,void * v)166 static void ct_seq_stop(struct seq_file *s, void *v)
167 	__releases(RCU)
168 {
169 	rcu_read_unlock();
170 }
171 
172 #ifdef CONFIG_NF_CONNTRACK_SECMARK
ct_show_secctx(struct seq_file * s,const struct nf_conn * ct)173 static void ct_show_secctx(struct seq_file *s, const struct nf_conn *ct)
174 {
175 	int ret;
176 	u32 len;
177 	char *secctx;
178 
179 	ret = security_secid_to_secctx(ct->secmark, &secctx, &len);
180 	if (ret)
181 		return;
182 
183 	seq_printf(s, "secctx=%s ", secctx);
184 
185 	security_release_secctx(secctx, len);
186 }
187 #else
ct_show_secctx(struct seq_file * s,const struct nf_conn * ct)188 static inline void ct_show_secctx(struct seq_file *s, const struct nf_conn *ct)
189 {
190 }
191 #endif
192 
193 #ifdef CONFIG_NF_CONNTRACK_ZONES
ct_show_zone(struct seq_file * s,const struct nf_conn * ct,int dir)194 static void ct_show_zone(struct seq_file *s, const struct nf_conn *ct,
195 			 int dir)
196 {
197 	const struct nf_conntrack_zone *zone = nf_ct_zone(ct);
198 
199 	if (zone->dir != dir)
200 		return;
201 	switch (zone->dir) {
202 	case NF_CT_DEFAULT_ZONE_DIR:
203 		seq_printf(s, "zone=%u ", zone->id);
204 		break;
205 	case NF_CT_ZONE_DIR_ORIG:
206 		seq_printf(s, "zone-orig=%u ", zone->id);
207 		break;
208 	case NF_CT_ZONE_DIR_REPL:
209 		seq_printf(s, "zone-reply=%u ", zone->id);
210 		break;
211 	default:
212 		break;
213 	}
214 }
215 #else
ct_show_zone(struct seq_file * s,const struct nf_conn * ct,int dir)216 static inline void ct_show_zone(struct seq_file *s, const struct nf_conn *ct,
217 				int dir)
218 {
219 }
220 #endif
221 
222 #ifdef CONFIG_NF_CONNTRACK_TIMESTAMP
ct_show_delta_time(struct seq_file * s,const struct nf_conn * ct)223 static void ct_show_delta_time(struct seq_file *s, const struct nf_conn *ct)
224 {
225 	struct ct_iter_state *st = s->private;
226 	struct nf_conn_tstamp *tstamp;
227 	s64 delta_time;
228 
229 	tstamp = nf_conn_tstamp_find(ct);
230 	if (tstamp) {
231 		delta_time = st->time_now - tstamp->start;
232 		if (delta_time > 0)
233 			delta_time = div_s64(delta_time, NSEC_PER_SEC);
234 		else
235 			delta_time = 0;
236 
237 		seq_printf(s, "delta-time=%llu ",
238 			   (unsigned long long)delta_time);
239 	}
240 	return;
241 }
242 #else
243 static inline void
ct_show_delta_time(struct seq_file * s,const struct nf_conn * ct)244 ct_show_delta_time(struct seq_file *s, const struct nf_conn *ct)
245 {
246 }
247 #endif
248 
l3proto_name(u16 proto)249 static const char* l3proto_name(u16 proto)
250 {
251 	switch (proto) {
252 	case AF_INET: return "ipv4";
253 	case AF_INET6: return "ipv6";
254 	}
255 
256 	return "unknown";
257 }
258 
l4proto_name(u16 proto)259 static const char* l4proto_name(u16 proto)
260 {
261 	switch (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";
270 	}
271 
272 	return "unknown";
273 }
274 
275 static unsigned int
seq_print_acct(struct seq_file * s,const struct nf_conn * ct,int dir)276 seq_print_acct(struct seq_file *s, const struct nf_conn *ct, int dir)
277 {
278 	struct nf_conn_acct *acct;
279 	struct nf_conn_counter *counter;
280 
281 	acct = nf_conn_acct_find(ct);
282 	if (!acct)
283 		return 0;
284 
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));
289 
290 	return 0;
291 }
292 
293 /* return 0 on success, 1 in case of error */
ct_seq_show(struct seq_file * s,void * v)294 static int ct_seq_show(struct seq_file *s, void *v)
295 {
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);
300 	int ret = 0;
301 
302 	WARN_ON(!ct);
303 	if (unlikely(!atomic_inc_not_zero(&ct->ct_general.use)))
304 		return 0;
305 
306 	if (nf_ct_should_gc(ct)) {
307 		nf_ct_kill(ct);
308 		goto release;
309 	}
310 
311 	/* we only want to print DIR_ORIGINAL */
312 	if (NF_CT_DIRECTION(hash))
313 		goto release;
314 
315 	if (!net_eq(nf_ct_net(ct), net))
316 		goto release;
317 
318 	l4proto = nf_ct_l4proto_find(nf_ct_protonum(ct));
319 
320 	ret = -ENOSPC;
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));
324 
325 	if (!test_bit(IPS_OFFLOAD_BIT, &ct->status))
326 		seq_printf(s, "%ld ", nf_ct_expires(ct)  / HZ);
327 
328 	if (l4proto->print_conntrack)
329 		l4proto->print_conntrack(s, ct);
330 
331 	print_tuple(s, &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
332 		    l4proto);
333 
334 	ct_show_zone(s, ct, NF_CT_ZONE_DIR_ORIG);
335 
336 	if (seq_has_overflowed(s))
337 		goto release;
338 
339 	if (seq_print_acct(s, ct, IP_CT_DIR_ORIGINAL))
340 		goto release;
341 
342 	if (!(test_bit(IPS_SEEN_REPLY_BIT, &ct->status)))
343 		seq_puts(s, "[UNREPLIED] ");
344 
345 	print_tuple(s, &ct->tuplehash[IP_CT_DIR_REPLY].tuple, l4proto);
346 
347 	ct_show_zone(s, ct, NF_CT_ZONE_DIR_REPL);
348 
349 	if (seq_print_acct(s, ct, IP_CT_DIR_REPLY))
350 		goto release;
351 
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] ");
358 
359 	if (seq_has_overflowed(s))
360 		goto release;
361 
362 #if defined(CONFIG_NF_CONNTRACK_MARK)
363 	seq_printf(s, "mark=%u ", READ_ONCE(ct->mark));
364 #endif
365 
366 	ct_show_secctx(s, ct);
367 	ct_show_zone(s, ct, NF_CT_DEFAULT_ZONE_DIR);
368 	ct_show_delta_time(s, ct);
369 
370 	seq_printf(s, "use=%u\n", atomic_read(&ct->ct_general.use));
371 
372 	if (seq_has_overflowed(s))
373 		goto release;
374 
375 	ret = 0;
376 release:
377 	nf_ct_put(ct);
378 	return ret;
379 }
380 
381 static const struct seq_operations ct_seq_ops = {
382 	.start = ct_seq_start,
383 	.next  = ct_seq_next,
384 	.stop  = ct_seq_stop,
385 	.show  = ct_seq_show
386 };
387 
ct_cpu_seq_start(struct seq_file * seq,loff_t * pos)388 static void *ct_cpu_seq_start(struct seq_file *seq, loff_t *pos)
389 {
390 	struct net *net = seq_file_net(seq);
391 	int cpu;
392 
393 	if (*pos == 0)
394 		return SEQ_START_TOKEN;
395 
396 	for (cpu = *pos-1; cpu < nr_cpu_ids; ++cpu) {
397 		if (!cpu_possible(cpu))
398 			continue;
399 		*pos = cpu + 1;
400 		return per_cpu_ptr(net->ct.stat, cpu);
401 	}
402 
403 	return NULL;
404 }
405 
ct_cpu_seq_next(struct seq_file * seq,void * v,loff_t * pos)406 static void *ct_cpu_seq_next(struct seq_file *seq, void *v, loff_t *pos)
407 {
408 	struct net *net = seq_file_net(seq);
409 	int cpu;
410 
411 	for (cpu = *pos; cpu < nr_cpu_ids; ++cpu) {
412 		if (!cpu_possible(cpu))
413 			continue;
414 		*pos = cpu + 1;
415 		return per_cpu_ptr(net->ct.stat, cpu);
416 	}
417 	(*pos)++;
418 	return NULL;
419 }
420 
ct_cpu_seq_stop(struct seq_file * seq,void * v)421 static void ct_cpu_seq_stop(struct seq_file *seq, void *v)
422 {
423 }
424 
ct_cpu_seq_show(struct seq_file * seq,void * v)425 static int ct_cpu_seq_show(struct seq_file *seq, void *v)
426 {
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;
430 
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");
433 		return 0;
434 	}
435 
436 	seq_printf(seq, "%08x  %08x %08x %08x %08x %08x %08x %08x "
437 			"%08x %08x %08x %08x %08x  %08x %08x %08x %08x\n",
438 		   nr_conntracks,
439 		   st->clash_resolve,
440 		   st->found,
441 		   0,
442 		   st->invalid,
443 		   0,
444 		   0,
445 		   0,
446 		   st->insert,
447 		   st->insert_failed,
448 		   st->drop,
449 		   st->early_drop,
450 		   st->error,
451 
452 		   st->expect_new,
453 		   st->expect_create,
454 		   st->expect_delete,
455 		   st->search_restart
456 		);
457 	return 0;
458 }
459 
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,
465 };
466 
nf_conntrack_standalone_init_proc(struct net * net)467 static int nf_conntrack_standalone_init_proc(struct net *net)
468 {
469 	struct proc_dir_entry *pde;
470 	kuid_t root_uid;
471 	kgid_t root_gid;
472 
473 	pde = proc_create_net("nf_conntrack", 0440, net->proc_net, &ct_seq_ops,
474 			sizeof(struct ct_iter_state));
475 	if (!pde)
476 		goto out_nf_conntrack;
477 
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);
482 
483 	pde = proc_create_net("nf_conntrack", 0444, net->proc_net_stat,
484 			&ct_cpu_seq_ops, sizeof(struct seq_net_private));
485 	if (!pde)
486 		goto out_stat_nf_conntrack;
487 	return 0;
488 
489 out_stat_nf_conntrack:
490 	remove_proc_entry("nf_conntrack", net->proc_net);
491 out_nf_conntrack:
492 	return -ENOMEM;
493 }
494 
nf_conntrack_standalone_fini_proc(struct net * net)495 static void nf_conntrack_standalone_fini_proc(struct net *net)
496 {
497 	remove_proc_entry("nf_conntrack", net->proc_net_stat);
498 	remove_proc_entry("nf_conntrack", net->proc_net);
499 }
500 #else
nf_conntrack_standalone_init_proc(struct net * net)501 static int nf_conntrack_standalone_init_proc(struct net *net)
502 {
503 	return 0;
504 }
505 
nf_conntrack_standalone_fini_proc(struct net * net)506 static void nf_conntrack_standalone_fini_proc(struct net *net)
507 {
508 }
509 #endif /* CONFIG_NF_CONNTRACK_PROCFS */
510 
511 /* Sysctl support */
512 
513 #ifdef CONFIG_SYSCTL
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;
517 
518 /* size the user *wants to set */
519 static unsigned int nf_conntrack_htable_size_user __read_mostly;
520 
521 static int
nf_conntrack_hash_sysctl(struct ctl_table * table,int write,void * buffer,size_t * lenp,loff_t * ppos)522 nf_conntrack_hash_sysctl(struct ctl_table *table, int write,
523 			 void *buffer, size_t *lenp, loff_t *ppos)
524 {
525 	int ret;
526 
527 	/* module_param hashsize could have changed value */
528 	nf_conntrack_htable_size_user = nf_conntrack_htable_size;
529 
530 	ret = proc_dointvec(table, write, buffer, lenp, ppos);
531 	if (ret < 0 || !write)
532 		return ret;
533 
534 	/* update ret, we might not be able to satisfy request */
535 	ret = nf_conntrack_hash_resize(nf_conntrack_htable_size_user);
536 
537 	/* update it to the actual value used by conntrack */
538 	nf_conntrack_htable_size_user = nf_conntrack_htable_size;
539 	return ret;
540 }
541 
542 static struct ctl_table_header *nf_ct_netfilter_header;
543 
544 enum nf_ct_sysctl_index {
545 	NF_SYSCTL_CT_MAX,
546 	NF_SYSCTL_CT_COUNT,
547 	NF_SYSCTL_CT_BUCKETS,
548 	NF_SYSCTL_CT_CHECKSUM,
549 	NF_SYSCTL_CT_LOG_INVALID,
550 	NF_SYSCTL_CT_EXPECT_MAX,
551 	NF_SYSCTL_CT_ACCT,
552 	NF_SYSCTL_CT_HELPER,
553 #ifdef CONFIG_NF_CONNTRACK_EVENTS
554 	NF_SYSCTL_CT_EVENTS,
555 #endif
556 #ifdef CONFIG_NF_CONNTRACK_TIMESTAMP
557 	NF_SYSCTL_CT_TIMESTAMP,
558 #endif
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,
587 #endif
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,
597 #endif
598 #ifdef CONFIG_NF_CT_PROTO_GRE
599 	NF_SYSCTL_CT_PROTO_TIMEOUT_GRE,
600 	NF_SYSCTL_CT_PROTO_TIMEOUT_GRE_STREAM,
601 #endif
602 
603 	__NF_SYSCTL_CT_LAST_SYSCTL,
604 };
605 
606 #define NF_SYSCTL_CT_LAST_SYSCTL (__NF_SYSCTL_CT_LAST_SYSCTL + 1)
607 
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),
613 		.mode		= 0644,
614 		.proc_handler	= proc_dointvec,
615 	},
616 	[NF_SYSCTL_CT_COUNT] = {
617 		.procname	= "nf_conntrack_count",
618 		.data		= &init_net.ct.count,
619 		.maxlen		= sizeof(int),
620 		.mode		= 0444,
621 		.proc_handler	= proc_dointvec,
622 	},
623 	[NF_SYSCTL_CT_BUCKETS] = {
624 		.procname       = "nf_conntrack_buckets",
625 		.data           = &nf_conntrack_htable_size_user,
626 		.maxlen         = sizeof(unsigned int),
627 		.mode           = 0644,
628 		.proc_handler   = nf_conntrack_hash_sysctl,
629 	},
630 	[NF_SYSCTL_CT_CHECKSUM] = {
631 		.procname	= "nf_conntrack_checksum",
632 		.data		= &init_net.ct.sysctl_checksum,
633 		.maxlen		= sizeof(int),
634 		.mode		= 0644,
635 		.proc_handler	= proc_dointvec_minmax,
636 		.extra1 	= SYSCTL_ZERO,
637 		.extra2 	= SYSCTL_ONE,
638 	},
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),
643 		.mode		= 0644,
644 		.proc_handler	= proc_dointvec_minmax,
645 		.extra1		= &log_invalid_proto_min,
646 		.extra2		= &log_invalid_proto_max,
647 	},
648 	[NF_SYSCTL_CT_EXPECT_MAX] = {
649 		.procname	= "nf_conntrack_expect_max",
650 		.data		= &nf_ct_expect_max,
651 		.maxlen		= sizeof(int),
652 		.mode		= 0644,
653 		.proc_handler	= proc_dointvec,
654 	},
655 	[NF_SYSCTL_CT_ACCT] = {
656 		.procname	= "nf_conntrack_acct",
657 		.data		= &init_net.ct.sysctl_acct,
658 		.maxlen		= sizeof(int),
659 		.mode		= 0644,
660 		.proc_handler	= proc_dointvec_minmax,
661 		.extra1 	= SYSCTL_ZERO,
662 		.extra2 	= SYSCTL_ONE,
663 	},
664 	[NF_SYSCTL_CT_HELPER] = {
665 		.procname	= "nf_conntrack_helper",
666 		.data		= &init_net.ct.sysctl_auto_assign_helper,
667 		.maxlen		= sizeof(int),
668 		.mode		= 0644,
669 		.proc_handler	= proc_dointvec_minmax,
670 		.extra1 	= SYSCTL_ZERO,
671 		.extra2 	= SYSCTL_ONE,
672 	},
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),
678 		.mode		= 0644,
679 		.proc_handler	= proc_dointvec_minmax,
680 		.extra1 	= SYSCTL_ZERO,
681 		.extra2 	= SYSCTL_ONE,
682 	},
683 #endif
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),
689 		.mode		= 0644,
690 		.proc_handler	= proc_dointvec_minmax,
691 		.extra1 	= SYSCTL_ZERO,
692 		.extra2 	= SYSCTL_ONE,
693 	},
694 #endif
695 	[NF_SYSCTL_CT_PROTO_TIMEOUT_GENERIC] = {
696 		.procname	= "nf_conntrack_generic_timeout",
697 		.maxlen		= sizeof(unsigned int),
698 		.mode		= 0644,
699 		.proc_handler	= proc_dointvec_jiffies,
700 	},
701 	[NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_SYN_SENT] = {
702 		.procname	= "nf_conntrack_tcp_timeout_syn_sent",
703 		.maxlen		= sizeof(unsigned int),
704 		.mode		= 0644,
705 		.proc_handler	= proc_dointvec_jiffies,
706 	},
707 	[NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_SYN_RECV] = {
708 		.procname	= "nf_conntrack_tcp_timeout_syn_recv",
709 		.maxlen		= sizeof(unsigned int),
710 		.mode		= 0644,
711 		.proc_handler	= proc_dointvec_jiffies,
712 	},
713 	[NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_ESTABLISHED] = {
714 		.procname	= "nf_conntrack_tcp_timeout_established",
715 		.maxlen		= sizeof(unsigned int),
716 		.mode		= 0644,
717 		.proc_handler	= proc_dointvec_jiffies,
718 	},
719 	[NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_FIN_WAIT] = {
720 		.procname	= "nf_conntrack_tcp_timeout_fin_wait",
721 		.maxlen		= sizeof(unsigned int),
722 		.mode		= 0644,
723 		.proc_handler	= proc_dointvec_jiffies,
724 	},
725 	[NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_CLOSE_WAIT] = {
726 		.procname	= "nf_conntrack_tcp_timeout_close_wait",
727 		.maxlen		= sizeof(unsigned int),
728 		.mode		= 0644,
729 		.proc_handler	= proc_dointvec_jiffies,
730 	},
731 	[NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_LAST_ACK] = {
732 		.procname	= "nf_conntrack_tcp_timeout_last_ack",
733 		.maxlen		= sizeof(unsigned int),
734 		.mode		= 0644,
735 		.proc_handler	= proc_dointvec_jiffies,
736 	},
737 	[NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_TIME_WAIT] = {
738 		.procname	= "nf_conntrack_tcp_timeout_time_wait",
739 		.maxlen		= sizeof(unsigned int),
740 		.mode		= 0644,
741 		.proc_handler	= proc_dointvec_jiffies,
742 	},
743 	[NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_CLOSE] = {
744 		.procname	= "nf_conntrack_tcp_timeout_close",
745 		.maxlen		= sizeof(unsigned int),
746 		.mode		= 0644,
747 		.proc_handler	= proc_dointvec_jiffies,
748 	},
749 	[NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_RETRANS] = {
750 		.procname	= "nf_conntrack_tcp_timeout_max_retrans",
751 		.maxlen		= sizeof(unsigned int),
752 		.mode		= 0644,
753 		.proc_handler	= proc_dointvec_jiffies,
754 	},
755 	[NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_UNACK] = {
756 		.procname	= "nf_conntrack_tcp_timeout_unacknowledged",
757 		.maxlen		= sizeof(unsigned int),
758 		.mode		= 0644,
759 		.proc_handler	= proc_dointvec_jiffies,
760 	},
761 	[NF_SYSCTL_CT_PROTO_TCP_LOOSE] = {
762 		.procname	= "nf_conntrack_tcp_loose",
763 		.maxlen		= sizeof(int),
764 		.mode		= 0644,
765 		.proc_handler	= proc_dointvec_minmax,
766 		.extra1 	= SYSCTL_ZERO,
767 		.extra2 	= SYSCTL_ONE,
768 	},
769 	[NF_SYSCTL_CT_PROTO_TCP_LIBERAL] = {
770 		.procname       = "nf_conntrack_tcp_be_liberal",
771 		.maxlen         = sizeof(int),
772 		.mode           = 0644,
773 		.proc_handler	= proc_dointvec_minmax,
774 		.extra1 	= SYSCTL_ZERO,
775 		.extra2 	= SYSCTL_ONE,
776 	},
777 	[NF_SYSCTL_CT_PROTO_TCP_MAX_RETRANS] = {
778 		.procname	= "nf_conntrack_tcp_max_retrans",
779 		.maxlen		= sizeof(unsigned int),
780 		.mode		= 0644,
781 		.proc_handler	= proc_dointvec,
782 	},
783 	[NF_SYSCTL_CT_PROTO_TIMEOUT_UDP] = {
784 		.procname	= "nf_conntrack_udp_timeout",
785 		.maxlen		= sizeof(unsigned int),
786 		.mode		= 0644,
787 		.proc_handler	= proc_dointvec_jiffies,
788 	},
789 	[NF_SYSCTL_CT_PROTO_TIMEOUT_UDP_STREAM] = {
790 		.procname	= "nf_conntrack_udp_timeout_stream",
791 		.maxlen		= sizeof(unsigned int),
792 		.mode		= 0644,
793 		.proc_handler	= proc_dointvec_jiffies,
794 	},
795 	[NF_SYSCTL_CT_PROTO_TIMEOUT_ICMP] = {
796 		.procname	= "nf_conntrack_icmp_timeout",
797 		.maxlen		= sizeof(unsigned int),
798 		.mode		= 0644,
799 		.proc_handler	= proc_dointvec_jiffies,
800 	},
801 	[NF_SYSCTL_CT_PROTO_TIMEOUT_ICMPV6] = {
802 		.procname	= "nf_conntrack_icmpv6_timeout",
803 		.maxlen		= sizeof(unsigned int),
804 		.mode		= 0644,
805 		.proc_handler	= proc_dointvec_jiffies,
806 	},
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),
811 		.mode		= 0644,
812 		.proc_handler	= proc_dointvec_jiffies,
813 	},
814 	[NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_COOKIE_WAIT] = {
815 		.procname	= "nf_conntrack_sctp_timeout_cookie_wait",
816 		.maxlen		= sizeof(unsigned int),
817 		.mode		= 0644,
818 		.proc_handler	= proc_dointvec_jiffies,
819 	},
820 	[NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_COOKIE_ECHOED] = {
821 		.procname	= "nf_conntrack_sctp_timeout_cookie_echoed",
822 		.maxlen		= sizeof(unsigned int),
823 		.mode		= 0644,
824 		.proc_handler	= proc_dointvec_jiffies,
825 	},
826 	[NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_ESTABLISHED] = {
827 		.procname	= "nf_conntrack_sctp_timeout_established",
828 		.maxlen		= sizeof(unsigned int),
829 		.mode		= 0644,
830 		.proc_handler	= proc_dointvec_jiffies,
831 	},
832 	[NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_SHUTDOWN_SENT] = {
833 		.procname	= "nf_conntrack_sctp_timeout_shutdown_sent",
834 		.maxlen		= sizeof(unsigned int),
835 		.mode		= 0644,
836 		.proc_handler	= proc_dointvec_jiffies,
837 	},
838 	[NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_SHUTDOWN_RECD] = {
839 		.procname	= "nf_conntrack_sctp_timeout_shutdown_recd",
840 		.maxlen		= sizeof(unsigned int),
841 		.mode		= 0644,
842 		.proc_handler	= proc_dointvec_jiffies,
843 	},
844 	[NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_SHUTDOWN_ACK_SENT] = {
845 		.procname	= "nf_conntrack_sctp_timeout_shutdown_ack_sent",
846 		.maxlen		= sizeof(unsigned int),
847 		.mode		= 0644,
848 		.proc_handler	= proc_dointvec_jiffies,
849 	},
850 	[NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_HEARTBEAT_SENT] = {
851 		.procname	= "nf_conntrack_sctp_timeout_heartbeat_sent",
852 		.maxlen		= sizeof(unsigned int),
853 		.mode		= 0644,
854 		.proc_handler	= proc_dointvec_jiffies,
855 	},
856 	[NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_HEARTBEAT_ACKED] = {
857 		.procname       = "nf_conntrack_sctp_timeout_heartbeat_acked",
858 		.maxlen         = sizeof(unsigned int),
859 		.mode           = 0644,
860 		.proc_handler   = proc_dointvec_jiffies,
861 	},
862 #endif
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),
867 		.mode		= 0644,
868 		.proc_handler	= proc_dointvec_jiffies,
869 	},
870 	[NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_RESPOND] = {
871 		.procname	= "nf_conntrack_dccp_timeout_respond",
872 		.maxlen		= sizeof(unsigned int),
873 		.mode		= 0644,
874 		.proc_handler	= proc_dointvec_jiffies,
875 	},
876 	[NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_PARTOPEN] = {
877 		.procname	= "nf_conntrack_dccp_timeout_partopen",
878 		.maxlen		= sizeof(unsigned int),
879 		.mode		= 0644,
880 		.proc_handler	= proc_dointvec_jiffies,
881 	},
882 	[NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_OPEN] = {
883 		.procname	= "nf_conntrack_dccp_timeout_open",
884 		.maxlen		= sizeof(unsigned int),
885 		.mode		= 0644,
886 		.proc_handler	= proc_dointvec_jiffies,
887 	},
888 	[NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_CLOSEREQ] = {
889 		.procname	= "nf_conntrack_dccp_timeout_closereq",
890 		.maxlen		= sizeof(unsigned int),
891 		.mode		= 0644,
892 		.proc_handler	= proc_dointvec_jiffies,
893 	},
894 	[NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_CLOSING] = {
895 		.procname	= "nf_conntrack_dccp_timeout_closing",
896 		.maxlen		= sizeof(unsigned int),
897 		.mode		= 0644,
898 		.proc_handler	= proc_dointvec_jiffies,
899 	},
900 	[NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_TIMEWAIT] = {
901 		.procname	= "nf_conntrack_dccp_timeout_timewait",
902 		.maxlen		= sizeof(unsigned int),
903 		.mode		= 0644,
904 		.proc_handler	= proc_dointvec_jiffies,
905 	},
906 	[NF_SYSCTL_CT_PROTO_DCCP_LOOSE] = {
907 		.procname	= "nf_conntrack_dccp_loose",
908 		.maxlen		= sizeof(int),
909 		.mode		= 0644,
910 		.proc_handler	= proc_dointvec_minmax,
911 		.extra1 	= SYSCTL_ZERO,
912 		.extra2 	= SYSCTL_ONE,
913 	},
914 #endif
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),
919 		.mode           = 0644,
920 		.proc_handler   = proc_dointvec_jiffies,
921 	},
922 	[NF_SYSCTL_CT_PROTO_TIMEOUT_GRE_STREAM] = {
923 		.procname       = "nf_conntrack_gre_timeout_stream",
924 		.maxlen         = sizeof(unsigned int),
925 		.mode           = 0644,
926 		.proc_handler   = proc_dointvec_jiffies,
927 	},
928 #endif
929 	{}
930 };
931 
932 static struct ctl_table nf_ct_netfilter_table[] = {
933 	{
934 		.procname	= "nf_conntrack_max",
935 		.data		= &nf_conntrack_max,
936 		.maxlen		= sizeof(int),
937 		.mode		= 0644,
938 		.proc_handler	= proc_dointvec,
939 	},
940 	{ }
941 };
942 
nf_conntrack_standalone_init_tcp_sysctl(struct net * net,struct ctl_table * table)943 static void nf_conntrack_standalone_init_tcp_sysctl(struct net *net,
944 						    struct ctl_table *table)
945 {
946 	struct nf_tcp_net *tn = nf_tcp_pernet(net);
947 
948 #define XASSIGN(XNAME, tn) \
949 	table[NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_ ## XNAME].data = \
950 			&(tn)->timeouts[TCP_CONNTRACK_ ## XNAME]
951 
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);
959 	XASSIGN(CLOSE, tn);
960 	XASSIGN(RETRANS, tn);
961 	XASSIGN(UNACK, tn);
962 #undef XASSIGN
963 #define XASSIGN(XNAME, rval) \
964 	table[NF_SYSCTL_CT_PROTO_TCP_ ## XNAME].data = (rval)
965 
966 	XASSIGN(LOOSE, &tn->tcp_loose);
967 	XASSIGN(LIBERAL, &tn->tcp_be_liberal);
968 	XASSIGN(MAX_RETRANS, &tn->tcp_max_retrans);
969 #undef XASSIGN
970 }
971 
nf_conntrack_standalone_init_sctp_sysctl(struct net * net,struct ctl_table * table)972 static void nf_conntrack_standalone_init_sctp_sysctl(struct net *net,
973 						     struct ctl_table *table)
974 {
975 #ifdef CONFIG_NF_CT_PROTO_SCTP
976 	struct nf_sctp_net *sn = nf_sctp_pernet(net);
977 
978 #define XASSIGN(XNAME, sn) \
979 	table[NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_ ## XNAME].data = \
980 			&(sn)->timeouts[SCTP_CONNTRACK_ ## XNAME]
981 
982 	XASSIGN(CLOSED, sn);
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);
991 #undef XASSIGN
992 #endif
993 }
994 
nf_conntrack_standalone_init_dccp_sysctl(struct net * net,struct ctl_table * table)995 static void nf_conntrack_standalone_init_dccp_sysctl(struct net *net,
996 						     struct ctl_table *table)
997 {
998 #ifdef CONFIG_NF_CT_PROTO_DCCP
999 	struct nf_dccp_net *dn = nf_dccp_pernet(net);
1000 
1001 #define XASSIGN(XNAME, dn) \
1002 	table[NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_ ## XNAME].data = \
1003 			&(dn)->dccp_timeout[CT_DCCP_ ## XNAME]
1004 
1005 	XASSIGN(REQUEST, dn);
1006 	XASSIGN(RESPOND, dn);
1007 	XASSIGN(PARTOPEN, dn);
1008 	XASSIGN(OPEN, dn);
1009 	XASSIGN(CLOSEREQ, dn);
1010 	XASSIGN(CLOSING, dn);
1011 	XASSIGN(TIMEWAIT, dn);
1012 #undef XASSIGN
1013 
1014 	table[NF_SYSCTL_CT_PROTO_DCCP_LOOSE].data = &dn->dccp_loose;
1015 #endif
1016 }
1017 
nf_conntrack_standalone_init_gre_sysctl(struct net * net,struct ctl_table * table)1018 static void nf_conntrack_standalone_init_gre_sysctl(struct net *net,
1019 						    struct ctl_table *table)
1020 {
1021 #ifdef CONFIG_NF_CT_PROTO_GRE
1022 	struct nf_gre_net *gn = nf_gre_pernet(net);
1023 
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];
1026 #endif
1027 }
1028 
nf_conntrack_standalone_init_sysctl(struct net * net)1029 static int nf_conntrack_standalone_init_sysctl(struct net *net)
1030 {
1031 	struct nf_udp_net *un = nf_udp_pernet(net);
1032 	struct ctl_table *table;
1033 
1034 	BUILD_BUG_ON(ARRAY_SIZE(nf_ct_sysctl_table) != NF_SYSCTL_CT_LAST_SYSCTL);
1035 
1036 	table = kmemdup(nf_ct_sysctl_table, sizeof(nf_ct_sysctl_table),
1037 			GFP_KERNEL);
1038 	if (!table)
1039 		return -ENOMEM;
1040 
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;
1048 #endif
1049 #ifdef CONFIG_NF_CONNTRACK_TIMESTAMP
1050 	table[NF_SYSCTL_CT_TIMESTAMP].data = &net->ct.sysctl_tstamp;
1051 #endif
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];
1057 
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);
1062 
1063 	/* Don't allow non-init_net ns to alter global sysctls */
1064 	if (!net_eq(&init_net, net)) {
1065 		table[NF_SYSCTL_CT_MAX].mode = 0444;
1066 		table[NF_SYSCTL_CT_EXPECT_MAX].mode = 0444;
1067 		table[NF_SYSCTL_CT_BUCKETS].mode = 0444;
1068 	}
1069 
1070 	net->ct.sysctl_header = register_net_sysctl(net, "net/netfilter", table);
1071 	if (!net->ct.sysctl_header)
1072 		goto out_unregister_netfilter;
1073 
1074 	return 0;
1075 
1076 out_unregister_netfilter:
1077 	kfree(table);
1078 	return -ENOMEM;
1079 }
1080 
nf_conntrack_standalone_fini_sysctl(struct net * net)1081 static void nf_conntrack_standalone_fini_sysctl(struct net *net)
1082 {
1083 	struct ctl_table *table;
1084 
1085 	table = net->ct.sysctl_header->ctl_table_arg;
1086 	unregister_net_sysctl_table(net->ct.sysctl_header);
1087 	kfree(table);
1088 }
1089 #else
nf_conntrack_standalone_init_sysctl(struct net * net)1090 static int nf_conntrack_standalone_init_sysctl(struct net *net)
1091 {
1092 	return 0;
1093 }
1094 
nf_conntrack_standalone_fini_sysctl(struct net * net)1095 static void nf_conntrack_standalone_fini_sysctl(struct net *net)
1096 {
1097 }
1098 #endif /* CONFIG_SYSCTL */
1099 
nf_conntrack_fini_net(struct net * net)1100 static void nf_conntrack_fini_net(struct net *net)
1101 {
1102 	if (enable_hooks)
1103 		nf_ct_netns_put(net, NFPROTO_INET);
1104 
1105 	nf_conntrack_standalone_fini_proc(net);
1106 	nf_conntrack_standalone_fini_sysctl(net);
1107 }
1108 
nf_conntrack_pernet_init(struct net * net)1109 static int nf_conntrack_pernet_init(struct net *net)
1110 {
1111 	int ret;
1112 
1113 	net->ct.sysctl_checksum = 1;
1114 
1115 	ret = nf_conntrack_standalone_init_sysctl(net);
1116 	if (ret < 0)
1117 		return ret;
1118 
1119 	ret = nf_conntrack_standalone_init_proc(net);
1120 	if (ret < 0)
1121 		goto out_proc;
1122 
1123 	ret = nf_conntrack_init_net(net);
1124 	if (ret < 0)
1125 		goto out_init_net;
1126 
1127 	if (enable_hooks) {
1128 		ret = nf_ct_netns_get(net, NFPROTO_INET);
1129 		if (ret < 0)
1130 			goto out_hooks;
1131 	}
1132 
1133 	return 0;
1134 
1135 out_hooks:
1136 	nf_conntrack_cleanup_net(net);
1137 out_init_net:
1138 	nf_conntrack_standalone_fini_proc(net);
1139 out_proc:
1140 	nf_conntrack_standalone_fini_sysctl(net);
1141 	return ret;
1142 }
1143 
nf_conntrack_pernet_exit(struct list_head * net_exit_list)1144 static void nf_conntrack_pernet_exit(struct list_head *net_exit_list)
1145 {
1146 	struct net *net;
1147 
1148 	list_for_each_entry(net, net_exit_list, exit_list)
1149 		nf_conntrack_fini_net(net);
1150 
1151 	nf_conntrack_cleanup_net_list(net_exit_list);
1152 }
1153 
1154 static struct pernet_operations nf_conntrack_net_ops = {
1155 	.init		= nf_conntrack_pernet_init,
1156 	.exit_batch	= nf_conntrack_pernet_exit,
1157 	.id		= &nf_conntrack_net_id,
1158 	.size = sizeof(struct nf_conntrack_net),
1159 };
1160 
nf_conntrack_standalone_init(void)1161 static int __init nf_conntrack_standalone_init(void)
1162 {
1163 	int ret = nf_conntrack_init_start();
1164 	if (ret < 0)
1165 		goto out_start;
1166 
1167 	BUILD_BUG_ON(NFCT_INFOMASK <= IP_CT_NUMBER);
1168 
1169 #ifdef CONFIG_SYSCTL
1170 	nf_ct_netfilter_header =
1171 		register_net_sysctl(&init_net, "net", nf_ct_netfilter_table);
1172 	if (!nf_ct_netfilter_header) {
1173 		pr_err("nf_conntrack: can't register to sysctl.\n");
1174 		ret = -ENOMEM;
1175 		goto out_sysctl;
1176 	}
1177 
1178 	nf_conntrack_htable_size_user = nf_conntrack_htable_size;
1179 #endif
1180 
1181 	ret = register_pernet_subsys(&nf_conntrack_net_ops);
1182 	if (ret < 0)
1183 		goto out_pernet;
1184 
1185 	nf_conntrack_init_end();
1186 	return 0;
1187 
1188 out_pernet:
1189 #ifdef CONFIG_SYSCTL
1190 	unregister_net_sysctl_table(nf_ct_netfilter_header);
1191 out_sysctl:
1192 #endif
1193 	nf_conntrack_cleanup_end();
1194 out_start:
1195 	return ret;
1196 }
1197 
nf_conntrack_standalone_fini(void)1198 static void __exit nf_conntrack_standalone_fini(void)
1199 {
1200 	nf_conntrack_cleanup_start();
1201 	unregister_pernet_subsys(&nf_conntrack_net_ops);
1202 #ifdef CONFIG_SYSCTL
1203 	unregister_net_sysctl_table(nf_ct_netfilter_header);
1204 #endif
1205 	nf_conntrack_cleanup_end();
1206 }
1207 
1208 module_init(nf_conntrack_standalone_init);
1209 module_exit(nf_conntrack_standalone_fini);
1210