2 * Copyright (C) 2006-2008 Tobias Brunner
3 * Copyright (C) 2005-2008 Martin Willi
4 * Copyright (C) 2006 Daniel Roethlisberger
5 * Copyright (C) 2005 Jan Hutter
6 * Hochschule fuer Technik Rapperswil
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
29 ENUM(child_sa_state_names
, CHILD_CREATED
, CHILD_DESTROYING
,
38 typedef struct sa_policy_t sa_policy_t
;
41 * Struct used to store information for a policy. This
42 * is needed since we must provide all this information
43 * for deleting a policy...
47 * Traffic selector for us
49 traffic_selector_t
*my_ts
;
52 * Traffic selector for other
54 traffic_selector_t
*other_ts
;
57 typedef struct private_child_sa_t private_child_sa_t
;
60 * Private data of a child_sa_t object.
62 struct private_child_sa_t
{
64 * Public interface of child_sa_t.
69 /** address of peer */
73 /** actual used SPI, 0 if unused */
75 /** Compression Parameter Index (CPI) used, 0 if unused */
80 * Allocated SPI for a ESP proposal candidates
82 u_int32_t alloc_esp_spi
;
85 * Allocated SPI for a AH proposal candidates
87 u_int32_t alloc_ah_spi
;
90 * Protocol used to protect this SA, ESP|AH
92 protocol_id_t protocol
;
95 * List containing sa_policy_t objects
97 linked_list_t
*policies
;
100 * Separate list for local traffic selectors
102 linked_list_t
*my_ts
;
105 * Separate list for remote traffic selectors
107 linked_list_t
*other_ts
;
110 * reqid used for this child_sa
115 * encryption algorithm used for this SA
120 * key size of enc_alg
125 * integrity protection algorithm used for this SA
130 * key size of int_alg
135 * time, on which SA was installed
140 * absolute time when rekeying is scheduled
145 * state of the CHILD_SA
147 child_sa_state_t state
;
150 * Specifies if UDP encapsulation is enabled (NAT traversal)
155 * Specifies the IPComp transform used (IPCOMP_NONE if disabled)
157 ipcomp_transform_t ipcomp
;
160 * TRUE if we allocated (or tried to allocate) a CPI
165 * mode this SA uses, tunnel/transport
170 * virtual IP assigned to local host
175 * config used to create this child
180 * cached interface name for iptables
186 * Implementation of child_sa_t.get_name.
188 static char *get_name(private_child_sa_t
*this)
190 return this->config
->get_name(this->config
);
194 * Implements child_sa_t.get_reqid
196 static u_int32_t
get_reqid(private_child_sa_t
*this)
202 * Implements child_sa_t.get_spi
204 u_int32_t
get_spi(private_child_sa_t
*this, bool inbound
)
210 return this->other
.spi
;
214 * Implements child_sa_t.get_cpi
216 u_int16_t
get_cpi(private_child_sa_t
*this, bool inbound
)
222 return this->other
.cpi
;
226 * Implements child_sa_t.get_protocol
228 protocol_id_t
get_protocol(private_child_sa_t
*this)
230 return this->protocol
;
234 * Implements child_sa_t.get_state
236 static child_sa_state_t
get_state(private_child_sa_t
*this)
242 * Implements child_sa_t.get_config
244 static child_cfg_t
* get_config(private_child_sa_t
*this)
250 * Implementation of child_sa_t.get_stats.
252 static void get_stats(private_child_sa_t
*this, ipsec_mode_t
*mode
,
253 encryption_algorithm_t
*encr_algo
, size_t *encr_len
,
254 integrity_algorithm_t
*int_algo
, size_t *int_len
,
255 u_int32_t
*rekey
, u_int32_t
*use_in
, u_int32_t
*use_out
,
259 iterator_t
*iterator
;
260 u_int32_t in
= 0, out
= 0, fwd
= 0, time
;
262 iterator
= this->policies
->create_iterator(this->policies
, TRUE
);
263 while (iterator
->iterate(iterator
, (void**)&policy
))
266 if (charon
->kernel_interface
->query_policy(charon
->kernel_interface
,
267 policy
->other_ts
, policy
->my_ts
, POLICY_IN
, &time
) == SUCCESS
)
271 if (charon
->kernel_interface
->query_policy(charon
->kernel_interface
,
272 policy
->my_ts
, policy
->other_ts
, POLICY_OUT
, &time
) == SUCCESS
)
274 out
= max(out
, time
);
276 if (charon
->kernel_interface
->query_policy(charon
->kernel_interface
,
277 policy
->other_ts
, policy
->my_ts
, POLICY_FWD
, &time
) == SUCCESS
)
279 fwd
= max(fwd
, time
);
282 iterator
->destroy(iterator
);
285 *encr_algo
= this->enc_alg
;
286 *encr_len
= this->enc_size
;
287 *int_algo
= this->int_alg
;
288 *int_len
= this->int_size
;
289 *rekey
= this->rekey_time
;
296 * Run the up/down script
298 static void updown(private_child_sa_t
*this, bool up
)
301 iterator_t
*iterator
;
304 script
= this->config
->get_updown(this->config
);
311 iterator
= this->policies
->create_iterator(this->policies
, TRUE
);
312 while (iterator
->iterate(iterator
, (void**)&policy
))
315 char *my_client
, *other_client
, *my_client_mask
, *other_client_mask
;
316 char *pos
, *virtual_ip
;
319 /* get subnet/bits from string */
320 asprintf(&my_client
, "%R", policy
->my_ts
);
321 pos
= strchr(my_client
, '/');
323 my_client_mask
= pos
+ 1;
324 pos
= strchr(my_client_mask
, '[');
329 asprintf(&other_client
, "%R", policy
->other_ts
);
330 pos
= strchr(other_client
, '/');
332 other_client_mask
= pos
+ 1;
333 pos
= strchr(other_client_mask
, '[');
339 if (this->virtual_ip
)
341 asprintf(&virtual_ip
, "PLUTO_MY_SOURCEIP='%H' ",
346 asprintf(&virtual_ip
, "");
349 /* we cache the iface name, as it may not be available when
350 * the SA gets deleted */
354 this->iface
= charon
->kernel_interface
->get_interface(
355 charon
->kernel_interface
, this->me
.addr
);
358 /* build the command with all env variables.
359 * TODO: PLUTO_PEER_CA and PLUTO_NEXT_HOP are currently missing
361 snprintf(command
, sizeof(command
),
363 "PLUTO_VERSION='1.1' "
364 "PLUTO_VERB='%s%s%s' "
365 "PLUTO_CONNECTION='%s' "
366 "PLUTO_INTERFACE='%s' "
370 "PLUTO_MY_CLIENT='%s/%s' "
371 "PLUTO_MY_CLIENT_NET='%s' "
372 "PLUTO_MY_CLIENT_MASK='%s' "
373 "PLUTO_MY_PORT='%u' "
374 "PLUTO_MY_PROTOCOL='%u' "
376 "PLUTO_PEER_ID='%D' "
377 "PLUTO_PEER_CLIENT='%s/%s' "
378 "PLUTO_PEER_CLIENT_NET='%s' "
379 "PLUTO_PEER_CLIENT_MASK='%s' "
380 "PLUTO_PEER_PORT='%u' "
381 "PLUTO_PEER_PROTOCOL='%u' "
386 policy
->my_ts
->is_host(policy
->my_ts
,
387 this->me
.addr
) ?
"-host" : "-client",
388 this->me
.addr
->get_family(this->me
.addr
) == AF_INET ?
"" : "-v6",
389 this->config
->get_name(this->config
),
390 this->iface ?
this->iface
: "unknown",
394 my_client
, my_client_mask
,
395 my_client
, my_client_mask
,
396 policy
->my_ts
->get_from_port(policy
->my_ts
),
397 policy
->my_ts
->get_protocol(policy
->my_ts
),
400 other_client
, other_client_mask
,
401 other_client
, other_client_mask
,
402 policy
->other_ts
->get_from_port(policy
->other_ts
),
403 policy
->other_ts
->get_protocol(policy
->other_ts
),
405 this->config
->get_hostaccess(this->config
) ?
406 "PLUTO_HOST_ACCESS='1' " : "",
412 DBG3(DBG_CHD
, "running updown script: %s", command
);
413 shell
= popen(command
, "r");
417 DBG1(DBG_CHD
, "could not execute updown script '%s'", script
);
425 if (fgets(resp
, sizeof(resp
), shell
) == NULL
)
429 DBG1(DBG_CHD
, "error reading output from updown script");
439 char *e
= resp
+ strlen(resp
);
440 if (e
> resp
&& e
[-1] == '\n')
441 { /* trim trailing '\n' */
444 DBG1(DBG_CHD
, "updown: %s", resp
);
449 iterator
->destroy(iterator
);
453 * Implements child_sa_t.set_state
455 static void set_state(private_child_sa_t
*this, child_sa_state_t state
)
457 if (state
== CHILD_INSTALLED
)
461 charon
->bus
->child_state_change(charon
->bus
, &this->public, state
);
466 * Allocate SPI for a single proposal
468 static status_t
alloc_proposal(private_child_sa_t
*this, proposal_t
*proposal
)
470 protocol_id_t protocol
= proposal
->get_protocol(proposal
);
472 if (protocol
== PROTO_AH
)
474 /* get a new spi for AH, if not already done */
475 if (this->alloc_ah_spi
== 0)
477 if (charon
->kernel_interface
->get_spi(
478 charon
->kernel_interface
,
479 this->other
.addr
, this->me
.addr
,
480 PROTO_AH
, this->reqid
,
481 &this->alloc_ah_spi
) != SUCCESS
)
486 proposal
->set_spi(proposal
, this->alloc_ah_spi
);
488 if (protocol
== PROTO_ESP
)
490 /* get a new spi for ESP, if not already done */
491 if (this->alloc_esp_spi
== 0)
493 if (charon
->kernel_interface
->get_spi(
494 charon
->kernel_interface
,
495 this->other
.addr
, this->me
.addr
,
496 PROTO_ESP
, this->reqid
,
497 &this->alloc_esp_spi
) != SUCCESS
)
502 proposal
->set_spi(proposal
, this->alloc_esp_spi
);
509 * Implements child_sa_t.alloc
511 static status_t
alloc(private_child_sa_t
*this, linked_list_t
*proposals
)
513 iterator_t
*iterator
;
514 proposal_t
*proposal
;
516 /* iterator through proposals to update spis */
517 iterator
= proposals
->create_iterator(proposals
, TRUE
);
518 while(iterator
->iterate(iterator
, (void**)&proposal
))
520 if (alloc_proposal(this, proposal
) != SUCCESS
)
522 iterator
->destroy(iterator
);
526 iterator
->destroy(iterator
);
530 static status_t
install(private_child_sa_t
*this, proposal_t
*proposal
,
531 ipsec_mode_t mode
, prf_plus_t
*prf_plus
, bool mine
)
533 u_int32_t spi
, soft
, hard
;
538 this->protocol
= proposal
->get_protocol(proposal
);
540 /* now we have to decide which spi to use. Use self allocated, if "mine",
541 * or the one in the proposal, if not "mine" (others). Additionally,
542 * source and dest host switch depending on the role */
545 /* if we have allocated SPIs for AH and ESP, we must delete the unused
547 if (this->protocol
== PROTO_ESP
)
549 this->me
.spi
= this->alloc_esp_spi
;
550 if (this->alloc_ah_spi
)
552 charon
->kernel_interface
->del_sa(charon
->kernel_interface
, this->me
.addr
,
553 this->alloc_ah_spi
, PROTO_AH
);
558 this->me
.spi
= this->alloc_ah_spi
;
559 if (this->alloc_esp_spi
)
561 charon
->kernel_interface
->del_sa(charon
->kernel_interface
, this->me
.addr
,
562 this->alloc_esp_spi
, PROTO_ESP
);
567 src
= this->other
.addr
;
571 this->other
.spi
= proposal
->get_spi(proposal
);
572 spi
= this->other
.spi
;
574 dst
= this->other
.addr
;
577 DBG2(DBG_CHD
, "adding %s %N SA", mine ?
"inbound" : "outbound",
578 protocol_id_names
, this->protocol
);
580 /* select encryption algo */
581 if (proposal
->get_algorithm(proposal
, ENCRYPTION_ALGORITHM
,
582 &this->enc_alg
, &this->enc_size
))
584 DBG2(DBG_CHD
, " using %N for encryption",
585 encryption_algorithm_names
, this->enc_alg
);
588 /* select integrity algo */
589 if (proposal
->get_algorithm(proposal
, INTEGRITY_ALGORITHM
,
590 &this->int_alg
, &this->int_size
))
592 DBG2(DBG_CHD
, " using %N for integrity",
593 integrity_algorithm_names
, this->int_alg
);
595 soft
= this->config
->get_lifetime(this->config
, TRUE
);
596 hard
= this->config
->get_lifetime(this->config
, FALSE
);
598 /* send SA down to the kernel */
599 DBG2(DBG_CHD
, " SPI 0x%.8x, src %H dst %H", ntohl(spi
), src
, dst
);
601 if (this->ipcomp
!= IPCOMP_NONE
)
603 /* we install an additional IPComp SA */
604 u_int32_t cpi
= htonl(ntohs(mine ?
this->me
.cpi
: this->other
.cpi
));
605 status
= charon
->kernel_interface
->add_sa(charon
->kernel_interface
,
606 src
, dst
, cpi
, IPPROTO_COMP
, this->reqid
, 0, 0,
607 ENCR_UNDEFINED
, 0, AUTH_UNDEFINED
, 0, NULL
, mode
,
608 this->ipcomp
, FALSE
, mine
);
611 status
= charon
->kernel_interface
->add_sa(charon
->kernel_interface
,
612 src
, dst
, spi
, this->protocol
, this->reqid
, mine ? soft
: 0, hard
,
613 this->enc_alg
, this->enc_size
, this->int_alg
, this->int_size
,
614 prf_plus
, mode
, IPCOMP_NONE
, this->encap
, mine
);
616 this->install_time
= time(NULL
);
617 this->rekey_time
= this->install_time
+ soft
;
621 static status_t
add(private_child_sa_t
*this, proposal_t
*proposal
,
622 ipsec_mode_t mode
, prf_plus_t
*prf_plus
)
624 u_int32_t outbound_spi
, inbound_spi
;
626 /* backup outbound spi, as alloc overwrites it */
627 outbound_spi
= proposal
->get_spi(proposal
);
629 /* get SPIs inbound SAs */
630 if (alloc_proposal(this, proposal
) != SUCCESS
)
634 inbound_spi
= proposal
->get_spi(proposal
);
636 /* install inbound SAs */
637 if (install(this, proposal
, mode
, prf_plus
, TRUE
) != SUCCESS
)
642 /* install outbound SAs, restore spi*/
643 proposal
->set_spi(proposal
, outbound_spi
);
644 if (install(this, proposal
, mode
, prf_plus
, FALSE
) != SUCCESS
)
648 proposal
->set_spi(proposal
, inbound_spi
);
653 static status_t
update(private_child_sa_t
*this, proposal_t
*proposal
,
654 ipsec_mode_t mode
, prf_plus_t
*prf_plus
)
656 u_int32_t inbound_spi
;
658 /* backup received spi, as install() overwrites it */
659 inbound_spi
= proposal
->get_spi(proposal
);
661 /* install outbound SAs */
662 if (install(this, proposal
, mode
, prf_plus
, FALSE
) != SUCCESS
)
668 proposal
->set_spi(proposal
, inbound_spi
);
669 /* install inbound SAs */
670 if (install(this, proposal
, mode
, prf_plus
, TRUE
) != SUCCESS
)
678 static status_t
add_policies(private_child_sa_t
*this,
679 linked_list_t
*my_ts_list
, linked_list_t
*other_ts_list
,
680 ipsec_mode_t mode
, protocol_id_t proto
)
682 iterator_t
*my_iter
, *other_iter
;
683 traffic_selector_t
*my_ts
, *other_ts
;
684 /* use low prio for ROUTED policies */
685 bool high_prio
= (this->state
!= CHILD_CREATED
);
687 if (this->protocol
== PROTO_NONE
)
688 { /* update if not set yet */
689 this->protocol
= proto
;
692 /* iterate over both lists */
693 my_iter
= my_ts_list
->create_iterator(my_ts_list
, TRUE
);
694 other_iter
= other_ts_list
->create_iterator(other_ts_list
, TRUE
);
695 while (my_iter
->iterate(my_iter
, (void**)&my_ts
))
697 other_iter
->reset(other_iter
);
698 while (other_iter
->iterate(other_iter
, (void**)&other_ts
))
700 /* set up policies for every entry in my_ts_list to every entry in other_ts_list */
704 if (my_ts
->get_type(my_ts
) != other_ts
->get_type(other_ts
))
707 "CHILD_SA policy uses two different IP families - ignored");
711 /* only set up policies if protocol matches, or if one is zero (any) */
712 if (my_ts
->get_protocol(my_ts
) != other_ts
->get_protocol(other_ts
) &&
713 my_ts
->get_protocol(my_ts
) && other_ts
->get_protocol(other_ts
))
716 "CHILD_SA policy uses two different protocols - ignored");
720 /* install 3 policies: out, in and forward */
721 status
= charon
->kernel_interface
->add_policy(charon
->kernel_interface
,
722 this->me
.addr
, this->other
.addr
, my_ts
, other_ts
, POLICY_OUT
,
723 this->protocol
, this->reqid
, high_prio
, mode
, this->ipcomp
);
725 status
|= charon
->kernel_interface
->add_policy(charon
->kernel_interface
,
726 this->other
.addr
, this->me
.addr
, other_ts
, my_ts
, POLICY_IN
,
727 this->protocol
, this->reqid
, high_prio
, mode
, this->ipcomp
);
729 status
|= charon
->kernel_interface
->add_policy(charon
->kernel_interface
,
730 this->other
.addr
, this->me
.addr
, other_ts
, my_ts
, POLICY_FWD
,
731 this->protocol
, this->reqid
, high_prio
, mode
, this->ipcomp
);
733 if (status
!= SUCCESS
)
735 my_iter
->destroy(my_iter
);
736 other_iter
->destroy(other_iter
);
740 /* store policy to delete/update them later */
741 policy
= malloc_thing(sa_policy_t
);
742 policy
->my_ts
= my_ts
->clone(my_ts
);
743 policy
->other_ts
= other_ts
->clone(other_ts
);
744 this->policies
->insert_last(this->policies
, policy
);
745 /* add to separate list to query them via get_*_traffic_selectors() */
746 this->my_ts
->insert_last(this->my_ts
, policy
->my_ts
);
747 this->other_ts
->insert_last(this->other_ts
, policy
->other_ts
);
750 my_iter
->destroy(my_iter
);
751 other_iter
->destroy(other_iter
);
753 /* switch to routed state if no SAD entry set up */
754 if (this->state
== CHILD_CREATED
)
756 set_state(this, CHILD_ROUTED
);
758 /* needed to update hosts */
764 * Implementation of child_sa_t.get_traffic_selectors.
766 static linked_list_t
*get_traffic_selectors(private_child_sa_t
*this, bool local
)
772 return this->other_ts
;
776 * Implementation of child_sa_t.get_use_time
778 static status_t
get_use_time(private_child_sa_t
*this, bool inbound
, time_t *use_time
)
780 iterator_t
*iterator
;
782 status_t status
= FAILED
;
784 *use_time
= UNDEFINED_TIME
;
786 iterator
= this->policies
->create_iterator(this->policies
, TRUE
);
787 while (iterator
->iterate(iterator
, (void**)&policy
))
791 time_t in
= UNDEFINED_TIME
, fwd
= UNDEFINED_TIME
;
793 status
= charon
->kernel_interface
->query_policy(
794 charon
->kernel_interface
,
795 policy
->other_ts
, policy
->my_ts
,
796 POLICY_IN
, (u_int32_t
*)&in
);
797 status
|= charon
->kernel_interface
->query_policy(
798 charon
->kernel_interface
,
799 policy
->other_ts
, policy
->my_ts
,
800 POLICY_FWD
, (u_int32_t
*)&fwd
);
801 *use_time
= max(in
, fwd
);
805 status
= charon
->kernel_interface
->query_policy(
806 charon
->kernel_interface
,
807 policy
->my_ts
, policy
->other_ts
,
808 POLICY_OUT
, (u_int32_t
*)use_time
);
811 iterator
->destroy(iterator
);
816 * Implementation of child_sa_t.update_hosts.
818 static status_t
update_hosts(private_child_sa_t
*this,
819 host_t
*me
, host_t
*other
, bool encap
)
821 /* anything changed at all? */
822 if (me
->equals(me
, this->me
.addr
) &&
823 other
->equals(other
, this->other
.addr
) && this->encap
== encap
)
827 /* run updown script to remove iptables rules */
832 if (this->ipcomp
!= IPCOMP_NONE
)
834 /* update our (initator) IPComp SA */
835 charon
->kernel_interface
->update_sa(charon
->kernel_interface
, htonl(ntohs(this->me
.cpi
)),
836 IPPROTO_COMP
, this->other
.addr
, this->me
.addr
, other
, me
, FALSE
);
837 /* update his (responder) IPComp SA */
838 charon
->kernel_interface
->update_sa(charon
->kernel_interface
, htonl(ntohs(this->other
.cpi
)),
839 IPPROTO_COMP
, this->me
.addr
, this->other
.addr
, me
, other
, FALSE
);
842 /* update our (initator) SA */
843 charon
->kernel_interface
->update_sa(charon
->kernel_interface
, this->me
.spi
,
844 this->protocol
, this->other
.addr
, this->me
.addr
, other
, me
, encap
);
845 /* update his (responder) SA */
846 charon
->kernel_interface
->update_sa(charon
->kernel_interface
, this->other
.spi
,
847 this->protocol
, this->me
.addr
, this->other
.addr
, me
, other
, encap
);
849 /* update policies */
850 if (!me
->ip_equals(me
, this->me
.addr
) ||
851 !other
->ip_equals(other
, this->other
.addr
))
853 iterator_t
*iterator
;
856 /* always use high priorities, as hosts getting updated are INSTALLED */
857 iterator
= this->policies
->create_iterator(this->policies
, TRUE
);
858 while (iterator
->iterate(iterator
, (void**)&policy
))
860 /* remove old policies first */
861 charon
->kernel_interface
->del_policy(charon
->kernel_interface
,
862 policy
->my_ts
, policy
->other_ts
, POLICY_OUT
);
863 charon
->kernel_interface
->del_policy(charon
->kernel_interface
,
864 policy
->other_ts
, policy
->my_ts
, POLICY_IN
);
865 charon
->kernel_interface
->del_policy(charon
->kernel_interface
,
866 policy
->other_ts
, policy
->my_ts
, POLICY_FWD
);
868 /* check whether we have to update a "dynamic" traffic selector */
869 if (!me
->ip_equals(me
, this->me
.addr
) &&
870 policy
->my_ts
->is_host(policy
->my_ts
, this->me
.addr
))
872 policy
->my_ts
->set_address(policy
->my_ts
, me
);
874 if (!other
->ip_equals(other
, this->other
.addr
) &&
875 policy
->other_ts
->is_host(policy
->other_ts
, this->other
.addr
))
877 policy
->other_ts
->set_address(policy
->other_ts
, other
);
880 /* we reinstall the virtual IP to handle interface roaming
882 if (this->virtual_ip
)
884 charon
->kernel_interface
->del_ip(charon
->kernel_interface
,
886 charon
->kernel_interface
->add_ip(charon
->kernel_interface
,
887 this->virtual_ip
, me
);
890 /* reinstall updated policies */
891 charon
->kernel_interface
->add_policy(charon
->kernel_interface
,
892 me
, other
, policy
->my_ts
, policy
->other_ts
, POLICY_OUT
,
893 this->protocol
, this->reqid
, TRUE
, this->mode
, this->ipcomp
);
894 charon
->kernel_interface
->add_policy(charon
->kernel_interface
,
895 other
, me
, policy
->other_ts
, policy
->my_ts
, POLICY_IN
,
896 this->protocol
, this->reqid
, TRUE
, this->mode
, this->ipcomp
);
897 charon
->kernel_interface
->add_policy(charon
->kernel_interface
,
898 other
, me
, policy
->other_ts
, policy
->my_ts
, POLICY_FWD
,
899 this->protocol
, this->reqid
, TRUE
, this->mode
, this->ipcomp
);
901 iterator
->destroy(iterator
);
905 if (!me
->equals(me
, this->me
.addr
))
907 this->me
.addr
->destroy(this->me
.addr
);
908 this->me
.addr
= me
->clone(me
);
910 if (!other
->equals(other
, this->other
.addr
))
912 this->other
.addr
->destroy(this->other
.addr
);
913 this->other
.addr
= other
->clone(other
);
916 /* install new iptables rules */
923 * Implementation of child_sa_t.set_virtual_ip.
925 static void set_virtual_ip(private_child_sa_t
*this, host_t
*ip
)
927 this->virtual_ip
= ip
->clone(ip
);
931 * Implementation of child_sa_t.activate_ipcomp.
933 static void activate_ipcomp(private_child_sa_t
*this, ipcomp_transform_t ipcomp
,
936 this->ipcomp
= ipcomp
;
937 this->other
.cpi
= other_cpi
;
941 * Implementation of child_sa_t.allocate_cpi.
943 static u_int16_t
allocate_cpi(private_child_sa_t
*this)
945 if (!this->cpi_allocated
)
947 charon
->kernel_interface
->get_cpi(charon
->kernel_interface
,
948 this->other
.addr
, this->me
.addr
, this->reqid
, &this->me
.cpi
);
949 this->cpi_allocated
= TRUE
;
955 * Implementation of child_sa_t.destroy.
957 static void destroy(private_child_sa_t
*this)
961 if (this->state
== CHILD_DELETING
|| this->state
== CHILD_INSTALLED
)
966 set_state(this, CHILD_DESTROYING
);
968 /* delete SAs in the kernel, if they are set up */
971 charon
->kernel_interface
->del_sa(charon
->kernel_interface
,
972 this->me
.addr
, this->me
.spi
, this->protocol
);
974 if (this->alloc_esp_spi
&& this->alloc_esp_spi
!= this->me
.spi
)
976 charon
->kernel_interface
->del_sa(charon
->kernel_interface
,
977 this->me
.addr
, this->alloc_esp_spi
, PROTO_ESP
);
979 if (this->alloc_ah_spi
&& this->alloc_ah_spi
!= this->me
.spi
)
981 charon
->kernel_interface
->del_sa(charon
->kernel_interface
,
982 this->me
.addr
, this->alloc_ah_spi
, PROTO_AH
);
986 charon
->kernel_interface
->del_sa(charon
->kernel_interface
,
987 this->other
.addr
, this->other
.spi
, this->protocol
);
991 charon
->kernel_interface
->del_sa(charon
->kernel_interface
,
992 this->me
.addr
, htonl(ntohs(this->me
.cpi
)), IPPROTO_COMP
);
996 charon
->kernel_interface
->del_sa(charon
->kernel_interface
,
997 this->other
.addr
, htonl(ntohs(this->other
.cpi
)), IPPROTO_COMP
);
1000 /* delete all policies in the kernel */
1001 while (this->policies
->remove_last(this->policies
, (void**)&policy
) == SUCCESS
)
1003 /* let rekeyed policies, as they are used by another child_sa */
1004 charon
->kernel_interface
->del_policy(charon
->kernel_interface
,
1005 policy
->my_ts
, policy
->other_ts
,
1008 charon
->kernel_interface
->del_policy(charon
->kernel_interface
,
1009 policy
->other_ts
, policy
->my_ts
,
1012 charon
->kernel_interface
->del_policy(charon
->kernel_interface
,
1013 policy
->other_ts
, policy
->my_ts
,
1015 policy
->my_ts
->destroy(policy
->my_ts
);
1016 policy
->other_ts
->destroy(policy
->other_ts
);
1019 this->policies
->destroy(this->policies
);
1021 this->my_ts
->destroy(this->my_ts
);
1022 this->other_ts
->destroy(this->other_ts
);
1023 this->me
.addr
->destroy(this->me
.addr
);
1024 this->other
.addr
->destroy(this->other
.addr
);
1025 this->me
.id
->destroy(this->me
.id
);
1026 this->other
.id
->destroy(this->other
.id
);
1027 this->config
->destroy(this->config
);
1029 DESTROY_IF(this->virtual_ip
);
1034 * Described in header.
1036 child_sa_t
* child_sa_create(host_t
*me
, host_t
* other
,
1037 identification_t
*my_id
, identification_t
*other_id
,
1038 child_cfg_t
*config
, u_int32_t rekey
, bool encap
)
1040 static u_int32_t reqid
= 0;
1041 private_child_sa_t
*this = malloc_thing(private_child_sa_t
);
1043 /* public functions */
1044 this->public.get_name
= (char*(*)(child_sa_t
*))get_name
;
1045 this->public.get_reqid
= (u_int32_t(*)(child_sa_t
*))get_reqid
;
1046 this->public.get_spi
= (u_int32_t(*)(child_sa_t
*, bool))get_spi
;
1047 this->public.get_cpi
= (u_int16_t(*)(child_sa_t
*, bool))get_cpi
;
1048 this->public.get_protocol
= (protocol_id_t(*)(child_sa_t
*))get_protocol
;
1049 this->public.get_stats
= (void(*)(child_sa_t
*, ipsec_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
;
1050 this->public.alloc
= (status_t(*)(child_sa_t
*,linked_list_t
*))alloc
;
1051 this->public.add
= (status_t(*)(child_sa_t
*,proposal_t
*,ipsec_mode_t
,prf_plus_t
*))add
;
1052 this->public.update
= (status_t(*)(child_sa_t
*,proposal_t
*,ipsec_mode_t
,prf_plus_t
*))update
;
1053 this->public.update_hosts
= (status_t (*)(child_sa_t
*,host_t
*,host_t
*,bool))update_hosts
;
1054 this->public.add_policies
= (status_t (*)(child_sa_t
*, linked_list_t
*,linked_list_t
*,ipsec_mode_t
,protocol_id_t
))add_policies
;
1055 this->public.get_traffic_selectors
= (linked_list_t
*(*)(child_sa_t
*,bool))get_traffic_selectors
;
1056 this->public.get_use_time
= (status_t (*)(child_sa_t
*,bool,time_t*))get_use_time
;
1057 this->public.set_state
= (void(*)(child_sa_t
*,child_sa_state_t
))set_state
;
1058 this->public.get_state
= (child_sa_state_t(*)(child_sa_t
*))get_state
;
1059 this->public.get_config
= (child_cfg_t
*(*)(child_sa_t
*))get_config
;
1060 this->public.activate_ipcomp
= (void(*)(child_sa_t
*,ipcomp_transform_t
,u_int16_t
))activate_ipcomp
;
1061 this->public.allocate_cpi
= (u_int16_t(*)(child_sa_t
*))allocate_cpi
;
1062 this->public.set_virtual_ip
= (void(*)(child_sa_t
*,host_t
*))set_virtual_ip
;
1063 this->public.destroy
= (void(*)(child_sa_t
*))destroy
;
1066 this->me
.addr
= me
->clone(me
);
1067 this->other
.addr
= other
->clone(other
);
1068 this->me
.id
= my_id
->clone(my_id
);
1069 this->other
.id
= other_id
->clone(other_id
);
1072 this->other
.spi
= 0;
1073 this->other
.cpi
= 0;
1074 this->alloc_ah_spi
= 0;
1075 this->alloc_esp_spi
= 0;
1076 this->encap
= encap
;
1077 this->cpi_allocated
= FALSE
;
1078 this->ipcomp
= IPCOMP_NONE
;
1079 this->state
= CHILD_CREATED
;
1080 /* reuse old reqid if we are rekeying an existing CHILD_SA */
1081 this->reqid
= rekey ? rekey
: ++reqid
;
1082 this->enc_alg
= ENCR_UNDEFINED
;
1084 this->int_alg
= AUTH_UNDEFINED
;
1086 this->policies
= linked_list_create();
1087 this->my_ts
= linked_list_create();
1088 this->other_ts
= linked_list_create();
1089 this->protocol
= PROTO_NONE
;
1090 this->mode
= MODE_TUNNEL
;
1091 this->virtual_ip
= NULL
;
1093 this->config
= config
;
1094 config
->get_ref(config
);
1096 return &this->public;