refactored PA-TNC attribute error handling
[strongswan.git] / src / libimcv / imv / imv_agent.c
1 /*
2 * Copyright (C) 2011 Andreas Steffen, HSR Hochschule fuer Technik Rapperswil
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * for more details.
13 */
14
15 #include "imcv.h"
16 #include "imv_agent.h"
17
18 #include <tncif_names.h>
19
20 #include <debug.h>
21 #include <threading/rwlock.h>
22
23 typedef struct private_imv_agent_t private_imv_agent_t;
24
25 /**
26 * Private data of an imv_agent_t object.
27 */
28 struct private_imv_agent_t {
29
30 /**
31 * Public members of imv_agent_t
32 */
33 imv_agent_t public;
34
35 /**
36 * name of IMV
37 */
38 const char *name;
39
40 /**
41 * message vendor ID of IMV
42 */
43 TNC_VendorID vendor_id;
44
45 /**
46 * message subtype of IMV
47 */
48 TNC_MessageSubtype subtype;
49
50 /**
51 * Maximum PA-TNC Message size
52 */
53 size_t max_msg_len;
54
55 /**
56 * ID of IMV as assigned by TNCS
57 */
58 TNC_IMVID id;
59
60 /**
61 * List of additional IMV IDs assigned by TNCS
62 */
63 linked_list_t *additional_ids;
64
65 /**
66 * list of TNCS connection entries
67 */
68 linked_list_t *connections;
69
70 /**
71 * rwlock to lock TNCS connection entries
72 */
73 rwlock_t *connection_lock;
74
75 /**
76 * Inform a TNCS about the set of message types the IMV is able to receive
77 *
78 * @param imv_id IMV ID assigned by TNCS
79 * @param supported_types list of supported message types
80 * @param type_count number of list elements
81 * @return TNC result code
82 */
83 TNC_Result (*report_message_types)(TNC_IMVID imv_id,
84 TNC_MessageTypeList supported_types,
85 TNC_UInt32 type_count);
86
87 /**
88 * Inform a TNCS about the set of message types the IMV is able to receive
89 *
90 * @param imv_id IMV ID assigned by TNCS
91 * @param supported_vids list of supported message vendor IDs
92 * @param supported_subtypes list of supported message subtypes
93 * @param type_count number of list elements
94 * @return TNC result code
95 */
96 TNC_Result (*report_message_types_long)(TNC_IMVID imv_id,
97 TNC_VendorIDList supported_vids,
98 TNC_MessageSubtypeList supported_subtypes,
99 TNC_UInt32 type_count);
100
101 /**
102 * Call when an IMV-IMC message is to be sent
103 *
104 * @param imv_id IMV ID assigned by TNCS
105 * @param connection_id network connection ID assigned by TNCS
106 * @param msg message to send
107 * @param msg_len message length in bytes
108 * @param msg_type message type
109 * @return TNC result code
110 */
111 TNC_Result (*send_message)(TNC_IMVID imv_id,
112 TNC_ConnectionID connection_id,
113 TNC_BufferReference msg,
114 TNC_UInt32 msg_len,
115 TNC_MessageType msg_type);
116
117 /**
118 * Call when an IMV-IMC message is to be sent with long message types
119 *
120 * @param imv_id IMV ID assigned by TNCS
121 * @param connection_id network connection ID assigned by TNCS
122 * @param msg_flags message flags
123 * @param msg message to send
124 * @param msg_len message length in bytes
125 * @param msg_vid message vendor ID
126 * @param msg_subtype message subtype
127 * @param dst_imc_id destination IMC ID
128 * @return TNC result code
129 */
130 TNC_Result (*send_message_long)(TNC_IMVID imv_id,
131 TNC_ConnectionID connection_id,
132 TNC_UInt32 msg_flags,
133 TNC_BufferReference msg,
134 TNC_UInt32 msg_len,
135 TNC_VendorID msg_vid,
136 TNC_MessageSubtype msg_subtype,
137 TNC_UInt32 dst_imc_id);
138
139 /**
140 * Deliver IMV Action Recommendation and IMV Evaluation Results to the TNCS
141 *
142 * @param imv_id IMV ID assigned by TNCS
143 # @param connection_id network connection ID assigned by TNCS
144 * @param rec IMV action recommendation
145 * @param eval IMV evaluation result
146 * @return TNC result code
147 */
148 TNC_Result (*provide_recommendation)(TNC_IMVID imv_id,
149 TNC_ConnectionID connection_id,
150 TNC_IMV_Action_Recommendation rec,
151 TNC_IMV_Evaluation_Result eval);
152
153 /**
154 * Get the value of an attribute associated with a connection
155 * or with the TNCS as a whole.
156 *
157 * @param imv_id IMV ID assigned by TNCS
158 * @param connection_id network connection ID assigned by TNCS
159 * @param attribute_id attribute ID
160 * @param buffer_len length of buffer in bytes
161 * @param buffer buffer
162 * @param out_value_len size in bytes of attribute stored in buffer
163 * @return TNC result code
164 */
165 TNC_Result (*get_attribute)(TNC_IMVID imv_id,
166 TNC_ConnectionID connection_id,
167 TNC_AttributeID attribute_id,
168 TNC_UInt32 buffer_len,
169 TNC_BufferReference buffer,
170 TNC_UInt32 *out_value_len);
171
172 /**
173 * Set the value of an attribute associated with a connection
174 * or with the TNCS as a whole.
175 *
176 * @param imv_id IMV ID assigned by TNCS
177 * @param connection_id network connection ID assigned by TNCS
178 * @param attribute_id attribute ID
179 * @param buffer_len length of buffer in bytes
180 * @param buffer buffer
181 * @return TNC result code
182 */
183 TNC_Result (*set_attribute)(TNC_IMVID imv_id,
184 TNC_ConnectionID connection_id,
185 TNC_AttributeID attribute_id,
186 TNC_UInt32 buffer_len,
187 TNC_BufferReference buffer);
188
189 /**
190 * Reserve an additional IMV ID
191 *
192 * @param imv_id primary IMV ID assigned by TNCS
193 * @param out_imv_id additional IMV ID assigned by TNCS
194 * @return TNC result code
195 */
196 TNC_Result (*reserve_additional_id)(TNC_IMVID imv_id,
197 TNC_UInt32 *out_imv_id);
198
199 };
200
201 METHOD(imv_agent_t, bind_functions, TNC_Result,
202 private_imv_agent_t *this, TNC_TNCS_BindFunctionPointer bind_function)
203 {
204 if (!bind_function)
205 {
206 DBG1(DBG_IMV, "TNC server failed to provide bind function");
207 return TNC_RESULT_INVALID_PARAMETER;
208 }
209 if (bind_function(this->id, "TNC_TNCS_ReportMessageTypes",
210 (void**)&this->report_message_types) != TNC_RESULT_SUCCESS)
211 {
212 this->report_message_types = NULL;
213 }
214 if (bind_function(this->id, "TNC_TNCS_ReportMessageTypesLong",
215 (void**)&this->report_message_types_long) != TNC_RESULT_SUCCESS)
216 {
217 this->report_message_types_long = NULL;
218 }
219 if (bind_function(this->id, "TNC_TNCS_RequestHandshakeRetry",
220 (void**)&this->public.request_handshake_retry) != TNC_RESULT_SUCCESS)
221 {
222 this->public.request_handshake_retry = NULL;
223 }
224 if (bind_function(this->id, "TNC_TNCS_SendMessage",
225 (void**)&this->send_message) != TNC_RESULT_SUCCESS)
226 {
227 this->send_message = NULL;
228 }
229 if (bind_function(this->id, "TNC_TNCS_SendMessageLong",
230 (void**)&this->send_message_long) != TNC_RESULT_SUCCESS)
231 {
232 this->send_message_long = NULL;
233 }
234 if (bind_function(this->id, "TNC_TNCS_ProvideRecommendation",
235 (void**)&this->provide_recommendation) != TNC_RESULT_SUCCESS)
236 {
237 this->provide_recommendation = NULL;
238 }
239 if (bind_function(this->id, "TNC_TNCS_GetAttribute",
240 (void**)&this->get_attribute) != TNC_RESULT_SUCCESS)
241 {
242 this->get_attribute = NULL;
243 }
244 if (bind_function(this->id, "TNC_TNCS_SetAttribute",
245 (void**)&this->set_attribute) != TNC_RESULT_SUCCESS)
246 {
247 this->set_attribute = NULL;
248 }
249 if (bind_function(this->id, "TNC_TNCC_ReserveAdditionalIMVID",
250 (void**)&this->reserve_additional_id) != TNC_RESULT_SUCCESS)
251 {
252 this->reserve_additional_id = NULL;
253 }
254 DBG2(DBG_IMV, "IMV %u \"%s\" provided with bind function",
255 this->id, this->name);
256
257 if (this->report_message_types_long)
258 {
259 this->report_message_types_long(this->id, &this->vendor_id,
260 &this->subtype, 1);
261 }
262 else if (this->report_message_types &&
263 this->vendor_id <= TNC_VENDORID_ANY &&
264 this->subtype <= TNC_SUBTYPE_ANY)
265 {
266 TNC_MessageType type;
267
268 type = (this->vendor_id << 8) | this->subtype;
269 this->report_message_types(this->id, &type, 1);
270 }
271 return TNC_RESULT_SUCCESS;
272 }
273
274 /**
275 * finds a connection state based on its Connection ID
276 */
277 static imv_state_t* find_connection(private_imv_agent_t *this,
278 TNC_ConnectionID id)
279 {
280 enumerator_t *enumerator;
281 imv_state_t *state, *found = NULL;
282
283 this->connection_lock->read_lock(this->connection_lock);
284 enumerator = this->connections->create_enumerator(this->connections);
285 while (enumerator->enumerate(enumerator, &state))
286 {
287 if (id == state->get_connection_id(state))
288 {
289 found = state;
290 break;
291 }
292 }
293 enumerator->destroy(enumerator);
294 this->connection_lock->unlock(this->connection_lock);
295
296 return found;
297 }
298
299 /**
300 * delete a connection state with a given Connection ID
301 */
302 static bool delete_connection(private_imv_agent_t *this, TNC_ConnectionID id)
303 {
304 enumerator_t *enumerator;
305 imv_state_t *state;
306 bool found = FALSE;
307
308 this->connection_lock->write_lock(this->connection_lock);
309 enumerator = this->connections->create_enumerator(this->connections);
310 while (enumerator->enumerate(enumerator, &state))
311 {
312 if (id == state->get_connection_id(state))
313 {
314 found = TRUE;
315 state->destroy(state);
316 this->connections->remove_at(this->connections, enumerator);
317 break;
318 }
319 }
320 enumerator->destroy(enumerator);
321 this->connection_lock->unlock(this->connection_lock);
322
323 return found;
324 }
325
326 /**
327 * Read a boolean attribute
328 */
329 static bool get_bool_attribute(private_imv_agent_t *this, TNC_ConnectionID id,
330 TNC_AttributeID attribute_id)
331 {
332 TNC_UInt32 len;
333 char buf[4];
334
335 return this->get_attribute &&
336 this->get_attribute(this->id, id, attribute_id, 4, buf, &len) ==
337 TNC_RESULT_SUCCESS && len == 1 && *buf == 0x01;
338 }
339
340 /**
341 * Read a string attribute
342 */
343 static char* get_str_attribute(private_imv_agent_t *this, TNC_ConnectionID id,
344 TNC_AttributeID attribute_id)
345 {
346 TNC_UInt32 len;
347 char buf[BUF_LEN];
348
349 if (this->get_attribute &&
350 this->get_attribute(this->id, id, attribute_id, BUF_LEN, buf, &len) ==
351 TNC_RESULT_SUCCESS && len <= BUF_LEN)
352 {
353 return strdup(buf);
354 }
355 return NULL;
356 }
357
358 METHOD(imv_agent_t, create_state, TNC_Result,
359 private_imv_agent_t *this, imv_state_t *state)
360 {
361 TNC_ConnectionID conn_id;
362 char *tnccs_p = NULL, *tnccs_v = NULL, *t_p = NULL, *t_v = NULL;
363 bool has_long = FALSE, has_excl = FALSE, has_soh = FALSE;
364
365 conn_id = state->get_connection_id(state);
366 if (find_connection(this, conn_id))
367 {
368 DBG1(DBG_IMV, "IMV %u \"%s\" already created a state for Connection ID %u",
369 this->id, this->name, conn_id);
370 state->destroy(state);
371 return TNC_RESULT_OTHER;
372 }
373
374 /* Get and display attributes from TNCS via IF-IMV */
375 has_long = get_bool_attribute(this, conn_id, TNC_ATTRIBUTEID_HAS_LONG_TYPES);
376 has_excl = get_bool_attribute(this, conn_id, TNC_ATTRIBUTEID_HAS_EXCLUSIVE);
377 has_soh = get_bool_attribute(this, conn_id, TNC_ATTRIBUTEID_HAS_SOH);
378 tnccs_p = get_str_attribute(this, conn_id, TNC_ATTRIBUTEID_IFTNCCS_PROTOCOL);
379 tnccs_v = get_str_attribute(this, conn_id, TNC_ATTRIBUTEID_IFTNCCS_VERSION);
380 t_p = get_str_attribute(this, conn_id, TNC_ATTRIBUTEID_IFT_PROTOCOL);
381 t_v = get_str_attribute(this, conn_id, TNC_ATTRIBUTEID_IFT_VERSION);
382
383 state->set_flags(state, has_long, has_excl);
384
385 DBG2(DBG_IMV, "IMV %u \"%s\" created a state for Connection ID %u: "
386 "%s %s with %slong %sexcl %ssoh over %s %s",
387 this->id, this->name, conn_id, tnccs_p ? tnccs_p:"?",
388 tnccs_v ? tnccs_v:"?", has_long ? "+":"-", has_excl ? "+":"-",
389 has_soh ? "+":"-", t_p ? t_p:"?", t_v ? t_v :"?");
390 free(tnccs_p);
391 free(tnccs_v);
392 free(t_p);
393 free(t_v);
394
395 this->connection_lock->write_lock(this->connection_lock);
396 this->connections->insert_last(this->connections, state);
397 this->connection_lock->unlock(this->connection_lock);
398 return TNC_RESULT_SUCCESS;
399 }
400
401 METHOD(imv_agent_t, delete_state, TNC_Result,
402 private_imv_agent_t *this, TNC_ConnectionID connection_id)
403 {
404 if (!delete_connection(this, connection_id))
405 {
406 DBG1(DBG_IMV, "IMV %u \"%s\" has no state for Connection ID %u",
407 this->id, this->name, connection_id);
408 return TNC_RESULT_FATAL;
409 }
410 DBG2(DBG_IMV, "IMV %u \"%s\" deleted the state of Connection ID %u",
411 this->id, this->name, connection_id);
412 return TNC_RESULT_SUCCESS;
413 }
414
415 METHOD(imv_agent_t, change_state, TNC_Result,
416 private_imv_agent_t *this, TNC_ConnectionID connection_id,
417 TNC_ConnectionState new_state,
418 imv_state_t **state_p)
419 {
420 imv_state_t *state;
421
422 switch (new_state)
423 {
424 case TNC_CONNECTION_STATE_HANDSHAKE:
425 case TNC_CONNECTION_STATE_ACCESS_ALLOWED:
426 case TNC_CONNECTION_STATE_ACCESS_ISOLATED:
427 case TNC_CONNECTION_STATE_ACCESS_NONE:
428 state = find_connection(this, connection_id);
429 if (!state)
430 {
431 DBG1(DBG_IMV, "IMV %u \"%s\" has no state for Connection ID %u",
432 this->id, this->name, connection_id);
433 return TNC_RESULT_FATAL;
434 }
435 state->change_state(state, new_state);
436 DBG2(DBG_IMV, "IMV %u \"%s\" changed state of Connection ID %u to '%N'",
437 this->id, this->name, connection_id,
438 TNC_Connection_State_names, new_state);
439 if (state_p)
440 {
441 *state_p = state;
442 }
443 break;
444 case TNC_CONNECTION_STATE_CREATE:
445 DBG1(DBG_IMV, "state '%N' should be handled by create_state()",
446 TNC_Connection_State_names, new_state);
447 return TNC_RESULT_FATAL;
448 case TNC_CONNECTION_STATE_DELETE:
449 DBG1(DBG_IMV, "state '%N' should be handled by delete_state()",
450 TNC_Connection_State_names, new_state);
451 return TNC_RESULT_FATAL;
452 default:
453 DBG1(DBG_IMV, "IMV %u \"%s\" was notified of unknown state %u "
454 "for Connection ID %u",
455 this->id, this->name, new_state, connection_id);
456 return TNC_RESULT_INVALID_PARAMETER;
457 }
458 return TNC_RESULT_SUCCESS;
459 }
460
461 METHOD(imv_agent_t, get_state, bool,
462 private_imv_agent_t *this, TNC_ConnectionID connection_id,
463 imv_state_t **state)
464 {
465 *state = find_connection(this, connection_id);
466 if (!*state)
467 {
468 DBG1(DBG_IMV, "IMV %u \"%s\" has no state for Connection ID %u",
469 this->id, this->name, connection_id);
470 return FALSE;
471 }
472 return TRUE;
473 }
474
475 METHOD(imv_agent_t, send_message, TNC_Result,
476 private_imv_agent_t *this, TNC_ConnectionID connection_id, bool excl,
477 TNC_UInt32 src_imv_id, TNC_UInt32 dst_imc_id, linked_list_t *attr_list)
478 {
479 TNC_MessageType type;
480 TNC_UInt32 msg_flags;
481 TNC_Result result = TNC_RESULT_FATAL;
482 imv_state_t *state;
483 pa_tnc_attr_t *attr;
484 pa_tnc_msg_t *pa_tnc_msg;
485 chunk_t msg;
486 enumerator_t *enumerator;
487
488 state = find_connection(this, connection_id);
489 if (!state)
490 {
491 DBG1(DBG_IMV, "IMV %u \"%s\" has no state for Connection ID %u",
492 this->id, this->name, connection_id);
493 return TNC_RESULT_FATAL;
494 }
495
496 pa_tnc_msg = pa_tnc_msg_create(this->max_msg_len);
497 while (attr_list->get_count(attr_list))
498 {
499 pa_tnc_msg = pa_tnc_msg_create(this->max_msg_len);
500
501 enumerator = attr_list->create_enumerator(attr_list);
502 while (enumerator->enumerate(enumerator, &attr))
503 {
504 if (!pa_tnc_msg->add_attribute(pa_tnc_msg, attr))
505 {
506 break;
507 }
508 attr_list->remove_at(attr_list, enumerator);
509 }
510 enumerator->destroy(enumerator);
511
512 /* build and send the PA-TNC message via the IF-IMV interface */
513 pa_tnc_msg->build(pa_tnc_msg);
514 msg = pa_tnc_msg->get_encoding(pa_tnc_msg);
515
516 if (state->has_long(state) && this->send_message_long)
517 {
518 if (!src_imv_id)
519 {
520 src_imv_id = this->id;
521 }
522 msg_flags = excl ? TNC_MESSAGE_FLAGS_EXCLUSIVE : 0;
523
524 result = this->send_message_long(src_imv_id, connection_id,
525 msg_flags, msg.ptr, msg.len, this->vendor_id,
526 this->subtype, dst_imc_id);
527 }
528 else if (this->send_message)
529 {
530 type = (this->vendor_id << 8) | this->subtype;
531
532 result = this->send_message(this->id, connection_id, msg.ptr,
533 msg.len, type);
534 }
535
536 pa_tnc_msg->destroy(pa_tnc_msg);
537
538 if (result != TNC_RESULT_SUCCESS)
539 {
540 break;
541 }
542 }
543 return result;
544 }
545
546 METHOD(imv_agent_t, set_recommendation, TNC_Result,
547 private_imv_agent_t *this, TNC_ConnectionID connection_id,
548 TNC_IMV_Action_Recommendation rec,
549 TNC_IMV_Evaluation_Result eval)
550 {
551 imv_state_t *state;
552
553 state = find_connection(this, connection_id);
554 if (!state)
555 {
556 DBG1(DBG_IMV, "IMV %u \"%s\" has no state for Connection ID %u",
557 this->id, this->name, connection_id);
558 return TNC_RESULT_FATAL;
559 }
560
561 state->set_recommendation(state, rec, eval);
562 return this->provide_recommendation(this->id, connection_id, rec, eval);
563 }
564
565 METHOD(imv_agent_t, receive_message, TNC_Result,
566 private_imv_agent_t *this, imv_state_t *state, chunk_t msg,
567 TNC_VendorID msg_vid, TNC_MessageSubtype msg_subtype,
568 TNC_UInt32 src_imc_id, TNC_UInt32 dst_imv_id, pa_tnc_msg_t **pa_tnc_msg)
569 {
570 pa_tnc_msg_t *pa_msg;
571 pa_tnc_attr_t *error_attr;
572 linked_list_t *error_attr_list;
573 enumerator_t *enumerator;
574 TNC_UInt32 src_imv_id, dst_imc_id;
575 TNC_ConnectionID connection_id;
576 TNC_Result result;
577
578 connection_id = state->get_connection_id(state);
579
580 if (state->has_long(state))
581 {
582 if (dst_imv_id != TNC_IMVID_ANY)
583 {
584 DBG2(DBG_IMV, "IMV %u \"%s\" received message for Connection ID %u "
585 "from IMC %u to IMV %u", this->id, this->name,
586 connection_id, src_imc_id, dst_imv_id);
587 }
588 else
589 {
590 DBG2(DBG_IMV, "IMV %u \"%s\" received message for Connection ID %u "
591 "from IMC %u", this->id, this->name, connection_id,
592 src_imc_id);
593 }
594 }
595 else
596 {
597 DBG2(DBG_IMV, "IMV %u \"%s\" received message for Connection ID %u",
598 this->id, this->name, connection_id);
599 }
600
601 *pa_tnc_msg = NULL;
602 pa_msg = pa_tnc_msg_create_from_data(msg);
603
604 switch (pa_msg->process(pa_msg))
605 {
606 case SUCCESS:
607 *pa_tnc_msg = pa_msg;
608 break;
609 case VERIFY_ERROR:
610 /* extract and copy by refence all error attributes */
611 error_attr_list = linked_list_create();
612
613 enumerator = pa_msg->create_error_enumerator(pa_msg);
614 while (enumerator->enumerate(enumerator, &error_attr))
615 {
616 error_attr_list->insert_last(error_attr_list,
617 error_attr->get_ref(error_attr));
618 }
619 enumerator->destroy(enumerator);
620
621 src_imv_id = (dst_imv_id == TNC_IMVID_ANY) ? this->id : dst_imv_id;
622 dst_imc_id = state->has_excl(state) ? src_imc_id : TNC_IMCID_ANY;
623
624 result = send_message(this, connection_id, state->has_excl(state),
625 src_imv_id, dst_imc_id, error_attr_list);
626
627 error_attr_list->destroy(error_attr_list);
628 pa_msg->destroy(pa_msg);
629 return result;
630 case FAILED:
631 default:
632 pa_msg->destroy(pa_msg);
633 state->set_recommendation(state,
634 TNC_IMV_ACTION_RECOMMENDATION_NO_RECOMMENDATION,
635 TNC_IMV_EVALUATION_RESULT_ERROR);
636 return this->provide_recommendation(this->id, connection_id,
637 TNC_IMV_ACTION_RECOMMENDATION_NO_RECOMMENDATION,
638 TNC_IMV_EVALUATION_RESULT_ERROR);
639 }
640 return TNC_RESULT_SUCCESS;
641 }
642
643 METHOD(imv_agent_t, provide_recommendation, TNC_Result,
644 private_imv_agent_t *this, TNC_ConnectionID connection_id)
645 {
646 imv_state_t *state;
647 TNC_IMV_Action_Recommendation rec;
648 TNC_IMV_Evaluation_Result eval;
649 TNC_UInt32 lang_len;
650 char buf[BUF_LEN];
651 chunk_t pref_lang = { buf, 0 }, reason_string, reason_lang;
652
653 state = find_connection(this, connection_id);
654 if (!state)
655 {
656 DBG1(DBG_IMV, "IMV %u \"%s\" has no state for Connection ID %u",
657 this->id, this->name, connection_id);
658 return TNC_RESULT_FATAL;
659 }
660 state->get_recommendation(state, &rec, &eval);
661
662
663 /* send a reason string if action recommendation is not allow */
664 if (rec != TNC_IMV_ACTION_RECOMMENDATION_ALLOW)
665 {
666 /* check if there a preferred language has been requested */
667 if (this->get_attribute &&
668 this->get_attribute(this->id, connection_id,
669 TNC_ATTRIBUTEID_PREFERRED_LANGUAGE, BUF_LEN,
670 buf, &lang_len) == TNC_RESULT_SUCCESS &&
671 lang_len <= BUF_LEN)
672 {
673 pref_lang.len = lang_len;
674 DBG2(DBG_IMV, "preferred language is '%.*s'",
675 pref_lang.len, pref_lang.ptr);
676 }
677
678 /* find a reason string for the preferred or default language and set it */
679 if (this->set_attribute &&
680 state->get_reason_string(state, pref_lang, &reason_string,
681 &reason_lang))
682 {
683 this->set_attribute(this->id, connection_id,
684 TNC_ATTRIBUTEID_REASON_STRING,
685 reason_string.len, reason_string.ptr);
686 this->set_attribute(this->id, connection_id,
687 TNC_ATTRIBUTEID_REASON_LANGUAGE,
688 reason_lang.len, reason_lang.ptr);
689 }
690 }
691
692 return this->provide_recommendation(this->id, connection_id, rec, eval);
693 }
694
695 METHOD(imv_agent_t, reserve_additional_ids, TNC_Result,
696 private_imv_agent_t *this, int count)
697 {
698 TNC_Result result;
699 TNC_UInt32 id;
700 void *pointer;
701
702 if (!this->reserve_additional_id)
703 {
704 DBG1(DBG_IMV, "IMV %u \"%s\" did not detect the capability to reserve "
705 "additional IMV IDs from the TNCS", this->id, this->name);
706 return TNC_RESULT_ILLEGAL_OPERATION;
707 }
708 while (count > 0)
709 {
710 result = this->reserve_additional_id(this->id, &id);
711 if (result != TNC_RESULT_SUCCESS)
712 {
713 DBG1(DBG_IMV, "IMV %u \"%s\" failed to reserve %d additional IMV IDs",
714 this->id, this->name, count);
715 return result;
716 }
717 count--;
718
719 /* store the scalar value in the pointer */
720 pointer = (void*)id;
721 this->additional_ids->insert_last(this->additional_ids, pointer);
722 DBG2(DBG_IMV, "IMV %u \"%s\" reserved additional ID %u",
723 this->id, this->name, id);
724 }
725 return TNC_RESULT_SUCCESS;
726 }
727
728 METHOD(imv_agent_t, count_additional_ids, int,
729 private_imv_agent_t *this)
730 {
731 return this->additional_ids->get_count(this->additional_ids);
732 }
733
734 METHOD(imv_agent_t, create_id_enumerator, enumerator_t*,
735 private_imv_agent_t *this)
736 {
737 return this->additional_ids->create_enumerator(this->additional_ids);
738 }
739
740 METHOD(imv_agent_t, destroy, void,
741 private_imv_agent_t *this)
742 {
743 DBG1(DBG_IMV, "IMV %u \"%s\" terminated", this->id, this->name);
744 this->additional_ids->destroy(this->additional_ids);
745 this->connections->destroy_offset(this->connections,
746 offsetof(imv_state_t, destroy));
747 this->connection_lock->destroy(this->connection_lock);
748 free(this);
749
750 /* decrease the reference count or terminate */
751 libimcv_deinit();
752 }
753
754 /**
755 * Described in header.
756 */
757 imv_agent_t *imv_agent_create(const char *name,
758 pen_t vendor_id, u_int32_t subtype,
759 TNC_IMVID id, TNC_Version *actual_version)
760 {
761 private_imv_agent_t *this;
762
763 /* initialize or increase the reference count */
764 if (!libimcv_init())
765 {
766 return NULL;
767 }
768
769 INIT(this,
770 .public = {
771 .bind_functions = _bind_functions,
772 .create_state = _create_state,
773 .delete_state = _delete_state,
774 .change_state = _change_state,
775 .get_state = _get_state,
776 .send_message = _send_message,
777 .receive_message = _receive_message,
778 .set_recommendation = _set_recommendation,
779 .provide_recommendation = _provide_recommendation,
780 .reserve_additional_ids = _reserve_additional_ids,
781 .count_additional_ids = _count_additional_ids,
782 .create_id_enumerator = _create_id_enumerator,
783 .destroy = _destroy,
784 },
785 .name = name,
786 .vendor_id = vendor_id,
787 .subtype = subtype,
788 .max_msg_len = 65490,
789 .id = id,
790 .additional_ids = linked_list_create(),
791 .connections = linked_list_create(),
792 .connection_lock = rwlock_create(RWLOCK_TYPE_DEFAULT),
793 );
794
795 *actual_version = TNC_IFIMV_VERSION_1;
796 DBG1(DBG_IMV, "IMV %u \"%s\" initialized", this->id, this->name);
797
798 return &this->public;
799 }
800
801