Pablo Neira Ayuso | a992ca2 | 2011-01-19 16:00:07 +0100 | [diff] [blame] | 1 | /* |
| 2 | * (C) 2010 Pablo Neira Ayuso <pablo@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 (or any later at your option). |
| 7 | */ |
| 8 | |
| 9 | #include <linux/netfilter.h> |
| 10 | #include <linux/slab.h> |
| 11 | #include <linux/kernel.h> |
| 12 | #include <linux/moduleparam.h> |
| 13 | |
| 14 | #include <net/netfilter/nf_conntrack.h> |
| 15 | #include <net/netfilter/nf_conntrack_extend.h> |
| 16 | #include <net/netfilter/nf_conntrack_timestamp.h> |
| 17 | |
Rusty Russell | eb93992 | 2011-12-19 14:08:01 +0000 | [diff] [blame] | 18 | static bool nf_ct_tstamp __read_mostly; |
Pablo Neira Ayuso | a992ca2 | 2011-01-19 16:00:07 +0100 | [diff] [blame] | 19 | |
| 20 | module_param_named(tstamp, nf_ct_tstamp, bool, 0644); |
| 21 | MODULE_PARM_DESC(tstamp, "Enable connection tracking flow timestamping."); |
| 22 | |
| 23 | #ifdef CONFIG_SYSCTL |
| 24 | static struct ctl_table tstamp_sysctl_table[] = { |
| 25 | { |
| 26 | .procname = "nf_conntrack_timestamp", |
| 27 | .data = &init_net.ct.sysctl_tstamp, |
| 28 | .maxlen = sizeof(unsigned int), |
| 29 | .mode = 0644, |
| 30 | .proc_handler = proc_dointvec, |
| 31 | }, |
| 32 | {} |
| 33 | }; |
| 34 | #endif /* CONFIG_SYSCTL */ |
| 35 | |
| 36 | static struct nf_ct_ext_type tstamp_extend __read_mostly = { |
| 37 | .len = sizeof(struct nf_conn_tstamp), |
| 38 | .align = __alignof__(struct nf_conn_tstamp), |
| 39 | .id = NF_CT_EXT_TSTAMP, |
| 40 | }; |
| 41 | |
| 42 | #ifdef CONFIG_SYSCTL |
| 43 | static int nf_conntrack_tstamp_init_sysctl(struct net *net) |
| 44 | { |
| 45 | struct ctl_table *table; |
| 46 | |
| 47 | table = kmemdup(tstamp_sysctl_table, sizeof(tstamp_sysctl_table), |
| 48 | GFP_KERNEL); |
| 49 | if (!table) |
| 50 | goto out; |
| 51 | |
| 52 | table[0].data = &net->ct.sysctl_tstamp; |
| 53 | |
Eric W. Biederman | 464dc80 | 2012-11-16 03:02:59 +0000 | [diff] [blame] | 54 | /* Don't export sysctls to unprivileged users */ |
| 55 | if (net->user_ns != &init_user_ns) |
| 56 | table[0].procname = NULL; |
| 57 | |
Eric W. Biederman | ec8f23c | 2012-04-19 13:44:49 +0000 | [diff] [blame] | 58 | net->ct.tstamp_sysctl_header = register_net_sysctl(net, "net/netfilter", |
| 59 | table); |
Pablo Neira Ayuso | a992ca2 | 2011-01-19 16:00:07 +0100 | [diff] [blame] | 60 | if (!net->ct.tstamp_sysctl_header) { |
| 61 | printk(KERN_ERR "nf_ct_tstamp: can't register to sysctl.\n"); |
| 62 | goto out_register; |
| 63 | } |
| 64 | return 0; |
| 65 | |
| 66 | out_register: |
| 67 | kfree(table); |
| 68 | out: |
| 69 | return -ENOMEM; |
| 70 | } |
| 71 | |
| 72 | static void nf_conntrack_tstamp_fini_sysctl(struct net *net) |
| 73 | { |
| 74 | struct ctl_table *table; |
| 75 | |
| 76 | table = net->ct.tstamp_sysctl_header->ctl_table_arg; |
| 77 | unregister_net_sysctl_table(net->ct.tstamp_sysctl_header); |
| 78 | kfree(table); |
| 79 | } |
| 80 | #else |
| 81 | static int nf_conntrack_tstamp_init_sysctl(struct net *net) |
| 82 | { |
| 83 | return 0; |
| 84 | } |
| 85 | |
| 86 | static void nf_conntrack_tstamp_fini_sysctl(struct net *net) |
| 87 | { |
| 88 | } |
| 89 | #endif |
| 90 | |
Gao feng | 73f4001 | 2013-01-21 22:10:27 +0000 | [diff] [blame] | 91 | int nf_conntrack_tstamp_pernet_init(struct net *net) |
| 92 | { |
| 93 | net->ct.sysctl_tstamp = nf_ct_tstamp; |
| 94 | return nf_conntrack_tstamp_init_sysctl(net); |
| 95 | } |
| 96 | |
| 97 | void nf_conntrack_tstamp_pernet_fini(struct net *net) |
| 98 | { |
| 99 | nf_conntrack_tstamp_fini_sysctl(net); |
| 100 | nf_ct_extend_unregister(&tstamp_extend); |
| 101 | } |
| 102 | |
| 103 | int nf_conntrack_tstamp_init(void) |
Pablo Neira Ayuso | a992ca2 | 2011-01-19 16:00:07 +0100 | [diff] [blame] | 104 | { |
| 105 | int ret; |
Gao feng | 73f4001 | 2013-01-21 22:10:27 +0000 | [diff] [blame] | 106 | ret = nf_ct_extend_register(&tstamp_extend); |
Pablo Neira Ayuso | a992ca2 | 2011-01-19 16:00:07 +0100 | [diff] [blame] | 107 | if (ret < 0) |
Gao feng | 73f4001 | 2013-01-21 22:10:27 +0000 | [diff] [blame] | 108 | pr_err("nf_ct_tstamp: Unable to register extension\n"); |
Pablo Neira Ayuso | a992ca2 | 2011-01-19 16:00:07 +0100 | [diff] [blame] | 109 | return ret; |
| 110 | } |
| 111 | |
Gao feng | 73f4001 | 2013-01-21 22:10:27 +0000 | [diff] [blame] | 112 | void nf_conntrack_tstamp_fini(void) |
Pablo Neira Ayuso | a992ca2 | 2011-01-19 16:00:07 +0100 | [diff] [blame] | 113 | { |
Gao feng | 73f4001 | 2013-01-21 22:10:27 +0000 | [diff] [blame] | 114 | nf_ct_extend_unregister(&tstamp_extend); |
Pablo Neira Ayuso | a992ca2 | 2011-01-19 16:00:07 +0100 | [diff] [blame] | 115 | } |