2 * Copyright (C) 2008 Thomas Kallenberg
3 * Copyright (C) 2008 Tobias Brunner
4 * Copyright (C) 2008 Martin Willi
5 * Hochschule fuer Technik Rapperswil
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 #include "uci_config.h"
22 #include "uci_parser.h"
26 typedef struct private_uci_config_t private_uci_config_t
;
29 * Private data of an uci_config_t object
31 struct private_uci_config_t
{
45 * enumerator implementation for create_peer_cfg_enumerator
48 /** implements enumerator */
50 /** currently enumerated peer config */
52 /** inner uci_parser section enumerator */
57 * create a proposal from a string, with fallback to default
59 static proposal_t
*create_proposal(char *string
, protocol_id_t proto
)
61 proposal_t
*proposal
= NULL
;
65 proposal
= proposal_create_from_string(proto
, string
);
68 { /* UCI default is aes/sha1 only */
69 if (proto
== PROTO_IKE
)
71 proposal
= proposal_create_from_string(proto
,
72 "aes128-aes192-aes256-sha1-modp1536-modp2048");
76 proposal
= proposal_create_from_string(proto
,
77 "aes128-aes192-aes256-sha1");
84 * create an traffic selector, fallback to dynamic
86 static traffic_selector_t
*create_ts(char *string
)
94 string
= strdupa(string
);
95 pos
= strchr(string
, '/');
103 if (strchr(string
, ':'))
108 net
= host_create_from_string(string
, 0);
111 return traffic_selector_create_from_subnet(net
, netbits
, 0, 0);
114 return traffic_selector_create_dynamic(0, 0, 65535);
118 * create a rekey time from a string with hours, with fallback
120 static u_int
create_rekey(char *string
)
126 rekey
= atoi(string
);
137 * Implementation of peer_enumerator_t.public.enumerate
139 static bool peer_enumerator_enumerate(peer_enumerator_t
*this, peer_cfg_t
**cfg
)
141 char *name
, *ike_proposal
, *esp_proposal
, *ike_rekey
, *esp_rekey
;
142 char *local_id
, *local_addr
, *local_net
;
143 char *remote_id
, *remote_addr
, *remote_net
;
144 child_cfg_t
*child_cfg
;
147 lifetime_cfg_t lifetime
= {
149 .life
= create_rekey(esp_rekey
) + 300,
150 .rekey
= create_rekey(esp_rekey
),
159 local_addr
= "0.0.0.0";
160 remote_addr
= "0.0.0.0";
168 if (this->inner
->enumerate(this->inner
, &name
, &local_id
, &remote_id
,
169 &local_addr
, &remote_addr
, &local_net
, &remote_net
,
170 &ike_proposal
, &esp_proposal
, &ike_rekey
, &esp_rekey
))
172 DESTROY_IF(this->peer_cfg
);
173 ike_cfg
= ike_cfg_create(FALSE
, FALSE
,
174 local_addr
, IKEV2_UDP_PORT
, remote_addr
, IKEV2_UDP_PORT
);
175 ike_cfg
->add_proposal(ike_cfg
, create_proposal(ike_proposal
, PROTO_IKE
));
176 this->peer_cfg
= peer_cfg_create(
177 name
, 2, ike_cfg
, CERT_SEND_IF_ASKED
, UNIQUE_NO
,
178 1, create_rekey(ike_rekey
), 0, /* keytries, rekey, reauth */
179 1800, 900, /* jitter, overtime */
180 TRUE
, 60, /* mobike, dpddelay */
181 NULL
, NULL
, /* vip, pool */
182 FALSE
, NULL
, NULL
); /* mediation, med by, peer id */
183 auth
= auth_cfg_create();
184 auth
->add(auth
, AUTH_RULE_AUTH_CLASS
, AUTH_CLASS_PSK
);
185 auth
->add(auth
, AUTH_RULE_IDENTITY
,
186 identification_create_from_string(local_id
));
187 this->peer_cfg
->add_auth_cfg(this->peer_cfg
, auth
, TRUE
);
189 auth
= auth_cfg_create();
190 auth
->add(auth
, AUTH_RULE_AUTH_CLASS
, AUTH_CLASS_PSK
);
193 auth
->add(auth
, AUTH_RULE_IDENTITY
,
194 identification_create_from_string(remote_id
));
196 this->peer_cfg
->add_auth_cfg(this->peer_cfg
, auth
, FALSE
);
198 child_cfg
= child_cfg_create(name
, &lifetime
, NULL
, TRUE
, MODE_TUNNEL
,
199 ACTION_NONE
, ACTION_NONE
, FALSE
, 0, 0,
201 child_cfg
->add_proposal(child_cfg
, create_proposal(esp_proposal
, PROTO_ESP
));
202 child_cfg
->add_traffic_selector(child_cfg
, TRUE
, create_ts(local_net
));
203 child_cfg
->add_traffic_selector(child_cfg
, FALSE
, create_ts(remote_net
));
204 this->peer_cfg
->add_child_cfg(this->peer_cfg
, child_cfg
);
205 *cfg
= this->peer_cfg
;
212 * Implementation of peer_enumerator_t.public.destroy
214 static void peer_enumerator_destroy(peer_enumerator_t
*this)
216 DESTROY_IF(this->peer_cfg
);
217 this->inner
->destroy(this->inner
);
222 * Implementation of backend_t.create_peer_cfg_enumerator.
224 static enumerator_t
* create_peer_cfg_enumerator(private_uci_config_t
*this,
225 identification_t
*me
,
226 identification_t
*other
)
228 peer_enumerator_t
*e
= malloc_thing(peer_enumerator_t
);
230 e
->public.enumerate
= (void*)peer_enumerator_enumerate
;
231 e
->public.destroy
= (void*)peer_enumerator_destroy
;
233 e
->inner
= this->parser
->create_section_enumerator(this->parser
,
234 "local_id", "remote_id", "local_addr", "remote_addr",
235 "local_net", "remote_net", "ike_proposal", "esp_proposal",
236 "ike_rekey", "esp_rekey", NULL
);
246 * enumerator implementation for create_ike_cfg_enumerator
249 /** implements enumerator */
251 /** currently enumerated ike config */
253 /** inner uci_parser section enumerator */
258 * Implementation of peer_enumerator_t.public.enumerate
260 static bool ike_enumerator_enumerate(ike_enumerator_t
*this, ike_cfg_t
**cfg
)
262 char *local_addr
, *remote_addr
, *ike_proposal
;
265 local_addr
= "0.0.0.0";
266 remote_addr
= "0.0.0.0";
269 if (this->inner
->enumerate(this->inner
, NULL
,
270 &local_addr
, &remote_addr
, &ike_proposal
))
272 DESTROY_IF(this->ike_cfg
);
273 this->ike_cfg
= ike_cfg_create(FALSE
, FALSE
, local_addr
, IKEV2_UDP_PORT
,
274 remote_addr
, IKEV2_UDP_PORT
);
275 this->ike_cfg
->add_proposal(this->ike_cfg
,
276 create_proposal(ike_proposal
, PROTO_IKE
));
278 *cfg
= this->ike_cfg
;
285 * Implementation of ike_enumerator_t.public.destroy
287 static void ike_enumerator_destroy(ike_enumerator_t
*this)
289 DESTROY_IF(this->ike_cfg
);
290 this->inner
->destroy(this->inner
);
295 * Implementation of backend_t.create_ike_cfg_enumerator.
297 static enumerator_t
* create_ike_cfg_enumerator(private_uci_config_t
*this,
298 host_t
*me
, host_t
*other
)
300 ike_enumerator_t
*e
= malloc_thing(ike_enumerator_t
);
302 e
->public.enumerate
= (void*)ike_enumerator_enumerate
;
303 e
->public.destroy
= (void*)ike_enumerator_destroy
;
305 e
->inner
= this->parser
->create_section_enumerator(this->parser
,
306 "local_addr", "remote_addr", "ike_proposal", NULL
);
316 * implements backend_t.get_peer_cfg_by_name.
318 static peer_cfg_t
*get_peer_cfg_by_name(private_uci_config_t
*this, char *name
)
320 enumerator_t
*enumerator
;
321 peer_cfg_t
*current
, *found
= NULL
;
323 enumerator
= create_peer_cfg_enumerator(this, NULL
, NULL
);
326 while (enumerator
->enumerate(enumerator
, ¤t
))
328 if (streq(name
, current
->get_name(current
)))
330 found
= current
->get_ref(current
);
334 enumerator
->destroy(enumerator
);
340 * Implementation of uci_config_t.destroy.
342 static void destroy(private_uci_config_t
*this)
348 * Described in header.
350 uci_config_t
*uci_config_create(uci_parser_t
*parser
)
352 private_uci_config_t
*this = malloc_thing(private_uci_config_t
);
354 this->public.backend
.create_peer_cfg_enumerator
= (enumerator_t
*(*)(backend_t
*, identification_t
*me
, identification_t
*other
))create_peer_cfg_enumerator
;
355 this->public.backend
.create_ike_cfg_enumerator
= (enumerator_t
*(*)(backend_t
*, host_t
*me
, host_t
*other
))create_ike_cfg_enumerator
;
356 this->public.backend
.get_peer_cfg_by_name
= (peer_cfg_t
* (*)(backend_t
*,char*))get_peer_cfg_by_name
;
357 this->public.destroy
= (void(*)(uci_config_t
*))destroy
;
358 this->parser
= parser
;
360 return &this->public;