Clear virtual IPs before storing assigned ones on the IKE_SA
[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_local_vip = TRUE, 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 if (first_local_vip)
348 {
349 ike_sa->clear_virtual_ips(ike_sa, TRUE);
350 first_local_vip = FALSE;
351 }
352 ike_sa->add_virtual_ip(ike_sa, TRUE, value.host);
353 break;
354 case HA_REMOTE_VIP:
355 if (!received_vip)
356 {
357 ike_sa->clear_virtual_ips(ike_sa, FALSE);
358 }
359 ike_sa->add_virtual_ip(ike_sa, FALSE, value.host);
360 received_vip = TRUE;
361 break;
362 case HA_PEER_ADDR:
363 if (first_peer_addr)
364 {
365 ike_sa->clear_peer_addresses(ike_sa);
366 first_peer_addr = FALSE;
367 }
368 ike_sa->add_peer_address(ike_sa, value.host->clone(value.host));
369 break;
370 case HA_CONFIG_NAME:
371 peer_cfg = charon->backends->get_peer_cfg_by_name(
372 charon->backends, value.str);
373 if (peer_cfg)
374 {
375 ike_sa->set_peer_cfg(ike_sa, peer_cfg);
376 peer_cfg->destroy(peer_cfg);
377 }
378 else
379 {
380 DBG1(DBG_IKE, "HA is missing nodes peer configuration");
381 }
382 break;
383 case HA_EXTENSIONS:
384 set_extension(ike_sa, value.u32, EXT_NATT);
385 set_extension(ike_sa, value.u32, EXT_MOBIKE);
386 set_extension(ike_sa, value.u32, EXT_HASH_AND_URL);
387 set_extension(ike_sa, value.u32, EXT_MULTIPLE_AUTH);
388 set_extension(ike_sa, value.u32, EXT_STRONGSWAN);
389 set_extension(ike_sa, value.u32, EXT_EAP_ONLY_AUTHENTICATION);
390 set_extension(ike_sa, value.u32, EXT_MS_WINDOWS);
391 set_extension(ike_sa, value.u32, EXT_XAUTH);
392 set_extension(ike_sa, value.u32, EXT_DPD);
393 break;
394 case HA_CONDITIONS:
395 set_condition(ike_sa, value.u32, COND_NAT_ANY);
396 set_condition(ike_sa, value.u32, COND_NAT_HERE);
397 set_condition(ike_sa, value.u32, COND_NAT_THERE);
398 set_condition(ike_sa, value.u32, COND_NAT_FAKE);
399 set_condition(ike_sa, value.u32, COND_EAP_AUTHENTICATED);
400 set_condition(ike_sa, value.u32, COND_CERTREQ_SEEN);
401 set_condition(ike_sa, value.u32, COND_ORIGINAL_INITIATOR);
402 set_condition(ike_sa, value.u32, COND_STALE);
403 set_condition(ike_sa, value.u32, COND_INIT_CONTACT_SEEN);
404 set_condition(ike_sa, value.u32, COND_XAUTH_AUTHENTICATED);
405 break;
406 default:
407 break;
408 }
409 }
410 enumerator->destroy(enumerator);
411
412 if (ike_sa)
413 {
414 if (ike_sa->get_state(ike_sa) == IKE_CONNECTING &&
415 ike_sa->get_peer_cfg(ike_sa))
416 {
417 DBG1(DBG_CFG, "installed HA passive IKE_SA '%s' %H[%Y]...%H[%Y]",
418 ike_sa->get_name(ike_sa),
419 ike_sa->get_my_host(ike_sa), ike_sa->get_my_id(ike_sa),
420 ike_sa->get_other_host(ike_sa), ike_sa->get_other_id(ike_sa));
421 ike_sa->set_state(ike_sa, IKE_PASSIVE);
422 }
423 if (received_vip)
424 {
425 enumerator_t *pools, *vips;
426 host_t *vip;
427 char *pool;
428
429 peer_cfg = ike_sa->get_peer_cfg(ike_sa);
430 if (peer_cfg)
431 {
432 pools = peer_cfg->create_pool_enumerator(peer_cfg);
433 while (pools->enumerate(pools, &pool))
434 {
435 vips = ike_sa->create_virtual_ip_enumerator(ike_sa, FALSE);
436 while (vips->enumerate(vips, &vip))
437 {
438 this->attr->reserve(this->attr, pool, vip);
439 }
440 vips->destroy(vips);
441 }
442 pools->destroy(pools);
443 }
444 }
445 if (ike_sa->get_version(ike_sa) == IKEV1)
446 {
447 lib->processor->queue_job(lib->processor, (job_t*)
448 adopt_children_job_create(ike_sa->get_id(ike_sa)));
449 }
450 this->cache->cache(this->cache, ike_sa, message);
451 charon->ike_sa_manager->checkin(charon->ike_sa_manager, ike_sa);
452 }
453 else
454 {
455 DBG1(DBG_CFG, "passive HA IKE_SA to update not found");
456 message->destroy(message);
457 }
458 }
459
460 /**
461 * Process messages of type IKE_MID_INITIATOR/RESPONDER
462 */
463 static void process_ike_mid(private_ha_dispatcher_t *this,
464 ha_message_t *message, bool initiator)
465 {
466 ha_message_attribute_t attribute;
467 ha_message_value_t value;
468 enumerator_t *enumerator;
469 ike_sa_t *ike_sa = NULL;
470 u_int32_t mid = 0;
471
472 enumerator = message->create_attribute_enumerator(message);
473 while (enumerator->enumerate(enumerator, &attribute, &value))
474 {
475 switch (attribute)
476 {
477 case HA_IKE_ID:
478 ike_sa = charon->ike_sa_manager->checkout(charon->ike_sa_manager,
479 value.ike_sa_id);
480 break;
481 case HA_MID:
482 mid = value.u32;
483 break;
484 default:
485 break;
486 }
487 }
488 enumerator->destroy(enumerator);
489
490 if (ike_sa)
491 {
492 if (mid)
493 {
494 ike_sa->set_message_id(ike_sa, initiator, mid);
495 }
496 this->cache->cache(this->cache, ike_sa, message);
497 charon->ike_sa_manager->checkin(charon->ike_sa_manager, ike_sa);
498 }
499 else
500 {
501 message->destroy(message);
502 }
503 }
504
505 /**
506 * Process messages of type IKE_IV
507 */
508 static void process_ike_iv(private_ha_dispatcher_t *this, ha_message_t *message)
509 {
510 ha_message_attribute_t attribute;
511 ha_message_value_t value;
512 enumerator_t *enumerator;
513 ike_sa_t *ike_sa = NULL;
514 chunk_t iv = chunk_empty;
515
516 enumerator = message->create_attribute_enumerator(message);
517 while (enumerator->enumerate(enumerator, &attribute, &value))
518 {
519 switch (attribute)
520 {
521 case HA_IKE_ID:
522 ike_sa = charon->ike_sa_manager->checkout(charon->ike_sa_manager,
523 value.ike_sa_id);
524 break;
525 case HA_IV:
526 iv = value.chunk;
527 break;
528 default:
529 break;
530 }
531 }
532 enumerator->destroy(enumerator);
533
534 if (ike_sa)
535 {
536 if (ike_sa->get_version(ike_sa) == IKEV1)
537 {
538 if (iv.len)
539 {
540 keymat_v1_t *keymat;
541
542 keymat = (keymat_v1_t*)ike_sa->get_keymat(ike_sa);
543 if (keymat->update_iv(keymat, 0, iv))
544 {
545 keymat->confirm_iv(keymat, 0);
546 }
547 }
548 }
549 this->cache->cache(this->cache, ike_sa, message);
550 charon->ike_sa_manager->checkin(charon->ike_sa_manager, ike_sa);
551 }
552 else
553 {
554 message->destroy(message);
555 }
556 }
557
558 /**
559 * Process messages of type IKE_DELETE
560 */
561 static void process_ike_delete(private_ha_dispatcher_t *this,
562 ha_message_t *message)
563 {
564 ha_message_attribute_t attribute;
565 ha_message_value_t value;
566 enumerator_t *enumerator;
567 ike_sa_t *ike_sa = NULL;
568
569 enumerator = message->create_attribute_enumerator(message);
570 while (enumerator->enumerate(enumerator, &attribute, &value))
571 {
572 switch (attribute)
573 {
574 case HA_IKE_ID:
575 ike_sa = charon->ike_sa_manager->checkout(
576 charon->ike_sa_manager, value.ike_sa_id);
577 break;
578 default:
579 break;
580 }
581 }
582 enumerator->destroy(enumerator);
583 if (ike_sa)
584 {
585 this->cache->cache(this->cache, ike_sa, message);
586 charon->ike_sa_manager->checkin_and_destroy(
587 charon->ike_sa_manager, ike_sa);
588 }
589 else
590 {
591 message->destroy(message);
592 }
593 }
594
595 /**
596 * Lookup a child cfg from the peer cfg by name
597 */
598 static child_cfg_t* find_child_cfg(ike_sa_t *ike_sa, char *name)
599 {
600 peer_cfg_t *peer_cfg;
601 child_cfg_t *current, *found = NULL;
602 enumerator_t *enumerator;
603
604 peer_cfg = ike_sa->get_peer_cfg(ike_sa);
605 if (peer_cfg)
606 {
607 enumerator = peer_cfg->create_child_cfg_enumerator(peer_cfg);
608 while (enumerator->enumerate(enumerator, &current))
609 {
610 if (streq(current->get_name(current), name))
611 {
612 found = current;
613 break;
614 }
615 }
616 enumerator->destroy(enumerator);
617 }
618 return found;
619 }
620
621 /**
622 * Process messages of type CHILD_ADD
623 */
624 static void process_child_add(private_ha_dispatcher_t *this,
625 ha_message_t *message)
626 {
627 ha_message_attribute_t attribute;
628 ha_message_value_t value;
629 enumerator_t *enumerator;
630 ike_sa_t *ike_sa = NULL;
631 char *config_name = "";
632 child_cfg_t *config = NULL;
633 child_sa_t *child_sa;
634 proposal_t *proposal;
635 bool initiator = FALSE, failed = FALSE, ok = FALSE;
636 u_int32_t inbound_spi = 0, outbound_spi = 0;
637 u_int16_t inbound_cpi = 0, outbound_cpi = 0;
638 u_int8_t mode = MODE_TUNNEL, ipcomp = 0;
639 u_int16_t encr = ENCR_UNDEFINED, integ = AUTH_UNDEFINED, len = 0;
640 u_int16_t esn = NO_EXT_SEQ_NUMBERS;
641 u_int seg_i, seg_o;
642 chunk_t nonce_i = chunk_empty, nonce_r = chunk_empty, secret = chunk_empty;
643 chunk_t encr_i, integ_i, encr_r, integ_r;
644 linked_list_t *local_ts, *remote_ts;
645 diffie_hellman_t *dh = NULL;
646
647 enumerator = message->create_attribute_enumerator(message);
648 while (enumerator->enumerate(enumerator, &attribute, &value))
649 {
650 switch (attribute)
651 {
652 case HA_IKE_ID:
653 ike_sa = charon->ike_sa_manager->checkout(charon->ike_sa_manager,
654 value.ike_sa_id);
655 break;
656 case HA_CONFIG_NAME:
657 config_name = value.str;
658 break;
659 case HA_INITIATOR:
660 initiator = value.u8;
661 break;
662 case HA_INBOUND_SPI:
663 inbound_spi = value.u32;
664 break;
665 case HA_OUTBOUND_SPI:
666 outbound_spi = value.u32;
667 break;
668 case HA_INBOUND_CPI:
669 inbound_cpi = value.u32;
670 break;
671 case HA_OUTBOUND_CPI:
672 outbound_cpi = value.u32;
673 break;
674 case HA_IPSEC_MODE:
675 mode = value.u8;
676 break;
677 case HA_IPCOMP:
678 ipcomp = value.u8;
679 break;
680 case HA_ALG_ENCR:
681 encr = value.u16;
682 break;
683 case HA_ALG_ENCR_LEN:
684 len = value.u16;
685 break;
686 case HA_ALG_INTEG:
687 integ = value.u16;
688 break;
689 case HA_ESN:
690 esn = value.u16;
691 break;
692 case HA_NONCE_I:
693 nonce_i = value.chunk;
694 break;
695 case HA_NONCE_R:
696 nonce_r = value.chunk;
697 break;
698 case HA_SECRET:
699 secret = value.chunk;
700 break;
701 default:
702 break;
703 }
704 }
705 enumerator->destroy(enumerator);
706
707 if (!ike_sa)
708 {
709 DBG1(DBG_CHD, "IKE_SA for HA CHILD_SA not found");
710 message->destroy(message);
711 return;
712 }
713 config = find_child_cfg(ike_sa, config_name);
714 if (!config)
715 {
716 DBG1(DBG_CHD, "HA is missing nodes child configuration");
717 charon->ike_sa_manager->checkin(charon->ike_sa_manager, ike_sa);
718 message->destroy(message);
719 return;
720 }
721
722 child_sa = child_sa_create(ike_sa->get_my_host(ike_sa),
723 ike_sa->get_other_host(ike_sa), config, 0,
724 ike_sa->has_condition(ike_sa, COND_NAT_ANY));
725 child_sa->set_mode(child_sa, mode);
726 child_sa->set_protocol(child_sa, PROTO_ESP);
727 child_sa->set_ipcomp(child_sa, ipcomp);
728
729 proposal = proposal_create(PROTO_ESP, 0);
730 if (integ)
731 {
732 proposal->add_algorithm(proposal, INTEGRITY_ALGORITHM, integ, 0);
733 }
734 if (encr)
735 {
736 proposal->add_algorithm(proposal, ENCRYPTION_ALGORITHM, encr, len);
737 }
738 proposal->add_algorithm(proposal, EXTENDED_SEQUENCE_NUMBERS, esn, 0);
739 if (secret.len)
740 {
741 dh = ha_diffie_hellman_create(secret, chunk_empty);
742 }
743 if (ike_sa->get_version(ike_sa) == IKEV2)
744 {
745 keymat_v2_t *keymat_v2 = (keymat_v2_t*)ike_sa->get_keymat(ike_sa);
746
747 ok = keymat_v2->derive_child_keys(keymat_v2, proposal, dh,
748 nonce_i, nonce_r, &encr_i, &integ_i, &encr_r, &integ_r);
749 }
750 if (ike_sa->get_version(ike_sa) == IKEV1)
751 {
752 keymat_v1_t *keymat_v1 = (keymat_v1_t*)ike_sa->get_keymat(ike_sa);
753 u_int32_t spi_i, spi_r;
754
755 spi_i = initiator ? inbound_spi : outbound_spi;
756 spi_r = initiator ? outbound_spi : inbound_spi;
757
758 ok = keymat_v1->derive_child_keys(keymat_v1, proposal, dh, spi_i, spi_r,
759 nonce_i, nonce_r, &encr_i, &integ_i, &encr_r, &integ_r);
760 }
761 DESTROY_IF(dh);
762 if (!ok)
763 {
764 DBG1(DBG_CHD, "HA CHILD_SA key derivation failed");
765 child_sa->destroy(child_sa);
766 proposal->destroy(proposal);
767 charon->ike_sa_manager->checkin(charon->ike_sa_manager, ike_sa);
768 return;
769 }
770 child_sa->set_proposal(child_sa, proposal);
771 child_sa->set_state(child_sa, CHILD_INSTALLING);
772 proposal->destroy(proposal);
773
774 /* TODO: Change CHILD_SA API to avoid cloning twice */
775 local_ts = linked_list_create();
776 remote_ts = linked_list_create();
777 enumerator = message->create_attribute_enumerator(message);
778 while (enumerator->enumerate(enumerator, &attribute, &value))
779 {
780 switch (attribute)
781 {
782 case HA_LOCAL_TS:
783 local_ts->insert_last(local_ts, value.ts->clone(value.ts));
784 break;
785 case HA_REMOTE_TS:
786 remote_ts->insert_last(remote_ts, value.ts->clone(value.ts));
787 break;
788 default:
789 break;
790 }
791 }
792 enumerator->destroy(enumerator);
793
794 if (initiator)
795 {
796 if (child_sa->install(child_sa, encr_r, integ_r, inbound_spi,
797 inbound_cpi, TRUE, TRUE, local_ts, remote_ts) != SUCCESS ||
798 child_sa->install(child_sa, encr_i, integ_i, outbound_spi,
799 outbound_cpi, FALSE, TRUE, local_ts, remote_ts) != SUCCESS)
800 {
801 failed = TRUE;
802 }
803 }
804 else
805 {
806 if (child_sa->install(child_sa, encr_i, integ_i, inbound_spi,
807 inbound_cpi, TRUE, TRUE, local_ts, remote_ts) != SUCCESS ||
808 child_sa->install(child_sa, encr_r, integ_r, outbound_spi,
809 outbound_cpi, FALSE, TRUE, local_ts, remote_ts) != SUCCESS)
810 {
811 failed = TRUE;
812 }
813 }
814 chunk_clear(&encr_i);
815 chunk_clear(&integ_i);
816 chunk_clear(&encr_r);
817 chunk_clear(&integ_r);
818
819 if (failed)
820 {
821 DBG1(DBG_CHD, "HA CHILD_SA installation failed");
822 child_sa->destroy(child_sa);
823 local_ts->destroy_offset(local_ts, offsetof(traffic_selector_t, destroy));
824 remote_ts->destroy_offset(remote_ts, offsetof(traffic_selector_t, destroy));
825 charon->ike_sa_manager->checkin(charon->ike_sa_manager, ike_sa);
826 message->destroy(message);
827 return;
828 }
829
830 seg_i = this->kernel->get_segment_spi(this->kernel,
831 ike_sa->get_my_host(ike_sa), inbound_spi);
832 seg_o = this->kernel->get_segment_spi(this->kernel,
833 ike_sa->get_other_host(ike_sa), outbound_spi);
834
835 DBG1(DBG_CFG, "installed HA CHILD_SA %s{%d} %#R=== %#R "
836 "(segment in: %d%s, out: %d%s)", child_sa->get_name(child_sa),
837 child_sa->get_reqid(child_sa), local_ts, remote_ts,
838 seg_i, this->segments->is_active(this->segments, seg_i) ? "*" : "",
839 seg_o, this->segments->is_active(this->segments, seg_o) ? "*" : "");
840 child_sa->add_policies(child_sa, local_ts, remote_ts);
841 local_ts->destroy_offset(local_ts, offsetof(traffic_selector_t, destroy));
842 remote_ts->destroy_offset(remote_ts, offsetof(traffic_selector_t, destroy));
843
844 child_sa->set_state(child_sa, CHILD_INSTALLED);
845 ike_sa->add_child_sa(ike_sa, child_sa);
846 message->destroy(message);
847 charon->ike_sa_manager->checkin(charon->ike_sa_manager, ike_sa);
848 }
849
850 /**
851 * Process messages of type CHILD_DELETE
852 */
853 static void process_child_delete(private_ha_dispatcher_t *this,
854 ha_message_t *message)
855 {
856 ha_message_attribute_t attribute;
857 ha_message_value_t value;
858 enumerator_t *enumerator;
859 ike_sa_t *ike_sa = NULL;
860 child_sa_t *child_sa;
861 u_int32_t spi = 0;
862
863 enumerator = message->create_attribute_enumerator(message);
864 while (enumerator->enumerate(enumerator, &attribute, &value))
865 {
866 switch (attribute)
867 {
868 case HA_IKE_ID:
869 ike_sa = charon->ike_sa_manager->checkout(charon->ike_sa_manager,
870 value.ike_sa_id);
871 break;
872 case HA_INBOUND_SPI:
873 spi = value.u32;
874 break;
875 default:
876 break;
877 }
878 }
879 enumerator->destroy(enumerator);
880
881 if (ike_sa)
882 {
883 child_sa = ike_sa->get_child_sa(ike_sa, PROTO_ESP, spi, TRUE);
884 if (child_sa)
885 {
886 ike_sa->destroy_child_sa(ike_sa, PROTO_ESP, spi);
887 }
888 charon->ike_sa_manager->checkin(charon->ike_sa_manager, ike_sa);
889 }
890 message->destroy(message);
891 }
892
893 /**
894 * Process messages of type SEGMENT_TAKE/DROP
895 */
896 static void process_segment(private_ha_dispatcher_t *this,
897 ha_message_t *message, bool take)
898 {
899 ha_message_attribute_t attribute;
900 ha_message_value_t value;
901 enumerator_t *enumerator;
902
903 enumerator = message->create_attribute_enumerator(message);
904 while (enumerator->enumerate(enumerator, &attribute, &value))
905 {
906 switch (attribute)
907 {
908 case HA_SEGMENT:
909 if (take)
910 {
911 DBG1(DBG_CFG, "remote node takes segment %d", value.u16);
912 this->segments->deactivate(this->segments, value.u16, FALSE);
913 }
914 else
915 {
916 DBG1(DBG_CFG, "remote node drops segment %d", value.u16);
917 this->segments->activate(this->segments, value.u16, FALSE);
918 }
919 break;
920 default:
921 break;
922 }
923 }
924 enumerator->destroy(enumerator);
925 message->destroy(message);
926 }
927
928 /**
929 * Process messages of type STATUS
930 */
931 static void process_status(private_ha_dispatcher_t *this,
932 ha_message_t *message)
933 {
934 ha_message_attribute_t attribute;
935 ha_message_value_t value;
936 enumerator_t *enumerator;
937 segment_mask_t mask = 0;
938
939 enumerator = message->create_attribute_enumerator(message);
940 while (enumerator->enumerate(enumerator, &attribute, &value))
941 {
942 switch (attribute)
943 {
944 case HA_SEGMENT:
945 mask |= SEGMENTS_BIT(value.u16);
946 break;
947 default:
948 break;
949 }
950 }
951 enumerator->destroy(enumerator);
952
953 this->segments->handle_status(this->segments, mask);
954 message->destroy(message);
955 }
956
957 /**
958 * Process messages of type RESYNC
959 */
960 static void process_resync(private_ha_dispatcher_t *this,
961 ha_message_t *message)
962 {
963 ha_message_attribute_t attribute;
964 ha_message_value_t value;
965 enumerator_t *enumerator;
966
967 enumerator = message->create_attribute_enumerator(message);
968 while (enumerator->enumerate(enumerator, &attribute, &value))
969 {
970 switch (attribute)
971 {
972 case HA_SEGMENT:
973 this->cache->resync(this->cache, value.u16);
974 break;
975 default:
976 break;
977 }
978 }
979 enumerator->destroy(enumerator);
980 message->destroy(message);
981 }
982
983 /**
984 * Dispatcher job function
985 */
986 static job_requeue_t dispatch(private_ha_dispatcher_t *this)
987 {
988 ha_message_t *message;
989 ha_message_type_t type;
990
991 message = this->socket->pull(this->socket);
992 type = message->get_type(message);
993 if (type != HA_STATUS)
994 {
995 DBG2(DBG_CFG, "received HA %N message", ha_message_type_names,
996 message->get_type(message));
997 }
998 switch (type)
999 {
1000 case HA_IKE_ADD:
1001 process_ike_add(this, message);
1002 break;
1003 case HA_IKE_UPDATE:
1004 process_ike_update(this, message);
1005 break;
1006 case HA_IKE_MID_INITIATOR:
1007 process_ike_mid(this, message, TRUE);
1008 break;
1009 case HA_IKE_MID_RESPONDER:
1010 process_ike_mid(this, message, FALSE);
1011 break;
1012 case HA_IKE_IV:
1013 process_ike_iv(this, message);
1014 break;
1015 case HA_IKE_DELETE:
1016 process_ike_delete(this, message);
1017 break;
1018 case HA_CHILD_ADD:
1019 process_child_add(this, message);
1020 break;
1021 case HA_CHILD_DELETE:
1022 process_child_delete(this, message);
1023 break;
1024 case HA_SEGMENT_DROP:
1025 process_segment(this, message, FALSE);
1026 break;
1027 case HA_SEGMENT_TAKE:
1028 process_segment(this, message, TRUE);
1029 break;
1030 case HA_STATUS:
1031 process_status(this, message);
1032 break;
1033 case HA_RESYNC:
1034 process_resync(this, message);
1035 break;
1036 default:
1037 DBG1(DBG_CFG, "received unknown HA message type %d", type);
1038 message->destroy(message);
1039 break;
1040 }
1041 return JOB_REQUEUE_DIRECT;
1042 }
1043
1044 METHOD(ha_dispatcher_t, destroy, void,
1045 private_ha_dispatcher_t *this)
1046 {
1047 free(this);
1048 }
1049
1050 /**
1051 * See header
1052 */
1053 ha_dispatcher_t *ha_dispatcher_create(ha_socket_t *socket,
1054 ha_segments_t *segments, ha_cache_t *cache,
1055 ha_kernel_t *kernel, ha_attribute_t *attr)
1056 {
1057 private_ha_dispatcher_t *this;
1058
1059
1060 INIT(this,
1061 .public = {
1062 .destroy = _destroy,
1063 },
1064 .socket = socket,
1065 .segments = segments,
1066 .cache = cache,
1067 .kernel = kernel,
1068 .attr = attr,
1069 );
1070 lib->processor->queue_job(lib->processor,
1071 (job_t*)callback_job_create_with_prio((callback_job_cb_t)dispatch, this,
1072 NULL, (callback_job_cancel_t)return_false, JOB_PRIO_CRITICAL));
1073
1074 return &this->public;
1075 }
1076