added HA resync option to (re-)integrate nodes to a cluster
[strongswan.git] / src / charon / plugins / ha_sync / ha_sync_child.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 * $Id$
16 */
17
18 #include "ha_sync_child.h"
19
20 typedef struct private_ha_sync_child_t private_ha_sync_child_t;
21
22 /**
23 * Private data of an ha_sync_child_t object.
24 */
25 struct private_ha_sync_child_t {
26
27 /**
28 * Public ha_sync_child_t interface.
29 */
30 ha_sync_child_t public;
31
32 /**
33 * socket we use for syncing
34 */
35 ha_sync_socket_t *socket;
36 };
37
38 /**
39 * Implementation of listener_t.child_keys
40 */
41 static bool child_keys(private_ha_sync_child_t *this, ike_sa_t *ike_sa,
42 child_sa_t *child_sa, diffie_hellman_t *dh,
43 chunk_t nonce_i, chunk_t nonce_r)
44 {
45 ha_sync_message_t *m;
46 chunk_t secret;
47 proposal_t *proposal;
48 u_int16_t alg, len;
49 linked_list_t *list;
50 enumerator_t *enumerator;
51 traffic_selector_t *ts;
52
53 m = ha_sync_message_create(HA_SYNC_CHILD_ADD);
54
55 m->add_attribute(m, HA_SYNC_IKE_ID, ike_sa->get_id(ike_sa));
56 m->add_attribute(m, HA_SYNC_INBOUND_SPI, child_sa->get_spi(child_sa, TRUE));
57 m->add_attribute(m, HA_SYNC_OUTBOUND_SPI, child_sa->get_spi(child_sa, FALSE));
58 m->add_attribute(m, HA_SYNC_INBOUND_CPI, child_sa->get_cpi(child_sa, TRUE));
59 m->add_attribute(m, HA_SYNC_OUTBOUND_CPI, child_sa->get_cpi(child_sa, FALSE));
60 m->add_attribute(m, HA_SYNC_IPSEC_MODE, child_sa->get_mode(child_sa));
61 m->add_attribute(m, HA_SYNC_IPCOMP, child_sa->get_ipcomp(child_sa));
62 m->add_attribute(m, HA_SYNC_CONFIG_NAME, child_sa->get_name(child_sa));
63
64 proposal = child_sa->get_proposal(child_sa);
65 if (proposal->get_algorithm(proposal, ENCRYPTION_ALGORITHM, &alg, &len))
66 {
67 m->add_attribute(m, HA_SYNC_ALG_ENCR, alg);
68 if (len)
69 {
70 m->add_attribute(m, HA_SYNC_ALG_ENCR_LEN, len);
71 }
72 }
73 if (proposal->get_algorithm(proposal, INTEGRITY_ALGORITHM, &alg, NULL))
74 {
75 m->add_attribute(m, HA_SYNC_ALG_INTEG, alg);
76 }
77 m->add_attribute(m, HA_SYNC_NONCE_I, nonce_i);
78 m->add_attribute(m, HA_SYNC_NONCE_R, nonce_r);
79 if (dh && dh->get_shared_secret(dh, &secret) == SUCCESS)
80 {
81 m->add_attribute(m, HA_SYNC_SECRET, secret);
82 chunk_clear(&secret);
83 }
84
85 list = child_sa->get_traffic_selectors(child_sa, TRUE);
86 enumerator = list->create_enumerator(list);
87 while (enumerator->enumerate(enumerator, &ts))
88 {
89 m->add_attribute(m, HA_SYNC_LOCAL_TS, ts);
90 }
91 enumerator->destroy(enumerator);
92 list = child_sa->get_traffic_selectors(child_sa, FALSE);
93 enumerator = list->create_enumerator(list);
94 while (enumerator->enumerate(enumerator, &ts))
95 {
96 m->add_attribute(m, HA_SYNC_REMOTE_TS, ts);
97 }
98 enumerator->destroy(enumerator);
99
100 this->socket->push(this->socket, m);
101 m->destroy(m);
102
103 return TRUE;
104 }
105
106 /**
107 * Implementation of listener_t.child_state_change
108 */
109 static bool child_state_change(private_ha_sync_child_t *this, ike_sa_t *ike_sa,
110 child_sa_t *child_sa, child_sa_state_t state)
111 {
112 if (ike_sa->get_state(ike_sa) == IKE_PASSIVE)
113 { /* only sync active IKE_SAs */
114 return TRUE;
115 }
116
117 if (state == CHILD_DESTROYING)
118 {
119 ha_sync_message_t *m;
120
121 m = ha_sync_message_create(HA_SYNC_CHILD_DELETE);
122
123 m->add_attribute(m, HA_SYNC_IKE_ID, ike_sa->get_id(ike_sa));
124 m->add_attribute(m, HA_SYNC_INBOUND_SPI,
125 child_sa->get_spi(child_sa, TRUE));
126 this->socket->push(this->socket, m);
127 m->destroy(m);
128 }
129 return TRUE;
130 }
131
132 /**
133 * Implementation of ha_sync_child_t.destroy.
134 */
135 static void destroy(private_ha_sync_child_t *this)
136 {
137 free(this);
138 }
139
140 /**
141 * See header
142 */
143 ha_sync_child_t *ha_sync_child_create(ha_sync_socket_t *socket)
144 {
145 private_ha_sync_child_t *this = malloc_thing(private_ha_sync_child_t);
146
147 memset(&this->public.listener, 0, sizeof(listener_t));
148 this->public.listener.child_keys = (bool(*)(listener_t*, ike_sa_t *ike_sa, child_sa_t *child_sa, diffie_hellman_t *dh, chunk_t nonce_i, chunk_t nonce_r))child_keys;
149 this->public.listener.child_state_change = (bool(*)(listener_t*,ike_sa_t *ike_sa, child_sa_t *child_sa, child_sa_state_t state))child_state_change;
150 this->public.destroy = (void(*)(ha_sync_child_t*))destroy;
151
152 this->socket = socket;
153
154 return &this->public;
155 }
156