Added member fields for reserved bits and bytes in all payloads
[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 encoding_rule_t delete_payload_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 { SPIS, 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, void,
149 private_delete_payload_t *this, encoding_rule_t **rules, size_t *rule_count)
150 {
151 *rules = delete_payload_encodings;
152 *rule_count = countof(delete_payload_encodings);
153 }
154
155 METHOD(payload_t, get_payload_type, payload_type_t,
156 private_delete_payload_t *this)
157 {
158 return DELETE;
159 }
160
161 METHOD(payload_t, get_next_type, payload_type_t,
162 private_delete_payload_t *this)
163 {
164 return this->next_payload;
165 }
166
167 METHOD(payload_t, set_next_type, void,
168 private_delete_payload_t *this,payload_type_t type)
169 {
170 this->next_payload = type;
171 }
172
173 METHOD(payload_t, get_length, size_t,
174 private_delete_payload_t *this)
175 {
176 return this->payload_length;
177 }
178
179 METHOD(delete_payload_t, get_protocol_id, protocol_id_t,
180 private_delete_payload_t *this)
181 {
182 return this->protocol_id;
183 }
184
185 METHOD(delete_payload_t, add_spi, void,
186 private_delete_payload_t *this, u_int32_t spi)
187 {
188 switch (this->protocol_id)
189 {
190 case PROTO_AH:
191 case PROTO_ESP:
192 this->spi_count++;
193 this->payload_length += sizeof(spi);
194 this->spis = chunk_cat("mc", this->spis, chunk_from_thing(spi));
195 break;
196 default:
197 break;
198 }
199 }
200
201 /**
202 * SPI enumerator implementation
203 */
204 typedef struct {
205 /** implements enumerator_t */
206 enumerator_t public;
207 /** remaining SPIs */
208 chunk_t spis;
209 } spi_enumerator_t;
210
211 METHOD(enumerator_t, spis_enumerate, bool,
212 spi_enumerator_t *this, u_int32_t *spi)
213 {
214 if (this->spis.len >= sizeof(*spi))
215 {
216 memcpy(spi, this->spis.ptr, sizeof(*spi));
217 this->spis = chunk_skip(this->spis, sizeof(*spi));
218 return TRUE;
219 }
220 return FALSE;
221 }
222
223 METHOD(delete_payload_t, create_spi_enumerator, enumerator_t*,
224 private_delete_payload_t *this)
225 {
226 spi_enumerator_t *e;
227
228 if (this->spi_size != sizeof(u_int32_t))
229 {
230 return enumerator_create_empty();
231 }
232 INIT(e,
233 .public = {
234 .enumerate = (void*)_spis_enumerate,
235 .destroy = (void*)free,
236 },
237 .spis = this->spis,
238 );
239 return &e->public;
240 }
241
242 METHOD2(payload_t, delete_payload_t, destroy, void,
243 private_delete_payload_t *this)
244 {
245 free(this->spis.ptr);
246 free(this);
247 }
248
249 /*
250 * Described in header
251 */
252 delete_payload_t *delete_payload_create(protocol_id_t protocol_id)
253 {
254 private_delete_payload_t *this;
255
256 INIT(this,
257 .public = {
258 .payload_interface = {
259 .verify = _verify,
260 .get_encoding_rules = _get_encoding_rules,
261 .get_length = _get_length,
262 .get_next_type = _get_next_type,
263 .set_next_type = _set_next_type,
264 .get_type = _get_payload_type,
265 .destroy = _destroy,
266 },
267 .get_protocol_id = _get_protocol_id,
268 .add_spi = _add_spi,
269 .create_spi_enumerator = _create_spi_enumerator,
270 .destroy = _destroy,
271 },
272 .next_payload = NO_PAYLOAD,
273 .payload_length = DELETE_PAYLOAD_HEADER_LENGTH,
274 .protocol_id = protocol_id,
275 .spi_size = protocol_id == PROTO_AH || protocol_id == PROTO_ESP ? 4 : 0,
276 );
277 return &this->public;
278 }