b0efec54a7255d30d08bdbdcfc4ff05e3945cce7
[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 .copy_df = !this->config->has_option(this->config, OPT_NO_COPY_DF),
895 .copy_ecn = !this->config->has_option(this->config, OPT_NO_COPY_ECN),
896 .initiator = initiator,
897 .inbound = inbound,
898 .update = update,
899 };
900
901 status = charon->kernel->add_sa(charon->kernel, &id, &sa);
902
903 my_ts->destroy(my_ts);
904 other_ts->destroy(other_ts);
905 free(lifetime);
906
907 return status;
908 }
909
910 METHOD(child_sa_t, install, status_t,
911 private_child_sa_t *this, chunk_t encr, chunk_t integ, uint32_t spi,
912 uint16_t cpi, bool initiator, bool inbound, bool tfcv3)
913 {
914 return install_internal(this, encr, integ, spi, cpi, initiator, inbound,
915 tfcv3);
916 }
917
918 /**
919 * Check kernel interface if policy updates are required
920 */
921 static bool require_policy_update()
922 {
923 kernel_feature_t f;
924
925 f = charon->kernel->get_features(charon->kernel);
926 return !(f & KERNEL_NO_POLICY_UPDATES);
927 }
928
929 /**
930 * Prepare SA config to install/delete policies
931 */
932 static void prepare_sa_cfg(private_child_sa_t *this, ipsec_sa_cfg_t *my_sa,
933 ipsec_sa_cfg_t *other_sa)
934 {
935 enumerator_t *enumerator;
936
937 *my_sa = (ipsec_sa_cfg_t){
938 .mode = this->mode,
939 .reqid = this->reqid,
940 .ipcomp = {
941 .transform = this->ipcomp,
942 },
943 };
944 *other_sa = *my_sa;
945
946 my_sa->ipcomp.cpi = this->my_cpi;
947 other_sa->ipcomp.cpi = this->other_cpi;
948
949 if (this->protocol == PROTO_ESP)
950 {
951 my_sa->esp.use = TRUE;
952 my_sa->esp.spi = this->my_spi;
953 other_sa->esp.use = TRUE;
954 other_sa->esp.spi = this->other_spi;
955 }
956 else
957 {
958 my_sa->ah.use = TRUE;
959 my_sa->ah.spi = this->my_spi;
960 other_sa->ah.use = TRUE;
961 other_sa->ah.spi = this->other_spi;
962 }
963
964 enumerator = create_policy_enumerator(this);
965 while (enumerator->enumerate(enumerator, NULL, NULL))
966 {
967 my_sa->policy_count++;
968 other_sa->policy_count++;
969 }
970 enumerator->destroy(enumerator);
971 }
972
973 /**
974 * Install inbound policie(s): in, fwd
975 */
976 static status_t install_policies_inbound(private_child_sa_t *this,
977 host_t *my_addr, host_t *other_addr, traffic_selector_t *my_ts,
978 traffic_selector_t *other_ts, ipsec_sa_cfg_t *my_sa,
979 ipsec_sa_cfg_t *other_sa, policy_type_t type,
980 policy_priority_t priority, uint32_t manual_prio)
981 {
982 kernel_ipsec_policy_id_t in_id = {
983 .dir = POLICY_IN,
984 .src_ts = other_ts,
985 .dst_ts = my_ts,
986 .mark = this->mark_in,
987 };
988 kernel_ipsec_manage_policy_t in_policy = {
989 .type = type,
990 .prio = priority,
991 .manual_prio = manual_prio,
992 .src = other_addr,
993 .dst = my_addr,
994 .sa = my_sa,
995 };
996 status_t status = SUCCESS;
997
998 status |= charon->kernel->add_policy(charon->kernel, &in_id, &in_policy);
999 if (this->mode != MODE_TRANSPORT)
1000 {
1001 in_id.dir = POLICY_FWD;
1002 status |= charon->kernel->add_policy(charon->kernel, &in_id, &in_policy);
1003 }
1004 return status;
1005 }
1006
1007 /**
1008 * Install outbound policie(s): out, [fwd]
1009 */
1010 static status_t install_policies_outbound(private_child_sa_t *this,
1011 host_t *my_addr, host_t *other_addr, traffic_selector_t *my_ts,
1012 traffic_selector_t *other_ts, ipsec_sa_cfg_t *my_sa,
1013 ipsec_sa_cfg_t *other_sa, policy_type_t type,
1014 policy_priority_t priority, uint32_t manual_prio)
1015 {
1016 kernel_ipsec_policy_id_t out_id = {
1017 .dir = POLICY_OUT,
1018 .src_ts = my_ts,
1019 .dst_ts = other_ts,
1020 .mark = this->mark_out,
1021 .interface = this->config->get_interface(this->config),
1022 };
1023 kernel_ipsec_manage_policy_t out_policy = {
1024 .type = type,
1025 .prio = priority,
1026 .manual_prio = manual_prio,
1027 .src = my_addr,
1028 .dst = other_addr,
1029 .sa = other_sa,
1030 };
1031 status_t status = SUCCESS;
1032
1033 status |= charon->kernel->add_policy(charon->kernel, &out_id, &out_policy);
1034
1035 if (this->mode != MODE_TRANSPORT && this->policies_fwd_out)
1036 {
1037 /* install an "outbound" FWD policy in case there is a drop policy
1038 * matching outbound forwarded traffic, to allow another tunnel to use
1039 * the reversed subnets and do the same we don't set a reqid (this also
1040 * allows the kernel backend to distinguish between the two types of
1041 * FWD policies). To avoid problems with symmetrically overlapping
1042 * policies of two SAs we install them with reduced priority. As they
1043 * basically act as bypass policies for drop policies we use a higher
1044 * priority than is used for them. */
1045 out_id.dir = POLICY_FWD;
1046 other_sa->reqid = 0;
1047 if (priority == POLICY_PRIORITY_DEFAULT)
1048 {
1049 out_policy.prio = POLICY_PRIORITY_ROUTED;
1050 }
1051 status |= charon->kernel->add_policy(charon->kernel, &out_id,
1052 &out_policy);
1053 /* reset the reqid for any other further policies */
1054 other_sa->reqid = this->reqid;
1055 }
1056 return status;
1057 }
1058
1059 /**
1060 * Install all policies
1061 */
1062 static status_t install_policies_internal(private_child_sa_t *this,
1063 host_t *my_addr, host_t *other_addr, traffic_selector_t *my_ts,
1064 traffic_selector_t *other_ts, ipsec_sa_cfg_t *my_sa,
1065 ipsec_sa_cfg_t *other_sa, policy_type_t type,
1066 policy_priority_t priority, uint32_t manual_prio, bool outbound)
1067 {
1068 status_t status = SUCCESS;
1069
1070 status |= install_policies_inbound(this, my_addr, other_addr, my_ts,
1071 other_ts, my_sa, other_sa, type, priority, manual_prio);
1072 if (outbound)
1073 {
1074 status |= install_policies_outbound(this, my_addr, other_addr, my_ts,
1075 other_ts, my_sa, other_sa, type, priority, manual_prio);
1076 }
1077 return status;
1078 }
1079
1080 /**
1081 * Delete inbound policies: in, fwd
1082 */
1083 static void del_policies_inbound(private_child_sa_t *this,
1084 host_t *my_addr, host_t *other_addr, traffic_selector_t *my_ts,
1085 traffic_selector_t *other_ts, ipsec_sa_cfg_t *my_sa,
1086 ipsec_sa_cfg_t *other_sa, policy_type_t type,
1087 policy_priority_t priority, uint32_t manual_prio)
1088 {
1089 kernel_ipsec_policy_id_t in_id = {
1090 .dir = POLICY_IN,
1091 .src_ts = other_ts,
1092 .dst_ts = my_ts,
1093 .mark = this->mark_in,
1094 };
1095 kernel_ipsec_manage_policy_t in_policy = {
1096 .type = type,
1097 .prio = priority,
1098 .manual_prio = manual_prio,
1099 .src = other_addr,
1100 .dst = my_addr,
1101 .sa = my_sa,
1102 };
1103
1104 charon->kernel->del_policy(charon->kernel, &in_id, &in_policy);
1105
1106 if (this->mode != MODE_TRANSPORT)
1107 {
1108 in_id.dir = POLICY_FWD;
1109 charon->kernel->del_policy(charon->kernel, &in_id, &in_policy);
1110 }
1111 }
1112
1113 /**
1114 * Delete outbound policies: out, [fwd]
1115 */
1116 static void del_policies_outbound(private_child_sa_t *this,
1117 host_t *my_addr, host_t *other_addr, traffic_selector_t *my_ts,
1118 traffic_selector_t *other_ts, ipsec_sa_cfg_t *my_sa,
1119 ipsec_sa_cfg_t *other_sa, policy_type_t type,
1120 policy_priority_t priority, uint32_t manual_prio)
1121 {
1122 kernel_ipsec_policy_id_t out_id = {
1123 .dir = POLICY_OUT,
1124 .src_ts = my_ts,
1125 .dst_ts = other_ts,
1126 .mark = this->mark_out,
1127 .interface = this->config->get_interface(this->config),
1128 };
1129 kernel_ipsec_manage_policy_t out_policy = {
1130 .type = type,
1131 .prio = priority,
1132 .manual_prio = manual_prio,
1133 .src = my_addr,
1134 .dst = other_addr,
1135 .sa = other_sa,
1136 };
1137
1138 charon->kernel->del_policy(charon->kernel, &out_id, &out_policy);
1139
1140 if (this->mode != MODE_TRANSPORT && this->policies_fwd_out)
1141 {
1142 out_id.dir = POLICY_FWD;
1143 other_sa->reqid = 0;
1144 if (priority == POLICY_PRIORITY_DEFAULT)
1145 {
1146 out_policy.prio = POLICY_PRIORITY_ROUTED;
1147 }
1148 charon->kernel->del_policy(charon->kernel, &out_id, &out_policy);
1149 other_sa->reqid = this->reqid;
1150 }
1151 }
1152
1153 /**
1154 * Delete in- and outbound policies
1155 */
1156 static void del_policies_internal(private_child_sa_t *this,
1157 host_t *my_addr, host_t *other_addr, traffic_selector_t *my_ts,
1158 traffic_selector_t *other_ts, ipsec_sa_cfg_t *my_sa,
1159 ipsec_sa_cfg_t *other_sa, policy_type_t type,
1160 policy_priority_t priority, uint32_t manual_prio, bool outbound)
1161 {
1162 if (outbound)
1163 {
1164 del_policies_outbound(this, my_addr, other_addr, my_ts, other_ts, my_sa,
1165 other_sa, type, priority, manual_prio);
1166 }
1167 del_policies_inbound(this, my_addr, other_addr, my_ts, other_ts, my_sa,
1168 other_sa, type, priority, manual_prio);
1169 }
1170
1171 METHOD(child_sa_t, set_policies, void,
1172 private_child_sa_t *this, linked_list_t *my_ts_list,
1173 linked_list_t *other_ts_list)
1174 {
1175 enumerator_t *enumerator;
1176 traffic_selector_t *my_ts, *other_ts;
1177
1178 if (array_count(this->my_ts))
1179 {
1180 array_destroy_offset(this->my_ts,
1181 offsetof(traffic_selector_t, destroy));
1182 this->my_ts = array_create(0, 0);
1183 }
1184 enumerator = my_ts_list->create_enumerator(my_ts_list);
1185 while (enumerator->enumerate(enumerator, &my_ts))
1186 {
1187 array_insert(this->my_ts, ARRAY_TAIL, my_ts->clone(my_ts));
1188 }
1189 enumerator->destroy(enumerator);
1190 array_sort(this->my_ts, (void*)traffic_selector_cmp, NULL);
1191
1192 if (array_count(this->other_ts))
1193 {
1194 array_destroy_offset(this->other_ts,
1195 offsetof(traffic_selector_t, destroy));
1196 this->other_ts = array_create(0, 0);
1197 }
1198 enumerator = other_ts_list->create_enumerator(other_ts_list);
1199 while (enumerator->enumerate(enumerator, &other_ts))
1200 {
1201 array_insert(this->other_ts, ARRAY_TAIL, other_ts->clone(other_ts));
1202 }
1203 enumerator->destroy(enumerator);
1204 array_sort(this->other_ts, (void*)traffic_selector_cmp, NULL);
1205 }
1206
1207 METHOD(child_sa_t, install_policies, status_t,
1208 private_child_sa_t *this)
1209 {
1210 enumerator_t *enumerator;
1211 linked_list_t *my_ts_list, *other_ts_list;
1212 traffic_selector_t *my_ts, *other_ts;
1213 status_t status = SUCCESS;
1214 bool install_outbound = FALSE;
1215
1216 if (!this->reqid_allocated && !this->static_reqid)
1217 {
1218 my_ts_list = linked_list_create_from_enumerator(
1219 array_create_enumerator(this->my_ts));
1220 other_ts_list = linked_list_create_from_enumerator(
1221 array_create_enumerator(this->other_ts));
1222 status = charon->kernel->alloc_reqid(
1223 charon->kernel, my_ts_list, other_ts_list,
1224 this->mark_in, this->mark_out, &this->reqid);
1225 my_ts_list->destroy(my_ts_list);
1226 other_ts_list->destroy(other_ts_list);
1227 if (status != SUCCESS)
1228 {
1229 return status;
1230 }
1231 this->reqid_allocated = TRUE;
1232 }
1233
1234 if (!(this->outbound_state & CHILD_OUTBOUND_REGISTERED))
1235 {
1236 install_outbound = TRUE;
1237 this->outbound_state |= CHILD_OUTBOUND_POLICIES;
1238 }
1239
1240 if (!this->config->has_option(this->config, OPT_NO_POLICIES))
1241 {
1242 policy_priority_t priority;
1243 ipsec_sa_cfg_t my_sa, other_sa;
1244 uint32_t manual_prio;
1245
1246 prepare_sa_cfg(this, &my_sa, &other_sa);
1247 manual_prio = this->config->get_manual_prio(this->config);
1248
1249 /* if we're not in state CHILD_INSTALLING (i.e. if there is no SAD
1250 * entry) we install a trap policy */
1251 this->trap = this->state == CHILD_CREATED;
1252 priority = this->trap ? POLICY_PRIORITY_ROUTED
1253 : POLICY_PRIORITY_DEFAULT;
1254
1255 /* enumerate pairs of traffic selectors */
1256 enumerator = create_policy_enumerator(this);
1257 while (enumerator->enumerate(enumerator, &my_ts, &other_ts))
1258 {
1259 status |= install_policies_internal(this, this->my_addr,
1260 this->other_addr, my_ts, other_ts,
1261 &my_sa, &other_sa, POLICY_IPSEC, priority,
1262 manual_prio, install_outbound);
1263 if (status != SUCCESS)
1264 {
1265 break;
1266 }
1267 }
1268 enumerator->destroy(enumerator);
1269 }
1270
1271 if (status == SUCCESS && this->trap)
1272 {
1273 set_state(this, CHILD_ROUTED);
1274 }
1275 return status;
1276 }
1277
1278 METHOD(child_sa_t, register_outbound, status_t,
1279 private_child_sa_t *this, chunk_t encr, chunk_t integ, uint32_t spi,
1280 uint16_t cpi, bool tfcv3)
1281 {
1282 status_t status;
1283
1284 /* if the kernel supports installing SPIs with policies we install the
1285 * SA immediately as it will only be used once we update the policies */
1286 if (charon->kernel->get_features(charon->kernel) & KERNEL_POLICY_SPI)
1287 {
1288 status = install_internal(this, encr, integ, spi, cpi, FALSE, FALSE,
1289 tfcv3);
1290 }
1291 else
1292 {
1293 DBG2(DBG_CHD, "registering outbound %N SA", protocol_id_names,
1294 this->protocol);
1295 DBG2(DBG_CHD, " SPI 0x%.8x, src %H dst %H", ntohl(spi), this->my_addr,
1296 this->other_addr);
1297
1298 this->other_spi = spi;
1299 this->other_cpi = cpi;
1300 this->encr_r = chunk_clone(encr);
1301 this->integ_r = chunk_clone(integ);
1302 this->tfcv3 = tfcv3;
1303 status = SUCCESS;
1304 }
1305 this->outbound_state |= CHILD_OUTBOUND_REGISTERED;
1306 return status;
1307 }
1308
1309 METHOD(child_sa_t, install_outbound, status_t,
1310 private_child_sa_t *this)
1311 {
1312 enumerator_t *enumerator;
1313 traffic_selector_t *my_ts, *other_ts;
1314 status_t status = SUCCESS;
1315
1316 if (!(this->outbound_state & CHILD_OUTBOUND_SA))
1317 {
1318 status = install_internal(this, this->encr_r, this->integ_r,
1319 this->other_spi, this->other_cpi, FALSE,
1320 FALSE, this->tfcv3);
1321 chunk_clear(&this->encr_r);
1322 chunk_clear(&this->integ_r);
1323 }
1324 this->outbound_state &= ~CHILD_OUTBOUND_REGISTERED;
1325 if (status != SUCCESS)
1326 {
1327 return status;
1328 }
1329 if (!this->config->has_option(this->config, OPT_NO_POLICIES) &&
1330 !(this->outbound_state & CHILD_OUTBOUND_POLICIES))
1331 {
1332 ipsec_sa_cfg_t my_sa, other_sa;
1333 uint32_t manual_prio;
1334
1335 prepare_sa_cfg(this, &my_sa, &other_sa);
1336 manual_prio = this->config->get_manual_prio(this->config);
1337
1338 enumerator = create_policy_enumerator(this);
1339 while (enumerator->enumerate(enumerator, &my_ts, &other_ts))
1340 {
1341 status |= install_policies_outbound(this, this->my_addr,
1342 this->other_addr, my_ts, other_ts,
1343 &my_sa, &other_sa, POLICY_IPSEC,
1344 POLICY_PRIORITY_DEFAULT, manual_prio);
1345 if (status != SUCCESS)
1346 {
1347 break;
1348 }
1349 }
1350 enumerator->destroy(enumerator);
1351 }
1352 this->outbound_state |= CHILD_OUTBOUND_POLICIES;
1353 return status;
1354 }
1355
1356 METHOD(child_sa_t, remove_outbound, void,
1357 private_child_sa_t *this)
1358 {
1359 enumerator_t *enumerator;
1360 traffic_selector_t *my_ts, *other_ts;
1361
1362 if (!(this->outbound_state & CHILD_OUTBOUND_SA))
1363 {
1364 if (this->outbound_state & CHILD_OUTBOUND_REGISTERED)
1365 {
1366 chunk_clear(&this->encr_r);
1367 chunk_clear(&this->integ_r);
1368 this->outbound_state = CHILD_OUTBOUND_NONE;
1369 }
1370 return;
1371 }
1372
1373 if (!this->config->has_option(this->config, OPT_NO_POLICIES) &&
1374 (this->outbound_state & CHILD_OUTBOUND_POLICIES))
1375 {
1376 ipsec_sa_cfg_t my_sa, other_sa;
1377 uint32_t manual_prio;
1378
1379 prepare_sa_cfg(this, &my_sa, &other_sa);
1380 manual_prio = this->config->get_manual_prio(this->config);
1381
1382 enumerator = create_policy_enumerator(this);
1383 while (enumerator->enumerate(enumerator, &my_ts, &other_ts))
1384 {
1385 del_policies_outbound(this, this->my_addr, this->other_addr,
1386 my_ts, other_ts, &my_sa, &other_sa,
1387 POLICY_IPSEC, POLICY_PRIORITY_DEFAULT,
1388 manual_prio);
1389 }
1390 enumerator->destroy(enumerator);
1391 }
1392
1393 kernel_ipsec_sa_id_t id = {
1394 .src = this->my_addr,
1395 .dst = this->other_addr,
1396 .spi = this->other_spi,
1397 .proto = proto_ike2ip(this->protocol),
1398 .mark = this->mark_out,
1399 };
1400 kernel_ipsec_del_sa_t sa = {
1401 .cpi = this->other_cpi,
1402 };
1403 charon->kernel->del_sa(charon->kernel, &id, &sa);
1404 this->outbound_state = CHILD_OUTBOUND_NONE;
1405 }
1406
1407 METHOD(child_sa_t, set_rekey_spi, void,
1408 private_child_sa_t *this, uint32_t spi)
1409 {
1410 this->rekey_spi = spi;
1411 }
1412
1413 METHOD(child_sa_t, get_rekey_spi, uint32_t,
1414 private_child_sa_t *this)
1415 {
1416 return this->rekey_spi;
1417 }
1418
1419 CALLBACK(reinstall_vip, void,
1420 host_t *vip, va_list args)
1421 {
1422 host_t *me;
1423 char *iface;
1424
1425 VA_ARGS_VGET(args, me);
1426 if (charon->kernel->get_interface(charon->kernel, me, &iface))
1427 {
1428 charon->kernel->del_ip(charon->kernel, vip, -1, TRUE);
1429 charon->kernel->add_ip(charon->kernel, vip, -1, iface);
1430 free(iface);
1431 }
1432 }
1433
1434 /**
1435 * Update addresses and encap state of IPsec SAs in the kernel
1436 */
1437 static status_t update_sas(private_child_sa_t *this, host_t *me, host_t *other,
1438 bool encap)
1439 {
1440 /* update our (initiator) SA */
1441 if (this->my_spi)
1442 {
1443 kernel_ipsec_sa_id_t id = {
1444 .src = this->other_addr,
1445 .dst = this->my_addr,
1446 .spi = this->my_spi,
1447 .proto = proto_ike2ip(this->protocol),
1448 .mark = mark_in_sa(this),
1449 };
1450 kernel_ipsec_update_sa_t sa = {
1451 .cpi = this->ipcomp != IPCOMP_NONE ? this->my_cpi : 0,
1452 .new_src = other,
1453 .new_dst = me,
1454 .encap = this->encap,
1455 .new_encap = encap,
1456 };
1457 if (charon->kernel->update_sa(charon->kernel, &id,
1458 &sa) == NOT_SUPPORTED)
1459 {
1460 return NOT_SUPPORTED;
1461 }
1462 }
1463
1464 /* update his (responder) SA */
1465 if (this->other_spi && (this->outbound_state & CHILD_OUTBOUND_SA))
1466 {
1467 kernel_ipsec_sa_id_t id = {
1468 .src = this->my_addr,
1469 .dst = this->other_addr,
1470 .spi = this->other_spi,
1471 .proto = proto_ike2ip(this->protocol),
1472 .mark = this->mark_out,
1473 };
1474 kernel_ipsec_update_sa_t sa = {
1475 .cpi = this->ipcomp != IPCOMP_NONE ? this->other_cpi : 0,
1476 .new_src = me,
1477 .new_dst = other,
1478 .encap = this->encap,
1479 .new_encap = encap,
1480 };
1481 if (charon->kernel->update_sa(charon->kernel, &id,
1482 &sa) == NOT_SUPPORTED)
1483 {
1484 return NOT_SUPPORTED;
1485 }
1486 }
1487 /* we currently ignore the actual return values above */
1488 return SUCCESS;
1489 }
1490
1491 METHOD(child_sa_t, update, status_t,
1492 private_child_sa_t *this, host_t *me, host_t *other, linked_list_t *vips,
1493 bool encap)
1494 {
1495 child_sa_state_t old;
1496 bool transport_proxy_mode;
1497
1498 /* anything changed at all? */
1499 if (me->equals(me, this->my_addr) &&
1500 other->equals(other, this->other_addr) && this->encap == encap)
1501 {
1502 return SUCCESS;
1503 }
1504
1505 old = this->state;
1506 set_state(this, CHILD_UPDATING);
1507 transport_proxy_mode = this->mode == MODE_TRANSPORT &&
1508 this->config->has_option(this->config,
1509 OPT_PROXY_MODE);
1510
1511 if (!this->config->has_option(this->config, OPT_NO_POLICIES) &&
1512 require_policy_update())
1513 {
1514 ipsec_sa_cfg_t my_sa, other_sa;
1515 enumerator_t *enumerator;
1516 traffic_selector_t *my_ts, *other_ts;
1517 uint32_t manual_prio;
1518 status_t state;
1519 bool outbound;
1520
1521 prepare_sa_cfg(this, &my_sa, &other_sa);
1522 manual_prio = this->config->get_manual_prio(this->config);
1523 outbound = (this->outbound_state & CHILD_OUTBOUND_POLICIES);
1524
1525 enumerator = create_policy_enumerator(this);
1526 while (enumerator->enumerate(enumerator, &my_ts, &other_ts))
1527 {
1528 /* install drop policy to avoid traffic leaks, acquires etc. */
1529 if (outbound)
1530 {
1531 install_policies_outbound(this, this->my_addr, this->other_addr,
1532 my_ts, other_ts, &my_sa, &other_sa, POLICY_DROP,
1533 POLICY_PRIORITY_DEFAULT, manual_prio);
1534 }
1535 /* remove old policies */
1536 del_policies_internal(this, this->my_addr, this->other_addr,
1537 my_ts, other_ts, &my_sa, &other_sa, POLICY_IPSEC,
1538 POLICY_PRIORITY_DEFAULT, manual_prio, outbound);
1539 }
1540 enumerator->destroy(enumerator);
1541
1542 /* update the IPsec SAs */
1543 state = update_sas(this, me, other, encap);
1544
1545 enumerator = create_policy_enumerator(this);
1546 while (enumerator->enumerate(enumerator, &my_ts, &other_ts))
1547 {
1548 traffic_selector_t *old_my_ts = NULL, *old_other_ts = NULL;
1549
1550 /* reinstall the previous policies if we can't update the SAs */
1551 if (state == NOT_SUPPORTED)
1552 {
1553 install_policies_internal(this, this->my_addr, this->other_addr,
1554 my_ts, other_ts, &my_sa, &other_sa, POLICY_IPSEC,
1555 POLICY_PRIORITY_DEFAULT, manual_prio, outbound);
1556 }
1557 else
1558 {
1559 /* check if we have to update a "dynamic" traffic selector */
1560 if (!me->ip_equals(me, this->my_addr) &&
1561 my_ts->is_host(my_ts, this->my_addr))
1562 {
1563 old_my_ts = my_ts->clone(my_ts);
1564 my_ts->set_address(my_ts, me);
1565 }
1566 if (!other->ip_equals(other, this->other_addr) &&
1567 other_ts->is_host(other_ts, this->other_addr))
1568 {
1569 old_other_ts = other_ts->clone(other_ts);
1570 other_ts->set_address(other_ts, other);
1571 }
1572
1573 /* we reinstall the virtual IP to handle interface roaming
1574 * correctly */
1575 vips->invoke_function(vips, reinstall_vip, me);
1576
1577 /* reinstall updated policies */
1578 install_policies_internal(this, me, other, my_ts, other_ts,
1579 &my_sa, &other_sa, POLICY_IPSEC,
1580 POLICY_PRIORITY_DEFAULT, manual_prio, outbound);
1581 }
1582 /* remove the drop policy */
1583 if (outbound)
1584 {
1585 del_policies_outbound(this, this->my_addr, this->other_addr,
1586 old_my_ts ?: my_ts, old_other_ts ?: other_ts,
1587 &my_sa, &other_sa, POLICY_DROP,
1588 POLICY_PRIORITY_DEFAULT, 0);
1589 }
1590
1591 DESTROY_IF(old_my_ts);
1592 DESTROY_IF(old_other_ts);
1593 }
1594 enumerator->destroy(enumerator);
1595
1596 if (state == NOT_SUPPORTED)
1597 {
1598 set_state(this, old);
1599 return NOT_SUPPORTED;
1600 }
1601
1602 }
1603 else if (!transport_proxy_mode)
1604 {
1605 if (update_sas(this, me, other, encap) == NOT_SUPPORTED)
1606 {
1607 set_state(this, old);
1608 return NOT_SUPPORTED;
1609 }
1610 }
1611
1612 if (!transport_proxy_mode)
1613 {
1614 /* apply hosts */
1615 if (!me->equals(me, this->my_addr))
1616 {
1617 this->my_addr->destroy(this->my_addr);
1618 this->my_addr = me->clone(me);
1619 }
1620 if (!other->equals(other, this->other_addr))
1621 {
1622 this->other_addr->destroy(this->other_addr);
1623 this->other_addr = other->clone(other);
1624 }
1625 }
1626
1627 this->encap = encap;
1628 set_state(this, old);
1629
1630 return SUCCESS;
1631 }
1632
1633 METHOD(child_sa_t, destroy, void,
1634 private_child_sa_t *this)
1635 {
1636 enumerator_t *enumerator;
1637 traffic_selector_t *my_ts, *other_ts;
1638 policy_priority_t priority;
1639
1640 priority = this->trap ? POLICY_PRIORITY_ROUTED : POLICY_PRIORITY_DEFAULT;
1641
1642 set_state(this, CHILD_DESTROYING);
1643
1644 if (!this->config->has_option(this->config, OPT_NO_POLICIES))
1645 {
1646 ipsec_sa_cfg_t my_sa, other_sa;
1647 uint32_t manual_prio;
1648 bool del_outbound;
1649
1650 prepare_sa_cfg(this, &my_sa, &other_sa);
1651 manual_prio = this->config->get_manual_prio(this->config);
1652 del_outbound = (this->outbound_state & CHILD_OUTBOUND_POLICIES) ||
1653 this->trap;
1654
1655 /* delete all policies in the kernel */
1656 enumerator = create_policy_enumerator(this);
1657 while (enumerator->enumerate(enumerator, &my_ts, &other_ts))
1658 {
1659 del_policies_internal(this, this->my_addr,
1660 this->other_addr, my_ts, other_ts, &my_sa, &other_sa,
1661 POLICY_IPSEC, priority, manual_prio, del_outbound);
1662 }
1663 enumerator->destroy(enumerator);
1664 }
1665
1666 /* delete SAs in the kernel, if they are set up */
1667 if (this->my_spi)
1668 {
1669 kernel_ipsec_sa_id_t id = {
1670 .src = this->other_addr,
1671 .dst = this->my_addr,
1672 .spi = this->my_spi,
1673 .proto = proto_ike2ip(this->protocol),
1674 .mark = mark_in_sa(this),
1675 };
1676 kernel_ipsec_del_sa_t sa = {
1677 .cpi = this->my_cpi,
1678 };
1679 charon->kernel->del_sa(charon->kernel, &id, &sa);
1680 }
1681 if (this->other_spi && (this->outbound_state & CHILD_OUTBOUND_SA))
1682 {
1683 kernel_ipsec_sa_id_t id = {
1684 .src = this->my_addr,
1685 .dst = this->other_addr,
1686 .spi = this->other_spi,
1687 .proto = proto_ike2ip(this->protocol),
1688 .mark = this->mark_out,
1689 };
1690 kernel_ipsec_del_sa_t sa = {
1691 .cpi = this->other_cpi,
1692 };
1693 charon->kernel->del_sa(charon->kernel, &id, &sa);
1694 }
1695
1696 if (this->reqid_allocated)
1697 {
1698 if (charon->kernel->release_reqid(charon->kernel,
1699 this->reqid, this->mark_in, this->mark_out) != SUCCESS)
1700 {
1701 DBG1(DBG_CHD, "releasing reqid %u failed", this->reqid);
1702 }
1703 }
1704
1705 array_destroy_offset(this->my_ts, offsetof(traffic_selector_t, destroy));
1706 array_destroy_offset(this->other_ts, offsetof(traffic_selector_t, destroy));
1707 this->my_addr->destroy(this->my_addr);
1708 this->other_addr->destroy(this->other_addr);
1709 DESTROY_IF(this->proposal);
1710 this->config->destroy(this->config);
1711 chunk_clear(&this->encr_r);
1712 chunk_clear(&this->integ_r);
1713 free(this);
1714 }
1715
1716 /**
1717 * Get proxy address for one side, if any
1718 */
1719 static host_t* get_proxy_addr(child_cfg_t *config, host_t *ike, bool local)
1720 {
1721 host_t *host = NULL;
1722 uint8_t mask;
1723 enumerator_t *enumerator;
1724 linked_list_t *ts_list, *list;
1725 traffic_selector_t *ts;
1726
1727 list = linked_list_create_with_items(ike, NULL);
1728 ts_list = config->get_traffic_selectors(config, local, NULL, list, FALSE);
1729 list->destroy(list);
1730
1731 enumerator = ts_list->create_enumerator(ts_list);
1732 while (enumerator->enumerate(enumerator, &ts))
1733 {
1734 if (ts->is_host(ts, NULL) && ts->to_subnet(ts, &host, &mask))
1735 {
1736 DBG1(DBG_CHD, "%s address: %H is a transport mode proxy for %H",
1737 local ? "my" : "other", ike, host);
1738 break;
1739 }
1740 }
1741 enumerator->destroy(enumerator);
1742 ts_list->destroy_offset(ts_list, offsetof(traffic_selector_t, destroy));
1743
1744 if (!host)
1745 {
1746 host = ike->clone(ike);
1747 }
1748 return host;
1749 }
1750
1751 /**
1752 * Described in header.
1753 */
1754 child_sa_t * child_sa_create(host_t *me, host_t* other,
1755 child_cfg_t *config, uint32_t reqid, bool encap,
1756 u_int mark_in, u_int mark_out)
1757 {
1758 private_child_sa_t *this;
1759 static refcount_t unique_id = 0, unique_mark = 0;
1760 refcount_t mark = 0;
1761
1762 INIT(this,
1763 .public = {
1764 .get_name = _get_name,
1765 .get_reqid = _get_reqid,
1766 .get_unique_id = _get_unique_id,
1767 .get_config = _get_config,
1768 .get_state = _get_state,
1769 .set_state = _set_state,
1770 .get_outbound_state = _get_outbound_state,
1771 .get_spi = _get_spi,
1772 .get_cpi = _get_cpi,
1773 .get_protocol = _get_protocol,
1774 .set_protocol = _set_protocol,
1775 .get_mode = _get_mode,
1776 .set_mode = _set_mode,
1777 .get_proposal = _get_proposal,
1778 .set_proposal = _set_proposal,
1779 .get_lifetime = _get_lifetime,
1780 .get_installtime = _get_installtime,
1781 .get_usestats = _get_usestats,
1782 .get_mark = _get_mark,
1783 .has_encap = _has_encap,
1784 .get_ipcomp = _get_ipcomp,
1785 .set_ipcomp = _set_ipcomp,
1786 .get_close_action = _get_close_action,
1787 .set_close_action = _set_close_action,
1788 .get_dpd_action = _get_dpd_action,
1789 .set_dpd_action = _set_dpd_action,
1790 .alloc_spi = _alloc_spi,
1791 .alloc_cpi = _alloc_cpi,
1792 .install = _install,
1793 .register_outbound = _register_outbound,
1794 .install_outbound = _install_outbound,
1795 .remove_outbound = _remove_outbound,
1796 .set_rekey_spi = _set_rekey_spi,
1797 .get_rekey_spi = _get_rekey_spi,
1798 .update = _update,
1799 .set_policies = _set_policies,
1800 .install_policies = _install_policies,
1801 .create_ts_enumerator = _create_ts_enumerator,
1802 .create_policy_enumerator = _create_policy_enumerator,
1803 .destroy = _destroy,
1804 },
1805 .encap = encap,
1806 .ipcomp = IPCOMP_NONE,
1807 .state = CHILD_CREATED,
1808 .my_ts = array_create(0, 0),
1809 .other_ts = array_create(0, 0),
1810 .protocol = PROTO_NONE,
1811 .mode = MODE_TUNNEL,
1812 .close_action = config->get_close_action(config),
1813 .dpd_action = config->get_dpd_action(config),
1814 .reqid = config->get_reqid(config),
1815 .unique_id = ref_get(&unique_id),
1816 .mark_in = config->get_mark(config, TRUE),
1817 .mark_out = config->get_mark(config, FALSE),
1818 .install_time = time_monotonic(NULL),
1819 .policies_fwd_out = config->has_option(config, OPT_FWD_OUT_POLICIES),
1820 );
1821
1822 this->config = config;
1823 config->get_ref(config);
1824
1825 if (mark_in)
1826 {
1827 this->mark_in.value = mark_in;
1828 }
1829 if (mark_out)
1830 {
1831 this->mark_out.value = mark_out;
1832 }
1833
1834 if (MARK_IS_UNIQUE(this->mark_in.value) ||
1835 MARK_IS_UNIQUE(this->mark_out.value))
1836 {
1837 bool unique_dir;
1838
1839 unique_dir = this->mark_in.value == MARK_UNIQUE_DIR ||
1840 this->mark_out.value == MARK_UNIQUE_DIR;
1841
1842 if (!unique_dir)
1843 {
1844 mark = ref_get(&unique_mark);
1845 }
1846 if (MARK_IS_UNIQUE(this->mark_in.value))
1847 {
1848 if (unique_dir)
1849 {
1850 mark = ref_get(&unique_mark);
1851 }
1852 this->mark_in.value = mark;
1853 }
1854 if (MARK_IS_UNIQUE(this->mark_out.value))
1855 {
1856 if (unique_dir)
1857 {
1858 mark = ref_get(&unique_mark);
1859 }
1860 this->mark_out.value = mark;
1861 }
1862 }
1863
1864 if (!this->reqid)
1865 {
1866 /* reuse old reqid if we are rekeying an existing CHILD_SA and when
1867 * initiating a trap policy. While the reqid cache would find the same
1868 * reqid for our selectors, this does not work in a special case: If an
1869 * SA is triggered by a trap policy, but the negotiated TS get
1870 * narrowed, we still must reuse the same reqid to successfully
1871 * replace the temporary SA on the kernel level. Rekeying such an SA
1872 * requires an explicit reqid, as the cache currently knows the original
1873 * selectors only for that reqid. */
1874 this->reqid = reqid;
1875 }
1876 else
1877 {
1878 this->static_reqid = TRUE;
1879 }
1880
1881 /* MIPv6 proxy transport mode sets SA endpoints to TS hosts */
1882 if (config->get_mode(config) == MODE_TRANSPORT &&
1883 config->has_option(config, OPT_PROXY_MODE))
1884 {
1885 this->mode = MODE_TRANSPORT;
1886
1887 this->my_addr = get_proxy_addr(config, me, TRUE);
1888 this->other_addr = get_proxy_addr(config, other, FALSE);
1889 }
1890 else
1891 {
1892 this->my_addr = me->clone(me);
1893 this->other_addr = other->clone(other);
1894 }
1895 return &this->public;
1896 }