88160fe4f5eb391a4872015bbccc4edfb16483f6
[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 ike_sa->inherit_pre(ike_sa, old_sa);
249 ike_sa->inherit_post(ike_sa, old_sa);
250 charon->ike_sa_manager->checkin_and_destroy(
251 charon->ike_sa_manager, old_sa);
252 old_sa = NULL;
253 }
254 ike_sa->set_state(ike_sa, IKE_CONNECTING);
255 ike_sa->set_proposal(ike_sa, proposal);
256 this->cache->cache(this->cache, ike_sa, message);
257 message = NULL;
258 charon->ike_sa_manager->checkin(charon->ike_sa_manager, ike_sa);
259 }
260 else
261 {
262 DBG1(DBG_IKE, "HA keymat derivation failed");
263 ike_sa->destroy(ike_sa);
264 }
265 charon->bus->set_sa(charon->bus, NULL);
266 proposal->destroy(proposal);
267 }
268 if (old_sa)
269 {
270 charon->ike_sa_manager->checkin(charon->ike_sa_manager, old_sa);
271 }
272 DESTROY_IF(message);
273 }
274
275 /**
276 * Apply a condition flag to the IKE_SA if it is in set
277 */
278 static void set_condition(ike_sa_t *ike_sa, ike_condition_t set,
279 ike_condition_t flag)
280 {
281 ike_sa->set_condition(ike_sa, flag, flag & set);
282 }
283
284 /**
285 * Apply a extension flag to the IKE_SA if it is in set
286 */
287 static void set_extension(ike_sa_t *ike_sa, ike_extension_t set,
288 ike_extension_t flag)
289 {
290 if (flag & set)
291 {
292 ike_sa->enable_extension(ike_sa, flag);
293 }
294 }
295
296 /**
297 * Process messages of type IKE_UPDATE
298 */
299 static void process_ike_update(private_ha_dispatcher_t *this,
300 ha_message_t *message)
301 {
302 ha_message_attribute_t attribute;
303 ha_message_value_t value;
304 enumerator_t *enumerator;
305 ike_sa_t *ike_sa = NULL;
306 peer_cfg_t *peer_cfg = NULL;
307 auth_cfg_t *auth;
308 bool received_vip = FALSE, first_local_vip = TRUE, first_peer_addr = TRUE;
309
310 enumerator = message->create_attribute_enumerator(message);
311 while (enumerator->enumerate(enumerator, &attribute, &value))
312 {
313 if (attribute != HA_IKE_ID && ike_sa == NULL)
314 {
315 /* must be first attribute */
316 break;
317 }
318 switch (attribute)
319 {
320 case HA_IKE_ID:
321 ike_sa = charon->ike_sa_manager->checkout(charon->ike_sa_manager,
322 value.ike_sa_id);
323 break;
324 case HA_LOCAL_ID:
325 ike_sa->set_my_id(ike_sa, value.id->clone(value.id));
326 break;
327 case HA_REMOTE_ID:
328 ike_sa->set_other_id(ike_sa, value.id->clone(value.id));
329 break;
330 case HA_REMOTE_EAP_ID:
331 auth = auth_cfg_create();
332 auth->add(auth, AUTH_RULE_EAP_IDENTITY, value.id->clone(value.id));
333 ike_sa->add_auth_cfg(ike_sa, FALSE, auth);
334 break;
335 case HA_LOCAL_ADDR:
336 ike_sa->set_my_host(ike_sa, value.host->clone(value.host));
337 break;
338 case HA_REMOTE_ADDR:
339 ike_sa->set_other_host(ike_sa, value.host->clone(value.host));
340 break;
341 case HA_LOCAL_VIP:
342 if (first_local_vip)
343 {
344 ike_sa->clear_virtual_ips(ike_sa, TRUE);
345 first_local_vip = FALSE;
346 }
347 ike_sa->add_virtual_ip(ike_sa, TRUE, value.host);
348 break;
349 case HA_REMOTE_VIP:
350 if (!received_vip)
351 {
352 ike_sa->clear_virtual_ips(ike_sa, FALSE);
353 }
354 ike_sa->add_virtual_ip(ike_sa, FALSE, value.host);
355 received_vip = TRUE;
356 break;
357 case HA_PEER_ADDR:
358 if (first_peer_addr)
359 {
360 ike_sa->clear_peer_addresses(ike_sa);
361 first_peer_addr = FALSE;
362 }
363 ike_sa->add_peer_address(ike_sa, value.host->clone(value.host));
364 break;
365 case HA_CONFIG_NAME:
366 peer_cfg = charon->backends->get_peer_cfg_by_name(
367 charon->backends, value.str);
368 if (peer_cfg)
369 {
370 ike_sa->set_peer_cfg(ike_sa, peer_cfg);
371 peer_cfg->destroy(peer_cfg);
372 }
373 else
374 {
375 DBG1(DBG_IKE, "HA is missing nodes peer configuration");
376 charon->ike_sa_manager->checkin_and_destroy(
377 charon->ike_sa_manager, ike_sa);
378 ike_sa = NULL;
379 }
380 break;
381 case HA_EXTENSIONS:
382 set_extension(ike_sa, value.u32, EXT_NATT);
383 set_extension(ike_sa, value.u32, EXT_MOBIKE);
384 set_extension(ike_sa, value.u32, EXT_HASH_AND_URL);
385 set_extension(ike_sa, value.u32, EXT_MULTIPLE_AUTH);
386 set_extension(ike_sa, value.u32, EXT_STRONGSWAN);
387 set_extension(ike_sa, value.u32, EXT_EAP_ONLY_AUTHENTICATION);
388 set_extension(ike_sa, value.u32, EXT_MS_WINDOWS);
389 set_extension(ike_sa, value.u32, EXT_XAUTH);
390 set_extension(ike_sa, value.u32, EXT_DPD);
391 break;
392 case HA_CONDITIONS:
393 set_condition(ike_sa, value.u32, COND_NAT_ANY);
394 set_condition(ike_sa, value.u32, COND_NAT_HERE);
395 set_condition(ike_sa, value.u32, COND_NAT_THERE);
396 set_condition(ike_sa, value.u32, COND_NAT_FAKE);
397 set_condition(ike_sa, value.u32, COND_EAP_AUTHENTICATED);
398 set_condition(ike_sa, value.u32, COND_CERTREQ_SEEN);
399 set_condition(ike_sa, value.u32, COND_ORIGINAL_INITIATOR);
400 set_condition(ike_sa, value.u32, COND_STALE);
401 set_condition(ike_sa, value.u32, COND_INIT_CONTACT_SEEN);
402 set_condition(ike_sa, value.u32, COND_XAUTH_AUTHENTICATED);
403 break;
404 default:
405 break;
406 }
407 }
408 enumerator->destroy(enumerator);
409
410 if (ike_sa)
411 {
412 if (ike_sa->get_state(ike_sa) == IKE_CONNECTING &&
413 ike_sa->get_peer_cfg(ike_sa))
414 {
415 DBG1(DBG_CFG, "installed HA passive IKE_SA '%s' %H[%Y]...%H[%Y]",
416 ike_sa->get_name(ike_sa),
417 ike_sa->get_my_host(ike_sa), ike_sa->get_my_id(ike_sa),
418 ike_sa->get_other_host(ike_sa), ike_sa->get_other_id(ike_sa));
419 ike_sa->set_state(ike_sa, IKE_PASSIVE);
420 }
421 if (received_vip)
422 {
423 enumerator_t *pools, *vips;
424 host_t *vip;
425 char *pool;
426
427 peer_cfg = ike_sa->get_peer_cfg(ike_sa);
428 if (peer_cfg)
429 {
430 pools = peer_cfg->create_pool_enumerator(peer_cfg);
431 while (pools->enumerate(pools, &pool))
432 {
433 vips = ike_sa->create_virtual_ip_enumerator(ike_sa, FALSE);
434 while (vips->enumerate(vips, &vip))
435 {
436 this->attr->reserve(this->attr, pool, vip);
437 }
438 vips->destroy(vips);
439 }
440 pools->destroy(pools);
441 }
442 }
443 #ifdef USE_IKEV1
444 if (ike_sa->get_version(ike_sa) == IKEV1)
445 {
446 lib->processor->queue_job(lib->processor, (job_t*)
447 adopt_children_job_create(ike_sa->get_id(ike_sa)));
448 }
449 #endif /* USE_IKEV1 */
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 0, 0);
726 child_sa->set_mode(child_sa, mode);
727 child_sa->set_protocol(child_sa, PROTO_ESP);
728 child_sa->set_ipcomp(child_sa, ipcomp);
729
730 proposal = proposal_create(PROTO_ESP, 0);
731 if (integ)
732 {
733 proposal->add_algorithm(proposal, INTEGRITY_ALGORITHM, integ, 0);
734 }
735 if (encr)
736 {
737 proposal->add_algorithm(proposal, ENCRYPTION_ALGORITHM, encr, len);
738 }
739 proposal->add_algorithm(proposal, EXTENDED_SEQUENCE_NUMBERS, esn, 0);
740 if (secret.len)
741 {
742 dh = ha_diffie_hellman_create(secret, chunk_empty);
743 }
744 if (ike_sa->get_version(ike_sa) == IKEV2)
745 {
746 keymat_v2_t *keymat_v2 = (keymat_v2_t*)ike_sa->get_keymat(ike_sa);
747
748 ok = keymat_v2->derive_child_keys(keymat_v2, proposal, dh,
749 nonce_i, nonce_r, &encr_i, &integ_i, &encr_r, &integ_r);
750 }
751 if (ike_sa->get_version(ike_sa) == IKEV1)
752 {
753 keymat_v1_t *keymat_v1 = (keymat_v1_t*)ike_sa->get_keymat(ike_sa);
754 u_int32_t spi_i, spi_r;
755
756 spi_i = initiator ? inbound_spi : outbound_spi;
757 spi_r = initiator ? outbound_spi : inbound_spi;
758
759 ok = keymat_v1->derive_child_keys(keymat_v1, proposal, dh, spi_i, spi_r,
760 nonce_i, nonce_r, &encr_i, &integ_i, &encr_r, &integ_r);
761 }
762 DESTROY_IF(dh);
763 if (!ok)
764 {
765 DBG1(DBG_CHD, "HA CHILD_SA key derivation failed");
766 child_sa->destroy(child_sa);
767 proposal->destroy(proposal);
768 charon->ike_sa_manager->checkin(charon->ike_sa_manager, ike_sa);
769 return;
770 }
771 child_sa->set_proposal(child_sa, proposal);
772 child_sa->set_state(child_sa, CHILD_INSTALLING);
773 proposal->destroy(proposal);
774
775 /* TODO: Change CHILD_SA API to avoid cloning twice */
776 local_ts = linked_list_create();
777 remote_ts = linked_list_create();
778 enumerator = message->create_attribute_enumerator(message);
779 while (enumerator->enumerate(enumerator, &attribute, &value))
780 {
781 switch (attribute)
782 {
783 case HA_LOCAL_TS:
784 local_ts->insert_last(local_ts, value.ts->clone(value.ts));
785 break;
786 case HA_REMOTE_TS:
787 remote_ts->insert_last(remote_ts, value.ts->clone(value.ts));
788 break;
789 default:
790 break;
791 }
792 }
793 enumerator->destroy(enumerator);
794
795 if (initiator)
796 {
797 if (child_sa->install(child_sa, encr_r, integ_r, inbound_spi,
798 inbound_cpi, initiator, TRUE, TRUE,
799 local_ts, remote_ts) != SUCCESS ||
800 child_sa->install(child_sa, encr_i, integ_i, outbound_spi,
801 outbound_cpi, initiator, FALSE, TRUE,
802 local_ts, remote_ts) != SUCCESS)
803 {
804 failed = TRUE;
805 }
806 }
807 else
808 {
809 if (child_sa->install(child_sa, encr_i, integ_i, inbound_spi,
810 inbound_cpi, initiator, TRUE, TRUE,
811 local_ts, remote_ts) != SUCCESS ||
812 child_sa->install(child_sa, encr_r, integ_r, outbound_spi,
813 outbound_cpi, initiator, FALSE, TRUE,
814 local_ts, remote_ts) != SUCCESS)
815 {
816 failed = TRUE;
817 }
818 }
819 chunk_clear(&encr_i);
820 chunk_clear(&integ_i);
821 chunk_clear(&encr_r);
822 chunk_clear(&integ_r);
823
824 if (failed)
825 {
826 DBG1(DBG_CHD, "HA CHILD_SA installation failed");
827 child_sa->destroy(child_sa);
828 local_ts->destroy_offset(local_ts, offsetof(traffic_selector_t, destroy));
829 remote_ts->destroy_offset(remote_ts, offsetof(traffic_selector_t, destroy));
830 charon->ike_sa_manager->checkin(charon->ike_sa_manager, ike_sa);
831 message->destroy(message);
832 return;
833 }
834
835 seg_i = this->kernel->get_segment_spi(this->kernel,
836 ike_sa->get_my_host(ike_sa), inbound_spi);
837 seg_o = this->kernel->get_segment_spi(this->kernel,
838 ike_sa->get_other_host(ike_sa), outbound_spi);
839
840 DBG1(DBG_CFG, "installed HA CHILD_SA %s{%d} %#R=== %#R "
841 "(segment in: %d%s, out: %d%s)", child_sa->get_name(child_sa),
842 child_sa->get_unique_id(child_sa), local_ts, remote_ts,
843 seg_i, this->segments->is_active(this->segments, seg_i) ? "*" : "",
844 seg_o, this->segments->is_active(this->segments, seg_o) ? "*" : "");
845 child_sa->add_policies(child_sa, local_ts, remote_ts);
846 local_ts->destroy_offset(local_ts, offsetof(traffic_selector_t, destroy));
847 remote_ts->destroy_offset(remote_ts, offsetof(traffic_selector_t, destroy));
848
849 child_sa->set_state(child_sa, CHILD_INSTALLED);
850 ike_sa->add_child_sa(ike_sa, child_sa);
851 message->destroy(message);
852 charon->ike_sa_manager->checkin(charon->ike_sa_manager, ike_sa);
853 }
854
855 /**
856 * Process messages of type CHILD_DELETE
857 */
858 static void process_child_delete(private_ha_dispatcher_t *this,
859 ha_message_t *message)
860 {
861 ha_message_attribute_t attribute;
862 ha_message_value_t value;
863 enumerator_t *enumerator;
864 ike_sa_t *ike_sa = NULL;
865 child_sa_t *child_sa;
866 u_int32_t spi = 0;
867
868 enumerator = message->create_attribute_enumerator(message);
869 while (enumerator->enumerate(enumerator, &attribute, &value))
870 {
871 switch (attribute)
872 {
873 case HA_IKE_ID:
874 ike_sa = charon->ike_sa_manager->checkout(charon->ike_sa_manager,
875 value.ike_sa_id);
876 break;
877 case HA_INBOUND_SPI:
878 spi = value.u32;
879 break;
880 default:
881 break;
882 }
883 }
884 enumerator->destroy(enumerator);
885
886 if (ike_sa)
887 {
888 child_sa = ike_sa->get_child_sa(ike_sa, PROTO_ESP, spi, TRUE);
889 if (child_sa)
890 {
891 ike_sa->destroy_child_sa(ike_sa, PROTO_ESP, spi);
892 }
893 charon->ike_sa_manager->checkin(charon->ike_sa_manager, ike_sa);
894 }
895 message->destroy(message);
896 }
897
898 /**
899 * Process messages of type SEGMENT_TAKE/DROP
900 */
901 static void process_segment(private_ha_dispatcher_t *this,
902 ha_message_t *message, bool take)
903 {
904 ha_message_attribute_t attribute;
905 ha_message_value_t value;
906 enumerator_t *enumerator;
907
908 enumerator = message->create_attribute_enumerator(message);
909 while (enumerator->enumerate(enumerator, &attribute, &value))
910 {
911 switch (attribute)
912 {
913 case HA_SEGMENT:
914 if (take)
915 {
916 DBG1(DBG_CFG, "remote node takes segment %d", value.u16);
917 this->segments->deactivate(this->segments, value.u16, FALSE);
918 }
919 else
920 {
921 DBG1(DBG_CFG, "remote node drops segment %d", value.u16);
922 this->segments->activate(this->segments, value.u16, FALSE);
923 }
924 break;
925 default:
926 break;
927 }
928 }
929 enumerator->destroy(enumerator);
930 message->destroy(message);
931 }
932
933 /**
934 * Process messages of type STATUS
935 */
936 static void process_status(private_ha_dispatcher_t *this,
937 ha_message_t *message)
938 {
939 ha_message_attribute_t attribute;
940 ha_message_value_t value;
941 enumerator_t *enumerator;
942 segment_mask_t mask = 0;
943
944 enumerator = message->create_attribute_enumerator(message);
945 while (enumerator->enumerate(enumerator, &attribute, &value))
946 {
947 switch (attribute)
948 {
949 case HA_SEGMENT:
950 mask |= SEGMENTS_BIT(value.u16);
951 break;
952 default:
953 break;
954 }
955 }
956 enumerator->destroy(enumerator);
957
958 this->segments->handle_status(this->segments, mask);
959 message->destroy(message);
960 }
961
962 /**
963 * Process messages of type RESYNC
964 */
965 static void process_resync(private_ha_dispatcher_t *this,
966 ha_message_t *message)
967 {
968 ha_message_attribute_t attribute;
969 ha_message_value_t value;
970 enumerator_t *enumerator;
971
972 enumerator = message->create_attribute_enumerator(message);
973 while (enumerator->enumerate(enumerator, &attribute, &value))
974 {
975 switch (attribute)
976 {
977 case HA_SEGMENT:
978 this->cache->resync(this->cache, value.u16);
979 break;
980 default:
981 break;
982 }
983 }
984 enumerator->destroy(enumerator);
985 message->destroy(message);
986 }
987
988 /**
989 * Dispatcher job function
990 */
991 static job_requeue_t dispatch(private_ha_dispatcher_t *this)
992 {
993 ha_message_t *message;
994 ha_message_type_t type;
995
996 message = this->socket->pull(this->socket);
997 type = message->get_type(message);
998 if (type != HA_STATUS)
999 {
1000 DBG2(DBG_CFG, "received HA %N message", ha_message_type_names,
1001 message->get_type(message));
1002 }
1003 switch (type)
1004 {
1005 case HA_IKE_ADD:
1006 process_ike_add(this, message);
1007 break;
1008 case HA_IKE_UPDATE:
1009 process_ike_update(this, message);
1010 break;
1011 case HA_IKE_MID_INITIATOR:
1012 process_ike_mid(this, message, TRUE);
1013 break;
1014 case HA_IKE_MID_RESPONDER:
1015 process_ike_mid(this, message, FALSE);
1016 break;
1017 case HA_IKE_IV:
1018 process_ike_iv(this, message);
1019 break;
1020 case HA_IKE_DELETE:
1021 process_ike_delete(this, message);
1022 break;
1023 case HA_CHILD_ADD:
1024 process_child_add(this, message);
1025 break;
1026 case HA_CHILD_DELETE:
1027 process_child_delete(this, message);
1028 break;
1029 case HA_SEGMENT_DROP:
1030 process_segment(this, message, FALSE);
1031 break;
1032 case HA_SEGMENT_TAKE:
1033 process_segment(this, message, TRUE);
1034 break;
1035 case HA_STATUS:
1036 process_status(this, message);
1037 break;
1038 case HA_RESYNC:
1039 process_resync(this, message);
1040 break;
1041 default:
1042 DBG1(DBG_CFG, "received unknown HA message type %d", type);
1043 message->destroy(message);
1044 break;
1045 }
1046 return JOB_REQUEUE_DIRECT;
1047 }
1048
1049 METHOD(ha_dispatcher_t, destroy, void,
1050 private_ha_dispatcher_t *this)
1051 {
1052 free(this);
1053 }
1054
1055 /**
1056 * See header
1057 */
1058 ha_dispatcher_t *ha_dispatcher_create(ha_socket_t *socket,
1059 ha_segments_t *segments, ha_cache_t *cache,
1060 ha_kernel_t *kernel, ha_attribute_t *attr)
1061 {
1062 private_ha_dispatcher_t *this;
1063
1064
1065 INIT(this,
1066 .public = {
1067 .destroy = _destroy,
1068 },
1069 .socket = socket,
1070 .segments = segments,
1071 .cache = cache,
1072 .kernel = kernel,
1073 .attr = attr,
1074 );
1075 lib->processor->queue_job(lib->processor,
1076 (job_t*)callback_job_create_with_prio((callback_job_cb_t)dispatch, this,
1077 NULL, (callback_job_cancel_t)return_false, JOB_PRIO_CRITICAL));
1078
1079 return &this->public;
1080 }