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