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>
21 #include <utils/debug.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 * Get the value of an attribute associated with a connection
99 * or with the TNCC as a whole.
101 * @param imc_id IMC ID assigned by TNCC
102 * @param connection_id network connection ID assigned by TNCC
103 * @param attribute_id attribute ID
104 * @param buffer_len length of buffer in bytes
105 * @param buffer buffer
106 * @param out_value_len size in bytes of attribute stored in buffer
107 * @return TNC result code
109 TNC_Result (*get_attribute
)(TNC_IMCID imc_id
,
110 TNC_ConnectionID connection_id
,
111 TNC_AttributeID attribute_id
,
112 TNC_UInt32 buffer_len
,
113 TNC_BufferReference buffer
,
114 TNC_UInt32
*out_value_len
);
117 * Set the value of an attribute associated with a connection
118 * or with the TNCC as a whole.
120 * @param imc_id IMV ID assigned by TNCC
121 * @param connection_id network connection ID assigned by TNCC
122 * @param attribute_id attribute ID
123 * @param buffer_len length of buffer in bytes
124 * @param buffer buffer
125 * @return TNC result code
127 TNC_Result (*set_attribute
)(TNC_IMCID imc_id
,
128 TNC_ConnectionID connection_id
,
129 TNC_AttributeID attribute_id
,
130 TNC_UInt32 buffer_len
,
131 TNC_BufferReference buffer
);
134 * Reserve an additional IMC ID
136 * @param imc_id primary IMC ID assigned by TNCC
137 * @param out_imc_id additional IMC ID assigned by TNCC
138 * @return TNC result code
140 TNC_Result (*reserve_additional_id
)(TNC_IMCID imc_id
,
141 TNC_UInt32
*out_imc_id
);
145 METHOD(imc_agent_t
, bind_functions
, TNC_Result
,
146 private_imc_agent_t
*this, TNC_TNCC_BindFunctionPointer bind_function
)
150 DBG1(DBG_IMC
, "TNC client failed to provide bind function");
151 return TNC_RESULT_INVALID_PARAMETER
;
153 if (bind_function(this->id
, "TNC_TNCC_ReportMessageTypes",
154 (void**)&this->report_message_types
) != TNC_RESULT_SUCCESS
)
156 this->report_message_types
= NULL
;
158 if (bind_function(this->id
, "TNC_TNCC_ReportMessageTypesLong",
159 (void**)&this->report_message_types_long
) != TNC_RESULT_SUCCESS
)
161 this->report_message_types_long
= NULL
;
163 if (bind_function(this->id
, "TNC_TNCC_RequestHandshakeRetry",
164 (void**)&this->public.request_handshake_retry
) != TNC_RESULT_SUCCESS
)
166 this->public.request_handshake_retry
= NULL
;
168 if (bind_function(this->id
, "TNC_TNCC_SendMessage",
169 (void**)&this->public.send_message
) != TNC_RESULT_SUCCESS
)
171 this->public.send_message
= NULL
;
173 if (bind_function(this->id
, "TNC_TNCC_SendMessageLong",
174 (void**)&this->public.send_message_long
) != TNC_RESULT_SUCCESS
)
176 this->public.send_message_long
= NULL
;
178 if (bind_function(this->id
, "TNC_TNCC_GetAttribute",
179 (void**)&this->get_attribute
) != TNC_RESULT_SUCCESS
)
181 this->get_attribute
= NULL
;
183 if (bind_function(this->id
, "TNC_TNCC_SetAttribute",
184 (void**)&this->set_attribute
) != TNC_RESULT_SUCCESS
)
186 this->set_attribute
= NULL
;
188 if (bind_function(this->id
, "TNC_TNCC_ReserveAdditionalIMCID",
189 (void**)&this->reserve_additional_id
) != TNC_RESULT_SUCCESS
)
191 this->reserve_additional_id
= NULL
;
193 DBG2(DBG_IMC
, "IMC %u \"%s\" provided with bind function",
194 this->id
, this->name
);
196 if (this->report_message_types_long
)
198 TNC_VendorIDList vendor_id_list
;
199 TNC_MessageSubtypeList subtype_list
;
202 vendor_id_list
= malloc(this->type_count
* sizeof(TNC_UInt32
));
203 subtype_list
= malloc(this->type_count
* sizeof(TNC_UInt32
));
205 for (i
= 0; i
< this->type_count
; i
++)
207 vendor_id_list
[i
] = this->supported_types
[i
].vendor_id
;
208 subtype_list
[i
] = this->supported_types
[i
].type
;
210 this->report_message_types_long(this->id
, vendor_id_list
, subtype_list
,
212 free(vendor_id_list
);
215 else if (this->report_message_types
)
217 TNC_MessageTypeList type_list
;
220 type_list
= malloc(this->type_count
* sizeof(TNC_UInt32
));
222 for (i
= 0; i
< this->type_count
; i
++)
224 type_list
[i
] = (this->supported_types
[i
].vendor_id
<< 8) |
225 (this->supported_types
[i
].type
& 0xff);
227 this->report_message_types(this->id
, type_list
, this->type_count
);
230 return TNC_RESULT_SUCCESS
;
234 * finds a connection state based on its Connection ID
236 static imc_state_t
* find_connection(private_imc_agent_t
*this,
239 enumerator_t
*enumerator
;
240 imc_state_t
*state
, *found
= NULL
;
242 this->connection_lock
->read_lock(this->connection_lock
);
243 enumerator
= this->connections
->create_enumerator(this->connections
);
244 while (enumerator
->enumerate(enumerator
, &state
))
246 if (id
== state
->get_connection_id(state
))
252 enumerator
->destroy(enumerator
);
253 this->connection_lock
->unlock(this->connection_lock
);
259 * delete a connection state with a given Connection ID
261 static bool delete_connection(private_imc_agent_t
*this, TNC_ConnectionID id
)
263 enumerator_t
*enumerator
;
267 this->connection_lock
->write_lock(this->connection_lock
);
268 enumerator
= this->connections
->create_enumerator(this->connections
);
269 while (enumerator
->enumerate(enumerator
, &state
))
271 if (id
== state
->get_connection_id(state
))
274 state
->destroy(state
);
275 this->connections
->remove_at(this->connections
, enumerator
);
279 enumerator
->destroy(enumerator
);
280 this->connection_lock
->unlock(this->connection_lock
);
286 * Read a boolean attribute
288 static bool get_bool_attribute(private_imc_agent_t
*this, TNC_ConnectionID id
,
289 TNC_AttributeID attribute_id
)
294 return this->get_attribute
&&
295 this->get_attribute(this->id
, id
, attribute_id
, 4, buf
, &len
) ==
296 TNC_RESULT_SUCCESS
&& len
== 1 && *buf
== 0x01;
300 * Read a string attribute
302 static char* get_str_attribute(private_imc_agent_t
*this, TNC_ConnectionID id
,
303 TNC_AttributeID attribute_id
)
308 if (this->get_attribute
&&
309 this->get_attribute(this->id
, id
, attribute_id
, BUF_LEN
, buf
, &len
) ==
310 TNC_RESULT_SUCCESS
&& len
<= BUF_LEN
)
318 * Read an UInt32 attribute
320 static u_int32_t
get_uint_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
, 4, buf
, &len
) ==
328 TNC_RESULT_SUCCESS
&& len
== 4)
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
;
341 u_int32_t max_msg_len
;
343 conn_id
= state
->get_connection_id(state
);
344 if (find_connection(this, conn_id
))
346 DBG1(DBG_IMC
, "IMC %u \"%s\" already created a state for Connection ID %u",
347 this->id
, this->name
, conn_id
);
348 state
->destroy(state
);
349 return TNC_RESULT_OTHER
;
352 /* Get and display attributes from TNCC via IF-IMC */
353 has_long
= get_bool_attribute(this, conn_id
, TNC_ATTRIBUTEID_HAS_LONG_TYPES
);
354 has_excl
= get_bool_attribute(this, conn_id
, TNC_ATTRIBUTEID_HAS_EXCLUSIVE
);
355 has_soh
= get_bool_attribute(this, conn_id
, TNC_ATTRIBUTEID_HAS_SOH
);
356 tnccs_p
= get_str_attribute(this, conn_id
, TNC_ATTRIBUTEID_IFTNCCS_PROTOCOL
);
357 tnccs_v
= get_str_attribute(this, conn_id
, TNC_ATTRIBUTEID_IFTNCCS_VERSION
);
358 t_p
= get_str_attribute(this, conn_id
, TNC_ATTRIBUTEID_IFT_PROTOCOL
);
359 t_v
= get_str_attribute(this, conn_id
, TNC_ATTRIBUTEID_IFT_VERSION
);
360 max_msg_len
= get_uint_attribute(this, conn_id
, TNC_ATTRIBUTEID_MAX_MESSAGE_SIZE
);
362 state
->set_flags(state
, has_long
, has_excl
);
363 state
->set_max_msg_len(state
, max_msg_len
);
365 DBG2(DBG_IMC
, "IMC %u \"%s\" created a state for %s %s Connection ID %u: "
366 "%slong %sexcl %ssoh", this->id
, this->name
,
367 tnccs_p ? tnccs_p
:"?", tnccs_v ? tnccs_v
:"?", conn_id
,
368 has_long ?
"+":"-", has_excl ?
"+":"-", has_soh ?
"+":"-");
369 DBG2(DBG_IMC
, " over %s %s with maximum PA-TNC message size of %u bytes",
370 t_p ? t_p
:"?", t_v ? t_v
:"?", max_msg_len
);
377 this->connection_lock
->write_lock(this->connection_lock
);
378 this->connections
->insert_last(this->connections
, state
);
379 this->connection_lock
->unlock(this->connection_lock
);
380 return TNC_RESULT_SUCCESS
;
383 METHOD(imc_agent_t
, delete_state
, TNC_Result
,
384 private_imc_agent_t
*this, TNC_ConnectionID connection_id
)
386 if (!delete_connection(this, connection_id
))
388 DBG1(DBG_IMC
, "IMC %u \"%s\" has no state for Connection ID %u",
389 this->id
, this->name
, connection_id
);
390 return TNC_RESULT_FATAL
;
392 DBG2(DBG_IMC
, "IMC %u \"%s\" deleted the state of Connection ID %u",
393 this->id
, this->name
, connection_id
);
394 return TNC_RESULT_SUCCESS
;
397 METHOD(imc_agent_t
, change_state
, TNC_Result
,
398 private_imc_agent_t
*this, TNC_ConnectionID connection_id
,
399 TNC_ConnectionState new_state
,
400 imc_state_t
**state_p
)
406 case TNC_CONNECTION_STATE_HANDSHAKE
:
407 case TNC_CONNECTION_STATE_ACCESS_ALLOWED
:
408 case TNC_CONNECTION_STATE_ACCESS_ISOLATED
:
409 case TNC_CONNECTION_STATE_ACCESS_NONE
:
410 state
= find_connection(this, connection_id
);
414 DBG1(DBG_IMC
, "IMC %u \"%s\" has no state for Connection ID %u",
415 this->id
, this->name
, connection_id
);
416 return TNC_RESULT_FATAL
;
418 state
->change_state(state
, new_state
);
419 DBG2(DBG_IMC
, "IMC %u \"%s\" changed state of Connection ID %u to '%N'",
420 this->id
, this->name
, connection_id
,
421 TNC_Connection_State_names
, new_state
);
427 case TNC_CONNECTION_STATE_CREATE
:
428 DBG1(DBG_IMC
, "state '%N' should be handled by create_state()",
429 TNC_Connection_State_names
, new_state
);
430 return TNC_RESULT_FATAL
;
431 case TNC_CONNECTION_STATE_DELETE
:
432 DBG1(DBG_IMC
, "state '%N' should be handled by delete_state()",
433 TNC_Connection_State_names
, new_state
);
434 return TNC_RESULT_FATAL
;
436 DBG1(DBG_IMC
, "IMC %u \"%s\" was notified of unknown state %u "
437 "for Connection ID %u",
438 this->id
, this->name
, new_state
, connection_id
);
439 return TNC_RESULT_INVALID_PARAMETER
;
441 return TNC_RESULT_SUCCESS
;
444 METHOD(imc_agent_t
, get_state
, bool,
445 private_imc_agent_t
*this, TNC_ConnectionID connection_id
,
448 *state
= find_connection(this, connection_id
);
451 DBG1(DBG_IMC
, "IMC %u \"%s\" has no state for Connection ID %u",
452 this->id
, this->name
, connection_id
);
458 METHOD(imc_agent_t
, get_name
, const char*,
459 private_imc_agent_t
*this)
464 METHOD(imc_agent_t
, get_id
, TNC_IMCID
,
465 private_imc_agent_t
*this)
470 METHOD(imc_agent_t
, reserve_additional_ids
, TNC_Result
,
471 private_imc_agent_t
*this, int count
)
477 if (!this->reserve_additional_id
)
479 DBG1(DBG_IMC
, "IMC %u \"%s\" did not detect the capability to reserve "
480 "additional IMC IDs from the TNCC", this->id
, this->name
);
481 return TNC_RESULT_ILLEGAL_OPERATION
;
485 result
= this->reserve_additional_id(this->id
, &id
);
486 if (result
!= TNC_RESULT_SUCCESS
)
488 DBG1(DBG_IMC
, "IMC %u \"%s\" failed to reserve %d additional IMC IDs",
489 this->id
, this->name
, count
);
494 /* store the scalar value in the pointer */
496 this->additional_ids
->insert_last(this->additional_ids
, pointer
);
497 DBG2(DBG_IMC
, "IMC %u \"%s\" reserved additional ID %u",
498 this->id
, this->name
, id
);
500 return TNC_RESULT_SUCCESS
;
503 METHOD(imc_agent_t
, count_additional_ids
, int,
504 private_imc_agent_t
*this)
506 return this->additional_ids
->get_count(this->additional_ids
);
509 METHOD(imc_agent_t
, create_id_enumerator
, enumerator_t
*,
510 private_imc_agent_t
*this)
512 return this->additional_ids
->create_enumerator(this->additional_ids
);
515 METHOD(imc_agent_t
, destroy
, void,
516 private_imc_agent_t
*this)
518 DBG1(DBG_IMC
, "IMC %u \"%s\" terminated", this->id
, this->name
);
519 this->additional_ids
->destroy(this->additional_ids
);
520 this->connections
->destroy_function(this->connections
, free
);
521 this->connection_lock
->destroy(this->connection_lock
);
524 /* decrease the reference count or terminate */
529 * Described in header.
531 imc_agent_t
*imc_agent_create(const char *name
,
532 pen_type_t
*supported_types
, u_int32_t type_count
,
533 TNC_IMCID id
, TNC_Version
*actual_version
)
535 private_imc_agent_t
*this;
537 /* initialize or increase the reference count */
545 .bind_functions
= _bind_functions
,
546 .create_state
= _create_state
,
547 .delete_state
= _delete_state
,
548 .change_state
= _change_state
,
549 .get_state
= _get_state
,
550 .get_name
= _get_name
,
552 .reserve_additional_ids
= _reserve_additional_ids
,
553 .count_additional_ids
= _count_additional_ids
,
554 .create_id_enumerator
= _create_id_enumerator
,
558 .supported_types
= supported_types
,
559 .type_count
= type_count
,
561 .additional_ids
= linked_list_create(),
562 .connections
= linked_list_create(),
563 .connection_lock
= rwlock_create(RWLOCK_TYPE_DEFAULT
),
566 *actual_version
= TNC_IFIMC_VERSION_1
;
567 DBG1(DBG_IMC
, "IMC %u \"%s\" initialized", this->id
, this->name
);
569 return &this->public;