]> bbs.cooldavid.org Git - net-next-2.6.git/blame - include/net/netfilter/nf_conntrack_ecache.h
netfilter: ctnetlink: support selective event delivery
[net-next-2.6.git] / include / net / netfilter / nf_conntrack_ecache.h
CommitLineData
f6180121
MJ
1/*
2 * connection tracking event cache.
3 */
4
5#ifndef _NF_CONNTRACK_ECACHE_H
6#define _NF_CONNTRACK_ECACHE_H
7#include <net/netfilter/nf_conntrack.h>
8
6058fa6b 9#include <net/net_namespace.h>
f6180121 10#include <net/netfilter/nf_conntrack_expect.h>
a0891aa6
PNA
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>
f6180121 14
a0891aa6 15struct nf_conntrack_ecache {
0cebe4b4
PM
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 */
a0891aa6 21};
6bfea198 22
a0891aa6
PNA
23static inline struct nf_conntrack_ecache *
24nf_ct_ecache_find(const struct nf_conn *ct)
25{
26 return nf_ct_ext_find(ct, NF_CT_EXT_ECACHE);
27}
6bfea198 28
a0891aa6 29static inline struct nf_conntrack_ecache *
0cebe4b4 30nf_ct_ecache_ext_add(struct nf_conn *ct, u16 ctmask, u16 expmask, gfp_t gfp)
a0891aa6
PNA
31{
32 struct net *net = nf_ct_net(ct);
0cebe4b4 33 struct nf_conntrack_ecache *e;
6bfea198 34
0cebe4b4
PM
35 if (!ctmask && !expmask && net->ct.sysctl_events) {
36 ctmask = ~0;
37 expmask = ~0;
38 }
39 if (!ctmask && !expmask)
a0891aa6 40 return NULL;
6bfea198 41
0cebe4b4
PM
42 e = nf_ct_ext_add(ct, NF_CT_EXT_ECACHE, gfp);
43 if (e) {
44 e->ctmask = ctmask;
45 e->expmask = expmask;
46 }
47 return e;
6bfea198
PNA
48};
49
f6180121 50#ifdef CONFIG_NF_CONNTRACK_EVENTS
19abb7b0
PNA
51/* This structure is passed to event handler */
52struct nf_ct_event {
53 struct nf_conn *ct;
54 u32 pid;
55 int report;
56};
57
e34d5c1a
PNA
58struct nf_ct_event_notifier {
59 int (*fcn)(unsigned int events, struct nf_ct_event *item);
60};
61
62extern struct nf_ct_event_notifier *nf_conntrack_event_cb;
63extern int nf_conntrack_register_notifier(struct nf_ct_event_notifier *nb);
64extern void nf_conntrack_unregister_notifier(struct nf_ct_event_notifier *nb);
f6180121 65
a0891aa6 66extern void nf_ct_deliver_cached_events(struct nf_conn *ct);
f6180121
MJ
67
68static inline void
a71996fc 69nf_conntrack_event_cache(enum ip_conntrack_events event, struct nf_conn *ct)
f6180121 70{
a0891aa6
PNA
71 struct nf_conntrack_ecache *e;
72
73 if (nf_conntrack_event_cb == NULL)
74 return;
75
76 e = nf_ct_ecache_find(ct);
77 if (e == NULL)
78 return;
79
0cebe4b4
PM
80 if (!(e->ctmask & (1 << event)))
81 return;
82
a0891aa6 83 set_bit(event, &e->cache);
f6180121
MJ
84}
85
dd7669a9 86static inline int
a0891aa6
PNA
87nf_conntrack_eventmask_report(unsigned int eventmask,
88 struct nf_conn *ct,
89 u32 pid,
90 int report)
f6180121 91{
dd7669a9 92 int ret = 0;
e34d5c1a 93 struct nf_ct_event_notifier *notify;
dd7669a9 94 struct nf_conntrack_ecache *e;
e34d5c1a
PNA
95
96 rcu_read_lock();
97 notify = rcu_dereference(nf_conntrack_event_cb);
98 if (notify == NULL)
99 goto out_unlock;
100
dd7669a9
PNA
101 e = nf_ct_ecache_find(ct);
102 if (e == NULL)
103 goto out_unlock;
104
e34d5c1a
PNA
105 if (nf_ct_is_confirmed(ct) && !nf_ct_is_dying(ct)) {
106 struct nf_ct_event item = {
107 .ct = ct,
dd7669a9 108 .pid = e->pid ? e->pid : pid,
e34d5c1a
PNA
109 .report = report
110 };
dd7669a9
PNA
111 /* This is a resent of a destroy event? If so, skip missed */
112 unsigned long missed = e->pid ? 0 : e->missed;
113
0cebe4b4
PM
114 if (!((eventmask | missed) & e->ctmask))
115 goto out_unlock;
116
dd7669a9
PNA
117 ret = notify->fcn(eventmask | missed, &item);
118 if (unlikely(ret < 0 || missed)) {
119 spin_lock_bh(&ct->lock);
120 if (ret < 0) {
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)
126 e->pid = pid;
127 else
128 e->missed |= eventmask;
129 } else
130 e->missed &= ~missed;
131 spin_unlock_bh(&ct->lock);
132 }
e34d5c1a
PNA
133 }
134out_unlock:
135 rcu_read_unlock();
dd7669a9 136 return ret;
f6180121
MJ
137}
138
dd7669a9 139static inline int
a0891aa6
PNA
140nf_conntrack_event_report(enum ip_conntrack_events event, struct nf_conn *ct,
141 u32 pid, int report)
142{
dd7669a9 143 return nf_conntrack_eventmask_report(1 << event, ct, pid, report);
a0891aa6
PNA
144}
145
dd7669a9 146static inline int
19abb7b0
PNA
147nf_conntrack_event(enum ip_conntrack_events event, struct nf_conn *ct)
148{
dd7669a9 149 return nf_conntrack_eventmask_report(1 << event, ct, 0, 0);
19abb7b0
PNA
150}
151
152struct nf_exp_event {
153 struct nf_conntrack_expect *exp;
154 u32 pid;
155 int report;
156};
157
e34d5c1a
PNA
158struct nf_exp_event_notifier {
159 int (*fcn)(unsigned int events, struct nf_exp_event *item);
160};
161
162extern struct nf_exp_event_notifier *nf_expect_event_cb;
163extern int nf_ct_expect_register_notifier(struct nf_exp_event_notifier *nb);
164extern void nf_ct_expect_unregister_notifier(struct nf_exp_event_notifier *nb);
010c7d6f 165
19abb7b0
PNA
166static inline void
167nf_ct_expect_event_report(enum ip_conntrack_expect_events event,
168 struct nf_conntrack_expect *exp,
169 u32 pid,
170 int report)
171{
e34d5c1a 172 struct nf_exp_event_notifier *notify;
0cebe4b4 173 struct nf_conntrack_ecache *e;
e34d5c1a
PNA
174
175 rcu_read_lock();
176 notify = rcu_dereference(nf_expect_event_cb);
177 if (notify == NULL)
178 goto out_unlock;
179
0cebe4b4
PM
180 e = nf_ct_ecache_find(exp->master);
181 if (e == NULL)
a0891aa6
PNA
182 goto out_unlock;
183
0cebe4b4 184 if (e->expmask & (1 << event)) {
e34d5c1a
PNA
185 struct nf_exp_event item = {
186 .exp = exp,
187 .pid = pid,
188 .report = report
189 };
a0891aa6 190 notify->fcn(1 << event, &item);
e34d5c1a
PNA
191 }
192out_unlock:
193 rcu_read_unlock();
19abb7b0
PNA
194}
195
f6180121 196static inline void
6823645d
PM
197nf_ct_expect_event(enum ip_conntrack_expect_events event,
198 struct nf_conntrack_expect *exp)
f6180121 199{
19abb7b0 200 nf_ct_expect_event_report(event, exp, 0, 0);
f6180121
MJ
201}
202
6058fa6b
AD
203extern int nf_conntrack_ecache_init(struct net *net);
204extern void nf_conntrack_ecache_fini(struct net *net);
205
f6180121
MJ
206#else /* CONFIG_NF_CONNTRACK_EVENTS */
207
208static inline void nf_conntrack_event_cache(enum ip_conntrack_events event,
64f1b653 209 struct nf_conn *ct) {}
dd7669a9
PNA
210static inline int nf_conntrack_eventmask_report(unsigned int eventmask,
211 struct nf_conn *ct,
212 u32 pid,
213 int report) { return 0; }
214static inline int nf_conntrack_event(enum ip_conntrack_events event,
215 struct nf_conn *ct) { return 0; }
216static inline int nf_conntrack_event_report(enum ip_conntrack_events event,
217 struct nf_conn *ct,
218 u32 pid,
219 int report) { return 0; }
f6180121 220static inline void nf_ct_deliver_cached_events(const struct nf_conn *ct) {}
6823645d
PM
221static inline void nf_ct_expect_event(enum ip_conntrack_expect_events event,
222 struct nf_conntrack_expect *exp) {}
19abb7b0
PNA
223static inline void nf_ct_expect_event_report(enum ip_conntrack_expect_events e,
224 struct nf_conntrack_expect *exp,
225 u32 pid,
226 int report) {}
6058fa6b
AD
227
228static inline int nf_conntrack_ecache_init(struct net *net)
229{
230 return 0;
bb21c95e 231}
6058fa6b
AD
232
233static inline void nf_conntrack_ecache_fini(struct net *net)
234{
235}
f6180121
MJ
236#endif /* CONFIG_NF_CONNTRACK_EVENTS */
237
238#endif /*_NF_CONNTRACK_ECACHE_H*/
239