2 * Copyright (C) 2011-2012 Andreas Steffen
3 * HSR Hochschule fuer Technik Rapperswil
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>.
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
17 #include "imc_agent.h"
19 #include <tncif_names.h>
22 #include <threading/rwlock.h>
24 typedef struct private_imc_agent_t private_imc_agent_t
;
27 * Private data of an imc_agent_t object.
29 struct private_imc_agent_t
{
32 * Public members of imc_agent_t
42 * message types registered by IMC
44 pen_type_t
*supported_types
;
47 * number of message types registered by IMC
52 * ID of IMC as assigned by TNCC
57 * List of additional IMC IDs assigned by TNCC
59 linked_list_t
*additional_ids
;
62 * list of TNCC connection entries
64 linked_list_t
*connections
;
67 * rwlock to lock TNCC connection entries
69 rwlock_t
*connection_lock
;
72 * Inform a TNCC about the set of message types the IMC is able to receive
74 * @param imc_id IMC ID assigned by TNCC
75 * @param supported_types list of supported message types
76 * @param type_count number of list elements
77 * @return TNC result code
79 TNC_Result (*report_message_types
)(TNC_IMCID imc_id
,
80 TNC_MessageTypeList supported_types
,
81 TNC_UInt32 type_count
);
84 * Inform a TNCC about the set of message types the IMC is able to receive
86 * @param imc_id IMC ID assigned by TNCC
87 * @param supported_vids list of supported message vendor IDs
88 * @param supported_subtypes list of supported message subtypes
89 * @param type_count number of list elements
90 * @return TNC result code
92 TNC_Result (*report_message_types_long
)(TNC_IMCID imc_id
,
93 TNC_VendorIDList supported_vids
,
94 TNC_MessageSubtypeList supported_subtypes
,
95 TNC_UInt32 type_count
);
98 * Call when an IMC-IMC message is to be sent
100 * @param imc_id IMC ID assigned by TNCC
101 * @param connection_id network connection ID assigned by TNCC
102 * @param msg message to send
103 * @param msg_len message length in bytes
104 * @param msg_type message type
105 * @return TNC result code
107 TNC_Result (*send_message
)(TNC_IMCID imc_id
,
108 TNC_ConnectionID connection_id
,
109 TNC_BufferReference msg
,
111 TNC_MessageType msg_type
);
115 * Call when an IMC-IMC message is to be sent with long message types
117 * @param imc_id IMC ID assigned by TNCC
118 * @param connection_id network connection ID assigned by TNCC
119 * @param msg_flags message flags
120 * @param msg message to send
121 * @param msg_len message length in bytes
122 * @param msg_vid message vendor ID
123 * @param msg_subtype message subtype
124 * @param dst_imc_id destination IMV ID
125 * @return TNC result code
127 TNC_Result (*send_message_long
)(TNC_IMCID imc_id
,
128 TNC_ConnectionID connection_id
,
129 TNC_UInt32 msg_flags
,
130 TNC_BufferReference msg
,
132 TNC_VendorID msg_vid
,
133 TNC_MessageSubtype msg_subtype
,
134 TNC_UInt32 dst_imv_id
);
137 * Get the value of an attribute associated with a connection
138 * or with the TNCC as a whole.
140 * @param imc_id IMC ID assigned by TNCC
141 * @param connection_id network connection ID assigned by TNCC
142 * @param attribute_id attribute ID
143 * @param buffer_len length of buffer in bytes
144 * @param buffer buffer
145 * @param out_value_len size in bytes of attribute stored in buffer
146 * @return TNC result code
148 TNC_Result (*get_attribute
)(TNC_IMCID imc_id
,
149 TNC_ConnectionID connection_id
,
150 TNC_AttributeID attribute_id
,
151 TNC_UInt32 buffer_len
,
152 TNC_BufferReference buffer
,
153 TNC_UInt32
*out_value_len
);
156 * Set the value of an attribute associated with a connection
157 * or with the TNCC as a whole.
159 * @param imc_id IMV ID assigned by TNCC
160 * @param connection_id network connection ID assigned by TNCC
161 * @param attribute_id attribute ID
162 * @param buffer_len length of buffer in bytes
163 * @param buffer buffer
164 * @return TNC result code
166 TNC_Result (*set_attribute
)(TNC_IMCID imc_id
,
167 TNC_ConnectionID connection_id
,
168 TNC_AttributeID attribute_id
,
169 TNC_UInt32 buffer_len
,
170 TNC_BufferReference buffer
);
173 * Reserve an additional IMC ID
175 * @param imc_id primary IMC ID assigned by TNCC
176 * @param out_imc_id additional IMC ID assigned by TNCC
177 * @return TNC result code
179 TNC_Result (*reserve_additional_id
)(TNC_IMCID imc_id
,
180 TNC_UInt32
*out_imc_id
);
184 METHOD(imc_agent_t
, bind_functions
, TNC_Result
,
185 private_imc_agent_t
*this, TNC_TNCC_BindFunctionPointer bind_function
)
189 DBG1(DBG_IMC
, "TNC client failed to provide bind function");
190 return TNC_RESULT_INVALID_PARAMETER
;
192 if (bind_function(this->id
, "TNC_TNCC_ReportMessageTypes",
193 (void**)&this->report_message_types
) != TNC_RESULT_SUCCESS
)
195 this->report_message_types
= NULL
;
197 if (bind_function(this->id
, "TNC_TNCC_ReportMessageTypesLong",
198 (void**)&this->report_message_types_long
) != TNC_RESULT_SUCCESS
)
200 this->report_message_types_long
= NULL
;
202 if (bind_function(this->id
, "TNC_TNCC_RequestHandshakeRetry",
203 (void**)&this->public.request_handshake_retry
) != TNC_RESULT_SUCCESS
)
205 this->public.request_handshake_retry
= NULL
;
207 if (bind_function(this->id
, "TNC_TNCC_SendMessage",
208 (void**)&this->send_message
) != TNC_RESULT_SUCCESS
)
210 this->send_message
= NULL
;
212 if (bind_function(this->id
, "TNC_TNCC_SendMessageLong",
213 (void**)&this->send_message_long
) != TNC_RESULT_SUCCESS
)
215 this->send_message_long
= NULL
;
217 if (bind_function(this->id
, "TNC_TNCC_GetAttribute",
218 (void**)&this->get_attribute
) != TNC_RESULT_SUCCESS
)
220 this->get_attribute
= NULL
;
222 if (bind_function(this->id
, "TNC_TNCC_SetAttribute",
223 (void**)&this->set_attribute
) != TNC_RESULT_SUCCESS
)
225 this->set_attribute
= NULL
;
227 if (bind_function(this->id
, "TNC_TNCC_ReserveAdditionalIMCID",
228 (void**)&this->reserve_additional_id
) != TNC_RESULT_SUCCESS
)
230 this->reserve_additional_id
= NULL
;
232 DBG2(DBG_IMC
, "IMC %u \"%s\" provided with bind function",
233 this->id
, this->name
);
235 if (this->report_message_types_long
)
237 TNC_VendorIDList vendor_id_list
;
238 TNC_MessageSubtypeList subtype_list
;
241 vendor_id_list
= malloc(this->type_count
* sizeof(TNC_UInt32
));
242 subtype_list
= malloc(this->type_count
* sizeof(TNC_UInt32
));
244 for (i
= 0; i
< this->type_count
; i
++)
246 vendor_id_list
[i
] = this->supported_types
[i
].vendor_id
;
247 subtype_list
[i
] = this->supported_types
[i
].type
;
249 this->report_message_types_long(this->id
, vendor_id_list
, subtype_list
,
251 free(vendor_id_list
);
254 else if (this->report_message_types
)
256 TNC_MessageTypeList type_list
;
259 type_list
= malloc(this->type_count
* sizeof(TNC_UInt32
));
261 for (i
= 0; i
< this->type_count
; i
++)
263 type_list
[i
] = (this->supported_types
[i
].vendor_id
<< 8) |
264 (this->supported_types
[i
].type
& 0xff);
266 this->report_message_types(this->id
, type_list
, this->type_count
);
269 return TNC_RESULT_SUCCESS
;
273 * finds a connection state based on its Connection ID
275 static imc_state_t
* find_connection(private_imc_agent_t
*this,
278 enumerator_t
*enumerator
;
279 imc_state_t
*state
, *found
= NULL
;
281 this->connection_lock
->read_lock(this->connection_lock
);
282 enumerator
= this->connections
->create_enumerator(this->connections
);
283 while (enumerator
->enumerate(enumerator
, &state
))
285 if (id
== state
->get_connection_id(state
))
291 enumerator
->destroy(enumerator
);
292 this->connection_lock
->unlock(this->connection_lock
);
298 * delete a connection state with a given Connection ID
300 static bool delete_connection(private_imc_agent_t
*this, TNC_ConnectionID id
)
302 enumerator_t
*enumerator
;
306 this->connection_lock
->write_lock(this->connection_lock
);
307 enumerator
= this->connections
->create_enumerator(this->connections
);
308 while (enumerator
->enumerate(enumerator
, &state
))
310 if (id
== state
->get_connection_id(state
))
313 state
->destroy(state
);
314 this->connections
->remove_at(this->connections
, enumerator
);
318 enumerator
->destroy(enumerator
);
319 this->connection_lock
->unlock(this->connection_lock
);
325 * Read a boolean attribute
327 static bool get_bool_attribute(private_imc_agent_t
*this, TNC_ConnectionID id
,
328 TNC_AttributeID attribute_id
)
333 return this->get_attribute
&&
334 this->get_attribute(this->id
, id
, attribute_id
, 4, buf
, &len
) ==
335 TNC_RESULT_SUCCESS
&& len
== 1 && *buf
== 0x01;
339 * Read a string attribute
341 static char* get_str_attribute(private_imc_agent_t
*this, TNC_ConnectionID id
,
342 TNC_AttributeID attribute_id
)
347 if (this->get_attribute
&&
348 this->get_attribute(this->id
, id
, attribute_id
, BUF_LEN
, buf
, &len
) ==
349 TNC_RESULT_SUCCESS
&& len
<= BUF_LEN
)
357 * Read an UInt32 attribute
359 static u_int32_t
get_uint_attribute(private_imc_agent_t
*this, TNC_ConnectionID id
,
360 TNC_AttributeID attribute_id
)
365 if (this->get_attribute
&&
366 this->get_attribute(this->id
, id
, attribute_id
, 4, buf
, &len
) ==
367 TNC_RESULT_SUCCESS
&& len
== 4)
374 METHOD(imc_agent_t
, create_state
, TNC_Result
,
375 private_imc_agent_t
*this, imc_state_t
*state
)
377 TNC_ConnectionID conn_id
;
378 char *tnccs_p
= NULL
, *tnccs_v
= NULL
, *t_p
= NULL
, *t_v
= NULL
;
379 bool has_long
= FALSE
, has_excl
= FALSE
, has_soh
= FALSE
;
380 u_int32_t max_msg_len
;
382 conn_id
= state
->get_connection_id(state
);
383 if (find_connection(this, conn_id
))
385 DBG1(DBG_IMC
, "IMC %u \"%s\" already created a state for Connection ID %u",
386 this->id
, this->name
, conn_id
);
387 state
->destroy(state
);
388 return TNC_RESULT_OTHER
;
391 /* Get and display attributes from TNCC via IF-IMC */
392 has_long
= get_bool_attribute(this, conn_id
, TNC_ATTRIBUTEID_HAS_LONG_TYPES
);
393 has_excl
= get_bool_attribute(this, conn_id
, TNC_ATTRIBUTEID_HAS_EXCLUSIVE
);
394 has_soh
= get_bool_attribute(this, conn_id
, TNC_ATTRIBUTEID_HAS_SOH
);
395 tnccs_p
= get_str_attribute(this, conn_id
, TNC_ATTRIBUTEID_IFTNCCS_PROTOCOL
);
396 tnccs_v
= get_str_attribute(this, conn_id
, TNC_ATTRIBUTEID_IFTNCCS_VERSION
);
397 t_p
= get_str_attribute(this, conn_id
, TNC_ATTRIBUTEID_IFT_PROTOCOL
);
398 t_v
= get_str_attribute(this, conn_id
, TNC_ATTRIBUTEID_IFT_VERSION
);
399 max_msg_len
= get_uint_attribute(this, conn_id
, TNC_ATTRIBUTEID_MAX_MESSAGE_SIZE
);
401 state
->set_flags(state
, has_long
, has_excl
);
402 state
->set_max_msg_len(state
, max_msg_len
);
404 DBG2(DBG_IMC
, "IMC %u \"%s\" created a state for %s %s Connection ID %u: "
405 "%slong %sexcl %ssoh", this->id
, this->name
,
406 tnccs_p ? tnccs_p
:"?", tnccs_v ? tnccs_v
:"?", conn_id
,
407 has_long ?
"+":"-", has_excl ?
"+":"-", has_soh ?
"+":"-");
408 DBG2(DBG_IMC
, " over %s %s with maximum PA-TNC message size of %u bytes",
409 t_p ? t_p
:"?", t_v ? t_v
:"?", max_msg_len
);
416 this->connection_lock
->write_lock(this->connection_lock
);
417 this->connections
->insert_last(this->connections
, state
);
418 this->connection_lock
->unlock(this->connection_lock
);
419 return TNC_RESULT_SUCCESS
;
422 METHOD(imc_agent_t
, delete_state
, TNC_Result
,
423 private_imc_agent_t
*this, TNC_ConnectionID connection_id
)
425 if (!delete_connection(this, connection_id
))
427 DBG1(DBG_IMC
, "IMC %u \"%s\" has no state for Connection ID %u",
428 this->id
, this->name
, connection_id
);
429 return TNC_RESULT_FATAL
;
431 DBG2(DBG_IMC
, "IMC %u \"%s\" deleted the state of Connection ID %u",
432 this->id
, this->name
, connection_id
);
433 return TNC_RESULT_SUCCESS
;
436 METHOD(imc_agent_t
, change_state
, TNC_Result
,
437 private_imc_agent_t
*this, TNC_ConnectionID connection_id
,
438 TNC_ConnectionState new_state
,
439 imc_state_t
**state_p
)
445 case TNC_CONNECTION_STATE_HANDSHAKE
:
446 case TNC_CONNECTION_STATE_ACCESS_ALLOWED
:
447 case TNC_CONNECTION_STATE_ACCESS_ISOLATED
:
448 case TNC_CONNECTION_STATE_ACCESS_NONE
:
449 state
= find_connection(this, connection_id
);
453 DBG1(DBG_IMC
, "IMC %u \"%s\" has no state for Connection ID %u",
454 this->id
, this->name
, connection_id
);
455 return TNC_RESULT_FATAL
;
457 state
->change_state(state
, new_state
);
458 DBG2(DBG_IMC
, "IMC %u \"%s\" changed state of Connection ID %u to '%N'",
459 this->id
, this->name
, connection_id
,
460 TNC_Connection_State_names
, new_state
);
466 case TNC_CONNECTION_STATE_CREATE
:
467 DBG1(DBG_IMC
, "state '%N' should be handled by create_state()",
468 TNC_Connection_State_names
, new_state
);
469 return TNC_RESULT_FATAL
;
470 case TNC_CONNECTION_STATE_DELETE
:
471 DBG1(DBG_IMC
, "state '%N' should be handled by delete_state()",
472 TNC_Connection_State_names
, new_state
);
473 return TNC_RESULT_FATAL
;
475 DBG1(DBG_IMC
, "IMC %u \"%s\" was notified of unknown state %u "
476 "for Connection ID %u",
477 this->id
, this->name
, new_state
, connection_id
);
478 return TNC_RESULT_INVALID_PARAMETER
;
480 return TNC_RESULT_SUCCESS
;
483 METHOD(imc_agent_t
, get_state
, bool,
484 private_imc_agent_t
*this, TNC_ConnectionID connection_id
,
487 *state
= find_connection(this, connection_id
);
490 DBG1(DBG_IMC
, "IMC %u \"%s\" has no state for Connection ID %u",
491 this->id
, this->name
, connection_id
);
497 METHOD(imc_agent_t
, send_message
, TNC_Result
,
498 private_imc_agent_t
*this, TNC_ConnectionID connection_id
, bool excl
,
499 TNC_UInt32 src_imc_id
, TNC_UInt32 dst_imv_id
, TNC_VendorID msg_vid
,
500 TNC_MessageSubtype msg_subtype
, linked_list_t
*attr_list
)
502 TNC_MessageType type
;
503 TNC_UInt32 msg_flags
;
504 TNC_Result result
= TNC_RESULT_FATAL
;
507 pa_tnc_msg_t
*pa_tnc_msg
;
509 enumerator_t
*enumerator
;
512 state
= find_connection(this, connection_id
);
515 DBG1(DBG_IMV
, "IMC %u \"%s\" has no state for Connection ID %u",
516 this->id
, this->name
, connection_id
);
517 return TNC_RESULT_FATAL
;
520 while (attr_list
->get_count(attr_list
))
522 pa_tnc_msg
= pa_tnc_msg_create(state
->get_max_msg_len(state
));
525 enumerator
= attr_list
->create_enumerator(attr_list
);
526 while (enumerator
->enumerate(enumerator
, &attr
))
528 if (pa_tnc_msg
->add_attribute(pa_tnc_msg
, attr
))
540 DBG1(DBG_IMC
, "PA-TNC attribute too large to send, deleted");
544 attr_list
->remove_at(attr_list
, enumerator
);
546 enumerator
->destroy(enumerator
);
548 /* build and send the PA-TNC message via the IF-IMC interface */
549 if (!pa_tnc_msg
->build(pa_tnc_msg
))
551 pa_tnc_msg
->destroy(pa_tnc_msg
);
552 return TNC_RESULT_FATAL
;
554 msg
= pa_tnc_msg
->get_encoding(pa_tnc_msg
);
556 if (state
->has_long(state
) && this->send_message_long
)
560 src_imc_id
= this->id
;
562 msg_flags
= excl ? TNC_MESSAGE_FLAGS_EXCLUSIVE
: 0;
564 result
= this->send_message_long(src_imc_id
, connection_id
,
565 msg_flags
, msg
.ptr
, msg
.len
, msg_vid
,
566 msg_subtype
, dst_imv_id
);
568 else if (this->send_message
)
570 type
= msg_vid
<< 8 | msg_subtype
;
572 result
= this->send_message(this->id
, connection_id
, msg
.ptr
,
576 pa_tnc_msg
->destroy(pa_tnc_msg
);
578 if (result
!= TNC_RESULT_SUCCESS
)
586 METHOD(imc_agent_t
, receive_message
, TNC_Result
,
587 private_imc_agent_t
*this, imc_state_t
*state
, chunk_t msg
,
588 TNC_VendorID msg_vid
, TNC_MessageSubtype msg_subtype
,
589 TNC_UInt32 src_imv_id
, TNC_UInt32 dst_imc_id
, pa_tnc_msg_t
**pa_tnc_msg
)
591 pa_tnc_msg_t
*pa_msg
;
592 pa_tnc_attr_t
*error_attr
;
593 linked_list_t
*error_attr_list
;
594 enumerator_t
*enumerator
;
595 TNC_UInt32 src_imc_id
, dst_imv_id
;
596 TNC_ConnectionID connection_id
;
599 connection_id
= state
->get_connection_id(state
);
601 if (state
->has_long(state
))
603 if (dst_imc_id
!= TNC_IMCID_ANY
)
605 DBG2(DBG_IMC
, "IMC %u \"%s\" received message for Connection ID %u "
606 "from IMV %u to IMC %u", this->id
, this->name
,
607 connection_id
, src_imv_id
, dst_imc_id
);
611 DBG2(DBG_IMC
, "IMC %u \"%s\" received message for Connection ID %u "
612 "from IMV %u", this->id
, this->name
, connection_id
,
618 DBG2(DBG_IMC
, "IMC %u \"%s\" received message for Connection ID %u",
619 this->id
, this->name
, connection_id
);
623 pa_msg
= pa_tnc_msg_create_from_data(msg
);
625 switch (pa_msg
->process(pa_msg
))
628 *pa_tnc_msg
= pa_msg
;
631 /* extract and copy by refence all error attributes */
632 error_attr_list
= linked_list_create();
634 enumerator
= pa_msg
->create_error_enumerator(pa_msg
);
635 while (enumerator
->enumerate(enumerator
, &error_attr
))
637 error_attr_list
->insert_last(error_attr_list
,
638 error_attr
->get_ref(error_attr
));
640 enumerator
->destroy(enumerator
);
642 src_imc_id
= (dst_imc_id
== TNC_IMCID_ANY
) ?
this->id
: dst_imc_id
;
643 dst_imv_id
= state
->has_excl(state
) ? src_imv_id
: TNC_IMVID_ANY
;
645 result
= send_message(this, connection_id
, state
->has_excl(state
),
646 src_imc_id
, dst_imv_id
, msg_vid
, msg_subtype
,
649 error_attr_list
->destroy(error_attr_list
);
650 pa_msg
->destroy(pa_msg
);
654 pa_msg
->destroy(pa_msg
);
655 return TNC_RESULT_FATAL
;
657 return TNC_RESULT_SUCCESS
;
660 METHOD(imc_agent_t
, reserve_additional_ids
, TNC_Result
,
661 private_imc_agent_t
*this, int count
)
667 if (!this->reserve_additional_id
)
669 DBG1(DBG_IMC
, "IMC %u \"%s\" did not detect the capability to reserve "
670 "additional IMC IDs from the TNCC", this->id
, this->name
);
671 return TNC_RESULT_ILLEGAL_OPERATION
;
675 result
= this->reserve_additional_id(this->id
, &id
);
676 if (result
!= TNC_RESULT_SUCCESS
)
678 DBG1(DBG_IMC
, "IMC %u \"%s\" failed to reserve %d additional IMC IDs",
679 this->id
, this->name
, count
);
684 /* store the scalar value in the pointer */
686 this->additional_ids
->insert_last(this->additional_ids
, pointer
);
687 DBG2(DBG_IMC
, "IMC %u \"%s\" reserved additional ID %u",
688 this->id
, this->name
, id
);
690 return TNC_RESULT_SUCCESS
;
693 METHOD(imc_agent_t
, count_additional_ids
, int,
694 private_imc_agent_t
*this)
696 return this->additional_ids
->get_count(this->additional_ids
);
699 METHOD(imc_agent_t
, create_id_enumerator
, enumerator_t
*,
700 private_imc_agent_t
*this)
702 return this->additional_ids
->create_enumerator(this->additional_ids
);
705 METHOD(imc_agent_t
, destroy
, void,
706 private_imc_agent_t
*this)
708 DBG1(DBG_IMC
, "IMC %u \"%s\" terminated", this->id
, this->name
);
709 this->additional_ids
->destroy(this->additional_ids
);
710 this->connections
->destroy_function(this->connections
, free
);
711 this->connection_lock
->destroy(this->connection_lock
);
714 /* decrease the reference count or terminate */
719 * Described in header.
721 imc_agent_t
*imc_agent_create(const char *name
,
722 pen_type_t
*supported_types
, u_int32_t type_count
,
723 TNC_IMCID id
, TNC_Version
*actual_version
)
725 private_imc_agent_t
*this;
727 /* initialize or increase the reference count */
735 .bind_functions
= _bind_functions
,
736 .create_state
= _create_state
,
737 .delete_state
= _delete_state
,
738 .change_state
= _change_state
,
739 .get_state
= _get_state
,
740 .send_message
= _send_message
,
741 .receive_message
= _receive_message
,
742 .reserve_additional_ids
= _reserve_additional_ids
,
743 .count_additional_ids
= _count_additional_ids
,
744 .create_id_enumerator
= _create_id_enumerator
,
748 .supported_types
= supported_types
,
749 .type_count
= type_count
,
751 .additional_ids
= linked_list_create(),
752 .connections
= linked_list_create(),
753 .connection_lock
= rwlock_create(RWLOCK_TYPE_DEFAULT
),
756 *actual_version
= TNC_IFIMC_VERSION_1
;
757 DBG1(DBG_IMC
, "IMC %u \"%s\" initialized", this->id
, this->name
);
759 return &this->public;