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 */
21 struct timer_list timeout;
24 static inline struct nf_conntrack_ecache *
25 nf_ct_ecache_find(const struct nf_conn *ct)
27 #ifdef CONFIG_NF_CONNTRACK_EVENTS
28 return nf_ct_ext_find(ct, NF_CT_EXT_ECACHE);
34 static inline struct nf_conntrack_ecache *
35 nf_ct_ecache_ext_add(struct nf_conn *ct, u16 ctmask, u16 expmask, gfp_t gfp)
37 #ifdef CONFIG_NF_CONNTRACK_EVENTS
38 struct net *net = nf_ct_net(ct);
39 struct nf_conntrack_ecache *e;
41 if (!ctmask && !expmask && net->ct.sysctl_events) {
45 if (!ctmask && !expmask)
48 e = nf_ct_ext_add(ct, NF_CT_EXT_ECACHE, gfp);
59 #ifdef CONFIG_NF_CONNTRACK_EVENTS
60 /* This structure is passed to event handler */
67 struct nf_ct_event_notifier {
68 int (*fcn)(unsigned int events, struct nf_ct_event *item);
71 int nf_conntrack_register_notifier(struct net *net,
72 struct nf_ct_event_notifier *nb);
73 void nf_conntrack_unregister_notifier(struct net *net,
74 struct nf_ct_event_notifier *nb);
76 void nf_ct_deliver_cached_events(struct nf_conn *ct);
79 nf_conntrack_event_cache(enum ip_conntrack_events event, struct nf_conn *ct)
81 struct net *net = nf_ct_net(ct);
82 struct nf_conntrack_ecache *e;
84 if (!rcu_access_pointer(net->ct.nf_conntrack_event_cb))
87 e = nf_ct_ecache_find(ct);
91 set_bit(event, &e->cache);
95 nf_conntrack_eventmask_report(unsigned int eventmask,
101 struct net *net = nf_ct_net(ct);
102 struct nf_ct_event_notifier *notify;
103 struct nf_conntrack_ecache *e;
106 notify = rcu_dereference(net->ct.nf_conntrack_event_cb);
110 e = nf_ct_ecache_find(ct);
114 if (nf_ct_is_confirmed(ct) && !nf_ct_is_dying(ct)) {
115 struct nf_ct_event item = {
117 .portid = e->portid ? e->portid : portid,
120 /* This is a resent of a destroy event? If so, skip missed */
121 unsigned long missed = e->portid ? 0 : e->missed;
123 if (!((eventmask | missed) & e->ctmask))
126 ret = notify->fcn(eventmask | missed, &item);
127 if (unlikely(ret < 0 || missed)) {
128 spin_lock_bh(&ct->lock);
130 /* This is a destroy event that has been
131 * triggered by a process, we store the PORTID
132 * to include it in the retransmission. */
133 if (eventmask & (1 << IPCT_DESTROY) &&
134 e->portid == 0 && portid != 0)
137 e->missed |= eventmask;
139 e->missed &= ~missed;
140 spin_unlock_bh(&ct->lock);
149 nf_conntrack_event_report(enum ip_conntrack_events event, struct nf_conn *ct,
150 u32 portid, int report)
152 return nf_conntrack_eventmask_report(1 << event, ct, portid, report);
156 nf_conntrack_event(enum ip_conntrack_events event, struct nf_conn *ct)
158 return nf_conntrack_eventmask_report(1 << event, ct, 0, 0);
161 struct nf_exp_event {
162 struct nf_conntrack_expect *exp;
167 struct nf_exp_event_notifier {
168 int (*fcn)(unsigned int events, struct nf_exp_event *item);
171 int nf_ct_expect_register_notifier(struct net *net,
172 struct nf_exp_event_notifier *nb);
173 void nf_ct_expect_unregister_notifier(struct net *net,
174 struct nf_exp_event_notifier *nb);
177 nf_ct_expect_event_report(enum ip_conntrack_expect_events event,
178 struct nf_conntrack_expect *exp,
182 struct net *net = nf_ct_exp_net(exp);
183 struct nf_exp_event_notifier *notify;
184 struct nf_conntrack_ecache *e;
187 notify = rcu_dereference(net->ct.nf_expect_event_cb);
191 e = nf_ct_ecache_find(exp->master);
195 if (e->expmask & (1 << event)) {
196 struct nf_exp_event item = {
201 notify->fcn(1 << event, &item);
208 nf_ct_expect_event(enum ip_conntrack_expect_events event,
209 struct nf_conntrack_expect *exp)
211 nf_ct_expect_event_report(event, exp, 0, 0);
214 int nf_conntrack_ecache_pernet_init(struct net *net);
215 void nf_conntrack_ecache_pernet_fini(struct net *net);
217 int nf_conntrack_ecache_init(void);
218 void nf_conntrack_ecache_fini(void);
219 #else /* CONFIG_NF_CONNTRACK_EVENTS */
221 static inline void nf_conntrack_event_cache(enum ip_conntrack_events event,
222 struct nf_conn *ct) {}
223 static inline int nf_conntrack_eventmask_report(unsigned int eventmask,
226 int report) { return 0; }
227 static inline int nf_conntrack_event(enum ip_conntrack_events event,
228 struct nf_conn *ct) { return 0; }
229 static inline int nf_conntrack_event_report(enum ip_conntrack_events event,
232 int report) { return 0; }
233 static inline void nf_ct_deliver_cached_events(const struct nf_conn *ct) {}
234 static inline void nf_ct_expect_event(enum ip_conntrack_expect_events event,
235 struct nf_conntrack_expect *exp) {}
236 static inline void nf_ct_expect_event_report(enum ip_conntrack_expect_events e,
237 struct nf_conntrack_expect *exp,
241 static inline int nf_conntrack_ecache_pernet_init(struct net *net)
246 static inline void nf_conntrack_ecache_pernet_fini(struct net *net)
250 static inline int nf_conntrack_ecache_init(void)
255 static inline void nf_conntrack_ecache_fini(void)
258 #endif /* CONFIG_NF_CONNTRACK_EVENTS */
260 #endif /*_NF_CONNTRACK_ECACHE_H*/