cosmetics
[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 typedef enum ike_sa_state_t ike_sa_state_t;
29 typedef struct ike_sa_t ike_sa_t;
30
31 #include <library.h>
32 #include <encoding/message.h>
33 #include <encoding/payloads/proposal_substructure.h>
34 #include <sa/ike_sa_id.h>
35 #include <sa/child_sa.h>
36 #include <sa/tasks/task.h>
37 #include <utils/randomizer.h>
38 #include <crypto/prfs/prf.h>
39 #include <crypto/crypters/crypter.h>
40 #include <crypto/signers/signer.h>
41 #include <config/peer_cfg.h>
42 #include <config/ike_cfg.h>
43
44 /**
45 * Timeout in milliseconds after that a half open IKE_SA gets deleted.
46 *
47 * @ingroup sa
48 */
49 #define HALF_OPEN_IKE_SA_TIMEOUT 30000
50
51 /**
52 * Interval to send keepalives when NATed, in seconds.
53 *
54 * @ingroup sa
55 */
56 #define KEEPALIVE_INTERVAL 20
57
58 /**
59 * After which time rekeying should be retried if it failed, in seconds.
60 *
61 * @ingroup sa
62 */
63 #define RETRY_INTERVAL 30
64
65 /**
66 * Jitter to subtract from RETRY_INTERVAL to randomize rekey retry.
67 *
68 * @ingroup sa
69 */
70 #define RETRY_JITTER 20
71
72
73 /**
74 * @brief State of an IKE_SA.
75 *
76 * An IKE_SA passes various states in its lifetime. A newly created
77 * SA is in the state CREATED.
78 * @verbatim
79 +----------------+
80 ¦ SA_CREATED ¦
81 +----------------+
82 ¦
83 on initiate()---> ¦ <----- on IKE_SA_INIT received
84 V
85 +----------------+
86 ¦ SA_CONNECTING ¦
87 +----------------+
88 ¦
89 ¦ <----- on IKE_AUTH successfully completed
90 V
91 +----------------+
92 ¦ SA_ESTABLISHED ¦-------------------------+ <-- on rekeying
93 +----------------+ ¦
94 ¦ V
95 on delete()---> ¦ <----- on IKE_SA +-------------+
96 ¦ delete request ¦ SA_REKEYING ¦
97 ¦ received +-------------+
98 V ¦
99 +----------------+ ¦
100 ¦ SA_DELETING ¦<------------------------+ <-- after rekeying
101 +----------------+
102 ¦
103 ¦ <----- after delete() acknowledged
104 ¦
105 \V/
106 X
107 / \
108 @endverbatim
109 *
110 * @ingroup sa
111 */
112 enum ike_sa_state_t {
113
114 /**
115 * IKE_SA just got created, but is not initiating nor responding yet.
116 */
117 IKE_CREATED,
118
119 /**
120 * IKE_SA gets initiated actively or passively
121 */
122 IKE_CONNECTING,
123
124 /**
125 * IKE_SA is fully established
126 */
127 IKE_ESTABLISHED,
128
129 /**
130 * IKE_SA rekeying in progress
131 */
132 IKE_REKEYING,
133
134 /**
135 * IKE_SA is in progress of deletion
136 */
137 IKE_DELETING,
138 };
139
140 /**
141 * enum names for ike_sa_state_t.
142 */
143 extern enum_name_t *ike_sa_state_names;
144
145 /**
146 * @brief Class ike_sa_t representing an IKE_SA.
147 *
148 * An IKE_SA contains crypto information related to a connection
149 * with a peer. It contains multiple IPsec CHILD_SA, for which
150 * it is responsible. All traffic is handled by an IKE_SA, using
151 * the task manager and its tasks.
152 *
153 * @b Constructors:
154 * - ike_sa_create()
155 *
156 * @ingroup sa
157 */
158 struct ike_sa_t {
159
160 /**
161 * @brief Get the id of the SA.
162 *
163 * Returned ike_sa_id_t object is not getting cloned!
164 *
165 * @param this calling object
166 * @return ike_sa's ike_sa_id_t
167 */
168 ike_sa_id_t* (*get_id) (ike_sa_t *this);
169
170 /**
171 * @brief Get the numerical ID uniquely defining this IKE_SA.
172 *
173 * @param this calling object
174 * @return unique ID
175 */
176 u_int32_t (*get_unique_id) (ike_sa_t *this);
177
178 /**
179 * @brief Get the state of the IKE_SA.
180 *
181 * @param this calling object
182 * @return state of the IKE_SA
183 */
184 ike_sa_state_t (*get_state) (ike_sa_t *this);
185
186 /**
187 * @brief Get some statistics about this IKE_SA.
188 *
189 * @param next_rekeying when the next rekeying is scheduled
190 */
191 void (*get_stats)(ike_sa_t *this, u_int32_t *next_rekeying);
192
193 /**
194 * @brief Set the state of the IKE_SA.
195 *
196 * @param this calling object
197 * @param state state to set for the IKE_SA
198 */
199 void (*set_state) (ike_sa_t *this, ike_sa_state_t ike_sa);
200
201 /**
202 * @brief Get the name of the connection this IKE_SA uses.
203 *
204 * @param this calling object
205 * @return name
206 */
207 char* (*get_name) (ike_sa_t *this);
208
209 /**
210 * @brief Get the own host address.
211 *
212 * @param this calling object
213 * @return host address
214 */
215 host_t* (*get_my_host) (ike_sa_t *this);
216
217 /**
218 * @brief Set the own host address.
219 *
220 * @param this calling object
221 * @param me host address
222 */
223 void (*set_my_host) (ike_sa_t *this, host_t *me);
224
225 /**
226 * @brief Get the other peers host address.
227 *
228 * @param this calling object
229 * @return host address
230 */
231 host_t* (*get_other_host) (ike_sa_t *this);
232
233 /**
234 * @brief Set the others host address.
235 *
236 * @param this calling object
237 * @param other host address
238 */
239 void (*set_other_host) (ike_sa_t *this, host_t *other);
240
241 /**
242 * @brief Get the own identification.
243 *
244 * @param this calling object
245 * @return identification
246 */
247 identification_t* (*get_my_id) (ike_sa_t *this);
248
249 /**
250 * @brief Set the own identification.
251 *
252 * @param this calling object
253 * @param me identification
254 */
255 void (*set_my_id) (ike_sa_t *this, identification_t *me);
256
257 /**
258 * @brief Get the other peers identification.
259 *
260 * @param this calling object
261 * @return identification
262 */
263 identification_t* (*get_other_id) (ike_sa_t *this);
264
265 /**
266 * @brief Set the other peers identification.
267 *
268 * @param this calling object
269 * @param other identification
270 */
271 void (*set_other_id) (ike_sa_t *this, identification_t *other);
272
273 /**
274 * @brief Get the config used to setup this IKE_SA.
275 *
276 * @param this calling object
277 * @return ike_config
278 */
279 ike_cfg_t* (*get_ike_cfg) (ike_sa_t *this);
280
281 /**
282 * @brief Set the config to setup this IKE_SA.
283 *
284 * @param this calling object
285 * @param config ike_config to use
286 */
287 void (*set_ike_cfg) (ike_sa_t *this, ike_cfg_t* config);
288
289 /**
290 * @brief Get the peer config used by this IKE_SA.
291 *
292 * @param this calling object
293 * @return peer_config
294 */
295 peer_cfg_t* (*get_peer_cfg) (ike_sa_t *this);
296
297 /**
298 * @brief Set the peer config to use with this IKE_SA.
299 *
300 * @param this calling object
301 * @param config peer_config to use
302 */
303 void (*set_peer_cfg) (ike_sa_t *this, peer_cfg_t *config);
304
305 /**
306 * @brief Initiate a new connection.
307 *
308 * The configs are owned by the IKE_SA after the call.
309 *
310 * @param this calling object
311 * @param child_cfg child config to create CHILD from
312 * @return
313 * - SUCCESS if initialization started
314 * - DESTROY_ME if initialization failed
315 */
316 status_t (*initiate) (ike_sa_t *this, child_cfg_t *child_cfg);
317
318 /**
319 * @brief Route a policy in the kernel.
320 *
321 * Installs the policies in the kernel. If traffic matches,
322 * the kernel requests connection setup from the IKE_SA via acquire().
323 *
324 * @param this calling object
325 * @param child_cfg child config to route
326 * @return
327 * - SUCCESS if routed successfully
328 * - FAILED if routing failed
329 */
330 status_t (*route) (ike_sa_t *this, child_cfg_t *child_cfg);
331
332 /**
333 * @brief Unroute a policy in the kernel previously routed.
334 *
335 * @param this calling object
336 * @param child_cfg child config to unroute
337 * @return
338 * - SUCCESS if route removed
339 * - DESTROY_ME if last CHILD_SA was unrouted
340 */
341 status_t (*unroute) (ike_sa_t *this, child_cfg_t *child_cfg);
342
343 /**
344 * @brief Acquire connection setup for an installed kernel policy.
345 *
346 * If an installed policy raises an acquire, the kernel calls
347 * this function to establish the CHILD_SA (and maybe the IKE_SA).
348 *
349 * @param this calling object
350 * @param reqid reqid of the CHILD_SA the policy belongs to.
351 * @return
352 * - SUCCESS if initialization started
353 * - DESTROY_ME if initialization failed
354 */
355 status_t (*acquire) (ike_sa_t *this, u_int32_t reqid);
356
357 /**
358 * @brief Initiates the deletion of an IKE_SA.
359 *
360 * Sends a delete message to the remote peer and waits for
361 * its response. If the response comes in, or a timeout occurs,
362 * the IKE SA gets deleted.
363 *
364 * @param this calling object
365 * @return
366 * - SUCCESS if deletion is initialized
367 * - INVALID_STATE, if the IKE_SA is not in
368 * an established state and can not be
369 * delete (but destroyed).
370 */
371 status_t (*delete) (ike_sa_t *this);
372
373 /**
374 * @brief Processes a incoming IKEv2-Message.
375 *
376 * Message processing may fail. If a critical failure occurs,
377 * process_message() return DESTROY_ME. Then the caller must
378 * destroy the IKE_SA immediatly, as it is unusable.
379 *
380 * @param this calling object
381 * @param message message to process
382 * @return
383 * - SUCCESS
384 * - FAILED
385 * - DESTROY_ME if this IKE_SA MUST be deleted
386 */
387 status_t (*process_message) (ike_sa_t *this, message_t *message);
388
389 /**
390 * @brief Generate a IKE message to send it to the peer.
391 *
392 * This method generates all payloads in the message and encrypts/signs
393 * the packet.
394 *
395 * @param this calling object
396 * @param message message to generate
397 * @param packet generated output packet
398 * @return
399 * - SUCCESS
400 * - FAILED
401 * - DESTROY_ME if this IKE_SA MUST be deleted
402 */
403 status_t (*generate_message) (ike_sa_t *this, message_t *message,
404 packet_t **packet);
405
406 /**
407 * @brief Retransmits a request.
408 *
409 * @param this calling object
410 * @param message_id ID of the request to retransmit
411 * @return
412 * - SUCCESS
413 * - NOT_FOUND if request doesn't have to be retransmited
414 */
415 status_t (*retransmit) (ike_sa_t *this, u_int32_t message_id);
416
417 /**
418 * @brief Sends a DPD request to the peer.
419 *
420 * To check if a peer is still alive, periodic
421 * empty INFORMATIONAL messages are sent if no
422 * other traffic was received.
423 *
424 * @param this calling object
425 * @return
426 * - SUCCESS
427 * - DESTROY_ME, if peer did not respond
428 */
429 status_t (*send_dpd) (ike_sa_t *this);
430
431 /**
432 * @brief Sends a keep alive packet.
433 *
434 * To refresh NAT tables in a NAT router
435 * between the peers, periodic empty
436 * UDP packets are sent if no other traffic
437 * was sent.
438 *
439 * @param this calling object
440 */
441 void (*send_keepalive) (ike_sa_t *this);
442
443 /**
444 * @brief Check if NAT traversal is enabled for this IKE_SA.
445 *
446 * @param this calling object
447 * @return TRUE if NAT traversal enabled
448 */
449 bool (*is_natt_enabled) (ike_sa_t *this);
450
451 /**
452 * @brief Enable NAT detection for this IKE_SA.
453 *
454 * If a Network address translation is detected with
455 * NAT_DETECTION notifys, a SA must switch to ports
456 * 4500. To enable this behavior, call enable_natt().
457 * It is relevant which peer is NATted, this is specified
458 * with the "local" parameter. Call it twice when both
459 * are NATted.
460 *
461 * @param this calling object
462 * @param local TRUE, if we are NATted, FALSE if other
463 */
464 void (*enable_natt) (ike_sa_t *this, bool local);
465
466 /**
467 * @brief Derive all keys and create the transforms for IKE communication.
468 *
469 * Keys are derived using the diffie hellman secret, nonces and internal
470 * stored SPIs.
471 * Key derivation differs when an IKE_SA is set up to replace an
472 * existing IKE_SA (rekeying). The SK_d key from the old IKE_SA
473 * is included in the derivation process.
474 *
475 * @param this calling object
476 * @param proposal proposal which contains algorithms to use
477 * @param secret secret derived from DH exchange, gets freed
478 * @param nonce_i initiators nonce
479 * @param nonce_r responders nonce
480 * @param initiator TRUE if initiator, FALSE otherwise
481 * @param child_prf PRF with SK_d key when rekeying, NULL otherwise
482 * @param old_prf general purpose PRF of old SA when rekeying
483 */
484 status_t (*derive_keys)(ike_sa_t *this, proposal_t* proposal, chunk_t secret,
485 chunk_t nonce_i, chunk_t nonce_r,
486 bool initiator, prf_t *child_prf, prf_t *old_prf);
487
488 /**
489 * @brief Get a multi purpose prf for the negotiated PRF function.
490 *
491 * @param this calling object
492 * @return pointer to prf_t object
493 */
494 prf_t *(*get_prf) (ike_sa_t *this);
495
496 /**
497 * @brief Get the prf-object, which is used to derive keys for child SAs.
498 *
499 * @param this calling object
500 * @return pointer to prf_t object
501 */
502 prf_t *(*get_child_prf) (ike_sa_t *this);
503
504 /**
505 * @brief Get the key to build outgoing authentication data.
506 *
507 * @param this calling object
508 * @return pointer to prf_t object
509 */
510 chunk_t (*get_skp_build) (ike_sa_t *this);
511
512 /**
513 * @brief Get the key to verify incoming authentication data.
514 *
515 * @param this calling object
516 * @return pointer to prf_t object
517 */
518 chunk_t (*get_skp_verify) (ike_sa_t *this);
519
520 /**
521 * @brief Associates a child SA to this IKE SA
522 *
523 * @param this calling object
524 * @param child_sa child_sa to add
525 */
526 void (*add_child_sa) (ike_sa_t *this, child_sa_t *child_sa);
527
528 /**
529 * @brief Get a CHILD_SA identified by protocol and SPI.
530 *
531 * @param this calling object
532 * @param protocol protocol of the SA
533 * @param spi SPI of the CHILD_SA
534 * @param inbound TRUE if SPI is inbound, FALSE if outbound
535 * @return child_sa, or NULL if none found
536 */
537 child_sa_t* (*get_child_sa) (ike_sa_t *this, protocol_id_t protocol,
538 u_int32_t spi, bool inbound);
539
540 /**
541 * @brief Create an iterator over all CHILD_SAs.
542 *
543 * @param this calling object
544 * @return iterator
545 */
546 iterator_t* (*create_child_sa_iterator) (ike_sa_t *this);
547
548 /**
549 * @brief Rekey the CHILD SA with the specified reqid.
550 *
551 * Looks for a CHILD SA owned by this IKE_SA, and start the rekeing.
552 *
553 * @param this calling object
554 * @param protocol protocol of the SA
555 * @param spi inbound SPI of the CHILD_SA
556 * @return
557 * - NOT_FOUND, if IKE_SA has no such CHILD_SA
558 * - SUCCESS, if rekeying initiated
559 */
560 status_t (*rekey_child_sa) (ike_sa_t *this, protocol_id_t protocol, u_int32_t spi);
561
562 /**
563 * @brief Close the CHILD SA with the specified protocol/SPI.
564 *
565 * Looks for a CHILD SA owned by this IKE_SA, deletes it and
566 * notify's the remote peer about the delete. The associated
567 * states and policies in the kernel get deleted, if they exist.
568 *
569 * @param this calling object
570 * @param protocol protocol of the SA
571 * @param spi inbound SPI of the CHILD_SA
572 * @return
573 * - NOT_FOUND, if IKE_SA has no such CHILD_SA
574 * - SUCCESS, if delete message sent
575 */
576 status_t (*delete_child_sa) (ike_sa_t *this, protocol_id_t protocol, u_int32_t spi);
577
578 /**
579 * @brief Destroy a CHILD SA with the specified protocol/SPI.
580 *
581 * Looks for a CHILD SA owned by this IKE_SA and destroys it.
582 *
583 * @param this calling object
584 * @param protocol protocol of the SA
585 * @param spi inbound SPI of the CHILD_SA
586 * @return
587 * - NOT_FOUND, if IKE_SA has no such CHILD_SA
588 * - SUCCESS
589 */
590 status_t (*destroy_child_sa) (ike_sa_t *this, protocol_id_t protocol, u_int32_t spi);
591
592 /**
593 * @brief Rekey the IKE_SA.
594 *
595 * Sets up a new IKE_SA, moves all CHILDs to it and deletes this IKE_SA.
596 *
597 * @param this calling object
598 * @return - SUCCESS, if IKE_SA rekeying initiated
599 */
600 status_t (*rekey) (ike_sa_t *this);
601
602 /**
603 * @brief Restablish the IKE_SA.
604 *
605 * Create a completely new IKE_SA with authentication, recreates all children
606 * within the IKE_SA, but lets the old IKE_SA untouched.
607 *
608 * @param this calling object
609 */
610 void (*reestablish) (ike_sa_t *this);
611
612 /**
613 * @brief Set the virtual IP to use for this IKE_SA and its children.
614 *
615 * The virtual IP is assigned per IKE_SA, not per CHILD_SA. It has the same
616 * lifetime as the IKE_SA.
617 *
618 * @param this calling object
619 */
620 void (*set_virtual_ip) (ike_sa_t *this, bool local, host_t *ip);
621
622 /**
623 * @brief Get the virtual IP configured.
624 *
625 * @param this calling object
626 * @param local TRUE to get local virtual IP, FALSE for remote
627 */
628 host_t* (*get_virtual_ip) (ike_sa_t *this, bool local);
629
630 /**
631 * @brief Add a DNS server to the system.
632 *
633 * An IRAS may send a DNS server. To use it, it is installed on the
634 * system. The DNS entry has a lifetime until the IKE_SA gets closed.
635 *
636 * @param this calling object
637 * @param dns DNS server to install on the system
638 */
639 void (*add_dns_server) (ike_sa_t *this, host_t *dns);
640
641 /**
642 * @brief Inherit all attributes of other to this after rekeying.
643 *
644 * When rekeying is completed, all CHILD_SAs, the virtual IP and all
645 * outstanding tasks are moved from other to this.
646 * As this call may initiate inherited tasks, a status is returned.
647 *
648 * @param this calling object
649 * @param other other task to inherit from
650 * @return DESTROY_ME if initiation of inherited task failed
651 */
652 status_t (*inherit) (ike_sa_t *this, ike_sa_t *other);
653
654 /**
655 * @brief Reset the IKE_SA, useable when initiating fails
656 *
657 * @param this calling object
658 */
659 void (*reset) (ike_sa_t *this);
660
661 /**
662 * @brief Destroys a ike_sa_t object.
663 *
664 * @param this calling object
665 */
666 void (*destroy) (ike_sa_t *this);
667 };
668
669 /**
670 * @brief Creates an ike_sa_t object with a specific ID.
671 *
672 * @param ike_sa_id ike_sa_id_t object to associate with new IKE_SA
673 * @return ike_sa_t object
674 *
675 * @ingroup sa
676 */
677 ike_sa_t *ike_sa_create(ike_sa_id_t *ike_sa_id);
678
679 #endif /* IKE_SA_H_ */