Use XAuth/EAP remote identity for uniqueness check
[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
21 typedef struct private_adopt_children_job_t private_adopt_children_job_t;
22
23 /**
24 * Private data of an adopt_children_job_t object.
25 */
26 struct private_adopt_children_job_t {
27
28 /**
29 * Public adopt_children_job_t interface.
30 */
31 adopt_children_job_t public;
32
33 /**
34 * IKE_SA id to adopt children from
35 */
36 ike_sa_id_t *id;
37 };
38
39 METHOD(job_t, destroy, void,
40 private_adopt_children_job_t *this)
41 {
42 this->id->destroy(this->id);
43 free(this);
44 }
45
46 METHOD(job_t, execute, void,
47 private_adopt_children_job_t *this)
48 {
49 identification_t *my_id, *other_id, *xauth;
50 host_t *me, *other;
51 peer_cfg_t *cfg;
52 linked_list_t *children;
53 enumerator_t *enumerator, *childenum;
54 ike_sa_id_t *id;
55 ike_sa_t *ike_sa;
56 child_sa_t *child_sa;
57
58 ike_sa = charon->ike_sa_manager->checkout(charon->ike_sa_manager, this->id);
59 if (ike_sa)
60 {
61 /* get what we need from new SA */
62 me = ike_sa->get_my_host(ike_sa);
63 me = me->clone(me);
64 other = ike_sa->get_other_host(ike_sa);
65 other = other->clone(other);
66 my_id = ike_sa->get_my_id(ike_sa);
67 my_id = my_id->clone(my_id);
68 other_id = ike_sa->get_other_id(ike_sa);
69 other_id = other_id->clone(other_id);
70 xauth = ike_sa->get_other_eap_id(ike_sa);
71 xauth = xauth->clone(xauth);
72 cfg = ike_sa->get_peer_cfg(ike_sa);
73 cfg->get_ref(cfg);
74
75 charon->ike_sa_manager->checkin(charon->ike_sa_manager, ike_sa);
76
77 /* find old SA to adopt children from */
78 children = linked_list_create();
79 enumerator = charon->ike_sa_manager->create_id_enumerator(
80 charon->ike_sa_manager, my_id, xauth,
81 other->get_family(other));
82 while (enumerator->enumerate(enumerator, &id))
83 {
84 if (id->equals(id, this->id))
85 { /* not from self */
86 continue;
87 }
88 ike_sa = charon->ike_sa_manager->checkout(charon->ike_sa_manager, id);
89 if (ike_sa)
90 {
91 if ((ike_sa->get_state(ike_sa) == IKE_ESTABLISHED ||
92 ike_sa->get_state(ike_sa) == IKE_PASSIVE) &&
93 me->equals(me, ike_sa->get_my_host(ike_sa)) &&
94 other->equals(other, ike_sa->get_other_host(ike_sa)) &&
95 other_id->equals(other_id, ike_sa->get_other_id(ike_sa)) &&
96 cfg->equals(cfg, ike_sa->get_peer_cfg(ike_sa)))
97 {
98 childenum = ike_sa->create_child_sa_enumerator(ike_sa);
99 while (childenum->enumerate(childenum, &child_sa))
100 {
101 ike_sa->remove_child_sa(ike_sa, childenum);
102 children->insert_last(children, child_sa);
103 }
104 childenum->destroy(childenum);
105 DBG1(DBG_IKE, "detected reauth of existing IKE_SA, "
106 "adopting %d children", children->get_count(children));
107 ike_sa->set_state(ike_sa, IKE_DELETING);
108 charon->bus->ike_updown(charon->bus, ike_sa, FALSE);
109 charon->ike_sa_manager->checkin_and_destroy(
110 charon->ike_sa_manager, ike_sa);
111 }
112 else
113 {
114 charon->ike_sa_manager->checkin(
115 charon->ike_sa_manager, ike_sa);
116 }
117 if (children->get_count(children))
118 {
119 break;
120 }
121 }
122 }
123 enumerator->destroy(enumerator);
124
125 me->destroy(me);
126 other->destroy(other);
127 my_id->destroy(my_id);
128 other_id->destroy(other_id);
129 xauth->destroy(xauth);
130 cfg->destroy(cfg);
131
132 if (children->get_count(children))
133 {
134 /* adopt children by new SA */
135 ike_sa = charon->ike_sa_manager->checkout(charon->ike_sa_manager,
136 this->id);
137 if (ike_sa)
138 {
139 while (children->remove_last(children,
140 (void**)&child_sa) == SUCCESS)
141 {
142 ike_sa->add_child_sa(ike_sa, child_sa);
143 }
144 charon->ike_sa_manager->checkin(charon->ike_sa_manager, ike_sa);
145 }
146 }
147 children->destroy_offset(children, offsetof(child_sa_t, destroy));
148 }
149 destroy(this);
150 }
151
152 METHOD(job_t, get_priority, job_priority_t,
153 private_adopt_children_job_t *this)
154 {
155 return JOB_PRIO_HIGH;
156 }
157
158 /**
159 * See header
160 */
161 adopt_children_job_t *adopt_children_job_create(ike_sa_id_t *id)
162 {
163 private_adopt_children_job_t *this;
164
165 INIT(this,
166 .public = {
167 .job_interface = {
168 .execute = _execute,
169 .get_priority = _get_priority,
170 .destroy = _destroy,
171 },
172 },
173 .id = id->clone(id),
174 );
175
176 return &this->public;
177 }