Merge tag 'v3.10.108' into update
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / net / netfilter / nf_conntrack_ecache.c
CommitLineData
f6180121
MJ
1/* Event cache for netfilter. */
2
f229f6ce
PM
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>
f6180121
MJ
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>
f6180121
MJ
21#include <linux/kernel.h>
22#include <linux/netdevice.h>
5a0e3ad6 23#include <linux/slab.h>
bc3b2d7f 24#include <linux/export.h>
f6180121
MJ
25
26#include <net/netfilter/nf_conntrack.h>
f6180121 27#include <net/netfilter/nf_conntrack_core.h>
a0891aa6 28#include <net/netfilter/nf_conntrack_extend.h>
f6180121 29
e34d5c1a 30static DEFINE_MUTEX(nf_ct_ecache_mutex);
13b18339 31
f6180121
MJ
32/* deliver cached events and clear cache entry - must be called with locally
33 * disabled softirqs */
a0891aa6 34void nf_ct_deliver_cached_events(struct nf_conn *ct)
f6180121 35{
70e9942f 36 struct net *net = nf_ct_net(ct);
58020f77 37 unsigned long events, missed;
e34d5c1a 38 struct nf_ct_event_notifier *notify;
a0891aa6 39 struct nf_conntrack_ecache *e;
58020f77
TZ
40 struct nf_ct_event item;
41 int ret;
e34d5c1a
PNA
42
43 rcu_read_lock();
70e9942f 44 notify = rcu_dereference(net->ct.nf_conntrack_event_cb);
e34d5c1a
PNA
45 if (notify == NULL)
46 goto out_unlock;
47
a0891aa6
PNA
48 e = nf_ct_ecache_find(ct);
49 if (e == NULL)
50 goto out_unlock;
51
52 events = xchg(&e->cache, 0);
53
58020f77
TZ
54 if (!nf_ct_is_confirmed(ct) || nf_ct_is_dying(ct) || !events)
55 goto out_unlock;
56
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 missed = e->missed;
61
62 if (!((events | missed) & e->ctmask))
63 goto out_unlock;
64
65 item.ct = ct;
15e47304 66 item.portid = 0;
58020f77
TZ
67 item.report = 0;
68
69 ret = notify->fcn(events | missed, &item);
70
71 if (likely(ret >= 0 && !missed))
72 goto out_unlock;
73
74 spin_lock_bh(&ct->lock);
75 if (ret < 0)
76 e->missed |= events;
77 else
78 e->missed &= ~missed;
79 spin_unlock_bh(&ct->lock);
f6180121 80
e34d5c1a
PNA
81out_unlock:
82 rcu_read_unlock();
f6180121 83}
13b18339 84EXPORT_SYMBOL_GPL(nf_ct_deliver_cached_events);
f6180121 85
70e9942f
PNA
86int nf_conntrack_register_notifier(struct net *net,
87 struct nf_ct_event_notifier *new)
010c7d6f 88{
031d7709 89 int ret;
b56f2d55 90 struct nf_ct_event_notifier *notify;
e34d5c1a
PNA
91
92 mutex_lock(&nf_ct_ecache_mutex);
70e9942f 93 notify = rcu_dereference_protected(net->ct.nf_conntrack_event_cb,
b56f2d55
PM
94 lockdep_is_held(&nf_ct_ecache_mutex));
95 if (notify != NULL) {
e34d5c1a
PNA
96 ret = -EBUSY;
97 goto out_unlock;
98 }
cf778b00 99 rcu_assign_pointer(net->ct.nf_conntrack_event_cb, new);
031d7709 100 ret = 0;
e34d5c1a
PNA
101
102out_unlock:
103 mutex_unlock(&nf_ct_ecache_mutex);
104 return ret;
010c7d6f
PM
105}
106EXPORT_SYMBOL_GPL(nf_conntrack_register_notifier);
107
70e9942f
PNA
108void nf_conntrack_unregister_notifier(struct net *net,
109 struct nf_ct_event_notifier *new)
010c7d6f 110{
b56f2d55
PM
111 struct nf_ct_event_notifier *notify;
112
e34d5c1a 113 mutex_lock(&nf_ct_ecache_mutex);
70e9942f 114 notify = rcu_dereference_protected(net->ct.nf_conntrack_event_cb,
b56f2d55
PM
115 lockdep_is_held(&nf_ct_ecache_mutex));
116 BUG_ON(notify != new);
70e9942f 117 RCU_INIT_POINTER(net->ct.nf_conntrack_event_cb, NULL);
e34d5c1a 118 mutex_unlock(&nf_ct_ecache_mutex);
09ab11eb 119 /* synchronize_rcu() is called from ctnetlink_exit. */
010c7d6f
PM
120}
121EXPORT_SYMBOL_GPL(nf_conntrack_unregister_notifier);
122
70e9942f
PNA
123int nf_ct_expect_register_notifier(struct net *net,
124 struct nf_exp_event_notifier *new)
010c7d6f 125{
031d7709 126 int ret;
b56f2d55 127 struct nf_exp_event_notifier *notify;
e34d5c1a
PNA
128
129 mutex_lock(&nf_ct_ecache_mutex);
70e9942f 130 notify = rcu_dereference_protected(net->ct.nf_expect_event_cb,
b56f2d55
PM
131 lockdep_is_held(&nf_ct_ecache_mutex));
132 if (notify != NULL) {
e34d5c1a
PNA
133 ret = -EBUSY;
134 goto out_unlock;
135 }
cf778b00 136 rcu_assign_pointer(net->ct.nf_expect_event_cb, new);
031d7709 137 ret = 0;
e34d5c1a
PNA
138
139out_unlock:
140 mutex_unlock(&nf_ct_ecache_mutex);
141 return ret;
010c7d6f 142}
6823645d 143EXPORT_SYMBOL_GPL(nf_ct_expect_register_notifier);
010c7d6f 144
70e9942f
PNA
145void nf_ct_expect_unregister_notifier(struct net *net,
146 struct nf_exp_event_notifier *new)
010c7d6f 147{
b56f2d55
PM
148 struct nf_exp_event_notifier *notify;
149
e34d5c1a 150 mutex_lock(&nf_ct_ecache_mutex);
70e9942f 151 notify = rcu_dereference_protected(net->ct.nf_expect_event_cb,
b56f2d55
PM
152 lockdep_is_held(&nf_ct_ecache_mutex));
153 BUG_ON(notify != new);
70e9942f 154 RCU_INIT_POINTER(net->ct.nf_expect_event_cb, NULL);
e34d5c1a 155 mutex_unlock(&nf_ct_ecache_mutex);
09ab11eb 156 /* synchronize_rcu() is called from ctnetlink_exit. */
010c7d6f 157}
6823645d 158EXPORT_SYMBOL_GPL(nf_ct_expect_unregister_notifier);
a0891aa6
PNA
159
160#define NF_CT_EVENTS_DEFAULT 1
161static int nf_ct_events __read_mostly = NF_CT_EVENTS_DEFAULT;
dd7669a9 162static int nf_ct_events_retry_timeout __read_mostly = 15*HZ;
a0891aa6
PNA
163
164#ifdef CONFIG_SYSCTL
165static struct ctl_table event_sysctl_table[] = {
166 {
a0891aa6
PNA
167 .procname = "nf_conntrack_events",
168 .data = &init_net.ct.sysctl_events,
169 .maxlen = sizeof(unsigned int),
170 .mode = 0644,
171 .proc_handler = proc_dointvec,
172 },
dd7669a9 173 {
dd7669a9
PNA
174 .procname = "nf_conntrack_events_retry_timeout",
175 .data = &init_net.ct.sysctl_events_retry_timeout,
176 .maxlen = sizeof(unsigned int),
177 .mode = 0644,
178 .proc_handler = proc_dointvec_jiffies,
179 },
a0891aa6
PNA
180 {}
181};
182#endif /* CONFIG_SYSCTL */
183
184static struct nf_ct_ext_type event_extend __read_mostly = {
185 .len = sizeof(struct nf_conntrack_ecache),
186 .align = __alignof__(struct nf_conntrack_ecache),
187 .id = NF_CT_EXT_ECACHE,
188};
189
190#ifdef CONFIG_SYSCTL
191static int nf_conntrack_event_init_sysctl(struct net *net)
192{
193 struct ctl_table *table;
194
195 table = kmemdup(event_sysctl_table, sizeof(event_sysctl_table),
196 GFP_KERNEL);
197 if (!table)
198 goto out;
199
200 table[0].data = &net->ct.sysctl_events;
dd7669a9 201 table[1].data = &net->ct.sysctl_events_retry_timeout;
a0891aa6 202
464dc801
EB
203 /* Don't export sysctls to unprivileged users */
204 if (net->user_ns != &init_user_ns)
205 table[0].procname = NULL;
206
a0891aa6 207 net->ct.event_sysctl_header =
ec8f23ce 208 register_net_sysctl(net, "net/netfilter", table);
a0891aa6
PNA
209 if (!net->ct.event_sysctl_header) {
210 printk(KERN_ERR "nf_ct_event: can't register to sysctl.\n");
211 goto out_register;
212 }
213 return 0;
214
215out_register:
216 kfree(table);
217out:
218 return -ENOMEM;
219}
220
221static void nf_conntrack_event_fini_sysctl(struct net *net)
222{
223 struct ctl_table *table;
224
225 table = net->ct.event_sysctl_header->ctl_table_arg;
226 unregister_net_sysctl_table(net->ct.event_sysctl_header);
227 kfree(table);
228}
229#else
230static int nf_conntrack_event_init_sysctl(struct net *net)
231{
232 return 0;
233}
234
235static void nf_conntrack_event_fini_sysctl(struct net *net)
236{
237}
238#endif /* CONFIG_SYSCTL */
239
3fe0f943 240int nf_conntrack_ecache_pernet_init(struct net *net)
a0891aa6 241{
a0891aa6 242 net->ct.sysctl_events = nf_ct_events;
dd7669a9 243 net->ct.sysctl_events_retry_timeout = nf_ct_events_retry_timeout;
3fe0f943
G
244 return nf_conntrack_event_init_sysctl(net);
245}
a0891aa6 246
3fe0f943
G
247void nf_conntrack_ecache_pernet_fini(struct net *net)
248{
249 nf_conntrack_event_fini_sysctl(net);
250}
a0891aa6 251
3fe0f943
G
252int nf_conntrack_ecache_init(void)
253{
254 int ret = nf_ct_extend_register(&event_extend);
a0891aa6 255 if (ret < 0)
3fe0f943 256 pr_err("nf_ct_event: Unable to register event extension.\n");
a0891aa6
PNA
257 return ret;
258}
259
3fe0f943 260void nf_conntrack_ecache_fini(void)
a0891aa6 261{
3fe0f943 262 nf_ct_extend_unregister(&event_extend);
a0891aa6 263}