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