kernel-interface: Raise mapping event with a proto/SPI/dst tuple
[strongswan.git] / src / libcharon / processing / jobs / adopt_children_job.c
1 /*
2 * Copyright (C) 2012 Martin Willi
3 * Copyright (C) 2012 revosec AG
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
16 #include "adopt_children_job.h"
17
18 #include <daemon.h>
19 #include <hydra.h>
20 #include <collections/array.h>
21
22 typedef struct private_adopt_children_job_t private_adopt_children_job_t;
23
24 /**
25 * Private data of an adopt_children_job_t object.
26 */
27 struct private_adopt_children_job_t {
28
29 /**
30 * Public adopt_children_job_t interface.
31 */
32 adopt_children_job_t public;
33
34 /**
35 * IKE_SA id to adopt children from
36 */
37 ike_sa_id_t *id;
38
39 /**
40 * Tasks queued for execution
41 */
42 array_t *tasks;
43 };
44
45 METHOD(job_t, destroy, void,
46 private_adopt_children_job_t *this)
47 {
48 array_destroy_offset(this->tasks, offsetof(task_t, destroy));
49 this->id->destroy(this->id);
50 free(this);
51 }
52
53 METHOD(job_t, execute, job_requeue_t,
54 private_adopt_children_job_t *this)
55 {
56 identification_t *my_id, *other_id, *xauth;
57 host_t *me, *other;
58 peer_cfg_t *cfg;
59 linked_list_t *children;
60 enumerator_t *enumerator, *childenum;
61 ike_sa_id_t *id;
62 ike_sa_t *ike_sa;
63 child_sa_t *child_sa;
64
65 ike_sa = charon->ike_sa_manager->checkout(charon->ike_sa_manager, this->id);
66 if (ike_sa)
67 {
68 /* get what we need from new SA */
69 me = ike_sa->get_my_host(ike_sa);
70 me = me->clone(me);
71 other = ike_sa->get_other_host(ike_sa);
72 other = other->clone(other);
73 my_id = ike_sa->get_my_id(ike_sa);
74 my_id = my_id->clone(my_id);
75 other_id = ike_sa->get_other_id(ike_sa);
76 other_id = other_id->clone(other_id);
77 xauth = ike_sa->get_other_eap_id(ike_sa);
78 xauth = xauth->clone(xauth);
79 cfg = ike_sa->get_peer_cfg(ike_sa);
80 cfg->get_ref(cfg);
81
82 charon->ike_sa_manager->checkin(charon->ike_sa_manager, ike_sa);
83
84 /* find old SA to adopt children from */
85 children = linked_list_create();
86 enumerator = charon->ike_sa_manager->create_id_enumerator(
87 charon->ike_sa_manager, my_id, xauth,
88 other->get_family(other));
89 while (enumerator->enumerate(enumerator, &id))
90 {
91 if (id->equals(id, this->id))
92 { /* not from self */
93 continue;
94 }
95 ike_sa = charon->ike_sa_manager->checkout(charon->ike_sa_manager, id);
96 if (ike_sa)
97 {
98 if ((ike_sa->get_state(ike_sa) == IKE_ESTABLISHED ||
99 ike_sa->get_state(ike_sa) == IKE_PASSIVE) &&
100 me->equals(me, ike_sa->get_my_host(ike_sa)) &&
101 other->equals(other, ike_sa->get_other_host(ike_sa)) &&
102 other_id->equals(other_id, ike_sa->get_other_id(ike_sa)) &&
103 cfg->equals(cfg, ike_sa->get_peer_cfg(ike_sa)))
104 {
105 childenum = ike_sa->create_child_sa_enumerator(ike_sa);
106 while (childenum->enumerate(childenum, &child_sa))
107 {
108 ike_sa->remove_child_sa(ike_sa, childenum);
109 children->insert_last(children, child_sa);
110 }
111 childenum->destroy(childenum);
112 if (children->get_count(children))
113 {
114 DBG1(DBG_IKE, "detected reauth of existing IKE_SA, "
115 "adopting %d children",
116 children->get_count(children));
117 }
118 ike_sa->set_state(ike_sa, IKE_DELETING);
119 charon->bus->ike_updown(charon->bus, ike_sa, FALSE);
120 charon->ike_sa_manager->checkin_and_destroy(
121 charon->ike_sa_manager, ike_sa);
122 }
123 else
124 {
125 charon->ike_sa_manager->checkin(
126 charon->ike_sa_manager, ike_sa);
127 }
128 if (children->get_count(children))
129 {
130 break;
131 }
132 }
133 }
134 enumerator->destroy(enumerator);
135
136 me->destroy(me);
137 other->destroy(other);
138 my_id->destroy(my_id);
139 other_id->destroy(other_id);
140 xauth->destroy(xauth);
141 cfg->destroy(cfg);
142
143 if (children->get_count(children))
144 {
145 /* adopt children by new SA */
146 ike_sa = charon->ike_sa_manager->checkout(charon->ike_sa_manager,
147 this->id);
148 if (ike_sa)
149 {
150 while (children->remove_last(children,
151 (void**)&child_sa) == SUCCESS)
152 {
153 ike_sa->add_child_sa(ike_sa, child_sa);
154 }
155 charon->ike_sa_manager->checkin(charon->ike_sa_manager, ike_sa);
156 }
157 }
158 children->destroy_offset(children, offsetof(child_sa_t, destroy));
159
160 if (array_count(this->tasks))
161 {
162 ike_sa = charon->ike_sa_manager->checkout(charon->ike_sa_manager,
163 this->id);
164 if (ike_sa)
165 {
166 task_t *task;
167
168 while (array_remove(this->tasks, ARRAY_HEAD, &task))
169 {
170 task->migrate(task, ike_sa);
171 ike_sa->queue_task(ike_sa, task);
172 }
173 if (ike_sa->initiate(ike_sa, NULL, 0, NULL, NULL) == DESTROY_ME)
174 {
175 charon->ike_sa_manager->checkin_and_destroy(
176 charon->ike_sa_manager, ike_sa);
177 }
178 else
179 {
180 charon->ike_sa_manager->checkin(charon->ike_sa_manager,
181 ike_sa);
182 }
183 }
184 }
185 }
186 return JOB_REQUEUE_NONE;
187 }
188
189 METHOD(job_t, get_priority, job_priority_t,
190 private_adopt_children_job_t *this)
191 {
192 return JOB_PRIO_HIGH;
193 }
194
195 METHOD(adopt_children_job_t, queue_task, void,
196 private_adopt_children_job_t *this, task_t *task)
197 {
198 array_insert_create(&this->tasks, ARRAY_TAIL, task);
199 }
200
201 /**
202 * See header
203 */
204 adopt_children_job_t *adopt_children_job_create(ike_sa_id_t *id)
205 {
206 private_adopt_children_job_t *this;
207
208 INIT(this,
209 .public = {
210 .job_interface = {
211 .execute = _execute,
212 .get_priority = _get_priority,
213 .destroy = _destroy,
214 },
215 .queue_task = _queue_task,
216 },
217 .id = id->clone(id),
218 );
219
220 return &this->public;
221 }