• 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 #endif
587 #ifdef CONFIG_NF_CT_PROTO_DCCP
588 	NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_REQUEST,
589 	NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_RESPOND,
590 	NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_PARTOPEN,
591 	NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_OPEN,
592 	NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_CLOSEREQ,
593 	NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_CLOSING,
594 	NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_TIMEWAIT,
595 	NF_SYSCTL_CT_PROTO_DCCP_LOOSE,
596 #endif
597 #ifdef CONFIG_NF_CT_PROTO_GRE
598 	NF_SYSCTL_CT_PROTO_TIMEOUT_GRE,
599 	NF_SYSCTL_CT_PROTO_TIMEOUT_GRE_STREAM,
600 #endif
601 
602 	__NF_SYSCTL_CT_LAST_SYSCTL,
603 };
604 
605 #define NF_SYSCTL_CT_LAST_SYSCTL (__NF_SYSCTL_CT_LAST_SYSCTL + 1)
606 
607 static struct ctl_table nf_ct_sysctl_table[] = {
608 	[NF_SYSCTL_CT_MAX] = {
609 		.procname	= "nf_conntrack_max",
610 		.data		= &nf_conntrack_max,
611 		.maxlen		= sizeof(int),
612 		.mode		= 0644,
613 		.proc_handler	= proc_dointvec,
614 	},
615 	[NF_SYSCTL_CT_COUNT] = {
616 		.procname	= "nf_conntrack_count",
617 		.data		= &init_net.ct.count,
618 		.maxlen		= sizeof(int),
619 		.mode		= 0444,
620 		.proc_handler	= proc_dointvec,
621 	},
622 	[NF_SYSCTL_CT_BUCKETS] = {
623 		.procname       = "nf_conntrack_buckets",
624 		.data           = &nf_conntrack_htable_size_user,
625 		.maxlen         = sizeof(unsigned int),
626 		.mode           = 0644,
627 		.proc_handler   = nf_conntrack_hash_sysctl,
628 	},
629 	[NF_SYSCTL_CT_CHECKSUM] = {
630 		.procname	= "nf_conntrack_checksum",
631 		.data		= &init_net.ct.sysctl_checksum,
632 		.maxlen		= sizeof(int),
633 		.mode		= 0644,
634 		.proc_handler	= proc_dointvec_minmax,
635 		.extra1 	= SYSCTL_ZERO,
636 		.extra2 	= SYSCTL_ONE,
637 	},
638 	[NF_SYSCTL_CT_LOG_INVALID] = {
639 		.procname	= "nf_conntrack_log_invalid",
640 		.data		= &init_net.ct.sysctl_log_invalid,
641 		.maxlen		= sizeof(unsigned int),
642 		.mode		= 0644,
643 		.proc_handler	= proc_dointvec_minmax,
644 		.extra1		= &log_invalid_proto_min,
645 		.extra2		= &log_invalid_proto_max,
646 	},
647 	[NF_SYSCTL_CT_EXPECT_MAX] = {
648 		.procname	= "nf_conntrack_expect_max",
649 		.data		= &nf_ct_expect_max,
650 		.maxlen		= sizeof(int),
651 		.mode		= 0644,
652 		.proc_handler	= proc_dointvec,
653 	},
654 	[NF_SYSCTL_CT_ACCT] = {
655 		.procname	= "nf_conntrack_acct",
656 		.data		= &init_net.ct.sysctl_acct,
657 		.maxlen		= sizeof(int),
658 		.mode		= 0644,
659 		.proc_handler	= proc_dointvec_minmax,
660 		.extra1 	= SYSCTL_ZERO,
661 		.extra2 	= SYSCTL_ONE,
662 	},
663 	[NF_SYSCTL_CT_HELPER] = {
664 		.procname	= "nf_conntrack_helper",
665 		.data		= &init_net.ct.sysctl_auto_assign_helper,
666 		.maxlen		= sizeof(int),
667 		.mode		= 0644,
668 		.proc_handler	= proc_dointvec_minmax,
669 		.extra1 	= SYSCTL_ZERO,
670 		.extra2 	= SYSCTL_ONE,
671 	},
672 #ifdef CONFIG_NF_CONNTRACK_EVENTS
673 	[NF_SYSCTL_CT_EVENTS] = {
674 		.procname	= "nf_conntrack_events",
675 		.data		= &init_net.ct.sysctl_events,
676 		.maxlen		= sizeof(int),
677 		.mode		= 0644,
678 		.proc_handler	= proc_dointvec_minmax,
679 		.extra1 	= SYSCTL_ZERO,
680 		.extra2 	= SYSCTL_ONE,
681 	},
682 #endif
683 #ifdef CONFIG_NF_CONNTRACK_TIMESTAMP
684 	[NF_SYSCTL_CT_TIMESTAMP] = {
685 		.procname	= "nf_conntrack_timestamp",
686 		.data		= &init_net.ct.sysctl_tstamp,
687 		.maxlen		= sizeof(int),
688 		.mode		= 0644,
689 		.proc_handler	= proc_dointvec_minmax,
690 		.extra1 	= SYSCTL_ZERO,
691 		.extra2 	= SYSCTL_ONE,
692 	},
693 #endif
694 	[NF_SYSCTL_CT_PROTO_TIMEOUT_GENERIC] = {
695 		.procname	= "nf_conntrack_generic_timeout",
696 		.maxlen		= sizeof(unsigned int),
697 		.mode		= 0644,
698 		.proc_handler	= proc_dointvec_jiffies,
699 	},
700 	[NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_SYN_SENT] = {
701 		.procname	= "nf_conntrack_tcp_timeout_syn_sent",
702 		.maxlen		= sizeof(unsigned int),
703 		.mode		= 0644,
704 		.proc_handler	= proc_dointvec_jiffies,
705 	},
706 	[NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_SYN_RECV] = {
707 		.procname	= "nf_conntrack_tcp_timeout_syn_recv",
708 		.maxlen		= sizeof(unsigned int),
709 		.mode		= 0644,
710 		.proc_handler	= proc_dointvec_jiffies,
711 	},
712 	[NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_ESTABLISHED] = {
713 		.procname	= "nf_conntrack_tcp_timeout_established",
714 		.maxlen		= sizeof(unsigned int),
715 		.mode		= 0644,
716 		.proc_handler	= proc_dointvec_jiffies,
717 	},
718 	[NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_FIN_WAIT] = {
719 		.procname	= "nf_conntrack_tcp_timeout_fin_wait",
720 		.maxlen		= sizeof(unsigned int),
721 		.mode		= 0644,
722 		.proc_handler	= proc_dointvec_jiffies,
723 	},
724 	[NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_CLOSE_WAIT] = {
725 		.procname	= "nf_conntrack_tcp_timeout_close_wait",
726 		.maxlen		= sizeof(unsigned int),
727 		.mode		= 0644,
728 		.proc_handler	= proc_dointvec_jiffies,
729 	},
730 	[NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_LAST_ACK] = {
731 		.procname	= "nf_conntrack_tcp_timeout_last_ack",
732 		.maxlen		= sizeof(unsigned int),
733 		.mode		= 0644,
734 		.proc_handler	= proc_dointvec_jiffies,
735 	},
736 	[NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_TIME_WAIT] = {
737 		.procname	= "nf_conntrack_tcp_timeout_time_wait",
738 		.maxlen		= sizeof(unsigned int),
739 		.mode		= 0644,
740 		.proc_handler	= proc_dointvec_jiffies,
741 	},
742 	[NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_CLOSE] = {
743 		.procname	= "nf_conntrack_tcp_timeout_close",
744 		.maxlen		= sizeof(unsigned int),
745 		.mode		= 0644,
746 		.proc_handler	= proc_dointvec_jiffies,
747 	},
748 	[NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_RETRANS] = {
749 		.procname	= "nf_conntrack_tcp_timeout_max_retrans",
750 		.maxlen		= sizeof(unsigned int),
751 		.mode		= 0644,
752 		.proc_handler	= proc_dointvec_jiffies,
753 	},
754 	[NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_UNACK] = {
755 		.procname	= "nf_conntrack_tcp_timeout_unacknowledged",
756 		.maxlen		= sizeof(unsigned int),
757 		.mode		= 0644,
758 		.proc_handler	= proc_dointvec_jiffies,
759 	},
760 	[NF_SYSCTL_CT_PROTO_TCP_LOOSE] = {
761 		.procname	= "nf_conntrack_tcp_loose",
762 		.maxlen		= sizeof(int),
763 		.mode		= 0644,
764 		.proc_handler	= proc_dointvec_minmax,
765 		.extra1 	= SYSCTL_ZERO,
766 		.extra2 	= SYSCTL_ONE,
767 	},
768 	[NF_SYSCTL_CT_PROTO_TCP_LIBERAL] = {
769 		.procname       = "nf_conntrack_tcp_be_liberal",
770 		.maxlen         = sizeof(int),
771 		.mode           = 0644,
772 		.proc_handler	= proc_dointvec_minmax,
773 		.extra1 	= SYSCTL_ZERO,
774 		.extra2 	= SYSCTL_ONE,
775 	},
776 	[NF_SYSCTL_CT_PROTO_TCP_MAX_RETRANS] = {
777 		.procname	= "nf_conntrack_tcp_max_retrans",
778 		.maxlen		= sizeof(unsigned int),
779 		.mode		= 0644,
780 		.proc_handler	= proc_dointvec,
781 	},
782 	[NF_SYSCTL_CT_PROTO_TIMEOUT_UDP] = {
783 		.procname	= "nf_conntrack_udp_timeout",
784 		.maxlen		= sizeof(unsigned int),
785 		.mode		= 0644,
786 		.proc_handler	= proc_dointvec_jiffies,
787 	},
788 	[NF_SYSCTL_CT_PROTO_TIMEOUT_UDP_STREAM] = {
789 		.procname	= "nf_conntrack_udp_timeout_stream",
790 		.maxlen		= sizeof(unsigned int),
791 		.mode		= 0644,
792 		.proc_handler	= proc_dointvec_jiffies,
793 	},
794 	[NF_SYSCTL_CT_PROTO_TIMEOUT_ICMP] = {
795 		.procname	= "nf_conntrack_icmp_timeout",
796 		.maxlen		= sizeof(unsigned int),
797 		.mode		= 0644,
798 		.proc_handler	= proc_dointvec_jiffies,
799 	},
800 	[NF_SYSCTL_CT_PROTO_TIMEOUT_ICMPV6] = {
801 		.procname	= "nf_conntrack_icmpv6_timeout",
802 		.maxlen		= sizeof(unsigned int),
803 		.mode		= 0644,
804 		.proc_handler	= proc_dointvec_jiffies,
805 	},
806 #ifdef CONFIG_NF_CT_PROTO_SCTP
807 	[NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_CLOSED] = {
808 		.procname	= "nf_conntrack_sctp_timeout_closed",
809 		.maxlen		= sizeof(unsigned int),
810 		.mode		= 0644,
811 		.proc_handler	= proc_dointvec_jiffies,
812 	},
813 	[NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_COOKIE_WAIT] = {
814 		.procname	= "nf_conntrack_sctp_timeout_cookie_wait",
815 		.maxlen		= sizeof(unsigned int),
816 		.mode		= 0644,
817 		.proc_handler	= proc_dointvec_jiffies,
818 	},
819 	[NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_COOKIE_ECHOED] = {
820 		.procname	= "nf_conntrack_sctp_timeout_cookie_echoed",
821 		.maxlen		= sizeof(unsigned int),
822 		.mode		= 0644,
823 		.proc_handler	= proc_dointvec_jiffies,
824 	},
825 	[NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_ESTABLISHED] = {
826 		.procname	= "nf_conntrack_sctp_timeout_established",
827 		.maxlen		= sizeof(unsigned int),
828 		.mode		= 0644,
829 		.proc_handler	= proc_dointvec_jiffies,
830 	},
831 	[NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_SHUTDOWN_SENT] = {
832 		.procname	= "nf_conntrack_sctp_timeout_shutdown_sent",
833 		.maxlen		= sizeof(unsigned int),
834 		.mode		= 0644,
835 		.proc_handler	= proc_dointvec_jiffies,
836 	},
837 	[NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_SHUTDOWN_RECD] = {
838 		.procname	= "nf_conntrack_sctp_timeout_shutdown_recd",
839 		.maxlen		= sizeof(unsigned int),
840 		.mode		= 0644,
841 		.proc_handler	= proc_dointvec_jiffies,
842 	},
843 	[NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_SHUTDOWN_ACK_SENT] = {
844 		.procname	= "nf_conntrack_sctp_timeout_shutdown_ack_sent",
845 		.maxlen		= sizeof(unsigned int),
846 		.mode		= 0644,
847 		.proc_handler	= proc_dointvec_jiffies,
848 	},
849 	[NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_HEARTBEAT_SENT] = {
850 		.procname	= "nf_conntrack_sctp_timeout_heartbeat_sent",
851 		.maxlen		= sizeof(unsigned int),
852 		.mode		= 0644,
853 		.proc_handler	= proc_dointvec_jiffies,
854 	},
855 #endif
856 #ifdef CONFIG_NF_CT_PROTO_DCCP
857 	[NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_REQUEST] = {
858 		.procname	= "nf_conntrack_dccp_timeout_request",
859 		.maxlen		= sizeof(unsigned int),
860 		.mode		= 0644,
861 		.proc_handler	= proc_dointvec_jiffies,
862 	},
863 	[NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_RESPOND] = {
864 		.procname	= "nf_conntrack_dccp_timeout_respond",
865 		.maxlen		= sizeof(unsigned int),
866 		.mode		= 0644,
867 		.proc_handler	= proc_dointvec_jiffies,
868 	},
869 	[NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_PARTOPEN] = {
870 		.procname	= "nf_conntrack_dccp_timeout_partopen",
871 		.maxlen		= sizeof(unsigned int),
872 		.mode		= 0644,
873 		.proc_handler	= proc_dointvec_jiffies,
874 	},
875 	[NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_OPEN] = {
876 		.procname	= "nf_conntrack_dccp_timeout_open",
877 		.maxlen		= sizeof(unsigned int),
878 		.mode		= 0644,
879 		.proc_handler	= proc_dointvec_jiffies,
880 	},
881 	[NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_CLOSEREQ] = {
882 		.procname	= "nf_conntrack_dccp_timeout_closereq",
883 		.maxlen		= sizeof(unsigned int),
884 		.mode		= 0644,
885 		.proc_handler	= proc_dointvec_jiffies,
886 	},
887 	[NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_CLOSING] = {
888 		.procname	= "nf_conntrack_dccp_timeout_closing",
889 		.maxlen		= sizeof(unsigned int),
890 		.mode		= 0644,
891 		.proc_handler	= proc_dointvec_jiffies,
892 	},
893 	[NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_TIMEWAIT] = {
894 		.procname	= "nf_conntrack_dccp_timeout_timewait",
895 		.maxlen		= sizeof(unsigned int),
896 		.mode		= 0644,
897 		.proc_handler	= proc_dointvec_jiffies,
898 	},
899 	[NF_SYSCTL_CT_PROTO_DCCP_LOOSE] = {
900 		.procname	= "nf_conntrack_dccp_loose",
901 		.maxlen		= sizeof(int),
902 		.mode		= 0644,
903 		.proc_handler	= proc_dointvec_minmax,
904 		.extra1 	= SYSCTL_ZERO,
905 		.extra2 	= SYSCTL_ONE,
906 	},
907 #endif
908 #ifdef CONFIG_NF_CT_PROTO_GRE
909 	[NF_SYSCTL_CT_PROTO_TIMEOUT_GRE] = {
910 		.procname       = "nf_conntrack_gre_timeout",
911 		.maxlen         = sizeof(unsigned int),
912 		.mode           = 0644,
913 		.proc_handler   = proc_dointvec_jiffies,
914 	},
915 	[NF_SYSCTL_CT_PROTO_TIMEOUT_GRE_STREAM] = {
916 		.procname       = "nf_conntrack_gre_timeout_stream",
917 		.maxlen         = sizeof(unsigned int),
918 		.mode           = 0644,
919 		.proc_handler   = proc_dointvec_jiffies,
920 	},
921 #endif
922 	{}
923 };
924 
925 static struct ctl_table nf_ct_netfilter_table[] = {
926 	{
927 		.procname	= "nf_conntrack_max",
928 		.data		= &nf_conntrack_max,
929 		.maxlen		= sizeof(int),
930 		.mode		= 0644,
931 		.proc_handler	= proc_dointvec,
932 	},
933 	{ }
934 };
935 
nf_conntrack_standalone_init_tcp_sysctl(struct net * net,struct ctl_table * table)936 static void nf_conntrack_standalone_init_tcp_sysctl(struct net *net,
937 						    struct ctl_table *table)
938 {
939 	struct nf_tcp_net *tn = nf_tcp_pernet(net);
940 
941 #define XASSIGN(XNAME, tn) \
942 	table[NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_ ## XNAME].data = \
943 			&(tn)->timeouts[TCP_CONNTRACK_ ## XNAME]
944 
945 	XASSIGN(SYN_SENT, tn);
946 	XASSIGN(SYN_RECV, tn);
947 	XASSIGN(ESTABLISHED, tn);
948 	XASSIGN(FIN_WAIT, tn);
949 	XASSIGN(CLOSE_WAIT, tn);
950 	XASSIGN(LAST_ACK, tn);
951 	XASSIGN(TIME_WAIT, tn);
952 	XASSIGN(CLOSE, tn);
953 	XASSIGN(RETRANS, tn);
954 	XASSIGN(UNACK, tn);
955 #undef XASSIGN
956 #define XASSIGN(XNAME, rval) \
957 	table[NF_SYSCTL_CT_PROTO_TCP_ ## XNAME].data = (rval)
958 
959 	XASSIGN(LOOSE, &tn->tcp_loose);
960 	XASSIGN(LIBERAL, &tn->tcp_be_liberal);
961 	XASSIGN(MAX_RETRANS, &tn->tcp_max_retrans);
962 #undef XASSIGN
963 }
964 
nf_conntrack_standalone_init_sctp_sysctl(struct net * net,struct ctl_table * table)965 static void nf_conntrack_standalone_init_sctp_sysctl(struct net *net,
966 						     struct ctl_table *table)
967 {
968 #ifdef CONFIG_NF_CT_PROTO_SCTP
969 	struct nf_sctp_net *sn = nf_sctp_pernet(net);
970 
971 #define XASSIGN(XNAME, sn) \
972 	table[NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_ ## XNAME].data = \
973 			&(sn)->timeouts[SCTP_CONNTRACK_ ## XNAME]
974 
975 	XASSIGN(CLOSED, sn);
976 	XASSIGN(COOKIE_WAIT, sn);
977 	XASSIGN(COOKIE_ECHOED, sn);
978 	XASSIGN(ESTABLISHED, sn);
979 	XASSIGN(SHUTDOWN_SENT, sn);
980 	XASSIGN(SHUTDOWN_RECD, sn);
981 	XASSIGN(SHUTDOWN_ACK_SENT, sn);
982 	XASSIGN(HEARTBEAT_SENT, sn);
983 #undef XASSIGN
984 #endif
985 }
986 
nf_conntrack_standalone_init_dccp_sysctl(struct net * net,struct ctl_table * table)987 static void nf_conntrack_standalone_init_dccp_sysctl(struct net *net,
988 						     struct ctl_table *table)
989 {
990 #ifdef CONFIG_NF_CT_PROTO_DCCP
991 	struct nf_dccp_net *dn = nf_dccp_pernet(net);
992 
993 #define XASSIGN(XNAME, dn) \
994 	table[NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_ ## XNAME].data = \
995 			&(dn)->dccp_timeout[CT_DCCP_ ## XNAME]
996 
997 	XASSIGN(REQUEST, dn);
998 	XASSIGN(RESPOND, dn);
999 	XASSIGN(PARTOPEN, dn);
1000 	XASSIGN(OPEN, dn);
1001 	XASSIGN(CLOSEREQ, dn);
1002 	XASSIGN(CLOSING, dn);
1003 	XASSIGN(TIMEWAIT, dn);
1004 #undef XASSIGN
1005 
1006 	table[NF_SYSCTL_CT_PROTO_DCCP_LOOSE].data = &dn->dccp_loose;
1007 #endif
1008 }
1009 
nf_conntrack_standalone_init_gre_sysctl(struct net * net,struct ctl_table * table)1010 static void nf_conntrack_standalone_init_gre_sysctl(struct net *net,
1011 						    struct ctl_table *table)
1012 {
1013 #ifdef CONFIG_NF_CT_PROTO_GRE
1014 	struct nf_gre_net *gn = nf_gre_pernet(net);
1015 
1016 	table[NF_SYSCTL_CT_PROTO_TIMEOUT_GRE].data = &gn->timeouts[GRE_CT_UNREPLIED];
1017 	table[NF_SYSCTL_CT_PROTO_TIMEOUT_GRE_STREAM].data = &gn->timeouts[GRE_CT_REPLIED];
1018 #endif
1019 }
1020 
nf_conntrack_standalone_init_sysctl(struct net * net)1021 static int nf_conntrack_standalone_init_sysctl(struct net *net)
1022 {
1023 	struct nf_udp_net *un = nf_udp_pernet(net);
1024 	struct ctl_table *table;
1025 
1026 	BUILD_BUG_ON(ARRAY_SIZE(nf_ct_sysctl_table) != NF_SYSCTL_CT_LAST_SYSCTL);
1027 
1028 	table = kmemdup(nf_ct_sysctl_table, sizeof(nf_ct_sysctl_table),
1029 			GFP_KERNEL);
1030 	if (!table)
1031 		return -ENOMEM;
1032 
1033 	table[NF_SYSCTL_CT_COUNT].data = &net->ct.count;
1034 	table[NF_SYSCTL_CT_CHECKSUM].data = &net->ct.sysctl_checksum;
1035 	table[NF_SYSCTL_CT_LOG_INVALID].data = &net->ct.sysctl_log_invalid;
1036 	table[NF_SYSCTL_CT_ACCT].data = &net->ct.sysctl_acct;
1037 	table[NF_SYSCTL_CT_HELPER].data = &net->ct.sysctl_auto_assign_helper;
1038 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1039 	table[NF_SYSCTL_CT_EVENTS].data = &net->ct.sysctl_events;
1040 #endif
1041 #ifdef CONFIG_NF_CONNTRACK_TIMESTAMP
1042 	table[NF_SYSCTL_CT_TIMESTAMP].data = &net->ct.sysctl_tstamp;
1043 #endif
1044 	table[NF_SYSCTL_CT_PROTO_TIMEOUT_GENERIC].data = &nf_generic_pernet(net)->timeout;
1045 	table[NF_SYSCTL_CT_PROTO_TIMEOUT_ICMP].data = &nf_icmp_pernet(net)->timeout;
1046 	table[NF_SYSCTL_CT_PROTO_TIMEOUT_ICMPV6].data = &nf_icmpv6_pernet(net)->timeout;
1047 	table[NF_SYSCTL_CT_PROTO_TIMEOUT_UDP].data = &un->timeouts[UDP_CT_UNREPLIED];
1048 	table[NF_SYSCTL_CT_PROTO_TIMEOUT_UDP_STREAM].data = &un->timeouts[UDP_CT_REPLIED];
1049 
1050 	nf_conntrack_standalone_init_tcp_sysctl(net, table);
1051 	nf_conntrack_standalone_init_sctp_sysctl(net, table);
1052 	nf_conntrack_standalone_init_dccp_sysctl(net, table);
1053 	nf_conntrack_standalone_init_gre_sysctl(net, table);
1054 
1055 	/* Don't allow non-init_net ns to alter global sysctls */
1056 	if (!net_eq(&init_net, net)) {
1057 		table[NF_SYSCTL_CT_MAX].mode = 0444;
1058 		table[NF_SYSCTL_CT_EXPECT_MAX].mode = 0444;
1059 		table[NF_SYSCTL_CT_BUCKETS].mode = 0444;
1060 	}
1061 
1062 	net->ct.sysctl_header = register_net_sysctl(net, "net/netfilter", table);
1063 	if (!net->ct.sysctl_header)
1064 		goto out_unregister_netfilter;
1065 
1066 	return 0;
1067 
1068 out_unregister_netfilter:
1069 	kfree(table);
1070 	return -ENOMEM;
1071 }
1072 
nf_conntrack_standalone_fini_sysctl(struct net * net)1073 static void nf_conntrack_standalone_fini_sysctl(struct net *net)
1074 {
1075 	struct ctl_table *table;
1076 
1077 	table = net->ct.sysctl_header->ctl_table_arg;
1078 	unregister_net_sysctl_table(net->ct.sysctl_header);
1079 	kfree(table);
1080 }
1081 #else
nf_conntrack_standalone_init_sysctl(struct net * net)1082 static int nf_conntrack_standalone_init_sysctl(struct net *net)
1083 {
1084 	return 0;
1085 }
1086 
nf_conntrack_standalone_fini_sysctl(struct net * net)1087 static void nf_conntrack_standalone_fini_sysctl(struct net *net)
1088 {
1089 }
1090 #endif /* CONFIG_SYSCTL */
1091 
nf_conntrack_fini_net(struct net * net)1092 static void nf_conntrack_fini_net(struct net *net)
1093 {
1094 	if (enable_hooks)
1095 		nf_ct_netns_put(net, NFPROTO_INET);
1096 
1097 	nf_conntrack_standalone_fini_proc(net);
1098 	nf_conntrack_standalone_fini_sysctl(net);
1099 }
1100 
nf_conntrack_pernet_init(struct net * net)1101 static int nf_conntrack_pernet_init(struct net *net)
1102 {
1103 	int ret;
1104 
1105 	net->ct.sysctl_checksum = 1;
1106 
1107 	ret = nf_conntrack_standalone_init_sysctl(net);
1108 	if (ret < 0)
1109 		return ret;
1110 
1111 	ret = nf_conntrack_standalone_init_proc(net);
1112 	if (ret < 0)
1113 		goto out_proc;
1114 
1115 	ret = nf_conntrack_init_net(net);
1116 	if (ret < 0)
1117 		goto out_init_net;
1118 
1119 	if (enable_hooks) {
1120 		ret = nf_ct_netns_get(net, NFPROTO_INET);
1121 		if (ret < 0)
1122 			goto out_hooks;
1123 	}
1124 
1125 	return 0;
1126 
1127 out_hooks:
1128 	nf_conntrack_cleanup_net(net);
1129 out_init_net:
1130 	nf_conntrack_standalone_fini_proc(net);
1131 out_proc:
1132 	nf_conntrack_standalone_fini_sysctl(net);
1133 	return ret;
1134 }
1135 
nf_conntrack_pernet_exit(struct list_head * net_exit_list)1136 static void nf_conntrack_pernet_exit(struct list_head *net_exit_list)
1137 {
1138 	struct net *net;
1139 
1140 	list_for_each_entry(net, net_exit_list, exit_list)
1141 		nf_conntrack_fini_net(net);
1142 
1143 	nf_conntrack_cleanup_net_list(net_exit_list);
1144 }
1145 
1146 static struct pernet_operations nf_conntrack_net_ops = {
1147 	.init		= nf_conntrack_pernet_init,
1148 	.exit_batch	= nf_conntrack_pernet_exit,
1149 	.id		= &nf_conntrack_net_id,
1150 	.size = sizeof(struct nf_conntrack_net),
1151 };
1152 
nf_conntrack_standalone_init(void)1153 static int __init nf_conntrack_standalone_init(void)
1154 {
1155 	int ret = nf_conntrack_init_start();
1156 	if (ret < 0)
1157 		goto out_start;
1158 
1159 	BUILD_BUG_ON(NFCT_INFOMASK <= IP_CT_NUMBER);
1160 
1161 #ifdef CONFIG_SYSCTL
1162 	nf_ct_netfilter_header =
1163 		register_net_sysctl(&init_net, "net", nf_ct_netfilter_table);
1164 	if (!nf_ct_netfilter_header) {
1165 		pr_err("nf_conntrack: can't register to sysctl.\n");
1166 		ret = -ENOMEM;
1167 		goto out_sysctl;
1168 	}
1169 
1170 	nf_conntrack_htable_size_user = nf_conntrack_htable_size;
1171 #endif
1172 
1173 	nf_conntrack_init_end();
1174 
1175 	ret = register_pernet_subsys(&nf_conntrack_net_ops);
1176 	if (ret < 0)
1177 		goto out_pernet;
1178 
1179 	return 0;
1180 
1181 out_pernet:
1182 #ifdef CONFIG_SYSCTL
1183 	unregister_net_sysctl_table(nf_ct_netfilter_header);
1184 out_sysctl:
1185 #endif
1186 	nf_conntrack_cleanup_end();
1187 out_start:
1188 	return ret;
1189 }
1190 
nf_conntrack_standalone_fini(void)1191 static void __exit nf_conntrack_standalone_fini(void)
1192 {
1193 	nf_conntrack_cleanup_start();
1194 	unregister_pernet_subsys(&nf_conntrack_net_ops);
1195 #ifdef CONFIG_SYSCTL
1196 	unregister_net_sysctl_table(nf_ct_netfilter_header);
1197 #endif
1198 	nf_conntrack_cleanup_end();
1199 }
1200 
1201 module_init(nf_conntrack_standalone_init);
1202 module_exit(nf_conntrack_standalone_fini);
1203