blob: d28011b428455d83d1230210982699cb8774c55b [file] [log] [blame]
Martin Josefssonf6180122006-11-29 02:35:01 +01001/* Event cache for netfilter. */
2
Patrick McHardyf229f6c2013-04-06 15:24:29 +02003/*
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>
Martin Josefssonf6180122006-11-29 02:35:01 +01008 *
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>
Martin Josefssonf6180122006-11-29 02:35:01 +010021#include <linux/kernel.h>
22#include <linux/netdevice.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040024#include <linux/export.h>
Martin Josefssonf6180122006-11-29 02:35:01 +010025
26#include <net/netfilter/nf_conntrack.h>
Martin Josefssonf6180122006-11-29 02:35:01 +010027#include <net/netfilter/nf_conntrack_core.h>
Pablo Neira Ayusoa0891aa2009-06-13 12:26:29 +020028#include <net/netfilter/nf_conntrack_extend.h>
Martin Josefssonf6180122006-11-29 02:35:01 +010029
Pablo Neira Ayusoe34d5c12009-06-03 10:32:06 +020030static DEFINE_MUTEX(nf_ct_ecache_mutex);
Patrick McHardy13b18332006-12-02 22:11:25 -080031
Florian Westphal95005072014-06-10 23:12:56 +020032#define ECACHE_RETRY_WAIT (HZ/10)
33
34enum retry_state {
35 STATE_CONGESTED,
36 STATE_RESTART,
37 STATE_DONE,
38};
39
40static enum retry_state ecache_work_evict_list(struct ct_pcpu *pcpu)
41{
42 struct nf_conn *refs[16];
43 struct nf_conntrack_tuple_hash *h;
44 struct hlist_nulls_node *n;
45 unsigned int evicted = 0;
46 enum retry_state ret = STATE_DONE;
47
48 spin_lock(&pcpu->lock);
49
50 hlist_nulls_for_each_entry(h, n, &pcpu->dying, hnnode) {
51 struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h);
52
53 if (nf_ct_is_dying(ct))
54 continue;
55
56 if (nf_conntrack_event(IPCT_DESTROY, ct)) {
57 ret = STATE_CONGESTED;
58 break;
59 }
60
61 /* we've got the event delivered, now it's dying */
62 set_bit(IPS_DYING_BIT, &ct->status);
63 refs[evicted] = ct;
64
65 if (++evicted >= ARRAY_SIZE(refs)) {
66 ret = STATE_RESTART;
67 break;
68 }
69 }
70
71 spin_unlock(&pcpu->lock);
72
73 /* can't _put while holding lock */
74 while (evicted)
75 nf_ct_put(refs[--evicted]);
76
77 return ret;
78}
79
80static void ecache_work(struct work_struct *work)
81{
82 struct netns_ct *ctnet =
83 container_of(work, struct netns_ct, ecache_dwork.work);
84 int cpu, delay = -1;
85 struct ct_pcpu *pcpu;
86
87 local_bh_disable();
88
89 for_each_possible_cpu(cpu) {
90 enum retry_state ret;
91
92 pcpu = per_cpu_ptr(ctnet->pcpu_lists, cpu);
93
94 ret = ecache_work_evict_list(pcpu);
95
96 switch (ret) {
97 case STATE_CONGESTED:
98 delay = ECACHE_RETRY_WAIT;
99 goto out;
100 case STATE_RESTART:
101 delay = 0;
102 break;
103 case STATE_DONE:
104 break;
105 }
106 }
107
108 out:
109 local_bh_enable();
110
111 ctnet->ecache_dwork_pending = delay > 0;
112 if (delay >= 0)
113 schedule_delayed_work(&ctnet->ecache_dwork, delay);
114}
115
Florian Westphal3c435e22016-04-11 21:52:35 +0200116int nf_conntrack_eventmask_report(unsigned int eventmask, struct nf_conn *ct,
117 u32 portid, int report)
118{
119 int ret = 0;
120 struct net *net = nf_ct_net(ct);
121 struct nf_ct_event_notifier *notify;
122 struct nf_conntrack_ecache *e;
123
124 rcu_read_lock();
125 notify = rcu_dereference(net->ct.nf_conntrack_event_cb);
126 if (!notify)
127 goto out_unlock;
128
129 e = nf_ct_ecache_find(ct);
130 if (!e)
131 goto out_unlock;
132
133 if (nf_ct_is_confirmed(ct) && !nf_ct_is_dying(ct)) {
134 struct nf_ct_event item = {
135 .ct = ct,
136 .portid = e->portid ? e->portid : portid,
137 .report = report
138 };
139 /* This is a resent of a destroy event? If so, skip missed */
140 unsigned long missed = e->portid ? 0 : e->missed;
141
142 if (!((eventmask | missed) & e->ctmask))
143 goto out_unlock;
144
145 ret = notify->fcn(eventmask | missed, &item);
146 if (unlikely(ret < 0 || missed)) {
147 spin_lock_bh(&ct->lock);
148 if (ret < 0) {
149 /* This is a destroy event that has been
150 * triggered by a process, we store the PORTID
151 * to include it in the retransmission.
152 */
153 if (eventmask & (1 << IPCT_DESTROY) &&
154 e->portid == 0 && portid != 0)
155 e->portid = portid;
156 else
157 e->missed |= eventmask;
158 } else {
159 e->missed &= ~missed;
160 }
161 spin_unlock_bh(&ct->lock);
162 }
163 }
164out_unlock:
165 rcu_read_unlock();
166 return ret;
167}
168EXPORT_SYMBOL_GPL(nf_conntrack_eventmask_report);
169
Martin Josefssonf6180122006-11-29 02:35:01 +0100170/* deliver cached events and clear cache entry - must be called with locally
171 * disabled softirqs */
Pablo Neira Ayusoa0891aa2009-06-13 12:26:29 +0200172void nf_ct_deliver_cached_events(struct nf_conn *ct)
Martin Josefssonf6180122006-11-29 02:35:01 +0100173{
Pablo Neira Ayuso70e99422011-11-22 00:16:51 +0100174 struct net *net = nf_ct_net(ct);
Tony Zelenoff58020f72012-02-22 10:48:01 +0400175 unsigned long events, missed;
Pablo Neira Ayusoe34d5c12009-06-03 10:32:06 +0200176 struct nf_ct_event_notifier *notify;
Pablo Neira Ayusoa0891aa2009-06-13 12:26:29 +0200177 struct nf_conntrack_ecache *e;
Tony Zelenoff58020f72012-02-22 10:48:01 +0400178 struct nf_ct_event item;
179 int ret;
Pablo Neira Ayusoe34d5c12009-06-03 10:32:06 +0200180
181 rcu_read_lock();
Pablo Neira Ayuso70e99422011-11-22 00:16:51 +0100182 notify = rcu_dereference(net->ct.nf_conntrack_event_cb);
Pablo Neira Ayusoe34d5c12009-06-03 10:32:06 +0200183 if (notify == NULL)
184 goto out_unlock;
185
Pablo Neira Ayusoa0891aa2009-06-13 12:26:29 +0200186 e = nf_ct_ecache_find(ct);
187 if (e == NULL)
188 goto out_unlock;
189
190 events = xchg(&e->cache, 0);
191
Tony Zelenoff58020f72012-02-22 10:48:01 +0400192 if (!nf_ct_is_confirmed(ct) || nf_ct_is_dying(ct) || !events)
193 goto out_unlock;
Pablo Neira Ayuso19abb7b2008-11-18 11:56:20 +0100194
Tony Zelenoff58020f72012-02-22 10:48:01 +0400195 /* We make a copy of the missed event cache without taking
196 * the lock, thus we may send missed events twice. However,
197 * this does not harm and it happens very rarely. */
198 missed = e->missed;
Pablo Neira Ayuso3db7e932011-02-01 16:06:30 +0100199
Tony Zelenoff58020f72012-02-22 10:48:01 +0400200 if (!((events | missed) & e->ctmask))
201 goto out_unlock;
202
203 item.ct = ct;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000204 item.portid = 0;
Tony Zelenoff58020f72012-02-22 10:48:01 +0400205 item.report = 0;
206
207 ret = notify->fcn(events | missed, &item);
208
209 if (likely(ret >= 0 && !missed))
210 goto out_unlock;
211
212 spin_lock_bh(&ct->lock);
213 if (ret < 0)
214 e->missed |= events;
215 else
216 e->missed &= ~missed;
217 spin_unlock_bh(&ct->lock);
Martin Josefssonf6180122006-11-29 02:35:01 +0100218
Pablo Neira Ayusoe34d5c12009-06-03 10:32:06 +0200219out_unlock:
220 rcu_read_unlock();
Martin Josefssonf6180122006-11-29 02:35:01 +0100221}
Patrick McHardy13b18332006-12-02 22:11:25 -0800222EXPORT_SYMBOL_GPL(nf_ct_deliver_cached_events);
Martin Josefssonf6180122006-11-29 02:35:01 +0100223
Florian Westphalecdfb482016-04-11 21:52:36 +0200224void nf_ct_expect_event_report(enum ip_conntrack_expect_events event,
225 struct nf_conntrack_expect *exp,
226 u32 portid, int report)
227
228{
229 struct net *net = nf_ct_exp_net(exp);
230 struct nf_exp_event_notifier *notify;
231 struct nf_conntrack_ecache *e;
232
233 rcu_read_lock();
234 notify = rcu_dereference(net->ct.nf_expect_event_cb);
235 if (!notify)
236 goto out_unlock;
237
238 e = nf_ct_ecache_find(exp->master);
239 if (!e)
240 goto out_unlock;
241
242 if (e->expmask & (1 << event)) {
243 struct nf_exp_event item = {
244 .exp = exp,
245 .portid = portid,
246 .report = report
247 };
248 notify->fcn(1 << event, &item);
249 }
250out_unlock:
251 rcu_read_unlock();
252}
253
Pablo Neira Ayuso70e99422011-11-22 00:16:51 +0100254int nf_conntrack_register_notifier(struct net *net,
255 struct nf_ct_event_notifier *new)
Patrick McHardy010c7d62007-03-14 16:40:10 -0700256{
Tony Zelenoff031d7702012-03-08 23:35:39 +0000257 int ret;
Patrick McHardyb56f2d52010-05-10 18:47:57 +0200258 struct nf_ct_event_notifier *notify;
Pablo Neira Ayusoe34d5c12009-06-03 10:32:06 +0200259
260 mutex_lock(&nf_ct_ecache_mutex);
Pablo Neira Ayuso70e99422011-11-22 00:16:51 +0100261 notify = rcu_dereference_protected(net->ct.nf_conntrack_event_cb,
Patrick McHardyb56f2d52010-05-10 18:47:57 +0200262 lockdep_is_held(&nf_ct_ecache_mutex));
263 if (notify != NULL) {
Pablo Neira Ayusoe34d5c12009-06-03 10:32:06 +0200264 ret = -EBUSY;
265 goto out_unlock;
266 }
Eric Dumazetcf778b02012-01-12 04:41:32 +0000267 rcu_assign_pointer(net->ct.nf_conntrack_event_cb, new);
Tony Zelenoff031d7702012-03-08 23:35:39 +0000268 ret = 0;
Pablo Neira Ayusoe34d5c12009-06-03 10:32:06 +0200269
270out_unlock:
271 mutex_unlock(&nf_ct_ecache_mutex);
272 return ret;
Patrick McHardy010c7d62007-03-14 16:40:10 -0700273}
274EXPORT_SYMBOL_GPL(nf_conntrack_register_notifier);
275
Pablo Neira Ayuso70e99422011-11-22 00:16:51 +0100276void nf_conntrack_unregister_notifier(struct net *net,
277 struct nf_ct_event_notifier *new)
Patrick McHardy010c7d62007-03-14 16:40:10 -0700278{
Patrick McHardyb56f2d52010-05-10 18:47:57 +0200279 struct nf_ct_event_notifier *notify;
280
Pablo Neira Ayusoe34d5c12009-06-03 10:32:06 +0200281 mutex_lock(&nf_ct_ecache_mutex);
Pablo Neira Ayuso70e99422011-11-22 00:16:51 +0100282 notify = rcu_dereference_protected(net->ct.nf_conntrack_event_cb,
Patrick McHardyb56f2d52010-05-10 18:47:57 +0200283 lockdep_is_held(&nf_ct_ecache_mutex));
284 BUG_ON(notify != new);
Pablo Neira Ayuso70e99422011-11-22 00:16:51 +0100285 RCU_INIT_POINTER(net->ct.nf_conntrack_event_cb, NULL);
Pablo Neira Ayusoe34d5c12009-06-03 10:32:06 +0200286 mutex_unlock(&nf_ct_ecache_mutex);
Patrick McHardy010c7d62007-03-14 16:40:10 -0700287}
288EXPORT_SYMBOL_GPL(nf_conntrack_unregister_notifier);
289
Pablo Neira Ayuso70e99422011-11-22 00:16:51 +0100290int nf_ct_expect_register_notifier(struct net *net,
291 struct nf_exp_event_notifier *new)
Patrick McHardy010c7d62007-03-14 16:40:10 -0700292{
Tony Zelenoff031d7702012-03-08 23:35:39 +0000293 int ret;
Patrick McHardyb56f2d52010-05-10 18:47:57 +0200294 struct nf_exp_event_notifier *notify;
Pablo Neira Ayusoe34d5c12009-06-03 10:32:06 +0200295
296 mutex_lock(&nf_ct_ecache_mutex);
Pablo Neira Ayuso70e99422011-11-22 00:16:51 +0100297 notify = rcu_dereference_protected(net->ct.nf_expect_event_cb,
Patrick McHardyb56f2d52010-05-10 18:47:57 +0200298 lockdep_is_held(&nf_ct_ecache_mutex));
299 if (notify != NULL) {
Pablo Neira Ayusoe34d5c12009-06-03 10:32:06 +0200300 ret = -EBUSY;
301 goto out_unlock;
302 }
Eric Dumazetcf778b02012-01-12 04:41:32 +0000303 rcu_assign_pointer(net->ct.nf_expect_event_cb, new);
Tony Zelenoff031d7702012-03-08 23:35:39 +0000304 ret = 0;
Pablo Neira Ayusoe34d5c12009-06-03 10:32:06 +0200305
306out_unlock:
307 mutex_unlock(&nf_ct_ecache_mutex);
308 return ret;
Patrick McHardy010c7d62007-03-14 16:40:10 -0700309}
Patrick McHardy68236452007-07-07 22:30:49 -0700310EXPORT_SYMBOL_GPL(nf_ct_expect_register_notifier);
Patrick McHardy010c7d62007-03-14 16:40:10 -0700311
Pablo Neira Ayuso70e99422011-11-22 00:16:51 +0100312void nf_ct_expect_unregister_notifier(struct net *net,
313 struct nf_exp_event_notifier *new)
Patrick McHardy010c7d62007-03-14 16:40:10 -0700314{
Patrick McHardyb56f2d52010-05-10 18:47:57 +0200315 struct nf_exp_event_notifier *notify;
316
Pablo Neira Ayusoe34d5c12009-06-03 10:32:06 +0200317 mutex_lock(&nf_ct_ecache_mutex);
Pablo Neira Ayuso70e99422011-11-22 00:16:51 +0100318 notify = rcu_dereference_protected(net->ct.nf_expect_event_cb,
Patrick McHardyb56f2d52010-05-10 18:47:57 +0200319 lockdep_is_held(&nf_ct_ecache_mutex));
320 BUG_ON(notify != new);
Pablo Neira Ayuso70e99422011-11-22 00:16:51 +0100321 RCU_INIT_POINTER(net->ct.nf_expect_event_cb, NULL);
Pablo Neira Ayusoe34d5c12009-06-03 10:32:06 +0200322 mutex_unlock(&nf_ct_ecache_mutex);
Patrick McHardy010c7d62007-03-14 16:40:10 -0700323}
Patrick McHardy68236452007-07-07 22:30:49 -0700324EXPORT_SYMBOL_GPL(nf_ct_expect_unregister_notifier);
Pablo Neira Ayusoa0891aa2009-06-13 12:26:29 +0200325
326#define NF_CT_EVENTS_DEFAULT 1
327static int nf_ct_events __read_mostly = NF_CT_EVENTS_DEFAULT;
328
329#ifdef CONFIG_SYSCTL
330static struct ctl_table event_sysctl_table[] = {
331 {
Pablo Neira Ayusoa0891aa2009-06-13 12:26:29 +0200332 .procname = "nf_conntrack_events",
333 .data = &init_net.ct.sysctl_events,
334 .maxlen = sizeof(unsigned int),
335 .mode = 0644,
336 .proc_handler = proc_dointvec,
337 },
338 {}
339};
340#endif /* CONFIG_SYSCTL */
341
342static struct nf_ct_ext_type event_extend __read_mostly = {
343 .len = sizeof(struct nf_conntrack_ecache),
344 .align = __alignof__(struct nf_conntrack_ecache),
345 .id = NF_CT_EXT_ECACHE,
346};
347
348#ifdef CONFIG_SYSCTL
349static int nf_conntrack_event_init_sysctl(struct net *net)
350{
351 struct ctl_table *table;
352
353 table = kmemdup(event_sysctl_table, sizeof(event_sysctl_table),
354 GFP_KERNEL);
355 if (!table)
356 goto out;
357
358 table[0].data = &net->ct.sysctl_events;
359
Eric W. Biederman464dc802012-11-16 03:02:59 +0000360 /* Don't export sysctls to unprivileged users */
361 if (net->user_ns != &init_user_ns)
362 table[0].procname = NULL;
363
Pablo Neira Ayusoa0891aa2009-06-13 12:26:29 +0200364 net->ct.event_sysctl_header =
Eric W. Biedermanec8f23c2012-04-19 13:44:49 +0000365 register_net_sysctl(net, "net/netfilter", table);
Pablo Neira Ayusoa0891aa2009-06-13 12:26:29 +0200366 if (!net->ct.event_sysctl_header) {
367 printk(KERN_ERR "nf_ct_event: can't register to sysctl.\n");
368 goto out_register;
369 }
370 return 0;
371
372out_register:
373 kfree(table);
374out:
375 return -ENOMEM;
376}
377
378static void nf_conntrack_event_fini_sysctl(struct net *net)
379{
380 struct ctl_table *table;
381
382 table = net->ct.event_sysctl_header->ctl_table_arg;
383 unregister_net_sysctl_table(net->ct.event_sysctl_header);
384 kfree(table);
385}
386#else
387static int nf_conntrack_event_init_sysctl(struct net *net)
388{
389 return 0;
390}
391
392static void nf_conntrack_event_fini_sysctl(struct net *net)
393{
394}
395#endif /* CONFIG_SYSCTL */
396
Gao feng3fe0f942013-01-21 22:10:28 +0000397int nf_conntrack_ecache_pernet_init(struct net *net)
Pablo Neira Ayusoa0891aa2009-06-13 12:26:29 +0200398{
Pablo Neira Ayusoa0891aa2009-06-13 12:26:29 +0200399 net->ct.sysctl_events = nf_ct_events;
Florian Westphal95005072014-06-10 23:12:56 +0200400 INIT_DELAYED_WORK(&net->ct.ecache_dwork, ecache_work);
Gao feng3fe0f942013-01-21 22:10:28 +0000401 return nf_conntrack_event_init_sysctl(net);
402}
Pablo Neira Ayusoa0891aa2009-06-13 12:26:29 +0200403
Gao feng3fe0f942013-01-21 22:10:28 +0000404void nf_conntrack_ecache_pernet_fini(struct net *net)
405{
Florian Westphal95005072014-06-10 23:12:56 +0200406 cancel_delayed_work_sync(&net->ct.ecache_dwork);
Gao feng3fe0f942013-01-21 22:10:28 +0000407 nf_conntrack_event_fini_sysctl(net);
408}
Pablo Neira Ayusoa0891aa2009-06-13 12:26:29 +0200409
Gao feng3fe0f942013-01-21 22:10:28 +0000410int nf_conntrack_ecache_init(void)
411{
412 int ret = nf_ct_extend_register(&event_extend);
Pablo Neira Ayusoa0891aa2009-06-13 12:26:29 +0200413 if (ret < 0)
Gao feng3fe0f942013-01-21 22:10:28 +0000414 pr_err("nf_ct_event: Unable to register event extension.\n");
Pablo Neira Ayusoa0891aa2009-06-13 12:26:29 +0200415 return ret;
416}
417
Gao feng3fe0f942013-01-21 22:10:28 +0000418void nf_conntrack_ecache_fini(void)
Pablo Neira Ayusoa0891aa2009-06-13 12:26:29 +0200419{
Gao feng3fe0f942013-01-21 22:10:28 +0000420 nf_ct_extend_unregister(&event_extend);
Pablo Neira Ayusoa0891aa2009-06-13 12:26:29 +0200421}