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