Merge git://git.kernel.org/pub/scm/linux/kernel/git/sfrench/cifs-2.6
[linux-2.6] / net / netfilter / nf_conntrack_standalone.c
1 /* This file contains all the functions required for the standalone
2    nf_conntrack module.
3
4    These are not required by the compatibility layer.
5 */
6
7 /* (C) 1999-2001 Paul `Rusty' Russell
8  * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License version 2 as
12  * published by the Free Software Foundation.
13  *
14  * 16 Dec 2003: Yasuyuki Kozakai @USAGI <yasuyuki.kozakai@toshiba.co.jp>
15  *      - generalize L3 protocol dependent part.
16  *
17  * Derived from net/ipv4/netfilter/ip_conntrack_standalone.c
18  */
19
20 #include <linux/types.h>
21 #include <linux/netfilter.h>
22 #include <linux/module.h>
23 #include <linux/skbuff.h>
24 #include <linux/proc_fs.h>
25 #include <linux/seq_file.h>
26 #include <linux/percpu.h>
27 #include <linux/netdevice.h>
28 #ifdef CONFIG_SYSCTL
29 #include <linux/sysctl.h>
30 #endif
31
32 #include <net/netfilter/nf_conntrack.h>
33 #include <net/netfilter/nf_conntrack_core.h>
34 #include <net/netfilter/nf_conntrack_l3proto.h>
35 #include <net/netfilter/nf_conntrack_l4proto.h>
36 #include <net/netfilter/nf_conntrack_expect.h>
37 #include <net/netfilter/nf_conntrack_helper.h>
38
39 #if 0
40 #define DEBUGP printk
41 #else
42 #define DEBUGP(format, args...)
43 #endif
44
45 MODULE_LICENSE("GPL");
46
47 #ifdef CONFIG_PROC_FS
48 int
49 print_tuple(struct seq_file *s, const struct nf_conntrack_tuple *tuple,
50             struct nf_conntrack_l3proto *l3proto,
51             struct nf_conntrack_l4proto *l4proto)
52 {
53         return l3proto->print_tuple(s, tuple) || l4proto->print_tuple(s, tuple);
54 }
55 EXPORT_SYMBOL_GPL(print_tuple);
56
57 #ifdef CONFIG_NF_CT_ACCT
58 static unsigned int
59 seq_print_counters(struct seq_file *s,
60                    const struct ip_conntrack_counter *counter)
61 {
62         return seq_printf(s, "packets=%llu bytes=%llu ",
63                           (unsigned long long)counter->packets,
64                           (unsigned long long)counter->bytes);
65 }
66 #else
67 #define seq_print_counters(x, y)        0
68 #endif
69
70 struct ct_iter_state {
71         unsigned int bucket;
72 };
73
74 static struct list_head *ct_get_first(struct seq_file *seq)
75 {
76         struct ct_iter_state *st = seq->private;
77
78         for (st->bucket = 0;
79              st->bucket < nf_conntrack_htable_size;
80              st->bucket++) {
81                 if (!list_empty(&nf_conntrack_hash[st->bucket]))
82                         return nf_conntrack_hash[st->bucket].next;
83         }
84         return NULL;
85 }
86
87 static struct list_head *ct_get_next(struct seq_file *seq, struct list_head *head)
88 {
89         struct ct_iter_state *st = seq->private;
90
91         head = head->next;
92         while (head == &nf_conntrack_hash[st->bucket]) {
93                 if (++st->bucket >= nf_conntrack_htable_size)
94                         return NULL;
95                 head = nf_conntrack_hash[st->bucket].next;
96         }
97         return head;
98 }
99
100 static struct list_head *ct_get_idx(struct seq_file *seq, loff_t pos)
101 {
102         struct list_head *head = ct_get_first(seq);
103
104         if (head)
105                 while (pos && (head = ct_get_next(seq, head)))
106                         pos--;
107         return pos ? NULL : head;
108 }
109
110 static void *ct_seq_start(struct seq_file *seq, loff_t *pos)
111 {
112         read_lock_bh(&nf_conntrack_lock);
113         return ct_get_idx(seq, *pos);
114 }
115
116 static void *ct_seq_next(struct seq_file *s, void *v, loff_t *pos)
117 {
118         (*pos)++;
119         return ct_get_next(s, v);
120 }
121
122 static void ct_seq_stop(struct seq_file *s, void *v)
123 {
124         read_unlock_bh(&nf_conntrack_lock);
125 }
126
127 /* return 0 on success, 1 in case of error */
128 static int ct_seq_show(struct seq_file *s, void *v)
129 {
130         const struct nf_conntrack_tuple_hash *hash = v;
131         const struct nf_conn *conntrack = nf_ct_tuplehash_to_ctrack(hash);
132         struct nf_conntrack_l3proto *l3proto;
133         struct nf_conntrack_l4proto *l4proto;
134
135         NF_CT_ASSERT(conntrack);
136
137         /* we only want to print DIR_ORIGINAL */
138         if (NF_CT_DIRECTION(hash))
139                 return 0;
140
141         l3proto = __nf_ct_l3proto_find(conntrack->tuplehash[IP_CT_DIR_ORIGINAL]
142                                        .tuple.src.l3num);
143
144         NF_CT_ASSERT(l3proto);
145         l4proto = __nf_ct_l4proto_find(conntrack->tuplehash[IP_CT_DIR_ORIGINAL]
146                                    .tuple.src.l3num,
147                                    conntrack->tuplehash[IP_CT_DIR_ORIGINAL]
148                                    .tuple.dst.protonum);
149         NF_CT_ASSERT(l4proto);
150
151         if (seq_printf(s, "%-8s %u %-8s %u %ld ",
152                        l3proto->name,
153                        conntrack->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num,
154                        l4proto->name,
155                        conntrack->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum,
156                        timer_pending(&conntrack->timeout)
157                        ? (long)(conntrack->timeout.expires - jiffies)/HZ : 0) != 0)
158                 return -ENOSPC;
159
160         if (l3proto->print_conntrack(s, conntrack))
161                 return -ENOSPC;
162
163         if (l4proto->print_conntrack(s, conntrack))
164                 return -ENOSPC;
165
166         if (print_tuple(s, &conntrack->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
167                         l3proto, l4proto))
168                 return -ENOSPC;
169
170         if (seq_print_counters(s, &conntrack->counters[IP_CT_DIR_ORIGINAL]))
171                 return -ENOSPC;
172
173         if (!(test_bit(IPS_SEEN_REPLY_BIT, &conntrack->status)))
174                 if (seq_printf(s, "[UNREPLIED] "))
175                         return -ENOSPC;
176
177         if (print_tuple(s, &conntrack->tuplehash[IP_CT_DIR_REPLY].tuple,
178                         l3proto, l4proto))
179                 return -ENOSPC;
180
181         if (seq_print_counters(s, &conntrack->counters[IP_CT_DIR_REPLY]))
182                 return -ENOSPC;
183
184         if (test_bit(IPS_ASSURED_BIT, &conntrack->status))
185                 if (seq_printf(s, "[ASSURED] "))
186                         return -ENOSPC;
187
188 #if defined(CONFIG_NF_CONNTRACK_MARK)
189         if (seq_printf(s, "mark=%u ", conntrack->mark))
190                 return -ENOSPC;
191 #endif
192
193 #ifdef CONFIG_NF_CONNTRACK_SECMARK
194         if (seq_printf(s, "secmark=%u ", conntrack->secmark))
195                 return -ENOSPC;
196 #endif
197
198         if (seq_printf(s, "use=%u\n", atomic_read(&conntrack->ct_general.use)))
199                 return -ENOSPC;
200         
201         return 0;
202 }
203
204 static struct seq_operations ct_seq_ops = {
205         .start = ct_seq_start,
206         .next  = ct_seq_next,
207         .stop  = ct_seq_stop,
208         .show  = ct_seq_show
209 };
210
211 static int ct_open(struct inode *inode, struct file *file)
212 {
213         struct seq_file *seq;
214         struct ct_iter_state *st;
215         int ret;
216
217         st = kmalloc(sizeof(struct ct_iter_state), GFP_KERNEL);
218         if (st == NULL)
219                 return -ENOMEM;
220         ret = seq_open(file, &ct_seq_ops);
221         if (ret)
222                 goto out_free;
223         seq          = file->private_data;
224         seq->private = st;
225         memset(st, 0, sizeof(struct ct_iter_state));
226         return ret;
227 out_free:
228         kfree(st);
229         return ret;
230 }
231
232 static const struct file_operations ct_file_ops = {
233         .owner   = THIS_MODULE,
234         .open    = ct_open,
235         .read    = seq_read,
236         .llseek  = seq_lseek,
237         .release = seq_release_private,
238 };
239
240 static void *ct_cpu_seq_start(struct seq_file *seq, loff_t *pos)
241 {
242         int cpu;
243
244         if (*pos == 0)
245                 return SEQ_START_TOKEN;
246
247         for (cpu = *pos-1; cpu < NR_CPUS; ++cpu) {
248                 if (!cpu_possible(cpu))
249                         continue;
250                 *pos = cpu + 1;
251                 return &per_cpu(nf_conntrack_stat, cpu);
252         }
253
254         return NULL;
255 }
256
257 static void *ct_cpu_seq_next(struct seq_file *seq, void *v, loff_t *pos)
258 {
259         int cpu;
260
261         for (cpu = *pos; cpu < NR_CPUS; ++cpu) {
262                 if (!cpu_possible(cpu))
263                         continue;
264                 *pos = cpu + 1;
265                 return &per_cpu(nf_conntrack_stat, cpu);
266         }
267
268         return NULL;
269 }
270
271 static void ct_cpu_seq_stop(struct seq_file *seq, void *v)
272 {
273 }
274
275 static int ct_cpu_seq_show(struct seq_file *seq, void *v)
276 {
277         unsigned int nr_conntracks = atomic_read(&nf_conntrack_count);
278         struct ip_conntrack_stat *st = v;
279
280         if (v == SEQ_START_TOKEN) {
281                 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");
282                 return 0;
283         }
284
285         seq_printf(seq, "%08x  %08x %08x %08x %08x %08x %08x %08x "
286                         "%08x %08x %08x %08x %08x  %08x %08x %08x \n",
287                    nr_conntracks,
288                    st->searched,
289                    st->found,
290                    st->new,
291                    st->invalid,
292                    st->ignore,
293                    st->delete,
294                    st->delete_list,
295                    st->insert,
296                    st->insert_failed,
297                    st->drop,
298                    st->early_drop,
299                    st->error,
300
301                    st->expect_new,
302                    st->expect_create,
303                    st->expect_delete
304                 );
305         return 0;
306 }
307
308 static 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(file, &ct_cpu_seq_ops);
318 }
319
320 static const struct file_operations ct_cpu_seq_fops = {
321         .owner   = THIS_MODULE,
322         .open    = ct_cpu_seq_open,
323         .read    = seq_read,
324         .llseek  = seq_lseek,
325         .release = seq_release_private,
326 };
327 #endif /* CONFIG_PROC_FS */
328
329 /* Sysctl support */
330
331 int nf_conntrack_checksum __read_mostly = 1;
332 EXPORT_SYMBOL_GPL(nf_conntrack_checksum);
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_sysctl_header;
340
341 static ctl_table nf_ct_sysctl_table[] = {
342         {
343                 .ctl_name       = NET_NF_CONNTRACK_MAX,
344                 .procname       = "nf_conntrack_max",
345                 .data           = &nf_conntrack_max,
346                 .maxlen         = sizeof(int),
347                 .mode           = 0644,
348                 .proc_handler   = &proc_dointvec,
349         },
350         {
351                 .ctl_name       = NET_NF_CONNTRACK_COUNT,
352                 .procname       = "nf_conntrack_count",
353                 .data           = &nf_conntrack_count,
354                 .maxlen         = sizeof(int),
355                 .mode           = 0444,
356                 .proc_handler   = &proc_dointvec,
357         },
358         {
359                 .ctl_name       = NET_NF_CONNTRACK_BUCKETS,
360                 .procname       = "nf_conntrack_buckets",
361                 .data           = &nf_conntrack_htable_size,
362                 .maxlen         = sizeof(unsigned int),
363                 .mode           = 0444,
364                 .proc_handler   = &proc_dointvec,
365         },
366         {
367                 .ctl_name       = NET_NF_CONNTRACK_CHECKSUM,
368                 .procname       = "nf_conntrack_checksum",
369                 .data           = &nf_conntrack_checksum,
370                 .maxlen         = sizeof(unsigned int),
371                 .mode           = 0644,
372                 .proc_handler   = &proc_dointvec,
373         },
374         {
375                 .ctl_name       = NET_NF_CONNTRACK_LOG_INVALID,
376                 .procname       = "nf_conntrack_log_invalid",
377                 .data           = &nf_ct_log_invalid,
378                 .maxlen         = sizeof(unsigned int),
379                 .mode           = 0644,
380                 .proc_handler   = &proc_dointvec_minmax,
381                 .strategy       = &sysctl_intvec,
382                 .extra1         = &log_invalid_proto_min,
383                 .extra2         = &log_invalid_proto_max,
384         },
385
386         { .ctl_name = 0 }
387 };
388
389 #define NET_NF_CONNTRACK_MAX 2089
390
391 static ctl_table nf_ct_netfilter_table[] = {
392         {
393                 .ctl_name       = NET_NETFILTER,
394                 .procname       = "netfilter",
395                 .mode           = 0555,
396                 .child          = nf_ct_sysctl_table,
397         },
398         {
399                 .ctl_name       = NET_NF_CONNTRACK_MAX,
400                 .procname       = "nf_conntrack_max",
401                 .data           = &nf_conntrack_max,
402                 .maxlen         = sizeof(int),
403                 .mode           = 0644,
404                 .proc_handler   = &proc_dointvec,
405         },
406         { .ctl_name = 0 }
407 };
408
409 static ctl_table nf_ct_net_table[] = {
410         {
411                 .ctl_name       = CTL_NET,
412                 .procname       = "net",
413                 .mode           = 0555,
414                 .child          = nf_ct_netfilter_table,
415         },
416         { .ctl_name = 0 }
417 };
418 EXPORT_SYMBOL_GPL(nf_ct_log_invalid);
419 #endif /* CONFIG_SYSCTL */
420
421 static int __init nf_conntrack_standalone_init(void)
422 {
423 #ifdef CONFIG_PROC_FS
424         struct proc_dir_entry *proc, *proc_exp, *proc_stat;
425 #endif
426         int ret = 0;
427
428         ret = nf_conntrack_init();
429         if (ret < 0)
430                 return ret;
431
432 #ifdef CONFIG_PROC_FS
433         proc = proc_net_fops_create("nf_conntrack", 0440, &ct_file_ops);
434         if (!proc) goto cleanup_init;
435
436         proc_exp = proc_net_fops_create("nf_conntrack_expect", 0440,
437                                         &exp_file_ops);
438         if (!proc_exp) goto cleanup_proc;
439
440         proc_stat = create_proc_entry("nf_conntrack", S_IRUGO, proc_net_stat);
441         if (!proc_stat)
442                 goto cleanup_proc_exp;
443
444         proc_stat->proc_fops = &ct_cpu_seq_fops;
445         proc_stat->owner = THIS_MODULE;
446 #endif
447 #ifdef CONFIG_SYSCTL
448         nf_ct_sysctl_header = register_sysctl_table(nf_ct_net_table);
449         if (nf_ct_sysctl_header == NULL) {
450                 printk("nf_conntrack: can't register to sysctl.\n");
451                 ret = -ENOMEM;
452                 goto cleanup_proc_stat;
453         }
454 #endif
455         return ret;
456
457 #ifdef CONFIG_SYSCTL
458  cleanup_proc_stat:
459 #endif
460 #ifdef CONFIG_PROC_FS
461         remove_proc_entry("nf_conntrack", proc_net_stat);
462  cleanup_proc_exp:
463         proc_net_remove("nf_conntrack_expect");
464  cleanup_proc:
465         proc_net_remove("nf_conntrack");
466  cleanup_init:
467 #endif /* CNFIG_PROC_FS */
468         nf_conntrack_cleanup();
469         return ret;
470 }
471
472 static void __exit nf_conntrack_standalone_fini(void)
473 {
474 #ifdef CONFIG_SYSCTL
475         unregister_sysctl_table(nf_ct_sysctl_header);
476 #endif
477 #ifdef CONFIG_PROC_FS
478         remove_proc_entry("nf_conntrack", proc_net_stat);
479         proc_net_remove("nf_conntrack_expect");
480         proc_net_remove("nf_conntrack");
481 #endif /* CNFIG_PROC_FS */
482         nf_conntrack_cleanup();
483 }
484
485 module_init(nf_conntrack_standalone_init);
486 module_exit(nf_conntrack_standalone_fini);
487
488 /* Some modules need us, but don't depend directly on any symbol.
489    They should call this. */
490 void need_conntrack(void)
491 {
492 }
493 EXPORT_SYMBOL_GPL(need_conntrack);