netns xfrm: fix "ip xfrm state|policy count" misreport
[safe/jmp/linux-2.6] / net / netfilter / nf_conntrack_standalone.c
1 /* (C) 1999-2001 Paul `Rusty' Russell
2  * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  */
8
9 #include <linux/types.h>
10 #include <linux/netfilter.h>
11 #include <linux/module.h>
12 #include <linux/skbuff.h>
13 #include <linux/proc_fs.h>
14 #include <linux/seq_file.h>
15 #include <linux/percpu.h>
16 #include <linux/netdevice.h>
17 #include <net/net_namespace.h>
18 #ifdef CONFIG_SYSCTL
19 #include <linux/sysctl.h>
20 #endif
21
22 #include <net/netfilter/nf_conntrack.h>
23 #include <net/netfilter/nf_conntrack_core.h>
24 #include <net/netfilter/nf_conntrack_l3proto.h>
25 #include <net/netfilter/nf_conntrack_l4proto.h>
26 #include <net/netfilter/nf_conntrack_expect.h>
27 #include <net/netfilter/nf_conntrack_helper.h>
28 #include <net/netfilter/nf_conntrack_acct.h>
29
30 MODULE_LICENSE("GPL");
31
32 #ifdef CONFIG_PROC_FS
33 int
34 print_tuple(struct seq_file *s, const struct nf_conntrack_tuple *tuple,
35             const struct nf_conntrack_l3proto *l3proto,
36             const struct nf_conntrack_l4proto *l4proto)
37 {
38         return l3proto->print_tuple(s, tuple) || l4proto->print_tuple(s, tuple);
39 }
40 EXPORT_SYMBOL_GPL(print_tuple);
41
42 struct ct_iter_state {
43         struct seq_net_private p;
44         unsigned int bucket;
45 };
46
47 static struct hlist_nulls_node *ct_get_first(struct seq_file *seq)
48 {
49         struct net *net = seq_file_net(seq);
50         struct ct_iter_state *st = seq->private;
51         struct hlist_nulls_node *n;
52
53         for (st->bucket = 0;
54              st->bucket < nf_conntrack_htable_size;
55              st->bucket++) {
56                 n = rcu_dereference(net->ct.hash[st->bucket].first);
57                 if (!is_a_nulls(n))
58                         return n;
59         }
60         return NULL;
61 }
62
63 static struct hlist_nulls_node *ct_get_next(struct seq_file *seq,
64                                       struct hlist_nulls_node *head)
65 {
66         struct net *net = seq_file_net(seq);
67         struct ct_iter_state *st = seq->private;
68
69         head = rcu_dereference(head->next);
70         while (is_a_nulls(head)) {
71                 if (likely(get_nulls_value(head) == st->bucket)) {
72                         if (++st->bucket >= nf_conntrack_htable_size)
73                                 return NULL;
74                 }
75                 head = rcu_dereference(net->ct.hash[st->bucket].first);
76         }
77         return head;
78 }
79
80 static struct hlist_nulls_node *ct_get_idx(struct seq_file *seq, loff_t pos)
81 {
82         struct hlist_nulls_node *head = ct_get_first(seq);
83
84         if (head)
85                 while (pos && (head = ct_get_next(seq, head)))
86                         pos--;
87         return pos ? NULL : head;
88 }
89
90 static void *ct_seq_start(struct seq_file *seq, loff_t *pos)
91         __acquires(RCU)
92 {
93         rcu_read_lock();
94         return ct_get_idx(seq, *pos);
95 }
96
97 static void *ct_seq_next(struct seq_file *s, void *v, loff_t *pos)
98 {
99         (*pos)++;
100         return ct_get_next(s, v);
101 }
102
103 static void ct_seq_stop(struct seq_file *s, void *v)
104         __releases(RCU)
105 {
106         rcu_read_unlock();
107 }
108
109 /* return 0 on success, 1 in case of error */
110 static int ct_seq_show(struct seq_file *s, void *v)
111 {
112         struct nf_conntrack_tuple_hash *hash = v;
113         struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(hash);
114         const struct nf_conntrack_l3proto *l3proto;
115         const struct nf_conntrack_l4proto *l4proto;
116         int ret = 0;
117
118         NF_CT_ASSERT(ct);
119         if (unlikely(!atomic_inc_not_zero(&ct->ct_general.use)))
120                 return 0;
121
122         /* we only want to print DIR_ORIGINAL */
123         if (NF_CT_DIRECTION(hash))
124                 goto release;
125
126         l3proto = __nf_ct_l3proto_find(nf_ct_l3num(ct));
127         NF_CT_ASSERT(l3proto);
128         l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
129         NF_CT_ASSERT(l4proto);
130
131         ret = -ENOSPC;
132         if (seq_printf(s, "%-8s %u %-8s %u %ld ",
133                        l3proto->name, nf_ct_l3num(ct),
134                        l4proto->name, nf_ct_protonum(ct),
135                        timer_pending(&ct->timeout)
136                        ? (long)(ct->timeout.expires - jiffies)/HZ : 0) != 0)
137                 goto release;
138
139         if (l4proto->print_conntrack && l4proto->print_conntrack(s, ct))
140                 goto release;
141
142         if (print_tuple(s, &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
143                         l3proto, l4proto))
144                 goto release;
145
146         if (seq_print_acct(s, ct, IP_CT_DIR_ORIGINAL))
147                 goto release;
148
149         if (!(test_bit(IPS_SEEN_REPLY_BIT, &ct->status)))
150                 if (seq_printf(s, "[UNREPLIED] "))
151                         goto release;
152
153         if (print_tuple(s, &ct->tuplehash[IP_CT_DIR_REPLY].tuple,
154                         l3proto, l4proto))
155                 goto release;
156
157         if (seq_print_acct(s, ct, IP_CT_DIR_REPLY))
158                 goto release;
159
160         if (test_bit(IPS_ASSURED_BIT, &ct->status))
161                 if (seq_printf(s, "[ASSURED] "))
162                         goto release;
163
164 #if defined(CONFIG_NF_CONNTRACK_MARK)
165         if (seq_printf(s, "mark=%u ", ct->mark))
166                 goto release;
167 #endif
168
169 #ifdef CONFIG_NF_CONNTRACK_SECMARK
170         if (seq_printf(s, "secmark=%u ", ct->secmark))
171                 goto release;
172 #endif
173
174         if (seq_printf(s, "use=%u\n", atomic_read(&ct->ct_general.use)))
175                 goto release;
176
177         ret = 0;
178 release:
179         nf_ct_put(ct);
180         return 0;
181 }
182
183 static const struct seq_operations ct_seq_ops = {
184         .start = ct_seq_start,
185         .next  = ct_seq_next,
186         .stop  = ct_seq_stop,
187         .show  = ct_seq_show
188 };
189
190 static int ct_open(struct inode *inode, struct file *file)
191 {
192         return seq_open_net(inode, file, &ct_seq_ops,
193                         sizeof(struct ct_iter_state));
194 }
195
196 static const struct file_operations ct_file_ops = {
197         .owner   = THIS_MODULE,
198         .open    = ct_open,
199         .read    = seq_read,
200         .llseek  = seq_lseek,
201         .release = seq_release_net,
202 };
203
204 static void *ct_cpu_seq_start(struct seq_file *seq, loff_t *pos)
205 {
206         struct net *net = seq_file_net(seq);
207         int cpu;
208
209         if (*pos == 0)
210                 return SEQ_START_TOKEN;
211
212         for (cpu = *pos-1; cpu < nr_cpu_ids; ++cpu) {
213                 if (!cpu_possible(cpu))
214                         continue;
215                 *pos = cpu + 1;
216                 return per_cpu_ptr(net->ct.stat, cpu);
217         }
218
219         return NULL;
220 }
221
222 static void *ct_cpu_seq_next(struct seq_file *seq, void *v, loff_t *pos)
223 {
224         struct net *net = seq_file_net(seq);
225         int cpu;
226
227         for (cpu = *pos; cpu < nr_cpu_ids; ++cpu) {
228                 if (!cpu_possible(cpu))
229                         continue;
230                 *pos = cpu + 1;
231                 return per_cpu_ptr(net->ct.stat, cpu);
232         }
233
234         return NULL;
235 }
236
237 static void ct_cpu_seq_stop(struct seq_file *seq, void *v)
238 {
239 }
240
241 static int ct_cpu_seq_show(struct seq_file *seq, void *v)
242 {
243         struct net *net = seq_file_net(seq);
244         unsigned int nr_conntracks = atomic_read(&net->ct.count);
245         const struct ip_conntrack_stat *st = v;
246
247         if (v == SEQ_START_TOKEN) {
248                 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\n");
249                 return 0;
250         }
251
252         seq_printf(seq, "%08x  %08x %08x %08x %08x %08x %08x %08x "
253                         "%08x %08x %08x %08x %08x  %08x %08x %08x \n",
254                    nr_conntracks,
255                    st->searched,
256                    st->found,
257                    st->new,
258                    st->invalid,
259                    st->ignore,
260                    st->delete,
261                    st->delete_list,
262                    st->insert,
263                    st->insert_failed,
264                    st->drop,
265                    st->early_drop,
266                    st->error,
267
268                    st->expect_new,
269                    st->expect_create,
270                    st->expect_delete
271                 );
272         return 0;
273 }
274
275 static const struct seq_operations ct_cpu_seq_ops = {
276         .start  = ct_cpu_seq_start,
277         .next   = ct_cpu_seq_next,
278         .stop   = ct_cpu_seq_stop,
279         .show   = ct_cpu_seq_show,
280 };
281
282 static int ct_cpu_seq_open(struct inode *inode, struct file *file)
283 {
284         return seq_open_net(inode, file, &ct_cpu_seq_ops,
285                             sizeof(struct seq_net_private));
286 }
287
288 static const struct file_operations ct_cpu_seq_fops = {
289         .owner   = THIS_MODULE,
290         .open    = ct_cpu_seq_open,
291         .read    = seq_read,
292         .llseek  = seq_lseek,
293         .release = seq_release_net,
294 };
295
296 static int nf_conntrack_standalone_init_proc(struct net *net)
297 {
298         struct proc_dir_entry *pde;
299
300         pde = proc_net_fops_create(net, "nf_conntrack", 0440, &ct_file_ops);
301         if (!pde)
302                 goto out_nf_conntrack;
303
304         pde = proc_create("nf_conntrack", S_IRUGO, net->proc_net_stat,
305                           &ct_cpu_seq_fops);
306         if (!pde)
307                 goto out_stat_nf_conntrack;
308         return 0;
309
310 out_stat_nf_conntrack:
311         proc_net_remove(net, "nf_conntrack");
312 out_nf_conntrack:
313         return -ENOMEM;
314 }
315
316 static void nf_conntrack_standalone_fini_proc(struct net *net)
317 {
318         remove_proc_entry("nf_conntrack", net->proc_net_stat);
319         proc_net_remove(net, "nf_conntrack");
320 }
321 #else
322 static int nf_conntrack_standalone_init_proc(struct net *net)
323 {
324         return 0;
325 }
326
327 static void nf_conntrack_standalone_fini_proc(struct net *net)
328 {
329 }
330 #endif /* CONFIG_PROC_FS */
331
332 /* Sysctl support */
333
334 #ifdef CONFIG_SYSCTL
335 /* Log invalid packets of a given protocol */
336 static int log_invalid_proto_min = 0;
337 static int log_invalid_proto_max = 255;
338
339 static struct ctl_table_header *nf_ct_netfilter_header;
340
341 static ctl_table nf_ct_sysctl_table[] = {
342         {
343                 .procname       = "nf_conntrack_max",
344                 .data           = &nf_conntrack_max,
345                 .maxlen         = sizeof(int),
346                 .mode           = 0644,
347                 .proc_handler   = proc_dointvec,
348         },
349         {
350                 .procname       = "nf_conntrack_count",
351                 .data           = &init_net.ct.count,
352                 .maxlen         = sizeof(int),
353                 .mode           = 0444,
354                 .proc_handler   = proc_dointvec,
355         },
356         {
357                 .procname       = "nf_conntrack_buckets",
358                 .data           = &nf_conntrack_htable_size,
359                 .maxlen         = sizeof(unsigned int),
360                 .mode           = 0444,
361                 .proc_handler   = proc_dointvec,
362         },
363         {
364                 .procname       = "nf_conntrack_checksum",
365                 .data           = &init_net.ct.sysctl_checksum,
366                 .maxlen         = sizeof(unsigned int),
367                 .mode           = 0644,
368                 .proc_handler   = proc_dointvec,
369         },
370         {
371                 .procname       = "nf_conntrack_log_invalid",
372                 .data           = &init_net.ct.sysctl_log_invalid,
373                 .maxlen         = sizeof(unsigned int),
374                 .mode           = 0644,
375                 .proc_handler   = proc_dointvec_minmax,
376                 .extra1         = &log_invalid_proto_min,
377                 .extra2         = &log_invalid_proto_max,
378         },
379         {
380                 .procname       = "nf_conntrack_expect_max",
381                 .data           = &nf_ct_expect_max,
382                 .maxlen         = sizeof(int),
383                 .mode           = 0644,
384                 .proc_handler   = proc_dointvec,
385         },
386         { }
387 };
388
389 #define NET_NF_CONNTRACK_MAX 2089
390
391 static ctl_table nf_ct_netfilter_table[] = {
392         {
393                 .procname       = "nf_conntrack_max",
394                 .data           = &nf_conntrack_max,
395                 .maxlen         = sizeof(int),
396                 .mode           = 0644,
397                 .proc_handler   = proc_dointvec,
398         },
399         { }
400 };
401
402 static struct ctl_path nf_ct_path[] = {
403         { .procname = "net", },
404         { }
405 };
406
407 static int nf_conntrack_standalone_init_sysctl(struct net *net)
408 {
409         struct ctl_table *table;
410
411         if (net_eq(net, &init_net)) {
412                 nf_ct_netfilter_header =
413                        register_sysctl_paths(nf_ct_path, nf_ct_netfilter_table);
414                 if (!nf_ct_netfilter_header)
415                         goto out;
416         }
417
418         table = kmemdup(nf_ct_sysctl_table, sizeof(nf_ct_sysctl_table),
419                         GFP_KERNEL);
420         if (!table)
421                 goto out_kmemdup;
422
423         table[1].data = &net->ct.count;
424         table[3].data = &net->ct.sysctl_checksum;
425         table[4].data = &net->ct.sysctl_log_invalid;
426
427         net->ct.sysctl_header = register_net_sysctl_table(net,
428                                         nf_net_netfilter_sysctl_path, table);
429         if (!net->ct.sysctl_header)
430                 goto out_unregister_netfilter;
431
432         return 0;
433
434 out_unregister_netfilter:
435         kfree(table);
436 out_kmemdup:
437         if (net_eq(net, &init_net))
438                 unregister_sysctl_table(nf_ct_netfilter_header);
439 out:
440         printk("nf_conntrack: can't register to sysctl.\n");
441         return -ENOMEM;
442 }
443
444 static void nf_conntrack_standalone_fini_sysctl(struct net *net)
445 {
446         struct ctl_table *table;
447
448         if (net_eq(net, &init_net))
449                 unregister_sysctl_table(nf_ct_netfilter_header);
450         table = net->ct.sysctl_header->ctl_table_arg;
451         unregister_net_sysctl_table(net->ct.sysctl_header);
452         kfree(table);
453 }
454 #else
455 static int nf_conntrack_standalone_init_sysctl(struct net *net)
456 {
457         return 0;
458 }
459
460 static void nf_conntrack_standalone_fini_sysctl(struct net *net)
461 {
462 }
463 #endif /* CONFIG_SYSCTL */
464
465 static int nf_conntrack_net_init(struct net *net)
466 {
467         int ret;
468
469         ret = nf_conntrack_init(net);
470         if (ret < 0)
471                 goto out_init;
472         ret = nf_conntrack_standalone_init_proc(net);
473         if (ret < 0)
474                 goto out_proc;
475         net->ct.sysctl_checksum = 1;
476         net->ct.sysctl_log_invalid = 0;
477         ret = nf_conntrack_standalone_init_sysctl(net);
478         if (ret < 0)
479                 goto out_sysctl;
480         return 0;
481
482 out_sysctl:
483         nf_conntrack_standalone_fini_proc(net);
484 out_proc:
485         nf_conntrack_cleanup(net);
486 out_init:
487         return ret;
488 }
489
490 static void nf_conntrack_net_exit(struct net *net)
491 {
492         nf_conntrack_standalone_fini_sysctl(net);
493         nf_conntrack_standalone_fini_proc(net);
494         nf_conntrack_cleanup(net);
495 }
496
497 static struct pernet_operations nf_conntrack_net_ops = {
498         .init = nf_conntrack_net_init,
499         .exit = nf_conntrack_net_exit,
500 };
501
502 static int __init nf_conntrack_standalone_init(void)
503 {
504         return register_pernet_subsys(&nf_conntrack_net_ops);
505 }
506
507 static void __exit nf_conntrack_standalone_fini(void)
508 {
509         unregister_pernet_subsys(&nf_conntrack_net_ops);
510 }
511
512 module_init(nf_conntrack_standalone_init);
513 module_exit(nf_conntrack_standalone_fini);
514
515 /* Some modules need us, but don't depend directly on any symbol.
516    They should call this. */
517 void need_conntrack(void)
518 {
519 }
520 EXPORT_SYMBOL_GPL(need_conntrack);