IMC/IMV gets v1.3 attributes from TNCC/TNCS
[strongswan.git] / src / libimcv / imc / imc_agent.c
1 /*
2 * Copyright (C) 2011 Andreas Steffen, HSR Hochschule fuer Technik Rapperswil
3 *
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>.
8 *
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
12 * for more details.
13 */
14
15 #include "imcv.h"
16 #include "imc_agent.h"
17
18 #include <tncif_names.h>
19
20 #include <debug.h>
21 #include <utils/linked_list.h>
22 #include <threading/rwlock.h>
23
24 typedef struct private_imc_agent_t private_imc_agent_t;
25
26 /**
27 * Private data of an imc_agent_t object.
28 */
29 struct private_imc_agent_t {
30
31 /**
32 * Public members of imc_agent_t
33 */
34 imc_agent_t public;
35
36 /**
37 * name of IMC
38 */
39 const char *name;
40
41 /**
42 * message vendor ID of IMC
43 */
44 TNC_VendorID vendor_id;
45
46 /**
47 * message subtype of IMC
48 */
49 TNC_MessageSubtype subtype;
50
51 /**
52 * ID of IMC as assigned by TNCC
53 */
54 TNC_IMCID id;
55
56 /**
57 * list of TNCC connection entries
58 */
59 linked_list_t *connections;
60
61 /**
62 * rwlock to lock TNCC connection entries
63 */
64 rwlock_t *connection_lock;
65
66 /**
67 * Inform a TNCC about the set of message types the IMC is able to receive
68 *
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
73 */
74 TNC_Result (*report_message_types)(TNC_IMCID imc_id,
75 TNC_MessageTypeList supported_types,
76 TNC_UInt32 type_count);
77
78 /**
79 * Inform a TNCC about the set of message types the IMC is able to receive
80 *
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
86 */
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);
91
92 /**
93 * Call when an IMC-IMC message is to be sent
94 *
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
101 */
102 TNC_Result (*send_message)(TNC_IMCID imc_id,
103 TNC_ConnectionID connection_id,
104 TNC_BufferReference msg,
105 TNC_UInt32 msg_len,
106 TNC_MessageType msg_type);
107
108 /**
109 * Get the value of an attribute associated with a connection
110 * or with the TNCC as a whole.
111 *
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
119 */
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);
126
127 /**
128 * Set the value of an attribute associated with a connection
129 * or with the TNCC as a whole.
130 *
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
137 */
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);
143 };
144
145 METHOD(imc_agent_t, bind_functions, TNC_Result,
146 private_imc_agent_t *this, TNC_TNCC_BindFunctionPointer bind_function)
147 {
148 if (!bind_function)
149 {
150 DBG1(DBG_IMC, "TNC client failed to provide bind function");
151 return TNC_RESULT_INVALID_PARAMETER;
152 }
153 if (bind_function(this->id, "TNC_TNCC_ReportMessageTypes",
154 (void**)&this->report_message_types) != TNC_RESULT_SUCCESS)
155 {
156 this->report_message_types = NULL;
157 }
158 if (bind_function(this->id, "TNC_TNCC_ReportMessageTypesLong",
159 (void**)&this->report_message_types_long) != TNC_RESULT_SUCCESS)
160 {
161 this->report_message_types_long = NULL;
162 }
163 if (bind_function(this->id, "TNC_TNCC_RequestHandshakeRetry",
164 (void**)&this->public.request_handshake_retry) != TNC_RESULT_SUCCESS)
165 {
166 this->public.request_handshake_retry = NULL;
167 }
168 if (bind_function(this->id, "TNC_TNCC_SendMessage",
169 (void**)&this->send_message) != TNC_RESULT_SUCCESS)
170 {
171 this->send_message = NULL;
172 }
173 if (bind_function(this->id, "TNC_TNCC_GetAttribute",
174 (void**)&this->get_attribute) != TNC_RESULT_SUCCESS)
175 {
176 this->get_attribute = NULL;
177 }
178 if (bind_function(this->id, "TNC_TNCC_SetAttribute",
179 (void**)&this->set_attribute) != TNC_RESULT_SUCCESS)
180 {
181 this->set_attribute = NULL;
182 }
183 DBG2(DBG_IMC, "IMC %u \"%s\" provided with bind function",
184 this->id, this->name);
185
186 if (this->report_message_types_long)
187 {
188 this->report_message_types_long(this->id, &this->vendor_id,
189 &this->subtype, 1);
190 }
191 else if (this->report_message_types &&
192 this->vendor_id <= TNC_VENDORID_ANY &&
193 this->subtype <= TNC_SUBTYPE_ANY)
194 {
195 TNC_MessageType type;
196
197 type = (this->vendor_id << 8) | this->subtype;
198 this->report_message_types(this->id, &type, 1);
199 }
200 return TNC_RESULT_SUCCESS;
201 }
202
203 /**
204 * finds a connection state based on its Connection ID
205 */
206 static imc_state_t* find_connection(private_imc_agent_t *this,
207 TNC_ConnectionID id)
208 {
209 enumerator_t *enumerator;
210 imc_state_t *state, *found = NULL;
211
212 this->connection_lock->read_lock(this->connection_lock);
213 enumerator = this->connections->create_enumerator(this->connections);
214 while (enumerator->enumerate(enumerator, &state))
215 {
216 if (id == state->get_connection_id(state))
217 {
218 found = state;
219 break;
220 }
221 }
222 enumerator->destroy(enumerator);
223 this->connection_lock->unlock(this->connection_lock);
224
225 return found;
226 }
227
228 /**
229 * delete a connection state with a given Connection ID
230 */
231 static bool delete_connection(private_imc_agent_t *this, TNC_ConnectionID id)
232 {
233 enumerator_t *enumerator;
234 imc_state_t *state;
235 bool found = FALSE;
236
237 this->connection_lock->write_lock(this->connection_lock);
238 enumerator = this->connections->create_enumerator(this->connections);
239 while (enumerator->enumerate(enumerator, &state))
240 {
241 if (id == state->get_connection_id(state))
242 {
243 found = TRUE;
244 state->destroy(state);
245 this->connections->remove_at(this->connections, enumerator);
246 break;
247 }
248 }
249 enumerator->destroy(enumerator);
250 this->connection_lock->unlock(this->connection_lock);
251
252 return found;
253 }
254
255 /**
256 * Read a boolean attribute
257 */
258 static bool get_bool_attribute(private_imc_agent_t *this, TNC_ConnectionID id,
259 TNC_AttributeID attribute_id)
260 {
261 TNC_UInt32 len;
262 char buf[4];
263
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;
267 }
268
269 /**
270 * Read a string attribute
271 */
272 static char* get_str_attribute(private_imc_agent_t *this, TNC_ConnectionID id,
273 TNC_AttributeID attribute_id)
274 {
275 TNC_UInt32 len;
276 char buf[BUF_LEN];
277
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)
281 {
282 return strdup(buf);
283 }
284 return NULL;
285 }
286
287 METHOD(imc_agent_t, create_state, TNC_Result,
288 private_imc_agent_t *this, imc_state_t *state)
289 {
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;
293
294 conn_id = state->get_connection_id(state);
295 if (find_connection(this, conn_id))
296 {
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;
301 }
302
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);
311
312 DBG2(DBG_IMC, "IMC %u \"%s\" created a state for Connection ID %u: "
313 "%s %s with %slong %sexcl %ssoh over %s %s",
314 this->id, this->name, conn_id, tnccs_p ? tnccs_p:"?",
315 tnccs_v ? tnccs_v:"?", has_long ? "+":"-", has_excl ? "+":"-",
316 has_soh ? "+":"-", t_p ? t_p:"?", t_v ? t_v :"?");
317 free(tnccs_p);
318 free(tnccs_v);
319 free(t_p);
320 free(t_v);
321
322 this->connection_lock->write_lock(this->connection_lock);
323 this->connections->insert_last(this->connections, state);
324 this->connection_lock->unlock(this->connection_lock);
325 return TNC_RESULT_SUCCESS;
326 }
327
328 METHOD(imc_agent_t, delete_state, TNC_Result,
329 private_imc_agent_t *this, TNC_ConnectionID connection_id)
330 {
331 if (!delete_connection(this, connection_id))
332 {
333 DBG1(DBG_IMC, "IMC %u \"%s\" has no state for Connection ID %u",
334 this->id, this->name, connection_id);
335 return TNC_RESULT_FATAL;
336 }
337 DBG2(DBG_IMC, "IMC %u \"%s\" deleted the state of Connection ID %u",
338 this->id, this->name, connection_id);
339 return TNC_RESULT_SUCCESS;
340 }
341
342 METHOD(imc_agent_t, change_state, TNC_Result,
343 private_imc_agent_t *this, TNC_ConnectionID connection_id,
344 TNC_ConnectionState new_state,
345 imc_state_t **state_p)
346 {
347 imc_state_t *state;
348
349 switch (new_state)
350 {
351 case TNC_CONNECTION_STATE_HANDSHAKE:
352 case TNC_CONNECTION_STATE_ACCESS_ALLOWED:
353 case TNC_CONNECTION_STATE_ACCESS_ISOLATED:
354 case TNC_CONNECTION_STATE_ACCESS_NONE:
355 state = find_connection(this, connection_id);
356
357 if (!state)
358 {
359 DBG1(DBG_IMC, "IMC %u \"%s\" has no state for Connection ID %u",
360 this->id, this->name, connection_id);
361 return TNC_RESULT_FATAL;
362 }
363 state->change_state(state, new_state);
364 DBG2(DBG_IMC, "IMC %u \"%s\" changed state of Connection ID %u to '%N'",
365 this->id, this->name, connection_id,
366 TNC_Connection_State_names, new_state);
367 if (state_p)
368 {
369 *state_p = state;
370 }
371 break;
372 case TNC_CONNECTION_STATE_CREATE:
373 DBG1(DBG_IMC, "state '%N' should be handled by create_state()",
374 TNC_Connection_State_names, new_state);
375 return TNC_RESULT_FATAL;
376 case TNC_CONNECTION_STATE_DELETE:
377 DBG1(DBG_IMC, "state '%N' should be handled by delete_state()",
378 TNC_Connection_State_names, new_state);
379 return TNC_RESULT_FATAL;
380 default:
381 DBG1(DBG_IMC, "IMC %u \"%s\" was notified of unknown state %u "
382 "for Connection ID %u",
383 this->id, this->name, new_state, connection_id);
384 return TNC_RESULT_INVALID_PARAMETER;
385 }
386 return TNC_RESULT_SUCCESS;
387 }
388
389 METHOD(imc_agent_t, get_state, bool,
390 private_imc_agent_t *this, TNC_ConnectionID connection_id,
391 imc_state_t **state)
392 {
393 *state = find_connection(this, connection_id);
394 if (!*state)
395 {
396 DBG1(DBG_IMC, "IMC %u \"%s\" has no state for Connection ID %u",
397 this->id, this->name, connection_id);
398 return FALSE;
399 }
400 return TRUE;
401 }
402
403 METHOD(imc_agent_t, send_message, TNC_Result,
404 private_imc_agent_t *this, TNC_ConnectionID connection_id, chunk_t msg)
405 {
406 TNC_MessageType type;
407
408 if (!this->send_message)
409 {
410 return TNC_RESULT_FATAL;
411 }
412 type = (this->vendor_id << 8) | this->subtype;
413 return this->send_message(this->id, connection_id, msg.ptr, msg.len, type);
414 }
415
416 METHOD(imc_agent_t, receive_message, TNC_Result,
417 private_imc_agent_t *this, TNC_ConnectionID connection_id, chunk_t msg,
418 TNC_MessageType msg_type, pa_tnc_msg_t **pa_tnc_msg)
419 {
420 pa_tnc_msg_t *pa_msg, *error_msg;
421 pa_tnc_attr_t *error_attr;
422 enumerator_t *enumerator;
423 TNC_Result result;
424
425 DBG2(DBG_IMV, "IMC %u \"%s\" received message type 0x%08x for Connection ID %u",
426 this->id, this->name, msg_type, connection_id);
427
428 *pa_tnc_msg = NULL;
429 pa_msg = pa_tnc_msg_create_from_data(msg);
430
431 switch (pa_msg->process(pa_msg))
432 {
433 case SUCCESS:
434 *pa_tnc_msg = pa_msg;
435 break;
436 case VERIFY_ERROR:
437 if (!this->send_message)
438 {
439 /* TNCC doen't have a SendMessage() function */
440 return TNC_RESULT_FATAL;
441 }
442
443 /* build error message */
444 error_msg = pa_tnc_msg_create();
445 enumerator = pa_msg->create_error_enumerator(pa_msg);
446 while (enumerator->enumerate(enumerator, &error_attr))
447 {
448 error_msg->add_attribute(error_msg,
449 error_attr->get_ref(error_attr));
450 }
451 enumerator->destroy(enumerator);
452 error_msg->build(error_msg);
453
454 /* send error message */
455 msg = error_msg->get_encoding(error_msg);
456 result = this->send_message(this->id, connection_id,
457 msg.ptr, msg.len, msg_type);
458
459 /* clean up */
460 error_msg->destroy(error_msg);
461 pa_msg->destroy(pa_msg);
462 return result;
463 case FAILED:
464 default:
465 pa_msg->destroy(pa_msg);
466 return TNC_RESULT_FATAL;
467 }
468 return TNC_RESULT_SUCCESS;
469 }
470
471 METHOD(imc_agent_t, destroy, void,
472 private_imc_agent_t *this)
473 {
474 DBG1(DBG_IMC, "IMC %u \"%s\" terminated", this->id, this->name);
475 this->connections->destroy_function(this->connections, free);
476 this->connection_lock->destroy(this->connection_lock);
477 free(this);
478
479 /* decrease the reference count or terminate */
480 libimcv_deinit();
481 }
482
483 /**
484 * Described in header.
485 */
486 imc_agent_t *imc_agent_create(const char *name,
487 pen_t vendor_id, u_int32_t subtype,
488 TNC_IMCID id, TNC_Version *actual_version)
489 {
490 private_imc_agent_t *this;
491
492 /* initialize or increase the reference count */
493 if (!libimcv_init())
494 {
495 return NULL;
496 }
497
498 INIT(this,
499 .public = {
500 .bind_functions = _bind_functions,
501 .create_state = _create_state,
502 .delete_state = _delete_state,
503 .change_state = _change_state,
504 .get_state = _get_state,
505 .send_message = _send_message,
506 .receive_message = _receive_message,
507 .destroy = _destroy,
508 },
509 .name = name,
510 .vendor_id = vendor_id,
511 .subtype = subtype,
512 .id = id,
513 .connections = linked_list_create(),
514 .connection_lock = rwlock_create(RWLOCK_TYPE_DEFAULT),
515 );
516
517 *actual_version = TNC_IFIMC_VERSION_1;
518 DBG1(DBG_IMC, "IMC %u \"%s\" initialized", this->id, this->name);
519
520 return &this->public;
521 }
522