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