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