fixing IKE_SA rekeying if charon.ikesa_table_size > 1
[strongswan.git] / src / charon / sa / ike_sa_manager.h
1 /*
2 * Copyright (C) 2008 Tobias Brunner
3 * Copyright (C) 2005-2008 Martin Willi
4 * Copyright (C) 2005 Jan Hutter
5 * Hochschule fuer Technik Rapperswil
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 * for more details.
16 *
17 * $Id$
18 */
19
20 /**
21 * @defgroup ike_sa_manager ike_sa_manager
22 * @{ @ingroup sa
23 */
24
25 #ifndef IKE_SA_MANAGER_H_
26 #define IKE_SA_MANAGER_H_
27
28 typedef struct ike_sa_manager_t ike_sa_manager_t;
29
30 #include <library.h>
31 #include <sa/ike_sa.h>
32 #include <encoding/message.h>
33 #include <config/peer_cfg.h>
34
35 /**
36 * Manages and synchronizes access to all IKE_SAs.
37 *
38 * To synchronize access to thread-unsave IKE_SAs, they are checked out for
39 * use and checked in afterwards. A checked out SA is exclusively accessible
40 * by the owning thread.
41 */
42 struct ike_sa_manager_t {
43
44 /**
45 * Checkout an existing IKE_SA.
46 *
47 * @param ike_sa_id the SA identifier, will be updated
48 * @returns
49 * - checked out IKE_SA if found
50 * - NULL, if specified IKE_SA is not found.
51 */
52 ike_sa_t* (*checkout) (ike_sa_manager_t* this, ike_sa_id_t *sa_id);
53
54 /**
55 * Create and check out a new IKE_SA.
56 *
57 * @note If initiator equals FALSE, the returned IKE_SA is not registered
58 * in the manager.
59 *
60 * @param initiator TRUE for initiator, FALSE otherwise
61 * @returns created and checked out IKE_SA
62 */
63 ike_sa_t* (*checkout_new) (ike_sa_manager_t* this, bool initiator);
64
65 /**
66 * Checkout an IKE_SA by a message.
67 *
68 * In some situations, it is necessary that the manager knows the
69 * message to use for the checkout. This has the following reasons:
70 *
71 * 1. If the targeted IKE_SA is already processing a message, we do not
72 * check it out if the message ID is the same.
73 * 2. If it is an IKE_SA_INIT request, we have to check if it is a
74 * retransmission. If so, we have to drop the message, we would
75 * create another unneeded IKE_SA for each retransmitted packet.
76 *
77 * A call to checkout_by_message() returns a (maybe new created) IKE_SA.
78 * If processing the message does not make sense (for the reasons above),
79 * NULL is returned.
80 *
81 * @param ike_sa_id the SA identifier, will be updated
82 * @returns
83 * - checked out/created IKE_SA
84 * - NULL to not process message further
85 */
86 ike_sa_t* (*checkout_by_message) (ike_sa_manager_t* this, message_t *message);
87
88 /**
89 * Checkout an IKE_SA for initiation by a peer_config.
90 *
91 * To initiate, a CHILD_SA may be established within an existing IKE_SA.
92 * This call checks for an existing IKE_SA by comparing the configuration.
93 * If the CHILD_SA can be created in an existing IKE_SA, the matching SA
94 * is returned.
95 * If no IKE_SA is found, a new one is created. This is also the case when
96 * the found IKE_SA is in the DELETING state.
97 *
98 * @param peer_cfg configuration used to find an existing IKE_SA
99 * @return checked out/created IKE_SA
100 */
101 ike_sa_t* (*checkout_by_config) (ike_sa_manager_t* this,
102 peer_cfg_t *peer_cfg);
103
104 /**
105 * Check for duplicates of the given IKE_SA.
106 *
107 * Measures are taken according to the uniqueness policy of the IKE_SA.
108 * The return value indicates whether duplicates have been found and if
109 * further measures should be taken (e.g. cancelling an IKE_AUTH exchange).
110 * check_uniqueness() must be called before the IKE_SA is complete,
111 * deadlocks occur otherwise.
112 *
113 * @param ike_sa ike_sa to check
114 * @return TRUE, if the given IKE_SA has duplicates and
115 * should be deleted
116 */
117 bool (*check_uniqueness)(ike_sa_manager_t *this, ike_sa_t *ike_sa);
118
119 /**
120 * Check out an IKE_SA a unique ID.
121 *
122 * Every IKE_SA and every CHILD_SA is uniquely identified by an ID.
123 * These checkout function uses, depending
124 * on the child parameter, the unique ID of the IKE_SA or the reqid
125 * of one of a IKE_SAs CHILD_SA.
126 *
127 * @param id unique ID of the object
128 * @param child TRUE to use CHILD, FALSE to use IKE_SA
129 * @return
130 * - checked out IKE_SA, if found
131 * - NULL, if not found
132 */
133 ike_sa_t* (*checkout_by_id) (ike_sa_manager_t* this, u_int32_t id,
134 bool child);
135
136 /**
137 * Check out an IKE_SA by the policy/connection name.
138 *
139 * Check out the IKE_SA by the configuration name, either from the IKE- or
140 * one of its CHILD_SAs.
141 *
142 * @param name name of the connection/policy
143 * @param child TRUE to use policy name, FALSE to use conn name
144 * @return
145 * - checked out IKE_SA, if found
146 * - NULL, if not found
147 */
148 ike_sa_t* (*checkout_by_name) (ike_sa_manager_t* this, char *name,
149 bool child);
150
151 /**
152 * Create an enumerator over all stored IKE_SAs.
153 *
154 * While enumerating an IKE_SA, it is temporarily checked out and
155 * automatically checked in after the current enumeration step.
156 *
157 * @return enumerator over all IKE_SAs.
158 */
159 enumerator_t *(*create_enumerator) (ike_sa_manager_t* this);
160
161 /**
162 * Checkin the SA after usage.
163 *
164 * If the IKE_SA is not registered in the manager, a new entry is created.
165 *
166 * @param ike_sa_id the SA identifier, will be updated
167 * @param ike_sa checked out SA
168 */
169 void (*checkin) (ike_sa_manager_t* this, ike_sa_t *ike_sa);
170
171 /**
172 * Destroy a checked out SA.
173 *
174 * The IKE SA is destroyed without notification of the remote peer.
175 * Use this only if the other peer doesn't respond or behaves not
176 * as predicted.
177 * Checking in and destruction is an atomic operation (for the IKE_SA),
178 * so this can be called if the SA is in a "unclean" state, without the
179 * risk that another thread can get the SA.
180 *
181 * @param ike_sa SA to delete
182 */
183 void (*checkin_and_destroy) (ike_sa_manager_t* this, ike_sa_t *ike_sa);
184
185 /**
186 * Get the number of IKE_SAs which are in the connecting state.
187 *
188 * To prevent the server from resource exhaustion, cookies and other
189 * mechanisms are used. The number of half open IKE_SAs is a good
190 * indicator to see if a peer is flooding the server.
191 * If a host is supplied, only the number of half open IKE_SAs initiated
192 * from this IP are counted.
193 * Only SAs for which we are the responder are counted.
194 *
195 * @param ip NULL for all, IP for half open IKE_SAs with IP
196 * @return number of half open IKE_SAs
197 */
198 int (*get_half_open_count) (ike_sa_manager_t *this, host_t *ip);
199
200 /**
201 * Delete all existing IKE_SAs and destroy them immediately.
202 *
203 * Threads will be driven out, so all SAs can be deleted cleanly.
204 */
205 void (*flush)(ike_sa_manager_t *this);
206
207 /**
208 * Destroys the manager with all associated SAs.
209 *
210 * A call to flush() is required before calling destroy.
211 */
212 void (*destroy) (ike_sa_manager_t *this);
213 };
214
215 /**
216 * Create the IKE_SA manager.
217 *
218 * @returns ike_sa_manager_t object, NULL if initialization fails
219 */
220 ike_sa_manager_t *ike_sa_manager_create(void);
221
222 #endif /** IKE_SA_MANAGER_H_ @}*/