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