refactored tnccs->remove_connection()
[strongswan.git] / src / libcharon / plugins / tnccs_20 / tnccs_20.c
1 /*
2 * Copyright (C) 2010 Sansar Choinyanbuu
3 * Copyright (C) 2010 Andreas Steffen
4 * HSR Hochschule fuer Technik Rapperswil
5 *
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>.
10 *
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
14 * for more details.
15 */
16
17 #include "tnccs_20.h"
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"
28
29 #include <debug.h>
30 #include <daemon.h>
31 #include <threading/mutex.h>
32 #include <tnc/tncif.h>
33 #include <tnc/tncifimv.h>
34 #include <tnc/tnccs/tnccs.h>
35
36 typedef struct private_tnccs_20_t private_tnccs_20_t;
37
38 /**
39 * Private data of a tnccs_20_t object.
40 */
41 struct private_tnccs_20_t {
42
43 /**
44 * Public tls_t interface.
45 */
46 tls_t public;
47
48 /**
49 * TNCC if TRUE, TNCS if FALSE
50 */
51 bool is_server;
52
53 /**
54 * PB-TNC State Machine
55 */
56 pb_tnc_state_machine_t *state_machine;
57
58 /**
59 * Connection ID assigned to this TNCCS connection
60 */
61 TNC_ConnectionID connection_id;
62
63 /**
64 * PB-TNC batch being constructed
65 */
66 pb_tnc_batch_t *batch;
67
68 /**
69 * Mutex locking the batch in construction
70 */
71 mutex_t *mutex;
72
73 /**
74 * Flag set while processing
75 */
76 bool fatal_error;
77
78 /**
79 * Flag set by IMC/IMV RequestHandshakeRetry() function
80 */
81 bool request_handshake_retry;
82
83 /**
84 * Set of IMV recommendations (TNC Server only)
85 */
86 recommendations_t *recs;
87 };
88
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,
92 TNC_UInt32 msg_len,
93 TNC_MessageType msg_type)
94 {
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;
99
100 msg_sub_type = msg_type & TNC_SUBTYPE_ANY;
101 msg_vendor_id = (msg_type >> 8) & TNC_VENDORID_ANY;
102
103 pb_tnc_msg = pb_pa_msg_create(msg_vendor_id, msg_sub_type, imc_id, imv_id,
104 chunk_create(msg, msg_len));
105
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);
109 if (!this->batch)
110 {
111 this->batch = pb_tnc_batch_create(this->is_server, batch_type);
112 }
113 if (this->batch->get_type(this->batch) == batch_type)
114 {
115 this->batch->add_msg(this->batch, pb_tnc_msg);
116 }
117 else
118 {
119 pb_tnc_msg->destroy(pb_tnc_msg);
120 }
121 this->mutex->unlock(this->mutex);
122 }
123
124 /**
125 * Handle a single PB-TNC message according to its type
126 */
127 static void handle_message(private_tnccs_20_t *this, pb_tnc_msg_t *msg)
128 {
129 switch (msg->get_type(msg))
130 {
131 case PB_MSG_EXPERIMENTAL:
132 /* nothing to do */
133 break;
134 case PB_MSG_PA:
135 {
136 pb_pa_msg_t *pa_msg;
137 TNC_MessageType msg_type;
138 u_int32_t vendor_id, subtype;
139 chunk_t msg_body;
140
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);
145
146 DBG2(DBG_TNC, "handling PB-PA message type 0x%08x", msg_type);
147
148 if (this->is_server)
149 {
150 charon->imvs->receive_message(charon->imvs,
151 this->connection_id, msg_body.ptr, msg_body.len, msg_type);
152 }
153 else
154 {
155 charon->imcs->receive_message(charon->imcs,
156 this->connection_id, msg_body.ptr, msg_body.len,msg_type);
157 }
158 break;
159 }
160 case PB_MSG_ASSESSMENT_RESULT:
161 {
162 pb_assessment_result_msg_t *assess_msg;
163 u_int32_t result;
164
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);
169 break;
170 }
171 case PB_MSG_ACCESS_RECOMMENDATION:
172 {
173 pb_access_recommendation_msg_t *rec_msg;
174 pb_access_recommendation_code_t rec;
175 TNC_ConnectionState state = TNC_CONNECTION_STATE_ACCESS_NONE;
176
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);
181 switch (rec)
182 {
183 case PB_REC_ACCESS_ALLOWED:
184 state = TNC_CONNECTION_STATE_ACCESS_ALLOWED;
185 break;
186 case PB_REC_ACCESS_DENIED:
187 state = TNC_CONNECTION_STATE_ACCESS_NONE;
188 break;
189 case PB_REC_QUARANTINED:
190 state = TNC_CONNECTION_STATE_ACCESS_ISOLATED;
191 }
192 charon->imcs->notify_connection_change(charon->imcs,
193 this->connection_id, state);
194 break;
195 }
196 case PB_MSG_REMEDIATION_PARAMETERS:
197 {
198 /* TODO : Remediation parameters message processing */
199 break;
200 }
201 case PB_MSG_ERROR:
202 {
203 pb_error_msg_t *err_msg;
204 bool fatal;
205 u_int32_t vendor_id;
206 u_int16_t error_code;
207
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);
212
213 if (fatal)
214 {
215 this->fatal_error = TRUE;
216 }
217
218 if (vendor_id == IETF_VENDOR_ID)
219 {
220 switch (error_code)
221 {
222 case PB_ERROR_INVALID_PARAMETER:
223 case PB_ERROR_UNSUPPORTED_MANDATORY_MSG:
224 DBG1(DBG_TNC, "received %s PB-TNC error '%N' "
225 "(offset %u bytes)",
226 fatal ? "fatal" : "non-fatal",
227 pb_tnc_error_code_names, error_code,
228 err_msg->get_offset(err_msg));
229 break;
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));
236 break;
237 case PB_ERROR_UNEXPECTED_BATCH_TYPE:
238 case PB_ERROR_LOCAL_ERROR:
239 default:
240 DBG1(DBG_TNC, "received %s PB-TNC error '%N'",
241 fatal ? "fatal" : "non-fatal",
242 pb_tnc_error_code_names, error_code);
243 break;
244 }
245 }
246 else
247 {
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);
252 }
253 break;
254 }
255 case PB_MSG_LANGUAGE_PREFERENCE:
256 {
257 pb_language_preference_msg_t *lang_msg;
258 chunk_t lang;
259
260 lang_msg = (pb_language_preference_msg_t*)msg;
261 lang = lang_msg->get_language_preference(lang_msg);
262
263 DBG2(DBG_TNC, "setting language preference to '%.*s'",
264 lang.len, lang.ptr);
265 this->recs->set_preferred_language(this->recs, lang);
266 break;
267 }
268 case PB_MSG_REASON_STRING:
269 {
270 pb_reason_string_msg_t *reason_msg;
271 chunk_t reason_string, language_code;
272
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,
277 reason_string.ptr);
278 DBG2(DBG_TNC, "language code is '%.*s", language_code.len,
279 language_code.ptr);
280 break;
281 }
282 default:
283 break;
284 }
285 }
286
287 /**
288 * Build a CRETRY or SRETRY batch
289 */
290 static void build_retry_batch(private_tnccs_20_t *this)
291 {
292 if (this->batch)
293 {
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);
297 }
298 this->batch = pb_tnc_batch_create(this->is_server,
299 this->is_server ? PB_BATCH_SRETRY : PB_BATCH_CRETRY);
300 }
301
302 METHOD(tls_t, process, status_t,
303 private_tnccs_20_t *this, void *buf, size_t buflen)
304 {
305 chunk_t data;
306 pb_tnc_batch_t *batch;
307 pb_tnc_msg_t *msg;
308 enumerator_t *enumerator;
309 status_t status;
310
311 if (this->is_server && !this->connection_id)
312 {
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)
317 {
318 return FAILED;
319 }
320 charon->imvs->notify_connection_change(charon->imvs,
321 this->connection_id, TNC_CONNECTION_STATE_CREATE);
322 charon->imvs->notify_connection_change(charon->imvs,
323 this->connection_id, TNC_CONNECTION_STATE_HANDSHAKE);
324 }
325
326 data = chunk_create(buf, buflen);
327 DBG1(DBG_TNC, "received TNCCS batch (%u bytes) for Connection ID %u",
328 data.len, this->connection_id);
329 DBG3(DBG_TNC, "%B", &data);
330 batch = pb_tnc_batch_create_from_data(this->is_server, data);
331 status = batch->process(batch, this->state_machine);
332
333 if (status != FAILED)
334 {
335 enumerator_t *enumerator;
336 pb_tnc_msg_t *msg;
337 pb_tnc_batch_type_t batch_type;
338 bool empty = TRUE;
339
340 batch_type = batch->get_type(batch);
341
342 if (batch_type == PB_BATCH_CRETRY)
343 {
344 /* Send an SRETRY batch in response */
345 this->mutex->lock(this->mutex);
346 build_retry_batch(this);
347 this->mutex->unlock(this->mutex);
348 }
349 else if (batch_type == PB_BATCH_SRETRY)
350 {
351 /* Restart the measurements */
352 charon->imcs->notify_connection_change(charon->imcs,
353 this->connection_id, TNC_CONNECTION_STATE_HANDSHAKE);
354 charon->imcs->begin_handshake(charon->imcs, this->connection_id);
355 }
356
357 enumerator = batch->create_msg_enumerator(batch);
358 while (enumerator->enumerate(enumerator, &msg))
359 {
360 handle_message(this, msg);
361 empty = FALSE;
362 }
363 enumerator->destroy(enumerator);
364
365 /* received an empty CLOSE batch from PB-TNC client */
366 if (this->is_server && batch_type == PB_BATCH_CLOSE && empty)
367 {
368 batch->destroy(batch);
369 if (this->fatal_error)
370 {
371 DBG1(DBG_TNC, "a fatal PB-TNC error occurred, "
372 "terminating connection");
373 return FAILED;
374 }
375 else
376 {
377 return SUCCESS;
378 }
379 }
380
381 if (this->is_server)
382 {
383 charon->imvs->batch_ending(charon->imvs, this->connection_id);
384 }
385 else
386 {
387 charon->imcs->batch_ending(charon->imcs, this->connection_id);
388 }
389 }
390
391 switch (status)
392 {
393 case FAILED:
394 this->fatal_error = TRUE;
395 this->mutex->lock(this->mutex);
396 if (this->batch)
397 {
398 DBG1(DBG_TNC, "cancelling PB-TNC %N batch",
399 pb_tnc_batch_type_names, this->batch->get_type(this->batch));
400 this->batch->destroy(this->batch);
401 }
402 this->batch = pb_tnc_batch_create(this->is_server, PB_BATCH_CLOSE);
403 this->mutex->unlock(this->mutex);
404 /* fall through to add error messages to outbound batch */
405 case VERIFY_ERROR:
406 enumerator = batch->create_error_enumerator(batch);
407 while (enumerator->enumerate(enumerator, &msg))
408 {
409 this->mutex->lock(this->mutex);
410 this->batch->add_msg(this->batch, msg->get_ref(msg));
411 this->mutex->unlock(this->mutex);
412 }
413 enumerator->destroy(enumerator);
414 break;
415 case SUCCESS:
416 default:
417 break;
418 }
419 batch->destroy(batch);
420
421 return NEED_MORE;
422 }
423
424 /**
425 * Build a RESULT batch if a final recommendation is available
426 */
427 static void check_and_build_recommendation(private_tnccs_20_t *this)
428 {
429 TNC_IMV_Action_Recommendation rec;
430 TNC_IMV_Evaluation_Result eval;
431 TNC_IMVID id;
432 chunk_t reason, language;
433 enumerator_t *enumerator;
434 pb_tnc_msg_t *msg;
435
436 if (!this->recs->have_recommendation(this->recs, &rec, &eval))
437 {
438 charon->imvs->solicit_recommendation(charon->imvs, this->connection_id);
439 }
440 if (this->recs->have_recommendation(this->recs, &rec, &eval))
441 {
442 this->batch = pb_tnc_batch_create(this->is_server, PB_BATCH_RESULT);
443
444 msg = pb_assessment_result_msg_create(eval);
445 this->batch->add_msg(this->batch, msg);
446
447 /**
448 * IMV Action Recommendation and PB Access Recommendation codes
449 * are shifted by one.
450 */
451 msg = pb_access_recommendation_msg_create(rec + 1);
452 this->batch->add_msg(this->batch, msg);
453
454 enumerator = this->recs->create_reason_enumerator(this->recs);
455 while (enumerator->enumerate(enumerator, &id, &reason, &language))
456 {
457 msg = pb_reason_string_msg_create(reason, language);
458 this->batch->add_msg(this->batch, msg);
459 }
460 enumerator->destroy(enumerator);
461 }
462 }
463
464 METHOD(tls_t, build, status_t,
465 private_tnccs_20_t *this, void *buf, size_t *buflen, size_t *msglen)
466 {
467 status_t status;
468
469 /* Initialize the connection */
470 if (!this->is_server && !this->connection_id)
471 {
472 pb_tnc_msg_t *msg;
473 char *pref_lang;
474
475 this->connection_id = charon->tnccs->create_connection(charon->tnccs,
476 (tnccs_t*)this, _send_msg,
477 &this->request_handshake_retry, NULL);
478 if (!this->connection_id)
479 {
480 return FAILED;
481 }
482
483 /* Create PB-TNC Language Preference message */
484 pref_lang = charon->imcs->get_preferred_language(charon->imcs);
485 msg = pb_language_preference_msg_create(chunk_create(pref_lang,
486 strlen(pref_lang)));
487 this->mutex->lock(this->mutex);
488 this->batch = pb_tnc_batch_create(this->is_server, PB_BATCH_CDATA);
489 this->batch->add_msg(this->batch, msg);
490 this->mutex->unlock(this->mutex);
491
492 charon->imcs->notify_connection_change(charon->imcs,
493 this->connection_id, TNC_CONNECTION_STATE_CREATE);
494 charon->imcs->notify_connection_change(charon->imcs,
495 this->connection_id, TNC_CONNECTION_STATE_HANDSHAKE);
496 charon->imcs->begin_handshake(charon->imcs, this->connection_id);
497 }
498
499 if (this->is_server && this->fatal_error &&
500 this->state_machine->get_state(this->state_machine) == PB_STATE_END)
501 {
502 DBG1(DBG_TNC, "a fatal PB-TNC error occurred, terminating connection");
503 return FAILED;
504 }
505
506 /* Do not allow any asynchronous IMCs or IMVs to add additional messages */
507 this->mutex->lock(this->mutex);
508
509 if (this->request_handshake_retry)
510 {
511 build_retry_batch(this);
512
513 /* Reset the flag for the next handshake retry request */
514 this->request_handshake_retry = FALSE;
515 }
516
517 if (!this->batch)
518 {
519 pb_tnc_state_t state;
520
521 state = this->state_machine->get_state(this->state_machine);
522 if (this->is_server)
523 {
524 if (state == PB_STATE_SERVER_WORKING)
525 {
526 check_and_build_recommendation(this);
527 }
528 }
529 else
530 {
531 /**
532 * if the DECIDED state has been reached and no CRETRY is under way
533 * or if a CLOSE batch with error messages has been received,
534 * a PB-TNC client replies with an empty CLOSE batch.
535 */
536 if (state == PB_STATE_DECIDED || state == PB_STATE_END)
537 {
538 this->batch = pb_tnc_batch_create(this->is_server, PB_BATCH_CLOSE);
539 }
540 }
541 }
542
543 if (this->batch)
544 {
545 pb_tnc_batch_type_t batch_type;
546 chunk_t data;
547
548 batch_type = this->batch->get_type(this->batch);
549
550 if (this->state_machine->send_batch(this->state_machine, batch_type))
551 {
552 this->batch->build(this->batch);
553 data = this->batch->get_encoding(this->batch);
554 DBG1(DBG_TNC, "sending PB-TNC %N batch (%d bytes) for Connection ID %u",
555 pb_tnc_batch_type_names, batch_type, data.len,
556 this->connection_id);
557 DBG3(DBG_TNC, "%B", &data);
558 *msglen = data.len;
559
560 if (data.len > *buflen)
561 {
562 DBG1(DBG_TNC, "fragmentation of PB-TNC batch not supported yet");
563 }
564 else
565 {
566 *buflen = data.len;
567 }
568 memcpy(buf, data.ptr, *buflen);
569 status = ALREADY_DONE;
570 }
571 else
572 {
573 DBG1(DBG_TNC, "cancelling unexpected PB-TNC batch type: %N",
574 pb_tnc_batch_type_names, batch_type);
575 status = INVALID_STATE;
576 }
577
578 this->batch->destroy(this->batch);
579 this->batch = NULL;
580 }
581 else
582 {
583 DBG1(DBG_TNC, "no PB-TNC batch to send");
584 status = INVALID_STATE;
585 }
586 this->mutex->unlock(this->mutex);
587
588 return status;
589 }
590
591 METHOD(tls_t, is_server, bool,
592 private_tnccs_20_t *this)
593 {
594 return this->is_server;
595 }
596
597 METHOD(tls_t, get_purpose, tls_purpose_t,
598 private_tnccs_20_t *this)
599 {
600 return TLS_PURPOSE_EAP_TNC;
601 }
602
603 METHOD(tls_t, is_complete, bool,
604 private_tnccs_20_t *this)
605 {
606 TNC_IMV_Action_Recommendation rec;
607 TNC_IMV_Evaluation_Result eval;
608
609 if (this->recs && this->recs->have_recommendation(this->recs, &rec, &eval))
610 {
611 return charon->imvs->enforce_recommendation(charon->imvs, rec, eval);
612 }
613 else
614 {
615 return FALSE;
616 }
617 }
618
619 METHOD(tls_t, get_eap_msk, chunk_t,
620 private_tnccs_20_t *this)
621 {
622 return chunk_empty;
623 }
624
625 METHOD(tls_t, destroy, void,
626 private_tnccs_20_t *this)
627 {
628 charon->tnccs->remove_connection(charon->tnccs, this->connection_id,
629 this->is_server);
630 this->state_machine->destroy(this->state_machine);
631 this->mutex->destroy(this->mutex);
632 DESTROY_IF(this->batch);
633 free(this);
634 }
635
636 /**
637 * See header
638 */
639 tls_t *tnccs_20_create(bool is_server)
640 {
641 private_tnccs_20_t *this;
642
643 INIT(this,
644 .public = {
645 .process = _process,
646 .build = _build,
647 .is_server = _is_server,
648 .get_purpose = _get_purpose,
649 .is_complete = _is_complete,
650 .get_eap_msk = _get_eap_msk,
651 .destroy = _destroy,
652 },
653 .is_server = is_server,
654 .state_machine = pb_tnc_state_machine_create(is_server),
655 .mutex = mutex_create(MUTEX_TYPE_DEFAULT),
656 );
657
658 return &this->public;
659 }