perf probe: Add --bootconfig to output definition in bootconfig format
[linux-2.6-microblaze.git] / net / netfilter / nf_conntrack_ecache.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Event cache for netfilter. */
3
4 /*
5  * (C) 2005 Harald Welte <laforge@gnumonks.org>
6  * (C) 2005 Patrick McHardy <kaber@trash.net>
7  * (C) 2005-2006 Netfilter Core Team <coreteam@netfilter.org>
8  * (C) 2005 USAGI/WIDE Project <http://www.linux-ipv6.org>
9  */
10
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12
13 #include <linux/types.h>
14 #include <linux/netfilter.h>
15 #include <linux/skbuff.h>
16 #include <linux/vmalloc.h>
17 #include <linux/stddef.h>
18 #include <linux/err.h>
19 #include <linux/percpu.h>
20 #include <linux/kernel.h>
21 #include <linux/netdevice.h>
22 #include <linux/slab.h>
23 #include <linux/export.h>
24
25 #include <net/netfilter/nf_conntrack.h>
26 #include <net/netfilter/nf_conntrack_core.h>
27 #include <net/netfilter/nf_conntrack_ecache.h>
28 #include <net/netfilter/nf_conntrack_extend.h>
29
30 extern unsigned int nf_conntrack_net_id;
31
32 static DEFINE_MUTEX(nf_ct_ecache_mutex);
33
34 #define ECACHE_RETRY_WAIT (HZ/10)
35 #define ECACHE_STACK_ALLOC (256 / sizeof(void *))
36
37 enum retry_state {
38         STATE_CONGESTED,
39         STATE_RESTART,
40         STATE_DONE,
41 };
42
43 static enum retry_state ecache_work_evict_list(struct ct_pcpu *pcpu)
44 {
45         struct nf_conn *refs[ECACHE_STACK_ALLOC];
46         enum retry_state ret = STATE_DONE;
47         struct nf_conntrack_tuple_hash *h;
48         struct hlist_nulls_node *n;
49         unsigned int evicted = 0;
50
51         spin_lock(&pcpu->lock);
52
53         hlist_nulls_for_each_entry(h, n, &pcpu->dying, hnnode) {
54                 struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h);
55                 struct nf_conntrack_ecache *e;
56
57                 if (!nf_ct_is_confirmed(ct))
58                         continue;
59
60                 /* This ecache access is safe because the ct is on the
61                  * pcpu dying list and we hold the spinlock -- the entry
62                  * cannot be free'd until after the lock is released.
63                  *
64                  * This is true even if ct has a refcount of 0: the
65                  * cpu that is about to free the entry must remove it
66                  * from the dying list and needs the lock to do so.
67                  */
68                 e = nf_ct_ecache_find(ct);
69                 if (!e || e->state != NFCT_ECACHE_DESTROY_FAIL)
70                         continue;
71
72                 /* ct is in NFCT_ECACHE_DESTROY_FAIL state, this means
73                  * the worker owns this entry: the ct will remain valid
74                  * until the worker puts its ct reference.
75                  */
76                 if (nf_conntrack_event(IPCT_DESTROY, ct)) {
77                         ret = STATE_CONGESTED;
78                         break;
79                 }
80
81                 e->state = NFCT_ECACHE_DESTROY_SENT;
82                 refs[evicted] = ct;
83
84                 if (++evicted >= ARRAY_SIZE(refs)) {
85                         ret = STATE_RESTART;
86                         break;
87                 }
88         }
89
90         spin_unlock(&pcpu->lock);
91
92         /* can't _put while holding lock */
93         while (evicted)
94                 nf_ct_put(refs[--evicted]);
95
96         return ret;
97 }
98
99 static void ecache_work(struct work_struct *work)
100 {
101         struct nf_conntrack_net *cnet = container_of(work, struct nf_conntrack_net, ecache_dwork.work);
102         struct netns_ct *ctnet = cnet->ct_net;
103         int cpu, delay = -1;
104         struct ct_pcpu *pcpu;
105
106         local_bh_disable();
107
108         for_each_possible_cpu(cpu) {
109                 enum retry_state ret;
110
111                 pcpu = per_cpu_ptr(ctnet->pcpu_lists, cpu);
112
113                 ret = ecache_work_evict_list(pcpu);
114
115                 switch (ret) {
116                 case STATE_CONGESTED:
117                         delay = ECACHE_RETRY_WAIT;
118                         goto out;
119                 case STATE_RESTART:
120                         delay = 0;
121                         break;
122                 case STATE_DONE:
123                         break;
124                 }
125         }
126
127  out:
128         local_bh_enable();
129
130         ctnet->ecache_dwork_pending = delay > 0;
131         if (delay >= 0)
132                 schedule_delayed_work(&cnet->ecache_dwork, delay);
133 }
134
135 int nf_conntrack_eventmask_report(unsigned int eventmask, struct nf_conn *ct,
136                                   u32 portid, int report)
137 {
138         int ret = 0;
139         struct net *net = nf_ct_net(ct);
140         struct nf_ct_event_notifier *notify;
141         struct nf_conntrack_ecache *e;
142
143         rcu_read_lock();
144         notify = rcu_dereference(net->ct.nf_conntrack_event_cb);
145         if (!notify)
146                 goto out_unlock;
147
148         e = nf_ct_ecache_find(ct);
149         if (!e)
150                 goto out_unlock;
151
152         if (nf_ct_is_confirmed(ct)) {
153                 struct nf_ct_event item = {
154                         .ct     = ct,
155                         .portid = e->portid ? e->portid : portid,
156                         .report = report
157                 };
158                 /* This is a resent of a destroy event? If so, skip missed */
159                 unsigned long missed = e->portid ? 0 : e->missed;
160
161                 if (!((eventmask | missed) & e->ctmask))
162                         goto out_unlock;
163
164                 ret = notify->fcn(eventmask | missed, &item);
165                 if (unlikely(ret < 0 || missed)) {
166                         spin_lock_bh(&ct->lock);
167                         if (ret < 0) {
168                                 /* This is a destroy event that has been
169                                  * triggered by a process, we store the PORTID
170                                  * to include it in the retransmission.
171                                  */
172                                 if (eventmask & (1 << IPCT_DESTROY)) {
173                                         if (e->portid == 0 && portid != 0)
174                                                 e->portid = portid;
175                                         e->state = NFCT_ECACHE_DESTROY_FAIL;
176                                 } else {
177                                         e->missed |= eventmask;
178                                 }
179                         } else {
180                                 e->missed &= ~missed;
181                         }
182                         spin_unlock_bh(&ct->lock);
183                 }
184         }
185 out_unlock:
186         rcu_read_unlock();
187         return ret;
188 }
189 EXPORT_SYMBOL_GPL(nf_conntrack_eventmask_report);
190
191 /* deliver cached events and clear cache entry - must be called with locally
192  * disabled softirqs */
193 void nf_ct_deliver_cached_events(struct nf_conn *ct)
194 {
195         struct net *net = nf_ct_net(ct);
196         unsigned long events, missed;
197         struct nf_ct_event_notifier *notify;
198         struct nf_conntrack_ecache *e;
199         struct nf_ct_event item;
200         int ret;
201
202         rcu_read_lock();
203         notify = rcu_dereference(net->ct.nf_conntrack_event_cb);
204         if (notify == NULL)
205                 goto out_unlock;
206
207         if (!nf_ct_is_confirmed(ct) || nf_ct_is_dying(ct))
208                 goto out_unlock;
209
210         e = nf_ct_ecache_find(ct);
211         if (e == NULL)
212                 goto out_unlock;
213
214         events = xchg(&e->cache, 0);
215
216         /* We make a copy of the missed event cache without taking
217          * the lock, thus we may send missed events twice. However,
218          * this does not harm and it happens very rarely. */
219         missed = e->missed;
220
221         if (!((events | missed) & e->ctmask))
222                 goto out_unlock;
223
224         item.ct = ct;
225         item.portid = 0;
226         item.report = 0;
227
228         ret = notify->fcn(events | missed, &item);
229
230         if (likely(ret == 0 && !missed))
231                 goto out_unlock;
232
233         spin_lock_bh(&ct->lock);
234         if (ret < 0)
235                 e->missed |= events;
236         else
237                 e->missed &= ~missed;
238         spin_unlock_bh(&ct->lock);
239
240 out_unlock:
241         rcu_read_unlock();
242 }
243 EXPORT_SYMBOL_GPL(nf_ct_deliver_cached_events);
244
245 void nf_ct_expect_event_report(enum ip_conntrack_expect_events event,
246                                struct nf_conntrack_expect *exp,
247                                u32 portid, int report)
248
249 {
250         struct net *net = nf_ct_exp_net(exp);
251         struct nf_exp_event_notifier *notify;
252         struct nf_conntrack_ecache *e;
253
254         rcu_read_lock();
255         notify = rcu_dereference(net->ct.nf_expect_event_cb);
256         if (!notify)
257                 goto out_unlock;
258
259         e = nf_ct_ecache_find(exp->master);
260         if (!e)
261                 goto out_unlock;
262
263         if (e->expmask & (1 << event)) {
264                 struct nf_exp_event item = {
265                         .exp    = exp,
266                         .portid = portid,
267                         .report = report
268                 };
269                 notify->fcn(1 << event, &item);
270         }
271 out_unlock:
272         rcu_read_unlock();
273 }
274
275 int nf_conntrack_register_notifier(struct net *net,
276                                    struct nf_ct_event_notifier *new)
277 {
278         int ret;
279         struct nf_ct_event_notifier *notify;
280
281         mutex_lock(&nf_ct_ecache_mutex);
282         notify = rcu_dereference_protected(net->ct.nf_conntrack_event_cb,
283                                            lockdep_is_held(&nf_ct_ecache_mutex));
284         if (notify != NULL) {
285                 ret = -EBUSY;
286                 goto out_unlock;
287         }
288         rcu_assign_pointer(net->ct.nf_conntrack_event_cb, new);
289         ret = 0;
290
291 out_unlock:
292         mutex_unlock(&nf_ct_ecache_mutex);
293         return ret;
294 }
295 EXPORT_SYMBOL_GPL(nf_conntrack_register_notifier);
296
297 void nf_conntrack_unregister_notifier(struct net *net,
298                                       struct nf_ct_event_notifier *new)
299 {
300         struct nf_ct_event_notifier *notify;
301
302         mutex_lock(&nf_ct_ecache_mutex);
303         notify = rcu_dereference_protected(net->ct.nf_conntrack_event_cb,
304                                            lockdep_is_held(&nf_ct_ecache_mutex));
305         BUG_ON(notify != new);
306         RCU_INIT_POINTER(net->ct.nf_conntrack_event_cb, NULL);
307         mutex_unlock(&nf_ct_ecache_mutex);
308         /* synchronize_rcu() is called from ctnetlink_exit. */
309 }
310 EXPORT_SYMBOL_GPL(nf_conntrack_unregister_notifier);
311
312 int nf_ct_expect_register_notifier(struct net *net,
313                                    struct nf_exp_event_notifier *new)
314 {
315         int ret;
316         struct nf_exp_event_notifier *notify;
317
318         mutex_lock(&nf_ct_ecache_mutex);
319         notify = rcu_dereference_protected(net->ct.nf_expect_event_cb,
320                                            lockdep_is_held(&nf_ct_ecache_mutex));
321         if (notify != NULL) {
322                 ret = -EBUSY;
323                 goto out_unlock;
324         }
325         rcu_assign_pointer(net->ct.nf_expect_event_cb, new);
326         ret = 0;
327
328 out_unlock:
329         mutex_unlock(&nf_ct_ecache_mutex);
330         return ret;
331 }
332 EXPORT_SYMBOL_GPL(nf_ct_expect_register_notifier);
333
334 void nf_ct_expect_unregister_notifier(struct net *net,
335                                       struct nf_exp_event_notifier *new)
336 {
337         struct nf_exp_event_notifier *notify;
338
339         mutex_lock(&nf_ct_ecache_mutex);
340         notify = rcu_dereference_protected(net->ct.nf_expect_event_cb,
341                                            lockdep_is_held(&nf_ct_ecache_mutex));
342         BUG_ON(notify != new);
343         RCU_INIT_POINTER(net->ct.nf_expect_event_cb, NULL);
344         mutex_unlock(&nf_ct_ecache_mutex);
345         /* synchronize_rcu() is called from ctnetlink_exit. */
346 }
347 EXPORT_SYMBOL_GPL(nf_ct_expect_unregister_notifier);
348
349 void nf_conntrack_ecache_work(struct net *net, enum nf_ct_ecache_state state)
350 {
351         struct nf_conntrack_net *cnet = net_generic(net, nf_conntrack_net_id);
352
353         if (state == NFCT_ECACHE_DESTROY_FAIL &&
354             !delayed_work_pending(&cnet->ecache_dwork)) {
355                 schedule_delayed_work(&cnet->ecache_dwork, HZ);
356                 net->ct.ecache_dwork_pending = true;
357         } else if (state == NFCT_ECACHE_DESTROY_SENT) {
358                 net->ct.ecache_dwork_pending = false;
359                 mod_delayed_work(system_wq, &cnet->ecache_dwork, 0);
360         }
361 }
362
363 #define NF_CT_EVENTS_DEFAULT 1
364 static int nf_ct_events __read_mostly = NF_CT_EVENTS_DEFAULT;
365
366 static const struct nf_ct_ext_type event_extend = {
367         .len    = sizeof(struct nf_conntrack_ecache),
368         .align  = __alignof__(struct nf_conntrack_ecache),
369         .id     = NF_CT_EXT_ECACHE,
370 };
371
372 void nf_conntrack_ecache_pernet_init(struct net *net)
373 {
374         struct nf_conntrack_net *cnet = net_generic(net, nf_conntrack_net_id);
375
376         net->ct.sysctl_events = nf_ct_events;
377         cnet->ct_net = &net->ct;
378         INIT_DELAYED_WORK(&cnet->ecache_dwork, ecache_work);
379 }
380
381 void nf_conntrack_ecache_pernet_fini(struct net *net)
382 {
383         struct nf_conntrack_net *cnet = net_generic(net, nf_conntrack_net_id);
384
385         cancel_delayed_work_sync(&cnet->ecache_dwork);
386 }
387
388 int nf_conntrack_ecache_init(void)
389 {
390         int ret = nf_ct_extend_register(&event_extend);
391         if (ret < 0)
392                 pr_err("Unable to register event extension\n");
393
394         BUILD_BUG_ON(__IPCT_MAX >= 16); /* ctmask, missed use u16 */
395
396         return ret;
397 }
398
399 void nf_conntrack_ecache_fini(void)
400 {
401         nf_ct_extend_unregister(&event_extend);
402 }