41040a5cff84c5fcd7de659defa1d92742c5ad97
[strongswan.git] / src / libimcv / ietf / ietf_attr_attr_request.c
1 /*
2 * Copyright (C) 2012 Andreas Steffen
3 * HSR Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include "imcv.h"
17 #include "ietf_attr_attr_request.h"
18
19 #include <pa_tnc/pa_tnc_msg.h>
20 #include <bio/bio_writer.h>
21 #include <bio/bio_reader.h>
22 #include <utils/linked_list.h>
23
24 #include <debug.h>
25
26 typedef struct private_ietf_attr_attr_request_t private_ietf_attr_attr_request_t;
27 typedef struct entry_t entry_t;
28
29 /**
30 * PA-TNC Attribute Request type (see section 4.2.1 of RFC 5792)
31 *
32 * 1 2 3
33 * 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
34 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
35 * | Reserved | PA-TNC Attribute Vendor ID |
36 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
37 * | PA-TNC Attribute Type |
38 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
39 * | Reserved | PA-TNC Attribute Vendor ID |
40 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
41 * | PA-TNC Attribute Type |
42 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
43 */
44
45 #define ATTR_REQUEST_ENTRY_SIZE 8
46
47 /**
48 * Private data of an ietf_attr_attr_request_t object.
49 */
50 struct private_ietf_attr_attr_request_t {
51
52 /**
53 * Public members of ietf_attr_attr_request_t
54 */
55 ietf_attr_attr_request_t public;
56
57 /**
58 * Vendor-specific attribute type
59 */
60 pen_type_t type;
61
62 /**
63 * Attribute value
64 */
65 chunk_t value;
66
67 /**
68 * Noskip flag
69 */
70 bool noskip_flag;
71
72 /**
73 * List of requested attribute types
74 */
75 linked_list_t *list;
76
77 /**
78 * Reference count
79 */
80 refcount_t ref;
81 };
82
83 /**
84 * Attribute type entry
85 */
86 struct entry_t {
87 pen_t vendor_id;
88 u_int32_t type;
89 };
90
91 METHOD(pa_tnc_attr_t, get_type, pen_type_t,
92 private_ietf_attr_attr_request_t *this)
93 {
94 return this->type;
95 }
96
97 METHOD(pa_tnc_attr_t, get_value, chunk_t,
98 private_ietf_attr_attr_request_t *this)
99 {
100 return this->value;
101 }
102
103 METHOD(pa_tnc_attr_t, get_noskip_flag, bool,
104 private_ietf_attr_attr_request_t *this)
105 {
106 return this->noskip_flag;
107 }
108
109 METHOD(pa_tnc_attr_t, set_noskip_flag,void,
110 private_ietf_attr_attr_request_t *this, bool noskip)
111 {
112 this->noskip_flag = noskip;
113 }
114
115 METHOD(pa_tnc_attr_t, build, void,
116 private_ietf_attr_attr_request_t *this)
117 {
118 bio_writer_t *writer;
119 enumerator_t *enumerator;
120 entry_t *entry;
121
122 if (this->value.ptr)
123 {
124 return;
125 }
126 writer = bio_writer_create(ATTR_REQUEST_ENTRY_SIZE *
127 this->list->get_count(this->list));
128
129 enumerator = this->list->create_enumerator(this->list);
130 while (enumerator->enumerate(enumerator, &entry))
131 {
132 writer->write_uint32(writer, entry->vendor_id);
133 writer->write_uint32(writer, entry->type);
134 }
135 enumerator->destroy(enumerator);
136
137 this->value = chunk_clone(writer->get_buf(writer));
138 writer->destroy(writer);
139 }
140
141 METHOD(ietf_attr_attr_request_t, add, void,
142 private_ietf_attr_attr_request_t *this, pen_t vendor_id, u_int32_t type)
143 {
144 entry_t *entry;
145
146 entry = malloc_thing(entry_t);
147 entry->vendor_id = vendor_id;
148 entry->type = type;
149 this->list->insert_last(this->list, entry);
150 }
151
152 METHOD(pa_tnc_attr_t, process, status_t,
153 private_ietf_attr_attr_request_t *this, u_int32_t *offset)
154 {
155 bio_reader_t *reader;
156 enum_name_t *pa_attr_names;
157 pen_t vendor_id;
158 u_int32_t type;
159 u_int8_t reserved;
160 int count;
161
162 count = this->value.len / ATTR_REQUEST_ENTRY_SIZE;
163 if (this->value.len != ATTR_REQUEST_ENTRY_SIZE * count)
164 {
165 DBG1(DBG_TNC, "incorrect attribute length for IETF attribute request");
166 *offset = 0;
167 return FAILED;
168 }
169
170 reader = bio_reader_create(this->value);
171 while (count--)
172 {
173 reader->read_uint8 (reader, &reserved);
174 reader->read_uint24(reader, &vendor_id);
175 reader->read_uint32(reader, &type);
176
177 pa_attr_names = imcv_pa_tnc_attributes->get_names(imcv_pa_tnc_attributes,
178 vendor_id);
179 if (pa_attr_names)
180 {
181 DBG2(DBG_TNC, " 0x%06x/0x%08x '%N/%N'", vendor_id, type,
182 pen_names, vendor_id, pa_attr_names, type);
183 }
184 else
185 {
186 DBG2(DBG_TNC, " 0x%06x/0x%08x '%N'", vendor_id, type,
187 pen_names, vendor_id);
188 }
189 add(this, vendor_id, type);
190 }
191 reader->destroy(reader);
192
193 return SUCCESS;
194 }
195
196 METHOD(pa_tnc_attr_t, get_ref, pa_tnc_attr_t*,
197 private_ietf_attr_attr_request_t *this)
198 {
199 ref_get(&this->ref);
200 return &this->public.pa_tnc_attribute;
201 }
202
203 METHOD(pa_tnc_attr_t, destroy, void,
204 private_ietf_attr_attr_request_t *this)
205 {
206 if (ref_put(&this->ref))
207 {
208 this->list->destroy_function(this->list, free);
209 free(this->value.ptr);
210 free(this);
211 }
212 }
213
214 /**
215 * Enumerate attribute type entries
216 */
217 static bool entry_filter(void *null, entry_t **entry, pen_t *vendor_id,
218 void *i2, u_int32_t *type)
219 {
220 *vendor_id = (*entry)->vendor_id;
221 *type = (*entry)->type;
222 return TRUE;
223 }
224
225 METHOD(ietf_attr_attr_request_t, create_enumerator, enumerator_t*,
226 private_ietf_attr_attr_request_t *this)
227 {
228 return enumerator_create_filter(this->list->create_enumerator(this->list),
229 (void*)entry_filter, NULL, NULL);
230 }
231
232 /**
233 * Described in header.
234 */
235 pa_tnc_attr_t *ietf_attr_attr_request_create(pen_t vendor_id, u_int32_t type)
236 {
237 private_ietf_attr_attr_request_t *this;
238
239 INIT(this,
240 .public = {
241 .pa_tnc_attribute = {
242 .get_type = _get_type,
243 .get_value = _get_value,
244 .get_noskip_flag = _get_noskip_flag,
245 .set_noskip_flag = _set_noskip_flag,
246 .build = _build,
247 .process = _process,
248 .get_ref = _get_ref,
249 .destroy = _destroy,
250 },
251 .add = _add,
252 .create_enumerator = _create_enumerator,
253 },
254 .type = { PEN_IETF, IETF_ATTR_ATTRIBUTE_REQUEST },
255 .list = linked_list_create(),
256 .ref = 1,
257 );
258 add(this, vendor_id, type);
259
260 return &this->public.pa_tnc_attribute;
261 }
262
263 /**
264 * Described in header.
265 */
266 pa_tnc_attr_t *ietf_attr_attr_request_create_from_data(chunk_t data)
267 {
268 private_ietf_attr_attr_request_t *this;
269
270 INIT(this,
271 .public = {
272 .pa_tnc_attribute = {
273 .get_type = _get_type,
274 .get_value = _get_value,
275 .build = _build,
276 .process = _process,
277 .get_ref = _get_ref,
278 .destroy = _destroy,
279 },
280 .add = _add,
281 .create_enumerator = _create_enumerator,
282 },
283 .type = { PEN_IETF,IETF_ATTR_ATTRIBUTE_REQUEST },
284 .value = chunk_clone(data),
285 .list = linked_list_create(),
286 .ref = 1,
287 );
288
289 return &this->public.pa_tnc_attribute;
290 }
291