2 * @file notify_payload.c
4 * @brief Declaration of the class notify_payload_t.
6 * An object of this type represents an IKEv2 Notify-Payload.
8 * See section 3.10 of Draft for details of this payload type.
13 * Copyright (C) 2005 Jan Hutter, Martin Willi
14 * Hochschule fuer Technik Rapperswil
16 * This program is free software; you can redistribute it and/or modify it
17 * under the terms of the GNU General Public License as published by the
18 * Free Software Foundation; either version 2 of the License, or (at your
19 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
21 * This program is distributed in the hope that it will be useful, but
22 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
23 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
30 #include "notify_payload.h"
32 #include <payloads/encodings.h>
33 #include <utils/allocator.h>
36 * Private data of an notify_payload_t Object
39 typedef struct private_notify_payload_s private_notify_payload_t
;
41 struct private_notify_payload_s
{
43 * public notify_payload_t interface
45 notify_payload_t
public;
50 u_int8_t next_payload
;
58 * Length of this payload
60 u_int16_t payload_length
;
75 u_int16_t notify_message_type
;
78 * Security parameter index (spi)
85 chunk_t notification_data
;
88 * @brief Computes the length of this payload.
90 * @param this calling private_ke_payload_t object
94 status_t (*compute_length
) (private_notify_payload_t
*this);
98 * Encoding rules to parse or generate a IKEv2-Notify Payload
100 * The defined offsets are the positions in a object of type
101 * private_notify_payload_t.
104 encoding_rule_t notify_payload_encodings
[] = {
105 /* 1 Byte next payload type, stored in the field next_payload */
106 { U_INT_8
, offsetof(private_notify_payload_t
, next_payload
) },
107 /* the critical bit */
108 { FLAG
, offsetof(private_notify_payload_t
, critical
) },
109 /* 7 Bit reserved bits, nowhere stored */
117 /* Length of the whole payload*/
118 { PAYLOAD_LENGTH
, offsetof(private_notify_payload_t
, payload_length
) },
119 /* Protocol ID as 8 bit field*/
120 { U_INT_8
, offsetof(private_notify_payload_t
, protocol_id
) },
121 /* SPI Size as 8 bit field*/
122 { SPI_SIZE
, offsetof(private_notify_payload_t
, spi_size
) },
123 /* Notify message type as 16 bit field*/
124 { U_INT_16
, offsetof(private_notify_payload_t
, notify_message_type
) },
125 /* SPI as variable length field*/
126 { SPI
, offsetof(private_notify_payload_t
, spi
) },
127 /* Key Exchange Data is from variable size */
128 { NOTIFICATION_DATA
, offsetof(private_notify_payload_t
, notification_data
) }
133 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
134 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
135 ! Next Payload !C! RESERVED ! Payload Length !
136 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
137 ! Protocol ID ! SPI Size ! Notify Message Type !
138 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
140 ~ Security Parameter Index (SPI) ~
142 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
144 ~ Notification Data ~
146 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
150 * Implements payload_t's verify function.
151 * See #payload_s.verify for description.
153 static status_t
verify(private_notify_payload_t
*this)
157 /* critical bit is set! */
160 if (this->protocol_id
> 3)
162 /* reserved for future use */
166 /* notify message types and data is not getting checked in here */
172 * Implements payload_t's get_encoding_rules function.
173 * See #payload_s.get_encoding_rules for description.
175 static status_t
get_encoding_rules(private_notify_payload_t
*this, encoding_rule_t
**rules
, size_t *rule_count
)
177 *rules
= notify_payload_encodings
;
178 *rule_count
= sizeof(notify_payload_encodings
) / sizeof(encoding_rule_t
);
183 * Implements payload_t's get_type function.
184 * See #payload_s.get_type for description.
186 static payload_type_t
get_type(private_notify_payload_t
*this)
192 * Implements payload_t's get_next_type function.
193 * See #payload_s.get_next_type for description.
195 static payload_type_t
get_next_type(private_notify_payload_t
*this)
197 return (this->next_payload
);
201 * Implements payload_t's set_next_type function.
202 * See #payload_s.set_next_type for description.
204 static status_t
set_next_type(private_notify_payload_t
*this,payload_type_t type
)
206 this->next_payload
= type
;
211 * Implements payload_t's get_length function.
212 * See #payload_s.get_length for description.
214 static size_t get_length(private_notify_payload_t
*this)
216 this->compute_length(this);
217 return this->payload_length
;
221 * Implements private_ke_payload_t's compute_length function.
222 * See #private_ke_payload_s.compute_length for description.
224 static status_t
compute_length (private_notify_payload_t
*this)
226 size_t length
= NOTIFY_PAYLOAD_HEADER_LENGTH
;
227 if (this->notification_data
.ptr
!= NULL
)
229 length
+= this->notification_data
.len
;
231 if (this->spi
.ptr
!= NULL
)
233 length
+= this->spi
.len
;
236 this->payload_length
= length
;
243 * Implements notify_payload_t's get_protocol_id function.
244 * See #notify_payload_s.get_protocol_id for description.
246 u_int8_t
get_protocol_id(private_notify_payload_t
*this)
248 return this->protocol_id
;
252 * Implements notify_payload_t's set_protocol_id function.
253 * See #notify_payload_s.set_protocol_id for description.
255 status_t
set_protocol_id(private_notify_payload_t
*this, u_int8_t protocol_id
)
257 this->protocol_id
= protocol_id
;
262 * Implements notify_payload_t's get_notification_data function.
263 * See #notify_payload_s.get_notification_data for description.
265 u_int16_t
get_notify_message_type(private_notify_payload_t
*this)
267 return this->notify_message_type
;
271 * Implements notify_payload_t's get_notification_data function.
272 * See #notify_payload_s.get_notification_data for description.
274 status_t
set_notify_message_type(private_notify_payload_t
*this, u_int16_t notify_message_type
)
276 this->notify_message_type
= notify_message_type
;
281 * Implements notify_payload_t's get_spi function.
282 * See #notify_payload_s.get_spi for description.
284 chunk_t
get_spi(private_notify_payload_t
*this)
290 * Implements notify_payload_t's set_spi function.
291 * See #notify_payload_s.set_spi for description.
293 status_t
set_spi(private_notify_payload_t
*this, chunk_t spi
)
295 /* destroy existing data first */
296 if (this->spi
.ptr
!= NULL
)
298 /* free existing value */
299 allocator_free(this->spi
.ptr
);
300 this->spi
.ptr
= NULL
;
305 this->spi
.ptr
= allocator_clone_bytes(spi
.ptr
,spi
.len
);
306 if (this->spi
.ptr
== NULL
)
310 this->spi
.len
= spi
.len
;
311 this->spi_size
= spi
.len
;
312 this->compute_length(this);
319 * Implements notify_payload_t's get_notification_data function.
320 * See #notify_payload_s.get_notification_data for description.
322 chunk_t
get_notification_data(private_notify_payload_t
*this)
324 return (this->notification_data
);
328 * Implements notify_payload_t's get_notification_data function.
329 * See #notify_payload_s.get_notification_data for description.
331 status_t
set_notification_data(private_notify_payload_t
*this, chunk_t notification_data
)
333 /* destroy existing data first */
334 if (this->notification_data
.ptr
!= NULL
)
336 /* free existing value */
337 allocator_free(this->notification_data
.ptr
);
338 this->notification_data
.ptr
= NULL
;
339 this->notification_data
.len
= 0;
343 this->notification_data
.ptr
= allocator_clone_bytes(notification_data
.ptr
,notification_data
.len
);
344 if (this->notification_data
.ptr
== NULL
)
348 this->notification_data
.len
= notification_data
.len
;
349 this->compute_length(this);
355 * Implements payload_t's and notify_payload_t's destroy function.
356 * See #payload_s.destroy or notify_payload_s.destroy for description.
358 static status_t
destroy(private_notify_payload_t
*this)
360 if (this->notification_data
.ptr
!= NULL
)
362 allocator_free(this->notification_data
.ptr
);
364 if (this->spi
.ptr
!= NULL
)
366 allocator_free(this->spi
.ptr
);
369 allocator_free(this);
374 * Described in header
376 notify_payload_t
*notify_payload_create()
378 private_notify_payload_t
*this = allocator_alloc_thing(private_notify_payload_t
);
383 /* interface functions */
384 this->public.payload_interface
.verify
= (status_t (*) (payload_t
*))verify
;
385 this->public.payload_interface
.get_encoding_rules
= (status_t (*) (payload_t
*, encoding_rule_t
**, size_t *) ) get_encoding_rules
;
386 this->public.payload_interface
.get_length
= (size_t (*) (payload_t
*)) get_length
;
387 this->public.payload_interface
.get_next_type
= (payload_type_t (*) (payload_t
*)) get_next_type
;
388 this->public.payload_interface
.set_next_type
= (status_t (*) (payload_t
*,payload_type_t
)) set_next_type
;
389 this->public.payload_interface
.get_type
= (payload_type_t (*) (payload_t
*)) get_type
;
390 this->public.payload_interface
.destroy
= (status_t (*) (payload_t
*))destroy
;
392 /* public functions */
393 this->public.get_protocol_id
= (u_int8_t (*) (notify_payload_t
*)) get_protocol_id
;
394 this->public.set_protocol_id
= (status_t (*) (notify_payload_t
*,u_int8_t
)) set_protocol_id
;
395 this->public.get_notify_message_type
= (u_int16_t (*) (notify_payload_t
*)) get_notify_message_type
;
396 this->public.set_notify_message_type
= (status_t (*) (notify_payload_t
*,u_int16_t
)) set_notify_message_type
;
397 this->public.get_spi
= (chunk_t (*) (notify_payload_t
*)) get_spi
;
398 this->public.set_spi
= (status_t (*) (notify_payload_t
*,chunk_t
)) set_spi
;
399 this->public.get_notification_data
= (chunk_t (*) (notify_payload_t
*)) get_notification_data
;
400 this->public.set_notification_data
= (status_t (*) (notify_payload_t
*,chunk_t
)) set_notification_data
;
401 this->public.destroy
= (status_t (*) (notify_payload_t
*)) destroy
;
403 /* private functions */
404 this->compute_length
= compute_length
;
406 /* set default values of the fields */
407 this->critical
= NOTIFY_PAYLOAD_CRITICAL_FLAG
;
408 this->next_payload
= NO_PAYLOAD
;
409 this->payload_length
= NOTIFY_PAYLOAD_HEADER_LENGTH
;
410 this->protocol_id
= 0;
411 this->notify_message_type
= 0;
412 this->spi
.ptr
= NULL
;
414 this->notification_data
.ptr
= NULL
;
415 this->notification_data
.len
= 0;
417 return (&(this->public));