]> git.ipfire.org Git - thirdparty/strongswan.git/blob - Source/charon/encoding/payloads/sa_payload.c
- moved packet and socket in new network-package
[thirdparty/strongswan.git] / Source / charon / encoding / payloads / sa_payload.c
1 /**
2 * @file sa_payload.c
3 *
4 * @brief Declaration of the class sa_payload_t.
5 *
6 * An object of this type represents an IKEv2 SA-Payload and contains proposal
7 * substructures.
8 *
9 */
10
11 /*
12 * Copyright (C) 2005 Jan Hutter, Martin Willi
13 * Hochschule fuer Technik Rapperswil
14 *
15 * This program is free software; you can redistribute it and/or modify it
16 * under the terms of the GNU General Public License as published by the
17 * Free Software Foundation; either version 2 of the License, or (at your
18 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
19 *
20 * This program is distributed in the hope that it will be useful, but
21 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
22 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
23 * for more details.
24 */
25
26 /* offsetof macro */
27 #include <stddef.h>
28
29 #include "sa_payload.h"
30
31 #include <encoding/payloads/encodings.h>
32 #include <utils/allocator.h>
33 #include <utils/linked_list.h>
34
35
36 /**
37 * Private data of an sa_payload_t' Object
38 *
39 */
40 typedef struct private_sa_payload_s private_sa_payload_t;
41
42 struct private_sa_payload_s {
43 /**
44 * public sa_payload_t interface
45 */
46 sa_payload_t public;
47
48 /**
49 * next payload type
50 */
51 u_int8_t next_payload;
52
53 /**
54 * Critical flag
55 */
56 bool critical;
57
58 /**
59 * Length of this payload
60 */
61 u_int16_t payload_length;
62
63 /**
64 * Proposals in this payload are stored in a linked_list_t
65 */
66 linked_list_t * proposals;
67
68 /**
69 * @brief Computes the length of this payload.
70 *
71 * @param this calling private_sa_payload_t object
72 * @return
73 * SUCCESS in any case
74 */
75 status_t (*compute_length) (private_sa_payload_t *this);
76 };
77
78 /**
79 * Encoding rules to parse or generate a IKEv2-SA Payload
80 *
81 * The defined offsets are the positions in a object of type
82 * private_sa_payload_t.
83 *
84 */
85 encoding_rule_t sa_payload_encodings[] = {
86 /* 1 Byte next payload type, stored in the field next_payload */
87 { U_INT_8, offsetof(private_sa_payload_t, next_payload) },
88 /* the critical bit */
89 { FLAG, offsetof(private_sa_payload_t, critical) },
90 /* 7 Bit reserved bits, nowhere stored */
91 { RESERVED_BIT, 0 },
92 { RESERVED_BIT, 0 },
93 { RESERVED_BIT, 0 },
94 { RESERVED_BIT, 0 },
95 { RESERVED_BIT, 0 },
96 { RESERVED_BIT, 0 },
97 { RESERVED_BIT, 0 },
98 /* Length of the whole SA payload*/
99 { PAYLOAD_LENGTH, offsetof(private_sa_payload_t, payload_length) },
100 /* Proposals are stored in a proposal substructure,
101 offset points to a linked_list_t pointer */
102 { PROPOSALS, offsetof(private_sa_payload_t, proposals) }
103 };
104
105 /*
106 1 2 3
107 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
108 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
109 ! Next Payload !C! RESERVED ! Payload Length !
110 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
111 ! !
112 ~ <Proposals> ~
113 ! !
114 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
115 */
116
117 /**
118 * Implements payload_t's verify function.
119 * See #payload_s.verify for description.
120 */
121 static status_t verify(private_sa_payload_t *this)
122 {
123 int proposal_number = 1;
124 status_t status;
125 linked_list_iterator_t *iterator;
126 bool first = TRUE;
127
128 if (this->critical)
129 {
130 /* critical bit set! */
131 return FAILED;
132 }
133
134 /* check proposal numbering */
135 status = this->proposals->create_iterator(this->proposals,&iterator,TRUE);
136 if (status != SUCCESS)
137 {
138 return status;
139 }
140
141 while(iterator->has_next(iterator))
142 {
143 proposal_substructure_t *current_proposal;
144 status = iterator->current(iterator,(void **)&current_proposal);
145 {
146 break;
147 }
148 if (current_proposal->get_proposal_number(current_proposal) > proposal_number)
149 {
150 if (first)
151 {
152 /* first number must be 1 */
153 status = FAILED;
154 break;
155 }
156
157 if (current_proposal->get_proposal_number(current_proposal) != (proposal_number + 1))
158 {
159 /* must be only one more then previous proposal */
160 status = FAILED;
161 break;
162 }
163 }
164 else if (current_proposal->get_proposal_number(current_proposal) < proposal_number)
165 {
166 iterator->destroy(iterator);
167 /* must not be smaller then proceeding one */
168 status = FAILED;
169 break;
170 }
171 first = FALSE;
172 }
173
174 iterator->destroy(iterator);
175 return status;
176 }
177
178
179 /**
180 * Implements payload_t's and sa_payload_t's destroy function.
181 * See #payload_s.destroy or sa_payload_s.destroy for description.
182 */
183 static status_t destroy(private_sa_payload_t *this)
184 {
185 /* all proposals are getting destroyed */
186 while (this->proposals->get_count(this->proposals) > 0)
187 {
188 proposal_substructure_t *current_proposal;
189 if (this->proposals->remove_last(this->proposals,(void **)&current_proposal) != SUCCESS)
190 {
191 break;
192 }
193 current_proposal->destroy(current_proposal);
194 }
195 this->proposals->destroy(this->proposals);
196
197 allocator_free(this);
198
199 return SUCCESS;
200 }
201
202 /**
203 * Implements payload_t's get_encoding_rules function.
204 * See #payload_s.get_encoding_rules for description.
205 */
206 static status_t get_encoding_rules(private_sa_payload_t *this, encoding_rule_t **rules, size_t *rule_count)
207 {
208 *rules = sa_payload_encodings;
209 *rule_count = sizeof(sa_payload_encodings) / sizeof(encoding_rule_t);
210
211 return SUCCESS;
212 }
213
214 /**
215 * Implements payload_t's get_type function.
216 * See #payload_s.get_type for description.
217 */
218 static payload_type_t get_type(private_sa_payload_t *this)
219 {
220 return SECURITY_ASSOCIATION;
221 }
222
223 /**
224 * Implements payload_t's get_next_type function.
225 * See #payload_s.get_next_type for description.
226 */
227 static payload_type_t get_next_type(private_sa_payload_t *this)
228 {
229 return (this->next_payload);
230 }
231
232 /**
233 * Implements payload_t's set_next_type function.
234 * See #payload_s.set_next_type for description.
235 */
236 static status_t set_next_type(private_sa_payload_t *this,payload_type_t type)
237 {
238 this->next_payload = type;
239 return SUCCESS;
240 }
241
242 /**
243 * Implements payload_t's get_length function.
244 * See #payload_s.get_length for description.
245 */
246 static size_t get_length(private_sa_payload_t *this)
247 {
248 this->compute_length(this);
249 return this->payload_length;
250 }
251
252 /**
253 * Implements sa_payload_t's create_proposal_substructure_iterator function.
254 * See #sa_payload_s.create_proposal_substructure_iterator for description.
255 */
256 static status_t create_proposal_substructure_iterator (private_sa_payload_t *this,linked_list_iterator_t **iterator,bool forward)
257 {
258 return (this->proposals->create_iterator(this->proposals,iterator,forward));
259 }
260
261 /**
262 * Implements sa_payload_t's add_proposal_substructure function.
263 * See #sa_payload_s.add_proposal_substructure for description.
264 */
265 static status_t add_proposal_substructure (private_sa_payload_t *this,proposal_substructure_t *proposal)
266 {
267 status_t status;
268 status = this->proposals->insert_last(this->proposals,(void *) proposal);
269 this->compute_length(this);
270 return status;
271 }
272
273 /**
274 * Implements private_sa_payload_t's compute_length function.
275 * See #private_sa_payload_s.compute_length for description.
276 */
277 static status_t compute_length (private_sa_payload_t *this)
278 {
279 linked_list_iterator_t *iterator;
280 status_t status;
281 size_t length = SA_PAYLOAD_HEADER_LENGTH;
282 status = this->proposals->create_iterator(this->proposals,&iterator,TRUE);
283 if (status != SUCCESS)
284 {
285 return length;
286 }
287 while (iterator->has_next(iterator))
288 {
289 payload_t *current_proposal;
290 iterator->current(iterator,(void **) &current_proposal);
291 length += current_proposal->get_length(current_proposal);
292 }
293 iterator->destroy(iterator);
294
295 this->payload_length = length;
296
297 return SUCCESS;
298 }
299
300 /*
301 * Described in header
302 */
303 sa_payload_t *sa_payload_create()
304 {
305 private_sa_payload_t *this = allocator_alloc_thing(private_sa_payload_t);
306 if (this == NULL)
307 {
308 return NULL;
309 }
310
311 /* public interface */
312 this->public.payload_interface.verify = (status_t (*) (payload_t *))verify;
313 this->public.payload_interface.get_encoding_rules = (status_t (*) (payload_t *, encoding_rule_t **, size_t *) ) get_encoding_rules;
314 this->public.payload_interface.get_length = (size_t (*) (payload_t *)) get_length;
315 this->public.payload_interface.get_next_type = (payload_type_t (*) (payload_t *)) get_next_type;
316 this->public.payload_interface.set_next_type = (status_t (*) (payload_t *,payload_type_t)) set_next_type;
317 this->public.payload_interface.get_type = (payload_type_t (*) (payload_t *)) get_type;
318 this->public.payload_interface.destroy = (status_t (*) (payload_t *))destroy;
319
320 /* public functions */
321 this->public.create_proposal_substructure_iterator = (status_t (*) (sa_payload_t *,linked_list_iterator_t **,bool)) create_proposal_substructure_iterator;
322 this->public.add_proposal_substructure = (status_t (*) (sa_payload_t *,proposal_substructure_t *)) add_proposal_substructure;
323 this->public.destroy = (status_t (*) (sa_payload_t *)) destroy;
324
325 /* private functions */
326 this->compute_length = compute_length;
327
328 /* set default values of the fields */
329 this->critical = SA_PAYLOAD_CRITICAL_FLAG;
330 this->next_payload = NO_PAYLOAD;
331 this->payload_length = SA_PAYLOAD_HEADER_LENGTH;
332
333 this->proposals = linked_list_create();
334
335 if (this->proposals == NULL)
336 {
337 allocator_free(this);
338 return NULL;
339 }
340 return (&(this->public));
341 }
342
343