netfilter: nf_conntrack_tstamp: add flow-based timestamp extension
[firefly-linux-kernel-4.4.55.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/slab.h>
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>
18 #include <linux/security.h>
19 #include <net/net_namespace.h>
20 #ifdef CONFIG_SYSCTL
21 #include <linux/sysctl.h>
22 #endif
23
24 #include <net/netfilter/nf_conntrack.h>
25 #include <net/netfilter/nf_conntrack_core.h>
26 #include <net/netfilter/nf_conntrack_l3proto.h>
27 #include <net/netfilter/nf_conntrack_l4proto.h>
28 #include <net/netfilter/nf_conntrack_expect.h>
29 #include <net/netfilter/nf_conntrack_helper.h>
30 #include <net/netfilter/nf_conntrack_acct.h>
31 #include <net/netfilter/nf_conntrack_zones.h>
32 #include <net/netfilter/nf_conntrack_timestamp.h>
33 #include <linux/rculist_nulls.h>
34
35 MODULE_LICENSE("GPL");
36
37 #ifdef CONFIG_PROC_FS
38 int
39 print_tuple(struct seq_file *s, const struct nf_conntrack_tuple *tuple,
40             const struct nf_conntrack_l3proto *l3proto,
41             const struct nf_conntrack_l4proto *l4proto)
42 {
43         return l3proto->print_tuple(s, tuple) || l4proto->print_tuple(s, tuple);
44 }
45 EXPORT_SYMBOL_GPL(print_tuple);
46
47 struct ct_iter_state {
48         struct seq_net_private p;
49         unsigned int bucket;
50         u_int64_t time_now;
51 };
52
53 static struct hlist_nulls_node *ct_get_first(struct seq_file *seq)
54 {
55         struct net *net = seq_file_net(seq);
56         struct ct_iter_state *st = seq->private;
57         struct hlist_nulls_node *n;
58
59         for (st->bucket = 0;
60              st->bucket < net->ct.htable_size;
61              st->bucket++) {
62                 n = rcu_dereference(hlist_nulls_first_rcu(&net->ct.hash[st->bucket]));
63                 if (!is_a_nulls(n))
64                         return n;
65         }
66         return NULL;
67 }
68
69 static struct hlist_nulls_node *ct_get_next(struct seq_file *seq,
70                                       struct hlist_nulls_node *head)
71 {
72         struct net *net = seq_file_net(seq);
73         struct ct_iter_state *st = seq->private;
74
75         head = rcu_dereference(hlist_nulls_next_rcu(head));
76         while (is_a_nulls(head)) {
77                 if (likely(get_nulls_value(head) == st->bucket)) {
78                         if (++st->bucket >= net->ct.htable_size)
79                                 return NULL;
80                 }
81                 head = rcu_dereference(
82                                 hlist_nulls_first_rcu(
83                                         &net->ct.hash[st->bucket]));
84         }
85         return head;
86 }
87
88 static struct hlist_nulls_node *ct_get_idx(struct seq_file *seq, loff_t pos)
89 {
90         struct hlist_nulls_node *head = ct_get_first(seq);
91
92         if (head)
93                 while (pos && (head = ct_get_next(seq, head)))
94                         pos--;
95         return pos ? NULL : head;
96 }
97
98 static void *ct_seq_start(struct seq_file *seq, loff_t *pos)
99         __acquires(RCU)
100 {
101         struct ct_iter_state *st = seq->private;
102
103         st->time_now = ktime_to_ns(ktime_get_real());
104         rcu_read_lock();
105         return ct_get_idx(seq, *pos);
106 }
107
108 static void *ct_seq_next(struct seq_file *s, void *v, loff_t *pos)
109 {
110         (*pos)++;
111         return ct_get_next(s, v);
112 }
113
114 static void ct_seq_stop(struct seq_file *s, void *v)
115         __releases(RCU)
116 {
117         rcu_read_unlock();
118 }
119
120 #ifdef CONFIG_NF_CONNTRACK_SECMARK
121 static int ct_show_secctx(struct seq_file *s, const struct nf_conn *ct)
122 {
123         int ret;
124         u32 len;
125         char *secctx;
126
127         ret = security_secid_to_secctx(ct->secmark, &secctx, &len);
128         if (ret)
129                 return 0;
130
131         ret = seq_printf(s, "secctx=%s ", secctx);
132
133         security_release_secctx(secctx, len);
134         return ret;
135 }
136 #else
137 static inline int ct_show_secctx(struct seq_file *s, const struct nf_conn *ct)
138 {
139         return 0;
140 }
141 #endif
142
143 #ifdef CONFIG_NF_CONNTRACK_TIMESTAMP
144 static u_int64_t ct_delta_time(u_int64_t time_now, const struct nf_conn *ct)
145 {
146         struct nf_conn_tstamp *tstamp;
147
148         tstamp = nf_conn_tstamp_find(ct);
149         if (tstamp) {
150                 u_int64_t delta_time = time_now - tstamp->start;
151                 return delta_time > 0 ? div_s64(delta_time, NSEC_PER_SEC) : 0;
152         }
153         return -1;
154 }
155
156 static int ct_show_delta_time(struct seq_file *s, const struct nf_conn *ct)
157 {
158         struct ct_iter_state *st = s->private;
159         u_int64_t delta_time;
160
161         delta_time = ct_delta_time(st->time_now, ct);
162         if (delta_time < 0)
163                 return 0;
164
165         return seq_printf(s, "delta-time=%llu ",
166                           (unsigned long long)delta_time);
167 }
168 #else
169 static inline int
170 ct_show_delta_time(struct seq_file *s, const struct nf_conn *ct)
171 {
172         return 0;
173 }
174 #endif
175
176 /* return 0 on success, 1 in case of error */
177 static int ct_seq_show(struct seq_file *s, void *v)
178 {
179         struct nf_conntrack_tuple_hash *hash = v;
180         struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(hash);
181         const struct nf_conntrack_l3proto *l3proto;
182         const struct nf_conntrack_l4proto *l4proto;
183         int ret = 0;
184
185         NF_CT_ASSERT(ct);
186         if (unlikely(!atomic_inc_not_zero(&ct->ct_general.use)))
187                 return 0;
188
189         /* we only want to print DIR_ORIGINAL */
190         if (NF_CT_DIRECTION(hash))
191                 goto release;
192
193         l3proto = __nf_ct_l3proto_find(nf_ct_l3num(ct));
194         NF_CT_ASSERT(l3proto);
195         l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
196         NF_CT_ASSERT(l4proto);
197
198         ret = -ENOSPC;
199         if (seq_printf(s, "%-8s %u %-8s %u %ld ",
200                        l3proto->name, nf_ct_l3num(ct),
201                        l4proto->name, nf_ct_protonum(ct),
202                        timer_pending(&ct->timeout)
203                        ? (long)(ct->timeout.expires - jiffies)/HZ : 0) != 0)
204                 goto release;
205
206         if (l4proto->print_conntrack && l4proto->print_conntrack(s, ct))
207                 goto release;
208
209         if (print_tuple(s, &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
210                         l3proto, l4proto))
211                 goto release;
212
213         if (seq_print_acct(s, ct, IP_CT_DIR_ORIGINAL))
214                 goto release;
215
216         if (!(test_bit(IPS_SEEN_REPLY_BIT, &ct->status)))
217                 if (seq_printf(s, "[UNREPLIED] "))
218                         goto release;
219
220         if (print_tuple(s, &ct->tuplehash[IP_CT_DIR_REPLY].tuple,
221                         l3proto, l4proto))
222                 goto release;
223
224         if (seq_print_acct(s, ct, IP_CT_DIR_REPLY))
225                 goto release;
226
227         if (test_bit(IPS_ASSURED_BIT, &ct->status))
228                 if (seq_printf(s, "[ASSURED] "))
229                         goto release;
230
231 #if defined(CONFIG_NF_CONNTRACK_MARK)
232         if (seq_printf(s, "mark=%u ", ct->mark))
233                 goto release;
234 #endif
235
236         if (ct_show_secctx(s, ct))
237                 goto release;
238
239 #ifdef CONFIG_NF_CONNTRACK_ZONES
240         if (seq_printf(s, "zone=%u ", nf_ct_zone(ct)))
241                 goto release;
242 #endif
243
244         if (ct_show_delta_time(s, ct))
245                 goto release;
246
247         if (seq_printf(s, "use=%u\n", atomic_read(&ct->ct_general.use)))
248                 goto release;
249
250         ret = 0;
251 release:
252         nf_ct_put(ct);
253         return 0;
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_net_fops_create(net, "nf_conntrack", 0440, &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         proc_net_remove(net, "nf_conntrack");
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         proc_net_remove(net, "nf_conntrack");
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_PROC_FS */
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 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 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 struct ctl_path nf_ct_path[] = {
477         { .procname = "net", },
478         { }
479 };
480
481 static int nf_conntrack_standalone_init_sysctl(struct net *net)
482 {
483         struct ctl_table *table;
484
485         if (net_eq(net, &init_net)) {
486                 nf_ct_netfilter_header =
487                        register_sysctl_paths(nf_ct_path, nf_ct_netfilter_table);
488                 if (!nf_ct_netfilter_header)
489                         goto out;
490         }
491
492         table = kmemdup(nf_ct_sysctl_table, sizeof(nf_ct_sysctl_table),
493                         GFP_KERNEL);
494         if (!table)
495                 goto out_kmemdup;
496
497         table[1].data = &net->ct.count;
498         table[2].data = &net->ct.htable_size;
499         table[3].data = &net->ct.sysctl_checksum;
500         table[4].data = &net->ct.sysctl_log_invalid;
501
502         net->ct.sysctl_header = register_net_sysctl_table(net,
503                                         nf_net_netfilter_sysctl_path, table);
504         if (!net->ct.sysctl_header)
505                 goto out_unregister_netfilter;
506
507         return 0;
508
509 out_unregister_netfilter:
510         kfree(table);
511 out_kmemdup:
512         if (net_eq(net, &init_net))
513                 unregister_sysctl_table(nf_ct_netfilter_header);
514 out:
515         printk(KERN_ERR "nf_conntrack: can't register to sysctl.\n");
516         return -ENOMEM;
517 }
518
519 static void nf_conntrack_standalone_fini_sysctl(struct net *net)
520 {
521         struct ctl_table *table;
522
523         if (net_eq(net, &init_net))
524                 unregister_sysctl_table(nf_ct_netfilter_header);
525         table = net->ct.sysctl_header->ctl_table_arg;
526         unregister_net_sysctl_table(net->ct.sysctl_header);
527         kfree(table);
528 }
529 #else
530 static int nf_conntrack_standalone_init_sysctl(struct net *net)
531 {
532         return 0;
533 }
534
535 static void nf_conntrack_standalone_fini_sysctl(struct net *net)
536 {
537 }
538 #endif /* CONFIG_SYSCTL */
539
540 static int nf_conntrack_net_init(struct net *net)
541 {
542         int ret;
543
544         ret = nf_conntrack_init(net);
545         if (ret < 0)
546                 goto out_init;
547         ret = nf_conntrack_standalone_init_proc(net);
548         if (ret < 0)
549                 goto out_proc;
550         net->ct.sysctl_checksum = 1;
551         net->ct.sysctl_log_invalid = 0;
552         ret = nf_conntrack_standalone_init_sysctl(net);
553         if (ret < 0)
554                 goto out_sysctl;
555         return 0;
556
557 out_sysctl:
558         nf_conntrack_standalone_fini_proc(net);
559 out_proc:
560         nf_conntrack_cleanup(net);
561 out_init:
562         return ret;
563 }
564
565 static void nf_conntrack_net_exit(struct net *net)
566 {
567         nf_conntrack_standalone_fini_sysctl(net);
568         nf_conntrack_standalone_fini_proc(net);
569         nf_conntrack_cleanup(net);
570 }
571
572 static struct pernet_operations nf_conntrack_net_ops = {
573         .init = nf_conntrack_net_init,
574         .exit = nf_conntrack_net_exit,
575 };
576
577 static int __init nf_conntrack_standalone_init(void)
578 {
579         return register_pernet_subsys(&nf_conntrack_net_ops);
580 }
581
582 static void __exit nf_conntrack_standalone_fini(void)
583 {
584         unregister_pernet_subsys(&nf_conntrack_net_ops);
585 }
586
587 module_init(nf_conntrack_standalone_init);
588 module_exit(nf_conntrack_standalone_fini);
589
590 /* Some modules need us, but don't depend directly on any symbol.
591    They should call this. */
592 void need_conntrack(void)
593 {
594 }
595 EXPORT_SYMBOL_GPL(need_conntrack);