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