fixed mobike address update from and to NAT
[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 DBG3(DBG_CHD, "running updown script: %s", command);
373 shell = popen(command, "r");
374
375 if (shell == NULL)
376 {
377 DBG1(DBG_CHD, "could not execute updown script '%s'", script);
378 return;
379 }
380
381 while (TRUE)
382 {
383 char resp[128];
384
385 if (fgets(resp, sizeof(resp), shell) == NULL)
386 {
387 if (ferror(shell))
388 {
389 DBG1(DBG_CHD, "error reading output from updown script");
390 return;
391 }
392 else
393 {
394 break;
395 }
396 }
397 else
398 {
399 char *e = resp + strlen(resp);
400 if (e > resp && e[-1] == '\n')
401 { /* trim trailing '\n' */
402 e[-1] = '\0';
403 }
404 DBG1(DBG_CHD, "updown: %s", resp);
405 }
406 }
407 pclose(shell);
408 }
409 iterator->destroy(iterator);
410 }
411
412 /**
413 * Implements child_sa_t.set_state
414 */
415 static void set_state(private_child_sa_t *this, child_sa_state_t state)
416 {
417 this->state = state;
418 if (state == CHILD_INSTALLED)
419 {
420 updown(this, TRUE);
421 }
422 }
423
424 /**
425 * Allocate SPI for a single proposal
426 */
427 static status_t alloc_proposal(private_child_sa_t *this, proposal_t *proposal)
428 {
429 protocol_id_t protocol = proposal->get_protocol(proposal);
430
431 if (protocol == PROTO_AH)
432 {
433 /* get a new spi for AH, if not already done */
434 if (this->alloc_ah_spi == 0)
435 {
436 if (charon->kernel_interface->get_spi(
437 charon->kernel_interface,
438 this->other.addr, this->me.addr,
439 PROTO_AH, this->reqid,
440 &this->alloc_ah_spi) != SUCCESS)
441 {
442 return FAILED;
443 }
444 }
445 proposal->set_spi(proposal, this->alloc_ah_spi);
446 }
447 if (protocol == PROTO_ESP)
448 {
449 /* get a new spi for ESP, if not already done */
450 if (this->alloc_esp_spi == 0)
451 {
452 if (charon->kernel_interface->get_spi(
453 charon->kernel_interface,
454 this->other.addr, this->me.addr,
455 PROTO_ESP, this->reqid,
456 &this->alloc_esp_spi) != SUCCESS)
457 {
458 return FAILED;
459 }
460 }
461 proposal->set_spi(proposal, this->alloc_esp_spi);
462 }
463 return SUCCESS;
464 }
465
466
467 /**
468 * Implements child_sa_t.alloc
469 */
470 static status_t alloc(private_child_sa_t *this, linked_list_t *proposals)
471 {
472 iterator_t *iterator;
473 proposal_t *proposal;
474
475 /* iterator through proposals to update spis */
476 iterator = proposals->create_iterator(proposals, TRUE);
477 while(iterator->iterate(iterator, (void**)&proposal))
478 {
479 if (alloc_proposal(this, proposal) != SUCCESS)
480 {
481 iterator->destroy(iterator);
482 return FAILED;
483 }
484 }
485 iterator->destroy(iterator);
486 return SUCCESS;
487 }
488
489 static status_t install(private_child_sa_t *this, proposal_t *proposal,
490 mode_t mode, prf_plus_t *prf_plus, bool mine)
491 {
492 u_int32_t spi, soft, hard;;
493 algorithm_t *enc_algo, *int_algo;
494 algorithm_t enc_algo_none = {ENCR_UNDEFINED, 0};
495 algorithm_t int_algo_none = {AUTH_UNDEFINED, 0};
496 host_t *src;
497 host_t *dst;
498 status_t status;
499
500 this->protocol = proposal->get_protocol(proposal);
501
502 /* now we have to decide which spi to use. Use self allocated, if "mine",
503 * or the one in the proposal, if not "mine" (others). Additionally,
504 * source and dest host switch depending on the role */
505 if (mine)
506 {
507 /* if we have allocated SPIs for AH and ESP, we must delete the unused
508 * one. */
509 if (this->protocol == PROTO_ESP)
510 {
511 this->me.spi = this->alloc_esp_spi;
512 if (this->alloc_ah_spi)
513 {
514 charon->kernel_interface->del_sa(charon->kernel_interface, this->me.addr,
515 this->alloc_ah_spi, PROTO_AH);
516 }
517 }
518 else
519 {
520 this->me.spi = this->alloc_ah_spi;
521 if (this->alloc_esp_spi)
522 {
523 charon->kernel_interface->del_sa(charon->kernel_interface, this->me.addr,
524 this->alloc_esp_spi, PROTO_ESP);
525 }
526 }
527 spi = this->me.spi;
528 dst = this->me.addr;
529 src = this->other.addr;
530 }
531 else
532 {
533 this->other.spi = proposal->get_spi(proposal);
534 spi = this->other.spi;
535 src = this->me.addr;
536 dst = this->other.addr;
537 }
538
539 DBG2(DBG_CHD, "adding %s %N SA", mine ? "inbound" : "outbound",
540 protocol_id_names, this->protocol);
541
542 /* select encryption algo */
543 if (proposal->get_algorithm(proposal, ENCRYPTION_ALGORITHM, &enc_algo))
544 {
545 DBG2(DBG_CHD, " using %N for encryption",
546 encryption_algorithm_names, enc_algo->algorithm);
547 }
548 else
549 {
550 enc_algo = &enc_algo_none;
551 }
552
553 /* select integrity algo */
554 if (proposal->get_algorithm(proposal, INTEGRITY_ALGORITHM, &int_algo))
555 {
556 DBG2(DBG_CHD, " using %N for integrity",
557 integrity_algorithm_names, int_algo->algorithm);
558 }
559 else
560 {
561 int_algo = &int_algo_none;
562 }
563
564 soft = this->config->get_lifetime(this->config, TRUE);
565 hard = this->config->get_lifetime(this->config, FALSE);
566
567 /* send SA down to the kernel */
568 DBG2(DBG_CHD, " SPI 0x%.8x, src %H dst %H", ntohl(spi), src, dst);
569 status = charon->kernel_interface->add_sa(charon->kernel_interface,
570 src, dst, spi, this->protocol,
571 this->reqid, mine ? soft : 0,
572 hard, enc_algo, int_algo,
573 prf_plus, mode, this->encap, mine);
574
575 this->encryption = *enc_algo;
576 this->integrity = *int_algo;
577 this->install_time = time(NULL);
578 this->rekey_time = this->install_time + soft;
579
580 return status;
581 }
582
583 static status_t add(private_child_sa_t *this, proposal_t *proposal,
584 mode_t mode, prf_plus_t *prf_plus)
585 {
586 u_int32_t outbound_spi, inbound_spi;
587
588 /* backup outbound spi, as alloc overwrites it */
589 outbound_spi = proposal->get_spi(proposal);
590
591 /* get SPIs inbound SAs */
592 if (alloc_proposal(this, proposal) != SUCCESS)
593 {
594 return FAILED;
595 }
596 inbound_spi = proposal->get_spi(proposal);
597
598 /* install inbound SAs */
599 if (install(this, proposal, mode, prf_plus, TRUE) != SUCCESS)
600 {
601 return FAILED;
602 }
603
604 /* install outbound SAs, restore spi*/
605 proposal->set_spi(proposal, outbound_spi);
606 if (install(this, proposal, mode, prf_plus, FALSE) != SUCCESS)
607 {
608 return FAILED;
609 }
610 proposal->set_spi(proposal, inbound_spi);
611
612 return SUCCESS;
613 }
614
615 static status_t update(private_child_sa_t *this, proposal_t *proposal,
616 mode_t mode, prf_plus_t *prf_plus)
617 {
618 u_int32_t inbound_spi;
619
620 /* backup received spi, as install() overwrites it */
621 inbound_spi = proposal->get_spi(proposal);
622
623 /* install outbound SAs */
624 if (install(this, proposal, mode, prf_plus, FALSE) != SUCCESS)
625 {
626 return FAILED;
627 }
628
629 /* restore spi */
630 proposal->set_spi(proposal, inbound_spi);
631 /* install inbound SAs */
632 if (install(this, proposal, mode, prf_plus, TRUE) != SUCCESS)
633 {
634 return FAILED;
635 }
636
637 return SUCCESS;
638 }
639
640 static status_t add_policies(private_child_sa_t *this,
641 linked_list_t *my_ts_list,
642 linked_list_t *other_ts_list, mode_t mode)
643 {
644 iterator_t *my_iter, *other_iter;
645 traffic_selector_t *my_ts, *other_ts;
646 /* use low prio for ROUTED policies */
647 bool high_prio = (this->state != CHILD_CREATED);
648
649 /* iterate over both lists */
650 my_iter = my_ts_list->create_iterator(my_ts_list, TRUE);
651 other_iter = other_ts_list->create_iterator(other_ts_list, TRUE);
652 while (my_iter->iterate(my_iter, (void**)&my_ts))
653 {
654 other_iter->reset(other_iter);
655 while (other_iter->iterate(other_iter, (void**)&other_ts))
656 {
657 /* set up policies for every entry in my_ts_list to every entry in other_ts_list */
658 status_t status;
659 sa_policy_t *policy;
660
661 if (my_ts->get_type(my_ts) != other_ts->get_type(other_ts))
662 {
663 DBG2(DBG_CHD,
664 "CHILD_SA policy uses two different IP families - ignored");
665 continue;
666 }
667
668 /* only set up policies if protocol matches, or if one is zero (any) */
669 if (my_ts->get_protocol(my_ts) != other_ts->get_protocol(other_ts) &&
670 my_ts->get_protocol(my_ts) && other_ts->get_protocol(other_ts))
671 {
672 DBG2(DBG_CHD,
673 "CHILD_SA policy uses two different protocols - ignored");
674 continue;
675 }
676
677 /* install 3 policies: out, in and forward */
678 status = charon->kernel_interface->add_policy(charon->kernel_interface,
679 this->me.addr, this->other.addr, my_ts, other_ts, POLICY_OUT,
680 this->protocol, this->reqid, high_prio, mode);
681
682 status |= charon->kernel_interface->add_policy(charon->kernel_interface,
683 this->other.addr, this->me.addr, other_ts, my_ts, POLICY_IN,
684 this->protocol, this->reqid, high_prio, mode);
685
686 status |= charon->kernel_interface->add_policy(charon->kernel_interface,
687 this->other.addr, this->me.addr, other_ts, my_ts, POLICY_FWD,
688 this->protocol, this->reqid, high_prio, mode);
689
690 if (status != SUCCESS)
691 {
692 my_iter->destroy(my_iter);
693 other_iter->destroy(other_iter);
694 return status;
695 }
696
697 /* store policy to delete/update them later */
698 policy = malloc_thing(sa_policy_t);
699 policy->my_ts = my_ts->clone(my_ts);
700 policy->other_ts = other_ts->clone(other_ts);
701 this->policies->insert_last(this->policies, policy);
702 /* add to separate list to query them via get_*_traffic_selectors() */
703 this->my_ts->insert_last(this->my_ts, policy->my_ts);
704 this->other_ts->insert_last(this->other_ts, policy->other_ts);
705 }
706 }
707 my_iter->destroy(my_iter);
708 other_iter->destroy(other_iter);
709
710 /* switch to routed state if no SAD entry set up */
711 if (this->state == CHILD_CREATED)
712 {
713 this->state = CHILD_ROUTED;
714 }
715 /* needed to update hosts */
716 this->mode = mode;
717 return SUCCESS;
718 }
719
720 /**
721 * Implementation of child_sa_t.get_traffic_selectors.
722 */
723 static linked_list_t *get_traffic_selectors(private_child_sa_t *this, bool local)
724 {
725 if (local)
726 {
727 return this->my_ts;
728 }
729 return this->other_ts;
730 }
731
732 /**
733 * Implementation of child_sa_t.get_use_time
734 */
735 static status_t get_use_time(private_child_sa_t *this, bool inbound, time_t *use_time)
736 {
737 iterator_t *iterator;
738 sa_policy_t *policy;
739 status_t status = FAILED;
740
741 *use_time = UNDEFINED_TIME;
742
743 iterator = this->policies->create_iterator(this->policies, TRUE);
744 while (iterator->iterate(iterator, (void**)&policy))
745 {
746 if (inbound)
747 {
748 time_t in = UNDEFINED_TIME, fwd = UNDEFINED_TIME;
749
750 status = charon->kernel_interface->query_policy(
751 charon->kernel_interface,
752 policy->other_ts, policy->my_ts,
753 POLICY_IN, (u_int32_t*)&in);
754 status |= charon->kernel_interface->query_policy(
755 charon->kernel_interface,
756 policy->other_ts, policy->my_ts,
757 POLICY_FWD, (u_int32_t*)&fwd);
758 *use_time = max(in, fwd);
759 }
760 else
761 {
762 status = charon->kernel_interface->query_policy(
763 charon->kernel_interface,
764 policy->my_ts, policy->other_ts,
765 POLICY_OUT, (u_int32_t*)use_time);
766 }
767 }
768 iterator->destroy(iterator);
769 return status;
770 }
771
772 /**
773 * Implementation of child_sa_t.update_hosts.
774 */
775 static status_t update_hosts(private_child_sa_t *this,
776 host_t *me, host_t *other, bool encap)
777 {
778 /* anything changed at all? */
779 if (me->equals(me, this->me.addr) && other->equals(other, this->other.addr))
780 {
781 return SUCCESS;
782 }
783
784 /* run updown script to remove iptables rules */
785 updown(this, FALSE);
786
787 /* update our (initator) SAs */
788 charon->kernel_interface->update_sa(charon->kernel_interface, this->me.spi,
789 this->protocol, this->other.addr, this->me.addr, other, me);
790 /* update his (responder) SAs */
791 charon->kernel_interface->update_sa(charon->kernel_interface, this->other.spi,
792 this->protocol, this->me.addr, this->other.addr, me, other);
793
794 /* update policies */
795 if (!me->ip_equals(me, this->me.addr) ||
796 !other->ip_equals(other, this->other.addr))
797 {
798 iterator_t *iterator;
799 sa_policy_t *policy;
800
801 /* always use high priorities, as hosts getting updated are INSTALLED */
802 iterator = this->policies->create_iterator(this->policies, TRUE);
803 while (iterator->iterate(iterator, (void**)&policy))
804 {
805 /* remove old policies first */
806 charon->kernel_interface->del_policy(charon->kernel_interface,
807 policy->my_ts, policy->other_ts, POLICY_OUT);
808 charon->kernel_interface->del_policy(charon->kernel_interface,
809 policy->other_ts, policy->my_ts, POLICY_IN);
810 charon->kernel_interface->del_policy(charon->kernel_interface,
811 policy->other_ts, policy->my_ts, POLICY_FWD);
812
813 /* check wether we have to update a "dynamic" traffic selector */
814 if (!me->ip_equals(me, this->me.addr) &&
815 policy->my_ts->is_host(policy->my_ts, this->me.addr))
816 {
817 policy->my_ts->set_address(policy->my_ts, me);
818 }
819 if (!other->ip_equals(other, this->other.addr) &&
820 policy->other_ts->is_host(policy->other_ts, this->other.addr))
821 {
822 policy->other_ts->set_address(policy->other_ts, other);
823 }
824
825 /* reinstall updated policies */
826 charon->kernel_interface->add_policy(charon->kernel_interface,
827 me, other, policy->my_ts, policy->other_ts, POLICY_OUT,
828 this->protocol, this->reqid, TRUE, this->mode);
829 charon->kernel_interface->add_policy(charon->kernel_interface,
830 other, me, policy->other_ts, policy->my_ts, POLICY_IN,
831 this->protocol, this->reqid, TRUE, this->mode);
832 charon->kernel_interface->add_policy(charon->kernel_interface,
833 other, me, policy->other_ts, policy->my_ts, POLICY_FWD,
834 this->protocol, this->reqid, TRUE, this->mode);
835 }
836 iterator->destroy(iterator);
837 }
838
839 /* apply hosts */
840 if (!me->equals(me, this->me.addr))
841 {
842 this->me.addr->destroy(this->me.addr);
843 this->me.addr = me->clone(me);
844 }
845 if (!other->equals(other, this->other.addr))
846 {
847 this->other.addr->destroy(this->other.addr);
848 this->other.addr = other->clone(other);
849 }
850
851 /* install new iptables rules */
852 updown(this, TRUE);
853
854 return SUCCESS;
855 }
856
857 /**
858 * Implementation of child_sa_t.set_virtual_ip.
859 */
860 static void set_virtual_ip(private_child_sa_t *this, host_t *ip)
861 {
862 this->virtual_ip = ip->clone(ip);
863 }
864
865 /**
866 * Implementation of child_sa_t.destroy.
867 */
868 static void destroy(private_child_sa_t *this)
869 {
870 sa_policy_t *policy;
871
872 if (this->state == CHILD_DELETING || this->state == CHILD_INSTALLED)
873 {
874 updown(this, FALSE);
875 }
876
877 /* delete SAs in the kernel, if they are set up */
878 if (this->me.spi)
879 {
880 charon->kernel_interface->del_sa(charon->kernel_interface,
881 this->me.addr, this->me.spi, this->protocol);
882 }
883 if (this->alloc_esp_spi && this->alloc_esp_spi != this->me.spi)
884 {
885 charon->kernel_interface->del_sa(charon->kernel_interface,
886 this->me.addr, this->alloc_esp_spi, PROTO_ESP);
887 }
888 if (this->alloc_ah_spi && this->alloc_ah_spi != this->me.spi)
889 {
890 charon->kernel_interface->del_sa(charon->kernel_interface,
891 this->me.addr, this->alloc_ah_spi, PROTO_AH);
892 }
893 if (this->other.spi)
894 {
895 charon->kernel_interface->del_sa(charon->kernel_interface,
896 this->other.addr, this->other.spi, this->protocol);
897 }
898
899 /* delete all policies in the kernel */
900 while (this->policies->remove_last(this->policies, (void**)&policy) == SUCCESS)
901 {
902 /* let rekeyed policies, as they are used by another child_sa */
903 charon->kernel_interface->del_policy(charon->kernel_interface,
904 policy->my_ts, policy->other_ts,
905 POLICY_OUT);
906
907 charon->kernel_interface->del_policy(charon->kernel_interface,
908 policy->other_ts, policy->my_ts,
909 POLICY_IN);
910
911 charon->kernel_interface->del_policy(charon->kernel_interface,
912 policy->other_ts, policy->my_ts,
913 POLICY_FWD);
914 policy->my_ts->destroy(policy->my_ts);
915 policy->other_ts->destroy(policy->other_ts);
916 free(policy);
917 }
918 this->policies->destroy(this->policies);
919
920 this->my_ts->destroy(this->my_ts);
921 this->other_ts->destroy(this->other_ts);
922 this->me.addr->destroy(this->me.addr);
923 this->other.addr->destroy(this->other.addr);
924 this->me.id->destroy(this->me.id);
925 this->other.id->destroy(this->other.id);
926 this->config->destroy(this->config);
927 DESTROY_IF(this->virtual_ip);
928 free(this);
929 }
930
931 /*
932 * Described in header.
933 */
934 child_sa_t * child_sa_create(host_t *me, host_t* other,
935 identification_t *my_id, identification_t *other_id,
936 child_cfg_t *config, u_int32_t rekey, bool encap)
937 {
938 static u_int32_t reqid = 0;
939 private_child_sa_t *this = malloc_thing(private_child_sa_t);
940
941 /* public functions */
942 this->public.get_name = (char*(*)(child_sa_t*))get_name;
943 this->public.get_reqid = (u_int32_t(*)(child_sa_t*))get_reqid;
944 this->public.get_spi = (u_int32_t(*)(child_sa_t*, bool))get_spi;
945 this->public.get_protocol = (protocol_id_t(*)(child_sa_t*))get_protocol;
946 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;
947 this->public.alloc = (status_t(*)(child_sa_t*,linked_list_t*))alloc;
948 this->public.add = (status_t(*)(child_sa_t*,proposal_t*,mode_t,prf_plus_t*))add;
949 this->public.update = (status_t(*)(child_sa_t*,proposal_t*,mode_t,prf_plus_t*))update;
950 this->public.update_hosts = (status_t (*)(child_sa_t*,host_t*,host_t*,bool))update_hosts;
951 this->public.add_policies = (status_t (*)(child_sa_t*, linked_list_t*,linked_list_t*,mode_t))add_policies;
952 this->public.get_traffic_selectors = (linked_list_t*(*)(child_sa_t*,bool))get_traffic_selectors;
953 this->public.get_use_time = (status_t (*)(child_sa_t*,bool,time_t*))get_use_time;
954 this->public.set_state = (void(*)(child_sa_t*,child_sa_state_t))set_state;
955 this->public.get_state = (child_sa_state_t(*)(child_sa_t*))get_state;
956 this->public.get_config = (child_cfg_t*(*)(child_sa_t*))get_config;
957 this->public.set_virtual_ip = (void(*)(child_sa_t*,host_t*))set_virtual_ip;
958 this->public.destroy = (void(*)(child_sa_t*))destroy;
959
960 /* private data */
961 this->me.addr = me->clone(me);
962 this->other.addr = other->clone(other);
963 this->me.id = my_id->clone(my_id);
964 this->other.id = other_id->clone(other_id);
965 this->me.spi = 0;
966 this->other.spi = 0;
967 this->alloc_ah_spi = 0;
968 this->alloc_esp_spi = 0;
969 this->encap = encap;
970 this->state = CHILD_CREATED;
971 /* reuse old reqid if we are rekeying an existing CHILD_SA */
972 this->reqid = rekey ? rekey : ++reqid;
973 this->encryption.algorithm = ENCR_UNDEFINED;
974 this->encryption.key_size = 0;
975 this->integrity.algorithm = AUTH_UNDEFINED;
976 this->encryption.key_size = 0;
977 this->policies = linked_list_create();
978 this->my_ts = linked_list_create();
979 this->other_ts = linked_list_create();
980 this->protocol = PROTO_NONE;
981 this->mode = MODE_TUNNEL;
982 this->virtual_ip = NULL;
983 this->config = config;
984 config->get_ref(config);
985
986 return &this->public;
987 }