blob: 12d6a6327b631b3143530add12a58f17a9a3d783 [file] [log] [blame]
Patrick McHardye4bd8bc2006-11-29 02:35:22 +01001/* 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
28static unsigned int
29seq_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
40struct ct_iter_state {
41 unsigned int bucket;
42};
43
Patrick McHardyf205c5e2007-07-07 22:28:14 -070044static struct hlist_node *ct_get_first(struct seq_file *seq)
Patrick McHardye4bd8bc2006-11-29 02:35:22 +010045{
46 struct ct_iter_state *st = seq->private;
47
48 for (st->bucket = 0;
49 st->bucket < nf_conntrack_htable_size;
50 st->bucket++) {
Patrick McHardyf205c5e2007-07-07 22:28:14 -070051 if (!hlist_empty(&nf_conntrack_hash[st->bucket]))
52 return nf_conntrack_hash[st->bucket].first;
Patrick McHardye4bd8bc2006-11-29 02:35:22 +010053 }
54 return NULL;
55}
56
Patrick McHardyf205c5e2007-07-07 22:28:14 -070057static struct hlist_node *ct_get_next(struct seq_file *seq,
58 struct hlist_node *head)
Patrick McHardye4bd8bc2006-11-29 02:35:22 +010059{
60 struct ct_iter_state *st = seq->private;
61
62 head = head->next;
Patrick McHardyf205c5e2007-07-07 22:28:14 -070063 while (head == NULL) {
Patrick McHardye4bd8bc2006-11-29 02:35:22 +010064 if (++st->bucket >= nf_conntrack_htable_size)
65 return NULL;
Patrick McHardyf205c5e2007-07-07 22:28:14 -070066 head = nf_conntrack_hash[st->bucket].first;
Patrick McHardye4bd8bc2006-11-29 02:35:22 +010067 }
68 return head;
69}
70
Patrick McHardyf205c5e2007-07-07 22:28:14 -070071static struct hlist_node *ct_get_idx(struct seq_file *seq, loff_t pos)
Patrick McHardye4bd8bc2006-11-29 02:35:22 +010072{
Patrick McHardyf205c5e2007-07-07 22:28:14 -070073 struct hlist_node *head = ct_get_first(seq);
Patrick McHardye4bd8bc2006-11-29 02:35:22 +010074
75 if (head)
76 while (pos && (head = ct_get_next(seq, head)))
77 pos--;
78 return pos ? NULL : head;
79}
80
81static 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
87static 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
93static void ct_seq_stop(struct seq_file *s, void *v)
94{
95 read_unlock_bh(&nf_conntrack_lock);
96}
97
98static 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
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900139 if (seq_print_counters(s, &ct->counters[IP_CT_DIR_ORIGINAL]))
Patrick McHardye4bd8bc2006-11-29 02:35:22 +0100140 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
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900150 if (seq_print_counters(s, &ct->counters[IP_CT_DIR_REPLY]))
Patrick McHardye4bd8bc2006-11-29 02:35:22 +0100151 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
173static 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
180static 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;
196out_free:
197 kfree(st);
198 return ret;
199}
200
Arjan van de Ven9a321442007-02-12 00:55:35 -0800201static const struct file_operations ct_file_ops = {
Patrick McHardye4bd8bc2006-11-29 02:35:22 +0100202 .owner = THIS_MODULE,
203 .open = ct_open,
204 .read = seq_read,
205 .llseek = seq_lseek,
206 .release = seq_release_private,
207};
208
209/* expects */
210static void *exp_seq_start(struct seq_file *s, loff_t *pos)
211{
Patrick McHardy68236452007-07-07 22:30:49 -0700212 struct list_head *e = &nf_ct_expect_list;
Patrick McHardye4bd8bc2006-11-29 02:35:22 +0100213 loff_t i;
214
215 /* strange seq_file api calls stop even if we fail,
216 * thus we need to grab lock since stop unlocks */
217 read_lock_bh(&nf_conntrack_lock);
218
219 if (list_empty(e))
220 return NULL;
221
222 for (i = 0; i <= *pos; i++) {
223 e = e->next;
Patrick McHardy68236452007-07-07 22:30:49 -0700224 if (e == &nf_ct_expect_list)
Patrick McHardye4bd8bc2006-11-29 02:35:22 +0100225 return NULL;
226 }
227 return e;
228}
229
230static void *exp_seq_next(struct seq_file *s, void *v, loff_t *pos)
231{
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900232 struct list_head *e = v;
Patrick McHardye4bd8bc2006-11-29 02:35:22 +0100233
234 ++*pos;
235 e = e->next;
236
Patrick McHardy68236452007-07-07 22:30:49 -0700237 if (e == &nf_ct_expect_list)
Patrick McHardye4bd8bc2006-11-29 02:35:22 +0100238 return NULL;
239
240 return e;
241}
242
243static void exp_seq_stop(struct seq_file *s, void *v)
244{
245 read_unlock_bh(&nf_conntrack_lock);
246}
247
248static int exp_seq_show(struct seq_file *s, void *v)
249{
250 struct nf_conntrack_expect *exp = v;
251
252 if (exp->tuple.src.l3num != AF_INET)
253 return 0;
254
255 if (exp->timeout.function)
256 seq_printf(s, "%ld ", timer_pending(&exp->timeout)
257 ? (long)(exp->timeout.expires - jiffies)/HZ : 0);
258 else
259 seq_printf(s, "- ");
260
261 seq_printf(s, "proto=%u ", exp->tuple.dst.protonum);
262
263 print_tuple(s, &exp->tuple,
264 __nf_ct_l3proto_find(exp->tuple.src.l3num),
265 __nf_ct_l4proto_find(exp->tuple.src.l3num,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900266 exp->tuple.dst.protonum));
Patrick McHardye4bd8bc2006-11-29 02:35:22 +0100267 return seq_putc(s, '\n');
268}
269
270static struct seq_operations exp_seq_ops = {
271 .start = exp_seq_start,
272 .next = exp_seq_next,
273 .stop = exp_seq_stop,
274 .show = exp_seq_show
275};
276
277static int exp_open(struct inode *inode, struct file *file)
278{
279 return seq_open(file, &exp_seq_ops);
280}
281
Arjan van de Ven9a321442007-02-12 00:55:35 -0800282static const struct file_operations ip_exp_file_ops = {
Patrick McHardye4bd8bc2006-11-29 02:35:22 +0100283 .owner = THIS_MODULE,
284 .open = exp_open,
285 .read = seq_read,
286 .llseek = seq_lseek,
287 .release = seq_release
288};
289
290static void *ct_cpu_seq_start(struct seq_file *seq, loff_t *pos)
291{
292 int cpu;
293
294 if (*pos == 0)
295 return SEQ_START_TOKEN;
296
297 for (cpu = *pos-1; cpu < NR_CPUS; ++cpu) {
298 if (!cpu_possible(cpu))
299 continue;
300 *pos = cpu+1;
301 return &per_cpu(nf_conntrack_stat, cpu);
302 }
303
304 return NULL;
305}
306
307static void *ct_cpu_seq_next(struct seq_file *seq, void *v, loff_t *pos)
308{
309 int cpu;
310
311 for (cpu = *pos; cpu < NR_CPUS; ++cpu) {
312 if (!cpu_possible(cpu))
313 continue;
314 *pos = cpu+1;
315 return &per_cpu(nf_conntrack_stat, cpu);
316 }
317
318 return NULL;
319}
320
321static void ct_cpu_seq_stop(struct seq_file *seq, void *v)
322{
323}
324
325static int ct_cpu_seq_show(struct seq_file *seq, void *v)
326{
327 unsigned int nr_conntracks = atomic_read(&nf_conntrack_count);
328 struct ip_conntrack_stat *st = v;
329
330 if (v == SEQ_START_TOKEN) {
331 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");
332 return 0;
333 }
334
335 seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x %08x "
336 "%08x %08x %08x %08x %08x %08x %08x %08x \n",
337 nr_conntracks,
338 st->searched,
339 st->found,
340 st->new,
341 st->invalid,
342 st->ignore,
343 st->delete,
344 st->delete_list,
345 st->insert,
346 st->insert_failed,
347 st->drop,
348 st->early_drop,
349 st->error,
350
351 st->expect_new,
352 st->expect_create,
353 st->expect_delete
354 );
355 return 0;
356}
357
358static struct seq_operations ct_cpu_seq_ops = {
359 .start = ct_cpu_seq_start,
360 .next = ct_cpu_seq_next,
361 .stop = ct_cpu_seq_stop,
362 .show = ct_cpu_seq_show,
363};
364
365static int ct_cpu_seq_open(struct inode *inode, struct file *file)
366{
367 return seq_open(file, &ct_cpu_seq_ops);
368}
369
Arjan van de Ven9a321442007-02-12 00:55:35 -0800370static const struct file_operations ct_cpu_seq_fops = {
Patrick McHardye4bd8bc2006-11-29 02:35:22 +0100371 .owner = THIS_MODULE,
372 .open = ct_cpu_seq_open,
373 .read = seq_read,
374 .llseek = seq_lseek,
375 .release = seq_release_private,
376};
377
378int __init nf_conntrack_ipv4_compat_init(void)
379{
380 struct proc_dir_entry *proc, *proc_exp, *proc_stat;
381
382 proc = proc_net_fops_create("ip_conntrack", 0440, &ct_file_ops);
383 if (!proc)
384 goto err1;
385
386 proc_exp = proc_net_fops_create("ip_conntrack_expect", 0440,
387 &ip_exp_file_ops);
388 if (!proc_exp)
389 goto err2;
390
391 proc_stat = create_proc_entry("ip_conntrack", S_IRUGO, proc_net_stat);
392 if (!proc_stat)
393 goto err3;
394
395 proc_stat->proc_fops = &ct_cpu_seq_fops;
396 proc_stat->owner = THIS_MODULE;
397
398 return 0;
399
400err3:
401 proc_net_remove("ip_conntrack_expect");
402err2:
403 proc_net_remove("ip_conntrack");
404err1:
405 return -ENOMEM;
406}
407
408void __exit nf_conntrack_ipv4_compat_fini(void)
409{
410 remove_proc_entry("ip_conntrack", proc_net_stat);
411 proc_net_remove("ip_conntrack_expect");
412 proc_net_remove("ip_conntrack");
413}