blob: aee560b4768dda0522107a9aefeff5d382022516 [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>
21
22#include <net/netfilter/nf_conntrack.h>
Martin Josefssonf6180122006-11-29 02:35:01 +010023#include <net/netfilter/nf_conntrack_core.h>
Pablo Neira Ayusoa0891aa2009-06-13 12:26:29 +020024#include <net/netfilter/nf_conntrack_extend.h>
Martin Josefssonf6180122006-11-29 02:35:01 +010025
Pablo Neira Ayusoe34d5c12009-06-03 10:32:06 +020026static DEFINE_MUTEX(nf_ct_ecache_mutex);
Patrick McHardy13b18332006-12-02 22:11:25 -080027
Pablo Neira Ayusoe34d5c12009-06-03 10:32:06 +020028struct nf_ct_event_notifier *nf_conntrack_event_cb __read_mostly;
29EXPORT_SYMBOL_GPL(nf_conntrack_event_cb);
30
31struct nf_exp_event_notifier *nf_expect_event_cb __read_mostly;
32EXPORT_SYMBOL_GPL(nf_expect_event_cb);
Martin Josefssonf6180122006-11-29 02:35:01 +010033
Martin Josefssonf6180122006-11-29 02:35:01 +010034/* deliver cached events and clear cache entry - must be called with locally
35 * disabled softirqs */
Pablo Neira Ayusoa0891aa2009-06-13 12:26:29 +020036void nf_ct_deliver_cached_events(struct nf_conn *ct)
Martin Josefssonf6180122006-11-29 02:35:01 +010037{
Pablo Neira Ayusoa0891aa2009-06-13 12:26:29 +020038 unsigned long events;
Pablo Neira Ayusoe34d5c12009-06-03 10:32:06 +020039 struct nf_ct_event_notifier *notify;
Pablo Neira Ayusoa0891aa2009-06-13 12:26:29 +020040 struct nf_conntrack_ecache *e;
Pablo Neira Ayusoe34d5c12009-06-03 10:32:06 +020041
42 rcu_read_lock();
43 notify = rcu_dereference(nf_conntrack_event_cb);
44 if (notify == NULL)
45 goto out_unlock;
46
Pablo Neira Ayusoa0891aa2009-06-13 12:26:29 +020047 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) {
Pablo Neira Ayuso19abb7b2008-11-18 11:56:20 +010054 struct nf_ct_event item = {
Pablo Neira Ayusoa0891aa2009-06-13 12:26:29 +020055 .ct = ct,
Pablo Neira Ayuso19abb7b2008-11-18 11:56:20 +010056 .pid = 0,
57 .report = 0
58 };
Pablo Neira Ayusodd7669a2009-06-13 12:30:52 +020059 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;
Pablo Neira Ayuso19abb7b2008-11-18 11:56:20 +010064
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 Ayusoe34d5c12009-06-03 10:32:06 +020081int nf_conntrack_register_notifier(struct nf_ct_event_notifier *new)
Patrick McHardy010c7d62007-03-14 16:40:10 -070082{
Pablo Neira Ayusoe34d5c12009-06-03 10:32:06 +020083 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
96out_unlock:
97 mutex_unlock(&nf_ct_ecache_mutex);
98 return ret;
Patrick McHardy010c7d62007-03-14 16:40:10 -070099}
100EXPORT_SYMBOL_GPL(nf_conntrack_register_notifier);
101
Pablo Neira Ayusoe34d5c12009-06-03 10:32:06 +0200102void nf_conntrack_unregister_notifier(struct nf_ct_event_notifier *new)
Patrick McHardy010c7d62007-03-14 16:40:10 -0700103{
Pablo Neira Ayusoe34d5c12009-06-03 10:32:06 +0200104 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);
Patrick McHardy010c7d62007-03-14 16:40:10 -0700111}
112EXPORT_SYMBOL_GPL(nf_conntrack_unregister_notifier);
113
Pablo Neira Ayusoe34d5c12009-06-03 10:32:06 +0200114int nf_ct_expect_register_notifier(struct nf_exp_event_notifier *new)
Patrick McHardy010c7d62007-03-14 16:40:10 -0700115{
Pablo Neira Ayusoe34d5c12009-06-03 10:32:06 +0200116 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
129out_unlock:
130 mutex_unlock(&nf_ct_ecache_mutex);
131 return ret;
Patrick McHardy010c7d62007-03-14 16:40:10 -0700132}
Patrick McHardy68236452007-07-07 22:30:49 -0700133EXPORT_SYMBOL_GPL(nf_ct_expect_register_notifier);
Patrick McHardy010c7d62007-03-14 16:40:10 -0700134
Pablo Neira Ayusoe34d5c12009-06-03 10:32:06 +0200135void nf_ct_expect_unregister_notifier(struct nf_exp_event_notifier *new)
Patrick McHardy010c7d62007-03-14 16:40:10 -0700136{
Pablo Neira Ayusoe34d5c12009-06-03 10:32:06 +0200137 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);
Patrick McHardy010c7d62007-03-14 16:40:10 -0700144}
Patrick McHardy68236452007-07-07 22:30:49 -0700145EXPORT_SYMBOL_GPL(nf_ct_expect_unregister_notifier);
Pablo Neira Ayusoa0891aa2009-06-13 12:26:29 +0200146
147#define NF_CT_EVENTS_DEFAULT 1
148static int nf_ct_events __read_mostly = NF_CT_EVENTS_DEFAULT;
Pablo Neira Ayusodd7669a2009-06-13 12:30:52 +0200149static int nf_ct_events_retry_timeout __read_mostly = 15*HZ;
Pablo Neira Ayusoa0891aa2009-06-13 12:26:29 +0200150
151#ifdef CONFIG_SYSCTL
152static 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 },
Pablo Neira Ayusodd7669a2009-06-13 12:30:52 +0200161 {
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 },
Pablo Neira Ayusoa0891aa2009-06-13 12:26:29 +0200169 {}
170};
171#endif /* CONFIG_SYSCTL */
172
173static 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
180static 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;
Pablo Neira Ayusodd7669a2009-06-13 12:30:52 +0200190 table[1].data = &net->ct.sysctl_events_retry_timeout;
Pablo Neira Ayusoa0891aa2009-06-13 12:26:29 +0200191
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
201out_register:
202 kfree(table);
203out:
204 return -ENOMEM;
205}
206
207static 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
216static int nf_conntrack_event_init_sysctl(struct net *net)
217{
218 return 0;
219}
220
221static void nf_conntrack_event_fini_sysctl(struct net *net)
222{
223}
224#endif /* CONFIG_SYSCTL */
225
226int nf_conntrack_ecache_init(struct net *net)
227{
228 int ret;
229
230 net->ct.sysctl_events = nf_ct_events;
Pablo Neira Ayusodd7669a2009-06-13 12:30:52 +0200231 net->ct.sysctl_events_retry_timeout = nf_ct_events_retry_timeout;
Pablo Neira Ayusoa0891aa2009-06-13 12:26:29 +0200232
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
248out_sysctl:
249 if (net_eq(net, &init_net))
250 nf_ct_extend_unregister(&event_extend);
251out_extend_register:
252 return ret;
253}
254
255void 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}