Added member fields for reserved bits and bytes in all payloads
[strongswan.git] / src / libcharon / encoding / payloads / sa_payload.c
1 /*
2 * Copyright (C) 2005-2010 Martin Willi
3 * Copyright (C) 2005 Jan Hutter
4 * Hochschule fuer Technik Rapperswil
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 */
16
17 #include <stddef.h>
18
19 #include "sa_payload.h"
20
21 #include <encoding/payloads/encodings.h>
22 #include <utils/linked_list.h>
23 #include <daemon.h>
24
25
26 typedef struct private_sa_payload_t private_sa_payload_t;
27
28 /**
29 * Private data of an sa_payload_t object.
30 */
31 struct private_sa_payload_t {
32
33 /**
34 * Public sa_payload_t interface.
35 */
36 sa_payload_t public;
37
38 /**
39 * Next payload type.
40 */
41 u_int8_t next_payload;
42
43 /**
44 * Critical flag.
45 */
46 bool critical;
47
48 /**
49 * Reserved bits
50 */
51 bool reserved[7];
52
53 /**
54 * Length of this payload.
55 */
56 u_int16_t payload_length;
57
58 /**
59 * Proposals in this payload are stored in a linked_list_t.
60 */
61 linked_list_t * proposals;
62 };
63
64 /**
65 * Encoding rules to parse or generate a IKEv2-SA Payload
66 *
67 * The defined offsets are the positions in a object of type
68 * private_sa_payload_t.
69 */
70 encoding_rule_t sa_payload_encodings[] = {
71 /* 1 Byte next payload type, stored in the field next_payload */
72 { U_INT_8, offsetof(private_sa_payload_t, next_payload) },
73 /* the critical bit */
74 { FLAG, offsetof(private_sa_payload_t, critical) },
75 /* 7 Bit reserved bits, nowhere stored */
76 { RESERVED_BIT, offsetof(private_sa_payload_t, reserved[0]) },
77 { RESERVED_BIT, offsetof(private_sa_payload_t, reserved[1]) },
78 { RESERVED_BIT, offsetof(private_sa_payload_t, reserved[2]) },
79 { RESERVED_BIT, offsetof(private_sa_payload_t, reserved[3]) },
80 { RESERVED_BIT, offsetof(private_sa_payload_t, reserved[4]) },
81 { RESERVED_BIT, offsetof(private_sa_payload_t, reserved[5]) },
82 { RESERVED_BIT, offsetof(private_sa_payload_t, reserved[6]) },
83 /* Length of the whole SA payload*/
84 { PAYLOAD_LENGTH, offsetof(private_sa_payload_t, payload_length) },
85 /* Proposals are stored in a proposal substructure,
86 offset points to a linked_list_t pointer */
87 { PROPOSALS, offsetof(private_sa_payload_t, proposals) },
88 };
89
90 /*
91 1 2 3
92 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
93 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
94 ! Next Payload !C! RESERVED ! Payload Length !
95 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
96 ! !
97 ~ <Proposals> ~
98 ! !
99 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
100 */
101
102 METHOD(payload_t, verify, status_t,
103 private_sa_payload_t *this)
104 {
105 int expected_number = 1, current_number;
106 status_t status = SUCCESS;
107 enumerator_t *enumerator;
108 proposal_substructure_t *substruct;
109 bool first = TRUE;
110
111 /* check proposal numbering */
112 enumerator = this->proposals->create_enumerator(this->proposals);
113 while (enumerator->enumerate(enumerator, (void**)&substruct))
114 {
115 current_number = substruct->get_proposal_number(substruct);
116 if (current_number < expected_number)
117 {
118 if (current_number != expected_number + 1)
119 {
120 DBG1(DBG_ENC, "proposal number is %d, expected %d or %d",
121 current_number, expected_number, expected_number + 1);
122 status = FAILED;
123 break;
124 }
125 }
126 else if (current_number < expected_number)
127 {
128 DBG1(DBG_ENC, "proposal number smaller than previous");
129 status = FAILED;
130 break;
131 }
132
133 status = substruct->payload_interface.verify(&substruct->payload_interface);
134 if (status != SUCCESS)
135 {
136 DBG1(DBG_ENC, "PROPOSAL_SUBSTRUCTURE verification failed");
137 break;
138 }
139 first = FALSE;
140 expected_number = current_number;
141 }
142 enumerator->destroy(enumerator);
143 return status;
144 }
145
146 METHOD(payload_t, get_encoding_rules, void,
147 private_sa_payload_t *this, encoding_rule_t **rules, size_t *rule_count)
148 {
149 *rules = sa_payload_encodings;
150 *rule_count = countof(sa_payload_encodings);
151 }
152
153 METHOD(payload_t, get_type, payload_type_t,
154 private_sa_payload_t *this)
155 {
156 return SECURITY_ASSOCIATION;
157 }
158
159 METHOD(payload_t, get_next_type, payload_type_t,
160 private_sa_payload_t *this)
161 {
162 return this->next_payload;
163 }
164
165 METHOD(payload_t, set_next_type, void,
166 private_sa_payload_t *this,payload_type_t type)
167 {
168 this->next_payload = type;
169 }
170
171 /**
172 * recompute length of the payload.
173 */
174 static void compute_length(private_sa_payload_t *this)
175 {
176 enumerator_t *enumerator;
177 payload_t *current;
178 size_t length = SA_PAYLOAD_HEADER_LENGTH;
179
180 enumerator = this->proposals->create_enumerator(this->proposals);
181 while (enumerator->enumerate(enumerator, (void **)&current))
182 {
183 length += current->get_length(current);
184 }
185 enumerator->destroy(enumerator);
186
187 this->payload_length = length;
188 }
189
190 METHOD(payload_t, get_length, size_t,
191 private_sa_payload_t *this)
192 {
193 return this->payload_length;
194 }
195
196 METHOD(sa_payload_t, add_proposal, void,
197 private_sa_payload_t *this, proposal_t *proposal)
198 {
199 proposal_substructure_t *substruct, *last;
200 u_int count;
201
202 count = this->proposals->get_count(this->proposals);
203 substruct = proposal_substructure_create_from_proposal(proposal);
204 if (count > 0)
205 {
206 this->proposals->get_last(this->proposals, (void**)&last);
207 /* last transform is now not anymore last one */
208 last->set_is_last_proposal(last, FALSE);
209 }
210 substruct->set_is_last_proposal(substruct, TRUE);
211 if (proposal->get_number(proposal))
212 { /* use the selected proposals number, if any */
213 substruct->set_proposal_number(substruct, proposal->get_number(proposal));
214 }
215 else
216 {
217 substruct->set_proposal_number(substruct, count + 1);
218 }
219 this->proposals->insert_last(this->proposals, substruct);
220 compute_length(this);
221 }
222
223 METHOD(sa_payload_t, get_proposals, linked_list_t*,
224 private_sa_payload_t *this)
225 {
226 int struct_number = 0;
227 int ignore_struct_number = 0;
228 enumerator_t *enumerator;
229 proposal_substructure_t *substruct;
230 linked_list_t *list;
231 proposal_t *proposal;
232
233 list = linked_list_create();
234 /* we do not support proposals split up to two proposal substructures, as
235 * AH+ESP bundles are not supported in RFC4301 anymore.
236 * To handle such structures safely, we just skip proposals with multiple
237 * protocols.
238 */
239 enumerator = this->proposals->create_enumerator(this->proposals);
240 while (enumerator->enumerate(enumerator, &substruct))
241 {
242 /* check if a proposal has a single protocol */
243 if (substruct->get_proposal_number(substruct) == struct_number)
244 {
245 if (ignore_struct_number < struct_number)
246 {
247 /* remove an already added, if first of series */
248 list->remove_last(list, (void**)&proposal);
249 proposal->destroy(proposal);
250 ignore_struct_number = struct_number;
251 }
252 continue;
253 }
254 struct_number++;
255 proposal = substruct->get_proposal(substruct);
256 if (proposal)
257 {
258 list->insert_last(list, proposal);
259 }
260 }
261 enumerator->destroy(enumerator);
262 return list;
263 }
264
265 METHOD2(payload_t, sa_payload_t, destroy, void,
266 private_sa_payload_t *this)
267 {
268 this->proposals->destroy_offset(this->proposals,
269 offsetof(proposal_substructure_t, destroy));
270 free(this);
271 }
272
273 /*
274 * Described in header.
275 */
276 sa_payload_t *sa_payload_create()
277 {
278 private_sa_payload_t *this;
279
280 INIT(this,
281 .public = {
282 .payload_interface = {
283 .verify = _verify,
284 .get_encoding_rules = _get_encoding_rules,
285 .get_length = _get_length,
286 .get_next_type = _get_next_type,
287 .set_next_type = _set_next_type,
288 .get_type = _get_type,
289 .destroy = _destroy,
290 },
291 .add_proposal = _add_proposal,
292 .get_proposals = _get_proposals,
293 .destroy = _destroy,
294 },
295 .next_payload = NO_PAYLOAD,
296 .payload_length = SA_PAYLOAD_HEADER_LENGTH,
297 .proposals = linked_list_create(),
298 );
299 return &this->public;
300 }
301
302 /*
303 * Described in header.
304 */
305 sa_payload_t *sa_payload_create_from_proposal_list(linked_list_t *proposals)
306 {
307 private_sa_payload_t *this;
308 enumerator_t *enumerator;
309 proposal_t *proposal;
310
311 this = (private_sa_payload_t*)sa_payload_create();
312 enumerator = proposals->create_enumerator(proposals);
313 while (enumerator->enumerate(enumerator, &proposal))
314 {
315 add_proposal(this, proposal);
316 }
317 enumerator->destroy(enumerator);
318
319 return &this->public;
320 }
321
322 /*
323 * Described in header.
324 */
325 sa_payload_t *sa_payload_create_from_proposal(proposal_t *proposal)
326 {
327 private_sa_payload_t *this;
328
329 this = (private_sa_payload_t*)sa_payload_create();
330 add_proposal(this, proposal);
331
332 return &this->public;
333 }