1 /* SPDX-License-Identifier: GPL-2.0 */
3 * connection tracking event cache.
6 #ifndef _NF_CONNTRACK_ECACHE_H
7 #define _NF_CONNTRACK_ECACHE_H
8 #include <net/netfilter/nf_conntrack.h>
10 #include <net/net_namespace.h>
11 #include <net/netfilter/nf_conntrack_expect.h>
12 #include <linux/netfilter/nf_conntrack_common.h>
13 #include <linux/netfilter/nf_conntrack_tuple_common.h>
14 #include <net/netfilter/nf_conntrack_extend.h>
16 enum nf_ct_ecache_state {
17 NFCT_ECACHE_UNKNOWN, /* destroy event not sent */
18 NFCT_ECACHE_DESTROY_FAIL, /* tried but failed to send destroy event */
19 NFCT_ECACHE_DESTROY_SENT, /* sent destroy event after failure */
22 struct nf_conntrack_ecache {
23 unsigned long cache; /* bitops want long */
24 u16 missed; /* missed events */
25 u16 ctmask; /* bitmask of ct events to be delivered */
26 u16 expmask; /* bitmask of expect events to be delivered */
27 enum nf_ct_ecache_state state:8;/* ecache state */
28 u32 portid; /* netlink portid of destroyer */
31 static inline struct nf_conntrack_ecache *
32 nf_ct_ecache_find(const struct nf_conn *ct)
34 #ifdef CONFIG_NF_CONNTRACK_EVENTS
35 return nf_ct_ext_find(ct, NF_CT_EXT_ECACHE);
41 static inline struct nf_conntrack_ecache *
42 nf_ct_ecache_ext_add(struct nf_conn *ct, u16 ctmask, u16 expmask, gfp_t gfp)
44 #ifdef CONFIG_NF_CONNTRACK_EVENTS
45 struct net *net = nf_ct_net(ct);
46 struct nf_conntrack_ecache *e;
48 if (!ctmask && !expmask && net->ct.sysctl_events) {
52 if (!ctmask && !expmask)
55 e = nf_ct_ext_add(ct, NF_CT_EXT_ECACHE, gfp);
66 #ifdef CONFIG_NF_CONNTRACK_EVENTS
68 /* This structure is passed to event handler */
75 struct nf_ct_event_notifier {
76 int (*fcn)(unsigned int events, struct nf_ct_event *item);
79 int nf_conntrack_register_notifier(struct net *net,
80 struct nf_ct_event_notifier *nb);
81 void nf_conntrack_unregister_notifier(struct net *net,
82 struct nf_ct_event_notifier *nb);
84 void nf_ct_deliver_cached_events(struct nf_conn *ct);
85 int nf_conntrack_eventmask_report(unsigned int eventmask, struct nf_conn *ct,
86 u32 portid, int report);
90 static inline void nf_ct_deliver_cached_events(const struct nf_conn *ct)
94 static inline int nf_conntrack_eventmask_report(unsigned int eventmask,
105 nf_conntrack_event_cache(enum ip_conntrack_events event, struct nf_conn *ct)
107 #ifdef CONFIG_NF_CONNTRACK_EVENTS
108 struct net *net = nf_ct_net(ct);
109 struct nf_conntrack_ecache *e;
111 if (!rcu_access_pointer(net->ct.nf_conntrack_event_cb))
114 e = nf_ct_ecache_find(ct);
118 set_bit(event, &e->cache);
123 nf_conntrack_event_report(enum ip_conntrack_events event, struct nf_conn *ct,
124 u32 portid, int report)
126 #ifdef CONFIG_NF_CONNTRACK_EVENTS
127 const struct net *net = nf_ct_net(ct);
129 if (!rcu_access_pointer(net->ct.nf_conntrack_event_cb))
132 return nf_conntrack_eventmask_report(1 << event, ct, portid, report);
139 nf_conntrack_event(enum ip_conntrack_events event, struct nf_conn *ct)
141 #ifdef CONFIG_NF_CONNTRACK_EVENTS
142 const struct net *net = nf_ct_net(ct);
144 if (!rcu_access_pointer(net->ct.nf_conntrack_event_cb))
147 return nf_conntrack_eventmask_report(1 << event, ct, 0, 0);
153 #ifdef CONFIG_NF_CONNTRACK_EVENTS
155 struct nf_exp_event {
156 struct nf_conntrack_expect *exp;
161 struct nf_exp_event_notifier {
162 int (*fcn)(unsigned int events, struct nf_exp_event *item);
165 int nf_ct_expect_register_notifier(struct net *net,
166 struct nf_exp_event_notifier *nb);
167 void nf_ct_expect_unregister_notifier(struct net *net,
168 struct nf_exp_event_notifier *nb);
170 void nf_ct_expect_event_report(enum ip_conntrack_expect_events event,
171 struct nf_conntrack_expect *exp,
172 u32 portid, int report);
174 void nf_conntrack_ecache_pernet_init(struct net *net);
175 void nf_conntrack_ecache_pernet_fini(struct net *net);
177 int nf_conntrack_ecache_init(void);
178 void nf_conntrack_ecache_fini(void);
180 #else /* CONFIG_NF_CONNTRACK_EVENTS */
182 static inline void nf_ct_expect_event_report(enum ip_conntrack_expect_events e,
183 struct nf_conntrack_expect *exp,
189 static inline void nf_conntrack_ecache_pernet_init(struct net *net)
193 static inline void nf_conntrack_ecache_pernet_fini(struct net *net)
197 static inline int nf_conntrack_ecache_init(void)
202 static inline void nf_conntrack_ecache_fini(void)
206 #endif /* CONFIG_NF_CONNTRACK_EVENTS */
208 static inline void nf_conntrack_ecache_delayed_work(struct net *net)
210 #ifdef CONFIG_NF_CONNTRACK_EVENTS
211 if (!delayed_work_pending(&net->ct.ecache_dwork)) {
212 schedule_delayed_work(&net->ct.ecache_dwork, HZ);
213 net->ct.ecache_dwork_pending = true;
218 static inline void nf_conntrack_ecache_work(struct net *net)
220 #ifdef CONFIG_NF_CONNTRACK_EVENTS
221 if (net->ct.ecache_dwork_pending) {
222 net->ct.ecache_dwork_pending = false;
223 mod_delayed_work(system_wq, &net->ct.ecache_dwork, 0);
228 #endif /*_NF_CONNTRACK_ECACHE_H*/