IMC/IMV gets v1.3 attributes from TNCC/TNCS
[strongswan.git] / src / libimcv / imv / imv_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 "imv_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_imv_agent_t private_imv_agent_t;
25
26 /**
27 * Private data of an imv_agent_t object.
28 */
29 struct private_imv_agent_t {
30
31 /**
32 * Public members of imv_agent_t
33 */
34 imv_agent_t public;
35
36 /**
37 * name of IMV
38 */
39 const char *name;
40
41 /**
42 * message vendor ID of IMV
43 */
44 TNC_VendorID vendor_id;
45
46 /**
47 * message subtype of IMV
48 */
49 TNC_MessageSubtype subtype;
50
51 /**
52 * ID of IMV as assigned by TNCS
53 */
54 TNC_IMVID id;
55
56 /**
57 * list of TNCS connection entries
58 */
59 linked_list_t *connections;
60
61 /**
62 * rwlock to lock TNCS connection entries
63 */
64 rwlock_t *connection_lock;
65
66 /**
67 * Inform a TNCS about the set of message types the IMV is able to receive
68 *
69 * @param imv_id IMV ID assigned by TNCS
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_IMVID imv_id,
75 TNC_MessageTypeList supported_types,
76 TNC_UInt32 type_count);
77
78 /**
79 * Inform a TNCS about the set of message types the IMV is able to receive
80 *
81 * @param imv_id IMV ID assigned by TNCS
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_IMVID imv_id,
88 TNC_VendorIDList supported_vids,
89 TNC_MessageSubtypeList supported_subtypes,
90 TNC_UInt32 type_count);
91
92 /**
93 * Call when an IMV-IMC message is to be sent
94 *
95 * @param imv_id IMV ID assigned by TNCS
96 * @param connection_id network connection ID assigned by TNCS
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_IMVID imv_id,
103 TNC_ConnectionID connection_id,
104 TNC_BufferReference msg,
105 TNC_UInt32 msg_len,
106 TNC_MessageType msg_type);
107
108 /**
109 * Deliver IMV Action Recommendation and IMV Evaluation Results to the TNCS
110 *
111 * @param imv_id IMV ID assigned by TNCS
112 # @param connection_id network connection ID assigned by TNCS
113 * @param rec IMV action recommendation
114 * @param eval IMV evaluation result
115 * @return TNC result code
116 */
117 TNC_Result (*provide_recommendation)(TNC_IMVID imv_id,
118 TNC_ConnectionID connection_id,
119 TNC_IMV_Action_Recommendation rec,
120 TNC_IMV_Evaluation_Result eval);
121
122 /**
123 * Get the value of an attribute associated with a connection
124 * or with the TNCS as a whole.
125 *
126 * @param imv_id IMV ID assigned by TNCS
127 * @param connection_id network connection ID assigned by TNCS
128 * @param attribute_id attribute ID
129 * @param buffer_len length of buffer in bytes
130 * @param buffer buffer
131 * @param out_value_len size in bytes of attribute stored in buffer
132 * @return TNC result code
133 */
134 TNC_Result (*get_attribute)(TNC_IMVID imv_id,
135 TNC_ConnectionID connection_id,
136 TNC_AttributeID attribute_id,
137 TNC_UInt32 buffer_len,
138 TNC_BufferReference buffer,
139 TNC_UInt32 *out_value_len);
140
141 /**
142 * Set the value of an attribute associated with a connection
143 * or with the TNCS as a whole.
144 *
145 * @param imv_id IMV ID assigned by TNCS
146 * @param connection_id network connection ID assigned by TNCS
147 * @param attribute_id attribute ID
148 * @param buffer_len length of buffer in bytes
149 * @param buffer buffer
150 * @return TNC result code
151 */
152 TNC_Result (*set_attribute)(TNC_IMVID imv_id,
153 TNC_ConnectionID connection_id,
154 TNC_AttributeID attribute_id,
155 TNC_UInt32 buffer_len,
156 TNC_BufferReference buffer);
157 };
158
159 METHOD(imv_agent_t, bind_functions, TNC_Result,
160 private_imv_agent_t *this, TNC_TNCS_BindFunctionPointer bind_function)
161 {
162 if (!bind_function)
163 {
164 DBG1(DBG_IMV, "TNC server failed to provide bind function");
165 return TNC_RESULT_INVALID_PARAMETER;
166 }
167 if (bind_function(this->id, "TNC_TNCS_ReportMessageTypes",
168 (void**)&this->report_message_types) != TNC_RESULT_SUCCESS)
169 {
170 this->report_message_types = NULL;
171 }
172 if (bind_function(this->id, "TNC_TNCS_ReportMessageTypesLong",
173 (void**)&this->report_message_types_long) != TNC_RESULT_SUCCESS)
174 {
175 this->report_message_types_long = NULL;
176 }
177 if (bind_function(this->id, "TNC_TNCS_RequestHandshakeRetry",
178 (void**)&this->public.request_handshake_retry) != TNC_RESULT_SUCCESS)
179 {
180 this->public.request_handshake_retry = NULL;
181 }
182 if (bind_function(this->id, "TNC_TNCS_SendMessage",
183 (void**)&this->send_message) != TNC_RESULT_SUCCESS)
184 {
185 this->send_message = NULL;
186 }
187 if (bind_function(this->id, "TNC_TNCS_ProvideRecommendation",
188 (void**)&this->provide_recommendation) != TNC_RESULT_SUCCESS)
189 {
190 this->provide_recommendation = NULL;
191 }
192 if (bind_function(this->id, "TNC_TNCS_GetAttribute",
193 (void**)&this->get_attribute) != TNC_RESULT_SUCCESS)
194 {
195 this->get_attribute = NULL;
196 }
197 if (bind_function(this->id, "TNC_TNCS_SetAttribute",
198 (void**)&this->set_attribute) != TNC_RESULT_SUCCESS)
199 {
200 this->set_attribute = NULL;
201 }
202 DBG2(DBG_IMV, "IMV %u \"%s\" provided with bind function",
203 this->id, this->name);
204
205 if (this->report_message_types_long)
206 {
207 this->report_message_types_long(this->id, &this->vendor_id,
208 &this->subtype, 1);
209 }
210 else if (this->report_message_types &&
211 this->vendor_id <= TNC_VENDORID_ANY &&
212 this->subtype <= TNC_SUBTYPE_ANY)
213 {
214 TNC_MessageType type;
215
216 type = (this->vendor_id << 8) | this->subtype;
217 this->report_message_types(this->id, &type, 1);
218 }
219 return TNC_RESULT_SUCCESS;
220 }
221
222 /**
223 * finds a connection state based on its Connection ID
224 */
225 static imv_state_t* find_connection(private_imv_agent_t *this,
226 TNC_ConnectionID id)
227 {
228 enumerator_t *enumerator;
229 imv_state_t *state, *found = NULL;
230
231 this->connection_lock->read_lock(this->connection_lock);
232 enumerator = this->connections->create_enumerator(this->connections);
233 while (enumerator->enumerate(enumerator, &state))
234 {
235 if (id == state->get_connection_id(state))
236 {
237 found = state;
238 break;
239 }
240 }
241 enumerator->destroy(enumerator);
242 this->connection_lock->unlock(this->connection_lock);
243
244 return found;
245 }
246
247 /**
248 * delete a connection state with a given Connection ID
249 */
250 static bool delete_connection(private_imv_agent_t *this, TNC_ConnectionID id)
251 {
252 enumerator_t *enumerator;
253 imv_state_t *state;
254 bool found = FALSE;
255
256 this->connection_lock->write_lock(this->connection_lock);
257 enumerator = this->connections->create_enumerator(this->connections);
258 while (enumerator->enumerate(enumerator, &state))
259 {
260 if (id == state->get_connection_id(state))
261 {
262 found = TRUE;
263 state->destroy(state);
264 this->connections->remove_at(this->connections, enumerator);
265 break;
266 }
267 }
268 enumerator->destroy(enumerator);
269 this->connection_lock->unlock(this->connection_lock);
270
271 return found;
272 }
273
274 /**
275 * Read a boolean attribute
276 */
277 static bool get_bool_attribute(private_imv_agent_t *this, TNC_ConnectionID id,
278 TNC_AttributeID attribute_id)
279 {
280 TNC_UInt32 len;
281 char buf[4];
282
283 return this->get_attribute &&
284 this->get_attribute(this->id, id, attribute_id, 4, buf, &len) ==
285 TNC_RESULT_SUCCESS && len == 1 && *buf == 0x01;
286 }
287
288 /**
289 * Read a string attribute
290 */
291 static char* get_str_attribute(private_imv_agent_t *this, TNC_ConnectionID id,
292 TNC_AttributeID attribute_id)
293 {
294 TNC_UInt32 len;
295 char buf[BUF_LEN];
296
297 if (this->get_attribute &&
298 this->get_attribute(this->id, id, attribute_id, BUF_LEN, buf, &len) ==
299 TNC_RESULT_SUCCESS && len <= BUF_LEN)
300 {
301 return strdup(buf);
302 }
303 return NULL;
304 }
305
306 METHOD(imv_agent_t, create_state, TNC_Result,
307 private_imv_agent_t *this, imv_state_t *state)
308 {
309 TNC_ConnectionID conn_id;
310 char *tnccs_p = NULL, *tnccs_v = NULL, *t_p = NULL, *t_v = NULL;
311 bool has_long = FALSE, has_excl = FALSE, has_soh = FALSE;
312
313 conn_id = state->get_connection_id(state);
314 if (find_connection(this, conn_id))
315 {
316 DBG1(DBG_IMV, "IMV %u \"%s\" already created a state for Connection ID %u",
317 this->id, this->name, conn_id);
318 state->destroy(state);
319 return TNC_RESULT_OTHER;
320 }
321
322 /* Get and display attributes from TNCS via IF-IMV */
323 has_long = get_bool_attribute(this, conn_id, TNC_ATTRIBUTEID_HAS_LONG_TYPES);
324 has_excl = get_bool_attribute(this, conn_id, TNC_ATTRIBUTEID_HAS_EXCLUSIVE);
325 has_soh = get_bool_attribute(this, conn_id, TNC_ATTRIBUTEID_HAS_SOH);
326 tnccs_p = get_str_attribute(this, conn_id, TNC_ATTRIBUTEID_IFTNCCS_PROTOCOL);
327 tnccs_v = get_str_attribute(this, conn_id, TNC_ATTRIBUTEID_IFTNCCS_VERSION);
328 t_p = get_str_attribute(this, conn_id, TNC_ATTRIBUTEID_IFT_PROTOCOL);
329 t_v = get_str_attribute(this, conn_id, TNC_ATTRIBUTEID_IFT_VERSION);
330
331 DBG2(DBG_IMV, "IMV %u \"%s\" created a state for Connection ID %u: "
332 "%s %s with %slong %sexcl %ssoh over %s %s",
333 this->id, this->name, conn_id, tnccs_p ? tnccs_p:"?",
334 tnccs_v ? tnccs_v:"?", has_long ? "+":"-", has_excl ? "+":"-",
335 has_soh ? "+":"-", t_p ? t_p:"?", t_v ? t_v :"?");
336 free(tnccs_p);
337 free(tnccs_v);
338 free(t_p);
339 free(t_v);
340
341 this->connection_lock->write_lock(this->connection_lock);
342 this->connections->insert_last(this->connections, state);
343 this->connection_lock->unlock(this->connection_lock);
344 return TNC_RESULT_SUCCESS;
345 }
346
347 METHOD(imv_agent_t, delete_state, TNC_Result,
348 private_imv_agent_t *this, TNC_ConnectionID connection_id)
349 {
350 if (!delete_connection(this, connection_id))
351 {
352 DBG1(DBG_IMV, "IMV %u \"%s\" has no state for Connection ID %u",
353 this->id, this->name, connection_id);
354 return TNC_RESULT_FATAL;
355 }
356 DBG2(DBG_IMV, "IMV %u \"%s\" deleted the state of Connection ID %u",
357 this->id, this->name, connection_id);
358 return TNC_RESULT_SUCCESS;
359 }
360
361 METHOD(imv_agent_t, change_state, TNC_Result,
362 private_imv_agent_t *this, TNC_ConnectionID connection_id,
363 TNC_ConnectionState new_state,
364 imv_state_t **state_p)
365 {
366 imv_state_t *state;
367
368 switch (new_state)
369 {
370 case TNC_CONNECTION_STATE_HANDSHAKE:
371 case TNC_CONNECTION_STATE_ACCESS_ALLOWED:
372 case TNC_CONNECTION_STATE_ACCESS_ISOLATED:
373 case TNC_CONNECTION_STATE_ACCESS_NONE:
374 state = find_connection(this, connection_id);
375 if (!state)
376 {
377 DBG1(DBG_IMV, "IMV %u \"%s\" has no state for Connection ID %u",
378 this->id, this->name, connection_id);
379 return TNC_RESULT_FATAL;
380 }
381 state->change_state(state, new_state);
382 DBG2(DBG_IMV, "IMV %u \"%s\" changed state of Connection ID %u to '%N'",
383 this->id, this->name, connection_id,
384 TNC_Connection_State_names, new_state);
385 if (state_p)
386 {
387 *state_p = state;
388 }
389 break;
390 case TNC_CONNECTION_STATE_CREATE:
391 DBG1(DBG_IMV, "state '%N' should be handled by create_state()",
392 TNC_Connection_State_names, new_state);
393 return TNC_RESULT_FATAL;
394 case TNC_CONNECTION_STATE_DELETE:
395 DBG1(DBG_IMV, "state '%N' should be handled by delete_state()",
396 TNC_Connection_State_names, new_state);
397 return TNC_RESULT_FATAL;
398 default:
399 DBG1(DBG_IMV, "IMV %u \"%s\" was notified of unknown state %u "
400 "for Connection ID %u",
401 this->id, this->name, new_state, connection_id);
402 return TNC_RESULT_INVALID_PARAMETER;
403 }
404 return TNC_RESULT_SUCCESS;
405 }
406
407 METHOD(imv_agent_t, get_state, bool,
408 private_imv_agent_t *this, TNC_ConnectionID connection_id,
409 imv_state_t **state)
410 {
411 *state = find_connection(this, connection_id);
412 if (!*state)
413 {
414 DBG1(DBG_IMV, "IMV %u \"%s\" has no state for Connection ID %u",
415 this->id, this->name, connection_id);
416 return FALSE;
417 }
418 return TRUE;
419 }
420
421 METHOD(imv_agent_t, send_message, TNC_Result,
422 private_imv_agent_t *this, TNC_ConnectionID connection_id, chunk_t msg)
423 {
424 TNC_MessageType type;
425
426 if (!this->send_message)
427 {
428 return TNC_RESULT_FATAL;
429 }
430 type = (this->vendor_id << 8) | this->subtype;
431 return this->send_message(this->id, connection_id, msg.ptr, msg.len, type);
432 }
433
434 METHOD(imv_agent_t, set_recommendation, TNC_Result,
435 private_imv_agent_t *this, TNC_ConnectionID connection_id,
436 TNC_IMV_Action_Recommendation rec,
437 TNC_IMV_Evaluation_Result eval)
438 {
439 imv_state_t *state;
440
441 state = find_connection(this, connection_id);
442 if (!state)
443 {
444 DBG1(DBG_IMV, "IMV %u \"%s\" has no state for Connection ID %u",
445 this->id, this->name, connection_id);
446 return TNC_RESULT_FATAL;
447 }
448
449 state->set_recommendation(state, rec, eval);
450 return this->provide_recommendation(this->id, connection_id, rec, eval);
451 }
452
453 METHOD(imv_agent_t, receive_message, TNC_Result,
454 private_imv_agent_t *this, TNC_ConnectionID connection_id, chunk_t msg,
455 TNC_MessageType msg_type, pa_tnc_msg_t **pa_tnc_msg)
456 {
457 pa_tnc_msg_t *pa_msg, *error_msg;
458 pa_tnc_attr_t *error_attr;
459 enumerator_t *enumerator;
460 TNC_Result result;
461
462 DBG2(DBG_IMV, "IMV %u \"%s\" received message type 0x%08x for Connection ID %u",
463 this->id, this->name, msg_type, connection_id);
464
465 *pa_tnc_msg = NULL;
466 pa_msg = pa_tnc_msg_create_from_data(msg);
467
468 switch (pa_msg->process(pa_msg))
469 {
470 case SUCCESS:
471 *pa_tnc_msg = pa_msg;
472 break;
473 case VERIFY_ERROR:
474 if (!this->send_message)
475 {
476 /* TNCS doen't have a SendMessage() function */
477 return TNC_RESULT_FATAL;
478 }
479
480 /* build error message */
481 error_msg = pa_tnc_msg_create();
482 enumerator = pa_msg->create_error_enumerator(pa_msg);
483 while (enumerator->enumerate(enumerator, &error_attr))
484 {
485 error_msg->add_attribute(error_msg,
486 error_attr->get_ref(error_attr));
487 }
488 enumerator->destroy(enumerator);
489 error_msg->build(error_msg);
490
491 /* send error message */
492 msg = error_msg->get_encoding(error_msg);
493 result = this->send_message(this->id, connection_id,
494 msg.ptr, msg.len, msg_type);
495
496 /* clean up */
497 error_msg->destroy(error_msg);
498 pa_msg->destroy(pa_msg);
499 return result;
500 case FAILED:
501 default:
502 pa_msg->destroy(pa_msg);
503 return set_recommendation(this, connection_id,
504 TNC_IMV_ACTION_RECOMMENDATION_NO_RECOMMENDATION,
505 TNC_IMV_EVALUATION_RESULT_ERROR);
506 }
507 return TNC_RESULT_SUCCESS;
508 }
509
510 METHOD(imv_agent_t, provide_recommendation, TNC_Result,
511 private_imv_agent_t *this, TNC_ConnectionID connection_id)
512 {
513 imv_state_t *state;
514 TNC_IMV_Action_Recommendation rec;
515 TNC_IMV_Evaluation_Result eval;
516 TNC_UInt32 lang_len;
517 char buf[BUF_LEN];
518 chunk_t pref_lang = { buf, 0 }, reason_string, reason_lang;
519
520 state = find_connection(this, connection_id);
521 if (!state)
522 {
523 DBG1(DBG_IMV, "IMV %u \"%s\" has no state for Connection ID %u",
524 this->id, this->name, connection_id);
525 return TNC_RESULT_FATAL;
526 }
527 state->get_recommendation(state, &rec, &eval);
528
529
530 /* send a reason string if action recommendation is not allow */
531 if (rec != TNC_IMV_ACTION_RECOMMENDATION_ALLOW)
532 {
533 /* check if there a preferred language has been requested */
534 if (this->get_attribute &&
535 this->get_attribute(this->id, connection_id,
536 TNC_ATTRIBUTEID_PREFERRED_LANGUAGE, BUF_LEN,
537 buf, &lang_len) == TNC_RESULT_SUCCESS &&
538 lang_len <= BUF_LEN)
539 {
540 pref_lang.len = lang_len;
541 DBG2(DBG_IMV, "preferred language is '%.*s'",
542 pref_lang.len, pref_lang.ptr);
543 }
544
545 /* find a reason string for the preferred or default language and set it */
546 if (this->set_attribute &&
547 state->get_reason_string(state, pref_lang, &reason_string,
548 &reason_lang))
549 {
550 this->set_attribute(this->id, connection_id,
551 TNC_ATTRIBUTEID_REASON_STRING,
552 reason_string.len, reason_string.ptr);
553 this->set_attribute(this->id, connection_id,
554 TNC_ATTRIBUTEID_REASON_LANGUAGE,
555 reason_lang.len, reason_lang.ptr);
556 }
557 }
558
559 return this->provide_recommendation(this->id, connection_id, rec, eval);
560 }
561
562 METHOD(imv_agent_t, destroy, void,
563 private_imv_agent_t *this)
564 {
565 DBG1(DBG_IMV, "IMV %u \"%s\" terminated", this->id, this->name);
566 this->connections->destroy_offset(this->connections,
567 offsetof(imv_state_t, destroy));
568 this->connection_lock->destroy(this->connection_lock);
569 free(this);
570
571 /* decrease the reference count or terminate */
572 libimcv_deinit();
573 }
574
575 /**
576 * Described in header.
577 */
578 imv_agent_t *imv_agent_create(const char *name,
579 pen_t vendor_id, u_int32_t subtype,
580 TNC_IMVID id, TNC_Version *actual_version)
581 {
582 private_imv_agent_t *this;
583
584 /* initialize or increase the reference count */
585 if (!libimcv_init())
586 {
587 return NULL;
588 }
589
590 INIT(this,
591 .public = {
592 .bind_functions = _bind_functions,
593 .create_state = _create_state,
594 .delete_state = _delete_state,
595 .change_state = _change_state,
596 .get_state = _get_state,
597 .send_message = _send_message,
598 .receive_message = _receive_message,
599 .set_recommendation = _set_recommendation,
600 .provide_recommendation = _provide_recommendation,
601 .destroy = _destroy,
602 },
603 .name = name,
604 .vendor_id = vendor_id,
605 .subtype = subtype,
606 .id = id,
607 .connections = linked_list_create(),
608 .connection_lock = rwlock_create(RWLOCK_TYPE_DEFAULT),
609 );
610
611 *actual_version = TNC_IFIMV_VERSION_1;
612 DBG1(DBG_IMV, "IMV %u \"%s\" initialized", this->id, this->name);
613
614 return &this->public;
615 }
616
617