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