Mark CHILD_SAs used for trap policies to uninstall them properly.
[strongswan.git] / src / libcharon / sa / child_sa.c
1 /*
2 * Copyright (C) 2006-2011 Tobias Brunner
3 * Copyright (C) 2005-2008 Martin Willi
4 * Copyright (C) 2006 Daniel Roethlisberger
5 * Copyright (C) 2005 Jan Hutter
6 * Hochschule fuer Technik Rapperswil
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 * for more details.
17 */
18
19 #define _GNU_SOURCE
20 #include "child_sa.h"
21
22 #include <stdio.h>
23 #include <string.h>
24 #include <time.h>
25
26 #include <hydra.h>
27 #include <daemon.h>
28
29 ENUM(child_sa_state_names, CHILD_CREATED, CHILD_DESTROYING,
30 "CREATED",
31 "ROUTED",
32 "INSTALLING",
33 "INSTALLED",
34 "UPDATING",
35 "REKEYING",
36 "DELETING",
37 "DESTROYING",
38 );
39
40 typedef struct private_child_sa_t private_child_sa_t;
41
42 /**
43 * Private data of a child_sa_t object.
44 */
45 struct private_child_sa_t {
46 /**
47 * Public interface of child_sa_t.
48 */
49 child_sa_t public;
50
51 /**
52 * address of us
53 */
54 host_t *my_addr;
55
56 /**
57 * address of remote
58 */
59 host_t *other_addr;
60
61 /**
62 * our actually used SPI, 0 if unused
63 */
64 u_int32_t my_spi;
65
66 /**
67 * others used SPI, 0 if unused
68 */
69 u_int32_t other_spi;
70
71 /**
72 * our Compression Parameter Index (CPI) used, 0 if unused
73 */
74 u_int16_t my_cpi;
75
76 /**
77 * others Compression Parameter Index (CPI) used, 0 if unused
78 */
79 u_int16_t other_cpi;
80
81 /**
82 * List for local traffic selectors
83 */
84 linked_list_t *my_ts;
85
86 /**
87 * List for remote traffic selectors
88 */
89 linked_list_t *other_ts;
90
91 /**
92 * Protocol used to protect this SA, ESP|AH
93 */
94 protocol_id_t protocol;
95
96 /**
97 * reqid used for this child_sa
98 */
99 u_int32_t reqid;
100
101 /**
102 * inbound mark used for this child_sa
103 */
104 mark_t mark_in;
105
106 /**
107 * outbound mark used for this child_sa
108 */
109 mark_t mark_out;
110
111 /**
112 * absolute time when rekeying is scheduled
113 */
114 time_t rekey_time;
115
116 /**
117 * absolute time when the SA expires
118 */
119 time_t expire_time;
120
121 /**
122 * state of the CHILD_SA
123 */
124 child_sa_state_t state;
125
126 /**
127 * TRUE if this CHILD_SA is used to install trap policies
128 */
129 bool trap;
130
131 /**
132 * Specifies if UDP encapsulation is enabled (NAT traversal)
133 */
134 bool encap;
135
136 /**
137 * Specifies the IPComp transform used (IPCOMP_NONE if disabled)
138 */
139 ipcomp_transform_t ipcomp;
140
141 /**
142 * mode this SA uses, tunnel/transport
143 */
144 ipsec_mode_t mode;
145
146 /**
147 * Action to enforce if peer closes the CHILD_SA
148 */
149 action_t close_action;
150
151 /**
152 * Action to enforce if peer is considered dead
153 */
154 action_t dpd_action;
155
156 /**
157 * selected proposal
158 */
159 proposal_t *proposal;
160
161 /**
162 * config used to create this child
163 */
164 child_cfg_t *config;
165
166 /**
167 * time of last use in seconds (inbound)
168 */
169 u_int32_t my_usetime;
170
171 /**
172 * time of last use in seconds (outbound)
173 */
174 u_int32_t other_usetime;
175
176 /**
177 * last number of inbound bytes
178 */
179 u_int64_t my_usebytes;
180
181 /**
182 * last number of outbound bytes
183 */
184 u_int64_t other_usebytes;
185 };
186
187 /**
188 * convert an IKEv2 specific protocol identifier to the IP protocol identifier.
189 */
190 static inline u_int8_t proto_ike2ip(protocol_id_t protocol)
191 {
192 switch (protocol)
193 {
194 case PROTO_ESP:
195 return IPPROTO_ESP;
196 case PROTO_AH:
197 return IPPROTO_AH;
198 default:
199 return protocol;
200 }
201 }
202
203 METHOD(child_sa_t, get_name, char*,
204 private_child_sa_t *this)
205 {
206 return this->config->get_name(this->config);
207 }
208
209 METHOD(child_sa_t, get_reqid, u_int32_t,
210 private_child_sa_t *this)
211 {
212 return this->reqid;
213 }
214
215 METHOD(child_sa_t, get_config, child_cfg_t*,
216 private_child_sa_t *this)
217 {
218 return this->config;
219 }
220
221 METHOD(child_sa_t, set_state, void,
222 private_child_sa_t *this, child_sa_state_t state)
223 {
224 charon->bus->child_state_change(charon->bus, &this->public, state);
225 this->state = state;
226 }
227
228 METHOD(child_sa_t, get_state, child_sa_state_t,
229 private_child_sa_t *this)
230 {
231 return this->state;
232 }
233
234 METHOD(child_sa_t, get_spi, u_int32_t,
235 private_child_sa_t *this, bool inbound)
236 {
237 return inbound ? this->my_spi : this->other_spi;
238 }
239
240 METHOD(child_sa_t, get_cpi, u_int16_t,
241 private_child_sa_t *this, bool inbound)
242 {
243 return inbound ? this->my_cpi : this->other_cpi;
244 }
245
246 METHOD(child_sa_t, get_protocol, protocol_id_t,
247 private_child_sa_t *this)
248 {
249 return this->protocol;
250 }
251
252 METHOD(child_sa_t, set_protocol, void,
253 private_child_sa_t *this, protocol_id_t protocol)
254 {
255 this->protocol = protocol;
256 }
257
258 METHOD(child_sa_t, get_mode, ipsec_mode_t,
259 private_child_sa_t *this)
260 {
261 return this->mode;
262 }
263
264 METHOD(child_sa_t, set_mode, void,
265 private_child_sa_t *this, ipsec_mode_t mode)
266 {
267 this->mode = mode;
268 }
269
270 METHOD(child_sa_t, has_encap, bool,
271 private_child_sa_t *this)
272 {
273 return this->encap;
274 }
275
276 METHOD(child_sa_t, get_ipcomp, ipcomp_transform_t,
277 private_child_sa_t *this)
278 {
279 return this->ipcomp;
280 }
281
282 METHOD(child_sa_t, set_ipcomp, void,
283 private_child_sa_t *this, ipcomp_transform_t ipcomp)
284 {
285 this->ipcomp = ipcomp;
286 }
287
288 METHOD(child_sa_t, set_close_action, void,
289 private_child_sa_t *this, action_t action)
290 {
291 this->close_action = action;
292 }
293
294 METHOD(child_sa_t, get_close_action, action_t,
295 private_child_sa_t *this)
296 {
297 return this->close_action;
298 }
299
300 METHOD(child_sa_t, set_dpd_action, void,
301 private_child_sa_t *this, action_t action)
302 {
303 this->dpd_action = action;
304 }
305
306 METHOD(child_sa_t, get_dpd_action, action_t,
307 private_child_sa_t *this)
308 {
309 return this->dpd_action;
310 }
311
312 METHOD(child_sa_t, get_proposal, proposal_t*,
313 private_child_sa_t *this)
314 {
315 return this->proposal;
316 }
317
318 METHOD(child_sa_t, set_proposal, void,
319 private_child_sa_t *this, proposal_t *proposal)
320 {
321 this->proposal = proposal->clone(proposal);
322 }
323
324 METHOD(child_sa_t, get_traffic_selectors, linked_list_t*,
325 private_child_sa_t *this, bool local)
326 {
327 return local ? this->my_ts : this->other_ts;
328 }
329
330 typedef struct policy_enumerator_t policy_enumerator_t;
331
332 /**
333 * Private policy enumerator
334 */
335 struct policy_enumerator_t {
336 /** implements enumerator_t */
337 enumerator_t public;
338 /** enumerator over own TS */
339 enumerator_t *mine;
340 /** enumerator over others TS */
341 enumerator_t *other;
342 /** list of others TS, to recreate enumerator */
343 linked_list_t *list;
344 /** currently enumerating TS for "me" side */
345 traffic_selector_t *ts;
346 };
347
348 METHOD(enumerator_t, policy_enumerate, bool,
349 policy_enumerator_t *this, traffic_selector_t **my_out,
350 traffic_selector_t **other_out)
351 {
352 traffic_selector_t *other_ts;
353
354 while (this->ts || this->mine->enumerate(this->mine, &this->ts))
355 {
356 if (!this->other->enumerate(this->other, &other_ts))
357 { /* end of others list, restart with new of mine */
358 this->other->destroy(this->other);
359 this->other = this->list->create_enumerator(this->list);
360 this->ts = NULL;
361 continue;
362 }
363 if (this->ts->get_type(this->ts) != other_ts->get_type(other_ts))
364 { /* family mismatch */
365 continue;
366 }
367 if (this->ts->get_protocol(this->ts) &&
368 other_ts->get_protocol(other_ts) &&
369 this->ts->get_protocol(this->ts) != other_ts->get_protocol(other_ts))
370 { /* protocol mismatch */
371 continue;
372 }
373 *my_out = this->ts;
374 *other_out = other_ts;
375 return TRUE;
376 }
377 return FALSE;
378 }
379
380 METHOD(enumerator_t, policy_destroy, void,
381 policy_enumerator_t *this)
382 {
383 this->mine->destroy(this->mine);
384 this->other->destroy(this->other);
385 free(this);
386 }
387
388 METHOD(child_sa_t, create_policy_enumerator, enumerator_t*,
389 private_child_sa_t *this)
390 {
391 policy_enumerator_t *e;
392
393 INIT(e,
394 .public = {
395 .enumerate = (void*)_policy_enumerate,
396 .destroy = _policy_destroy,
397 },
398 .mine = this->my_ts->create_enumerator(this->my_ts),
399 .other = this->other_ts->create_enumerator(this->other_ts),
400 .list = this->other_ts,
401 .ts = NULL,
402 );
403
404 return &e->public;
405 }
406
407 /**
408 * update the cached usebytes
409 * returns SUCCESS if the usebytes have changed, FAILED if not or no SPIs
410 * are available, and NOT_SUPPORTED if the kernel interface does not support
411 * querying the usebytes.
412 */
413 static status_t update_usebytes(private_child_sa_t *this, bool inbound)
414 {
415 status_t status = FAILED;
416 u_int64_t bytes;
417
418 if (inbound)
419 {
420 if (this->my_spi)
421 {
422 status = hydra->kernel_interface->query_sa(hydra->kernel_interface,
423 this->other_addr, this->my_addr, this->my_spi,
424 proto_ike2ip(this->protocol), this->mark_in,
425 &bytes);
426 if (status == SUCCESS)
427 {
428 if (bytes > this->my_usebytes)
429 {
430 this->my_usebytes = bytes;
431 return SUCCESS;
432 }
433 return FAILED;
434 }
435 }
436 }
437 else
438 {
439 if (this->other_spi)
440 {
441 status = hydra->kernel_interface->query_sa(hydra->kernel_interface,
442 this->my_addr, this->other_addr, this->other_spi,
443 proto_ike2ip(this->protocol), this->mark_out,
444 &bytes);
445 if (status == SUCCESS)
446 {
447 if (bytes > this->other_usebytes)
448 {
449 this->other_usebytes = bytes;
450 return SUCCESS;
451 }
452 return FAILED;
453 }
454 }
455 }
456 return status;
457 }
458
459 /**
460 * updates the cached usetime
461 */
462 static void update_usetime(private_child_sa_t *this, bool inbound)
463 {
464 enumerator_t *enumerator;
465 traffic_selector_t *my_ts, *other_ts;
466 u_int32_t last_use = 0;
467
468 enumerator = create_policy_enumerator(this);
469 while (enumerator->enumerate(enumerator, &my_ts, &other_ts))
470 {
471 u_int32_t in, out, fwd;
472
473 if (inbound)
474 {
475 if (hydra->kernel_interface->query_policy(hydra->kernel_interface,
476 other_ts, my_ts, POLICY_IN, this->mark_in, &in) == SUCCESS)
477 {
478 last_use = max(last_use, in);
479 }
480 if (this->mode != MODE_TRANSPORT)
481 {
482 if (hydra->kernel_interface->query_policy(hydra->kernel_interface,
483 other_ts, my_ts, POLICY_FWD, this->mark_in, &fwd) == SUCCESS)
484 {
485 last_use = max(last_use, fwd);
486 }
487 }
488 }
489 else
490 {
491 if (hydra->kernel_interface->query_policy(hydra->kernel_interface,
492 my_ts, other_ts, POLICY_OUT, this->mark_out, &out) == SUCCESS)
493 {
494 last_use = max(last_use, out);
495 }
496 }
497 }
498 enumerator->destroy(enumerator);
499
500 if (last_use == 0)
501 {
502 return;
503 }
504 if (inbound)
505 {
506 this->my_usetime = last_use;
507 }
508 else
509 {
510 this->other_usetime = last_use;
511 }
512 }
513
514 METHOD(child_sa_t, get_usestats, void,
515 private_child_sa_t *this, bool inbound, time_t *time, u_int64_t *bytes)
516 {
517 if (update_usebytes(this, inbound) != FAILED)
518 {
519 /* there was traffic since last update or the kernel interface
520 * does not support querying the number of usebytes.
521 */
522 update_usetime(this, inbound);
523 }
524 if (time)
525 {
526 *time = inbound ? this->my_usetime : this->other_usetime;
527 }
528 if (bytes)
529 {
530 *bytes = inbound ? this->my_usebytes : this->other_usebytes;
531 }
532 }
533
534 METHOD(child_sa_t, get_mark, mark_t,
535 private_child_sa_t *this, bool inbound)
536 {
537 if (inbound)
538 {
539 return this->mark_in;
540 }
541 return this->mark_out;
542 }
543
544 METHOD(child_sa_t, get_lifetime, time_t,
545 private_child_sa_t *this, bool hard)
546 {
547 return hard ? this->expire_time : this->rekey_time;
548 }
549
550 METHOD(child_sa_t, alloc_spi, u_int32_t,
551 private_child_sa_t *this, protocol_id_t protocol)
552 {
553 if (hydra->kernel_interface->get_spi(hydra->kernel_interface,
554 this->other_addr, this->my_addr,
555 proto_ike2ip(protocol), this->reqid,
556 &this->my_spi) == SUCCESS)
557 {
558 return this->my_spi;
559 }
560 return 0;
561 }
562
563 METHOD(child_sa_t, alloc_cpi, u_int16_t,
564 private_child_sa_t *this)
565 {
566 if (hydra->kernel_interface->get_cpi(hydra->kernel_interface,
567 this->other_addr, this->my_addr,
568 this->reqid, &this->my_cpi) == SUCCESS)
569 {
570 return this->my_cpi;
571 }
572 return 0;
573 }
574
575 METHOD(child_sa_t, install, status_t,
576 private_child_sa_t *this, chunk_t encr, chunk_t integ, u_int32_t spi,
577 u_int16_t cpi, bool inbound, bool tfcv3, linked_list_t *my_ts,
578 linked_list_t *other_ts)
579 {
580 u_int16_t enc_alg = ENCR_UNDEFINED, int_alg = AUTH_UNDEFINED, size;
581 u_int16_t esn = NO_EXT_SEQ_NUMBERS;
582 traffic_selector_t *src_ts = NULL, *dst_ts = NULL;
583 time_t now;
584 lifetime_cfg_t *lifetime;
585 u_int32_t tfc = 0;
586 host_t *src, *dst;
587 status_t status;
588 bool update = FALSE;
589
590 /* now we have to decide which spi to use. Use self allocated, if "in",
591 * or the one in the proposal, if not "in" (others). Additionally,
592 * source and dest host switch depending on the role */
593 if (inbound)
594 {
595 dst = this->my_addr;
596 src = this->other_addr;
597 if (this->my_spi == spi)
598 { /* alloc_spi has been called, do an SA update */
599 update = TRUE;
600 }
601 this->my_spi = spi;
602 this->my_cpi = cpi;
603 }
604 else
605 {
606 src = this->my_addr;
607 dst = this->other_addr;
608 this->other_spi = spi;
609 this->other_cpi = cpi;
610
611 if (tfcv3)
612 {
613 tfc = this->config->get_tfc(this->config);
614 }
615 }
616
617 DBG2(DBG_CHD, "adding %s %N SA", inbound ? "inbound" : "outbound",
618 protocol_id_names, this->protocol);
619
620 /* send SA down to the kernel */
621 DBG2(DBG_CHD, " SPI 0x%.8x, src %H dst %H", ntohl(spi), src, dst);
622
623 this->proposal->get_algorithm(this->proposal, ENCRYPTION_ALGORITHM,
624 &enc_alg, &size);
625 this->proposal->get_algorithm(this->proposal, INTEGRITY_ALGORITHM,
626 &int_alg, &size);
627 this->proposal->get_algorithm(this->proposal, EXTENDED_SEQUENCE_NUMBERS,
628 &esn, NULL);
629
630 lifetime = this->config->get_lifetime(this->config);
631
632 now = time_monotonic(NULL);
633 if (lifetime->time.rekey)
634 {
635 this->rekey_time = now + lifetime->time.rekey;
636 }
637 if (lifetime->time.life)
638 {
639 this->expire_time = now + lifetime->time.life;
640 }
641
642 if (!lifetime->time.jitter && !inbound)
643 { /* avoid triggering multiple rekey events */
644 lifetime->time.rekey = 0;
645 }
646
647 if (this->mode == MODE_BEET || this->mode == MODE_TRANSPORT)
648 {
649 /* BEET requires the bound address from the traffic selectors.
650 * TODO: We add just the first traffic selector for now, as the
651 * kernel accepts a single TS per SA only */
652 if (inbound)
653 {
654 my_ts->get_first(my_ts, (void**)&dst_ts);
655 other_ts->get_first(other_ts, (void**)&src_ts);
656 }
657 else
658 {
659 my_ts->get_first(my_ts, (void**)&src_ts);
660 other_ts->get_first(other_ts, (void**)&dst_ts);
661 }
662 }
663
664 status = hydra->kernel_interface->add_sa(hydra->kernel_interface,
665 src, dst, spi, proto_ike2ip(this->protocol), this->reqid,
666 inbound ? this->mark_in : this->mark_out, tfc,
667 lifetime, enc_alg, encr, int_alg, integ, this->mode,
668 this->ipcomp, cpi, this->encap, esn, update, src_ts, dst_ts);
669
670 free(lifetime);
671
672 return status;
673 }
674
675 /**
676 * Install 3 policies: out, in and forward
677 */
678 static status_t install_policies_internal(private_child_sa_t *this,
679 host_t *my_addr, host_t *other_addr, traffic_selector_t *my_ts,
680 traffic_selector_t *other_ts, ipsec_sa_cfg_t *my_sa,
681 ipsec_sa_cfg_t *other_sa, policy_type_t type, policy_priority_t priority)
682 {
683 status_t status = SUCCESS;
684 status |= hydra->kernel_interface->add_policy(hydra->kernel_interface,
685 my_addr, other_addr, my_ts, other_ts,
686 POLICY_OUT, type, other_sa,
687 this->mark_out, priority);
688
689 status |= hydra->kernel_interface->add_policy(hydra->kernel_interface,
690 other_addr, my_addr, other_ts, my_ts,
691 POLICY_IN, type, my_sa,
692 this->mark_in, priority);
693 if (this->mode != MODE_TRANSPORT)
694 {
695 status |= hydra->kernel_interface->add_policy(hydra->kernel_interface,
696 other_addr, my_addr, other_ts, my_ts,
697 POLICY_FWD, type, my_sa,
698 this->mark_in, priority);
699 }
700 return status;
701 }
702
703 /**
704 * Delete 3 policies: out, in and forward
705 */
706 static void del_policies_internal(private_child_sa_t *this,
707 traffic_selector_t *my_ts, traffic_selector_t *other_ts,
708 policy_priority_t priority)
709 {
710 hydra->kernel_interface->del_policy(hydra->kernel_interface,
711 my_ts, other_ts, POLICY_OUT, this->reqid,
712 this->mark_out, priority);
713 hydra->kernel_interface->del_policy(hydra->kernel_interface,
714 other_ts, my_ts, POLICY_IN, this->reqid,
715 this->mark_in, priority);
716 if (this->mode != MODE_TRANSPORT)
717 {
718 hydra->kernel_interface->del_policy(hydra->kernel_interface,
719 other_ts, my_ts, POLICY_FWD, this->reqid,
720 this->mark_in, priority);
721 }
722 }
723
724 METHOD(child_sa_t, add_policies, status_t,
725 private_child_sa_t *this, linked_list_t *my_ts_list,
726 linked_list_t *other_ts_list)
727 {
728 enumerator_t *enumerator;
729 traffic_selector_t *my_ts, *other_ts;
730 status_t status = SUCCESS;
731
732 /* apply traffic selectors */
733 enumerator = my_ts_list->create_enumerator(my_ts_list);
734 while (enumerator->enumerate(enumerator, &my_ts))
735 {
736 this->my_ts->insert_last(this->my_ts, my_ts->clone(my_ts));
737 }
738 enumerator->destroy(enumerator);
739 enumerator = other_ts_list->create_enumerator(other_ts_list);
740 while (enumerator->enumerate(enumerator, &other_ts))
741 {
742 this->other_ts->insert_last(this->other_ts, other_ts->clone(other_ts));
743 }
744 enumerator->destroy(enumerator);
745
746 if (this->config->install_policy(this->config))
747 {
748 policy_priority_t priority;
749 ipsec_sa_cfg_t my_sa = {
750 .mode = this->mode,
751 .reqid = this->reqid,
752 .ipcomp = {
753 .transform = this->ipcomp,
754 },
755 }, other_sa = my_sa;
756
757 my_sa.ipcomp.cpi = this->my_cpi;
758 other_sa.ipcomp.cpi = this->other_cpi;
759
760 if (this->protocol == PROTO_ESP)
761 {
762 my_sa.esp.use = TRUE;
763 my_sa.esp.spi = this->my_spi;
764 other_sa.esp.use = TRUE;
765 other_sa.esp.spi = this->other_spi;
766 }
767 else
768 {
769 my_sa.ah.use = TRUE;
770 my_sa.ah.spi = this->my_spi;
771 other_sa.ah.use = TRUE;
772 other_sa.ah.spi = this->other_spi;
773 }
774
775 /* if we're not in state CHILD_INSTALLING (i.e. if there is no SAD
776 * entry) we install a trap policy */
777 this->trap = this->state == CHILD_CREATED;
778 priority = this->trap ? POLICY_PRIORITY_ROUTED
779 : POLICY_PRIORITY_DEFAULT;
780
781 /* enumerate pairs of traffic selectors */
782 enumerator = create_policy_enumerator(this);
783 while (enumerator->enumerate(enumerator, &my_ts, &other_ts))
784 {
785 /* install outbound drop policy to avoid packets leaving unencrypted
786 * when updating policies */
787 if (priority == POLICY_PRIORITY_DEFAULT)
788 {
789 status |= install_policies_internal(this, this->my_addr,
790 this->other_addr, my_ts, other_ts,
791 &my_sa, &other_sa, POLICY_DROP,
792 POLICY_PRIORITY_FALLBACK);
793 }
794
795 /* install policies */
796 status |= install_policies_internal(this, this->my_addr,
797 this->other_addr, my_ts, other_ts,
798 &my_sa, &other_sa, POLICY_IPSEC, priority);
799
800 if (status != SUCCESS)
801 {
802 break;
803 }
804 }
805 enumerator->destroy(enumerator);
806 }
807
808 if (status == SUCCESS && this->trap)
809 {
810 set_state(this, CHILD_ROUTED);
811 }
812 return status;
813 }
814
815 METHOD(child_sa_t, update, status_t,
816 private_child_sa_t *this, host_t *me, host_t *other, host_t *vip,
817 bool encap)
818 {
819 child_sa_state_t old;
820 bool transport_proxy_mode;
821
822 /* anything changed at all? */
823 if (me->equals(me, this->my_addr) &&
824 other->equals(other, this->other_addr) && this->encap == encap)
825 {
826 return SUCCESS;
827 }
828
829 old = this->state;
830 set_state(this, CHILD_UPDATING);
831 transport_proxy_mode = this->config->use_proxy_mode(this->config) &&
832 this->mode == MODE_TRANSPORT;
833
834 if (!transport_proxy_mode)
835 {
836 /* update our (initiator) SA */
837 if (this->my_spi)
838 {
839 if (hydra->kernel_interface->update_sa(hydra->kernel_interface,
840 this->my_spi, proto_ike2ip(this->protocol),
841 this->ipcomp != IPCOMP_NONE ? this->my_cpi : 0,
842 this->other_addr, this->my_addr, other, me,
843 this->encap, encap, this->mark_in) == NOT_SUPPORTED)
844 {
845 return NOT_SUPPORTED;
846 }
847 }
848
849 /* update his (responder) SA */
850 if (this->other_spi)
851 {
852 if (hydra->kernel_interface->update_sa(hydra->kernel_interface,
853 this->other_spi, proto_ike2ip(this->protocol),
854 this->ipcomp != IPCOMP_NONE ? this->other_cpi : 0,
855 this->my_addr, this->other_addr, me, other,
856 this->encap, encap, this->mark_out) == NOT_SUPPORTED)
857 {
858 return NOT_SUPPORTED;
859 }
860 }
861 }
862
863 if (this->config->install_policy(this->config))
864 {
865 ipsec_sa_cfg_t my_sa = {
866 .mode = this->mode,
867 .reqid = this->reqid,
868 .ipcomp = {
869 .transform = this->ipcomp,
870 },
871 }, other_sa = my_sa;
872
873 my_sa.ipcomp.cpi = this->my_cpi;
874 other_sa.ipcomp.cpi = this->other_cpi;
875
876 if (this->protocol == PROTO_ESP)
877 {
878 my_sa.esp.use = TRUE;
879 my_sa.esp.spi = this->my_spi;
880 other_sa.esp.use = TRUE;
881 other_sa.esp.spi = this->other_spi;
882 }
883 else
884 {
885 my_sa.ah.use = TRUE;
886 my_sa.ah.spi = this->my_spi;
887 other_sa.ah.use = TRUE;
888 other_sa.ah.spi = this->other_spi;
889 }
890
891 /* update policies */
892 if (!me->ip_equals(me, this->my_addr) ||
893 !other->ip_equals(other, this->other_addr))
894 {
895 enumerator_t *enumerator;
896 traffic_selector_t *my_ts, *other_ts;
897
898 /* always use high priorities, as hosts getting updated are INSTALLED */
899 enumerator = create_policy_enumerator(this);
900 while (enumerator->enumerate(enumerator, &my_ts, &other_ts))
901 {
902 traffic_selector_t *old_my_ts = NULL, *old_other_ts = NULL;
903 /* remove old policies first */
904 del_policies_internal(this, my_ts, other_ts,
905 POLICY_PRIORITY_DEFAULT);
906
907 /* check if we have to update a "dynamic" traffic selector */
908 if (!me->ip_equals(me, this->my_addr) &&
909 my_ts->is_host(my_ts, this->my_addr))
910 {
911 old_my_ts = my_ts->clone(my_ts);
912 my_ts->set_address(my_ts, me);
913 }
914 if (!other->ip_equals(other, this->other_addr) &&
915 other_ts->is_host(other_ts, this->other_addr))
916 {
917 old_other_ts = other_ts->clone(other_ts);
918 other_ts->set_address(other_ts, other);
919 }
920
921 /* we reinstall the virtual IP to handle interface roaming
922 * correctly */
923 if (vip)
924 {
925 hydra->kernel_interface->del_ip(hydra->kernel_interface,
926 vip);
927 hydra->kernel_interface->add_ip(hydra->kernel_interface,
928 vip, me);
929 }
930
931 /* reinstall updated policies */
932 install_policies_internal(this, me, other, my_ts, other_ts,
933 &my_sa, &other_sa, POLICY_IPSEC,
934 POLICY_PRIORITY_DEFAULT);
935
936 /* update fallback policies after the new policy is in place */
937 if (old_my_ts || old_other_ts)
938 {
939 del_policies_internal(this, old_my_ts ?: my_ts,
940 old_other_ts ?: other_ts,
941 POLICY_PRIORITY_FALLBACK);
942 install_policies_internal(this, me, other, my_ts, other_ts,
943 &my_sa, &other_sa, POLICY_DROP,
944 POLICY_PRIORITY_FALLBACK);
945 DESTROY_IF(old_my_ts);
946 DESTROY_IF(old_other_ts);
947 }
948 }
949 enumerator->destroy(enumerator);
950 }
951 }
952
953 if (!transport_proxy_mode)
954 {
955 /* apply hosts */
956 if (!me->equals(me, this->my_addr))
957 {
958 this->my_addr->destroy(this->my_addr);
959 this->my_addr = me->clone(me);
960 }
961 if (!other->equals(other, this->other_addr))
962 {
963 this->other_addr->destroy(this->other_addr);
964 this->other_addr = other->clone(other);
965 }
966 }
967
968 this->encap = encap;
969 set_state(this, old);
970
971 return SUCCESS;
972 }
973
974 METHOD(child_sa_t, destroy, void,
975 private_child_sa_t *this)
976 {
977 enumerator_t *enumerator;
978 traffic_selector_t *my_ts, *other_ts;
979 policy_priority_t priority;
980
981 priority = this->trap ? POLICY_PRIORITY_ROUTED : POLICY_PRIORITY_DEFAULT;
982
983 set_state(this, CHILD_DESTROYING);
984
985 /* delete SAs in the kernel, if they are set up */
986 if (this->my_spi)
987 {
988 /* if CHILD was not established, use PROTO_ESP used during alloc_spi().
989 * TODO: For AH support, we have to store protocol specific SPI.s */
990 if (this->protocol == PROTO_NONE)
991 {
992 this->protocol = PROTO_ESP;
993 }
994 hydra->kernel_interface->del_sa(hydra->kernel_interface,
995 this->other_addr, this->my_addr, this->my_spi,
996 proto_ike2ip(this->protocol), this->my_cpi,
997 this->mark_in);
998 }
999 if (this->other_spi)
1000 {
1001 hydra->kernel_interface->del_sa(hydra->kernel_interface,
1002 this->my_addr, this->other_addr, this->other_spi,
1003 proto_ike2ip(this->protocol), this->other_cpi,
1004 this->mark_out);
1005 }
1006
1007 if (this->config->install_policy(this->config))
1008 {
1009 /* delete all policies in the kernel */
1010 enumerator = create_policy_enumerator(this);
1011 while (enumerator->enumerate(enumerator, &my_ts, &other_ts))
1012 {
1013 del_policies_internal(this, my_ts, other_ts, priority);
1014 if (priority == POLICY_PRIORITY_DEFAULT)
1015 {
1016 del_policies_internal(this, my_ts, other_ts,
1017 POLICY_PRIORITY_FALLBACK);
1018 }
1019 }
1020 enumerator->destroy(enumerator);
1021 }
1022
1023 this->my_ts->destroy_offset(this->my_ts, offsetof(traffic_selector_t, destroy));
1024 this->other_ts->destroy_offset(this->other_ts, offsetof(traffic_selector_t, destroy));
1025 this->my_addr->destroy(this->my_addr);
1026 this->other_addr->destroy(this->other_addr);
1027 DESTROY_IF(this->proposal);
1028 this->config->destroy(this->config);
1029 free(this);
1030 }
1031
1032 /**
1033 * Described in header.
1034 */
1035 child_sa_t * child_sa_create(host_t *me, host_t* other,
1036 child_cfg_t *config, u_int32_t rekey, bool encap)
1037 {
1038 static u_int32_t reqid = 0;
1039 private_child_sa_t *this;
1040
1041 INIT(this,
1042 .public = {
1043 .get_name = _get_name,
1044 .get_reqid = _get_reqid,
1045 .get_config = _get_config,
1046 .get_state = _get_state,
1047 .set_state = _set_state,
1048 .get_spi = _get_spi,
1049 .get_cpi = _get_cpi,
1050 .get_protocol = _get_protocol,
1051 .set_protocol = _set_protocol,
1052 .get_mode = _get_mode,
1053 .set_mode = _set_mode,
1054 .get_proposal = _get_proposal,
1055 .set_proposal = _set_proposal,
1056 .get_lifetime = _get_lifetime,
1057 .get_usestats = _get_usestats,
1058 .get_mark = _get_mark,
1059 .has_encap = _has_encap,
1060 .get_ipcomp = _get_ipcomp,
1061 .set_ipcomp = _set_ipcomp,
1062 .get_close_action = _get_close_action,
1063 .set_close_action = _set_close_action,
1064 .get_dpd_action = _get_dpd_action,
1065 .set_dpd_action = _set_dpd_action,
1066 .alloc_spi = _alloc_spi,
1067 .alloc_cpi = _alloc_cpi,
1068 .install = _install,
1069 .update = _update,
1070 .add_policies = _add_policies,
1071 .get_traffic_selectors = _get_traffic_selectors,
1072 .create_policy_enumerator = _create_policy_enumerator,
1073 .destroy = _destroy,
1074 },
1075 .my_addr = me->clone(me),
1076 .other_addr = other->clone(other),
1077 .encap = encap,
1078 .ipcomp = IPCOMP_NONE,
1079 .state = CHILD_CREATED,
1080 .my_ts = linked_list_create(),
1081 .other_ts = linked_list_create(),
1082 .protocol = PROTO_NONE,
1083 .mode = MODE_TUNNEL,
1084 .close_action = config->get_close_action(config),
1085 .dpd_action = config->get_dpd_action(config),
1086 .reqid = config->get_reqid(config),
1087 .mark_in = config->get_mark(config, TRUE),
1088 .mark_out = config->get_mark(config, FALSE),
1089 );
1090
1091 this->config = config;
1092 config->get_ref(config);
1093
1094 if (!this->reqid)
1095 {
1096 /* reuse old reqid if we are rekeying an existing CHILD_SA */
1097 this->reqid = rekey ? rekey : ++reqid;
1098 }
1099
1100 if (this->mark_in.value == MARK_REQID)
1101 {
1102 this->mark_in.value = this->reqid;
1103 }
1104 if (this->mark_out.value == MARK_REQID)
1105 {
1106 this->mark_out.value = this->reqid;
1107 }
1108
1109 /* MIPv6 proxy transport mode sets SA endpoints to TS hosts */
1110 if (config->get_mode(config) == MODE_TRANSPORT &&
1111 config->use_proxy_mode(config))
1112 {
1113 ts_type_t type;
1114 int family;
1115 chunk_t addr;
1116 host_t *host;
1117 enumerator_t *enumerator;
1118 linked_list_t *my_ts_list, *other_ts_list;
1119 traffic_selector_t *my_ts, *other_ts;
1120
1121 this->mode = MODE_TRANSPORT;
1122
1123 my_ts_list = config->get_traffic_selectors(config, TRUE, NULL, me);
1124 enumerator = my_ts_list->create_enumerator(my_ts_list);
1125 if (enumerator->enumerate(enumerator, &my_ts))
1126 {
1127 if (my_ts->is_host(my_ts, NULL) &&
1128 !my_ts->is_host(my_ts, this->my_addr))
1129 {
1130 type = my_ts->get_type(my_ts);
1131 family = (type == TS_IPV4_ADDR_RANGE) ? AF_INET : AF_INET6;
1132 addr = my_ts->get_from_address(my_ts);
1133 host = host_create_from_chunk(family, addr, 0);
1134 free(addr.ptr);
1135 DBG1(DBG_CHD, "my address: %H is a transport mode proxy for %H",
1136 this->my_addr, host);
1137 this->my_addr->destroy(this->my_addr);
1138 this->my_addr = host;
1139 }
1140 }
1141 enumerator->destroy(enumerator);
1142 my_ts_list->destroy_offset(my_ts_list, offsetof(traffic_selector_t, destroy));
1143
1144 other_ts_list = config->get_traffic_selectors(config, FALSE, NULL, other);
1145 enumerator = other_ts_list->create_enumerator(other_ts_list);
1146 if (enumerator->enumerate(enumerator, &other_ts))
1147 {
1148 if (other_ts->is_host(other_ts, NULL) &&
1149 !other_ts->is_host(other_ts, this->other_addr))
1150 {
1151 type = other_ts->get_type(other_ts);
1152 family = (type == TS_IPV4_ADDR_RANGE) ? AF_INET : AF_INET6;
1153 addr = other_ts->get_from_address(other_ts);
1154 host = host_create_from_chunk(family, addr, 0);
1155 free(addr.ptr);
1156 DBG1(DBG_CHD, "other address: %H is a transport mode proxy for %H",
1157 this->other_addr, host);
1158 this->other_addr->destroy(this->other_addr);
1159 this->other_addr = host;
1160 }
1161 }
1162 enumerator->destroy(enumerator);
1163 other_ts_list->destroy_offset(other_ts_list, offsetof(traffic_selector_t, destroy));
1164 }
1165
1166 return &this->public;
1167 }