1 /* Event cache for netfilter. */
2 
3 /*
4  * (C) 2005 Harald Welte <laforge@gnumonks.org>
5  * (C) 2005 Patrick McHardy <kaber@trash.net>
6  * (C) 2005-2006 Netfilter Core Team <coreteam@netfilter.org>
7  * (C) 2005 USAGI/WIDE Project <http://www.linux-ipv6.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License version 2 as
11  * published by the Free Software Foundation.
12  */
13 
14 #include <linux/types.h>
15 #include <linux/netfilter.h>
16 #include <linux/skbuff.h>
17 #include <linux/vmalloc.h>
18 #include <linux/stddef.h>
19 #include <linux/err.h>
20 #include <linux/percpu.h>
21 #include <linux/kernel.h>
22 #include <linux/netdevice.h>
23 #include <linux/slab.h>
24 #include <linux/export.h>
25 
26 #include <net/netfilter/nf_conntrack.h>
27 #include <net/netfilter/nf_conntrack_core.h>
28 #include <net/netfilter/nf_conntrack_extend.h>
29 
30 static DEFINE_MUTEX(nf_ct_ecache_mutex);
31 
32 #define ECACHE_RETRY_WAIT (HZ/10)
33 
34 enum retry_state {
35 	STATE_CONGESTED,
36 	STATE_RESTART,
37 	STATE_DONE,
38 };
39 
40 static enum retry_state ecache_work_evict_list(struct ct_pcpu *pcpu)
41 {
42 	struct nf_conn *refs[16];
43 	struct nf_conntrack_tuple_hash *h;
44 	struct hlist_nulls_node *n;
45 	unsigned int evicted = 0;
46 	enum retry_state ret = STATE_DONE;
47 
48 	spin_lock(&pcpu->lock);
49 
50 	hlist_nulls_for_each_entry(h, n, &pcpu->dying, hnnode) {
51 		struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h);
52 
53 		if (nf_ct_is_dying(ct))
54 			continue;
55 
56 		if (nf_conntrack_event(IPCT_DESTROY, ct)) {
57 			ret = STATE_CONGESTED;
58 			break;
59 		}
60 
61 		/* we've got the event delivered, now it's dying */
62 		set_bit(IPS_DYING_BIT, &ct->status);
63 		refs[evicted] = ct;
64 
65 		if (++evicted >= ARRAY_SIZE(refs)) {
66 			ret = STATE_RESTART;
67 			break;
68 		}
69 	}
70 
71 	spin_unlock(&pcpu->lock);
72 
73 	/* can't _put while holding lock */
74 	while (evicted)
75 		nf_ct_put(refs[--evicted]);
76 
77 	return ret;
78 }
79 
80 static void ecache_work(struct work_struct *work)
81 {
82 	struct netns_ct *ctnet =
83 		container_of(work, struct netns_ct, ecache_dwork.work);
84 	int cpu, delay = -1;
85 	struct ct_pcpu *pcpu;
86 
87 	local_bh_disable();
88 
89 	for_each_possible_cpu(cpu) {
90 		enum retry_state ret;
91 
92 		pcpu = per_cpu_ptr(ctnet->pcpu_lists, cpu);
93 
94 		ret = ecache_work_evict_list(pcpu);
95 
96 		switch (ret) {
97 		case STATE_CONGESTED:
98 			delay = ECACHE_RETRY_WAIT;
99 			goto out;
100 		case STATE_RESTART:
101 			delay = 0;
102 			break;
103 		case STATE_DONE:
104 			break;
105 		}
106 	}
107 
108  out:
109 	local_bh_enable();
110 
111 	ctnet->ecache_dwork_pending = delay > 0;
112 	if (delay >= 0)
113 		schedule_delayed_work(&ctnet->ecache_dwork, delay);
114 }
115 
116 int nf_conntrack_eventmask_report(unsigned int eventmask, struct nf_conn *ct,
117 				  u32 portid, int report)
118 {
119 	int ret = 0;
120 	struct net *net = nf_ct_net(ct);
121 	struct nf_ct_event_notifier *notify;
122 	struct nf_conntrack_ecache *e;
123 
124 	rcu_read_lock();
125 	notify = rcu_dereference(net->ct.nf_conntrack_event_cb);
126 	if (!notify)
127 		goto out_unlock;
128 
129 	e = nf_ct_ecache_find(ct);
130 	if (!e)
131 		goto out_unlock;
132 
133 	if (nf_ct_is_confirmed(ct) && !nf_ct_is_dying(ct)) {
134 		struct nf_ct_event item = {
135 			.ct	= ct,
136 			.portid	= e->portid ? e->portid : portid,
137 			.report = report
138 		};
139 		/* This is a resent of a destroy event? If so, skip missed */
140 		unsigned long missed = e->portid ? 0 : e->missed;
141 
142 		if (!((eventmask | missed) & e->ctmask))
143 			goto out_unlock;
144 
145 		ret = notify->fcn(eventmask | missed, &item);
146 		if (unlikely(ret < 0 || missed)) {
147 			spin_lock_bh(&ct->lock);
148 			if (ret < 0) {
149 				/* This is a destroy event that has been
150 				 * triggered by a process, we store the PORTID
151 				 * to include it in the retransmission.
152 				 */
153 				if (eventmask & (1 << IPCT_DESTROY) &&
154 				    e->portid == 0 && portid != 0)
155 					e->portid = portid;
156 				else
157 					e->missed |= eventmask;
158 			} else {
159 				e->missed &= ~missed;
160 			}
161 			spin_unlock_bh(&ct->lock);
162 		}
163 	}
164 out_unlock:
165 	rcu_read_unlock();
166 	return ret;
167 }
168 EXPORT_SYMBOL_GPL(nf_conntrack_eventmask_report);
169 
170 /* deliver cached events and clear cache entry - must be called with locally
171  * disabled softirqs */
172 void nf_ct_deliver_cached_events(struct nf_conn *ct)
173 {
174 	struct net *net = nf_ct_net(ct);
175 	unsigned long events, missed;
176 	struct nf_ct_event_notifier *notify;
177 	struct nf_conntrack_ecache *e;
178 	struct nf_ct_event item;
179 	int ret;
180 
181 	rcu_read_lock();
182 	notify = rcu_dereference(net->ct.nf_conntrack_event_cb);
183 	if (notify == NULL)
184 		goto out_unlock;
185 
186 	e = nf_ct_ecache_find(ct);
187 	if (e == NULL)
188 		goto out_unlock;
189 
190 	events = xchg(&e->cache, 0);
191 
192 	if (!nf_ct_is_confirmed(ct) || nf_ct_is_dying(ct) || !events)
193 		goto out_unlock;
194 
195 	/* We make a copy of the missed event cache without taking
196 	 * the lock, thus we may send missed events twice. However,
197 	 * this does not harm and it happens very rarely. */
198 	missed = e->missed;
199 
200 	if (!((events | missed) & e->ctmask))
201 		goto out_unlock;
202 
203 	item.ct = ct;
204 	item.portid = 0;
205 	item.report = 0;
206 
207 	ret = notify->fcn(events | missed, &item);
208 
209 	if (likely(ret >= 0 && !missed))
210 		goto out_unlock;
211 
212 	spin_lock_bh(&ct->lock);
213 	if (ret < 0)
214 		e->missed |= events;
215 	else
216 		e->missed &= ~missed;
217 	spin_unlock_bh(&ct->lock);
218 
219 out_unlock:
220 	rcu_read_unlock();
221 }
222 EXPORT_SYMBOL_GPL(nf_ct_deliver_cached_events);
223 
224 int nf_conntrack_register_notifier(struct net *net,
225 				   struct nf_ct_event_notifier *new)
226 {
227 	int ret;
228 	struct nf_ct_event_notifier *notify;
229 
230 	mutex_lock(&nf_ct_ecache_mutex);
231 	notify = rcu_dereference_protected(net->ct.nf_conntrack_event_cb,
232 					   lockdep_is_held(&nf_ct_ecache_mutex));
233 	if (notify != NULL) {
234 		ret = -EBUSY;
235 		goto out_unlock;
236 	}
237 	rcu_assign_pointer(net->ct.nf_conntrack_event_cb, new);
238 	ret = 0;
239 
240 out_unlock:
241 	mutex_unlock(&nf_ct_ecache_mutex);
242 	return ret;
243 }
244 EXPORT_SYMBOL_GPL(nf_conntrack_register_notifier);
245 
246 void nf_conntrack_unregister_notifier(struct net *net,
247 				      struct nf_ct_event_notifier *new)
248 {
249 	struct nf_ct_event_notifier *notify;
250 
251 	mutex_lock(&nf_ct_ecache_mutex);
252 	notify = rcu_dereference_protected(net->ct.nf_conntrack_event_cb,
253 					   lockdep_is_held(&nf_ct_ecache_mutex));
254 	BUG_ON(notify != new);
255 	RCU_INIT_POINTER(net->ct.nf_conntrack_event_cb, NULL);
256 	mutex_unlock(&nf_ct_ecache_mutex);
257 }
258 EXPORT_SYMBOL_GPL(nf_conntrack_unregister_notifier);
259 
260 int nf_ct_expect_register_notifier(struct net *net,
261 				   struct nf_exp_event_notifier *new)
262 {
263 	int ret;
264 	struct nf_exp_event_notifier *notify;
265 
266 	mutex_lock(&nf_ct_ecache_mutex);
267 	notify = rcu_dereference_protected(net->ct.nf_expect_event_cb,
268 					   lockdep_is_held(&nf_ct_ecache_mutex));
269 	if (notify != NULL) {
270 		ret = -EBUSY;
271 		goto out_unlock;
272 	}
273 	rcu_assign_pointer(net->ct.nf_expect_event_cb, new);
274 	ret = 0;
275 
276 out_unlock:
277 	mutex_unlock(&nf_ct_ecache_mutex);
278 	return ret;
279 }
280 EXPORT_SYMBOL_GPL(nf_ct_expect_register_notifier);
281 
282 void nf_ct_expect_unregister_notifier(struct net *net,
283 				      struct nf_exp_event_notifier *new)
284 {
285 	struct nf_exp_event_notifier *notify;
286 
287 	mutex_lock(&nf_ct_ecache_mutex);
288 	notify = rcu_dereference_protected(net->ct.nf_expect_event_cb,
289 					   lockdep_is_held(&nf_ct_ecache_mutex));
290 	BUG_ON(notify != new);
291 	RCU_INIT_POINTER(net->ct.nf_expect_event_cb, NULL);
292 	mutex_unlock(&nf_ct_ecache_mutex);
293 }
294 EXPORT_SYMBOL_GPL(nf_ct_expect_unregister_notifier);
295 
296 #define NF_CT_EVENTS_DEFAULT 1
297 static int nf_ct_events __read_mostly = NF_CT_EVENTS_DEFAULT;
298 
299 #ifdef CONFIG_SYSCTL
300 static struct ctl_table event_sysctl_table[] = {
301 	{
302 		.procname	= "nf_conntrack_events",
303 		.data		= &init_net.ct.sysctl_events,
304 		.maxlen		= sizeof(unsigned int),
305 		.mode		= 0644,
306 		.proc_handler	= proc_dointvec,
307 	},
308 	{}
309 };
310 #endif /* CONFIG_SYSCTL */
311 
312 static struct nf_ct_ext_type event_extend __read_mostly = {
313 	.len	= sizeof(struct nf_conntrack_ecache),
314 	.align	= __alignof__(struct nf_conntrack_ecache),
315 	.id	= NF_CT_EXT_ECACHE,
316 };
317 
318 #ifdef CONFIG_SYSCTL
319 static int nf_conntrack_event_init_sysctl(struct net *net)
320 {
321 	struct ctl_table *table;
322 
323 	table = kmemdup(event_sysctl_table, sizeof(event_sysctl_table),
324 			GFP_KERNEL);
325 	if (!table)
326 		goto out;
327 
328 	table[0].data = &net->ct.sysctl_events;
329 
330 	/* Don't export sysctls to unprivileged users */
331 	if (net->user_ns != &init_user_ns)
332 		table[0].procname = NULL;
333 
334 	net->ct.event_sysctl_header =
335 		register_net_sysctl(net, "net/netfilter", table);
336 	if (!net->ct.event_sysctl_header) {
337 		printk(KERN_ERR "nf_ct_event: can't register to sysctl.\n");
338 		goto out_register;
339 	}
340 	return 0;
341 
342 out_register:
343 	kfree(table);
344 out:
345 	return -ENOMEM;
346 }
347 
348 static void nf_conntrack_event_fini_sysctl(struct net *net)
349 {
350 	struct ctl_table *table;
351 
352 	table = net->ct.event_sysctl_header->ctl_table_arg;
353 	unregister_net_sysctl_table(net->ct.event_sysctl_header);
354 	kfree(table);
355 }
356 #else
357 static int nf_conntrack_event_init_sysctl(struct net *net)
358 {
359 	return 0;
360 }
361 
362 static void nf_conntrack_event_fini_sysctl(struct net *net)
363 {
364 }
365 #endif /* CONFIG_SYSCTL */
366 
367 int nf_conntrack_ecache_pernet_init(struct net *net)
368 {
369 	net->ct.sysctl_events = nf_ct_events;
370 	INIT_DELAYED_WORK(&net->ct.ecache_dwork, ecache_work);
371 	return nf_conntrack_event_init_sysctl(net);
372 }
373 
374 void nf_conntrack_ecache_pernet_fini(struct net *net)
375 {
376 	cancel_delayed_work_sync(&net->ct.ecache_dwork);
377 	nf_conntrack_event_fini_sysctl(net);
378 }
379 
380 int nf_conntrack_ecache_init(void)
381 {
382 	int ret = nf_ct_extend_register(&event_extend);
383 	if (ret < 0)
384 		pr_err("nf_ct_event: Unable to register event extension.\n");
385 	return ret;
386 }
387 
388 void nf_conntrack_ecache_fini(void)
389 {
390 	nf_ct_extend_unregister(&event_extend);
391 }
392