2 * Copyright (C) 2010 Sansar Choinyanbuu
3 * Copyright (C) 2010 Andreas Steffen
4 * HSR Hochschule fuer Technik Rapperswil
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 #include "batch/pb_tnc_batch.h"
19 #include "messages/pb_tnc_msg.h"
20 #include "messages/pb_pa_msg.h"
21 #include "messages/pb_error_msg.h"
22 #include "messages/pb_assessment_result_msg.h"
23 #include "messages/pb_access_recommendation_msg.h"
24 #include "messages/pb_remediation_parameters_msg.h"
25 #include "messages/pb_reason_string_msg.h"
26 #include "messages/pb_language_preference_msg.h"
27 #include "state_machine/pb_tnc_state_machine.h"
31 #include <threading/mutex.h>
32 #include <tnc/tncif.h>
33 #include <tnc/tncifimv.h>
34 #include <tnc/tnccs/tnccs.h>
36 typedef struct private_tnccs_20_t private_tnccs_20_t
;
39 * Private data of a tnccs_20_t object.
41 struct private_tnccs_20_t
{
44 * Public tls_t interface.
49 * TNCC if TRUE, TNCS if FALSE
54 * PB-TNC State Machine
56 pb_tnc_state_machine_t
*state_machine
;
59 * Connection ID assigned to this TNCCS connection
61 TNC_ConnectionID connection_id
;
64 * PB-TNC batch being constructed
66 pb_tnc_batch_t
*batch
;
69 * Mutex locking the batch in construction
74 * Flag set while processing
79 * Flag set by IMC/IMV RequestHandshakeRetry() function
81 bool request_handshake_retry
;
84 * Set of IMV recommendations (TNC Server only)
86 recommendations_t
*recs
;
89 METHOD(tnccs_t
, send_msg
, void,
90 private_tnccs_20_t
* this, TNC_IMCID imc_id
, TNC_IMVID imv_id
,
91 TNC_BufferReference msg
,
93 TNC_MessageType msg_type
)
95 TNC_MessageSubtype msg_sub_type
;
96 TNC_VendorID msg_vendor_id
;
97 pb_tnc_msg_t
*pb_tnc_msg
;
98 pb_tnc_batch_type_t batch_type
;
100 msg_sub_type
= msg_type
& TNC_SUBTYPE_ANY
;
101 msg_vendor_id
= (msg_type
>> 8) & TNC_VENDORID_ANY
;
103 pb_tnc_msg
= pb_pa_msg_create(msg_vendor_id
, msg_sub_type
, imc_id
, imv_id
,
104 chunk_create(msg
, msg_len
));
106 /* adding PA message to SDATA or CDATA batch only */
107 batch_type
= this->is_server ? PB_BATCH_SDATA
: PB_BATCH_CDATA
;
108 this->mutex
->lock(this->mutex
);
111 this->batch
= pb_tnc_batch_create(this->is_server
, batch_type
);
113 if (this->batch
->get_type(this->batch
) == batch_type
)
115 this->batch
->add_msg(this->batch
, pb_tnc_msg
);
119 pb_tnc_msg
->destroy(pb_tnc_msg
);
121 this->mutex
->unlock(this->mutex
);
125 * Handle a single PB-TNC message according to its type
127 static void handle_message(private_tnccs_20_t
*this, pb_tnc_msg_t
*msg
)
129 switch (msg
->get_type(msg
))
131 case PB_MSG_EXPERIMENTAL
:
137 TNC_MessageType msg_type
;
138 u_int32_t vendor_id
, subtype
;
141 pa_msg
= (pb_pa_msg_t
*)msg
;
142 vendor_id
= pa_msg
->get_vendor_id(pa_msg
, &subtype
);
143 msg_type
= (vendor_id
<< 8) | (subtype
& 0xff);
144 msg_body
= pa_msg
->get_body(pa_msg
);
146 DBG2(DBG_TNC
, "handling PB-PA message type 0x%08x", msg_type
);
150 charon
->imvs
->receive_message(charon
->imvs
,
151 this->connection_id
, msg_body
.ptr
, msg_body
.len
, msg_type
);
155 charon
->imcs
->receive_message(charon
->imcs
,
156 this->connection_id
, msg_body
.ptr
, msg_body
.len
,msg_type
);
160 case PB_MSG_ASSESSMENT_RESULT
:
162 pb_assessment_result_msg_t
*assess_msg
;
165 assess_msg
= (pb_assessment_result_msg_t
*)msg
;
166 result
= assess_msg
->get_assessment_result(assess_msg
);
167 DBG1(DBG_TNC
, "PB-TNC assessment result is '%N'",
168 TNC_IMV_Evaluation_Result_names
, result
);
171 case PB_MSG_ACCESS_RECOMMENDATION
:
173 pb_access_recommendation_msg_t
*rec_msg
;
174 pb_access_recommendation_code_t rec
;
175 TNC_ConnectionState state
= TNC_CONNECTION_STATE_ACCESS_NONE
;
177 rec_msg
= (pb_access_recommendation_msg_t
*)msg
;
178 rec
= rec_msg
->get_access_recommendation(rec_msg
);
179 DBG1(DBG_TNC
, "PB-TNC access recommendation is '%N'",
180 pb_access_recommendation_code_names
, rec
);
183 case PB_REC_ACCESS_ALLOWED
:
184 state
= TNC_CONNECTION_STATE_ACCESS_ALLOWED
;
186 case PB_REC_ACCESS_DENIED
:
187 state
= TNC_CONNECTION_STATE_ACCESS_NONE
;
189 case PB_REC_QUARANTINED
:
190 state
= TNC_CONNECTION_STATE_ACCESS_ISOLATED
;
192 charon
->imcs
->notify_connection_change(charon
->imcs
,
193 this->connection_id
, state
);
196 case PB_MSG_REMEDIATION_PARAMETERS
:
198 /* TODO : Remediation parameters message processing */
203 pb_error_msg_t
*err_msg
;
206 u_int16_t error_code
;
208 err_msg
= (pb_error_msg_t
*)msg
;
209 fatal
= err_msg
->get_fatal_flag(err_msg
);
210 vendor_id
= err_msg
->get_vendor_id(err_msg
);
211 error_code
= err_msg
->get_error_code(err_msg
);
215 this->fatal_error
= TRUE
;
218 if (vendor_id
== IETF_VENDOR_ID
)
222 case PB_ERROR_INVALID_PARAMETER
:
223 case PB_ERROR_UNSUPPORTED_MANDATORY_MSG
:
224 DBG1(DBG_TNC
, "received %s PB-TNC error '%N' "
226 fatal ?
"fatal" : "non-fatal",
227 pb_tnc_error_code_names
, error_code
,
228 err_msg
->get_offset(err_msg
));
230 case PB_ERROR_VERSION_NOT_SUPPORTED
:
231 DBG1(DBG_TNC
, "received %s PB-TNC error '%N' "
232 "caused by bad version 0x%02x",
233 fatal ?
"fatal" : "non-fatal",
234 pb_tnc_error_code_names
, error_code
,
235 err_msg
->get_bad_version(err_msg
));
237 case PB_ERROR_UNEXPECTED_BATCH_TYPE
:
238 case PB_ERROR_LOCAL_ERROR
:
240 DBG1(DBG_TNC
, "received %s PB-TNC error '%N'",
241 fatal ?
"fatal" : "non-fatal",
242 pb_tnc_error_code_names
, error_code
);
248 DBG1(DBG_TNC
, "received %s PB-TNC error (%u) "
249 "with Vendor ID 0x%06x",
250 fatal ?
"fatal" : "non-fatal",
251 error_code
, vendor_id
);
255 case PB_MSG_LANGUAGE_PREFERENCE
:
257 pb_language_preference_msg_t
*lang_msg
;
260 lang_msg
= (pb_language_preference_msg_t
*)msg
;
261 lang
= lang_msg
->get_language_preference(lang_msg
);
263 DBG2(DBG_TNC
, "setting language preference to '%.*s'",
265 this->recs
->set_preferred_language(this->recs
, lang
);
268 case PB_MSG_REASON_STRING
:
270 pb_reason_string_msg_t
*reason_msg
;
271 chunk_t reason_string
, language_code
;
273 reason_msg
= (pb_reason_string_msg_t
*)msg
;
274 reason_string
= reason_msg
->get_reason_string(reason_msg
);
275 language_code
= reason_msg
->get_language_code(reason_msg
);
276 DBG2(DBG_TNC
, "reason string is '%.*s", reason_string
.len
,
278 DBG2(DBG_TNC
, "language code is '%.*s", language_code
.len
,
288 * Build a CRETRY or SRETRY batch
290 static void build_retry_batch(private_tnccs_20_t
*this)
294 DBG1(DBG_TNC
, "cancelling PB-TNC %N batch",
295 pb_tnc_batch_type_names
, this->batch
->get_type(this->batch
));
296 this->batch
->destroy(this->batch
);
298 this->batch
= pb_tnc_batch_create(this->is_server
,
299 this->is_server ? PB_BATCH_SRETRY
: PB_BATCH_CRETRY
);
302 METHOD(tls_t
, process
, status_t
,
303 private_tnccs_20_t
*this, void *buf
, size_t buflen
)
306 pb_tnc_batch_t
*batch
;
308 enumerator_t
*enumerator
;
311 if (this->is_server
&& !this->connection_id
)
313 this->connection_id
= charon
->tnccs
->create_connection(charon
->tnccs
,
314 (tnccs_t
*)this, _send_msg
,
315 &this->request_handshake_retry
, &this->recs
);
316 if (!this->connection_id
)
320 charon
->imvs
->notify_connection_change(charon
->imvs
,
321 this->connection_id
, TNC_CONNECTION_STATE_CREATE
);
324 data
= chunk_create(buf
, buflen
);
325 DBG1(DBG_TNC
, "received TNCCS batch (%u bytes) for Connection ID %u",
326 data
.len
, this->connection_id
);
327 DBG3(DBG_TNC
, "%B", &data
);
328 batch
= pb_tnc_batch_create_from_data(this->is_server
, data
);
329 status
= batch
->process(batch
, this->state_machine
);
331 if (status
!= FAILED
)
333 enumerator_t
*enumerator
;
335 pb_tnc_batch_type_t batch_type
;
338 batch_type
= batch
->get_type(batch
);
340 if (batch_type
== PB_BATCH_CRETRY
)
342 /* Send an SRETRY batch in response */
343 this->mutex
->lock(this->mutex
);
344 build_retry_batch(this);
345 this->mutex
->unlock(this->mutex
);
347 else if (batch_type
== PB_BATCH_SRETRY
)
349 /* Restart the measurements */
350 charon
->imcs
->notify_connection_change(charon
->imcs
,
351 this->connection_id
, TNC_CONNECTION_STATE_HANDSHAKE
);
352 charon
->imcs
->begin_handshake(charon
->imcs
, this->connection_id
);
355 enumerator
= batch
->create_msg_enumerator(batch
);
356 while (enumerator
->enumerate(enumerator
, &msg
))
358 handle_message(this, msg
);
361 enumerator
->destroy(enumerator
);
363 /* received an empty CLOSE batch from PB-TNC client */
364 if (this->is_server
&& batch_type
== PB_BATCH_CLOSE
&& empty
)
366 batch
->destroy(batch
);
367 if (this->fatal_error
)
369 DBG1(DBG_TNC
, "a fatal PB-TNC error occurred, "
370 "terminating connection");
381 charon
->imvs
->batch_ending(charon
->imvs
, this->connection_id
);
385 charon
->imcs
->batch_ending(charon
->imcs
, this->connection_id
);
392 this->fatal_error
= TRUE
;
393 this->mutex
->lock(this->mutex
);
396 DBG1(DBG_TNC
, "cancelling PB-TNC %N batch",
397 pb_tnc_batch_type_names
, this->batch
->get_type(this->batch
));
398 this->batch
->destroy(this->batch
);
400 this->batch
= pb_tnc_batch_create(this->is_server
, PB_BATCH_CLOSE
);
401 this->mutex
->unlock(this->mutex
);
402 /* fall through to add error messages to outbound batch */
404 enumerator
= batch
->create_error_enumerator(batch
);
405 while (enumerator
->enumerate(enumerator
, &msg
))
407 this->mutex
->lock(this->mutex
);
408 this->batch
->add_msg(this->batch
, msg
->get_ref(msg
));
409 this->mutex
->unlock(this->mutex
);
411 enumerator
->destroy(enumerator
);
417 batch
->destroy(batch
);
423 * Build a RESULT batch if a final recommendation is available
425 static void check_and_build_recommendation(private_tnccs_20_t
*this)
427 TNC_IMV_Action_Recommendation rec
;
428 TNC_IMV_Evaluation_Result eval
;
430 chunk_t reason
, language
;
431 enumerator_t
*enumerator
;
434 if (!this->recs
->have_recommendation(this->recs
, &rec
, &eval
))
436 charon
->imvs
->solicit_recommendation(charon
->imvs
, this->connection_id
);
438 if (this->recs
->have_recommendation(this->recs
, &rec
, &eval
))
440 this->batch
= pb_tnc_batch_create(this->is_server
, PB_BATCH_RESULT
);
442 msg
= pb_assessment_result_msg_create(eval
);
443 this->batch
->add_msg(this->batch
, msg
);
446 * IMV Action Recommendation and PB Access Recommendation codes
447 * are shifted by one.
449 msg
= pb_access_recommendation_msg_create(rec
+ 1);
450 this->batch
->add_msg(this->batch
, msg
);
452 enumerator
= this->recs
->create_reason_enumerator(this->recs
);
453 while (enumerator
->enumerate(enumerator
, &id
, &reason
, &language
))
455 msg
= pb_reason_string_msg_create(reason
, language
);
456 this->batch
->add_msg(this->batch
, msg
);
458 enumerator
->destroy(enumerator
);
462 METHOD(tls_t
, build
, status_t
,
463 private_tnccs_20_t
*this, void *buf
, size_t *buflen
, size_t *msglen
)
467 /* Initialize the connection */
468 if (!this->is_server
&& !this->connection_id
)
473 this->connection_id
= charon
->tnccs
->create_connection(charon
->tnccs
,
474 (tnccs_t
*)this, _send_msg
,
475 &this->request_handshake_retry
, NULL
);
476 if (!this->connection_id
)
481 /* Create PB-TNC Language Preference message */
482 pref_lang
= charon
->imcs
->get_preferred_language(charon
->imcs
);
483 msg
= pb_language_preference_msg_create(chunk_create(pref_lang
,
485 this->mutex
->lock(this->mutex
);
486 this->batch
= pb_tnc_batch_create(this->is_server
, PB_BATCH_CDATA
);
487 this->batch
->add_msg(this->batch
, msg
);
488 this->mutex
->unlock(this->mutex
);
490 charon
->imcs
->notify_connection_change(charon
->imcs
,
491 this->connection_id
, TNC_CONNECTION_STATE_CREATE
);
492 charon
->imcs
->notify_connection_change(charon
->imcs
,
493 this->connection_id
, TNC_CONNECTION_STATE_HANDSHAKE
);
494 charon
->imcs
->begin_handshake(charon
->imcs
, this->connection_id
);
497 if (this->is_server
&& this->fatal_error
&&
498 this->state_machine
->get_state(this->state_machine
) == PB_STATE_END
)
500 DBG1(DBG_TNC
, "a fatal PB-TNC error occurred, terminating connection");
504 /* Do not allow any asynchronous IMCs or IMVs to add additional messages */
505 this->mutex
->lock(this->mutex
);
507 if (this->request_handshake_retry
)
509 build_retry_batch(this);
511 /* Reset the flag for the next handshake retry request */
512 this->request_handshake_retry
= FALSE
;
517 pb_tnc_state_t state
;
519 state
= this->state_machine
->get_state(this->state_machine
);
522 if (state
== PB_STATE_SERVER_WORKING
)
524 check_and_build_recommendation(this);
530 * if the DECIDED state has been reached and no CRETRY is under way
531 * or if a CLOSE batch with error messages has been received,
532 * a PB-TNC client replies with an empty CLOSE batch.
534 if (state
== PB_STATE_DECIDED
|| state
== PB_STATE_END
)
536 this->batch
= pb_tnc_batch_create(this->is_server
, PB_BATCH_CLOSE
);
543 pb_tnc_batch_type_t batch_type
;
546 batch_type
= this->batch
->get_type(this->batch
);
548 if (this->state_machine
->send_batch(this->state_machine
, batch_type
))
550 this->batch
->build(this->batch
);
551 data
= this->batch
->get_encoding(this->batch
);
552 DBG1(DBG_TNC
, "sending PB-TNC %N batch (%d bytes) for Connection ID %u",
553 pb_tnc_batch_type_names
, batch_type
, data
.len
,
554 this->connection_id
);
555 DBG3(DBG_TNC
, "%B", &data
);
558 if (data
.len
> *buflen
)
560 DBG1(DBG_TNC
, "fragmentation of PB-TNC batch not supported yet");
566 memcpy(buf
, data
.ptr
, *buflen
);
567 status
= ALREADY_DONE
;
571 DBG1(DBG_TNC
, "cancelling unexpected PB-TNC batch type: %N",
572 pb_tnc_batch_type_names
, batch_type
);
573 status
= INVALID_STATE
;
576 this->batch
->destroy(this->batch
);
581 DBG1(DBG_TNC
, "no PB-TNC batch to send");
582 status
= INVALID_STATE
;
584 this->mutex
->unlock(this->mutex
);
589 METHOD(tls_t
, is_server
, bool,
590 private_tnccs_20_t
*this)
592 return this->is_server
;
595 METHOD(tls_t
, get_purpose
, tls_purpose_t
,
596 private_tnccs_20_t
*this)
598 return TLS_PURPOSE_EAP_TNC
;
601 METHOD(tls_t
, is_complete
, bool,
602 private_tnccs_20_t
*this)
604 TNC_IMV_Action_Recommendation rec
;
605 TNC_IMV_Evaluation_Result eval
;
607 if (this->recs
&& this->recs
->have_recommendation(this->recs
, &rec
, &eval
))
609 DBG2(DBG_TNC
, "Final recommendation is '%N' and evaluation is '%N'",
610 TNC_IMV_Action_Recommendation_names
, rec
,
611 TNC_IMV_Evaluation_Result_names
, eval
);
613 return charon
->imvs
->enforce_recommendation(charon
->imvs
, rec
);
621 METHOD(tls_t
, get_eap_msk
, chunk_t
,
622 private_tnccs_20_t
*this)
627 METHOD(tls_t
, destroy
, void,
628 private_tnccs_20_t
*this)
632 charon
->imvs
->notify_connection_change(charon
->imvs
,
633 this->connection_id
, TNC_CONNECTION_STATE_DELETE
);
637 charon
->imcs
->notify_connection_change(charon
->imcs
,
638 this->connection_id
, TNC_CONNECTION_STATE_DELETE
);
640 charon
->tnccs
->remove_connection(charon
->tnccs
, this->connection_id
);
641 this->state_machine
->destroy(this->state_machine
);
642 this->mutex
->destroy(this->mutex
);
643 DESTROY_IF(this->batch
);
650 tls_t
*tnccs_20_create(bool is_server
)
652 private_tnccs_20_t
*this;
658 .is_server
= _is_server
,
659 .get_purpose
= _get_purpose
,
660 .is_complete
= _is_complete
,
661 .get_eap_msk
= _get_eap_msk
,
664 .is_server
= is_server
,
665 .state_machine
= pb_tnc_state_machine_create(is_server
),
666 .mutex
= mutex_create(MUTEX_TYPE_DEFAULT
),
669 return &this->public;