added equals() method to peer_cfg, ike_cfg, proposals, auth_info
[strongswan.git] / src / charon / sa / child_sa.c
1 /*
2 * Copyright (C) 2005-2007 Martin Willi
3 * Copyright (C) 2006 Tobias Brunner, Daniel Roethlisberger
4 * Copyright (C) 2005 Jan Hutter
5 * Hochschule fuer Technik Rapperswil
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 * for more details.
16 *
17 * $Id$
18 */
19
20 #define _GNU_SOURCE
21 #include "child_sa.h"
22
23 #include <stdio.h>
24 #include <string.h>
25
26 #include <daemon.h>
27
28 ENUM(child_sa_state_names, CHILD_CREATED, CHILD_DELETING,
29 "CREATED",
30 "ROUTED",
31 "INSTALLED",
32 "REKEYING",
33 "DELETING",
34 );
35
36 typedef struct sa_policy_t sa_policy_t;
37
38 /**
39 * Struct used to store information for a policy. This
40 * is needed since we must provide all this information
41 * for deleting a policy...
42 */
43 struct sa_policy_t {
44 /**
45 * Traffic selector for us
46 */
47 traffic_selector_t *my_ts;
48
49 /**
50 * Traffic selector for other
51 */
52 traffic_selector_t *other_ts;
53 };
54
55 typedef struct private_child_sa_t private_child_sa_t;
56
57 /**
58 * Private data of a child_sa_t object.
59 */
60 struct private_child_sa_t {
61 /**
62 * Public interface of child_sa_t.
63 */
64 child_sa_t public;
65
66 struct {
67 /** address of peer */
68 host_t *addr;
69 /** id of peer */
70 identification_t *id;
71 /** actual used SPI, 0 if unused */
72 u_int32_t spi;
73 } me, other;
74
75 /**
76 * Allocated SPI for a ESP proposal candidates
77 */
78 u_int32_t alloc_esp_spi;
79
80 /**
81 * Allocated SPI for a AH proposal candidates
82 */
83 u_int32_t alloc_ah_spi;
84
85 /**
86 * Protocol used to protect this SA, ESP|AH
87 */
88 protocol_id_t protocol;
89
90 /**
91 * List containing sa_policy_t objects
92 */
93 linked_list_t *policies;
94
95 /**
96 * Seperate list for local traffic selectors
97 */
98 linked_list_t *my_ts;
99
100 /**
101 * Seperate list for remote traffic selectors
102 */
103 linked_list_t *other_ts;
104
105 /**
106 * reqid used for this child_sa
107 */
108 u_int32_t reqid;
109
110 /**
111 * encryption algorithm used for this SA
112 */
113 u_int16_t enc_alg;
114
115 /**
116 * key size of enc_alg
117 */
118 u_int16_t enc_size;
119
120 /**
121 * integrity protection algorithm used for this SA
122 */
123 u_int16_t int_alg;
124
125 /**
126 * key size of int_alg
127 */
128 u_int16_t int_size;
129
130 /**
131 * time, on which SA was installed
132 */
133 time_t install_time;
134
135 /**
136 * absolute time when rekeying is sceduled
137 */
138 time_t rekey_time;
139
140 /**
141 * state of the CHILD_SA
142 */
143 child_sa_state_t state;
144
145 /**
146 * Specifies if UDP encapsulation is enabled (NAT traversal)
147 */
148 bool encap;
149
150 /**
151 * mode this SA uses, tunnel/transport
152 */
153 mode_t mode;
154
155 /**
156 * virtual IP assinged to local host
157 */
158 host_t *virtual_ip;
159
160 /**
161 * config used to create this child
162 */
163 child_cfg_t *config;
164
165 /**
166 * cached interface name for iptables
167 */
168 char *iface;
169 };
170
171 /**
172 * Implementation of child_sa_t.get_name.
173 */
174 static char *get_name(private_child_sa_t *this)
175 {
176 return this->config->get_name(this->config);
177 }
178
179 /**
180 * Implements child_sa_t.get_reqid
181 */
182 static u_int32_t get_reqid(private_child_sa_t *this)
183 {
184 return this->reqid;
185 }
186
187 /**
188 * Implements child_sa_t.get_spi
189 */
190 u_int32_t get_spi(private_child_sa_t *this, bool inbound)
191 {
192 if (inbound)
193 {
194 return this->me.spi;
195 }
196 return this->other.spi;
197 }
198
199 /**
200 * Implements child_sa_t.get_protocol
201 */
202 protocol_id_t get_protocol(private_child_sa_t *this)
203 {
204 return this->protocol;
205 }
206
207 /**
208 * Implements child_sa_t.get_state
209 */
210 static child_sa_state_t get_state(private_child_sa_t *this)
211 {
212 return this->state;
213 }
214
215 /**
216 * Implements child_sa_t.get_config
217 */
218 static child_cfg_t* get_config(private_child_sa_t *this)
219 {
220 return this->config;
221 }
222
223 /**
224 * Implementation of child_sa_t.get_stats.
225 */
226 static void get_stats(private_child_sa_t *this, mode_t *mode,
227 encryption_algorithm_t *encr_algo, size_t *encr_len,
228 integrity_algorithm_t *int_algo, size_t *int_len,
229 u_int32_t *rekey, u_int32_t *use_in, u_int32_t *use_out,
230 u_int32_t *use_fwd)
231 {
232 sa_policy_t *policy;
233 iterator_t *iterator;
234 u_int32_t in = 0, out = 0, fwd = 0, time;
235
236 iterator = this->policies->create_iterator(this->policies, TRUE);
237 while (iterator->iterate(iterator, (void**)&policy))
238 {
239
240 if (charon->kernel_interface->query_policy(charon->kernel_interface,
241 policy->other_ts, policy->my_ts, POLICY_IN, &time) == SUCCESS)
242 {
243 in = max(in, time);
244 }
245 if (charon->kernel_interface->query_policy(charon->kernel_interface,
246 policy->my_ts, policy->other_ts, POLICY_OUT, &time) == SUCCESS)
247 {
248 out = max(out, time);
249 }
250 if (charon->kernel_interface->query_policy(charon->kernel_interface,
251 policy->other_ts, policy->my_ts, POLICY_FWD, &time) == SUCCESS)
252 {
253 fwd = max(fwd, time);
254 }
255 }
256 iterator->destroy(iterator);
257
258 *mode = this->mode;
259 *encr_algo = this->enc_alg;
260 *encr_len = this->enc_size;
261 *int_algo = this->int_alg;
262 *int_len = this->int_size;
263 *rekey = this->rekey_time;
264 *use_in = in;
265 *use_out = out;
266 *use_fwd = fwd;
267 }
268
269 /**
270 * Run the up/down script
271 */
272 static void updown(private_child_sa_t *this, bool up)
273 {
274 sa_policy_t *policy;
275 iterator_t *iterator;
276 char *script;
277
278 script = this->config->get_updown(this->config);
279
280 if (script == NULL)
281 {
282 return;
283 }
284
285 iterator = this->policies->create_iterator(this->policies, TRUE);
286 while (iterator->iterate(iterator, (void**)&policy))
287 {
288 char command[1024];
289 char *my_client, *other_client, *my_client_mask, *other_client_mask;
290 char *pos, *virtual_ip;
291 FILE *shell;
292
293 /* get subnet/bits from string */
294 asprintf(&my_client, "%R", policy->my_ts);
295 pos = strchr(my_client, '/');
296 *pos = '\0';
297 my_client_mask = pos + 1;
298 pos = strchr(my_client_mask, '[');
299 if (pos)
300 {
301 *pos = '\0';
302 }
303 asprintf(&other_client, "%R", policy->other_ts);
304 pos = strchr(other_client, '/');
305 *pos = '\0';
306 other_client_mask = pos + 1;
307 pos = strchr(other_client_mask, '[');
308 if (pos)
309 {
310 *pos = '\0';
311 }
312
313 if (this->virtual_ip)
314 {
315 asprintf(&virtual_ip, "PLUTO_MY_SOURCEIP='%H' ",
316 this->virtual_ip);
317 }
318 else
319 {
320 asprintf(&virtual_ip, "");
321 }
322
323 /* we cache the iface name, as it may not be available when
324 * the SA gets deleted */
325 if (up)
326 {
327 free(this->iface);
328 this->iface = charon->kernel_interface->get_interface(
329 charon->kernel_interface, this->me.addr);
330 }
331
332 /* build the command with all env variables.
333 * TODO: PLUTO_PEER_CA and PLUTO_NEXT_HOP are currently missing
334 */
335 snprintf(command, sizeof(command),
336 "2>&1 "
337 "PLUTO_VERSION='1.1' "
338 "PLUTO_VERB='%s%s%s' "
339 "PLUTO_CONNECTION='%s' "
340 "PLUTO_INTERFACE='%s' "
341 "PLUTO_REQID='%u' "
342 "PLUTO_ME='%H' "
343 "PLUTO_MY_ID='%D' "
344 "PLUTO_MY_CLIENT='%s/%s' "
345 "PLUTO_MY_CLIENT_NET='%s' "
346 "PLUTO_MY_CLIENT_MASK='%s' "
347 "PLUTO_MY_PORT='%u' "
348 "PLUTO_MY_PROTOCOL='%u' "
349 "PLUTO_PEER='%H' "
350 "PLUTO_PEER_ID='%D' "
351 "PLUTO_PEER_CLIENT='%s/%s' "
352 "PLUTO_PEER_CLIENT_NET='%s' "
353 "PLUTO_PEER_CLIENT_MASK='%s' "
354 "PLUTO_PEER_PORT='%u' "
355 "PLUTO_PEER_PROTOCOL='%u' "
356 "%s"
357 "%s"
358 "%s",
359 up ? "up" : "down",
360 policy->my_ts->is_host(policy->my_ts,
361 this->me.addr) ? "-host" : "-client",
362 this->me.addr->get_family(this->me.addr) == AF_INET ? "" : "-v6",
363 this->config->get_name(this->config),
364 this->iface ? this->iface : "unknown",
365 this->reqid,
366 this->me.addr,
367 this->me.id,
368 my_client, my_client_mask,
369 my_client, my_client_mask,
370 policy->my_ts->get_from_port(policy->my_ts),
371 policy->my_ts->get_protocol(policy->my_ts),
372 this->other.addr,
373 this->other.id,
374 other_client, other_client_mask,
375 other_client, other_client_mask,
376 policy->other_ts->get_from_port(policy->other_ts),
377 policy->other_ts->get_protocol(policy->other_ts),
378 virtual_ip,
379 this->config->get_hostaccess(this->config) ?
380 "PLUTO_HOST_ACCESS='1' " : "",
381 script);
382 free(my_client);
383 free(other_client);
384 free(virtual_ip);
385
386 DBG3(DBG_CHD, "running updown script: %s", command);
387 shell = popen(command, "r");
388
389 if (shell == NULL)
390 {
391 DBG1(DBG_CHD, "could not execute updown script '%s'", script);
392 return;
393 }
394
395 while (TRUE)
396 {
397 char resp[128];
398
399 if (fgets(resp, sizeof(resp), shell) == NULL)
400 {
401 if (ferror(shell))
402 {
403 DBG1(DBG_CHD, "error reading output from updown script");
404 return;
405 }
406 else
407 {
408 break;
409 }
410 }
411 else
412 {
413 char *e = resp + strlen(resp);
414 if (e > resp && e[-1] == '\n')
415 { /* trim trailing '\n' */
416 e[-1] = '\0';
417 }
418 DBG1(DBG_CHD, "updown: %s", resp);
419 }
420 }
421 pclose(shell);
422 }
423 iterator->destroy(iterator);
424 }
425
426 /**
427 * Implements child_sa_t.set_state
428 */
429 static void set_state(private_child_sa_t *this, child_sa_state_t state)
430 {
431 this->state = state;
432 if (state == CHILD_INSTALLED)
433 {
434 updown(this, TRUE);
435 }
436 }
437
438 /**
439 * Allocate SPI for a single proposal
440 */
441 static status_t alloc_proposal(private_child_sa_t *this, proposal_t *proposal)
442 {
443 protocol_id_t protocol = proposal->get_protocol(proposal);
444
445 if (protocol == PROTO_AH)
446 {
447 /* get a new spi for AH, if not already done */
448 if (this->alloc_ah_spi == 0)
449 {
450 if (charon->kernel_interface->get_spi(
451 charon->kernel_interface,
452 this->other.addr, this->me.addr,
453 PROTO_AH, this->reqid,
454 &this->alloc_ah_spi) != SUCCESS)
455 {
456 return FAILED;
457 }
458 }
459 proposal->set_spi(proposal, this->alloc_ah_spi);
460 }
461 if (protocol == PROTO_ESP)
462 {
463 /* get a new spi for ESP, if not already done */
464 if (this->alloc_esp_spi == 0)
465 {
466 if (charon->kernel_interface->get_spi(
467 charon->kernel_interface,
468 this->other.addr, this->me.addr,
469 PROTO_ESP, this->reqid,
470 &this->alloc_esp_spi) != SUCCESS)
471 {
472 return FAILED;
473 }
474 }
475 proposal->set_spi(proposal, this->alloc_esp_spi);
476 }
477 return SUCCESS;
478 }
479
480
481 /**
482 * Implements child_sa_t.alloc
483 */
484 static status_t alloc(private_child_sa_t *this, linked_list_t *proposals)
485 {
486 iterator_t *iterator;
487 proposal_t *proposal;
488
489 /* iterator through proposals to update spis */
490 iterator = proposals->create_iterator(proposals, TRUE);
491 while(iterator->iterate(iterator, (void**)&proposal))
492 {
493 if (alloc_proposal(this, proposal) != SUCCESS)
494 {
495 iterator->destroy(iterator);
496 return FAILED;
497 }
498 }
499 iterator->destroy(iterator);
500 return SUCCESS;
501 }
502
503 static status_t install(private_child_sa_t *this, proposal_t *proposal,
504 mode_t mode, prf_plus_t *prf_plus, bool mine)
505 {
506 u_int32_t spi, soft, hard;
507 host_t *src;
508 host_t *dst;
509 status_t status;
510
511 this->protocol = proposal->get_protocol(proposal);
512
513 /* now we have to decide which spi to use. Use self allocated, if "mine",
514 * or the one in the proposal, if not "mine" (others). Additionally,
515 * source and dest host switch depending on the role */
516 if (mine)
517 {
518 /* if we have allocated SPIs for AH and ESP, we must delete the unused
519 * one. */
520 if (this->protocol == PROTO_ESP)
521 {
522 this->me.spi = this->alloc_esp_spi;
523 if (this->alloc_ah_spi)
524 {
525 charon->kernel_interface->del_sa(charon->kernel_interface, this->me.addr,
526 this->alloc_ah_spi, PROTO_AH);
527 }
528 }
529 else
530 {
531 this->me.spi = this->alloc_ah_spi;
532 if (this->alloc_esp_spi)
533 {
534 charon->kernel_interface->del_sa(charon->kernel_interface, this->me.addr,
535 this->alloc_esp_spi, PROTO_ESP);
536 }
537 }
538 spi = this->me.spi;
539 dst = this->me.addr;
540 src = this->other.addr;
541 }
542 else
543 {
544 this->other.spi = proposal->get_spi(proposal);
545 spi = this->other.spi;
546 src = this->me.addr;
547 dst = this->other.addr;
548 }
549
550 DBG2(DBG_CHD, "adding %s %N SA", mine ? "inbound" : "outbound",
551 protocol_id_names, this->protocol);
552
553 /* select encryption algo */
554 if (proposal->get_algorithm(proposal, ENCRYPTION_ALGORITHM,
555 &this->enc_alg, &this->enc_size))
556 {
557 DBG2(DBG_CHD, " using %N for encryption",
558 encryption_algorithm_names, this->enc_alg);
559 }
560
561 /* select integrity algo */
562 if (proposal->get_algorithm(proposal, INTEGRITY_ALGORITHM,
563 &this->int_alg, &this->int_size))
564 {
565 DBG2(DBG_CHD, " using %N for integrity",
566 integrity_algorithm_names, this->int_alg);
567 }
568 soft = this->config->get_lifetime(this->config, TRUE);
569 hard = this->config->get_lifetime(this->config, FALSE);
570
571 /* send SA down to the kernel */
572 DBG2(DBG_CHD, " SPI 0x%.8x, src %H dst %H", ntohl(spi), src, dst);
573 status = charon->kernel_interface->add_sa(charon->kernel_interface,
574 src, dst, spi, this->protocol, this->reqid, mine ? soft : 0, hard,
575 this->enc_alg, this->enc_size, this->int_alg, this->int_size,
576 prf_plus, mode, this->encap, mine);
577
578 this->install_time = time(NULL);
579 this->rekey_time = this->install_time + soft;
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) &&
780 other->equals(other, this->other.addr) && this->encap == encap)
781 {
782 return SUCCESS;
783 }
784 /* run updown script to remove iptables rules */
785 updown(this, FALSE);
786
787 this->encap = encap;
788
789 /* update our (initator) SAs */
790 charon->kernel_interface->update_sa(charon->kernel_interface, this->me.spi,
791 this->protocol, this->other.addr, this->me.addr, other, me, encap);
792 /* update his (responder) SAs */
793 charon->kernel_interface->update_sa(charon->kernel_interface, this->other.spi,
794 this->protocol, this->me.addr, this->other.addr, me, other, encap);
795
796 /* update policies */
797 if (!me->ip_equals(me, this->me.addr) ||
798 !other->ip_equals(other, this->other.addr))
799 {
800 iterator_t *iterator;
801 sa_policy_t *policy;
802
803 /* always use high priorities, as hosts getting updated are INSTALLED */
804 iterator = this->policies->create_iterator(this->policies, TRUE);
805 while (iterator->iterate(iterator, (void**)&policy))
806 {
807 /* remove old policies first */
808 charon->kernel_interface->del_policy(charon->kernel_interface,
809 policy->my_ts, policy->other_ts, POLICY_OUT);
810 charon->kernel_interface->del_policy(charon->kernel_interface,
811 policy->other_ts, policy->my_ts, POLICY_IN);
812 charon->kernel_interface->del_policy(charon->kernel_interface,
813 policy->other_ts, policy->my_ts, POLICY_FWD);
814
815 /* check wether we have to update a "dynamic" traffic selector */
816 if (!me->ip_equals(me, this->me.addr) &&
817 policy->my_ts->is_host(policy->my_ts, this->me.addr))
818 {
819 policy->my_ts->set_address(policy->my_ts, me);
820 }
821 if (!other->ip_equals(other, this->other.addr) &&
822 policy->other_ts->is_host(policy->other_ts, this->other.addr))
823 {
824 policy->other_ts->set_address(policy->other_ts, other);
825 }
826
827 /* we reinstall the virtual IP to handle interface romaing
828 * correctly */
829 if (this->virtual_ip)
830 {
831 charon->kernel_interface->del_ip(charon->kernel_interface,
832 this->virtual_ip);
833 charon->kernel_interface->add_ip(charon->kernel_interface,
834 this->virtual_ip, me);
835 }
836
837 /* reinstall updated policies */
838 charon->kernel_interface->add_policy(charon->kernel_interface,
839 me, other, policy->my_ts, policy->other_ts, POLICY_OUT,
840 this->protocol, this->reqid, TRUE, this->mode);
841 charon->kernel_interface->add_policy(charon->kernel_interface,
842 other, me, policy->other_ts, policy->my_ts, POLICY_IN,
843 this->protocol, this->reqid, TRUE, this->mode);
844 charon->kernel_interface->add_policy(charon->kernel_interface,
845 other, me, policy->other_ts, policy->my_ts, POLICY_FWD,
846 this->protocol, this->reqid, TRUE, this->mode);
847 }
848 iterator->destroy(iterator);
849 }
850
851 /* apply hosts */
852 if (!me->equals(me, this->me.addr))
853 {
854 this->me.addr->destroy(this->me.addr);
855 this->me.addr = me->clone(me);
856 }
857 if (!other->equals(other, this->other.addr))
858 {
859 this->other.addr->destroy(this->other.addr);
860 this->other.addr = other->clone(other);
861 }
862
863 /* install new iptables rules */
864 updown(this, TRUE);
865
866 return SUCCESS;
867 }
868
869 /**
870 * Implementation of child_sa_t.set_virtual_ip.
871 */
872 static void set_virtual_ip(private_child_sa_t *this, host_t *ip)
873 {
874 this->virtual_ip = ip->clone(ip);
875 }
876
877 /**
878 * Implementation of child_sa_t.destroy.
879 */
880 static void destroy(private_child_sa_t *this)
881 {
882 sa_policy_t *policy;
883
884 if (this->state == CHILD_DELETING || this->state == CHILD_INSTALLED)
885 {
886 updown(this, FALSE);
887 }
888
889 /* delete SAs in the kernel, if they are set up */
890 if (this->me.spi)
891 {
892 charon->kernel_interface->del_sa(charon->kernel_interface,
893 this->me.addr, this->me.spi, this->protocol);
894 }
895 if (this->alloc_esp_spi && this->alloc_esp_spi != this->me.spi)
896 {
897 charon->kernel_interface->del_sa(charon->kernel_interface,
898 this->me.addr, this->alloc_esp_spi, PROTO_ESP);
899 }
900 if (this->alloc_ah_spi && this->alloc_ah_spi != this->me.spi)
901 {
902 charon->kernel_interface->del_sa(charon->kernel_interface,
903 this->me.addr, this->alloc_ah_spi, PROTO_AH);
904 }
905 if (this->other.spi)
906 {
907 charon->kernel_interface->del_sa(charon->kernel_interface,
908 this->other.addr, this->other.spi, this->protocol);
909 }
910
911 /* delete all policies in the kernel */
912 while (this->policies->remove_last(this->policies, (void**)&policy) == SUCCESS)
913 {
914 /* let rekeyed policies, as they are used by another child_sa */
915 charon->kernel_interface->del_policy(charon->kernel_interface,
916 policy->my_ts, policy->other_ts,
917 POLICY_OUT);
918
919 charon->kernel_interface->del_policy(charon->kernel_interface,
920 policy->other_ts, policy->my_ts,
921 POLICY_IN);
922
923 charon->kernel_interface->del_policy(charon->kernel_interface,
924 policy->other_ts, policy->my_ts,
925 POLICY_FWD);
926 policy->my_ts->destroy(policy->my_ts);
927 policy->other_ts->destroy(policy->other_ts);
928 free(policy);
929 }
930 this->policies->destroy(this->policies);
931
932 this->my_ts->destroy(this->my_ts);
933 this->other_ts->destroy(this->other_ts);
934 this->me.addr->destroy(this->me.addr);
935 this->other.addr->destroy(this->other.addr);
936 this->me.id->destroy(this->me.id);
937 this->other.id->destroy(this->other.id);
938 this->config->destroy(this->config);
939 free(this->iface);
940 DESTROY_IF(this->virtual_ip);
941 free(this);
942 }
943
944 /*
945 * Described in header.
946 */
947 child_sa_t * child_sa_create(host_t *me, host_t* other,
948 identification_t *my_id, identification_t *other_id,
949 child_cfg_t *config, u_int32_t rekey, bool encap)
950 {
951 static u_int32_t reqid = 0;
952 private_child_sa_t *this = malloc_thing(private_child_sa_t);
953
954 /* public functions */
955 this->public.get_name = (char*(*)(child_sa_t*))get_name;
956 this->public.get_reqid = (u_int32_t(*)(child_sa_t*))get_reqid;
957 this->public.get_spi = (u_int32_t(*)(child_sa_t*, bool))get_spi;
958 this->public.get_protocol = (protocol_id_t(*)(child_sa_t*))get_protocol;
959 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;
960 this->public.alloc = (status_t(*)(child_sa_t*,linked_list_t*))alloc;
961 this->public.add = (status_t(*)(child_sa_t*,proposal_t*,mode_t,prf_plus_t*))add;
962 this->public.update = (status_t(*)(child_sa_t*,proposal_t*,mode_t,prf_plus_t*))update;
963 this->public.update_hosts = (status_t (*)(child_sa_t*,host_t*,host_t*,bool))update_hosts;
964 this->public.add_policies = (status_t (*)(child_sa_t*, linked_list_t*,linked_list_t*,mode_t))add_policies;
965 this->public.get_traffic_selectors = (linked_list_t*(*)(child_sa_t*,bool))get_traffic_selectors;
966 this->public.get_use_time = (status_t (*)(child_sa_t*,bool,time_t*))get_use_time;
967 this->public.set_state = (void(*)(child_sa_t*,child_sa_state_t))set_state;
968 this->public.get_state = (child_sa_state_t(*)(child_sa_t*))get_state;
969 this->public.get_config = (child_cfg_t*(*)(child_sa_t*))get_config;
970 this->public.set_virtual_ip = (void(*)(child_sa_t*,host_t*))set_virtual_ip;
971 this->public.destroy = (void(*)(child_sa_t*))destroy;
972
973 /* private data */
974 this->me.addr = me->clone(me);
975 this->other.addr = other->clone(other);
976 this->me.id = my_id->clone(my_id);
977 this->other.id = other_id->clone(other_id);
978 this->me.spi = 0;
979 this->other.spi = 0;
980 this->alloc_ah_spi = 0;
981 this->alloc_esp_spi = 0;
982 this->encap = encap;
983 this->state = CHILD_CREATED;
984 /* reuse old reqid if we are rekeying an existing CHILD_SA */
985 this->reqid = rekey ? rekey : ++reqid;
986 this->enc_alg = ENCR_UNDEFINED;
987 this->enc_size = 0;
988 this->int_alg = AUTH_UNDEFINED;
989 this->int_size = 0;
990 this->policies = linked_list_create();
991 this->my_ts = linked_list_create();
992 this->other_ts = linked_list_create();
993 this->protocol = PROTO_NONE;
994 this->mode = MODE_TUNNEL;
995 this->virtual_ip = NULL;
996 this->iface = NULL;
997 this->config = config;
998 config->get_ref(config);
999
1000 return &this->public;
1001 }