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
67 /* This structure is passed to event handler */
74 struct nf_ct_event_notifier {
75 int (*fcn)(unsigned int events, struct nf_ct_event *item);
78 int nf_conntrack_register_notifier(struct net *net,
79 struct nf_ct_event_notifier *nb);
80 void nf_conntrack_unregister_notifier(struct net *net,
81 struct nf_ct_event_notifier *nb);
83 void nf_ct_deliver_cached_events(struct nf_conn *ct);
84 int nf_conntrack_eventmask_report(unsigned int eventmask, struct nf_conn *ct,
85 u32 portid, int report);
88 nf_conntrack_event_cache(enum ip_conntrack_events event, struct nf_conn *ct)
90 struct net *net = nf_ct_net(ct);
91 struct nf_conntrack_ecache *e;
93 if (!rcu_access_pointer(net->ct.nf_conntrack_event_cb))
96 e = nf_ct_ecache_find(ct);
100 set_bit(event, &e->cache);
104 nf_conntrack_event_report(enum ip_conntrack_events event, struct nf_conn *ct,
105 u32 portid, int report)
107 const struct net *net = nf_ct_net(ct);
109 if (!rcu_access_pointer(net->ct.nf_conntrack_event_cb))
112 return nf_conntrack_eventmask_report(1 << event, ct, portid, report);
116 nf_conntrack_event(enum ip_conntrack_events event, struct nf_conn *ct)
118 const struct net *net = nf_ct_net(ct);
120 if (!rcu_access_pointer(net->ct.nf_conntrack_event_cb))
123 return nf_conntrack_eventmask_report(1 << event, ct, 0, 0);
126 struct nf_exp_event {
127 struct nf_conntrack_expect *exp;
132 struct nf_exp_event_notifier {
133 int (*fcn)(unsigned int events, struct nf_exp_event *item);
136 int nf_ct_expect_register_notifier(struct net *net,
137 struct nf_exp_event_notifier *nb);
138 void nf_ct_expect_unregister_notifier(struct net *net,
139 struct nf_exp_event_notifier *nb);
141 void nf_ct_expect_event_report(enum ip_conntrack_expect_events event,
142 struct nf_conntrack_expect *exp,
143 u32 portid, int report);
145 int nf_conntrack_ecache_pernet_init(struct net *net);
146 void nf_conntrack_ecache_pernet_fini(struct net *net);
148 int nf_conntrack_ecache_init(void);
149 void nf_conntrack_ecache_fini(void);
151 static inline void nf_conntrack_ecache_delayed_work(struct net *net)
153 if (!delayed_work_pending(&net->ct.ecache_dwork)) {
154 schedule_delayed_work(&net->ct.ecache_dwork, HZ);
155 net->ct.ecache_dwork_pending = true;
159 static inline void nf_conntrack_ecache_work(struct net *net)
161 if (net->ct.ecache_dwork_pending) {
162 net->ct.ecache_dwork_pending = false;
163 mod_delayed_work(system_wq, &net->ct.ecache_dwork, 0);
166 #else /* CONFIG_NF_CONNTRACK_EVENTS */
167 static inline void nf_conntrack_event_cache(enum ip_conntrack_events event,
168 struct nf_conn *ct) {}
169 static inline int nf_conntrack_eventmask_report(unsigned int eventmask,
172 int report) { return 0; }
173 static inline int nf_conntrack_event(enum ip_conntrack_events event,
174 struct nf_conn *ct) { return 0; }
175 static inline int nf_conntrack_event_report(enum ip_conntrack_events event,
178 int report) { return 0; }
179 static inline void nf_ct_deliver_cached_events(const struct nf_conn *ct) {}
180 static inline void nf_ct_expect_event_report(enum ip_conntrack_expect_events e,
181 struct nf_conntrack_expect *exp,
185 static inline int nf_conntrack_ecache_pernet_init(struct net *net)
190 static inline void nf_conntrack_ecache_pernet_fini(struct net *net)
194 static inline int nf_conntrack_ecache_init(void)
199 static inline void nf_conntrack_ecache_fini(void)
203 static inline void nf_conntrack_ecache_delayed_work(struct net *net)
207 static inline void nf_conntrack_ecache_work(struct net *net)
210 #endif /* CONFIG_NF_CONNTRACK_EVENTS */
212 #endif /*_NF_CONNTRACK_ECACHE_H*/