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_DESTROY_FAIL, /* tried but failed to send destroy event */
18 NFCT_ECACHE_DESTROY_SENT, /* sent destroy event after failure */
21 struct nf_conntrack_ecache {
22 unsigned long cache; /* bitops want long */
23 u16 ctmask; /* bitmask of ct events to be delivered */
24 u16 expmask; /* bitmask of expect events to be delivered */
25 u32 missed; /* missed events */
26 u32 portid; /* netlink portid of destroyer */
29 static inline struct nf_conntrack_ecache *
30 nf_ct_ecache_find(const struct nf_conn *ct)
32 #ifdef CONFIG_NF_CONNTRACK_EVENTS
33 return nf_ct_ext_find(ct, NF_CT_EXT_ECACHE);
39 static inline bool nf_ct_ecache_exist(const struct nf_conn *ct)
41 #ifdef CONFIG_NF_CONNTRACK_EVENTS
42 return nf_ct_ext_exist(ct, NF_CT_EXT_ECACHE);
48 #ifdef CONFIG_NF_CONNTRACK_EVENTS
50 /* This structure is passed to event handler */
58 struct nf_conntrack_expect *exp;
63 struct nf_ct_event_notifier {
64 int (*ct_event)(unsigned int events, const struct nf_ct_event *item);
65 int (*exp_event)(unsigned int events, const struct nf_exp_event *item);
68 void nf_conntrack_register_notifier(struct net *net,
69 const struct nf_ct_event_notifier *nb);
70 void nf_conntrack_unregister_notifier(struct net *net);
72 void nf_ct_deliver_cached_events(struct nf_conn *ct);
73 int nf_conntrack_eventmask_report(unsigned int eventmask, struct nf_conn *ct,
74 u32 portid, int report);
76 bool nf_ct_ecache_ext_add(struct nf_conn *ct, u16 ctmask, u16 expmask, gfp_t gfp);
79 static inline void nf_ct_deliver_cached_events(const struct nf_conn *ct)
83 static inline int nf_conntrack_eventmask_report(unsigned int eventmask,
91 static inline bool nf_ct_ecache_ext_add(struct nf_conn *ct, u16 ctmask, u16 expmask, gfp_t gfp)
98 nf_conntrack_event_cache(enum ip_conntrack_events event, struct nf_conn *ct)
100 #ifdef CONFIG_NF_CONNTRACK_EVENTS
101 struct net *net = nf_ct_net(ct);
102 struct nf_conntrack_ecache *e;
104 if (!rcu_access_pointer(net->ct.nf_conntrack_event_cb))
107 e = nf_ct_ecache_find(ct);
111 set_bit(event, &e->cache);
116 nf_conntrack_event_report(enum ip_conntrack_events event, struct nf_conn *ct,
117 u32 portid, int report)
119 #ifdef CONFIG_NF_CONNTRACK_EVENTS
120 if (nf_ct_ecache_exist(ct))
121 return nf_conntrack_eventmask_report(1 << event, ct, portid, report);
127 nf_conntrack_event(enum ip_conntrack_events event, struct nf_conn *ct)
129 #ifdef CONFIG_NF_CONNTRACK_EVENTS
130 if (nf_ct_ecache_exist(ct))
131 return nf_conntrack_eventmask_report(1 << event, ct, 0, 0);
136 #ifdef CONFIG_NF_CONNTRACK_EVENTS
137 void nf_ct_expect_event_report(enum ip_conntrack_expect_events event,
138 struct nf_conntrack_expect *exp,
139 u32 portid, int report);
141 void nf_conntrack_ecache_work(struct net *net, enum nf_ct_ecache_state state);
143 void nf_conntrack_ecache_pernet_init(struct net *net);
144 void nf_conntrack_ecache_pernet_fini(struct net *net);
146 struct nf_conntrack_net_ecache *nf_conn_pernet_ecache(const struct net *net);
148 static inline bool nf_conntrack_ecache_dwork_pending(const struct net *net)
150 return net->ct.ecache_dwork_pending;
152 #else /* CONFIG_NF_CONNTRACK_EVENTS */
154 static inline void nf_ct_expect_event_report(enum ip_conntrack_expect_events e,
155 struct nf_conntrack_expect *exp,
161 static inline void nf_conntrack_ecache_work(struct net *net,
162 enum nf_ct_ecache_state s)
166 static inline void nf_conntrack_ecache_pernet_init(struct net *net)
170 static inline void nf_conntrack_ecache_pernet_fini(struct net *net)
173 static inline bool nf_conntrack_ecache_dwork_pending(const struct net *net) { return false; }
174 #endif /* CONFIG_NF_CONNTRACK_EVENTS */
175 #endif /*_NF_CONNTRACK_ECACHE_H*/