ikev1: Make sure SPIs in an IKEv1 DELETE payload match the current SA
[strongswan.git] / src / libcharon / sa / ikev1 / tasks / isakmp_delete.c
1 /*
2 * Copyright (C) 2015 Tobias Brunner
3 * Hochschule fuer Technik Rapperswil
4 *
5 * Copyright (C) 2011 Martin Willi
6 * Copyright (C) 2011 revosec AG
7 *
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>.
12 *
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
16 * for more details.
17 */
18
19 #include "isakmp_delete.h"
20
21 #include <daemon.h>
22 #include <encoding/payloads/delete_payload.h>
23
24 typedef struct private_isakmp_delete_t private_isakmp_delete_t;
25
26 /**
27 * Private members of a isakmp_delete_t task.
28 */
29 struct private_isakmp_delete_t {
30
31 /**
32 * Public methods and task_t interface.
33 */
34 isakmp_delete_t public;
35
36 /**
37 * Assigned IKE_SA.
38 */
39 ike_sa_t *ike_sa;
40 };
41
42 METHOD(task_t, build_i, status_t,
43 private_isakmp_delete_t *this, message_t *message)
44 {
45 delete_payload_t *delete_payload;
46 ike_sa_id_t *id;
47
48 DBG0(DBG_IKE, "deleting IKE_SA %s[%d] between %H[%Y]...%H[%Y]",
49 this->ike_sa->get_name(this->ike_sa),
50 this->ike_sa->get_unique_id(this->ike_sa),
51 this->ike_sa->get_my_host(this->ike_sa),
52 this->ike_sa->get_my_id(this->ike_sa),
53 this->ike_sa->get_other_host(this->ike_sa),
54 this->ike_sa->get_other_id(this->ike_sa));
55
56 delete_payload = delete_payload_create(PLV1_DELETE, PROTO_IKE);
57 id = this->ike_sa->get_id(this->ike_sa);
58 delete_payload->set_ike_spi(delete_payload, id->get_initiator_spi(id),
59 id->get_responder_spi(id));
60 message->add_payload(message, (payload_t*)delete_payload);
61
62 DBG1(DBG_IKE, "sending DELETE for IKE_SA %s[%d]",
63 this->ike_sa->get_name(this->ike_sa),
64 this->ike_sa->get_unique_id(this->ike_sa));
65
66 this->ike_sa->set_state(this->ike_sa, IKE_DELETING);
67 charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE);
68 return SUCCESS;
69 }
70
71 METHOD(task_t, process_i, status_t,
72 private_isakmp_delete_t *this, message_t *message)
73 {
74 return FAILED;
75 }
76
77 METHOD(task_t, process_r, status_t,
78 private_isakmp_delete_t *this, message_t *message)
79 {
80 enumerator_t *payloads;
81 payload_t *payload;
82 delete_payload_t *delete_payload;
83 ike_sa_id_t *id;
84 u_int64_t spi_i, spi_r;
85 bool found = FALSE;
86
87 /* some peers send DELETE payloads for other IKE_SAs, e.g. those for expired
88 * ones after a rekeyeing, make sure the SPIs match */
89 id = this->ike_sa->get_id(this->ike_sa);
90 payloads = message->create_payload_enumerator(message);
91 while (payloads->enumerate(payloads, &payload))
92 {
93 if (payload->get_type(payload) == PLV1_DELETE)
94 {
95 delete_payload = (delete_payload_t*)payload;
96 if (!delete_payload->get_ike_spi(delete_payload, &spi_i, &spi_r))
97 {
98 continue;
99 }
100 if (id->get_initiator_spi(id) == spi_i &&
101 id->get_responder_spi(id) == spi_r)
102 {
103 found = TRUE;
104 break;
105 }
106 }
107 }
108 payloads->destroy(payloads);
109
110 if (!found)
111 {
112 DBG1(DBG_IKE, "received DELETE for different IKE_SA, ignored");
113 return SUCCESS;
114 }
115
116 DBG1(DBG_IKE, "received DELETE for IKE_SA %s[%d]",
117 this->ike_sa->get_name(this->ike_sa),
118 this->ike_sa->get_unique_id(this->ike_sa));
119 DBG0(DBG_IKE, "deleting IKE_SA %s[%d] between %H[%Y]...%H[%Y]",
120 this->ike_sa->get_name(this->ike_sa),
121 this->ike_sa->get_unique_id(this->ike_sa),
122 this->ike_sa->get_my_host(this->ike_sa),
123 this->ike_sa->get_my_id(this->ike_sa),
124 this->ike_sa->get_other_host(this->ike_sa),
125 this->ike_sa->get_other_id(this->ike_sa));
126
127 if (this->ike_sa->get_state(this->ike_sa) == IKE_ESTABLISHED)
128 {
129 this->ike_sa->set_state(this->ike_sa, IKE_DELETING);
130 this->ike_sa->reestablish(this->ike_sa);
131 }
132 this->ike_sa->set_state(this->ike_sa, IKE_DELETING);
133 charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE);
134 return DESTROY_ME;
135 }
136
137 METHOD(task_t, build_r, status_t,
138 private_isakmp_delete_t *this, message_t *message)
139 {
140 return FAILED;
141 }
142
143 METHOD(task_t, get_type, task_type_t,
144 private_isakmp_delete_t *this)
145 {
146 return TASK_ISAKMP_DELETE;
147 }
148
149 METHOD(task_t, migrate, void,
150 private_isakmp_delete_t *this, ike_sa_t *ike_sa)
151 {
152 this->ike_sa = ike_sa;
153 }
154
155 METHOD(task_t, destroy, void,
156 private_isakmp_delete_t *this)
157 {
158 free(this);
159 }
160
161 /*
162 * Described in header.
163 */
164 isakmp_delete_t *isakmp_delete_create(ike_sa_t *ike_sa, bool initiator)
165 {
166 private_isakmp_delete_t *this;
167
168 INIT(this,
169 .public = {
170 .task = {
171 .get_type = _get_type,
172 .migrate = _migrate,
173 .destroy = _destroy,
174 },
175 },
176 .ike_sa = ike_sa,
177 );
178
179 if (initiator)
180 {
181 this->public.task.build = _build_i;
182 this->public.task.process = _process_i;
183 }
184 else
185 {
186 this->public.task.build = _build_r;
187 this->public.task.process = _process_r;
188 }
189
190 return &this->public;
191 }