0f015286e30b7bdf2625fbeb22fe2925cc01b0ed
[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 reqid reqid of CHILD_SA to unroute
337 * @return
338 * - SUCCESS if route removed
339 * - NOT_FOUND if CHILD_SA not found
340 * - DESTROY_ME if last CHILD_SA was unrouted
341 */
342 status_t (*unroute) (ike_sa_t *this, u_int32_t reqid);
343
344 /**
345 * @brief Acquire connection setup for an installed kernel policy.
346 *
347 * If an installed policy raises an acquire, the kernel calls
348 * this function to establish the CHILD_SA (and maybe the IKE_SA).
349 *
350 * @param this calling object
351 * @param reqid reqid of the CHILD_SA the policy belongs to.
352 * @return
353 * - SUCCESS if initialization started
354 * - DESTROY_ME if initialization failed
355 */
356 status_t (*acquire) (ike_sa_t *this, u_int32_t reqid);
357
358 /**
359 * @brief Initiates the deletion of an IKE_SA.
360 *
361 * Sends a delete message to the remote peer and waits for
362 * its response. If the response comes in, or a timeout occurs,
363 * the IKE SA gets deleted.
364 *
365 * @param this calling object
366 * @return
367 * - SUCCESS if deletion is initialized
368 * - INVALID_STATE, if the IKE_SA is not in
369 * an established state and can not be
370 * delete (but destroyed).
371 */
372 status_t (*delete) (ike_sa_t *this);
373
374 /**
375 * @brief Processes a incoming IKEv2-Message.
376 *
377 * Message processing may fail. If a critical failure occurs,
378 * process_message() return DESTROY_ME. Then the caller must
379 * destroy the IKE_SA immediatly, as it is unusable.
380 *
381 * @param this calling object
382 * @param message message to process
383 * @return
384 * - SUCCESS
385 * - FAILED
386 * - DESTROY_ME if this IKE_SA MUST be deleted
387 */
388 status_t (*process_message) (ike_sa_t *this, message_t *message);
389
390 /**
391 * @brief Generate a IKE message to send it to the peer.
392 *
393 * This method generates all payloads in the message and encrypts/signs
394 * the packet.
395 *
396 * @param this calling object
397 * @param message message to generate
398 * @param packet generated output packet
399 * @return
400 * - SUCCESS
401 * - FAILED
402 * - DESTROY_ME if this IKE_SA MUST be deleted
403 */
404 status_t (*generate_message) (ike_sa_t *this, message_t *message,
405 packet_t **packet);
406
407 /**
408 * @brief Retransmits a request.
409 *
410 * @param this calling object
411 * @param message_id ID of the request to retransmit
412 * @return
413 * - SUCCESS
414 * - NOT_FOUND if request doesn't have to be retransmited
415 */
416 status_t (*retransmit) (ike_sa_t *this, u_int32_t message_id);
417
418 /**
419 * @brief Sends a DPD request to the peer.
420 *
421 * To check if a peer is still alive, periodic
422 * empty INFORMATIONAL messages are sent if no
423 * other traffic was received.
424 *
425 * @param this calling object
426 * @return
427 * - SUCCESS
428 * - DESTROY_ME, if peer did not respond
429 */
430 status_t (*send_dpd) (ike_sa_t *this);
431
432 /**
433 * @brief Sends a keep alive packet.
434 *
435 * To refresh NAT tables in a NAT router
436 * between the peers, periodic empty
437 * UDP packets are sent if no other traffic
438 * was sent.
439 *
440 * @param this calling object
441 */
442 void (*send_keepalive) (ike_sa_t *this);
443
444 /**
445 * @brief Check if NAT traversal is enabled for this IKE_SA.
446 *
447 * @param this calling object
448 * @return TRUE if NAT traversal enabled
449 */
450 bool (*is_natt_enabled) (ike_sa_t *this);
451
452 /**
453 * @brief Enable NAT detection for this IKE_SA.
454 *
455 * If a Network address translation is detected with
456 * NAT_DETECTION notifys, a SA must switch to ports
457 * 4500. To enable this behavior, call enable_natt().
458 * It is relevant which peer is NATted, this is specified
459 * with the "local" parameter. Call it twice when both
460 * are NATted.
461 *
462 * @param this calling object
463 * @param local TRUE, if we are NATted, FALSE if other
464 */
465 void (*enable_natt) (ike_sa_t *this, bool local);
466
467 /**
468 * @brief Derive all keys and create the transforms for IKE communication.
469 *
470 * Keys are derived using the diffie hellman secret, nonces and internal
471 * stored SPIs.
472 * Key derivation differs when an IKE_SA is set up to replace an
473 * existing IKE_SA (rekeying). The SK_d key from the old IKE_SA
474 * is included in the derivation process.
475 *
476 * @param this calling object
477 * @param proposal proposal which contains algorithms to use
478 * @param secret secret derived from DH exchange, gets freed
479 * @param nonce_i initiators nonce
480 * @param nonce_r responders nonce
481 * @param initiator TRUE if initiator, FALSE otherwise
482 * @param child_prf PRF with SK_d key when rekeying, NULL otherwise
483 * @param old_prf general purpose PRF of old SA when rekeying
484 */
485 status_t (*derive_keys)(ike_sa_t *this, proposal_t* proposal, chunk_t secret,
486 chunk_t nonce_i, chunk_t nonce_r,
487 bool initiator, prf_t *child_prf, prf_t *old_prf);
488
489 /**
490 * @brief Get a multi purpose prf for the negotiated PRF function.
491 *
492 * @param this calling object
493 * @return pointer to prf_t object
494 */
495 prf_t *(*get_prf) (ike_sa_t *this);
496
497 /**
498 * @brief Get the prf-object, which is used to derive keys for child SAs.
499 *
500 * @param this calling object
501 * @return pointer to prf_t object
502 */
503 prf_t *(*get_child_prf) (ike_sa_t *this);
504
505 /**
506 * @brief Get the key to build outgoing authentication data.
507 *
508 * @param this calling object
509 * @return pointer to prf_t object
510 */
511 chunk_t (*get_skp_build) (ike_sa_t *this);
512
513 /**
514 * @brief Get the key to verify incoming authentication data.
515 *
516 * @param this calling object
517 * @return pointer to prf_t object
518 */
519 chunk_t (*get_skp_verify) (ike_sa_t *this);
520
521 /**
522 * @brief Associates a child SA to this IKE SA
523 *
524 * @param this calling object
525 * @param child_sa child_sa to add
526 */
527 void (*add_child_sa) (ike_sa_t *this, child_sa_t *child_sa);
528
529 /**
530 * @brief Get a CHILD_SA identified by protocol and SPI.
531 *
532 * @param this calling object
533 * @param protocol protocol of the SA
534 * @param spi SPI of the CHILD_SA
535 * @param inbound TRUE if SPI is inbound, FALSE if outbound
536 * @return child_sa, or NULL if none found
537 */
538 child_sa_t* (*get_child_sa) (ike_sa_t *this, protocol_id_t protocol,
539 u_int32_t spi, bool inbound);
540
541 /**
542 * @brief Create an iterator over all CHILD_SAs.
543 *
544 * @param this calling object
545 * @return iterator
546 */
547 iterator_t* (*create_child_sa_iterator) (ike_sa_t *this);
548
549 /**
550 * @brief Rekey the CHILD SA with the specified reqid.
551 *
552 * Looks for a CHILD SA owned by this IKE_SA, and start the rekeing.
553 *
554 * @param this calling object
555 * @param protocol protocol of the SA
556 * @param spi inbound SPI of the CHILD_SA
557 * @return
558 * - NOT_FOUND, if IKE_SA has no such CHILD_SA
559 * - SUCCESS, if rekeying initiated
560 */
561 status_t (*rekey_child_sa) (ike_sa_t *this, protocol_id_t protocol, u_int32_t spi);
562
563 /**
564 * @brief Close the CHILD SA with the specified protocol/SPI.
565 *
566 * Looks for a CHILD SA owned by this IKE_SA, deletes it and
567 * notify's the remote peer about the delete. The associated
568 * states and policies in the kernel get deleted, if they exist.
569 *
570 * @param this calling object
571 * @param protocol protocol of the SA
572 * @param spi inbound SPI of the CHILD_SA
573 * @return
574 * - NOT_FOUND, if IKE_SA has no such CHILD_SA
575 * - SUCCESS, if delete message sent
576 */
577 status_t (*delete_child_sa) (ike_sa_t *this, protocol_id_t protocol, u_int32_t spi);
578
579 /**
580 * @brief Destroy a CHILD SA with the specified protocol/SPI.
581 *
582 * Looks for a CHILD SA owned by this IKE_SA and destroys it.
583 *
584 * @param this calling object
585 * @param protocol protocol of the SA
586 * @param spi inbound SPI of the CHILD_SA
587 * @return
588 * - NOT_FOUND, if IKE_SA has no such CHILD_SA
589 * - SUCCESS
590 */
591 status_t (*destroy_child_sa) (ike_sa_t *this, protocol_id_t protocol, u_int32_t spi);
592
593 /**
594 * @brief Rekey the IKE_SA.
595 *
596 * Sets up a new IKE_SA, moves all CHILDs to it and deletes this IKE_SA.
597 *
598 * @param this calling object
599 * @return - SUCCESS, if IKE_SA rekeying initiated
600 */
601 status_t (*rekey) (ike_sa_t *this);
602
603 /**
604 * @brief Restablish the IKE_SA.
605 *
606 * Create a completely new IKE_SA with authentication, recreates all children
607 * within the IKE_SA, but lets the old IKE_SA untouched.
608 *
609 * @param this calling object
610 */
611 void (*reestablish) (ike_sa_t *this);
612
613 /**
614 * @brief Set the virtual IP to use for this IKE_SA and its children.
615 *
616 * The virtual IP is assigned per IKE_SA, not per CHILD_SA. It has the same
617 * lifetime as the IKE_SA.
618 *
619 * @param this calling object
620 */
621 void (*set_virtual_ip) (ike_sa_t *this, bool local, host_t *ip);
622
623 /**
624 * @brief Get the virtual IP configured.
625 *
626 * @param this calling object
627 * @param local TRUE to get local virtual IP, FALSE for remote
628 */
629 host_t* (*get_virtual_ip) (ike_sa_t *this, bool local);
630
631 /**
632 * @brief Add a DNS server to the system.
633 *
634 * An IRAS may send a DNS server. To use it, it is installed on the
635 * system. The DNS entry has a lifetime until the IKE_SA gets closed.
636 *
637 * @param this calling object
638 * @param dns DNS server to install on the system
639 */
640 void (*add_dns_server) (ike_sa_t *this, host_t *dns);
641
642 /**
643 * @brief Inherit all attributes of other to this after rekeying.
644 *
645 * When rekeying is completed, all CHILD_SAs, the virtual IP and all
646 * outstanding tasks are moved from other to this.
647 * As this call may initiate inherited tasks, a status is returned.
648 *
649 * @param this calling object
650 * @param other other task to inherit from
651 * @return DESTROY_ME if initiation of inherited task failed
652 */
653 status_t (*inherit) (ike_sa_t *this, ike_sa_t *other);
654
655 /**
656 * @brief Reset the IKE_SA, useable when initiating fails
657 *
658 * @param this calling object
659 */
660 void (*reset) (ike_sa_t *this);
661
662 /**
663 * @brief Destroys a ike_sa_t object.
664 *
665 * @param this calling object
666 */
667 void (*destroy) (ike_sa_t *this);
668 };
669
670 /**
671 * @brief Creates an ike_sa_t object with a specific ID.
672 *
673 * @param ike_sa_id ike_sa_id_t object to associate with new IKE_SA
674 * @return ike_sa_t object
675 *
676 * @ingroup sa
677 */
678 ike_sa_t *ike_sa_create(ike_sa_id_t *ike_sa_id);
679
680 #endif /* IKE_SA_H_ */