netfilter: nf_conntrack: push zone object into functions
[deliverable/linux.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 * (C) 2005-2012 Patrick McHardy <kaber@trash.net>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 */
9
10 #include <linux/types.h>
11 #include <linux/netfilter.h>
12 #include <linux/slab.h>
13 #include <linux/module.h>
14 #include <linux/skbuff.h>
15 #include <linux/proc_fs.h>
16 #include <linux/seq_file.h>
17 #include <linux/percpu.h>
18 #include <linux/netdevice.h>
19 #include <linux/security.h>
20 #include <net/net_namespace.h>
21 #ifdef CONFIG_SYSCTL
22 #include <linux/sysctl.h>
23 #endif
24
25 #include <net/netfilter/nf_conntrack.h>
26 #include <net/netfilter/nf_conntrack_core.h>
27 #include <net/netfilter/nf_conntrack_l3proto.h>
28 #include <net/netfilter/nf_conntrack_l4proto.h>
29 #include <net/netfilter/nf_conntrack_expect.h>
30 #include <net/netfilter/nf_conntrack_helper.h>
31 #include <net/netfilter/nf_conntrack_acct.h>
32 #include <net/netfilter/nf_conntrack_zones.h>
33 #include <net/netfilter/nf_conntrack_timestamp.h>
34 #include <linux/rculist_nulls.h>
35
36 MODULE_LICENSE("GPL");
37
38 #ifdef CONFIG_NF_CONNTRACK_PROCFS
39 void
40 print_tuple(struct seq_file *s, const struct nf_conntrack_tuple *tuple,
41 const struct nf_conntrack_l3proto *l3proto,
42 const struct nf_conntrack_l4proto *l4proto)
43 {
44 l3proto->print_tuple(s, tuple);
45 l4proto->print_tuple(s, tuple);
46 }
47 EXPORT_SYMBOL_GPL(print_tuple);
48
49 struct ct_iter_state {
50 struct seq_net_private p;
51 unsigned int bucket;
52 u_int64_t time_now;
53 };
54
55 static struct hlist_nulls_node *ct_get_first(struct seq_file *seq)
56 {
57 struct net *net = seq_file_net(seq);
58 struct ct_iter_state *st = seq->private;
59 struct hlist_nulls_node *n;
60
61 for (st->bucket = 0;
62 st->bucket < net->ct.htable_size;
63 st->bucket++) {
64 n = rcu_dereference(hlist_nulls_first_rcu(&net->ct.hash[st->bucket]));
65 if (!is_a_nulls(n))
66 return n;
67 }
68 return NULL;
69 }
70
71 static struct hlist_nulls_node *ct_get_next(struct seq_file *seq,
72 struct hlist_nulls_node *head)
73 {
74 struct net *net = seq_file_net(seq);
75 struct ct_iter_state *st = seq->private;
76
77 head = rcu_dereference(hlist_nulls_next_rcu(head));
78 while (is_a_nulls(head)) {
79 if (likely(get_nulls_value(head) == st->bucket)) {
80 if (++st->bucket >= net->ct.htable_size)
81 return NULL;
82 }
83 head = rcu_dereference(
84 hlist_nulls_first_rcu(
85 &net->ct.hash[st->bucket]));
86 }
87 return head;
88 }
89
90 static struct hlist_nulls_node *ct_get_idx(struct seq_file *seq, loff_t pos)
91 {
92 struct hlist_nulls_node *head = ct_get_first(seq);
93
94 if (head)
95 while (pos && (head = ct_get_next(seq, head)))
96 pos--;
97 return pos ? NULL : head;
98 }
99
100 static void *ct_seq_start(struct seq_file *seq, loff_t *pos)
101 __acquires(RCU)
102 {
103 struct ct_iter_state *st = seq->private;
104
105 st->time_now = ktime_get_real_ns();
106 rcu_read_lock();
107 return ct_get_idx(seq, *pos);
108 }
109
110 static void *ct_seq_next(struct seq_file *s, void *v, loff_t *pos)
111 {
112 (*pos)++;
113 return ct_get_next(s, v);
114 }
115
116 static void ct_seq_stop(struct seq_file *s, void *v)
117 __releases(RCU)
118 {
119 rcu_read_unlock();
120 }
121
122 #ifdef CONFIG_NF_CONNTRACK_SECMARK
123 static void ct_show_secctx(struct seq_file *s, const struct nf_conn *ct)
124 {
125 int ret;
126 u32 len;
127 char *secctx;
128
129 ret = security_secid_to_secctx(ct->secmark, &secctx, &len);
130 if (ret)
131 return;
132
133 seq_printf(s, "secctx=%s ", secctx);
134
135 security_release_secctx(secctx, len);
136 }
137 #else
138 static inline void ct_show_secctx(struct seq_file *s, const struct nf_conn *ct)
139 {
140 }
141 #endif
142
143 #ifdef CONFIG_NF_CONNTRACK_ZONES
144 static void ct_show_zone(struct seq_file *s, const struct nf_conn *ct)
145 {
146 seq_printf(s, "zone=%u ", nf_ct_zone(ct)->id);
147 }
148 #else
149 static inline void ct_show_zone(struct seq_file *s, const struct nf_conn *ct)
150 {
151 }
152 #endif
153
154 #ifdef CONFIG_NF_CONNTRACK_TIMESTAMP
155 static void ct_show_delta_time(struct seq_file *s, const struct nf_conn *ct)
156 {
157 struct ct_iter_state *st = s->private;
158 struct nf_conn_tstamp *tstamp;
159 s64 delta_time;
160
161 tstamp = nf_conn_tstamp_find(ct);
162 if (tstamp) {
163 delta_time = st->time_now - tstamp->start;
164 if (delta_time > 0)
165 delta_time = div_s64(delta_time, NSEC_PER_SEC);
166 else
167 delta_time = 0;
168
169 seq_printf(s, "delta-time=%llu ",
170 (unsigned long long)delta_time);
171 }
172 return;
173 }
174 #else
175 static inline void
176 ct_show_delta_time(struct seq_file *s, const struct nf_conn *ct)
177 {
178 }
179 #endif
180
181 /* return 0 on success, 1 in case of error */
182 static int ct_seq_show(struct seq_file *s, void *v)
183 {
184 struct nf_conntrack_tuple_hash *hash = v;
185 struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(hash);
186 const struct nf_conntrack_l3proto *l3proto;
187 const struct nf_conntrack_l4proto *l4proto;
188 int ret = 0;
189
190 NF_CT_ASSERT(ct);
191 if (unlikely(!atomic_inc_not_zero(&ct->ct_general.use)))
192 return 0;
193
194 /* we only want to print DIR_ORIGINAL */
195 if (NF_CT_DIRECTION(hash))
196 goto release;
197
198 l3proto = __nf_ct_l3proto_find(nf_ct_l3num(ct));
199 NF_CT_ASSERT(l3proto);
200 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
201 NF_CT_ASSERT(l4proto);
202
203 ret = -ENOSPC;
204 seq_printf(s, "%-8s %u %-8s %u %ld ",
205 l3proto->name, nf_ct_l3num(ct),
206 l4proto->name, nf_ct_protonum(ct),
207 timer_pending(&ct->timeout)
208 ? (long)(ct->timeout.expires - jiffies)/HZ : 0);
209
210 if (l4proto->print_conntrack)
211 l4proto->print_conntrack(s, ct);
212
213 print_tuple(s, &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
214 l3proto, l4proto);
215
216 if (seq_has_overflowed(s))
217 goto release;
218
219 if (seq_print_acct(s, ct, IP_CT_DIR_ORIGINAL))
220 goto release;
221
222 if (!(test_bit(IPS_SEEN_REPLY_BIT, &ct->status)))
223 seq_printf(s, "[UNREPLIED] ");
224
225 print_tuple(s, &ct->tuplehash[IP_CT_DIR_REPLY].tuple,
226 l3proto, l4proto);
227
228 if (seq_print_acct(s, ct, IP_CT_DIR_REPLY))
229 goto release;
230
231 if (test_bit(IPS_ASSURED_BIT, &ct->status))
232 seq_printf(s, "[ASSURED] ");
233
234 if (seq_has_overflowed(s))
235 goto release;
236
237 #if defined(CONFIG_NF_CONNTRACK_MARK)
238 seq_printf(s, "mark=%u ", ct->mark);
239 #endif
240
241 ct_show_secctx(s, ct);
242 ct_show_zone(s, ct);
243 ct_show_delta_time(s, ct);
244
245 seq_printf(s, "use=%u\n", atomic_read(&ct->ct_general.use));
246
247 if (seq_has_overflowed(s))
248 goto release;
249
250 ret = 0;
251 release:
252 nf_ct_put(ct);
253 return ret;
254 }
255
256 static const struct seq_operations ct_seq_ops = {
257 .start = ct_seq_start,
258 .next = ct_seq_next,
259 .stop = ct_seq_stop,
260 .show = ct_seq_show
261 };
262
263 static int ct_open(struct inode *inode, struct file *file)
264 {
265 return seq_open_net(inode, file, &ct_seq_ops,
266 sizeof(struct ct_iter_state));
267 }
268
269 static const struct file_operations ct_file_ops = {
270 .owner = THIS_MODULE,
271 .open = ct_open,
272 .read = seq_read,
273 .llseek = seq_lseek,
274 .release = seq_release_net,
275 };
276
277 static void *ct_cpu_seq_start(struct seq_file *seq, loff_t *pos)
278 {
279 struct net *net = seq_file_net(seq);
280 int cpu;
281
282 if (*pos == 0)
283 return SEQ_START_TOKEN;
284
285 for (cpu = *pos-1; cpu < nr_cpu_ids; ++cpu) {
286 if (!cpu_possible(cpu))
287 continue;
288 *pos = cpu + 1;
289 return per_cpu_ptr(net->ct.stat, cpu);
290 }
291
292 return NULL;
293 }
294
295 static void *ct_cpu_seq_next(struct seq_file *seq, void *v, loff_t *pos)
296 {
297 struct net *net = seq_file_net(seq);
298 int cpu;
299
300 for (cpu = *pos; cpu < nr_cpu_ids; ++cpu) {
301 if (!cpu_possible(cpu))
302 continue;
303 *pos = cpu + 1;
304 return per_cpu_ptr(net->ct.stat, cpu);
305 }
306
307 return NULL;
308 }
309
310 static void ct_cpu_seq_stop(struct seq_file *seq, void *v)
311 {
312 }
313
314 static int ct_cpu_seq_show(struct seq_file *seq, void *v)
315 {
316 struct net *net = seq_file_net(seq);
317 unsigned int nr_conntracks = atomic_read(&net->ct.count);
318 const struct ip_conntrack_stat *st = v;
319
320 if (v == SEQ_START_TOKEN) {
321 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");
322 return 0;
323 }
324
325 seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x %08x "
326 "%08x %08x %08x %08x %08x %08x %08x %08x %08x\n",
327 nr_conntracks,
328 st->searched,
329 st->found,
330 st->new,
331 st->invalid,
332 st->ignore,
333 st->delete,
334 st->delete_list,
335 st->insert,
336 st->insert_failed,
337 st->drop,
338 st->early_drop,
339 st->error,
340
341 st->expect_new,
342 st->expect_create,
343 st->expect_delete,
344 st->search_restart
345 );
346 return 0;
347 }
348
349 static const struct seq_operations ct_cpu_seq_ops = {
350 .start = ct_cpu_seq_start,
351 .next = ct_cpu_seq_next,
352 .stop = ct_cpu_seq_stop,
353 .show = ct_cpu_seq_show,
354 };
355
356 static int ct_cpu_seq_open(struct inode *inode, struct file *file)
357 {
358 return seq_open_net(inode, file, &ct_cpu_seq_ops,
359 sizeof(struct seq_net_private));
360 }
361
362 static const struct file_operations ct_cpu_seq_fops = {
363 .owner = THIS_MODULE,
364 .open = ct_cpu_seq_open,
365 .read = seq_read,
366 .llseek = seq_lseek,
367 .release = seq_release_net,
368 };
369
370 static int nf_conntrack_standalone_init_proc(struct net *net)
371 {
372 struct proc_dir_entry *pde;
373
374 pde = proc_create("nf_conntrack", 0440, net->proc_net, &ct_file_ops);
375 if (!pde)
376 goto out_nf_conntrack;
377
378 pde = proc_create("nf_conntrack", S_IRUGO, net->proc_net_stat,
379 &ct_cpu_seq_fops);
380 if (!pde)
381 goto out_stat_nf_conntrack;
382 return 0;
383
384 out_stat_nf_conntrack:
385 remove_proc_entry("nf_conntrack", net->proc_net);
386 out_nf_conntrack:
387 return -ENOMEM;
388 }
389
390 static void nf_conntrack_standalone_fini_proc(struct net *net)
391 {
392 remove_proc_entry("nf_conntrack", net->proc_net_stat);
393 remove_proc_entry("nf_conntrack", net->proc_net);
394 }
395 #else
396 static int nf_conntrack_standalone_init_proc(struct net *net)
397 {
398 return 0;
399 }
400
401 static void nf_conntrack_standalone_fini_proc(struct net *net)
402 {
403 }
404 #endif /* CONFIG_NF_CONNTRACK_PROCFS */
405
406 /* Sysctl support */
407
408 #ifdef CONFIG_SYSCTL
409 /* Log invalid packets of a given protocol */
410 static int log_invalid_proto_min = 0;
411 static int log_invalid_proto_max = 255;
412
413 static struct ctl_table_header *nf_ct_netfilter_header;
414
415 static struct ctl_table nf_ct_sysctl_table[] = {
416 {
417 .procname = "nf_conntrack_max",
418 .data = &nf_conntrack_max,
419 .maxlen = sizeof(int),
420 .mode = 0644,
421 .proc_handler = proc_dointvec,
422 },
423 {
424 .procname = "nf_conntrack_count",
425 .data = &init_net.ct.count,
426 .maxlen = sizeof(int),
427 .mode = 0444,
428 .proc_handler = proc_dointvec,
429 },
430 {
431 .procname = "nf_conntrack_buckets",
432 .data = &init_net.ct.htable_size,
433 .maxlen = sizeof(unsigned int),
434 .mode = 0444,
435 .proc_handler = proc_dointvec,
436 },
437 {
438 .procname = "nf_conntrack_checksum",
439 .data = &init_net.ct.sysctl_checksum,
440 .maxlen = sizeof(unsigned int),
441 .mode = 0644,
442 .proc_handler = proc_dointvec,
443 },
444 {
445 .procname = "nf_conntrack_log_invalid",
446 .data = &init_net.ct.sysctl_log_invalid,
447 .maxlen = sizeof(unsigned int),
448 .mode = 0644,
449 .proc_handler = proc_dointvec_minmax,
450 .extra1 = &log_invalid_proto_min,
451 .extra2 = &log_invalid_proto_max,
452 },
453 {
454 .procname = "nf_conntrack_expect_max",
455 .data = &nf_ct_expect_max,
456 .maxlen = sizeof(int),
457 .mode = 0644,
458 .proc_handler = proc_dointvec,
459 },
460 { }
461 };
462
463 #define NET_NF_CONNTRACK_MAX 2089
464
465 static struct ctl_table nf_ct_netfilter_table[] = {
466 {
467 .procname = "nf_conntrack_max",
468 .data = &nf_conntrack_max,
469 .maxlen = sizeof(int),
470 .mode = 0644,
471 .proc_handler = proc_dointvec,
472 },
473 { }
474 };
475
476 static int nf_conntrack_standalone_init_sysctl(struct net *net)
477 {
478 struct ctl_table *table;
479
480 table = kmemdup(nf_ct_sysctl_table, sizeof(nf_ct_sysctl_table),
481 GFP_KERNEL);
482 if (!table)
483 goto out_kmemdup;
484
485 table[1].data = &net->ct.count;
486 table[2].data = &net->ct.htable_size;
487 table[3].data = &net->ct.sysctl_checksum;
488 table[4].data = &net->ct.sysctl_log_invalid;
489
490 /* Don't export sysctls to unprivileged users */
491 if (net->user_ns != &init_user_ns)
492 table[0].procname = NULL;
493
494 net->ct.sysctl_header = register_net_sysctl(net, "net/netfilter", table);
495 if (!net->ct.sysctl_header)
496 goto out_unregister_netfilter;
497
498 return 0;
499
500 out_unregister_netfilter:
501 kfree(table);
502 out_kmemdup:
503 return -ENOMEM;
504 }
505
506 static void nf_conntrack_standalone_fini_sysctl(struct net *net)
507 {
508 struct ctl_table *table;
509
510 table = net->ct.sysctl_header->ctl_table_arg;
511 unregister_net_sysctl_table(net->ct.sysctl_header);
512 kfree(table);
513 }
514 #else
515 static int nf_conntrack_standalone_init_sysctl(struct net *net)
516 {
517 return 0;
518 }
519
520 static void nf_conntrack_standalone_fini_sysctl(struct net *net)
521 {
522 }
523 #endif /* CONFIG_SYSCTL */
524
525 static int nf_conntrack_pernet_init(struct net *net)
526 {
527 int ret;
528
529 ret = nf_conntrack_init_net(net);
530 if (ret < 0)
531 goto out_init;
532
533 ret = nf_conntrack_standalone_init_proc(net);
534 if (ret < 0)
535 goto out_proc;
536
537 net->ct.sysctl_checksum = 1;
538 net->ct.sysctl_log_invalid = 0;
539 ret = nf_conntrack_standalone_init_sysctl(net);
540 if (ret < 0)
541 goto out_sysctl;
542
543 return 0;
544
545 out_sysctl:
546 nf_conntrack_standalone_fini_proc(net);
547 out_proc:
548 nf_conntrack_cleanup_net(net);
549 out_init:
550 return ret;
551 }
552
553 static void nf_conntrack_pernet_exit(struct list_head *net_exit_list)
554 {
555 struct net *net;
556
557 list_for_each_entry(net, net_exit_list, exit_list) {
558 nf_conntrack_standalone_fini_sysctl(net);
559 nf_conntrack_standalone_fini_proc(net);
560 }
561 nf_conntrack_cleanup_net_list(net_exit_list);
562 }
563
564 static struct pernet_operations nf_conntrack_net_ops = {
565 .init = nf_conntrack_pernet_init,
566 .exit_batch = nf_conntrack_pernet_exit,
567 };
568
569 static int __init nf_conntrack_standalone_init(void)
570 {
571 int ret = nf_conntrack_init_start();
572 if (ret < 0)
573 goto out_start;
574
575 #ifdef CONFIG_SYSCTL
576 nf_ct_netfilter_header =
577 register_net_sysctl(&init_net, "net", nf_ct_netfilter_table);
578 if (!nf_ct_netfilter_header) {
579 pr_err("nf_conntrack: can't register to sysctl.\n");
580 ret = -ENOMEM;
581 goto out_sysctl;
582 }
583 #endif
584
585 ret = register_pernet_subsys(&nf_conntrack_net_ops);
586 if (ret < 0)
587 goto out_pernet;
588
589 nf_conntrack_init_end();
590 return 0;
591
592 out_pernet:
593 #ifdef CONFIG_SYSCTL
594 unregister_net_sysctl_table(nf_ct_netfilter_header);
595 out_sysctl:
596 #endif
597 nf_conntrack_cleanup_end();
598 out_start:
599 return ret;
600 }
601
602 static void __exit nf_conntrack_standalone_fini(void)
603 {
604 nf_conntrack_cleanup_start();
605 unregister_pernet_subsys(&nf_conntrack_net_ops);
606 #ifdef CONFIG_SYSCTL
607 unregister_net_sysctl_table(nf_ct_netfilter_header);
608 #endif
609 nf_conntrack_cleanup_end();
610 }
611
612 module_init(nf_conntrack_standalone_init);
613 module_exit(nf_conntrack_standalone_fini);
614
615 /* Some modules need us, but don't depend directly on any symbol.
616 They should call this. */
617 void need_conntrack(void)
618 {
619 }
620 EXPORT_SYMBOL_GPL(need_conntrack);
This page took 0.043285 seconds and 5 git commands to generate.