ike-sa: Add option to force the destruction of an IKE_SA after initiating a delete
[strongswan.git] / src / libcharon / sa / child_sa.c
1 /*
2 * Copyright (C) 2006-2018 Tobias Brunner
3 * Copyright (C) 2016 Andreas Steffen
4 * Copyright (C) 2005-2008 Martin Willi
5 * Copyright (C) 2006 Daniel Roethlisberger
6 * Copyright (C) 2005 Jan Hutter
7 * HSR Hochschule fuer Technik Rapperswil
8 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 * for more details.
18 */
19
20 #define _GNU_SOURCE
21 #include "child_sa.h"
22
23 #include <stdio.h>
24 #include <string.h>
25 #include <time.h>
26
27 #include <daemon.h>
28 #include <collections/array.h>
29
30 ENUM(child_sa_state_names, CHILD_CREATED, CHILD_DESTROYING,
31 "CREATED",
32 "ROUTED",
33 "INSTALLING",
34 "INSTALLED",
35 "UPDATING",
36 "REKEYING",
37 "REKEYED",
38 "RETRYING",
39 "DELETING",
40 "DELETED",
41 "DESTROYING",
42 );
43
44 ENUM_FLAGS(child_sa_outbound_state_names, CHILD_OUTBOUND_REGISTERED, CHILD_OUTBOUND_POLICIES,
45 "REGISTERED",
46 "SA",
47 "POLICIES",
48 );
49
50 typedef struct private_child_sa_t private_child_sa_t;
51
52 /**
53 * Private data of a child_sa_t object.
54 */
55 struct private_child_sa_t {
56 /**
57 * Public interface of child_sa_t.
58 */
59 child_sa_t public;
60
61 /**
62 * address of us
63 */
64 host_t *my_addr;
65
66 /**
67 * address of remote
68 */
69 host_t *other_addr;
70
71 /**
72 * our actually used SPI, 0 if unused
73 */
74 uint32_t my_spi;
75
76 /**
77 * others used SPI, 0 if unused
78 */
79 uint32_t other_spi;
80
81 /**
82 * our Compression Parameter Index (CPI) used, 0 if unused
83 */
84 uint16_t my_cpi;
85
86 /**
87 * others Compression Parameter Index (CPI) used, 0 if unused
88 */
89 uint16_t other_cpi;
90
91 /**
92 * Array for local traffic selectors
93 */
94 array_t *my_ts;
95
96 /**
97 * Array for remote traffic selectors
98 */
99 array_t *other_ts;
100
101 /**
102 * Outbound encryption key cached during a rekeying
103 */
104 chunk_t encr_r;
105
106 /**
107 * Outbound integrity key cached during a rekeying
108 */
109 chunk_t integ_r;
110
111 /**
112 * Whether the outbound SA has only been registered yet during a rekeying
113 */
114 child_sa_outbound_state_t outbound_state;
115
116 /**
117 * Whether the peer supports TFCv3
118 */
119 bool tfcv3;
120
121 /**
122 * The outbound SPI of the CHILD_SA that replaced this one during a rekeying
123 */
124 uint32_t rekey_spi;
125
126 /**
127 * Protocol used to protect this SA, ESP|AH
128 */
129 protocol_id_t protocol;
130
131 /**
132 * reqid used for this child_sa
133 */
134 uint32_t reqid;
135
136 /**
137 * Did we allocate/confirm and must release the reqid?
138 */
139 bool reqid_allocated;
140
141 /**
142 * Is the reqid statically configured
143 */
144 bool static_reqid;
145
146 /**
147 * Unique CHILD_SA identifier
148 */
149 uint32_t unique_id;
150
151 /**
152 * Whether FWD policieis in the outbound direction should be installed
153 */
154 bool policies_fwd_out;
155
156 /**
157 * inbound mark used for this child_sa
158 */
159 mark_t mark_in;
160
161 /**
162 * outbound mark used for this child_sa
163 */
164 mark_t mark_out;
165
166 /**
167 * absolute time when rekeying is scheduled
168 */
169 time_t rekey_time;
170
171 /**
172 * absolute time when the SA expires
173 */
174 time_t expire_time;
175
176 /**
177 * absolute time when SA has been installed
178 */
179 time_t install_time;
180
181 /**
182 * state of the CHILD_SA
183 */
184 child_sa_state_t state;
185
186 /**
187 * TRUE if this CHILD_SA is used to install trap policies
188 */
189 bool trap;
190
191 /**
192 * Specifies if UDP encapsulation is enabled (NAT traversal)
193 */
194 bool encap;
195
196 /**
197 * Specifies the IPComp transform used (IPCOMP_NONE if disabled)
198 */
199 ipcomp_transform_t ipcomp;
200
201 /**
202 * mode this SA uses, tunnel/transport
203 */
204 ipsec_mode_t mode;
205
206 /**
207 * Action to enforce if peer closes the CHILD_SA
208 */
209 action_t close_action;
210
211 /**
212 * Action to enforce if peer is considered dead
213 */
214 action_t dpd_action;
215
216 /**
217 * selected proposal
218 */
219 proposal_t *proposal;
220
221 /**
222 * config used to create this child
223 */
224 child_cfg_t *config;
225
226 /**
227 * time of last use in seconds (inbound)
228 */
229 time_t my_usetime;
230
231 /**
232 * time of last use in seconds (outbound)
233 */
234 time_t other_usetime;
235
236 /**
237 * last number of inbound bytes
238 */
239 uint64_t my_usebytes;
240
241 /**
242 * last number of outbound bytes
243 */
244 uint64_t other_usebytes;
245
246 /**
247 * last number of inbound packets
248 */
249 uint64_t my_usepackets;
250
251 /**
252 * last number of outbound bytes
253 */
254 uint64_t other_usepackets;
255 };
256
257 /**
258 * Convert an IKEv2 specific protocol identifier to the IP protocol identifier
259 */
260 static inline uint8_t proto_ike2ip(protocol_id_t protocol)
261 {
262 switch (protocol)
263 {
264 case PROTO_ESP:
265 return IPPROTO_ESP;
266 case PROTO_AH:
267 return IPPROTO_AH;
268 default:
269 return protocol;
270 }
271 }
272
273 /**
274 * Returns the mark to use on the inbound SA
275 */
276 static inline mark_t mark_in_sa(private_child_sa_t *this)
277 {
278 if (this->config->has_option(this->config, OPT_MARK_IN_SA))
279 {
280 return this->mark_in;
281 }
282 return (mark_t){};
283 }
284
285 METHOD(child_sa_t, get_name, char*,
286 private_child_sa_t *this)
287 {
288 return this->config->get_name(this->config);
289 }
290
291 METHOD(child_sa_t, get_reqid, uint32_t,
292 private_child_sa_t *this)
293 {
294 return this->reqid;
295 }
296
297 METHOD(child_sa_t, get_unique_id, uint32_t,
298 private_child_sa_t *this)
299 {
300 return this->unique_id;
301 }
302
303 METHOD(child_sa_t, get_config, child_cfg_t*,
304 private_child_sa_t *this)
305 {
306 return this->config;
307 }
308
309 METHOD(child_sa_t, set_state, void,
310 private_child_sa_t *this, child_sa_state_t state)
311 {
312 if (this->state != state)
313 {
314 DBG2(DBG_CHD, "CHILD_SA %s{%d} state change: %N => %N",
315 get_name(this), this->unique_id,
316 child_sa_state_names, this->state,
317 child_sa_state_names, state);
318 charon->bus->child_state_change(charon->bus, &this->public, state);
319 this->state = state;
320 }
321 }
322
323 METHOD(child_sa_t, get_state, child_sa_state_t,
324 private_child_sa_t *this)
325 {
326 return this->state;
327 }
328
329 METHOD(child_sa_t, get_outbound_state, child_sa_outbound_state_t,
330 private_child_sa_t *this)
331 {
332 return this->outbound_state;
333 }
334
335 METHOD(child_sa_t, get_spi, uint32_t,
336 private_child_sa_t *this, bool inbound)
337 {
338 return inbound ? this->my_spi : this->other_spi;
339 }
340
341 METHOD(child_sa_t, get_cpi, uint16_t,
342 private_child_sa_t *this, bool inbound)
343 {
344 return inbound ? this->my_cpi : this->other_cpi;
345 }
346
347 METHOD(child_sa_t, get_protocol, protocol_id_t,
348 private_child_sa_t *this)
349 {
350 return this->protocol;
351 }
352
353 METHOD(child_sa_t, set_protocol, void,
354 private_child_sa_t *this, protocol_id_t protocol)
355 {
356 this->protocol = protocol;
357 }
358
359 METHOD(child_sa_t, get_mode, ipsec_mode_t,
360 private_child_sa_t *this)
361 {
362 return this->mode;
363 }
364
365 METHOD(child_sa_t, set_mode, void,
366 private_child_sa_t *this, ipsec_mode_t mode)
367 {
368 this->mode = mode;
369 }
370
371 METHOD(child_sa_t, has_encap, bool,
372 private_child_sa_t *this)
373 {
374 return this->encap;
375 }
376
377 METHOD(child_sa_t, get_ipcomp, ipcomp_transform_t,
378 private_child_sa_t *this)
379 {
380 return this->ipcomp;
381 }
382
383 METHOD(child_sa_t, set_ipcomp, void,
384 private_child_sa_t *this, ipcomp_transform_t ipcomp)
385 {
386 this->ipcomp = ipcomp;
387 }
388
389 METHOD(child_sa_t, set_close_action, void,
390 private_child_sa_t *this, action_t action)
391 {
392 this->close_action = action;
393 }
394
395 METHOD(child_sa_t, get_close_action, action_t,
396 private_child_sa_t *this)
397 {
398 return this->close_action;
399 }
400
401 METHOD(child_sa_t, set_dpd_action, void,
402 private_child_sa_t *this, action_t action)
403 {
404 this->dpd_action = action;
405 }
406
407 METHOD(child_sa_t, get_dpd_action, action_t,
408 private_child_sa_t *this)
409 {
410 return this->dpd_action;
411 }
412
413 METHOD(child_sa_t, get_proposal, proposal_t*,
414 private_child_sa_t *this)
415 {
416 return this->proposal;
417 }
418
419 METHOD(child_sa_t, set_proposal, void,
420 private_child_sa_t *this, proposal_t *proposal)
421 {
422 this->proposal = proposal->clone(proposal);
423 }
424
425 METHOD(child_sa_t, create_ts_enumerator, enumerator_t*,
426 private_child_sa_t *this, bool local)
427 {
428 if (local)
429 {
430 return array_create_enumerator(this->my_ts);
431 }
432 return array_create_enumerator(this->other_ts);
433 }
434
435 typedef struct policy_enumerator_t policy_enumerator_t;
436
437 /**
438 * Private policy enumerator
439 */
440 struct policy_enumerator_t {
441 /** implements enumerator_t */
442 enumerator_t public;
443 /** enumerator over own TS */
444 enumerator_t *mine;
445 /** enumerator over others TS */
446 enumerator_t *other;
447 /** array of others TS, to recreate enumerator */
448 array_t *array;
449 /** currently enumerating TS for "me" side */
450 traffic_selector_t *ts;
451 };
452
453 METHOD(enumerator_t, policy_enumerate, bool,
454 policy_enumerator_t *this, va_list args)
455 {
456 traffic_selector_t *other_ts, **my_out, **other_out;
457
458 VA_ARGS_VGET(args, my_out, other_out);
459
460 while (this->ts || this->mine->enumerate(this->mine, &this->ts))
461 {
462 if (!this->other->enumerate(this->other, &other_ts))
463 { /* end of others list, restart with new of mine */
464 this->other->destroy(this->other);
465 this->other = array_create_enumerator(this->array);
466 this->ts = NULL;
467 continue;
468 }
469 if (this->ts->get_type(this->ts) != other_ts->get_type(other_ts))
470 { /* family mismatch */
471 continue;
472 }
473 if (this->ts->get_protocol(this->ts) &&
474 other_ts->get_protocol(other_ts) &&
475 this->ts->get_protocol(this->ts) != other_ts->get_protocol(other_ts))
476 { /* protocol mismatch */
477 continue;
478 }
479 if (my_out)
480 {
481 *my_out = this->ts;
482 }
483 if (other_out)
484 {
485 *other_out = other_ts;
486 }
487 return TRUE;
488 }
489 return FALSE;
490 }
491
492 METHOD(enumerator_t, policy_destroy, void,
493 policy_enumerator_t *this)
494 {
495 this->mine->destroy(this->mine);
496 this->other->destroy(this->other);
497 free(this);
498 }
499
500 METHOD(child_sa_t, create_policy_enumerator, enumerator_t*,
501 private_child_sa_t *this)
502 {
503 policy_enumerator_t *e;
504
505 INIT(e,
506 .public = {
507 .enumerate = enumerator_enumerate_default,
508 .venumerate = _policy_enumerate,
509 .destroy = _policy_destroy,
510 },
511 .mine = array_create_enumerator(this->my_ts),
512 .other = array_create_enumerator(this->other_ts),
513 .array = this->other_ts,
514 .ts = NULL,
515 );
516
517 return &e->public;
518 }
519
520 /**
521 * update the cached usebytes
522 * returns SUCCESS if the usebytes have changed, FAILED if not or no SPIs
523 * are available, and NOT_SUPPORTED if the kernel interface does not support
524 * querying the usebytes.
525 */
526 static status_t update_usebytes(private_child_sa_t *this, bool inbound)
527 {
528 status_t status = FAILED;
529 uint64_t bytes, packets;
530 time_t time;
531
532 if (inbound)
533 {
534 if (this->my_spi)
535 {
536 kernel_ipsec_sa_id_t id = {
537 .src = this->other_addr,
538 .dst = this->my_addr,
539 .spi = this->my_spi,
540 .proto = proto_ike2ip(this->protocol),
541 .mark = mark_in_sa(this),
542 };
543 kernel_ipsec_query_sa_t query = {};
544
545 status = charon->kernel->query_sa(charon->kernel, &id, &query,
546 &bytes, &packets, &time);
547 if (status == SUCCESS)
548 {
549 if (bytes > this->my_usebytes)
550 {
551 this->my_usebytes = bytes;
552 this->my_usepackets = packets;
553 if (time)
554 {
555 this->my_usetime = time;
556 }
557 }
558 else
559 {
560 status = FAILED;
561 }
562 }
563 }
564 }
565 else
566 {
567 if (this->other_spi && (this->outbound_state & CHILD_OUTBOUND_SA))
568 {
569 kernel_ipsec_sa_id_t id = {
570 .src = this->my_addr,
571 .dst = this->other_addr,
572 .spi = this->other_spi,
573 .proto = proto_ike2ip(this->protocol),
574 .mark = this->mark_out,
575 };
576 kernel_ipsec_query_sa_t query = {};
577
578 status = charon->kernel->query_sa(charon->kernel, &id, &query,
579 &bytes, &packets, &time);
580 if (status == SUCCESS)
581 {
582 if (bytes > this->other_usebytes)
583 {
584 this->other_usebytes = bytes;
585 this->other_usepackets = packets;
586 if (time)
587 {
588 this->other_usetime = time;
589 }
590 }
591 else
592 {
593 status = FAILED;
594 }
595 }
596 }
597 }
598 return status;
599 }
600
601 /**
602 * updates the cached usetime
603 */
604 static bool update_usetime(private_child_sa_t *this, bool inbound)
605 {
606 enumerator_t *enumerator;
607 traffic_selector_t *my_ts, *other_ts;
608 time_t last_use = 0;
609
610 enumerator = create_policy_enumerator(this);
611 while (enumerator->enumerate(enumerator, &my_ts, &other_ts))
612 {
613 time_t in, out, fwd;
614
615 if (inbound)
616 {
617 kernel_ipsec_policy_id_t id = {
618 .dir = POLICY_IN,
619 .src_ts = other_ts,
620 .dst_ts = my_ts,
621 .mark = this->mark_in,
622 };
623 kernel_ipsec_query_policy_t query = {};
624
625 if (charon->kernel->query_policy(charon->kernel, &id, &query,
626 &in) == SUCCESS)
627 {
628 last_use = max(last_use, in);
629 }
630 if (this->mode != MODE_TRANSPORT)
631 {
632 id.dir = POLICY_FWD;
633 if (charon->kernel->query_policy(charon->kernel, &id, &query,
634 &fwd) == SUCCESS)
635 {
636 last_use = max(last_use, fwd);
637 }
638 }
639 }
640 else
641 {
642 kernel_ipsec_policy_id_t id = {
643 .dir = POLICY_OUT,
644 .src_ts = my_ts,
645 .dst_ts = other_ts,
646 .mark = this->mark_out,
647 .interface = this->config->get_interface(this->config),
648 };
649 kernel_ipsec_query_policy_t query = {};
650
651 if (charon->kernel->query_policy(charon->kernel, &id, &query,
652 &out) == SUCCESS)
653 {
654 last_use = max(last_use, out);
655 }
656 }
657 }
658 enumerator->destroy(enumerator);
659
660 if (last_use == 0)
661 {
662 return FALSE;
663 }
664 if (inbound)
665 {
666 this->my_usetime = last_use;
667 }
668 else
669 {
670 this->other_usetime = last_use;
671 }
672 return TRUE;
673 }
674
675 METHOD(child_sa_t, get_usestats, void,
676 private_child_sa_t *this, bool inbound,
677 time_t *time, uint64_t *bytes, uint64_t *packets)
678 {
679 if ((!bytes && !packets) || update_usebytes(this, inbound) != FAILED)
680 {
681 /* there was traffic since last update or the kernel interface
682 * does not support querying the number of usebytes.
683 */
684 if (time)
685 {
686 if (!update_usetime(this, inbound) && !bytes && !packets)
687 {
688 /* if policy query did not yield a usetime, query SAs instead */
689 update_usebytes(this, inbound);
690 }
691 }
692 }
693 if (time)
694 {
695 *time = inbound ? this->my_usetime : this->other_usetime;
696 }
697 if (bytes)
698 {
699 *bytes = inbound ? this->my_usebytes : this->other_usebytes;
700 }
701 if (packets)
702 {
703 *packets = inbound ? this->my_usepackets : this->other_usepackets;
704 }
705 }
706
707 METHOD(child_sa_t, get_mark, mark_t,
708 private_child_sa_t *this, bool inbound)
709 {
710 if (inbound)
711 {
712 return this->mark_in;
713 }
714 return this->mark_out;
715 }
716
717 METHOD(child_sa_t, get_lifetime, time_t,
718 private_child_sa_t *this, bool hard)
719 {
720 return hard ? this->expire_time : this->rekey_time;
721 }
722
723 METHOD(child_sa_t, get_installtime, time_t,
724 private_child_sa_t *this)
725 {
726 return this->install_time;
727 }
728
729 METHOD(child_sa_t, alloc_spi, uint32_t,
730 private_child_sa_t *this, protocol_id_t protocol)
731 {
732 if (charon->kernel->get_spi(charon->kernel, this->other_addr, this->my_addr,
733 proto_ike2ip(protocol), &this->my_spi) == SUCCESS)
734 {
735 /* if we allocate a SPI, but then are unable to establish the SA, we
736 * need to know the protocol family to delete the partial SA */
737 this->protocol = protocol;
738 return this->my_spi;
739 }
740 return 0;
741 }
742
743 METHOD(child_sa_t, alloc_cpi, uint16_t,
744 private_child_sa_t *this)
745 {
746 if (charon->kernel->get_cpi(charon->kernel, this->other_addr, this->my_addr,
747 &this->my_cpi) == SUCCESS)
748 {
749 return this->my_cpi;
750 }
751 return 0;
752 }
753
754 /**
755 * Install the given SA in the kernel
756 */
757 static status_t install_internal(private_child_sa_t *this, chunk_t encr,
758 chunk_t integ, uint32_t spi, uint16_t cpi, bool initiator, bool inbound,
759 bool tfcv3)
760 {
761 uint16_t enc_alg = ENCR_UNDEFINED, int_alg = AUTH_UNDEFINED, size;
762 uint16_t esn = NO_EXT_SEQ_NUMBERS;
763 linked_list_t *my_ts, *other_ts, *src_ts, *dst_ts;
764 time_t now;
765 kernel_ipsec_sa_id_t id;
766 kernel_ipsec_add_sa_t sa;
767 lifetime_cfg_t *lifetime;
768 uint32_t tfc = 0;
769 host_t *src, *dst;
770 status_t status;
771 bool update = FALSE;
772
773 /* BEET requires the bound address from the traffic selectors */
774 my_ts = linked_list_create_from_enumerator(
775 array_create_enumerator(this->my_ts));
776 other_ts = linked_list_create_from_enumerator(
777 array_create_enumerator(this->other_ts));
778
779 /* now we have to decide which spi to use. Use self allocated, if "in",
780 * or the one in the proposal, if not "in" (others). Additionally,
781 * source and dest host switch depending on the role */
782 if (inbound)
783 {
784 dst = this->my_addr;
785 src = this->other_addr;
786 if (this->my_spi == spi)
787 { /* alloc_spi has been called, do an SA update */
788 update = TRUE;
789 }
790 this->my_spi = spi;
791 this->my_cpi = cpi;
792 dst_ts = my_ts;
793 src_ts = other_ts;
794 }
795 else
796 {
797 src = this->my_addr;
798 dst = this->other_addr;
799 this->other_spi = spi;
800 this->other_cpi = cpi;
801 src_ts = my_ts;
802 dst_ts = other_ts;
803
804 if (tfcv3)
805 {
806 tfc = this->config->get_tfc(this->config);
807 }
808 this->outbound_state |= CHILD_OUTBOUND_SA;
809 }
810
811 DBG2(DBG_CHD, "adding %s %N SA", inbound ? "inbound" : "outbound",
812 protocol_id_names, this->protocol);
813
814 /* send SA down to the kernel */
815 DBG2(DBG_CHD, " SPI 0x%.8x, src %H dst %H", ntohl(spi), src, dst);
816
817 this->proposal->get_algorithm(this->proposal, ENCRYPTION_ALGORITHM,
818 &enc_alg, &size);
819 this->proposal->get_algorithm(this->proposal, INTEGRITY_ALGORITHM,
820 &int_alg, &size);
821 this->proposal->get_algorithm(this->proposal, EXTENDED_SEQUENCE_NUMBERS,
822 &esn, NULL);
823
824 if (int_alg == AUTH_HMAC_SHA2_256_128 &&
825 this->config->has_option(this->config, OPT_SHA256_96))
826 {
827 DBG2(DBG_CHD, " using %N with 96-bit truncation",
828 integrity_algorithm_names, int_alg);
829 int_alg = AUTH_HMAC_SHA2_256_96;
830 }
831
832 if (!this->reqid_allocated && !this->static_reqid)
833 {
834 status = charon->kernel->alloc_reqid(charon->kernel, my_ts, other_ts,
835 this->mark_in, this->mark_out, &this->reqid);
836 if (status != SUCCESS)
837 {
838 my_ts->destroy(my_ts);
839 other_ts->destroy(other_ts);
840 return status;
841 }
842 this->reqid_allocated = TRUE;
843 }
844
845 lifetime = this->config->get_lifetime(this->config, TRUE);
846
847 now = time_monotonic(NULL);
848 if (lifetime->time.rekey)
849 {
850 if (this->rekey_time)
851 {
852 this->rekey_time = min(this->rekey_time, now + lifetime->time.rekey);
853 }
854 else
855 {
856 this->rekey_time = now + lifetime->time.rekey;
857 }
858 }
859 if (lifetime->time.life)
860 {
861 this->expire_time = now + lifetime->time.life;
862 }
863
864 if (!lifetime->time.jitter && !inbound)
865 { /* avoid triggering multiple rekey events */
866 lifetime->time.rekey = 0;
867 }
868
869 id = (kernel_ipsec_sa_id_t){
870 .src = src,
871 .dst = dst,
872 .spi = spi,
873 .proto = proto_ike2ip(this->protocol),
874 .mark = inbound ? mark_in_sa(this) : this->mark_out,
875 };
876 sa = (kernel_ipsec_add_sa_t){
877 .reqid = this->reqid,
878 .mode = this->mode,
879 .src_ts = src_ts,
880 .dst_ts = dst_ts,
881 .interface = inbound ? NULL : this->config->get_interface(this->config),
882 .lifetime = lifetime,
883 .enc_alg = enc_alg,
884 .enc_key = encr,
885 .int_alg = int_alg,
886 .int_key = integ,
887 .replay_window = this->config->get_replay_window(this->config),
888 .tfc = tfc,
889 .ipcomp = this->ipcomp,
890 .cpi = cpi,
891 .encap = this->encap,
892 .hw_offload = this->config->get_hw_offload(this->config),
893 .esn = esn,
894 .initiator = initiator,
895 .inbound = inbound,
896 .update = update,
897 };
898
899 status = charon->kernel->add_sa(charon->kernel, &id, &sa);
900
901 my_ts->destroy(my_ts);
902 other_ts->destroy(other_ts);
903 free(lifetime);
904
905 return status;
906 }
907
908 METHOD(child_sa_t, install, status_t,
909 private_child_sa_t *this, chunk_t encr, chunk_t integ, uint32_t spi,
910 uint16_t cpi, bool initiator, bool inbound, bool tfcv3)
911 {
912 return install_internal(this, encr, integ, spi, cpi, initiator, inbound,
913 tfcv3);
914 }
915
916 /**
917 * Check kernel interface if policy updates are required
918 */
919 static bool require_policy_update()
920 {
921 kernel_feature_t f;
922
923 f = charon->kernel->get_features(charon->kernel);
924 return !(f & KERNEL_NO_POLICY_UPDATES);
925 }
926
927 /**
928 * Prepare SA config to install/delete policies
929 */
930 static void prepare_sa_cfg(private_child_sa_t *this, ipsec_sa_cfg_t *my_sa,
931 ipsec_sa_cfg_t *other_sa)
932 {
933 enumerator_t *enumerator;
934
935 *my_sa = (ipsec_sa_cfg_t){
936 .mode = this->mode,
937 .reqid = this->reqid,
938 .ipcomp = {
939 .transform = this->ipcomp,
940 },
941 };
942 *other_sa = *my_sa;
943
944 my_sa->ipcomp.cpi = this->my_cpi;
945 other_sa->ipcomp.cpi = this->other_cpi;
946
947 if (this->protocol == PROTO_ESP)
948 {
949 my_sa->esp.use = TRUE;
950 my_sa->esp.spi = this->my_spi;
951 other_sa->esp.use = TRUE;
952 other_sa->esp.spi = this->other_spi;
953 }
954 else
955 {
956 my_sa->ah.use = TRUE;
957 my_sa->ah.spi = this->my_spi;
958 other_sa->ah.use = TRUE;
959 other_sa->ah.spi = this->other_spi;
960 }
961
962 enumerator = create_policy_enumerator(this);
963 while (enumerator->enumerate(enumerator, NULL, NULL))
964 {
965 my_sa->policy_count++;
966 other_sa->policy_count++;
967 }
968 enumerator->destroy(enumerator);
969 }
970
971 /**
972 * Install inbound policie(s): in, fwd
973 */
974 static status_t install_policies_inbound(private_child_sa_t *this,
975 host_t *my_addr, host_t *other_addr, traffic_selector_t *my_ts,
976 traffic_selector_t *other_ts, ipsec_sa_cfg_t *my_sa,
977 ipsec_sa_cfg_t *other_sa, policy_type_t type,
978 policy_priority_t priority, uint32_t manual_prio)
979 {
980 kernel_ipsec_policy_id_t in_id = {
981 .dir = POLICY_IN,
982 .src_ts = other_ts,
983 .dst_ts = my_ts,
984 .mark = this->mark_in,
985 };
986 kernel_ipsec_manage_policy_t in_policy = {
987 .type = type,
988 .prio = priority,
989 .manual_prio = manual_prio,
990 .src = other_addr,
991 .dst = my_addr,
992 .sa = my_sa,
993 };
994 status_t status = SUCCESS;
995
996 status |= charon->kernel->add_policy(charon->kernel, &in_id, &in_policy);
997 if (this->mode != MODE_TRANSPORT)
998 {
999 in_id.dir = POLICY_FWD;
1000 status |= charon->kernel->add_policy(charon->kernel, &in_id, &in_policy);
1001 }
1002 return status;
1003 }
1004
1005 /**
1006 * Install outbound policie(s): out, [fwd]
1007 */
1008 static status_t install_policies_outbound(private_child_sa_t *this,
1009 host_t *my_addr, host_t *other_addr, traffic_selector_t *my_ts,
1010 traffic_selector_t *other_ts, ipsec_sa_cfg_t *my_sa,
1011 ipsec_sa_cfg_t *other_sa, policy_type_t type,
1012 policy_priority_t priority, uint32_t manual_prio)
1013 {
1014 kernel_ipsec_policy_id_t out_id = {
1015 .dir = POLICY_OUT,
1016 .src_ts = my_ts,
1017 .dst_ts = other_ts,
1018 .mark = this->mark_out,
1019 .interface = this->config->get_interface(this->config),
1020 };
1021 kernel_ipsec_manage_policy_t out_policy = {
1022 .type = type,
1023 .prio = priority,
1024 .manual_prio = manual_prio,
1025 .src = my_addr,
1026 .dst = other_addr,
1027 .sa = other_sa,
1028 };
1029 status_t status = SUCCESS;
1030
1031 status |= charon->kernel->add_policy(charon->kernel, &out_id, &out_policy);
1032
1033 if (this->mode != MODE_TRANSPORT && this->policies_fwd_out)
1034 {
1035 /* install an "outbound" FWD policy in case there is a drop policy
1036 * matching outbound forwarded traffic, to allow another tunnel to use
1037 * the reversed subnets and do the same we don't set a reqid (this also
1038 * allows the kernel backend to distinguish between the two types of
1039 * FWD policies). To avoid problems with symmetrically overlapping
1040 * policies of two SAs we install them with reduced priority. As they
1041 * basically act as bypass policies for drop policies we use a higher
1042 * priority than is used for them. */
1043 out_id.dir = POLICY_FWD;
1044 other_sa->reqid = 0;
1045 if (priority == POLICY_PRIORITY_DEFAULT)
1046 {
1047 out_policy.prio = POLICY_PRIORITY_ROUTED;
1048 }
1049 status |= charon->kernel->add_policy(charon->kernel, &out_id,
1050 &out_policy);
1051 /* reset the reqid for any other further policies */
1052 other_sa->reqid = this->reqid;
1053 }
1054 return status;
1055 }
1056
1057 /**
1058 * Install all policies
1059 */
1060 static status_t install_policies_internal(private_child_sa_t *this,
1061 host_t *my_addr, host_t *other_addr, traffic_selector_t *my_ts,
1062 traffic_selector_t *other_ts, ipsec_sa_cfg_t *my_sa,
1063 ipsec_sa_cfg_t *other_sa, policy_type_t type,
1064 policy_priority_t priority, uint32_t manual_prio, bool outbound)
1065 {
1066 status_t status = SUCCESS;
1067
1068 status |= install_policies_inbound(this, my_addr, other_addr, my_ts,
1069 other_ts, my_sa, other_sa, type, priority, manual_prio);
1070 if (outbound)
1071 {
1072 status |= install_policies_outbound(this, my_addr, other_addr, my_ts,
1073 other_ts, my_sa, other_sa, type, priority, manual_prio);
1074 }
1075 return status;
1076 }
1077
1078 /**
1079 * Delete inbound policies: in, fwd
1080 */
1081 static void del_policies_inbound(private_child_sa_t *this,
1082 host_t *my_addr, host_t *other_addr, traffic_selector_t *my_ts,
1083 traffic_selector_t *other_ts, ipsec_sa_cfg_t *my_sa,
1084 ipsec_sa_cfg_t *other_sa, policy_type_t type,
1085 policy_priority_t priority, uint32_t manual_prio)
1086 {
1087 kernel_ipsec_policy_id_t in_id = {
1088 .dir = POLICY_IN,
1089 .src_ts = other_ts,
1090 .dst_ts = my_ts,
1091 .mark = this->mark_in,
1092 };
1093 kernel_ipsec_manage_policy_t in_policy = {
1094 .type = type,
1095 .prio = priority,
1096 .manual_prio = manual_prio,
1097 .src = other_addr,
1098 .dst = my_addr,
1099 .sa = my_sa,
1100 };
1101
1102 charon->kernel->del_policy(charon->kernel, &in_id, &in_policy);
1103
1104 if (this->mode != MODE_TRANSPORT)
1105 {
1106 in_id.dir = POLICY_FWD;
1107 charon->kernel->del_policy(charon->kernel, &in_id, &in_policy);
1108 }
1109 }
1110
1111 /**
1112 * Delete outbound policies: out, [fwd]
1113 */
1114 static void del_policies_outbound(private_child_sa_t *this,
1115 host_t *my_addr, host_t *other_addr, traffic_selector_t *my_ts,
1116 traffic_selector_t *other_ts, ipsec_sa_cfg_t *my_sa,
1117 ipsec_sa_cfg_t *other_sa, policy_type_t type,
1118 policy_priority_t priority, uint32_t manual_prio)
1119 {
1120 kernel_ipsec_policy_id_t out_id = {
1121 .dir = POLICY_OUT,
1122 .src_ts = my_ts,
1123 .dst_ts = other_ts,
1124 .mark = this->mark_out,
1125 .interface = this->config->get_interface(this->config),
1126 };
1127 kernel_ipsec_manage_policy_t out_policy = {
1128 .type = type,
1129 .prio = priority,
1130 .manual_prio = manual_prio,
1131 .src = my_addr,
1132 .dst = other_addr,
1133 .sa = other_sa,
1134 };
1135
1136 charon->kernel->del_policy(charon->kernel, &out_id, &out_policy);
1137
1138 if (this->mode != MODE_TRANSPORT && this->policies_fwd_out)
1139 {
1140 out_id.dir = POLICY_FWD;
1141 other_sa->reqid = 0;
1142 if (priority == POLICY_PRIORITY_DEFAULT)
1143 {
1144 out_policy.prio = POLICY_PRIORITY_ROUTED;
1145 }
1146 charon->kernel->del_policy(charon->kernel, &out_id, &out_policy);
1147 other_sa->reqid = this->reqid;
1148 }
1149 }
1150
1151 /**
1152 * Delete in- and outbound policies
1153 */
1154 static void del_policies_internal(private_child_sa_t *this,
1155 host_t *my_addr, host_t *other_addr, traffic_selector_t *my_ts,
1156 traffic_selector_t *other_ts, ipsec_sa_cfg_t *my_sa,
1157 ipsec_sa_cfg_t *other_sa, policy_type_t type,
1158 policy_priority_t priority, uint32_t manual_prio, bool outbound)
1159 {
1160 if (outbound)
1161 {
1162 del_policies_outbound(this, my_addr, other_addr, my_ts, other_ts, my_sa,
1163 other_sa, type, priority, manual_prio);
1164 }
1165 del_policies_inbound(this, my_addr, other_addr, my_ts, other_ts, my_sa,
1166 other_sa, type, priority, manual_prio);
1167 }
1168
1169 METHOD(child_sa_t, set_policies, void,
1170 private_child_sa_t *this, linked_list_t *my_ts_list,
1171 linked_list_t *other_ts_list)
1172 {
1173 enumerator_t *enumerator;
1174 traffic_selector_t *my_ts, *other_ts;
1175
1176 if (array_count(this->my_ts))
1177 {
1178 array_destroy_offset(this->my_ts,
1179 offsetof(traffic_selector_t, destroy));
1180 this->my_ts = array_create(0, 0);
1181 }
1182 enumerator = my_ts_list->create_enumerator(my_ts_list);
1183 while (enumerator->enumerate(enumerator, &my_ts))
1184 {
1185 array_insert(this->my_ts, ARRAY_TAIL, my_ts->clone(my_ts));
1186 }
1187 enumerator->destroy(enumerator);
1188 array_sort(this->my_ts, (void*)traffic_selector_cmp, NULL);
1189
1190 if (array_count(this->other_ts))
1191 {
1192 array_destroy_offset(this->other_ts,
1193 offsetof(traffic_selector_t, destroy));
1194 this->other_ts = array_create(0, 0);
1195 }
1196 enumerator = other_ts_list->create_enumerator(other_ts_list);
1197 while (enumerator->enumerate(enumerator, &other_ts))
1198 {
1199 array_insert(this->other_ts, ARRAY_TAIL, other_ts->clone(other_ts));
1200 }
1201 enumerator->destroy(enumerator);
1202 array_sort(this->other_ts, (void*)traffic_selector_cmp, NULL);
1203 }
1204
1205 METHOD(child_sa_t, install_policies, status_t,
1206 private_child_sa_t *this)
1207 {
1208 enumerator_t *enumerator;
1209 linked_list_t *my_ts_list, *other_ts_list;
1210 traffic_selector_t *my_ts, *other_ts;
1211 status_t status = SUCCESS;
1212 bool install_outbound = FALSE;
1213
1214 if (!this->reqid_allocated && !this->static_reqid)
1215 {
1216 my_ts_list = linked_list_create_from_enumerator(
1217 array_create_enumerator(this->my_ts));
1218 other_ts_list = linked_list_create_from_enumerator(
1219 array_create_enumerator(this->other_ts));
1220 status = charon->kernel->alloc_reqid(
1221 charon->kernel, my_ts_list, other_ts_list,
1222 this->mark_in, this->mark_out, &this->reqid);
1223 my_ts_list->destroy(my_ts_list);
1224 other_ts_list->destroy(other_ts_list);
1225 if (status != SUCCESS)
1226 {
1227 return status;
1228 }
1229 this->reqid_allocated = TRUE;
1230 }
1231
1232 if (!(this->outbound_state & CHILD_OUTBOUND_REGISTERED))
1233 {
1234 install_outbound = TRUE;
1235 this->outbound_state |= CHILD_OUTBOUND_POLICIES;
1236 }
1237
1238 if (!this->config->has_option(this->config, OPT_NO_POLICIES))
1239 {
1240 policy_priority_t priority;
1241 ipsec_sa_cfg_t my_sa, other_sa;
1242 uint32_t manual_prio;
1243
1244 prepare_sa_cfg(this, &my_sa, &other_sa);
1245 manual_prio = this->config->get_manual_prio(this->config);
1246
1247 /* if we're not in state CHILD_INSTALLING (i.e. if there is no SAD
1248 * entry) we install a trap policy */
1249 this->trap = this->state == CHILD_CREATED;
1250 priority = this->trap ? POLICY_PRIORITY_ROUTED
1251 : POLICY_PRIORITY_DEFAULT;
1252
1253 /* enumerate pairs of traffic selectors */
1254 enumerator = create_policy_enumerator(this);
1255 while (enumerator->enumerate(enumerator, &my_ts, &other_ts))
1256 {
1257 status |= install_policies_internal(this, this->my_addr,
1258 this->other_addr, my_ts, other_ts,
1259 &my_sa, &other_sa, POLICY_IPSEC, priority,
1260 manual_prio, install_outbound);
1261 if (status != SUCCESS)
1262 {
1263 break;
1264 }
1265 }
1266 enumerator->destroy(enumerator);
1267 }
1268
1269 if (status == SUCCESS && this->trap)
1270 {
1271 set_state(this, CHILD_ROUTED);
1272 }
1273 return status;
1274 }
1275
1276 METHOD(child_sa_t, register_outbound, status_t,
1277 private_child_sa_t *this, chunk_t encr, chunk_t integ, uint32_t spi,
1278 uint16_t cpi, bool tfcv3)
1279 {
1280 status_t status;
1281
1282 /* if the kernel supports installing SPIs with policies we install the
1283 * SA immediately as it will only be used once we update the policies */
1284 if (charon->kernel->get_features(charon->kernel) & KERNEL_POLICY_SPI)
1285 {
1286 status = install_internal(this, encr, integ, spi, cpi, FALSE, FALSE,
1287 tfcv3);
1288 }
1289 else
1290 {
1291 DBG2(DBG_CHD, "registering outbound %N SA", protocol_id_names,
1292 this->protocol);
1293 DBG2(DBG_CHD, " SPI 0x%.8x, src %H dst %H", ntohl(spi), this->my_addr,
1294 this->other_addr);
1295
1296 this->other_spi = spi;
1297 this->other_cpi = cpi;
1298 this->encr_r = chunk_clone(encr);
1299 this->integ_r = chunk_clone(integ);
1300 this->tfcv3 = tfcv3;
1301 status = SUCCESS;
1302 }
1303 this->outbound_state |= CHILD_OUTBOUND_REGISTERED;
1304 return status;
1305 }
1306
1307 METHOD(child_sa_t, install_outbound, status_t,
1308 private_child_sa_t *this)
1309 {
1310 enumerator_t *enumerator;
1311 traffic_selector_t *my_ts, *other_ts;
1312 status_t status = SUCCESS;
1313
1314 if (!(this->outbound_state & CHILD_OUTBOUND_SA))
1315 {
1316 status = install_internal(this, this->encr_r, this->integ_r,
1317 this->other_spi, this->other_cpi, FALSE,
1318 FALSE, this->tfcv3);
1319 chunk_clear(&this->encr_r);
1320 chunk_clear(&this->integ_r);
1321 }
1322 this->outbound_state &= ~CHILD_OUTBOUND_REGISTERED;
1323 if (status != SUCCESS)
1324 {
1325 return status;
1326 }
1327 if (!this->config->has_option(this->config, OPT_NO_POLICIES) &&
1328 !(this->outbound_state & CHILD_OUTBOUND_POLICIES))
1329 {
1330 ipsec_sa_cfg_t my_sa, other_sa;
1331 uint32_t manual_prio;
1332
1333 prepare_sa_cfg(this, &my_sa, &other_sa);
1334 manual_prio = this->config->get_manual_prio(this->config);
1335
1336 enumerator = create_policy_enumerator(this);
1337 while (enumerator->enumerate(enumerator, &my_ts, &other_ts))
1338 {
1339 status |= install_policies_outbound(this, this->my_addr,
1340 this->other_addr, my_ts, other_ts,
1341 &my_sa, &other_sa, POLICY_IPSEC,
1342 POLICY_PRIORITY_DEFAULT, manual_prio);
1343 if (status != SUCCESS)
1344 {
1345 break;
1346 }
1347 }
1348 enumerator->destroy(enumerator);
1349 }
1350 this->outbound_state |= CHILD_OUTBOUND_POLICIES;
1351 return status;
1352 }
1353
1354 METHOD(child_sa_t, remove_outbound, void,
1355 private_child_sa_t *this)
1356 {
1357 enumerator_t *enumerator;
1358 traffic_selector_t *my_ts, *other_ts;
1359
1360 if (!(this->outbound_state & CHILD_OUTBOUND_SA))
1361 {
1362 if (this->outbound_state & CHILD_OUTBOUND_REGISTERED)
1363 {
1364 chunk_clear(&this->encr_r);
1365 chunk_clear(&this->integ_r);
1366 this->outbound_state = CHILD_OUTBOUND_NONE;
1367 }
1368 return;
1369 }
1370
1371 if (!this->config->has_option(this->config, OPT_NO_POLICIES) &&
1372 (this->outbound_state & CHILD_OUTBOUND_POLICIES))
1373 {
1374 ipsec_sa_cfg_t my_sa, other_sa;
1375 uint32_t manual_prio;
1376
1377 prepare_sa_cfg(this, &my_sa, &other_sa);
1378 manual_prio = this->config->get_manual_prio(this->config);
1379
1380 enumerator = create_policy_enumerator(this);
1381 while (enumerator->enumerate(enumerator, &my_ts, &other_ts))
1382 {
1383 del_policies_outbound(this, this->my_addr, this->other_addr,
1384 my_ts, other_ts, &my_sa, &other_sa,
1385 POLICY_IPSEC, POLICY_PRIORITY_DEFAULT,
1386 manual_prio);
1387 }
1388 enumerator->destroy(enumerator);
1389 }
1390
1391 kernel_ipsec_sa_id_t id = {
1392 .src = this->my_addr,
1393 .dst = this->other_addr,
1394 .spi = this->other_spi,
1395 .proto = proto_ike2ip(this->protocol),
1396 .mark = this->mark_out,
1397 };
1398 kernel_ipsec_del_sa_t sa = {
1399 .cpi = this->other_cpi,
1400 };
1401 charon->kernel->del_sa(charon->kernel, &id, &sa);
1402 this->outbound_state = CHILD_OUTBOUND_NONE;
1403 }
1404
1405 METHOD(child_sa_t, set_rekey_spi, void,
1406 private_child_sa_t *this, uint32_t spi)
1407 {
1408 this->rekey_spi = spi;
1409 }
1410
1411 METHOD(child_sa_t, get_rekey_spi, uint32_t,
1412 private_child_sa_t *this)
1413 {
1414 return this->rekey_spi;
1415 }
1416
1417 CALLBACK(reinstall_vip, void,
1418 host_t *vip, va_list args)
1419 {
1420 host_t *me;
1421 char *iface;
1422
1423 VA_ARGS_VGET(args, me);
1424 if (charon->kernel->get_interface(charon->kernel, me, &iface))
1425 {
1426 charon->kernel->del_ip(charon->kernel, vip, -1, TRUE);
1427 charon->kernel->add_ip(charon->kernel, vip, -1, iface);
1428 free(iface);
1429 }
1430 }
1431
1432 /**
1433 * Update addresses and encap state of IPsec SAs in the kernel
1434 */
1435 static status_t update_sas(private_child_sa_t *this, host_t *me, host_t *other,
1436 bool encap)
1437 {
1438 /* update our (initiator) SA */
1439 if (this->my_spi)
1440 {
1441 kernel_ipsec_sa_id_t id = {
1442 .src = this->other_addr,
1443 .dst = this->my_addr,
1444 .spi = this->my_spi,
1445 .proto = proto_ike2ip(this->protocol),
1446 .mark = mark_in_sa(this),
1447 };
1448 kernel_ipsec_update_sa_t sa = {
1449 .cpi = this->ipcomp != IPCOMP_NONE ? this->my_cpi : 0,
1450 .new_src = other,
1451 .new_dst = me,
1452 .encap = this->encap,
1453 .new_encap = encap,
1454 };
1455 if (charon->kernel->update_sa(charon->kernel, &id,
1456 &sa) == NOT_SUPPORTED)
1457 {
1458 return NOT_SUPPORTED;
1459 }
1460 }
1461
1462 /* update his (responder) SA */
1463 if (this->other_spi && (this->outbound_state & CHILD_OUTBOUND_SA))
1464 {
1465 kernel_ipsec_sa_id_t id = {
1466 .src = this->my_addr,
1467 .dst = this->other_addr,
1468 .spi = this->other_spi,
1469 .proto = proto_ike2ip(this->protocol),
1470 .mark = this->mark_out,
1471 };
1472 kernel_ipsec_update_sa_t sa = {
1473 .cpi = this->ipcomp != IPCOMP_NONE ? this->other_cpi : 0,
1474 .new_src = me,
1475 .new_dst = other,
1476 .encap = this->encap,
1477 .new_encap = encap,
1478 };
1479 if (charon->kernel->update_sa(charon->kernel, &id,
1480 &sa) == NOT_SUPPORTED)
1481 {
1482 return NOT_SUPPORTED;
1483 }
1484 }
1485 /* we currently ignore the actual return values above */
1486 return SUCCESS;
1487 }
1488
1489 METHOD(child_sa_t, update, status_t,
1490 private_child_sa_t *this, host_t *me, host_t *other, linked_list_t *vips,
1491 bool encap)
1492 {
1493 child_sa_state_t old;
1494 bool transport_proxy_mode;
1495
1496 /* anything changed at all? */
1497 if (me->equals(me, this->my_addr) &&
1498 other->equals(other, this->other_addr) && this->encap == encap)
1499 {
1500 return SUCCESS;
1501 }
1502
1503 old = this->state;
1504 set_state(this, CHILD_UPDATING);
1505 transport_proxy_mode = this->mode == MODE_TRANSPORT &&
1506 this->config->has_option(this->config,
1507 OPT_PROXY_MODE);
1508
1509 if (!this->config->has_option(this->config, OPT_NO_POLICIES) &&
1510 require_policy_update())
1511 {
1512 ipsec_sa_cfg_t my_sa, other_sa;
1513 enumerator_t *enumerator;
1514 traffic_selector_t *my_ts, *other_ts;
1515 uint32_t manual_prio;
1516 status_t state;
1517 bool outbound;
1518
1519 prepare_sa_cfg(this, &my_sa, &other_sa);
1520 manual_prio = this->config->get_manual_prio(this->config);
1521 outbound = (this->outbound_state & CHILD_OUTBOUND_POLICIES);
1522
1523 enumerator = create_policy_enumerator(this);
1524 while (enumerator->enumerate(enumerator, &my_ts, &other_ts))
1525 {
1526 /* install drop policy to avoid traffic leaks, acquires etc. */
1527 if (outbound)
1528 {
1529 install_policies_outbound(this, this->my_addr, this->other_addr,
1530 my_ts, other_ts, &my_sa, &other_sa, POLICY_DROP,
1531 POLICY_PRIORITY_DEFAULT, manual_prio);
1532 }
1533 /* remove old policies */
1534 del_policies_internal(this, this->my_addr, this->other_addr,
1535 my_ts, other_ts, &my_sa, &other_sa, POLICY_IPSEC,
1536 POLICY_PRIORITY_DEFAULT, manual_prio, outbound);
1537 }
1538 enumerator->destroy(enumerator);
1539
1540 /* update the IPsec SAs */
1541 state = update_sas(this, me, other, encap);
1542
1543 enumerator = create_policy_enumerator(this);
1544 while (enumerator->enumerate(enumerator, &my_ts, &other_ts))
1545 {
1546 traffic_selector_t *old_my_ts = NULL, *old_other_ts = NULL;
1547
1548 /* reinstall the previous policies if we can't update the SAs */
1549 if (state == NOT_SUPPORTED)
1550 {
1551 install_policies_internal(this, this->my_addr, this->other_addr,
1552 my_ts, other_ts, &my_sa, &other_sa, POLICY_IPSEC,
1553 POLICY_PRIORITY_DEFAULT, manual_prio, outbound);
1554 }
1555 else
1556 {
1557 /* check if we have to update a "dynamic" traffic selector */
1558 if (!me->ip_equals(me, this->my_addr) &&
1559 my_ts->is_host(my_ts, this->my_addr))
1560 {
1561 old_my_ts = my_ts->clone(my_ts);
1562 my_ts->set_address(my_ts, me);
1563 }
1564 if (!other->ip_equals(other, this->other_addr) &&
1565 other_ts->is_host(other_ts, this->other_addr))
1566 {
1567 old_other_ts = other_ts->clone(other_ts);
1568 other_ts->set_address(other_ts, other);
1569 }
1570
1571 /* we reinstall the virtual IP to handle interface roaming
1572 * correctly */
1573 vips->invoke_function(vips, reinstall_vip, me);
1574
1575 /* reinstall updated policies */
1576 install_policies_internal(this, me, other, my_ts, other_ts,
1577 &my_sa, &other_sa, POLICY_IPSEC,
1578 POLICY_PRIORITY_DEFAULT, manual_prio, outbound);
1579 }
1580 /* remove the drop policy */
1581 if (outbound)
1582 {
1583 del_policies_outbound(this, this->my_addr, this->other_addr,
1584 old_my_ts ?: my_ts, old_other_ts ?: other_ts,
1585 &my_sa, &other_sa, POLICY_DROP,
1586 POLICY_PRIORITY_DEFAULT, 0);
1587 }
1588
1589 DESTROY_IF(old_my_ts);
1590 DESTROY_IF(old_other_ts);
1591 }
1592 enumerator->destroy(enumerator);
1593
1594 if (state == NOT_SUPPORTED)
1595 {
1596 set_state(this, old);
1597 return NOT_SUPPORTED;
1598 }
1599
1600 }
1601 else if (!transport_proxy_mode)
1602 {
1603 if (update_sas(this, me, other, encap) == NOT_SUPPORTED)
1604 {
1605 set_state(this, old);
1606 return NOT_SUPPORTED;
1607 }
1608 }
1609
1610 if (!transport_proxy_mode)
1611 {
1612 /* apply hosts */
1613 if (!me->equals(me, this->my_addr))
1614 {
1615 this->my_addr->destroy(this->my_addr);
1616 this->my_addr = me->clone(me);
1617 }
1618 if (!other->equals(other, this->other_addr))
1619 {
1620 this->other_addr->destroy(this->other_addr);
1621 this->other_addr = other->clone(other);
1622 }
1623 }
1624
1625 this->encap = encap;
1626 set_state(this, old);
1627
1628 return SUCCESS;
1629 }
1630
1631 METHOD(child_sa_t, destroy, void,
1632 private_child_sa_t *this)
1633 {
1634 enumerator_t *enumerator;
1635 traffic_selector_t *my_ts, *other_ts;
1636 policy_priority_t priority;
1637
1638 priority = this->trap ? POLICY_PRIORITY_ROUTED : POLICY_PRIORITY_DEFAULT;
1639
1640 set_state(this, CHILD_DESTROYING);
1641
1642 if (!this->config->has_option(this->config, OPT_NO_POLICIES))
1643 {
1644 ipsec_sa_cfg_t my_sa, other_sa;
1645 uint32_t manual_prio;
1646 bool del_outbound;
1647
1648 prepare_sa_cfg(this, &my_sa, &other_sa);
1649 manual_prio = this->config->get_manual_prio(this->config);
1650 del_outbound = (this->outbound_state & CHILD_OUTBOUND_POLICIES) ||
1651 this->trap;
1652
1653 /* delete all policies in the kernel */
1654 enumerator = create_policy_enumerator(this);
1655 while (enumerator->enumerate(enumerator, &my_ts, &other_ts))
1656 {
1657 del_policies_internal(this, this->my_addr,
1658 this->other_addr, my_ts, other_ts, &my_sa, &other_sa,
1659 POLICY_IPSEC, priority, manual_prio, del_outbound);
1660 }
1661 enumerator->destroy(enumerator);
1662 }
1663
1664 /* delete SAs in the kernel, if they are set up */
1665 if (this->my_spi)
1666 {
1667 kernel_ipsec_sa_id_t id = {
1668 .src = this->other_addr,
1669 .dst = this->my_addr,
1670 .spi = this->my_spi,
1671 .proto = proto_ike2ip(this->protocol),
1672 .mark = mark_in_sa(this),
1673 };
1674 kernel_ipsec_del_sa_t sa = {
1675 .cpi = this->my_cpi,
1676 };
1677 charon->kernel->del_sa(charon->kernel, &id, &sa);
1678 }
1679 if (this->other_spi && (this->outbound_state & CHILD_OUTBOUND_SA))
1680 {
1681 kernel_ipsec_sa_id_t id = {
1682 .src = this->my_addr,
1683 .dst = this->other_addr,
1684 .spi = this->other_spi,
1685 .proto = proto_ike2ip(this->protocol),
1686 .mark = this->mark_out,
1687 };
1688 kernel_ipsec_del_sa_t sa = {
1689 .cpi = this->other_cpi,
1690 };
1691 charon->kernel->del_sa(charon->kernel, &id, &sa);
1692 }
1693
1694 if (this->reqid_allocated)
1695 {
1696 if (charon->kernel->release_reqid(charon->kernel,
1697 this->reqid, this->mark_in, this->mark_out) != SUCCESS)
1698 {
1699 DBG1(DBG_CHD, "releasing reqid %u failed", this->reqid);
1700 }
1701 }
1702
1703 array_destroy_offset(this->my_ts, offsetof(traffic_selector_t, destroy));
1704 array_destroy_offset(this->other_ts, offsetof(traffic_selector_t, destroy));
1705 this->my_addr->destroy(this->my_addr);
1706 this->other_addr->destroy(this->other_addr);
1707 DESTROY_IF(this->proposal);
1708 this->config->destroy(this->config);
1709 chunk_clear(&this->encr_r);
1710 chunk_clear(&this->integ_r);
1711 free(this);
1712 }
1713
1714 /**
1715 * Get proxy address for one side, if any
1716 */
1717 static host_t* get_proxy_addr(child_cfg_t *config, host_t *ike, bool local)
1718 {
1719 host_t *host = NULL;
1720 uint8_t mask;
1721 enumerator_t *enumerator;
1722 linked_list_t *ts_list, *list;
1723 traffic_selector_t *ts;
1724
1725 list = linked_list_create_with_items(ike, NULL);
1726 ts_list = config->get_traffic_selectors(config, local, NULL, list);
1727 list->destroy(list);
1728
1729 enumerator = ts_list->create_enumerator(ts_list);
1730 while (enumerator->enumerate(enumerator, &ts))
1731 {
1732 if (ts->is_host(ts, NULL) && ts->to_subnet(ts, &host, &mask))
1733 {
1734 DBG1(DBG_CHD, "%s address: %H is a transport mode proxy for %H",
1735 local ? "my" : "other", ike, host);
1736 break;
1737 }
1738 }
1739 enumerator->destroy(enumerator);
1740 ts_list->destroy_offset(ts_list, offsetof(traffic_selector_t, destroy));
1741
1742 if (!host)
1743 {
1744 host = ike->clone(ike);
1745 }
1746 return host;
1747 }
1748
1749 /**
1750 * Described in header.
1751 */
1752 child_sa_t * child_sa_create(host_t *me, host_t* other,
1753 child_cfg_t *config, uint32_t reqid, bool encap,
1754 u_int mark_in, u_int mark_out)
1755 {
1756 private_child_sa_t *this;
1757 static refcount_t unique_id = 0, unique_mark = 0;
1758 refcount_t mark = 0;
1759
1760 INIT(this,
1761 .public = {
1762 .get_name = _get_name,
1763 .get_reqid = _get_reqid,
1764 .get_unique_id = _get_unique_id,
1765 .get_config = _get_config,
1766 .get_state = _get_state,
1767 .set_state = _set_state,
1768 .get_outbound_state = _get_outbound_state,
1769 .get_spi = _get_spi,
1770 .get_cpi = _get_cpi,
1771 .get_protocol = _get_protocol,
1772 .set_protocol = _set_protocol,
1773 .get_mode = _get_mode,
1774 .set_mode = _set_mode,
1775 .get_proposal = _get_proposal,
1776 .set_proposal = _set_proposal,
1777 .get_lifetime = _get_lifetime,
1778 .get_installtime = _get_installtime,
1779 .get_usestats = _get_usestats,
1780 .get_mark = _get_mark,
1781 .has_encap = _has_encap,
1782 .get_ipcomp = _get_ipcomp,
1783 .set_ipcomp = _set_ipcomp,
1784 .get_close_action = _get_close_action,
1785 .set_close_action = _set_close_action,
1786 .get_dpd_action = _get_dpd_action,
1787 .set_dpd_action = _set_dpd_action,
1788 .alloc_spi = _alloc_spi,
1789 .alloc_cpi = _alloc_cpi,
1790 .install = _install,
1791 .register_outbound = _register_outbound,
1792 .install_outbound = _install_outbound,
1793 .remove_outbound = _remove_outbound,
1794 .set_rekey_spi = _set_rekey_spi,
1795 .get_rekey_spi = _get_rekey_spi,
1796 .update = _update,
1797 .set_policies = _set_policies,
1798 .install_policies = _install_policies,
1799 .create_ts_enumerator = _create_ts_enumerator,
1800 .create_policy_enumerator = _create_policy_enumerator,
1801 .destroy = _destroy,
1802 },
1803 .encap = encap,
1804 .ipcomp = IPCOMP_NONE,
1805 .state = CHILD_CREATED,
1806 .my_ts = array_create(0, 0),
1807 .other_ts = array_create(0, 0),
1808 .protocol = PROTO_NONE,
1809 .mode = MODE_TUNNEL,
1810 .close_action = config->get_close_action(config),
1811 .dpd_action = config->get_dpd_action(config),
1812 .reqid = config->get_reqid(config),
1813 .unique_id = ref_get(&unique_id),
1814 .mark_in = config->get_mark(config, TRUE),
1815 .mark_out = config->get_mark(config, FALSE),
1816 .install_time = time_monotonic(NULL),
1817 .policies_fwd_out = config->has_option(config, OPT_FWD_OUT_POLICIES),
1818 );
1819
1820 this->config = config;
1821 config->get_ref(config);
1822
1823 if (mark_in)
1824 {
1825 this->mark_in.value = mark_in;
1826 }
1827 if (mark_out)
1828 {
1829 this->mark_out.value = mark_out;
1830 }
1831
1832 if (MARK_IS_UNIQUE(this->mark_in.value) ||
1833 MARK_IS_UNIQUE(this->mark_out.value))
1834 {
1835 bool unique_dir;
1836
1837 unique_dir = this->mark_in.value == MARK_UNIQUE_DIR ||
1838 this->mark_out.value == MARK_UNIQUE_DIR;
1839
1840 if (!unique_dir)
1841 {
1842 mark = ref_get(&unique_mark);
1843 }
1844 if (MARK_IS_UNIQUE(this->mark_in.value))
1845 {
1846 if (unique_dir)
1847 {
1848 mark = ref_get(&unique_mark);
1849 }
1850 this->mark_in.value = mark;
1851 }
1852 if (MARK_IS_UNIQUE(this->mark_out.value))
1853 {
1854 if (unique_dir)
1855 {
1856 mark = ref_get(&unique_mark);
1857 }
1858 this->mark_out.value = mark;
1859 }
1860 }
1861
1862 if (!this->reqid)
1863 {
1864 /* reuse old reqid if we are rekeying an existing CHILD_SA and when
1865 * initiating a trap policy. While the reqid cache would find the same
1866 * reqid for our selectors, this does not work in a special case: If an
1867 * SA is triggered by a trap policy, but the negotiated TS get
1868 * narrowed, we still must reuse the same reqid to successfully
1869 * replace the temporary SA on the kernel level. Rekeying such an SA
1870 * requires an explicit reqid, as the cache currently knows the original
1871 * selectors only for that reqid. */
1872 this->reqid = reqid;
1873 }
1874 else
1875 {
1876 this->static_reqid = TRUE;
1877 }
1878
1879 /* MIPv6 proxy transport mode sets SA endpoints to TS hosts */
1880 if (config->get_mode(config) == MODE_TRANSPORT &&
1881 config->has_option(config, OPT_PROXY_MODE))
1882 {
1883 this->mode = MODE_TRANSPORT;
1884
1885 this->my_addr = get_proxy_addr(config, me, TRUE);
1886 this->other_addr = get_proxy_addr(config, other, FALSE);
1887 }
1888 else
1889 {
1890 this->my_addr = me->clone(me);
1891 this->other_addr = other->clone(other);
1892 }
1893 return &this->public;
1894 }