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