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