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