2 * Copyright (C) 2006-2011 Tobias Brunner
3 * Copyright (C) 2006 Daniel Roethlisberger
4 * Copyright (C) 2005-2009 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
20 * @defgroup ike_sa ike_sa
27 typedef enum ike_extension_t ike_extension_t
;
28 typedef enum ike_condition_t ike_condition_t
;
29 typedef enum ike_sa_state_t ike_sa_state_t
;
30 typedef enum statistic_t statistic_t
;
31 typedef struct ike_sa_t ike_sa_t
;
34 #include <attributes/attribute_handler.h>
35 #include <encoding/message.h>
36 #include <encoding/payloads/proposal_substructure.h>
37 #include <encoding/payloads/configuration_attribute.h>
38 #include <sa/ike_sa_id.h>
39 #include <sa/child_sa.h>
40 #include <sa/task_manager.h>
41 #include <sa/keymat.h>
42 #include <config/peer_cfg.h>
43 #include <config/ike_cfg.h>
44 #include <credentials/auth_cfg.h>
47 * Timeout in seconds after that a half open IKE_SA gets deleted.
49 #define HALF_OPEN_IKE_SA_TIMEOUT 30
52 * Interval to send keepalives when NATed, in seconds.
54 #define KEEPALIVE_INTERVAL 20
57 * After which time rekeying should be retried if it failed, in seconds.
59 #define RETRY_INTERVAL 30
62 * Jitter to subtract from RETRY_INTERVAL to randomize rekey retry.
64 #define RETRY_JITTER 20
67 * Extensions (or optional features) the peer supports
69 enum ike_extension_t
{
72 * peer supports NAT traversal as specified in RFC4306
77 * peer supports MOBIKE (RFC4555)
82 * peer supports HTTP cert lookups as specified in RFC4306
84 EXT_HASH_AND_URL
= (1<<2),
87 * peer supports multiple authentication exchanges, RFC4739
89 EXT_MULTIPLE_AUTH
= (1<<3),
92 * peer uses strongSwan, accept private use extensions
94 EXT_STRONGSWAN
= (1<<4),
97 * peer supports EAP-only authentication, draft-eronen-ipsec-ikev2-eap-auth
99 EXT_EAP_ONLY_AUTHENTICATION
= (1<<5),
103 * Conditions of an IKE_SA, change during its lifetime
105 enum ike_condition_t
{
108 * Connection is natted (or faked) somewhere
110 COND_NAT_ANY
= (1<<0),
115 COND_NAT_HERE
= (1<<1),
118 * other is behind NAT
120 COND_NAT_THERE
= (1<<2),
123 * Faking NAT to enforce UDP encapsulation
125 COND_NAT_FAKE
= (1<<3),
128 * peer has been authenticated using EAP at least once
130 COND_EAP_AUTHENTICATED
= (1<<4),
133 * received a certificate request from the peer
135 COND_CERTREQ_SEEN
= (1<<5),
138 * Local peer is the "original" IKE initiator. Unaffected from rekeying.
140 COND_ORIGINAL_INITIATOR
= (1<<6),
143 * IKE_SA is stale, the peer is currently unreachable (MOBIKE)
149 * Timing information and statistics to query from an SA
152 /** Timestamp of SA establishement */
153 STAT_ESTABLISHED
= 0,
154 /** Timestamp of scheudled rekeying */
156 /** Timestamp of scheudled reauthentication */
158 /** Timestamp of scheudled delete */
160 /** Timestamp of last inbound IKE packet */
162 /** Timestamp of last outbound IKE packet */
169 * State of an IKE_SA.
171 * An IKE_SA passes various states in its lifetime. A newly created
172 * SA is in the state CREATED.
178 on initiate()---> ¦ <----- on IKE_SA_INIT received
184 ¦ <----- on IKE_AUTH successfully completed
187 ¦ SA_ESTABLISHED ¦-------------------------+ <-- on rekeying
190 on delete()---> ¦ <----- on IKE_SA +-------------+
191 ¦ delete request ¦ SA_REKEYING ¦
192 ¦ received +-------------+
195 ¦ SA_DELETING ¦<------------------------+ <-- after rekeying
198 ¦ <----- after delete() acknowledged
205 enum ike_sa_state_t
{
208 * IKE_SA just got created, but is not initiating nor responding yet.
213 * IKE_SA gets initiated actively or passively
218 * IKE_SA is fully established
223 * IKE_SA is managed externally and does not process messages
228 * IKE_SA rekeying in progress
233 * IKE_SA is in progress of deletion
238 * IKE_SA object gets destroyed
244 * enum names for ike_sa_state_t.
246 extern enum_name_t
*ike_sa_state_names
;
249 * Class ike_sa_t representing an IKE_SA.
251 * An IKE_SA contains crypto information related to a connection
252 * with a peer. It contains multiple IPsec CHILD_SA, for which
253 * it is responsible. All traffic is handled by an IKE_SA, using
254 * the task manager and its tasks.
259 * Get the id of the SA.
261 * Returned ike_sa_id_t object is not getting cloned!
263 * @return ike_sa's ike_sa_id_t
265 ike_sa_id_t
* (*get_id
) (ike_sa_t
*this);
268 * Get the numerical ID uniquely defining this IKE_SA.
272 u_int32_t (*get_unique_id
) (ike_sa_t
*this);
275 * Get the state of the IKE_SA.
277 * @return state of the IKE_SA
279 ike_sa_state_t (*get_state
) (ike_sa_t
*this);
282 * Set the state of the IKE_SA.
284 * @param state state to set for the IKE_SA
286 void (*set_state
) (ike_sa_t
*this, ike_sa_state_t ike_sa
);
289 * Get the name of the connection this IKE_SA uses.
293 char* (*get_name
) (ike_sa_t
*this);
296 * Get statistic values from the IKE_SA.
298 * @param kind kind of requested value
299 * @return value as integer
301 u_int32_t (*get_statistic
)(ike_sa_t
*this, statistic_t kind
);
304 * Get the own host address.
306 * @return host address
308 host_t
* (*get_my_host
) (ike_sa_t
*this);
311 * Set the own host address.
313 * @param me host address
315 void (*set_my_host
) (ike_sa_t
*this, host_t
*me
);
318 * Get the other peers host address.
320 * @return host address
322 host_t
* (*get_other_host
) (ike_sa_t
*this);
325 * Set the others host address.
327 * @param other host address
329 void (*set_other_host
) (ike_sa_t
*this, host_t
*other
);
332 * Float to port 4500 (e.g. if a NAT is detected).
334 * The port of either endpoint is changed only if it is currently
335 * set to the default value of 500.
337 void (*float_ports
)(ike_sa_t
*this);
340 * Update the IKE_SAs host.
342 * Hosts may be NULL to use current host.
344 * @param me new local host address, or NULL
345 * @param other new remote host address, or NULL
346 * @param force force update
348 void (*update_hosts
)(ike_sa_t
*this, host_t
*me
, host_t
*other
, bool force
);
351 * Get the own identification.
353 * @return identification
355 identification_t
* (*get_my_id
) (ike_sa_t
*this);
358 * Set the own identification.
360 * @param me identification
362 void (*set_my_id
) (ike_sa_t
*this, identification_t
*me
);
365 * Get the other peer's identification.
367 * @return identification
369 identification_t
* (*get_other_id
) (ike_sa_t
*this);
372 * Get the others peer identity, but prefer an EAP-Identity.
374 * @return EAP or IKEv2 identity
376 identification_t
* (*get_other_eap_id
)(ike_sa_t
*this);
379 * Set the other peer's identification.
381 * @param other identification
383 void (*set_other_id
) (ike_sa_t
*this, identification_t
*other
);
386 * Get the config used to setup this IKE_SA.
390 ike_cfg_t
* (*get_ike_cfg
) (ike_sa_t
*this);
393 * Set the config to setup this IKE_SA.
395 * @param config ike_config to use
397 void (*set_ike_cfg
) (ike_sa_t
*this, ike_cfg_t
* config
);
400 * Get the peer config used by this IKE_SA.
402 * @return peer_config
404 peer_cfg_t
* (*get_peer_cfg
) (ike_sa_t
*this);
407 * Set the peer config to use with this IKE_SA.
409 * @param config peer_config to use
411 void (*set_peer_cfg
) (ike_sa_t
*this, peer_cfg_t
*config
);
414 * Get the authentication config with rules of the current auth round.
416 * @param local TRUE for local rules, FALSE for remote constraints
417 * @return current cfg
419 auth_cfg_t
* (*get_auth_cfg
)(ike_sa_t
*this, bool local
);
422 * Insert a completed authentication round.
424 * @param local TRUE for own rules, FALSE for others constraints
425 * @param cfg auth config to append
427 void (*add_auth_cfg
)(ike_sa_t
*this, bool local
, auth_cfg_t
*cfg
);
430 * Create an enumerator over added authentication rounds.
432 * @param local TRUE for own rules, FALSE for others constraints
433 * @return enumerator over auth_cfg_t
435 enumerator_t
* (*create_auth_cfg_enumerator
)(ike_sa_t
*this, bool local
);
438 * Get the selected proposal of this IKE_SA.
440 * @return selected proposal
442 proposal_t
* (*get_proposal
)(ike_sa_t
*this);
445 * Set the proposal selected for this IKE_SA.
447 * @param selected proposal
449 void (*set_proposal
)(ike_sa_t
*this, proposal_t
*proposal
);
452 * Set the message id of the IKE_SA.
454 * The IKE_SA stores two message IDs, one for initiating exchanges (send)
455 * and one to respond to exchanges (expect).
457 * @param initiate TRUE to set message ID for initiating
458 * @param mid message id to set
460 void (*set_message_id
)(ike_sa_t
*this, bool initiate
, u_int32_t mid
);
463 * Add an additional address for the peer.
465 * In MOBIKE, a peer may transmit additional addresses where it is
466 * reachable. These are stored in the IKE_SA.
467 * The own list of addresses is not stored, they are queried from
468 * the kernel when required.
470 * @param host host to add to list
472 void (*add_additional_address
)(ike_sa_t
*this, host_t
*host
);
475 * Create an enumerator over all additional addresses of the peer.
477 * @return enumerator over addresses
479 enumerator_t
* (*create_additional_address_enumerator
)(ike_sa_t
*this);
482 * Remove all additional addresses of the peer.
484 void (*remove_additional_addresses
)(ike_sa_t
*this);
487 * Check if mappings have changed on a NAT for our source address.
489 * @param hash received DESTINATION_IP hash
490 * @return TRUE if mappings have changed
492 bool (*has_mapping_changed
)(ike_sa_t
*this, chunk_t hash
);
495 * Enable an extension the peer supports.
497 * If support for an IKE extension is detected, this method is called
498 * to enable that extension and behave accordingly.
500 * @param extension extension to enable
502 void (*enable_extension
)(ike_sa_t
*this, ike_extension_t extension
);
505 * Check if the peer supports an extension.
507 * @param extension extension to check for support
508 * @return TRUE if peer supports it, FALSE otherwise
510 bool (*supports_extension
)(ike_sa_t
*this, ike_extension_t extension
);
513 * Enable/disable a condition flag for this IKE_SA.
515 * @param condition condition to enable/disable
516 * @param enable TRUE to enable condition, FALSE to disable
518 void (*set_condition
) (ike_sa_t
*this, ike_condition_t condition
, bool enable
);
521 * Check if a condition flag is set.
523 * @param condition condition to check
524 * @return TRUE if condition flag set, FALSE otherwise
526 bool (*has_condition
) (ike_sa_t
*this, ike_condition_t condition
);
529 * Get the number of queued MOBIKE address updates.
531 * @return number of pending updates
533 u_int32_t (*get_pending_updates
)(ike_sa_t
*this);
536 * Set the number of queued MOBIKE address updates.
538 * @param updates number of pending updates
540 void (*set_pending_updates
)(ike_sa_t
*this, u_int32_t updates
);
544 * Activate mediation server functionality for this IKE_SA.
546 void (*act_as_mediation_server
) (ike_sa_t
*this);
549 * Get the server reflexive host.
551 * @return server reflexive host
553 host_t
* (*get_server_reflexive_host
) (ike_sa_t
*this);
556 * Set the server reflexive host.
558 * @param host server reflexive host
560 void (*set_server_reflexive_host
) (ike_sa_t
*this, host_t
*host
);
563 * Get the connect ID.
567 chunk_t (*get_connect_id
) (ike_sa_t
*this);
570 * Initiate the mediation of a mediated connection (i.e. initiate a
571 * ME_CONNECT exchange to a mediation server).
573 * @param mediated_cfg peer_cfg of the mediated connection
575 * - SUCCESS if initialization started
576 * - DESTROY_ME if initialization failed
578 status_t (*initiate_mediation
) (ike_sa_t
*this, peer_cfg_t
*mediated_cfg
);
581 * Initiate the mediated connection
583 * @param me local endpoint (gets cloned)
584 * @param other remote endpoint (gets cloned)
585 * @param connect_id connect ID (gets cloned)
587 * - SUCCESS if initialization started
588 * - DESTROY_ME if initialization failed
590 status_t (*initiate_mediated
) (ike_sa_t
*this, host_t
*me
, host_t
*other
,
594 * Relay data from one peer to another (i.e. initiate a ME_CONNECT exchange
599 * @param requester ID of the requesting peer
600 * @param connect_id data of the ME_CONNECTID payload
601 * @param connect_key data of the ME_CONNECTKEY payload
602 * @param endpoints endpoints
603 * @param response TRUE if this is a response
605 * - SUCCESS if relay started
606 * - DESTROY_ME if relay failed
608 status_t (*relay
) (ike_sa_t
*this, identification_t
*requester
,
609 chunk_t connect_id
, chunk_t connect_key
,
610 linked_list_t
*endpoints
, bool response
);
613 * Send a callback to a peer.
617 * @param peer_id ID of the other peer
619 * - SUCCESS if response started
620 * - DESTROY_ME if response failed
622 status_t (*callback
) (ike_sa_t
*this, identification_t
*peer_id
);
625 * Respond to a ME_CONNECT request.
629 * @param peer_id ID of the other peer
630 * @param connect_id the connect ID supplied by the initiator
632 * - SUCCESS if response started
633 * - DESTROY_ME if response failed
635 status_t (*respond
) (ike_sa_t
*this, identification_t
*peer_id
,
640 * Initiate a new connection.
642 * The configs are owned by the IKE_SA after the call. If the initiate
643 * is triggered by a packet, traffic selectors of the packet can be added
646 * @param child_cfg child config to create CHILD from
647 * @param reqid reqid to use for CHILD_SA, 0 assigne uniquely
648 * @param tsi source of triggering packet
649 * @param tsr destination of triggering packet.
651 * - SUCCESS if initialization started
652 * - DESTROY_ME if initialization failed
654 status_t (*initiate
) (ike_sa_t
*this, child_cfg_t
*child_cfg
,
655 u_int32_t reqid
, traffic_selector_t
*tsi
,
656 traffic_selector_t
*tsr
);
659 * Initiates the deletion of an IKE_SA.
661 * Sends a delete message to the remote peer and waits for
662 * its response. If the response comes in, or a timeout occurs,
663 * the IKE SA gets deleted.
666 * - SUCCESS if deletion is initialized
667 * - DESTROY_ME, if the IKE_SA is not in
668 * an established state and can not be
669 * deleted (but destroyed).
671 status_t (*delete) (ike_sa_t
*this);
674 * Update IKE_SAs after network interfaces have changed.
676 * Whenever the network interface configuration changes, the kernel
677 * interface calls roam() on each IKE_SA. The IKE_SA then checks if
678 * the new network config requires changes, and handles appropriate.
679 * If MOBIKE is supported, addresses are updated; If not, the tunnel is
682 * @param address TRUE if address list changed, FALSE otherwise
683 * @return SUCCESS, FAILED, DESTROY_ME
685 status_t (*roam
)(ike_sa_t
*this, bool address
);
688 * Processes a incoming IKEv2-Message.
690 * Message processing may fail. If a critical failure occurs,
691 * process_message() return DESTROY_ME. Then the caller must
692 * destroy the IKE_SA immediately, as it is unusable.
694 * @param message message to process
698 * - DESTROY_ME if this IKE_SA MUST be deleted
700 status_t (*process_message
) (ike_sa_t
*this, message_t
*message
);
703 * Generate a IKE message to send it to the peer.
705 * This method generates all payloads in the message and encrypts/signs
708 * @param message message to generate
709 * @param packet generated output packet
713 * - DESTROY_ME if this IKE_SA MUST be deleted
715 status_t (*generate_message
) (ike_sa_t
*this, message_t
*message
,
719 * Retransmits a request.
721 * @param message_id ID of the request to retransmit
724 * - NOT_FOUND if request doesn't have to be retransmited
726 status_t (*retransmit
) (ike_sa_t
*this, u_int32_t message_id
);
729 * Sends a DPD request to the peer.
731 * To check if a peer is still alive, periodic
732 * empty INFORMATIONAL messages are sent if no
733 * other traffic was received.
737 * - DESTROY_ME, if peer did not respond
739 status_t (*send_dpd
) (ike_sa_t
*this);
742 * Sends a keep alive packet.
744 * To refresh NAT tables in a NAT router
745 * between the peers, periodic empty
746 * UDP packets are sent if no other traffic
749 void (*send_keepalive
) (ike_sa_t
*this);
752 * Get the keying material of this IKE_SA.
754 * @return per IKE_SA keymat instance
756 keymat_t
* (*get_keymat
)(ike_sa_t
*this);
759 * Associates a child SA to this IKE SA
761 * @param child_sa child_sa to add
763 void (*add_child_sa
) (ike_sa_t
*this, child_sa_t
*child_sa
);
766 * Get a CHILD_SA identified by protocol and SPI.
768 * @param protocol protocol of the SA
769 * @param spi SPI of the CHILD_SA
770 * @param inbound TRUE if SPI is inbound, FALSE if outbound
771 * @return child_sa, or NULL if none found
773 child_sa_t
* (*get_child_sa
) (ike_sa_t
*this, protocol_id_t protocol
,
774 u_int32_t spi
, bool inbound
);
777 * Get the number of CHILD_SAs.
779 * @return number of CHILD_SAs
781 int (*get_child_count
) (ike_sa_t
*this);
784 * Create an enumerator over all CHILD_SAs.
788 enumerator_t
* (*create_child_sa_enumerator
) (ike_sa_t
*this);
791 * Remove the CHILD_SA the given enumerator points to from this IKE_SA.
793 * @param enumerator enumerator pointing to CHILD_SA
795 void (*remove_child_sa
) (ike_sa_t
*this, enumerator_t
*enumerator
);
798 * Rekey the CHILD SA with the specified reqid.
800 * Looks for a CHILD SA owned by this IKE_SA, and start the rekeing.
802 * @param protocol protocol of the SA
803 * @param spi inbound SPI of the CHILD_SA
805 * - NOT_FOUND, if IKE_SA has no such CHILD_SA
806 * - SUCCESS, if rekeying initiated
808 status_t (*rekey_child_sa
) (ike_sa_t
*this, protocol_id_t protocol
, u_int32_t spi
);
811 * Close the CHILD SA with the specified protocol/SPI.
813 * Looks for a CHILD SA owned by this IKE_SA, deletes it and
814 * notify's the remote peer about the delete. The associated
815 * states and policies in the kernel get deleted, if they exist.
817 * @param protocol protocol of the SA
818 * @param spi inbound SPI of the CHILD_SA
820 * - NOT_FOUND, if IKE_SA has no such CHILD_SA
821 * - SUCCESS, if delete message sent
823 status_t (*delete_child_sa
) (ike_sa_t
*this, protocol_id_t protocol
, u_int32_t spi
);
826 * Destroy a CHILD SA with the specified protocol/SPI.
828 * Looks for a CHILD SA owned by this IKE_SA and destroys it.
830 * @param protocol protocol of the SA
831 * @param spi inbound SPI of the CHILD_SA
833 * - NOT_FOUND, if IKE_SA has no such CHILD_SA
836 status_t (*destroy_child_sa
) (ike_sa_t
*this, protocol_id_t protocol
, u_int32_t spi
);
841 * Sets up a new IKE_SA, moves all CHILDs to it and deletes this IKE_SA.
843 * @return - SUCCESS, if IKE_SA rekeying initiated
845 status_t (*rekey
) (ike_sa_t
*this);
848 * Reauthenticate the IKE_SA.
850 * Create a completely new IKE_SA with authentication, recreates all children
851 * within the IKE_SA, closes this IKE_SA.
853 * @return DESTROY_ME to destroy the IKE_SA
855 status_t (*reauth
) (ike_sa_t
*this);
858 * Restablish the IKE_SA.
860 * Reestablish an IKE_SA after it has been closed.
862 * @return DESTROY_ME to destroy the IKE_SA
864 status_t (*reestablish
) (ike_sa_t
*this);
867 * Set the lifetime limit received from a AUTH_LIFETIME notify.
869 * @param lifetime lifetime in seconds
871 void (*set_auth_lifetime
)(ike_sa_t
*this, u_int32_t lifetime
);
874 * Set the virtual IP to use for this IKE_SA and its children.
876 * The virtual IP is assigned per IKE_SA, not per CHILD_SA. It has the same
877 * lifetime as the IKE_SA.
879 * @param local TRUE to set local address, FALSE for remote
880 * @param ip IP to set as virtual IP
882 void (*set_virtual_ip
) (ike_sa_t
*this, bool local
, host_t
*ip
);
885 * Get the virtual IP configured.
887 * @param local TRUE to get local virtual IP, FALSE for remote
888 * @return host_t *virtual IP
890 host_t
* (*get_virtual_ip
) (ike_sa_t
*this, bool local
);
893 * Register a configuration attribute to the IKE_SA.
895 * If an IRAS sends a configuration attribute it is installed and
896 * registered at the IKE_SA. Attributes are inherit()ed and get released
897 * when the IKE_SA is closed.
899 * @param handler handler installed the attribute, use for release()
900 * @param type configuration attribute type
901 * @param data associated attribute data
903 void (*add_configuration_attribute
)(ike_sa_t
*this,
904 attribute_handler_t
*handler
,
905 configuration_attribute_type_t type
, chunk_t data
);
908 * Set local and remote host addresses to be used for IKE.
910 * These addresses are communicated via the KMADDRESS field of a MIGRATE
911 * message sent via the NETLINK or PF _KEY kernel socket interface.
913 * @param local local kmaddress
914 * @param remote remote kmaddress
916 void (*set_kmaddress
) (ike_sa_t
*this, host_t
*local
, host_t
*remote
);
919 * Create enumerator over a task queue of this IKE_SA.
921 * @param queue type to enumerate
922 * @return enumerator over task_t
924 enumerator_t
* (*create_task_enumerator
)(ike_sa_t
*this, task_queue_t queue
);
927 * Inherit all attributes of other to this after rekeying.
929 * When rekeying is completed, all CHILD_SAs, the virtual IP and all
930 * outstanding tasks are moved from other to this.
931 * As this call may initiate inherited tasks, a status is returned.
933 * @param other other task to inherit from
935 void (*inherit
) (ike_sa_t
*this, ike_sa_t
*other
);
938 * Reset the IKE_SA, useable when initiating fails
940 void (*reset
) (ike_sa_t
*this);
943 * Destroys a ike_sa_t object.
945 void (*destroy
) (ike_sa_t
*this);
949 * Creates an ike_sa_t object with a specific ID.
951 * @param ike_sa_id ike_sa_id_t object to associate with new IKE_SA
952 * @return ike_sa_t object
954 ike_sa_t
*ike_sa_create(ike_sa_id_t
*ike_sa_id
);
956 #endif /** IKE_SA_H_ @}*/