4 * @brief Interface of ike_sa_t.
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
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>.
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
28 typedef enum ike_extension_t ike_extension_t
;
29 typedef enum ike_sa_state_t ike_sa_state_t
;
30 typedef struct ike_sa_t ike_sa_t
;
33 #include <encoding/message.h>
34 #include <encoding/payloads/proposal_substructure.h>
35 #include <sa/ike_sa_id.h>
36 #include <sa/child_sa.h>
37 #include <sa/tasks/task.h>
38 #include <utils/randomizer.h>
39 #include <crypto/prfs/prf.h>
40 #include <crypto/crypters/crypter.h>
41 #include <crypto/signers/signer.h>
42 #include <crypto/ca.h>
43 #include <config/peer_cfg.h>
44 #include <config/ike_cfg.h>
47 * Timeout in milliseconds after that a half open IKE_SA gets deleted.
51 #define HALF_OPEN_IKE_SA_TIMEOUT 30000
54 * Interval to send keepalives when NATed, in seconds.
58 #define KEEPALIVE_INTERVAL 20
61 * After which time rekeying should be retried if it failed, in seconds.
65 #define RETRY_INTERVAL 30
68 * Jitter to subtract from RETRY_INTERVAL to randomize rekey retry.
72 #define RETRY_JITTER 20
75 * @brief Extensions (or optional features) the peer supports
77 enum ike_extension_t
{
80 * peer supports NAT traversal as specified in RFC4306
85 * peer supports MOBIKE (RFC4555)
91 * @brief State of an IKE_SA.
93 * An IKE_SA passes various states in its lifetime. A newly created
94 * SA is in the state CREATED.
100 on initiate()---> ¦ <----- on IKE_SA_INIT received
106 ¦ <----- on IKE_AUTH successfully completed
109 ¦ SA_ESTABLISHED ¦-------------------------+ <-- on rekeying
112 on delete()---> ¦ <----- on IKE_SA +-------------+
113 ¦ delete request ¦ SA_REKEYING ¦
114 ¦ received +-------------+
117 ¦ SA_DELETING ¦<------------------------+ <-- after rekeying
120 ¦ <----- after delete() acknowledged
129 enum ike_sa_state_t
{
132 * IKE_SA just got created, but is not initiating nor responding yet.
137 * IKE_SA gets initiated actively or passively
142 * IKE_SA is fully established
147 * IKE_SA rekeying in progress
152 * IKE_SA is in progress of deletion
158 * enum names for ike_sa_state_t.
160 extern enum_name_t
*ike_sa_state_names
;
163 * @brief Class ike_sa_t representing an IKE_SA.
165 * An IKE_SA contains crypto information related to a connection
166 * with a peer. It contains multiple IPsec CHILD_SA, for which
167 * it is responsible. All traffic is handled by an IKE_SA, using
168 * the task manager and its tasks.
178 * @brief Get the id of the SA.
180 * Returned ike_sa_id_t object is not getting cloned!
182 * @param this calling object
183 * @return ike_sa's ike_sa_id_t
185 ike_sa_id_t
* (*get_id
) (ike_sa_t
*this);
188 * @brief Get the numerical ID uniquely defining this IKE_SA.
190 * @param this calling object
193 u_int32_t (*get_unique_id
) (ike_sa_t
*this);
196 * @brief Get the state of the IKE_SA.
198 * @param this calling object
199 * @return state of the IKE_SA
201 ike_sa_state_t (*get_state
) (ike_sa_t
*this);
204 * @brief Get some statistics about this IKE_SA.
206 * @param next_rekeying when the next rekeying is scheduled
208 void (*get_stats
)(ike_sa_t
*this, u_int32_t
*next_rekeying
);
211 * @brief Set the state of the IKE_SA.
213 * @param this calling object
214 * @param state state to set for the IKE_SA
216 void (*set_state
) (ike_sa_t
*this, ike_sa_state_t ike_sa
);
219 * @brief Get the name of the connection this IKE_SA uses.
221 * @param this calling object
224 char* (*get_name
) (ike_sa_t
*this);
227 * @brief Get the own host address.
229 * @param this calling object
230 * @return host address
232 host_t
* (*get_my_host
) (ike_sa_t
*this);
235 * @brief Set the own host address.
237 * @param this calling object
238 * @param me host address
240 void (*set_my_host
) (ike_sa_t
*this, host_t
*me
);
243 * @brief Get the other peers host address.
245 * @param this calling object
246 * @return host address
248 host_t
* (*get_other_host
) (ike_sa_t
*this);
251 * @brief Set the others host address.
253 * @param this calling object
254 * @param other host address
256 void (*set_other_host
) (ike_sa_t
*this, host_t
*other
);
259 * @brief Get the own identification.
261 * @param this calling object
262 * @return identification
264 identification_t
* (*get_my_id
) (ike_sa_t
*this);
267 * @brief Set the own identification.
269 * @param this calling object
270 * @param me identification
272 void (*set_my_id
) (ike_sa_t
*this, identification_t
*me
);
275 * @brief Get the other peer's identification.
277 * @param this calling object
278 * @return identification
280 identification_t
* (*get_other_id
) (ike_sa_t
*this);
283 * @brief Set the other peer's identification.
285 * @param this calling object
286 * @param other identification
288 void (*set_other_id
) (ike_sa_t
*this, identification_t
*other
);
291 * @brief Get the other peer's certification authority
293 * @param this calling object
294 * @return ca_info_t record of other ca
296 ca_info_t
* (*get_other_ca
) (ike_sa_t
*this);
299 * @brief Set the other peer's certification authority
301 * @param this calling object
302 * @param other_ca ca_info_t record of other ca
304 void (*set_other_ca
) (ike_sa_t
*this, ca_info_t
*other_ca
);
307 * @brief Get the config used to setup this IKE_SA.
309 * @param this calling object
312 ike_cfg_t
* (*get_ike_cfg
) (ike_sa_t
*this);
315 * @brief Set the config to setup this IKE_SA.
317 * @param this calling object
318 * @param config ike_config to use
320 void (*set_ike_cfg
) (ike_sa_t
*this, ike_cfg_t
* config
);
323 * @brief Get the peer config used by this IKE_SA.
325 * @param this calling object
326 * @return peer_config
328 peer_cfg_t
* (*get_peer_cfg
) (ike_sa_t
*this);
331 * @brief Set the peer config to use with this IKE_SA.
333 * @param this calling object
334 * @param config peer_config to use
336 void (*set_peer_cfg
) (ike_sa_t
*this, peer_cfg_t
*config
);
339 * @brief Check if the peer supports an extension.
341 * @param this calling object
342 * @param extension extension to check for support
343 * @return TRUE if peer supports it, FALSE otherwise
345 bool (*supports_extension
)(ike_sa_t
*this, ike_extension_t extension
);
348 * @brief Enable an extension the peer supports.
350 * If support for an IKE extension is detected, this method is called
351 * to enable that extension and behave accordingly.
353 * @param this calling object
354 * @param extension extension to enable
356 void (*enable_extension
)(ike_sa_t
*this, ike_extension_t extension
);
359 * @brief Initiate a new connection.
361 * The configs are owned by the IKE_SA after the call.
363 * @param this calling object
364 * @param child_cfg child config to create CHILD from
366 * - SUCCESS if initialization started
367 * - DESTROY_ME if initialization failed
369 status_t (*initiate
) (ike_sa_t
*this, child_cfg_t
*child_cfg
);
372 * @brief Route a policy in the kernel.
374 * Installs the policies in the kernel. If traffic matches,
375 * the kernel requests connection setup from the IKE_SA via acquire().
377 * @param this calling object
378 * @param child_cfg child config to route
380 * - SUCCESS if routed successfully
381 * - FAILED if routing failed
383 status_t (*route
) (ike_sa_t
*this, child_cfg_t
*child_cfg
);
386 * @brief Unroute a policy in the kernel previously routed.
388 * @param this calling object
389 * @param reqid reqid of CHILD_SA to unroute
391 * - SUCCESS if route removed
392 * - NOT_FOUND if CHILD_SA not found
393 * - DESTROY_ME if last CHILD_SA was unrouted
395 status_t (*unroute
) (ike_sa_t
*this, u_int32_t reqid
);
398 * @brief Acquire connection setup for an installed kernel policy.
400 * If an installed policy raises an acquire, the kernel calls
401 * this function to establish the CHILD_SA (and maybe the IKE_SA).
403 * @param this calling object
404 * @param reqid reqid of the CHILD_SA the policy belongs to.
406 * - SUCCESS if initialization started
407 * - DESTROY_ME if initialization failed
409 status_t (*acquire
) (ike_sa_t
*this, u_int32_t reqid
);
412 * @brief Initiates the deletion of an IKE_SA.
414 * Sends a delete message to the remote peer and waits for
415 * its response. If the response comes in, or a timeout occurs,
416 * the IKE SA gets deleted.
418 * @param this calling object
420 * - SUCCESS if deletion is initialized
421 * - INVALID_STATE, if the IKE_SA is not in
422 * an established state and can not be
423 * delete (but destroyed).
425 status_t (*delete) (ike_sa_t
*this);
428 * @brief Processes a incoming IKEv2-Message.
430 * Message processing may fail. If a critical failure occurs,
431 * process_message() return DESTROY_ME. Then the caller must
432 * destroy the IKE_SA immediatly, as it is unusable.
434 * @param this calling object
435 * @param message message to process
439 * - DESTROY_ME if this IKE_SA MUST be deleted
441 status_t (*process_message
) (ike_sa_t
*this, message_t
*message
);
444 * @brief Generate a IKE message to send it to the peer.
446 * This method generates all payloads in the message and encrypts/signs
449 * @param this calling object
450 * @param message message to generate
451 * @param packet generated output packet
455 * - DESTROY_ME if this IKE_SA MUST be deleted
457 status_t (*generate_message
) (ike_sa_t
*this, message_t
*message
,
461 * @brief Retransmits a request.
463 * @param this calling object
464 * @param message_id ID of the request to retransmit
467 * - NOT_FOUND if request doesn't have to be retransmited
469 status_t (*retransmit
) (ike_sa_t
*this, u_int32_t message_id
);
472 * @brief Sends a DPD request to the peer.
474 * To check if a peer is still alive, periodic
475 * empty INFORMATIONAL messages are sent if no
476 * other traffic was received.
478 * @param this calling object
481 * - DESTROY_ME, if peer did not respond
483 status_t (*send_dpd
) (ike_sa_t
*this);
486 * @brief Sends a keep alive packet.
488 * To refresh NAT tables in a NAT router
489 * between the peers, periodic empty
490 * UDP packets are sent if no other traffic
493 * @param this calling object
495 void (*send_keepalive
) (ike_sa_t
*this);
498 * @brief Check if NAT traversal is enabled for this IKE_SA.
500 * @param this calling object
501 * @return TRUE if NAT traversal enabled
503 bool (*is_natt_enabled
) (ike_sa_t
*this);
506 * @brief Enable NAT detection for this IKE_SA.
508 * If a Network address translation is detected with
509 * NAT_DETECTION notifys, a SA must switch to ports
510 * 4500. To enable this behavior, call enable_natt().
511 * It is relevant which peer is NATted, this is specified
512 * with the "local" parameter. Call it twice when both
515 * @param this calling object
516 * @param local TRUE, if we are NATted, FALSE if other
518 void (*enable_natt
) (ike_sa_t
*this, bool local
);
521 * @brief Derive all keys and create the transforms for IKE communication.
523 * Keys are derived using the diffie hellman secret, nonces and internal
525 * Key derivation differs when an IKE_SA is set up to replace an
526 * existing IKE_SA (rekeying). The SK_d key from the old IKE_SA
527 * is included in the derivation process.
529 * @param this calling object
530 * @param proposal proposal which contains algorithms to use
531 * @param secret secret derived from DH exchange, gets freed
532 * @param nonce_i initiators nonce
533 * @param nonce_r responders nonce
534 * @param initiator TRUE if initiator, FALSE otherwise
535 * @param child_prf PRF with SK_d key when rekeying, NULL otherwise
536 * @param old_prf general purpose PRF of old SA when rekeying
538 status_t (*derive_keys
)(ike_sa_t
*this, proposal_t
* proposal
, chunk_t secret
,
539 chunk_t nonce_i
, chunk_t nonce_r
,
540 bool initiator
, prf_t
*child_prf
, prf_t
*old_prf
);
543 * @brief Get a multi purpose prf for the negotiated PRF function.
545 * @param this calling object
546 * @return pointer to prf_t object
548 prf_t
*(*get_prf
) (ike_sa_t
*this);
551 * @brief Get the prf-object, which is used to derive keys for child SAs.
553 * @param this calling object
554 * @return pointer to prf_t object
556 prf_t
*(*get_child_prf
) (ike_sa_t
*this);
559 * @brief Get the key to build outgoing authentication data.
561 * @param this calling object
562 * @return pointer to prf_t object
564 chunk_t (*get_skp_build
) (ike_sa_t
*this);
567 * @brief Get the key to verify incoming authentication data.
569 * @param this calling object
570 * @return pointer to prf_t object
572 chunk_t (*get_skp_verify
) (ike_sa_t
*this);
575 * @brief Associates a child SA to this IKE SA
577 * @param this calling object
578 * @param child_sa child_sa to add
580 void (*add_child_sa
) (ike_sa_t
*this, child_sa_t
*child_sa
);
583 * @brief Get a CHILD_SA identified by protocol and SPI.
585 * @param this calling object
586 * @param protocol protocol of the SA
587 * @param spi SPI of the CHILD_SA
588 * @param inbound TRUE if SPI is inbound, FALSE if outbound
589 * @return child_sa, or NULL if none found
591 child_sa_t
* (*get_child_sa
) (ike_sa_t
*this, protocol_id_t protocol
,
592 u_int32_t spi
, bool inbound
);
595 * @brief Create an iterator over all CHILD_SAs.
597 * @param this calling object
600 iterator_t
* (*create_child_sa_iterator
) (ike_sa_t
*this);
603 * @brief Rekey the CHILD SA with the specified reqid.
605 * Looks for a CHILD SA owned by this IKE_SA, and start the rekeing.
607 * @param this calling object
608 * @param protocol protocol of the SA
609 * @param spi inbound SPI of the CHILD_SA
611 * - NOT_FOUND, if IKE_SA has no such CHILD_SA
612 * - SUCCESS, if rekeying initiated
614 status_t (*rekey_child_sa
) (ike_sa_t
*this, protocol_id_t protocol
, u_int32_t spi
);
617 * @brief Close the CHILD SA with the specified protocol/SPI.
619 * Looks for a CHILD SA owned by this IKE_SA, deletes it and
620 * notify's the remote peer about the delete. The associated
621 * states and policies in the kernel get deleted, if they exist.
623 * @param this calling object
624 * @param protocol protocol of the SA
625 * @param spi inbound SPI of the CHILD_SA
627 * - NOT_FOUND, if IKE_SA has no such CHILD_SA
628 * - SUCCESS, if delete message sent
630 status_t (*delete_child_sa
) (ike_sa_t
*this, protocol_id_t protocol
, u_int32_t spi
);
633 * @brief Destroy a CHILD SA with the specified protocol/SPI.
635 * Looks for a CHILD SA owned by this IKE_SA and destroys it.
637 * @param this calling object
638 * @param protocol protocol of the SA
639 * @param spi inbound SPI of the CHILD_SA
641 * - NOT_FOUND, if IKE_SA has no such CHILD_SA
644 status_t (*destroy_child_sa
) (ike_sa_t
*this, protocol_id_t protocol
, u_int32_t spi
);
647 * @brief Rekey the IKE_SA.
649 * Sets up a new IKE_SA, moves all CHILDs to it and deletes this IKE_SA.
651 * @param this calling object
652 * @return - SUCCESS, if IKE_SA rekeying initiated
654 status_t (*rekey
) (ike_sa_t
*this);
657 * @brief Restablish the IKE_SA.
659 * Create a completely new IKE_SA with authentication, recreates all children
660 * within the IKE_SA, closes this IKE_SA.
662 * @param this calling object
663 * @return DESTROY_ME to destroy the IKE_SA
665 status_t (*reestablish
) (ike_sa_t
*this);
668 * @brief Set the virtual IP to use for this IKE_SA and its children.
670 * The virtual IP is assigned per IKE_SA, not per CHILD_SA. It has the same
671 * lifetime as the IKE_SA.
673 * @param this calling object
675 void (*set_virtual_ip
) (ike_sa_t
*this, bool local
, host_t
*ip
);
678 * @brief Get the virtual IP configured.
680 * @param this calling object
681 * @param local TRUE to get local virtual IP, FALSE for remote
683 host_t
* (*get_virtual_ip
) (ike_sa_t
*this, bool local
);
686 * @brief Add a DNS server to the system.
688 * An IRAS may send a DNS server. To use it, it is installed on the
689 * system. The DNS entry has a lifetime until the IKE_SA gets closed.
691 * @param this calling object
692 * @param dns DNS server to install on the system
694 void (*add_dns_server
) (ike_sa_t
*this, host_t
*dns
);
697 * @brief Inherit all attributes of other to this after rekeying.
699 * When rekeying is completed, all CHILD_SAs, the virtual IP and all
700 * outstanding tasks are moved from other to this.
701 * As this call may initiate inherited tasks, a status is returned.
703 * @param this calling object
704 * @param other other task to inherit from
705 * @return DESTROY_ME if initiation of inherited task failed
707 status_t (*inherit
) (ike_sa_t
*this, ike_sa_t
*other
);
710 * @brief Reset the IKE_SA, useable when initiating fails
712 * @param this calling object
714 void (*reset
) (ike_sa_t
*this);
717 * @brief Destroys a ike_sa_t object.
719 * @param this calling object
721 void (*destroy
) (ike_sa_t
*this);
725 * @brief Creates an ike_sa_t object with a specific ID.
727 * @param ike_sa_id ike_sa_id_t object to associate with new IKE_SA
728 * @return ike_sa_t object
732 ike_sa_t
*ike_sa_create(ike_sa_id_t
*ike_sa_id
);
734 #endif /* IKE_SA_H_ */