2 * connection tracking event cache.
5 #ifndef _NF_CONNTRACK_ECACHE_H
6 #define _NF_CONNTRACK_ECACHE_H
7 #include <net/netfilter/nf_conntrack.h>
9 #include <net/net_namespace.h>
10 #include <net/netfilter/nf_conntrack_expect.h>
11 #include <linux/netfilter/nf_conntrack_common.h>
12 #include <linux/netfilter/nf_conntrack_tuple_common.h>
13 #include <net/netfilter/nf_conntrack_extend.h>
15 struct nf_conntrack_ecache {
16 unsigned long cache; /* bitops want long */
17 unsigned long missed; /* missed events */
18 u16 ctmask; /* bitmask of ct events to be delivered */
19 u16 expmask; /* bitmask of expect events to be delivered */
20 u32 portid; /* netlink portid of destroyer */
23 static inline struct nf_conntrack_ecache *
24 nf_ct_ecache_find(const struct nf_conn *ct)
26 #ifdef CONFIG_NF_CONNTRACK_EVENTS
27 return nf_ct_ext_find(ct, NF_CT_EXT_ECACHE);
33 static inline struct nf_conntrack_ecache *
34 nf_ct_ecache_ext_add(struct nf_conn *ct, u16 ctmask, u16 expmask, gfp_t gfp)
36 #ifdef CONFIG_NF_CONNTRACK_EVENTS
37 struct net *net = nf_ct_net(ct);
38 struct nf_conntrack_ecache *e;
40 if (!ctmask && !expmask && net->ct.sysctl_events) {
44 if (!ctmask && !expmask)
47 e = nf_ct_ext_add(ct, NF_CT_EXT_ECACHE, gfp);
58 #ifdef CONFIG_NF_CONNTRACK_EVENTS
59 /* This structure is passed to event handler */
66 struct nf_ct_event_notifier {
67 int (*fcn)(unsigned int events, struct nf_ct_event *item);
70 int nf_conntrack_register_notifier(struct net *net,
71 struct nf_ct_event_notifier *nb);
72 void nf_conntrack_unregister_notifier(struct net *net,
73 struct nf_ct_event_notifier *nb);
75 void nf_ct_deliver_cached_events(struct nf_conn *ct);
78 nf_conntrack_event_cache(enum ip_conntrack_events event, struct nf_conn *ct)
80 struct net *net = nf_ct_net(ct);
81 struct nf_conntrack_ecache *e;
83 if (!rcu_access_pointer(net->ct.nf_conntrack_event_cb))
86 e = nf_ct_ecache_find(ct);
90 set_bit(event, &e->cache);
94 nf_conntrack_eventmask_report(unsigned int eventmask,
100 struct net *net = nf_ct_net(ct);
101 struct nf_ct_event_notifier *notify;
102 struct nf_conntrack_ecache *e;
105 notify = rcu_dereference(net->ct.nf_conntrack_event_cb);
109 e = nf_ct_ecache_find(ct);
113 if (nf_ct_is_confirmed(ct) && !nf_ct_is_dying(ct)) {
114 struct nf_ct_event item = {
116 .portid = e->portid ? e->portid : portid,
119 /* This is a resent of a destroy event? If so, skip missed */
120 unsigned long missed = e->portid ? 0 : e->missed;
122 if (!((eventmask | missed) & e->ctmask))
125 ret = notify->fcn(eventmask | missed, &item);
126 if (unlikely(ret < 0 || missed)) {
127 spin_lock_bh(&ct->lock);
129 /* This is a destroy event that has been
130 * triggered by a process, we store the PORTID
131 * to include it in the retransmission. */
132 if (eventmask & (1 << IPCT_DESTROY) &&
133 e->portid == 0 && portid != 0)
136 e->missed |= eventmask;
138 e->missed &= ~missed;
139 spin_unlock_bh(&ct->lock);
148 nf_conntrack_event_report(enum ip_conntrack_events event, struct nf_conn *ct,
149 u32 portid, int report)
151 return nf_conntrack_eventmask_report(1 << event, ct, portid, report);
155 nf_conntrack_event(enum ip_conntrack_events event, struct nf_conn *ct)
157 return nf_conntrack_eventmask_report(1 << event, ct, 0, 0);
160 struct nf_exp_event {
161 struct nf_conntrack_expect *exp;
166 struct nf_exp_event_notifier {
167 int (*fcn)(unsigned int events, struct nf_exp_event *item);
170 int nf_ct_expect_register_notifier(struct net *net,
171 struct nf_exp_event_notifier *nb);
172 void nf_ct_expect_unregister_notifier(struct net *net,
173 struct nf_exp_event_notifier *nb);
176 nf_ct_expect_event_report(enum ip_conntrack_expect_events event,
177 struct nf_conntrack_expect *exp,
181 struct net *net = nf_ct_exp_net(exp);
182 struct nf_exp_event_notifier *notify;
183 struct nf_conntrack_ecache *e;
186 notify = rcu_dereference(net->ct.nf_expect_event_cb);
190 e = nf_ct_ecache_find(exp->master);
194 if (e->expmask & (1 << event)) {
195 struct nf_exp_event item = {
200 notify->fcn(1 << event, &item);
207 nf_ct_expect_event(enum ip_conntrack_expect_events event,
208 struct nf_conntrack_expect *exp)
210 nf_ct_expect_event_report(event, exp, 0, 0);
213 int nf_conntrack_ecache_pernet_init(struct net *net);
214 void nf_conntrack_ecache_pernet_fini(struct net *net);
216 int nf_conntrack_ecache_init(void);
217 void nf_conntrack_ecache_fini(void);
219 static inline void nf_conntrack_ecache_delayed_work(struct net *net)
221 if (!delayed_work_pending(&net->ct.ecache_dwork)) {
222 schedule_delayed_work(&net->ct.ecache_dwork, HZ);
223 net->ct.ecache_dwork_pending = true;
227 static inline void nf_conntrack_ecache_work(struct net *net)
229 if (net->ct.ecache_dwork_pending) {
230 net->ct.ecache_dwork_pending = false;
231 mod_delayed_work(system_wq, &net->ct.ecache_dwork, 0);
234 #else /* CONFIG_NF_CONNTRACK_EVENTS */
235 static inline void nf_conntrack_event_cache(enum ip_conntrack_events event,
236 struct nf_conn *ct) {}
237 static inline int nf_conntrack_eventmask_report(unsigned int eventmask,
240 int report) { return 0; }
241 static inline int nf_conntrack_event(enum ip_conntrack_events event,
242 struct nf_conn *ct) { return 0; }
243 static inline int nf_conntrack_event_report(enum ip_conntrack_events event,
246 int report) { return 0; }
247 static inline void nf_ct_deliver_cached_events(const struct nf_conn *ct) {}
248 static inline void nf_ct_expect_event(enum ip_conntrack_expect_events event,
249 struct nf_conntrack_expect *exp) {}
250 static inline void nf_ct_expect_event_report(enum ip_conntrack_expect_events e,
251 struct nf_conntrack_expect *exp,
255 static inline int nf_conntrack_ecache_pernet_init(struct net *net)
260 static inline void nf_conntrack_ecache_pernet_fini(struct net *net)
264 static inline int nf_conntrack_ecache_init(void)
269 static inline void nf_conntrack_ecache_fini(void)
273 static inline void nf_conntrack_ecache_delayed_work(struct net *net)
277 static inline void nf_conntrack_ecache_work(struct net *net)
280 #endif /* CONFIG_NF_CONNTRACK_EVENTS */
282 #endif /*_NF_CONNTRACK_ECACHE_H*/