]>
Commit | Line | Data |
---|---|---|
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 | unsigned int nf_conntrack_net_id __read_mostly; | |
28 | ||
29 | #ifdef CONFIG_NF_CONNTRACK_PROCFS | |
30 | void | |
31 | print_tuple(struct seq_file *s, const struct nf_conntrack_tuple *tuple, | |
32 | const struct nf_conntrack_l4proto *l4proto) | |
33 | { | |
34 | switch (tuple->src.l3num) { | |
35 | case NFPROTO_IPV4: | |
36 | seq_printf(s, "src=%pI4 dst=%pI4 ", | |
37 | &tuple->src.u3.ip, &tuple->dst.u3.ip); | |
38 | break; | |
39 | case NFPROTO_IPV6: | |
40 | seq_printf(s, "src=%pI6 dst=%pI6 ", | |
41 | tuple->src.u3.ip6, tuple->dst.u3.ip6); | |
42 | break; | |
43 | default: | |
44 | break; | |
45 | } | |
46 | ||
47 | switch (l4proto->l4proto) { | |
48 | case IPPROTO_ICMP: | |
49 | seq_printf(s, "type=%u code=%u id=%u ", | |
50 | tuple->dst.u.icmp.type, | |
51 | tuple->dst.u.icmp.code, | |
52 | ntohs(tuple->src.u.icmp.id)); | |
53 | break; | |
54 | case IPPROTO_TCP: | |
55 | seq_printf(s, "sport=%hu dport=%hu ", | |
56 | ntohs(tuple->src.u.tcp.port), | |
57 | ntohs(tuple->dst.u.tcp.port)); | |
58 | break; | |
59 | case IPPROTO_UDPLITE: /* fallthrough */ | |
60 | case IPPROTO_UDP: | |
61 | seq_printf(s, "sport=%hu dport=%hu ", | |
62 | ntohs(tuple->src.u.udp.port), | |
63 | ntohs(tuple->dst.u.udp.port)); | |
64 | ||
65 | break; | |
66 | case IPPROTO_DCCP: | |
67 | seq_printf(s, "sport=%hu dport=%hu ", | |
68 | ntohs(tuple->src.u.dccp.port), | |
69 | ntohs(tuple->dst.u.dccp.port)); | |
70 | break; | |
71 | case IPPROTO_SCTP: | |
72 | seq_printf(s, "sport=%hu dport=%hu ", | |
73 | ntohs(tuple->src.u.sctp.port), | |
74 | ntohs(tuple->dst.u.sctp.port)); | |
75 | break; | |
76 | case IPPROTO_ICMPV6: | |
77 | seq_printf(s, "type=%u code=%u id=%u ", | |
78 | tuple->dst.u.icmp.type, | |
79 | tuple->dst.u.icmp.code, | |
80 | ntohs(tuple->src.u.icmp.id)); | |
81 | break; | |
82 | case IPPROTO_GRE: | |
83 | seq_printf(s, "srckey=0x%x dstkey=0x%x ", | |
84 | ntohs(tuple->src.u.gre.key), | |
85 | ntohs(tuple->dst.u.gre.key)); | |
86 | break; | |
87 | default: | |
88 | break; | |
89 | } | |
90 | } | |
91 | EXPORT_SYMBOL_GPL(print_tuple); | |
92 | ||
93 | struct ct_iter_state { | |
94 | struct seq_net_private p; | |
95 | struct hlist_nulls_head *hash; | |
96 | unsigned int htable_size; | |
97 | unsigned int bucket; | |
98 | u_int64_t time_now; | |
99 | }; | |
100 | ||
101 | static struct hlist_nulls_node *ct_get_first(struct seq_file *seq) | |
102 | { | |
103 | struct ct_iter_state *st = seq->private; | |
104 | struct hlist_nulls_node *n; | |
105 | ||
106 | for (st->bucket = 0; | |
107 | st->bucket < st->htable_size; | |
108 | st->bucket++) { | |
109 | n = rcu_dereference( | |
110 | hlist_nulls_first_rcu(&st->hash[st->bucket])); | |
111 | if (!is_a_nulls(n)) | |
112 | return n; | |
113 | } | |
114 | return NULL; | |
115 | } | |
116 | ||
117 | static struct hlist_nulls_node *ct_get_next(struct seq_file *seq, | |
118 | struct hlist_nulls_node *head) | |
119 | { | |
120 | struct ct_iter_state *st = seq->private; | |
121 | ||
122 | head = rcu_dereference(hlist_nulls_next_rcu(head)); | |
123 | while (is_a_nulls(head)) { | |
124 | if (likely(get_nulls_value(head) == st->bucket)) { | |
125 | if (++st->bucket >= st->htable_size) | |
126 | return NULL; | |
127 | } | |
128 | head = rcu_dereference( | |
129 | hlist_nulls_first_rcu(&st->hash[st->bucket])); | |
130 | } | |
131 | return head; | |
132 | } | |
133 | ||
134 | static struct hlist_nulls_node *ct_get_idx(struct seq_file *seq, loff_t pos) | |
135 | { | |
136 | struct hlist_nulls_node *head = ct_get_first(seq); | |
137 | ||
138 | if (head) | |
139 | while (pos && (head = ct_get_next(seq, head))) | |
140 | pos--; | |
141 | return pos ? NULL : head; | |
142 | } | |
143 | ||
144 | static void *ct_seq_start(struct seq_file *seq, loff_t *pos) | |
145 | __acquires(RCU) | |
146 | { | |
147 | struct ct_iter_state *st = seq->private; | |
148 | ||
149 | st->time_now = ktime_get_real_ns(); | |
150 | rcu_read_lock(); | |
151 | ||
152 | nf_conntrack_get_ht(&st->hash, &st->htable_size); | |
153 | return ct_get_idx(seq, *pos); | |
154 | } | |
155 | ||
156 | static void *ct_seq_next(struct seq_file *s, void *v, loff_t *pos) | |
157 | { | |
158 | (*pos)++; | |
159 | return ct_get_next(s, v); | |
160 | } | |
161 | ||
162 | static void ct_seq_stop(struct seq_file *s, void *v) | |
163 | __releases(RCU) | |
164 | { | |
165 | rcu_read_unlock(); | |
166 | } | |
167 | ||
168 | #ifdef CONFIG_NF_CONNTRACK_SECMARK | |
169 | static void ct_show_secctx(struct seq_file *s, const struct nf_conn *ct) | |
170 | { | |
171 | int ret; | |
172 | u32 len; | |
173 | char *secctx; | |
174 | ||
175 | ret = security_secid_to_secctx(ct->secmark, &secctx, &len); | |
176 | if (ret) | |
177 | return; | |
178 | ||
179 | seq_printf(s, "secctx=%s ", secctx); | |
180 | ||
181 | security_release_secctx(secctx, len); | |
182 | } | |
183 | #else | |
184 | static inline void ct_show_secctx(struct seq_file *s, const struct nf_conn *ct) | |
185 | { | |
186 | } | |
187 | #endif | |
188 | ||
189 | #ifdef CONFIG_NF_CONNTRACK_ZONES | |
190 | static void ct_show_zone(struct seq_file *s, const struct nf_conn *ct, | |
191 | int dir) | |
192 | { | |
193 | const struct nf_conntrack_zone *zone = nf_ct_zone(ct); | |
194 | ||
195 | if (zone->dir != dir) | |
196 | return; | |
197 | switch (zone->dir) { | |
198 | case NF_CT_DEFAULT_ZONE_DIR: | |
199 | seq_printf(s, "zone=%u ", zone->id); | |
200 | break; | |
201 | case NF_CT_ZONE_DIR_ORIG: | |
202 | seq_printf(s, "zone-orig=%u ", zone->id); | |
203 | break; | |
204 | case NF_CT_ZONE_DIR_REPL: | |
205 | seq_printf(s, "zone-reply=%u ", zone->id); | |
206 | break; | |
207 | default: | |
208 | break; | |
209 | } | |
210 | } | |
211 | #else | |
212 | static inline void ct_show_zone(struct seq_file *s, const struct nf_conn *ct, | |
213 | int dir) | |
214 | { | |
215 | } | |
216 | #endif | |
217 | ||
218 | #ifdef CONFIG_NF_CONNTRACK_TIMESTAMP | |
219 | static void ct_show_delta_time(struct seq_file *s, const struct nf_conn *ct) | |
220 | { | |
221 | struct ct_iter_state *st = s->private; | |
222 | struct nf_conn_tstamp *tstamp; | |
223 | s64 delta_time; | |
224 | ||
225 | tstamp = nf_conn_tstamp_find(ct); | |
226 | if (tstamp) { | |
227 | delta_time = st->time_now - tstamp->start; | |
228 | if (delta_time > 0) | |
229 | delta_time = div_s64(delta_time, NSEC_PER_SEC); | |
230 | else | |
231 | delta_time = 0; | |
232 | ||
233 | seq_printf(s, "delta-time=%llu ", | |
234 | (unsigned long long)delta_time); | |
235 | } | |
236 | return; | |
237 | } | |
238 | #else | |
239 | static inline void | |
240 | ct_show_delta_time(struct seq_file *s, const struct nf_conn *ct) | |
241 | { | |
242 | } | |
243 | #endif | |
244 | ||
245 | static const char* l3proto_name(u16 proto) | |
246 | { | |
247 | switch (proto) { | |
248 | case AF_INET: return "ipv4"; | |
249 | case AF_INET6: return "ipv6"; | |
250 | } | |
251 | ||
252 | return "unknown"; | |
253 | } | |
254 | ||
255 | static const char* l4proto_name(u16 proto) | |
256 | { | |
257 | switch (proto) { | |
258 | case IPPROTO_ICMP: return "icmp"; | |
259 | case IPPROTO_TCP: return "tcp"; | |
260 | case IPPROTO_UDP: return "udp"; | |
261 | case IPPROTO_DCCP: return "dccp"; | |
262 | case IPPROTO_GRE: return "gre"; | |
263 | case IPPROTO_SCTP: return "sctp"; | |
264 | case IPPROTO_UDPLITE: return "udplite"; | |
265 | } | |
266 | ||
267 | return "unknown"; | |
268 | } | |
269 | ||
270 | static unsigned int | |
271 | seq_print_acct(struct seq_file *s, const struct nf_conn *ct, int dir) | |
272 | { | |
273 | struct nf_conn_acct *acct; | |
274 | struct nf_conn_counter *counter; | |
275 | ||
276 | acct = nf_conn_acct_find(ct); | |
277 | if (!acct) | |
278 | return 0; | |
279 | ||
280 | counter = acct->counter; | |
281 | seq_printf(s, "packets=%llu bytes=%llu ", | |
282 | (unsigned long long)atomic64_read(&counter[dir].packets), | |
283 | (unsigned long long)atomic64_read(&counter[dir].bytes)); | |
284 | ||
285 | return 0; | |
286 | } | |
287 | ||
288 | /* return 0 on success, 1 in case of error */ | |
289 | static int ct_seq_show(struct seq_file *s, void *v) | |
290 | { | |
291 | struct nf_conntrack_tuple_hash *hash = v; | |
292 | struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(hash); | |
293 | const struct nf_conntrack_l4proto *l4proto; | |
294 | struct net *net = seq_file_net(s); | |
295 | int ret = 0; | |
296 | ||
297 | WARN_ON(!ct); | |
298 | if (unlikely(!atomic_inc_not_zero(&ct->ct_general.use))) | |
299 | return 0; | |
300 | ||
301 | if (nf_ct_should_gc(ct)) { | |
302 | nf_ct_kill(ct); | |
303 | goto release; | |
304 | } | |
305 | ||
306 | /* we only want to print DIR_ORIGINAL */ | |
307 | if (NF_CT_DIRECTION(hash)) | |
308 | goto release; | |
309 | ||
310 | if (!net_eq(nf_ct_net(ct), net)) | |
311 | goto release; | |
312 | ||
313 | l4proto = __nf_ct_l4proto_find(nf_ct_protonum(ct)); | |
314 | WARN_ON(!l4proto); | |
315 | ||
316 | ret = -ENOSPC; | |
317 | seq_printf(s, "%-8s %u %-8s %u ", | |
318 | l3proto_name(nf_ct_l3num(ct)), nf_ct_l3num(ct), | |
319 | l4proto_name(l4proto->l4proto), nf_ct_protonum(ct)); | |
320 | ||
321 | if (!test_bit(IPS_OFFLOAD_BIT, &ct->status)) | |
322 | seq_printf(s, "%ld ", nf_ct_expires(ct) / HZ); | |
323 | ||
324 | if (l4proto->print_conntrack) | |
325 | l4proto->print_conntrack(s, ct); | |
326 | ||
327 | print_tuple(s, &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple, | |
328 | l4proto); | |
329 | ||
330 | ct_show_zone(s, ct, NF_CT_ZONE_DIR_ORIG); | |
331 | ||
332 | if (seq_has_overflowed(s)) | |
333 | goto release; | |
334 | ||
335 | if (seq_print_acct(s, ct, IP_CT_DIR_ORIGINAL)) | |
336 | goto release; | |
337 | ||
338 | if (!(test_bit(IPS_SEEN_REPLY_BIT, &ct->status))) | |
339 | seq_puts(s, "[UNREPLIED] "); | |
340 | ||
341 | print_tuple(s, &ct->tuplehash[IP_CT_DIR_REPLY].tuple, l4proto); | |
342 | ||
343 | ct_show_zone(s, ct, NF_CT_ZONE_DIR_REPL); | |
344 | ||
345 | if (seq_print_acct(s, ct, IP_CT_DIR_REPLY)) | |
346 | goto release; | |
347 | ||
348 | if (test_bit(IPS_OFFLOAD_BIT, &ct->status)) | |
349 | seq_puts(s, "[OFFLOAD] "); | |
350 | else if (test_bit(IPS_ASSURED_BIT, &ct->status)) | |
351 | seq_puts(s, "[ASSURED] "); | |
352 | ||
353 | if (seq_has_overflowed(s)) | |
354 | goto release; | |
355 | ||
356 | #if defined(CONFIG_NF_CONNTRACK_MARK) | |
357 | seq_printf(s, "mark=%u ", ct->mark); | |
358 | #endif | |
359 | ||
360 | ct_show_secctx(s, ct); | |
361 | ct_show_zone(s, ct, NF_CT_DEFAULT_ZONE_DIR); | |
362 | ct_show_delta_time(s, ct); | |
363 | ||
364 | seq_printf(s, "use=%u\n", atomic_read(&ct->ct_general.use)); | |
365 | ||
366 | if (seq_has_overflowed(s)) | |
367 | goto release; | |
368 | ||
369 | ret = 0; | |
370 | release: | |
371 | nf_ct_put(ct); | |
372 | return ret; | |
373 | } | |
374 | ||
375 | static const struct seq_operations ct_seq_ops = { | |
376 | .start = ct_seq_start, | |
377 | .next = ct_seq_next, | |
378 | .stop = ct_seq_stop, | |
379 | .show = ct_seq_show | |
380 | }; | |
381 | ||
382 | static void *ct_cpu_seq_start(struct seq_file *seq, loff_t *pos) | |
383 | { | |
384 | struct net *net = seq_file_net(seq); | |
385 | int cpu; | |
386 | ||
387 | if (*pos == 0) | |
388 | return SEQ_START_TOKEN; | |
389 | ||
390 | for (cpu = *pos-1; cpu < nr_cpu_ids; ++cpu) { | |
391 | if (!cpu_possible(cpu)) | |
392 | continue; | |
393 | *pos = cpu + 1; | |
394 | return per_cpu_ptr(net->ct.stat, cpu); | |
395 | } | |
396 | ||
397 | return NULL; | |
398 | } | |
399 | ||
400 | static void *ct_cpu_seq_next(struct seq_file *seq, void *v, loff_t *pos) | |
401 | { | |
402 | struct net *net = seq_file_net(seq); | |
403 | int cpu; | |
404 | ||
405 | for (cpu = *pos; cpu < nr_cpu_ids; ++cpu) { | |
406 | if (!cpu_possible(cpu)) | |
407 | continue; | |
408 | *pos = cpu + 1; | |
409 | return per_cpu_ptr(net->ct.stat, cpu); | |
410 | } | |
411 | ||
412 | return NULL; | |
413 | } | |
414 | ||
415 | static void ct_cpu_seq_stop(struct seq_file *seq, void *v) | |
416 | { | |
417 | } | |
418 | ||
419 | static int ct_cpu_seq_show(struct seq_file *seq, void *v) | |
420 | { | |
421 | struct net *net = seq_file_net(seq); | |
422 | unsigned int nr_conntracks = atomic_read(&net->ct.count); | |
423 | const struct ip_conntrack_stat *st = v; | |
424 | ||
425 | if (v == SEQ_START_TOKEN) { | |
426 | seq_puts(seq, "entries searched found new invalid ignore delete delete_list insert insert_failed drop early_drop icmp_error expect_new expect_create expect_delete search_restart\n"); | |
427 | return 0; | |
428 | } | |
429 | ||
430 | seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x %08x " | |
431 | "%08x %08x %08x %08x %08x %08x %08x %08x %08x\n", | |
432 | nr_conntracks, | |
433 | 0, | |
434 | st->found, | |
435 | 0, | |
436 | st->invalid, | |
437 | st->ignore, | |
438 | 0, | |
439 | 0, | |
440 | st->insert, | |
441 | st->insert_failed, | |
442 | st->drop, | |
443 | st->early_drop, | |
444 | st->error, | |
445 | ||
446 | st->expect_new, | |
447 | st->expect_create, | |
448 | st->expect_delete, | |
449 | st->search_restart | |
450 | ); | |
451 | return 0; | |
452 | } | |
453 | ||
454 | static const struct seq_operations ct_cpu_seq_ops = { | |
455 | .start = ct_cpu_seq_start, | |
456 | .next = ct_cpu_seq_next, | |
457 | .stop = ct_cpu_seq_stop, | |
458 | .show = ct_cpu_seq_show, | |
459 | }; | |
460 | ||
461 | static int nf_conntrack_standalone_init_proc(struct net *net) | |
462 | { | |
463 | struct proc_dir_entry *pde; | |
464 | kuid_t root_uid; | |
465 | kgid_t root_gid; | |
466 | ||
467 | pde = proc_create_net("nf_conntrack", 0440, net->proc_net, &ct_seq_ops, | |
468 | sizeof(struct ct_iter_state)); | |
469 | if (!pde) | |
470 | goto out_nf_conntrack; | |
471 | ||
472 | root_uid = make_kuid(net->user_ns, 0); | |
473 | root_gid = make_kgid(net->user_ns, 0); | |
474 | if (uid_valid(root_uid) && gid_valid(root_gid)) | |
475 | proc_set_user(pde, root_uid, root_gid); | |
476 | ||
477 | pde = proc_create_net("nf_conntrack", 0444, net->proc_net_stat, | |
478 | &ct_cpu_seq_ops, sizeof(struct seq_net_private)); | |
479 | if (!pde) | |
480 | goto out_stat_nf_conntrack; | |
481 | return 0; | |
482 | ||
483 | out_stat_nf_conntrack: | |
484 | remove_proc_entry("nf_conntrack", net->proc_net); | |
485 | out_nf_conntrack: | |
486 | return -ENOMEM; | |
487 | } | |
488 | ||
489 | static void nf_conntrack_standalone_fini_proc(struct net *net) | |
490 | { | |
491 | remove_proc_entry("nf_conntrack", net->proc_net_stat); | |
492 | remove_proc_entry("nf_conntrack", net->proc_net); | |
493 | } | |
494 | #else | |
495 | static int nf_conntrack_standalone_init_proc(struct net *net) | |
496 | { | |
497 | return 0; | |
498 | } | |
499 | ||
500 | static void nf_conntrack_standalone_fini_proc(struct net *net) | |
501 | { | |
502 | } | |
503 | #endif /* CONFIG_NF_CONNTRACK_PROCFS */ | |
504 | ||
505 | /* Sysctl support */ | |
506 | ||
507 | #ifdef CONFIG_SYSCTL | |
508 | /* Log invalid packets of a given protocol */ | |
509 | static int log_invalid_proto_min __read_mostly; | |
510 | static int log_invalid_proto_max __read_mostly = 255; | |
511 | ||
512 | /* size the user *wants to set */ | |
513 | static unsigned int nf_conntrack_htable_size_user __read_mostly; | |
514 | ||
515 | static int | |
516 | nf_conntrack_hash_sysctl(struct ctl_table *table, int write, | |
517 | void __user *buffer, size_t *lenp, loff_t *ppos) | |
518 | { | |
519 | int ret; | |
520 | ||
521 | ret = proc_dointvec(table, write, buffer, lenp, ppos); | |
522 | if (ret < 0 || !write) | |
523 | return ret; | |
524 | ||
525 | /* update ret, we might not be able to satisfy request */ | |
526 | ret = nf_conntrack_hash_resize(nf_conntrack_htable_size_user); | |
527 | ||
528 | /* update it to the actual value used by conntrack */ | |
529 | nf_conntrack_htable_size_user = nf_conntrack_htable_size; | |
530 | return ret; | |
531 | } | |
532 | ||
533 | static struct ctl_table_header *nf_ct_netfilter_header; | |
534 | ||
535 | static struct ctl_table nf_ct_sysctl_table[] = { | |
536 | { | |
537 | .procname = "nf_conntrack_max", | |
538 | .data = &nf_conntrack_max, | |
539 | .maxlen = sizeof(int), | |
540 | .mode = 0644, | |
541 | .proc_handler = proc_dointvec, | |
542 | }, | |
543 | { | |
544 | .procname = "nf_conntrack_count", | |
545 | .data = &init_net.ct.count, | |
546 | .maxlen = sizeof(int), | |
547 | .mode = 0444, | |
548 | .proc_handler = proc_dointvec, | |
549 | }, | |
550 | { | |
551 | .procname = "nf_conntrack_buckets", | |
552 | .data = &nf_conntrack_htable_size_user, | |
553 | .maxlen = sizeof(unsigned int), | |
554 | .mode = 0644, | |
555 | .proc_handler = nf_conntrack_hash_sysctl, | |
556 | }, | |
557 | { | |
558 | .procname = "nf_conntrack_checksum", | |
559 | .data = &init_net.ct.sysctl_checksum, | |
560 | .maxlen = sizeof(unsigned int), | |
561 | .mode = 0644, | |
562 | .proc_handler = proc_dointvec, | |
563 | }, | |
564 | { | |
565 | .procname = "nf_conntrack_log_invalid", | |
566 | .data = &init_net.ct.sysctl_log_invalid, | |
567 | .maxlen = sizeof(unsigned int), | |
568 | .mode = 0644, | |
569 | .proc_handler = proc_dointvec_minmax, | |
570 | .extra1 = &log_invalid_proto_min, | |
571 | .extra2 = &log_invalid_proto_max, | |
572 | }, | |
573 | { | |
574 | .procname = "nf_conntrack_expect_max", | |
575 | .data = &nf_ct_expect_max, | |
576 | .maxlen = sizeof(int), | |
577 | .mode = 0644, | |
578 | .proc_handler = proc_dointvec, | |
579 | }, | |
580 | { } | |
581 | }; | |
582 | ||
583 | static struct ctl_table nf_ct_netfilter_table[] = { | |
584 | { | |
585 | .procname = "nf_conntrack_max", | |
586 | .data = &nf_conntrack_max, | |
587 | .maxlen = sizeof(int), | |
588 | .mode = 0644, | |
589 | .proc_handler = proc_dointvec, | |
590 | }, | |
591 | { } | |
592 | }; | |
593 | ||
594 | static int nf_conntrack_standalone_init_sysctl(struct net *net) | |
595 | { | |
596 | struct ctl_table *table; | |
597 | ||
598 | table = kmemdup(nf_ct_sysctl_table, sizeof(nf_ct_sysctl_table), | |
599 | GFP_KERNEL); | |
600 | if (!table) | |
601 | goto out_kmemdup; | |
602 | ||
603 | table[1].data = &net->ct.count; | |
604 | table[3].data = &net->ct.sysctl_checksum; | |
605 | table[4].data = &net->ct.sysctl_log_invalid; | |
606 | ||
607 | /* Don't export sysctls to unprivileged users */ | |
608 | if (net->user_ns != &init_user_ns) | |
609 | table[0].procname = NULL; | |
610 | ||
611 | if (!net_eq(&init_net, net)) | |
612 | table[2].mode = 0444; | |
613 | ||
614 | net->ct.sysctl_header = register_net_sysctl(net, "net/netfilter", table); | |
615 | if (!net->ct.sysctl_header) | |
616 | goto out_unregister_netfilter; | |
617 | ||
618 | return 0; | |
619 | ||
620 | out_unregister_netfilter: | |
621 | kfree(table); | |
622 | out_kmemdup: | |
623 | return -ENOMEM; | |
624 | } | |
625 | ||
626 | static void nf_conntrack_standalone_fini_sysctl(struct net *net) | |
627 | { | |
628 | struct ctl_table *table; | |
629 | ||
630 | table = net->ct.sysctl_header->ctl_table_arg; | |
631 | unregister_net_sysctl_table(net->ct.sysctl_header); | |
632 | kfree(table); | |
633 | } | |
634 | #else | |
635 | static int nf_conntrack_standalone_init_sysctl(struct net *net) | |
636 | { | |
637 | return 0; | |
638 | } | |
639 | ||
640 | static void nf_conntrack_standalone_fini_sysctl(struct net *net) | |
641 | { | |
642 | } | |
643 | #endif /* CONFIG_SYSCTL */ | |
644 | ||
645 | static int nf_conntrack_pernet_init(struct net *net) | |
646 | { | |
647 | int ret; | |
648 | ||
649 | ret = nf_conntrack_init_net(net); | |
650 | if (ret < 0) | |
651 | goto out_init; | |
652 | ||
653 | ret = nf_conntrack_standalone_init_proc(net); | |
654 | if (ret < 0) | |
655 | goto out_proc; | |
656 | ||
657 | net->ct.sysctl_checksum = 1; | |
658 | net->ct.sysctl_log_invalid = 0; | |
659 | ret = nf_conntrack_standalone_init_sysctl(net); | |
660 | if (ret < 0) | |
661 | goto out_sysctl; | |
662 | ||
663 | return 0; | |
664 | ||
665 | out_sysctl: | |
666 | nf_conntrack_standalone_fini_proc(net); | |
667 | out_proc: | |
668 | nf_conntrack_cleanup_net(net); | |
669 | out_init: | |
670 | return ret; | |
671 | } | |
672 | ||
673 | static void nf_conntrack_pernet_exit(struct list_head *net_exit_list) | |
674 | { | |
675 | struct net *net; | |
676 | ||
677 | list_for_each_entry(net, net_exit_list, exit_list) { | |
678 | nf_conntrack_standalone_fini_sysctl(net); | |
679 | nf_conntrack_standalone_fini_proc(net); | |
680 | } | |
681 | nf_conntrack_cleanup_net_list(net_exit_list); | |
682 | } | |
683 | ||
684 | static struct pernet_operations nf_conntrack_net_ops = { | |
685 | .init = nf_conntrack_pernet_init, | |
686 | .exit_batch = nf_conntrack_pernet_exit, | |
687 | .id = &nf_conntrack_net_id, | |
688 | .size = sizeof(struct nf_conntrack_net), | |
689 | }; | |
690 | ||
691 | static int __init nf_conntrack_standalone_init(void) | |
692 | { | |
693 | int ret = nf_conntrack_init_start(); | |
694 | if (ret < 0) | |
695 | goto out_start; | |
696 | ||
697 | BUILD_BUG_ON(SKB_NFCT_PTRMASK != NFCT_PTRMASK); | |
698 | BUILD_BUG_ON(NFCT_INFOMASK <= IP_CT_NUMBER); | |
699 | ||
700 | #ifdef CONFIG_SYSCTL | |
701 | nf_ct_netfilter_header = | |
702 | register_net_sysctl(&init_net, "net", nf_ct_netfilter_table); | |
703 | if (!nf_ct_netfilter_header) { | |
704 | pr_err("nf_conntrack: can't register to sysctl.\n"); | |
705 | ret = -ENOMEM; | |
706 | goto out_sysctl; | |
707 | } | |
708 | ||
709 | nf_conntrack_htable_size_user = nf_conntrack_htable_size; | |
710 | #endif | |
711 | ||
712 | ret = register_pernet_subsys(&nf_conntrack_net_ops); | |
713 | if (ret < 0) | |
714 | goto out_pernet; | |
715 | ||
716 | nf_conntrack_init_end(); | |
717 | return 0; | |
718 | ||
719 | out_pernet: | |
720 | #ifdef CONFIG_SYSCTL | |
721 | unregister_net_sysctl_table(nf_ct_netfilter_header); | |
722 | out_sysctl: | |
723 | #endif | |
724 | nf_conntrack_cleanup_end(); | |
725 | out_start: | |
726 | return ret; | |
727 | } | |
728 | ||
729 | static void __exit nf_conntrack_standalone_fini(void) | |
730 | { | |
731 | nf_conntrack_cleanup_start(); | |
732 | unregister_pernet_subsys(&nf_conntrack_net_ops); | |
733 | #ifdef CONFIG_SYSCTL | |
734 | unregister_net_sysctl_table(nf_ct_netfilter_header); | |
735 | #endif | |
736 | nf_conntrack_cleanup_end(); | |
737 | } | |
738 | ||
739 | module_init(nf_conntrack_standalone_init); | |
740 | module_exit(nf_conntrack_standalone_fini); |