fixed firewall script invocation when interface is not available anymore
[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 object.
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 * cached interface name for iptables
162 */
163 char *iface;
164 };
165
166 /**
167 * Implementation of child_sa_t.get_name.
168 */
169 static char *get_name(private_child_sa_t *this)
170 {
171 return this->config->get_name(this->config);
172 }
173
174 /**
175 * Implements child_sa_t.get_reqid
176 */
177 static u_int32_t get_reqid(private_child_sa_t *this)
178 {
179 return this->reqid;
180 }
181
182 /**
183 * Implements child_sa_t.get_spi
184 */
185 u_int32_t get_spi(private_child_sa_t *this, bool inbound)
186 {
187 if (inbound)
188 {
189 return this->me.spi;
190 }
191 return this->other.spi;
192 }
193
194 /**
195 * Implements child_sa_t.get_protocol
196 */
197 protocol_id_t get_protocol(private_child_sa_t *this)
198 {
199 return this->protocol;
200 }
201
202 /**
203 * Implements child_sa_t.get_state
204 */
205 static child_sa_state_t get_state(private_child_sa_t *this)
206 {
207 return this->state;
208 }
209
210 /**
211 * Implements child_sa_t.get_config
212 */
213 static child_cfg_t* get_config(private_child_sa_t *this)
214 {
215 return this->config;
216 }
217
218 /**
219 * Implementation of child_sa_t.get_stats.
220 */
221 static void get_stats(private_child_sa_t *this, mode_t *mode,
222 encryption_algorithm_t *encr_algo, size_t *encr_len,
223 integrity_algorithm_t *int_algo, size_t *int_len,
224 u_int32_t *rekey, u_int32_t *use_in, u_int32_t *use_out,
225 u_int32_t *use_fwd)
226 {
227 sa_policy_t *policy;
228 iterator_t *iterator;
229 u_int32_t in = 0, out = 0, fwd = 0, time;
230
231 iterator = this->policies->create_iterator(this->policies, TRUE);
232 while (iterator->iterate(iterator, (void**)&policy))
233 {
234
235 if (charon->kernel_interface->query_policy(charon->kernel_interface,
236 policy->other_ts, policy->my_ts, POLICY_IN, &time) == SUCCESS)
237 {
238 in = max(in, time);
239 }
240 if (charon->kernel_interface->query_policy(charon->kernel_interface,
241 policy->my_ts, policy->other_ts, POLICY_OUT, &time) == SUCCESS)
242 {
243 out = max(out, time);
244 }
245 if (charon->kernel_interface->query_policy(charon->kernel_interface,
246 policy->other_ts, policy->my_ts, POLICY_FWD, &time) == SUCCESS)
247 {
248 fwd = max(fwd, time);
249 }
250 }
251 iterator->destroy(iterator);
252
253 *mode = this->mode;
254 *encr_algo = this->encryption.algorithm;
255 *encr_len = this->encryption.key_size;
256 *int_algo = this->integrity.algorithm;
257 *int_len = this->integrity.key_size;
258 *rekey = this->rekey_time;
259 *use_in = in;
260 *use_out = out;
261 *use_fwd = fwd;
262 }
263
264 /**
265 * Run the up/down script
266 */
267 static void updown(private_child_sa_t *this, bool up)
268 {
269 sa_policy_t *policy;
270 iterator_t *iterator;
271 char *script;
272
273 script = this->config->get_updown(this->config);
274
275 if (script == NULL)
276 {
277 return;
278 }
279
280 iterator = this->policies->create_iterator(this->policies, TRUE);
281 while (iterator->iterate(iterator, (void**)&policy))
282 {
283 char command[1024];
284 char *my_client, *other_client, *my_client_mask, *other_client_mask;
285 char *pos, *virtual_ip;
286 FILE *shell;
287
288 /* get subnet/bits from string */
289 asprintf(&my_client, "%R", policy->my_ts);
290 pos = strchr(my_client, '/');
291 *pos = '\0';
292 my_client_mask = pos + 1;
293 pos = strchr(my_client_mask, '[');
294 if (pos)
295 {
296 *pos = '\0';
297 }
298 asprintf(&other_client, "%R", policy->other_ts);
299 pos = strchr(other_client, '/');
300 *pos = '\0';
301 other_client_mask = pos + 1;
302 pos = strchr(other_client_mask, '[');
303 if (pos)
304 {
305 *pos = '\0';
306 }
307
308 if (this->virtual_ip)
309 {
310 asprintf(&virtual_ip, "PLUTO_MY_SOURCEIP='%H' ",
311 this->virtual_ip);
312 }
313 else
314 {
315 asprintf(&virtual_ip, "");
316 }
317
318 /* we cache the iface name, as it may not be available when
319 * the SA gets deleted */
320 if (up)
321 {
322 free(this->iface);
323 this->iface = charon->kernel_interface->get_interface(
324 charon->kernel_interface, this->me.addr);
325 }
326
327 /* build the command with all env variables.
328 * TODO: PLUTO_PEER_CA and PLUTO_NEXT_HOP are currently missing
329 */
330 snprintf(command, sizeof(command),
331 "2>&1 "
332 "PLUTO_VERSION='1.1' "
333 "PLUTO_VERB='%s%s%s' "
334 "PLUTO_CONNECTION='%s' "
335 "PLUTO_INTERFACE='%s' "
336 "PLUTO_REQID='%u' "
337 "PLUTO_ME='%H' "
338 "PLUTO_MY_ID='%D' "
339 "PLUTO_MY_CLIENT='%s/%s' "
340 "PLUTO_MY_CLIENT_NET='%s' "
341 "PLUTO_MY_CLIENT_MASK='%s' "
342 "PLUTO_MY_PORT='%u' "
343 "PLUTO_MY_PROTOCOL='%u' "
344 "PLUTO_PEER='%H' "
345 "PLUTO_PEER_ID='%D' "
346 "PLUTO_PEER_CLIENT='%s/%s' "
347 "PLUTO_PEER_CLIENT_NET='%s' "
348 "PLUTO_PEER_CLIENT_MASK='%s' "
349 "PLUTO_PEER_PORT='%u' "
350 "PLUTO_PEER_PROTOCOL='%u' "
351 "%s"
352 "%s"
353 "%s",
354 up ? "up" : "down",
355 policy->my_ts->is_host(policy->my_ts,
356 this->me.addr) ? "-host" : "-client",
357 this->me.addr->get_family(this->me.addr) == AF_INET ? "" : "-ipv6",
358 this->config->get_name(this->config),
359 this->iface ? this->iface : "unknown",
360 this->reqid,
361 this->me.addr,
362 this->me.id,
363 my_client, my_client_mask,
364 my_client, my_client_mask,
365 policy->my_ts->get_from_port(policy->my_ts),
366 policy->my_ts->get_protocol(policy->my_ts),
367 this->other.addr,
368 this->other.id,
369 other_client, other_client_mask,
370 other_client, other_client_mask,
371 policy->other_ts->get_from_port(policy->other_ts),
372 policy->other_ts->get_protocol(policy->other_ts),
373 virtual_ip,
374 this->config->get_hostaccess(this->config) ?
375 "PLUTO_HOST_ACCESS='1' " : "",
376 script);
377 free(my_client);
378 free(other_client);
379 free(virtual_ip);
380
381 DBG3(DBG_CHD, "running updown script: %s", command);
382 shell = popen(command, "r");
383
384 if (shell == NULL)
385 {
386 DBG1(DBG_CHD, "could not execute updown script '%s'", script);
387 return;
388 }
389
390 while (TRUE)
391 {
392 char resp[128];
393
394 if (fgets(resp, sizeof(resp), shell) == NULL)
395 {
396 if (ferror(shell))
397 {
398 DBG1(DBG_CHD, "error reading output from updown script");
399 return;
400 }
401 else
402 {
403 break;
404 }
405 }
406 else
407 {
408 char *e = resp + strlen(resp);
409 if (e > resp && e[-1] == '\n')
410 { /* trim trailing '\n' */
411 e[-1] = '\0';
412 }
413 DBG1(DBG_CHD, "updown: %s", resp);
414 }
415 }
416 pclose(shell);
417 }
418 iterator->destroy(iterator);
419 }
420
421 /**
422 * Implements child_sa_t.set_state
423 */
424 static void set_state(private_child_sa_t *this, child_sa_state_t state)
425 {
426 this->state = state;
427 if (state == CHILD_INSTALLED)
428 {
429 updown(this, TRUE);
430 }
431 }
432
433 /**
434 * Allocate SPI for a single proposal
435 */
436 static status_t alloc_proposal(private_child_sa_t *this, proposal_t *proposal)
437 {
438 protocol_id_t protocol = proposal->get_protocol(proposal);
439
440 if (protocol == PROTO_AH)
441 {
442 /* get a new spi for AH, if not already done */
443 if (this->alloc_ah_spi == 0)
444 {
445 if (charon->kernel_interface->get_spi(
446 charon->kernel_interface,
447 this->other.addr, this->me.addr,
448 PROTO_AH, this->reqid,
449 &this->alloc_ah_spi) != SUCCESS)
450 {
451 return FAILED;
452 }
453 }
454 proposal->set_spi(proposal, this->alloc_ah_spi);
455 }
456 if (protocol == PROTO_ESP)
457 {
458 /* get a new spi for ESP, if not already done */
459 if (this->alloc_esp_spi == 0)
460 {
461 if (charon->kernel_interface->get_spi(
462 charon->kernel_interface,
463 this->other.addr, this->me.addr,
464 PROTO_ESP, this->reqid,
465 &this->alloc_esp_spi) != SUCCESS)
466 {
467 return FAILED;
468 }
469 }
470 proposal->set_spi(proposal, this->alloc_esp_spi);
471 }
472 return SUCCESS;
473 }
474
475
476 /**
477 * Implements child_sa_t.alloc
478 */
479 static status_t alloc(private_child_sa_t *this, linked_list_t *proposals)
480 {
481 iterator_t *iterator;
482 proposal_t *proposal;
483
484 /* iterator through proposals to update spis */
485 iterator = proposals->create_iterator(proposals, TRUE);
486 while(iterator->iterate(iterator, (void**)&proposal))
487 {
488 if (alloc_proposal(this, proposal) != SUCCESS)
489 {
490 iterator->destroy(iterator);
491 return FAILED;
492 }
493 }
494 iterator->destroy(iterator);
495 return SUCCESS;
496 }
497
498 static status_t install(private_child_sa_t *this, proposal_t *proposal,
499 mode_t mode, prf_plus_t *prf_plus, bool mine)
500 {
501 u_int32_t spi, soft, hard;;
502 algorithm_t *enc_algo, *int_algo;
503 algorithm_t enc_algo_none = {ENCR_UNDEFINED, 0};
504 algorithm_t int_algo_none = {AUTH_UNDEFINED, 0};
505 host_t *src;
506 host_t *dst;
507 status_t status;
508
509 this->protocol = proposal->get_protocol(proposal);
510
511 /* now we have to decide which spi to use. Use self allocated, if "mine",
512 * or the one in the proposal, if not "mine" (others). Additionally,
513 * source and dest host switch depending on the role */
514 if (mine)
515 {
516 /* if we have allocated SPIs for AH and ESP, we must delete the unused
517 * one. */
518 if (this->protocol == PROTO_ESP)
519 {
520 this->me.spi = this->alloc_esp_spi;
521 if (this->alloc_ah_spi)
522 {
523 charon->kernel_interface->del_sa(charon->kernel_interface, this->me.addr,
524 this->alloc_ah_spi, PROTO_AH);
525 }
526 }
527 else
528 {
529 this->me.spi = this->alloc_ah_spi;
530 if (this->alloc_esp_spi)
531 {
532 charon->kernel_interface->del_sa(charon->kernel_interface, this->me.addr,
533 this->alloc_esp_spi, PROTO_ESP);
534 }
535 }
536 spi = this->me.spi;
537 dst = this->me.addr;
538 src = this->other.addr;
539 }
540 else
541 {
542 this->other.spi = proposal->get_spi(proposal);
543 spi = this->other.spi;
544 src = this->me.addr;
545 dst = this->other.addr;
546 }
547
548 DBG2(DBG_CHD, "adding %s %N SA", mine ? "inbound" : "outbound",
549 protocol_id_names, this->protocol);
550
551 /* select encryption algo */
552 if (proposal->get_algorithm(proposal, ENCRYPTION_ALGORITHM, &enc_algo))
553 {
554 DBG2(DBG_CHD, " using %N for encryption",
555 encryption_algorithm_names, enc_algo->algorithm);
556 }
557 else
558 {
559 enc_algo = &enc_algo_none;
560 }
561
562 /* select integrity algo */
563 if (proposal->get_algorithm(proposal, INTEGRITY_ALGORITHM, &int_algo))
564 {
565 DBG2(DBG_CHD, " using %N for integrity",
566 integrity_algorithm_names, int_algo->algorithm);
567 }
568 else
569 {
570 int_algo = &int_algo_none;
571 }
572
573 soft = this->config->get_lifetime(this->config, TRUE);
574 hard = this->config->get_lifetime(this->config, FALSE);
575
576 /* send SA down to the kernel */
577 DBG2(DBG_CHD, " SPI 0x%.8x, src %H dst %H", ntohl(spi), src, dst);
578 status = charon->kernel_interface->add_sa(charon->kernel_interface,
579 src, dst, spi, this->protocol,
580 this->reqid, mine ? soft : 0,
581 hard, enc_algo, int_algo,
582 prf_plus, mode, this->encap, mine);
583
584 this->encryption = *enc_algo;
585 this->integrity = *int_algo;
586 this->install_time = time(NULL);
587 this->rekey_time = this->install_time + soft;
588
589 return status;
590 }
591
592 static status_t add(private_child_sa_t *this, proposal_t *proposal,
593 mode_t mode, prf_plus_t *prf_plus)
594 {
595 u_int32_t outbound_spi, inbound_spi;
596
597 /* backup outbound spi, as alloc overwrites it */
598 outbound_spi = proposal->get_spi(proposal);
599
600 /* get SPIs inbound SAs */
601 if (alloc_proposal(this, proposal) != SUCCESS)
602 {
603 return FAILED;
604 }
605 inbound_spi = proposal->get_spi(proposal);
606
607 /* install inbound SAs */
608 if (install(this, proposal, mode, prf_plus, TRUE) != SUCCESS)
609 {
610 return FAILED;
611 }
612
613 /* install outbound SAs, restore spi*/
614 proposal->set_spi(proposal, outbound_spi);
615 if (install(this, proposal, mode, prf_plus, FALSE) != SUCCESS)
616 {
617 return FAILED;
618 }
619 proposal->set_spi(proposal, inbound_spi);
620
621 return SUCCESS;
622 }
623
624 static status_t update(private_child_sa_t *this, proposal_t *proposal,
625 mode_t mode, prf_plus_t *prf_plus)
626 {
627 u_int32_t inbound_spi;
628
629 /* backup received spi, as install() overwrites it */
630 inbound_spi = proposal->get_spi(proposal);
631
632 /* install outbound SAs */
633 if (install(this, proposal, mode, prf_plus, FALSE) != SUCCESS)
634 {
635 return FAILED;
636 }
637
638 /* restore spi */
639 proposal->set_spi(proposal, inbound_spi);
640 /* install inbound SAs */
641 if (install(this, proposal, mode, prf_plus, TRUE) != SUCCESS)
642 {
643 return FAILED;
644 }
645
646 return SUCCESS;
647 }
648
649 static status_t add_policies(private_child_sa_t *this,
650 linked_list_t *my_ts_list,
651 linked_list_t *other_ts_list, mode_t mode)
652 {
653 iterator_t *my_iter, *other_iter;
654 traffic_selector_t *my_ts, *other_ts;
655 /* use low prio for ROUTED policies */
656 bool high_prio = (this->state != CHILD_CREATED);
657
658 /* iterate over both lists */
659 my_iter = my_ts_list->create_iterator(my_ts_list, TRUE);
660 other_iter = other_ts_list->create_iterator(other_ts_list, TRUE);
661 while (my_iter->iterate(my_iter, (void**)&my_ts))
662 {
663 other_iter->reset(other_iter);
664 while (other_iter->iterate(other_iter, (void**)&other_ts))
665 {
666 /* set up policies for every entry in my_ts_list to every entry in other_ts_list */
667 status_t status;
668 sa_policy_t *policy;
669
670 if (my_ts->get_type(my_ts) != other_ts->get_type(other_ts))
671 {
672 DBG2(DBG_CHD,
673 "CHILD_SA policy uses two different IP families - ignored");
674 continue;
675 }
676
677 /* only set up policies if protocol matches, or if one is zero (any) */
678 if (my_ts->get_protocol(my_ts) != other_ts->get_protocol(other_ts) &&
679 my_ts->get_protocol(my_ts) && other_ts->get_protocol(other_ts))
680 {
681 DBG2(DBG_CHD,
682 "CHILD_SA policy uses two different protocols - ignored");
683 continue;
684 }
685
686 /* install 3 policies: out, in and forward */
687 status = charon->kernel_interface->add_policy(charon->kernel_interface,
688 this->me.addr, this->other.addr, my_ts, other_ts, POLICY_OUT,
689 this->protocol, this->reqid, high_prio, mode);
690
691 status |= charon->kernel_interface->add_policy(charon->kernel_interface,
692 this->other.addr, this->me.addr, other_ts, my_ts, POLICY_IN,
693 this->protocol, this->reqid, high_prio, mode);
694
695 status |= charon->kernel_interface->add_policy(charon->kernel_interface,
696 this->other.addr, this->me.addr, other_ts, my_ts, POLICY_FWD,
697 this->protocol, this->reqid, high_prio, mode);
698
699 if (status != SUCCESS)
700 {
701 my_iter->destroy(my_iter);
702 other_iter->destroy(other_iter);
703 return status;
704 }
705
706 /* store policy to delete/update them later */
707 policy = malloc_thing(sa_policy_t);
708 policy->my_ts = my_ts->clone(my_ts);
709 policy->other_ts = other_ts->clone(other_ts);
710 this->policies->insert_last(this->policies, policy);
711 /* add to separate list to query them via get_*_traffic_selectors() */
712 this->my_ts->insert_last(this->my_ts, policy->my_ts);
713 this->other_ts->insert_last(this->other_ts, policy->other_ts);
714 }
715 }
716 my_iter->destroy(my_iter);
717 other_iter->destroy(other_iter);
718
719 /* switch to routed state if no SAD entry set up */
720 if (this->state == CHILD_CREATED)
721 {
722 this->state = CHILD_ROUTED;
723 }
724 /* needed to update hosts */
725 this->mode = mode;
726 return SUCCESS;
727 }
728
729 /**
730 * Implementation of child_sa_t.get_traffic_selectors.
731 */
732 static linked_list_t *get_traffic_selectors(private_child_sa_t *this, bool local)
733 {
734 if (local)
735 {
736 return this->my_ts;
737 }
738 return this->other_ts;
739 }
740
741 /**
742 * Implementation of child_sa_t.get_use_time
743 */
744 static status_t get_use_time(private_child_sa_t *this, bool inbound, time_t *use_time)
745 {
746 iterator_t *iterator;
747 sa_policy_t *policy;
748 status_t status = FAILED;
749
750 *use_time = UNDEFINED_TIME;
751
752 iterator = this->policies->create_iterator(this->policies, TRUE);
753 while (iterator->iterate(iterator, (void**)&policy))
754 {
755 if (inbound)
756 {
757 time_t in = UNDEFINED_TIME, fwd = UNDEFINED_TIME;
758
759 status = charon->kernel_interface->query_policy(
760 charon->kernel_interface,
761 policy->other_ts, policy->my_ts,
762 POLICY_IN, (u_int32_t*)&in);
763 status |= charon->kernel_interface->query_policy(
764 charon->kernel_interface,
765 policy->other_ts, policy->my_ts,
766 POLICY_FWD, (u_int32_t*)&fwd);
767 *use_time = max(in, fwd);
768 }
769 else
770 {
771 status = charon->kernel_interface->query_policy(
772 charon->kernel_interface,
773 policy->my_ts, policy->other_ts,
774 POLICY_OUT, (u_int32_t*)use_time);
775 }
776 }
777 iterator->destroy(iterator);
778 return status;
779 }
780
781 /**
782 * Implementation of child_sa_t.update_hosts.
783 */
784 static status_t update_hosts(private_child_sa_t *this,
785 host_t *me, host_t *other, bool encap)
786 {
787 /* anything changed at all? */
788 if (me->equals(me, this->me.addr) &&
789 other->equals(other, this->other.addr) && this->encap == encap)
790 {
791 return SUCCESS;
792 }
793 /* run updown script to remove iptables rules */
794 updown(this, FALSE);
795
796 this->encap = encap;
797
798 /* update our (initator) SAs */
799 charon->kernel_interface->update_sa(charon->kernel_interface, this->me.spi,
800 this->protocol, this->other.addr, this->me.addr, other, me, encap);
801 /* update his (responder) SAs */
802 charon->kernel_interface->update_sa(charon->kernel_interface, this->other.spi,
803 this->protocol, this->me.addr, this->other.addr, me, other, encap);
804
805 /* update policies */
806 if (!me->ip_equals(me, this->me.addr) ||
807 !other->ip_equals(other, this->other.addr))
808 {
809 iterator_t *iterator;
810 sa_policy_t *policy;
811
812 /* always use high priorities, as hosts getting updated are INSTALLED */
813 iterator = this->policies->create_iterator(this->policies, TRUE);
814 while (iterator->iterate(iterator, (void**)&policy))
815 {
816 /* remove old policies first */
817 charon->kernel_interface->del_policy(charon->kernel_interface,
818 policy->my_ts, policy->other_ts, POLICY_OUT);
819 charon->kernel_interface->del_policy(charon->kernel_interface,
820 policy->other_ts, policy->my_ts, POLICY_IN);
821 charon->kernel_interface->del_policy(charon->kernel_interface,
822 policy->other_ts, policy->my_ts, POLICY_FWD);
823
824 /* check wether we have to update a "dynamic" traffic selector */
825 if (!me->ip_equals(me, this->me.addr) &&
826 policy->my_ts->is_host(policy->my_ts, this->me.addr))
827 {
828 policy->my_ts->set_address(policy->my_ts, me);
829 }
830 if (!other->ip_equals(other, this->other.addr) &&
831 policy->other_ts->is_host(policy->other_ts, this->other.addr))
832 {
833 policy->other_ts->set_address(policy->other_ts, other);
834 }
835
836 /* reinstall updated policies */
837 charon->kernel_interface->add_policy(charon->kernel_interface,
838 me, other, policy->my_ts, policy->other_ts, POLICY_OUT,
839 this->protocol, this->reqid, TRUE, this->mode);
840 charon->kernel_interface->add_policy(charon->kernel_interface,
841 other, me, policy->other_ts, policy->my_ts, POLICY_IN,
842 this->protocol, this->reqid, TRUE, this->mode);
843 charon->kernel_interface->add_policy(charon->kernel_interface,
844 other, me, policy->other_ts, policy->my_ts, POLICY_FWD,
845 this->protocol, this->reqid, TRUE, this->mode);
846 }
847 iterator->destroy(iterator);
848 }
849
850 /* apply hosts */
851 if (!me->equals(me, this->me.addr))
852 {
853 this->me.addr->destroy(this->me.addr);
854 this->me.addr = me->clone(me);
855 }
856 if (!other->equals(other, this->other.addr))
857 {
858 this->other.addr->destroy(this->other.addr);
859 this->other.addr = other->clone(other);
860 }
861
862 /* install new iptables rules */
863 updown(this, TRUE);
864
865 return SUCCESS;
866 }
867
868 /**
869 * Implementation of child_sa_t.set_virtual_ip.
870 */
871 static void set_virtual_ip(private_child_sa_t *this, host_t *ip)
872 {
873 this->virtual_ip = ip->clone(ip);
874 }
875
876 /**
877 * Implementation of child_sa_t.destroy.
878 */
879 static void destroy(private_child_sa_t *this)
880 {
881 sa_policy_t *policy;
882
883 if (this->state == CHILD_DELETING || this->state == CHILD_INSTALLED)
884 {
885 updown(this, FALSE);
886 }
887
888 /* delete SAs in the kernel, if they are set up */
889 if (this->me.spi)
890 {
891 charon->kernel_interface->del_sa(charon->kernel_interface,
892 this->me.addr, this->me.spi, this->protocol);
893 }
894 if (this->alloc_esp_spi && this->alloc_esp_spi != this->me.spi)
895 {
896 charon->kernel_interface->del_sa(charon->kernel_interface,
897 this->me.addr, this->alloc_esp_spi, PROTO_ESP);
898 }
899 if (this->alloc_ah_spi && this->alloc_ah_spi != this->me.spi)
900 {
901 charon->kernel_interface->del_sa(charon->kernel_interface,
902 this->me.addr, this->alloc_ah_spi, PROTO_AH);
903 }
904 if (this->other.spi)
905 {
906 charon->kernel_interface->del_sa(charon->kernel_interface,
907 this->other.addr, this->other.spi, this->protocol);
908 }
909
910 /* delete all policies in the kernel */
911 while (this->policies->remove_last(this->policies, (void**)&policy) == SUCCESS)
912 {
913 /* let rekeyed policies, as they are used by another child_sa */
914 charon->kernel_interface->del_policy(charon->kernel_interface,
915 policy->my_ts, policy->other_ts,
916 POLICY_OUT);
917
918 charon->kernel_interface->del_policy(charon->kernel_interface,
919 policy->other_ts, policy->my_ts,
920 POLICY_IN);
921
922 charon->kernel_interface->del_policy(charon->kernel_interface,
923 policy->other_ts, policy->my_ts,
924 POLICY_FWD);
925 policy->my_ts->destroy(policy->my_ts);
926 policy->other_ts->destroy(policy->other_ts);
927 free(policy);
928 }
929 this->policies->destroy(this->policies);
930
931 this->my_ts->destroy(this->my_ts);
932 this->other_ts->destroy(this->other_ts);
933 this->me.addr->destroy(this->me.addr);
934 this->other.addr->destroy(this->other.addr);
935 this->me.id->destroy(this->me.id);
936 this->other.id->destroy(this->other.id);
937 this->config->destroy(this->config);
938 free(this->iface);
939 DESTROY_IF(this->virtual_ip);
940 free(this);
941 }
942
943 /*
944 * Described in header.
945 */
946 child_sa_t * child_sa_create(host_t *me, host_t* other,
947 identification_t *my_id, identification_t *other_id,
948 child_cfg_t *config, u_int32_t rekey, bool encap)
949 {
950 static u_int32_t reqid = 0;
951 private_child_sa_t *this = malloc_thing(private_child_sa_t);
952
953 /* public functions */
954 this->public.get_name = (char*(*)(child_sa_t*))get_name;
955 this->public.get_reqid = (u_int32_t(*)(child_sa_t*))get_reqid;
956 this->public.get_spi = (u_int32_t(*)(child_sa_t*, bool))get_spi;
957 this->public.get_protocol = (protocol_id_t(*)(child_sa_t*))get_protocol;
958 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;
959 this->public.alloc = (status_t(*)(child_sa_t*,linked_list_t*))alloc;
960 this->public.add = (status_t(*)(child_sa_t*,proposal_t*,mode_t,prf_plus_t*))add;
961 this->public.update = (status_t(*)(child_sa_t*,proposal_t*,mode_t,prf_plus_t*))update;
962 this->public.update_hosts = (status_t (*)(child_sa_t*,host_t*,host_t*,bool))update_hosts;
963 this->public.add_policies = (status_t (*)(child_sa_t*, linked_list_t*,linked_list_t*,mode_t))add_policies;
964 this->public.get_traffic_selectors = (linked_list_t*(*)(child_sa_t*,bool))get_traffic_selectors;
965 this->public.get_use_time = (status_t (*)(child_sa_t*,bool,time_t*))get_use_time;
966 this->public.set_state = (void(*)(child_sa_t*,child_sa_state_t))set_state;
967 this->public.get_state = (child_sa_state_t(*)(child_sa_t*))get_state;
968 this->public.get_config = (child_cfg_t*(*)(child_sa_t*))get_config;
969 this->public.set_virtual_ip = (void(*)(child_sa_t*,host_t*))set_virtual_ip;
970 this->public.destroy = (void(*)(child_sa_t*))destroy;
971
972 /* private data */
973 this->me.addr = me->clone(me);
974 this->other.addr = other->clone(other);
975 this->me.id = my_id->clone(my_id);
976 this->other.id = other_id->clone(other_id);
977 this->me.spi = 0;
978 this->other.spi = 0;
979 this->alloc_ah_spi = 0;
980 this->alloc_esp_spi = 0;
981 this->encap = encap;
982 this->state = CHILD_CREATED;
983 /* reuse old reqid if we are rekeying an existing CHILD_SA */
984 this->reqid = rekey ? rekey : ++reqid;
985 this->encryption.algorithm = ENCR_UNDEFINED;
986 this->encryption.key_size = 0;
987 this->integrity.algorithm = AUTH_UNDEFINED;
988 this->encryption.key_size = 0;
989 this->policies = linked_list_create();
990 this->my_ts = linked_list_create();
991 this->other_ts = linked_list_create();
992 this->protocol = PROTO_NONE;
993 this->mode = MODE_TUNNEL;
994 this->virtual_ip = NULL;
995 this->iface = NULL;
996 this->config = config;
997 config->get_ref(config);
998
999 return &this->public;
1000 }