Use a generic list encoding rule we can use to specify the wrapped payload type
[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 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 { U_INT_8, offsetof(private_cp_payload_t, type) },
102 /* 3 reserved bytes */
103 { RESERVED_BYTE, offsetof(private_cp_payload_t, reserved_byte[0])},
104 { RESERVED_BYTE, offsetof(private_cp_payload_t, reserved_byte[1])},
105 { RESERVED_BYTE, offsetof(private_cp_payload_t, reserved_byte[2])},
106 /* list of configuration attributes in a list */
107 { PAYLOAD_LIST + CONFIGURATION_ATTRIBUTE,
108 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, int,
146 private_cp_payload_t *this, encoding_rule_t **rules)
147 {
148 *rules = encodings;
149 return countof(encodings);
150 }
151
152 METHOD(payload_t, get_header_length, int,
153 private_cp_payload_t *this)
154 {
155 return 8;
156 }
157
158 METHOD(payload_t, get_type, payload_type_t,
159 private_cp_payload_t *this)
160 {
161 return CONFIGURATION;
162 }
163
164 METHOD(payload_t, get_next_type, payload_type_t,
165 private_cp_payload_t *this)
166 {
167 return this->next_payload;
168 }
169
170 METHOD(payload_t, set_next_type, void,
171 private_cp_payload_t *this,payload_type_t type)
172 {
173 this->next_payload = type;
174 }
175
176 /**
177 * recompute the length of the payload.
178 */
179 static void compute_length(private_cp_payload_t *this)
180 {
181 enumerator_t *enumerator;
182 payload_t *attribute;
183
184 this->payload_length = get_header_length(this);
185
186 enumerator = this->attributes->create_enumerator(this->attributes);
187 while (enumerator->enumerate(enumerator, &attribute))
188 {
189 this->payload_length += attribute->get_length(attribute);
190 }
191 enumerator->destroy(enumerator);
192 }
193
194 METHOD(payload_t, get_length, size_t,
195 private_cp_payload_t *this)
196 {
197 return this->payload_length;
198 }
199
200 METHOD(cp_payload_t, create_attribute_enumerator, enumerator_t*,
201 private_cp_payload_t *this)
202 {
203 return this->attributes->create_enumerator(this->attributes);
204 }
205
206 METHOD(cp_payload_t, add_attribute, void,
207 private_cp_payload_t *this, configuration_attribute_t *attribute)
208 {
209 this->attributes->insert_last(this->attributes, attribute);
210 compute_length(this);
211 }
212
213 METHOD(cp_payload_t, get_config_type, config_type_t,
214 private_cp_payload_t *this)
215 {
216 return this->type;
217 }
218
219 METHOD2(payload_t, cp_payload_t, destroy, void,
220 private_cp_payload_t *this)
221 {
222 this->attributes->destroy_offset(this->attributes,
223 offsetof(configuration_attribute_t, destroy));
224 free(this);
225 }
226
227 /*
228 * Described in header.
229 */
230 cp_payload_t *cp_payload_create_type(config_type_t type)
231 {
232 private_cp_payload_t *this;
233
234 INIT(this,
235 .public = {
236 .payload_interface = {
237 .verify = _verify,
238 .get_encoding_rules = _get_encoding_rules,
239 .get_header_length = _get_header_length,
240 .get_length = _get_length,
241 .get_next_type = _get_next_type,
242 .set_next_type = _set_next_type,
243 .get_type = _get_type,
244 .destroy = _destroy,
245 },
246 .create_attribute_enumerator = _create_attribute_enumerator,
247 .add_attribute = _add_attribute,
248 .get_type = _get_config_type,
249 .destroy = _destroy,
250 },
251 .next_payload = NO_PAYLOAD,
252 .payload_length = get_header_length(this),
253 .attributes = linked_list_create(),
254 .type = type,
255 );
256 return &this->public;
257 }
258
259 /*
260 * Described in header.
261 */
262 cp_payload_t *cp_payload_create()
263 {
264 return cp_payload_create_type(CFG_REQUEST);
265 }