Whitespace cleanups in tnc code
[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 }
323
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);
330
331 if (status != FAILED)
332 {
333 enumerator_t *enumerator;
334 pb_tnc_msg_t *msg;
335 pb_tnc_batch_type_t batch_type;
336 bool empty = TRUE;
337
338 batch_type = batch->get_type(batch);
339
340 if (batch_type == PB_BATCH_CRETRY)
341 {
342 /* Send an SRETRY batch in response */
343 this->mutex->lock(this->mutex);
344 build_retry_batch(this);
345 this->mutex->unlock(this->mutex);
346 }
347 else if (batch_type == PB_BATCH_SRETRY)
348 {
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);
353 }
354
355 enumerator = batch->create_msg_enumerator(batch);
356 while (enumerator->enumerate(enumerator, &msg))
357 {
358 handle_message(this, msg);
359 empty = FALSE;
360 }
361 enumerator->destroy(enumerator);
362
363 /* received an empty CLOSE batch from PB-TNC client */
364 if (this->is_server && batch_type == PB_BATCH_CLOSE && empty)
365 {
366 batch->destroy(batch);
367 if (this->fatal_error)
368 {
369 DBG1(DBG_TNC, "a fatal PB-TNC error occurred, "
370 "terminating connection");
371 return FAILED;
372 }
373 else
374 {
375 return SUCCESS;
376 }
377 }
378
379 if (this->is_server)
380 {
381 charon->imvs->batch_ending(charon->imvs, this->connection_id);
382 }
383 else
384 {
385 charon->imcs->batch_ending(charon->imcs, this->connection_id);
386 }
387 }
388
389 switch (status)
390 {
391 case FAILED:
392 this->fatal_error = TRUE;
393 this->mutex->lock(this->mutex);
394 if (this->batch)
395 {
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);
399 }
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 */
403 case VERIFY_ERROR:
404 enumerator = batch->create_error_enumerator(batch);
405 while (enumerator->enumerate(enumerator, &msg))
406 {
407 this->mutex->lock(this->mutex);
408 this->batch->add_msg(this->batch, msg->get_ref(msg));
409 this->mutex->unlock(this->mutex);
410 }
411 enumerator->destroy(enumerator);
412 break;
413 case SUCCESS:
414 default:
415 break;
416 }
417 batch->destroy(batch);
418
419 return NEED_MORE;
420 }
421
422 /**
423 * Build a RESULT batch if a final recommendation is available
424 */
425 static void check_and_build_recommendation(private_tnccs_20_t *this)
426 {
427 TNC_IMV_Action_Recommendation rec;
428 TNC_IMV_Evaluation_Result eval;
429 TNC_IMVID id;
430 chunk_t reason, language;
431 enumerator_t *enumerator;
432 pb_tnc_msg_t *msg;
433
434 if (!this->recs->have_recommendation(this->recs, &rec, &eval))
435 {
436 charon->imvs->solicit_recommendation(charon->imvs, this->connection_id);
437 }
438 if (this->recs->have_recommendation(this->recs, &rec, &eval))
439 {
440 this->batch = pb_tnc_batch_create(this->is_server, PB_BATCH_RESULT);
441
442 msg = pb_assessment_result_msg_create(eval);
443 this->batch->add_msg(this->batch, msg);
444
445 /**
446 * IMV Action Recommendation and PB Access Recommendation codes
447 * are shifted by one.
448 */
449 msg = pb_access_recommendation_msg_create(rec + 1);
450 this->batch->add_msg(this->batch, msg);
451
452 enumerator = this->recs->create_reason_enumerator(this->recs);
453 while (enumerator->enumerate(enumerator, &id, &reason, &language))
454 {
455 msg = pb_reason_string_msg_create(reason, language);
456 this->batch->add_msg(this->batch, msg);
457 }
458 enumerator->destroy(enumerator);
459 }
460 }
461
462 METHOD(tls_t, build, status_t,
463 private_tnccs_20_t *this, void *buf, size_t *buflen, size_t *msglen)
464 {
465 status_t status;
466
467 /* Initialize the connection */
468 if (!this->is_server && !this->connection_id)
469 {
470 pb_tnc_msg_t *msg;
471 char *pref_lang;
472
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)
477 {
478 return FAILED;
479 }
480
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,
484 strlen(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);
489
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);
495 }
496
497 if (this->is_server && this->fatal_error &&
498 this->state_machine->get_state(this->state_machine) == PB_STATE_END)
499 {
500 DBG1(DBG_TNC, "a fatal PB-TNC error occurred, terminating connection");
501 return FAILED;
502 }
503
504 /* Do not allow any asynchronous IMCs or IMVs to add additional messages */
505 this->mutex->lock(this->mutex);
506
507 if (this->request_handshake_retry)
508 {
509 build_retry_batch(this);
510
511 /* Reset the flag for the next handshake retry request */
512 this->request_handshake_retry = FALSE;
513 }
514
515 if (!this->batch)
516 {
517 pb_tnc_state_t state;
518
519 state = this->state_machine->get_state(this->state_machine);
520 if (this->is_server)
521 {
522 if (state == PB_STATE_SERVER_WORKING)
523 {
524 check_and_build_recommendation(this);
525 }
526 }
527 else
528 {
529 /**
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.
533 */
534 if (state == PB_STATE_DECIDED || state == PB_STATE_END)
535 {
536 this->batch = pb_tnc_batch_create(this->is_server, PB_BATCH_CLOSE);
537 }
538 }
539 }
540
541 if (this->batch)
542 {
543 pb_tnc_batch_type_t batch_type;
544 chunk_t data;
545
546 batch_type = this->batch->get_type(this->batch);
547
548 if (this->state_machine->send_batch(this->state_machine, batch_type))
549 {
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);
556 *msglen = data.len;
557
558 if (data.len > *buflen)
559 {
560 DBG1(DBG_TNC, "fragmentation of PB-TNC batch not supported yet");
561 }
562 else
563 {
564 *buflen = data.len;
565 }
566 memcpy(buf, data.ptr, *buflen);
567 status = ALREADY_DONE;
568 }
569 else
570 {
571 DBG1(DBG_TNC, "cancelling unexpected PB-TNC batch type: %N",
572 pb_tnc_batch_type_names, batch_type);
573 status = INVALID_STATE;
574 }
575
576 this->batch->destroy(this->batch);
577 this->batch = NULL;
578 }
579 else
580 {
581 DBG1(DBG_TNC, "no PB-TNC batch to send");
582 status = INVALID_STATE;
583 }
584 this->mutex->unlock(this->mutex);
585
586 return status;
587 }
588
589 METHOD(tls_t, is_server, bool,
590 private_tnccs_20_t *this)
591 {
592 return this->is_server;
593 }
594
595 METHOD(tls_t, get_purpose, tls_purpose_t,
596 private_tnccs_20_t *this)
597 {
598 return TLS_PURPOSE_EAP_TNC;
599 }
600
601 METHOD(tls_t, is_complete, bool,
602 private_tnccs_20_t *this)
603 {
604 TNC_IMV_Action_Recommendation rec;
605 TNC_IMV_Evaluation_Result eval;
606
607 if (this->recs && this->recs->have_recommendation(this->recs, &rec, &eval))
608 {
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);
612
613 return charon->imvs->enforce_recommendation(charon->imvs, rec);
614 }
615 else
616 {
617 return FALSE;
618 }
619 }
620
621 METHOD(tls_t, get_eap_msk, chunk_t,
622 private_tnccs_20_t *this)
623 {
624 return chunk_empty;
625 }
626
627 METHOD(tls_t, destroy, void,
628 private_tnccs_20_t *this)
629 {
630 if (this->is_server)
631 {
632 charon->imvs->notify_connection_change(charon->imvs,
633 this->connection_id, TNC_CONNECTION_STATE_DELETE);
634 }
635 else
636 {
637 charon->imcs->notify_connection_change(charon->imcs,
638 this->connection_id, TNC_CONNECTION_STATE_DELETE);
639 }
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);
644 free(this);
645 }
646
647 /**
648 * See header
649 */
650 tls_t *tnccs_20_create(bool is_server)
651 {
652 private_tnccs_20_t *this;
653
654 INIT(this,
655 .public = {
656 .process = _process,
657 .build = _build,
658 .is_server = _is_server,
659 .get_purpose = _get_purpose,
660 .is_complete = _is_complete,
661 .get_eap_msk = _get_eap_msk,
662 .destroy = _destroy,
663 },
664 .is_server = is_server,
665 .state_machine = pb_tnc_state_machine_create(is_server),
666 .mutex = mutex_create(MUTEX_TYPE_DEFAULT),
667 );
668
669 return &this->public;
670 }