2 * Copyright (C) 2011 Andreas Steffen, HSR Hochschule fuer Technik Rapperswil
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>.
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
16 #include "imv_agent.h"
18 #include <tncif_names.h>
21 #include <utils/linked_list.h>
22 #include <threading/rwlock.h>
24 typedef struct private_imv_agent_t private_imv_agent_t
;
27 * Private data of an imv_agent_t object.
29 struct private_imv_agent_t
{
32 * Public members of imv_agent_t
42 * message vendor ID of IMV
44 TNC_VendorID vendor_id
;
47 * message subtype of IMV
49 TNC_MessageSubtype subtype
;
52 * ID of IMV as assigned by TNCS
57 * list of TNCS connection entries
59 linked_list_t
*connections
;
62 * rwlock to lock TNCS connection entries
64 rwlock_t
*connection_lock
;
67 * Inform a TNCS about the set of message types the IMV is able to receive
69 * @param imv_id IMV ID assigned by TNCS
70 * @param supported_types list of supported message types
71 * @param type_count number of list elements
72 * @return TNC result code
74 TNC_Result (*report_message_types
)(TNC_IMVID imv_id
,
75 TNC_MessageTypeList supported_types
,
76 TNC_UInt32 type_count
);
79 * Inform a TNCS about the set of message types the IMV is able to receive
81 * @param imv_id IMV ID assigned by TNCS
82 * @param supported_vids list of supported message vendor IDs
83 * @param supported_subtypes list of supported message subtypes
84 * @param type_count number of list elements
85 * @return TNC result code
87 TNC_Result (*report_message_types_long
)(TNC_IMVID imv_id
,
88 TNC_VendorIDList supported_vids
,
89 TNC_MessageSubtypeList supported_subtypes
,
90 TNC_UInt32 type_count
);
93 * Call when an IMV-IMC message is to be sent
95 * @param imv_id IMV ID assigned by TNCS
96 * @param connection_id network connection ID assigned by TNCS
97 * @param msg message to send
98 * @param msg_len message length in bytes
99 * @param msg_type message type
100 * @return TNC result code
102 TNC_Result (*send_message
)(TNC_IMVID imv_id
,
103 TNC_ConnectionID connection_id
,
104 TNC_BufferReference msg
,
106 TNC_MessageType msg_type
);
109 * Deliver IMV Action Recommendation and IMV Evaluation Results to the TNCS
111 * @param imv_id IMV ID assigned by TNCS
112 # @param connection_id network connection ID assigned by TNCS
113 * @param rec IMV action recommendation
114 * @param eval IMV evaluation result
115 * @return TNC result code
117 TNC_Result (*provide_recommendation
)(TNC_IMVID imv_id
,
118 TNC_ConnectionID connection_id
,
119 TNC_IMV_Action_Recommendation rec
,
120 TNC_IMV_Evaluation_Result eval
);
123 * Get the value of an attribute associated with a connection
124 * or with the TNCS as a whole.
126 * @param imv_id IMV ID assigned by TNCS
127 * @param connection_id network connection ID assigned by TNCS
128 * @param attribute_id attribute ID
129 * @param buffer_len length of buffer in bytes
130 * @param buffer buffer
131 * @param out_value_len size in bytes of attribute stored in buffer
132 * @return TNC result code
134 TNC_Result (*get_attribute
)(TNC_IMVID imv_id
,
135 TNC_ConnectionID connection_id
,
136 TNC_AttributeID attribute_id
,
137 TNC_UInt32 buffer_len
,
138 TNC_BufferReference buffer
,
139 TNC_UInt32
*out_value_len
);
142 * Set the value of an attribute associated with a connection
143 * or with the TNCS as a whole.
145 * @param imv_id IMV ID assigned by TNCS
146 * @param connection_id network connection ID assigned by TNCS
147 * @param attribute_id attribute ID
148 * @param buffer_len length of buffer in bytes
149 * @param buffer buffer
150 * @return TNC result code
152 TNC_Result (*set_attribute
)(TNC_IMVID imv_id
,
153 TNC_ConnectionID connection_id
,
154 TNC_AttributeID attribute_id
,
155 TNC_UInt32 buffer_len
,
156 TNC_BufferReference buffer
);
159 METHOD(imv_agent_t
, bind_functions
, TNC_Result
,
160 private_imv_agent_t
*this, TNC_TNCS_BindFunctionPointer bind_function
)
164 DBG1(DBG_IMV
, "TNC server failed to provide bind function");
165 return TNC_RESULT_INVALID_PARAMETER
;
167 if (bind_function(this->id
, "TNC_TNCS_ReportMessageTypes",
168 (void**)&this->report_message_types
) != TNC_RESULT_SUCCESS
)
170 this->report_message_types
= NULL
;
172 if (bind_function(this->id
, "TNC_TNCS_ReportMessageTypesLong",
173 (void**)&this->report_message_types_long
) != TNC_RESULT_SUCCESS
)
175 this->report_message_types_long
= NULL
;
177 if (bind_function(this->id
, "TNC_TNCS_RequestHandshakeRetry",
178 (void**)&this->public.request_handshake_retry
) != TNC_RESULT_SUCCESS
)
180 this->public.request_handshake_retry
= NULL
;
182 if (bind_function(this->id
, "TNC_TNCS_SendMessage",
183 (void**)&this->send_message
) != TNC_RESULT_SUCCESS
)
185 this->send_message
= NULL
;
187 if (bind_function(this->id
, "TNC_TNCS_ProvideRecommendation",
188 (void**)&this->provide_recommendation
) != TNC_RESULT_SUCCESS
)
190 this->provide_recommendation
= NULL
;
192 if (bind_function(this->id
, "TNC_TNCS_GetAttribute",
193 (void**)&this->get_attribute
) != TNC_RESULT_SUCCESS
)
195 this->get_attribute
= NULL
;
197 if (bind_function(this->id
, "TNC_TNCS_SetAttribute",
198 (void**)&this->set_attribute
) != TNC_RESULT_SUCCESS
)
200 this->set_attribute
= NULL
;
202 DBG2(DBG_IMV
, "IMV %u \"%s\" provided with bind function",
203 this->id
, this->name
);
205 if (this->report_message_types_long
)
207 this->report_message_types_long(this->id
, &this->vendor_id
,
210 else if (this->report_message_types
&&
211 this->vendor_id
<= TNC_VENDORID_ANY
&&
212 this->subtype
<= TNC_SUBTYPE_ANY
)
214 TNC_MessageType type
;
216 type
= (this->vendor_id
<< 8) | this->subtype
;
217 this->report_message_types(this->id
, &type
, 1);
219 return TNC_RESULT_SUCCESS
;
223 * finds a connection state based on its Connection ID
225 static imv_state_t
* find_connection(private_imv_agent_t
*this,
228 enumerator_t
*enumerator
;
229 imv_state_t
*state
, *found
= NULL
;
231 this->connection_lock
->read_lock(this->connection_lock
);
232 enumerator
= this->connections
->create_enumerator(this->connections
);
233 while (enumerator
->enumerate(enumerator
, &state
))
235 if (id
== state
->get_connection_id(state
))
241 enumerator
->destroy(enumerator
);
242 this->connection_lock
->unlock(this->connection_lock
);
248 * delete a connection state with a given Connection ID
250 static bool delete_connection(private_imv_agent_t
*this, TNC_ConnectionID id
)
252 enumerator_t
*enumerator
;
256 this->connection_lock
->write_lock(this->connection_lock
);
257 enumerator
= this->connections
->create_enumerator(this->connections
);
258 while (enumerator
->enumerate(enumerator
, &state
))
260 if (id
== state
->get_connection_id(state
))
263 state
->destroy(state
);
264 this->connections
->remove_at(this->connections
, enumerator
);
268 enumerator
->destroy(enumerator
);
269 this->connection_lock
->unlock(this->connection_lock
);
274 METHOD(imv_agent_t
, create_state
, TNC_Result
,
275 private_imv_agent_t
*this, imv_state_t
*state
)
277 TNC_ConnectionID connection_id
;
279 connection_id
= state
->get_connection_id(state
);
280 if (find_connection(this, connection_id
))
282 DBG1(DBG_IMV
, "IMV %u \"%s\" already created a state for Connection ID %u",
283 this->id
, this->name
, connection_id
);
284 state
->destroy(state
);
285 return TNC_RESULT_OTHER
;
287 this->connection_lock
->write_lock(this->connection_lock
);
288 this->connections
->insert_last(this->connections
, state
);
289 this->connection_lock
->unlock(this->connection_lock
);
290 DBG2(DBG_IMV
, "IMV %u \"%s\" created a state for Connection ID %u",
291 this->id
, this->name
, connection_id
);
292 return TNC_RESULT_SUCCESS
;
295 METHOD(imv_agent_t
, delete_state
, TNC_Result
,
296 private_imv_agent_t
*this, TNC_ConnectionID connection_id
)
298 if (!delete_connection(this, connection_id
))
300 DBG1(DBG_IMV
, "IMV %u \"%s\" has no state for Connection ID %u",
301 this->id
, this->name
, connection_id
);
302 return TNC_RESULT_FATAL
;
304 DBG2(DBG_IMV
, "IMV %u \"%s\" deleted the state of Connection ID %u",
305 this->id
, this->name
, connection_id
);
306 return TNC_RESULT_SUCCESS
;
309 METHOD(imv_agent_t
, change_state
, TNC_Result
,
310 private_imv_agent_t
*this, TNC_ConnectionID connection_id
,
311 TNC_ConnectionState new_state
,
312 imv_state_t
**state_p
)
318 case TNC_CONNECTION_STATE_HANDSHAKE
:
319 case TNC_CONNECTION_STATE_ACCESS_ALLOWED
:
320 case TNC_CONNECTION_STATE_ACCESS_ISOLATED
:
321 case TNC_CONNECTION_STATE_ACCESS_NONE
:
322 state
= find_connection(this, connection_id
);
325 DBG1(DBG_IMV
, "IMV %u \"%s\" has no state for Connection ID %u",
326 this->id
, this->name
, connection_id
);
327 return TNC_RESULT_FATAL
;
329 state
->change_state(state
, new_state
);
330 DBG2(DBG_IMV
, "IMV %u \"%s\" changed state of Connection ID %u to '%N'",
331 this->id
, this->name
, connection_id
,
332 TNC_Connection_State_names
, new_state
);
338 case TNC_CONNECTION_STATE_CREATE
:
339 DBG1(DBG_IMV
, "state '%N' should be handled by create_state()",
340 TNC_Connection_State_names
, new_state
);
341 return TNC_RESULT_FATAL
;
342 case TNC_CONNECTION_STATE_DELETE
:
343 DBG1(DBG_IMV
, "state '%N' should be handled by delete_state()",
344 TNC_Connection_State_names
, new_state
);
345 return TNC_RESULT_FATAL
;
347 DBG1(DBG_IMV
, "IMV %u \"%s\" was notified of unknown state %u "
348 "for Connection ID %u",
349 this->id
, this->name
, new_state
, connection_id
);
350 return TNC_RESULT_INVALID_PARAMETER
;
352 return TNC_RESULT_SUCCESS
;
355 METHOD(imv_agent_t
, get_state
, bool,
356 private_imv_agent_t
*this, TNC_ConnectionID connection_id
,
359 *state
= find_connection(this, connection_id
);
362 DBG1(DBG_IMV
, "IMV %u \"%s\" has no state for Connection ID %u",
363 this->id
, this->name
, connection_id
);
369 METHOD(imv_agent_t
, send_message
, TNC_Result
,
370 private_imv_agent_t
*this, TNC_ConnectionID connection_id
, chunk_t msg
)
372 TNC_MessageType type
;
374 if (!this->send_message
)
376 return TNC_RESULT_FATAL
;
378 type
= (this->vendor_id
<< 8) | this->subtype
;
379 return this->send_message(this->id
, connection_id
, msg
.ptr
, msg
.len
, type
);
382 METHOD(imv_agent_t
, set_recommendation
, TNC_Result
,
383 private_imv_agent_t
*this, TNC_ConnectionID connection_id
,
384 TNC_IMV_Action_Recommendation rec
,
385 TNC_IMV_Evaluation_Result eval
)
389 state
= find_connection(this, connection_id
);
392 DBG1(DBG_IMV
, "IMV %u \"%s\" has no state for Connection ID %u",
393 this->id
, this->name
, connection_id
);
394 return TNC_RESULT_FATAL
;
397 state
->set_recommendation(state
, rec
, eval
);
398 return this->provide_recommendation(this->id
, connection_id
, rec
, eval
);
401 METHOD(imv_agent_t
, receive_message
, TNC_Result
,
402 private_imv_agent_t
*this, TNC_ConnectionID connection_id
, chunk_t msg
,
403 TNC_MessageType msg_type
, pa_tnc_msg_t
**pa_tnc_msg
)
405 pa_tnc_msg_t
*pa_msg
, *error_msg
;
406 pa_tnc_attr_t
*error_attr
;
407 enumerator_t
*enumerator
;
410 DBG2(DBG_IMV
, "IMV %u \"%s\" received message type 0x%08x for Connection ID %u",
411 this->id
, this->name
, msg_type
, connection_id
);
414 pa_msg
= pa_tnc_msg_create_from_data(msg
);
416 switch (pa_msg
->process(pa_msg
))
419 *pa_tnc_msg
= pa_msg
;
422 if (!this->send_message
)
424 /* TNCS doen't have a SendMessage() function */
425 return TNC_RESULT_FATAL
;
428 /* build error message */
429 error_msg
= pa_tnc_msg_create();
430 enumerator
= pa_msg
->create_error_enumerator(pa_msg
);
431 while (enumerator
->enumerate(enumerator
, &error_attr
))
433 error_msg
->add_attribute(error_msg
,
434 error_attr
->get_ref(error_attr
));
436 enumerator
->destroy(enumerator
);
437 error_msg
->build(error_msg
);
439 /* send error message */
440 msg
= error_msg
->get_encoding(error_msg
);
441 result
= this->send_message(this->id
, connection_id
,
442 msg
.ptr
, msg
.len
, msg_type
);
445 error_msg
->destroy(error_msg
);
446 pa_msg
->destroy(pa_msg
);
450 pa_msg
->destroy(pa_msg
);
451 return set_recommendation(this, connection_id
,
452 TNC_IMV_ACTION_RECOMMENDATION_NO_RECOMMENDATION
,
453 TNC_IMV_EVALUATION_RESULT_ERROR
);
455 return TNC_RESULT_SUCCESS
;
458 METHOD(imv_agent_t
, provide_recommendation
, TNC_Result
,
459 private_imv_agent_t
*this, TNC_ConnectionID connection_id
)
462 TNC_IMV_Action_Recommendation rec
;
463 TNC_IMV_Evaluation_Result eval
;
466 chunk_t pref_lang
= { buf
, 0 }, reason_string
, reason_lang
;
468 state
= find_connection(this, connection_id
);
471 DBG1(DBG_IMV
, "IMV %u \"%s\" has no state for Connection ID %u",
472 this->id
, this->name
, connection_id
);
473 return TNC_RESULT_FATAL
;
475 state
->get_recommendation(state
, &rec
, &eval
);
478 /* send a reason string if action recommendation is not allow */
479 if (rec
!= TNC_IMV_ACTION_RECOMMENDATION_ALLOW
)
481 /* check if there a preferred language has been requested */
482 if (this->get_attribute
&&
483 this->get_attribute(this->id
, connection_id
,
484 TNC_ATTRIBUTEID_PREFERRED_LANGUAGE
, BUF_LEN
,
485 buf
, &lang_len
) == TNC_RESULT_SUCCESS
&&
488 pref_lang
.len
= lang_len
;
489 DBG2(DBG_IMV
, "preferred language is '%.*s'",
490 pref_lang
.len
, pref_lang
.ptr
);
493 /* find a reason string for the preferred or default language and set it */
494 if (this->set_attribute
&&
495 state
->get_reason_string(state
, pref_lang
, &reason_string
,
498 this->set_attribute(this->id
, connection_id
,
499 TNC_ATTRIBUTEID_REASON_STRING
,
500 reason_string
.len
, reason_string
.ptr
);
501 this->set_attribute(this->id
, connection_id
,
502 TNC_ATTRIBUTEID_REASON_LANGUAGE
,
503 reason_lang
.len
, reason_lang
.ptr
);
507 return this->provide_recommendation(this->id
, connection_id
, rec
, eval
);
510 METHOD(imv_agent_t
, destroy
, void,
511 private_imv_agent_t
*this)
513 DBG1(DBG_IMV
, "IMV %u \"%s\" terminated", this->id
, this->name
);
514 this->connections
->destroy_offset(this->connections
,
515 offsetof(imv_state_t
, destroy
));
516 this->connection_lock
->destroy(this->connection_lock
);
519 /* decrease the reference count or terminate */
524 * Described in header.
526 imv_agent_t
*imv_agent_create(const char *name
,
527 pen_t vendor_id
, u_int32_t subtype
,
528 TNC_IMVID id
, TNC_Version
*actual_version
)
530 private_imv_agent_t
*this;
532 /* initialize or increase the reference count */
540 .bind_functions
= _bind_functions
,
541 .create_state
= _create_state
,
542 .delete_state
= _delete_state
,
543 .change_state
= _change_state
,
544 .get_state
= _get_state
,
545 .send_message
= _send_message
,
546 .receive_message
= _receive_message
,
547 .set_recommendation
= _set_recommendation
,
548 .provide_recommendation
= _provide_recommendation
,
552 .vendor_id
= vendor_id
,
555 .connections
= linked_list_create(),
556 .connection_lock
= rwlock_create(RWLOCK_TYPE_DEFAULT
),
559 *actual_version
= TNC_IFIMV_VERSION_1
;
560 DBG1(DBG_IMV
, "IMV %u \"%s\" initialized", this->id
, this->name
);
562 return &this->public;