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 pid; /* netlink pid of destroyer */
23 static inline struct nf_conntrack_ecache *
24 nf_ct_ecache_find(const struct nf_conn *ct)
26 return nf_ct_ext_find(ct, NF_CT_EXT_ECACHE);
29 static inline struct nf_conntrack_ecache *
30 nf_ct_ecache_ext_add(struct nf_conn *ct, u16 ctmask, u16 expmask, gfp_t gfp)
32 struct net *net = nf_ct_net(ct);
33 struct nf_conntrack_ecache *e;
35 if (!ctmask && !expmask && net->ct.sysctl_events) {
39 if (!ctmask && !expmask)
42 e = nf_ct_ext_add(ct, NF_CT_EXT_ECACHE, gfp);
50 #ifdef CONFIG_NF_CONNTRACK_EVENTS
51 /* This structure is passed to event handler */
58 struct nf_ct_event_notifier {
59 int (*fcn)(unsigned int events, struct nf_ct_event *item);
62 extern struct nf_ct_event_notifier *nf_conntrack_event_cb;
63 extern int nf_conntrack_register_notifier(struct nf_ct_event_notifier *nb);
64 extern void nf_conntrack_unregister_notifier(struct nf_ct_event_notifier *nb);
66 extern void nf_ct_deliver_cached_events(struct nf_conn *ct);
69 nf_conntrack_event_cache(enum ip_conntrack_events event, struct nf_conn *ct)
71 struct nf_conntrack_ecache *e;
73 if (nf_conntrack_event_cb == NULL)
76 e = nf_ct_ecache_find(ct);
80 if (!(e->ctmask & (1 << event)))
83 set_bit(event, &e->cache);
87 nf_conntrack_eventmask_report(unsigned int eventmask,
93 struct nf_ct_event_notifier *notify;
94 struct nf_conntrack_ecache *e;
97 notify = rcu_dereference(nf_conntrack_event_cb);
101 e = nf_ct_ecache_find(ct);
105 if (nf_ct_is_confirmed(ct) && !nf_ct_is_dying(ct)) {
106 struct nf_ct_event item = {
108 .pid = e->pid ? e->pid : pid,
111 /* This is a resent of a destroy event? If so, skip missed */
112 unsigned long missed = e->pid ? 0 : e->missed;
114 if (!((eventmask | missed) & e->ctmask))
117 ret = notify->fcn(eventmask | missed, &item);
118 if (unlikely(ret < 0 || missed)) {
119 spin_lock_bh(&ct->lock);
121 /* This is a destroy event that has been
122 * triggered by a process, we store the PID
123 * to include it in the retransmission. */
124 if (eventmask & (1 << IPCT_DESTROY) &&
125 e->pid == 0 && pid != 0)
128 e->missed |= eventmask;
130 e->missed &= ~missed;
131 spin_unlock_bh(&ct->lock);
140 nf_conntrack_event_report(enum ip_conntrack_events event, struct nf_conn *ct,
143 return nf_conntrack_eventmask_report(1 << event, ct, pid, report);
147 nf_conntrack_event(enum ip_conntrack_events event, struct nf_conn *ct)
149 return nf_conntrack_eventmask_report(1 << event, ct, 0, 0);
152 struct nf_exp_event {
153 struct nf_conntrack_expect *exp;
158 struct nf_exp_event_notifier {
159 int (*fcn)(unsigned int events, struct nf_exp_event *item);
162 extern struct nf_exp_event_notifier *nf_expect_event_cb;
163 extern int nf_ct_expect_register_notifier(struct nf_exp_event_notifier *nb);
164 extern void nf_ct_expect_unregister_notifier(struct nf_exp_event_notifier *nb);
167 nf_ct_expect_event_report(enum ip_conntrack_expect_events event,
168 struct nf_conntrack_expect *exp,
172 struct nf_exp_event_notifier *notify;
173 struct nf_conntrack_ecache *e;
176 notify = rcu_dereference(nf_expect_event_cb);
180 e = nf_ct_ecache_find(exp->master);
184 if (e->expmask & (1 << event)) {
185 struct nf_exp_event item = {
190 notify->fcn(1 << event, &item);
197 nf_ct_expect_event(enum ip_conntrack_expect_events event,
198 struct nf_conntrack_expect *exp)
200 nf_ct_expect_event_report(event, exp, 0, 0);
203 extern int nf_conntrack_ecache_init(struct net *net);
204 extern void nf_conntrack_ecache_fini(struct net *net);
206 #else /* CONFIG_NF_CONNTRACK_EVENTS */
208 static inline void nf_conntrack_event_cache(enum ip_conntrack_events event,
209 struct nf_conn *ct) {}
210 static inline int nf_conntrack_eventmask_report(unsigned int eventmask,
213 int report) { return 0; }
214 static inline int nf_conntrack_event(enum ip_conntrack_events event,
215 struct nf_conn *ct) { return 0; }
216 static inline int nf_conntrack_event_report(enum ip_conntrack_events event,
219 int report) { return 0; }
220 static inline void nf_ct_deliver_cached_events(const struct nf_conn *ct) {}
221 static inline void nf_ct_expect_event(enum ip_conntrack_expect_events event,
222 struct nf_conntrack_expect *exp) {}
223 static inline void nf_ct_expect_event_report(enum ip_conntrack_expect_events e,
224 struct nf_conntrack_expect *exp,
228 static inline int nf_conntrack_ecache_init(struct net *net)
233 static inline void nf_conntrack_ecache_fini(struct net *net)
236 #endif /* CONFIG_NF_CONNTRACK_EVENTS */
238 #endif /*_NF_CONNTRACK_ECACHE_H*/