[NETFILTER]: nf_conntrack: make print_conntrack function optional for l4protos
[linux-2.6.git] / net / ipv4 / netfilter / nf_conntrack_l3proto_ipv4_compat.c
1 /* ip_conntrack proc compat - based on ip_conntrack_standalone.c
2  *
3  * (C) 1999-2001 Paul `Rusty' Russell
4  * (C) 2002-2006 Netfilter Core Team <coreteam@netfilter.org>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  */
10 #include <linux/types.h>
11 #include <linux/proc_fs.h>
12 #include <linux/seq_file.h>
13 #include <linux/percpu.h>
14 #include <net/net_namespace.h>
15
16 #include <linux/netfilter.h>
17 #include <net/netfilter/nf_conntrack_core.h>
18 #include <net/netfilter/nf_conntrack_l3proto.h>
19 #include <net/netfilter/nf_conntrack_l4proto.h>
20 #include <net/netfilter/nf_conntrack_expect.h>
21
22 #ifdef CONFIG_NF_CT_ACCT
23 static unsigned int
24 seq_print_counters(struct seq_file *s,
25                    const struct ip_conntrack_counter *counter)
26 {
27         return seq_printf(s, "packets=%llu bytes=%llu ",
28                           (unsigned long long)counter->packets,
29                           (unsigned long long)counter->bytes);
30 }
31 #else
32 #define seq_print_counters(x, y)        0
33 #endif
34
35 struct ct_iter_state {
36         unsigned int bucket;
37 };
38
39 static struct hlist_node *ct_get_first(struct seq_file *seq)
40 {
41         struct ct_iter_state *st = seq->private;
42
43         for (st->bucket = 0;
44              st->bucket < nf_conntrack_htable_size;
45              st->bucket++) {
46                 if (!hlist_empty(&nf_conntrack_hash[st->bucket]))
47                         return nf_conntrack_hash[st->bucket].first;
48         }
49         return NULL;
50 }
51
52 static struct hlist_node *ct_get_next(struct seq_file *seq,
53                                       struct hlist_node *head)
54 {
55         struct ct_iter_state *st = seq->private;
56
57         head = head->next;
58         while (head == NULL) {
59                 if (++st->bucket >= nf_conntrack_htable_size)
60                         return NULL;
61                 head = nf_conntrack_hash[st->bucket].first;
62         }
63         return head;
64 }
65
66 static struct hlist_node *ct_get_idx(struct seq_file *seq, loff_t pos)
67 {
68         struct hlist_node *head = ct_get_first(seq);
69
70         if (head)
71                 while (pos && (head = ct_get_next(seq, head)))
72                         pos--;
73         return pos ? NULL : head;
74 }
75
76 static void *ct_seq_start(struct seq_file *seq, loff_t *pos)
77 {
78         read_lock_bh(&nf_conntrack_lock);
79         return ct_get_idx(seq, *pos);
80 }
81
82 static void *ct_seq_next(struct seq_file *s, void *v, loff_t *pos)
83 {
84         (*pos)++;
85         return ct_get_next(s, v);
86 }
87
88 static void ct_seq_stop(struct seq_file *s, void *v)
89 {
90         read_unlock_bh(&nf_conntrack_lock);
91 }
92
93 static int ct_seq_show(struct seq_file *s, void *v)
94 {
95         const struct nf_conntrack_tuple_hash *hash = v;
96         const struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(hash);
97         struct nf_conntrack_l3proto *l3proto;
98         struct nf_conntrack_l4proto *l4proto;
99
100         NF_CT_ASSERT(ct);
101
102         /* we only want to print DIR_ORIGINAL */
103         if (NF_CT_DIRECTION(hash))
104                 return 0;
105         if (ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num != AF_INET)
106                 return 0;
107
108         l3proto = __nf_ct_l3proto_find(ct->tuplehash[IP_CT_DIR_ORIGINAL]
109                                        .tuple.src.l3num);
110         NF_CT_ASSERT(l3proto);
111         l4proto = __nf_ct_l4proto_find(ct->tuplehash[IP_CT_DIR_ORIGINAL]
112                                        .tuple.src.l3num,
113                                        ct->tuplehash[IP_CT_DIR_ORIGINAL]
114                                        .tuple.dst.protonum);
115         NF_CT_ASSERT(l4proto);
116
117         if (seq_printf(s, "%-8s %u %ld ",
118                       l4proto->name,
119                       ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum,
120                       timer_pending(&ct->timeout)
121                       ? (long)(ct->timeout.expires - jiffies)/HZ : 0) != 0)
122                 return -ENOSPC;
123
124         if (l4proto->print_conntrack && l4proto->print_conntrack(s, ct))
125                 return -ENOSPC;
126
127         if (print_tuple(s, &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
128                         l3proto, l4proto))
129                 return -ENOSPC;
130
131         if (seq_print_counters(s, &ct->counters[IP_CT_DIR_ORIGINAL]))
132                 return -ENOSPC;
133
134         if (!(test_bit(IPS_SEEN_REPLY_BIT, &ct->status)))
135                 if (seq_printf(s, "[UNREPLIED] "))
136                         return -ENOSPC;
137
138         if (print_tuple(s, &ct->tuplehash[IP_CT_DIR_REPLY].tuple,
139                         l3proto, l4proto))
140                 return -ENOSPC;
141
142         if (seq_print_counters(s, &ct->counters[IP_CT_DIR_REPLY]))
143                 return -ENOSPC;
144
145         if (test_bit(IPS_ASSURED_BIT, &ct->status))
146                 if (seq_printf(s, "[ASSURED] "))
147                         return -ENOSPC;
148
149 #ifdef CONFIG_NF_CONNTRACK_MARK
150         if (seq_printf(s, "mark=%u ", ct->mark))
151                 return -ENOSPC;
152 #endif
153
154 #ifdef CONFIG_NF_CONNTRACK_SECMARK
155         if (seq_printf(s, "secmark=%u ", ct->secmark))
156                 return -ENOSPC;
157 #endif
158
159         if (seq_printf(s, "use=%u\n", atomic_read(&ct->ct_general.use)))
160                 return -ENOSPC;
161
162         return 0;
163 }
164
165 static const struct seq_operations ct_seq_ops = {
166         .start = ct_seq_start,
167         .next  = ct_seq_next,
168         .stop  = ct_seq_stop,
169         .show  = ct_seq_show
170 };
171
172 static int ct_open(struct inode *inode, struct file *file)
173 {
174         return seq_open_private(file, &ct_seq_ops,
175                         sizeof(struct ct_iter_state));
176 }
177
178 static const struct file_operations ct_file_ops = {
179         .owner   = THIS_MODULE,
180         .open    = ct_open,
181         .read    = seq_read,
182         .llseek  = seq_lseek,
183         .release = seq_release_private,
184 };
185
186 /* expects */
187 struct ct_expect_iter_state {
188         unsigned int bucket;
189 };
190
191 static struct hlist_node *ct_expect_get_first(struct seq_file *seq)
192 {
193         struct ct_expect_iter_state *st = seq->private;
194
195         for (st->bucket = 0; st->bucket < nf_ct_expect_hsize; st->bucket++) {
196                 if (!hlist_empty(&nf_ct_expect_hash[st->bucket]))
197                         return nf_ct_expect_hash[st->bucket].first;
198         }
199         return NULL;
200 }
201
202 static struct hlist_node *ct_expect_get_next(struct seq_file *seq,
203                                              struct hlist_node *head)
204 {
205         struct ct_expect_iter_state *st = seq->private;
206
207         head = head->next;
208         while (head == NULL) {
209                 if (++st->bucket >= nf_ct_expect_hsize)
210                         return NULL;
211                 head = nf_ct_expect_hash[st->bucket].first;
212         }
213         return head;
214 }
215
216 static struct hlist_node *ct_expect_get_idx(struct seq_file *seq, loff_t pos)
217 {
218         struct hlist_node *head = ct_expect_get_first(seq);
219
220         if (head)
221                 while (pos && (head = ct_expect_get_next(seq, head)))
222                         pos--;
223         return pos ? NULL : head;
224 }
225
226 static void *exp_seq_start(struct seq_file *seq, loff_t *pos)
227 {
228         read_lock_bh(&nf_conntrack_lock);
229         return ct_expect_get_idx(seq, *pos);
230 }
231
232 static void *exp_seq_next(struct seq_file *seq, void *v, loff_t *pos)
233 {
234         (*pos)++;
235         return ct_expect_get_next(seq, v);
236 }
237
238 static void exp_seq_stop(struct seq_file *seq, void *v)
239 {
240         read_unlock_bh(&nf_conntrack_lock);
241 }
242
243 static int exp_seq_show(struct seq_file *s, void *v)
244 {
245         struct nf_conntrack_expect *exp;
246         struct hlist_node *n = v;
247
248         exp = hlist_entry(n, struct nf_conntrack_expect, hnode);
249
250         if (exp->tuple.src.l3num != AF_INET)
251                 return 0;
252
253         if (exp->timeout.function)
254                 seq_printf(s, "%ld ", timer_pending(&exp->timeout)
255                            ? (long)(exp->timeout.expires - jiffies)/HZ : 0);
256         else
257                 seq_printf(s, "- ");
258
259         seq_printf(s, "proto=%u ", exp->tuple.dst.protonum);
260
261         print_tuple(s, &exp->tuple,
262                     __nf_ct_l3proto_find(exp->tuple.src.l3num),
263                     __nf_ct_l4proto_find(exp->tuple.src.l3num,
264                                          exp->tuple.dst.protonum));
265         return seq_putc(s, '\n');
266 }
267
268 static const struct seq_operations exp_seq_ops = {
269         .start = exp_seq_start,
270         .next = exp_seq_next,
271         .stop = exp_seq_stop,
272         .show = exp_seq_show
273 };
274
275 static int exp_open(struct inode *inode, struct file *file)
276 {
277         return seq_open_private(file, &exp_seq_ops,
278                         sizeof(struct ct_expect_iter_state));
279 }
280
281 static const struct file_operations ip_exp_file_ops = {
282         .owner   = THIS_MODULE,
283         .open    = exp_open,
284         .read    = seq_read,
285         .llseek  = seq_lseek,
286         .release = seq_release_private,
287 };
288
289 static void *ct_cpu_seq_start(struct seq_file *seq, loff_t *pos)
290 {
291         int cpu;
292
293         if (*pos == 0)
294                 return SEQ_START_TOKEN;
295
296         for (cpu = *pos-1; cpu < NR_CPUS; ++cpu) {
297                 if (!cpu_possible(cpu))
298                         continue;
299                 *pos = cpu+1;
300                 return &per_cpu(nf_conntrack_stat, cpu);
301         }
302
303         return NULL;
304 }
305
306 static void *ct_cpu_seq_next(struct seq_file *seq, void *v, loff_t *pos)
307 {
308         int cpu;
309
310         for (cpu = *pos; cpu < NR_CPUS; ++cpu) {
311                 if (!cpu_possible(cpu))
312                         continue;
313                 *pos = cpu+1;
314                 return &per_cpu(nf_conntrack_stat, cpu);
315         }
316
317         return NULL;
318 }
319
320 static void ct_cpu_seq_stop(struct seq_file *seq, void *v)
321 {
322 }
323
324 static int ct_cpu_seq_show(struct seq_file *seq, void *v)
325 {
326         unsigned int nr_conntracks = atomic_read(&nf_conntrack_count);
327         struct ip_conntrack_stat *st = v;
328
329         if (v == SEQ_START_TOKEN) {
330                 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");
331                 return 0;
332         }
333
334         seq_printf(seq, "%08x  %08x %08x %08x %08x %08x %08x %08x "
335                         "%08x %08x %08x %08x %08x  %08x %08x %08x \n",
336                    nr_conntracks,
337                    st->searched,
338                    st->found,
339                    st->new,
340                    st->invalid,
341                    st->ignore,
342                    st->delete,
343                    st->delete_list,
344                    st->insert,
345                    st->insert_failed,
346                    st->drop,
347                    st->early_drop,
348                    st->error,
349
350                    st->expect_new,
351                    st->expect_create,
352                    st->expect_delete
353                 );
354         return 0;
355 }
356
357 static const struct seq_operations ct_cpu_seq_ops = {
358         .start  = ct_cpu_seq_start,
359         .next   = ct_cpu_seq_next,
360         .stop   = ct_cpu_seq_stop,
361         .show   = ct_cpu_seq_show,
362 };
363
364 static int ct_cpu_seq_open(struct inode *inode, struct file *file)
365 {
366         return seq_open(file, &ct_cpu_seq_ops);
367 }
368
369 static const struct file_operations ct_cpu_seq_fops = {
370         .owner   = THIS_MODULE,
371         .open    = ct_cpu_seq_open,
372         .read    = seq_read,
373         .llseek  = seq_lseek,
374         .release = seq_release_private,
375 };
376
377 int __init nf_conntrack_ipv4_compat_init(void)
378 {
379         struct proc_dir_entry *proc, *proc_exp, *proc_stat;
380
381         proc = proc_net_fops_create(&init_net, "ip_conntrack", 0440, &ct_file_ops);
382         if (!proc)
383                 goto err1;
384
385         proc_exp = proc_net_fops_create(&init_net, "ip_conntrack_expect", 0440,
386                                         &ip_exp_file_ops);
387         if (!proc_exp)
388                 goto err2;
389
390         proc_stat = create_proc_entry("ip_conntrack", S_IRUGO, init_net.proc_net_stat);
391         if (!proc_stat)
392                 goto err3;
393
394         proc_stat->proc_fops = &ct_cpu_seq_fops;
395         proc_stat->owner = THIS_MODULE;
396
397         return 0;
398
399 err3:
400         proc_net_remove(&init_net, "ip_conntrack_expect");
401 err2:
402         proc_net_remove(&init_net, "ip_conntrack");
403 err1:
404         return -ENOMEM;
405 }
406
407 void __exit nf_conntrack_ipv4_compat_fini(void)
408 {
409         remove_proc_entry("ip_conntrack", init_net.proc_net_stat);
410         proc_net_remove(&init_net, "ip_conntrack_expect");
411         proc_net_remove(&init_net, "ip_conntrack");
412 }