Add the reqid to kernel_ipsec_t.del_policy.
[strongswan.git] / src / libcharon / sa / mediation_manager.c
1 /*
2 * Copyright (C) 2007 Tobias Brunner
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include "mediation_manager.h"
17
18 #include <daemon.h>
19 #include <threading/mutex.h>
20 #include <utils/linked_list.h>
21 #include <processing/jobs/mediation_job.h>
22
23 typedef struct peer_t peer_t;
24
25 /**
26 * An entry in the linked list.
27 */
28 struct peer_t {
29 /** id of the peer */
30 identification_t *id;
31
32 /** sa id of the peer, NULL if offline */
33 ike_sa_id_t *ike_sa_id;
34
35 /** list of peer ids that reuested this peer */
36 linked_list_t *requested_by;
37 };
38
39 /**
40 * Implementation of peer_t.destroy.
41 */
42 static void peer_destroy(peer_t *this)
43 {
44 DESTROY_IF(this->id);
45 DESTROY_IF(this->ike_sa_id);
46 this->requested_by->destroy_offset(this->requested_by,
47 offsetof(identification_t, destroy));
48 free(this);
49 }
50
51 /**
52 * Creates a new entry for the list.
53 */
54 static peer_t *peer_create(identification_t *id, ike_sa_id_t* ike_sa_id)
55 {
56 peer_t *this = malloc_thing(peer_t);
57
58 /* clone everything */
59 this->id = id->clone(id);
60 this->ike_sa_id = ike_sa_id ? ike_sa_id->clone(ike_sa_id) : NULL;
61 this->requested_by = linked_list_create();
62
63 return this;
64 }
65
66 typedef struct private_mediation_manager_t private_mediation_manager_t;
67
68 /**
69 * Additional private members of mediation_manager_t.
70 */
71 struct private_mediation_manager_t {
72 /**
73 * Public interface of mediation_manager_t.
74 */
75 mediation_manager_t public;
76
77 /**
78 * Lock for exclusivly accessing the manager.
79 */
80 mutex_t *mutex;
81
82 /**
83 * Linked list with state entries.
84 */
85 linked_list_t *peers;
86 };
87
88 /**
89 * Registers a peer's ID at another peer, if it is not yet registered
90 */
91 static void register_peer(peer_t *peer, identification_t *peer_id)
92 {
93 iterator_t *iterator;
94 identification_t *current;
95
96 iterator = peer->requested_by->create_iterator(peer->requested_by, TRUE);
97 while (iterator->iterate(iterator, (void**)&current))
98 {
99 if (peer_id->equals(peer_id, current))
100 {
101 iterator->destroy(iterator);
102 return;
103 }
104 }
105 iterator->destroy(iterator);
106
107 peer->requested_by->insert_last(peer->requested_by,
108 peer_id->clone(peer_id));
109 }
110
111 /**
112 * Get a peer_t object by a peer's id
113 */
114 static status_t get_peer_by_id(private_mediation_manager_t *this,
115 identification_t *id, peer_t **peer)
116 {
117 iterator_t *iterator;
118 peer_t *current;
119 status_t status = NOT_FOUND;
120
121 iterator = this->peers->create_iterator(this->peers, TRUE);
122 while (iterator->iterate(iterator, (void**)&current))
123 {
124 if (id->equals(id, current->id))
125 {
126 if (peer)
127 {
128 *peer = current;
129 }
130 status = SUCCESS;
131 break;
132 }
133 }
134 iterator->destroy(iterator);
135
136 return status;
137 }
138
139 /**
140 * Check if a given peer is registered at other peers. If so, remove it there
141 * and then remove peers completely that are not online and have no registered
142 * peers.
143 */
144 static void unregister_peer(private_mediation_manager_t *this,
145 identification_t *peer_id)
146 {
147 iterator_t *iterator, *iterator_r;
148 peer_t *peer;
149 identification_t *registered;
150
151 iterator = this->peers->create_iterator(this->peers, TRUE);
152 while (iterator->iterate(iterator, (void**)&peer))
153 {
154 iterator_r = peer->requested_by->create_iterator(peer->requested_by,
155 TRUE);
156 while (iterator_r->iterate(iterator_r, (void**)&registered))
157 {
158 if (peer_id->equals(peer_id, registered))
159 {
160 iterator_r->remove(iterator_r);
161 registered->destroy(registered);
162 break;
163 }
164 }
165 iterator_r->destroy(iterator_r);
166
167 if (!peer->ike_sa_id && !peer->requested_by->get_count(peer->requested_by))
168 {
169 iterator->remove(iterator);
170 peer_destroy(peer);
171 break;
172 }
173 }
174 iterator->destroy(iterator);
175 }
176
177 /**
178 * Implementation of mediation_manager_t.remove
179 */
180 static void remove_sa(private_mediation_manager_t *this, ike_sa_id_t *ike_sa_id)
181 {
182 iterator_t *iterator;
183 peer_t *peer;
184
185 this->mutex->lock(this->mutex);
186
187 iterator = this->peers->create_iterator(this->peers, TRUE);
188 while (iterator->iterate(iterator, (void**)&peer))
189 {
190 if (ike_sa_id->equals(ike_sa_id, peer->ike_sa_id))
191 {
192 iterator->remove(iterator);
193
194 unregister_peer(this, peer->id);
195
196 peer_destroy(peer);
197 break;
198 }
199 }
200 iterator->destroy(iterator);
201
202 this->mutex->unlock(this->mutex);
203 }
204
205 /**
206 * Implementation of mediation_manager_t.update_sa_id
207 */
208 static void update_sa_id(private_mediation_manager_t *this, identification_t *peer_id, ike_sa_id_t *ike_sa_id)
209 {
210 iterator_t *iterator;
211 peer_t *peer;
212 bool found = FALSE;
213
214 this->mutex->lock(this->mutex);
215
216 iterator = this->peers->create_iterator(this->peers, TRUE);
217 while (iterator->iterate(iterator, (void**)&peer))
218 {
219 if (peer_id->equals(peer_id, peer->id))
220 {
221 DESTROY_IF(peer->ike_sa_id);
222 found = TRUE;
223 break;
224 }
225 }
226 iterator->destroy(iterator);
227
228 if (!found)
229 {
230 DBG2(DBG_IKE, "adding peer '%Y'", peer_id);
231 peer = peer_create(peer_id, NULL);
232 this->peers->insert_last(this->peers, peer);
233 }
234
235 DBG2(DBG_IKE, "changing registered IKE_SA ID of peer '%Y'", peer_id);
236 peer->ike_sa_id = ike_sa_id ? ike_sa_id->clone(ike_sa_id) : NULL;
237
238 /* send callbacks to registered peers */
239 identification_t *requester;
240 while(peer->requested_by->remove_last(peer->requested_by,
241 (void**)&requester) == SUCCESS)
242 {
243 job_t *job = (job_t*)mediation_callback_job_create(requester, peer_id);
244 lib->processor->queue_job(lib->processor, job);
245 requester->destroy(requester);
246 }
247
248 this->mutex->unlock(this->mutex);
249 }
250
251 /**
252 * Implementation of mediation_manager_t.check.
253 */
254 static ike_sa_id_t *check(private_mediation_manager_t *this,
255 identification_t *peer_id)
256 {
257 peer_t *peer;
258 ike_sa_id_t *ike_sa_id;
259
260 this->mutex->lock(this->mutex);
261
262 if (get_peer_by_id(this, peer_id, &peer) != SUCCESS)
263 {
264 this->mutex->unlock(this->mutex);
265 return NULL;
266 }
267
268 ike_sa_id = peer->ike_sa_id;
269
270 this->mutex->unlock(this->mutex);
271
272 return ike_sa_id;
273 }
274
275 /**
276 * Implementation of mediation_manager_t.check_and_register.
277 */
278 static ike_sa_id_t *check_and_register(private_mediation_manager_t *this,
279 identification_t *peer_id, identification_t *requester)
280 {
281 peer_t *peer;
282 ike_sa_id_t *ike_sa_id;
283
284 this->mutex->lock(this->mutex);
285
286 if (get_peer_by_id(this, peer_id, &peer) != SUCCESS)
287 {
288 DBG2(DBG_IKE, "adding peer %Y", peer_id);
289 peer = peer_create(peer_id, NULL);
290 this->peers->insert_last(this->peers, peer);
291 }
292
293 if (!peer->ike_sa_id)
294 {
295 /* the peer is not online */
296 DBG2(DBG_IKE, "requested peer '%Y' is offline, registering peer '%Y'",
297 peer_id, requester);
298 register_peer(peer, requester);
299 this->mutex->unlock(this->mutex);
300 return NULL;
301 }
302
303 ike_sa_id = peer->ike_sa_id;
304
305 this->mutex->unlock(this->mutex);
306
307 return ike_sa_id;
308 }
309
310 /**
311 * Implementation of mediation_manager_t.destroy.
312 */
313 static void destroy(private_mediation_manager_t *this)
314 {
315 this->mutex->lock(this->mutex);
316
317 this->peers->destroy_function(this->peers, (void*)peer_destroy);
318
319 this->mutex->unlock(this->mutex);
320 this->mutex->destroy(this->mutex);
321 free(this);
322 }
323
324 /*
325 * Described in header.
326 */
327 mediation_manager_t *mediation_manager_create()
328 {
329 private_mediation_manager_t *this = malloc_thing(private_mediation_manager_t);
330
331 this->public.destroy = (void(*)(mediation_manager_t*))destroy;
332 this->public.remove = (void(*)(mediation_manager_t*,ike_sa_id_t*))remove_sa;
333 this->public.update_sa_id = (void(*)(mediation_manager_t*,identification_t*,ike_sa_id_t*))update_sa_id;
334 this->public.check = (ike_sa_id_t*(*)(mediation_manager_t*,identification_t*))check;
335 this->public.check_and_register = (ike_sa_id_t*(*)(mediation_manager_t*,identification_t*,identification_t*))check_and_register;
336
337 this->peers = linked_list_create();
338 this->mutex = mutex_create(MUTEX_TYPE_DEFAULT);
339
340 return (mediation_manager_t*)this;
341 }