attribute-manager: Pass the full IKE_SA to provider methods
[strongswan.git] / src / libcharon / sa / ikev1 / tasks / mode_config.c
1 /*
2 * Copyright (C) 2011 Martin Willi
3 * Copyright (C) 2011 revosec AG
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 "mode_config.h"
17
18 #include <daemon.h>
19 #include <encoding/payloads/cp_payload.h>
20
21 typedef struct private_mode_config_t private_mode_config_t;
22
23 /**
24 * Private members of a mode_config_t task.
25 */
26 struct private_mode_config_t {
27
28 /**
29 * Public methods and task_t interface.
30 */
31 mode_config_t public;
32
33 /**
34 * Assigned IKE_SA.
35 */
36 ike_sa_t *ike_sa;
37
38 /**
39 * Are we the initiator?
40 */
41 bool initiator;
42
43 /**
44 * Use pull (CFG_REQUEST/RESPONSE) or push (CFG_SET/ACK)?
45 */
46 bool pull;
47
48 /**
49 * Received list of virtual IPs, host_t*
50 */
51 linked_list_t *vips;
52
53 /**
54 * Requested/received list of attributes, entry_t
55 */
56 linked_list_t *attributes;
57
58 /**
59 * Identifier to include in response
60 */
61 u_int16_t identifier;
62 };
63
64 /**
65 * Entry for a attribute and associated handler
66 */
67 typedef struct {
68 /** attribute type */
69 configuration_attribute_type_t type;
70 /** handler for this attribute */
71 attribute_handler_t *handler;
72 } entry_t;
73
74 /**
75 * build INTERNAL_IPV4/6_ADDRESS attribute from virtual ip
76 */
77 static configuration_attribute_t *build_vip(host_t *vip)
78 {
79 configuration_attribute_type_t type;
80 chunk_t chunk, prefix;
81
82 if (vip->get_family(vip) == AF_INET)
83 {
84 type = INTERNAL_IP4_ADDRESS;
85 if (vip->is_anyaddr(vip))
86 {
87 chunk = chunk_empty;
88 }
89 else
90 {
91 chunk = vip->get_address(vip);
92 }
93 }
94 else
95 {
96 type = INTERNAL_IP6_ADDRESS;
97 if (vip->is_anyaddr(vip))
98 {
99 chunk = chunk_empty;
100 }
101 else
102 {
103 prefix = chunk_alloca(1);
104 *prefix.ptr = 64;
105 chunk = vip->get_address(vip);
106 chunk = chunk_cata("cc", chunk, prefix);
107 }
108 }
109 return configuration_attribute_create_chunk(PLV1_CONFIGURATION_ATTRIBUTE,
110 type, chunk);
111 }
112
113 /**
114 * Handle a received attribute as initiator
115 */
116 static void handle_attribute(private_mode_config_t *this,
117 configuration_attribute_t *ca)
118 {
119 attribute_handler_t *handler = NULL;
120 enumerator_t *enumerator;
121 entry_t *entry;
122
123 /* find the handler which requested this attribute */
124 enumerator = this->attributes->create_enumerator(this->attributes);
125 while (enumerator->enumerate(enumerator, &entry))
126 {
127 if (entry->type == ca->get_type(ca))
128 {
129 handler = entry->handler;
130 this->attributes->remove_at(this->attributes, enumerator);
131 free(entry);
132 break;
133 }
134 }
135 enumerator->destroy(enumerator);
136
137 /* and pass it to the handle function */
138 handler = charon->attributes->handle(charon->attributes,
139 this->ike_sa->get_other_id(this->ike_sa), handler,
140 ca->get_type(ca), ca->get_chunk(ca));
141 this->ike_sa->add_configuration_attribute(this->ike_sa,
142 handler, ca->get_type(ca), ca->get_chunk(ca));
143 }
144
145 /**
146 * process a single configuration attribute
147 */
148 static void process_attribute(private_mode_config_t *this,
149 configuration_attribute_t *ca)
150 {
151 host_t *ip;
152 chunk_t addr;
153 int family = AF_INET6;
154
155 switch (ca->get_type(ca))
156 {
157 case INTERNAL_IP4_ADDRESS:
158 family = AF_INET;
159 /* fall */
160 case INTERNAL_IP6_ADDRESS:
161 {
162 addr = ca->get_chunk(ca);
163 if (addr.len == 0)
164 {
165 ip = host_create_any(family);
166 }
167 else
168 {
169 /* skip prefix byte in IPv6 payload*/
170 if (family == AF_INET6)
171 {
172 addr.len--;
173 }
174 ip = host_create_from_chunk(family, addr, 0);
175 }
176 if (ip)
177 {
178 this->vips->insert_last(this->vips, ip);
179 }
180 break;
181 }
182 default:
183 {
184 if (this->initiator == this->pull)
185 {
186 handle_attribute(this, ca);
187 }
188 }
189 }
190 }
191
192 /**
193 * Check if config allows push mode when acting as task responder
194 */
195 static bool accept_push(private_mode_config_t *this)
196 {
197 enumerator_t *enumerator;
198 peer_cfg_t *config;
199 bool vip;
200 host_t *host;
201
202 config = this->ike_sa->get_peer_cfg(this->ike_sa);
203 enumerator = config->create_virtual_ip_enumerator(config);
204 vip = enumerator->enumerate(enumerator, &host);
205 enumerator->destroy(enumerator);
206
207 return vip && !config->use_pull_mode(config);
208 }
209
210 /**
211 * Scan for configuration payloads and attributes
212 */
213 static void process_payloads(private_mode_config_t *this, message_t *message)
214 {
215 enumerator_t *enumerator, *attributes;
216 payload_t *payload;
217
218 enumerator = message->create_payload_enumerator(message);
219 while (enumerator->enumerate(enumerator, &payload))
220 {
221 if (payload->get_type(payload) == PLV1_CONFIGURATION)
222 {
223 cp_payload_t *cp = (cp_payload_t*)payload;
224 configuration_attribute_t *ca;
225
226 switch (cp->get_type(cp))
227 {
228 case CFG_SET:
229 /* when acting as a responder, we detect the mode using
230 * the type of configuration payload. But we should double
231 * check the peer is allowed to use push mode on us. */
232 if (!this->initiator && accept_push(this))
233 {
234 this->pull = FALSE;
235 }
236 /* FALL */
237 case CFG_REQUEST:
238 this->identifier = cp->get_identifier(cp);
239 /* FALL */
240 case CFG_REPLY:
241 attributes = cp->create_attribute_enumerator(cp);
242 while (attributes->enumerate(attributes, &ca))
243 {
244 DBG2(DBG_IKE, "processing %N attribute",
245 configuration_attribute_type_names, ca->get_type(ca));
246 process_attribute(this, ca);
247 }
248 attributes->destroy(attributes);
249 break;
250 case CFG_ACK:
251 break;
252 default:
253 DBG1(DBG_IKE, "ignoring %N config payload",
254 config_type_names, cp->get_type(cp));
255 break;
256 }
257 }
258 }
259 enumerator->destroy(enumerator);
260 }
261
262 /**
263 * Add an attribute to a configuration payload, and store it in task
264 */
265 static void add_attribute(private_mode_config_t *this, cp_payload_t *cp,
266 configuration_attribute_type_t type, chunk_t data,
267 attribute_handler_t *handler)
268 {
269 entry_t *entry;
270
271 cp->add_attribute(cp,
272 configuration_attribute_create_chunk(PLV1_CONFIGURATION_ATTRIBUTE,
273 type, data));
274 INIT(entry,
275 .type = type,
276 .handler = handler,
277 );
278 this->attributes->insert_last(this->attributes, entry);
279 }
280
281 /**
282 * Build a CFG_REQUEST as initiator
283 */
284 static status_t build_request(private_mode_config_t *this, message_t *message)
285 {
286 cp_payload_t *cp;
287 enumerator_t *enumerator;
288 attribute_handler_t *handler;
289 peer_cfg_t *config;
290 configuration_attribute_type_t type;
291 chunk_t data;
292 linked_list_t *vips;
293 host_t *host;
294
295 cp = cp_payload_create_type(PLV1_CONFIGURATION, CFG_REQUEST);
296
297 vips = linked_list_create();
298
299 /* reuse virtual IP if we already have one */
300 enumerator = this->ike_sa->create_virtual_ip_enumerator(this->ike_sa, TRUE);
301 while (enumerator->enumerate(enumerator, &host))
302 {
303 vips->insert_last(vips, host);
304 }
305 enumerator->destroy(enumerator);
306
307 if (vips->get_count(vips) == 0)
308 {
309 config = this->ike_sa->get_peer_cfg(this->ike_sa);
310 enumerator = config->create_virtual_ip_enumerator(config);
311 while (enumerator->enumerate(enumerator, &host))
312 {
313 vips->insert_last(vips, host);
314 }
315 enumerator->destroy(enumerator);
316 }
317
318 if (vips->get_count(vips))
319 {
320 enumerator = vips->create_enumerator(vips);
321 while (enumerator->enumerate(enumerator, &host))
322 {
323 cp->add_attribute(cp, build_vip(host));
324 }
325 enumerator->destroy(enumerator);
326 }
327
328 enumerator = charon->attributes->create_initiator_enumerator(
329 charon->attributes,
330 this->ike_sa->get_other_id(this->ike_sa), vips);
331 while (enumerator->enumerate(enumerator, &handler, &type, &data))
332 {
333 add_attribute(this, cp, type, data, handler);
334 }
335 enumerator->destroy(enumerator);
336
337 vips->destroy(vips);
338
339 message->add_payload(message, (payload_t*)cp);
340
341 return NEED_MORE;
342 }
343
344 /**
345 * Build a CFG_SET as initiator
346 */
347 static status_t build_set(private_mode_config_t *this, message_t *message)
348 {
349 enumerator_t *enumerator;
350 configuration_attribute_type_t type;
351 chunk_t value;
352 cp_payload_t *cp;
353 peer_cfg_t *config;
354 identification_t *id;
355 linked_list_t *pools;
356 host_t *any4, *any6, *found;
357 char *name;
358
359 cp = cp_payload_create_type(PLV1_CONFIGURATION, CFG_SET);
360
361 id = this->ike_sa->get_other_eap_id(this->ike_sa);
362 config = this->ike_sa->get_peer_cfg(this->ike_sa);
363 any4 = host_create_any(AF_INET);
364 any6 = host_create_any(AF_INET6);
365
366 this->ike_sa->clear_virtual_ips(this->ike_sa, FALSE);
367
368 /* in push mode, we ask each configured pool for an address */
369 enumerator = config->create_pool_enumerator(config);
370 while (enumerator->enumerate(enumerator, &name))
371 {
372 pools = linked_list_create_with_items(name, NULL);
373 /* try IPv4, then IPv6 */
374 found = charon->attributes->acquire_address(charon->attributes,
375 pools, this->ike_sa, any4);
376 if (!found)
377 {
378 found = charon->attributes->acquire_address(charon->attributes,
379 pools, this->ike_sa, any6);
380 }
381 pools->destroy(pools);
382 if (found)
383 {
384 DBG1(DBG_IKE, "assigning virtual IP %H to peer '%Y'", found, id);
385 this->ike_sa->add_virtual_ip(this->ike_sa, FALSE, found);
386 cp->add_attribute(cp, build_vip(found));
387 this->vips->insert_last(this->vips, found);
388 }
389 }
390 enumerator->destroy(enumerator);
391
392 any4->destroy(any4);
393 any6->destroy(any6);
394
395 charon->bus->assign_vips(charon->bus, this->ike_sa, TRUE);
396
397 /* query registered providers for additional attributes to include */
398 pools = linked_list_create_from_enumerator(
399 config->create_pool_enumerator(config));
400 enumerator = charon->attributes->create_responder_enumerator(
401 charon->attributes, pools, this->ike_sa, this->vips);
402 while (enumerator->enumerate(enumerator, &type, &value))
403 {
404 add_attribute(this, cp, type, value, NULL);
405 }
406 enumerator->destroy(enumerator);
407 pools->destroy(pools);
408
409 message->add_payload(message, (payload_t*)cp);
410
411 return SUCCESS;
412 }
413
414 METHOD(task_t, build_i, status_t,
415 private_mode_config_t *this, message_t *message)
416 {
417 if (this->pull)
418 {
419 return build_request(this, message);
420 }
421 return build_set(this, message);
422 }
423
424 /**
425 * Store received virtual IPs to the IKE_SA, install them
426 */
427 static void install_vips(private_mode_config_t *this)
428 {
429 enumerator_t *enumerator;
430 host_t *host;
431
432 this->ike_sa->clear_virtual_ips(this->ike_sa, TRUE);
433
434 enumerator = this->vips->create_enumerator(this->vips);
435 while (enumerator->enumerate(enumerator, &host))
436 {
437 if (!host->is_anyaddr(host))
438 {
439 this->ike_sa->add_virtual_ip(this->ike_sa, TRUE, host);
440 }
441 }
442 enumerator->destroy(enumerator);
443
444 charon->bus->handle_vips(charon->bus, this->ike_sa, TRUE);
445 }
446
447 METHOD(task_t, process_r, status_t,
448 private_mode_config_t *this, message_t *message)
449 {
450 process_payloads(this, message);
451
452 if (!this->pull)
453 {
454 install_vips(this);
455 }
456 return NEED_MORE;
457 }
458
459 /**
460 * Build CFG_REPLY message after receiving CFG_REQUEST
461 */
462 static status_t build_reply(private_mode_config_t *this, message_t *message)
463 {
464 enumerator_t *enumerator;
465 configuration_attribute_type_t type;
466 chunk_t value;
467 cp_payload_t *cp;
468 peer_cfg_t *config;
469 identification_t *id;
470 linked_list_t *vips, *pools;
471 host_t *requested;
472
473 cp = cp_payload_create_type(PLV1_CONFIGURATION, CFG_REPLY);
474
475 id = this->ike_sa->get_other_eap_id(this->ike_sa);
476 config = this->ike_sa->get_peer_cfg(this->ike_sa);
477 vips = linked_list_create();
478 pools = linked_list_create_from_enumerator(
479 config->create_pool_enumerator(config));
480
481 this->ike_sa->clear_virtual_ips(this->ike_sa, FALSE);
482
483 enumerator = this->vips->create_enumerator(this->vips);
484 while (enumerator->enumerate(enumerator, &requested))
485 {
486 host_t *found = NULL;
487
488 /* query all pools until we get an address */
489 DBG1(DBG_IKE, "peer requested virtual IP %H", requested);
490
491 found = charon->attributes->acquire_address(charon->attributes,
492 pools, this->ike_sa, requested);
493 if (found)
494 {
495 DBG1(DBG_IKE, "assigning virtual IP %H to peer '%Y'", found, id);
496 this->ike_sa->add_virtual_ip(this->ike_sa, FALSE, found);
497 cp->add_attribute(cp, build_vip(found));
498 vips->insert_last(vips, found);
499 }
500 else
501 {
502 DBG1(DBG_IKE, "no virtual IP found for %H requested by '%Y'",
503 requested, id);
504 }
505 }
506 enumerator->destroy(enumerator);
507
508 charon->bus->assign_vips(charon->bus, this->ike_sa, TRUE);
509
510 /* query registered providers for additional attributes to include */
511 enumerator = charon->attributes->create_responder_enumerator(
512 charon->attributes, pools, this->ike_sa, vips);
513 while (enumerator->enumerate(enumerator, &type, &value))
514 {
515 cp->add_attribute(cp,
516 configuration_attribute_create_chunk(PLV1_CONFIGURATION_ATTRIBUTE,
517 type, value));
518 }
519 enumerator->destroy(enumerator);
520 vips->destroy_offset(vips, offsetof(host_t, destroy));
521 pools->destroy(pools);
522
523 cp->set_identifier(cp, this->identifier);
524 message->add_payload(message, (payload_t*)cp);
525
526 return SUCCESS;
527 }
528
529 /**
530 * Build CFG_ACK for a received CFG_SET
531 */
532 static status_t build_ack(private_mode_config_t *this, message_t *message)
533 {
534 cp_payload_t *cp;
535 enumerator_t *enumerator;
536 host_t *host;
537 configuration_attribute_type_t type;
538 entry_t *entry;
539
540 cp = cp_payload_create_type(PLV1_CONFIGURATION, CFG_ACK);
541
542 /* return empty attributes for installed IPs */
543
544 enumerator = this->vips->create_enumerator(this->vips);
545 while (enumerator->enumerate(enumerator, &host))
546 {
547 type = INTERNAL_IP6_ADDRESS;
548 if (host->get_family(host) == AF_INET6)
549 {
550 type = INTERNAL_IP6_ADDRESS;
551 }
552 else
553 {
554 type = INTERNAL_IP4_ADDRESS;
555 }
556 cp->add_attribute(cp, configuration_attribute_create_chunk(
557 PLV1_CONFIGURATION_ATTRIBUTE, type, chunk_empty));
558 }
559 enumerator->destroy(enumerator);
560
561 enumerator = this->attributes->create_enumerator(this->attributes);
562 while (enumerator->enumerate(enumerator, &entry))
563 {
564 cp->add_attribute(cp,
565 configuration_attribute_create_chunk(PLV1_CONFIGURATION_ATTRIBUTE,
566 entry->type, chunk_empty));
567 }
568 enumerator->destroy(enumerator);
569
570 cp->set_identifier(cp, this->identifier);
571 message->add_payload(message, (payload_t*)cp);
572
573 return SUCCESS;
574 }
575
576 METHOD(task_t, build_r, status_t,
577 private_mode_config_t *this, message_t *message)
578 {
579 if (this->pull)
580 {
581 return build_reply(this, message);
582 }
583 return build_ack(this, message);
584 }
585
586 METHOD(task_t, process_i, status_t,
587 private_mode_config_t *this, message_t *message)
588 {
589 process_payloads(this, message);
590
591 if (this->pull)
592 {
593 install_vips(this);
594 }
595 return SUCCESS;
596 }
597
598 METHOD(task_t, get_type, task_type_t,
599 private_mode_config_t *this)
600 {
601 return TASK_MODE_CONFIG;
602 }
603
604 METHOD(task_t, migrate, void,
605 private_mode_config_t *this, ike_sa_t *ike_sa)
606 {
607 this->ike_sa = ike_sa;
608 this->vips->destroy_offset(this->vips, offsetof(host_t, destroy));
609 this->vips = linked_list_create();
610 this->attributes->destroy_function(this->attributes, free);
611 this->attributes = linked_list_create();
612 }
613
614 METHOD(task_t, destroy, void,
615 private_mode_config_t *this)
616 {
617 this->vips->destroy_offset(this->vips, offsetof(host_t, destroy));
618 this->attributes->destroy_function(this->attributes, free);
619 free(this);
620 }
621
622 /*
623 * Described in header.
624 */
625 mode_config_t *mode_config_create(ike_sa_t *ike_sa, bool initiator, bool pull)
626 {
627 private_mode_config_t *this;
628
629 INIT(this,
630 .public = {
631 .task = {
632 .get_type = _get_type,
633 .migrate = _migrate,
634 .destroy = _destroy,
635 },
636 },
637 .initiator = initiator,
638 .pull = initiator ? pull : TRUE,
639 .ike_sa = ike_sa,
640 .attributes = linked_list_create(),
641 .vips = linked_list_create(),
642 );
643
644 if (initiator)
645 {
646 this->public.task.build = _build_i;
647 this->public.task.process = _process_i;
648 }
649 else
650 {
651 this->public.task.build = _build_r;
652 this->public.task.process = _process_r;
653 }
654
655 return &this->public;
656 }