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