old child_sa gets deleted after rekeying
[strongswan.git] / src / charon / encoding / payloads / delete_payload.c
1 /**
2 * @file delete_payload.c
3 *
4 * @brief Implementation of delete_payload_t.
5 *
6 */
7
8 /*
9 * Copyright (C) 2005 Jan Hutter, Martin Willi
10 * Hochschule fuer Technik Rapperswil
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
21 */
22
23 #include <stddef.h>
24
25 #include "delete_payload.h"
26
27
28 typedef struct private_delete_payload_t private_delete_payload_t;
29
30 /**
31 * Private data of an delete_payload_t object.
32 *
33 */
34 struct private_delete_payload_t {
35 /**
36 * Public delete_payload_t interface.
37 */
38 delete_payload_t public;
39
40 /**
41 * Next payload type.
42 */
43 u_int8_t next_payload;
44
45 /**
46 * Critical flag.
47 */
48 bool critical;
49
50 /**
51 * Length of this payload.
52 */
53 u_int16_t payload_length;
54
55 /**
56 * Protocol ID.
57 */
58 u_int8_t protocol_id;
59
60 /**
61 * SPI Size.
62 */
63 u_int8_t spi_size;
64
65 /**
66 * Number of SPI's.
67 */
68 u_int16_t spi_count;
69
70 /**
71 * The contained SPI's.
72 */
73 chunk_t spis;
74
75 /**
76 * List containing u_int32_t spis
77 */
78 linked_list_t *spi_list;
79 };
80
81 /**
82 * Encoding rules to parse or generate a DELETE payload
83 *
84 * The defined offsets are the positions in a object of type
85 * private_delete_payload_t.
86 *
87 */
88 encoding_rule_t delete_payload_encodings[] = {
89 /* 1 Byte next payload type, stored in the field next_payload */
90 { U_INT_8, offsetof(private_delete_payload_t, next_payload) },
91 /* the critical bit */
92 { FLAG, offsetof(private_delete_payload_t, critical) },
93 /* 7 Bit reserved bits, nowhere stored */
94 { RESERVED_BIT, 0 },
95 { RESERVED_BIT, 0 },
96 { RESERVED_BIT, 0 },
97 { RESERVED_BIT, 0 },
98 { RESERVED_BIT, 0 },
99 { RESERVED_BIT, 0 },
100 { RESERVED_BIT, 0 },
101 /* Length of the whole payload*/
102 { PAYLOAD_LENGTH, offsetof(private_delete_payload_t, payload_length)},
103 { U_INT_8, offsetof(private_delete_payload_t, protocol_id) },
104 { U_INT_8, offsetof(private_delete_payload_t, spi_size) },
105 { U_INT_16, offsetof(private_delete_payload_t, spi_count) },
106 /* some delete data bytes, length is defined in PAYLOAD_LENGTH */
107 { SPIS, offsetof(private_delete_payload_t, spis) }
108 };
109
110 /*
111 1 2 3
112 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
113 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
114 ! Next Payload !C! RESERVED ! Payload Length !
115 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
116 ! Protocol ID ! SPI Size ! # of SPIs !
117 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
118 ! !
119 ~ Security Parameter Index(es) (SPI) ~
120 ! !
121 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
122 */
123
124 /**
125 * Implementation of payload_t.verify.
126 */
127 static status_t verify(private_delete_payload_t *this)
128 {
129 switch (this->protocol_id)
130 {
131 case PROTO_AH:
132 case PROTO_ESP:
133 if (this->spi_size != 4)
134 {
135 return FAILED;
136 }
137 break;
138 case PROTO_IKE:
139 case 0:
140 /* IKE deletion has no spi assigned! */
141 if (this->spi_size != 0)
142 {
143 return FAILED;
144 }
145 break;
146 default:
147 return FAILED;
148 }
149 if (this->spis.len != (this->spi_count * this->spi_size))
150 {
151 return FAILED;
152 }
153 return SUCCESS;
154 }
155
156 /**
157 * Implementation of delete_payload_t.get_encoding_rules.
158 */
159 static void get_encoding_rules(private_delete_payload_t *this, encoding_rule_t **rules, size_t *rule_count)
160 {
161 *rules = delete_payload_encodings;
162 *rule_count = sizeof(delete_payload_encodings) / sizeof(encoding_rule_t);
163 }
164
165 /**
166 * Implementation of payload_t.get_type.
167 */
168 static payload_type_t get_payload_type(private_delete_payload_t *this)
169 {
170 return DELETE;
171 }
172
173 /**
174 * Implementation of payload_t.get_next_type.
175 */
176 static payload_type_t get_next_type(private_delete_payload_t *this)
177 {
178 return (this->next_payload);
179 }
180
181 /**
182 * Implementation of payload_t.set_next_type.
183 */
184 static void set_next_type(private_delete_payload_t *this,payload_type_t type)
185 {
186 this->next_payload = type;
187 }
188
189 /**
190 * Implementation of payload_t.get_length.
191 */
192 static size_t get_length(private_delete_payload_t *this)
193 {
194 return this->payload_length;
195 }
196
197 /**
198 * Implementation of delete_payload_t.set_protocol_id.
199 */
200 static void set_protocol_id (private_delete_payload_t *this, protocol_id_t protocol_id)
201 {
202 this->protocol_id = protocol_id;
203 }
204
205 /**
206 * Implementation of delete_payload_t.get_protocol_id.
207 */
208 static protocol_id_t get_protocol_id (private_delete_payload_t *this)
209 {
210 return (this->protocol_id);
211 }
212
213 /**
214 * Implementation of delete_payload_t.set_spi_size.
215 */
216 static void set_spi_size (private_delete_payload_t *this, u_int8_t spi_size)
217 {
218 this->spi_size = spi_size;
219 }
220
221 /**
222 * Implementation of delete_payload_t.get_spi_size.
223 */
224 static u_int8_t get_spi_size (private_delete_payload_t *this)
225 {
226 return (this->spi_size);
227 }
228
229 /**
230 * Implementation of delete_payload_t.set_spi_count.
231 */
232 static void set_spi_count (private_delete_payload_t *this, u_int16_t spi_count)
233 {
234 this->spi_count = spi_count;
235 }
236
237 /**
238 * Implementation of delete_payload_t.get_spi_count.
239 */
240 static u_int16_t get_spi_count(private_delete_payload_t *this)
241 {
242 return (this->spi_count);
243 }
244
245 /**
246 * Implementation of delete_payload_t.set_spis.
247 */
248 static void set_spis(private_delete_payload_t *this, chunk_t spis)
249 {
250 if (this->spis.ptr != NULL)
251 {
252 chunk_free(&(this->spis));
253 }
254 this->spis.ptr = clalloc(spis.ptr,spis.len);
255 this->spis.len = spis.len;
256 this->payload_length = DELETE_PAYLOAD_HEADER_LENGTH + this->spis.len;
257 }
258
259 /**
260 * Implementation of delete_payload_t.get_spis.
261 */
262 static chunk_t get_spis (private_delete_payload_t *this)
263 {
264 return (this->spis);
265 }
266
267 /**
268 * Implementation of delete_payload_t.add_spi.
269 */
270 static void add_spi(private_delete_payload_t *this, u_int32_t spi)
271 {
272 /* only add SPIs if AH|ESP, ignore others */
273 if (this->protocol_id == PROTO_AH || this->protocol_id == PROTO_ESP)
274 {
275 this->spi_count += 1;
276 this->spis.len += this->spi_size;
277 this->spis.ptr = realloc(this->spis.ptr, this->spis.len);
278 *(u_int32_t*)(this->spis.ptr + (this->spis.len / this->spi_size - 1)) = spi;
279 }
280 }
281
282 /**
283 * Implementation of delete_payload_t.create_spi_iterator.
284 */
285 static iterator_t* create_spi_iterator(private_delete_payload_t *this)
286 {
287 int i;
288
289 if (this->spi_list == NULL)
290 {
291 this->spi_list = linked_list_create();
292 /* only parse SPIs if AH|ESP */
293 if (this->protocol_id == PROTO_AH || this->protocol_id == PROTO_ESP)
294 {
295 for (i = 0; i < this->spi_count; i++)
296 {
297 u_int32_t spi = *(u_int32_t*)(this->spis.ptr + i * this->spi_size);
298 this->spi_list->insert_last(this->spi_list, (void*)spi);
299 }
300 }
301 }
302 return this->spi_list->create_iterator(this->spi_list, TRUE);
303 }
304
305 /**
306 * Implementation of payload_t.destroy and delete_payload_t.destroy.
307 */
308 static void destroy(private_delete_payload_t *this)
309 {
310 if (this->spis.ptr != NULL)
311 {
312 chunk_free(&this->spis);
313 }
314 if (this->spi_list)
315 {
316 this->spi_list->destroy(this->spi_list);
317 }
318 free(this);
319 }
320
321 /*
322 * Described in header
323 */
324 delete_payload_t *delete_payload_create(protocol_id_t protocol_id)
325 {
326 private_delete_payload_t *this = malloc_thing(private_delete_payload_t);
327
328 /* interface functions */
329 this->public.payload_interface.verify = (status_t (*) (payload_t *))verify;
330 this->public.payload_interface.get_encoding_rules = (void (*) (payload_t *, encoding_rule_t **, size_t *) ) get_encoding_rules;
331 this->public.payload_interface.get_length = (size_t (*) (payload_t *)) get_length;
332 this->public.payload_interface.get_next_type = (payload_type_t (*) (payload_t *)) get_next_type;
333 this->public.payload_interface.set_next_type = (void (*) (payload_t *,payload_type_t)) set_next_type;
334 this->public.payload_interface.get_type = (payload_type_t (*) (payload_t *)) get_payload_type;
335 this->public.payload_interface.destroy = (void (*) (payload_t *))destroy;
336
337 /* public functions */
338 this->public.destroy = (void (*) (delete_payload_t *)) destroy;
339 this->public.set_protocol_id = (void (*) (delete_payload_t *,protocol_id_t)) set_protocol_id;
340 this->public.get_protocol_id = (protocol_id_t (*) (delete_payload_t *)) get_protocol_id;
341 this->public.set_spi_size = (void (*) (delete_payload_t *,u_int8_t)) set_spi_size;
342 this->public.get_spi_size = (u_int8_t (*) (delete_payload_t *)) get_spi_size;
343 this->public.set_spi_count = (void (*) (delete_payload_t *,u_int16_t)) set_spi_count;
344 this->public.get_spi_count = (u_int16_t (*) (delete_payload_t *)) get_spi_count;
345 this->public.set_spis = (void (*) (delete_payload_t *,chunk_t)) set_spis;
346 this->public.get_spis = (chunk_t (*) (delete_payload_t *)) get_spis;
347 this->public.add_spi = (void (*) (delete_payload_t *,u_int32_t))add_spi;
348 this->public.create_spi_iterator = (iterator_t* (*) (delete_payload_t *)) create_spi_iterator;
349
350 /* private variables */
351 this->critical = FALSE;
352 this->next_payload = NO_PAYLOAD;
353 this->payload_length = DELETE_PAYLOAD_HEADER_LENGTH;
354 this->protocol_id = protocol_id;
355 this->spi_size = protocol_id == PROTO_AH || protocol_id == PROTO_ESP ? 4 : 0;
356 this->spi_count = 0;
357 this->spis = CHUNK_INITIALIZER;
358 this->spi_list = NULL;
359
360 return (&this->public);
361 }