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