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_sa_state_t ike_sa_state_t
;
29 typedef struct ike_sa_t ike_sa_t
;
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>
45 * Timeout in milliseconds after that a half open IKE_SA gets deleted.
49 #define HALF_OPEN_IKE_SA_TIMEOUT 30000
52 * 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.
63 #define RETRY_INTERVAL 30
66 * Jitter to subtract from RETRY_INTERVAL to randomize rekey retry.
70 #define RETRY_JITTER 20
74 * @brief State of an IKE_SA.
76 * An IKE_SA passes various states in its lifetime. A newly created
77 * SA is in the state CREATED.
83 on initiate()---> ¦ <----- on IKE_SA_INIT received
89 ¦ <----- on IKE_AUTH successfully completed
92 ¦ SA_ESTABLISHED ¦-------------------------+ <-- on rekeying
95 on delete()---> ¦ <----- on IKE_SA +-------------+
96 ¦ delete request ¦ SA_REKEYING ¦
97 ¦ received +-------------+
100 ¦ SA_DELETING ¦<------------------------+ <-- after rekeying
103 ¦ <----- after delete() acknowledged
112 enum ike_sa_state_t
{
115 * IKE_SA just got created, but is not initiating nor responding yet.
120 * IKE_SA gets initiated actively or passively
125 * IKE_SA is fully established
130 * IKE_SA rekeying in progress
135 * IKE_SA is in progress of deletion
141 * enum names for ike_sa_state_t.
143 extern enum_name_t
*ike_sa_state_names
;
146 * @brief Class ike_sa_t representing an IKE_SA.
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.
161 * @brief Get the id of the SA.
163 * Returned ike_sa_id_t object is not getting cloned!
165 * @param this calling object
166 * @return ike_sa's ike_sa_id_t
168 ike_sa_id_t
* (*get_id
) (ike_sa_t
*this);
171 * @brief Get the numerical ID uniquely defining this IKE_SA.
173 * @param this calling object
176 u_int32_t (*get_unique_id
) (ike_sa_t
*this);
179 * @brief Get the state of the IKE_SA.
181 * @param this calling object
182 * @return state of the IKE_SA
184 ike_sa_state_t (*get_state
) (ike_sa_t
*this);
187 * @brief Get some statistics about this IKE_SA.
189 * @param next_rekeying when the next rekeying is scheduled
191 void (*get_stats
)(ike_sa_t
*this, u_int32_t
*next_rekeying
);
194 * @brief Set the state of the IKE_SA.
196 * @param this calling object
197 * @param state state to set for the IKE_SA
199 void (*set_state
) (ike_sa_t
*this, ike_sa_state_t ike_sa
);
202 * @brief Get the name of the connection this IKE_SA uses.
204 * @param this calling object
207 char* (*get_name
) (ike_sa_t
*this);
210 * @brief Get the own host address.
212 * @param this calling object
213 * @return host address
215 host_t
* (*get_my_host
) (ike_sa_t
*this);
218 * @brief Set the own host address.
220 * @param this calling object
221 * @param me host address
223 void (*set_my_host
) (ike_sa_t
*this, host_t
*me
);
226 * @brief Get the other peers host address.
228 * @param this calling object
229 * @return host address
231 host_t
* (*get_other_host
) (ike_sa_t
*this);
234 * @brief Set the others host address.
236 * @param this calling object
237 * @param other host address
239 void (*set_other_host
) (ike_sa_t
*this, host_t
*other
);
242 * @brief Get the own identification.
244 * @param this calling object
245 * @return identification
247 identification_t
* (*get_my_id
) (ike_sa_t
*this);
250 * @brief Set the own identification.
252 * @param this calling object
253 * @param me identification
255 void (*set_my_id
) (ike_sa_t
*this, identification_t
*me
);
258 * @brief Get the other peers identification.
260 * @param this calling object
261 * @return identification
263 identification_t
* (*get_other_id
) (ike_sa_t
*this);
266 * @brief Set the other peers identification.
268 * @param this calling object
269 * @param other identification
271 void (*set_other_id
) (ike_sa_t
*this, identification_t
*other
);
274 * @brief Get the config used to setup this IKE_SA.
276 * @param this calling object
279 ike_cfg_t
* (*get_ike_cfg
) (ike_sa_t
*this);
282 * @brief Set the config to setup this IKE_SA.
284 * @param this calling object
285 * @param config ike_config to use
287 void (*set_ike_cfg
) (ike_sa_t
*this, ike_cfg_t
* config
);
290 * @brief Get the peer config used by this IKE_SA.
292 * @param this calling object
293 * @return peer_config
295 peer_cfg_t
* (*get_peer_cfg
) (ike_sa_t
*this);
298 * @brief Set the peer config to use with this IKE_SA.
300 * @param this calling object
301 * @param config peer_config to use
303 void (*set_peer_cfg
) (ike_sa_t
*this, peer_cfg_t
*config
);
306 * @brief Initiate a new connection.
308 * The configs are owned by the IKE_SA after the call.
310 * @param this calling object
311 * @param child_cfg child config to create CHILD from
313 * - SUCCESS if initialization started
314 * - DESTROY_ME if initialization failed
316 status_t (*initiate
) (ike_sa_t
*this, child_cfg_t
*child_cfg
);
319 * @brief Route a policy in the kernel.
321 * Installs the policies in the kernel. If traffic matches,
322 * the kernel requests connection setup from the IKE_SA via acquire().
324 * @param this calling object
325 * @param child_cfg child config to route
327 * - SUCCESS if routed successfully
328 * - FAILED if routing failed
330 status_t (*route
) (ike_sa_t
*this, child_cfg_t
*child_cfg
);
333 * @brief Unroute a policy in the kernel previously routed.
335 * @param this calling object
336 * @param reqid reqid of CHILD_SA to unroute
338 * - SUCCESS if route removed
339 * - NOT_FOUND if CHILD_SA not found
340 * - DESTROY_ME if last CHILD_SA was unrouted
342 status_t (*unroute
) (ike_sa_t
*this, u_int32_t reqid
);
345 * @brief Acquire connection setup for an installed kernel policy.
347 * If an installed policy raises an acquire, the kernel calls
348 * this function to establish the CHILD_SA (and maybe the IKE_SA).
350 * @param this calling object
351 * @param reqid reqid of the CHILD_SA the policy belongs to.
353 * - SUCCESS if initialization started
354 * - DESTROY_ME if initialization failed
356 status_t (*acquire
) (ike_sa_t
*this, u_int32_t reqid
);
359 * @brief Initiates the deletion of an IKE_SA.
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.
365 * @param this calling object
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).
372 status_t (*delete) (ike_sa_t
*this);
375 * @brief Processes a incoming IKEv2-Message.
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.
381 * @param this calling object
382 * @param message message to process
386 * - DESTROY_ME if this IKE_SA MUST be deleted
388 status_t (*process_message
) (ike_sa_t
*this, message_t
*message
);
391 * @brief Generate a IKE message to send it to the peer.
393 * This method generates all payloads in the message and encrypts/signs
396 * @param this calling object
397 * @param message message to generate
398 * @param packet generated output packet
402 * - DESTROY_ME if this IKE_SA MUST be deleted
404 status_t (*generate_message
) (ike_sa_t
*this, message_t
*message
,
408 * @brief Retransmits a request.
410 * @param this calling object
411 * @param message_id ID of the request to retransmit
414 * - NOT_FOUND if request doesn't have to be retransmited
416 status_t (*retransmit
) (ike_sa_t
*this, u_int32_t message_id
);
419 * @brief Sends a DPD request to the peer.
421 * To check if a peer is still alive, periodic
422 * empty INFORMATIONAL messages are sent if no
423 * other traffic was received.
425 * @param this calling object
428 * - DESTROY_ME, if peer did not respond
430 status_t (*send_dpd
) (ike_sa_t
*this);
433 * @brief Sends a keep alive packet.
435 * To refresh NAT tables in a NAT router
436 * between the peers, periodic empty
437 * UDP packets are sent if no other traffic
440 * @param this calling object
442 void (*send_keepalive
) (ike_sa_t
*this);
445 * @brief Check if NAT traversal is enabled for this IKE_SA.
447 * @param this calling object
448 * @return TRUE if NAT traversal enabled
450 bool (*is_natt_enabled
) (ike_sa_t
*this);
453 * @brief Enable NAT detection for this IKE_SA.
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
462 * @param this calling object
463 * @param local TRUE, if we are NATted, FALSE if other
465 void (*enable_natt
) (ike_sa_t
*this, bool local
);
468 * @brief Derive all keys and create the transforms for IKE communication.
470 * Keys are derived using the diffie hellman secret, nonces and internal
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.
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
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
);
490 * @brief Get a multi purpose prf for the negotiated PRF function.
492 * @param this calling object
493 * @return pointer to prf_t object
495 prf_t
*(*get_prf
) (ike_sa_t
*this);
498 * @brief Get the prf-object, which is used to derive keys for child SAs.
500 * @param this calling object
501 * @return pointer to prf_t object
503 prf_t
*(*get_child_prf
) (ike_sa_t
*this);
506 * @brief Get the key to build outgoing authentication data.
508 * @param this calling object
509 * @return pointer to prf_t object
511 chunk_t (*get_skp_build
) (ike_sa_t
*this);
514 * @brief Get the key to verify incoming authentication data.
516 * @param this calling object
517 * @return pointer to prf_t object
519 chunk_t (*get_skp_verify
) (ike_sa_t
*this);
522 * @brief Associates a child SA to this IKE SA
524 * @param this calling object
525 * @param child_sa child_sa to add
527 void (*add_child_sa
) (ike_sa_t
*this, child_sa_t
*child_sa
);
530 * @brief Get a CHILD_SA identified by protocol and SPI.
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
538 child_sa_t
* (*get_child_sa
) (ike_sa_t
*this, protocol_id_t protocol
,
539 u_int32_t spi
, bool inbound
);
542 * @brief Create an iterator over all CHILD_SAs.
544 * @param this calling object
547 iterator_t
* (*create_child_sa_iterator
) (ike_sa_t
*this);
550 * @brief Rekey the CHILD SA with the specified reqid.
552 * Looks for a CHILD SA owned by this IKE_SA, and start the rekeing.
554 * @param this calling object
555 * @param protocol protocol of the SA
556 * @param spi inbound SPI of the CHILD_SA
558 * - NOT_FOUND, if IKE_SA has no such CHILD_SA
559 * - SUCCESS, if rekeying initiated
561 status_t (*rekey_child_sa
) (ike_sa_t
*this, protocol_id_t protocol
, u_int32_t spi
);
564 * @brief Close the CHILD SA with the specified protocol/SPI.
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.
570 * @param this calling object
571 * @param protocol protocol of the SA
572 * @param spi inbound SPI of the CHILD_SA
574 * - NOT_FOUND, if IKE_SA has no such CHILD_SA
575 * - SUCCESS, if delete message sent
577 status_t (*delete_child_sa
) (ike_sa_t
*this, protocol_id_t protocol
, u_int32_t spi
);
580 * @brief Destroy a CHILD SA with the specified protocol/SPI.
582 * Looks for a CHILD SA owned by this IKE_SA and destroys it.
584 * @param this calling object
585 * @param protocol protocol of the SA
586 * @param spi inbound SPI of the CHILD_SA
588 * - NOT_FOUND, if IKE_SA has no such CHILD_SA
591 status_t (*destroy_child_sa
) (ike_sa_t
*this, protocol_id_t protocol
, u_int32_t spi
);
594 * @brief Rekey the IKE_SA.
596 * Sets up a new IKE_SA, moves all CHILDs to it and deletes this IKE_SA.
598 * @param this calling object
599 * @return - SUCCESS, if IKE_SA rekeying initiated
601 status_t (*rekey
) (ike_sa_t
*this);
604 * @brief Restablish the IKE_SA.
606 * Create a completely new IKE_SA with authentication, recreates all children
607 * within the IKE_SA, but lets the old IKE_SA untouched.
609 * @param this calling object
611 void (*reestablish
) (ike_sa_t
*this);
614 * @brief Set the virtual IP to use for this IKE_SA and its children.
616 * The virtual IP is assigned per IKE_SA, not per CHILD_SA. It has the same
617 * lifetime as the IKE_SA.
619 * @param this calling object
621 void (*set_virtual_ip
) (ike_sa_t
*this, bool local
, host_t
*ip
);
624 * @brief Get the virtual IP configured.
626 * @param this calling object
627 * @param local TRUE to get local virtual IP, FALSE for remote
629 host_t
* (*get_virtual_ip
) (ike_sa_t
*this, bool local
);
632 * @brief Add a DNS server to the system.
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.
637 * @param this calling object
638 * @param dns DNS server to install on the system
640 void (*add_dns_server
) (ike_sa_t
*this, host_t
*dns
);
643 * @brief Inherit all attributes of other to this after rekeying.
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.
649 * @param this calling object
650 * @param other other task to inherit from
651 * @return DESTROY_ME if initiation of inherited task failed
653 status_t (*inherit
) (ike_sa_t
*this, ike_sa_t
*other
);
656 * @brief Reset the IKE_SA, useable when initiating fails
658 * @param this calling object
660 void (*reset
) (ike_sa_t
*this);
663 * @brief Destroys a ike_sa_t object.
665 * @param this calling object
667 void (*destroy
) (ike_sa_t
*this);
671 * @brief Creates an ike_sa_t object with a specific ID.
673 * @param ike_sa_id ike_sa_id_t object to associate with new IKE_SA
674 * @return ike_sa_t object
678 ike_sa_t
*ike_sa_create(ike_sa_id_t
*ike_sa_id
);
680 #endif /* IKE_SA_H_ */