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 "imc_agent.h"
18 #include <tncif_names.h>
21 #include <threading/rwlock.h>
23 typedef struct private_imc_agent_t private_imc_agent_t
;
26 * Private data of an imc_agent_t object.
28 struct private_imc_agent_t
{
31 * Public members of imc_agent_t
41 * message vendor ID of IMC
43 TNC_VendorID vendor_id
;
46 * message subtype of IMC
48 TNC_MessageSubtype subtype
;
51 * ID of IMC as assigned by TNCC
56 * List of additional IMC IDs assigned by TNCC
58 linked_list_t
*additional_ids
;
61 * list of TNCC connection entries
63 linked_list_t
*connections
;
66 * rwlock to lock TNCC connection entries
68 rwlock_t
*connection_lock
;
71 * Inform a TNCC about the set of message types the IMC is able to receive
73 * @param imc_id IMC ID assigned by TNCC
74 * @param supported_types list of supported message types
75 * @param type_count number of list elements
76 * @return TNC result code
78 TNC_Result (*report_message_types
)(TNC_IMCID imc_id
,
79 TNC_MessageTypeList supported_types
,
80 TNC_UInt32 type_count
);
83 * Inform a TNCC about the set of message types the IMC is able to receive
85 * @param imc_id IMC ID assigned by TNCC
86 * @param supported_vids list of supported message vendor IDs
87 * @param supported_subtypes list of supported message subtypes
88 * @param type_count number of list elements
89 * @return TNC result code
91 TNC_Result (*report_message_types_long
)(TNC_IMCID imc_id
,
92 TNC_VendorIDList supported_vids
,
93 TNC_MessageSubtypeList supported_subtypes
,
94 TNC_UInt32 type_count
);
97 * Call when an IMC-IMC message is to be sent
99 * @param imc_id IMC ID assigned by TNCC
100 * @param connection_id network connection ID assigned by TNCC
101 * @param msg message to send
102 * @param msg_len message length in bytes
103 * @param msg_type message type
104 * @return TNC result code
106 TNC_Result (*send_message
)(TNC_IMCID imc_id
,
107 TNC_ConnectionID connection_id
,
108 TNC_BufferReference msg
,
110 TNC_MessageType msg_type
);
114 * Call when an IMC-IMC message is to be sent with long message types
116 * @param imc_id IMC ID assigned by TNCC
117 * @param connection_id network connection ID assigned by TNCC
118 * @param msg_flags message flags
119 * @param msg message to send
120 * @param msg_len message length in bytes
121 * @param msg_vid message vendor ID
122 * @param msg_subtype message subtype
123 * @param dst_imc_id destination IMV ID
124 * @return TNC result code
126 TNC_Result (*send_message_long
)(TNC_IMCID imc_id
,
127 TNC_ConnectionID connection_id
,
128 TNC_UInt32 msg_flags
,
129 TNC_BufferReference msg
,
131 TNC_VendorID msg_vid
,
132 TNC_MessageSubtype msg_subtype
,
133 TNC_UInt32 dst_imv_id
);
136 * Get the value of an attribute associated with a connection
137 * or with the TNCC as a whole.
139 * @param imc_id IMC ID assigned by TNCC
140 * @param connection_id network connection ID assigned by TNCC
141 * @param attribute_id attribute ID
142 * @param buffer_len length of buffer in bytes
143 * @param buffer buffer
144 * @param out_value_len size in bytes of attribute stored in buffer
145 * @return TNC result code
147 TNC_Result (*get_attribute
)(TNC_IMCID imc_id
,
148 TNC_ConnectionID connection_id
,
149 TNC_AttributeID attribute_id
,
150 TNC_UInt32 buffer_len
,
151 TNC_BufferReference buffer
,
152 TNC_UInt32
*out_value_len
);
155 * Set the value of an attribute associated with a connection
156 * or with the TNCC as a whole.
158 * @param imc_id IMV ID assigned by TNCC
159 * @param connection_id network connection ID assigned by TNCC
160 * @param attribute_id attribute ID
161 * @param buffer_len length of buffer in bytes
162 * @param buffer buffer
163 * @return TNC result code
165 TNC_Result (*set_attribute
)(TNC_IMCID imc_id
,
166 TNC_ConnectionID connection_id
,
167 TNC_AttributeID attribute_id
,
168 TNC_UInt32 buffer_len
,
169 TNC_BufferReference buffer
);
172 * Reserve an additional IMC ID
174 * @param imc_id primary IMC ID assigned by TNCC
175 * @param out_imc_id additional IMC ID assigned by TNCC
176 * @return TNC result code
178 TNC_Result (*reserve_additional_id
)(TNC_IMCID imc_id
,
179 TNC_UInt32
*out_imc_id
);
183 METHOD(imc_agent_t
, bind_functions
, TNC_Result
,
184 private_imc_agent_t
*this, TNC_TNCC_BindFunctionPointer bind_function
)
188 DBG1(DBG_IMC
, "TNC client failed to provide bind function");
189 return TNC_RESULT_INVALID_PARAMETER
;
191 if (bind_function(this->id
, "TNC_TNCC_ReportMessageTypes",
192 (void**)&this->report_message_types
) != TNC_RESULT_SUCCESS
)
194 this->report_message_types
= NULL
;
196 if (bind_function(this->id
, "TNC_TNCC_ReportMessageTypesLong",
197 (void**)&this->report_message_types_long
) != TNC_RESULT_SUCCESS
)
199 this->report_message_types_long
= NULL
;
201 if (bind_function(this->id
, "TNC_TNCC_RequestHandshakeRetry",
202 (void**)&this->public.request_handshake_retry
) != TNC_RESULT_SUCCESS
)
204 this->public.request_handshake_retry
= NULL
;
206 if (bind_function(this->id
, "TNC_TNCC_SendMessage",
207 (void**)&this->send_message
) != TNC_RESULT_SUCCESS
)
209 this->send_message
= NULL
;
211 if (bind_function(this->id
, "TNC_TNCC_SendMessageLong",
212 (void**)&this->send_message_long
) != TNC_RESULT_SUCCESS
)
214 this->send_message_long
= NULL
;
216 if (bind_function(this->id
, "TNC_TNCC_GetAttribute",
217 (void**)&this->get_attribute
) != TNC_RESULT_SUCCESS
)
219 this->get_attribute
= NULL
;
221 if (bind_function(this->id
, "TNC_TNCC_SetAttribute",
222 (void**)&this->set_attribute
) != TNC_RESULT_SUCCESS
)
224 this->set_attribute
= NULL
;
226 if (bind_function(this->id
, "TNC_TNCC_ReserveAdditionalIMCID",
227 (void**)&this->reserve_additional_id
) != TNC_RESULT_SUCCESS
)
229 this->reserve_additional_id
= NULL
;
231 DBG2(DBG_IMC
, "IMC %u \"%s\" provided with bind function",
232 this->id
, this->name
);
234 if (this->report_message_types_long
)
236 this->report_message_types_long(this->id
, &this->vendor_id
,
239 else if (this->report_message_types
&&
240 this->vendor_id
<= TNC_VENDORID_ANY
&&
241 this->subtype
<= TNC_SUBTYPE_ANY
)
243 TNC_MessageType type
;
245 type
= (this->vendor_id
<< 8) | this->subtype
;
246 this->report_message_types(this->id
, &type
, 1);
248 return TNC_RESULT_SUCCESS
;
252 * finds a connection state based on its Connection ID
254 static imc_state_t
* find_connection(private_imc_agent_t
*this,
257 enumerator_t
*enumerator
;
258 imc_state_t
*state
, *found
= NULL
;
260 this->connection_lock
->read_lock(this->connection_lock
);
261 enumerator
= this->connections
->create_enumerator(this->connections
);
262 while (enumerator
->enumerate(enumerator
, &state
))
264 if (id
== state
->get_connection_id(state
))
270 enumerator
->destroy(enumerator
);
271 this->connection_lock
->unlock(this->connection_lock
);
277 * delete a connection state with a given Connection ID
279 static bool delete_connection(private_imc_agent_t
*this, TNC_ConnectionID id
)
281 enumerator_t
*enumerator
;
285 this->connection_lock
->write_lock(this->connection_lock
);
286 enumerator
= this->connections
->create_enumerator(this->connections
);
287 while (enumerator
->enumerate(enumerator
, &state
))
289 if (id
== state
->get_connection_id(state
))
292 state
->destroy(state
);
293 this->connections
->remove_at(this->connections
, enumerator
);
297 enumerator
->destroy(enumerator
);
298 this->connection_lock
->unlock(this->connection_lock
);
304 * Read a boolean attribute
306 static bool get_bool_attribute(private_imc_agent_t
*this, TNC_ConnectionID id
,
307 TNC_AttributeID attribute_id
)
312 return this->get_attribute
&&
313 this->get_attribute(this->id
, id
, attribute_id
, 4, buf
, &len
) ==
314 TNC_RESULT_SUCCESS
&& len
== 1 && *buf
== 0x01;
318 * Read a string attribute
320 static char* get_str_attribute(private_imc_agent_t
*this, TNC_ConnectionID id
,
321 TNC_AttributeID attribute_id
)
326 if (this->get_attribute
&&
327 this->get_attribute(this->id
, id
, attribute_id
, BUF_LEN
, buf
, &len
) ==
328 TNC_RESULT_SUCCESS
&& len
<= BUF_LEN
)
335 METHOD(imc_agent_t
, create_state
, TNC_Result
,
336 private_imc_agent_t
*this, imc_state_t
*state
)
338 TNC_ConnectionID conn_id
;
339 char *tnccs_p
= NULL
, *tnccs_v
= NULL
, *t_p
= NULL
, *t_v
= NULL
;
340 bool has_long
= FALSE
, has_excl
= FALSE
, has_soh
= FALSE
;
342 conn_id
= state
->get_connection_id(state
);
343 if (find_connection(this, conn_id
))
345 DBG1(DBG_IMC
, "IMC %u \"%s\" already created a state for Connection ID %u",
346 this->id
, this->name
, conn_id
);
347 state
->destroy(state
);
348 return TNC_RESULT_OTHER
;
351 /* Get and display attributes from TNCC via IF-IMC */
352 has_long
= get_bool_attribute(this, conn_id
, TNC_ATTRIBUTEID_HAS_LONG_TYPES
);
353 has_excl
= get_bool_attribute(this, conn_id
, TNC_ATTRIBUTEID_HAS_EXCLUSIVE
);
354 has_soh
= get_bool_attribute(this, conn_id
, TNC_ATTRIBUTEID_HAS_SOH
);
355 tnccs_p
= get_str_attribute(this, conn_id
, TNC_ATTRIBUTEID_IFTNCCS_PROTOCOL
);
356 tnccs_v
= get_str_attribute(this, conn_id
, TNC_ATTRIBUTEID_IFTNCCS_VERSION
);
357 t_p
= get_str_attribute(this, conn_id
, TNC_ATTRIBUTEID_IFT_PROTOCOL
);
358 t_v
= get_str_attribute(this, conn_id
, TNC_ATTRIBUTEID_IFT_VERSION
);
360 state
->set_flags(state
, has_long
, has_excl
);
362 DBG2(DBG_IMC
, "IMC %u \"%s\" created a state for Connection ID %u: "
363 "%s %s with %slong %sexcl %ssoh over %s %s",
364 this->id
, this->name
, conn_id
, tnccs_p ? tnccs_p
:"?",
365 tnccs_v ? tnccs_v
:"?", has_long ?
"+":"-", has_excl ?
"+":"-",
366 has_soh ?
"+":"-", t_p ? t_p
:"?", t_v ? t_v
:"?");
372 this->connection_lock
->write_lock(this->connection_lock
);
373 this->connections
->insert_last(this->connections
, state
);
374 this->connection_lock
->unlock(this->connection_lock
);
375 return TNC_RESULT_SUCCESS
;
378 METHOD(imc_agent_t
, delete_state
, TNC_Result
,
379 private_imc_agent_t
*this, TNC_ConnectionID connection_id
)
381 if (!delete_connection(this, connection_id
))
383 DBG1(DBG_IMC
, "IMC %u \"%s\" has no state for Connection ID %u",
384 this->id
, this->name
, connection_id
);
385 return TNC_RESULT_FATAL
;
387 DBG2(DBG_IMC
, "IMC %u \"%s\" deleted the state of Connection ID %u",
388 this->id
, this->name
, connection_id
);
389 return TNC_RESULT_SUCCESS
;
392 METHOD(imc_agent_t
, change_state
, TNC_Result
,
393 private_imc_agent_t
*this, TNC_ConnectionID connection_id
,
394 TNC_ConnectionState new_state
,
395 imc_state_t
**state_p
)
401 case TNC_CONNECTION_STATE_HANDSHAKE
:
402 case TNC_CONNECTION_STATE_ACCESS_ALLOWED
:
403 case TNC_CONNECTION_STATE_ACCESS_ISOLATED
:
404 case TNC_CONNECTION_STATE_ACCESS_NONE
:
405 state
= find_connection(this, connection_id
);
409 DBG1(DBG_IMC
, "IMC %u \"%s\" has no state for Connection ID %u",
410 this->id
, this->name
, connection_id
);
411 return TNC_RESULT_FATAL
;
413 state
->change_state(state
, new_state
);
414 DBG2(DBG_IMC
, "IMC %u \"%s\" changed state of Connection ID %u to '%N'",
415 this->id
, this->name
, connection_id
,
416 TNC_Connection_State_names
, new_state
);
422 case TNC_CONNECTION_STATE_CREATE
:
423 DBG1(DBG_IMC
, "state '%N' should be handled by create_state()",
424 TNC_Connection_State_names
, new_state
);
425 return TNC_RESULT_FATAL
;
426 case TNC_CONNECTION_STATE_DELETE
:
427 DBG1(DBG_IMC
, "state '%N' should be handled by delete_state()",
428 TNC_Connection_State_names
, new_state
);
429 return TNC_RESULT_FATAL
;
431 DBG1(DBG_IMC
, "IMC %u \"%s\" was notified of unknown state %u "
432 "for Connection ID %u",
433 this->id
, this->name
, new_state
, connection_id
);
434 return TNC_RESULT_INVALID_PARAMETER
;
436 return TNC_RESULT_SUCCESS
;
439 METHOD(imc_agent_t
, get_state
, bool,
440 private_imc_agent_t
*this, TNC_ConnectionID connection_id
,
443 *state
= find_connection(this, connection_id
);
446 DBG1(DBG_IMC
, "IMC %u \"%s\" has no state for Connection ID %u",
447 this->id
, this->name
, connection_id
);
453 METHOD(imc_agent_t
, send_message
, TNC_Result
,
454 private_imc_agent_t
*this, TNC_ConnectionID connection_id
, bool excl
,
455 TNC_UInt32 src_imc_id
, TNC_UInt32 dst_imv_id
, linked_list_t
*attr_list
)
457 TNC_MessageType type
;
458 TNC_UInt32 msg_flags
;
459 TNC_Result result
= TNC_RESULT_FATAL
;
462 pa_tnc_msg_t
*pa_tnc_msg
;
465 state
= find_connection(this, connection_id
);
468 DBG1(DBG_IMV
, "IMC %u \"%s\" has no state for Connection ID %u",
469 this->id
, this->name
, connection_id
);
470 return TNC_RESULT_FATAL
;
473 pa_tnc_msg
= pa_tnc_msg_create();
475 while (attr_list
->remove_first(attr_list
, (void**)&attr
) == SUCCESS
)
477 pa_tnc_msg
->add_attribute(pa_tnc_msg
, attr
);
479 pa_tnc_msg
->build(pa_tnc_msg
);
480 msg
= pa_tnc_msg
->get_encoding(pa_tnc_msg
);
482 if (state
->has_long(state
) && this->send_message_long
)
486 src_imc_id
= this->id
;
488 msg_flags
= excl ? TNC_MESSAGE_FLAGS_EXCLUSIVE
: 0;
490 result
= this->send_message_long(src_imc_id
, connection_id
, msg_flags
,
491 msg
.ptr
, msg
.len
, this->vendor_id
,
492 this->subtype
, dst_imv_id
);
494 else if (this->send_message
)
496 type
= (this->vendor_id
<< 8) | this->subtype
;
498 result
= this->send_message(this->id
, connection_id
, msg
.ptr
, msg
.len
,
501 pa_tnc_msg
->destroy(pa_tnc_msg
);
506 METHOD(imc_agent_t
, receive_message
, TNC_Result
,
507 private_imc_agent_t
*this, imc_state_t
*state
, chunk_t msg
,
508 TNC_VendorID msg_vid
, TNC_MessageSubtype msg_subtype
,
509 TNC_UInt32 src_imv_id
, TNC_UInt32 dst_imc_id
, pa_tnc_msg_t
**pa_tnc_msg
)
511 pa_tnc_msg_t
*pa_msg
, *error_msg
;
512 pa_tnc_attr_t
*error_attr
;
513 enumerator_t
*enumerator
;
514 TNC_MessageType msg_type
;
515 TNC_UInt32 msg_flags
, src_imc_id
, dst_imv_id
;
516 TNC_ConnectionID connection_id
;
519 connection_id
= state
->get_connection_id(state
);
521 if (state
->has_long(state
))
523 if (dst_imc_id
!= TNC_IMCID_ANY
)
525 DBG2(DBG_IMC
, "IMC %u \"%s\" received message for Connection ID %u "
526 "from IMV %u to IMC %u", this->id
, this->name
,
527 connection_id
, src_imv_id
, dst_imc_id
);
531 DBG2(DBG_IMC
, "IMC %u \"%s\" received message for Connection ID %u "
532 "from IMV %u", this->id
, this->name
, connection_id
,
538 DBG2(DBG_IMC
, "IMC %u \"%s\" received message for Connection ID %u",
539 this->id
, this->name
, connection_id
);
543 pa_msg
= pa_tnc_msg_create_from_data(msg
);
545 switch (pa_msg
->process(pa_msg
))
548 *pa_tnc_msg
= pa_msg
;
551 /* build error message */
552 error_msg
= pa_tnc_msg_create();
553 enumerator
= pa_msg
->create_error_enumerator(pa_msg
);
554 while (enumerator
->enumerate(enumerator
, &error_attr
))
556 error_msg
->add_attribute(error_msg
,
557 error_attr
->get_ref(error_attr
));
559 enumerator
->destroy(enumerator
);
560 error_msg
->build(error_msg
);
562 /* send error message */
563 if (state
->has_long(state
) && this->send_message_long
)
565 if (state
->has_excl(state
))
567 msg_flags
= TNC_MESSAGE_FLAGS_EXCLUSIVE
;
568 dst_imv_id
= src_imv_id
;
573 dst_imv_id
= TNC_IMVID_ANY
;
575 src_imc_id
= (dst_imc_id
== TNC_IMCID_ANY
) ?
this->id
578 result
= this->send_message_long(src_imc_id
, connection_id
,
579 msg_flags
, msg
.ptr
, msg
.len
, msg_vid
,
580 msg_subtype
, dst_imv_id
);
582 else if (this->send_message
)
584 msg_type
= (msg_vid
<< 8) | msg_subtype
;
586 result
= this->send_message(this->id
, connection_id
,
587 msg
.ptr
, msg
.len
, msg_type
);
591 result
= TNC_RESULT_FATAL
;
595 error_msg
->destroy(error_msg
);
596 pa_msg
->destroy(pa_msg
);
600 pa_msg
->destroy(pa_msg
);
601 return TNC_RESULT_FATAL
;
603 return TNC_RESULT_SUCCESS
;
606 METHOD(imc_agent_t
, reserve_additional_ids
, TNC_Result
,
607 private_imc_agent_t
*this, int count
)
613 if (!this->reserve_additional_id
)
615 DBG1(DBG_IMC
, "IMC %u \"%s\" did not detect the capability to reserve "
616 "additional IMC IDs from the TNCC", this->id
, this->name
);
617 return TNC_RESULT_ILLEGAL_OPERATION
;
621 result
= this->reserve_additional_id(this->id
, &id
);
622 if (result
!= TNC_RESULT_SUCCESS
)
624 DBG1(DBG_IMC
, "IMC %u \"%s\" failed to reserve %d additional IMC IDs",
625 this->id
, this->name
, count
);
630 /* store the scalar value in the pointer */
632 this->additional_ids
->insert_last(this->additional_ids
, pointer
);
633 DBG2(DBG_IMC
, "IMC %u \"%s\" reserved additional ID %u",
634 this->id
, this->name
, id
);
636 return TNC_RESULT_SUCCESS
;
639 METHOD(imc_agent_t
, count_additional_ids
, int,
640 private_imc_agent_t
*this)
642 return this->additional_ids
->get_count(this->additional_ids
);
645 METHOD(imc_agent_t
, create_id_enumerator
, enumerator_t
*,
646 private_imc_agent_t
*this)
648 return this->additional_ids
->create_enumerator(this->additional_ids
);
651 METHOD(imc_agent_t
, destroy
, void,
652 private_imc_agent_t
*this)
654 DBG1(DBG_IMC
, "IMC %u \"%s\" terminated", this->id
, this->name
);
655 this->additional_ids
->destroy(this->additional_ids
);
656 this->connections
->destroy_function(this->connections
, free
);
657 this->connection_lock
->destroy(this->connection_lock
);
660 /* decrease the reference count or terminate */
665 * Described in header.
667 imc_agent_t
*imc_agent_create(const char *name
,
668 pen_t vendor_id
, u_int32_t subtype
,
669 TNC_IMCID id
, TNC_Version
*actual_version
)
671 private_imc_agent_t
*this;
673 /* initialize or increase the reference count */
681 .bind_functions
= _bind_functions
,
682 .create_state
= _create_state
,
683 .delete_state
= _delete_state
,
684 .change_state
= _change_state
,
685 .get_state
= _get_state
,
686 .send_message
= _send_message
,
687 .receive_message
= _receive_message
,
688 .reserve_additional_ids
= _reserve_additional_ids
,
689 .count_additional_ids
= _count_additional_ids
,
690 .create_id_enumerator
= _create_id_enumerator
,
694 .vendor_id
= vendor_id
,
697 .additional_ids
= linked_list_create(),
698 .connections
= linked_list_create(),
699 .connection_lock
= rwlock_create(RWLOCK_TYPE_DEFAULT
),
702 *actual_version
= TNC_IFIMC_VERSION_1
;
703 DBG1(DBG_IMC
, "IMC %u \"%s\" initialized", this->id
, this->name
);
705 return &this->public;