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