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
)
336 * Read an UInt32 attribute
338 static u_int32_t
get_uint_attribute(private_imc_agent_t
*this, TNC_ConnectionID id
,
339 TNC_AttributeID attribute_id
)
344 if (this->get_attribute
&&
345 this->get_attribute(this->id
, id
, attribute_id
, 4, buf
, &len
) ==
346 TNC_RESULT_SUCCESS
&& len
== 4)
353 METHOD(imc_agent_t
, create_state
, TNC_Result
,
354 private_imc_agent_t
*this, imc_state_t
*state
)
356 TNC_ConnectionID conn_id
;
357 char *tnccs_p
= NULL
, *tnccs_v
= NULL
, *t_p
= NULL
, *t_v
= NULL
;
358 bool has_long
= FALSE
, has_excl
= FALSE
, has_soh
= FALSE
;
359 u_int32_t max_msg_len
;
361 conn_id
= state
->get_connection_id(state
);
362 if (find_connection(this, conn_id
))
364 DBG1(DBG_IMC
, "IMC %u \"%s\" already created a state for Connection ID %u",
365 this->id
, this->name
, conn_id
);
366 state
->destroy(state
);
367 return TNC_RESULT_OTHER
;
370 /* Get and display attributes from TNCC via IF-IMC */
371 has_long
= get_bool_attribute(this, conn_id
, TNC_ATTRIBUTEID_HAS_LONG_TYPES
);
372 has_excl
= get_bool_attribute(this, conn_id
, TNC_ATTRIBUTEID_HAS_EXCLUSIVE
);
373 has_soh
= get_bool_attribute(this, conn_id
, TNC_ATTRIBUTEID_HAS_SOH
);
374 tnccs_p
= get_str_attribute(this, conn_id
, TNC_ATTRIBUTEID_IFTNCCS_PROTOCOL
);
375 tnccs_v
= get_str_attribute(this, conn_id
, TNC_ATTRIBUTEID_IFTNCCS_VERSION
);
376 t_p
= get_str_attribute(this, conn_id
, TNC_ATTRIBUTEID_IFT_PROTOCOL
);
377 t_v
= get_str_attribute(this, conn_id
, TNC_ATTRIBUTEID_IFT_VERSION
);
378 max_msg_len
= get_uint_attribute(this, conn_id
, TNC_ATTRIBUTEID_MAX_MESSAGE_SIZE
);
380 state
->set_flags(state
, has_long
, has_excl
);
381 state
->set_max_msg_len(state
, max_msg_len
);
383 DBG2(DBG_IMC
, "IMC %u \"%s\" created a state for %s %s Connection ID %u: "
384 "%slong %sexcl %ssoh", this->id
, this->name
,
385 tnccs_p ? tnccs_p
:"?", tnccs_v ? tnccs_v
:"?", conn_id
,
386 has_long ?
"+":"-", has_excl ?
"+":"-", has_soh ?
"+":"-");
387 DBG2(DBG_IMC
, " over %s %s with maximum PA-TNC msg size of %u bytes",
388 t_p ? t_p
:"?", t_v ? t_v
:"?", max_msg_len
);
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
;
401 METHOD(imc_agent_t
, delete_state
, TNC_Result
,
402 private_imc_agent_t
*this, TNC_ConnectionID connection_id
)
404 if (!delete_connection(this, connection_id
))
406 DBG1(DBG_IMC
, "IMC %u \"%s\" has no state for Connection ID %u",
407 this->id
, this->name
, connection_id
);
408 return TNC_RESULT_FATAL
;
410 DBG2(DBG_IMC
, "IMC %u \"%s\" deleted the state of Connection ID %u",
411 this->id
, this->name
, connection_id
);
412 return TNC_RESULT_SUCCESS
;
415 METHOD(imc_agent_t
, change_state
, TNC_Result
,
416 private_imc_agent_t
*this, TNC_ConnectionID connection_id
,
417 TNC_ConnectionState new_state
,
418 imc_state_t
**state_p
)
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
);
432 DBG1(DBG_IMC
, "IMC %u \"%s\" has no state for Connection ID %u",
433 this->id
, this->name
, connection_id
);
434 return TNC_RESULT_FATAL
;
436 state
->change_state(state
, new_state
);
437 DBG2(DBG_IMC
, "IMC %u \"%s\" changed state of Connection ID %u to '%N'",
438 this->id
, this->name
, connection_id
,
439 TNC_Connection_State_names
, new_state
);
445 case TNC_CONNECTION_STATE_CREATE
:
446 DBG1(DBG_IMC
, "state '%N' should be handled by create_state()",
447 TNC_Connection_State_names
, new_state
);
448 return TNC_RESULT_FATAL
;
449 case TNC_CONNECTION_STATE_DELETE
:
450 DBG1(DBG_IMC
, "state '%N' should be handled by delete_state()",
451 TNC_Connection_State_names
, new_state
);
452 return TNC_RESULT_FATAL
;
454 DBG1(DBG_IMC
, "IMC %u \"%s\" was notified of unknown state %u "
455 "for Connection ID %u",
456 this->id
, this->name
, new_state
, connection_id
);
457 return TNC_RESULT_INVALID_PARAMETER
;
459 return TNC_RESULT_SUCCESS
;
462 METHOD(imc_agent_t
, get_state
, bool,
463 private_imc_agent_t
*this, TNC_ConnectionID connection_id
,
466 *state
= find_connection(this, connection_id
);
469 DBG1(DBG_IMC
, "IMC %u \"%s\" has no state for Connection ID %u",
470 this->id
, this->name
, connection_id
);
476 METHOD(imc_agent_t
, send_message
, TNC_Result
,
477 private_imc_agent_t
*this, TNC_ConnectionID connection_id
, bool excl
,
478 TNC_UInt32 src_imc_id
, TNC_UInt32 dst_imv_id
, linked_list_t
*attr_list
)
480 TNC_MessageType type
;
481 TNC_UInt32 msg_flags
;
482 TNC_Result result
= TNC_RESULT_FATAL
;
485 pa_tnc_msg_t
*pa_tnc_msg
;
487 enumerator_t
*enumerator
;
489 state
= find_connection(this, connection_id
);
492 DBG1(DBG_IMV
, "IMC %u \"%s\" has no state for Connection ID %u",
493 this->id
, this->name
, connection_id
);
494 return TNC_RESULT_FATAL
;
497 while (attr_list
->get_count(attr_list
))
499 pa_tnc_msg
= pa_tnc_msg_create(state
->get_max_msg_len(state
));
501 enumerator
= attr_list
->create_enumerator(attr_list
);
502 while (enumerator
->enumerate(enumerator
, &attr
))
504 if (!pa_tnc_msg
->add_attribute(pa_tnc_msg
, attr
))
508 attr_list
->remove_at(attr_list
, enumerator
);
510 enumerator
->destroy(enumerator
);
512 /* build and send the PA-TNC message via the IF-IMC interface */
513 pa_tnc_msg
->build(pa_tnc_msg
);
514 msg
= pa_tnc_msg
->get_encoding(pa_tnc_msg
);
516 if (state
->has_long(state
) && this->send_message_long
)
520 src_imc_id
= this->id
;
522 msg_flags
= excl ? TNC_MESSAGE_FLAGS_EXCLUSIVE
: 0;
524 result
= this->send_message_long(src_imc_id
, connection_id
,
525 msg_flags
, msg
.ptr
, msg
.len
, this->vendor_id
,
526 this->subtype
, dst_imv_id
);
528 else if (this->send_message
)
530 type
= (this->vendor_id
<< 8) | this->subtype
;
532 result
= this->send_message(this->id
, connection_id
, msg
.ptr
,
536 pa_tnc_msg
->destroy(pa_tnc_msg
);
538 if (result
!= TNC_RESULT_SUCCESS
)
546 METHOD(imc_agent_t
, receive_message
, TNC_Result
,
547 private_imc_agent_t
*this, imc_state_t
*state
, chunk_t msg
,
548 TNC_VendorID msg_vid
, TNC_MessageSubtype msg_subtype
,
549 TNC_UInt32 src_imv_id
, TNC_UInt32 dst_imc_id
, pa_tnc_msg_t
**pa_tnc_msg
)
551 pa_tnc_msg_t
*pa_msg
;
552 pa_tnc_attr_t
*error_attr
;
553 linked_list_t
*error_attr_list
;
554 enumerator_t
*enumerator
;
555 TNC_UInt32 src_imc_id
, dst_imv_id
;
556 TNC_ConnectionID connection_id
;
559 connection_id
= state
->get_connection_id(state
);
561 if (state
->has_long(state
))
563 if (dst_imc_id
!= TNC_IMCID_ANY
)
565 DBG2(DBG_IMC
, "IMC %u \"%s\" received message for Connection ID %u "
566 "from IMV %u to IMC %u", this->id
, this->name
,
567 connection_id
, src_imv_id
, dst_imc_id
);
571 DBG2(DBG_IMC
, "IMC %u \"%s\" received message for Connection ID %u "
572 "from IMV %u", this->id
, this->name
, connection_id
,
578 DBG2(DBG_IMC
, "IMC %u \"%s\" received message for Connection ID %u",
579 this->id
, this->name
, connection_id
);
583 pa_msg
= pa_tnc_msg_create_from_data(msg
);
585 switch (pa_msg
->process(pa_msg
))
588 *pa_tnc_msg
= pa_msg
;
591 /* extract and copy by refence all error attributes */
592 error_attr_list
= linked_list_create();
594 enumerator
= pa_msg
->create_error_enumerator(pa_msg
);
595 while (enumerator
->enumerate(enumerator
, &error_attr
))
597 error_attr_list
->insert_last(error_attr_list
,
598 error_attr
->get_ref(error_attr
));
600 enumerator
->destroy(enumerator
);
602 src_imc_id
= (dst_imc_id
== TNC_IMCID_ANY
) ?
this->id
: dst_imc_id
;
603 dst_imv_id
= state
->has_excl(state
) ? src_imv_id
: TNC_IMVID_ANY
;
605 result
= send_message(this, connection_id
, state
->has_excl(state
),
606 src_imc_id
, dst_imv_id
, error_attr_list
);
608 error_attr_list
->destroy(error_attr_list
);
609 pa_msg
->destroy(pa_msg
);
613 pa_msg
->destroy(pa_msg
);
614 return TNC_RESULT_FATAL
;
616 return TNC_RESULT_SUCCESS
;
619 METHOD(imc_agent_t
, reserve_additional_ids
, TNC_Result
,
620 private_imc_agent_t
*this, int count
)
626 if (!this->reserve_additional_id
)
628 DBG1(DBG_IMC
, "IMC %u \"%s\" did not detect the capability to reserve "
629 "additional IMC IDs from the TNCC", this->id
, this->name
);
630 return TNC_RESULT_ILLEGAL_OPERATION
;
634 result
= this->reserve_additional_id(this->id
, &id
);
635 if (result
!= TNC_RESULT_SUCCESS
)
637 DBG1(DBG_IMC
, "IMC %u \"%s\" failed to reserve %d additional IMC IDs",
638 this->id
, this->name
, count
);
643 /* store the scalar value in the pointer */
645 this->additional_ids
->insert_last(this->additional_ids
, pointer
);
646 DBG2(DBG_IMC
, "IMC %u \"%s\" reserved additional ID %u",
647 this->id
, this->name
, id
);
649 return TNC_RESULT_SUCCESS
;
652 METHOD(imc_agent_t
, count_additional_ids
, int,
653 private_imc_agent_t
*this)
655 return this->additional_ids
->get_count(this->additional_ids
);
658 METHOD(imc_agent_t
, create_id_enumerator
, enumerator_t
*,
659 private_imc_agent_t
*this)
661 return this->additional_ids
->create_enumerator(this->additional_ids
);
664 METHOD(imc_agent_t
, destroy
, void,
665 private_imc_agent_t
*this)
667 DBG1(DBG_IMC
, "IMC %u \"%s\" terminated", this->id
, this->name
);
668 this->additional_ids
->destroy(this->additional_ids
);
669 this->connections
->destroy_function(this->connections
, free
);
670 this->connection_lock
->destroy(this->connection_lock
);
673 /* decrease the reference count or terminate */
678 * Described in header.
680 imc_agent_t
*imc_agent_create(const char *name
,
681 pen_t vendor_id
, u_int32_t subtype
,
682 TNC_IMCID id
, TNC_Version
*actual_version
)
684 private_imc_agent_t
*this;
686 /* initialize or increase the reference count */
694 .bind_functions
= _bind_functions
,
695 .create_state
= _create_state
,
696 .delete_state
= _delete_state
,
697 .change_state
= _change_state
,
698 .get_state
= _get_state
,
699 .send_message
= _send_message
,
700 .receive_message
= _receive_message
,
701 .reserve_additional_ids
= _reserve_additional_ids
,
702 .count_additional_ids
= _count_additional_ids
,
703 .create_id_enumerator
= _create_id_enumerator
,
707 .vendor_id
= vendor_id
,
710 .additional_ids
= linked_list_create(),
711 .connections
= linked_list_create(),
712 .connection_lock
= rwlock_create(RWLOCK_TYPE_DEFAULT
),
715 *actual_version
= TNC_IFIMC_VERSION_1
;
716 DBG1(DBG_IMC
, "IMC %u \"%s\" initialized", this->id
, this->name
);
718 return &this->public;