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