]>
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 <[email protected]> | |
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> | |
5a0e3ad6 | 21 | #include <linux/slab.h> |
bc3b2d7f | 22 | #include <linux/export.h> |
f6180121 MJ |
23 | |
24 | #include <net/netfilter/nf_conntrack.h> | |
f6180121 | 25 | #include <net/netfilter/nf_conntrack_core.h> |
a0891aa6 | 26 | #include <net/netfilter/nf_conntrack_extend.h> |
f6180121 | 27 | |
e34d5c1a | 28 | static DEFINE_MUTEX(nf_ct_ecache_mutex); |
13b18339 | 29 | |
f6180121 MJ |
30 | /* deliver cached events and clear cache entry - must be called with locally |
31 | * disabled softirqs */ | |
a0891aa6 | 32 | void nf_ct_deliver_cached_events(struct nf_conn *ct) |
f6180121 | 33 | { |
70e9942f | 34 | struct net *net = nf_ct_net(ct); |
58020f77 | 35 | unsigned long events, missed; |
e34d5c1a | 36 | struct nf_ct_event_notifier *notify; |
a0891aa6 | 37 | struct nf_conntrack_ecache *e; |
58020f77 TZ |
38 | struct nf_ct_event item; |
39 | int ret; | |
e34d5c1a PNA |
40 | |
41 | rcu_read_lock(); | |
70e9942f | 42 | notify = rcu_dereference(net->ct.nf_conntrack_event_cb); |
e34d5c1a PNA |
43 | if (notify == NULL) |
44 | goto out_unlock; | |
45 | ||
a0891aa6 PNA |
46 | e = nf_ct_ecache_find(ct); |
47 | if (e == NULL) | |
48 | goto out_unlock; | |
49 | ||
50 | events = xchg(&e->cache, 0); | |
51 | ||
58020f77 TZ |
52 | if (!nf_ct_is_confirmed(ct) || nf_ct_is_dying(ct) || !events) |
53 | goto out_unlock; | |
54 | ||
55 | /* We make a copy of the missed event cache without taking | |
56 | * the lock, thus we may send missed events twice. However, | |
57 | * this does not harm and it happens very rarely. */ | |
58 | missed = e->missed; | |
59 | ||
60 | if (!((events | missed) & e->ctmask)) | |
61 | goto out_unlock; | |
62 | ||
63 | item.ct = ct; | |
15e47304 | 64 | item.portid = 0; |
58020f77 TZ |
65 | item.report = 0; |
66 | ||
67 | ret = notify->fcn(events | missed, &item); | |
68 | ||
69 | if (likely(ret >= 0 && !missed)) | |
70 | goto out_unlock; | |
71 | ||
72 | spin_lock_bh(&ct->lock); | |
73 | if (ret < 0) | |
74 | e->missed |= events; | |
75 | else | |
76 | e->missed &= ~missed; | |
77 | spin_unlock_bh(&ct->lock); | |
f6180121 | 78 | |
e34d5c1a PNA |
79 | out_unlock: |
80 | rcu_read_unlock(); | |
f6180121 | 81 | } |
13b18339 | 82 | EXPORT_SYMBOL_GPL(nf_ct_deliver_cached_events); |
f6180121 | 83 | |
70e9942f PNA |
84 | int nf_conntrack_register_notifier(struct net *net, |
85 | struct nf_ct_event_notifier *new) | |
010c7d6f | 86 | { |
031d7709 | 87 | int ret; |
b56f2d55 | 88 | struct nf_ct_event_notifier *notify; |
e34d5c1a PNA |
89 | |
90 | mutex_lock(&nf_ct_ecache_mutex); | |
70e9942f | 91 | notify = rcu_dereference_protected(net->ct.nf_conntrack_event_cb, |
b56f2d55 PM |
92 | lockdep_is_held(&nf_ct_ecache_mutex)); |
93 | if (notify != NULL) { | |
e34d5c1a PNA |
94 | ret = -EBUSY; |
95 | goto out_unlock; | |
96 | } | |
cf778b00 | 97 | rcu_assign_pointer(net->ct.nf_conntrack_event_cb, new); |
031d7709 | 98 | ret = 0; |
e34d5c1a PNA |
99 | |
100 | out_unlock: | |
101 | mutex_unlock(&nf_ct_ecache_mutex); | |
102 | return ret; | |
010c7d6f PM |
103 | } |
104 | EXPORT_SYMBOL_GPL(nf_conntrack_register_notifier); | |
105 | ||
70e9942f PNA |
106 | void nf_conntrack_unregister_notifier(struct net *net, |
107 | struct nf_ct_event_notifier *new) | |
010c7d6f | 108 | { |
b56f2d55 PM |
109 | struct nf_ct_event_notifier *notify; |
110 | ||
e34d5c1a | 111 | mutex_lock(&nf_ct_ecache_mutex); |
70e9942f | 112 | notify = rcu_dereference_protected(net->ct.nf_conntrack_event_cb, |
b56f2d55 PM |
113 | lockdep_is_held(&nf_ct_ecache_mutex)); |
114 | BUG_ON(notify != new); | |
70e9942f | 115 | RCU_INIT_POINTER(net->ct.nf_conntrack_event_cb, NULL); |
e34d5c1a | 116 | mutex_unlock(&nf_ct_ecache_mutex); |
010c7d6f PM |
117 | } |
118 | EXPORT_SYMBOL_GPL(nf_conntrack_unregister_notifier); | |
119 | ||
70e9942f PNA |
120 | int nf_ct_expect_register_notifier(struct net *net, |
121 | struct nf_exp_event_notifier *new) | |
010c7d6f | 122 | { |
031d7709 | 123 | int ret; |
b56f2d55 | 124 | struct nf_exp_event_notifier *notify; |
e34d5c1a PNA |
125 | |
126 | mutex_lock(&nf_ct_ecache_mutex); | |
70e9942f | 127 | notify = rcu_dereference_protected(net->ct.nf_expect_event_cb, |
b56f2d55 PM |
128 | lockdep_is_held(&nf_ct_ecache_mutex)); |
129 | if (notify != NULL) { | |
e34d5c1a PNA |
130 | ret = -EBUSY; |
131 | goto out_unlock; | |
132 | } | |
cf778b00 | 133 | rcu_assign_pointer(net->ct.nf_expect_event_cb, new); |
031d7709 | 134 | ret = 0; |
e34d5c1a PNA |
135 | |
136 | out_unlock: | |
137 | mutex_unlock(&nf_ct_ecache_mutex); | |
138 | return ret; | |
010c7d6f | 139 | } |
6823645d | 140 | EXPORT_SYMBOL_GPL(nf_ct_expect_register_notifier); |
010c7d6f | 141 | |
70e9942f PNA |
142 | void nf_ct_expect_unregister_notifier(struct net *net, |
143 | struct nf_exp_event_notifier *new) | |
010c7d6f | 144 | { |
b56f2d55 PM |
145 | struct nf_exp_event_notifier *notify; |
146 | ||
e34d5c1a | 147 | mutex_lock(&nf_ct_ecache_mutex); |
70e9942f | 148 | notify = rcu_dereference_protected(net->ct.nf_expect_event_cb, |
b56f2d55 PM |
149 | lockdep_is_held(&nf_ct_ecache_mutex)); |
150 | BUG_ON(notify != new); | |
70e9942f | 151 | RCU_INIT_POINTER(net->ct.nf_expect_event_cb, NULL); |
e34d5c1a | 152 | mutex_unlock(&nf_ct_ecache_mutex); |
010c7d6f | 153 | } |
6823645d | 154 | EXPORT_SYMBOL_GPL(nf_ct_expect_unregister_notifier); |
a0891aa6 PNA |
155 | |
156 | #define NF_CT_EVENTS_DEFAULT 1 | |
157 | static int nf_ct_events __read_mostly = NF_CT_EVENTS_DEFAULT; | |
dd7669a9 | 158 | static int nf_ct_events_retry_timeout __read_mostly = 15*HZ; |
a0891aa6 PNA |
159 | |
160 | #ifdef CONFIG_SYSCTL | |
161 | static struct ctl_table event_sysctl_table[] = { | |
162 | { | |
a0891aa6 PNA |
163 | .procname = "nf_conntrack_events", |
164 | .data = &init_net.ct.sysctl_events, | |
165 | .maxlen = sizeof(unsigned int), | |
166 | .mode = 0644, | |
167 | .proc_handler = proc_dointvec, | |
168 | }, | |
dd7669a9 | 169 | { |
dd7669a9 PNA |
170 | .procname = "nf_conntrack_events_retry_timeout", |
171 | .data = &init_net.ct.sysctl_events_retry_timeout, | |
172 | .maxlen = sizeof(unsigned int), | |
173 | .mode = 0644, | |
174 | .proc_handler = proc_dointvec_jiffies, | |
175 | }, | |
a0891aa6 PNA |
176 | {} |
177 | }; | |
178 | #endif /* CONFIG_SYSCTL */ | |
179 | ||
180 | static struct nf_ct_ext_type event_extend __read_mostly = { | |
181 | .len = sizeof(struct nf_conntrack_ecache), | |
182 | .align = __alignof__(struct nf_conntrack_ecache), | |
183 | .id = NF_CT_EXT_ECACHE, | |
184 | }; | |
185 | ||
186 | #ifdef CONFIG_SYSCTL | |
187 | static int nf_conntrack_event_init_sysctl(struct net *net) | |
188 | { | |
189 | struct ctl_table *table; | |
190 | ||
191 | table = kmemdup(event_sysctl_table, sizeof(event_sysctl_table), | |
192 | GFP_KERNEL); | |
193 | if (!table) | |
194 | goto out; | |
195 | ||
196 | table[0].data = &net->ct.sysctl_events; | |
dd7669a9 | 197 | table[1].data = &net->ct.sysctl_events_retry_timeout; |
a0891aa6 PNA |
198 | |
199 | net->ct.event_sysctl_header = | |
ec8f23ce | 200 | register_net_sysctl(net, "net/netfilter", table); |
a0891aa6 PNA |
201 | if (!net->ct.event_sysctl_header) { |
202 | printk(KERN_ERR "nf_ct_event: can't register to sysctl.\n"); | |
203 | goto out_register; | |
204 | } | |
205 | return 0; | |
206 | ||
207 | out_register: | |
208 | kfree(table); | |
209 | out: | |
210 | return -ENOMEM; | |
211 | } | |
212 | ||
213 | static void nf_conntrack_event_fini_sysctl(struct net *net) | |
214 | { | |
215 | struct ctl_table *table; | |
216 | ||
217 | table = net->ct.event_sysctl_header->ctl_table_arg; | |
218 | unregister_net_sysctl_table(net->ct.event_sysctl_header); | |
219 | kfree(table); | |
220 | } | |
221 | #else | |
222 | static int nf_conntrack_event_init_sysctl(struct net *net) | |
223 | { | |
224 | return 0; | |
225 | } | |
226 | ||
227 | static void nf_conntrack_event_fini_sysctl(struct net *net) | |
228 | { | |
229 | } | |
230 | #endif /* CONFIG_SYSCTL */ | |
231 | ||
232 | int nf_conntrack_ecache_init(struct net *net) | |
233 | { | |
234 | int ret; | |
235 | ||
236 | net->ct.sysctl_events = nf_ct_events; | |
dd7669a9 | 237 | net->ct.sysctl_events_retry_timeout = nf_ct_events_retry_timeout; |
a0891aa6 PNA |
238 | |
239 | if (net_eq(net, &init_net)) { | |
240 | ret = nf_ct_extend_register(&event_extend); | |
241 | if (ret < 0) { | |
242 | printk(KERN_ERR "nf_ct_event: Unable to register " | |
243 | "event extension.\n"); | |
244 | goto out_extend_register; | |
245 | } | |
246 | } | |
247 | ||
248 | ret = nf_conntrack_event_init_sysctl(net); | |
249 | if (ret < 0) | |
250 | goto out_sysctl; | |
251 | ||
252 | return 0; | |
253 | ||
254 | out_sysctl: | |
255 | if (net_eq(net, &init_net)) | |
256 | nf_ct_extend_unregister(&event_extend); | |
257 | out_extend_register: | |
258 | return ret; | |
259 | } | |
260 | ||
261 | void nf_conntrack_ecache_fini(struct net *net) | |
262 | { | |
263 | nf_conntrack_event_fini_sysctl(net); | |
264 | if (net_eq(net, &init_net)) | |
265 | nf_ct_extend_unregister(&event_extend); | |
266 | } |