Merge tag 'for-4.21/block-20190102' of git://git.kernel.dk/linux-block
[linux] / net / netfilter / nf_conntrack_standalone.c
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 enum nf_ct_sysctl_index {
536         NF_SYSCTL_CT_MAX,
537         NF_SYSCTL_CT_COUNT,
538         NF_SYSCTL_CT_BUCKETS,
539         NF_SYSCTL_CT_CHECKSUM,
540         NF_SYSCTL_CT_LOG_INVALID,
541         NF_SYSCTL_CT_EXPECT_MAX,
542         NF_SYSCTL_CT_ACCT,
543         NF_SYSCTL_CT_HELPER,
544 #ifdef CONFIG_NF_CONNTRACK_EVENTS
545         NF_SYSCTL_CT_EVENTS,
546 #endif
547 #ifdef CONFIG_NF_CONNTRACK_TIMESTAMP
548         NF_SYSCTL_CT_TIMESTAMP,
549 #endif
550 };
551
552 static struct ctl_table nf_ct_sysctl_table[] = {
553         [NF_SYSCTL_CT_MAX] = {
554                 .procname       = "nf_conntrack_max",
555                 .data           = &nf_conntrack_max,
556                 .maxlen         = sizeof(int),
557                 .mode           = 0644,
558                 .proc_handler   = proc_dointvec,
559         },
560         [NF_SYSCTL_CT_COUNT] = {
561                 .procname       = "nf_conntrack_count",
562                 .data           = &init_net.ct.count,
563                 .maxlen         = sizeof(int),
564                 .mode           = 0444,
565                 .proc_handler   = proc_dointvec,
566         },
567         [NF_SYSCTL_CT_BUCKETS] = {
568                 .procname       = "nf_conntrack_buckets",
569                 .data           = &nf_conntrack_htable_size_user,
570                 .maxlen         = sizeof(unsigned int),
571                 .mode           = 0644,
572                 .proc_handler   = nf_conntrack_hash_sysctl,
573         },
574         [NF_SYSCTL_CT_CHECKSUM] = {
575                 .procname       = "nf_conntrack_checksum",
576                 .data           = &init_net.ct.sysctl_checksum,
577                 .maxlen         = sizeof(unsigned int),
578                 .mode           = 0644,
579                 .proc_handler   = proc_dointvec,
580         },
581         [NF_SYSCTL_CT_LOG_INVALID] = {
582                 .procname       = "nf_conntrack_log_invalid",
583                 .data           = &init_net.ct.sysctl_log_invalid,
584                 .maxlen         = sizeof(unsigned int),
585                 .mode           = 0644,
586                 .proc_handler   = proc_dointvec_minmax,
587                 .extra1         = &log_invalid_proto_min,
588                 .extra2         = &log_invalid_proto_max,
589         },
590         [NF_SYSCTL_CT_EXPECT_MAX] = {
591                 .procname       = "nf_conntrack_expect_max",
592                 .data           = &nf_ct_expect_max,
593                 .maxlen         = sizeof(int),
594                 .mode           = 0644,
595                 .proc_handler   = proc_dointvec,
596         },
597         [NF_SYSCTL_CT_ACCT] = {
598                 .procname       = "nf_conntrack_acct",
599                 .data           = &init_net.ct.sysctl_acct,
600                 .maxlen         = sizeof(unsigned int),
601                 .mode           = 0644,
602                 .proc_handler   = proc_dointvec,
603         },
604         [NF_SYSCTL_CT_HELPER] = {
605                 .procname       = "nf_conntrack_helper",
606                 .data           = &init_net.ct.sysctl_auto_assign_helper,
607                 .maxlen         = sizeof(unsigned int),
608                 .mode           = 0644,
609                 .proc_handler   = proc_dointvec,
610         },
611 #ifdef CONFIG_NF_CONNTRACK_EVENTS
612         [NF_SYSCTL_CT_EVENTS] = {
613                 .procname       = "nf_conntrack_events",
614                 .data           = &init_net.ct.sysctl_events,
615                 .maxlen         = sizeof(unsigned int),
616                 .mode           = 0644,
617                 .proc_handler   = proc_dointvec,
618         },
619 #endif
620 #ifdef CONFIG_NF_CONNTRACK_TIMESTAMP
621         [NF_SYSCTL_CT_TIMESTAMP] = {
622                 .procname       = "nf_conntrack_timestamp",
623                 .data           = &init_net.ct.sysctl_tstamp,
624                 .maxlen         = sizeof(unsigned int),
625                 .mode           = 0644,
626                 .proc_handler   = proc_dointvec,
627         },
628 #endif
629         { }
630 };
631
632 static struct ctl_table nf_ct_netfilter_table[] = {
633         {
634                 .procname       = "nf_conntrack_max",
635                 .data           = &nf_conntrack_max,
636                 .maxlen         = sizeof(int),
637                 .mode           = 0644,
638                 .proc_handler   = proc_dointvec,
639         },
640         { }
641 };
642
643 static int nf_conntrack_standalone_init_sysctl(struct net *net)
644 {
645         struct ctl_table *table;
646
647         table = kmemdup(nf_ct_sysctl_table, sizeof(nf_ct_sysctl_table),
648                         GFP_KERNEL);
649         if (!table)
650                 goto out_kmemdup;
651
652         table[NF_SYSCTL_CT_COUNT].data = &net->ct.count;
653         table[NF_SYSCTL_CT_CHECKSUM].data = &net->ct.sysctl_checksum;
654         table[NF_SYSCTL_CT_LOG_INVALID].data = &net->ct.sysctl_log_invalid;
655 #ifdef CONFIG_NF_CONNTRACK_EVENTS
656         table[NF_SYSCTL_CT_EVENTS].data = &net->ct.sysctl_events;
657 #endif
658
659         /* Don't export sysctls to unprivileged users */
660         if (net->user_ns != &init_user_ns) {
661                 table[NF_SYSCTL_CT_MAX].procname = NULL;
662                 table[NF_SYSCTL_CT_ACCT].procname = NULL;
663                 table[NF_SYSCTL_CT_HELPER].procname = NULL;
664 #ifdef CONFIG_NF_CONNTRACK_TIMESTAMP
665                 table[NF_SYSCTL_CT_TIMESTAMP].procname = NULL;
666 #endif
667 #ifdef CONFIG_NF_CONNTRACK_EVENTS
668                 table[NF_SYSCTL_CT_EVENTS].procname = NULL;
669 #endif
670         }
671
672         if (!net_eq(&init_net, net))
673                 table[NF_SYSCTL_CT_BUCKETS].mode = 0444;
674
675         net->ct.sysctl_header = register_net_sysctl(net, "net/netfilter", table);
676         if (!net->ct.sysctl_header)
677                 goto out_unregister_netfilter;
678
679         return 0;
680
681 out_unregister_netfilter:
682         kfree(table);
683 out_kmemdup:
684         return -ENOMEM;
685 }
686
687 static void nf_conntrack_standalone_fini_sysctl(struct net *net)
688 {
689         struct ctl_table *table;
690
691         table = net->ct.sysctl_header->ctl_table_arg;
692         unregister_net_sysctl_table(net->ct.sysctl_header);
693         kfree(table);
694 }
695 #else
696 static int nf_conntrack_standalone_init_sysctl(struct net *net)
697 {
698         return 0;
699 }
700
701 static void nf_conntrack_standalone_fini_sysctl(struct net *net)
702 {
703 }
704 #endif /* CONFIG_SYSCTL */
705
706 static int nf_conntrack_pernet_init(struct net *net)
707 {
708         int ret;
709
710         ret = nf_conntrack_init_net(net);
711         if (ret < 0)
712                 goto out_init;
713
714         ret = nf_conntrack_standalone_init_proc(net);
715         if (ret < 0)
716                 goto out_proc;
717
718         net->ct.sysctl_checksum = 1;
719         net->ct.sysctl_log_invalid = 0;
720         ret = nf_conntrack_standalone_init_sysctl(net);
721         if (ret < 0)
722                 goto out_sysctl;
723
724         return 0;
725
726 out_sysctl:
727         nf_conntrack_standalone_fini_proc(net);
728 out_proc:
729         nf_conntrack_cleanup_net(net);
730 out_init:
731         return ret;
732 }
733
734 static void nf_conntrack_pernet_exit(struct list_head *net_exit_list)
735 {
736         struct net *net;
737
738         list_for_each_entry(net, net_exit_list, exit_list) {
739                 nf_conntrack_standalone_fini_sysctl(net);
740                 nf_conntrack_standalone_fini_proc(net);
741         }
742         nf_conntrack_cleanup_net_list(net_exit_list);
743 }
744
745 static struct pernet_operations nf_conntrack_net_ops = {
746         .init           = nf_conntrack_pernet_init,
747         .exit_batch     = nf_conntrack_pernet_exit,
748         .id             = &nf_conntrack_net_id,
749         .size = sizeof(struct nf_conntrack_net),
750 };
751
752 static int __init nf_conntrack_standalone_init(void)
753 {
754         int ret = nf_conntrack_init_start();
755         if (ret < 0)
756                 goto out_start;
757
758         BUILD_BUG_ON(SKB_NFCT_PTRMASK != NFCT_PTRMASK);
759         BUILD_BUG_ON(NFCT_INFOMASK <= IP_CT_NUMBER);
760
761 #ifdef CONFIG_SYSCTL
762         nf_ct_netfilter_header =
763                 register_net_sysctl(&init_net, "net", nf_ct_netfilter_table);
764         if (!nf_ct_netfilter_header) {
765                 pr_err("nf_conntrack: can't register to sysctl.\n");
766                 ret = -ENOMEM;
767                 goto out_sysctl;
768         }
769
770         nf_conntrack_htable_size_user = nf_conntrack_htable_size;
771 #endif
772
773         ret = register_pernet_subsys(&nf_conntrack_net_ops);
774         if (ret < 0)
775                 goto out_pernet;
776
777         nf_conntrack_init_end();
778         return 0;
779
780 out_pernet:
781 #ifdef CONFIG_SYSCTL
782         unregister_net_sysctl_table(nf_ct_netfilter_header);
783 out_sysctl:
784 #endif
785         nf_conntrack_cleanup_end();
786 out_start:
787         return ret;
788 }
789
790 static void __exit nf_conntrack_standalone_fini(void)
791 {
792         nf_conntrack_cleanup_start();
793         unregister_pernet_subsys(&nf_conntrack_net_ops);
794 #ifdef CONFIG_SYSCTL
795         unregister_net_sysctl_table(nf_ct_netfilter_header);
796 #endif
797         nf_conntrack_cleanup_end();
798 }
799
800 module_init(nf_conntrack_standalone_init);
801 module_exit(nf_conntrack_standalone_fini);