blob: 14af6329bdda21843ada4be624093e1ac6f980fb [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
Martin Josefssonf6180122006-11-29 02:35:01 +010030/* deliver cached events and clear cache entry - must be called with locally
31 * disabled softirqs */
Pablo Neira Ayusoa0891aa2009-06-13 12:26:29 +020032void nf_ct_deliver_cached_events(struct nf_conn *ct)
Martin Josefssonf6180122006-11-29 02:35:01 +010033{
Pablo Neira Ayuso70e99422011-11-22 00:16:51 +010034 struct net *net = nf_ct_net(ct);
Pablo Neira Ayusoa0891aa2009-06-13 12:26:29 +020035 unsigned long events;
Pablo Neira Ayusoe34d5c12009-06-03 10:32:06 +020036 struct nf_ct_event_notifier *notify;
Pablo Neira Ayusoa0891aa2009-06-13 12:26:29 +020037 struct nf_conntrack_ecache *e;
Pablo Neira Ayusoe34d5c12009-06-03 10:32:06 +020038
39 rcu_read_lock();
Pablo Neira Ayuso70e99422011-11-22 00:16:51 +010040 notify = rcu_dereference(net->ct.nf_conntrack_event_cb);
Pablo Neira Ayusoe34d5c12009-06-03 10:32:06 +020041 if (notify == NULL)
42 goto out_unlock;
43
Pablo Neira Ayusoa0891aa2009-06-13 12:26:29 +020044 e = nf_ct_ecache_find(ct);
45 if (e == NULL)
46 goto out_unlock;
47
48 events = xchg(&e->cache, 0);
49
50 if (nf_ct_is_confirmed(ct) && !nf_ct_is_dying(ct) && events) {
Pablo Neira Ayuso19abb7b2008-11-18 11:56:20 +010051 struct nf_ct_event item = {
Pablo Neira Ayusoa0891aa2009-06-13 12:26:29 +020052 .ct = ct,
Pablo Neira Ayuso19abb7b2008-11-18 11:56:20 +010053 .pid = 0,
54 .report = 0
55 };
Pablo Neira Ayusodd7669a2009-06-13 12:30:52 +020056 int ret;
57 /* We make a copy of the missed event cache without taking
58 * the lock, thus we may send missed events twice. However,
59 * this does not harm and it happens very rarely. */
60 unsigned long missed = e->missed;
Pablo Neira Ayuso19abb7b2008-11-18 11:56:20 +010061
Pablo Neira Ayuso3db7e932011-02-01 16:06:30 +010062 if (!((events | missed) & e->ctmask))
63 goto out_unlock;
64
Pablo Neira Ayusodd7669a2009-06-13 12:30:52 +020065 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 }
Pablo Neira Ayuso19abb7b2008-11-18 11:56:20 +010074 }
Martin Josefssonf6180122006-11-29 02:35:01 +010075
Pablo Neira Ayusoe34d5c12009-06-03 10:32:06 +020076out_unlock:
77 rcu_read_unlock();
Martin Josefssonf6180122006-11-29 02:35:01 +010078}
Patrick McHardy13b18332006-12-02 22:11:25 -080079EXPORT_SYMBOL_GPL(nf_ct_deliver_cached_events);
Martin Josefssonf6180122006-11-29 02:35:01 +010080
Pablo Neira Ayuso70e99422011-11-22 00:16:51 +010081int nf_conntrack_register_notifier(struct net *net,
82 struct nf_ct_event_notifier *new)
Patrick McHardy010c7d62007-03-14 16:40:10 -070083{
Pablo Neira Ayusoe34d5c12009-06-03 10:32:06 +020084 int ret = 0;
Patrick McHardyb56f2d52010-05-10 18:47:57 +020085 struct nf_ct_event_notifier *notify;
Pablo Neira Ayusoe34d5c12009-06-03 10:32:06 +020086
87 mutex_lock(&nf_ct_ecache_mutex);
Pablo Neira Ayuso70e99422011-11-22 00:16:51 +010088 notify = rcu_dereference_protected(net->ct.nf_conntrack_event_cb,
Patrick McHardyb56f2d52010-05-10 18:47:57 +020089 lockdep_is_held(&nf_ct_ecache_mutex));
90 if (notify != NULL) {
Pablo Neira Ayusoe34d5c12009-06-03 10:32:06 +020091 ret = -EBUSY;
92 goto out_unlock;
93 }
Eric Dumazetcf778b02012-01-12 04:41:32 +000094 rcu_assign_pointer(net->ct.nf_conntrack_event_cb, new);
Pablo Neira Ayusoe34d5c12009-06-03 10:32:06 +020095 mutex_unlock(&nf_ct_ecache_mutex);
96 return ret;
97
98out_unlock:
99 mutex_unlock(&nf_ct_ecache_mutex);
100 return ret;
Patrick McHardy010c7d62007-03-14 16:40:10 -0700101}
102EXPORT_SYMBOL_GPL(nf_conntrack_register_notifier);
103
Pablo Neira Ayuso70e99422011-11-22 00:16:51 +0100104void nf_conntrack_unregister_notifier(struct net *net,
105 struct nf_ct_event_notifier *new)
Patrick McHardy010c7d62007-03-14 16:40:10 -0700106{
Patrick McHardyb56f2d52010-05-10 18:47:57 +0200107 struct nf_ct_event_notifier *notify;
108
Pablo Neira Ayusoe34d5c12009-06-03 10:32:06 +0200109 mutex_lock(&nf_ct_ecache_mutex);
Pablo Neira Ayuso70e99422011-11-22 00:16:51 +0100110 notify = rcu_dereference_protected(net->ct.nf_conntrack_event_cb,
Patrick McHardyb56f2d52010-05-10 18:47:57 +0200111 lockdep_is_held(&nf_ct_ecache_mutex));
112 BUG_ON(notify != new);
Pablo Neira Ayuso70e99422011-11-22 00:16:51 +0100113 RCU_INIT_POINTER(net->ct.nf_conntrack_event_cb, NULL);
Pablo Neira Ayusoe34d5c12009-06-03 10:32:06 +0200114 mutex_unlock(&nf_ct_ecache_mutex);
Patrick McHardy010c7d62007-03-14 16:40:10 -0700115}
116EXPORT_SYMBOL_GPL(nf_conntrack_unregister_notifier);
117
Pablo Neira Ayuso70e99422011-11-22 00:16:51 +0100118int nf_ct_expect_register_notifier(struct net *net,
119 struct nf_exp_event_notifier *new)
Patrick McHardy010c7d62007-03-14 16:40:10 -0700120{
Pablo Neira Ayusoe34d5c12009-06-03 10:32:06 +0200121 int ret = 0;
Patrick McHardyb56f2d52010-05-10 18:47:57 +0200122 struct nf_exp_event_notifier *notify;
Pablo Neira Ayusoe34d5c12009-06-03 10:32:06 +0200123
124 mutex_lock(&nf_ct_ecache_mutex);
Pablo Neira Ayuso70e99422011-11-22 00:16:51 +0100125 notify = rcu_dereference_protected(net->ct.nf_expect_event_cb,
Patrick McHardyb56f2d52010-05-10 18:47:57 +0200126 lockdep_is_held(&nf_ct_ecache_mutex));
127 if (notify != NULL) {
Pablo Neira Ayusoe34d5c12009-06-03 10:32:06 +0200128 ret = -EBUSY;
129 goto out_unlock;
130 }
Eric Dumazetcf778b02012-01-12 04:41:32 +0000131 rcu_assign_pointer(net->ct.nf_expect_event_cb, new);
Pablo Neira Ayusoe34d5c12009-06-03 10:32:06 +0200132 mutex_unlock(&nf_ct_ecache_mutex);
133 return ret;
134
135out_unlock:
136 mutex_unlock(&nf_ct_ecache_mutex);
137 return ret;
Patrick McHardy010c7d62007-03-14 16:40:10 -0700138}
Patrick McHardy68236452007-07-07 22:30:49 -0700139EXPORT_SYMBOL_GPL(nf_ct_expect_register_notifier);
Patrick McHardy010c7d62007-03-14 16:40:10 -0700140
Pablo Neira Ayuso70e99422011-11-22 00:16:51 +0100141void nf_ct_expect_unregister_notifier(struct net *net,
142 struct nf_exp_event_notifier *new)
Patrick McHardy010c7d62007-03-14 16:40:10 -0700143{
Patrick McHardyb56f2d52010-05-10 18:47:57 +0200144 struct nf_exp_event_notifier *notify;
145
Pablo Neira Ayusoe34d5c12009-06-03 10:32:06 +0200146 mutex_lock(&nf_ct_ecache_mutex);
Pablo Neira Ayuso70e99422011-11-22 00:16:51 +0100147 notify = rcu_dereference_protected(net->ct.nf_expect_event_cb,
Patrick McHardyb56f2d52010-05-10 18:47:57 +0200148 lockdep_is_held(&nf_ct_ecache_mutex));
149 BUG_ON(notify != new);
Pablo Neira Ayuso70e99422011-11-22 00:16:51 +0100150 RCU_INIT_POINTER(net->ct.nf_expect_event_cb, NULL);
Pablo Neira Ayusoe34d5c12009-06-03 10:32:06 +0200151 mutex_unlock(&nf_ct_ecache_mutex);
Patrick McHardy010c7d62007-03-14 16:40:10 -0700152}
Patrick McHardy68236452007-07-07 22:30:49 -0700153EXPORT_SYMBOL_GPL(nf_ct_expect_unregister_notifier);
Pablo Neira Ayusoa0891aa2009-06-13 12:26:29 +0200154
155#define NF_CT_EVENTS_DEFAULT 1
156static int nf_ct_events __read_mostly = NF_CT_EVENTS_DEFAULT;
Pablo Neira Ayusodd7669a2009-06-13 12:30:52 +0200157static int nf_ct_events_retry_timeout __read_mostly = 15*HZ;
Pablo Neira Ayusoa0891aa2009-06-13 12:26:29 +0200158
159#ifdef CONFIG_SYSCTL
160static struct ctl_table event_sysctl_table[] = {
161 {
Pablo Neira Ayusoa0891aa2009-06-13 12:26:29 +0200162 .procname = "nf_conntrack_events",
163 .data = &init_net.ct.sysctl_events,
164 .maxlen = sizeof(unsigned int),
165 .mode = 0644,
166 .proc_handler = proc_dointvec,
167 },
Pablo Neira Ayusodd7669a2009-06-13 12:30:52 +0200168 {
Pablo Neira Ayusodd7669a2009-06-13 12:30:52 +0200169 .procname = "nf_conntrack_events_retry_timeout",
170 .data = &init_net.ct.sysctl_events_retry_timeout,
171 .maxlen = sizeof(unsigned int),
172 .mode = 0644,
173 .proc_handler = proc_dointvec_jiffies,
174 },
Pablo Neira Ayusoa0891aa2009-06-13 12:26:29 +0200175 {}
176};
177#endif /* CONFIG_SYSCTL */
178
179static struct nf_ct_ext_type event_extend __read_mostly = {
180 .len = sizeof(struct nf_conntrack_ecache),
181 .align = __alignof__(struct nf_conntrack_ecache),
182 .id = NF_CT_EXT_ECACHE,
183};
184
185#ifdef CONFIG_SYSCTL
186static int nf_conntrack_event_init_sysctl(struct net *net)
187{
188 struct ctl_table *table;
189
190 table = kmemdup(event_sysctl_table, sizeof(event_sysctl_table),
191 GFP_KERNEL);
192 if (!table)
193 goto out;
194
195 table[0].data = &net->ct.sysctl_events;
Pablo Neira Ayusodd7669a2009-06-13 12:30:52 +0200196 table[1].data = &net->ct.sysctl_events_retry_timeout;
Pablo Neira Ayusoa0891aa2009-06-13 12:26:29 +0200197
198 net->ct.event_sysctl_header =
199 register_net_sysctl_table(net,
200 nf_net_netfilter_sysctl_path, table);
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
207out_register:
208 kfree(table);
209out:
210 return -ENOMEM;
211}
212
213static 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
222static int nf_conntrack_event_init_sysctl(struct net *net)
223{
224 return 0;
225}
226
227static void nf_conntrack_event_fini_sysctl(struct net *net)
228{
229}
230#endif /* CONFIG_SYSCTL */
231
232int nf_conntrack_ecache_init(struct net *net)
233{
234 int ret;
235
236 net->ct.sysctl_events = nf_ct_events;
Pablo Neira Ayusodd7669a2009-06-13 12:30:52 +0200237 net->ct.sysctl_events_retry_timeout = nf_ct_events_retry_timeout;
Pablo Neira Ayusoa0891aa2009-06-13 12:26:29 +0200238
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
254out_sysctl:
255 if (net_eq(net, &init_net))
256 nf_ct_extend_unregister(&event_extend);
257out_extend_register:
258 return ret;
259}
260
261void 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}