425bde0c3acd7964f851799367b3c8b988c2bac6
[strongswan.git] / src / libcharon / encoding / payloads / delete_payload.c
1 /*
2 * Copyright (C) 2005-2010 Martin Willi
3 * Copyright (C) 2010 revosec AG
4 * Copyright (C) 2005 Jan Hutter
5 * Hochschule fuer Technik Rapperswil
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 * for more details.
16 */
17
18 #include <stddef.h>
19
20 #include "delete_payload.h"
21
22
23 typedef struct private_delete_payload_t private_delete_payload_t;
24
25 /**
26 * Private data of an delete_payload_t object.
27 *
28 */
29 struct private_delete_payload_t {
30 /**
31 * Public delete_payload_t interface.
32 */
33 delete_payload_t public;
34
35 /**
36 * Next payload type.
37 */
38 u_int8_t next_payload;
39
40 /**
41 * Critical flag.
42 */
43 bool critical;
44
45 /**
46 * reserved bits
47 */
48 bool reserved[7];
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 /**
77 * Encoding rules to parse or generate a DELETE payload
78 *
79 * The defined offsets are the positions in a object of type
80 * private_delete_payload_t.
81 */
82 static encoding_rule_t encodings[] = {
83 /* 1 Byte next payload type, stored in the field next_payload */
84 { U_INT_8, offsetof(private_delete_payload_t, next_payload) },
85 /* the critical bit */
86 { FLAG, offsetof(private_delete_payload_t, critical) },
87 /* 7 Bit reserved bits */
88 { RESERVED_BIT, offsetof(private_delete_payload_t, reserved[0]) },
89 { RESERVED_BIT, offsetof(private_delete_payload_t, reserved[1]) },
90 { RESERVED_BIT, offsetof(private_delete_payload_t, reserved[2]) },
91 { RESERVED_BIT, offsetof(private_delete_payload_t, reserved[3]) },
92 { RESERVED_BIT, offsetof(private_delete_payload_t, reserved[4]) },
93 { RESERVED_BIT, offsetof(private_delete_payload_t, reserved[5]) },
94 { RESERVED_BIT, offsetof(private_delete_payload_t, reserved[6]) },
95 /* Length of the whole payload*/
96 { PAYLOAD_LENGTH, offsetof(private_delete_payload_t, payload_length) },
97 { U_INT_8, offsetof(private_delete_payload_t, protocol_id) },
98 { U_INT_8, offsetof(private_delete_payload_t, spi_size) },
99 { U_INT_16, offsetof(private_delete_payload_t, spi_count) },
100 /* some delete data bytes, length is defined in PAYLOAD_LENGTH */
101 { CHUNK_DATA, offsetof(private_delete_payload_t, spis) }
102 };
103
104 /*
105 1 2 3
106 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
107 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
108 ! Next Payload !C! RESERVED ! Payload Length !
109 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
110 ! Protocol ID ! SPI Size ! # of SPIs !
111 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
112 ! !
113 ~ Security Parameter Index(es) (SPI) ~
114 ! !
115 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
116 */
117
118 METHOD(payload_t, verify, status_t,
119 private_delete_payload_t *this)
120 {
121 switch (this->protocol_id)
122 {
123 case PROTO_AH:
124 case PROTO_ESP:
125 if (this->spi_size != 4)
126 {
127 return FAILED;
128 }
129 break;
130 case PROTO_IKE:
131 case 0:
132 /* IKE deletion has no spi assigned! */
133 if (this->spi_size != 0)
134 {
135 return FAILED;
136 }
137 break;
138 default:
139 return FAILED;
140 }
141 if (this->spis.len != (this->spi_count * this->spi_size))
142 {
143 return FAILED;
144 }
145 return SUCCESS;
146 }
147
148 METHOD(payload_t, get_encoding_rules, int,
149 private_delete_payload_t *this, encoding_rule_t **rules)
150 {
151 *rules = encodings;
152 return countof(encodings);
153 }
154
155 METHOD(payload_t, get_header_length, int,
156 private_delete_payload_t *this)
157 {
158 return 8;
159 }
160
161 METHOD(payload_t, get_payload_type, payload_type_t,
162 private_delete_payload_t *this)
163 {
164 return DELETE;
165 }
166
167 METHOD(payload_t, get_next_type, payload_type_t,
168 private_delete_payload_t *this)
169 {
170 return this->next_payload;
171 }
172
173 METHOD(payload_t, set_next_type, void,
174 private_delete_payload_t *this,payload_type_t type)
175 {
176 this->next_payload = type;
177 }
178
179 METHOD(payload_t, get_length, size_t,
180 private_delete_payload_t *this)
181 {
182 return this->payload_length;
183 }
184
185 METHOD(delete_payload_t, get_protocol_id, protocol_id_t,
186 private_delete_payload_t *this)
187 {
188 return this->protocol_id;
189 }
190
191 METHOD(delete_payload_t, add_spi, void,
192 private_delete_payload_t *this, u_int32_t spi)
193 {
194 switch (this->protocol_id)
195 {
196 case PROTO_AH:
197 case PROTO_ESP:
198 this->spi_count++;
199 this->payload_length += sizeof(spi);
200 this->spis = chunk_cat("mc", this->spis, chunk_from_thing(spi));
201 break;
202 default:
203 break;
204 }
205 }
206
207 /**
208 * SPI enumerator implementation
209 */
210 typedef struct {
211 /** implements enumerator_t */
212 enumerator_t public;
213 /** remaining SPIs */
214 chunk_t spis;
215 } spi_enumerator_t;
216
217 METHOD(enumerator_t, spis_enumerate, bool,
218 spi_enumerator_t *this, u_int32_t *spi)
219 {
220 if (this->spis.len >= sizeof(*spi))
221 {
222 memcpy(spi, this->spis.ptr, sizeof(*spi));
223 this->spis = chunk_skip(this->spis, sizeof(*spi));
224 return TRUE;
225 }
226 return FALSE;
227 }
228
229 METHOD(delete_payload_t, create_spi_enumerator, enumerator_t*,
230 private_delete_payload_t *this)
231 {
232 spi_enumerator_t *e;
233
234 if (this->spi_size != sizeof(u_int32_t))
235 {
236 return enumerator_create_empty();
237 }
238 INIT(e,
239 .public = {
240 .enumerate = (void*)_spis_enumerate,
241 .destroy = (void*)free,
242 },
243 .spis = this->spis,
244 );
245 return &e->public;
246 }
247
248 METHOD2(payload_t, delete_payload_t, destroy, void,
249 private_delete_payload_t *this)
250 {
251 free(this->spis.ptr);
252 free(this);
253 }
254
255 /*
256 * Described in header
257 */
258 delete_payload_t *delete_payload_create(protocol_id_t protocol_id)
259 {
260 private_delete_payload_t *this;
261
262 INIT(this,
263 .public = {
264 .payload_interface = {
265 .verify = _verify,
266 .get_encoding_rules = _get_encoding_rules,
267 .get_header_length = _get_header_length,
268 .get_length = _get_length,
269 .get_next_type = _get_next_type,
270 .set_next_type = _set_next_type,
271 .get_type = _get_payload_type,
272 .destroy = _destroy,
273 },
274 .get_protocol_id = _get_protocol_id,
275 .add_spi = _add_spi,
276 .create_spi_enumerator = _create_spi_enumerator,
277 .destroy = _destroy,
278 },
279 .next_payload = NO_PAYLOAD,
280 .payload_length = get_header_length(this),
281 .protocol_id = protocol_id,
282 .spi_size = protocol_id == PROTO_AH || protocol_id == PROTO_ESP ? 4 : 0,
283 );
284 return &this->public;
285 }