4 * @brief Declaration of the class ike_header_t.
6 * An object of this type represents an ike header and is used to
7 * generate and parse ike headers.
12 * Copyright (C) 2005 Jan Hutter, Martin Willi
13 * Hochschule fuer Technik Rapperswil
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>.
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
29 #include "ike_header.h"
31 #include "encodings.h"
32 #include "../utils/allocator.h"
34 typedef struct private_ike_header_s private_ike_header_t
;
36 struct private_ike_header_s
{
43 * SPI of the initiator
45 u_int64_t initiator_spi
;
47 * SPI of the responder
49 u_int64_t responder_spi
;
53 u_int8_t next_payload
;
67 u_int8_t exchange_type
;
70 * Flags of the Message
75 * Sender is initiator of the associated IKE_SA_INIT-Exchange
79 * is protocol supporting higher version?
83 * TRUE, if this is a response, FALSE if its a Request
88 * Associated Message-ID
92 * Length of the whole IKEv2-Message (header and all payloads)
100 * Encoding rules to parse or generate a IKEv2-Header
102 * The defined offsets are the positions in a object of type
106 encoding_rule_t ike_header_encodings
[] = {
107 /* 8 Byte SPI, stored in the field initiator_spi */
108 { U_INT_64
, offsetof(private_ike_header_t
, initiator_spi
) },
109 /* 8 Byte SPI, stored in the field responder_spi */
110 { U_INT_64
, offsetof(private_ike_header_t
, responder_spi
) },
111 /* 1 Byte next payload type, stored in the field next_payload */
112 { U_INT_8
, offsetof(private_ike_header_t
, next_payload
) },
113 /* 4 Bit major version, stored in the field maj_version */
114 { U_INT_4
, offsetof(private_ike_header_t
, maj_version
) },
115 /* 4 Bit minor version, stored in the field min_version */
116 { U_INT_4
, offsetof(private_ike_header_t
, min_version
) },
117 /* 8 Bit for the exchange type */
118 { U_INT_8
, offsetof(private_ike_header_t
, exchange_type
) },
119 /* 2 Bit reserved bits, nowhere stored */
122 /* 3 Bit flags, stored in the fields response, version and initiator */
123 { FLAG
, offsetof(private_ike_header_t
, flags
.response
) },
124 { FLAG
, offsetof(private_ike_header_t
, flags
.version
) },
125 { FLAG
, offsetof(private_ike_header_t
, flags
.initiator
) },
126 /* 3 Bit reserved bits, nowhere stored */
130 /* 4 Byte message id, stored in the field message_id */
131 { U_INT_32
, offsetof(private_ike_header_t
, message_id
) },
132 /* 4 Byte length fied, stored in the field length */
133 { HEADER_LENGTH
, offsetof(private_ike_header_t
, length
) }
137 * Implements ike_header_t's get_next_payload fuction.
138 * See #ike_header_t.get_next_payload for description.
140 static u_int8_t
get_next_payload(private_ike_header_t
*this)
142 return this->next_payload
;
146 * Implements ike_header_t's set_next_payload fuction.
147 * See #ike_header_t.set_next_payload for description.
149 static void set_next_payload(private_ike_header_t
*this, u_int8_t next_payload
)
151 this->next_payload
= next_payload
;
155 * Implements ike_header_t's get_initiator_spi fuction.
156 * See #ike_header_t.get_initiator_spi for description.
158 static u_int64_t
get_initiator_spi(private_ike_header_t
*this)
160 return this->initiator_spi
;
164 * Implements ike_header_t's set_initiator_spi fuction.
165 * See #ike_header_t.set_initiator_spi for description.
167 static void set_initiator_spi(private_ike_header_t
*this, u_int64_t initiator_spi
)
169 this->initiator_spi
= initiator_spi
;
173 * Implements ike_header_t's get_responder_spi fuction.
174 * See #ike_header_t.get_responder_spi for description.
176 static u_int64_t
get_responder_spi(private_ike_header_t
*this)
178 return this->responder_spi
;
182 * Implements ike_header_t's set_responder_spi fuction.
183 * See #ike_header_t.set_responder_spi for description.
185 static void set_responder_spi(private_ike_header_t
*this, u_int64_t responder_spi
)
187 this->responder_spi
= responder_spi
;
191 * Implements ike_header_t's get_maj_version fuction.
192 * See #ike_header_t.get_maj_version for description.
194 static u_int8_t
get_maj_version(private_ike_header_t
*this)
196 return this->maj_version
;
200 * Implements ike_header_t's get_min_version fuction.
201 * See #ike_header_t.get_min_version for description.
203 static u_int8_t
get_min_version(private_ike_header_t
*this)
205 return this->min_version
;
209 * Implements ike_header_t's get_response_flag fuction.
210 * See #ike_header_t.get_response_flag for description.
212 static bool get_response_flag(private_ike_header_t
*this)
214 return this->flags
.response
;
218 * Implements ike_header_t's set_response_flag fuction.
219 * See #ike_header_t.set_response_flag for description.
221 static void set_response_flag(private_ike_header_t
*this, bool response
)
223 this->flags
.response
= response
;
227 * Implements ike_header_t's get_version_flag fuction.
228 * See #ike_header_t.get_version_flag for description.
230 static bool get_version_flag(private_ike_header_t
*this)
232 return this->flags
.version
;
236 * Implements ike_header_t's get_initiator_flag fuction.
237 * See #ike_header_t.get_initiator_flag for description.
239 static bool get_initiator_flag(private_ike_header_t
*this)
241 return this->flags
.initiator
;
245 * Implements ike_header_t's set_initiator_flag fuction.
246 * See #ike_header_t.set_initiator_flag for description.
248 static void set_initiator_flag(private_ike_header_t
*this, bool initiator
)
250 this->flags
.initiator
= initiator
;
254 * Implements ike_header_t's get_exchange_type function
255 * See #ike_header_t.get_exchange_type for description.
257 static u_int8_t
get_exchange_type(private_ike_header_t
*this)
259 return this->exchange_type
;
263 * Implements ike_header_t's set_exchange_type function.
264 * See #ike_header_t.set_exchange_type for description.
266 static void set_exchange_type(private_ike_header_t
*this, u_int8_t exchange_type
)
268 this->exchange_type
= exchange_type
;
272 * Implements ike_header_t's get_message_id function.
273 * See #ike_header_t.get_message_id for description.
275 static u_int32_t
get_message_id(private_ike_header_t
*this)
277 return this->message_id
;
281 * Implements ike_header_t's set_message_id function.
282 * See #ike_header_t.set_message_id for description.
284 static void set_message_id(private_ike_header_t
*this, u_int32_t message_id
)
286 this->message_id
= message_id
;
290 * Implements payload_t's and ike_header_t's destroy function.
291 * See #payload_s.destroy or ike_header_s.destroy for description.
293 static status_t
destroy(ike_header_t
*this)
295 allocator_free(this);
301 * Implements payload_t's get_encoding_rules function.
302 * See #payload_s.get_encoding_rules for description.
304 static status_t
get_encoding_rules(payload_t
*this, encoding_rule_t
**rules
, size_t *rule_count
)
306 *rules
= ike_header_encodings
;
307 *rule_count
= sizeof(ike_header_encodings
) / sizeof(encoding_rule_t
);
313 * Implements payload_t's get_type function.
314 * See #payload_s.get_type for description.
316 static payload_type_t
get_type(payload_t
*this)
322 * Implements payload_t's get_next_type function.
323 * See #payload_s.get_next_type for description.
325 static payload_type_t
get_next_type(payload_t
*this)
327 return (((private_ike_header_t
*)this)->next_payload
);
331 * Implements payload_t's get_length function.
332 * See #payload_s.get_length for description.
334 static size_t get_length(payload_t
*this)
336 return (((private_ike_header_t
*)this)->length
);
340 * Described in header
342 ike_header_t
*ike_header_create()
344 private_ike_header_t
*this = allocator_alloc_thing(private_ike_header_t
);
350 this->public.payload_interface
.get_encoding_rules
= get_encoding_rules
;
351 this->public.payload_interface
.get_length
= get_length
;
352 this->public.payload_interface
.get_next_type
= get_next_type
;
353 this->public.payload_interface
.get_type
= get_type
;
354 this->public.payload_interface
.destroy
= (status_t (*) (payload_t
*))destroy
;
355 this->public.destroy
= destroy
;
358 this->public.get_next_payload
= (u_int8_t (*) (ike_header_t
*))get_next_payload
;
359 this->public.set_next_payload
= (void (*) (ike_header_t
*,u_int8_t
))set_next_payload
;
360 this->public.get_initiator_spi
= (u_int64_t (*) (ike_header_t
*))get_initiator_spi
;
361 this->public.set_initiator_spi
= (void (*) (ike_header_t
*,u_int64_t
))set_initiator_spi
;
362 this->public.get_responder_spi
= (u_int64_t (*) (ike_header_t
*))get_responder_spi
;
363 this->public.set_responder_spi
= (void (*) (ike_header_t
*,u_int64_t
))set_responder_spi
;
364 this->public.get_maj_version
= (u_int8_t (*) (ike_header_t
*))get_maj_version
;
365 this->public.get_min_version
= (u_int8_t (*) (ike_header_t
*))get_min_version
;
366 this->public.get_response_flag
= (bool (*) (ike_header_t
*))get_response_flag
;
367 this->public.set_response_flag
= (void (*) (ike_header_t
*,bool))set_response_flag
;
368 this->public.get_version_flag
= (bool (*) (ike_header_t
*))get_version_flag
;
369 this->public.get_initiator_flag
= (bool (*) (ike_header_t
*))get_initiator_flag
;
370 this->public.set_initiator_flag
= (void (*) (ike_header_t
*,bool))set_initiator_flag
;
371 this->public.get_exchange_type
= (u_int8_t (*) (ike_header_t
*))get_exchange_type
;
372 this->public.set_exchange_type
= (void (*) (ike_header_t
*,u_int8_t
))set_exchange_type
;
373 this->public.get_message_id
= (u_int32_t (*) (ike_header_t
*))get_message_id
;
374 this->public.set_message_id
= (void (*) (ike_header_t
*,u_int32_t
))set_message_id
;
376 /* set default values of the fields */
377 this->initiator_spi
= 0;
378 this->responder_spi
= 0;
379 this->next_payload
= 0;
380 this->maj_version
= IKE_MAJOR_VERSION
;
381 this->min_version
= IKE_MINOR_VERSION
;
382 this->exchange_type
= NOT_SET
;
383 this->flags
.initiator
= TRUE
;
384 this->flags
.version
= HIGHER_VERSION_SUPPORTED_FLAG
;
385 this->flags
.response
= FALSE
;
386 this->message_id
= 0;
387 this->length
= IKE_HEADER_LENGTH
;
390 return (ike_header_t
*)this;