updated Doxyfile
[strongswan.git] / src / charon / encoding / payloads / ike_header.c
1 /*
2 * Copyright (C) 2007 Tobias Brunner
3 * Copyright (C) 2005-2006 Martin Willi
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 * $Id$
18 */
19
20 /* offsetof macro */
21 #include <stddef.h>
22
23 #include "ike_header.h"
24
25 #include <encoding/payloads/encodings.h>
26
27
28 typedef struct private_ike_header_t private_ike_header_t;
29
30 /**
31 * Private data of an ike_header_t object.
32 *
33 */
34 struct private_ike_header_t {
35 /**
36 * Public interface.
37 */
38 ike_header_t public;
39
40 /**
41 * SPI of the initiator.
42 */
43 u_int64_t initiator_spi;
44
45 /**
46 * SPI of the responder.
47 */
48 u_int64_t responder_spi;
49
50 /**
51 * Next payload type.
52 */
53 u_int8_t next_payload;
54 /**
55 * IKE major version.
56 */
57 u_int8_t maj_version;
58
59 /**
60 * IKE minor version.
61 */
62 u_int8_t min_version;
63
64 /**
65 * Exchange type .
66 */
67 u_int8_t exchange_type;
68
69 /**
70 * Flags of the Message.
71 *
72 */
73 struct {
74 /**
75 * Sender is initiator of the associated IKE_SA_INIT-Exchange.
76 */
77 bool initiator;
78
79 /**
80 * Is protocol supporting higher version?
81 */
82 bool version;
83
84 /**
85 * TRUE, if this is a response, FALSE if its a Request.
86 */
87 bool response;
88 } flags;
89
90 /**
91 * Associated Message-ID.
92 */
93 u_int32_t message_id;
94
95 /**
96 * Length of the whole IKEv2-Message (header and all payloads).
97 */
98 u_int32_t length;
99 };
100
101 ENUM_BEGIN(exchange_type_names, EXCHANGE_TYPE_UNDEFINED, EXCHANGE_TYPE_UNDEFINED,
102 "EXCHANGE_TYPE_UNDEFINED");
103 ENUM_NEXT(exchange_type_names, IKE_SA_INIT, INFORMATIONAL, EXCHANGE_TYPE_UNDEFINED,
104 "IKE_SA_INIT",
105 "IKE_AUTH",
106 "CREATE_CHILD_SA",
107 "INFORMATIONAL");
108 #ifdef ME
109 ENUM_NEXT(exchange_type_names, ME_CONNECT, ME_CONNECT, INFORMATIONAL,
110 "ME_CONNECT");
111 ENUM_END(exchange_type_names, ME_CONNECT);
112 #else
113 ENUM_END(exchange_type_names, INFORMATIONAL);
114 #endif /* ME */
115
116 /**
117 * Encoding rules to parse or generate a IKEv2-Header.
118 *
119 * The defined offsets are the positions in a object of type
120 * ike_header_t.
121 *
122 */
123 encoding_rule_t ike_header_encodings[] = {
124 /* 8 Byte SPI, stored in the field initiator_spi */
125 { IKE_SPI, offsetof(private_ike_header_t, initiator_spi) },
126 /* 8 Byte SPI, stored in the field responder_spi */
127 { IKE_SPI, offsetof(private_ike_header_t, responder_spi) },
128 /* 1 Byte next payload type, stored in the field next_payload */
129 { U_INT_8, offsetof(private_ike_header_t, next_payload) },
130 /* 4 Bit major version, stored in the field maj_version */
131 { U_INT_4, offsetof(private_ike_header_t, maj_version) },
132 /* 4 Bit minor version, stored in the field min_version */
133 { U_INT_4, offsetof(private_ike_header_t, min_version) },
134 /* 8 Bit for the exchange type */
135 { U_INT_8, offsetof(private_ike_header_t, exchange_type) },
136 /* 2 Bit reserved bits, nowhere stored */
137 { RESERVED_BIT, 0 },
138 { RESERVED_BIT, 0 },
139 /* 3 Bit flags, stored in the fields response, version and initiator */
140 { FLAG, offsetof(private_ike_header_t, flags.response) },
141 { FLAG, offsetof(private_ike_header_t, flags.version) },
142 { FLAG, offsetof(private_ike_header_t, flags.initiator) },
143 /* 3 Bit reserved bits, nowhere stored */
144 { RESERVED_BIT, 0 },
145 { RESERVED_BIT, 0 },
146 { RESERVED_BIT, 0 },
147 /* 4 Byte message id, stored in the field message_id */
148 { U_INT_32, offsetof(private_ike_header_t, message_id) },
149 /* 4 Byte length fied, stored in the field length */
150 { HEADER_LENGTH, offsetof(private_ike_header_t, length) }
151 };
152
153
154 /* 1 2 3
155 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
156 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
157 ! IKE_SA Initiator's SPI !
158 ! !
159 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
160 ! IKE_SA Responder's SPI !
161 ! !
162 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
163 ! Next Payload ! MjVer ! MnVer ! Exchange Type ! Flags !
164 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
165 ! Message ID !
166 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
167 ! Length !
168 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
169 */
170
171
172 /**
173 * Implementation of payload_t.verify.
174 */
175 static status_t verify(private_ike_header_t *this)
176 {
177 if ((this->exchange_type < IKE_SA_INIT) ||
178 ((this->exchange_type > INFORMATIONAL)
179 #ifdef ME
180 && (this->exchange_type != ME_CONNECT)
181 #endif /* ME */
182 ))
183 {
184 /* unsupported exchange type */
185 return FAILED;
186 }
187
188 if (this->initiator_spi == 0
189 #ifdef ME
190 /* we allow zero spi for INFORMATIONAL exchanges,
191 * to allow connectivity checks */
192 && this->exchange_type != INFORMATIONAL
193 #endif /* ME */
194 )
195 {
196 /* initiator spi not set */
197 return FAILED;
198 }
199
200 /* verification of version is not done in here */
201
202 return SUCCESS;
203 }
204
205 /**
206 * Implementation of payload_t.set_next_type.
207 */
208 static void set_next_type(payload_t *this,payload_type_t type)
209 {
210 ((private_ike_header_t *)this)->next_payload = type;
211 }
212 /**
213 * Implementation of ike_header_t.get_initiator_spi.
214 */
215 static u_int64_t get_initiator_spi(private_ike_header_t *this)
216 {
217 return this->initiator_spi;
218 }
219
220 /**
221 * Implementation of ike_header_t.set_initiator_spi.
222 */
223 static void set_initiator_spi(private_ike_header_t *this, u_int64_t initiator_spi)
224 {
225 this->initiator_spi = initiator_spi;
226 }
227
228 /**
229 * Implementation of ike_header_t.get_responder_spi.
230 */
231 static u_int64_t get_responder_spi(private_ike_header_t *this)
232 {
233 return this->responder_spi;
234 }
235
236 /**
237 * Implementation of ike_header_t.set_responder_spi.
238 */
239 static void set_responder_spi(private_ike_header_t *this, u_int64_t responder_spi)
240 {
241 this->responder_spi = responder_spi;
242 }
243
244 /**
245 * Implementation of ike_header_t.get_maj_version.
246 */
247 static u_int8_t get_maj_version(private_ike_header_t *this)
248 {
249 return this->maj_version;
250 }
251
252 /**
253 * Implementation of ike_header_t.get_min_version.
254 */
255 static u_int8_t get_min_version(private_ike_header_t *this)
256 {
257 return this->min_version;
258 }
259
260 /**
261 * Implementation of ike_header_t.get_response_flag.
262 */
263 static bool get_response_flag(private_ike_header_t *this)
264 {
265 return this->flags.response;
266 }
267
268 /**
269 * Implementation of ike_header_t.set_response_flag.
270 */
271 static void set_response_flag(private_ike_header_t *this, bool response)
272 {
273 this->flags.response = response;
274 }
275
276 /**
277 * Implementation of ike_header_t.get_version_flag.
278 */
279 static bool get_version_flag(private_ike_header_t *this)
280 {
281 return this->flags.version;
282 }
283
284 /**
285 * Implementation of ike_header_t.get_initiator_flag.
286 */
287 static bool get_initiator_flag(private_ike_header_t *this)
288 {
289 return this->flags.initiator;
290 }
291
292 /**
293 * Implementation of ike_header_t.set_initiator_flag.
294 */
295 static void set_initiator_flag(private_ike_header_t *this, bool initiator)
296 {
297 this->flags.initiator = initiator;
298 }
299
300 /**
301 * Implementation of ike_header_t.get_exchange_type.
302 */
303 static u_int8_t get_exchange_type(private_ike_header_t *this)
304 {
305 return this->exchange_type;
306 }
307
308 /**
309 * Implementation of ike_header_t.set_exchange_type.
310 */
311 static void set_exchange_type(private_ike_header_t *this, u_int8_t exchange_type)
312 {
313 this->exchange_type = exchange_type;
314 }
315
316 /**
317 * Implements ike_header_t's get_message_id function.
318 * See #ike_header_t.get_message_id for description.
319 */
320 static u_int32_t get_message_id(private_ike_header_t *this)
321 {
322 return this->message_id;
323 }
324
325 /**
326 * Implementation of ike_header_t.set_message_id.
327 */
328 static void set_message_id(private_ike_header_t *this, u_int32_t message_id)
329 {
330 this->message_id = message_id;
331 }
332
333 /**
334 * Implementation of ike_header_t.destroy and payload_t.destroy.
335 */
336 static void destroy(ike_header_t *this)
337 {
338 free(this);
339 }
340
341 /**
342 * Implementation of payload_t.get_encoding_rules.
343 */
344 static void get_encoding_rules(payload_t *this, encoding_rule_t **rules, size_t *rule_count)
345 {
346 *rules = ike_header_encodings;
347 *rule_count = sizeof(ike_header_encodings) / sizeof(encoding_rule_t);
348 }
349
350 /**
351 * Implementation of payload_t.get_type.
352 */
353 static payload_type_t get_type(payload_t *this)
354 {
355 return HEADER;
356 }
357
358 /**
359 * Implementation of payload_t.get_next_type.
360 */
361 static payload_type_t get_next_type(payload_t *this)
362 {
363 return (((private_ike_header_t*)this)->next_payload);
364 }
365
366 /**
367 * Implementation of payload_t.get_length.
368 */
369 static size_t get_length(payload_t *this)
370 {
371 return (((private_ike_header_t*)this)->length);
372 }
373
374 /*
375 * Described in header.
376 */
377 ike_header_t *ike_header_create()
378 {
379 private_ike_header_t *this = malloc_thing(private_ike_header_t);
380
381 this->public.payload_interface.verify = (status_t (*) (payload_t *))verify;
382 this->public.payload_interface.get_encoding_rules = get_encoding_rules;
383 this->public.payload_interface.get_length = get_length;
384 this->public.payload_interface.get_next_type = get_next_type;
385 this->public.payload_interface.set_next_type = set_next_type;
386 this->public.payload_interface.get_type = get_type;
387 this->public.payload_interface.destroy = (void (*) (payload_t *))destroy;
388 this->public.destroy = destroy;
389
390 this->public.get_initiator_spi = (u_int64_t (*) (ike_header_t*))get_initiator_spi;
391 this->public.set_initiator_spi = (void (*) (ike_header_t*,u_int64_t))set_initiator_spi;
392 this->public.get_responder_spi = (u_int64_t (*) (ike_header_t*))get_responder_spi;
393 this->public.set_responder_spi = (void (*) (ike_header_t *,u_int64_t))set_responder_spi;
394 this->public.get_maj_version = (u_int8_t (*) (ike_header_t*))get_maj_version;
395 this->public.get_min_version = (u_int8_t (*) (ike_header_t*))get_min_version;
396 this->public.get_response_flag = (bool (*) (ike_header_t*))get_response_flag;
397 this->public.set_response_flag = (void (*) (ike_header_t*,bool))set_response_flag;
398 this->public.get_version_flag = (bool (*) (ike_header_t*))get_version_flag;
399 this->public.get_initiator_flag = (bool (*) (ike_header_t*))get_initiator_flag;
400 this->public.set_initiator_flag = (void (*) (ike_header_t*,bool))set_initiator_flag;
401 this->public.get_exchange_type = (u_int8_t (*) (ike_header_t*))get_exchange_type;
402 this->public.set_exchange_type = (void (*) (ike_header_t*,u_int8_t))set_exchange_type;
403 this->public.get_message_id = (u_int32_t (*) (ike_header_t*))get_message_id;
404 this->public.set_message_id = (void (*) (ike_header_t*,u_int32_t))set_message_id;
405
406 /* set default values of the fields */
407 this->initiator_spi = 0;
408 this->responder_spi = 0;
409 this->next_payload = 0;
410 this->maj_version = IKE_MAJOR_VERSION;
411 this->min_version = IKE_MINOR_VERSION;
412 this->exchange_type = EXCHANGE_TYPE_UNDEFINED;
413 this->flags.initiator = TRUE;
414 this->flags.version = HIGHER_VERSION_SUPPORTED_FLAG;
415 this->flags.response = FALSE;
416 this->message_id = 0;
417 this->length = IKE_HEADER_LENGTH;
418
419 return (ike_header_t*)this;
420 }