Support multiple virtual IPs on peer_cfg and ike_sa classes
[strongswan.git] / src / libcharon / plugins / ha / ha_dispatcher.c
1 /*
2 * Copyright (C) 2008 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 "ha_dispatcher.h"
17
18 #include <daemon.h>
19 #include <sa/ikev2/keymat_v2.h>
20 #include <sa/ikev1/keymat_v1.h>
21 #include <processing/jobs/callback_job.h>
22 #include <processing/jobs/adopt_children_job.h>
23
24 typedef struct private_ha_dispatcher_t private_ha_dispatcher_t;
25 typedef struct ha_diffie_hellman_t ha_diffie_hellman_t;
26
27 /**
28 * Private data of an ha_dispatcher_t object.
29 */
30 struct private_ha_dispatcher_t {
31
32 /**
33 * Public ha_dispatcher_t interface.
34 */
35 ha_dispatcher_t public;
36
37 /**
38 * socket to pull messages from
39 */
40 ha_socket_t *socket;
41
42 /**
43 * segments to control
44 */
45 ha_segments_t *segments;
46
47 /**
48 * Cache for resync
49 */
50 ha_cache_t *cache;
51
52 /**
53 * Kernel helper
54 */
55 ha_kernel_t *kernel;
56
57 /**
58 * HA enabled pool
59 */
60 ha_attribute_t *attr;
61 };
62
63 /**
64 * DH implementation for HA synced DH values
65 */
66 struct ha_diffie_hellman_t {
67
68 /**
69 * Implements diffie_hellman_t
70 */
71 diffie_hellman_t dh;
72
73 /**
74 * Shared secret
75 */
76 chunk_t secret;
77
78 /**
79 * Own public value
80 */
81 chunk_t pub;
82 };
83
84 METHOD(diffie_hellman_t, dh_get_shared_secret, status_t,
85 ha_diffie_hellman_t *this, chunk_t *secret)
86 {
87 *secret = chunk_clone(this->secret);
88 return SUCCESS;
89 }
90
91 METHOD(diffie_hellman_t, dh_get_my_public_value, void,
92 ha_diffie_hellman_t *this, chunk_t *value)
93 {
94 *value = chunk_clone(this->pub);
95 }
96
97 METHOD(diffie_hellman_t, dh_destroy, void,
98 ha_diffie_hellman_t *this)
99 {
100 free(this);
101 }
102
103 /**
104 * Create a HA synced DH implementation
105 */
106 static diffie_hellman_t *ha_diffie_hellman_create(chunk_t secret, chunk_t pub)
107 {
108 ha_diffie_hellman_t *this;
109
110 INIT(this,
111 .dh = {
112 .get_shared_secret = _dh_get_shared_secret,
113 .get_my_public_value = _dh_get_my_public_value,
114 .destroy = _dh_destroy,
115 },
116 .secret = secret,
117 .pub = pub,
118 );
119
120 return &this->dh;
121 }
122
123 /**
124 * Process messages of type IKE_ADD
125 */
126 static void process_ike_add(private_ha_dispatcher_t *this, ha_message_t *message)
127 {
128 ha_message_attribute_t attribute;
129 ha_message_value_t value;
130 enumerator_t *enumerator;
131 ike_sa_t *ike_sa = NULL, *old_sa = NULL;
132 ike_version_t version = IKEV2;
133 u_int16_t encr = 0, len = 0, integ = 0, prf = 0, old_prf = PRF_UNDEFINED;
134 chunk_t nonce_i = chunk_empty, nonce_r = chunk_empty;
135 chunk_t secret = chunk_empty, old_skd = chunk_empty;
136 chunk_t dh_local = chunk_empty, dh_remote = chunk_empty, psk = chunk_empty;
137 bool ok = FALSE;
138
139 enumerator = message->create_attribute_enumerator(message);
140 while (enumerator->enumerate(enumerator, &attribute, &value))
141 {
142 switch (attribute)
143 {
144 case HA_IKE_ID:
145 ike_sa = ike_sa_create(value.ike_sa_id,
146 value.ike_sa_id->is_initiator(value.ike_sa_id), version);
147 break;
148 case HA_IKE_REKEY_ID:
149 old_sa = charon->ike_sa_manager->checkout(charon->ike_sa_manager,
150 value.ike_sa_id);
151 break;
152 case HA_IKE_VERSION:
153 version = value.u8;
154 break;
155 case HA_NONCE_I:
156 nonce_i = value.chunk;
157 break;
158 case HA_NONCE_R:
159 nonce_r = value.chunk;
160 break;
161 case HA_SECRET:
162 secret = value.chunk;
163 break;
164 case HA_LOCAL_DH:
165 dh_local = value.chunk;
166 break;
167 case HA_REMOTE_DH:
168 dh_remote = value.chunk;
169 break;
170 case HA_PSK:
171 psk = value.chunk;
172 break;
173 case HA_OLD_SKD:
174 old_skd = value.chunk;
175 break;
176 case HA_ALG_ENCR:
177 encr = value.u16;
178 break;
179 case HA_ALG_ENCR_LEN:
180 len = value.u16;
181 break;
182 case HA_ALG_INTEG:
183 integ = value.u16;
184 break;
185 case HA_ALG_PRF:
186 prf = value.u16;
187 break;
188 case HA_ALG_OLD_PRF:
189 old_prf = value.u16;
190 break;
191 default:
192 break;
193 }
194 }
195 enumerator->destroy(enumerator);
196
197 if (ike_sa)
198 {
199 proposal_t *proposal;
200 diffie_hellman_t *dh;
201
202 proposal = proposal_create(PROTO_IKE, 0);
203 if (integ)
204 {
205 proposal->add_algorithm(proposal, INTEGRITY_ALGORITHM, integ, 0);
206 }
207 if (encr)
208 {
209 proposal->add_algorithm(proposal, ENCRYPTION_ALGORITHM, encr, len);
210 }
211 if (prf)
212 {
213 proposal->add_algorithm(proposal, PSEUDO_RANDOM_FUNCTION, prf, 0);
214 }
215 charon->bus->set_sa(charon->bus, ike_sa);
216 dh = ha_diffie_hellman_create(secret, dh_local);
217 if (ike_sa->get_version(ike_sa) == IKEV2)
218 {
219 keymat_v2_t *keymat_v2 = (keymat_v2_t*)ike_sa->get_keymat(ike_sa);
220
221 ok = keymat_v2->derive_ike_keys(keymat_v2, proposal, dh, nonce_i,
222 nonce_r, ike_sa->get_id(ike_sa), old_prf, old_skd);
223 }
224 if (ike_sa->get_version(ike_sa) == IKEV1)
225 {
226 keymat_v1_t *keymat_v1 = (keymat_v1_t*)ike_sa->get_keymat(ike_sa);
227 shared_key_t *shared = NULL;
228 auth_method_t method = AUTH_RSA;
229
230 if (psk.len)
231 {
232 method = AUTH_PSK;
233 shared = shared_key_create(SHARED_IKE, chunk_clone(psk));
234 }
235 if (keymat_v1->create_hasher(keymat_v1, proposal))
236 {
237 ok = keymat_v1->derive_ike_keys(keymat_v1, proposal,
238 dh, dh_remote, nonce_i, nonce_r,
239 ike_sa->get_id(ike_sa), method, shared);
240 }
241 DESTROY_IF(shared);
242 }
243 dh->destroy(dh);
244 if (ok)
245 {
246 if (old_sa)
247 {
248 peer_cfg_t *peer_cfg = old_sa->get_peer_cfg(old_sa);
249
250 if (peer_cfg)
251 {
252 ike_sa->set_peer_cfg(ike_sa, peer_cfg);
253 ike_sa->inherit(ike_sa, old_sa);
254 }
255 charon->ike_sa_manager->checkin_and_destroy(
256 charon->ike_sa_manager, old_sa);
257 old_sa = NULL;
258 }
259 ike_sa->set_state(ike_sa, IKE_CONNECTING);
260 ike_sa->set_proposal(ike_sa, proposal);
261 this->cache->cache(this->cache, ike_sa, message);
262 message = NULL;
263 charon->ike_sa_manager->checkin(charon->ike_sa_manager, ike_sa);
264 }
265 else
266 {
267 DBG1(DBG_IKE, "HA keymat derivation failed");
268 ike_sa->destroy(ike_sa);
269 }
270 charon->bus->set_sa(charon->bus, NULL);
271 proposal->destroy(proposal);
272 }
273 if (old_sa)
274 {
275 charon->ike_sa_manager->checkin(charon->ike_sa_manager, old_sa);
276 }
277 DESTROY_IF(message);
278 }
279
280 /**
281 * Apply a condition flag to the IKE_SA if it is in set
282 */
283 static void set_condition(ike_sa_t *ike_sa, ike_condition_t set,
284 ike_condition_t flag)
285 {
286 ike_sa->set_condition(ike_sa, flag, flag & set);
287 }
288
289 /**
290 * Apply a extension flag to the IKE_SA if it is in set
291 */
292 static void set_extension(ike_sa_t *ike_sa, ike_extension_t set,
293 ike_extension_t flag)
294 {
295 if (flag & set)
296 {
297 ike_sa->enable_extension(ike_sa, flag);
298 }
299 }
300
301 /**
302 * Process messages of type IKE_UPDATE
303 */
304 static void process_ike_update(private_ha_dispatcher_t *this,
305 ha_message_t *message)
306 {
307 ha_message_attribute_t attribute;
308 ha_message_value_t value;
309 enumerator_t *enumerator;
310 ike_sa_t *ike_sa = NULL;
311 peer_cfg_t *peer_cfg = NULL;
312 auth_cfg_t *auth;
313 bool received_vip = FALSE, first_peer_addr = TRUE;
314
315 enumerator = message->create_attribute_enumerator(message);
316 while (enumerator->enumerate(enumerator, &attribute, &value))
317 {
318 if (attribute != HA_IKE_ID && ike_sa == NULL)
319 {
320 /* must be first attribute */
321 break;
322 }
323 switch (attribute)
324 {
325 case HA_IKE_ID:
326 ike_sa = charon->ike_sa_manager->checkout(charon->ike_sa_manager,
327 value.ike_sa_id);
328 break;
329 case HA_LOCAL_ID:
330 ike_sa->set_my_id(ike_sa, value.id->clone(value.id));
331 break;
332 case HA_REMOTE_ID:
333 ike_sa->set_other_id(ike_sa, value.id->clone(value.id));
334 break;
335 case HA_REMOTE_EAP_ID:
336 auth = auth_cfg_create();
337 auth->add(auth, AUTH_RULE_EAP_IDENTITY, value.id->clone(value.id));
338 ike_sa->add_auth_cfg(ike_sa, FALSE, auth);
339 break;
340 case HA_LOCAL_ADDR:
341 ike_sa->set_my_host(ike_sa, value.host->clone(value.host));
342 break;
343 case HA_REMOTE_ADDR:
344 ike_sa->set_other_host(ike_sa, value.host->clone(value.host));
345 break;
346 case HA_LOCAL_VIP:
347 ike_sa->add_virtual_ip(ike_sa, TRUE, value.host);
348 break;
349 case HA_REMOTE_VIP:
350 ike_sa->add_virtual_ip(ike_sa, FALSE, value.host);
351 received_vip = TRUE;
352 break;
353 case HA_PEER_ADDR:
354 if (first_peer_addr)
355 {
356 ike_sa->clear_peer_addresses(ike_sa);
357 first_peer_addr = FALSE;
358 }
359 ike_sa->add_peer_address(ike_sa, value.host->clone(value.host));
360 break;
361 case HA_CONFIG_NAME:
362 peer_cfg = charon->backends->get_peer_cfg_by_name(
363 charon->backends, value.str);
364 if (peer_cfg)
365 {
366 ike_sa->set_peer_cfg(ike_sa, peer_cfg);
367 peer_cfg->destroy(peer_cfg);
368 }
369 else
370 {
371 DBG1(DBG_IKE, "HA is missing nodes peer configuration");
372 }
373 break;
374 case HA_EXTENSIONS:
375 set_extension(ike_sa, value.u32, EXT_NATT);
376 set_extension(ike_sa, value.u32, EXT_MOBIKE);
377 set_extension(ike_sa, value.u32, EXT_HASH_AND_URL);
378 set_extension(ike_sa, value.u32, EXT_MULTIPLE_AUTH);
379 set_extension(ike_sa, value.u32, EXT_STRONGSWAN);
380 set_extension(ike_sa, value.u32, EXT_EAP_ONLY_AUTHENTICATION);
381 set_extension(ike_sa, value.u32, EXT_MS_WINDOWS);
382 set_extension(ike_sa, value.u32, EXT_XAUTH);
383 set_extension(ike_sa, value.u32, EXT_DPD);
384 break;
385 case HA_CONDITIONS:
386 set_condition(ike_sa, value.u32, COND_NAT_ANY);
387 set_condition(ike_sa, value.u32, COND_NAT_HERE);
388 set_condition(ike_sa, value.u32, COND_NAT_THERE);
389 set_condition(ike_sa, value.u32, COND_NAT_FAKE);
390 set_condition(ike_sa, value.u32, COND_EAP_AUTHENTICATED);
391 set_condition(ike_sa, value.u32, COND_CERTREQ_SEEN);
392 set_condition(ike_sa, value.u32, COND_ORIGINAL_INITIATOR);
393 set_condition(ike_sa, value.u32, COND_STALE);
394 set_condition(ike_sa, value.u32, COND_INIT_CONTACT_SEEN);
395 set_condition(ike_sa, value.u32, COND_XAUTH_AUTHENTICATED);
396 break;
397 default:
398 break;
399 }
400 }
401 enumerator->destroy(enumerator);
402
403 if (ike_sa)
404 {
405 if (ike_sa->get_state(ike_sa) == IKE_CONNECTING &&
406 ike_sa->get_peer_cfg(ike_sa))
407 {
408 DBG1(DBG_CFG, "installed HA passive IKE_SA '%s' %H[%Y]...%H[%Y]",
409 ike_sa->get_name(ike_sa),
410 ike_sa->get_my_host(ike_sa), ike_sa->get_my_id(ike_sa),
411 ike_sa->get_other_host(ike_sa), ike_sa->get_other_id(ike_sa));
412 ike_sa->set_state(ike_sa, IKE_PASSIVE);
413 }
414 if (received_vip)
415 {
416 host_t *vip;
417 char *pool;
418
419 peer_cfg = ike_sa->get_peer_cfg(ike_sa);
420 if (peer_cfg)
421 {
422 pool = peer_cfg->get_pool(peer_cfg);
423 if (pool)
424 {
425 enumerator = ike_sa->create_virtual_ip_enumerator(ike_sa,
426 FALSE);
427 while (enumerator->enumerate(enumerator, &vip))
428 {
429 this->attr->reserve(this->attr, pool, vip);
430 }
431 enumerator->destroy(enumerator);
432 }
433 }
434 }
435 if (ike_sa->get_version(ike_sa) == IKEV1)
436 {
437 lib->processor->queue_job(lib->processor, (job_t*)
438 adopt_children_job_create(ike_sa->get_id(ike_sa)));
439 }
440 this->cache->cache(this->cache, ike_sa, message);
441 charon->ike_sa_manager->checkin(charon->ike_sa_manager, ike_sa);
442 }
443 else
444 {
445 DBG1(DBG_CFG, "passive HA IKE_SA to update not found");
446 message->destroy(message);
447 }
448 }
449
450 /**
451 * Process messages of type IKE_MID_INITIATOR/RESPONDER
452 */
453 static void process_ike_mid(private_ha_dispatcher_t *this,
454 ha_message_t *message, bool initiator)
455 {
456 ha_message_attribute_t attribute;
457 ha_message_value_t value;
458 enumerator_t *enumerator;
459 ike_sa_t *ike_sa = NULL;
460 u_int32_t mid = 0;
461
462 enumerator = message->create_attribute_enumerator(message);
463 while (enumerator->enumerate(enumerator, &attribute, &value))
464 {
465 switch (attribute)
466 {
467 case HA_IKE_ID:
468 ike_sa = charon->ike_sa_manager->checkout(charon->ike_sa_manager,
469 value.ike_sa_id);
470 break;
471 case HA_MID:
472 mid = value.u32;
473 break;
474 default:
475 break;
476 }
477 }
478 enumerator->destroy(enumerator);
479
480 if (ike_sa)
481 {
482 if (mid)
483 {
484 ike_sa->set_message_id(ike_sa, initiator, mid);
485 }
486 this->cache->cache(this->cache, ike_sa, message);
487 charon->ike_sa_manager->checkin(charon->ike_sa_manager, ike_sa);
488 }
489 else
490 {
491 message->destroy(message);
492 }
493 }
494
495 /**
496 * Process messages of type IKE_IV
497 */
498 static void process_ike_iv(private_ha_dispatcher_t *this, ha_message_t *message)
499 {
500 ha_message_attribute_t attribute;
501 ha_message_value_t value;
502 enumerator_t *enumerator;
503 ike_sa_t *ike_sa = NULL;
504 chunk_t iv = chunk_empty;
505
506 enumerator = message->create_attribute_enumerator(message);
507 while (enumerator->enumerate(enumerator, &attribute, &value))
508 {
509 switch (attribute)
510 {
511 case HA_IKE_ID:
512 ike_sa = charon->ike_sa_manager->checkout(charon->ike_sa_manager,
513 value.ike_sa_id);
514 break;
515 case HA_IV:
516 iv = value.chunk;
517 break;
518 default:
519 break;
520 }
521 }
522 enumerator->destroy(enumerator);
523
524 if (ike_sa)
525 {
526 if (ike_sa->get_version(ike_sa) == IKEV1)
527 {
528 if (iv.len)
529 {
530 keymat_v1_t *keymat;
531
532 keymat = (keymat_v1_t*)ike_sa->get_keymat(ike_sa);
533 if (keymat->update_iv(keymat, 0, iv))
534 {
535 keymat->confirm_iv(keymat, 0);
536 }
537 }
538 }
539 this->cache->cache(this->cache, ike_sa, message);
540 charon->ike_sa_manager->checkin(charon->ike_sa_manager, ike_sa);
541 }
542 else
543 {
544 message->destroy(message);
545 }
546 }
547
548 /**
549 * Process messages of type IKE_DELETE
550 */
551 static void process_ike_delete(private_ha_dispatcher_t *this,
552 ha_message_t *message)
553 {
554 ha_message_attribute_t attribute;
555 ha_message_value_t value;
556 enumerator_t *enumerator;
557 ike_sa_t *ike_sa = NULL;
558
559 enumerator = message->create_attribute_enumerator(message);
560 while (enumerator->enumerate(enumerator, &attribute, &value))
561 {
562 switch (attribute)
563 {
564 case HA_IKE_ID:
565 ike_sa = charon->ike_sa_manager->checkout(
566 charon->ike_sa_manager, value.ike_sa_id);
567 break;
568 default:
569 break;
570 }
571 }
572 enumerator->destroy(enumerator);
573 if (ike_sa)
574 {
575 this->cache->cache(this->cache, ike_sa, message);
576 charon->ike_sa_manager->checkin_and_destroy(
577 charon->ike_sa_manager, ike_sa);
578 }
579 else
580 {
581 message->destroy(message);
582 }
583 }
584
585 /**
586 * Lookup a child cfg from the peer cfg by name
587 */
588 static child_cfg_t* find_child_cfg(ike_sa_t *ike_sa, char *name)
589 {
590 peer_cfg_t *peer_cfg;
591 child_cfg_t *current, *found = NULL;
592 enumerator_t *enumerator;
593
594 peer_cfg = ike_sa->get_peer_cfg(ike_sa);
595 if (peer_cfg)
596 {
597 enumerator = peer_cfg->create_child_cfg_enumerator(peer_cfg);
598 while (enumerator->enumerate(enumerator, &current))
599 {
600 if (streq(current->get_name(current), name))
601 {
602 found = current;
603 break;
604 }
605 }
606 enumerator->destroy(enumerator);
607 }
608 return found;
609 }
610
611 /**
612 * Process messages of type CHILD_ADD
613 */
614 static void process_child_add(private_ha_dispatcher_t *this,
615 ha_message_t *message)
616 {
617 ha_message_attribute_t attribute;
618 ha_message_value_t value;
619 enumerator_t *enumerator;
620 ike_sa_t *ike_sa = NULL;
621 char *config_name = "";
622 child_cfg_t *config = NULL;
623 child_sa_t *child_sa;
624 proposal_t *proposal;
625 bool initiator = FALSE, failed = FALSE, ok = FALSE;
626 u_int32_t inbound_spi = 0, outbound_spi = 0;
627 u_int16_t inbound_cpi = 0, outbound_cpi = 0;
628 u_int8_t mode = MODE_TUNNEL, ipcomp = 0;
629 u_int16_t encr = ENCR_UNDEFINED, integ = AUTH_UNDEFINED, len = 0;
630 u_int16_t esn = NO_EXT_SEQ_NUMBERS;
631 u_int seg_i, seg_o;
632 chunk_t nonce_i = chunk_empty, nonce_r = chunk_empty, secret = chunk_empty;
633 chunk_t encr_i, integ_i, encr_r, integ_r;
634 linked_list_t *local_ts, *remote_ts;
635 diffie_hellman_t *dh = NULL;
636
637 enumerator = message->create_attribute_enumerator(message);
638 while (enumerator->enumerate(enumerator, &attribute, &value))
639 {
640 switch (attribute)
641 {
642 case HA_IKE_ID:
643 ike_sa = charon->ike_sa_manager->checkout(charon->ike_sa_manager,
644 value.ike_sa_id);
645 break;
646 case HA_CONFIG_NAME:
647 config_name = value.str;
648 break;
649 case HA_INITIATOR:
650 initiator = value.u8;
651 break;
652 case HA_INBOUND_SPI:
653 inbound_spi = value.u32;
654 break;
655 case HA_OUTBOUND_SPI:
656 outbound_spi = value.u32;
657 break;
658 case HA_INBOUND_CPI:
659 inbound_cpi = value.u32;
660 break;
661 case HA_OUTBOUND_CPI:
662 outbound_cpi = value.u32;
663 break;
664 case HA_IPSEC_MODE:
665 mode = value.u8;
666 break;
667 case HA_IPCOMP:
668 ipcomp = value.u8;
669 break;
670 case HA_ALG_ENCR:
671 encr = value.u16;
672 break;
673 case HA_ALG_ENCR_LEN:
674 len = value.u16;
675 break;
676 case HA_ALG_INTEG:
677 integ = value.u16;
678 break;
679 case HA_ESN:
680 esn = value.u16;
681 break;
682 case HA_NONCE_I:
683 nonce_i = value.chunk;
684 break;
685 case HA_NONCE_R:
686 nonce_r = value.chunk;
687 break;
688 case HA_SECRET:
689 secret = value.chunk;
690 break;
691 default:
692 break;
693 }
694 }
695 enumerator->destroy(enumerator);
696
697 if (!ike_sa)
698 {
699 DBG1(DBG_CHD, "IKE_SA for HA CHILD_SA not found");
700 message->destroy(message);
701 return;
702 }
703 config = find_child_cfg(ike_sa, config_name);
704 if (!config)
705 {
706 DBG1(DBG_CHD, "HA is missing nodes child configuration");
707 charon->ike_sa_manager->checkin(charon->ike_sa_manager, ike_sa);
708 message->destroy(message);
709 return;
710 }
711
712 child_sa = child_sa_create(ike_sa->get_my_host(ike_sa),
713 ike_sa->get_other_host(ike_sa), config, 0,
714 ike_sa->has_condition(ike_sa, COND_NAT_ANY));
715 child_sa->set_mode(child_sa, mode);
716 child_sa->set_protocol(child_sa, PROTO_ESP);
717 child_sa->set_ipcomp(child_sa, ipcomp);
718
719 proposal = proposal_create(PROTO_ESP, 0);
720 if (integ)
721 {
722 proposal->add_algorithm(proposal, INTEGRITY_ALGORITHM, integ, 0);
723 }
724 if (encr)
725 {
726 proposal->add_algorithm(proposal, ENCRYPTION_ALGORITHM, encr, len);
727 }
728 proposal->add_algorithm(proposal, EXTENDED_SEQUENCE_NUMBERS, esn, 0);
729 if (secret.len)
730 {
731 dh = ha_diffie_hellman_create(secret, chunk_empty);
732 }
733 if (ike_sa->get_version(ike_sa) == IKEV2)
734 {
735 keymat_v2_t *keymat_v2 = (keymat_v2_t*)ike_sa->get_keymat(ike_sa);
736
737 ok = keymat_v2->derive_child_keys(keymat_v2, proposal, dh,
738 nonce_i, nonce_r, &encr_i, &integ_i, &encr_r, &integ_r);
739 }
740 if (ike_sa->get_version(ike_sa) == IKEV1)
741 {
742 keymat_v1_t *keymat_v1 = (keymat_v1_t*)ike_sa->get_keymat(ike_sa);
743 u_int32_t spi_i, spi_r;
744
745 spi_i = initiator ? inbound_spi : outbound_spi;
746 spi_r = initiator ? outbound_spi : inbound_spi;
747
748 ok = keymat_v1->derive_child_keys(keymat_v1, proposal, dh, spi_i, spi_r,
749 nonce_i, nonce_r, &encr_i, &integ_i, &encr_r, &integ_r);
750 }
751 DESTROY_IF(dh);
752 if (!ok)
753 {
754 DBG1(DBG_CHD, "HA CHILD_SA key derivation failed");
755 child_sa->destroy(child_sa);
756 proposal->destroy(proposal);
757 charon->ike_sa_manager->checkin(charon->ike_sa_manager, ike_sa);
758 return;
759 }
760 child_sa->set_proposal(child_sa, proposal);
761 child_sa->set_state(child_sa, CHILD_INSTALLING);
762 proposal->destroy(proposal);
763
764 /* TODO: Change CHILD_SA API to avoid cloning twice */
765 local_ts = linked_list_create();
766 remote_ts = linked_list_create();
767 enumerator = message->create_attribute_enumerator(message);
768 while (enumerator->enumerate(enumerator, &attribute, &value))
769 {
770 switch (attribute)
771 {
772 case HA_LOCAL_TS:
773 local_ts->insert_last(local_ts, value.ts->clone(value.ts));
774 break;
775 case HA_REMOTE_TS:
776 remote_ts->insert_last(remote_ts, value.ts->clone(value.ts));
777 break;
778 default:
779 break;
780 }
781 }
782 enumerator->destroy(enumerator);
783
784 if (initiator)
785 {
786 if (child_sa->install(child_sa, encr_r, integ_r, inbound_spi,
787 inbound_cpi, TRUE, TRUE, local_ts, remote_ts) != SUCCESS ||
788 child_sa->install(child_sa, encr_i, integ_i, outbound_spi,
789 outbound_cpi, FALSE, TRUE, local_ts, remote_ts) != SUCCESS)
790 {
791 failed = TRUE;
792 }
793 }
794 else
795 {
796 if (child_sa->install(child_sa, encr_i, integ_i, inbound_spi,
797 inbound_cpi, TRUE, TRUE, local_ts, remote_ts) != SUCCESS ||
798 child_sa->install(child_sa, encr_r, integ_r, outbound_spi,
799 outbound_cpi, FALSE, TRUE, local_ts, remote_ts) != SUCCESS)
800 {
801 failed = TRUE;
802 }
803 }
804 chunk_clear(&encr_i);
805 chunk_clear(&integ_i);
806 chunk_clear(&encr_r);
807 chunk_clear(&integ_r);
808
809 if (failed)
810 {
811 DBG1(DBG_CHD, "HA CHILD_SA installation failed");
812 child_sa->destroy(child_sa);
813 local_ts->destroy_offset(local_ts, offsetof(traffic_selector_t, destroy));
814 remote_ts->destroy_offset(remote_ts, offsetof(traffic_selector_t, destroy));
815 charon->ike_sa_manager->checkin(charon->ike_sa_manager, ike_sa);
816 message->destroy(message);
817 return;
818 }
819
820 seg_i = this->kernel->get_segment_spi(this->kernel,
821 ike_sa->get_my_host(ike_sa), inbound_spi);
822 seg_o = this->kernel->get_segment_spi(this->kernel,
823 ike_sa->get_other_host(ike_sa), outbound_spi);
824
825 DBG1(DBG_CFG, "installed HA CHILD_SA %s{%d} %#R=== %#R "
826 "(segment in: %d%s, out: %d%s)", child_sa->get_name(child_sa),
827 child_sa->get_reqid(child_sa), local_ts, remote_ts,
828 seg_i, this->segments->is_active(this->segments, seg_i) ? "*" : "",
829 seg_o, this->segments->is_active(this->segments, seg_o) ? "*" : "");
830 child_sa->add_policies(child_sa, local_ts, remote_ts);
831 local_ts->destroy_offset(local_ts, offsetof(traffic_selector_t, destroy));
832 remote_ts->destroy_offset(remote_ts, offsetof(traffic_selector_t, destroy));
833
834 child_sa->set_state(child_sa, CHILD_INSTALLED);
835 ike_sa->add_child_sa(ike_sa, child_sa);
836 message->destroy(message);
837 charon->ike_sa_manager->checkin(charon->ike_sa_manager, ike_sa);
838 }
839
840 /**
841 * Process messages of type CHILD_DELETE
842 */
843 static void process_child_delete(private_ha_dispatcher_t *this,
844 ha_message_t *message)
845 {
846 ha_message_attribute_t attribute;
847 ha_message_value_t value;
848 enumerator_t *enumerator;
849 ike_sa_t *ike_sa = NULL;
850 child_sa_t *child_sa;
851 u_int32_t spi = 0;
852
853 enumerator = message->create_attribute_enumerator(message);
854 while (enumerator->enumerate(enumerator, &attribute, &value))
855 {
856 switch (attribute)
857 {
858 case HA_IKE_ID:
859 ike_sa = charon->ike_sa_manager->checkout(charon->ike_sa_manager,
860 value.ike_sa_id);
861 break;
862 case HA_INBOUND_SPI:
863 spi = value.u32;
864 break;
865 default:
866 break;
867 }
868 }
869 enumerator->destroy(enumerator);
870
871 if (ike_sa)
872 {
873 child_sa = ike_sa->get_child_sa(ike_sa, PROTO_ESP, spi, TRUE);
874 if (child_sa)
875 {
876 ike_sa->destroy_child_sa(ike_sa, PROTO_ESP, spi);
877 }
878 charon->ike_sa_manager->checkin(charon->ike_sa_manager, ike_sa);
879 }
880 message->destroy(message);
881 }
882
883 /**
884 * Process messages of type SEGMENT_TAKE/DROP
885 */
886 static void process_segment(private_ha_dispatcher_t *this,
887 ha_message_t *message, bool take)
888 {
889 ha_message_attribute_t attribute;
890 ha_message_value_t value;
891 enumerator_t *enumerator;
892
893 enumerator = message->create_attribute_enumerator(message);
894 while (enumerator->enumerate(enumerator, &attribute, &value))
895 {
896 switch (attribute)
897 {
898 case HA_SEGMENT:
899 if (take)
900 {
901 DBG1(DBG_CFG, "remote node takes segment %d", value.u16);
902 this->segments->deactivate(this->segments, value.u16, FALSE);
903 }
904 else
905 {
906 DBG1(DBG_CFG, "remote node drops segment %d", value.u16);
907 this->segments->activate(this->segments, value.u16, FALSE);
908 }
909 break;
910 default:
911 break;
912 }
913 }
914 enumerator->destroy(enumerator);
915 message->destroy(message);
916 }
917
918 /**
919 * Process messages of type STATUS
920 */
921 static void process_status(private_ha_dispatcher_t *this,
922 ha_message_t *message)
923 {
924 ha_message_attribute_t attribute;
925 ha_message_value_t value;
926 enumerator_t *enumerator;
927 segment_mask_t mask = 0;
928
929 enumerator = message->create_attribute_enumerator(message);
930 while (enumerator->enumerate(enumerator, &attribute, &value))
931 {
932 switch (attribute)
933 {
934 case HA_SEGMENT:
935 mask |= SEGMENTS_BIT(value.u16);
936 break;
937 default:
938 break;
939 }
940 }
941 enumerator->destroy(enumerator);
942
943 this->segments->handle_status(this->segments, mask);
944 message->destroy(message);
945 }
946
947 /**
948 * Process messages of type RESYNC
949 */
950 static void process_resync(private_ha_dispatcher_t *this,
951 ha_message_t *message)
952 {
953 ha_message_attribute_t attribute;
954 ha_message_value_t value;
955 enumerator_t *enumerator;
956
957 enumerator = message->create_attribute_enumerator(message);
958 while (enumerator->enumerate(enumerator, &attribute, &value))
959 {
960 switch (attribute)
961 {
962 case HA_SEGMENT:
963 this->cache->resync(this->cache, value.u16);
964 break;
965 default:
966 break;
967 }
968 }
969 enumerator->destroy(enumerator);
970 message->destroy(message);
971 }
972
973 /**
974 * Dispatcher job function
975 */
976 static job_requeue_t dispatch(private_ha_dispatcher_t *this)
977 {
978 ha_message_t *message;
979 ha_message_type_t type;
980
981 message = this->socket->pull(this->socket);
982 type = message->get_type(message);
983 if (type != HA_STATUS)
984 {
985 DBG2(DBG_CFG, "received HA %N message", ha_message_type_names,
986 message->get_type(message));
987 }
988 switch (type)
989 {
990 case HA_IKE_ADD:
991 process_ike_add(this, message);
992 break;
993 case HA_IKE_UPDATE:
994 process_ike_update(this, message);
995 break;
996 case HA_IKE_MID_INITIATOR:
997 process_ike_mid(this, message, TRUE);
998 break;
999 case HA_IKE_MID_RESPONDER:
1000 process_ike_mid(this, message, FALSE);
1001 break;
1002 case HA_IKE_IV:
1003 process_ike_iv(this, message);
1004 break;
1005 case HA_IKE_DELETE:
1006 process_ike_delete(this, message);
1007 break;
1008 case HA_CHILD_ADD:
1009 process_child_add(this, message);
1010 break;
1011 case HA_CHILD_DELETE:
1012 process_child_delete(this, message);
1013 break;
1014 case HA_SEGMENT_DROP:
1015 process_segment(this, message, FALSE);
1016 break;
1017 case HA_SEGMENT_TAKE:
1018 process_segment(this, message, TRUE);
1019 break;
1020 case HA_STATUS:
1021 process_status(this, message);
1022 break;
1023 case HA_RESYNC:
1024 process_resync(this, message);
1025 break;
1026 default:
1027 DBG1(DBG_CFG, "received unknown HA message type %d", type);
1028 message->destroy(message);
1029 break;
1030 }
1031 return JOB_REQUEUE_DIRECT;
1032 }
1033
1034 METHOD(ha_dispatcher_t, destroy, void,
1035 private_ha_dispatcher_t *this)
1036 {
1037 free(this);
1038 }
1039
1040 /**
1041 * See header
1042 */
1043 ha_dispatcher_t *ha_dispatcher_create(ha_socket_t *socket,
1044 ha_segments_t *segments, ha_cache_t *cache,
1045 ha_kernel_t *kernel, ha_attribute_t *attr)
1046 {
1047 private_ha_dispatcher_t *this;
1048
1049
1050 INIT(this,
1051 .public = {
1052 .destroy = _destroy,
1053 },
1054 .socket = socket,
1055 .segments = segments,
1056 .cache = cache,
1057 .kernel = kernel,
1058 .attr = attr,
1059 );
1060 lib->processor->queue_job(lib->processor,
1061 (job_t*)callback_job_create_with_prio((callback_job_cb_t)dispatch, this,
1062 NULL, (callback_job_cancel_t)return_false, JOB_PRIO_CRITICAL));
1063
1064 return &this->public;
1065 }
1066