1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Event cache for netfilter. */
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>
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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/kernel.h>
20 #include <linux/netdevice.h>
21 #include <linux/slab.h>
22 #include <linux/export.h>
24 #include <net/netfilter/nf_conntrack.h>
25 #include <net/netfilter/nf_conntrack_core.h>
26 #include <net/netfilter/nf_conntrack_ecache.h>
27 #include <net/netfilter/nf_conntrack_extend.h>
29 static DEFINE_MUTEX(nf_ct_ecache_mutex
);
31 #define DYING_NULLS_VAL ((1 << 30) + 1)
32 #define ECACHE_MAX_JIFFIES msecs_to_jiffies(10)
33 #define ECACHE_RETRY_JIFFIES msecs_to_jiffies(10)
41 struct nf_conntrack_net_ecache
*nf_conn_pernet_ecache(const struct net
*net
)
43 struct nf_conntrack_net
*cnet
= nf_ct_pernet(net
);
47 #if IS_MODULE(CONFIG_NF_CT_NETLINK)
48 EXPORT_SYMBOL_GPL(nf_conn_pernet_ecache
);
51 static enum retry_state
ecache_work_evict_list(struct nf_conntrack_net
*cnet
)
53 unsigned long stop
= jiffies
+ ECACHE_MAX_JIFFIES
;
54 struct hlist_nulls_head evicted_list
;
55 enum retry_state ret
= STATE_DONE
;
56 struct nf_conntrack_tuple_hash
*h
;
57 struct hlist_nulls_node
*n
;
60 INIT_HLIST_NULLS_HEAD(&evicted_list
, DYING_NULLS_VAL
);
64 spin_lock_bh(&cnet
->ecache
.dying_lock
);
66 hlist_nulls_for_each_entry_safe(h
, n
, &cnet
->ecache
.dying_list
, hnnode
) {
67 struct nf_conn
*ct
= nf_ct_tuplehash_to_ctrack(h
);
69 /* The worker owns all entries, ct remains valid until nf_ct_put
72 if (nf_conntrack_event(IPCT_DESTROY
, ct
)) {
73 ret
= STATE_CONGESTED
;
77 hlist_nulls_del_rcu(&ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].hnnode
);
78 hlist_nulls_add_head(&ct
->tuplehash
[IP_CT_DIR_REPLY
].hnnode
, &evicted_list
);
80 if (time_after(stop
, jiffies
)) {
86 spin_unlock_bh(&cnet
->ecache
.dying_lock
);
92 spin_unlock_bh(&cnet
->ecache
.dying_lock
);
94 hlist_nulls_for_each_entry_safe(h
, n
, &evicted_list
, hnnode
) {
95 struct nf_conn
*ct
= nf_ct_tuplehash_to_ctrack(h
);
97 hlist_nulls_del_rcu(&ct
->tuplehash
[IP_CT_DIR_REPLY
].hnnode
);
106 static void ecache_work(struct work_struct
*work
)
108 struct nf_conntrack_net
*cnet
= container_of(work
, struct nf_conntrack_net
, ecache
.dwork
.work
);
111 ret
= ecache_work_evict_list(cnet
);
113 case STATE_CONGESTED
:
114 delay
= ECACHE_RETRY_JIFFIES
;
124 schedule_delayed_work(&cnet
->ecache
.dwork
, delay
);
127 static int __nf_conntrack_eventmask_report(struct nf_conntrack_ecache
*e
,
130 const struct nf_ct_event
*item
)
132 struct net
*net
= nf_ct_net(item
->ct
);
133 struct nf_ct_event_notifier
*notify
;
137 if (!((events
| missed
) & e
->ctmask
))
142 notify
= rcu_dereference(net
->ct
.nf_conntrack_event_cb
);
148 ret
= notify
->ct_event(events
| missed
, item
);
151 if (likely(ret
>= 0 && missed
== 0))
155 old
= READ_ONCE(e
->missed
);
159 want
= old
& ~missed
;
160 } while (cmpxchg(&e
->missed
, old
, want
) != old
);
165 static void nf_ct_ecache_tstamp_refresh(struct nf_conntrack_ecache
*e
)
167 #ifdef CONFIG_NF_CONNTRACK_TIMESTAMP
168 if (local64_read(&e
->timestamp
))
169 local64_set(&e
->timestamp
, ktime_get_real_ns());
173 int nf_conntrack_eventmask_report(unsigned int events
, struct nf_conn
*ct
,
174 u32 portid
, int report
)
176 struct nf_conntrack_ecache
*e
;
177 struct nf_ct_event item
;
181 if (!nf_ct_is_confirmed(ct
))
184 e
= nf_ct_ecache_find(ct
);
188 memset(&item
, 0, sizeof(item
));
191 item
.portid
= e
->portid
? e
->portid
: portid
;
192 item
.report
= report
;
194 /* This is a resent of a destroy event? If so, skip missed */
195 missed
= e
->portid
? 0 : e
->missed
;
197 nf_ct_ecache_tstamp_refresh(e
);
199 ret
= __nf_conntrack_eventmask_report(e
, events
, missed
, &item
);
200 if (unlikely(ret
< 0 && (events
& (1 << IPCT_DESTROY
)))) {
201 /* This is a destroy event that has been triggered by a process,
202 * we store the PORTID to include it in the retransmission.
204 if (e
->portid
== 0 && portid
!= 0)
210 EXPORT_SYMBOL_GPL(nf_conntrack_eventmask_report
);
212 /* deliver cached events and clear cache entry - must be called with locally
213 * disabled softirqs */
214 void nf_ct_deliver_cached_events(struct nf_conn
*ct
)
216 struct nf_conntrack_ecache
*e
;
217 struct nf_ct_event item
;
220 if (!nf_ct_is_confirmed(ct
) || nf_ct_is_dying(ct
))
223 e
= nf_ct_ecache_find(ct
);
227 events
= xchg(&e
->cache
, 0);
233 /* We make a copy of the missed event cache without taking
234 * the lock, thus we may send missed events twice. However,
235 * this does not harm and it happens very rarely.
237 __nf_conntrack_eventmask_report(e
, events
, e
->missed
, &item
);
239 EXPORT_SYMBOL_GPL(nf_ct_deliver_cached_events
);
241 void nf_ct_expect_event_report(enum ip_conntrack_expect_events event
,
242 struct nf_conntrack_expect
*exp
,
243 u32 portid
, int report
)
246 struct net
*net
= nf_ct_exp_net(exp
);
247 struct nf_ct_event_notifier
*notify
;
248 struct nf_conntrack_ecache
*e
;
251 notify
= rcu_dereference(net
->ct
.nf_conntrack_event_cb
);
255 e
= nf_ct_ecache_find(exp
->master
);
259 if (e
->expmask
& (1 << event
)) {
260 struct nf_exp_event item
= {
265 notify
->exp_event(1 << event
, &item
);
271 void nf_conntrack_register_notifier(struct net
*net
,
272 const struct nf_ct_event_notifier
*new)
274 struct nf_ct_event_notifier
*notify
;
276 mutex_lock(&nf_ct_ecache_mutex
);
277 notify
= rcu_dereference_protected(net
->ct
.nf_conntrack_event_cb
,
278 lockdep_is_held(&nf_ct_ecache_mutex
));
279 WARN_ON_ONCE(notify
);
280 rcu_assign_pointer(net
->ct
.nf_conntrack_event_cb
, new);
281 mutex_unlock(&nf_ct_ecache_mutex
);
283 EXPORT_SYMBOL_GPL(nf_conntrack_register_notifier
);
285 void nf_conntrack_unregister_notifier(struct net
*net
)
287 mutex_lock(&nf_ct_ecache_mutex
);
288 RCU_INIT_POINTER(net
->ct
.nf_conntrack_event_cb
, NULL
);
289 mutex_unlock(&nf_ct_ecache_mutex
);
290 /* synchronize_rcu() is called after netns pre_exit */
292 EXPORT_SYMBOL_GPL(nf_conntrack_unregister_notifier
);
294 void nf_conntrack_ecache_work(struct net
*net
, enum nf_ct_ecache_state state
)
296 struct nf_conntrack_net
*cnet
= nf_ct_pernet(net
);
298 if (state
== NFCT_ECACHE_DESTROY_FAIL
&&
299 !delayed_work_pending(&cnet
->ecache
.dwork
)) {
300 schedule_delayed_work(&cnet
->ecache
.dwork
, HZ
);
301 net
->ct
.ecache_dwork_pending
= true;
302 } else if (state
== NFCT_ECACHE_DESTROY_SENT
) {
303 if (!hlist_nulls_empty(&cnet
->ecache
.dying_list
))
304 mod_delayed_work(system_wq
, &cnet
->ecache
.dwork
, 0);
306 net
->ct
.ecache_dwork_pending
= false;
310 static void nf_ct_ecache_tstamp_new(const struct nf_conn
*ct
, struct nf_conntrack_ecache
*e
)
312 #ifdef CONFIG_NF_CONNTRACK_TIMESTAMP
315 if (nf_ct_ext_exist(ct
, NF_CT_EXT_TSTAMP
))
316 ts
= ktime_get_real_ns();
318 local64_set(&e
->timestamp
, ts
);
322 bool nf_ct_ecache_ext_add(struct nf_conn
*ct
, u16 ctmask
, u16 expmask
, gfp_t gfp
)
324 struct net
*net
= nf_ct_net(ct
);
325 struct nf_conntrack_ecache
*e
;
327 switch (net
->ct
.sysctl_events
) {
329 /* assignment via template / ruleset? ignore sysctl. */
330 if (ctmask
|| expmask
)
333 case 2: /* autodetect: no event listener, don't allocate extension. */
334 if (!READ_ONCE(nf_ctnetlink_has_listener
))
338 /* always allocate an extension. */
339 if (!ctmask
&& !expmask
) {
349 e
= nf_ct_ext_add(ct
, NF_CT_EXT_ECACHE
, gfp
);
351 nf_ct_ecache_tstamp_new(ct
, e
);
353 e
->expmask
= expmask
;
358 EXPORT_SYMBOL_GPL(nf_ct_ecache_ext_add
);
360 #define NF_CT_EVENTS_DEFAULT 2
361 static int nf_ct_events __read_mostly
= NF_CT_EVENTS_DEFAULT
;
363 void nf_conntrack_ecache_pernet_init(struct net
*net
)
365 struct nf_conntrack_net
*cnet
= nf_ct_pernet(net
);
367 net
->ct
.sysctl_events
= nf_ct_events
;
369 INIT_DELAYED_WORK(&cnet
->ecache
.dwork
, ecache_work
);
370 INIT_HLIST_NULLS_HEAD(&cnet
->ecache
.dying_list
, DYING_NULLS_VAL
);
371 spin_lock_init(&cnet
->ecache
.dying_lock
);
373 BUILD_BUG_ON(__IPCT_MAX
>= 16); /* e->ctmask is u16 */
376 void nf_conntrack_ecache_pernet_fini(struct net
*net
)
378 struct nf_conntrack_net
*cnet
= nf_ct_pernet(net
);
380 cancel_delayed_work_sync(&cnet
->ecache
.dwork
);