stroke: Allow specifying the ipsec.secrets location in strongswan.conf
[strongswan.git] / src / libcharon / bus / bus.h
1 /*
2 * Copyright (C) 2012-2014 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 <utils/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 /** local peer authentication failed (by us or by peer), no arguments */
90 ALERT_LOCAL_AUTH_FAILED,
91 /** peer authentication failed, no arguments */
92 ALERT_PEER_AUTH_FAILED,
93 /** failed to resolve peer address, no arguments */
94 ALERT_PEER_ADDR_FAILED,
95 /** peer did not respond to initial message, current try (int, 0-based) */
96 ALERT_PEER_INIT_UNREACHABLE,
97 /** received IKE message with invalid SPI, argument is message_t* */
98 ALERT_INVALID_IKE_SPI,
99 /** received IKE message with invalid header, argument is message_t* */
100 ALERT_PARSE_ERROR_HEADER,
101 /** received IKE message with invalid body, argument is message_t*,
102 * followed by a status_t result returned by message_t.parse_body(). */
103 ALERT_PARSE_ERROR_BODY,
104 /** sending a retransmit for a message, argument is packet_t */
105 ALERT_RETRANSMIT_SEND,
106 /** sending retransmits timed out, argument is packet_t, if available */
107 ALERT_RETRANSMIT_SEND_TIMEOUT,
108 /** received a retransmit for a message, argument is message_t */
109 ALERT_RETRANSMIT_RECEIVE,
110 /** received half-open timeout before IKE_SA established, no argument */
111 ALERT_HALF_OPEN_TIMEOUT,
112 /** IKE proposals do not match, argument is linked_list_t of proposal_t */
113 ALERT_PROPOSAL_MISMATCH_IKE,
114 /** CHILD proposals do not match, argument is linked_list_t of proposal_t */
115 ALERT_PROPOSAL_MISMATCH_CHILD,
116 /** traffic selectors do not match, arguments are two linked_list_t
117 * containing traffic_selector_t for initiator and for responder */
118 ALERT_TS_MISMATCH,
119 /** traffic selectors have been narrowed by the peer, arguments are
120 * an int (TRUE for local TS), a linked_list_t* (final TS list), and the
121 * child_cfg_t*. */
122 ALERT_TS_NARROWED,
123 /** Installation of IPsec SAs failed, argument is child_sa_t */
124 ALERT_INSTALL_CHILD_SA_FAILED,
125 /** Installation of IPsec Policy failed, argument is child_sa_t */
126 ALERT_INSTALL_CHILD_POLICY_FAILED,
127 /** IKE_SA deleted because of "replace" unique policy, no argument */
128 ALERT_UNIQUE_REPLACE,
129 /** IKE_SA deleted because of "keep" unique policy, no argument */
130 ALERT_UNIQUE_KEEP,
131 /** IKE_SA kept on failed child SA establishment, no argument */
132 ALERT_KEEP_ON_CHILD_SA_FAILURE,
133 /** allocating virtual IP failed, linked_list_t of host_t requested */
134 ALERT_VIP_FAILURE,
135 /** an authorize() hook failed, no argument */
136 ALERT_AUTHORIZATION_FAILED,
137 /** IKE_SA hit the hard lifetime limit before it could be rekeyed */
138 ALERT_IKE_SA_EXPIRED,
139 /** Certificate rejected; it has expired, certificate_t */
140 ALERT_CERT_EXPIRED,
141 /** Certificate rejected; it has been revoked, certificate_t */
142 ALERT_CERT_REVOKED,
143 /** Validating certificate status failed, certificate_t */
144 ALERT_CERT_VALIDATION_FAILED,
145 /** Certificate rejected; no trusted issuer found, certificate_t */
146 ALERT_CERT_NO_ISSUER,
147 /** Certificate rejected; root not trusted, certificate_t */
148 ALERT_CERT_UNTRUSTED_ROOT,
149 /** Certificate rejected; trustchain length exceeds limit, certificate_t */
150 ALERT_CERT_EXCEEDED_PATH_LEN,
151 /** Certificate rejected; other policy violation, certificate_t */
152 ALERT_CERT_POLICY_VIOLATION,
153 };
154
155 /**
156 * Kind of narrow hook.
157 *
158 * There is a non-authenticated (IKE_AUTH) and a authenticated
159 * (CREATE_CHILD_SA) narrowing hook for the initiator. Only one of these
160 * hooks is invoked before the exchange.
161 * To verify the traffic selectors negotiated, each PRE hook has a POST
162 * counterpart that follows. POST hooks are invoked with an authenticated peer.
163 * It is usually not a good idea to narrow in the POST hooks,
164 * as the resulting traffic selector is not negotiated and results
165 * in non-matching policies.
166 */
167 enum narrow_hook_t {
168 /** invoked as initiator before exchange, peer is not yet authenticated */
169 NARROW_INITIATOR_PRE_NOAUTH,
170 /** invoked as initiator before exchange, peer is authenticated */
171 NARROW_INITIATOR_PRE_AUTH,
172 /** invoked as responder during exchange, peer is authenticated */
173 NARROW_RESPONDER,
174 /** invoked as responder after exchange, peer is authenticated */
175 NARROW_RESPONDER_POST,
176 /** invoked as initiator after exchange, follows a INITIATOR_PRE_NOAUTH */
177 NARROW_INITIATOR_POST_NOAUTH,
178 /** invoked as initiator after exchange, follows a INITIATOR_PRE_AUTH */
179 NARROW_INITIATOR_POST_AUTH,
180 };
181
182 /**
183 * The bus receives events and sends them to all registered listeners.
184 *
185 * Loggers are handled separately.
186 */
187 struct bus_t {
188
189 /**
190 * Register a listener to the bus.
191 *
192 * A registered listener receives all events which are sent to the bus.
193 * The listener is passive; the thread which emitted the event
194 * processes the listener routine.
195 *
196 * @param listener listener to register.
197 */
198 void (*add_listener) (bus_t *this, listener_t *listener);
199
200 /**
201 * Unregister a listener from the bus.
202 *
203 * @param listener listener to unregister.
204 */
205 void (*remove_listener) (bus_t *this, listener_t *listener);
206
207 /**
208 * Register a logger with the bus.
209 *
210 * The logger is passive; the thread which emitted the event
211 * processes the logger routine. This routine may be called concurrently
212 * by multiple threads. Recursive calls are not prevented, so logger that
213 * may cause recursive calls are responsible to avoid infinite loops.
214 *
215 * During registration get_level() is called for all log groups and the
216 * logger is registered to receive log messages for groups for which
217 * the requested log level is > LEVEL_SILENT and whose level is lower
218 * or equal than the requested level.
219 *
220 * To update the registered log levels call add_logger again with the
221 * same logger and return the new levels from get_level().
222 *
223 * @param logger logger to register.
224 */
225 void (*add_logger) (bus_t *this, logger_t *logger);
226
227 /**
228 * Unregister a logger from the bus.
229 *
230 * @param logger logger to unregister.
231 */
232 void (*remove_logger) (bus_t *this, logger_t *logger);
233
234 /**
235 * Set the IKE_SA the calling thread is using.
236 *
237 * To associate a received log message with an IKE_SA without passing it as
238 * parameter each time, the thread registers the currently used IKE_SA
239 * during check-out. Before check-in, the thread unregisters the IKE_SA.
240 * This IKE_SA is stored per-thread, so each thread has its own IKE_SA
241 * registered.
242 *
243 * @param ike_sa ike_sa to register, or NULL to unregister
244 */
245 void (*set_sa) (bus_t *this, ike_sa_t *ike_sa);
246
247 /**
248 * Get the IKE_SA the calling thread is currently using.
249 *
250 * If a thread currently does not know what IKE_SA it is processing,
251 * it can call get_sa() to look up the SA set during checkout via set_sa().
252 *
253 * @return registered ike_sa, NULL if none registered
254 */
255 ike_sa_t* (*get_sa)(bus_t *this);
256
257 /**
258 * Send a log message to the bus.
259 *
260 * The format string specifies an additional informational or error
261 * message with a printf() like variable argument list.
262 * Use the DBG() macros.
263 *
264 * @param group debugging group
265 * @param level verbosity level of the signal
266 * @param format printf() style format string
267 * @param ... printf() style argument list
268 */
269 void (*log)(bus_t *this, debug_t group, level_t level, char* format, ...);
270
271 /**
272 * Send a log message to the bus using va_list arguments.
273 *
274 * Same as bus_t.log(), but uses va_list argument list.
275 *
276 * @param group kind of the signal (up, down, rekeyed, ...)
277 * @param level verbosity level of the signal
278 * @param format printf() style format string
279 * @param args va_list arguments
280 */
281 void (*vlog)(bus_t *this, debug_t group, level_t level,
282 char* format, va_list args);
283
284 /**
285 * Raise an alert over the bus.
286 *
287 * @param alert kind of alert
288 * @param ... alert specific arguments
289 */
290 void (*alert)(bus_t *this, alert_t alert, ...);
291
292 /**
293 * Send a IKE_SA state change event to the bus.
294 *
295 * @param ike_sa IKE_SA which changes its state
296 * @param state new state IKE_SA changes to
297 */
298 void (*ike_state_change)(bus_t *this, ike_sa_t *ike_sa,
299 ike_sa_state_t state);
300 /**
301 * Send a CHILD_SA state change event to the bus.
302 *
303 * @param child_sa CHILD_SA which changes its state
304 * @param state new state CHILD_SA changes to
305 */
306 void (*child_state_change)(bus_t *this, child_sa_t *child_sa,
307 child_sa_state_t state);
308 /**
309 * Message send/receive hook.
310 *
311 * The hook is invoked twice for each message: Once with plain, parsed data
312 * and once encoded and encrypted.
313 *
314 * @param message message to send/receive
315 * @param incoming TRUE for incoming messages, FALSE for outgoing
316 * @param plain TRUE if message is parsed and decrypted, FALSE it not
317 */
318 void (*message)(bus_t *this, message_t *message, bool incoming, bool plain);
319
320 /**
321 * IKE_SA authorization hook.
322 *
323 * @param final TRUE if this is the final invocation
324 * @return TRUE to establish IKE_SA, FALSE to send AUTH_FAILED
325 */
326 bool (*authorize)(bus_t *this, bool final);
327
328 /**
329 * CHILD_SA traffic selector narrowing hook.
330 *
331 * @param child_sa CHILD_SA set up with these traffic selectors
332 * @param type type of hook getting invoked
333 * @param local list of local traffic selectors to narrow
334 * @param remote list of remote traffic selectors to narrow
335 */
336 void (*narrow)(bus_t *this, child_sa_t *child_sa, narrow_hook_t type,
337 linked_list_t *local, linked_list_t *remote);
338
339 /**
340 * IKE_SA keymat hook.
341 *
342 * @param ike_sa IKE_SA this keymat belongs to
343 * @param dh diffie hellman shared secret
344 * @param dh_other others DH public value (IKEv1 only)
345 * @param nonce_i initiators nonce
346 * @param nonce_r responders nonce
347 * @param rekey IKE_SA we are rekeying, if any (IKEv2 only)
348 * @param shared shared key used for key derivation (IKEv1-PSK only)
349 */
350 void (*ike_keys)(bus_t *this, ike_sa_t *ike_sa, diffie_hellman_t *dh,
351 chunk_t dh_other, chunk_t nonce_i, chunk_t nonce_r,
352 ike_sa_t *rekey, shared_key_t *shared);
353
354 /**
355 * CHILD_SA keymat hook.
356 *
357 * @param child_sa CHILD_SA this keymat is used for
358 * @param initiator initiator of the CREATE_CHILD_SA exchange
359 * @param dh diffie hellman shared secret
360 * @param nonce_i initiators nonce
361 * @param nonce_r responders nonce
362 */
363 void (*child_keys)(bus_t *this, child_sa_t *child_sa, bool initiator,
364 diffie_hellman_t *dh, chunk_t nonce_i, chunk_t nonce_r);
365
366 /**
367 * IKE_SA up/down hook.
368 *
369 * @param ike_sa IKE_SA coming up/going down
370 * @param up TRUE for an up event, FALSE for a down event
371 */
372 void (*ike_updown)(bus_t *this, ike_sa_t *ike_sa, bool up);
373
374 /**
375 * IKE_SA rekeying hook.
376 *
377 * @param old rekeyed and obsolete IKE_SA
378 * @param new new IKE_SA replacing old
379 */
380 void (*ike_rekey)(bus_t *this, ike_sa_t *old, ike_sa_t *new);
381
382 /**
383 * IKE_SA reestablishing hook (before resolving hosts).
384 *
385 * @param old reestablished and obsolete IKE_SA
386 * @param new new IKE_SA replacing old
387 */
388 void (*ike_reestablish_pre)(bus_t *this, ike_sa_t *old, ike_sa_t *new);
389
390 /**
391 * IKE_SA reestablishing hook (after configuring and initiating the new
392 * IKE_SA).
393 *
394 * @param old reestablished and obsolete IKE_SA
395 * @param new new IKE_SA replacing old
396 * @param initiated TRUE if initiated successfully, FALSE otherwise
397 */
398 void (*ike_reestablish_post)(bus_t *this, ike_sa_t *old, ike_sa_t *new,
399 bool initiated);
400
401 /**
402 * CHILD_SA up/down hook.
403 *
404 * @param child_sa CHILD_SA coming up/going down
405 * @param up TRUE for an up event, FALSE for a down event
406 */
407 void (*child_updown)(bus_t *this, child_sa_t *child_sa, bool up);
408
409 /**
410 * CHILD_SA rekeying hook.
411 *
412 * @param old rekeyed and obsolete CHILD_SA
413 * @param new new CHILD_SA replacing old
414 */
415 void (*child_rekey)(bus_t *this, child_sa_t *old, child_sa_t *new);
416
417 /**
418 * Virtual IP assignment hook.
419 *
420 * @param ike_sa IKE_SA the VIPs are assigned to
421 * @param assign TRUE if assigned to IKE_SA, FALSE if released
422 */
423 void (*assign_vips)(bus_t *this, ike_sa_t *ike_sa, bool assign);
424
425 /**
426 * Virtual IP handler hook.
427 *
428 * @param ike_sa IKE_SA the VIPs/attributes got handled on
429 * @param assign TRUE after installing attributes, FALSE on release
430 */
431 void (*handle_vips)(bus_t *this, ike_sa_t *ike_sa, bool handle);
432
433 /**
434 * Destroy the event bus.
435 */
436 void (*destroy) (bus_t *this);
437 };
438
439 /**
440 * Create the event bus which forwards events to its listeners.
441 *
442 * @return event bus instance
443 */
444 bus_t *bus_create();
445
446 #endif /** BUS_H_ @}*/