Added additional debug info to peer config matching
[strongswan.git] / src / libcharon / config / backend_manager.c
1 /*
2 * Copyright (C) 2007-2009 Martin Willi
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 "backend_manager.h"
17
18 #include <sys/types.h>
19
20 #include <daemon.h>
21 #include <utils/linked_list.h>
22 #include <threading/rwlock.h>
23
24
25 typedef struct private_backend_manager_t private_backend_manager_t;
26
27 /**
28 * Private data of an backend_manager_t object.
29 */
30 struct private_backend_manager_t {
31
32 /**
33 * Public part of backend_manager_t object.
34 */
35 backend_manager_t public;
36
37 /**
38 * list of registered backends
39 */
40 linked_list_t *backends;
41
42 /**
43 * rwlock for backends
44 */
45 rwlock_t *lock;
46 };
47
48 /**
49 * match of an ike_cfg
50 */
51 typedef enum ike_cfg_match_t {
52 MATCH_NONE = 0x00,
53 MATCH_ANY = 0x01,
54 MATCH_ME = 0x04,
55 MATCH_OTHER = 0x08,
56 } ike_cfg_match_t;
57
58 /**
59 * data to pass nested IKE enumerator
60 */
61 typedef struct {
62 private_backend_manager_t *this;
63 host_t *me;
64 host_t *other;
65 } ike_data_t;
66
67 /**
68 * inner enumerator constructor for IKE cfgs
69 */
70 static enumerator_t *ike_enum_create(backend_t *backend, ike_data_t *data)
71 {
72 return backend->create_ike_cfg_enumerator(backend, data->me, data->other);
73 }
74
75 /**
76 * get a match of a candidate ike_cfg for two hosts
77 */
78 static ike_cfg_match_t get_ike_match(ike_cfg_t *cand, host_t *me, host_t *other)
79 {
80 host_t *me_cand, *other_cand;
81 ike_cfg_match_t match = MATCH_NONE;
82
83 if (me)
84 {
85 me_cand = host_create_from_dns(cand->get_my_addr(cand),
86 me->get_family(me), 0);
87 if (!me_cand)
88 {
89 return MATCH_NONE;
90 }
91 if (me_cand->ip_equals(me_cand, me))
92 {
93 match += MATCH_ME;
94 }
95 else if (me_cand->is_anyaddr(me_cand))
96 {
97 match += MATCH_ANY;
98 }
99 else
100 {
101 me_cand->destroy(me_cand);
102 return MATCH_NONE;
103 }
104 me_cand->destroy(me_cand);
105 }
106 else
107 {
108 match += MATCH_ANY;
109 }
110
111 if (other)
112 {
113 other_cand = host_create_from_dns(cand->get_other_addr(cand),
114 other->get_family(other), 0);
115 if (!other_cand)
116 {
117 return MATCH_NONE;
118 }
119 if (other_cand->ip_equals(other_cand, other))
120 {
121 match += MATCH_OTHER;
122 }
123 else if (other_cand->is_anyaddr(other_cand))
124 {
125 match += MATCH_ANY;
126 }
127 else
128 {
129 other_cand->destroy(other_cand);
130 return MATCH_NONE;
131 }
132 other_cand->destroy(other_cand);
133 }
134 else
135 {
136 match += MATCH_ANY;
137 }
138 return match;
139 }
140
141 METHOD(backend_manager_t, get_ike_cfg, ike_cfg_t*,
142 private_backend_manager_t *this, host_t *me, host_t *other)
143 {
144 ike_cfg_t *current, *found = NULL;
145 enumerator_t *enumerator;
146 ike_cfg_match_t match, best = MATCH_ANY;
147 ike_data_t *data;
148
149 data = malloc_thing(ike_data_t);
150 data->this = this;
151 data->me = me;
152 data->other = other;
153
154 DBG2(DBG_CFG, "looking for an ike config for %H...%H", me, other);
155
156 this->lock->read_lock(this->lock);
157 enumerator = enumerator_create_nested(
158 this->backends->create_enumerator(this->backends),
159 (void*)ike_enum_create, data, (void*)free);
160 while (enumerator->enumerate(enumerator, (void**)&current))
161 {
162 match = get_ike_match(current, me, other);
163
164 if (match)
165 {
166 DBG2(DBG_CFG, " candidate: %s...%s, prio %d",
167 current->get_my_addr(current),
168 current->get_other_addr(current), match);
169 if (match > best)
170 {
171 DESTROY_IF(found);
172 found = current;
173 found->get_ref(found);
174 best = match;
175 }
176 }
177 }
178 enumerator->destroy(enumerator);
179 this->lock->unlock(this->lock);
180 if (found)
181 {
182 DBG2(DBG_CFG, "found matching ike config: %s...%s with prio %d",
183 found->get_my_addr(found), found->get_other_addr(found), best);
184 }
185 return found;
186 }
187
188 /**
189 * Get the best ID match in one of the configs auth_cfg
190 */
191 static id_match_t get_peer_match(identification_t *id,
192 peer_cfg_t *cfg, bool local)
193 {
194 enumerator_t *enumerator;
195 auth_cfg_t *auth;
196 identification_t *candidate;
197 id_match_t match = ID_MATCH_NONE;
198
199 if (!id)
200 {
201 return ID_MATCH_ANY;
202 }
203
204 /* compare first auth config only */
205 enumerator = cfg->create_auth_cfg_enumerator(cfg, local);
206 if (enumerator->enumerate(enumerator, &auth))
207 {
208 candidate = auth->get(auth, AUTH_RULE_IDENTITY);
209 if (candidate)
210 {
211 match = id->matches(id, candidate);
212 /* match vice-versa, as the proposed IDr might be ANY */
213 if (!match)
214 {
215 match = candidate->matches(candidate, id);
216 }
217 }
218 else
219 {
220 match = ID_MATCH_ANY;
221 }
222 }
223 enumerator->destroy(enumerator);
224 return match;
225 }
226
227 /**
228 * data to pass nested peer enumerator
229 */
230 typedef struct {
231 rwlock_t *lock;
232 identification_t *me;
233 identification_t *other;
234 } peer_data_t;
235
236 /**
237 * list element to help sorting
238 */
239 typedef struct {
240 id_match_t match_peer;
241 ike_cfg_match_t match_ike;
242 peer_cfg_t *cfg;
243 } match_entry_t;
244
245 /**
246 * inner enumerator constructor for peer cfgs
247 */
248 static enumerator_t *peer_enum_create(backend_t *backend, peer_data_t *data)
249 {
250 return backend->create_peer_cfg_enumerator(backend, data->me, data->other);
251 }
252
253 /**
254 * unlock/cleanup peer enumerator
255 */
256 static void peer_enum_destroy(peer_data_t *data)
257 {
258 data->lock->unlock(data->lock);
259 free(data);
260 }
261
262 /**
263 * convert enumerator value from match_entry to config
264 */
265 static bool peer_enum_filter(linked_list_t *configs,
266 match_entry_t **in, peer_cfg_t **out)
267 {
268 *out = (*in)->cfg;
269 return TRUE;
270 }
271
272 /**
273 * Clean up temporary config list
274 */
275 static void peer_enum_filter_destroy(linked_list_t *configs)
276 {
277 match_entry_t *entry;
278
279 while (configs->remove_last(configs, (void**)&entry) == SUCCESS)
280 {
281 entry->cfg->destroy(entry->cfg);
282 free(entry);
283 }
284 configs->destroy(configs);
285 }
286
287 /**
288 * Insert entry into match-sorted list, using helper
289 */
290 static void insert_sorted(match_entry_t *entry, linked_list_t *list,
291 linked_list_t *helper)
292 {
293 match_entry_t *current;
294
295 while (list->remove_first(list, (void**)&current) == SUCCESS)
296 {
297 helper->insert_last(helper, current);
298 }
299 while (helper->remove_first(helper, (void**)&current) == SUCCESS)
300 {
301 if (entry && (
302 (entry->match_ike > current->match_ike &&
303 entry->match_peer >= current->match_peer) ||
304 (entry->match_ike >= current->match_ike &&
305 entry->match_peer > current->match_peer)))
306 {
307 list->insert_last(list, entry);
308 entry = NULL;
309 }
310 list->insert_last(list, current);
311 }
312 if (entry)
313 {
314 list->insert_last(list, entry);
315 }
316 }
317
318 METHOD(backend_manager_t, create_peer_cfg_enumerator, enumerator_t*,
319 private_backend_manager_t *this, host_t *me, host_t *other,
320 identification_t *my_id, identification_t *other_id)
321 {
322 enumerator_t *enumerator;
323 peer_data_t *data;
324 peer_cfg_t *cfg;
325 linked_list_t *configs, *helper;
326
327 data = malloc_thing(peer_data_t);
328 data->lock = this->lock;
329 data->me = my_id;
330 data->other = other_id;
331
332 /* create a sorted list with all matches */
333 this->lock->read_lock(this->lock);
334 enumerator = enumerator_create_nested(
335 this->backends->create_enumerator(this->backends),
336 (void*)peer_enum_create, data, (void*)peer_enum_destroy);
337
338 if (!me && !other && !my_id && !other_id)
339 { /* shortcut if we are doing a "listall" */
340 return enumerator;
341 }
342
343 DBG1(DBG_CFG, "looking for peer configs matching %H[%Y]...%H[%Y]",
344 me, my_id, other, other_id);
345
346 configs = linked_list_create();
347 /* only once allocated helper list for sorting */
348 helper = linked_list_create();
349 while (enumerator->enumerate(enumerator, &cfg))
350 {
351 id_match_t match_peer_me, match_peer_other;
352 ike_cfg_match_t match_ike;
353 match_entry_t *entry;
354 chunk_t data;
355
356 match_peer_me = get_peer_match(my_id, cfg, TRUE);
357 data = my_id->get_encoding(my_id);
358 DBG3(DBG_CFG, "match_peer_me: %d (%N -> %#B)", match_peer_me,
359 id_type_names, my_id->get_type(my_id), &data);
360 match_peer_other = get_peer_match(other_id, cfg, FALSE);
361 data = other_id->get_encoding(other_id);
362 DBG3(DBG_CFG, "match_peer_other: %d (%N -> %#B)", match_peer_other,
363 id_type_names, other_id->get_type(other_id), &data);
364 match_ike = get_ike_match(cfg->get_ike_cfg(cfg), me, other);
365 DBG3(DBG_CFG, "match_ike: %d (%H %H)", match_ike, me, other);
366
367 if (match_peer_me && match_peer_other && match_ike)
368 {
369 DBG2(DBG_CFG, " candidate \"%s\", match: %d/%d/%d (me/other/ike)",
370 cfg->get_name(cfg), match_peer_me, match_peer_other, match_ike);
371
372 entry = malloc_thing(match_entry_t);
373 entry->match_peer = match_peer_me + match_peer_other;
374 entry->match_ike = match_ike;
375 entry->cfg = cfg->get_ref(cfg);
376 insert_sorted(entry, configs, helper);
377 }
378 }
379 enumerator->destroy(enumerator);
380 helper->destroy(helper);
381
382 return enumerator_create_filter(configs->create_enumerator(configs),
383 (void*)peer_enum_filter, configs,
384 (void*)peer_enum_filter_destroy);
385 }
386
387 METHOD(backend_manager_t, get_peer_cfg_by_name, peer_cfg_t*,
388 private_backend_manager_t *this, char *name)
389 {
390 backend_t *backend;
391 peer_cfg_t *config = NULL;
392 enumerator_t *enumerator;
393
394 this->lock->read_lock(this->lock);
395 enumerator = this->backends->create_enumerator(this->backends);
396 while (config == NULL && enumerator->enumerate(enumerator, (void**)&backend))
397 {
398 config = backend->get_peer_cfg_by_name(backend, name);
399 }
400 enumerator->destroy(enumerator);
401 this->lock->unlock(this->lock);
402 return config;
403 }
404
405 METHOD(backend_manager_t, remove_backend, void,
406 private_backend_manager_t *this, backend_t *backend)
407 {
408 this->lock->write_lock(this->lock);
409 this->backends->remove(this->backends, backend, NULL);
410 this->lock->unlock(this->lock);
411 }
412
413 METHOD(backend_manager_t, add_backend, void,
414 private_backend_manager_t *this, backend_t *backend)
415 {
416 this->lock->write_lock(this->lock);
417 this->backends->insert_last(this->backends, backend);
418 this->lock->unlock(this->lock);
419 }
420
421 METHOD(backend_manager_t, destroy, void,
422 private_backend_manager_t *this)
423 {
424 this->backends->destroy(this->backends);
425 this->lock->destroy(this->lock);
426 free(this);
427 }
428
429 /*
430 * Described in header-file
431
432 */
433 backend_manager_t *backend_manager_create()
434 {
435 private_backend_manager_t *this;
436
437 INIT(this,
438 .public = {
439 .get_ike_cfg = _get_ike_cfg,
440 .get_peer_cfg_by_name = _get_peer_cfg_by_name,
441 .create_peer_cfg_enumerator = _create_peer_cfg_enumerator,
442 .add_backend = _add_backend,
443 .remove_backend = _remove_backend,
444 .destroy = _destroy,
445 },
446 .backends = linked_list_create(),
447 .lock = rwlock_create(RWLOCK_TYPE_DEFAULT),
448 );
449
450 return &this->public;
451 }
452