Raise a bus alert when IKE message body parsing fails
[strongswan.git] / src / libcharon / bus / bus.h
1 /*
2 * Copyright (C) 2012 Tobias Brunner
3 * Copyright (C) 2006-2009 Martin Willi
4 * Hochschule fuer Technik Rapperswil
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 */
16
17 /**
18 * @defgroup bus bus
19 * @{ @ingroup libcharon
20 */
21
22 #ifndef BUS_H_
23 #define BUS_H_
24
25 typedef enum alert_t alert_t;
26 typedef enum narrow_hook_t narrow_hook_t;
27 typedef struct bus_t bus_t;
28
29 #include <stdarg.h>
30
31 #include <debug.h>
32 #include <sa/ike_sa.h>
33 #include <sa/child_sa.h>
34 #include <processing/jobs/job.h>
35 #include <bus/listeners/logger.h>
36 #include <bus/listeners/listener.h>
37
38 /* undefine the definitions from libstrongswan */
39 #undef DBG0
40 #undef DBG1
41 #undef DBG2
42 #undef DBG3
43 #undef DBG4
44
45 #ifndef DEBUG_LEVEL
46 # define DEBUG_LEVEL 4
47 #endif /* DEBUG_LEVEL */
48
49 #if DEBUG_LEVEL >= 0
50 #define DBG0(group, format, ...) charon->bus->log(charon->bus, group, 0, format, ##__VA_ARGS__)
51 #endif /* DEBUG_LEVEL >= 0 */
52 #if DEBUG_LEVEL >= 1
53 #define DBG1(group, format, ...) charon->bus->log(charon->bus, group, 1, format, ##__VA_ARGS__)
54 #endif /* DEBUG_LEVEL >= 1 */
55 #if DEBUG_LEVEL >= 2
56 #define DBG2(group, format, ...) charon->bus->log(charon->bus, group, 2, format, ##__VA_ARGS__)
57 #endif /* DEBUG_LEVEL >= 2 */
58 #if DEBUG_LEVEL >= 3
59 #define DBG3(group, format, ...) charon->bus->log(charon->bus, group, 3, format, ##__VA_ARGS__)
60 #endif /* DEBUG_LEVEL >= 3 */
61 #if DEBUG_LEVEL >= 4
62 #define DBG4(group, format, ...) charon->bus->log(charon->bus, group, 4, format, ##__VA_ARGS__)
63 #endif /* DEBUG_LEVEL >= 4 */
64
65 #ifndef DBG0
66 # define DBG0(...) {}
67 #endif /* DBG0 */
68 #ifndef DBG1
69 # define DBG1(...) {}
70 #endif /* DBG1 */
71 #ifndef DBG2
72 # define DBG2(...) {}
73 #endif /* DBG2 */
74 #ifndef DBG3
75 # define DBG3(...) {}
76 #endif /* DBG3 */
77 #ifndef DBG4
78 # define DBG4(...) {}
79 #endif /* DBG4 */
80
81 /**
82 * Kind of alerts to raise.
83 */
84 enum alert_t {
85 /** a RADIUS server did not respond, no additional arguments */
86 ALERT_RADIUS_NOT_RESPONDING,
87 /** a shutdown signal has been received, argument is the signal (int) */
88 ALERT_SHUTDOWN_SIGNAL,
89 /** peer authentication failed, no arguments */
90 ALERT_PEER_AUTH_FAILED,
91 /** failed to resolve peer address, no arguments */
92 ALERT_PEER_ADDR_FAILED,
93 /** peer did not respond to initial message, current try (int, 0-based) */
94 ALERT_PEER_INIT_UNREACHABLE,
95 /** received IKE message with invalid SPI, argument is message_t* */
96 ALERT_INVALID_IKE_SPI,
97 /** received IKE message with invalid header, argument is message_t* */
98 ALERT_PARSE_ERROR_HEADER,
99 /** received IKE message with invalid body, argument is message_t*,
100 * followed by a status_t result returned by message_t.parse_body(). */
101 ALERT_PARSE_ERROR_BODY,
102 };
103
104 /**
105 * Kind of narrow hook.
106 *
107 * There is a non-authenticated (IKE_AUTH) and a authenticated
108 * (CREATE_CHILD_SA) narrowing hook for the initiator. Only one of these
109 * hooks is invoked before the exchange.
110 * To verify the traffic selectors negotiated, each PRE hook has a POST
111 * counterpart that follows. POST hooks are invoked with an authenticated peer.
112 * It is usually not a good idea to narrow in the POST hooks,
113 * as the resulting traffic selector is not negotiated and results
114 * in non-matching policies.
115 */
116 enum narrow_hook_t {
117 /** invoked as initiator before exchange, peer is not yet authenticated */
118 NARROW_INITIATOR_PRE_NOAUTH,
119 /** invoked as initiator before exchange, peer is authenticated */
120 NARROW_INITIATOR_PRE_AUTH,
121 /** invoked as responder during exchange, peer is authenticated */
122 NARROW_RESPONDER,
123 /** invoked as responder after exchange, peer is authenticated */
124 NARROW_RESPONDER_POST,
125 /** invoked as initiator after exchange, follows a INITIATOR_PRE_NOAUTH */
126 NARROW_INITIATOR_POST_NOAUTH,
127 /** invoked as initiator after exchange, follows a INITIATOR_PRE_AUTH */
128 NARROW_INITIATOR_POST_AUTH,
129 };
130
131 /**
132 * The bus receives events and sends them to all registered listeners.
133 *
134 * Loggers are handled separately.
135 */
136 struct bus_t {
137
138 /**
139 * Register a listener to the bus.
140 *
141 * A registered listener receives all events which are sent to the bus.
142 * The listener is passive; the thread which emitted the event
143 * processes the listener routine.
144 *
145 * @param listener listener to register.
146 */
147 void (*add_listener) (bus_t *this, listener_t *listener);
148
149 /**
150 * Unregister a listener from the bus.
151 *
152 * @param listener listener to unregister.
153 */
154 void (*remove_listener) (bus_t *this, listener_t *listener);
155
156 /**
157 * Register a logger with the bus.
158 *
159 * The logger is passive; the thread which emitted the event
160 * processes the logger routine. This routine may be called concurrently
161 * by multiple threads. Recursive calls are not prevented, so logger that
162 * may cause recursive calls are responsible to avoid infinite loops.
163 *
164 * During registration get_level() is called for all log groups and the
165 * logger is registered to receive log messages for groups for which
166 * the requested log level is > LEVEL_SILENT and whose level is lower
167 * or equal than the requested level.
168 *
169 * To update the registered log levels call add_logger again with the
170 * same logger and return the new levels from get_level().
171 *
172 * @param logger logger to register.
173 */
174 void (*add_logger) (bus_t *this, logger_t *logger);
175
176 /**
177 * Unregister a logger from the bus.
178 *
179 * @param logger logger to unregister.
180 */
181 void (*remove_logger) (bus_t *this, logger_t *logger);
182
183 /**
184 * Set the IKE_SA the calling thread is using.
185 *
186 * To associate a received log message with an IKE_SA without passing it as
187 * parameter each time, the thread registers the currently used IKE_SA
188 * during check-out. Before check-in, the thread unregisters the IKE_SA.
189 * This IKE_SA is stored per-thread, so each thread has its own IKE_SA
190 * registered.
191 *
192 * @param ike_sa ike_sa to register, or NULL to unregister
193 */
194 void (*set_sa) (bus_t *this, ike_sa_t *ike_sa);
195
196 /**
197 * Get the IKE_SA the calling thread is currently using.
198 *
199 * If a thread currently does not know what IKE_SA it is processing,
200 * it can call get_sa() to look up the SA set during checkout via set_sa().
201 *
202 * @return registered ike_sa, NULL if none registered
203 */
204 ike_sa_t* (*get_sa)(bus_t *this);
205
206 /**
207 * Send a log message to the bus.
208 *
209 * The format string specifies an additional informational or error
210 * message with a printf() like variable argument list.
211 * Use the DBG() macros.
212 *
213 * @param group debugging group
214 * @param level verbosity level of the signal
215 * @param format printf() style format string
216 * @param ... printf() style argument list
217 */
218 void (*log)(bus_t *this, debug_t group, level_t level, char* format, ...);
219
220 /**
221 * Send a log message to the bus using va_list arguments.
222 *
223 * Same as bus_t.log(), but uses va_list argument list.
224 *
225 * @param group kind of the signal (up, down, rekeyed, ...)
226 * @param level verbosity level of the signal
227 * @param format printf() style format string
228 * @param args va_list arguments
229 */
230 void (*vlog)(bus_t *this, debug_t group, level_t level,
231 char* format, va_list args);
232
233 /**
234 * Raise an alert over the bus.
235 *
236 * @param alert kind of alert
237 * @param ... alert specific arguments
238 */
239 void (*alert)(bus_t *this, alert_t alert, ...);
240
241 /**
242 * Send a IKE_SA state change event to the bus.
243 *
244 * @param ike_sa IKE_SA which changes its state
245 * @param state new state IKE_SA changes to
246 */
247 void (*ike_state_change)(bus_t *this, ike_sa_t *ike_sa,
248 ike_sa_state_t state);
249 /**
250 * Send a CHILD_SA state change event to the bus.
251 *
252 * @param child_sa CHILD_SA which changes its state
253 * @param state new state CHILD_SA changes to
254 */
255 void (*child_state_change)(bus_t *this, child_sa_t *child_sa,
256 child_sa_state_t state);
257 /**
258 * Message send/receive hook.
259 *
260 * The hook is invoked twice for each message: Once with plain, parsed data
261 * and once encoded and encrypted.
262 *
263 * @param message message to send/receive
264 * @param incoming TRUE for incoming messages, FALSE for outgoing
265 * @param plain TRUE if message is parsed and decrypted, FALSE it not
266 */
267 void (*message)(bus_t *this, message_t *message, bool incoming, bool plain);
268
269 /**
270 * IKE_SA authorization hook.
271 *
272 * @param final TRUE if this is the final invocation
273 * @return TRUE to establish IKE_SA, FALSE to send AUTH_FAILED
274 */
275 bool (*authorize)(bus_t *this, bool final);
276
277 /**
278 * CHILD_SA traffic selector narrowing hook.
279 *
280 * @param child_sa CHILD_SA set up with these traffic selectors
281 * @param type type of hook getting invoked
282 * @param local list of local traffic selectors to narrow
283 * @param remote list of remote traffic selectors to narrow
284 */
285 void (*narrow)(bus_t *this, child_sa_t *child_sa, narrow_hook_t type,
286 linked_list_t *local, linked_list_t *remote);
287
288 /**
289 * IKE_SA keymat hook.
290 *
291 * @param ike_sa IKE_SA this keymat belongs to
292 * @param dh diffie hellman shared secret
293 * @param dh_other others DH public value (IKEv1 only)
294 * @param nonce_i initiators nonce
295 * @param nonce_r responders nonce
296 * @param rekey IKE_SA we are rekeying, if any (IKEv2 only)
297 * @param shared shared key used for key derivation (IKEv1-PSK only)
298 */
299 void (*ike_keys)(bus_t *this, ike_sa_t *ike_sa, diffie_hellman_t *dh,
300 chunk_t dh_other, chunk_t nonce_i, chunk_t nonce_r,
301 ike_sa_t *rekey, shared_key_t *shared);
302
303 /**
304 * CHILD_SA keymat hook.
305 *
306 * @param child_sa CHILD_SA this keymat is used for
307 * @param initiator initiator of the CREATE_CHILD_SA exchange
308 * @param dh diffie hellman shared secret
309 * @param nonce_i initiators nonce
310 * @param nonce_r responders nonce
311 */
312 void (*child_keys)(bus_t *this, child_sa_t *child_sa, bool initiator,
313 diffie_hellman_t *dh, chunk_t nonce_i, chunk_t nonce_r);
314
315 /**
316 * IKE_SA up/down hook.
317 *
318 * @param ike_sa IKE_SA coming up/going down
319 * @param up TRUE for an up event, FALSE for a down event
320 */
321 void (*ike_updown)(bus_t *this, ike_sa_t *ike_sa, bool up);
322
323 /**
324 * IKE_SA rekeying hook.
325 *
326 * @param old rekeyed and obsolete IKE_SA
327 * @param new new IKE_SA replacing old
328 */
329 void (*ike_rekey)(bus_t *this, ike_sa_t *old, ike_sa_t *new);
330
331 /**
332 * IKE_SA reestablishing hook.
333 *
334 * @param old reestablished and obsolete IKE_SA
335 * @param new new IKE_SA replacing old
336 */
337 void (*ike_reestablish)(bus_t *this, ike_sa_t *old, ike_sa_t *new);
338
339 /**
340 * CHILD_SA up/down hook.
341 *
342 * @param child_sa CHILD_SA coming up/going down
343 * @param up TRUE for an up event, FALSE for a down event
344 */
345 void (*child_updown)(bus_t *this, child_sa_t *child_sa, bool up);
346
347 /**
348 * CHILD_SA rekeying hook.
349 *
350 * @param old rekeyed and obsolete CHILD_SA
351 * @param new new CHILD_SA replacing old
352 */
353 void (*child_rekey)(bus_t *this, child_sa_t *old, child_sa_t *new);
354
355 /**
356 * Destroy the event bus.
357 */
358 void (*destroy) (bus_t *this);
359 };
360
361 /**
362 * Create the event bus which forwards events to its listeners.
363 *
364 * @return event bus instance
365 */
366 bus_t *bus_create();
367
368 #endif /** BUS_H_ @}*/