ikev1: Don't log a reauthentication detection message if no children adopted
[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, job_requeue_t,
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 if (children->get_count(children))
106 {
107 DBG1(DBG_IKE, "detected reauth of existing IKE_SA, "
108 "adopting %d children",
109 children->get_count(children));
110 }
111 ike_sa->set_state(ike_sa, IKE_DELETING);
112 charon->bus->ike_updown(charon->bus, ike_sa, FALSE);
113 charon->ike_sa_manager->checkin_and_destroy(
114 charon->ike_sa_manager, ike_sa);
115 }
116 else
117 {
118 charon->ike_sa_manager->checkin(
119 charon->ike_sa_manager, ike_sa);
120 }
121 if (children->get_count(children))
122 {
123 break;
124 }
125 }
126 }
127 enumerator->destroy(enumerator);
128
129 me->destroy(me);
130 other->destroy(other);
131 my_id->destroy(my_id);
132 other_id->destroy(other_id);
133 xauth->destroy(xauth);
134 cfg->destroy(cfg);
135
136 if (children->get_count(children))
137 {
138 /* adopt children by new SA */
139 ike_sa = charon->ike_sa_manager->checkout(charon->ike_sa_manager,
140 this->id);
141 if (ike_sa)
142 {
143 while (children->remove_last(children,
144 (void**)&child_sa) == SUCCESS)
145 {
146 ike_sa->add_child_sa(ike_sa, child_sa);
147 }
148 charon->ike_sa_manager->checkin(charon->ike_sa_manager, ike_sa);
149 }
150 }
151 children->destroy_offset(children, offsetof(child_sa_t, destroy));
152 }
153 return JOB_REQUEUE_NONE;
154 }
155
156 METHOD(job_t, get_priority, job_priority_t,
157 private_adopt_children_job_t *this)
158 {
159 return JOB_PRIO_HIGH;
160 }
161
162 /**
163 * See header
164 */
165 adopt_children_job_t *adopt_children_job_create(ike_sa_id_t *id)
166 {
167 private_adopt_children_job_t *this;
168
169 INIT(this,
170 .public = {
171 .job_interface = {
172 .execute = _execute,
173 .get_priority = _get_priority,
174 .destroy = _destroy,
175 },
176 },
177 .id = id->clone(id),
178 );
179
180 return &this->public;
181 }