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