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 | }; | |
dd7669a9 PNA |
59 | int ret; |
60 | /* We make a copy of the missed event cache without taking | |
61 | * the lock, thus we may send missed events twice. However, | |
62 | * this does not harm and it happens very rarely. */ | |
63 | unsigned long missed = e->missed; | |
64 | ||
65 | ret = notify->fcn(events | missed, &item); | |
66 | if (unlikely(ret < 0 || missed)) { | |
67 | spin_lock_bh(&ct->lock); | |
68 | if (ret < 0) | |
69 | e->missed |= events; | |
70 | else | |
71 | e->missed &= ~missed; | |
72 | spin_unlock_bh(&ct->lock); | |
73 | } | |
19abb7b0 | 74 | } |
f6180121 | 75 | |
e34d5c1a PNA |
76 | out_unlock: |
77 | rcu_read_unlock(); | |
f6180121 | 78 | } |
13b18339 | 79 | EXPORT_SYMBOL_GPL(nf_ct_deliver_cached_events); |
f6180121 | 80 | |
e34d5c1a | 81 | int nf_conntrack_register_notifier(struct nf_ct_event_notifier *new) |
010c7d6f | 82 | { |
e34d5c1a PNA |
83 | int ret = 0; |
84 | struct nf_ct_event_notifier *notify; | |
85 | ||
86 | mutex_lock(&nf_ct_ecache_mutex); | |
87 | notify = rcu_dereference(nf_conntrack_event_cb); | |
88 | if (notify != NULL) { | |
89 | ret = -EBUSY; | |
90 | goto out_unlock; | |
91 | } | |
92 | rcu_assign_pointer(nf_conntrack_event_cb, new); | |
93 | mutex_unlock(&nf_ct_ecache_mutex); | |
94 | return ret; | |
95 | ||
96 | out_unlock: | |
97 | mutex_unlock(&nf_ct_ecache_mutex); | |
98 | return ret; | |
010c7d6f PM |
99 | } |
100 | EXPORT_SYMBOL_GPL(nf_conntrack_register_notifier); | |
101 | ||
e34d5c1a | 102 | void nf_conntrack_unregister_notifier(struct nf_ct_event_notifier *new) |
010c7d6f | 103 | { |
e34d5c1a PNA |
104 | struct nf_ct_event_notifier *notify; |
105 | ||
106 | mutex_lock(&nf_ct_ecache_mutex); | |
107 | notify = rcu_dereference(nf_conntrack_event_cb); | |
108 | BUG_ON(notify != new); | |
109 | rcu_assign_pointer(nf_conntrack_event_cb, NULL); | |
110 | mutex_unlock(&nf_ct_ecache_mutex); | |
010c7d6f PM |
111 | } |
112 | EXPORT_SYMBOL_GPL(nf_conntrack_unregister_notifier); | |
113 | ||
e34d5c1a | 114 | int nf_ct_expect_register_notifier(struct nf_exp_event_notifier *new) |
010c7d6f | 115 | { |
e34d5c1a PNA |
116 | int ret = 0; |
117 | struct nf_exp_event_notifier *notify; | |
118 | ||
119 | mutex_lock(&nf_ct_ecache_mutex); | |
120 | notify = rcu_dereference(nf_expect_event_cb); | |
121 | if (notify != NULL) { | |
122 | ret = -EBUSY; | |
123 | goto out_unlock; | |
124 | } | |
125 | rcu_assign_pointer(nf_expect_event_cb, new); | |
126 | mutex_unlock(&nf_ct_ecache_mutex); | |
127 | return ret; | |
128 | ||
129 | out_unlock: | |
130 | mutex_unlock(&nf_ct_ecache_mutex); | |
131 | return ret; | |
010c7d6f | 132 | } |
6823645d | 133 | EXPORT_SYMBOL_GPL(nf_ct_expect_register_notifier); |
010c7d6f | 134 | |
e34d5c1a | 135 | void nf_ct_expect_unregister_notifier(struct nf_exp_event_notifier *new) |
010c7d6f | 136 | { |
e34d5c1a PNA |
137 | struct nf_exp_event_notifier *notify; |
138 | ||
139 | mutex_lock(&nf_ct_ecache_mutex); | |
140 | notify = rcu_dereference(nf_expect_event_cb); | |
141 | BUG_ON(notify != new); | |
142 | rcu_assign_pointer(nf_expect_event_cb, NULL); | |
143 | mutex_unlock(&nf_ct_ecache_mutex); | |
010c7d6f | 144 | } |
6823645d | 145 | EXPORT_SYMBOL_GPL(nf_ct_expect_unregister_notifier); |
a0891aa6 PNA |
146 | |
147 | #define NF_CT_EVENTS_DEFAULT 1 | |
148 | static int nf_ct_events __read_mostly = NF_CT_EVENTS_DEFAULT; | |
dd7669a9 | 149 | static int nf_ct_events_retry_timeout __read_mostly = 15*HZ; |
a0891aa6 PNA |
150 | |
151 | #ifdef CONFIG_SYSCTL | |
152 | static struct ctl_table event_sysctl_table[] = { | |
153 | { | |
154 | .ctl_name = CTL_UNNUMBERED, | |
155 | .procname = "nf_conntrack_events", | |
156 | .data = &init_net.ct.sysctl_events, | |
157 | .maxlen = sizeof(unsigned int), | |
158 | .mode = 0644, | |
159 | .proc_handler = proc_dointvec, | |
160 | }, | |
dd7669a9 PNA |
161 | { |
162 | .ctl_name = CTL_UNNUMBERED, | |
163 | .procname = "nf_conntrack_events_retry_timeout", | |
164 | .data = &init_net.ct.sysctl_events_retry_timeout, | |
165 | .maxlen = sizeof(unsigned int), | |
166 | .mode = 0644, | |
167 | .proc_handler = proc_dointvec_jiffies, | |
168 | }, | |
a0891aa6 PNA |
169 | {} |
170 | }; | |
171 | #endif /* CONFIG_SYSCTL */ | |
172 | ||
173 | static struct nf_ct_ext_type event_extend __read_mostly = { | |
174 | .len = sizeof(struct nf_conntrack_ecache), | |
175 | .align = __alignof__(struct nf_conntrack_ecache), | |
176 | .id = NF_CT_EXT_ECACHE, | |
177 | }; | |
178 | ||
179 | #ifdef CONFIG_SYSCTL | |
180 | static int nf_conntrack_event_init_sysctl(struct net *net) | |
181 | { | |
182 | struct ctl_table *table; | |
183 | ||
184 | table = kmemdup(event_sysctl_table, sizeof(event_sysctl_table), | |
185 | GFP_KERNEL); | |
186 | if (!table) | |
187 | goto out; | |
188 | ||
189 | table[0].data = &net->ct.sysctl_events; | |
dd7669a9 | 190 | table[1].data = &net->ct.sysctl_events_retry_timeout; |
a0891aa6 PNA |
191 | |
192 | net->ct.event_sysctl_header = | |
193 | register_net_sysctl_table(net, | |
194 | nf_net_netfilter_sysctl_path, table); | |
195 | if (!net->ct.event_sysctl_header) { | |
196 | printk(KERN_ERR "nf_ct_event: can't register to sysctl.\n"); | |
197 | goto out_register; | |
198 | } | |
199 | return 0; | |
200 | ||
201 | out_register: | |
202 | kfree(table); | |
203 | out: | |
204 | return -ENOMEM; | |
205 | } | |
206 | ||
207 | static void nf_conntrack_event_fini_sysctl(struct net *net) | |
208 | { | |
209 | struct ctl_table *table; | |
210 | ||
211 | table = net->ct.event_sysctl_header->ctl_table_arg; | |
212 | unregister_net_sysctl_table(net->ct.event_sysctl_header); | |
213 | kfree(table); | |
214 | } | |
215 | #else | |
216 | static int nf_conntrack_event_init_sysctl(struct net *net) | |
217 | { | |
218 | return 0; | |
219 | } | |
220 | ||
221 | static void nf_conntrack_event_fini_sysctl(struct net *net) | |
222 | { | |
223 | } | |
224 | #endif /* CONFIG_SYSCTL */ | |
225 | ||
226 | int nf_conntrack_ecache_init(struct net *net) | |
227 | { | |
228 | int ret; | |
229 | ||
230 | net->ct.sysctl_events = nf_ct_events; | |
dd7669a9 | 231 | net->ct.sysctl_events_retry_timeout = nf_ct_events_retry_timeout; |
a0891aa6 PNA |
232 | |
233 | if (net_eq(net, &init_net)) { | |
234 | ret = nf_ct_extend_register(&event_extend); | |
235 | if (ret < 0) { | |
236 | printk(KERN_ERR "nf_ct_event: Unable to register " | |
237 | "event extension.\n"); | |
238 | goto out_extend_register; | |
239 | } | |
240 | } | |
241 | ||
242 | ret = nf_conntrack_event_init_sysctl(net); | |
243 | if (ret < 0) | |
244 | goto out_sysctl; | |
245 | ||
246 | return 0; | |
247 | ||
248 | out_sysctl: | |
249 | if (net_eq(net, &init_net)) | |
250 | nf_ct_extend_unregister(&event_extend); | |
251 | out_extend_register: | |
252 | return ret; | |
253 | } | |
254 | ||
255 | void nf_conntrack_ecache_fini(struct net *net) | |
256 | { | |
257 | nf_conntrack_event_fini_sysctl(net); | |
258 | if (net_eq(net, &init_net)) | |
259 | nf_ct_extend_unregister(&event_extend); | |
260 | } |