3ff0e32ea0de7e0c1f0b38662c13ac267d9631ce
[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 ENUM(mode_names, MODE_TRANSPORT, MODE_BEET,
42 "TRANSPORT",
43 "TUNNEL",
44 "2",
45 "3",
46 "BEET"
47 );
48
49 typedef struct private_policy_t private_policy_t;
50
51 /**
52 * Private data of an policy_t object
53 */
54 struct private_policy_t {
55
56 /**
57 * Public part
58 */
59 policy_t public;
60
61 /**
62 * Number of references hold by others to this policy
63 */
64 refcount_t refcount;
65
66 /**
67 * Name of the policy, used to query it
68 */
69 char *name;
70
71 /**
72 * id to use to identify us
73 */
74 identification_t *my_id;
75
76 /**
77 * allowed id for other
78 */
79 identification_t *other_id;
80
81 /**
82 * virtual IP to use locally
83 */
84 host_t *my_virtual_ip;
85
86 /**
87 * virtual IP to use remotly
88 */
89 host_t *other_virtual_ip;
90
91 /**
92 * Method to use for own authentication data
93 */
94 auth_method_t auth_method;
95
96 /**
97 * EAP type to use for peer authentication
98 */
99 eap_type_t eap_type;
100
101 /**
102 * we have a cert issued by this CA
103 */
104 identification_t *my_ca;
105
106 /**
107 * we require the other end to have a cert issued by this CA
108 */
109 identification_t *other_ca;
110
111 /**
112 * updown script
113 */
114 char *updown;
115
116 /**
117 * allow host access
118 */
119 bool hostaccess;
120
121 /**
122 * list for all proposals
123 */
124 linked_list_t *proposals;
125
126 /**
127 * list for traffic selectors for my site
128 */
129 linked_list_t *my_ts;
130
131 /**
132 * list for traffic selectors for others site
133 */
134 linked_list_t *other_ts;
135
136 /**
137 * Time before an SA gets invalid
138 */
139 u_int32_t soft_lifetime;
140
141 /**
142 * Time before an SA gets rekeyed
143 */
144 u_int32_t hard_lifetime;
145
146 /**
147 * Time, which specifies the range of a random value
148 * substracted from soft_lifetime.
149 */
150 u_int32_t jitter;
151
152 /**
153 * What to do with an SA when other peer seams to be dead?
154 */
155 bool dpd_action;
156
157 /**
158 * Mode to propose for a initiated CHILD: tunnel/transport
159 */
160 mode_t mode;
161 };
162
163 /**
164 * Implementation of policy_t.get_name
165 */
166 static char *get_name(private_policy_t *this)
167 {
168 return this->name;
169 }
170
171 /**
172 * Implementation of policy_t.get_my_id
173 */
174 static identification_t *get_my_id(private_policy_t *this)
175 {
176 return this->my_id;
177 }
178
179 /**
180 * Implementation of policy_t.get_other_id
181 */
182 static identification_t *get_other_id(private_policy_t *this)
183 {
184 return this->other_id;
185 }
186
187 /**
188 * Implementation of policy_t.get_my_ca
189 */
190 static identification_t *get_my_ca(private_policy_t *this)
191 {
192 return this->my_ca;
193 }
194
195 /**
196 * Implementation of policy_t.get_other_ca
197 */
198 static identification_t *get_other_ca(private_policy_t *this)
199 {
200 return this->other_ca;
201 }
202
203 /**
204 * Implementation of connection_t.auth_method_t.
205 */
206 static auth_method_t get_auth_method(private_policy_t *this)
207 {
208 return this->auth_method;
209 }
210
211 /**
212 * Implementation of connection_t.get_eap_type.
213 */
214 static eap_type_t get_eap_type(private_policy_t *this)
215 {
216 return this->eap_type;
217 }
218
219 /**
220 * Get traffic selectors, with wildcard-address update
221 */
222 static linked_list_t *get_traffic_selectors(private_policy_t *this,
223 linked_list_t *list, host_t *host)
224 {
225 iterator_t *iterator;
226 traffic_selector_t *current;
227 linked_list_t *result = linked_list_create();
228
229 iterator = list->create_iterator(list, TRUE);
230
231 while (iterator->iterate(iterator, (void**)&current))
232 {
233 /* we make a copy of the TS, this allows us to update wildcard
234 * addresses in it. We won't pollute the shared policy. */
235 current = current->clone(current);
236 if (host)
237 {
238 current->set_address(current, host);
239 }
240
241 result->insert_last(result, (void*)current);
242 }
243 iterator->destroy(iterator);
244 return result;
245 }
246
247 /**
248 * Implementation of policy_t.get_my_traffic_selectors
249 */
250 static linked_list_t *get_my_traffic_selectors(private_policy_t *this, host_t *me)
251 {
252 return get_traffic_selectors(this, this->my_ts, me);
253 }
254
255 /**
256 * Implementation of policy_t.get_other_traffic_selectors
257 */
258 static linked_list_t *get_other_traffic_selectors(private_policy_t *this, host_t *other)
259 {
260 return get_traffic_selectors(this, this->other_ts, other);
261 }
262
263 /**
264 * Narrow traffic selectors, with wildcard-address update in "stored".
265 */
266 static linked_list_t *select_traffic_selectors(private_policy_t *this,
267 linked_list_t *stored,
268 linked_list_t *supplied,
269 host_t *host)
270 {
271 iterator_t *supplied_iter, *stored_iter;
272 traffic_selector_t *supplied_ts, *stored_ts, *selected_ts;
273 linked_list_t *selected = linked_list_create();
274
275 DBG2(DBG_CFG, "selecting traffic selectors");
276
277 stored_iter = stored->create_iterator(stored, TRUE);
278 supplied_iter = supplied->create_iterator(supplied, TRUE);
279
280 /* iterate over all stored selectors */
281 while (stored_iter->iterate(stored_iter, (void**)&stored_ts))
282 {
283 /* we make a copy of the TS, this allows us to update wildcard
284 * addresses in it. We won't pollute the shared policy. */
285 stored_ts = stored_ts->clone(stored_ts);
286 if (host)
287 {
288 stored_ts->set_address(stored_ts, host);
289 }
290
291 supplied_iter->reset(supplied_iter);
292 /* iterate over all supplied traffic selectors */
293 while (supplied_iter->iterate(supplied_iter, (void**)&supplied_ts))
294 {
295 DBG2(DBG_CFG, "stored %R <=> %R received",
296 stored_ts, supplied_ts);
297
298 selected_ts = stored_ts->get_subset(stored_ts, supplied_ts);
299 if (selected_ts)
300 {
301 /* got a match, add to list */
302 selected->insert_last(selected, (void*)selected_ts);
303
304 DBG2(DBG_CFG, "found traffic selector for %s: %R",
305 stored == this->my_ts ? "us" : "other", selected_ts);
306 }
307 }
308 stored_ts->destroy(stored_ts);
309 }
310 stored_iter->destroy(stored_iter);
311 supplied_iter->destroy(supplied_iter);
312
313 return selected;
314 }
315
316 /**
317 * Implementation of private_policy_t.select_my_traffic_selectors
318 */
319 static linked_list_t *select_my_traffic_selectors(private_policy_t *this,
320 linked_list_t *supplied,
321 host_t *me)
322 {
323 return select_traffic_selectors(this, this->my_ts, supplied, me);
324 }
325
326 /**
327 * Implementation of private_policy_t.select_other_traffic_selectors
328 */
329 static linked_list_t *select_other_traffic_selectors(private_policy_t *this,
330 linked_list_t *supplied,
331 host_t* other)
332 {
333 return select_traffic_selectors(this, this->other_ts, supplied, other);
334 }
335
336 /**
337 * Implementation of policy_t.get_proposal_iterator
338 */
339 static linked_list_t *get_proposals(private_policy_t *this)
340 {
341 iterator_t *iterator;
342 proposal_t *current;
343 linked_list_t *proposals = linked_list_create();
344
345 iterator = this->proposals->create_iterator(this->proposals, TRUE);
346 while (iterator->iterate(iterator, (void**)&current))
347 {
348 current = current->clone(current);
349 proposals->insert_last(proposals, (void*)current);
350 }
351 iterator->destroy(iterator);
352
353 return proposals;
354 }
355
356 /**
357 * Implementation of policy_t.select_proposal
358 */
359 static proposal_t *select_proposal(private_policy_t *this, linked_list_t *proposals)
360 {
361 iterator_t *stored_iter, *supplied_iter;
362 proposal_t *stored, *supplied, *selected;
363
364 stored_iter = this->proposals->create_iterator(this->proposals, TRUE);
365 supplied_iter = proposals->create_iterator(proposals, TRUE);
366
367 /* compare all stored proposals with all supplied. Stored ones are preferred. */
368 while (stored_iter->iterate(stored_iter, (void**)&stored))
369 {
370 supplied_iter->reset(supplied_iter);
371 while (supplied_iter->iterate(supplied_iter, (void**)&supplied))
372 {
373 selected = stored->select(stored, supplied);
374 if (selected)
375 {
376 /* they match, return */
377 stored_iter->destroy(stored_iter);
378 supplied_iter->destroy(supplied_iter);
379 return selected;
380 }
381 }
382 }
383
384 /* no proposal match :-(, will result in a NO_PROPOSAL_CHOSEN... */
385 stored_iter->destroy(stored_iter);
386 supplied_iter->destroy(supplied_iter);
387
388 return NULL;
389 }
390
391 /**
392 * Implementation of policy_t.add_authorities
393 */
394 static void add_authorities(private_policy_t *this, identification_t *my_ca, identification_t *other_ca)
395 {
396 this->my_ca = my_ca;
397 this->other_ca = other_ca;
398 }
399
400 /**
401 * Implementation of policy_t.get_updown
402 */
403 static char* get_updown(private_policy_t *this)
404 {
405 return this->updown;
406 }
407
408 /**
409 * Implementation of policy_t.get_hostaccess
410 */
411 static bool get_hostaccess(private_policy_t *this)
412 {
413 return this->hostaccess;
414 }
415
416 /**
417 * Implements policy_t.get_dpd_action
418 */
419 static dpd_action_t get_dpd_action(private_policy_t *this)
420 {
421 return this->dpd_action;
422 }
423
424 /**
425 * Implementation of policy_t.add_my_traffic_selector
426 */
427 static void add_my_traffic_selector(private_policy_t *this, traffic_selector_t *traffic_selector)
428 {
429 this->my_ts->insert_last(this->my_ts, (void*)traffic_selector);
430 }
431
432 /**
433 * Implementation of policy_t.add_other_traffic_selector
434 */
435 static void add_other_traffic_selector(private_policy_t *this, traffic_selector_t *traffic_selector)
436 {
437 this->other_ts->insert_last(this->other_ts, (void*)traffic_selector);
438 }
439
440 /**
441 * Implementation of policy_t.add_proposal
442 */
443 static void add_proposal(private_policy_t *this, proposal_t *proposal)
444 {
445 this->proposals->insert_last(this->proposals, (void*)proposal);
446 }
447
448 /**
449 * Implementation of policy_t.get_soft_lifetime
450 */
451 static u_int32_t get_soft_lifetime(private_policy_t *this)
452 {
453 if (this->jitter == 0)
454 {
455 return this->soft_lifetime ;
456 }
457 return this->soft_lifetime - (random() % this->jitter);
458 }
459
460 /**
461 * Implementation of policy_t.get_hard_lifetime
462 */
463 static u_int32_t get_hard_lifetime(private_policy_t *this)
464 {
465 return this->hard_lifetime;
466 }
467
468 /**
469 * Implementation of policy_t.get_mode.
470 */
471 static mode_t get_mode(private_policy_t *this)
472 {
473 return this->mode;
474 }
475
476 /**
477 * Implementation of policy_t.get_virtual_ip.
478 */
479 static host_t* get_virtual_ip(private_policy_t *this, host_t *suggestion)
480 {
481 if (suggestion == NULL)
482 {
483 if (this->my_virtual_ip)
484 {
485 return this->my_virtual_ip->clone(this->my_virtual_ip);
486 }
487 return NULL;
488 }
489 if (this->other_virtual_ip)
490 {
491 return this->other_virtual_ip->clone(this->other_virtual_ip);
492 }
493 if (suggestion->is_anyaddr(suggestion))
494 {
495 return NULL;
496 }
497 return suggestion->clone(suggestion);
498 }
499
500 /**
501 * Implements policy_t.get_ref.
502 */
503 static void get_ref(private_policy_t *this)
504 {
505 ref_get(&this->refcount);
506 }
507
508 /**
509 * Implements policy_t.destroy.
510 */
511 static void destroy(private_policy_t *this)
512 {
513 if (ref_put(&this->refcount))
514 {
515
516 this->proposals->destroy_offset(this->proposals, offsetof(proposal_t, destroy));
517 this->my_ts->destroy_offset(this->my_ts, offsetof(traffic_selector_t, destroy));
518 this->other_ts->destroy_offset(this->other_ts, offsetof(traffic_selector_t, destroy));
519
520 /* delete certification authorities */
521 DESTROY_IF(this->my_ca);
522 DESTROY_IF(this->other_ca);
523
524 /* delete updown script */
525 if (this->updown)
526 {
527 free(this->updown);
528 }
529
530 /* delete ids */
531 this->my_id->destroy(this->my_id);
532 this->other_id->destroy(this->other_id);
533 DESTROY_IF(this->my_virtual_ip);
534 DESTROY_IF(this->other_virtual_ip);
535
536 free(this->name);
537 free(this);
538 }
539 }
540
541 /*
542 * Described in header-file
543 */
544 policy_t *policy_create(char *name, identification_t *my_id, identification_t *other_id,
545 host_t *my_virtual_ip, host_t *other_virtual_ip,
546 auth_method_t auth_method, eap_type_t eap_type,
547 u_int32_t hard_lifetime, u_int32_t soft_lifetime,
548 u_int32_t jitter, char *updown, bool hostaccess,
549 mode_t mode, dpd_action_t dpd_action)
550 {
551 private_policy_t *this = malloc_thing(private_policy_t);
552
553 /* public functions */
554 this->public.get_name = (char* (*) (policy_t*))get_name;
555 this->public.get_my_id = (identification_t* (*) (policy_t*))get_my_id;
556 this->public.get_other_id = (identification_t* (*) (policy_t*))get_other_id;
557 this->public.get_my_ca = (identification_t* (*) (policy_t*))get_my_ca;
558 this->public.get_other_ca = (identification_t* (*) (policy_t*))get_other_ca;
559 this->public.get_auth_method = (auth_method_t (*) (policy_t*)) get_auth_method;
560 this->public.get_eap_type = (eap_type_t (*) (policy_t*)) get_eap_type;
561 this->public.get_my_traffic_selectors = (linked_list_t* (*) (policy_t*,host_t*))get_my_traffic_selectors;
562 this->public.get_other_traffic_selectors = (linked_list_t* (*) (policy_t*,host_t*))get_other_traffic_selectors;
563 this->public.select_my_traffic_selectors = (linked_list_t* (*) (policy_t*,linked_list_t*,host_t*))select_my_traffic_selectors;
564 this->public.select_other_traffic_selectors = (linked_list_t* (*) (policy_t*,linked_list_t*,host_t*))select_other_traffic_selectors;
565 this->public.get_proposals = (linked_list_t* (*) (policy_t*))get_proposals;
566 this->public.select_proposal = (proposal_t* (*) (policy_t*,linked_list_t*))select_proposal;
567 this->public.add_my_traffic_selector = (void (*) (policy_t*,traffic_selector_t*))add_my_traffic_selector;
568 this->public.add_other_traffic_selector = (void (*) (policy_t*,traffic_selector_t*))add_other_traffic_selector;
569 this->public.add_proposal = (void (*) (policy_t*,proposal_t*))add_proposal;
570 this->public.add_authorities = (void (*) (policy_t*,identification_t*,identification_t*))add_authorities;
571 this->public.get_updown = (char* (*) (policy_t*))get_updown;
572 this->public.get_hostaccess = (bool (*) (policy_t*))get_hostaccess;
573 this->public.get_dpd_action = (dpd_action_t (*) (policy_t*))get_dpd_action;
574 this->public.get_soft_lifetime = (u_int32_t (*) (policy_t *))get_soft_lifetime;
575 this->public.get_hard_lifetime = (u_int32_t (*) (policy_t *))get_hard_lifetime;
576 this->public.get_mode = (mode_t (*) (policy_t *))get_mode;
577 this->public.get_virtual_ip = (host_t* (*)(policy_t*,host_t*))get_virtual_ip;
578 this->public.get_ref = (void (*) (policy_t*))get_ref;
579 this->public.destroy = (void (*) (policy_t*))destroy;
580
581 /* apply init values */
582 this->name = strdup(name);
583 this->my_id = my_id;
584 this->other_id = other_id;
585 this->my_virtual_ip = my_virtual_ip;
586 this->other_virtual_ip = other_virtual_ip;
587 this->auth_method = auth_method;
588 this->eap_type = eap_type;
589 this->hard_lifetime = hard_lifetime;
590 this->soft_lifetime = soft_lifetime;
591 this->jitter = jitter;
592 this->updown = (updown == NULL) ? NULL : strdup(updown);
593 this->hostaccess = hostaccess;
594 this->dpd_action = dpd_action;
595 this->mode = mode;
596
597 /* initialize private members*/
598 this->refcount = 1;
599 this->my_ca = NULL;
600 this->other_ca = NULL;
601 this->proposals = linked_list_create();
602 this->my_ts = linked_list_create();
603 this->other_ts = linked_list_create();
604
605 return &this->public;
606 }