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