6f9a16bc980b89a4fd70d33a27aced73d0f6ba94
[strongswan.git] / Source / charon / payloads / ike_header.c
1 /**
2 * @file ike_header.c
3 *
4 * @brief Declaration of the class ike_header_t.
5 *
6 * An object of this type represents an ike header and is used to
7 * generate and parse ike headers.
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 "ike_header.h"
30
31 #include "encodings.h"
32 #include "../utils/allocator.h"
33
34 typedef struct private_ike_header_s private_ike_header_t;
35
36 struct private_ike_header_s {
37 /**
38 * public interface
39 */
40 ike_header_t public;
41
42 /**
43 * SPI of the initiator
44 */
45 u_int64_t initiator_spi;
46 /**
47 * SPI of the responder
48 */
49 u_int64_t responder_spi;
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 * is protocol supporting higher version?
80 */
81 bool version;
82 /**
83 * TRUE, if this is a response, FALSE if its a Request
84 */
85 bool response;
86 } flags;
87 /**
88 * Associated Message-ID
89 */
90 u_int32_t message_id;
91 /**
92 * Length of the whole IKEv2-Message (header and all payloads)
93 */
94 u_int32_t length;
95 };
96
97 /**
98 * mappings used to get strings for exchange_type_t
99 */
100 mapping_t exchange_type_m[] = {
101 {EXCHANGE_TYPE_UNDEFINED, "EXCHANGE_TYPE_UNDEFINED"},
102 {IKE_SA_INIT, "IKE_SA_INIT"},
103 {IKE_AUTH, "IKE_AUTH"},
104 {CREATE_CHILD_SA, "CREATE_CHILD_SA"},
105 {INFORMATIONAL, "INFORMATIONAL"}
106 };
107
108
109 /**
110 * Encoding rules to parse or generate a IKEv2-Header
111 *
112 * The defined offsets are the positions in a object of type
113 * ike_header_t.
114 *
115 */
116 encoding_rule_t ike_header_encodings[] = {
117 /* 8 Byte SPI, stored in the field initiator_spi */
118 { U_INT_64, offsetof(private_ike_header_t, initiator_spi) },
119 /* 8 Byte SPI, stored in the field responder_spi */
120 { U_INT_64, offsetof(private_ike_header_t, responder_spi) },
121 /* 1 Byte next payload type, stored in the field next_payload */
122 { U_INT_8, offsetof(private_ike_header_t, next_payload) },
123 /* 4 Bit major version, stored in the field maj_version */
124 { U_INT_4, offsetof(private_ike_header_t, maj_version) },
125 /* 4 Bit minor version, stored in the field min_version */
126 { U_INT_4, offsetof(private_ike_header_t, min_version) },
127 /* 8 Bit for the exchange type */
128 { U_INT_8, offsetof(private_ike_header_t, exchange_type) },
129 /* 2 Bit reserved bits, nowhere stored */
130 { RESERVED_BIT, 0 },
131 { RESERVED_BIT, 0 },
132 /* 3 Bit flags, stored in the fields response, version and initiator */
133 { FLAG, offsetof(private_ike_header_t, flags.response) },
134 { FLAG, offsetof(private_ike_header_t, flags.version) },
135 { FLAG, offsetof(private_ike_header_t, flags.initiator) },
136 /* 3 Bit reserved bits, nowhere stored */
137 { RESERVED_BIT, 0 },
138 { RESERVED_BIT, 0 },
139 { RESERVED_BIT, 0 },
140 /* 4 Byte message id, stored in the field message_id */
141 { U_INT_32, offsetof(private_ike_header_t, message_id) },
142 /* 4 Byte length fied, stored in the field length */
143 { HEADER_LENGTH, offsetof(private_ike_header_t, length) }
144 };
145
146 /**
147 * Implements payload_t's set_next_type function.
148 * See #payload_s.set_next_type for description.
149 */
150 static status_t set_next_type(payload_t *this,payload_type_t type)
151 {
152 ((private_ike_header_t *)this)->next_payload = type;
153 return SUCCESS;
154 }
155 /**
156 * Implements ike_header_t's get_initiator_spi fuction.
157 * See #ike_header_t.get_initiator_spi for description.
158 */
159 static u_int64_t get_initiator_spi(private_ike_header_t *this)
160 {
161 return this->initiator_spi;
162 }
163
164 /**
165 * Implements ike_header_t's set_initiator_spi fuction.
166 * See #ike_header_t.set_initiator_spi for description.
167 */
168 static void set_initiator_spi(private_ike_header_t *this, u_int64_t initiator_spi)
169 {
170 this->initiator_spi = initiator_spi;
171 }
172
173 /**
174 * Implements ike_header_t's get_responder_spi fuction.
175 * See #ike_header_t.get_responder_spi for description.
176 */
177 static u_int64_t get_responder_spi(private_ike_header_t *this)
178 {
179 return this->responder_spi;
180 }
181
182 /**
183 * Implements ike_header_t's set_responder_spi fuction.
184 * See #ike_header_t.set_responder_spi for description.
185 */
186 static void set_responder_spi(private_ike_header_t *this, u_int64_t responder_spi)
187 {
188 this->responder_spi = responder_spi;
189 }
190
191 /**
192 * Implements ike_header_t's get_maj_version fuction.
193 * See #ike_header_t.get_maj_version for description.
194 */
195 static u_int8_t get_maj_version(private_ike_header_t *this)
196 {
197 return this->maj_version;
198 }
199
200 /**
201 * Implements ike_header_t's get_min_version fuction.
202 * See #ike_header_t.get_min_version for description.
203 */
204 static u_int8_t get_min_version(private_ike_header_t *this)
205 {
206 return this->min_version;
207 }
208
209 /**
210 * Implements ike_header_t's get_response_flag fuction.
211 * See #ike_header_t.get_response_flag for description.
212 */
213 static bool get_response_flag(private_ike_header_t *this)
214 {
215 return this->flags.response;
216 }
217
218 /**
219 * Implements ike_header_t's set_response_flag fuction.
220 * See #ike_header_t.set_response_flag for description.
221 */
222 static void set_response_flag(private_ike_header_t *this, bool response)
223 {
224 this->flags.response = response;
225 }
226
227 /**
228 * Implements ike_header_t's get_version_flag fuction.
229 * See #ike_header_t.get_version_flag for description.
230 */
231 static bool get_version_flag(private_ike_header_t *this)
232 {
233 return this->flags.version;
234 }
235
236 /**
237 * Implements ike_header_t's get_initiator_flag fuction.
238 * See #ike_header_t.get_initiator_flag for description.
239 */
240 static bool get_initiator_flag(private_ike_header_t *this)
241 {
242 return this->flags.initiator;
243 }
244
245 /**
246 * Implements ike_header_t's set_initiator_flag fuction.
247 * See #ike_header_t.set_initiator_flag for description.
248 */
249 static void set_initiator_flag(private_ike_header_t *this, bool initiator)
250 {
251 this->flags.initiator = initiator;
252 }
253
254 /**
255 * Implements ike_header_t's get_exchange_type function
256 * See #ike_header_t.get_exchange_type for description.
257 */
258 static u_int8_t get_exchange_type(private_ike_header_t *this)
259 {
260 return this->exchange_type;
261 }
262
263 /**
264 * Implements ike_header_t's set_exchange_type function.
265 * See #ike_header_t.set_exchange_type for description.
266 */
267 static void set_exchange_type(private_ike_header_t *this, u_int8_t exchange_type)
268 {
269 this->exchange_type = exchange_type;
270 }
271
272 /**
273 * Implements ike_header_t's get_message_id function.
274 * See #ike_header_t.get_message_id for description.
275 */
276 static u_int32_t get_message_id(private_ike_header_t *this)
277 {
278 return this->message_id;
279 }
280
281 /**
282 * Implements ike_header_t's set_message_id function.
283 * See #ike_header_t.set_message_id for description.
284 */
285 static void set_message_id(private_ike_header_t *this, u_int32_t message_id)
286 {
287 this->message_id = message_id;
288 }
289
290 /**
291 * Implements payload_t's and ike_header_t's destroy function.
292 * See #payload_s.destroy or ike_header_s.destroy for description.
293 */
294 static status_t destroy(ike_header_t *this)
295 {
296 allocator_free(this);
297
298 return SUCCESS;
299 }
300
301 /**
302 * Implements payload_t's get_encoding_rules function.
303 * See #payload_s.get_encoding_rules for description.
304 */
305 static status_t get_encoding_rules(payload_t *this, encoding_rule_t **rules, size_t *rule_count)
306 {
307 *rules = ike_header_encodings;
308 *rule_count = sizeof(ike_header_encodings) / sizeof(encoding_rule_t);
309
310 return SUCCESS;
311 }
312
313 /**
314 * Implements payload_t's get_type function.
315 * See #payload_s.get_type for description.
316 */
317 static payload_type_t get_type(payload_t *this)
318 {
319 return HEADER;
320 }
321
322 /**
323 * Implements payload_t's get_next_type function.
324 * See #payload_s.get_next_type for description.
325 */
326 static payload_type_t get_next_type(payload_t *this)
327 {
328 return (((private_ike_header_t*)this)->next_payload);
329 }
330
331 /**
332 * Implements payload_t's get_length function.
333 * See #payload_s.get_length for description.
334 */
335 static size_t get_length(payload_t *this)
336 {
337 return (((private_ike_header_t*)this)->length);
338 }
339
340 /*
341 * Described in header
342 */
343 ike_header_t *ike_header_create()
344 {
345 private_ike_header_t *this = allocator_alloc_thing(private_ike_header_t);
346 if (this == NULL)
347 {
348 return NULL;
349 }
350
351 this->public.payload_interface.get_encoding_rules = get_encoding_rules;
352 this->public.payload_interface.get_length = get_length;
353 this->public.payload_interface.get_next_type = get_next_type;
354 this->public.payload_interface.set_next_type = set_next_type;
355 this->public.payload_interface.get_type = get_type;
356 this->public.payload_interface.destroy = (status_t (*) (payload_t *))destroy;
357 this->public.destroy = destroy;
358
359 this->public.get_initiator_spi = (u_int64_t (*) (ike_header_t*))get_initiator_spi;
360 this->public.set_initiator_spi = (void (*) (ike_header_t*,u_int64_t))set_initiator_spi;
361 this->public.get_responder_spi = (u_int64_t (*) (ike_header_t*))get_responder_spi;
362 this->public.set_responder_spi = (void (*) (ike_header_t *,u_int64_t))set_responder_spi;
363 this->public.get_maj_version = (u_int8_t (*) (ike_header_t*))get_maj_version;
364 this->public.get_min_version = (u_int8_t (*) (ike_header_t*))get_min_version;
365 this->public.get_response_flag = (bool (*) (ike_header_t*))get_response_flag;
366 this->public.set_response_flag = (void (*) (ike_header_t*,bool))set_response_flag;
367 this->public.get_version_flag = (bool (*) (ike_header_t*))get_version_flag;
368 this->public.get_initiator_flag = (bool (*) (ike_header_t*))get_initiator_flag;
369 this->public.set_initiator_flag = (void (*) (ike_header_t*,bool))set_initiator_flag;
370 this->public.get_exchange_type = (u_int8_t (*) (ike_header_t*))get_exchange_type;
371 this->public.set_exchange_type = (void (*) (ike_header_t*,u_int8_t))set_exchange_type;
372 this->public.get_message_id = (u_int32_t (*) (ike_header_t*))get_message_id;
373 this->public.set_message_id = (void (*) (ike_header_t*,u_int32_t))set_message_id;
374
375 /* set default values of the fields */
376 this->initiator_spi = 0;
377 this->responder_spi = 0;
378 this->next_payload = 0;
379 this->maj_version = IKE_MAJOR_VERSION;
380 this->min_version = IKE_MINOR_VERSION;
381 this->exchange_type = EXCHANGE_TYPE_UNDEFINED;
382 this->flags.initiator = TRUE;
383 this->flags.version = HIGHER_VERSION_SUPPORTED_FLAG;
384 this->flags.response = FALSE;
385 this->message_id = 0;
386 this->length = IKE_HEADER_LENGTH;
387
388
389 return (ike_header_t*)this;
390 }
391
392