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 <utils/linked_list.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 vendor ID of IMC
44 TNC_VendorID vendor_id
;
47 * message subtype of IMC
49 TNC_MessageSubtype subtype
;
52 * ID of IMC as assigned by TNCC
57 * list of TNCC connection entries
59 linked_list_t
*connections
;
62 * rwlock to lock TNCC connection entries
64 rwlock_t
*connection_lock
;
67 * Inform a TNCC about the set of message types the IMC is able to receive
69 * @param imc_id IMC ID assigned by TNCC
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_IMCID imc_id
,
75 TNC_MessageTypeList supported_types
,
76 TNC_UInt32 type_count
);
79 * Inform a TNCC about the set of message types the IMC is able to receive
81 * @param imc_id IMC ID assigned by TNCC
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_IMCID imc_id
,
88 TNC_VendorIDList supported_vids
,
89 TNC_MessageSubtypeList supported_subtypes
,
90 TNC_UInt32 type_count
);
93 * Call when an IMC-IMC message is to be sent
95 * @param imc_id IMC ID assigned by TNCC
96 * @param connection_id network connection ID assigned by TNCC
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_IMCID imc_id
,
103 TNC_ConnectionID connection_id
,
104 TNC_BufferReference msg
,
106 TNC_MessageType msg_type
);
109 * Get the value of an attribute associated with a connection
110 * or with the TNCC as a whole.
112 * @param imc_id IMC ID assigned by TNCC
113 * @param connection_id network connection ID assigned by TNCC
114 * @param attribute_id attribute ID
115 * @param buffer_len length of buffer in bytes
116 * @param buffer buffer
117 * @param out_value_len size in bytes of attribute stored in buffer
118 * @return TNC result code
120 TNC_Result (*get_attribute
)(TNC_IMCID imc_id
,
121 TNC_ConnectionID connection_id
,
122 TNC_AttributeID attribute_id
,
123 TNC_UInt32 buffer_len
,
124 TNC_BufferReference buffer
,
125 TNC_UInt32
*out_value_len
);
128 * Set the value of an attribute associated with a connection
129 * or with the TNCC as a whole.
131 * @param imc_id IMV ID assigned by TNCC
132 * @param connection_id network connection ID assigned by TNCC
133 * @param attribute_id attribute ID
134 * @param buffer_len length of buffer in bytes
135 * @param buffer buffer
136 * @return TNC result code
138 TNC_Result (*set_attribute
)(TNC_IMCID imc_id
,
139 TNC_ConnectionID connection_id
,
140 TNC_AttributeID attribute_id
,
141 TNC_UInt32 buffer_len
,
142 TNC_BufferReference buffer
);
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->send_message
) != TNC_RESULT_SUCCESS
)
171 this->send_message
= NULL
;
173 if (bind_function(this->id
, "TNC_TNCC_GetAttribute",
174 (void**)&this->get_attribute
) != TNC_RESULT_SUCCESS
)
176 this->get_attribute
= NULL
;
178 if (bind_function(this->id
, "TNC_TNCC_SetAttribute",
179 (void**)&this->set_attribute
) != TNC_RESULT_SUCCESS
)
181 this->set_attribute
= NULL
;
183 DBG2(DBG_IMC
, "IMC %u \"%s\" provided with bind function",
184 this->id
, this->name
);
186 if (this->report_message_types_long
)
188 this->report_message_types_long(this->id
, &this->vendor_id
,
191 else if (this->report_message_types
&&
192 this->vendor_id
<= TNC_VENDORID_ANY
&&
193 this->subtype
<= TNC_SUBTYPE_ANY
)
195 TNC_MessageType type
;
197 type
= (this->vendor_id
<< 8) | this->subtype
;
198 this->report_message_types(this->id
, &type
, 1);
200 return TNC_RESULT_SUCCESS
;
204 * finds a connection state based on its Connection ID
206 static imc_state_t
* find_connection(private_imc_agent_t
*this,
209 enumerator_t
*enumerator
;
210 imc_state_t
*state
, *found
= NULL
;
212 this->connection_lock
->read_lock(this->connection_lock
);
213 enumerator
= this->connections
->create_enumerator(this->connections
);
214 while (enumerator
->enumerate(enumerator
, &state
))
216 if (id
== state
->get_connection_id(state
))
222 enumerator
->destroy(enumerator
);
223 this->connection_lock
->unlock(this->connection_lock
);
229 * delete a connection state with a given Connection ID
231 static bool delete_connection(private_imc_agent_t
*this, TNC_ConnectionID id
)
233 enumerator_t
*enumerator
;
237 this->connection_lock
->write_lock(this->connection_lock
);
238 enumerator
= this->connections
->create_enumerator(this->connections
);
239 while (enumerator
->enumerate(enumerator
, &state
))
241 if (id
== state
->get_connection_id(state
))
244 state
->destroy(state
);
245 this->connections
->remove_at(this->connections
, enumerator
);
249 enumerator
->destroy(enumerator
);
250 this->connection_lock
->unlock(this->connection_lock
);
256 * Read a boolean attribute
258 static bool get_bool_attribute(private_imc_agent_t
*this, TNC_ConnectionID id
,
259 TNC_AttributeID attribute_id
)
264 return this->get_attribute
&&
265 this->get_attribute(this->id
, id
, attribute_id
, 4, buf
, &len
) ==
266 TNC_RESULT_SUCCESS
&& len
== 1 && *buf
== 0x01;
270 * Read a string attribute
272 static char* get_str_attribute(private_imc_agent_t
*this, TNC_ConnectionID id
,
273 TNC_AttributeID attribute_id
)
278 if (this->get_attribute
&&
279 this->get_attribute(this->id
, id
, attribute_id
, BUF_LEN
, buf
, &len
) ==
280 TNC_RESULT_SUCCESS
&& len
<= BUF_LEN
)
287 METHOD(imc_agent_t
, create_state
, TNC_Result
,
288 private_imc_agent_t
*this, imc_state_t
*state
)
290 TNC_ConnectionID conn_id
;
291 char *tnccs_p
= NULL
, *tnccs_v
= NULL
, *t_p
= NULL
, *t_v
= NULL
;
292 bool has_long
= FALSE
, has_excl
= FALSE
, has_soh
= FALSE
;
294 conn_id
= state
->get_connection_id(state
);
295 if (find_connection(this, conn_id
))
297 DBG1(DBG_IMC
, "IMC %u \"%s\" already created a state for Connection ID %u",
298 this->id
, this->name
, conn_id
);
299 state
->destroy(state
);
300 return TNC_RESULT_OTHER
;
303 /* Get and display attributes from TNCC via IF-IMC */
304 has_long
= get_bool_attribute(this, conn_id
, TNC_ATTRIBUTEID_HAS_LONG_TYPES
);
305 has_excl
= get_bool_attribute(this, conn_id
, TNC_ATTRIBUTEID_HAS_EXCLUSIVE
);
306 has_soh
= get_bool_attribute(this, conn_id
, TNC_ATTRIBUTEID_HAS_SOH
);
307 tnccs_p
= get_str_attribute(this, conn_id
, TNC_ATTRIBUTEID_IFTNCCS_PROTOCOL
);
308 tnccs_v
= get_str_attribute(this, conn_id
, TNC_ATTRIBUTEID_IFTNCCS_VERSION
);
309 t_p
= get_str_attribute(this, conn_id
, TNC_ATTRIBUTEID_IFT_PROTOCOL
);
310 t_v
= get_str_attribute(this, conn_id
, TNC_ATTRIBUTEID_IFT_VERSION
);
312 state
->set_flags(state
, has_long
, has_excl
);
314 DBG2(DBG_IMC
, "IMC %u \"%s\" created a state for Connection ID %u: "
315 "%s %s with %slong %sexcl %ssoh over %s %s",
316 this->id
, this->name
, conn_id
, tnccs_p ? tnccs_p
:"?",
317 tnccs_v ? tnccs_v
:"?", has_long ?
"+":"-", has_excl ?
"+":"-",
318 has_soh ?
"+":"-", t_p ? t_p
:"?", t_v ? t_v
:"?");
324 this->connection_lock
->write_lock(this->connection_lock
);
325 this->connections
->insert_last(this->connections
, state
);
326 this->connection_lock
->unlock(this->connection_lock
);
327 return TNC_RESULT_SUCCESS
;
330 METHOD(imc_agent_t
, delete_state
, TNC_Result
,
331 private_imc_agent_t
*this, TNC_ConnectionID connection_id
)
333 if (!delete_connection(this, connection_id
))
335 DBG1(DBG_IMC
, "IMC %u \"%s\" has no state for Connection ID %u",
336 this->id
, this->name
, connection_id
);
337 return TNC_RESULT_FATAL
;
339 DBG2(DBG_IMC
, "IMC %u \"%s\" deleted the state of Connection ID %u",
340 this->id
, this->name
, connection_id
);
341 return TNC_RESULT_SUCCESS
;
344 METHOD(imc_agent_t
, change_state
, TNC_Result
,
345 private_imc_agent_t
*this, TNC_ConnectionID connection_id
,
346 TNC_ConnectionState new_state
,
347 imc_state_t
**state_p
)
353 case TNC_CONNECTION_STATE_HANDSHAKE
:
354 case TNC_CONNECTION_STATE_ACCESS_ALLOWED
:
355 case TNC_CONNECTION_STATE_ACCESS_ISOLATED
:
356 case TNC_CONNECTION_STATE_ACCESS_NONE
:
357 state
= find_connection(this, connection_id
);
361 DBG1(DBG_IMC
, "IMC %u \"%s\" has no state for Connection ID %u",
362 this->id
, this->name
, connection_id
);
363 return TNC_RESULT_FATAL
;
365 state
->change_state(state
, new_state
);
366 DBG2(DBG_IMC
, "IMC %u \"%s\" changed state of Connection ID %u to '%N'",
367 this->id
, this->name
, connection_id
,
368 TNC_Connection_State_names
, new_state
);
374 case TNC_CONNECTION_STATE_CREATE
:
375 DBG1(DBG_IMC
, "state '%N' should be handled by create_state()",
376 TNC_Connection_State_names
, new_state
);
377 return TNC_RESULT_FATAL
;
378 case TNC_CONNECTION_STATE_DELETE
:
379 DBG1(DBG_IMC
, "state '%N' should be handled by delete_state()",
380 TNC_Connection_State_names
, new_state
);
381 return TNC_RESULT_FATAL
;
383 DBG1(DBG_IMC
, "IMC %u \"%s\" was notified of unknown state %u "
384 "for Connection ID %u",
385 this->id
, this->name
, new_state
, connection_id
);
386 return TNC_RESULT_INVALID_PARAMETER
;
388 return TNC_RESULT_SUCCESS
;
391 METHOD(imc_agent_t
, get_state
, bool,
392 private_imc_agent_t
*this, TNC_ConnectionID connection_id
,
395 *state
= find_connection(this, connection_id
);
398 DBG1(DBG_IMC
, "IMC %u \"%s\" has no state for Connection ID %u",
399 this->id
, this->name
, connection_id
);
405 METHOD(imc_agent_t
, send_message
, TNC_Result
,
406 private_imc_agent_t
*this, TNC_ConnectionID connection_id
, chunk_t msg
)
408 TNC_MessageType type
;
410 if (!this->send_message
)
412 return TNC_RESULT_FATAL
;
414 type
= (this->vendor_id
<< 8) | this->subtype
;
415 return this->send_message(this->id
, connection_id
, msg
.ptr
, msg
.len
, type
);
418 METHOD(imc_agent_t
, receive_message
, TNC_Result
,
419 private_imc_agent_t
*this, TNC_ConnectionID connection_id
, chunk_t msg
,
420 TNC_MessageType msg_type
, pa_tnc_msg_t
**pa_tnc_msg
)
422 pa_tnc_msg_t
*pa_msg
, *error_msg
;
423 pa_tnc_attr_t
*error_attr
;
424 enumerator_t
*enumerator
;
427 DBG2(DBG_IMV
, "IMC %u \"%s\" received message type 0x%08x for Connection ID %u",
428 this->id
, this->name
, msg_type
, connection_id
);
431 pa_msg
= pa_tnc_msg_create_from_data(msg
);
433 switch (pa_msg
->process(pa_msg
))
436 *pa_tnc_msg
= pa_msg
;
439 if (!this->send_message
)
441 /* TNCC doen't have a SendMessage() function */
442 return TNC_RESULT_FATAL
;
445 /* build error message */
446 error_msg
= pa_tnc_msg_create();
447 enumerator
= pa_msg
->create_error_enumerator(pa_msg
);
448 while (enumerator
->enumerate(enumerator
, &error_attr
))
450 error_msg
->add_attribute(error_msg
,
451 error_attr
->get_ref(error_attr
));
453 enumerator
->destroy(enumerator
);
454 error_msg
->build(error_msg
);
456 /* send error message */
457 msg
= error_msg
->get_encoding(error_msg
);
458 result
= this->send_message(this->id
, connection_id
,
459 msg
.ptr
, msg
.len
, msg_type
);
462 error_msg
->destroy(error_msg
);
463 pa_msg
->destroy(pa_msg
);
467 pa_msg
->destroy(pa_msg
);
468 return TNC_RESULT_FATAL
;
470 return TNC_RESULT_SUCCESS
;
473 METHOD(imc_agent_t
, destroy
, void,
474 private_imc_agent_t
*this)
476 DBG1(DBG_IMC
, "IMC %u \"%s\" terminated", this->id
, this->name
);
477 this->connections
->destroy_function(this->connections
, free
);
478 this->connection_lock
->destroy(this->connection_lock
);
481 /* decrease the reference count or terminate */
486 * Described in header.
488 imc_agent_t
*imc_agent_create(const char *name
,
489 pen_t vendor_id
, u_int32_t subtype
,
490 TNC_IMCID id
, TNC_Version
*actual_version
)
492 private_imc_agent_t
*this;
494 /* initialize or increase the reference count */
502 .bind_functions
= _bind_functions
,
503 .create_state
= _create_state
,
504 .delete_state
= _delete_state
,
505 .change_state
= _change_state
,
506 .get_state
= _get_state
,
507 .send_message
= _send_message
,
508 .receive_message
= _receive_message
,
512 .vendor_id
= vendor_id
,
515 .connections
= linked_list_create(),
516 .connection_lock
= rwlock_create(RWLOCK_TYPE_DEFAULT
),
519 *actual_version
= TNC_IFIMC_VERSION_1
;
520 DBG1(DBG_IMC
, "IMC %u \"%s\" initialized", this->id
, this->name
);
522 return &this->public;