blob: 6b368be937c615610a7fa7b90d26b58a2fa041d5 [file] [log] [blame]
Martin Josefssonf6180122006-11-29 02:35:01 +01001/* 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>
Martin Josefssonf6180122006-11-29 02:35:01 +010019#include <linux/kernel.h>
20#include <linux/netdevice.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040022#include <linux/export.h>
Martin Josefssonf6180122006-11-29 02:35:01 +010023
24#include <net/netfilter/nf_conntrack.h>
Martin Josefssonf6180122006-11-29 02:35:01 +010025#include <net/netfilter/nf_conntrack_core.h>
Pablo Neira Ayusoa0891aa2009-06-13 12:26:29 +020026#include <net/netfilter/nf_conntrack_extend.h>
Martin Josefssonf6180122006-11-29 02:35:01 +010027
Pablo Neira Ayusoe34d5c12009-06-03 10:32:06 +020028static DEFINE_MUTEX(nf_ct_ecache_mutex);
Patrick McHardy13b18332006-12-02 22:11:25 -080029
Arnd Bergmann0906a372010-03-09 20:59:15 +010030struct nf_ct_event_notifier __rcu *nf_conntrack_event_cb __read_mostly;
Pablo Neira Ayusoe34d5c12009-06-03 10:32:06 +020031EXPORT_SYMBOL_GPL(nf_conntrack_event_cb);
32
Arnd Bergmann0906a372010-03-09 20:59:15 +010033struct nf_exp_event_notifier __rcu *nf_expect_event_cb __read_mostly;
Pablo Neira Ayusoe34d5c12009-06-03 10:32:06 +020034EXPORT_SYMBOL_GPL(nf_expect_event_cb);
Martin Josefssonf6180122006-11-29 02:35:01 +010035
Martin Josefssonf6180122006-11-29 02:35:01 +010036/* deliver cached events and clear cache entry - must be called with locally
37 * disabled softirqs */
Pablo Neira Ayusoa0891aa2009-06-13 12:26:29 +020038void nf_ct_deliver_cached_events(struct nf_conn *ct)
Martin Josefssonf6180122006-11-29 02:35:01 +010039{
Pablo Neira Ayusoa0891aa2009-06-13 12:26:29 +020040 unsigned long events;
Pablo Neira Ayusoe34d5c12009-06-03 10:32:06 +020041 struct nf_ct_event_notifier *notify;
Pablo Neira Ayusoa0891aa2009-06-13 12:26:29 +020042 struct nf_conntrack_ecache *e;
Pablo Neira Ayusoe34d5c12009-06-03 10:32:06 +020043
44 rcu_read_lock();
45 notify = rcu_dereference(nf_conntrack_event_cb);
46 if (notify == NULL)
47 goto out_unlock;
48
Pablo Neira Ayusoa0891aa2009-06-13 12:26:29 +020049 e = nf_ct_ecache_find(ct);
50 if (e == NULL)
51 goto out_unlock;
52
53 events = xchg(&e->cache, 0);
54
55 if (nf_ct_is_confirmed(ct) && !nf_ct_is_dying(ct) && events) {
Pablo Neira Ayuso19abb7b2008-11-18 11:56:20 +010056 struct nf_ct_event item = {
Pablo Neira Ayusoa0891aa2009-06-13 12:26:29 +020057 .ct = ct,
Pablo Neira Ayuso19abb7b2008-11-18 11:56:20 +010058 .pid = 0,
59 .report = 0
60 };
Pablo Neira Ayusodd7669a2009-06-13 12:30:52 +020061 int ret;
62 /* We make a copy of the missed event cache without taking
63 * the lock, thus we may send missed events twice. However,
64 * this does not harm and it happens very rarely. */
65 unsigned long missed = e->missed;
Pablo Neira Ayuso19abb7b2008-11-18 11:56:20 +010066
Pablo Neira Ayuso3db7e932011-02-01 16:06:30 +010067 if (!((events | missed) & e->ctmask))
68 goto out_unlock;
69
Pablo Neira Ayusodd7669a2009-06-13 12:30:52 +020070 ret = notify->fcn(events | missed, &item);
71 if (unlikely(ret < 0 || missed)) {
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);
78 }
Pablo Neira Ayuso19abb7b2008-11-18 11:56:20 +010079 }
Martin Josefssonf6180122006-11-29 02:35:01 +010080
Pablo Neira Ayusoe34d5c12009-06-03 10:32:06 +020081out_unlock:
82 rcu_read_unlock();
Martin Josefssonf6180122006-11-29 02:35:01 +010083}
Patrick McHardy13b18332006-12-02 22:11:25 -080084EXPORT_SYMBOL_GPL(nf_ct_deliver_cached_events);
Martin Josefssonf6180122006-11-29 02:35:01 +010085
Pablo Neira Ayusoe34d5c12009-06-03 10:32:06 +020086int nf_conntrack_register_notifier(struct nf_ct_event_notifier *new)
Patrick McHardy010c7d62007-03-14 16:40:10 -070087{
Pablo Neira Ayusoe34d5c12009-06-03 10:32:06 +020088 int ret = 0;
Patrick McHardyb56f2d52010-05-10 18:47:57 +020089 struct nf_ct_event_notifier *notify;
Pablo Neira Ayusoe34d5c12009-06-03 10:32:06 +020090
91 mutex_lock(&nf_ct_ecache_mutex);
Patrick McHardyb56f2d52010-05-10 18:47:57 +020092 notify = rcu_dereference_protected(nf_conntrack_event_cb,
93 lockdep_is_held(&nf_ct_ecache_mutex));
94 if (notify != NULL) {
Pablo Neira Ayusoe34d5c12009-06-03 10:32:06 +020095 ret = -EBUSY;
96 goto out_unlock;
97 }
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +000098 RCU_INIT_POINTER(nf_conntrack_event_cb, new);
Pablo Neira Ayusoe34d5c12009-06-03 10:32:06 +020099 mutex_unlock(&nf_ct_ecache_mutex);
100 return ret;
101
102out_unlock:
103 mutex_unlock(&nf_ct_ecache_mutex);
104 return ret;
Patrick McHardy010c7d62007-03-14 16:40:10 -0700105}
106EXPORT_SYMBOL_GPL(nf_conntrack_register_notifier);
107
Pablo Neira Ayusoe34d5c12009-06-03 10:32:06 +0200108void nf_conntrack_unregister_notifier(struct nf_ct_event_notifier *new)
Patrick McHardy010c7d62007-03-14 16:40:10 -0700109{
Patrick McHardyb56f2d52010-05-10 18:47:57 +0200110 struct nf_ct_event_notifier *notify;
111
Pablo Neira Ayusoe34d5c12009-06-03 10:32:06 +0200112 mutex_lock(&nf_ct_ecache_mutex);
Patrick McHardyb56f2d52010-05-10 18:47:57 +0200113 notify = rcu_dereference_protected(nf_conntrack_event_cb,
114 lockdep_is_held(&nf_ct_ecache_mutex));
115 BUG_ON(notify != new);
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +0000116 RCU_INIT_POINTER(nf_conntrack_event_cb, NULL);
Pablo Neira Ayusoe34d5c12009-06-03 10:32:06 +0200117 mutex_unlock(&nf_ct_ecache_mutex);
Patrick McHardy010c7d62007-03-14 16:40:10 -0700118}
119EXPORT_SYMBOL_GPL(nf_conntrack_unregister_notifier);
120
Pablo Neira Ayusoe34d5c12009-06-03 10:32:06 +0200121int nf_ct_expect_register_notifier(struct nf_exp_event_notifier *new)
Patrick McHardy010c7d62007-03-14 16:40:10 -0700122{
Pablo Neira Ayusoe34d5c12009-06-03 10:32:06 +0200123 int ret = 0;
Patrick McHardyb56f2d52010-05-10 18:47:57 +0200124 struct nf_exp_event_notifier *notify;
Pablo Neira Ayusoe34d5c12009-06-03 10:32:06 +0200125
126 mutex_lock(&nf_ct_ecache_mutex);
Patrick McHardyb56f2d52010-05-10 18:47:57 +0200127 notify = rcu_dereference_protected(nf_expect_event_cb,
128 lockdep_is_held(&nf_ct_ecache_mutex));
129 if (notify != NULL) {
Pablo Neira Ayusoe34d5c12009-06-03 10:32:06 +0200130 ret = -EBUSY;
131 goto out_unlock;
132 }
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +0000133 RCU_INIT_POINTER(nf_expect_event_cb, new);
Pablo Neira Ayusoe34d5c12009-06-03 10:32:06 +0200134 mutex_unlock(&nf_ct_ecache_mutex);
135 return ret;
136
137out_unlock:
138 mutex_unlock(&nf_ct_ecache_mutex);
139 return ret;
Patrick McHardy010c7d62007-03-14 16:40:10 -0700140}
Patrick McHardy68236452007-07-07 22:30:49 -0700141EXPORT_SYMBOL_GPL(nf_ct_expect_register_notifier);
Patrick McHardy010c7d62007-03-14 16:40:10 -0700142
Pablo Neira Ayusoe34d5c12009-06-03 10:32:06 +0200143void nf_ct_expect_unregister_notifier(struct nf_exp_event_notifier *new)
Patrick McHardy010c7d62007-03-14 16:40:10 -0700144{
Patrick McHardyb56f2d52010-05-10 18:47:57 +0200145 struct nf_exp_event_notifier *notify;
146
Pablo Neira Ayusoe34d5c12009-06-03 10:32:06 +0200147 mutex_lock(&nf_ct_ecache_mutex);
Patrick McHardyb56f2d52010-05-10 18:47:57 +0200148 notify = rcu_dereference_protected(nf_expect_event_cb,
149 lockdep_is_held(&nf_ct_ecache_mutex));
150 BUG_ON(notify != new);
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +0000151 RCU_INIT_POINTER(nf_expect_event_cb, NULL);
Pablo Neira Ayusoe34d5c12009-06-03 10:32:06 +0200152 mutex_unlock(&nf_ct_ecache_mutex);
Patrick McHardy010c7d62007-03-14 16:40:10 -0700153}
Patrick McHardy68236452007-07-07 22:30:49 -0700154EXPORT_SYMBOL_GPL(nf_ct_expect_unregister_notifier);
Pablo Neira Ayusoa0891aa2009-06-13 12:26:29 +0200155
156#define NF_CT_EVENTS_DEFAULT 1
157static int nf_ct_events __read_mostly = NF_CT_EVENTS_DEFAULT;
Pablo Neira Ayusodd7669a2009-06-13 12:30:52 +0200158static int nf_ct_events_retry_timeout __read_mostly = 15*HZ;
Pablo Neira Ayusoa0891aa2009-06-13 12:26:29 +0200159
160#ifdef CONFIG_SYSCTL
161static struct ctl_table event_sysctl_table[] = {
162 {
Pablo Neira Ayusoa0891aa2009-06-13 12:26:29 +0200163 .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 },
Pablo Neira Ayusodd7669a2009-06-13 12:30:52 +0200169 {
Pablo Neira Ayusodd7669a2009-06-13 12:30:52 +0200170 .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 },
Pablo Neira Ayusoa0891aa2009-06-13 12:26:29 +0200176 {}
177};
178#endif /* CONFIG_SYSCTL */
179
180static 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
187static 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;
Pablo Neira Ayusodd7669a2009-06-13 12:30:52 +0200197 table[1].data = &net->ct.sysctl_events_retry_timeout;
Pablo Neira Ayusoa0891aa2009-06-13 12:26:29 +0200198
199 net->ct.event_sysctl_header =
200 register_net_sysctl_table(net,
201 nf_net_netfilter_sysctl_path, table);
202 if (!net->ct.event_sysctl_header) {
203 printk(KERN_ERR "nf_ct_event: can't register to sysctl.\n");
204 goto out_register;
205 }
206 return 0;
207
208out_register:
209 kfree(table);
210out:
211 return -ENOMEM;
212}
213
214static void nf_conntrack_event_fini_sysctl(struct net *net)
215{
216 struct ctl_table *table;
217
218 table = net->ct.event_sysctl_header->ctl_table_arg;
219 unregister_net_sysctl_table(net->ct.event_sysctl_header);
220 kfree(table);
221}
222#else
223static int nf_conntrack_event_init_sysctl(struct net *net)
224{
225 return 0;
226}
227
228static void nf_conntrack_event_fini_sysctl(struct net *net)
229{
230}
231#endif /* CONFIG_SYSCTL */
232
233int nf_conntrack_ecache_init(struct net *net)
234{
235 int ret;
236
237 net->ct.sysctl_events = nf_ct_events;
Pablo Neira Ayusodd7669a2009-06-13 12:30:52 +0200238 net->ct.sysctl_events_retry_timeout = nf_ct_events_retry_timeout;
Pablo Neira Ayusoa0891aa2009-06-13 12:26:29 +0200239
240 if (net_eq(net, &init_net)) {
241 ret = nf_ct_extend_register(&event_extend);
242 if (ret < 0) {
243 printk(KERN_ERR "nf_ct_event: Unable to register "
244 "event extension.\n");
245 goto out_extend_register;
246 }
247 }
248
249 ret = nf_conntrack_event_init_sysctl(net);
250 if (ret < 0)
251 goto out_sysctl;
252
253 return 0;
254
255out_sysctl:
256 if (net_eq(net, &init_net))
257 nf_ct_extend_unregister(&event_extend);
258out_extend_register:
259 return ret;
260}
261
262void nf_conntrack_ecache_fini(struct net *net)
263{
264 nf_conntrack_event_fini_sysctl(net);
265 if (net_eq(net, &init_net))
266 nf_ct_extend_unregister(&event_extend);
267}