add priority management for kernel policy
[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 #include <types.h>
29 #include <encoding/message.h>
30 #include <encoding/payloads/proposal_substructure.h>
31 #include <sa/ike_sa_id.h>
32 #include <sa/child_sa.h>
33 #include <config/configuration.h>
34 #include <utils/logger.h>
35 #include <utils/randomizer.h>
36 #include <crypto/prfs/prf.h>
37 #include <crypto/crypters/crypter.h>
38 #include <crypto/signers/signer.h>
39 #include <config/connections/connection.h>
40 #include <config/policies/policy.h>
41 #include <config/proposal.h>
42 #include <utils/logger.h>
43
44
45 typedef enum ike_sa_state_t ike_sa_state_t;
46
47 /**
48 * @brief State of an IKE_SA.
49 *
50 * An IKE_SA passes various states in its lifetime. A newly created
51 * SA is in the state CREATED.
52 * @verbatim
53 +----------------+
54 ¦ SA_CREATED ¦
55 +----------------+
56 ¦
57 on initiate()---> ¦ <----- on IKE_SA_INIT received
58 V
59 +----------------+
60 ¦ SA_CONNECTING ¦
61 +----------------+
62 ¦
63 ¦ <----- on IKE_AUTH successfully completed
64 V
65 +----------------+
66 ¦ SA_ESTABLISHED ¦-------------------------+ <-- on rekeying
67 +----------------+ ¦
68 ¦ V
69 on delete()---> ¦ <----- on IKE_SA +-------------+
70 ¦ delete request ¦ SA_REKEYING ¦
71 ¦ received +-------------+
72 V ¦
73 +----------------+ ¦
74 ¦ SA_DELETING ¦<------------------------+ <-- after rekeying
75 +----------------+
76 ¦
77 ¦ <----- after delete() acknowledged
78 ¦
79 \V/
80 X
81 / \
82 @endverbatim
83 *
84 * @ingroup sa
85 */
86 enum ike_sa_state_t {
87
88 /**
89 * IKE_SA just got created, but is not initiating nor responding yet.
90 */
91 IKE_CREATED,
92
93 /**
94 * IKE_SA gets initiated actively or passively
95 */
96 IKE_CONNECTING,
97
98 /**
99 * IKE_SA is fully established
100 */
101 IKE_ESTABLISHED,
102
103 /**
104 * IKE_SA rekeying in progress
105 */
106 IKE_REKEYING,
107
108 /**
109 * IKE_SA is in progress of deletion
110 */
111 IKE_DELETING,
112 };
113
114 /**
115 * String mappings for ike_sa_state_t.
116 */
117 extern mapping_t ike_sa_state_m[];
118
119
120 typedef struct ike_sa_t ike_sa_t;
121
122 /**
123 * @brief Class ike_sa_t representing an IKE_SA.
124 *
125 * An IKE_SA contains crypto information related to a connection
126 * with a peer. It contains multiple IPsec CHILD_SA, for which
127 * it is responsible. All traffic is handled by an IKE_SA, using
128 * transactions.
129 *
130 * @b Constructors:
131 * - ike_sa_create()
132 *
133 * @ingroup sa
134 */
135 struct ike_sa_t {
136
137 /**
138 * @brief Get the id of the SA.
139 *
140 * Returned ike_sa_id_t object is not getting cloned!
141 *
142 * @param this calling object
143 * @return ike_sa's ike_sa_id_t
144 */
145 ike_sa_id_t* (*get_id) (ike_sa_t *this);
146
147 /**
148 * @brief Get the state of the IKE_SA.
149 *
150 * @param this calling object
151 * @return state of the IKE_SA
152 */
153 ike_sa_state_t (*get_state) (ike_sa_t *this);
154
155 /**
156 * @brief Set the state of the IKE_SA.
157 *
158 * @param this calling object
159 * @param state state to set for the IKE_SA
160 */
161 void (*set_state) (ike_sa_t *this, ike_sa_state_t ike_sa);
162
163 /**
164 * @brief Get the name of the connection this IKE_SA uses.
165 *
166 * @param this calling object
167 * @return name
168 */
169 char* (*get_name) (ike_sa_t *this);
170
171 /**
172 * @brief Set the name of the connection this IKE_SA uses.
173 *
174 * @param this calling object
175 * @param name name, gets cloned
176 */
177 void (*set_name) (ike_sa_t *this, char* name);
178
179 /**
180 * @brief Get the own host address.
181 *
182 * @param this calling object
183 * @return host address
184 */
185 host_t* (*get_my_host) (ike_sa_t *this);
186
187 /**
188 * @brief Set the own host address.
189 *
190 * @param this calling object
191 * @param me host address
192 */
193 void (*set_my_host) (ike_sa_t *this, host_t *me);
194
195 /**
196 * @brief Get the other peers host address.
197 *
198 * @param this calling object
199 * @return host address
200 */
201 host_t* (*get_other_host) (ike_sa_t *this);
202
203 /**
204 * @brief Set the others host address.
205 *
206 * @param this calling object
207 * @param other host address
208 */
209 void (*set_other_host) (ike_sa_t *this, host_t *other);
210
211 /**
212 * @brief Get the own identification.
213 *
214 * @param this calling object
215 * @return identification
216 */
217 identification_t* (*get_my_id) (ike_sa_t *this);
218
219 /**
220 * @brief Set the own identification.
221 *
222 * @param this calling object
223 * @param me identification
224 */
225 void (*set_my_id) (ike_sa_t *this, identification_t *me);
226
227 /**
228 * @brief Get the other peers identification.
229 *
230 * @param this calling object
231 * @return identification
232 */
233 identification_t* (*get_other_id) (ike_sa_t *this);
234
235 /**
236 * @brief Set the other peers identification.
237 *
238 * @param this calling object
239 * @param other identification
240 */
241 void (*set_other_id) (ike_sa_t *this, identification_t *other);
242
243 /**
244 * @brief Initiate a new connection.
245 *
246 * The policy/connection is owned by the IKE_SA after the call, so
247 * do not modify or destroy it.
248 *
249 * @param this calling object
250 * @param connection connection to initiate
251 * @param policy policy to set up
252 * @return
253 * - SUCCESS if initialization started
254 * - DESTROY_ME if initialization failed and IKE_SA MUST be deleted
255 */
256 status_t (*initiate) (ike_sa_t *this, connection_t *connection, policy_t *policy);
257
258 /**
259 * @brief Route a policy in the kernel.
260 *
261 * Installs the policies in the kernel. If traffic matches,
262 * the kernel requests connection setup from the IKE_SA via acquire().
263 *
264 * @param this calling object
265 * @param connection connection definition used for routing
266 * @param policy policy to route
267 * @return
268 * - SUCCESS if routed successfully
269 * - FAILED if routing failed
270 */
271 status_t (*route) (ike_sa_t *this, connection_t *connection, policy_t *policy);
272
273 /**
274 * @brief Unroute a policy in the kernel previously routed.
275 *
276 * @param this calling object
277 * @param policy policy to route
278 * @return
279 * - SUCCESS if route removed
280 * - DESTROY_ME if last route was removed from
281 * an IKE_SA which was not established
282 */
283 status_t (*unroute) (ike_sa_t *this, policy_t *policy);
284
285 /**
286 * @brief Acquire connection setup for a policy.
287 *
288 * If an installed policy raises an acquire, the kernel calls
289 * this function to establish the CHILD_SA (and maybe the IKE_SA).
290 *
291 * @param this calling object
292 * @param reqid reqid of the CHILD_SA the policy belongs to.
293 * @return
294 * - SUCCESS if initialization started
295 * - DESTROY_ME if initialization failed and IKE_SA MUST be deleted
296 */
297 status_t (*acquire) (ike_sa_t *this, u_int32_t reqid);
298
299 /**
300 * @brief Initiates the deletion of an IKE_SA.
301 *
302 * Sends a delete message to the remote peer and waits for
303 * its response. If the response comes in, or a timeout occurs,
304 * the IKE SA gets deleted.
305 *
306 * @param this calling object
307 * @return
308 * - SUCCESS if deletion is initialized
309 * - INVALID_STATE, if the IKE_SA is not in
310 * an established state and can not be
311 * delete (but destroyed).
312 */
313 status_t (*delete) (ike_sa_t *this);
314
315 /**
316 * @brief Retransmits a request.
317 *
318 * @param this calling object
319 * @param message_id ID of the request to retransmit
320 * @return
321 * - SUCCESS
322 * - NOT_FOUND if request doesn't have to be retransmited
323 */
324 status_t (*retransmit_request) (ike_sa_t *this, u_int32_t message_id);
325
326 /**
327 * @brief Processes a incoming IKEv2-Message.
328 *
329 * Message processing may fail. If a critical failure occurs,
330 * process_message() return DESTROY_ME. Then the caller must
331 * destroy the IKE_SA immediatly, as it is unusable.
332 *
333 * @param this calling object
334 * @param[in] message message to process
335 * @return
336 * - SUCCESS
337 * - FAILED
338 * - DESTROY_ME if this IKE_SA MUST be deleted
339 */
340 status_t (*process_message) (ike_sa_t *this, message_t *message);
341
342 /**
343 * @brief Get the next message ID for a request.
344 *
345 * @param this calling object
346 * @return the next message id
347 */
348 u_int32_t (*get_next_message_id) (ike_sa_t *this);
349
350 /**
351 * @brief Check if NAT traversal is enabled for this IKE_SA.
352 *
353 * @param this calling object
354 * @return TRUE if NAT traversal enabled
355 */
356 bool (*is_natt_enabled) (ike_sa_t *this);
357
358 /**
359 * @brief Enable NAT detection for this IKE_SA.
360 *
361 * If a Network address translation is detected with
362 * NAT_DETECTION notifys, a SA must switch to ports
363 * 4500. To enable this behavior, call enable_natt().
364 * It is relevant which peer is NATted, this is specified
365 * with the "local" parameter. Call it twice when both
366 * are NATted.
367 *
368 * @param this calling object
369 * @param local TRUE, if we are NATted, FALSE if other
370 */
371 void (*enable_natt) (ike_sa_t *this, bool local);
372
373 /**
374 * @brief Sends a DPD request to the peer.
375 *
376 * To check if a peer is still alive, periodic
377 * empty INFORMATIONAL messages are sent if no
378 * other traffic was received.
379 *
380 * @param this calling object
381 * @return
382 * - SUCCESS
383 * - DESTROY_ME, if peer did not respond
384 */
385 status_t (*send_dpd) (ike_sa_t *this);
386
387 /**
388 * @brief Sends a keep alive packet.
389 *
390 * To refresh NAT tables in a NAT router
391 * between the peers, periodic empty
392 * UDP packets are sent if no other traffic
393 * was sent.
394 *
395 * @param this calling object
396 */
397 void (*send_keepalive) (ike_sa_t *this);
398
399 /**
400 * @brief Log the status of a the ike sa to a logger.
401 *
402 * The status of the IKE SA and all child SAs is logged.
403 * Supplying NULL as logger uses the internal child_sa logger
404 * to do the logging. The log is only done if the supplied
405 * connection name is NULL or matches the connections name.
406 *
407 * @param this calling object
408 * @param logger logger to use for logging
409 * @param name name of the connection
410 */
411 void (*log_status) (ike_sa_t *this, logger_t *logger, char *name);
412
413 /**
414 * @brief Derive all keys and create the transforms for IKE communication.
415 *
416 * Keys are derived using the diffie hellman secret, nonces and internal
417 * stored SPIs.
418 * Key derivation differs when an IKE_SA is set up to replace an
419 * existing IKE_SA (rekeying). The SK_d key from the old IKE_SA
420 * is included in the derivation process.
421 *
422 * @param this calling object
423 * @param proposal proposal which contains algorithms to use
424 * @param dh diffie hellman object with shared secret
425 * @param nonce_i initiators nonce
426 * @param nonce_r responders nonce
427 * @param initiator TRUE if initiator, FALSE otherwise
428 * @param child_prf PRF with SK_d key when rekeying, NULL otherwise
429 * @param old_prf general purpose PRF of old SA when rekeying
430 */
431 status_t (*derive_keys)(ike_sa_t *this, proposal_t* proposal,
432 diffie_hellman_t *dh,
433 chunk_t nonce_i, chunk_t nonce_r,
434 bool initiator, prf_t *child_prf, prf_t *old_prf);
435
436 /**
437 * @brief Get the multi purpose prf.
438 *
439 * @param this calling object
440 * @return pointer to prf_t object
441 */
442 prf_t *(*get_prf) (ike_sa_t *this);
443
444 /**
445 * @brief Get the prf-object, which is used to derive keys for child SAs.
446 *
447 * @param this calling object
448 * @return pointer to prf_t object
449 */
450 prf_t *(*get_child_prf) (ike_sa_t *this);
451
452 /**
453 * @brief Get the prf used for authentication of initiator.
454 *
455 * @param this calling object
456 * @return pointer to prf_t object
457 */
458 prf_t *(*get_prf_auth_i) (ike_sa_t *this);
459
460 /**
461 * @brief Get the prf used for authentication of responder.
462 *
463 * @param this calling object
464 * @return pointer to prf_t object
465 */
466 prf_t *(*get_prf_auth_r) (ike_sa_t *this);
467
468 /**
469 * @brief Associates a child SA to this IKE SA
470 *
471 * @param this calling object
472 * @param child_sa child_sa to add
473 */
474 void (*add_child_sa) (ike_sa_t *this, child_sa_t *child_sa);
475
476 /**
477 * @brief Check if an IKE_SA has one or more CHILD_SAs with a given reqid.
478 *
479 * @param this calling object
480 * @param reqid reqid of the CHILD
481 * @return TRUE if it has such a CHILD, FALSE if not
482 */
483 bool (*has_child_sa) (ike_sa_t *this, u_int32_t reqid);
484
485 /**
486 * @brief Get a CHILD_SA identified by protocol and SPI.
487 *
488 * @param this calling object
489 * @param protocol protocol of the SA
490 * @param spi SPI of the CHILD_SA
491 * @param inbound TRUE if SPI is inbound, FALSE if outbound
492 * @return child_sa, or NULL if none found
493 */
494 child_sa_t* (*get_child_sa) (ike_sa_t *this, protocol_id_t protocol,
495 u_int32_t spi, bool inbound);
496
497 /**
498 * @brief Create an iterator over all CHILD_SAs.
499 *
500 * @param this calling object
501 * @return iterator
502 */
503 iterator_t* (*create_child_sa_iterator) (ike_sa_t *this);
504
505 /**
506 * @brief Rekey the CHILD SA with the specified reqid.
507 *
508 * Looks for a CHILD SA owned by this IKE_SA, and start the rekeing.
509 *
510 * @param this calling object
511 * @param protocol protocol of the SA
512 * @param spi inbound SPI of the CHILD_SA
513 * @return
514 * - NOT_FOUND, if IKE_SA has no such CHILD_SA
515 * - SUCCESS, if rekeying initiated
516 */
517 status_t (*rekey_child_sa) (ike_sa_t *this, protocol_id_t protocol, u_int32_t spi);
518
519 /**
520 * @brief Close the CHILD SA with the specified protocol/SPI.
521 *
522 * Looks for a CHILD SA owned by this IKE_SA, deletes it and
523 * notify's the remote peer about the delete. The associated
524 * states and policies in the kernel get deleted, if they exist.
525 *
526 * @param this calling object
527 * @param protocol protocol of the SA
528 * @param spi inbound SPI of the CHILD_SA
529 * @return
530 * - NOT_FOUND, if IKE_SA has no such CHILD_SA
531 * - SUCCESS, if delete message sent
532 */
533 status_t (*delete_child_sa) (ike_sa_t *this, protocol_id_t protocol, u_int32_t spi);
534
535 /**
536 * @brief Destroy a CHILD SA with the specified protocol/SPI.
537 *
538 * Looks for a CHILD SA owned by this IKE_SA and destroys it.
539 *
540 * @param this calling object
541 * @param protocol protocol of the SA
542 * @param spi inbound SPI of the CHILD_SA
543 * @return
544 * - NOT_FOUND, if IKE_SA has no such CHILD_SA
545 * - SUCCESS
546 */
547 status_t (*destroy_child_sa) (ike_sa_t *this, protocol_id_t protocol, u_int32_t spi);
548
549 /**
550 * @brief Set lifetimes of an IKE_SA.
551 *
552 * Two lifetimes are specified. The soft_lifetime says, when rekeying should
553 * be initiated. The hard_lifetime says, when the IKE_SA has been expired
554 * and must be deleted. Normally, hard_lifetime > soft_lifetime, and
555 * hard_lifetime is only reached when rekeying at soft_lifetime fails.
556 *
557 * @param this calling object
558 * @param soft_lifetime soft_lifetime
559 * @param hard_lifetime hard_lifetime
560 */
561 void (*set_lifetimes) (ike_sa_t *this,
562 u_int32_t soft_lifetime, u_int32_t hard_lifetime);
563
564 /**
565 * @brief Rekey the IKE_SA.
566 *
567 * Sets up a new IKE_SA, moves all CHILDs to it and deletes this IKE_SA.
568 *
569 * @param this calling object
570 * @return - SUCCESS, if IKE_SA rekeying initiated
571 */
572 status_t (*rekey) (ike_sa_t *this);
573
574 /**
575 * @brief Get the transaction which rekeys this IKE_SA.
576 *
577 * @todo Fix include for rekey_ike_sa.h
578 *
579 * @param this calling object
580 * @return rekey_ike_sa_t transaction or NULL
581 */
582 void* (*get_rekeying_transaction) (ike_sa_t *this);
583
584 /**
585 * @brief Set the transaction which rekeys this IKE_SA.
586 *
587 * @param this calling object
588 * @param rekey rekey_ike_sa_t transaction or NULL
589 */
590 void (*set_rekeying_transaction) (ike_sa_t *this, void *rekey);
591
592 /**
593 * @brief Move all children from other IKE_SA to this IKE_SA.
594 *
595 * After rekeying completes, all children are switched over to the
596 * newly created IKE_SA.
597 *
598 * @param this stepfather
599 * @param other deceased (rekeyed) IKE_SA
600 */
601 void (*adopt_children) (ike_sa_t *this, ike_sa_t *other);
602
603 /**
604 * @brief Destroys a ike_sa_t object.
605 *
606 * @param this calling object
607 */
608 void (*destroy) (ike_sa_t *this);
609 };
610
611 /**
612 * @brief Creates an ike_sa_t object with a specific ID.
613 *
614 * The ID gets cloned internally.
615 *
616 * @param ike_sa_id ike_sa_id_t object to associate with new IKE_SA
617 * @return ike_sa_t object
618 *
619 * @ingroup sa
620 */
621 ike_sa_t *ike_sa_create(ike_sa_id_t *ike_sa_id);
622
623 #endif /* IKE_SA_H_ */