Merge branch 'opaque-ports'
[strongswan.git] / src / libimcv / imc / imc_msg.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 "imc_msg.h"
17
18 #include "ietf/ietf_attr.h"
19 #include "ietf/ietf_attr_assess_result.h"
20 #include "ietf/ietf_attr_remediation_instr.h"
21
22 #include <tncif_names.h>
23
24 #include <pen/pen.h>
25 #include <collections/linked_list.h>
26 #include <utils/debug.h>
27
28 typedef struct private_imc_msg_t private_imc_msg_t;
29
30 /**
31 * Private data of a imc_msg_t object.
32 *
33 */
34 struct private_imc_msg_t {
35
36 /**
37 * Public imc_msg_t interface.
38 */
39 imc_msg_t public;
40
41 /**
42 * Connection ID
43 */
44 TNC_ConnectionID connection_id;
45
46 /**
47 * source ID
48 */
49 TNC_UInt32 src_id;
50
51 /**
52 * destination ID
53 */
54 TNC_UInt32 dst_id;
55
56 /**
57 * PA-TNC message type
58 */
59 pen_type_t msg_type;
60
61 /**
62 * List of PA-TNC attributes to be sent
63 */
64 linked_list_t *attr_list;
65
66 /**
67 * PA-TNC message
68 */
69 pa_tnc_msg_t *pa_msg;
70
71 /**
72 * Assigned IMC agent
73 */
74 imc_agent_t *agent;
75
76 /**
77 * Assigned IMC state
78 */
79 imc_state_t *state;
80 };
81
82 METHOD(imc_msg_t, get_src_id, TNC_UInt32,
83 private_imc_msg_t *this)
84 {
85 return this->src_id;
86 }
87
88 METHOD(imc_msg_t, get_dst_id, TNC_UInt32,
89 private_imc_msg_t *this)
90 {
91 return this->dst_id;
92 }
93
94 METHOD(imc_msg_t, get_msg_type, pen_type_t,
95 private_imc_msg_t *this)
96 {
97 return this->msg_type;
98 }
99
100 METHOD(imc_msg_t, send_, TNC_Result,
101 private_imc_msg_t *this, bool excl)
102 {
103 pa_tnc_msg_t *pa_tnc_msg;
104 pa_tnc_attr_t *attr;
105 TNC_UInt32 msg_flags;
106 TNC_MessageType msg_type;
107 bool attr_added;
108 chunk_t msg;
109 enumerator_t *enumerator;
110 TNC_Result result = TNC_RESULT_SUCCESS;
111
112 while (this->attr_list->get_count(this->attr_list))
113 {
114 pa_tnc_msg = pa_tnc_msg_create(this->state->get_max_msg_len(this->state));
115 attr_added = FALSE;
116
117 enumerator = this->attr_list->create_enumerator(this->attr_list);
118 while (enumerator->enumerate(enumerator, &attr))
119 {
120 if (pa_tnc_msg->add_attribute(pa_tnc_msg, attr))
121 {
122 attr_added = TRUE;
123 }
124 else
125 {
126 if (attr_added)
127 {
128 break;
129 }
130 else
131 {
132 DBG1(DBG_IMC, "PA-TNC attribute too large to send, deleted");
133 attr->destroy(attr);
134 }
135 }
136 this->attr_list->remove_at(this->attr_list, enumerator);
137 }
138 enumerator->destroy(enumerator);
139
140 /* build and send the PA-TNC message via the IF-IMC interface */
141 if (!pa_tnc_msg->build(pa_tnc_msg))
142 {
143 pa_tnc_msg->destroy(pa_tnc_msg);
144 return TNC_RESULT_FATAL;
145 }
146 msg = pa_tnc_msg->get_encoding(pa_tnc_msg);
147 DBG3(DBG_IMC, "created PA-TNC message: %B", &msg);
148
149 if (this->state->has_long(this->state) && this->agent->send_message_long)
150 {
151 excl = excl && this->state->has_excl(this->state) &&
152 this->dst_id != TNC_IMVID_ANY;
153 msg_flags = excl ? TNC_MESSAGE_FLAGS_EXCLUSIVE : 0;
154 result = this->agent->send_message_long(this->src_id,
155 this->connection_id, msg_flags, msg.ptr, msg.len,
156 this->msg_type.vendor_id, this->msg_type.type,
157 this->dst_id);
158 }
159 else if (this->agent->send_message)
160 {
161 msg_type = (this->msg_type.vendor_id << 8) |
162 (this->msg_type.type & 0x000000ff);
163 result = this->agent->send_message(this->src_id, this->connection_id,
164 msg.ptr, msg.len, msg_type);
165 }
166
167 pa_tnc_msg->destroy(pa_tnc_msg);
168
169 if (result != TNC_RESULT_SUCCESS)
170 {
171 break;
172 }
173 }
174 return result;
175 }
176
177 /**
178 * Print a clearly visible assessment header to the log
179 */
180 static void print_assessment_header(const char *name, TNC_UInt32 id, bool *first)
181 {
182 if (*first)
183 {
184 DBG1(DBG_IMC, "***** assessment of IMC %u \"%s\" *****", id, name);
185 *first = FALSE;
186 }
187 }
188
189 /**
190 * Print a clearly visible assessment trailer to the log
191 */
192 static void print_assessment_trailer(bool first)
193 {
194 if (!first)
195 {
196 DBG1(DBG_IMC, "***** end of assessment *****");
197 }
198 }
199
200 METHOD(imc_msg_t, receive, TNC_Result,
201 private_imc_msg_t *this, bool *fatal_error)
202 {
203 TNC_UInt32 target_imc_id;
204 enumerator_t *enumerator;
205 pa_tnc_attr_t *attr;
206 pen_type_t attr_type;
207 chunk_t msg;
208 bool first = TRUE;
209
210 if (this->state->has_long(this->state))
211 {
212 if (this->dst_id != TNC_IMCID_ANY)
213 {
214 DBG2(DBG_IMC, "IMC %u \"%s\" received message for Connection ID %u "
215 "from IMV %u to IMC %u",
216 this->agent->get_id(this->agent),
217 this->agent->get_name(this->agent),
218 this->connection_id, this->src_id, this->dst_id);
219 }
220 else
221 {
222 DBG2(DBG_IMC, "IMC %u \"%s\" received message for Connection ID %u "
223 "from IMV %u", this->agent->get_id(this->agent),
224 this->agent->get_name(this->agent),
225 this->connection_id, this->src_id);
226 }
227 }
228 else
229 {
230 DBG2(DBG_IMC, "IMC %u \"%s\" received message for Connection ID %u",
231 this->agent->get_id(this->agent),
232 this->agent->get_name(this->agent),
233 this->connection_id);
234 }
235 msg = this->pa_msg->get_encoding(this->pa_msg);
236 DBG3(DBG_IMC, "%B", &msg);
237
238 switch (this->pa_msg->process(this->pa_msg))
239 {
240 case SUCCESS:
241 break;
242 case VERIFY_ERROR:
243 {
244 imc_msg_t *error_msg;
245 TNC_Result result;
246
247 error_msg = imc_msg_create_as_reply(&this->public);
248
249 /* extract and copy by reference all error attributes */
250 enumerator = this->pa_msg->create_error_enumerator(this->pa_msg);
251 while (enumerator->enumerate(enumerator, &attr))
252 {
253 error_msg->add_attribute(error_msg, attr->get_ref(attr));
254 }
255 enumerator->destroy(enumerator);
256
257 /*
258 * send the PA-TNC message containing all error attributes
259 * with the excl flag set
260 */
261 result = error_msg->send(error_msg, TRUE);
262 error_msg->destroy(error_msg);
263 return result;
264 }
265 case FAILED:
266 default:
267 return TNC_RESULT_FATAL;
268 }
269
270 /* determine target IMC ID */
271 target_imc_id = (this->dst_id != TNC_IMCID_ANY) ?
272 this->dst_id : this->agent->get_id(this->agent);
273
274 /* preprocess any received IETF standard error attributes */
275 *fatal_error = this->pa_msg->process_ietf_std_errors(this->pa_msg);
276
277 /* preprocess any received IETF assessment result attribute */
278 enumerator = this->pa_msg->create_attribute_enumerator(this->pa_msg);
279 while (enumerator->enumerate(enumerator, &attr))
280 {
281 attr_type = attr->get_type(attr);
282
283 if (attr_type.vendor_id != PEN_IETF)
284 {
285 continue;
286 }
287 if (attr_type.type == IETF_ATTR_ASSESSMENT_RESULT)
288 {
289 ietf_attr_assess_result_t *attr_cast;
290 TNC_IMV_Evaluation_Result result;
291
292 attr_cast = (ietf_attr_assess_result_t*)attr;
293 result = attr_cast->get_result(attr_cast);
294 this->state->set_result(this->state, target_imc_id, result);
295
296 print_assessment_header(this->agent->get_name(this->agent),
297 target_imc_id, &first);
298 DBG1(DBG_IMC, "assessment result is '%N'",
299 TNC_IMV_Evaluation_Result_names, result);
300 }
301 else if (attr_type.type == IETF_ATTR_REMEDIATION_INSTRUCTIONS)
302 {
303 ietf_attr_remediation_instr_t *attr_cast;
304 pen_type_t parameters_type;
305 chunk_t parameters, string, lang_code;
306
307 attr_cast = (ietf_attr_remediation_instr_t*)attr;
308 parameters_type = attr_cast->get_parameters_type(attr_cast);
309 parameters = attr_cast->get_parameters(attr_cast);
310
311 print_assessment_header(this->agent->get_name(this->agent),
312 target_imc_id, &first);
313 if (parameters_type.vendor_id == PEN_IETF)
314 {
315 switch (parameters_type.type)
316 {
317 case IETF_REMEDIATION_PARAMETERS_URI:
318 DBG1(DBG_IMC, "remediation uri: %.*s",
319 parameters.len, parameters.ptr);
320 break;
321 case IETF_REMEDIATION_PARAMETERS_STRING:
322 string = attr_cast->get_string(attr_cast, &lang_code);
323 DBG1(DBG_IMC, "remediation string: [%.*s]\n%.*s",
324 lang_code.len, lang_code.ptr,
325 string.len, string.ptr);
326 break;
327 default:
328 DBG1(DBG_IMC, "remediation parameters: %B", &parameters);
329 }
330 }
331 else
332 {
333 DBG1(DBG_IMC, "remediation parameters: %B", &parameters);
334 }
335 }
336 }
337 enumerator->destroy(enumerator);
338
339 print_assessment_trailer(first);
340
341 return TNC_RESULT_SUCCESS;
342 }
343
344 METHOD(imc_msg_t, add_attribute, void,
345 private_imc_msg_t *this, pa_tnc_attr_t *attr)
346 {
347 this->attr_list->insert_last(this->attr_list, attr);
348 }
349
350 METHOD(imc_msg_t, create_attribute_enumerator, enumerator_t*,
351 private_imc_msg_t *this)
352 {
353 return this->pa_msg->create_attribute_enumerator(this->pa_msg);
354 }
355
356 METHOD(imc_msg_t, get_encoding, chunk_t,
357 private_imc_msg_t *this)
358 {
359 if (this->pa_msg)
360 {
361 return this->pa_msg->get_encoding(this->pa_msg);
362 }
363 return chunk_empty;
364 }
365
366 METHOD(imc_msg_t, destroy, void,
367 private_imc_msg_t *this)
368 {
369 this->attr_list->destroy_offset(this->attr_list,
370 offsetof(pa_tnc_attr_t, destroy));
371 DESTROY_IF(this->pa_msg);
372 free(this);
373 }
374
375 /**
376 * See header
377 */
378 imc_msg_t *imc_msg_create(imc_agent_t *agent, imc_state_t *state,
379 TNC_ConnectionID connection_id,
380 TNC_UInt32 src_id, TNC_UInt32 dst_id,
381 pen_type_t msg_type)
382 {
383 private_imc_msg_t *this;
384
385 INIT(this,
386 .public = {
387 .get_src_id = _get_src_id,
388 .get_dst_id = _get_dst_id,
389 .get_msg_type = _get_msg_type,
390 .send = _send_,
391 .receive = _receive,
392 .add_attribute = _add_attribute,
393 .create_attribute_enumerator = _create_attribute_enumerator,
394 .get_encoding = _get_encoding,
395 .destroy = _destroy,
396 },
397 .connection_id = connection_id,
398 .src_id = src_id,
399 .dst_id = dst_id,
400 .msg_type = msg_type,
401 .attr_list = linked_list_create(),
402 .agent = agent,
403 .state = state,
404 );
405
406 return &this->public;
407 }
408
409 /**
410 * See header
411 */
412 imc_msg_t* imc_msg_create_as_reply(imc_msg_t *msg)
413 {
414 private_imc_msg_t *in;
415 TNC_UInt32 src_id;
416
417 in = (private_imc_msg_t*)msg;
418 src_id = (in->dst_id != TNC_IMCID_ANY) ?
419 in->dst_id : in->agent->get_id(in->agent);
420
421 return imc_msg_create(in->agent, in->state, in->connection_id, src_id,
422 in->src_id, in->msg_type);
423 }
424
425 /**
426 * See header
427 */
428 imc_msg_t *imc_msg_create_from_data(imc_agent_t *agent, imc_state_t *state,
429 TNC_ConnectionID connection_id,
430 TNC_MessageType msg_type,
431 chunk_t msg)
432 {
433 TNC_VendorID msg_vid;
434 TNC_MessageSubtype msg_subtype;
435
436 msg_vid = msg_type >> 8;
437 msg_subtype = msg_type & TNC_SUBTYPE_ANY;
438
439 return imc_msg_create_from_long_data(agent, state, connection_id,
440 TNC_IMVID_ANY, agent->get_id(agent),
441 msg_vid, msg_subtype, msg);
442 }
443
444 /**
445 * See header
446 */
447 imc_msg_t *imc_msg_create_from_long_data(imc_agent_t *agent, imc_state_t *state,
448 TNC_ConnectionID connection_id,
449 TNC_UInt32 src_id,
450 TNC_UInt32 dst_id,
451 TNC_VendorID msg_vid,
452 TNC_MessageSubtype msg_subtype,
453 chunk_t msg)
454 {
455 private_imc_msg_t *this;
456
457 this = (private_imc_msg_t*)imc_msg_create(agent, state,
458 connection_id, src_id, dst_id,
459 pen_type_create(msg_vid, msg_subtype));
460 this->pa_msg = pa_tnc_msg_create_from_data(msg);
461
462 return &this->public;
463 }