further mobike improvements, regarding to NAT-T
[strongswan.git] / src / charon / sa / child_sa.c
1 /**
2 * @file child_sa.c
3 *
4 * @brief Implementation of child_sa_t.
5 *
6 */
7
8 /*
9 * Copyright (C) 2005-2007 Martin Willi
10 * Copyright (C) 2006 Tobias Brunner, Daniel Roethlisberger
11 * Copyright (C) 2005 Jan Hutter
12 * Hochschule fuer Technik Rapperswil
13 *
14 * This program is free software; you can redistribute it and/or modify it
15 * under the terms of the GNU General Public License as published by the
16 * Free Software Foundation; either version 2 of the License, or (at your
17 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
18 *
19 * This program is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
21 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 * for more details.
23 */
24
25 #define _GNU_SOURCE
26 #include "child_sa.h"
27
28 #include <stdio.h>
29 #include <string.h>
30
31 #include <daemon.h>
32
33 ENUM(child_sa_state_names, CHILD_CREATED, CHILD_DELETING,
34 "CREATED",
35 "ROUTED",
36 "INSTALLED",
37 "REKEYING",
38 "DELETING",
39 );
40
41 typedef struct sa_policy_t sa_policy_t;
42
43 /**
44 * Struct used to store information for a policy. This
45 * is needed since we must provide all this information
46 * for deleting a policy...
47 */
48 struct sa_policy_t {
49 /**
50 * Traffic selector for us
51 */
52 traffic_selector_t *my_ts;
53
54 /**
55 * Traffic selector for other
56 */
57 traffic_selector_t *other_ts;
58 };
59
60 typedef struct private_child_sa_t private_child_sa_t;
61
62 /**
63 * Private data of a child_sa_t \ 1bject.
64 */
65 struct private_child_sa_t {
66 /**
67 * Public interface of child_sa_t.
68 */
69 child_sa_t public;
70
71 struct {
72 /** address of peer */
73 host_t *addr;
74 /** id of peer */
75 identification_t *id;
76 /** actual used SPI, 0 if unused */
77 u_int32_t spi;
78 } me, other;
79
80 /**
81 * Allocated SPI for a ESP proposal candidates
82 */
83 u_int32_t alloc_esp_spi;
84
85 /**
86 * Allocated SPI for a AH proposal candidates
87 */
88 u_int32_t alloc_ah_spi;
89
90 /**
91 * Protocol used to protect this SA, ESP|AH
92 */
93 protocol_id_t protocol;
94
95 /**
96 * List containing sa_policy_t objects
97 */
98 linked_list_t *policies;
99
100 /**
101 * Seperate list for local traffic selectors
102 */
103 linked_list_t *my_ts;
104
105 /**
106 * Seperate list for remote traffic selectors
107 */
108 linked_list_t *other_ts;
109
110 /**
111 * reqid used for this child_sa
112 */
113 u_int32_t reqid;
114
115 /**
116 * encryption algorithm used for this SA
117 */
118 algorithm_t encryption;
119
120 /**
121 * integrity protection algorithm used for this SA
122 */
123 algorithm_t integrity;
124
125 /**
126 * time, on which SA was installed
127 */
128 time_t install_time;
129
130 /**
131 * absolute time when rekeying is sceduled
132 */
133 time_t rekey_time;
134
135 /**
136 * state of the CHILD_SA
137 */
138 child_sa_state_t state;
139
140 /**
141 * Specifies if UDP encapsulation is enabled (NAT traversal)
142 */
143 bool encap;
144
145 /**
146 * mode this SA uses, tunnel/transport
147 */
148 mode_t mode;
149
150 /**
151 * virtual IP assinged to local host
152 */
153 host_t *virtual_ip;
154
155 /**
156 * config used to create this child
157 */
158 child_cfg_t *config;
159 };
160
161 /**
162 * Implementation of child_sa_t.get_name.
163 */
164 static char *get_name(private_child_sa_t *this)
165 {
166 return this->config->get_name(this->config);
167 }
168
169 /**
170 * Implements child_sa_t.get_reqid
171 */
172 static u_int32_t get_reqid(private_child_sa_t *this)
173 {
174 return this->reqid;
175 }
176
177 /**
178 * Implements child_sa_t.get_spi
179 */
180 u_int32_t get_spi(private_child_sa_t *this, bool inbound)
181 {
182 if (inbound)
183 {
184 return this->me.spi;
185 }
186 return this->other.spi;
187 }
188
189 /**
190 * Implements child_sa_t.get_protocol
191 */
192 protocol_id_t get_protocol(private_child_sa_t *this)
193 {
194 return this->protocol;
195 }
196
197 /**
198 * Implements child_sa_t.get_state
199 */
200 static child_sa_state_t get_state(private_child_sa_t *this)
201 {
202 return this->state;
203 }
204
205 /**
206 * Implements child_sa_t.get_config
207 */
208 static child_cfg_t* get_config(private_child_sa_t *this)
209 {
210 return this->config;
211 }
212
213 /**
214 * Implementation of child_sa_t.get_stats.
215 */
216 static void get_stats(private_child_sa_t *this, mode_t *mode,
217 encryption_algorithm_t *encr_algo, size_t *encr_len,
218 integrity_algorithm_t *int_algo, size_t *int_len,
219 u_int32_t *rekey, u_int32_t *use_in, u_int32_t *use_out,
220 u_int32_t *use_fwd)
221 {
222 sa_policy_t *policy;
223 iterator_t *iterator;
224 u_int32_t in = 0, out = 0, fwd = 0, time;
225
226 iterator = this->policies->create_iterator(this->policies, TRUE);
227 while (iterator->iterate(iterator, (void**)&policy))
228 {
229
230 if (charon->kernel_interface->query_policy(charon->kernel_interface,
231 policy->other_ts, policy->my_ts, POLICY_IN, &time) == SUCCESS)
232 {
233 in = max(in, time);
234 }
235 if (charon->kernel_interface->query_policy(charon->kernel_interface,
236 policy->my_ts, policy->other_ts, POLICY_OUT, &time) == SUCCESS)
237 {
238 out = max(out, time);
239 }
240 if (charon->kernel_interface->query_policy(charon->kernel_interface,
241 policy->other_ts, policy->my_ts, POLICY_FWD, &time) == SUCCESS)
242 {
243 fwd = max(fwd, time);
244 }
245 }
246 iterator->destroy(iterator);
247
248 *mode = this->mode;
249 *encr_algo = this->encryption.algorithm;
250 *encr_len = this->encryption.key_size;
251 *int_algo = this->integrity.algorithm;
252 *int_len = this->integrity.key_size;
253 *rekey = this->rekey_time;
254 *use_in = in;
255 *use_out = out;
256 *use_fwd = fwd;
257 }
258
259 /**
260 * Run the up/down script
261 */
262 static void updown(private_child_sa_t *this, bool up)
263 {
264 sa_policy_t *policy;
265 iterator_t *iterator;
266 char *script;
267
268 script = this->config->get_updown(this->config);
269
270 if (script == NULL)
271 {
272 return;
273 }
274
275 iterator = this->policies->create_iterator(this->policies, TRUE);
276 while (iterator->iterate(iterator, (void**)&policy))
277 {
278 char command[1024];
279 char *ifname = NULL;
280 char *my_client, *other_client, *my_client_mask, *other_client_mask;
281 char *pos, *virtual_ip;
282 FILE *shell;
283
284 /* get subnet/bits from string */
285 asprintf(&my_client, "%R", policy->my_ts);
286 pos = strchr(my_client, '/');
287 *pos = '\0';
288 my_client_mask = pos + 1;
289 pos = strchr(my_client_mask, '[');
290 if (pos)
291 {
292 *pos = '\0';
293 }
294 asprintf(&other_client, "%R", policy->other_ts);
295 pos = strchr(other_client, '/');
296 *pos = '\0';
297 other_client_mask = pos + 1;
298 pos = strchr(other_client_mask, '[');
299 if (pos)
300 {
301 *pos = '\0';
302 }
303
304 if (this->virtual_ip)
305 {
306 asprintf(&virtual_ip, "PLUTO_MY_SOURCEIP='%H' ",
307 this->virtual_ip);
308 }
309 else
310 {
311 asprintf(&virtual_ip, "");
312 }
313
314 ifname = charon->kernel_interface->get_interface(charon->kernel_interface,
315 this->me.addr);
316
317 /* build the command with all env variables.
318 * TODO: PLUTO_PEER_CA and PLUTO_NEXT_HOP are currently missing
319 */
320 snprintf(command, sizeof(command),
321 "2>&1 "
322 "PLUTO_VERSION='1.1' "
323 "PLUTO_VERB='%s%s%s' "
324 "PLUTO_CONNECTION='%s' "
325 "PLUTO_INTERFACE='%s' "
326 "PLUTO_REQID='%u' "
327 "PLUTO_ME='%H' "
328 "PLUTO_MY_ID='%D' "
329 "PLUTO_MY_CLIENT='%s/%s' "
330 "PLUTO_MY_CLIENT_NET='%s' "
331 "PLUTO_MY_CLIENT_MASK='%s' "
332 "PLUTO_MY_PORT='%u' "
333 "PLUTO_MY_PROTOCOL='%u' "
334 "PLUTO_PEER='%H' "
335 "PLUTO_PEER_ID='%D' "
336 "PLUTO_PEER_CLIENT='%s/%s' "
337 "PLUTO_PEER_CLIENT_NET='%s' "
338 "PLUTO_PEER_CLIENT_MASK='%s' "
339 "PLUTO_PEER_PORT='%u' "
340 "PLUTO_PEER_PROTOCOL='%u' "
341 "%s"
342 "%s"
343 "%s",
344 up ? "up" : "down",
345 policy->my_ts->is_host(policy->my_ts,
346 this->me.addr) ? "-host" : "-client",
347 this->me.addr->get_family(this->me.addr) == AF_INET ? "" : "-ipv6",
348 this->config->get_name(this->config),
349 ifname ? ifname : "(unknown)",
350 this->reqid,
351 this->me.addr,
352 this->me.id,
353 my_client, my_client_mask,
354 my_client, my_client_mask,
355 policy->my_ts->get_from_port(policy->my_ts),
356 policy->my_ts->get_protocol(policy->my_ts),
357 this->other.addr,
358 this->other.id,
359 other_client, other_client_mask,
360 other_client, other_client_mask,
361 policy->other_ts->get_from_port(policy->other_ts),
362 policy->other_ts->get_protocol(policy->other_ts),
363 virtual_ip,
364 this->config->get_hostaccess(this->config) ?
365 "PLUTO_HOST_ACCESS='1' " : "",
366 script);
367 free(ifname);
368 free(my_client);
369 free(other_client);
370 free(virtual_ip);
371
372 shell = popen(command, "r");
373
374 if (shell == NULL)
375 {
376 DBG1(DBG_CHD, "could not execute updown script '%s'", script);
377 return;
378 }
379
380 while (TRUE)
381 {
382 char resp[128];
383
384 if (fgets(resp, sizeof(resp), shell) == NULL)
385 {
386 if (ferror(shell))
387 {
388 DBG1(DBG_CHD, "error reading output from updown script");
389 return;
390 }
391 else
392 {
393 break;
394 }
395 }
396 else
397 {
398 char *e = resp + strlen(resp);
399 if (e > resp && e[-1] == '\n')
400 { /* trim trailing '\n' */
401 e[-1] = '\0';
402 }
403 DBG1(DBG_CHD, "updown: %s", resp);
404 }
405 }
406 pclose(shell);
407 }
408 iterator->destroy(iterator);
409 }
410
411 /**
412 * Implements child_sa_t.set_state
413 */
414 static void set_state(private_child_sa_t *this, child_sa_state_t state)
415 {
416 this->state = state;
417 if (state == CHILD_INSTALLED)
418 {
419 updown(this, TRUE);
420 }
421 }
422
423 /**
424 * Allocate SPI for a single proposal
425 */
426 static status_t alloc_proposal(private_child_sa_t *this, proposal_t *proposal)
427 {
428 protocol_id_t protocol = proposal->get_protocol(proposal);
429
430 if (protocol == PROTO_AH)
431 {
432 /* get a new spi for AH, if not already done */
433 if (this->alloc_ah_spi == 0)
434 {
435 if (charon->kernel_interface->get_spi(
436 charon->kernel_interface,
437 this->other.addr, this->me.addr,
438 PROTO_AH, this->reqid,
439 &this->alloc_ah_spi) != SUCCESS)
440 {
441 return FAILED;
442 }
443 }
444 proposal->set_spi(proposal, this->alloc_ah_spi);
445 }
446 if (protocol == PROTO_ESP)
447 {
448 /* get a new spi for ESP, if not already done */
449 if (this->alloc_esp_spi == 0)
450 {
451 if (charon->kernel_interface->get_spi(
452 charon->kernel_interface,
453 this->other.addr, this->me.addr,
454 PROTO_ESP, this->reqid,
455 &this->alloc_esp_spi) != SUCCESS)
456 {
457 return FAILED;
458 }
459 }
460 proposal->set_spi(proposal, this->alloc_esp_spi);
461 }
462 return SUCCESS;
463 }
464
465
466 /**
467 * Implements child_sa_t.alloc
468 */
469 static status_t alloc(private_child_sa_t *this, linked_list_t *proposals)
470 {
471 iterator_t *iterator;
472 proposal_t *proposal;
473
474 /* iterator through proposals to update spis */
475 iterator = proposals->create_iterator(proposals, TRUE);
476 while(iterator->iterate(iterator, (void**)&proposal))
477 {
478 if (alloc_proposal(this, proposal) != SUCCESS)
479 {
480 iterator->destroy(iterator);
481 return FAILED;
482 }
483 }
484 iterator->destroy(iterator);
485 return SUCCESS;
486 }
487
488 static status_t install(private_child_sa_t *this, proposal_t *proposal,
489 mode_t mode, prf_plus_t *prf_plus, bool mine)
490 {
491 u_int32_t spi, soft, hard;;
492 algorithm_t *enc_algo, *int_algo;
493 algorithm_t enc_algo_none = {ENCR_UNDEFINED, 0};
494 algorithm_t int_algo_none = {AUTH_UNDEFINED, 0};
495 host_t *src;
496 host_t *dst;
497 status_t status;
498
499 this->protocol = proposal->get_protocol(proposal);
500
501 /* now we have to decide which spi to use. Use self allocated, if "mine",
502 * or the one in the proposal, if not "mine" (others). Additionally,
503 * source and dest host switch depending on the role */
504 if (mine)
505 {
506 /* if we have allocated SPIs for AH and ESP, we must delete the unused
507 * one. */
508 if (this->protocol == PROTO_ESP)
509 {
510 this->me.spi = this->alloc_esp_spi;
511 if (this->alloc_ah_spi)
512 {
513 charon->kernel_interface->del_sa(charon->kernel_interface, this->me.addr,
514 this->alloc_ah_spi, PROTO_AH);
515 }
516 }
517 else
518 {
519 this->me.spi = this->alloc_ah_spi;
520 if (this->alloc_esp_spi)
521 {
522 charon->kernel_interface->del_sa(charon->kernel_interface, this->me.addr,
523 this->alloc_esp_spi, PROTO_ESP);
524 }
525 }
526 spi = this->me.spi;
527 dst = this->me.addr;
528 src = this->other.addr;
529 }
530 else
531 {
532 this->other.spi = proposal->get_spi(proposal);
533 spi = this->other.spi;
534 src = this->me.addr;
535 dst = this->other.addr;
536 }
537
538 DBG2(DBG_CHD, "adding %s %N SA", mine ? "inbound" : "outbound",
539 protocol_id_names, this->protocol);
540
541 /* select encryption algo */
542 if (proposal->get_algorithm(proposal, ENCRYPTION_ALGORITHM, &enc_algo))
543 {
544 DBG2(DBG_CHD, " using %N for encryption",
545 encryption_algorithm_names, enc_algo->algorithm);
546 }
547 else
548 {
549 enc_algo = &enc_algo_none;
550 }
551
552 /* select integrity algo */
553 if (proposal->get_algorithm(proposal, INTEGRITY_ALGORITHM, &int_algo))
554 {
555 DBG2(DBG_CHD, " using %N for integrity",
556 integrity_algorithm_names, int_algo->algorithm);
557 }
558 else
559 {
560 int_algo = &int_algo_none;
561 }
562
563 soft = this->config->get_lifetime(this->config, TRUE);
564 hard = this->config->get_lifetime(this->config, FALSE);
565
566 /* send SA down to the kernel */
567 DBG2(DBG_CHD, " SPI 0x%.8x, src %H dst %H", ntohl(spi), src, dst);
568 status = charon->kernel_interface->add_sa(charon->kernel_interface,
569 src, dst, spi, this->protocol,
570 this->reqid, mine ? soft : 0,
571 hard, enc_algo, int_algo,
572 prf_plus, mode, this->encap, mine);
573
574 this->encryption = *enc_algo;
575 this->integrity = *int_algo;
576 this->install_time = time(NULL);
577 this->rekey_time = this->install_time + soft;
578
579 return status;
580 }
581
582 static status_t add(private_child_sa_t *this, proposal_t *proposal,
583 mode_t mode, prf_plus_t *prf_plus)
584 {
585 u_int32_t outbound_spi, inbound_spi;
586
587 /* backup outbound spi, as alloc overwrites it */
588 outbound_spi = proposal->get_spi(proposal);
589
590 /* get SPIs inbound SAs */
591 if (alloc_proposal(this, proposal) != SUCCESS)
592 {
593 return FAILED;
594 }
595 inbound_spi = proposal->get_spi(proposal);
596
597 /* install inbound SAs */
598 if (install(this, proposal, mode, prf_plus, TRUE) != SUCCESS)
599 {
600 return FAILED;
601 }
602
603 /* install outbound SAs, restore spi*/
604 proposal->set_spi(proposal, outbound_spi);
605 if (install(this, proposal, mode, prf_plus, FALSE) != SUCCESS)
606 {
607 return FAILED;
608 }
609 proposal->set_spi(proposal, inbound_spi);
610
611 return SUCCESS;
612 }
613
614 static status_t update(private_child_sa_t *this, proposal_t *proposal,
615 mode_t mode, prf_plus_t *prf_plus)
616 {
617 u_int32_t inbound_spi;
618
619 /* backup received spi, as install() overwrites it */
620 inbound_spi = proposal->get_spi(proposal);
621
622 /* install outbound SAs */
623 if (install(this, proposal, mode, prf_plus, FALSE) != SUCCESS)
624 {
625 return FAILED;
626 }
627
628 /* restore spi */
629 proposal->set_spi(proposal, inbound_spi);
630 /* install inbound SAs */
631 if (install(this, proposal, mode, prf_plus, TRUE) != SUCCESS)
632 {
633 return FAILED;
634 }
635
636 return SUCCESS;
637 }
638
639 static status_t add_policies(private_child_sa_t *this,
640 linked_list_t *my_ts_list,
641 linked_list_t *other_ts_list, mode_t mode)
642 {
643 iterator_t *my_iter, *other_iter;
644 traffic_selector_t *my_ts, *other_ts;
645 /* use low prio for ROUTED policies */
646 bool high_prio = (this->state != CHILD_CREATED);
647
648 /* iterate over both lists */
649 my_iter = my_ts_list->create_iterator(my_ts_list, TRUE);
650 other_iter = other_ts_list->create_iterator(other_ts_list, TRUE);
651 while (my_iter->iterate(my_iter, (void**)&my_ts))
652 {
653 other_iter->reset(other_iter);
654 while (other_iter->iterate(other_iter, (void**)&other_ts))
655 {
656 /* set up policies for every entry in my_ts_list to every entry in other_ts_list */
657 status_t status;
658 sa_policy_t *policy;
659
660 if (my_ts->get_type(my_ts) != other_ts->get_type(other_ts))
661 {
662 DBG2(DBG_CHD,
663 "CHILD_SA policy uses two different IP families - ignored");
664 continue;
665 }
666
667 /* only set up policies if protocol matches, or if one is zero (any) */
668 if (my_ts->get_protocol(my_ts) != other_ts->get_protocol(other_ts) &&
669 my_ts->get_protocol(my_ts) && other_ts->get_protocol(other_ts))
670 {
671 DBG2(DBG_CHD,
672 "CHILD_SA policy uses two different protocols - ignored");
673 continue;
674 }
675
676 /* install 3 policies: out, in and forward */
677 status = charon->kernel_interface->add_policy(charon->kernel_interface,
678 this->me.addr, this->other.addr, my_ts, other_ts, POLICY_OUT,
679 this->protocol, this->reqid, high_prio, mode, FALSE);
680
681 status |= charon->kernel_interface->add_policy(charon->kernel_interface,
682 this->other.addr, this->me.addr, other_ts, my_ts, POLICY_IN,
683 this->protocol, this->reqid, high_prio, mode, FALSE);
684
685 status |= charon->kernel_interface->add_policy(charon->kernel_interface,
686 this->other.addr, this->me.addr, other_ts, my_ts, POLICY_FWD,
687 this->protocol, this->reqid, high_prio, mode, FALSE);
688
689 if (status != SUCCESS)
690 {
691 my_iter->destroy(my_iter);
692 other_iter->destroy(other_iter);
693 return status;
694 }
695
696 /* store policy to delete/update them later */
697 policy = malloc_thing(sa_policy_t);
698 policy->my_ts = my_ts->clone(my_ts);
699 policy->other_ts = other_ts->clone(other_ts);
700 this->policies->insert_last(this->policies, policy);
701 /* add to separate list to query them via get_*_traffic_selectors() */
702 this->my_ts->insert_last(this->my_ts, policy->my_ts);
703 this->other_ts->insert_last(this->other_ts, policy->other_ts);
704 }
705 }
706 my_iter->destroy(my_iter);
707 other_iter->destroy(other_iter);
708
709 /* switch to routed state if no SAD entry set up */
710 if (this->state == CHILD_CREATED)
711 {
712 this->state = CHILD_ROUTED;
713 }
714 /* needed to update hosts */
715 this->mode = mode;
716 return SUCCESS;
717 }
718
719 /**
720 * Implementation of child_sa_t.get_traffic_selectors.
721 */
722 static linked_list_t *get_traffic_selectors(private_child_sa_t *this, bool local)
723 {
724 if (local)
725 {
726 return this->my_ts;
727 }
728 return this->other_ts;
729 }
730
731 /**
732 * Implementation of child_sa_t.get_use_time
733 */
734 static status_t get_use_time(private_child_sa_t *this, bool inbound, time_t *use_time)
735 {
736 iterator_t *iterator;
737 sa_policy_t *policy;
738 status_t status = FAILED;
739
740 *use_time = UNDEFINED_TIME;
741
742 iterator = this->policies->create_iterator(this->policies, TRUE);
743 while (iterator->iterate(iterator, (void**)&policy))
744 {
745 if (inbound)
746 {
747 time_t in = UNDEFINED_TIME, fwd = UNDEFINED_TIME;
748
749 status = charon->kernel_interface->query_policy(
750 charon->kernel_interface,
751 policy->other_ts, policy->my_ts,
752 POLICY_IN, (u_int32_t*)&in);
753 status |= charon->kernel_interface->query_policy(
754 charon->kernel_interface,
755 policy->other_ts, policy->my_ts,
756 POLICY_FWD, (u_int32_t*)&fwd);
757 *use_time = max(in, fwd);
758 }
759 else
760 {
761 status = charon->kernel_interface->query_policy(
762 charon->kernel_interface,
763 policy->my_ts, policy->other_ts,
764 POLICY_OUT, (u_int32_t*)use_time);
765 }
766 }
767 iterator->destroy(iterator);
768 return status;
769 }
770
771 /**
772 * Implementation of child_sa_t.update_hosts.
773 */
774 static status_t update_hosts(private_child_sa_t *this,
775 host_t *me, host_t *other, bool encap)
776 {
777 /* anything changed at all? */
778 if (me->equals(me, this->me.addr) && other->equals(other, this->other.addr))
779 {
780 return SUCCESS;
781 }
782
783 /* update our (initator) SAs */
784 if (charon->kernel_interface->update_sa(
785 charon->kernel_interface, this->me.spi, this->protocol,
786 this->other.addr, this->me.addr, other, me) != SUCCESS)
787 {
788 return FAILED;
789 }
790
791 /* update his (responder) SAs */
792 if (charon->kernel_interface->update_sa(
793 charon->kernel_interface, this->other.spi, this->protocol,
794 this->me.addr, this->other.addr, me, other) != SUCCESS)
795 {
796 return FAILED;
797 }
798
799 /* update policies */
800 if (!me->ip_equals(me, this->me.addr) ||
801 !other->ip_equals(other, this->other.addr))
802 {
803 iterator_t *iterator;
804 sa_policy_t *policy;
805 status_t status;
806
807 /* always use high priorities, as hosts getting updated are INSTALLED */
808 iterator = this->policies->create_iterator(this->policies, TRUE);
809 while (iterator->iterate(iterator, (void**)&policy))
810 {
811 status = charon->kernel_interface->add_policy(
812 charon->kernel_interface, me, other,
813 policy->my_ts, policy->other_ts, POLICY_OUT,
814 this->protocol, this->reqid, TRUE, this->mode, TRUE);
815
816 status |= charon->kernel_interface->add_policy(
817 charon->kernel_interface, other, me,
818 policy->other_ts, policy->my_ts, POLICY_IN,
819 this->protocol, this->reqid, TRUE, this->mode, TRUE);
820
821 status |= charon->kernel_interface->add_policy(
822 charon->kernel_interface, other, me,
823 policy->other_ts, policy->my_ts, POLICY_FWD,
824 this->protocol, this->reqid, TRUE, this->mode, TRUE);
825
826 if (status != SUCCESS)
827 {
828 iterator->destroy(iterator);
829 return FAILED;
830 }
831 }
832 iterator->destroy(iterator);
833 }
834
835 /* finally apply hosts */
836 if (!me->equals(me, this->me.addr))
837 {
838 this->me.addr->destroy(this->me.addr);
839 this->me.addr = me->clone(me);
840 }
841 if (other->equals(other, this->other.addr))
842 {
843 this->other.addr->destroy(this->other.addr);
844 this->other.addr = other->clone(other);
845 }
846 return SUCCESS;
847 }
848
849 /**
850 * Implementation of child_sa_t.set_virtual_ip.
851 */
852 static void set_virtual_ip(private_child_sa_t *this, host_t *ip)
853 {
854 this->virtual_ip = ip->clone(ip);
855 }
856
857 /**
858 * Implementation of child_sa_t.destroy.
859 */
860 static void destroy(private_child_sa_t *this)
861 {
862 sa_policy_t *policy;
863
864 if (this->state == CHILD_DELETING || this->state == CHILD_INSTALLED)
865 {
866 updown(this, FALSE);
867 }
868
869 /* delete SAs in the kernel, if they are set up */
870 if (this->me.spi)
871 {
872 charon->kernel_interface->del_sa(charon->kernel_interface,
873 this->me.addr, this->me.spi, this->protocol);
874 }
875 if (this->alloc_esp_spi && this->alloc_esp_spi != this->me.spi)
876 {
877 charon->kernel_interface->del_sa(charon->kernel_interface,
878 this->me.addr, this->alloc_esp_spi, PROTO_ESP);
879 }
880 if (this->alloc_ah_spi && this->alloc_ah_spi != this->me.spi)
881 {
882 charon->kernel_interface->del_sa(charon->kernel_interface,
883 this->me.addr, this->alloc_ah_spi, PROTO_AH);
884 }
885 if (this->other.spi)
886 {
887 charon->kernel_interface->del_sa(charon->kernel_interface,
888 this->other.addr, this->other.spi, this->protocol);
889 }
890
891 /* delete all policies in the kernel */
892 while (this->policies->remove_last(this->policies, (void**)&policy) == SUCCESS)
893 {
894 /* let rekeyed policies, as they are used by another child_sa */
895 charon->kernel_interface->del_policy(charon->kernel_interface,
896 policy->my_ts, policy->other_ts,
897 POLICY_OUT);
898
899 charon->kernel_interface->del_policy(charon->kernel_interface,
900 policy->other_ts, policy->my_ts,
901 POLICY_IN);
902
903 charon->kernel_interface->del_policy(charon->kernel_interface,
904 policy->other_ts, policy->my_ts,
905 POLICY_FWD);
906 policy->my_ts->destroy(policy->my_ts);
907 policy->other_ts->destroy(policy->other_ts);
908 free(policy);
909 }
910 this->policies->destroy(this->policies);
911
912 this->my_ts->destroy(this->my_ts);
913 this->other_ts->destroy(this->other_ts);
914 this->me.addr->destroy(this->me.addr);
915 this->other.addr->destroy(this->other.addr);
916 this->me.id->destroy(this->me.id);
917 this->other.id->destroy(this->other.id);
918 this->config->destroy(this->config);
919 DESTROY_IF(this->virtual_ip);
920 free(this);
921 }
922
923 /*
924 * Described in header.
925 */
926 child_sa_t * child_sa_create(host_t *me, host_t* other,
927 identification_t *my_id, identification_t *other_id,
928 child_cfg_t *config, u_int32_t rekey, bool encap)
929 {
930 static u_int32_t reqid = 0;
931 private_child_sa_t *this = malloc_thing(private_child_sa_t);
932
933 /* public functions */
934 this->public.get_name = (char*(*)(child_sa_t*))get_name;
935 this->public.get_reqid = (u_int32_t(*)(child_sa_t*))get_reqid;
936 this->public.get_spi = (u_int32_t(*)(child_sa_t*, bool))get_spi;
937 this->public.get_protocol = (protocol_id_t(*)(child_sa_t*))get_protocol;
938 this->public.get_stats = (void(*)(child_sa_t*, mode_t*,encryption_algorithm_t*,size_t*,integrity_algorithm_t*,size_t*,u_int32_t*,u_int32_t*,u_int32_t*,u_int32_t*))get_stats;
939 this->public.alloc = (status_t(*)(child_sa_t*,linked_list_t*))alloc;
940 this->public.add = (status_t(*)(child_sa_t*,proposal_t*,mode_t,prf_plus_t*))add;
941 this->public.update = (status_t(*)(child_sa_t*,proposal_t*,mode_t,prf_plus_t*))update;
942 this->public.update_hosts = (status_t (*)(child_sa_t*,host_t*,host_t*,bool))update_hosts;
943 this->public.add_policies = (status_t (*)(child_sa_t*, linked_list_t*,linked_list_t*,mode_t))add_policies;
944 this->public.get_traffic_selectors = (linked_list_t*(*)(child_sa_t*,bool))get_traffic_selectors;
945 this->public.get_use_time = (status_t (*)(child_sa_t*,bool,time_t*))get_use_time;
946 this->public.set_state = (void(*)(child_sa_t*,child_sa_state_t))set_state;
947 this->public.get_state = (child_sa_state_t(*)(child_sa_t*))get_state;
948 this->public.get_config = (child_cfg_t*(*)(child_sa_t*))get_config;
949 this->public.set_virtual_ip = (void(*)(child_sa_t*,host_t*))set_virtual_ip;
950 this->public.destroy = (void(*)(child_sa_t*))destroy;
951
952 /* private data */
953 this->me.addr = me->clone(me);
954 this->other.addr = other->clone(other);
955 this->me.id = my_id->clone(my_id);
956 this->other.id = other_id->clone(other_id);
957 this->me.spi = 0;
958 this->other.spi = 0;
959 this->alloc_ah_spi = 0;
960 this->alloc_esp_spi = 0;
961 this->encap = encap;
962 this->state = CHILD_CREATED;
963 /* reuse old reqid if we are rekeying an existing CHILD_SA */
964 this->reqid = rekey ? rekey : ++reqid;
965 this->encryption.algorithm = ENCR_UNDEFINED;
966 this->encryption.key_size = 0;
967 this->integrity.algorithm = AUTH_UNDEFINED;
968 this->encryption.key_size = 0;
969 this->policies = linked_list_create();
970 this->my_ts = linked_list_create();
971 this->other_ts = linked_list_create();
972 this->protocol = PROTO_NONE;
973 this->mode = MODE_TUNNEL;
974 this->virtual_ip = NULL;
975 this->config = config;
976 config->get_ref(config);
977
978 return &this->public;
979 }