Accept NULL identities passed to peer config enumeration
[strongswan.git] / src / libcharon / config / child_cfg.c
1 /*
2 * Copyright (C) 2008-2009 Tobias Brunner
3 * Copyright (C) 2005-2007 Martin Willi
4 * Copyright (C) 2005 Jan Hutter
5 * Hochschule fuer Technik Rapperswil
6 *
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>.
11 *
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
15 * for more details.
16 */
17
18 #include "child_cfg.h"
19
20 #include <stdint.h>
21
22 #include <daemon.h>
23
24 ENUM(action_names, ACTION_NONE, ACTION_RESTART,
25 "clear",
26 "hold",
27 "restart",
28 );
29
30 typedef struct private_child_cfg_t private_child_cfg_t;
31
32 /**
33 * Private data of an child_cfg_t object
34 */
35 struct private_child_cfg_t {
36
37 /**
38 * Public part
39 */
40 child_cfg_t public;
41
42 /**
43 * Number of references hold by others to this child_cfg
44 */
45 refcount_t refcount;
46
47 /**
48 * Name of the child_cfg, used to query it
49 */
50 char *name;
51
52 /**
53 * list for all proposals
54 */
55 linked_list_t *proposals;
56
57 /**
58 * list for traffic selectors for my site
59 */
60 linked_list_t *my_ts;
61
62 /**
63 * list for traffic selectors for others site
64 */
65 linked_list_t *other_ts;
66
67 /**
68 * updown script
69 */
70 char *updown;
71
72 /**
73 * allow host access
74 */
75 bool hostaccess;
76
77 /**
78 * Mode to propose for a initiated CHILD: tunnel/transport
79 */
80 ipsec_mode_t mode;
81
82 /**
83 * action to take to start CHILD_SA
84 */
85 action_t start_action;
86
87 /**
88 * action to take on DPD
89 */
90 action_t dpd_action;
91
92 /**
93 * action to take on CHILD_SA close
94 */
95 action_t close_action;
96
97 /**
98 * CHILD_SA lifetime config
99 */
100 lifetime_cfg_t lifetime;
101
102 /**
103 * enable IPComp
104 */
105 bool use_ipcomp;
106
107 /**
108 * Inactivity timeout
109 */
110 u_int32_t inactivity;
111
112 /**
113 * Reqid to install CHILD_SA with
114 */
115 u_int32_t reqid;
116
117 /**
118 * Optional mark to install inbound CHILD_SA with
119 */
120 mark_t mark_in;
121
122 /**
123 * Optional mark to install outbound CHILD_SA with
124 */
125 mark_t mark_out;
126
127 /**
128 * Traffic Flow Confidentiality padding, if enabled
129 */
130 u_int32_t tfc;
131
132 /**
133 * set up IPsec transport SA in MIPv6 proxy mode
134 */
135 bool proxy_mode;
136
137 /**
138 * enable installation and removal of kernel IPsec policies
139 */
140 bool install_policy;
141 };
142
143 METHOD(child_cfg_t, get_name, char*,
144 private_child_cfg_t *this)
145 {
146 return this->name;
147 }
148
149 METHOD(child_cfg_t, add_proposal, void,
150 private_child_cfg_t *this, proposal_t *proposal)
151 {
152 this->proposals->insert_last(this->proposals, proposal);
153 }
154
155 METHOD(child_cfg_t, get_proposals, linked_list_t*,
156 private_child_cfg_t *this, bool strip_dh)
157 {
158 enumerator_t *enumerator;
159 proposal_t *current;
160 linked_list_t *proposals = linked_list_create();
161
162 enumerator = this->proposals->create_enumerator(this->proposals);
163 while (enumerator->enumerate(enumerator, &current))
164 {
165 current = current->clone(current);
166 if (strip_dh)
167 {
168 current->strip_dh(current);
169 }
170 proposals->insert_last(proposals, current);
171 }
172 enumerator->destroy(enumerator);
173
174 return proposals;
175 }
176
177 METHOD(child_cfg_t, select_proposal, proposal_t*,
178 private_child_cfg_t*this, linked_list_t *proposals, bool strip_dh,
179 bool private)
180 {
181 enumerator_t *stored_enum, *supplied_enum;
182 proposal_t *stored, *supplied, *selected = NULL;
183
184 stored_enum = this->proposals->create_enumerator(this->proposals);
185 supplied_enum = proposals->create_enumerator(proposals);
186
187 /* compare all stored proposals with all supplied. Stored ones are preferred. */
188 while (stored_enum->enumerate(stored_enum, &stored))
189 {
190 stored = stored->clone(stored);
191 while (supplied_enum->enumerate(supplied_enum, &supplied))
192 {
193 if (strip_dh)
194 {
195 stored->strip_dh(stored);
196 }
197 selected = stored->select(stored, supplied, private);
198 if (selected)
199 {
200 DBG2(DBG_CFG, "received proposals: %#P", proposals);
201 DBG2(DBG_CFG, "configured proposals: %#P", this->proposals);
202 DBG2(DBG_CFG, "selected proposal: %P", selected);
203 break;
204 }
205 }
206 stored->destroy(stored);
207 if (selected)
208 {
209 break;
210 }
211 supplied_enum->destroy(supplied_enum);
212 supplied_enum = proposals->create_enumerator(proposals);
213 }
214 stored_enum->destroy(stored_enum);
215 supplied_enum->destroy(supplied_enum);
216 if (selected == NULL)
217 {
218 DBG1(DBG_CFG, "received proposals: %#P", proposals);
219 DBG1(DBG_CFG, "configured proposals: %#P", this->proposals);
220 }
221 return selected;
222 }
223
224 METHOD(child_cfg_t, add_traffic_selector, void,
225 private_child_cfg_t *this, bool local, traffic_selector_t *ts)
226 {
227 if (local)
228 {
229 this->my_ts->insert_last(this->my_ts, ts);
230 }
231 else
232 {
233 this->other_ts->insert_last(this->other_ts, ts);
234 }
235 }
236
237 METHOD(child_cfg_t, get_traffic_selectors, linked_list_t*,
238 private_child_cfg_t *this, bool local, linked_list_t *supplied, host_t *host)
239 {
240 enumerator_t *e1, *e2;
241 traffic_selector_t *ts1, *ts2, *selected;
242 linked_list_t *result = linked_list_create();
243
244 if (local)
245 {
246 e1 = this->my_ts->create_enumerator(this->my_ts);
247 }
248 else
249 {
250 e1 = this->other_ts->create_enumerator(this->other_ts);
251 }
252
253 /* no list supplied, just fetch the stored traffic selectors */
254 if (supplied == NULL)
255 {
256 DBG2(DBG_CFG, "proposing traffic selectors for %s:",
257 local ? "us" : "other");
258 while (e1->enumerate(e1, &ts1))
259 {
260 /* we make a copy of the TS, this allows us to update dynamic TS' */
261 selected = ts1->clone(ts1);
262 if (host)
263 {
264 selected->set_address(selected, host);
265 }
266 DBG2(DBG_CFG, " %R (derived from %R)", selected, ts1);
267 result->insert_last(result, selected);
268 }
269 e1->destroy(e1);
270 }
271 else
272 {
273 DBG2(DBG_CFG, "selecting traffic selectors for %s:",
274 local ? "us" : "other");
275 e2 = supplied->create_enumerator(supplied);
276 /* iterate over all stored selectors */
277 while (e1->enumerate(e1, &ts1))
278 {
279 /* we make a copy of the TS, as we have to update dynamic TS' */
280 ts1 = ts1->clone(ts1);
281 if (host)
282 {
283 ts1->set_address(ts1, host);
284 }
285
286 /* iterate over all supplied traffic selectors */
287 while (e2->enumerate(e2, &ts2))
288 {
289 selected = ts1->get_subset(ts1, ts2);
290 if (selected)
291 {
292 DBG2(DBG_CFG, " config: %R, received: %R => match: %R",
293 ts1, ts2, selected);
294 result->insert_last(result, selected);
295 }
296 else
297 {
298 DBG2(DBG_CFG, " config: %R, received: %R => no match",
299 ts1, ts2);
300 }
301 }
302 e2->destroy(e2);
303 e2 = supplied->create_enumerator(supplied);
304 ts1->destroy(ts1);
305 }
306 e1->destroy(e1);
307 e2->destroy(e2);
308 }
309
310 /* remove any redundant traffic selectors in the list */
311 e1 = result->create_enumerator(result);
312 e2 = result->create_enumerator(result);
313 while (e1->enumerate(e1, &ts1))
314 {
315 while (e2->enumerate(e2, &ts2))
316 {
317 if (ts1 != ts2)
318 {
319 if (ts2->is_contained_in(ts2, ts1))
320 {
321 result->remove_at(result, e2);
322 ts2->destroy(ts2);
323 e1->destroy(e1);
324 e1 = result->create_enumerator(result);
325 break;
326 }
327 if (ts1->is_contained_in(ts1, ts2))
328 {
329 result->remove_at(result, e1);
330 ts1->destroy(ts1);
331 e2->destroy(e2);
332 e2 = result->create_enumerator(result);
333 break;
334 }
335 }
336 }
337 }
338 e1->destroy(e1);
339 e2->destroy(e2);
340
341 return result;
342 }
343
344 METHOD(child_cfg_t, get_updown, char*,
345 private_child_cfg_t *this)
346 {
347 return this->updown;
348 }
349
350 METHOD(child_cfg_t, get_hostaccess, bool,
351 private_child_cfg_t *this)
352 {
353 return this->hostaccess;
354 }
355
356 /**
357 * Applies jitter to the rekey value. Returns the new rekey value.
358 * Note: The distribution of random values is not perfect, but it
359 * should get the job done.
360 */
361 static u_int64_t apply_jitter(u_int64_t rekey, u_int64_t jitter)
362 {
363 if (jitter == 0)
364 {
365 return rekey;
366 }
367 jitter = (jitter == UINT64_MAX) ? jitter : jitter + 1;
368 return rekey - jitter * (random() / (RAND_MAX + 1.0));
369 }
370 #define APPLY_JITTER(l) l.rekey = apply_jitter(l.rekey, l.jitter)
371
372 METHOD(child_cfg_t, get_lifetime, lifetime_cfg_t*,
373 private_child_cfg_t *this)
374 {
375 lifetime_cfg_t *lft = malloc_thing(lifetime_cfg_t);
376 memcpy(lft, &this->lifetime, sizeof(lifetime_cfg_t));
377 APPLY_JITTER(lft->time);
378 APPLY_JITTER(lft->bytes);
379 APPLY_JITTER(lft->packets);
380 return lft;
381 }
382
383 METHOD(child_cfg_t, get_mode, ipsec_mode_t,
384 private_child_cfg_t *this)
385 {
386 return this->mode;
387 }
388
389 METHOD(child_cfg_t, get_start_action, action_t,
390 private_child_cfg_t *this)
391 {
392 return this->start_action;
393 }
394
395 METHOD(child_cfg_t, get_dpd_action, action_t,
396 private_child_cfg_t *this)
397 {
398 return this->dpd_action;
399 }
400
401 METHOD(child_cfg_t, get_close_action, action_t,
402 private_child_cfg_t *this)
403 {
404 return this->close_action;
405 }
406
407 METHOD(child_cfg_t, get_dh_group, diffie_hellman_group_t,
408 private_child_cfg_t *this)
409 {
410 enumerator_t *enumerator;
411 proposal_t *proposal;
412 u_int16_t dh_group = MODP_NONE;
413
414 enumerator = this->proposals->create_enumerator(this->proposals);
415 while (enumerator->enumerate(enumerator, &proposal))
416 {
417 if (proposal->get_algorithm(proposal, DIFFIE_HELLMAN_GROUP, &dh_group, NULL))
418 {
419 break;
420 }
421 }
422 enumerator->destroy(enumerator);
423 return dh_group;
424 }
425
426 METHOD(child_cfg_t, use_ipcomp, bool,
427 private_child_cfg_t *this)
428 {
429 return this->use_ipcomp;
430 }
431
432 METHOD(child_cfg_t, get_inactivity, u_int32_t,
433 private_child_cfg_t *this)
434 {
435 return this->inactivity;
436 }
437
438 METHOD(child_cfg_t, get_reqid, u_int32_t,
439 private_child_cfg_t *this)
440 {
441 return this->reqid;
442 }
443
444 METHOD(child_cfg_t, get_mark, mark_t,
445 private_child_cfg_t *this, bool inbound)
446 {
447 return inbound ? this->mark_in : this->mark_out;
448 }
449
450 METHOD(child_cfg_t, get_tfc, u_int32_t,
451 private_child_cfg_t *this)
452 {
453 return this->tfc;
454 }
455
456 METHOD(child_cfg_t, set_mipv6_options, void,
457 private_child_cfg_t *this, bool proxy_mode, bool install_policy)
458 {
459 this->proxy_mode = proxy_mode;
460 this->install_policy = install_policy;
461 }
462
463 METHOD(child_cfg_t, use_proxy_mode, bool,
464 private_child_cfg_t *this)
465 {
466 return this->proxy_mode;
467 }
468
469 METHOD(child_cfg_t, install_policy, bool,
470 private_child_cfg_t *this)
471 {
472 return this->install_policy;
473 }
474
475 METHOD(child_cfg_t, get_ref, child_cfg_t*,
476 private_child_cfg_t *this)
477 {
478 ref_get(&this->refcount);
479 return &this->public;
480 }
481
482 METHOD(child_cfg_t, destroy, void,
483 private_child_cfg_t *this)
484 {
485 if (ref_put(&this->refcount))
486 {
487 this->proposals->destroy_offset(this->proposals, offsetof(proposal_t, destroy));
488 this->my_ts->destroy_offset(this->my_ts, offsetof(traffic_selector_t, destroy));
489 this->other_ts->destroy_offset(this->other_ts, offsetof(traffic_selector_t, destroy));
490 if (this->updown)
491 {
492 free(this->updown);
493 }
494 free(this->name);
495 free(this);
496 }
497 }
498
499 /*
500 * Described in header-file
501 */
502 child_cfg_t *child_cfg_create(char *name, lifetime_cfg_t *lifetime,
503 char *updown, bool hostaccess,
504 ipsec_mode_t mode, action_t start_action,
505 action_t dpd_action, action_t close_action,
506 bool ipcomp, u_int32_t inactivity, u_int32_t reqid,
507 mark_t *mark_in, mark_t *mark_out, u_int32_t tfc)
508 {
509 private_child_cfg_t *this;
510
511 INIT(this,
512 .public = {
513 .get_name = _get_name,
514 .add_traffic_selector = _add_traffic_selector,
515 .get_traffic_selectors = _get_traffic_selectors,
516 .add_proposal = _add_proposal,
517 .get_proposals = _get_proposals,
518 .select_proposal = _select_proposal,
519 .get_updown = _get_updown,
520 .get_hostaccess = _get_hostaccess,
521 .get_mode = _get_mode,
522 .get_start_action = _get_start_action,
523 .get_dpd_action = _get_dpd_action,
524 .get_close_action = _get_close_action,
525 .get_lifetime = _get_lifetime,
526 .get_dh_group = _get_dh_group,
527 .set_mipv6_options = _set_mipv6_options,
528 .use_ipcomp = _use_ipcomp,
529 .get_inactivity = _get_inactivity,
530 .get_reqid = _get_reqid,
531 .get_mark = _get_mark,
532 .get_tfc = _get_tfc,
533 .use_proxy_mode = _use_proxy_mode,
534 .install_policy = _install_policy,
535 .get_ref = _get_ref,
536 .destroy = _destroy,
537 },
538 .name = strdup(name),
539 .updown = strdupnull(updown),
540 .hostaccess = hostaccess,
541 .mode = mode,
542 .start_action = start_action,
543 .dpd_action = dpd_action,
544 .close_action = close_action,
545 .use_ipcomp = ipcomp,
546 .inactivity = inactivity,
547 .reqid = reqid,
548 .proxy_mode = FALSE,
549 .install_policy = TRUE,
550 .refcount = 1,
551 .proposals = linked_list_create(),
552 .my_ts = linked_list_create(),
553 .other_ts = linked_list_create(),
554 .tfc = tfc,
555 );
556
557 if (mark_in)
558 {
559 this->mark_in = *mark_in;
560 }
561 if (mark_out)
562 {
563 this->mark_out = *mark_out;
564 }
565 memcpy(&this->lifetime, lifetime, sizeof(lifetime_cfg_t));
566
567 return &this->public;
568 }
569