f7ec5768591b4c328dbd07b60b9d65d2ee43824d
[strongswan.git] / src / charon / processing / jobs / migrate_job.c
1 /*
2 * Copyright (C) 2006 Martin Willi
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 *
15 * $Id: acquire_job.c 4535 2008-10-31 01:43:23Z andreas $
16 */
17
18 #include "migrate_job.h"
19
20 #include <daemon.h>
21
22 #include <config/child_cfg.h>
23
24
25 typedef struct private_migrate_job_t private_migrate_job_t;
26
27 /**
28 * Private data of a migrate_job_t object.
29 */
30 struct private_migrate_job_t {
31 /**
32 * Public migrate_job_t interface.
33 */
34 migrate_job_t public;
35
36 /**
37 * reqid of the CHILD_SA if it already exists
38 */
39 u_int32_t reqid;
40
41 /**
42 * source traffic selector
43 */
44 traffic_selector_t *src_ts;
45
46 /**
47 * destination traffic selector
48 */
49 traffic_selector_t *dst_ts;
50
51 /**
52 * local host address to be used
53 */
54 host_t *local;
55 };
56
57 /**
58 * Implementation of job_t.destroy.
59 */
60 static void destroy(private_migrate_job_t *this)
61 {
62 DESTROY_IF(this->src_ts);
63 DESTROY_IF(this->dst_ts);
64 DESTROY_IF(this->local);
65 free(this);
66 }
67
68 /**
69 * Implementation of job_t.execute.
70 */
71 static void execute(private_migrate_job_t *this)
72 {
73 ike_sa_t *ike_sa = NULL;
74
75 if (this->reqid)
76 {
77 ike_sa = charon->ike_sa_manager->checkout_by_id(charon->ike_sa_manager,
78 this->reqid, TRUE);
79 }
80 if (ike_sa == NULL)
81 {
82 enumerator_t *enumerator, *children;
83 peer_cfg_t *peer_cfg;
84 child_cfg_t *found_cfg = NULL;
85
86 enumerator = charon->backends->create_peer_cfg_enumerator(charon->backends);
87 while (enumerator->enumerate(enumerator, (void**)&peer_cfg))
88 {
89 child_cfg_t *child_cfg;
90
91 if (peer_cfg->get_ike_version(peer_cfg) != 2)
92 {
93 continue;
94 }
95
96 children = peer_cfg->create_child_cfg_enumerator(peer_cfg);
97 while (children->enumerate(children, &child_cfg))
98 {
99 if (child_cfg->equal_traffic_selectors(child_cfg, TRUE, this->src_ts) &&
100 child_cfg->equal_traffic_selectors(child_cfg, FALSE, this->dst_ts))
101 {
102 found_cfg = child_cfg;
103 break;
104 }
105 }
106 children->destroy(children);
107 if (found_cfg)
108 {
109 break;
110 }
111 }
112 enumerator->destroy(enumerator);
113
114 if (found_cfg == NULL)
115 {
116 DBG1(DBG_JOB, "no matching child config found for policy %R === %R",
117 this->src_ts, this->dst_ts);
118 destroy(this);
119 return;
120 }
121 DBG1(DBG_JOB, "found matching child config '%s' for policy %R === %R",
122 found_cfg->get_name(found_cfg),
123 this->src_ts, this->dst_ts);
124
125 ike_sa = charon->ike_sa_manager->checkout_by_config(charon->ike_sa_manager,
126 peer_cfg);
127 if (ike_sa->get_peer_cfg(ike_sa) == NULL)
128 {
129 host_t *my_host, *other_host;
130 ike_cfg_t *ike_cfg;
131
132 ike_sa->set_peer_cfg(ike_sa, peer_cfg);
133 ike_cfg = peer_cfg->get_ike_cfg(peer_cfg);
134 my_host = host_create_from_dns(ike_cfg->get_my_addr(ike_cfg), 0, 0);
135 other_host = host_create_from_dns(ike_cfg->get_other_addr(ike_cfg), 0, 0);
136 ike_sa->set_my_host(ike_sa, my_host);
137 ike_sa->set_other_host(ike_sa, other_host);
138 }
139 if (this->local)
140 {
141 ike_sa->set_my_host(ike_sa, this->local->clone(this->local));
142 }
143 /* add a CHILD_SA for 'found_cfg' with a policy that has already been
144 * installed in the kernel
145 */
146 }
147 else
148 {
149 DBG1(DBG_JOB, "found CHILD_SA with reqid {%d}", this->reqid);
150 if (this->local)
151 {
152 ike_sa->set_my_host(ike_sa, this->local);
153 }
154 }
155 charon->ike_sa_manager->checkin(charon->ike_sa_manager, ike_sa);
156 destroy(this);
157 }
158
159 /*
160 * Described in header
161 */
162 migrate_job_t *migrate_job_create(u_int32_t reqid,
163 traffic_selector_t *src_ts,
164 traffic_selector_t *dst_ts,
165 policy_dir_t dir,
166 host_t *local)
167 {
168 private_migrate_job_t *this = malloc_thing(private_migrate_job_t);
169
170 /* interface functions */
171 this->public.job_interface.execute = (void (*) (job_t *)) execute;
172 this->public.job_interface.destroy = (void (*)(job_t*)) destroy;
173
174 /* private variables */
175 this->reqid = reqid;
176 this->src_ts = (dir == POLICY_OUT) ? src_ts : dst_ts;
177 this->dst_ts = (dir == POLICY_OUT) ? dst_ts : src_ts;
178 this->local = local;
179
180 return &this->public;
181 }