2 * Copyright (C) 2006-2008 Tobias Brunner
3 * Copyright (C) 2006 Daniel Roethlisberger
4 * Copyright (C) 2005-2006 Martin Willi
5 * Copyright (C) 2005 Jan Hutter
6 * Hochschule fuer Technik Rapperswil
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 * @defgroup ike_sa ike_sa
29 typedef enum ike_extension_t ike_extension_t
;
30 typedef enum ike_condition_t ike_condition_t
;
31 typedef enum ike_sa_state_t ike_sa_state_t
;
32 typedef enum statistic_t statistic_t
;
33 typedef struct ike_sa_t ike_sa_t
;
36 #include <encoding/message.h>
37 #include <encoding/payloads/proposal_substructure.h>
38 #include <sa/ike_sa_id.h>
39 #include <sa/child_sa.h>
40 #include <sa/tasks/task.h>
41 #include <crypto/prfs/prf.h>
42 #include <crypto/crypters/crypter.h>
43 #include <crypto/signers/signer.h>
44 #include <config/peer_cfg.h>
45 #include <config/ike_cfg.h>
46 #include <credentials/auth_info.h>
49 * 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.
56 #define KEEPALIVE_INTERVAL 20
59 * After which time rekeying should be retried if it failed, in seconds.
61 #define RETRY_INTERVAL 30
64 * Jitter to subtract from RETRY_INTERVAL to randomize rekey retry.
66 #define RETRY_JITTER 20
69 * Extensions (or optional features) the peer supports
71 enum ike_extension_t
{
74 * peer supports NAT traversal as specified in RFC4306
79 * peer supports MOBIKE (RFC4555)
84 * peer supports HTTP cert lookups as specified in RFC4306
86 EXT_HASH_AND_URL
= (1<<2),
90 * Conditions of an IKE_SA, change during its lifetime
92 enum ike_condition_t
{
95 * Connection is natted (or faked) somewhere
97 COND_NAT_ANY
= (1<<0),
102 COND_NAT_HERE
= (1<<1),
105 * other is behind NAT
107 COND_NAT_THERE
= (1<<2),
110 * Faking NAT to enforce UDP encapsulation
112 COND_NAT_FAKE
= (1<<3),
115 * peer has ben authenticated using EAP
117 COND_EAP_AUTHENTICATED
= (1<<4),
120 * received a certificate request from the peer
122 COND_CERTREQ_SEEN
= (1<<5),
126 * Timing information and statistics to query from an SA
129 /** Timestamp of SA establishement */
130 STAT_ESTABLISHED
= 0,
131 /** Timestamp of scheudled rekeying */
133 /** Timestamp of scheudled reauthentication */
135 /** Timestamp of scheudled delete */
137 /** Timestamp of last inbound IKE packet */
139 /** Timestamp of last outbound IKE packet */
146 * State of an IKE_SA.
148 * An IKE_SA passes various states in its lifetime. A newly created
149 * SA is in the state CREATED.
155 on initiate()---> ¦ <----- on IKE_SA_INIT received
161 ¦ <----- on IKE_AUTH successfully completed
164 ¦ SA_ESTABLISHED ¦-------------------------+ <-- on rekeying
167 on delete()---> ¦ <----- on IKE_SA +-------------+
168 ¦ delete request ¦ SA_REKEYING ¦
169 ¦ received +-------------+
172 ¦ SA_DELETING ¦<------------------------+ <-- after rekeying
175 ¦ <----- after delete() acknowledged
182 enum ike_sa_state_t
{
185 * IKE_SA just got created, but is not initiating nor responding yet.
190 * IKE_SA gets initiated actively or passively
195 * IKE_SA is fully established
200 * IKE_SA rekeying in progress
205 * IKE_SA is in progress of deletion
210 * IKE_SA object gets destroyed
216 * enum names for ike_sa_state_t.
218 extern enum_name_t
*ike_sa_state_names
;
221 * Class ike_sa_t representing an IKE_SA.
223 * An IKE_SA contains crypto information related to a connection
224 * with a peer. It contains multiple IPsec CHILD_SA, for which
225 * it is responsible. All traffic is handled by an IKE_SA, using
226 * the task manager and its tasks.
231 * Get the id of the SA.
233 * Returned ike_sa_id_t object is not getting cloned!
235 * @return ike_sa's ike_sa_id_t
237 ike_sa_id_t
* (*get_id
) (ike_sa_t
*this);
240 * Get the numerical ID uniquely defining this IKE_SA.
244 u_int32_t (*get_unique_id
) (ike_sa_t
*this);
247 * Get the state of the IKE_SA.
249 * @return state of the IKE_SA
251 ike_sa_state_t (*get_state
) (ike_sa_t
*this);
254 * Set the state of the IKE_SA.
256 * @param state state to set for the IKE_SA
258 void (*set_state
) (ike_sa_t
*this, ike_sa_state_t ike_sa
);
261 * Get the name of the connection this IKE_SA uses.
265 char* (*get_name
) (ike_sa_t
*this);
268 * Get statistic values from the IKE_SA.
270 * @param kind kind of requested value
271 * @return value as integer
273 u_int32_t (*get_statistic
)(ike_sa_t
*this, statistic_t kind
);
276 * Get the own host address.
278 * @return host address
280 host_t
* (*get_my_host
) (ike_sa_t
*this);
283 * Set the own host address.
285 * @param me host address
287 void (*set_my_host
) (ike_sa_t
*this, host_t
*me
);
290 * Get the other peers host address.
292 * @return host address
294 host_t
* (*get_other_host
) (ike_sa_t
*this);
297 * Set the others host address.
299 * @param other host address
301 void (*set_other_host
) (ike_sa_t
*this, host_t
*other
);
304 * Update the IKE_SAs host.
306 * Hosts may be NULL to use current host.
308 * @param me new local host address, or NULL
309 * @param other new remote host address, or NULL
311 void (*update_hosts
)(ike_sa_t
*this, host_t
*me
, host_t
*other
);
314 * Get the own identification.
316 * @return identification
318 identification_t
* (*get_my_id
) (ike_sa_t
*this);
321 * Set the own identification.
323 * @param me identification
325 void (*set_my_id
) (ike_sa_t
*this, identification_t
*me
);
328 * Get the other peer's identification.
330 * @return identification
332 identification_t
* (*get_other_id
) (ike_sa_t
*this);
335 * Set the other peer's identification.
337 * @param other identification
339 void (*set_other_id
) (ike_sa_t
*this, identification_t
*other
);
342 * Get the peers EAP identity.
344 * The EAP identity is exchanged in a EAP-Identity exchange.
346 * @return identification, NULL if none set
348 identification_t
* (*get_eap_identity
) (ike_sa_t
*this);
351 * Set the peer's EAP identity.
353 * @param id identification
355 void (*set_eap_identity
) (ike_sa_t
*this, identification_t
*id
);
358 * Get the config used to setup this IKE_SA.
362 ike_cfg_t
* (*get_ike_cfg
) (ike_sa_t
*this);
365 * Set the config to setup this IKE_SA.
367 * @param config ike_config to use
369 void (*set_ike_cfg
) (ike_sa_t
*this, ike_cfg_t
* config
);
372 * Get the peer config used by this IKE_SA.
374 * @return peer_config
376 peer_cfg_t
* (*get_peer_cfg
) (ike_sa_t
*this);
379 * Set the peer config to use with this IKE_SA.
381 * @param config peer_config to use
383 void (*set_peer_cfg
) (ike_sa_t
*this, peer_cfg_t
*config
);
386 * Get authentication/authorization info for local peer.
388 * @return auth_info for me
390 auth_info_t
* (*get_my_auth
)(ike_sa_t
*this);
393 * Get authentication/authorization info for remote peer.
395 * @return auth_info for me
397 auth_info_t
* (*get_other_auth
)(ike_sa_t
*this);
400 * Add an additional address for the peer.
402 * In MOBIKE, a peer may transmit additional addresses where it is
403 * reachable. These are stored in the IKE_SA.
404 * The own list of addresses is not stored, they are queried from
405 * the kernel when required.
407 * @param host host to add to list
409 void (*add_additional_address
)(ike_sa_t
*this, host_t
*host
);
412 * Create an iterator over all additional addresses of the peer.
414 * @return iterator over addresses
416 iterator_t
* (*create_additional_address_iterator
)(ike_sa_t
*this);
419 * Check if mappings have changed on a NAT for our source address.
421 * @param hash received DESTINATION_IP hash
422 * @return TRUE if mappings have changed
424 bool (*has_mapping_changed
)(ike_sa_t
*this, chunk_t hash
);
427 * Enable an extension the peer supports.
429 * If support for an IKE extension is detected, this method is called
430 * to enable that extension and behave accordingly.
432 * @param extension extension to enable
434 void (*enable_extension
)(ike_sa_t
*this, ike_extension_t extension
);
437 * Check if the peer supports an extension.
439 * @param extension extension to check for support
440 * @return TRUE if peer supports it, FALSE otherwise
442 bool (*supports_extension
)(ike_sa_t
*this, ike_extension_t extension
);
445 * Enable/disable a condition flag for this IKE_SA.
447 * @param condition condition to enable/disable
448 * @param enable TRUE to enable condition, FALSE to disable
450 void (*set_condition
) (ike_sa_t
*this, ike_condition_t condition
, bool enable
);
453 * Check if a condition flag is set.
455 * @param condition condition to check
456 * @return TRUE if condition flag set, FALSE otherwise
458 bool (*has_condition
) (ike_sa_t
*this, ike_condition_t condition
);
461 * Get the number of queued MOBIKE address updates.
463 * @return number of pending updates
465 u_int32_t (*get_pending_updates
)(ike_sa_t
*this);
468 * Set the number of queued MOBIKE address updates.
470 * @param updates number of pending updates
472 void (*set_pending_updates
)(ike_sa_t
*this, u_int32_t updates
);
475 * Check if we are the original initiator of this IKE_SA (rekeying does not
478 bool (*is_ike_initiator
)(ike_sa_t
*this);
483 * Activate mediation server functionality for this IKE_SA.
485 void (*act_as_mediation_server
) (ike_sa_t
*this);
488 * Get the server reflexive host.
490 * @return server reflexive host
492 host_t
* (*get_server_reflexive_host
) (ike_sa_t
*this);
495 * Set the server reflexive host.
497 * @param host server reflexive host
499 void (*set_server_reflexive_host
) (ike_sa_t
*this, host_t
*host
);
502 * Get the connect ID.
506 chunk_t (*get_connect_id
) (ike_sa_t
*this);
509 * Initiate the mediation of a mediated connection (i.e. initiate a
510 * ME_CONNECT exchange).
512 * @param mediated_cfg peer_cfg of the mediated connection
514 * - SUCCESS if initialization started
515 * - DESTROY_ME if initialization failed
517 status_t (*initiate_mediation
) (ike_sa_t
*this, peer_cfg_t
*mediated_cfg
);
520 * Initiate the mediated connection
522 * @param me local endpoint (gets cloned)
523 * @param other remote endpoint (gets cloned)
524 * @param connect_id connect ID (gets cloned)
526 * - SUCCESS if initialization started
527 * - DESTROY_ME if initialization failed
529 status_t (*initiate_mediated
) (ike_sa_t
*this, host_t
*me
, host_t
*other
,
533 * Relay data from one peer to another (i.e. initiate a
534 * ME_CONNECT exchange).
538 * @param requester ID of the requesting peer
539 * @param connect_id data of the ME_CONNECTID payload
540 * @param connect_key data of the ME_CONNECTKEY payload
541 * @param endpoints endpoints
542 * @param response TRUE if this is a response
544 * - SUCCESS if relay started
545 * - DESTROY_ME if relay failed
547 status_t (*relay
) (ike_sa_t
*this, identification_t
*requester
, chunk_t connect_id
,
548 chunk_t connect_key
, linked_list_t
*endpoints
, bool response
);
551 * Send a callback to a peer.
555 * @param peer_id ID of the other peer
557 * - SUCCESS if response started
558 * - DESTROY_ME if response failed
560 status_t (*callback
) (ike_sa_t
*this, identification_t
*peer_id
);
563 * Respond to a ME_CONNECT request.
567 * @param peer_id ID of the other peer
568 * @param connect_id the connect ID supplied by the initiator
570 * - SUCCESS if response started
571 * - DESTROY_ME if response failed
573 status_t (*respond
) (ike_sa_t
*this, identification_t
*peer_id
, chunk_t connect_id
);
577 * Initiate a new connection.
579 * The configs are owned by the IKE_SA after the call.
581 * @param child_cfg child config to create CHILD from
583 * - SUCCESS if initialization started
584 * - DESTROY_ME if initialization failed
586 status_t (*initiate
) (ike_sa_t
*this, child_cfg_t
*child_cfg
);
589 * Route a policy in the kernel.
591 * Installs the policies in the kernel. If traffic matches,
592 * the kernel requests connection setup from the IKE_SA via acquire().
594 * @param child_cfg child config to route
596 * - SUCCESS if routed successfully
597 * - FAILED if routing failed
599 status_t (*route
) (ike_sa_t
*this, child_cfg_t
*child_cfg
);
602 * Unroute a policy in the kernel previously routed.
604 * @param reqid reqid of CHILD_SA to unroute
606 * - SUCCESS if route removed
607 * - NOT_FOUND if CHILD_SA not found
608 * - DESTROY_ME if last CHILD_SA was unrouted
610 status_t (*unroute
) (ike_sa_t
*this, u_int32_t reqid
);
613 * Acquire connection setup for an installed kernel policy.
615 * If an installed policy raises an acquire, the kernel calls
616 * this function to establish the CHILD_SA (and maybe the IKE_SA).
618 * @param reqid reqid of the CHILD_SA the policy belongs to.
620 * - SUCCESS if initialization started
621 * - DESTROY_ME if initialization failed
623 status_t (*acquire
) (ike_sa_t
*this, u_int32_t reqid
);
626 * Initiates the deletion of an IKE_SA.
628 * Sends a delete message to the remote peer and waits for
629 * its response. If the response comes in, or a timeout occurs,
630 * the IKE SA gets deleted.
633 * - SUCCESS if deletion is initialized
634 * - INVALID_STATE, if the IKE_SA is not in
635 * an established state and can not be
636 * delete (but destroyed).
638 status_t (*delete) (ike_sa_t
*this);
641 * Update IKE_SAs after network interfaces have changed.
643 * Whenever the network interface configuration changes, the kernel
644 * interface calls roam() on each IKE_SA. The IKE_SA then checks if
645 * the new network config requires changes, and handles appropriate.
646 * If MOBIKE is supported, addresses are updated; If not, the tunnel is
649 * @param address TRUE if address list changed, FALSE otherwise
650 * @return SUCCESS, FAILED, DESTROY_ME
652 status_t (*roam
)(ike_sa_t
*this, bool address
);
655 * Processes a incoming IKEv2-Message.
657 * Message processing may fail. If a critical failure occurs,
658 * process_message() return DESTROY_ME. Then the caller must
659 * destroy the IKE_SA immediatly, as it is unusable.
661 * @param message message to process
665 * - DESTROY_ME if this IKE_SA MUST be deleted
667 status_t (*process_message
) (ike_sa_t
*this, message_t
*message
);
670 * Generate a IKE message to send it to the peer.
672 * This method generates all payloads in the message and encrypts/signs
675 * @param message message to generate
676 * @param packet generated output packet
680 * - DESTROY_ME if this IKE_SA MUST be deleted
682 status_t (*generate_message
) (ike_sa_t
*this, message_t
*message
,
686 * Retransmits a request.
688 * @param message_id ID of the request to retransmit
691 * - NOT_FOUND if request doesn't have to be retransmited
693 status_t (*retransmit
) (ike_sa_t
*this, u_int32_t message_id
);
696 * Sends a DPD request to the peer.
698 * To check if a peer is still alive, periodic
699 * empty INFORMATIONAL messages are sent if no
700 * other traffic was received.
704 * - DESTROY_ME, if peer did not respond
706 status_t (*send_dpd
) (ike_sa_t
*this);
709 * Sends a keep alive packet.
711 * To refresh NAT tables in a NAT router
712 * between the peers, periodic empty
713 * UDP packets are sent if no other traffic
716 void (*send_keepalive
) (ike_sa_t
*this);
719 * Derive all keys and create the transforms for IKE communication.
721 * Keys are derived using the diffie hellman secret, nonces and internal
723 * Key derivation differs when an IKE_SA is set up to replace an
724 * existing IKE_SA (rekeying). The SK_d key from the old IKE_SA
725 * is included in the derivation process.
727 * @param proposal proposal which contains algorithms to use
728 * @param secret secret derived from DH exchange, gets freed
729 * @param nonce_i initiators nonce
730 * @param nonce_r responders nonce
731 * @param initiator TRUE if initiator, FALSE otherwise
732 * @param child_prf PRF with SK_d key when rekeying, NULL otherwise
733 * @param old_prf general purpose PRF of old SA when rekeying
735 status_t (*derive_keys
)(ike_sa_t
*this, proposal_t
* proposal
, chunk_t secret
,
736 chunk_t nonce_i
, chunk_t nonce_r
,
737 bool initiator
, prf_t
*child_prf
, prf_t
*old_prf
);
740 * Get the selected IKE proposal
742 * @return selected IKE proposal
744 proposal_t
* (*get_proposal
)(ike_sa_t
*this);
747 * Get a multi purpose prf for the negotiated PRF function.
749 * @return pointer to prf_t object
751 prf_t
*(*get_prf
) (ike_sa_t
*this);
754 * Get the prf-object, which is used to derive keys for child SAs.
756 * @return pointer to prf_t object
758 prf_t
*(*get_child_prf
) (ike_sa_t
*this);
761 * Get the key to build outgoing authentication data.
763 * @return pointer to prf_t object
765 chunk_t (*get_skp_build
) (ike_sa_t
*this);
768 * Get the key to verify incoming authentication data.
770 * @return pointer to prf_t object
772 chunk_t (*get_skp_verify
) (ike_sa_t
*this);
775 * Associates a child SA to this IKE SA
777 * @param child_sa child_sa to add
779 void (*add_child_sa
) (ike_sa_t
*this, child_sa_t
*child_sa
);
782 * Get a CHILD_SA identified by protocol and SPI.
784 * @param protocol protocol of the SA
785 * @param spi SPI of the CHILD_SA
786 * @param inbound TRUE if SPI is inbound, FALSE if outbound
787 * @return child_sa, or NULL if none found
789 child_sa_t
* (*get_child_sa
) (ike_sa_t
*this, protocol_id_t protocol
,
790 u_int32_t spi
, bool inbound
);
793 * Create an iterator over all CHILD_SAs.
797 iterator_t
* (*create_child_sa_iterator
) (ike_sa_t
*this);
800 * Rekey the CHILD SA with the specified reqid.
802 * Looks for a CHILD SA owned by this IKE_SA, and start the rekeing.
804 * @param protocol protocol of the SA
805 * @param spi inbound SPI of the CHILD_SA
807 * - NOT_FOUND, if IKE_SA has no such CHILD_SA
808 * - SUCCESS, if rekeying initiated
810 status_t (*rekey_child_sa
) (ike_sa_t
*this, protocol_id_t protocol
, u_int32_t spi
);
813 * Close the CHILD SA with the specified protocol/SPI.
815 * Looks for a CHILD SA owned by this IKE_SA, deletes it and
816 * notify's the remote peer about the delete. The associated
817 * states and policies in the kernel get deleted, if they exist.
819 * @param protocol protocol of the SA
820 * @param spi inbound SPI of the CHILD_SA
822 * - NOT_FOUND, if IKE_SA has no such CHILD_SA
823 * - SUCCESS, if delete message sent
825 status_t (*delete_child_sa
) (ike_sa_t
*this, protocol_id_t protocol
, u_int32_t spi
);
828 * Destroy a CHILD SA with the specified protocol/SPI.
830 * Looks for a CHILD SA owned by this IKE_SA and destroys it.
832 * @param protocol protocol of the SA
833 * @param spi inbound SPI of the CHILD_SA
835 * - NOT_FOUND, if IKE_SA has no such CHILD_SA
838 status_t (*destroy_child_sa
) (ike_sa_t
*this, protocol_id_t protocol
, u_int32_t spi
);
843 * Sets up a new IKE_SA, moves all CHILDs to it and deletes this IKE_SA.
845 * @return - SUCCESS, if IKE_SA rekeying initiated
847 status_t (*rekey
) (ike_sa_t
*this);
850 * Reauthenticate the IKE_SA.
852 * Create a completely new IKE_SA with authentication, recreates all children
853 * within the IKE_SA, closes this IKE_SA.
855 * @return DESTROY_ME to destroy the IKE_SA
857 status_t (*reauth
) (ike_sa_t
*this);
860 * Restablish the IKE_SA.
862 * Reestablish an IKE_SA after it has been closed.
864 * @return DESTROY_ME to destroy the IKE_SA
866 status_t (*reestablish
) (ike_sa_t
*this);
869 * Set the lifetime limit received from a AUTH_LIFETIME notify.
871 * @param lifetime lifetime in seconds
873 void (*set_auth_lifetime
)(ike_sa_t
*this, u_int32_t lifetime
);
876 * Set the virtual IP to use for this IKE_SA and its children.
878 * The virtual IP is assigned per IKE_SA, not per CHILD_SA. It has the same
879 * lifetime as the IKE_SA.
881 * @param local TRUE to set local address, FALSE for remote
882 * @param ip IP to set as virtual IP
884 void (*set_virtual_ip
) (ike_sa_t
*this, bool local
, host_t
*ip
);
887 * Get the virtual IP configured.
889 * @param local TRUE to get local virtual IP, FALSE for remote
890 * @return host_t *virtual IP
892 host_t
* (*get_virtual_ip
) (ike_sa_t
*this, bool local
);
895 * Add a DNS server to the system.
897 * An IRAS may send a DNS server. To use it, it is installed on the
898 * system. The DNS entry has a lifetime until the IKE_SA gets closed.
900 * @param dns DNS server to install on the system
902 void (*add_dns_server
) (ike_sa_t
*this, host_t
*dns
);
905 * Inherit all attributes of other to this after rekeying.
907 * When rekeying is completed, all CHILD_SAs, the virtual IP and all
908 * outstanding tasks are moved from other to this.
909 * As this call may initiate inherited tasks, a status is returned.
911 * @param other other task to inherit from
912 * @return DESTROY_ME if initiation of inherited task failed
914 status_t (*inherit
) (ike_sa_t
*this, ike_sa_t
*other
);
917 * Reset the IKE_SA, useable when initiating fails
919 void (*reset
) (ike_sa_t
*this);
922 * Destroys a ike_sa_t object.
924 void (*destroy
) (ike_sa_t
*this);
928 * Creates an ike_sa_t object with a specific ID.
930 * @param ike_sa_id ike_sa_id_t object to associate with new IKE_SA
931 * @return ike_sa_t object
933 ike_sa_t
*ike_sa_create(ike_sa_id_t
*ike_sa_id
);
935 #endif /* IKE_SA_H_ @} */