Added member fields for reserved bits and bytes in all payloads
[strongswan.git] / src / libcharon / encoding / payloads / cp_payload.c
1 /*
2 * Copyright (C) 2005-2010 Martin Willi
3 * Copyright (C) 2010 revosec AG
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 #include <stddef.h>
19
20 #include "cp_payload.h"
21
22 #include <encoding/payloads/encodings.h>
23 #include <utils/linked_list.h>
24
25 ENUM(config_type_names, CFG_REQUEST, CFG_ACK,
26 "CFG_REQUEST",
27 "CFG_REPLY",
28 "CFG_SET",
29 "CFG_ACK",
30 );
31
32 typedef struct private_cp_payload_t private_cp_payload_t;
33
34 /**
35 * Private data of an cp_payload_t object.
36 */
37 struct private_cp_payload_t {
38
39 /**
40 * Public cp_payload_t interface.
41 */
42 cp_payload_t public;
43
44 /**
45 * Next payload type.
46 */
47 u_int8_t next_payload;
48
49 /**
50 * Critical flag.
51 */
52 bool critical;
53
54 /**
55 * Reserved bits
56 */
57 bool reserved_bit[7];
58
59 /**
60 * Reserved bytes
61 */
62 u_int8_t reserved_byte[3];
63
64 /**
65 * Length of this payload.
66 */
67 u_int16_t payload_length;
68
69 /**
70 * List of attributes, as configuration_attribute_t
71 */
72 linked_list_t *attributes;
73
74 /**
75 * Config Type.
76 */
77 u_int8_t type;
78 };
79
80 /**
81 * Encoding rules to parse or generate a IKEv2-CP Payload
82 *
83 * The defined offsets are the positions in a object of type
84 * private_cp_payload_t.
85 */
86 encoding_rule_t cp_payload_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) }
109 };
110
111 /*
112 1 2 3
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 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
119 ! !
120 ~ Configuration Attributes ~
121 ! !
122 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
123 */
124
125 METHOD(payload_t, verify, status_t,
126 private_cp_payload_t *this)
127 {
128 status_t status = SUCCESS;
129 enumerator_t *enumerator;
130 payload_t *attribute;
131
132 enumerator = this->attributes->create_enumerator(this->attributes);
133 while (enumerator->enumerate(enumerator, &attribute))
134 {
135 status = attribute->verify(attribute);
136 if (status != SUCCESS)
137 {
138 break;
139 }
140 }
141 enumerator->destroy(enumerator);
142 return status;
143 }
144
145 METHOD(payload_t, get_encoding_rules, void,
146 private_cp_payload_t *this, encoding_rule_t **rules, size_t *rule_count)
147 {
148 *rules = cp_payload_encodings;
149 *rule_count = countof(cp_payload_encodings);
150 }
151
152 METHOD(payload_t, get_type, payload_type_t,
153 private_cp_payload_t *this)
154 {
155 return CONFIGURATION;
156 }
157
158 METHOD(payload_t, get_next_type, payload_type_t,
159 private_cp_payload_t *this)
160 {
161 return this->next_payload;
162 }
163
164 METHOD(payload_t, set_next_type, void,
165 private_cp_payload_t *this,payload_type_t type)
166 {
167 this->next_payload = type;
168 }
169
170 /**
171 * recompute the length of the payload.
172 */
173 static void compute_length(private_cp_payload_t *this)
174 {
175 enumerator_t *enumerator;
176 payload_t *attribute;
177
178 this->payload_length = CP_PAYLOAD_HEADER_LENGTH;
179
180 enumerator = this->attributes->create_enumerator(this->attributes);
181 while (enumerator->enumerate(enumerator, &attribute))
182 {
183 this->payload_length += attribute->get_length(attribute);
184 }
185 enumerator->destroy(enumerator);
186 }
187
188 METHOD(payload_t, get_length, size_t,
189 private_cp_payload_t *this)
190 {
191 return this->payload_length;
192 }
193
194 METHOD(cp_payload_t, create_attribute_enumerator, enumerator_t*,
195 private_cp_payload_t *this)
196 {
197 return this->attributes->create_enumerator(this->attributes);
198 }
199
200 METHOD(cp_payload_t, add_attribute, void,
201 private_cp_payload_t *this, configuration_attribute_t *attribute)
202 {
203 this->attributes->insert_last(this->attributes, attribute);
204 compute_length(this);
205 }
206
207 METHOD(cp_payload_t, get_config_type, config_type_t,
208 private_cp_payload_t *this)
209 {
210 return this->type;
211 }
212
213 METHOD2(payload_t, cp_payload_t, destroy, void,
214 private_cp_payload_t *this)
215 {
216 this->attributes->destroy_offset(this->attributes,
217 offsetof(configuration_attribute_t, destroy));
218 free(this);
219 }
220
221 /*
222 * Described in header.
223 */
224 cp_payload_t *cp_payload_create_type(config_type_t type)
225 {
226 private_cp_payload_t *this;
227
228 INIT(this,
229 .public = {
230 .payload_interface = {
231 .verify = _verify,
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,
237 .destroy = _destroy,
238 },
239 .create_attribute_enumerator = _create_attribute_enumerator,
240 .add_attribute = _add_attribute,
241 .get_type = _get_config_type,
242 .destroy = _destroy,
243 },
244 .next_payload = NO_PAYLOAD,
245 .payload_length = CP_PAYLOAD_HEADER_LENGTH,
246 .attributes = linked_list_create(),
247 .type = type,
248 );
249 return &this->public;
250 }
251
252 /*
253 * Described in header.
254 */
255 cp_payload_t *cp_payload_create()
256 {
257 return cp_payload_create_type(CFG_REQUEST);
258 }