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