refactored message handling
[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 "tnccs_20_types.h"
19 #include "batch/pb_tnc_batch.h"
20 #include "messages/pb_tnc_message.h"
21 #include "messages/pb_pa_message.h"
22 #include "messages/pb_error_message.h"
23 #include "messages/pb_assessment_result_message.h"
24 #include "messages/pb_access_recommendation_message.h"
25 #include "messages/pb_reason_string_message.h"
26 #include "messages/pb_language_preference_message.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_names.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 by IMC/IMV RequestHandshakeRetry() function
75 */
76 bool request_handshake_retry;
77
78 /**
79 * Set of IMV recommendations (TNC Server only)
80 */
81 recommendations_t *recs;
82 };
83
84 METHOD(tnccs_t, send_message, void,
85 private_tnccs_20_t* this, TNC_IMCID imc_id, TNC_IMVID imv_id,
86 TNC_BufferReference msg,
87 TNC_UInt32 msg_len,
88 TNC_MessageType msg_type)
89 {
90 TNC_MessageSubtype msg_sub_type;
91 TNC_VendorID msg_vendor_id;
92 pb_tnc_message_t *pb_tnc_msg;
93 pb_tnc_batch_type_t batch_type;
94
95 msg_sub_type = msg_type & TNC_SUBTYPE_ANY;
96 msg_vendor_id = (msg_type >> 8) & TNC_VENDORID_ANY;
97
98 pb_tnc_msg = pb_pa_message_create(msg_vendor_id, msg_sub_type, imc_id, imv_id,
99 chunk_create(msg, msg_len));
100
101 /* adding PA message to SDATA or CDATA batch only */
102 batch_type = this->is_server ? PB_BATCH_SDATA : PB_BATCH_CDATA;
103 this->mutex->lock(this->mutex);
104 if (!this->batch)
105 {
106 this->batch = pb_tnc_batch_create(this->is_server, batch_type);
107 }
108 if (this->batch->get_type(this->batch) == batch_type)
109 {
110 this->batch->add_message(this->batch, pb_tnc_msg);
111 }
112 else
113 {
114 pb_tnc_msg->destroy(pb_tnc_msg);
115 }
116 this->mutex->unlock(this->mutex);
117 }
118
119 static void handle_message(private_tnccs_20_t *this, pb_tnc_message_t *msg)
120 {
121 switch (msg->get_type(msg))
122 {
123 case PB_MSG_EXPERIMENTAL:
124 /* for experiments */
125 break;
126 case PB_MSG_PA:
127 {
128 pb_pa_message_t *pa_msg;
129 TNC_MessageType msg_type;
130 u_int32_t vendor_id, subtype;
131 chunk_t msg_body;
132
133 pa_msg = (pb_pa_message_t*)msg;
134 vendor_id = pa_msg->get_vendor_id(pa_msg, &subtype);
135 msg_type = (vendor_id << 8) | (subtype & 0xff);
136 msg_body = pa_msg->get_body(pa_msg);
137
138 DBG2(DBG_TNC, "handling message type 0x%08x", msg_type);
139
140 if (this->is_server)
141 {
142 charon->imvs->receive_message(charon->imvs,
143 this->connection_id, msg_body.ptr, msg_body.len, msg_type);
144 }
145 else
146 {
147 charon->imcs->receive_message(charon->imcs,
148 this->connection_id, msg_body.ptr, msg_body.len,msg_type);
149 }
150 break;
151 }
152 case PB_MSG_ASSESSMENT_RESULT:
153 {
154 pb_assessment_result_message_t *assess_msg;
155 u_int32_t result;
156
157 assess_msg = (pb_assessment_result_message_t*)msg;
158 result = assess_msg->get_assessment_result(assess_msg);
159 DBG1(DBG_TNC, "assessment result is '%N'",
160 evaluation_result_names, result);
161 break;
162 }
163 case PB_MSG_ACCESS_RECOMMENDATION:
164 {
165 pb_access_recommendation_message_t *rec_msg;
166 u_int16_t rec;
167
168 rec_msg = (pb_access_recommendation_message_t*)msg;
169 rec = rec_msg->get_access_recommendation(rec_msg);
170 DBG1(DBG_TNC, "access_recommendation is '%N'",
171 action_recommendation_names, rec);
172 break;
173 }
174 case PB_MSG_REMEDIATION_PARAMETERS:
175 {
176 /* TODO : Remediation parameters message processing */
177 break;
178 }
179 case PB_MSG_ERROR:
180 {
181 pb_error_message_t *err_msg;
182 bool fatal;
183 u_int32_t vendor_id;
184 u_int16_t error_code;
185
186 err_msg = (pb_error_message_t*)msg;
187 fatal = err_msg->get_fatal_flag(err_msg);
188 vendor_id = err_msg->get_vendor_id(err_msg);
189 error_code = err_msg->get_error_code(err_msg);
190
191 if (vendor_id == IETF_VENDOR_ID)
192 {
193 switch (error_code)
194 {
195 case PB_ERROR_INVALID_PARAMETER:
196 case PB_ERROR_UNSUPPORTED_MANDATORY_MESSAGE:
197 DBG1(DBG_TNC, "received %s PB-TNC Error '%N' "
198 "(offset %u bytes)",
199 fatal ? "fatal" : "non-fatal",
200 pb_tnc_error_code_names, error_code,
201 err_msg->get_offset(err_msg));
202 break;
203 case PB_ERROR_VERSION_NOT_SUPPORTED:
204 DBG1(DBG_TNC, "received %s PB-TNC Error '%N' "
205 "caused by bad version 0x%02x",
206 fatal ? "fatal" : "non-fatal",
207 pb_tnc_error_code_names, error_code,
208 err_msg->get_bad_version(err_msg));
209 break;
210 case PB_ERROR_UNEXPECTED_BATCH_TYPE:
211 case PB_ERROR_LOCAL_ERROR:
212 default:
213 DBG1(DBG_TNC, "received %s PB-TNC Error '%N'",
214 fatal ? "fatal" : "non-fatal",
215 pb_tnc_error_code_names, error_code);
216 break;
217 }
218 }
219 else
220 {
221 DBG1(DBG_TNC, "received %s PB-TNC Error (%u) "
222 "with Vendor ID 0x%06x",
223 fatal ? "fatal" : "non-fatal",
224 error_code, vendor_id);
225 }
226 break;
227 }
228 case PB_MSG_LANGUAGE_PREFERENCE:
229 {
230 pb_language_preference_message_t *lang_msg;
231 chunk_t lang;
232
233 lang_msg = (pb_language_preference_message_t*)msg;
234 lang = lang_msg->get_language_preference(lang_msg);
235
236 DBG2(DBG_TNC, "setting language preference '%.*s'", lang.len, lang.ptr);
237 this->recs->set_preferred_language(this->recs, lang);
238 break;
239 }
240 case PB_MSG_REASON_STRING:
241 {
242 pb_reason_string_message_t *reason_msg;
243 chunk_t reason_string, language_code;
244
245 reason_msg = (pb_reason_string_message_t*)msg;
246 reason_string = reason_msg->get_reason_string(reason_msg);
247 language_code = reason_msg->get_language_code(reason_msg);
248 DBG2(DBG_TNC, "reason string: '%.*s", reason_string.len,
249 reason_string.ptr);
250 DBG2(DBG_TNC, "language code: '%.*s", language_code.len,
251 language_code.ptr);
252 break;
253 }
254 default:
255 break;
256 }
257 }
258
259 METHOD(tls_t, process, status_t,
260 private_tnccs_20_t *this, void *buf, size_t buflen)
261 {
262 chunk_t data;
263 pb_tnc_batch_t *batch;
264 pb_tnc_message_t *msg;
265 enumerator_t *enumerator;
266 status_t status;
267
268 if (this->is_server && !this->connection_id)
269 {
270 this->connection_id = charon->tnccs->create_connection(charon->tnccs,
271 (tnccs_t*)this, _send_message,
272 &this->request_handshake_retry, &this->recs);
273 if (!this->connection_id)
274 {
275 return FAILED;
276 }
277 charon->imvs->notify_connection_change(charon->imvs,
278 this->connection_id, TNC_CONNECTION_STATE_CREATE);
279 }
280
281 data = chunk_create(buf, buflen);
282 DBG1(DBG_TNC, "received TNCCS Batch (%u bytes) for Connection ID %u",
283 data.len, this->connection_id);
284 DBG3(DBG_TNC, "%B", &data);
285 batch = pb_tnc_batch_create_from_data(this->is_server, data);
286 status = batch->process(batch, this->state_machine);
287
288 if (status != FAILED)
289 {
290 enumerator_t *enumerator;
291 pb_tnc_message_t *msg;
292 pb_tnc_batch_type_t batch_type;
293 bool empty = TRUE;
294
295 batch_type = batch->get_type(batch);
296
297 if (batch_type == PB_BATCH_CRETRY)
298 {
299 this->mutex->lock(this->mutex);
300 if (this->batch)
301 {
302 DBG1(DBG_TNC, "cancelling PB-TNC %N Batch",
303 pb_tnc_batch_type_names, this->batch->get_type(this->batch));
304 this->batch->destroy(this->batch);
305 }
306 this->batch = pb_tnc_batch_create(this->is_server, PB_BATCH_SRETRY);
307 this->mutex->unlock(this->mutex);
308 }
309 else if (batch_type == PB_BATCH_SRETRY)
310 {
311 /* Restart the measurements */
312 charon->imcs->notify_connection_change(charon->imcs,
313 this->connection_id, TNC_CONNECTION_STATE_HANDSHAKE);
314 charon->imcs->begin_handshake(charon->imcs, this->connection_id);
315 }
316
317 enumerator = batch->create_msg_enumerator(batch);
318 while (enumerator->enumerate(enumerator, &msg))
319 {
320 handle_message(this, msg);
321 empty = FALSE;
322 }
323 enumerator->destroy(enumerator);
324
325 /* received an empty CLOSE Batch from PB-TNC Client */
326 if (this->is_server && batch_type == PB_BATCH_CLOSE && empty)
327 {
328 batch->destroy(batch);
329 return SUCCESS;
330 }
331
332 if (this->is_server)
333 {
334 charon->imvs->batch_ending(charon->imvs, this->connection_id);
335 }
336 else
337 {
338 charon->imcs->batch_ending(charon->imcs, this->connection_id);
339 }
340 }
341
342 switch (status)
343 {
344 case FAILED:
345 this->mutex->lock(this->mutex);
346 if (this->batch)
347 {
348 DBG1(DBG_TNC, "cancelling PB-TNC %N Batch",
349 pb_tnc_batch_type_names, this->batch->get_type(this->batch));
350 this->batch->destroy(this->batch);
351 }
352 this->batch = pb_tnc_batch_create(this->is_server, PB_BATCH_CLOSE);
353 this->mutex->unlock(this->mutex);
354 /* fall through to add error messages to outbound batch */
355 case VERIFY_ERROR:
356 enumerator = batch->create_error_enumerator(batch);
357 while (enumerator->enumerate(enumerator, &msg))
358 {
359 this->mutex->lock(this->mutex);
360 this->batch->add_message(this->batch, msg->get_ref(msg));
361 this->mutex->unlock(this->mutex);
362 }
363 enumerator->destroy(enumerator);
364 break;
365 case SUCCESS:
366 default:
367 break;
368 }
369 batch->destroy(batch);
370
371 return NEED_MORE;
372 }
373
374 METHOD(tls_t, build, status_t,
375 private_tnccs_20_t *this, void *buf, size_t *buflen, size_t *msglen)
376 {
377 status_t status;
378
379 if (!this->is_server && !this->connection_id)
380 {
381 pb_tnc_message_t *msg;
382 char *pref_lang;
383
384 this->connection_id = charon->tnccs->create_connection(charon->tnccs,
385 (tnccs_t*)this, _send_message,
386 &this->request_handshake_retry, NULL);
387 if (!this->connection_id)
388 {
389 return FAILED;
390 }
391
392 /* Create PB-TNC Language Preference Message */
393 pref_lang = charon->imcs->get_preferred_language(charon->imcs);
394 msg = pb_language_preference_message_create(chunk_create(pref_lang,
395 strlen(pref_lang)));
396 this->mutex->lock(this->mutex);
397 this->batch = pb_tnc_batch_create(this->is_server, PB_BATCH_CDATA);
398 this->batch->add_message(this->batch, msg);
399 this->mutex->unlock(this->mutex);
400
401 charon->imcs->notify_connection_change(charon->imcs,
402 this->connection_id, TNC_CONNECTION_STATE_CREATE);
403 charon->imcs->notify_connection_change(charon->imcs,
404 this->connection_id, TNC_CONNECTION_STATE_HANDSHAKE);
405 charon->imcs->begin_handshake(charon->imcs, this->connection_id);
406 }
407
408 /* Do not allow any asynchronous IMCs or IMVs to add additional messages */
409 this->mutex->lock(this->mutex);
410
411 /**
412 * if the DECIDED state has been reached, close down the TNCCS connection
413 * by sending an empty CLOSE batch unless a CRETRY batch is under way
414 */
415 if (this->state_machine->get_state(this->state_machine) == PB_STATE_DECIDED &&
416 !this->batch)
417 {
418 this->batch = pb_tnc_batch_create(this->is_server, PB_BATCH_CLOSE);
419 }
420
421 if (this->batch)
422 {
423 pb_tnc_batch_type_t batch_type;
424 chunk_t data;
425
426 batch_type = this->batch->get_type(this->batch);
427
428 if (this->state_machine->send_batch(this->state_machine, batch_type))
429 {
430 this->batch->build(this->batch);
431 data = this->batch->get_encoding(this->batch);
432 DBG1(DBG_TNC, "sending PB-TNC %N Batch (%d bytes) for Connection ID %u",
433 pb_tnc_batch_type_names, batch_type, data.len,
434 this->connection_id);
435 DBG3(DBG_TNC, "%B", &data);
436
437 *msglen = data.len;
438 *buflen = data.len;
439 memcpy(buf, data.ptr, data.len);
440 status = ALREADY_DONE;
441 }
442 else
443 {
444 DBG1(DBG_TNC, "cancelling unexpected PB-TNC Batch Type: %N",
445 pb_tnc_batch_type_names, batch_type);
446 status = INVALID_STATE;
447 }
448
449 this->batch->destroy(this->batch);
450 this->batch = NULL;
451 }
452 else
453 {
454 DBG1(DBG_TNC, "no TNCCS Batch to send");
455 status = INVALID_STATE;
456 }
457 this->mutex->unlock(this->mutex);
458
459 return status;
460 }
461
462 METHOD(tls_t, is_server, bool,
463 private_tnccs_20_t *this)
464 {
465 return this->is_server;
466 }
467
468 METHOD(tls_t, get_purpose, tls_purpose_t,
469 private_tnccs_20_t *this)
470 {
471 return TLS_PURPOSE_EAP_TNC;
472 }
473
474 METHOD(tls_t, is_complete, bool,
475 private_tnccs_20_t *this)
476 {
477 TNC_IMV_Action_Recommendation rec;
478 TNC_IMV_Evaluation_Result eval;
479
480 if (this->recs && this->recs->have_recommendation(this->recs, &rec, &eval))
481 {
482 DBG2(DBG_TNC, "Final recommendation '%N' and evaluation '%N'",
483 action_recommendation_names, rec, evaluation_result_names, eval);
484
485 return charon->imvs->enforce_recommendation(charon->imvs, rec);
486 }
487 else
488 {
489 return FALSE;
490 }
491 }
492
493 METHOD(tls_t, get_eap_msk, chunk_t,
494 private_tnccs_20_t *this)
495 {
496 return chunk_empty;
497 }
498
499 METHOD(tls_t, destroy, void,
500 private_tnccs_20_t *this)
501 {
502 charon->tnccs->remove_connection(charon->tnccs, this->connection_id);
503 this->state_machine->destroy(this->state_machine);
504 this->mutex->destroy(this->mutex);
505 DESTROY_IF(this->batch);
506 free(this);
507 }
508
509 /**
510 * See header
511 */
512 tls_t *tnccs_20_create(bool is_server)
513 {
514 private_tnccs_20_t *this;
515
516 INIT(this,
517 .public = {
518 .process = _process,
519 .build = _build,
520 .is_server = _is_server,
521 .get_purpose = _get_purpose,
522 .is_complete = _is_complete,
523 .get_eap_msk = _get_eap_msk,
524 .destroy = _destroy,
525 },
526 .is_server = is_server,
527 .state_machine = pb_tnc_state_machine_create(is_server),
528 .mutex = mutex_create(MUTEX_TYPE_DEFAULT),
529 );
530
531 return &this->public;
532 }