]> bbs.cooldavid.org Git - net-next-2.6.git/blame - net/netfilter/nf_conntrack_standalone.c
netfilter: nf_conntrack: add support for "conntrack zones"
[net-next-2.6.git] / net / netfilter / nf_conntrack_standalone.c
CommitLineData
9fb9cbb1
YK
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.
9fb9cbb1
YK
7 */
8
9fb9cbb1
YK
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>
457c4cbc 17#include <net/net_namespace.h>
9fb9cbb1
YK
18#ifdef CONFIG_SYSCTL
19#include <linux/sysctl.h>
20#endif
21
9fb9cbb1 22#include <net/netfilter/nf_conntrack.h>
f6180121 23#include <net/netfilter/nf_conntrack_core.h>
9fb9cbb1 24#include <net/netfilter/nf_conntrack_l3proto.h>
605dcad6 25#include <net/netfilter/nf_conntrack_l4proto.h>
77ab9cff 26#include <net/netfilter/nf_conntrack_expect.h>
9fb9cbb1 27#include <net/netfilter/nf_conntrack_helper.h>
58401572 28#include <net/netfilter/nf_conntrack_acct.h>
5d0aa2cc 29#include <net/netfilter/nf_conntrack_zones.h>
9fb9cbb1 30
9fb9cbb1
YK
31MODULE_LICENSE("GPL");
32
9fb9cbb1 33#ifdef CONFIG_PROC_FS
77ab9cff 34int
9fb9cbb1 35print_tuple(struct seq_file *s, const struct nf_conntrack_tuple *tuple,
32948588
JE
36 const struct nf_conntrack_l3proto *l3proto,
37 const struct nf_conntrack_l4proto *l4proto)
9fb9cbb1 38{
605dcad6 39 return l3proto->print_tuple(s, tuple) || l4proto->print_tuple(s, tuple);
9fb9cbb1 40}
e4bd8bce 41EXPORT_SYMBOL_GPL(print_tuple);
9fb9cbb1 42
9fb9cbb1 43struct ct_iter_state {
b2ce2c74 44 struct seq_net_private p;
9fb9cbb1
YK
45 unsigned int bucket;
46};
47
ea781f19 48static struct hlist_nulls_node *ct_get_first(struct seq_file *seq)
9fb9cbb1 49{
b2ce2c74 50 struct net *net = seq_file_net(seq);
9fb9cbb1 51 struct ct_iter_state *st = seq->private;
ea781f19 52 struct hlist_nulls_node *n;
9fb9cbb1
YK
53
54 for (st->bucket = 0;
d696c7bd 55 st->bucket < net->ct.htable_size;
9fb9cbb1 56 st->bucket++) {
b2ce2c74 57 n = rcu_dereference(net->ct.hash[st->bucket].first);
ea781f19 58 if (!is_a_nulls(n))
76507f69 59 return n;
9fb9cbb1
YK
60 }
61 return NULL;
62}
63
ea781f19
ED
64static struct hlist_nulls_node *ct_get_next(struct seq_file *seq,
65 struct hlist_nulls_node *head)
9fb9cbb1 66{
b2ce2c74 67 struct net *net = seq_file_net(seq);
9fb9cbb1
YK
68 struct ct_iter_state *st = seq->private;
69
76507f69 70 head = rcu_dereference(head->next);
ea781f19
ED
71 while (is_a_nulls(head)) {
72 if (likely(get_nulls_value(head) == st->bucket)) {
d696c7bd 73 if (++st->bucket >= net->ct.htable_size)
ea781f19
ED
74 return NULL;
75 }
b2ce2c74 76 head = rcu_dereference(net->ct.hash[st->bucket].first);
9fb9cbb1
YK
77 }
78 return head;
79}
80
ea781f19 81static struct hlist_nulls_node *ct_get_idx(struct seq_file *seq, loff_t pos)
9fb9cbb1 82{
ea781f19 83 struct hlist_nulls_node *head = ct_get_first(seq);
9fb9cbb1
YK
84
85 if (head)
86 while (pos && (head = ct_get_next(seq, head)))
87 pos--;
88 return pos ? NULL : head;
89}
90
91static void *ct_seq_start(struct seq_file *seq, loff_t *pos)
76507f69 92 __acquires(RCU)
9fb9cbb1 93{
76507f69 94 rcu_read_lock();
9fb9cbb1
YK
95 return ct_get_idx(seq, *pos);
96}
97
98static void *ct_seq_next(struct seq_file *s, void *v, loff_t *pos)
99{
100 (*pos)++;
101 return ct_get_next(s, v);
102}
103
104static void ct_seq_stop(struct seq_file *s, void *v)
76507f69 105 __releases(RCU)
9fb9cbb1 106{
76507f69 107 rcu_read_unlock();
9fb9cbb1
YK
108}
109
110/* return 0 on success, 1 in case of error */
111static int ct_seq_show(struct seq_file *s, void *v)
112{
ea781f19
ED
113 struct nf_conntrack_tuple_hash *hash = v;
114 struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(hash);
32948588
JE
115 const struct nf_conntrack_l3proto *l3proto;
116 const struct nf_conntrack_l4proto *l4proto;
ea781f19 117 int ret = 0;
9fb9cbb1 118
c88130bc 119 NF_CT_ASSERT(ct);
ea781f19
ED
120 if (unlikely(!atomic_inc_not_zero(&ct->ct_general.use)))
121 return 0;
9fb9cbb1
YK
122
123 /* we only want to print DIR_ORIGINAL */
124 if (NF_CT_DIRECTION(hash))
ea781f19 125 goto release;
9fb9cbb1 126
5e8fbe2a 127 l3proto = __nf_ct_l3proto_find(nf_ct_l3num(ct));
9fb9cbb1 128 NF_CT_ASSERT(l3proto);
5e8fbe2a 129 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
605dcad6 130 NF_CT_ASSERT(l4proto);
9fb9cbb1 131
ea781f19 132 ret = -ENOSPC;
9fb9cbb1 133 if (seq_printf(s, "%-8s %u %-8s %u %ld ",
5e8fbe2a
PM
134 l3proto->name, nf_ct_l3num(ct),
135 l4proto->name, nf_ct_protonum(ct),
c88130bc
PM
136 timer_pending(&ct->timeout)
137 ? (long)(ct->timeout.expires - jiffies)/HZ : 0) != 0)
ea781f19 138 goto release;
9fb9cbb1 139
c88130bc 140 if (l4proto->print_conntrack && l4proto->print_conntrack(s, ct))
ea781f19 141 goto release;
9fb9cbb1 142
c88130bc 143 if (print_tuple(s, &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
605dcad6 144 l3proto, l4proto))
ea781f19 145 goto release;
9fb9cbb1 146
58401572 147 if (seq_print_acct(s, ct, IP_CT_DIR_ORIGINAL))
ea781f19 148 goto release;
9fb9cbb1 149
c88130bc 150 if (!(test_bit(IPS_SEEN_REPLY_BIT, &ct->status)))
9fb9cbb1 151 if (seq_printf(s, "[UNREPLIED] "))
ea781f19 152 goto release;
9fb9cbb1 153
c88130bc 154 if (print_tuple(s, &ct->tuplehash[IP_CT_DIR_REPLY].tuple,
605dcad6 155 l3proto, l4proto))
ea781f19 156 goto release;
9fb9cbb1 157
58401572 158 if (seq_print_acct(s, ct, IP_CT_DIR_REPLY))
ea781f19 159 goto release;
9fb9cbb1 160
c88130bc 161 if (test_bit(IPS_ASSURED_BIT, &ct->status))
9fb9cbb1 162 if (seq_printf(s, "[ASSURED] "))
ea781f19 163 goto release;
9fb9cbb1
YK
164
165#if defined(CONFIG_NF_CONNTRACK_MARK)
c88130bc 166 if (seq_printf(s, "mark=%u ", ct->mark))
ea781f19 167 goto release;
9fb9cbb1
YK
168#endif
169
7c9728c3 170#ifdef CONFIG_NF_CONNTRACK_SECMARK
c88130bc 171 if (seq_printf(s, "secmark=%u ", ct->secmark))
ea781f19 172 goto release;
7c9728c3
JM
173#endif
174
5d0aa2cc
PM
175#ifdef CONFIG_NF_CONNTRACK_ZONES
176 if (seq_printf(s, "zone=%u ", nf_ct_zone(ct)))
177 goto release;
178#endif
179
c88130bc 180 if (seq_printf(s, "use=%u\n", atomic_read(&ct->ct_general.use)))
ea781f19 181 goto release;
a5d29264 182
ea781f19
ED
183 ret = 0;
184release:
185 nf_ct_put(ct);
9fb9cbb1
YK
186 return 0;
187}
188
56b3d975 189static const struct seq_operations ct_seq_ops = {
9fb9cbb1
YK
190 .start = ct_seq_start,
191 .next = ct_seq_next,
192 .stop = ct_seq_stop,
193 .show = ct_seq_show
194};
195
196static int ct_open(struct inode *inode, struct file *file)
197{
b2ce2c74 198 return seq_open_net(inode, file, &ct_seq_ops,
e2da5913 199 sizeof(struct ct_iter_state));
9fb9cbb1
YK
200}
201
da7071d7 202static const struct file_operations ct_file_ops = {
9fb9cbb1
YK
203 .owner = THIS_MODULE,
204 .open = ct_open,
205 .read = seq_read,
206 .llseek = seq_lseek,
b2ce2c74 207 .release = seq_release_net,
9fb9cbb1
YK
208};
209
9fb9cbb1
YK
210static void *ct_cpu_seq_start(struct seq_file *seq, loff_t *pos)
211{
8e9df801 212 struct net *net = seq_file_net(seq);
9fb9cbb1
YK
213 int cpu;
214
215 if (*pos == 0)
216 return SEQ_START_TOKEN;
217
0f23174a 218 for (cpu = *pos-1; cpu < nr_cpu_ids; ++cpu) {
9fb9cbb1
YK
219 if (!cpu_possible(cpu))
220 continue;
221 *pos = cpu + 1;
8e9df801 222 return per_cpu_ptr(net->ct.stat, cpu);
9fb9cbb1
YK
223 }
224
225 return NULL;
226}
227
228static void *ct_cpu_seq_next(struct seq_file *seq, void *v, loff_t *pos)
229{
8e9df801 230 struct net *net = seq_file_net(seq);
9fb9cbb1
YK
231 int cpu;
232
0f23174a 233 for (cpu = *pos; cpu < nr_cpu_ids; ++cpu) {
9fb9cbb1
YK
234 if (!cpu_possible(cpu))
235 continue;
236 *pos = cpu + 1;
8e9df801 237 return per_cpu_ptr(net->ct.stat, cpu);
9fb9cbb1
YK
238 }
239
240 return NULL;
241}
242
243static void ct_cpu_seq_stop(struct seq_file *seq, void *v)
244{
245}
246
247static int ct_cpu_seq_show(struct seq_file *seq, void *v)
248{
8e9df801
AD
249 struct net *net = seq_file_net(seq);
250 unsigned int nr_conntracks = atomic_read(&net->ct.count);
32948588 251 const struct ip_conntrack_stat *st = v;
9fb9cbb1
YK
252
253 if (v == SEQ_START_TOKEN) {
254 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");
255 return 0;
256 }
257
258 seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x %08x "
259 "%08x %08x %08x %08x %08x %08x %08x %08x \n",
260 nr_conntracks,
261 st->searched,
262 st->found,
263 st->new,
264 st->invalid,
265 st->ignore,
266 st->delete,
267 st->delete_list,
268 st->insert,
269 st->insert_failed,
270 st->drop,
271 st->early_drop,
272 st->error,
273
274 st->expect_new,
275 st->expect_create,
276 st->expect_delete
277 );
278 return 0;
279}
280
56b3d975 281static const struct seq_operations ct_cpu_seq_ops = {
9fb9cbb1
YK
282 .start = ct_cpu_seq_start,
283 .next = ct_cpu_seq_next,
284 .stop = ct_cpu_seq_stop,
285 .show = ct_cpu_seq_show,
286};
287
288static int ct_cpu_seq_open(struct inode *inode, struct file *file)
289{
8e9df801
AD
290 return seq_open_net(inode, file, &ct_cpu_seq_ops,
291 sizeof(struct seq_net_private));
9fb9cbb1
YK
292}
293
da7071d7 294static const struct file_operations ct_cpu_seq_fops = {
9fb9cbb1
YK
295 .owner = THIS_MODULE,
296 .open = ct_cpu_seq_open,
297 .read = seq_read,
298 .llseek = seq_lseek,
8e9df801 299 .release = seq_release_net,
9fb9cbb1 300};
b916f7d4 301
b2ce2c74 302static int nf_conntrack_standalone_init_proc(struct net *net)
b916f7d4
AD
303{
304 struct proc_dir_entry *pde;
305
b2ce2c74 306 pde = proc_net_fops_create(net, "nf_conntrack", 0440, &ct_file_ops);
b916f7d4
AD
307 if (!pde)
308 goto out_nf_conntrack;
52c0e111 309
b2ce2c74 310 pde = proc_create("nf_conntrack", S_IRUGO, net->proc_net_stat,
52c0e111 311 &ct_cpu_seq_fops);
b916f7d4
AD
312 if (!pde)
313 goto out_stat_nf_conntrack;
b916f7d4
AD
314 return 0;
315
316out_stat_nf_conntrack:
b2ce2c74 317 proc_net_remove(net, "nf_conntrack");
b916f7d4
AD
318out_nf_conntrack:
319 return -ENOMEM;
320}
321
b2ce2c74 322static void nf_conntrack_standalone_fini_proc(struct net *net)
b916f7d4 323{
b2ce2c74
AD
324 remove_proc_entry("nf_conntrack", net->proc_net_stat);
325 proc_net_remove(net, "nf_conntrack");
b916f7d4
AD
326}
327#else
b2ce2c74 328static int nf_conntrack_standalone_init_proc(struct net *net)
b916f7d4
AD
329{
330 return 0;
331}
332
b2ce2c74 333static void nf_conntrack_standalone_fini_proc(struct net *net)
b916f7d4
AD
334{
335}
9fb9cbb1
YK
336#endif /* CONFIG_PROC_FS */
337
338/* Sysctl support */
339
340#ifdef CONFIG_SYSCTL
9fb9cbb1
YK
341/* Log invalid packets of a given protocol */
342static int log_invalid_proto_min = 0;
343static int log_invalid_proto_max = 255;
344
9714be7d 345static struct ctl_table_header *nf_ct_netfilter_header;
9fb9cbb1
YK
346
347static ctl_table nf_ct_sysctl_table[] = {
348 {
9fb9cbb1
YK
349 .procname = "nf_conntrack_max",
350 .data = &nf_conntrack_max,
351 .maxlen = sizeof(int),
352 .mode = 0644,
6d9f239a 353 .proc_handler = proc_dointvec,
9fb9cbb1
YK
354 },
355 {
9fb9cbb1 356 .procname = "nf_conntrack_count",
49ac8713 357 .data = &init_net.ct.count,
9fb9cbb1
YK
358 .maxlen = sizeof(int),
359 .mode = 0444,
6d9f239a 360 .proc_handler = proc_dointvec,
9fb9cbb1
YK
361 },
362 {
9fb9cbb1 363 .procname = "nf_conntrack_buckets",
d696c7bd 364 .data = &init_net.ct.htable_size,
9fb9cbb1
YK
365 .maxlen = sizeof(unsigned int),
366 .mode = 0444,
6d9f239a 367 .proc_handler = proc_dointvec,
9fb9cbb1 368 },
39a27a35 369 {
39a27a35 370 .procname = "nf_conntrack_checksum",
c04d0552 371 .data = &init_net.ct.sysctl_checksum,
39a27a35
PM
372 .maxlen = sizeof(unsigned int),
373 .mode = 0644,
6d9f239a 374 .proc_handler = proc_dointvec,
39a27a35 375 },
9fb9cbb1 376 {
9fb9cbb1 377 .procname = "nf_conntrack_log_invalid",
c2a2c7e0 378 .data = &init_net.ct.sysctl_log_invalid,
9fb9cbb1
YK
379 .maxlen = sizeof(unsigned int),
380 .mode = 0644,
6d9f239a 381 .proc_handler = proc_dointvec_minmax,
9fb9cbb1
YK
382 .extra1 = &log_invalid_proto_min,
383 .extra2 = &log_invalid_proto_max,
384 },
f264a7df 385 {
f264a7df
PM
386 .procname = "nf_conntrack_expect_max",
387 .data = &nf_ct_expect_max,
388 .maxlen = sizeof(int),
389 .mode = 0644,
6d9f239a 390 .proc_handler = proc_dointvec,
f264a7df 391 },
f8572d8f 392 { }
9fb9cbb1
YK
393};
394
395#define NET_NF_CONNTRACK_MAX 2089
396
397static ctl_table nf_ct_netfilter_table[] = {
9fb9cbb1 398 {
9fb9cbb1
YK
399 .procname = "nf_conntrack_max",
400 .data = &nf_conntrack_max,
401 .maxlen = sizeof(int),
402 .mode = 0644,
6d9f239a 403 .proc_handler = proc_dointvec,
9fb9cbb1 404 },
f8572d8f 405 { }
9fb9cbb1
YK
406};
407
9e232495 408static struct ctl_path nf_ct_path[] = {
f8572d8f 409 { .procname = "net", },
3d7cc2ba 410 { }
9fb9cbb1 411};
3d7cc2ba 412
80250707 413static int nf_conntrack_standalone_init_sysctl(struct net *net)
b916f7d4 414{
80250707
AD
415 struct ctl_table *table;
416
417 if (net_eq(net, &init_net)) {
418 nf_ct_netfilter_header =
419 register_sysctl_paths(nf_ct_path, nf_ct_netfilter_table);
420 if (!nf_ct_netfilter_header)
421 goto out;
422 }
423
424 table = kmemdup(nf_ct_sysctl_table, sizeof(nf_ct_sysctl_table),
425 GFP_KERNEL);
426 if (!table)
427 goto out_kmemdup;
428
429 table[1].data = &net->ct.count;
d696c7bd 430 table[2].data = &net->ct.htable_size;
c04d0552 431 table[3].data = &net->ct.sysctl_checksum;
c2a2c7e0 432 table[4].data = &net->ct.sysctl_log_invalid;
80250707
AD
433
434 net->ct.sysctl_header = register_net_sysctl_table(net,
435 nf_net_netfilter_sysctl_path, table);
436 if (!net->ct.sysctl_header)
9714be7d
KPO
437 goto out_unregister_netfilter;
438
b916f7d4
AD
439 return 0;
440
9714be7d 441out_unregister_netfilter:
80250707
AD
442 kfree(table);
443out_kmemdup:
444 if (net_eq(net, &init_net))
445 unregister_sysctl_table(nf_ct_netfilter_header);
9714be7d
KPO
446out:
447 printk("nf_conntrack: can't register to sysctl.\n");
448 return -ENOMEM;
b916f7d4
AD
449}
450
80250707 451static void nf_conntrack_standalone_fini_sysctl(struct net *net)
b916f7d4 452{
80250707
AD
453 struct ctl_table *table;
454
455 if (net_eq(net, &init_net))
456 unregister_sysctl_table(nf_ct_netfilter_header);
457 table = net->ct.sysctl_header->ctl_table_arg;
458 unregister_net_sysctl_table(net->ct.sysctl_header);
459 kfree(table);
b916f7d4
AD
460}
461#else
80250707 462static int nf_conntrack_standalone_init_sysctl(struct net *net)
b916f7d4
AD
463{
464 return 0;
465}
466
80250707 467static void nf_conntrack_standalone_fini_sysctl(struct net *net)
b916f7d4
AD
468{
469}
9fb9cbb1
YK
470#endif /* CONFIG_SYSCTL */
471
dfdb8d79
AD
472static int nf_conntrack_net_init(struct net *net)
473{
b2ce2c74
AD
474 int ret;
475
476 ret = nf_conntrack_init(net);
477 if (ret < 0)
478 goto out_init;
479 ret = nf_conntrack_standalone_init_proc(net);
480 if (ret < 0)
481 goto out_proc;
c04d0552 482 net->ct.sysctl_checksum = 1;
c2a2c7e0 483 net->ct.sysctl_log_invalid = 0;
80250707
AD
484 ret = nf_conntrack_standalone_init_sysctl(net);
485 if (ret < 0)
486 goto out_sysctl;
b2ce2c74
AD
487 return 0;
488
80250707
AD
489out_sysctl:
490 nf_conntrack_standalone_fini_proc(net);
b2ce2c74
AD
491out_proc:
492 nf_conntrack_cleanup(net);
493out_init:
494 return ret;
dfdb8d79
AD
495}
496
497static void nf_conntrack_net_exit(struct net *net)
498{
80250707 499 nf_conntrack_standalone_fini_sysctl(net);
b2ce2c74 500 nf_conntrack_standalone_fini_proc(net);
dfdb8d79
AD
501 nf_conntrack_cleanup(net);
502}
503
504static struct pernet_operations nf_conntrack_net_ops = {
505 .init = nf_conntrack_net_init,
506 .exit = nf_conntrack_net_exit,
507};
508
65b4b4e8 509static int __init nf_conntrack_standalone_init(void)
9fb9cbb1 510{
80250707 511 return register_pernet_subsys(&nf_conntrack_net_ops);
9fb9cbb1
YK
512}
513
65b4b4e8 514static void __exit nf_conntrack_standalone_fini(void)
9fb9cbb1 515{
dfdb8d79 516 unregister_pernet_subsys(&nf_conntrack_net_ops);
9fb9cbb1
YK
517}
518
65b4b4e8
AM
519module_init(nf_conntrack_standalone_init);
520module_exit(nf_conntrack_standalone_fini);
9fb9cbb1
YK
521
522/* Some modules need us, but don't depend directly on any symbol.
523 They should call this. */
2e4e6a17 524void need_conntrack(void)
9fb9cbb1
YK
525{
526}
13b18339 527EXPORT_SYMBOL_GPL(need_conntrack);