Added support for named attribute groups
[strongswan.git] / src / libcharon / sa / 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_value(type, chunk);
102 }
103
104 /**
105 * Handle a received attribute as initiator
106 */
107 static void handle_attribute(private_ike_config_t *this,
108 configuration_attribute_t *ca)
109 {
110 attribute_handler_t *handler = NULL;
111 enumerator_t *enumerator;
112 entry_t *entry;
113
114 /* find the handler which requested this attribute */
115 enumerator = this->requested->create_enumerator(this->requested);
116 while (enumerator->enumerate(enumerator, &entry))
117 {
118 if (entry->type == ca->get_type(ca))
119 {
120 handler = entry->handler;
121 this->requested->remove_at(this->requested, enumerator);
122 free(entry);
123 break;
124 }
125 }
126 enumerator->destroy(enumerator);
127
128 /* and pass it to the handle function */
129 handler = hydra->attributes->handle(hydra->attributes,
130 this->ike_sa->get_other_id(this->ike_sa), handler,
131 ca->get_type(ca), ca->get_value(ca));
132 if (handler)
133 {
134 this->ike_sa->add_configuration_attribute(this->ike_sa,
135 handler, ca->get_type(ca), ca->get_value(ca));
136 }
137 }
138
139 /**
140 * process a single configuration attribute
141 */
142 static void process_attribute(private_ike_config_t *this,
143 configuration_attribute_t *ca)
144 {
145 host_t *ip;
146 chunk_t addr;
147 int family = AF_INET6;
148
149 switch (ca->get_type(ca))
150 {
151 case INTERNAL_IP4_ADDRESS:
152 family = AF_INET;
153 /* fall */
154 case INTERNAL_IP6_ADDRESS:
155 {
156 addr = ca->get_value(ca);
157 if (addr.len == 0)
158 {
159 ip = host_create_any(family);
160 }
161 else
162 {
163 /* skip prefix byte in IPv6 payload*/
164 if (family == AF_INET6)
165 {
166 addr.len--;
167 }
168 ip = host_create_from_chunk(family, addr, 0);
169 }
170 if (ip)
171 {
172 DESTROY_IF(this->virtual_ip);
173 this->virtual_ip = ip;
174 }
175 break;
176 }
177 default:
178 {
179 if (this->initiator)
180 {
181 handle_attribute(this, ca);
182 }
183 }
184 }
185 }
186
187 /**
188 * Scan for configuration payloads and attributes
189 */
190 static void process_payloads(private_ike_config_t *this, message_t *message)
191 {
192 enumerator_t *enumerator, *attributes;
193 payload_t *payload;
194
195 enumerator = message->create_payload_enumerator(message);
196 while (enumerator->enumerate(enumerator, &payload))
197 {
198 if (payload->get_type(payload) == CONFIGURATION)
199 {
200 cp_payload_t *cp = (cp_payload_t*)payload;
201 configuration_attribute_t *ca;
202
203 switch (cp->get_type(cp))
204 {
205 case CFG_REQUEST:
206 case CFG_REPLY:
207 {
208 attributes = cp->create_attribute_enumerator(cp);
209 while (attributes->enumerate(attributes, &ca))
210 {
211 DBG2(DBG_IKE, "processing %N attribute",
212 configuration_attribute_type_names, ca->get_type(ca));
213 process_attribute(this, ca);
214 }
215 attributes->destroy(attributes);
216 break;
217 }
218 default:
219 DBG1(DBG_IKE, "ignoring %N config payload",
220 config_type_names, cp->get_type(cp));
221 break;
222 }
223 }
224 }
225 enumerator->destroy(enumerator);
226 }
227
228 /**
229 * Implementation of task_t.process for initiator
230 */
231 static status_t build_i(private_ike_config_t *this, message_t *message)
232 {
233 if (message->get_message_id(message) == 1)
234 { /* in first IKE_AUTH only */
235 cp_payload_t *cp = NULL;
236 enumerator_t *enumerator;
237 attribute_handler_t *handler;
238 peer_cfg_t *config;
239 configuration_attribute_type_t type;
240 chunk_t data;
241 host_t *vip;
242
243 /* reuse virtual IP if we already have one */
244 vip = this->ike_sa->get_virtual_ip(this->ike_sa, TRUE);
245 if (!vip)
246 {
247 config = this->ike_sa->get_peer_cfg(this->ike_sa);
248 vip = config->get_virtual_ip(config);
249 }
250 if (vip)
251 {
252 cp = cp_payload_create_type(CFG_REQUEST);
253 cp->add_attribute(cp, build_vip(vip));
254 }
255
256 enumerator = hydra->attributes->create_initiator_enumerator(hydra->attributes,
257 this->ike_sa->get_other_id(this->ike_sa), vip);
258 while (enumerator->enumerate(enumerator, &handler, &type, &data))
259 {
260 configuration_attribute_t *ca;
261 entry_t *entry;
262
263 /* create configuration attribute */
264 DBG2(DBG_IKE, "building %N attribute",
265 configuration_attribute_type_names, type);
266 ca = configuration_attribute_create_value(type, data);
267 if (!cp)
268 {
269 cp = cp_payload_create_type(CFG_REQUEST);
270 }
271 cp->add_attribute(cp, ca);
272
273 /* save handler along with requested type */
274 entry = malloc_thing(entry_t);
275 entry->type = type;
276 entry->handler = handler;
277
278 this->requested->insert_last(this->requested, entry);
279 }
280 enumerator->destroy(enumerator);
281
282 if (cp)
283 {
284 message->add_payload(message, (payload_t*)cp);
285 }
286 }
287 return NEED_MORE;
288 }
289
290 /**
291 * Implementation of task_t.process for responder
292 */
293 static status_t process_r(private_ike_config_t *this, message_t *message)
294 {
295 if (message->get_message_id(message) == 1)
296 { /* in first IKE_AUTH only */
297 process_payloads(this, message);
298 }
299 return NEED_MORE;
300 }
301
302 /**
303 * Implementation of task_t.build for responder
304 */
305 static status_t build_r(private_ike_config_t *this, message_t *message)
306 {
307 if (this->ike_sa->get_state(this->ike_sa) == IKE_ESTABLISHED)
308 { /* in last IKE_AUTH exchange */
309 enumerator_t *enumerator;
310 configuration_attribute_type_t type;
311 chunk_t value;
312 host_t *vip = NULL;
313 cp_payload_t *cp = NULL;
314 peer_cfg_t *config;
315 identification_t *id;
316
317 id = this->ike_sa->get_other_eap_id(this->ike_sa);
318
319 config = this->ike_sa->get_peer_cfg(this->ike_sa);
320 if (config && this->virtual_ip)
321 {
322 DBG1(DBG_IKE, "peer requested virtual IP %H", this->virtual_ip);
323 if (config->get_pool(config))
324 {
325 vip = hydra->attributes->acquire_address(hydra->attributes,
326 config->get_pool(config), id, this->virtual_ip);
327 }
328 if (vip == NULL)
329 {
330 DBG1(DBG_IKE, "no virtual IP found, sending %N",
331 notify_type_names, INTERNAL_ADDRESS_FAILURE);
332 message->add_notify(message, FALSE, INTERNAL_ADDRESS_FAILURE,
333 chunk_empty);
334 return SUCCESS;
335 }
336 DBG1(DBG_IKE, "assigning virtual IP %H to peer '%Y'", vip, id);
337 this->ike_sa->set_virtual_ip(this->ike_sa, FALSE, vip);
338
339 cp = cp_payload_create_type(CFG_REPLY);
340 cp->add_attribute(cp, build_vip(vip));
341 }
342
343 /* query registered providers for additional attributes to include */
344 enumerator = hydra->attributes->create_responder_enumerator(
345 hydra->attributes, config->get_pool(config), id, vip);
346 while (enumerator->enumerate(enumerator, &type, &value))
347 {
348 if (!cp)
349 {
350 cp = cp_payload_create_type(CFG_REPLY);
351 }
352 DBG2(DBG_IKE, "building %N attribute",
353 configuration_attribute_type_names, type);
354 cp->add_attribute(cp,
355 configuration_attribute_create_value(type, value));
356 }
357 enumerator->destroy(enumerator);
358
359 if (cp)
360 {
361 message->add_payload(message, (payload_t*)cp);
362 }
363 DESTROY_IF(vip);
364 return SUCCESS;
365 }
366 return NEED_MORE;
367 }
368
369 /**
370 * Implementation of task_t.process for initiator
371 */
372 static status_t process_i(private_ike_config_t *this, message_t *message)
373 {
374 if (this->ike_sa->get_state(this->ike_sa) == IKE_ESTABLISHED)
375 { /* in last IKE_AUTH exchange */
376
377 process_payloads(this, message);
378
379 if (this->virtual_ip)
380 {
381 this->ike_sa->set_virtual_ip(this->ike_sa, TRUE, this->virtual_ip);
382 }
383 return SUCCESS;
384 }
385 return NEED_MORE;
386 }
387
388 /**
389 * Implementation of task_t.get_type
390 */
391 static task_type_t get_type(private_ike_config_t *this)
392 {
393 return IKE_CONFIG;
394 }
395
396 /**
397 * Implementation of task_t.migrate
398 */
399 static void migrate(private_ike_config_t *this, ike_sa_t *ike_sa)
400 {
401 DESTROY_IF(this->virtual_ip);
402
403 this->ike_sa = ike_sa;
404 this->virtual_ip = NULL;
405 this->requested->destroy_function(this->requested, free);
406 this->requested = linked_list_create();
407 }
408
409 /**
410 * Implementation of task_t.destroy
411 */
412 static void destroy(private_ike_config_t *this)
413 {
414 DESTROY_IF(this->virtual_ip);
415 this->requested->destroy_function(this->requested, free);
416 free(this);
417 }
418
419 /*
420 * Described in header.
421 */
422 ike_config_t *ike_config_create(ike_sa_t *ike_sa, bool initiator)
423 {
424 private_ike_config_t *this = malloc_thing(private_ike_config_t);
425
426 this->public.task.get_type = (task_type_t(*)(task_t*))get_type;
427 this->public.task.migrate = (void(*)(task_t*,ike_sa_t*))migrate;
428 this->public.task.destroy = (void(*)(task_t*))destroy;
429
430 this->initiator = initiator;
431 this->ike_sa = ike_sa;
432 this->virtual_ip = NULL;
433 this->requested = linked_list_create();
434
435 if (initiator)
436 {
437 this->public.task.build = (status_t(*)(task_t*,message_t*))build_i;
438 this->public.task.process = (status_t(*)(task_t*,message_t*))process_i;
439 }
440 else
441 {
442 this->public.task.build = (status_t(*)(task_t*,message_t*))build_r;
443 this->public.task.process = (status_t(*)(task_t*,message_t*))process_r;
444 }
445
446 return &this->public;
447 }
448