2 * Copyright (C) 2008 Martin Willi
3 * Hochschule fuer Technik Rapperswil
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>.
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
16 #include "ha_sync_ike.h"
18 typedef struct private_ha_sync_ike_t private_ha_sync_ike_t
;
21 * Private data of an ha_sync_ike_t object.
23 struct private_ha_sync_ike_t
{
26 * Public ha_sync_ike_t interface.
31 * socket we use for syncing
33 ha_sync_socket_t
*socket
;
37 * Return condition if it is set on ike_sa
39 static ike_condition_t
copy_condition(ike_sa_t
*ike_sa
, ike_condition_t cond
)
41 if (ike_sa
->has_condition(ike_sa
, cond
))
49 * Return extension if it is supported by peers IKE_SA
51 static ike_extension_t
copy_extension(ike_sa_t
*ike_sa
, ike_extension_t ext
)
53 if (ike_sa
->supports_extension(ike_sa
, ext
))
61 * Implementation of listener_t.ike_keys
63 static bool ike_keys(private_ha_sync_ike_t
*this, ike_sa_t
*ike_sa
,
64 diffie_hellman_t
*dh
, chunk_t nonce_i
, chunk_t nonce_r
,
72 if (dh
->get_shared_secret(dh
, &secret
) != SUCCESS
)
77 m
= ha_sync_message_create(HA_SYNC_IKE_ADD
);
78 m
->add_attribute(m
, HA_SYNC_IKE_ID
, ike_sa
->get_id(ike_sa
));
85 keymat
= rekey
->get_keymat(rekey
);
86 m
->add_attribute(m
, HA_SYNC_IKE_REKEY_ID
, rekey
->get_id(rekey
));
87 m
->add_attribute(m
, HA_SYNC_ALG_OLD_PRF
, keymat
->get_skd(keymat
, &skd
));
88 m
->add_attribute(m
, HA_SYNC_OLD_SKD
, skd
);
91 proposal
= ike_sa
->get_proposal(ike_sa
);
92 if (proposal
->get_algorithm(proposal
, ENCRYPTION_ALGORITHM
, &alg
, &len
))
94 m
->add_attribute(m
, HA_SYNC_ALG_ENCR
, alg
);
97 m
->add_attribute(m
, HA_SYNC_ALG_ENCR_LEN
, len
);
100 if (proposal
->get_algorithm(proposal
, INTEGRITY_ALGORITHM
, &alg
, NULL
))
102 m
->add_attribute(m
, HA_SYNC_ALG_INTEG
, alg
);
104 if (proposal
->get_algorithm(proposal
, PSEUDO_RANDOM_FUNCTION
, &alg
, NULL
))
106 m
->add_attribute(m
, HA_SYNC_ALG_PRF
, alg
);
108 m
->add_attribute(m
, HA_SYNC_NONCE_I
, nonce_i
);
109 m
->add_attribute(m
, HA_SYNC_NONCE_R
, nonce_r
);
110 m
->add_attribute(m
, HA_SYNC_SECRET
, secret
);
111 chunk_clear(&secret
);
113 this->socket
->push(this->socket
, m
);
120 * Implementation of listener_t.ike_state_change
122 static bool ike_state_change(private_ha_sync_ike_t
*this, ike_sa_t
*ike_sa
,
123 ike_sa_state_t state
)
125 ha_sync_message_t
*m
;
127 if (ike_sa
->get_state(ike_sa
) == IKE_PASSIVE
)
128 { /* only sync active IKE_SAs */
134 case IKE_ESTABLISHED
:
136 iterator_t
*iterator
;
137 peer_cfg_t
*peer_cfg
;
138 u_int32_t extension
, condition
;
140 identification_t
*eap_id
;
143 peer_cfg
= ike_sa
->get_peer_cfg(ike_sa
);
145 condition
= copy_condition(ike_sa
, COND_NAT_ANY
)
146 | copy_condition(ike_sa
, COND_NAT_HERE
)
147 | copy_condition(ike_sa
, COND_NAT_THERE
)
148 | copy_condition(ike_sa
, COND_NAT_FAKE
)
149 | copy_condition(ike_sa
, COND_EAP_AUTHENTICATED
)
150 | copy_condition(ike_sa
, COND_CERTREQ_SEEN
)
151 | copy_condition(ike_sa
, COND_ORIGINAL_INITIATOR
);
153 extension
= copy_extension(ike_sa
, EXT_NATT
)
154 | copy_extension(ike_sa
, EXT_MOBIKE
)
155 | copy_extension(ike_sa
, EXT_HASH_AND_URL
);
157 eap_id
= ike_sa
->get_eap_identity(ike_sa
);
158 id
= ike_sa
->get_id(ike_sa
);
160 m
= ha_sync_message_create(HA_SYNC_IKE_UPDATE
);
161 m
->add_attribute(m
, HA_SYNC_IKE_ID
, id
);
162 m
->add_attribute(m
, HA_SYNC_LOCAL_ID
, ike_sa
->get_my_id(ike_sa
));
163 m
->add_attribute(m
, HA_SYNC_REMOTE_ID
, ike_sa
->get_other_id(ike_sa
));
164 m
->add_attribute(m
, HA_SYNC_LOCAL_ADDR
, ike_sa
->get_my_host(ike_sa
));
165 m
->add_attribute(m
, HA_SYNC_REMOTE_ADDR
, ike_sa
->get_other_host(ike_sa
));
166 m
->add_attribute(m
, HA_SYNC_CONDITIONS
, condition
);
167 m
->add_attribute(m
, HA_SYNC_EXTENSIONS
, extension
);
168 m
->add_attribute(m
, HA_SYNC_CONFIG_NAME
, peer_cfg
->get_name(peer_cfg
));
171 m
->add_attribute(m
, HA_SYNC_EAP_ID
, eap_id
);
173 iterator
= ike_sa
->create_additional_address_iterator(ike_sa
);
174 while (iterator
->iterate(iterator
, (void**)&addr
))
176 m
->add_attribute(m
, HA_SYNC_ADDITIONAL_ADDR
, addr
);
178 iterator
->destroy(iterator
);
183 m
= ha_sync_message_create(HA_SYNC_IKE_DELETE
);
184 m
->add_attribute(m
, HA_SYNC_IKE_ID
, ike_sa
->get_id(ike_sa
));
190 this->socket
->push(this->socket
, m
);
196 * Implementation of listener_t.message
198 static bool message_hook(private_ha_sync_ike_t
*this, ike_sa_t
*ike_sa
,
199 message_t
*message
, bool incoming
)
201 if (message
->get_exchange_type(message
) != IKE_SA_INIT
&&
202 message
->get_request(message
))
203 { /* we sync on requests, but skip it on IKE_SA_INIT */
204 ha_sync_message_t
*m
;
207 m
= ha_sync_message_create(HA_SYNC_IKE_UPDATE
);
208 m
->add_attribute(m
, HA_SYNC_IKE_ID
, ike_sa
->get_id(ike_sa
));
209 mid
= message
->get_message_id(message
) + 1;
212 m
->add_attribute(m
, HA_SYNC_RESPOND_MID
, mid
);
216 m
->add_attribute(m
, HA_SYNC_INITIATE_MID
, mid
);
218 this->socket
->push(this->socket
, m
);
221 if (ike_sa
->get_state(ike_sa
) == IKE_ESTABLISHED
&&
222 message
->get_exchange_type(message
) == IKE_AUTH
&&
223 !message
->get_request(message
))
224 { /* After IKE_SA has been established, sync peers virtual IP.
225 * We cannot sync it in the state_change hook, it is installed later.
226 * TODO: where to sync local VIP? */
227 ha_sync_message_t
*m
;
230 vip
= ike_sa
->get_virtual_ip(ike_sa
, FALSE
);
233 m
= ha_sync_message_create(HA_SYNC_IKE_UPDATE
);
234 m
->add_attribute(m
, HA_SYNC_IKE_ID
, ike_sa
->get_id(ike_sa
));
235 m
->add_attribute(m
, HA_SYNC_REMOTE_VIP
, vip
);
236 this->socket
->push(this->socket
, m
);
244 * Implementation of ha_sync_ike_t.destroy.
246 static void destroy(private_ha_sync_ike_t
*this)
254 ha_sync_ike_t
*ha_sync_ike_create(ha_sync_socket_t
*socket
)
256 private_ha_sync_ike_t
*this = malloc_thing(private_ha_sync_ike_t
);
258 memset(&this->public.listener
, 0, sizeof(listener_t
));
259 this->public.listener
.ike_keys
= (bool(*)(listener_t
*, ike_sa_t
*ike_sa
, diffie_hellman_t
*dh
,chunk_t nonce_i
, chunk_t nonce_r
, ike_sa_t
*rekey
))ike_keys
;
260 this->public.listener
.ike_state_change
= (bool(*)(listener_t
*,ike_sa_t
*ike_sa
, ike_sa_state_t state
))ike_state_change
;
261 this->public.listener
.message
= (bool(*)(listener_t
*, ike_sa_t
*, message_t
*,bool))message_hook
;
262 this->public.destroy
= (void(*)(ha_sync_ike_t
*))destroy
;
264 this->socket
= socket
;
266 return &this->public;