fixed dpd for responder
[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/logger.h>
35 #include <utils/randomizer.h>
36 #include <crypto/prfs/prf.h>
37 #include <crypto/crypters/crypter.h>
38 #include <crypto/signers/signer.h>
39 #include <config/connections/connection.h>
40 #include <config/policies/policy.h>
41 #include <config/proposal.h>
42 #include <utils/logger.h>
43
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 * String mappings for ike_sa_state_t.
116 */
117 extern mapping_t ike_sa_state_m[];
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 Set the delay before starting a DPD check in case of inactivity.
375 *
376 * @param this calling object
377 * @param delay dpd delay in seconds
378 */
379 void (*set_dpd_delay) (ike_sa_t *this, u_int32_t delay);
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 Log the status of a the ike sa to a logger.
409 *
410 * The status of the IKE SA and all child SAs is logged.
411 * Supplying NULL as logger uses the internal child_sa logger
412 * to do the logging. The log is only done if the supplied
413 * connection name is NULL or matches the connections name.
414 *
415 * @param this calling object
416 * @param logger logger to use for logging
417 * @param name name of the connection
418 */
419 void (*log_status) (ike_sa_t *this, logger_t *logger, char *name);
420
421 /**
422 * @brief Derive all keys and create the transforms for IKE communication.
423 *
424 * Keys are derived using the diffie hellman secret, nonces and internal
425 * stored SPIs.
426 * Key derivation differs when an IKE_SA is set up to replace an
427 * existing IKE_SA (rekeying). The SK_d key from the old IKE_SA
428 * is included in the derivation process.
429 *
430 * @param this calling object
431 * @param proposal proposal which contains algorithms to use
432 * @param dh diffie hellman object with shared secret
433 * @param nonce_i initiators nonce
434 * @param nonce_r responders nonce
435 * @param initiator TRUE if initiator, FALSE otherwise
436 * @param child_prf PRF with SK_d key when rekeying, NULL otherwise
437 * @param old_prf general purpose PRF of old SA when rekeying
438 */
439 status_t (*derive_keys)(ike_sa_t *this, proposal_t* proposal,
440 diffie_hellman_t *dh,
441 chunk_t nonce_i, chunk_t nonce_r,
442 bool initiator, prf_t *child_prf, prf_t *old_prf);
443
444 /**
445 * @brief Get the multi purpose prf.
446 *
447 * @param this calling object
448 * @return pointer to prf_t object
449 */
450 prf_t *(*get_prf) (ike_sa_t *this);
451
452 /**
453 * @brief Get the prf-object, which is used to derive keys for child SAs.
454 *
455 * @param this calling object
456 * @return pointer to prf_t object
457 */
458 prf_t *(*get_child_prf) (ike_sa_t *this);
459
460 /**
461 * @brief Get the prf used for authentication of initiator.
462 *
463 * @param this calling object
464 * @return pointer to prf_t object
465 */
466 prf_t *(*get_prf_auth_i) (ike_sa_t *this);
467
468 /**
469 * @brief Get the prf used for authentication of responder.
470 *
471 * @param this calling object
472 * @return pointer to prf_t object
473 */
474 prf_t *(*get_prf_auth_r) (ike_sa_t *this);
475
476 /**
477 * @brief Associates a child SA to this IKE SA
478 *
479 * @param this calling object
480 * @param child_sa child_sa to add
481 */
482 void (*add_child_sa) (ike_sa_t *this, child_sa_t *child_sa);
483
484 /**
485 * @brief Check if an IKE_SA has one or more CHILD_SAs with a given reqid.
486 *
487 * @param this calling object
488 * @param reqid reqid of the CHILD
489 * @return TRUE if it has such a CHILD, FALSE if not
490 */
491 bool (*has_child_sa) (ike_sa_t *this, u_int32_t reqid);
492
493 /**
494 * @brief Get a CHILD_SA identified by protocol and SPI.
495 *
496 * @param this calling object
497 * @param protocol protocol of the SA
498 * @param spi SPI of the CHILD_SA
499 * @param inbound TRUE if SPI is inbound, FALSE if outbound
500 * @return child_sa, or NULL if none found
501 */
502 child_sa_t* (*get_child_sa) (ike_sa_t *this, protocol_id_t protocol,
503 u_int32_t spi, bool inbound);
504
505 /**
506 * @brief Create an iterator over all CHILD_SAs.
507 *
508 * @param this calling object
509 * @return iterator
510 */
511 iterator_t* (*create_child_sa_iterator) (ike_sa_t *this);
512
513 /**
514 * @brief Rekey the CHILD SA with the specified reqid.
515 *
516 * Looks for a CHILD SA owned by this IKE_SA, and start the rekeing.
517 *
518 * @param this calling object
519 * @param protocol protocol of the SA
520 * @param spi inbound SPI of the CHILD_SA
521 * @return
522 * - NOT_FOUND, if IKE_SA has no such CHILD_SA
523 * - SUCCESS, if rekeying initiated
524 */
525 status_t (*rekey_child_sa) (ike_sa_t *this, protocol_id_t protocol, u_int32_t spi);
526
527 /**
528 * @brief Close the CHILD SA with the specified protocol/SPI.
529 *
530 * Looks for a CHILD SA owned by this IKE_SA, deletes it and
531 * notify's the remote peer about the delete. The associated
532 * states and policies in the kernel get deleted, if they exist.
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, if delete message sent
540 */
541 status_t (*delete_child_sa) (ike_sa_t *this, protocol_id_t protocol, u_int32_t spi);
542
543 /**
544 * @brief Destroy a CHILD SA with the specified protocol/SPI.
545 *
546 * Looks for a CHILD SA owned by this IKE_SA and destroys it.
547 *
548 * @param this calling object
549 * @param protocol protocol of the SA
550 * @param spi inbound SPI of the CHILD_SA
551 * @return
552 * - NOT_FOUND, if IKE_SA has no such CHILD_SA
553 * - SUCCESS
554 */
555 status_t (*destroy_child_sa) (ike_sa_t *this, protocol_id_t protocol, u_int32_t spi);
556
557 /**
558 * @brief Set lifetimes of an IKE_SA.
559 *
560 * Two lifetimes are specified. The soft_lifetime says, when rekeying should
561 * be initiated. The hard_lifetime says, when the IKE_SA has been expired
562 * and must be deleted. Normally, hard_lifetime > soft_lifetime, and
563 * hard_lifetime is only reached when rekeying at soft_lifetime fails.
564 *
565 * @param this calling object
566 * @param soft_lifetime soft_lifetime
567 * @param hard_lifetime hard_lifetime
568 */
569 void (*set_lifetimes) (ike_sa_t *this,
570 u_int32_t soft_lifetime, u_int32_t hard_lifetime);
571
572 /**
573 * @brief Rekey the IKE_SA.
574 *
575 * Sets up a new IKE_SA, moves all CHILDs to it and deletes this IKE_SA.
576 *
577 * @param this calling object
578 * @return - SUCCESS, if IKE_SA rekeying initiated
579 */
580 status_t (*rekey) (ike_sa_t *this);
581
582 /**
583 * @brief Get the transaction which rekeys this IKE_SA.
584 *
585 * @todo Fix include for rekey_ike_sa.h
586 *
587 * @param this calling object
588 * @return rekey_ike_sa_t transaction or NULL
589 */
590 void* (*get_rekeying_transaction) (ike_sa_t *this);
591
592 /**
593 * @brief Set the transaction which rekeys this IKE_SA.
594 *
595 * @param this calling object
596 * @param rekey rekey_ike_sa_t transaction or NULL
597 */
598 void (*set_rekeying_transaction) (ike_sa_t *this, void *rekey);
599
600 /**
601 * @brief Move all children from other IKE_SA to this IKE_SA.
602 *
603 * After rekeying completes, all children are switched over to the
604 * newly created IKE_SA.
605 *
606 * @param this stepfather
607 * @param other deceased (rekeyed) IKE_SA
608 */
609 void (*adopt_children) (ike_sa_t *this, ike_sa_t *other);
610
611 /**
612 * @brief Destroys a ike_sa_t object.
613 *
614 * @param this calling object
615 */
616 void (*destroy) (ike_sa_t *this);
617 };
618
619 /**
620 * @brief Creates an ike_sa_t object with a specific ID.
621 *
622 * The ID gets cloned internally.
623 *
624 * @param ike_sa_id ike_sa_id_t object to associate with new IKE_SA
625 * @return ike_sa_t object
626 *
627 * @ingroup sa
628 */
629 ike_sa_t *ike_sa_create(ike_sa_id_t *ike_sa_id);
630
631 #endif /* IKE_SA_H_ */