]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - net/netfilter/nf_conntrack_standalone.c
netfilter: conntrack: reduce size of l4protocol trackers
[mirror_ubuntu-hirsute-kernel.git] / net / netfilter / nf_conntrack_standalone.c
CommitLineData
9fb9cbb1
YK
1/* (C) 1999-2001 Paul `Rusty' Russell
2 * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
f229f6ce 3 * (C) 2005-2012 Patrick McHardy <kaber@trash.net>
9fb9cbb1
YK
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9fb9cbb1
YK
8 */
9
9fb9cbb1
YK
10#include <linux/types.h>
11#include <linux/netfilter.h>
5a0e3ad6 12#include <linux/slab.h>
9fb9cbb1
YK
13#include <linux/module.h>
14#include <linux/skbuff.h>
15#include <linux/proc_fs.h>
16#include <linux/seq_file.h>
17#include <linux/percpu.h>
18#include <linux/netdevice.h>
1ae4de0c 19#include <linux/security.h>
457c4cbc 20#include <net/net_namespace.h>
9fb9cbb1
YK
21#ifdef CONFIG_SYSCTL
22#include <linux/sysctl.h>
23#endif
24
9fb9cbb1 25#include <net/netfilter/nf_conntrack.h>
f6180121 26#include <net/netfilter/nf_conntrack_core.h>
9fb9cbb1 27#include <net/netfilter/nf_conntrack_l3proto.h>
605dcad6 28#include <net/netfilter/nf_conntrack_l4proto.h>
77ab9cff 29#include <net/netfilter/nf_conntrack_expect.h>
9fb9cbb1 30#include <net/netfilter/nf_conntrack_helper.h>
58401572 31#include <net/netfilter/nf_conntrack_acct.h>
5d0aa2cc 32#include <net/netfilter/nf_conntrack_zones.h>
a992ca2a 33#include <net/netfilter/nf_conntrack_timestamp.h>
0e60ebe0 34#include <linux/rculist_nulls.h>
9fb9cbb1 35
9fb9cbb1
YK
36MODULE_LICENSE("GPL");
37
54b07dca 38#ifdef CONFIG_NF_CONNTRACK_PROCFS
824f1fbe 39void
9fb9cbb1 40print_tuple(struct seq_file *s, const struct nf_conntrack_tuple *tuple,
32948588
JE
41 const struct nf_conntrack_l3proto *l3proto,
42 const struct nf_conntrack_l4proto *l4proto)
9fb9cbb1 43{
824f1fbe
JP
44 l3proto->print_tuple(s, tuple);
45 l4proto->print_tuple(s, tuple);
9fb9cbb1 46}
e4bd8bce 47EXPORT_SYMBOL_GPL(print_tuple);
9fb9cbb1 48
9fb9cbb1 49struct ct_iter_state {
b2ce2c74 50 struct seq_net_private p;
64b87639
LZ
51 struct hlist_nulls_head *hash;
52 unsigned int htable_size;
9fb9cbb1 53 unsigned int bucket;
a992ca2a 54 u_int64_t time_now;
9fb9cbb1
YK
55};
56
ea781f19 57static struct hlist_nulls_node *ct_get_first(struct seq_file *seq)
9fb9cbb1
YK
58{
59 struct ct_iter_state *st = seq->private;
ea781f19 60 struct hlist_nulls_node *n;
9fb9cbb1
YK
61
62 for (st->bucket = 0;
64b87639 63 st->bucket < st->htable_size;
9fb9cbb1 64 st->bucket++) {
64b87639
LZ
65 n = rcu_dereference(
66 hlist_nulls_first_rcu(&st->hash[st->bucket]));
ea781f19 67 if (!is_a_nulls(n))
76507f69 68 return n;
9fb9cbb1
YK
69 }
70 return NULL;
71}
72
ea781f19
ED
73static struct hlist_nulls_node *ct_get_next(struct seq_file *seq,
74 struct hlist_nulls_node *head)
9fb9cbb1
YK
75{
76 struct ct_iter_state *st = seq->private;
77
0e60ebe0 78 head = rcu_dereference(hlist_nulls_next_rcu(head));
ea781f19
ED
79 while (is_a_nulls(head)) {
80 if (likely(get_nulls_value(head) == st->bucket)) {
64b87639 81 if (++st->bucket >= st->htable_size)
ea781f19
ED
82 return NULL;
83 }
0e60ebe0 84 head = rcu_dereference(
64b87639 85 hlist_nulls_first_rcu(&st->hash[st->bucket]));
9fb9cbb1
YK
86 }
87 return head;
88}
89
ea781f19 90static struct hlist_nulls_node *ct_get_idx(struct seq_file *seq, loff_t pos)
9fb9cbb1 91{
ea781f19 92 struct hlist_nulls_node *head = ct_get_first(seq);
9fb9cbb1
YK
93
94 if (head)
95 while (pos && (head = ct_get_next(seq, head)))
96 pos--;
97 return pos ? NULL : head;
98}
99
100static void *ct_seq_start(struct seq_file *seq, loff_t *pos)
76507f69 101 __acquires(RCU)
9fb9cbb1 102{
a992ca2a
PNA
103 struct ct_iter_state *st = seq->private;
104
d2de875c 105 st->time_now = ktime_get_real_ns();
76507f69 106 rcu_read_lock();
64b87639
LZ
107
108 nf_conntrack_get_ht(&st->hash, &st->htable_size);
9fb9cbb1
YK
109 return ct_get_idx(seq, *pos);
110}
111
112static void *ct_seq_next(struct seq_file *s, void *v, loff_t *pos)
113{
114 (*pos)++;
115 return ct_get_next(s, v);
116}
117
118static void ct_seq_stop(struct seq_file *s, void *v)
76507f69 119 __releases(RCU)
9fb9cbb1 120{
76507f69 121 rcu_read_unlock();
9fb9cbb1
YK
122}
123
1ae4de0c 124#ifdef CONFIG_NF_CONNTRACK_SECMARK
e71456ae 125static void ct_show_secctx(struct seq_file *s, const struct nf_conn *ct)
1ae4de0c
EP
126{
127 int ret;
128 u32 len;
129 char *secctx;
130
131 ret = security_secid_to_secctx(ct->secmark, &secctx, &len);
132 if (ret)
e71456ae 133 return;
1ae4de0c 134
e71456ae 135 seq_printf(s, "secctx=%s ", secctx);
1ae4de0c
EP
136
137 security_release_secctx(secctx, len);
1ae4de0c
EP
138}
139#else
e71456ae 140static inline void ct_show_secctx(struct seq_file *s, const struct nf_conn *ct)
1ae4de0c 141{
1ae4de0c
EP
142}
143#endif
144
308ac914 145#ifdef CONFIG_NF_CONNTRACK_ZONES
deedb590
DB
146static void ct_show_zone(struct seq_file *s, const struct nf_conn *ct,
147 int dir)
308ac914 148{
deedb590
DB
149 const struct nf_conntrack_zone *zone = nf_ct_zone(ct);
150
151 if (zone->dir != dir)
152 return;
153 switch (zone->dir) {
154 case NF_CT_DEFAULT_ZONE_DIR:
155 seq_printf(s, "zone=%u ", zone->id);
156 break;
157 case NF_CT_ZONE_DIR_ORIG:
158 seq_printf(s, "zone-orig=%u ", zone->id);
159 break;
160 case NF_CT_ZONE_DIR_REPL:
161 seq_printf(s, "zone-reply=%u ", zone->id);
162 break;
163 default:
164 break;
165 }
308ac914
DB
166}
167#else
deedb590
DB
168static inline void ct_show_zone(struct seq_file *s, const struct nf_conn *ct,
169 int dir)
308ac914
DB
170{
171}
172#endif
173
a992ca2a 174#ifdef CONFIG_NF_CONNTRACK_TIMESTAMP
e71456ae 175static void ct_show_delta_time(struct seq_file *s, const struct nf_conn *ct)
a992ca2a 176{
f5c88f56 177 struct ct_iter_state *st = s->private;
a992ca2a 178 struct nf_conn_tstamp *tstamp;
f5c88f56 179 s64 delta_time;
a992ca2a
PNA
180
181 tstamp = nf_conn_tstamp_find(ct);
182 if (tstamp) {
f5c88f56
PM
183 delta_time = st->time_now - tstamp->start;
184 if (delta_time > 0)
185 delta_time = div_s64(delta_time, NSEC_PER_SEC);
186 else
187 delta_time = 0;
188
e71456ae
SRRH
189 seq_printf(s, "delta-time=%llu ",
190 (unsigned long long)delta_time);
a992ca2a 191 }
e71456ae 192 return;
a992ca2a
PNA
193}
194#else
e71456ae 195static inline void
a992ca2a
PNA
196ct_show_delta_time(struct seq_file *s, const struct nf_conn *ct)
197{
a992ca2a
PNA
198}
199#endif
200
a3134d53
FW
201static const char* l3proto_name(u16 proto)
202{
203 switch (proto) {
204 case AF_INET: return "ipv4";
205 case AF_INET6: return "ipv6";
206 }
207
208 return "unknown";
209}
210
09ec82f5
FW
211static const char* l4proto_name(u16 proto)
212{
213 switch (proto) {
214 case IPPROTO_ICMP: return "icmp";
215 case IPPROTO_TCP: return "tcp";
216 case IPPROTO_UDP: return "udp";
217 case IPPROTO_DCCP: return "dccp";
218 case IPPROTO_GRE: return "gre";
219 case IPPROTO_SCTP: return "sctp";
220 case IPPROTO_UDPLITE: return "udplite";
221 }
222
223 return "unknown";
224}
225
9fb9cbb1
YK
226/* return 0 on success, 1 in case of error */
227static int ct_seq_show(struct seq_file *s, void *v)
228{
ea781f19
ED
229 struct nf_conntrack_tuple_hash *hash = v;
230 struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(hash);
32948588
JE
231 const struct nf_conntrack_l3proto *l3proto;
232 const struct nf_conntrack_l4proto *l4proto;
e77e6ff5 233 struct net *net = seq_file_net(s);
ea781f19 234 int ret = 0;
9fb9cbb1 235
c88130bc 236 NF_CT_ASSERT(ct);
ea781f19
ED
237 if (unlikely(!atomic_inc_not_zero(&ct->ct_general.use)))
238 return 0;
9fb9cbb1 239
58e207e4
FW
240 if (nf_ct_should_gc(ct)) {
241 nf_ct_kill(ct);
242 goto release;
243 }
244
9fb9cbb1
YK
245 /* we only want to print DIR_ORIGINAL */
246 if (NF_CT_DIRECTION(hash))
ea781f19 247 goto release;
9fb9cbb1 248
e77e6ff5
LZ
249 if (!net_eq(nf_ct_net(ct), net))
250 goto release;
251
5e8fbe2a 252 l3proto = __nf_ct_l3proto_find(nf_ct_l3num(ct));
9fb9cbb1 253 NF_CT_ASSERT(l3proto);
5e8fbe2a 254 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
605dcad6 255 NF_CT_ASSERT(l4proto);
9fb9cbb1 256
ea781f19 257 ret = -ENOSPC;
e71456ae 258 seq_printf(s, "%-8s %u %-8s %u %ld ",
a3134d53 259 l3proto_name(l3proto->l3proto), nf_ct_l3num(ct),
09ec82f5 260 l4proto_name(l4proto->l4proto), nf_ct_protonum(ct),
d0b35b93 261 nf_ct_expires(ct) / HZ);
9fb9cbb1 262
37246a58
SRRH
263 if (l4proto->print_conntrack)
264 l4proto->print_conntrack(s, ct);
9fb9cbb1 265
824f1fbe
JP
266 print_tuple(s, &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
267 l3proto, l4proto);
9fb9cbb1 268
deedb590
DB
269 ct_show_zone(s, ct, NF_CT_ZONE_DIR_ORIG);
270
e71456ae
SRRH
271 if (seq_has_overflowed(s))
272 goto release;
273
58401572 274 if (seq_print_acct(s, ct, IP_CT_DIR_ORIGINAL))
ea781f19 275 goto release;
9fb9cbb1 276
c88130bc 277 if (!(test_bit(IPS_SEEN_REPLY_BIT, &ct->status)))
cdec2685 278 seq_puts(s, "[UNREPLIED] ");
9fb9cbb1 279
824f1fbe
JP
280 print_tuple(s, &ct->tuplehash[IP_CT_DIR_REPLY].tuple,
281 l3proto, l4proto);
9fb9cbb1 282
deedb590
DB
283 ct_show_zone(s, ct, NF_CT_ZONE_DIR_REPL);
284
58401572 285 if (seq_print_acct(s, ct, IP_CT_DIR_REPLY))
ea781f19 286 goto release;
9fb9cbb1 287
c88130bc 288 if (test_bit(IPS_ASSURED_BIT, &ct->status))
cdec2685 289 seq_puts(s, "[ASSURED] ");
9fb9cbb1 290
e71456ae 291 if (seq_has_overflowed(s))
ea781f19 292 goto release;
e71456ae
SRRH
293
294#if defined(CONFIG_NF_CONNTRACK_MARK)
295 seq_printf(s, "mark=%u ", ct->mark);
9fb9cbb1
YK
296#endif
297
e71456ae 298 ct_show_secctx(s, ct);
deedb590 299 ct_show_zone(s, ct, NF_CT_DEFAULT_ZONE_DIR);
e71456ae
SRRH
300 ct_show_delta_time(s, ct);
301
302 seq_printf(s, "use=%u\n", atomic_read(&ct->ct_general.use));
a992ca2a 303
e71456ae 304 if (seq_has_overflowed(s))
ea781f19 305 goto release;
a5d29264 306
ea781f19
ED
307 ret = 0;
308release:
309 nf_ct_put(ct);
d88d7de0 310 return ret;
9fb9cbb1
YK
311}
312
56b3d975 313static const struct seq_operations ct_seq_ops = {
9fb9cbb1
YK
314 .start = ct_seq_start,
315 .next = ct_seq_next,
316 .stop = ct_seq_stop,
317 .show = ct_seq_show
318};
319
320static int ct_open(struct inode *inode, struct file *file)
321{
b2ce2c74 322 return seq_open_net(inode, file, &ct_seq_ops,
e2da5913 323 sizeof(struct ct_iter_state));
9fb9cbb1
YK
324}
325
da7071d7 326static const struct file_operations ct_file_ops = {
9fb9cbb1
YK
327 .owner = THIS_MODULE,
328 .open = ct_open,
329 .read = seq_read,
330 .llseek = seq_lseek,
b2ce2c74 331 .release = seq_release_net,
9fb9cbb1
YK
332};
333
9fb9cbb1
YK
334static void *ct_cpu_seq_start(struct seq_file *seq, loff_t *pos)
335{
8e9df801 336 struct net *net = seq_file_net(seq);
9fb9cbb1
YK
337 int cpu;
338
339 if (*pos == 0)
340 return SEQ_START_TOKEN;
341
0f23174a 342 for (cpu = *pos-1; cpu < nr_cpu_ids; ++cpu) {
9fb9cbb1
YK
343 if (!cpu_possible(cpu))
344 continue;
345 *pos = cpu + 1;
8e9df801 346 return per_cpu_ptr(net->ct.stat, cpu);
9fb9cbb1
YK
347 }
348
349 return NULL;
350}
351
352static void *ct_cpu_seq_next(struct seq_file *seq, void *v, loff_t *pos)
353{
8e9df801 354 struct net *net = seq_file_net(seq);
9fb9cbb1
YK
355 int cpu;
356
0f23174a 357 for (cpu = *pos; cpu < nr_cpu_ids; ++cpu) {
9fb9cbb1
YK
358 if (!cpu_possible(cpu))
359 continue;
360 *pos = cpu + 1;
8e9df801 361 return per_cpu_ptr(net->ct.stat, cpu);
9fb9cbb1
YK
362 }
363
364 return NULL;
365}
366
367static void ct_cpu_seq_stop(struct seq_file *seq, void *v)
368{
369}
370
371static int ct_cpu_seq_show(struct seq_file *seq, void *v)
372{
8e9df801
AD
373 struct net *net = seq_file_net(seq);
374 unsigned int nr_conntracks = atomic_read(&net->ct.count);
32948588 375 const struct ip_conntrack_stat *st = v;
9fb9cbb1
YK
376
377 if (v == SEQ_START_TOKEN) {
cdec2685 378 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");
9fb9cbb1
YK
379 return 0;
380 }
381
382 seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x %08x "
af740b2c 383 "%08x %08x %08x %08x %08x %08x %08x %08x %08x\n",
9fb9cbb1 384 nr_conntracks,
8e8118f8 385 0,
9fb9cbb1 386 st->found,
8e8118f8 387 0,
9fb9cbb1
YK
388 st->invalid,
389 st->ignore,
8e8118f8
FW
390 0,
391 0,
9fb9cbb1
YK
392 st->insert,
393 st->insert_failed,
394 st->drop,
395 st->early_drop,
396 st->error,
397
398 st->expect_new,
399 st->expect_create,
af740b2c
JDB
400 st->expect_delete,
401 st->search_restart
9fb9cbb1
YK
402 );
403 return 0;
404}
405
56b3d975 406static const struct seq_operations ct_cpu_seq_ops = {
9fb9cbb1
YK
407 .start = ct_cpu_seq_start,
408 .next = ct_cpu_seq_next,
409 .stop = ct_cpu_seq_stop,
410 .show = ct_cpu_seq_show,
411};
412
413static int ct_cpu_seq_open(struct inode *inode, struct file *file)
414{
8e9df801
AD
415 return seq_open_net(inode, file, &ct_cpu_seq_ops,
416 sizeof(struct seq_net_private));
9fb9cbb1
YK
417}
418
da7071d7 419static const struct file_operations ct_cpu_seq_fops = {
9fb9cbb1
YK
420 .owner = THIS_MODULE,
421 .open = ct_cpu_seq_open,
422 .read = seq_read,
423 .llseek = seq_lseek,
8e9df801 424 .release = seq_release_net,
9fb9cbb1 425};
b916f7d4 426
b2ce2c74 427static int nf_conntrack_standalone_init_proc(struct net *net)
b916f7d4
AD
428{
429 struct proc_dir_entry *pde;
f13f2aee
PW
430 kuid_t root_uid;
431 kgid_t root_gid;
b916f7d4 432
d4beaa66 433 pde = proc_create("nf_conntrack", 0440, net->proc_net, &ct_file_ops);
b916f7d4
AD
434 if (!pde)
435 goto out_nf_conntrack;
52c0e111 436
f13f2aee
PW
437 root_uid = make_kuid(net->user_ns, 0);
438 root_gid = make_kgid(net->user_ns, 0);
439 if (uid_valid(root_uid) && gid_valid(root_gid))
440 proc_set_user(pde, root_uid, root_gid);
441
b2ce2c74 442 pde = proc_create("nf_conntrack", S_IRUGO, net->proc_net_stat,
52c0e111 443 &ct_cpu_seq_fops);
b916f7d4
AD
444 if (!pde)
445 goto out_stat_nf_conntrack;
b916f7d4
AD
446 return 0;
447
448out_stat_nf_conntrack:
ece31ffd 449 remove_proc_entry("nf_conntrack", net->proc_net);
b916f7d4
AD
450out_nf_conntrack:
451 return -ENOMEM;
452}
453
b2ce2c74 454static void nf_conntrack_standalone_fini_proc(struct net *net)
b916f7d4 455{
b2ce2c74 456 remove_proc_entry("nf_conntrack", net->proc_net_stat);
ece31ffd 457 remove_proc_entry("nf_conntrack", net->proc_net);
b916f7d4
AD
458}
459#else
b2ce2c74 460static int nf_conntrack_standalone_init_proc(struct net *net)
b916f7d4
AD
461{
462 return 0;
463}
464
b2ce2c74 465static void nf_conntrack_standalone_fini_proc(struct net *net)
b916f7d4
AD
466{
467}
54b07dca 468#endif /* CONFIG_NF_CONNTRACK_PROCFS */
9fb9cbb1
YK
469
470/* Sysctl support */
471
472#ifdef CONFIG_SYSCTL
9fb9cbb1 473/* Log invalid packets of a given protocol */
3183ab89
FW
474static int log_invalid_proto_min __read_mostly;
475static int log_invalid_proto_max __read_mostly = 255;
476
477/* size the user *wants to set */
478static unsigned int nf_conntrack_htable_size_user __read_mostly;
479
480static int
481nf_conntrack_hash_sysctl(struct ctl_table *table, int write,
482 void __user *buffer, size_t *lenp, loff_t *ppos)
483{
484 int ret;
485
486 ret = proc_dointvec(table, write, buffer, lenp, ppos);
487 if (ret < 0 || !write)
488 return ret;
489
490 /* update ret, we might not be able to satisfy request */
491 ret = nf_conntrack_hash_resize(nf_conntrack_htable_size_user);
492
493 /* update it to the actual value used by conntrack */
494 nf_conntrack_htable_size_user = nf_conntrack_htable_size;
495 return ret;
496}
9fb9cbb1 497
9714be7d 498static struct ctl_table_header *nf_ct_netfilter_header;
9fb9cbb1 499
fe2c6338 500static struct ctl_table nf_ct_sysctl_table[] = {
9fb9cbb1 501 {
9fb9cbb1
YK
502 .procname = "nf_conntrack_max",
503 .data = &nf_conntrack_max,
504 .maxlen = sizeof(int),
505 .mode = 0644,
6d9f239a 506 .proc_handler = proc_dointvec,
9fb9cbb1
YK
507 },
508 {
9fb9cbb1 509 .procname = "nf_conntrack_count",
49ac8713 510 .data = &init_net.ct.count,
9fb9cbb1
YK
511 .maxlen = sizeof(int),
512 .mode = 0444,
6d9f239a 513 .proc_handler = proc_dointvec,
9fb9cbb1
YK
514 },
515 {
9fb9cbb1 516 .procname = "nf_conntrack_buckets",
3183ab89 517 .data = &nf_conntrack_htable_size_user,
9fb9cbb1 518 .maxlen = sizeof(unsigned int),
3183ab89
FW
519 .mode = 0644,
520 .proc_handler = nf_conntrack_hash_sysctl,
9fb9cbb1 521 },
39a27a35 522 {
39a27a35 523 .procname = "nf_conntrack_checksum",
c04d0552 524 .data = &init_net.ct.sysctl_checksum,
39a27a35
PM
525 .maxlen = sizeof(unsigned int),
526 .mode = 0644,
6d9f239a 527 .proc_handler = proc_dointvec,
39a27a35 528 },
9fb9cbb1 529 {
9fb9cbb1 530 .procname = "nf_conntrack_log_invalid",
c2a2c7e0 531 .data = &init_net.ct.sysctl_log_invalid,
9fb9cbb1
YK
532 .maxlen = sizeof(unsigned int),
533 .mode = 0644,
6d9f239a 534 .proc_handler = proc_dointvec_minmax,
9fb9cbb1
YK
535 .extra1 = &log_invalid_proto_min,
536 .extra2 = &log_invalid_proto_max,
537 },
f264a7df 538 {
f264a7df
PM
539 .procname = "nf_conntrack_expect_max",
540 .data = &nf_ct_expect_max,
541 .maxlen = sizeof(int),
542 .mode = 0644,
6d9f239a 543 .proc_handler = proc_dointvec,
f264a7df 544 },
f8572d8f 545 { }
9fb9cbb1
YK
546};
547
fe2c6338 548static struct ctl_table nf_ct_netfilter_table[] = {
9fb9cbb1 549 {
9fb9cbb1
YK
550 .procname = "nf_conntrack_max",
551 .data = &nf_conntrack_max,
552 .maxlen = sizeof(int),
553 .mode = 0644,
6d9f239a 554 .proc_handler = proc_dointvec,
9fb9cbb1 555 },
f8572d8f 556 { }
9fb9cbb1
YK
557};
558
80250707 559static int nf_conntrack_standalone_init_sysctl(struct net *net)
b916f7d4 560{
80250707
AD
561 struct ctl_table *table;
562
80250707
AD
563 table = kmemdup(nf_ct_sysctl_table, sizeof(nf_ct_sysctl_table),
564 GFP_KERNEL);
565 if (!table)
566 goto out_kmemdup;
567
568 table[1].data = &net->ct.count;
c04d0552 569 table[3].data = &net->ct.sysctl_checksum;
c2a2c7e0 570 table[4].data = &net->ct.sysctl_log_invalid;
80250707 571
464dc801
EB
572 /* Don't export sysctls to unprivileged users */
573 if (net->user_ns != &init_user_ns)
574 table[0].procname = NULL;
575
3183ab89
FW
576 if (!net_eq(&init_net, net))
577 table[2].mode = 0444;
578
ec8f23ce 579 net->ct.sysctl_header = register_net_sysctl(net, "net/netfilter", table);
80250707 580 if (!net->ct.sysctl_header)
9714be7d
KPO
581 goto out_unregister_netfilter;
582
b916f7d4
AD
583 return 0;
584
9714be7d 585out_unregister_netfilter:
80250707
AD
586 kfree(table);
587out_kmemdup:
9714be7d 588 return -ENOMEM;
b916f7d4
AD
589}
590
80250707 591static void nf_conntrack_standalone_fini_sysctl(struct net *net)
b916f7d4 592{
80250707
AD
593 struct ctl_table *table;
594
80250707
AD
595 table = net->ct.sysctl_header->ctl_table_arg;
596 unregister_net_sysctl_table(net->ct.sysctl_header);
597 kfree(table);
b916f7d4
AD
598}
599#else
80250707 600static int nf_conntrack_standalone_init_sysctl(struct net *net)
b916f7d4
AD
601{
602 return 0;
603}
604
80250707 605static void nf_conntrack_standalone_fini_sysctl(struct net *net)
b916f7d4
AD
606{
607}
9fb9cbb1
YK
608#endif /* CONFIG_SYSCTL */
609
f94161c1 610static int nf_conntrack_pernet_init(struct net *net)
dfdb8d79 611{
b2ce2c74
AD
612 int ret;
613
f94161c1 614 ret = nf_conntrack_init_net(net);
b2ce2c74
AD
615 if (ret < 0)
616 goto out_init;
f94161c1 617
b2ce2c74
AD
618 ret = nf_conntrack_standalone_init_proc(net);
619 if (ret < 0)
620 goto out_proc;
f94161c1 621
c04d0552 622 net->ct.sysctl_checksum = 1;
c2a2c7e0 623 net->ct.sysctl_log_invalid = 0;
80250707
AD
624 ret = nf_conntrack_standalone_init_sysctl(net);
625 if (ret < 0)
626 goto out_sysctl;
f94161c1 627
b2ce2c74
AD
628 return 0;
629
80250707
AD
630out_sysctl:
631 nf_conntrack_standalone_fini_proc(net);
b2ce2c74 632out_proc:
f94161c1 633 nf_conntrack_cleanup_net(net);
b2ce2c74
AD
634out_init:
635 return ret;
dfdb8d79
AD
636}
637
dece40e8 638static void nf_conntrack_pernet_exit(struct list_head *net_exit_list)
dfdb8d79 639{
dece40e8
VD
640 struct net *net;
641
642 list_for_each_entry(net, net_exit_list, exit_list) {
643 nf_conntrack_standalone_fini_sysctl(net);
644 nf_conntrack_standalone_fini_proc(net);
645 }
646 nf_conntrack_cleanup_net_list(net_exit_list);
dfdb8d79
AD
647}
648
649static struct pernet_operations nf_conntrack_net_ops = {
dece40e8
VD
650 .init = nf_conntrack_pernet_init,
651 .exit_batch = nf_conntrack_pernet_exit,
dfdb8d79
AD
652};
653
65b4b4e8 654static int __init nf_conntrack_standalone_init(void)
9fb9cbb1 655{
f94161c1
G
656 int ret = nf_conntrack_init_start();
657 if (ret < 0)
658 goto out_start;
659
a9e419dc
FW
660 BUILD_BUG_ON(SKB_NFCT_PTRMASK != NFCT_PTRMASK);
661 BUILD_BUG_ON(NFCT_INFOMASK <= IP_CT_NUMBER);
662
5f9f946b 663#ifdef CONFIG_SYSCTL
f94161c1
G
664 nf_ct_netfilter_header =
665 register_net_sysctl(&init_net, "net", nf_ct_netfilter_table);
5f9f946b
PNA
666 if (!nf_ct_netfilter_header) {
667 pr_err("nf_conntrack: can't register to sysctl.\n");
5389090b 668 ret = -ENOMEM;
f94161c1 669 goto out_sysctl;
5f9f946b 670 }
3183ab89
FW
671
672 nf_conntrack_htable_size_user = nf_conntrack_htable_size;
5f9f946b 673#endif
f94161c1
G
674
675 ret = register_pernet_subsys(&nf_conntrack_net_ops);
676 if (ret < 0)
677 goto out_pernet;
678
679 nf_conntrack_init_end();
680 return 0;
681
682out_pernet:
5f9f946b 683#ifdef CONFIG_SYSCTL
f94161c1
G
684 unregister_net_sysctl_table(nf_ct_netfilter_header);
685out_sysctl:
5f9f946b 686#endif
f94161c1
G
687 nf_conntrack_cleanup_end();
688out_start:
689 return ret;
9fb9cbb1
YK
690}
691
65b4b4e8 692static void __exit nf_conntrack_standalone_fini(void)
9fb9cbb1 693{
f94161c1 694 nf_conntrack_cleanup_start();
dfdb8d79 695 unregister_pernet_subsys(&nf_conntrack_net_ops);
5f9f946b 696#ifdef CONFIG_SYSCTL
f94161c1 697 unregister_net_sysctl_table(nf_ct_netfilter_header);
5f9f946b 698#endif
1e47ee83 699 nf_conntrack_cleanup_end();
9fb9cbb1
YK
700}
701
65b4b4e8
AM
702module_init(nf_conntrack_standalone_init);
703module_exit(nf_conntrack_standalone_fini);
9fb9cbb1
YK
704
705/* Some modules need us, but don't depend directly on any symbol.
706 They should call this. */
2e4e6a17 707void need_conntrack(void)
9fb9cbb1
YK
708{
709}
13b18339 710EXPORT_SYMBOL_GPL(need_conntrack);