added support for transport mode and (experimental!) BEET mode
[strongswan.git] / src / charon / config / policies / policy.c
1 /**
2 * @file policy.c
3 *
4 * @brief Implementation of policy_t.
5 *
6 */
7
8 /*
9 * Copyright (C) 2005-2006 Martin Willi
10 * Copyright (C) 2005 Jan Hutter
11 * Hochschule fuer Technik Rapperswil
12 *
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
17 *
18 * This program is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 * for more details.
22 */
23
24 #include <time.h>
25 #include <string.h>
26 #include <unistd.h>
27
28 #include "policy.h"
29
30 #include <daemon.h>
31 #include <utils/linked_list.h>
32 #include <utils/identification.h>
33
34 ENUM(dpd_action_names, DPD_NONE, DPD_RESTART,
35 "DPD_NONE",
36 "DPD_CLEAR",
37 "DPD_ROUTE",
38 "DPD_RESTART"
39 );
40
41 typedef struct private_policy_t private_policy_t;
42
43 /**
44 * Private data of an policy_t object
45 */
46 struct private_policy_t {
47
48 /**
49 * Public part
50 */
51 policy_t public;
52
53 /**
54 * Number of references hold by others to this policy
55 */
56 refcount_t refcount;
57
58 /**
59 * Name of the policy, used to query it
60 */
61 char *name;
62
63 /**
64 * id to use to identify us
65 */
66 identification_t *my_id;
67
68 /**
69 * allowed id for other
70 */
71 identification_t *other_id;
72
73 /**
74 * Method to use for own authentication data
75 */
76 auth_method_t auth_method;
77
78 /**
79 * we have a cert issued by this CA
80 */
81 identification_t *my_ca;
82
83 /**
84 * we require the other end to have a cert issued by this CA
85 */
86 identification_t *other_ca;
87
88 /**
89 * updown script
90 */
91 char *updown;
92
93 /**
94 * allow host access
95 */
96 bool hostaccess;
97
98 /**
99 * list for all proposals
100 */
101 linked_list_t *proposals;
102
103 /**
104 * list for traffic selectors for my site
105 */
106 linked_list_t *my_ts;
107
108 /**
109 * list for traffic selectors for others site
110 */
111 linked_list_t *other_ts;
112
113 /**
114 * Time before an SA gets invalid
115 */
116 u_int32_t soft_lifetime;
117
118 /**
119 * Time before an SA gets rekeyed
120 */
121 u_int32_t hard_lifetime;
122
123 /**
124 * Time, which specifies the range of a random value
125 * substracted from soft_lifetime.
126 */
127 u_int32_t jitter;
128
129 /**
130 * What to do with an SA when other peer seams to be dead?
131 */
132 bool dpd_action;
133
134 /**
135 * Mode to propose for a initiated CHILD: tunnel/transport
136 */
137 mode_t mode;
138 };
139
140 /**
141 * Implementation of policy_t.get_name
142 */
143 static char *get_name(private_policy_t *this)
144 {
145 return this->name;
146 }
147
148 /**
149 * Implementation of policy_t.get_my_id
150 */
151 static identification_t *get_my_id(private_policy_t *this)
152 {
153 return this->my_id;
154 }
155
156 /**
157 * Implementation of policy_t.get_other_id
158 */
159 static identification_t *get_other_id(private_policy_t *this)
160 {
161 return this->other_id;
162 }
163
164 /**
165 * Implementation of policy_t.get_my_ca
166 */
167 static identification_t *get_my_ca(private_policy_t *this)
168 {
169 return this->my_ca;
170 }
171
172 /**
173 * Implementation of policy_t.get_other_ca
174 */
175 static identification_t *get_other_ca(private_policy_t *this)
176 {
177 return this->other_ca;
178 }
179
180 /**
181 * Implementation of connection_t.auth_method_t.
182 */
183 static auth_method_t get_auth_method(private_policy_t *this)
184 {
185 return this->auth_method;
186 }
187
188 /**
189 * Get traffic selectors, with wildcard-address update
190 */
191 static linked_list_t *get_traffic_selectors(private_policy_t *this, linked_list_t *list, host_t *host)
192 {
193 iterator_t *iterator;
194 traffic_selector_t *current;
195 linked_list_t *result = linked_list_create();
196
197 iterator = list->create_iterator(list, TRUE);
198
199 while (iterator->iterate(iterator, (void**)&current))
200 {
201 /* we make a copy of the TS, this allows us to update wildcard
202 * addresses in it. We won't pollute the shared policy. */
203 current = current->clone(current);
204 current->update_address_range(current, host);
205
206 result->insert_last(result, (void*)current);
207 }
208 iterator->destroy(iterator);
209 return result;
210 }
211
212 /**
213 * Implementation of policy_t.get_my_traffic_selectors
214 */
215 static linked_list_t *get_my_traffic_selectors(private_policy_t *this, host_t *me)
216 {
217 return get_traffic_selectors(this, this->my_ts, me);
218 }
219
220 /**
221 * Implementation of policy_t.get_other_traffic_selectors
222 */
223 static linked_list_t *get_other_traffic_selectors(private_policy_t *this, host_t *other)
224 {
225 return get_traffic_selectors(this, this->other_ts, other);
226 }
227
228 /**
229 * Narrow traffic selectors, with wildcard-address update in "stored".
230 */
231 static linked_list_t *select_traffic_selectors(private_policy_t *this,
232 linked_list_t *stored,
233 linked_list_t *supplied,
234 host_t *host)
235 {
236 iterator_t *supplied_iter, *stored_iter;
237 traffic_selector_t *supplied_ts, *stored_ts, *selected_ts;
238 linked_list_t *selected = linked_list_create();
239
240 DBG2(DBG_CFG, "selecting traffic selectors");
241
242 stored_iter = stored->create_iterator(stored, TRUE);
243 supplied_iter = supplied->create_iterator(supplied, TRUE);
244
245 /* iterate over all stored selectors */
246 while (stored_iter->iterate(stored_iter, (void**)&stored_ts))
247 {
248 /* we make a copy of the TS, this allows us to update wildcard
249 * addresses in it. We won't pollute the shared policy. */
250 stored_ts = stored_ts->clone(stored_ts);
251 stored_ts->update_address_range(stored_ts, host);
252
253 supplied_iter->reset(supplied_iter);
254 /* iterate over all supplied traffic selectors */
255 while (supplied_iter->iterate(supplied_iter, (void**)&supplied_ts))
256 {
257 DBG2(DBG_CFG, "stored %R <=> %R received",
258 stored_ts, supplied_ts);
259
260 selected_ts = stored_ts->get_subset(stored_ts, supplied_ts);
261 if (selected_ts)
262 {
263 /* got a match, add to list */
264 selected->insert_last(selected, (void*)selected_ts);
265
266 DBG2(DBG_CFG, "found traffic selector for %s: %R",
267 stored == this->my_ts ? "us" : "other", selected_ts);
268 }
269 }
270 stored_ts->destroy(stored_ts);
271 }
272 stored_iter->destroy(stored_iter);
273 supplied_iter->destroy(supplied_iter);
274
275 return selected;
276 }
277
278 /**
279 * Implementation of private_policy_t.select_my_traffic_selectors
280 */
281 static linked_list_t *select_my_traffic_selectors(private_policy_t *this,
282 linked_list_t *supplied,
283 host_t *me)
284 {
285 return select_traffic_selectors(this, this->my_ts, supplied, me);
286 }
287
288 /**
289 * Implementation of private_policy_t.select_other_traffic_selectors
290 */
291 static linked_list_t *select_other_traffic_selectors(private_policy_t *this,
292 linked_list_t *supplied,
293 host_t* other)
294 {
295 return select_traffic_selectors(this, this->other_ts, supplied, other);
296 }
297
298 /**
299 * Implementation of policy_t.get_proposal_iterator
300 */
301 static linked_list_t *get_proposals(private_policy_t *this)
302 {
303 iterator_t *iterator;
304 proposal_t *current;
305 linked_list_t *proposals = linked_list_create();
306
307 iterator = this->proposals->create_iterator(this->proposals, TRUE);
308 while (iterator->iterate(iterator, (void**)&current))
309 {
310 current = current->clone(current);
311 proposals->insert_last(proposals, (void*)current);
312 }
313 iterator->destroy(iterator);
314
315 return proposals;
316 }
317
318 /**
319 * Implementation of policy_t.select_proposal
320 */
321 static proposal_t *select_proposal(private_policy_t *this, linked_list_t *proposals)
322 {
323 iterator_t *stored_iter, *supplied_iter;
324 proposal_t *stored, *supplied, *selected;
325
326 stored_iter = this->proposals->create_iterator(this->proposals, TRUE);
327 supplied_iter = proposals->create_iterator(proposals, TRUE);
328
329 /* compare all stored proposals with all supplied. Stored ones are preferred. */
330 while (stored_iter->iterate(stored_iter, (void**)&stored))
331 {
332 supplied_iter->reset(supplied_iter);
333 while (supplied_iter->iterate(supplied_iter, (void**)&supplied))
334 {
335 selected = stored->select(stored, supplied);
336 if (selected)
337 {
338 /* they match, return */
339 stored_iter->destroy(stored_iter);
340 supplied_iter->destroy(supplied_iter);
341 return selected;
342 }
343 }
344 }
345
346 /* no proposal match :-(, will result in a NO_PROPOSAL_CHOSEN... */
347 stored_iter->destroy(stored_iter);
348 supplied_iter->destroy(supplied_iter);
349
350 return NULL;
351 }
352
353 /**
354 * Implementation of policy_t.add_authorities
355 */
356 static void add_authorities(private_policy_t *this, identification_t *my_ca, identification_t *other_ca)
357 {
358 this->my_ca = my_ca;
359 this->other_ca = other_ca;
360 }
361
362 /**
363 * Implementation of policy_t.get_updown
364 */
365 static char* get_updown(private_policy_t *this)
366 {
367 return this->updown;
368 }
369
370 /**
371 * Implementation of policy_t.get_hostaccess
372 */
373 static bool get_hostaccess(private_policy_t *this)
374 {
375 return this->hostaccess;
376 }
377
378 /**
379 * Implements policy_t.get_dpd_action
380 */
381 static dpd_action_t get_dpd_action(private_policy_t *this)
382 {
383 return this->dpd_action;
384 }
385
386 /**
387 * Implementation of policy_t.add_my_traffic_selector
388 */
389 static void add_my_traffic_selector(private_policy_t *this, traffic_selector_t *traffic_selector)
390 {
391 this->my_ts->insert_last(this->my_ts, (void*)traffic_selector);
392 }
393
394 /**
395 * Implementation of policy_t.add_other_traffic_selector
396 */
397 static void add_other_traffic_selector(private_policy_t *this, traffic_selector_t *traffic_selector)
398 {
399 this->other_ts->insert_last(this->other_ts, (void*)traffic_selector);
400 }
401
402 /**
403 * Implementation of policy_t.add_proposal
404 */
405 static void add_proposal(private_policy_t *this, proposal_t *proposal)
406 {
407 this->proposals->insert_last(this->proposals, (void*)proposal);
408 }
409
410 /**
411 * Implementation of policy_t.get_soft_lifetime
412 */
413 static u_int32_t get_soft_lifetime(private_policy_t *this)
414 {
415 if (this->jitter == 0)
416 {
417 return this->soft_lifetime ;
418 }
419 return this->soft_lifetime - (random() % this->jitter);
420 }
421
422 /**
423 * Implementation of policy_t.get_hard_lifetime
424 */
425 static u_int32_t get_hard_lifetime(private_policy_t *this)
426 {
427 return this->hard_lifetime;
428 }
429
430 /**
431 * Implementation of policy_t.get_mode.
432 */
433 static mode_t get_mode(private_policy_t *this)
434 {
435 return this->mode;
436 }
437
438 /**
439 * Implements policy_t.get_ref.
440 */
441 static void get_ref(private_policy_t *this)
442 {
443 ref_get(&this->refcount);
444 }
445
446 /**
447 * Implements policy_t.destroy.
448 */
449 static void destroy(private_policy_t *this)
450 {
451 if (ref_put(&this->refcount))
452 {
453
454 this->proposals->destroy_offset(this->proposals, offsetof(proposal_t, destroy));
455 this->my_ts->destroy_offset(this->my_ts, offsetof(traffic_selector_t, destroy));
456 this->other_ts->destroy_offset(this->other_ts, offsetof(traffic_selector_t, destroy));
457
458 /* delete certification authorities */
459 if (this->my_ca)
460 {
461 this->my_ca->destroy(this->my_ca);
462 }
463 if (this->other_ca)
464 {
465 this->other_ca->destroy(this->other_ca);
466 }
467
468 /* delete updown script */
469 if (this->updown)
470 {
471 free(this->updown);
472 }
473
474 /* delete ids */
475 this->my_id->destroy(this->my_id);
476 this->other_id->destroy(this->other_id);
477
478 free(this->name);
479 free(this);
480 }
481 }
482
483 /*
484 * Described in header-file
485 */
486 policy_t *policy_create(char *name, identification_t *my_id, identification_t *other_id,
487 auth_method_t auth_method,
488 u_int32_t hard_lifetime, u_int32_t soft_lifetime,
489 u_int32_t jitter, char *updown, bool hostaccess,
490 mode_t mode, dpd_action_t dpd_action)
491 {
492 private_policy_t *this = malloc_thing(private_policy_t);
493
494 /* public functions */
495 this->public.get_name = (char* (*) (policy_t*))get_name;
496 this->public.get_my_id = (identification_t* (*) (policy_t*))get_my_id;
497 this->public.get_other_id = (identification_t* (*) (policy_t*))get_other_id;
498 this->public.get_my_ca = (identification_t* (*) (policy_t*))get_my_ca;
499 this->public.get_other_ca = (identification_t* (*) (policy_t*))get_other_ca;
500 this->public.get_auth_method = (auth_method_t (*) (policy_t*)) get_auth_method;
501 this->public.get_my_traffic_selectors = (linked_list_t* (*) (policy_t*,host_t*))get_my_traffic_selectors;
502 this->public.get_other_traffic_selectors = (linked_list_t* (*) (policy_t*,host_t*))get_other_traffic_selectors;
503 this->public.select_my_traffic_selectors = (linked_list_t* (*) (policy_t*,linked_list_t*,host_t*))select_my_traffic_selectors;
504 this->public.select_other_traffic_selectors = (linked_list_t* (*) (policy_t*,linked_list_t*,host_t*))select_other_traffic_selectors;
505 this->public.get_proposals = (linked_list_t* (*) (policy_t*))get_proposals;
506 this->public.select_proposal = (proposal_t* (*) (policy_t*,linked_list_t*))select_proposal;
507 this->public.add_my_traffic_selector = (void (*) (policy_t*,traffic_selector_t*))add_my_traffic_selector;
508 this->public.add_other_traffic_selector = (void (*) (policy_t*,traffic_selector_t*))add_other_traffic_selector;
509 this->public.add_proposal = (void (*) (policy_t*,proposal_t*))add_proposal;
510 this->public.add_authorities = (void (*) (policy_t*,identification_t*,identification_t*))add_authorities;
511 this->public.get_updown = (char* (*) (policy_t*))get_updown;
512 this->public.get_hostaccess = (bool (*) (policy_t*))get_hostaccess;
513 this->public.get_dpd_action = (dpd_action_t (*) (policy_t*))get_dpd_action;
514 this->public.get_soft_lifetime = (u_int32_t (*) (policy_t *))get_soft_lifetime;
515 this->public.get_hard_lifetime = (u_int32_t (*) (policy_t *))get_hard_lifetime;
516 this->public.get_mode = (mode_t (*) (policy_t *))get_mode;
517 this->public.get_ref = (void (*) (policy_t*))get_ref;
518 this->public.destroy = (void (*) (policy_t*))destroy;
519
520 /* apply init values */
521 this->name = strdup(name);
522 this->my_id = my_id;
523 this->other_id = other_id;
524 this->auth_method = auth_method;
525 this->hard_lifetime = hard_lifetime;
526 this->soft_lifetime = soft_lifetime;
527 this->jitter = jitter;
528 this->updown = (updown == NULL) ? NULL : strdup(updown);
529 this->hostaccess = hostaccess;
530 this->dpd_action = dpd_action;
531 this->mode = mode;
532
533 /* initialize private members*/
534 this->refcount = 1;
535 this->my_ca = NULL;
536 this->other_ca = NULL;
537 this->proposals = linked_list_create();
538 this->my_ts = linked_list_create();
539 this->other_ts = linked_list_create();
540
541 return &this->public;
542 }