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