2 * Copyright (C) 2005-2010 Martin Willi
3 * Copyright (C) 2010 revosec AG
4 * Copyright (C) 2005 Jan Hutter
5 * Hochschule fuer Technik Rapperswil
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>.
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
20 #include "cp_payload.h"
22 #include <encoding/payloads/encodings.h>
23 #include <utils/linked_list.h>
25 ENUM(config_type_names
, CFG_REQUEST
, CFG_ACK
,
32 typedef struct private_cp_payload_t private_cp_payload_t
;
35 * Private data of an cp_payload_t object.
37 struct private_cp_payload_t
{
40 * Public cp_payload_t interface.
47 u_int8_t next_payload
;
62 u_int8_t reserved_byte
[3];
65 * Length of this payload.
67 u_int16_t payload_length
;
70 * List of attributes, as configuration_attribute_t
72 linked_list_t
*attributes
;
81 * Encoding rules to parse or generate a IKEv2-CP Payload
83 * The defined offsets are the positions in a object of type
84 * private_cp_payload_t.
86 static encoding_rule_t encodings
[] = {
87 /* 1 Byte next payload type, stored in the field next_payload */
88 { U_INT_8
, offsetof(private_cp_payload_t
, next_payload
) },
89 /* the critical bit */
90 { FLAG
, offsetof(private_cp_payload_t
, critical
) },
91 /* 7 Bit reserved bits */
92 { RESERVED_BIT
, offsetof(private_cp_payload_t
, reserved_bit
[0]) },
93 { RESERVED_BIT
, offsetof(private_cp_payload_t
, reserved_bit
[1]) },
94 { RESERVED_BIT
, offsetof(private_cp_payload_t
, reserved_bit
[2]) },
95 { RESERVED_BIT
, offsetof(private_cp_payload_t
, reserved_bit
[3]) },
96 { RESERVED_BIT
, offsetof(private_cp_payload_t
, reserved_bit
[4]) },
97 { RESERVED_BIT
, offsetof(private_cp_payload_t
, reserved_bit
[5]) },
98 { RESERVED_BIT
, offsetof(private_cp_payload_t
, reserved_bit
[6]) },
99 /* Length of the whole CP payload*/
100 { PAYLOAD_LENGTH
, offsetof(private_cp_payload_t
, payload_length
) },
101 /* Proposals are stored in a proposal substructure,
102 offset points to a linked_list_t pointer */
103 { U_INT_8
, offsetof(private_cp_payload_t
, type
) },
104 /* 3 reserved bytes */
105 { RESERVED_BYTE
, offsetof(private_cp_payload_t
, reserved_byte
[0])},
106 { RESERVED_BYTE
, offsetof(private_cp_payload_t
, reserved_byte
[1])},
107 { RESERVED_BYTE
, offsetof(private_cp_payload_t
, reserved_byte
[2])},
108 { CONFIGURATION_ATTRIBUTES
, offsetof(private_cp_payload_t
, attributes
) }
113 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
114 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
115 ! Next Payload !C! RESERVED ! Payload Length !
116 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
117 ! CFG Type ! RESERVED !
118 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
120 ~ Configuration Attributes ~
122 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
125 METHOD(payload_t
, verify
, status_t
,
126 private_cp_payload_t
*this)
128 status_t status
= SUCCESS
;
129 enumerator_t
*enumerator
;
130 payload_t
*attribute
;
132 enumerator
= this->attributes
->create_enumerator(this->attributes
);
133 while (enumerator
->enumerate(enumerator
, &attribute
))
135 status
= attribute
->verify(attribute
);
136 if (status
!= SUCCESS
)
141 enumerator
->destroy(enumerator
);
145 METHOD(payload_t
, get_encoding_rules
, int,
146 private_cp_payload_t
*this, encoding_rule_t
**rules
)
149 return countof(encodings
);
152 METHOD(payload_t
, get_type
, payload_type_t
,
153 private_cp_payload_t
*this)
155 return CONFIGURATION
;
158 METHOD(payload_t
, get_next_type
, payload_type_t
,
159 private_cp_payload_t
*this)
161 return this->next_payload
;
164 METHOD(payload_t
, set_next_type
, void,
165 private_cp_payload_t
*this,payload_type_t type
)
167 this->next_payload
= type
;
171 * recompute the length of the payload.
173 static void compute_length(private_cp_payload_t
*this)
175 enumerator_t
*enumerator
;
176 payload_t
*attribute
;
178 this->payload_length
= CP_PAYLOAD_HEADER_LENGTH
;
180 enumerator
= this->attributes
->create_enumerator(this->attributes
);
181 while (enumerator
->enumerate(enumerator
, &attribute
))
183 this->payload_length
+= attribute
->get_length(attribute
);
185 enumerator
->destroy(enumerator
);
188 METHOD(payload_t
, get_length
, size_t,
189 private_cp_payload_t
*this)
191 return this->payload_length
;
194 METHOD(cp_payload_t
, create_attribute_enumerator
, enumerator_t
*,
195 private_cp_payload_t
*this)
197 return this->attributes
->create_enumerator(this->attributes
);
200 METHOD(cp_payload_t
, add_attribute
, void,
201 private_cp_payload_t
*this, configuration_attribute_t
*attribute
)
203 this->attributes
->insert_last(this->attributes
, attribute
);
204 compute_length(this);
207 METHOD(cp_payload_t
, get_config_type
, config_type_t
,
208 private_cp_payload_t
*this)
213 METHOD2(payload_t
, cp_payload_t
, destroy
, void,
214 private_cp_payload_t
*this)
216 this->attributes
->destroy_offset(this->attributes
,
217 offsetof(configuration_attribute_t
, destroy
));
222 * Described in header.
224 cp_payload_t
*cp_payload_create_type(config_type_t type
)
226 private_cp_payload_t
*this;
230 .payload_interface
= {
232 .get_encoding_rules
= _get_encoding_rules
,
233 .get_length
= _get_length
,
234 .get_next_type
= _get_next_type
,
235 .set_next_type
= _set_next_type
,
236 .get_type
= _get_type
,
239 .create_attribute_enumerator
= _create_attribute_enumerator
,
240 .add_attribute
= _add_attribute
,
241 .get_type
= _get_config_type
,
244 .next_payload
= NO_PAYLOAD
,
245 .payload_length
= CP_PAYLOAD_HEADER_LENGTH
,
246 .attributes
= linked_list_create(),
249 return &this->public;
253 * Described in header.
255 cp_payload_t
*cp_payload_create()
257 return cp_payload_create_type(CFG_REQUEST
);