some improvements in signaling code
[strongswan.git] / src / charon / sa / ike_sa.h
1 /**
2 * @file ike_sa.h
3 *
4 * @brief Interface of ike_sa_t.
5 *
6 */
7
8 /*
9 * Copyright (C) 2006 Tobias Brunner, Daniel Roethlisberger
10 * Copyright (C) 2005-2006 Martin Willi
11 * Copyright (C) 2005 Jan Hutter
12 * Hochschule fuer Technik Rapperswil
13 *
14 * This program is free software; you can redistribute it and/or modify it
15 * under the terms of the GNU General Public License as published by the
16 * Free Software Foundation; either version 2 of the License, or (at your
17 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
18 *
19 * This program is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
21 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 * for more details.
23 */
24
25 #ifndef IKE_SA_H_
26 #define IKE_SA_H_
27
28 #include <types.h>
29 #include <encoding/message.h>
30 #include <encoding/payloads/proposal_substructure.h>
31 #include <sa/ike_sa_id.h>
32 #include <sa/child_sa.h>
33 #include <config/configuration.h>
34 #include <utils/randomizer.h>
35 #include <crypto/prfs/prf.h>
36 #include <crypto/crypters/crypter.h>
37 #include <crypto/signers/signer.h>
38 #include <config/connections/connection.h>
39 #include <config/policies/policy.h>
40 #include <config/proposal.h>
41
42
43 #define IKE_SA_PRINTF_SPEC 'K'
44
45 typedef enum ike_sa_state_t ike_sa_state_t;
46
47 /**
48 * @brief State of an IKE_SA.
49 *
50 * An IKE_SA passes various states in its lifetime. A newly created
51 * SA is in the state CREATED.
52 * @verbatim
53 +----------------+
54 ¦ SA_CREATED ¦
55 +----------------+
56 ¦
57 on initiate()---> ¦ <----- on IKE_SA_INIT received
58 V
59 +----------------+
60 ¦ SA_CONNECTING ¦
61 +----------------+
62 ¦
63 ¦ <----- on IKE_AUTH successfully completed
64 V
65 +----------------+
66 ¦ SA_ESTABLISHED ¦-------------------------+ <-- on rekeying
67 +----------------+ ¦
68 ¦ V
69 on delete()---> ¦ <----- on IKE_SA +-------------+
70 ¦ delete request ¦ SA_REKEYING ¦
71 ¦ received +-------------+
72 V ¦
73 +----------------+ ¦
74 ¦ SA_DELETING ¦<------------------------+ <-- after rekeying
75 +----------------+
76 ¦
77 ¦ <----- after delete() acknowledged
78 ¦
79 \V/
80 X
81 / \
82 @endverbatim
83 *
84 * @ingroup sa
85 */
86 enum ike_sa_state_t {
87
88 /**
89 * IKE_SA just got created, but is not initiating nor responding yet.
90 */
91 IKE_CREATED,
92
93 /**
94 * IKE_SA gets initiated actively or passively
95 */
96 IKE_CONNECTING,
97
98 /**
99 * IKE_SA is fully established
100 */
101 IKE_ESTABLISHED,
102
103 /**
104 * IKE_SA rekeying in progress
105 */
106 IKE_REKEYING,
107
108 /**
109 * IKE_SA is in progress of deletion
110 */
111 IKE_DELETING,
112 };
113
114 /**
115 * enum names for ike_sa_state_t.
116 */
117 extern enum_name_t *ike_sa_state_names;
118
119
120 typedef struct ike_sa_t ike_sa_t;
121
122 /**
123 * @brief Class ike_sa_t representing an IKE_SA.
124 *
125 * An IKE_SA contains crypto information related to a connection
126 * with a peer. It contains multiple IPsec CHILD_SA, for which
127 * it is responsible. All traffic is handled by an IKE_SA, using
128 * transactions.
129 *
130 * @b Constructors:
131 * - ike_sa_create()
132 *
133 * @ingroup sa
134 */
135 struct ike_sa_t {
136
137 /**
138 * @brief Get the id of the SA.
139 *
140 * Returned ike_sa_id_t object is not getting cloned!
141 *
142 * @param this calling object
143 * @return ike_sa's ike_sa_id_t
144 */
145 ike_sa_id_t* (*get_id) (ike_sa_t *this);
146
147 /**
148 * @brief Get the state of the IKE_SA.
149 *
150 * @param this calling object
151 * @return state of the IKE_SA
152 */
153 ike_sa_state_t (*get_state) (ike_sa_t *this);
154
155 /**
156 * @brief Set the state of the IKE_SA.
157 *
158 * @param this calling object
159 * @param state state to set for the IKE_SA
160 */
161 void (*set_state) (ike_sa_t *this, ike_sa_state_t ike_sa);
162
163 /**
164 * @brief Get the name of the connection this IKE_SA uses.
165 *
166 * @param this calling object
167 * @return name
168 */
169 char* (*get_name) (ike_sa_t *this);
170
171 /**
172 * @brief Set the name of the connection this IKE_SA uses.
173 *
174 * @param this calling object
175 * @param name name, gets cloned
176 */
177 void (*set_name) (ike_sa_t *this, char* name);
178
179 /**
180 * @brief Get the own host address.
181 *
182 * @param this calling object
183 * @return host address
184 */
185 host_t* (*get_my_host) (ike_sa_t *this);
186
187 /**
188 * @brief Set the own host address.
189 *
190 * @param this calling object
191 * @param me host address
192 */
193 void (*set_my_host) (ike_sa_t *this, host_t *me);
194
195 /**
196 * @brief Get the other peers host address.
197 *
198 * @param this calling object
199 * @return host address
200 */
201 host_t* (*get_other_host) (ike_sa_t *this);
202
203 /**
204 * @brief Set the others host address.
205 *
206 * @param this calling object
207 * @param other host address
208 */
209 void (*set_other_host) (ike_sa_t *this, host_t *other);
210
211 /**
212 * @brief Get the own identification.
213 *
214 * @param this calling object
215 * @return identification
216 */
217 identification_t* (*get_my_id) (ike_sa_t *this);
218
219 /**
220 * @brief Set the own identification.
221 *
222 * @param this calling object
223 * @param me identification
224 */
225 void (*set_my_id) (ike_sa_t *this, identification_t *me);
226
227 /**
228 * @brief Get the other peers identification.
229 *
230 * @param this calling object
231 * @return identification
232 */
233 identification_t* (*get_other_id) (ike_sa_t *this);
234
235 /**
236 * @brief Set the other peers identification.
237 *
238 * @param this calling object
239 * @param other identification
240 */
241 void (*set_other_id) (ike_sa_t *this, identification_t *other);
242
243 /**
244 * @brief Initiate a new connection.
245 *
246 * The policy/connection is owned by the IKE_SA after the call, so
247 * do not modify or destroy it.
248 *
249 * @param this calling object
250 * @param connection connection to initiate
251 * @param policy policy to set up
252 * @return
253 * - SUCCESS if initialization started
254 * - DESTROY_ME if initialization failed and IKE_SA MUST be deleted
255 */
256 status_t (*initiate) (ike_sa_t *this, connection_t *connection, policy_t *policy);
257
258 /**
259 * @brief Route a policy in the kernel.
260 *
261 * Installs the policies in the kernel. If traffic matches,
262 * the kernel requests connection setup from the IKE_SA via acquire().
263 *
264 * @param this calling object
265 * @param connection connection definition used for routing
266 * @param policy policy to route
267 * @return
268 * - SUCCESS if routed successfully
269 * - FAILED if routing failed
270 */
271 status_t (*route) (ike_sa_t *this, connection_t *connection, policy_t *policy);
272
273 /**
274 * @brief Unroute a policy in the kernel previously routed.
275 *
276 * @param this calling object
277 * @param policy policy to route
278 * @return
279 * - SUCCESS if route removed
280 * - DESTROY_ME if last route was removed from
281 * an IKE_SA which was not established
282 */
283 status_t (*unroute) (ike_sa_t *this, policy_t *policy);
284
285 /**
286 * @brief Acquire connection setup for a policy.
287 *
288 * If an installed policy raises an acquire, the kernel calls
289 * this function to establish the CHILD_SA (and maybe the IKE_SA).
290 *
291 * @param this calling object
292 * @param reqid reqid of the CHILD_SA the policy belongs to.
293 * @return
294 * - SUCCESS if initialization started
295 * - DESTROY_ME if initialization failed and IKE_SA MUST be deleted
296 */
297 status_t (*acquire) (ike_sa_t *this, u_int32_t reqid);
298
299 /**
300 * @brief Initiates the deletion of an IKE_SA.
301 *
302 * Sends a delete message to the remote peer and waits for
303 * its response. If the response comes in, or a timeout occurs,
304 * the IKE SA gets deleted.
305 *
306 * @param this calling object
307 * @return
308 * - SUCCESS if deletion is initialized
309 * - INVALID_STATE, if the IKE_SA is not in
310 * an established state and can not be
311 * delete (but destroyed).
312 */
313 status_t (*delete) (ike_sa_t *this);
314
315 /**
316 * @brief Retransmits a request.
317 *
318 * @param this calling object
319 * @param message_id ID of the request to retransmit
320 * @return
321 * - SUCCESS
322 * - NOT_FOUND if request doesn't have to be retransmited
323 */
324 status_t (*retransmit_request) (ike_sa_t *this, u_int32_t message_id);
325
326 /**
327 * @brief Processes a incoming IKEv2-Message.
328 *
329 * Message processing may fail. If a critical failure occurs,
330 * process_message() return DESTROY_ME. Then the caller must
331 * destroy the IKE_SA immediatly, as it is unusable.
332 *
333 * @param this calling object
334 * @param[in] message message to process
335 * @return
336 * - SUCCESS
337 * - FAILED
338 * - DESTROY_ME if this IKE_SA MUST be deleted
339 */
340 status_t (*process_message) (ike_sa_t *this, message_t *message);
341
342 /**
343 * @brief Get the next message ID for a request.
344 *
345 * @param this calling object
346 * @return the next message id
347 */
348 u_int32_t (*get_next_message_id) (ike_sa_t *this);
349
350 /**
351 * @brief Check if NAT traversal is enabled for this IKE_SA.
352 *
353 * @param this calling object
354 * @return TRUE if NAT traversal enabled
355 */
356 bool (*is_natt_enabled) (ike_sa_t *this);
357
358 /**
359 * @brief Enable NAT detection for this IKE_SA.
360 *
361 * If a Network address translation is detected with
362 * NAT_DETECTION notifys, a SA must switch to ports
363 * 4500. To enable this behavior, call enable_natt().
364 * It is relevant which peer is NATted, this is specified
365 * with the "local" parameter. Call it twice when both
366 * are NATted.
367 *
368 * @param this calling object
369 * @param local TRUE, if we are NATted, FALSE if other
370 */
371 void (*enable_natt) (ike_sa_t *this, bool local);
372
373 /**
374 * @brief Apply connection parameters for this IKE_SA.
375 *
376 * @param this calling object
377 * @param connection connection definition
378 */
379 void (*apply_connection) (ike_sa_t *this, connection_t *connection);
380
381 /**
382 * @brief Sends a DPD request to the peer.
383 *
384 * To check if a peer is still alive, periodic
385 * empty INFORMATIONAL messages are sent if no
386 * other traffic was received.
387 *
388 * @param this calling object
389 * @return
390 * - SUCCESS
391 * - DESTROY_ME, if peer did not respond
392 */
393 status_t (*send_dpd) (ike_sa_t *this);
394
395 /**
396 * @brief Sends a keep alive packet.
397 *
398 * To refresh NAT tables in a NAT router
399 * between the peers, periodic empty
400 * UDP packets are sent if no other traffic
401 * was sent.
402 *
403 * @param this calling object
404 */
405 void (*send_keepalive) (ike_sa_t *this);
406
407 /**
408 * @brief Derive all keys and create the transforms for IKE communication.
409 *
410 * Keys are derived using the diffie hellman secret, nonces and internal
411 * stored SPIs.
412 * Key derivation differs when an IKE_SA is set up to replace an
413 * existing IKE_SA (rekeying). The SK_d key from the old IKE_SA
414 * is included in the derivation process.
415 *
416 * @param this calling object
417 * @param proposal proposal which contains algorithms to use
418 * @param dh diffie hellman object with shared secret
419 * @param nonce_i initiators nonce
420 * @param nonce_r responders nonce
421 * @param initiator TRUE if initiator, FALSE otherwise
422 * @param child_prf PRF with SK_d key when rekeying, NULL otherwise
423 * @param old_prf general purpose PRF of old SA when rekeying
424 */
425 status_t (*derive_keys)(ike_sa_t *this, proposal_t* proposal,
426 diffie_hellman_t *dh,
427 chunk_t nonce_i, chunk_t nonce_r,
428 bool initiator, prf_t *child_prf, prf_t *old_prf);
429
430 /**
431 * @brief Get the multi purpose prf.
432 *
433 * @param this calling object
434 * @return pointer to prf_t object
435 */
436 prf_t *(*get_prf) (ike_sa_t *this);
437
438 /**
439 * @brief Get the prf-object, which is used to derive keys for child SAs.
440 *
441 * @param this calling object
442 * @return pointer to prf_t object
443 */
444 prf_t *(*get_child_prf) (ike_sa_t *this);
445
446 /**
447 * @brief Get the prf used for authentication of initiator.
448 *
449 * @param this calling object
450 * @return pointer to prf_t object
451 */
452 prf_t *(*get_prf_auth_i) (ike_sa_t *this);
453
454 /**
455 * @brief Get the prf used for authentication of responder.
456 *
457 * @param this calling object
458 * @return pointer to prf_t object
459 */
460 prf_t *(*get_prf_auth_r) (ike_sa_t *this);
461
462 /**
463 * @brief Associates a child SA to this IKE SA
464 *
465 * @param this calling object
466 * @param child_sa child_sa to add
467 */
468 void (*add_child_sa) (ike_sa_t *this, child_sa_t *child_sa);
469
470 /**
471 * @brief Check if an IKE_SA has one or more CHILD_SAs with a given reqid.
472 *
473 * @param this calling object
474 * @param reqid reqid of the CHILD
475 * @return TRUE if it has such a CHILD, FALSE if not
476 */
477 bool (*has_child_sa) (ike_sa_t *this, u_int32_t reqid);
478
479 /**
480 * @brief Get a CHILD_SA identified by protocol and SPI.
481 *
482 * @param this calling object
483 * @param protocol protocol of the SA
484 * @param spi SPI of the CHILD_SA
485 * @param inbound TRUE if SPI is inbound, FALSE if outbound
486 * @return child_sa, or NULL if none found
487 */
488 child_sa_t* (*get_child_sa) (ike_sa_t *this, protocol_id_t protocol,
489 u_int32_t spi, bool inbound);
490
491 /**
492 * @brief Create an iterator over all CHILD_SAs.
493 *
494 * @param this calling object
495 * @return iterator
496 */
497 iterator_t* (*create_child_sa_iterator) (ike_sa_t *this);
498
499 /**
500 * @brief Rekey the CHILD SA with the specified reqid.
501 *
502 * Looks for a CHILD SA owned by this IKE_SA, and start the rekeing.
503 *
504 * @param this calling object
505 * @param protocol protocol of the SA
506 * @param spi inbound SPI of the CHILD_SA
507 * @return
508 * - NOT_FOUND, if IKE_SA has no such CHILD_SA
509 * - SUCCESS, if rekeying initiated
510 */
511 status_t (*rekey_child_sa) (ike_sa_t *this, protocol_id_t protocol, u_int32_t spi);
512
513 /**
514 * @brief Close the CHILD SA with the specified protocol/SPI.
515 *
516 * Looks for a CHILD SA owned by this IKE_SA, deletes it and
517 * notify's the remote peer about the delete. The associated
518 * states and policies in the kernel get deleted, if they exist.
519 *
520 * @param this calling object
521 * @param protocol protocol of the SA
522 * @param spi inbound SPI of the CHILD_SA
523 * @return
524 * - NOT_FOUND, if IKE_SA has no such CHILD_SA
525 * - SUCCESS, if delete message sent
526 */
527 status_t (*delete_child_sa) (ike_sa_t *this, protocol_id_t protocol, u_int32_t spi);
528
529 /**
530 * @brief Destroy a CHILD SA with the specified protocol/SPI.
531 *
532 * Looks for a CHILD SA owned by this IKE_SA and destroys it.
533 *
534 * @param this calling object
535 * @param protocol protocol of the SA
536 * @param spi inbound SPI of the CHILD_SA
537 * @return
538 * - NOT_FOUND, if IKE_SA has no such CHILD_SA
539 * - SUCCESS
540 */
541 status_t (*destroy_child_sa) (ike_sa_t *this, protocol_id_t protocol, u_int32_t spi);
542
543 /**
544 * @brief Set lifetimes of an IKE_SA.
545 *
546 * Two lifetimes are specified. The soft_lifetime says, when rekeying should
547 * be initiated. The hard_lifetime says, when the IKE_SA has been expired
548 * and must be deleted. Normally, hard_lifetime > soft_lifetime, and
549 * hard_lifetime is only reached when rekeying at soft_lifetime fails.
550 *
551 * @param this calling object
552 * @param soft_lifetime soft_lifetime
553 * @param hard_lifetime hard_lifetime
554 */
555 void (*set_lifetimes) (ike_sa_t *this,
556 u_int32_t soft_lifetime, u_int32_t hard_lifetime);
557
558 /**
559 * @brief Rekey the IKE_SA.
560 *
561 * Sets up a new IKE_SA, moves all CHILDs to it and deletes this IKE_SA.
562 *
563 * @param this calling object
564 * @return - SUCCESS, if IKE_SA rekeying initiated
565 */
566 status_t (*rekey) (ike_sa_t *this);
567
568 /**
569 * @brief Get the transaction which rekeys this IKE_SA.
570 *
571 * @todo Fix include for rekey_ike_sa.h
572 *
573 * @param this calling object
574 * @return rekey_ike_sa_t transaction or NULL
575 */
576 void* (*get_rekeying_transaction) (ike_sa_t *this);
577
578 /**
579 * @brief Set the transaction which rekeys this IKE_SA.
580 *
581 * @param this calling object
582 * @param rekey rekey_ike_sa_t transaction or NULL
583 */
584 void (*set_rekeying_transaction) (ike_sa_t *this, void *rekey);
585
586 /**
587 * @brief Move all children from other IKE_SA to this IKE_SA.
588 *
589 * After rekeying completes, all children are switched over to the
590 * newly created IKE_SA.
591 *
592 * @param this stepfather
593 * @param other deceased (rekeyed) IKE_SA
594 */
595 void (*adopt_children) (ike_sa_t *this, ike_sa_t *other);
596
597 /**
598 * @brief Destroys a ike_sa_t object.
599 *
600 * @param this calling object
601 */
602 void (*destroy) (ike_sa_t *this);
603 };
604
605 /**
606 * @brief Creates an ike_sa_t object with a specific ID.
607 *
608 * The ID gets cloned internally.
609 *
610 * @param ike_sa_id ike_sa_id_t object to associate with new IKE_SA
611 * @return ike_sa_t object
612 *
613 * @ingroup sa
614 */
615 ike_sa_t *ike_sa_create(ike_sa_id_t *ike_sa_id);
616
617 #endif /* IKE_SA_H_ */