61fa2ef1f31131c59db91f7eddfa90170c66f0ae
[strongswan.git] / src / libcharon / sa / tasks / quick_delete.c
1 /*
2 * Copyright (C) 2011 Martin Willi
3 * Copyright (C) 2011 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 "quick_delete.h"
17
18 #include <daemon.h>
19 #include <encoding/payloads/delete_payload.h>
20
21 typedef struct private_quick_delete_t private_quick_delete_t;
22
23 /**
24 * Private members of a quick_delete_t task.
25 */
26 struct private_quick_delete_t {
27
28 /**
29 * Public methods and task_t interface.
30 */
31 quick_delete_t public;
32
33 /**
34 * Assigned IKE_SA.
35 */
36 ike_sa_t *ike_sa;
37
38 /**
39 * Are we the initiator?
40 */
41 bool initiator;
42
43 /**
44 * Protocol of CHILD_SA to delete
45 */
46 protocol_id_t protocol;
47
48 /**
49 * Inbound SPI of CHILD_SA to delete
50 */
51 u_int32_t spi;
52 };
53
54 /**
55 * Delete the specified CHILD_SA, if found
56 */
57 static bool delete_child(private_quick_delete_t *this,
58 protocol_id_t protocol, u_int32_t spi)
59 {
60 u_int64_t bytes_in, bytes_out;
61 child_sa_t *child_sa;
62
63 child_sa = this->ike_sa->get_child_sa(this->ike_sa, protocol, spi, TRUE);
64 if (!child_sa)
65 { /* fallback and check for outbound SA */
66 child_sa = this->ike_sa->get_child_sa(this->ike_sa, protocol, spi, FALSE);
67 if (!child_sa)
68 {
69 return FALSE;
70 }
71 this->spi = spi = child_sa->get_spi(child_sa, TRUE);
72 }
73
74 child_sa->set_state(child_sa, CHILD_DELETING);
75
76 child_sa->get_usestats(child_sa, TRUE, NULL, &bytes_in);
77 child_sa->get_usestats(child_sa, FALSE, NULL, &bytes_out);
78
79 DBG0(DBG_IKE, "closing CHILD_SA %s{%d} "
80 "with SPIs %.8x_i (%llu bytes) %.8x_o (%llu bytes) and TS %#R=== %#R",
81 child_sa->get_name(child_sa), child_sa->get_reqid(child_sa),
82 ntohl(child_sa->get_spi(child_sa, TRUE)), bytes_in,
83 ntohl(child_sa->get_spi(child_sa, FALSE)), bytes_out,
84 child_sa->get_traffic_selectors(child_sa, TRUE),
85 child_sa->get_traffic_selectors(child_sa, FALSE));
86
87 charon->bus->child_updown(charon->bus, child_sa, FALSE);
88
89 this->ike_sa->destroy_child_sa(this->ike_sa, protocol, spi);
90
91 /* TODO-IKEv1: handle close action? */
92
93 return TRUE;
94 }
95
96 METHOD(task_t, build_i, status_t,
97 private_quick_delete_t *this, message_t *message)
98 {
99 if (delete_child(this, this->protocol, this->spi))
100 {
101 delete_payload_t *delete_payload;
102
103 DBG1(DBG_IKE, "sending DELETE for %N CHILD_SA with SPI %.8x",
104 protocol_id_names, this->protocol, ntohl(this->spi));
105
106 delete_payload = delete_payload_create(DELETE_V1, PROTO_ESP);
107 delete_payload->add_spi(delete_payload, this->spi);
108 message->add_payload(message, &delete_payload->payload_interface);
109
110 return SUCCESS;
111 }
112 return FAILED;
113 }
114
115 METHOD(task_t, process_i, status_t,
116 private_quick_delete_t *this, message_t *message)
117 {
118 return FAILED;
119 }
120
121 METHOD(task_t, process_r, status_t,
122 private_quick_delete_t *this, message_t *message)
123 {
124 enumerator_t *payloads, *spis;
125 payload_t *payload;
126 delete_payload_t *delete_payload;
127 protocol_id_t protocol;
128 u_int32_t spi;
129
130 payloads = message->create_payload_enumerator(message);
131 while (payloads->enumerate(payloads, &payload))
132 {
133 if (payload->get_type(payload) == DELETE_V1)
134 {
135 delete_payload = (delete_payload_t*)payload;
136 protocol = delete_payload->get_protocol_id(delete_payload);
137 if (protocol != PROTO_ESP && protocol != PROTO_AH)
138 {
139 continue;
140 }
141 spis = delete_payload->create_spi_enumerator(delete_payload);
142 while (spis->enumerate(spis, &spi))
143 {
144 DBG1(DBG_IKE, "received DELETE for %N CHILD_SA with SPI %.8x",
145 protocol_id_names, protocol, ntohl(spi));
146 if (!delete_child(this, protocol, spi))
147 {
148 DBG1(DBG_IKE, "CHILD_SA not found, ignored");
149 continue;
150 }
151 }
152 spis->destroy(spis);
153 }
154 }
155 payloads->destroy(payloads);
156
157 return SUCCESS;
158 }
159
160 METHOD(task_t, build_r, status_t,
161 private_quick_delete_t *this, message_t *message)
162 {
163 return FAILED;
164 }
165
166 METHOD(task_t, get_type, task_type_t,
167 private_quick_delete_t *this)
168 {
169 return TASK_QUICK_DELETE;
170 }
171
172 METHOD(task_t, migrate, void,
173 private_quick_delete_t *this, ike_sa_t *ike_sa)
174 {
175 this->ike_sa = ike_sa;
176 }
177
178 METHOD(task_t, destroy, void,
179 private_quick_delete_t *this)
180 {
181 free(this);
182 }
183
184 /*
185 * Described in header.
186 */
187 quick_delete_t *quick_delete_create(ike_sa_t *ike_sa, protocol_id_t protocol,
188 u_int32_t spi)
189 {
190 private_quick_delete_t *this;
191
192 INIT(this,
193 .public = {
194 .task = {
195 .get_type = _get_type,
196 .migrate = _migrate,
197 .destroy = _destroy,
198 },
199 },
200 .ike_sa = ike_sa,
201 .protocol = protocol,
202 .spi = spi,
203 );
204
205 if (protocol != PROTO_NONE)
206 {
207 this->public.task.build = _build_i;
208 this->public.task.process = _process_i;
209 }
210 else
211 {
212 this->public.task.build = _build_r;
213 this->public.task.process = _process_r;
214 }
215 return &this->public;
216 }