Pass all configured pool names to attribute provider enumerator
[strongswan.git] / src / libcharon / sa / ikev2 / tasks / ike_config.c
1 /*
2 * Copyright (C) 2007 Martin Willi
3 * Copyright (C) 2006-2007 Fabian Hartmann, Noah Heusser
4 * Hochschule fuer Technik Rapperswil
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 */
16
17 #include "ike_config.h"
18
19 #include <daemon.h>
20 #include <hydra.h>
21 #include <encoding/payloads/cp_payload.h>
22
23 typedef struct private_ike_config_t private_ike_config_t;
24
25 /**
26 * Private members of a ike_config_t task.
27 */
28 struct private_ike_config_t {
29
30 /**
31 * Public methods and task_t interface.
32 */
33 ike_config_t public;
34
35 /**
36 * Assigned IKE_SA.
37 */
38 ike_sa_t *ike_sa;
39
40 /**
41 * Are we the initiator?
42 */
43 bool initiator;
44
45 /**
46 * virtual ip
47 */
48 host_t *virtual_ip;
49
50 /**
51 * list of attributes requested and its handler, entry_t
52 */
53 linked_list_t *requested;
54 };
55
56 /**
57 * Entry for a requested attribute and the requesting handler
58 */
59 typedef struct {
60 /** attribute requested */
61 configuration_attribute_type_t type;
62 /** handler requesting this attribute */
63 attribute_handler_t *handler;
64 } entry_t;
65
66 /**
67 * build INTERNAL_IPV4/6_ADDRESS attribute from virtual ip
68 */
69 static configuration_attribute_t *build_vip(host_t *vip)
70 {
71 configuration_attribute_type_t type;
72 chunk_t chunk, prefix;
73
74 if (vip->get_family(vip) == AF_INET)
75 {
76 type = INTERNAL_IP4_ADDRESS;
77 if (vip->is_anyaddr(vip))
78 {
79 chunk = chunk_empty;
80 }
81 else
82 {
83 chunk = vip->get_address(vip);
84 }
85 }
86 else
87 {
88 type = INTERNAL_IP6_ADDRESS;
89 if (vip->is_anyaddr(vip))
90 {
91 chunk = chunk_empty;
92 }
93 else
94 {
95 prefix = chunk_alloca(1);
96 *prefix.ptr = 64;
97 chunk = vip->get_address(vip);
98 chunk = chunk_cata("cc", chunk, prefix);
99 }
100 }
101 return configuration_attribute_create_chunk(CONFIGURATION_ATTRIBUTE,
102 type, chunk);
103 }
104
105 /**
106 * Handle a received attribute as initiator
107 */
108 static void handle_attribute(private_ike_config_t *this,
109 configuration_attribute_t *ca)
110 {
111 attribute_handler_t *handler = NULL;
112 enumerator_t *enumerator;
113 entry_t *entry;
114
115 /* find the handler which requested this attribute */
116 enumerator = this->requested->create_enumerator(this->requested);
117 while (enumerator->enumerate(enumerator, &entry))
118 {
119 if (entry->type == ca->get_type(ca))
120 {
121 handler = entry->handler;
122 this->requested->remove_at(this->requested, enumerator);
123 free(entry);
124 break;
125 }
126 }
127 enumerator->destroy(enumerator);
128
129 /* and pass it to the handle function */
130 handler = hydra->attributes->handle(hydra->attributes,
131 this->ike_sa->get_other_id(this->ike_sa), handler,
132 ca->get_type(ca), ca->get_chunk(ca));
133 if (handler)
134 {
135 this->ike_sa->add_configuration_attribute(this->ike_sa,
136 handler, ca->get_type(ca), ca->get_chunk(ca));
137 }
138 }
139
140 /**
141 * process a single configuration attribute
142 */
143 static void process_attribute(private_ike_config_t *this,
144 configuration_attribute_t *ca)
145 {
146 host_t *ip;
147 chunk_t addr;
148 int family = AF_INET6;
149
150 switch (ca->get_type(ca))
151 {
152 case INTERNAL_IP4_ADDRESS:
153 family = AF_INET;
154 /* fall */
155 case INTERNAL_IP6_ADDRESS:
156 {
157 addr = ca->get_chunk(ca);
158 if (addr.len == 0)
159 {
160 ip = host_create_any(family);
161 }
162 else
163 {
164 /* skip prefix byte in IPv6 payload*/
165 if (family == AF_INET6)
166 {
167 addr.len--;
168 }
169 ip = host_create_from_chunk(family, addr, 0);
170 }
171 if (ip)
172 {
173 DESTROY_IF(this->virtual_ip);
174 this->virtual_ip = ip;
175 }
176 break;
177 }
178 case INTERNAL_IP4_SERVER:
179 case INTERNAL_IP6_SERVER:
180 /* assume it's a Windows client if we see proprietary attributes */
181 this->ike_sa->enable_extension(this->ike_sa, EXT_MS_WINDOWS);
182 /* fall */
183 default:
184 {
185 if (this->initiator)
186 {
187 handle_attribute(this, ca);
188 }
189 }
190 }
191 }
192
193 /**
194 * Scan for configuration payloads and attributes
195 */
196 static void process_payloads(private_ike_config_t *this, message_t *message)
197 {
198 enumerator_t *enumerator, *attributes;
199 payload_t *payload;
200
201 enumerator = message->create_payload_enumerator(message);
202 while (enumerator->enumerate(enumerator, &payload))
203 {
204 if (payload->get_type(payload) == CONFIGURATION)
205 {
206 cp_payload_t *cp = (cp_payload_t*)payload;
207 configuration_attribute_t *ca;
208
209 switch (cp->get_type(cp))
210 {
211 case CFG_REQUEST:
212 case CFG_REPLY:
213 {
214 attributes = cp->create_attribute_enumerator(cp);
215 while (attributes->enumerate(attributes, &ca))
216 {
217 DBG2(DBG_IKE, "processing %N attribute",
218 configuration_attribute_type_names, ca->get_type(ca));
219 process_attribute(this, ca);
220 }
221 attributes->destroy(attributes);
222 break;
223 }
224 default:
225 DBG1(DBG_IKE, "ignoring %N config payload",
226 config_type_names, cp->get_type(cp));
227 break;
228 }
229 }
230 }
231 enumerator->destroy(enumerator);
232 }
233
234 METHOD(task_t, build_i, status_t,
235 private_ike_config_t *this, message_t *message)
236 {
237 if (message->get_message_id(message) == 1)
238 { /* in first IKE_AUTH only */
239 cp_payload_t *cp = NULL;
240 enumerator_t *enumerator;
241 attribute_handler_t *handler;
242 peer_cfg_t *config;
243 configuration_attribute_type_t type;
244 chunk_t data;
245 linked_list_t *vips;
246 host_t *host;
247
248 vips = linked_list_create();
249
250 /* reuse virtual IP if we already have one */
251 enumerator = this->ike_sa->create_virtual_ip_enumerator(this->ike_sa,
252 TRUE);
253 while (enumerator->enumerate(enumerator, &host))
254 {
255 vips->insert_last(vips, host);
256 }
257 enumerator->destroy(enumerator);
258
259 if (vips->get_count(vips) == 0)
260 {
261 config = this->ike_sa->get_peer_cfg(this->ike_sa);
262 enumerator = config->create_virtual_ip_enumerator(config);
263 while (enumerator->enumerate(enumerator, &host))
264 {
265 vips->insert_last(vips, host);
266 }
267 enumerator->destroy(enumerator);
268 }
269
270 if (vips->get_count(vips))
271 {
272 cp = cp_payload_create_type(CONFIGURATION, CFG_REQUEST);
273 enumerator = vips->create_enumerator(vips);
274 while (enumerator->enumerate(enumerator, &host))
275 {
276 cp->add_attribute(cp, build_vip(host));
277 }
278 enumerator->destroy(enumerator);
279 }
280
281 enumerator = hydra->attributes->create_initiator_enumerator(
282 hydra->attributes,
283 this->ike_sa->get_other_id(this->ike_sa), vips);
284 while (enumerator->enumerate(enumerator, &handler, &type, &data))
285 {
286 configuration_attribute_t *ca;
287 entry_t *entry;
288
289 /* create configuration attribute */
290 DBG2(DBG_IKE, "building %N attribute",
291 configuration_attribute_type_names, type);
292 ca = configuration_attribute_create_chunk(CONFIGURATION_ATTRIBUTE,
293 type, data);
294 if (!cp)
295 {
296 cp = cp_payload_create_type(CONFIGURATION, CFG_REQUEST);
297 }
298 cp->add_attribute(cp, ca);
299
300 /* save handler along with requested type */
301 entry = malloc_thing(entry_t);
302 entry->type = type;
303 entry->handler = handler;
304
305 this->requested->insert_last(this->requested, entry);
306 }
307 enumerator->destroy(enumerator);
308
309 vips->destroy(vips);
310
311 if (cp)
312 {
313 message->add_payload(message, (payload_t*)cp);
314 }
315 }
316 return NEED_MORE;
317 }
318
319 METHOD(task_t, process_r, status_t,
320 private_ike_config_t *this, message_t *message)
321 {
322 if (message->get_message_id(message) == 1)
323 { /* in first IKE_AUTH only */
324 process_payloads(this, message);
325 }
326 return NEED_MORE;
327 }
328
329 METHOD(task_t, build_r, status_t,
330 private_ike_config_t *this, message_t *message)
331 {
332 if (this->ike_sa->get_state(this->ike_sa) == IKE_ESTABLISHED)
333 { /* in last IKE_AUTH exchange */
334 enumerator_t *enumerator;
335 configuration_attribute_type_t type;
336 chunk_t value;
337 host_t *vip = NULL;
338 cp_payload_t *cp = NULL;
339 peer_cfg_t *config;
340 identification_t *id;
341 linked_list_t *vips, *pools;
342 char *pool;
343
344 id = this->ike_sa->get_other_eap_id(this->ike_sa);
345
346 config = this->ike_sa->get_peer_cfg(this->ike_sa);
347 enumerator = config->create_pool_enumerator(config);
348 if (!enumerator->enumerate(enumerator, &pool))
349 { /* TODO: currently we query the first pool, only */
350 pool = NULL;
351 }
352 enumerator->destroy(enumerator);
353
354 if (this->virtual_ip)
355 {
356 DBG1(DBG_IKE, "peer requested virtual IP %H", this->virtual_ip);
357 if (pool)
358 {
359 vip = hydra->attributes->acquire_address(hydra->attributes,
360 pool, id, this->virtual_ip);
361 }
362 if (vip == NULL)
363 {
364 DBG1(DBG_IKE, "no virtual IP found, sending %N",
365 notify_type_names, INTERNAL_ADDRESS_FAILURE);
366 message->add_notify(message, FALSE, INTERNAL_ADDRESS_FAILURE,
367 chunk_empty);
368 return SUCCESS;
369 }
370 DBG1(DBG_IKE, "assigning virtual IP %H to peer '%Y'", vip, id);
371 this->ike_sa->add_virtual_ip(this->ike_sa, FALSE, vip);
372
373 cp = cp_payload_create_type(CONFIGURATION, CFG_REPLY);
374 cp->add_attribute(cp, build_vip(vip));
375 }
376
377 /* query registered providers for additional attributes to include */
378 vips = linked_list_create();
379 /* TODO: use list with all assigned VIPs */
380 if (vip)
381 {
382 vips->insert_last(vips, vip);
383 }
384 pools = linked_list_create();
385 /* TODO: use list of all pools */
386 if (pool)
387 {
388 pools->insert_last(pools, pool);
389 }
390 enumerator = hydra->attributes->create_responder_enumerator(
391 hydra->attributes, pools, id, vips);
392 while (enumerator->enumerate(enumerator, &type, &value))
393 {
394 if (!cp)
395 {
396 cp = cp_payload_create_type(CONFIGURATION, CFG_REPLY);
397 }
398 DBG2(DBG_IKE, "building %N attribute",
399 configuration_attribute_type_names, type);
400 cp->add_attribute(cp,
401 configuration_attribute_create_chunk(CONFIGURATION_ATTRIBUTE,
402 type, value));
403 }
404 enumerator->destroy(enumerator);
405 vips->destroy(vips);
406 pools->destroy(pools);
407
408 if (cp)
409 {
410 message->add_payload(message, (payload_t*)cp);
411 }
412 DESTROY_IF(vip);
413 return SUCCESS;
414 }
415 return NEED_MORE;
416 }
417
418 METHOD(task_t, process_i, status_t,
419 private_ike_config_t *this, message_t *message)
420 {
421 if (this->ike_sa->get_state(this->ike_sa) == IKE_ESTABLISHED)
422 { /* in last IKE_AUTH exchange */
423
424 process_payloads(this, message);
425
426 if (this->virtual_ip &&
427 !this->virtual_ip->is_anyaddr(this->virtual_ip))
428 {
429 this->ike_sa->add_virtual_ip(this->ike_sa, TRUE, this->virtual_ip);
430 }
431 return SUCCESS;
432 }
433 return NEED_MORE;
434 }
435
436 METHOD(task_t, get_type, task_type_t,
437 private_ike_config_t *this)
438 {
439 return TASK_IKE_CONFIG;
440 }
441
442 METHOD(task_t, migrate, void,
443 private_ike_config_t *this, ike_sa_t *ike_sa)
444 {
445 DESTROY_IF(this->virtual_ip);
446
447 this->ike_sa = ike_sa;
448 this->virtual_ip = NULL;
449 this->requested->destroy_function(this->requested, free);
450 this->requested = linked_list_create();
451 }
452
453 METHOD(task_t, destroy, void,
454 private_ike_config_t *this)
455 {
456 DESTROY_IF(this->virtual_ip);
457 this->requested->destroy_function(this->requested, free);
458 free(this);
459 }
460
461 /*
462 * Described in header.
463 */
464 ike_config_t *ike_config_create(ike_sa_t *ike_sa, bool initiator)
465 {
466 private_ike_config_t *this;
467
468 INIT(this,
469 .public = {
470 .task = {
471 .get_type = _get_type,
472 .migrate = _migrate,
473 .destroy = _destroy,
474 },
475 },
476 .initiator = initiator,
477 .ike_sa = ike_sa,
478 .requested = linked_list_create(),
479 );
480
481 if (initiator)
482 {
483 this->public.task.build = _build_i;
484 this->public.task.process = _process_i;
485 }
486 else
487 {
488 this->public.task.build = _build_r;
489 this->public.task.process = _process_r;
490 }
491
492 return &this->public;
493 }