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