Move rw-eap-dynamic scenario to its proper location
[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 DBG2(DBG_CFG, "configured proposals: %#P", proposals);
175
176 return proposals;
177 }
178
179 METHOD(child_cfg_t, select_proposal, proposal_t*,
180 private_child_cfg_t*this, linked_list_t *proposals, bool strip_dh,
181 bool private)
182 {
183 enumerator_t *stored_enum, *supplied_enum;
184 proposal_t *stored, *supplied, *selected = NULL;
185
186 stored_enum = this->proposals->create_enumerator(this->proposals);
187 supplied_enum = proposals->create_enumerator(proposals);
188
189 /* compare all stored proposals with all supplied. Stored ones are preferred. */
190 while (stored_enum->enumerate(stored_enum, &stored))
191 {
192 stored = stored->clone(stored);
193 while (supplied_enum->enumerate(supplied_enum, &supplied))
194 {
195 if (strip_dh)
196 {
197 stored->strip_dh(stored);
198 }
199 selected = stored->select(stored, supplied, private);
200 if (selected)
201 {
202 DBG2(DBG_CFG, "received proposals: %#P", proposals);
203 DBG2(DBG_CFG, "configured proposals: %#P", this->proposals);
204 DBG2(DBG_CFG, "selected proposal: %P", selected);
205 break;
206 }
207 }
208 stored->destroy(stored);
209 if (selected)
210 {
211 break;
212 }
213 supplied_enum->destroy(supplied_enum);
214 supplied_enum = proposals->create_enumerator(proposals);
215 }
216 stored_enum->destroy(stored_enum);
217 supplied_enum->destroy(supplied_enum);
218 if (selected == NULL)
219 {
220 DBG1(DBG_CFG, "received proposals: %#P", proposals);
221 DBG1(DBG_CFG, "configured proposals: %#P", this->proposals);
222 }
223 return selected;
224 }
225
226 METHOD(child_cfg_t, add_traffic_selector, void,
227 private_child_cfg_t *this, bool local, traffic_selector_t *ts)
228 {
229 if (local)
230 {
231 this->my_ts->insert_last(this->my_ts, ts);
232 }
233 else
234 {
235 this->other_ts->insert_last(this->other_ts, ts);
236 }
237 }
238
239 METHOD(child_cfg_t, get_traffic_selectors, linked_list_t*,
240 private_child_cfg_t *this, bool local, linked_list_t *supplied,
241 linked_list_t *hosts)
242 {
243 enumerator_t *e1, *e2;
244 traffic_selector_t *ts1, *ts2, *selected;
245 linked_list_t *result, *derived;
246 host_t *host;
247
248 result = linked_list_create();
249 derived = linked_list_create();
250 if (local)
251 {
252 e1 = this->my_ts->create_enumerator(this->my_ts);
253 }
254 else
255 {
256 e1 = this->other_ts->create_enumerator(this->other_ts);
257 }
258 /* In a first step, replace "dynamic" TS with the host list */
259 while (e1->enumerate(e1, &ts1))
260 {
261 if (ts1->is_dynamic(ts1))
262 {
263 if (hosts)
264 {
265 e2 = hosts->create_enumerator(hosts);
266 while (e2->enumerate(e2, &host))
267 {
268 ts2 = ts1->clone(ts1);
269 ts2->set_address(ts2, host);
270 result->insert_last(derived, ts2);
271 }
272 e2->destroy(e2);
273 }
274 }
275 else
276 {
277 derived->insert_last(derived, ts1->clone(ts1));
278 }
279 }
280 e1->destroy(e1);
281
282 DBG2(DBG_CFG, "%s traffic selectors for %s:",
283 supplied ? "selecting" : "proposing", local ? "us" : "other");
284 if (supplied == NULL)
285 {
286 while (derived->remove_first(derived, (void**)&ts1) == SUCCESS)
287 {
288 DBG2(DBG_CFG, " %R", ts1);
289 result->insert_last(result, ts1);
290 }
291 }
292 else
293 {
294 e1 = supplied->create_enumerator(supplied);
295 /* enumerate all configured/derived selectors */
296 while (derived->remove_first(derived, (void**)&ts1) == SUCCESS)
297 {
298 /* enumerate all supplied traffic selectors */
299 while (e1->enumerate(e1, &ts2))
300 {
301 selected = ts1->get_subset(ts1, ts2);
302 if (selected)
303 {
304 DBG2(DBG_CFG, " config: %R, received: %R => match: %R",
305 ts1, ts2, selected);
306 result->insert_last(result, selected);
307 }
308 else
309 {
310 DBG2(DBG_CFG, " config: %R, received: %R => no match",
311 ts1, ts2);
312 }
313 }
314 supplied->reset_enumerator(supplied, e1);
315 ts1->destroy(ts1);
316 }
317 e1->destroy(e1);
318 }
319 derived->destroy(derived);
320
321 /* remove any redundant traffic selectors in the list */
322 e1 = result->create_enumerator(result);
323 e2 = result->create_enumerator(result);
324 while (e1->enumerate(e1, &ts1))
325 {
326 while (e2->enumerate(e2, &ts2))
327 {
328 if (ts1 != ts2)
329 {
330 if (ts2->is_contained_in(ts2, ts1))
331 {
332 result->remove_at(result, e2);
333 ts2->destroy(ts2);
334 result->reset_enumerator(result, e1);
335 break;
336 }
337 if (ts1->is_contained_in(ts1, ts2))
338 {
339 result->remove_at(result, e1);
340 ts1->destroy(ts1);
341 result->reset_enumerator(result, e2);
342 break;
343 }
344 }
345 }
346 }
347 e1->destroy(e1);
348 e2->destroy(e2);
349
350 return result;
351 }
352
353 METHOD(child_cfg_t, get_updown, char*,
354 private_child_cfg_t *this)
355 {
356 return this->updown;
357 }
358
359 METHOD(child_cfg_t, get_hostaccess, bool,
360 private_child_cfg_t *this)
361 {
362 return this->hostaccess;
363 }
364
365 /**
366 * Applies jitter to the rekey value. Returns the new rekey value.
367 * Note: The distribution of random values is not perfect, but it
368 * should get the job done.
369 */
370 static u_int64_t apply_jitter(u_int64_t rekey, u_int64_t jitter)
371 {
372 if (jitter == 0)
373 {
374 return rekey;
375 }
376 jitter = (jitter == UINT64_MAX) ? jitter : jitter + 1;
377 return rekey - jitter * (random() / (RAND_MAX + 1.0));
378 }
379 #define APPLY_JITTER(l) l.rekey = apply_jitter(l.rekey, l.jitter)
380
381 METHOD(child_cfg_t, get_lifetime, lifetime_cfg_t*,
382 private_child_cfg_t *this)
383 {
384 lifetime_cfg_t *lft = malloc_thing(lifetime_cfg_t);
385 memcpy(lft, &this->lifetime, sizeof(lifetime_cfg_t));
386 APPLY_JITTER(lft->time);
387 APPLY_JITTER(lft->bytes);
388 APPLY_JITTER(lft->packets);
389 return lft;
390 }
391
392 METHOD(child_cfg_t, get_mode, ipsec_mode_t,
393 private_child_cfg_t *this)
394 {
395 return this->mode;
396 }
397
398 METHOD(child_cfg_t, get_start_action, action_t,
399 private_child_cfg_t *this)
400 {
401 return this->start_action;
402 }
403
404 METHOD(child_cfg_t, get_dpd_action, action_t,
405 private_child_cfg_t *this)
406 {
407 return this->dpd_action;
408 }
409
410 METHOD(child_cfg_t, get_close_action, action_t,
411 private_child_cfg_t *this)
412 {
413 return this->close_action;
414 }
415
416 METHOD(child_cfg_t, get_dh_group, diffie_hellman_group_t,
417 private_child_cfg_t *this)
418 {
419 enumerator_t *enumerator;
420 proposal_t *proposal;
421 u_int16_t dh_group = MODP_NONE;
422
423 enumerator = this->proposals->create_enumerator(this->proposals);
424 while (enumerator->enumerate(enumerator, &proposal))
425 {
426 if (proposal->get_algorithm(proposal, DIFFIE_HELLMAN_GROUP, &dh_group, NULL))
427 {
428 break;
429 }
430 }
431 enumerator->destroy(enumerator);
432 return dh_group;
433 }
434
435 METHOD(child_cfg_t, use_ipcomp, bool,
436 private_child_cfg_t *this)
437 {
438 return this->use_ipcomp;
439 }
440
441 METHOD(child_cfg_t, get_inactivity, u_int32_t,
442 private_child_cfg_t *this)
443 {
444 return this->inactivity;
445 }
446
447 METHOD(child_cfg_t, get_reqid, u_int32_t,
448 private_child_cfg_t *this)
449 {
450 return this->reqid;
451 }
452
453 METHOD(child_cfg_t, get_mark, mark_t,
454 private_child_cfg_t *this, bool inbound)
455 {
456 return inbound ? this->mark_in : this->mark_out;
457 }
458
459 METHOD(child_cfg_t, get_tfc, u_int32_t,
460 private_child_cfg_t *this)
461 {
462 return this->tfc;
463 }
464
465 METHOD(child_cfg_t, set_mipv6_options, void,
466 private_child_cfg_t *this, bool proxy_mode, bool install_policy)
467 {
468 this->proxy_mode = proxy_mode;
469 this->install_policy = install_policy;
470 }
471
472 METHOD(child_cfg_t, use_proxy_mode, bool,
473 private_child_cfg_t *this)
474 {
475 return this->proxy_mode;
476 }
477
478 METHOD(child_cfg_t, install_policy, bool,
479 private_child_cfg_t *this)
480 {
481 return this->install_policy;
482 }
483
484 METHOD(child_cfg_t, get_ref, child_cfg_t*,
485 private_child_cfg_t *this)
486 {
487 ref_get(&this->refcount);
488 return &this->public;
489 }
490
491 METHOD(child_cfg_t, destroy, void,
492 private_child_cfg_t *this)
493 {
494 if (ref_put(&this->refcount))
495 {
496 this->proposals->destroy_offset(this->proposals, offsetof(proposal_t, destroy));
497 this->my_ts->destroy_offset(this->my_ts, offsetof(traffic_selector_t, destroy));
498 this->other_ts->destroy_offset(this->other_ts, offsetof(traffic_selector_t, destroy));
499 if (this->updown)
500 {
501 free(this->updown);
502 }
503 free(this->name);
504 free(this);
505 }
506 }
507
508 /*
509 * Described in header-file
510 */
511 child_cfg_t *child_cfg_create(char *name, lifetime_cfg_t *lifetime,
512 char *updown, bool hostaccess,
513 ipsec_mode_t mode, action_t start_action,
514 action_t dpd_action, action_t close_action,
515 bool ipcomp, u_int32_t inactivity, u_int32_t reqid,
516 mark_t *mark_in, mark_t *mark_out, u_int32_t tfc)
517 {
518 private_child_cfg_t *this;
519
520 INIT(this,
521 .public = {
522 .get_name = _get_name,
523 .add_traffic_selector = _add_traffic_selector,
524 .get_traffic_selectors = _get_traffic_selectors,
525 .add_proposal = _add_proposal,
526 .get_proposals = _get_proposals,
527 .select_proposal = _select_proposal,
528 .get_updown = _get_updown,
529 .get_hostaccess = _get_hostaccess,
530 .get_mode = _get_mode,
531 .get_start_action = _get_start_action,
532 .get_dpd_action = _get_dpd_action,
533 .get_close_action = _get_close_action,
534 .get_lifetime = _get_lifetime,
535 .get_dh_group = _get_dh_group,
536 .set_mipv6_options = _set_mipv6_options,
537 .use_ipcomp = _use_ipcomp,
538 .get_inactivity = _get_inactivity,
539 .get_reqid = _get_reqid,
540 .get_mark = _get_mark,
541 .get_tfc = _get_tfc,
542 .use_proxy_mode = _use_proxy_mode,
543 .install_policy = _install_policy,
544 .get_ref = _get_ref,
545 .destroy = _destroy,
546 },
547 .name = strdup(name),
548 .updown = strdupnull(updown),
549 .hostaccess = hostaccess,
550 .mode = mode,
551 .start_action = start_action,
552 .dpd_action = dpd_action,
553 .close_action = close_action,
554 .use_ipcomp = ipcomp,
555 .inactivity = inactivity,
556 .reqid = reqid,
557 .proxy_mode = FALSE,
558 .install_policy = TRUE,
559 .refcount = 1,
560 .proposals = linked_list_create(),
561 .my_ts = linked_list_create(),
562 .other_ts = linked_list_create(),
563 .tfc = tfc,
564 );
565
566 if (mark_in)
567 {
568 this->mark_in = *mark_in;
569 }
570 if (mark_out)
571 {
572 this->mark_out = *mark_out;
573 }
574 memcpy(&this->lifetime, lifetime, sizeof(lifetime_cfg_t));
575
576 return &this->public;
577 }
578