Commit | Line | Data |
---|---|---|
f6180121 MJ |
1 | /* Event cache for netfilter. */ |
2 | ||
3 | /* (C) 1999-2001 Paul `Rusty' Russell | |
4 | * (C) 2002-2006 Netfilter Core Team <coreteam@netfilter.org> | |
5 | * (C) 2003,2004 USAGI/WIDE Project <http://www.linux-ipv6.org> | |
6 | * | |
7 | * This program is free software; you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License version 2 as | |
9 | * published by the Free Software Foundation. | |
10 | */ | |
11 | ||
12 | #include <linux/types.h> | |
13 | #include <linux/netfilter.h> | |
14 | #include <linux/skbuff.h> | |
15 | #include <linux/vmalloc.h> | |
16 | #include <linux/stddef.h> | |
17 | #include <linux/err.h> | |
18 | #include <linux/percpu.h> | |
f6180121 MJ |
19 | #include <linux/kernel.h> |
20 | #include <linux/netdevice.h> | |
21 | ||
22 | #include <net/netfilter/nf_conntrack.h> | |
f6180121 | 23 | #include <net/netfilter/nf_conntrack_core.h> |
a0891aa6 | 24 | #include <net/netfilter/nf_conntrack_extend.h> |
f6180121 | 25 | |
e34d5c1a | 26 | static DEFINE_MUTEX(nf_ct_ecache_mutex); |
13b18339 | 27 | |
e34d5c1a PNA |
28 | struct nf_ct_event_notifier *nf_conntrack_event_cb __read_mostly; |
29 | EXPORT_SYMBOL_GPL(nf_conntrack_event_cb); | |
30 | ||
31 | struct nf_exp_event_notifier *nf_expect_event_cb __read_mostly; | |
32 | EXPORT_SYMBOL_GPL(nf_expect_event_cb); | |
f6180121 | 33 | |
f6180121 MJ |
34 | /* deliver cached events and clear cache entry - must be called with locally |
35 | * disabled softirqs */ | |
a0891aa6 | 36 | void nf_ct_deliver_cached_events(struct nf_conn *ct) |
f6180121 | 37 | { |
a0891aa6 | 38 | unsigned long events; |
e34d5c1a | 39 | struct nf_ct_event_notifier *notify; |
a0891aa6 | 40 | struct nf_conntrack_ecache *e; |
e34d5c1a PNA |
41 | |
42 | rcu_read_lock(); | |
43 | notify = rcu_dereference(nf_conntrack_event_cb); | |
44 | if (notify == NULL) | |
45 | goto out_unlock; | |
46 | ||
a0891aa6 PNA |
47 | e = nf_ct_ecache_find(ct); |
48 | if (e == NULL) | |
49 | goto out_unlock; | |
50 | ||
51 | events = xchg(&e->cache, 0); | |
52 | ||
53 | if (nf_ct_is_confirmed(ct) && !nf_ct_is_dying(ct) && events) { | |
19abb7b0 | 54 | struct nf_ct_event item = { |
a0891aa6 | 55 | .ct = ct, |
19abb7b0 PNA |
56 | .pid = 0, |
57 | .report = 0 | |
58 | }; | |
59 | ||
a0891aa6 | 60 | notify->fcn(events, &item); |
19abb7b0 | 61 | } |
f6180121 | 62 | |
e34d5c1a PNA |
63 | out_unlock: |
64 | rcu_read_unlock(); | |
f6180121 | 65 | } |
13b18339 | 66 | EXPORT_SYMBOL_GPL(nf_ct_deliver_cached_events); |
f6180121 | 67 | |
e34d5c1a | 68 | int nf_conntrack_register_notifier(struct nf_ct_event_notifier *new) |
010c7d6f | 69 | { |
e34d5c1a PNA |
70 | int ret = 0; |
71 | struct nf_ct_event_notifier *notify; | |
72 | ||
73 | mutex_lock(&nf_ct_ecache_mutex); | |
74 | notify = rcu_dereference(nf_conntrack_event_cb); | |
75 | if (notify != NULL) { | |
76 | ret = -EBUSY; | |
77 | goto out_unlock; | |
78 | } | |
79 | rcu_assign_pointer(nf_conntrack_event_cb, new); | |
80 | mutex_unlock(&nf_ct_ecache_mutex); | |
81 | return ret; | |
82 | ||
83 | out_unlock: | |
84 | mutex_unlock(&nf_ct_ecache_mutex); | |
85 | return ret; | |
010c7d6f PM |
86 | } |
87 | EXPORT_SYMBOL_GPL(nf_conntrack_register_notifier); | |
88 | ||
e34d5c1a | 89 | void nf_conntrack_unregister_notifier(struct nf_ct_event_notifier *new) |
010c7d6f | 90 | { |
e34d5c1a PNA |
91 | struct nf_ct_event_notifier *notify; |
92 | ||
93 | mutex_lock(&nf_ct_ecache_mutex); | |
94 | notify = rcu_dereference(nf_conntrack_event_cb); | |
95 | BUG_ON(notify != new); | |
96 | rcu_assign_pointer(nf_conntrack_event_cb, NULL); | |
97 | mutex_unlock(&nf_ct_ecache_mutex); | |
010c7d6f PM |
98 | } |
99 | EXPORT_SYMBOL_GPL(nf_conntrack_unregister_notifier); | |
100 | ||
e34d5c1a | 101 | int nf_ct_expect_register_notifier(struct nf_exp_event_notifier *new) |
010c7d6f | 102 | { |
e34d5c1a PNA |
103 | int ret = 0; |
104 | struct nf_exp_event_notifier *notify; | |
105 | ||
106 | mutex_lock(&nf_ct_ecache_mutex); | |
107 | notify = rcu_dereference(nf_expect_event_cb); | |
108 | if (notify != NULL) { | |
109 | ret = -EBUSY; | |
110 | goto out_unlock; | |
111 | } | |
112 | rcu_assign_pointer(nf_expect_event_cb, new); | |
113 | mutex_unlock(&nf_ct_ecache_mutex); | |
114 | return ret; | |
115 | ||
116 | out_unlock: | |
117 | mutex_unlock(&nf_ct_ecache_mutex); | |
118 | return ret; | |
010c7d6f | 119 | } |
6823645d | 120 | EXPORT_SYMBOL_GPL(nf_ct_expect_register_notifier); |
010c7d6f | 121 | |
e34d5c1a | 122 | void nf_ct_expect_unregister_notifier(struct nf_exp_event_notifier *new) |
010c7d6f | 123 | { |
e34d5c1a PNA |
124 | struct nf_exp_event_notifier *notify; |
125 | ||
126 | mutex_lock(&nf_ct_ecache_mutex); | |
127 | notify = rcu_dereference(nf_expect_event_cb); | |
128 | BUG_ON(notify != new); | |
129 | rcu_assign_pointer(nf_expect_event_cb, NULL); | |
130 | mutex_unlock(&nf_ct_ecache_mutex); | |
010c7d6f | 131 | } |
6823645d | 132 | EXPORT_SYMBOL_GPL(nf_ct_expect_unregister_notifier); |
a0891aa6 PNA |
133 | |
134 | #define NF_CT_EVENTS_DEFAULT 1 | |
135 | static int nf_ct_events __read_mostly = NF_CT_EVENTS_DEFAULT; | |
136 | ||
137 | #ifdef CONFIG_SYSCTL | |
138 | static struct ctl_table event_sysctl_table[] = { | |
139 | { | |
140 | .ctl_name = CTL_UNNUMBERED, | |
141 | .procname = "nf_conntrack_events", | |
142 | .data = &init_net.ct.sysctl_events, | |
143 | .maxlen = sizeof(unsigned int), | |
144 | .mode = 0644, | |
145 | .proc_handler = proc_dointvec, | |
146 | }, | |
147 | {} | |
148 | }; | |
149 | #endif /* CONFIG_SYSCTL */ | |
150 | ||
151 | static struct nf_ct_ext_type event_extend __read_mostly = { | |
152 | .len = sizeof(struct nf_conntrack_ecache), | |
153 | .align = __alignof__(struct nf_conntrack_ecache), | |
154 | .id = NF_CT_EXT_ECACHE, | |
155 | }; | |
156 | ||
157 | #ifdef CONFIG_SYSCTL | |
158 | static int nf_conntrack_event_init_sysctl(struct net *net) | |
159 | { | |
160 | struct ctl_table *table; | |
161 | ||
162 | table = kmemdup(event_sysctl_table, sizeof(event_sysctl_table), | |
163 | GFP_KERNEL); | |
164 | if (!table) | |
165 | goto out; | |
166 | ||
167 | table[0].data = &net->ct.sysctl_events; | |
168 | ||
169 | net->ct.event_sysctl_header = | |
170 | register_net_sysctl_table(net, | |
171 | nf_net_netfilter_sysctl_path, table); | |
172 | if (!net->ct.event_sysctl_header) { | |
173 | printk(KERN_ERR "nf_ct_event: can't register to sysctl.\n"); | |
174 | goto out_register; | |
175 | } | |
176 | return 0; | |
177 | ||
178 | out_register: | |
179 | kfree(table); | |
180 | out: | |
181 | return -ENOMEM; | |
182 | } | |
183 | ||
184 | static void nf_conntrack_event_fini_sysctl(struct net *net) | |
185 | { | |
186 | struct ctl_table *table; | |
187 | ||
188 | table = net->ct.event_sysctl_header->ctl_table_arg; | |
189 | unregister_net_sysctl_table(net->ct.event_sysctl_header); | |
190 | kfree(table); | |
191 | } | |
192 | #else | |
193 | static int nf_conntrack_event_init_sysctl(struct net *net) | |
194 | { | |
195 | return 0; | |
196 | } | |
197 | ||
198 | static void nf_conntrack_event_fini_sysctl(struct net *net) | |
199 | { | |
200 | } | |
201 | #endif /* CONFIG_SYSCTL */ | |
202 | ||
203 | int nf_conntrack_ecache_init(struct net *net) | |
204 | { | |
205 | int ret; | |
206 | ||
207 | net->ct.sysctl_events = nf_ct_events; | |
208 | ||
209 | if (net_eq(net, &init_net)) { | |
210 | ret = nf_ct_extend_register(&event_extend); | |
211 | if (ret < 0) { | |
212 | printk(KERN_ERR "nf_ct_event: Unable to register " | |
213 | "event extension.\n"); | |
214 | goto out_extend_register; | |
215 | } | |
216 | } | |
217 | ||
218 | ret = nf_conntrack_event_init_sysctl(net); | |
219 | if (ret < 0) | |
220 | goto out_sysctl; | |
221 | ||
222 | return 0; | |
223 | ||
224 | out_sysctl: | |
225 | if (net_eq(net, &init_net)) | |
226 | nf_ct_extend_unregister(&event_extend); | |
227 | out_extend_register: | |
228 | return ret; | |
229 | } | |
230 | ||
231 | void nf_conntrack_ecache_fini(struct net *net) | |
232 | { | |
233 | nf_conntrack_event_fini_sysctl(net); | |
234 | if (net_eq(net, &init_net)) | |
235 | nf_ct_extend_unregister(&event_extend); | |
236 | } |