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