OMAP3: SR: Fix init voltage on OPP change
[linux-ginger.git] / net / netfilter / nf_conntrack_ecache.c
blobaee560b4768dda0522107a9aefeff5d382022516
1 /* Event cache for netfilter. */
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>
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.
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>
19 #include <linux/kernel.h>
20 #include <linux/netdevice.h>
22 #include <net/netfilter/nf_conntrack.h>
23 #include <net/netfilter/nf_conntrack_core.h>
24 #include <net/netfilter/nf_conntrack_extend.h>
26 static DEFINE_MUTEX(nf_ct_ecache_mutex);
28 struct nf_ct_event_notifier *nf_conntrack_event_cb __read_mostly;
29 EXPORT_SYMBOL_GPL(nf_conntrack_event_cb);
31 struct nf_exp_event_notifier *nf_expect_event_cb __read_mostly;
32 EXPORT_SYMBOL_GPL(nf_expect_event_cb);
34 /* deliver cached events and clear cache entry - must be called with locally
35 * disabled softirqs */
36 void nf_ct_deliver_cached_events(struct nf_conn *ct)
38 unsigned long events;
39 struct nf_ct_event_notifier *notify;
40 struct nf_conntrack_ecache *e;
42 rcu_read_lock();
43 notify = rcu_dereference(nf_conntrack_event_cb);
44 if (notify == NULL)
45 goto out_unlock;
47 e = nf_ct_ecache_find(ct);
48 if (e == NULL)
49 goto out_unlock;
51 events = xchg(&e->cache, 0);
53 if (nf_ct_is_confirmed(ct) && !nf_ct_is_dying(ct) && events) {
54 struct nf_ct_event item = {
55 .ct = ct,
56 .pid = 0,
57 .report = 0
59 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;
65 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);
76 out_unlock:
77 rcu_read_unlock();
79 EXPORT_SYMBOL_GPL(nf_ct_deliver_cached_events);
81 int nf_conntrack_register_notifier(struct nf_ct_event_notifier *new)
83 int ret = 0;
84 struct nf_ct_event_notifier *notify;
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;
92 rcu_assign_pointer(nf_conntrack_event_cb, new);
93 mutex_unlock(&nf_ct_ecache_mutex);
94 return ret;
96 out_unlock:
97 mutex_unlock(&nf_ct_ecache_mutex);
98 return ret;
100 EXPORT_SYMBOL_GPL(nf_conntrack_register_notifier);
102 void nf_conntrack_unregister_notifier(struct nf_ct_event_notifier *new)
104 struct nf_ct_event_notifier *notify;
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);
112 EXPORT_SYMBOL_GPL(nf_conntrack_unregister_notifier);
114 int nf_ct_expect_register_notifier(struct nf_exp_event_notifier *new)
116 int ret = 0;
117 struct nf_exp_event_notifier *notify;
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;
125 rcu_assign_pointer(nf_expect_event_cb, new);
126 mutex_unlock(&nf_ct_ecache_mutex);
127 return ret;
129 out_unlock:
130 mutex_unlock(&nf_ct_ecache_mutex);
131 return ret;
133 EXPORT_SYMBOL_GPL(nf_ct_expect_register_notifier);
135 void nf_ct_expect_unregister_notifier(struct nf_exp_event_notifier *new)
137 struct nf_exp_event_notifier *notify;
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);
145 EXPORT_SYMBOL_GPL(nf_ct_expect_unregister_notifier);
147 #define NF_CT_EVENTS_DEFAULT 1
148 static int nf_ct_events __read_mostly = NF_CT_EVENTS_DEFAULT;
149 static int nf_ct_events_retry_timeout __read_mostly = 15*HZ;
151 #ifdef CONFIG_SYSCTL
152 static struct ctl_table event_sysctl_table[] = {
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,
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,
171 #endif /* CONFIG_SYSCTL */
173 static 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,
179 #ifdef CONFIG_SYSCTL
180 static int nf_conntrack_event_init_sysctl(struct net *net)
182 struct ctl_table *table;
184 table = kmemdup(event_sysctl_table, sizeof(event_sysctl_table),
185 GFP_KERNEL);
186 if (!table)
187 goto out;
189 table[0].data = &net->ct.sysctl_events;
190 table[1].data = &net->ct.sysctl_events_retry_timeout;
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;
199 return 0;
201 out_register:
202 kfree(table);
203 out:
204 return -ENOMEM;
207 static void nf_conntrack_event_fini_sysctl(struct net *net)
209 struct ctl_table *table;
211 table = net->ct.event_sysctl_header->ctl_table_arg;
212 unregister_net_sysctl_table(net->ct.event_sysctl_header);
213 kfree(table);
215 #else
216 static int nf_conntrack_event_init_sysctl(struct net *net)
218 return 0;
221 static void nf_conntrack_event_fini_sysctl(struct net *net)
224 #endif /* CONFIG_SYSCTL */
226 int nf_conntrack_ecache_init(struct net *net)
228 int ret;
230 net->ct.sysctl_events = nf_ct_events;
231 net->ct.sysctl_events_retry_timeout = nf_ct_events_retry_timeout;
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;
242 ret = nf_conntrack_event_init_sysctl(net);
243 if (ret < 0)
244 goto out_sysctl;
246 return 0;
248 out_sysctl:
249 if (net_eq(net, &init_net))
250 nf_ct_extend_unregister(&event_extend);
251 out_extend_register:
252 return ret;
255 void nf_conntrack_ecache_fini(struct net *net)
257 nf_conntrack_event_fini_sysctl(net);
258 if (net_eq(net, &init_net))
259 nf_ct_extend_unregister(&event_extend);