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 extern int nf_conntrack_register_notifier(struct net *net, struct nf_ct_event_notifier *nb);
72 extern void nf_conntrack_unregister_notifier(struct net *net, struct nf_ct_event_notifier *nb);
74 extern void nf_ct_deliver_cached_events(struct nf_conn *ct);
77 nf_conntrack_event_cache(enum ip_conntrack_events event, struct nf_conn *ct)
79 struct net *net = nf_ct_net(ct);
80 struct nf_conntrack_ecache *e;
82 if (!rcu_access_pointer(net->ct.nf_conntrack_event_cb))
85 e = nf_ct_ecache_find(ct);
89 set_bit(event, &e->cache);
93 nf_conntrack_eventmask_report(unsigned int eventmask,
99 struct net *net = nf_ct_net(ct);
100 struct nf_ct_event_notifier *notify;
101 struct nf_conntrack_ecache *e;
104 notify = rcu_dereference(net->ct.nf_conntrack_event_cb);
108 e = nf_ct_ecache_find(ct);
112 if (nf_ct_is_confirmed(ct) && !nf_ct_is_dying(ct)) {
113 struct nf_ct_event item = {
115 .portid = e->portid ? e->portid : portid,
118 /* This is a resent of a destroy event? If so, skip missed */
119 unsigned long missed = e->portid ? 0 : e->missed;
121 if (!((eventmask | missed) & e->ctmask))
124 ret = notify->fcn(eventmask | missed, &item);
125 if (unlikely(ret < 0 || missed)) {
126 spin_lock_bh(&ct->lock);
128 /* This is a destroy event that has been
129 * triggered by a process, we store the PORTID
130 * to include it in the retransmission. */
131 if (eventmask & (1 << IPCT_DESTROY) &&
132 e->portid == 0 && portid != 0)
135 e->missed |= eventmask;
137 e->missed &= ~missed;
138 spin_unlock_bh(&ct->lock);
147 nf_conntrack_event_report(enum ip_conntrack_events event, struct nf_conn *ct,
148 u32 portid, int report)
150 return nf_conntrack_eventmask_report(1 << event, ct, portid, report);
154 nf_conntrack_event(enum ip_conntrack_events event, struct nf_conn *ct)
156 return nf_conntrack_eventmask_report(1 << event, ct, 0, 0);
159 struct nf_exp_event {
160 struct nf_conntrack_expect *exp;
165 struct nf_exp_event_notifier {
166 int (*fcn)(unsigned int events, struct nf_exp_event *item);
169 extern int nf_ct_expect_register_notifier(struct net *net, struct nf_exp_event_notifier *nb);
170 extern void nf_ct_expect_unregister_notifier(struct net *net, struct nf_exp_event_notifier *nb);
173 nf_ct_expect_event_report(enum ip_conntrack_expect_events event,
174 struct nf_conntrack_expect *exp,
178 struct net *net = nf_ct_exp_net(exp);
179 struct nf_exp_event_notifier *notify;
180 struct nf_conntrack_ecache *e;
183 notify = rcu_dereference(net->ct.nf_expect_event_cb);
187 e = nf_ct_ecache_find(exp->master);
191 if (e->expmask & (1 << event)) {
192 struct nf_exp_event item = {
197 notify->fcn(1 << event, &item);
204 nf_ct_expect_event(enum ip_conntrack_expect_events event,
205 struct nf_conntrack_expect *exp)
207 nf_ct_expect_event_report(event, exp, 0, 0);
210 extern int nf_conntrack_ecache_pernet_init(struct net *net);
211 extern void nf_conntrack_ecache_pernet_fini(struct net *net);
213 extern int nf_conntrack_ecache_init(void);
214 extern void nf_conntrack_ecache_fini(void);
215 #else /* CONFIG_NF_CONNTRACK_EVENTS */
217 static inline void nf_conntrack_event_cache(enum ip_conntrack_events event,
218 struct nf_conn *ct) {}
219 static inline int nf_conntrack_eventmask_report(unsigned int eventmask,
222 int report) { return 0; }
223 static inline int nf_conntrack_event(enum ip_conntrack_events event,
224 struct nf_conn *ct) { return 0; }
225 static inline int nf_conntrack_event_report(enum ip_conntrack_events event,
228 int report) { return 0; }
229 static inline void nf_ct_deliver_cached_events(const struct nf_conn *ct) {}
230 static inline void nf_ct_expect_event(enum ip_conntrack_expect_events event,
231 struct nf_conntrack_expect *exp) {}
232 static inline void nf_ct_expect_event_report(enum ip_conntrack_expect_events e,
233 struct nf_conntrack_expect *exp,
237 static inline int nf_conntrack_ecache_pernet_init(struct net *net)
242 static inline void nf_conntrack_ecache_pernet_fini(struct net *net)
246 static inline int nf_conntrack_ecache_init(void)
251 static inline void nf_conntrack_ecache_fini(void)
254 #endif /* CONFIG_NF_CONNTRACK_EVENTS */
256 #endif /*_NF_CONNTRACK_ECACHE_H*/