1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
|
/*
* Copyright (C) 2010-2012 Andreas Steffen
* HSR Hochschule fuer Technik Rapperswil
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*/
#include "tnc_tnccs_manager.h"
#include <tnc/tnc.h>
#include <tnc/imv/imv_manager.h>
#include <tnc/imc/imc_manager.h>
#include <tnc/imv/imv_manager.h>
#include <utils/debug.h>
#include <collections/linked_list.h>
#include <threading/rwlock.h>
typedef struct private_tnc_tnccs_manager_t private_tnc_tnccs_manager_t;
typedef struct tnccs_entry_t tnccs_entry_t;
typedef struct tnccs_connection_entry_t tnccs_connection_entry_t;
/**
* TNCCS constructor entry
*/
struct tnccs_entry_t {
/**
* TNCCS protocol type
*/
tnccs_type_t type;
/**
* constructor function to create instance
*/
tnccs_constructor_t constructor;
};
/**
* TNCCS connection entry
*/
struct tnccs_connection_entry_t {
/**
* TNCCS connection ID
*/
TNC_ConnectionID id;
/**
* TNCCS protocol type
*/
tnccs_type_t type;
/**
* TNCCS instance
*/
tnccs_t *tnccs;
/**
* TNCCS send message function
*/
tnccs_send_message_t send_message;
/**
* TNCCS request handshake retry flag
*/
bool *request_handshake_retry;
/**
* Maximum size of a PA-TNC message
*/
u_int32_t max_msg_len;
/**
* collection of IMV recommendations
*/
recommendations_t *recs;
};
/**
* private data of tnc_tnccs_manager
*/
struct private_tnc_tnccs_manager_t {
/**
* public functions
*/
tnccs_manager_t public;
/**
* list of TNCCS protocol entries
*/
linked_list_t *protocols;
/**
* rwlock to lock the TNCCS protocol entries
*/
rwlock_t *protocol_lock;
/**
* connection ID counter
*/
TNC_ConnectionID connection_id;
/**
* list of TNCCS connection entries
*/
linked_list_t *connections;
/**
* rwlock to lock TNCCS connection entries
*/
rwlock_t *connection_lock;
};
METHOD(tnccs_manager_t, add_method, void,
private_tnc_tnccs_manager_t *this, tnccs_type_t type,
tnccs_constructor_t constructor)
{
tnccs_entry_t *entry;
entry = malloc_thing(tnccs_entry_t);
entry->type = type;
entry->constructor = constructor;
this->protocol_lock->write_lock(this->protocol_lock);
this->protocols->insert_last(this->protocols, entry);
this->protocol_lock->unlock(this->protocol_lock);
}
METHOD(tnccs_manager_t, remove_method, void,
private_tnc_tnccs_manager_t *this, tnccs_constructor_t constructor)
{
enumerator_t *enumerator;
tnccs_entry_t *entry;
this->protocol_lock->write_lock(this->protocol_lock);
enumerator = this->protocols->create_enumerator(this->protocols);
while (enumerator->enumerate(enumerator, &entry))
{
if (constructor == entry->constructor)
{
this->protocols->remove_at(this->protocols, enumerator);
free(entry);
}
}
enumerator->destroy(enumerator);
this->protocol_lock->unlock(this->protocol_lock);
}
METHOD(tnccs_manager_t, create_instance, tnccs_t*,
private_tnc_tnccs_manager_t *this, tnccs_type_t type, bool is_server)
{
enumerator_t *enumerator;
tnccs_entry_t *entry;
tnccs_t *protocol = NULL;
this->protocol_lock->read_lock(this->protocol_lock);
enumerator = this->protocols->create_enumerator(this->protocols);
while (enumerator->enumerate(enumerator, &entry))
{
if (type == entry->type)
{
protocol = entry->constructor(is_server);
if (protocol)
{
break;
}
}
}
enumerator->destroy(enumerator);
this->protocol_lock->unlock(this->protocol_lock);
return protocol;
}
METHOD(tnccs_manager_t, create_connection, TNC_ConnectionID,
private_tnc_tnccs_manager_t *this, tnccs_type_t type, tnccs_t *tnccs,
tnccs_send_message_t send_message, bool* request_handshake_retry,
u_int32_t max_msg_len, recommendations_t **recs)
{
tnccs_connection_entry_t *entry;
entry = malloc_thing(tnccs_connection_entry_t);
entry->type = type;
entry->tnccs = tnccs;
entry->send_message = send_message;
entry->request_handshake_retry = request_handshake_retry;
entry->max_msg_len = max_msg_len;
if (recs)
{
/* we assume a TNC Server needing recommendations from IMVs */
if (!tnc->imvs)
{
DBG1(DBG_TNC, "no IMV manager available!");
free(entry);
return 0;
}
entry->recs = tnc->imvs->create_recommendations(tnc->imvs);
*recs = entry->recs;
}
else
{
/* we assume a TNC Client */
if (!tnc->imcs)
{
DBG1(DBG_TNC, "no IMC manager available!");
free(entry);
return 0;
}
entry->recs = NULL;
}
this->connection_lock->write_lock(this->connection_lock);
entry->id = ++this->connection_id;
this->connections->insert_last(this->connections, entry);
this->connection_lock->unlock(this->connection_lock);
DBG1(DBG_TNC, "assigned TNCCS Connection ID %u", entry->id);
return entry->id;
}
METHOD(tnccs_manager_t, remove_connection, void,
private_tnc_tnccs_manager_t *this, TNC_ConnectionID id, bool is_server)
{
enumerator_t *enumerator;
tnccs_connection_entry_t *entry;
if (is_server)
{
if (tnc->imvs)
{
tnc->imvs->notify_connection_change(tnc->imvs, id,
TNC_CONNECTION_STATE_DELETE);
}
}
else
{
if (tnc->imcs)
{
tnc->imcs->notify_connection_change(tnc->imcs, id,
TNC_CONNECTION_STATE_DELETE);
}
}
this->connection_lock->write_lock(this->connection_lock);
enumerator = this->connections->create_enumerator(this->connections);
while (enumerator->enumerate(enumerator, &entry))
{
if (id == entry->id)
{
this->connections->remove_at(this->connections, enumerator);
if (entry->recs)
{
entry->recs->destroy(entry->recs);
}
free(entry);
DBG1(DBG_TNC, "removed TNCCS Connection ID %u", id);
}
}
enumerator->destroy(enumerator);
this->connection_lock->unlock(this->connection_lock);
}
METHOD(tnccs_manager_t, request_handshake_retry, TNC_Result,
private_tnc_tnccs_manager_t *this, bool is_imc, TNC_UInt32 imcv_id,
TNC_ConnectionID id,
TNC_RetryReason reason)
{
enumerator_t *enumerator;
tnccs_connection_entry_t *entry;
if (id == TNC_CONNECTIONID_ANY)
{
DBG2(DBG_TNC, "%s %u requests handshake retry for all connections "
"(reason: %u)", is_imc ? "IMC":"IMV", reason);
}
else
{
DBG2(DBG_TNC, "%s %u requests handshake retry for Connection ID %u "
"(reason: %u)", is_imc ? "IMC":"IMV", imcv_id, id, reason);
}
this->connection_lock->read_lock(this->connection_lock);
enumerator = this->connections->create_enumerator(this->connections);
while (enumerator->enumerate(enumerator, &entry))
{
if (id == TNC_CONNECTIONID_ANY || id == entry->id)
{
*entry->request_handshake_retry = TRUE;
break;
}
}
enumerator->destroy(enumerator);
this->connection_lock->unlock(this->connection_lock);
return TNC_RESULT_SUCCESS;
}
METHOD(tnccs_manager_t, send_message, TNC_Result,
private_tnc_tnccs_manager_t *this, TNC_IMCID imc_id, TNC_IMVID imv_id,
TNC_ConnectionID id,
TNC_UInt32 msg_flags,
TNC_BufferReference msg,
TNC_UInt32 msg_len,
TNC_VendorID msg_vid,
TNC_MessageSubtype msg_subtype)
{
enumerator_t *enumerator;
tnccs_connection_entry_t *entry;
tnccs_send_message_t send_message = NULL;
tnccs_t *tnccs = NULL;
if (msg_vid == TNC_VENDORID_ANY || msg_subtype == TNC_SUBTYPE_ANY)
{
DBG1(DBG_TNC, "not sending message of invalid type 0x%02x/0x%08x",
msg_vid, msg_subtype);
return TNC_RESULT_INVALID_PARAMETER;
}
this->connection_lock->read_lock(this->connection_lock);
enumerator = this->connections->create_enumerator(this->connections);
while (enumerator->enumerate(enumerator, &entry))
{
if (id == entry->id)
{
tnccs = entry->tnccs;
send_message = entry->send_message;
break;
}
}
enumerator->destroy(enumerator);
this->connection_lock->unlock(this->connection_lock);
if (tnccs && send_message)
{
return send_message(tnccs, imc_id, imv_id, msg_flags, msg, msg_len,
msg_vid, msg_subtype);
}
return TNC_RESULT_FATAL;
}
METHOD(tnccs_manager_t, provide_recommendation, TNC_Result,
private_tnc_tnccs_manager_t *this, TNC_IMVID imv_id,
TNC_ConnectionID id,
TNC_IMV_Action_Recommendation rec,
TNC_IMV_Evaluation_Result eval)
{
enumerator_t *enumerator;
tnccs_connection_entry_t *entry;
recommendations_t *recs = NULL;
this->connection_lock->read_lock(this->connection_lock);
enumerator = this->connections->create_enumerator(this->connections);
while (enumerator->enumerate(enumerator, &entry))
{
if (id == entry->id)
{
recs = entry->recs;
break;
}
}
enumerator->destroy(enumerator);
this->connection_lock->unlock(this->connection_lock);
if (recs)
{
recs->provide_recommendation(recs, imv_id, rec, eval);
return TNC_RESULT_SUCCESS;
}
return TNC_RESULT_FATAL;
}
/**
* Write the value of a boolean attribute into the buffer
*/
static TNC_Result bool_attribute(TNC_UInt32 buffer_len,
TNC_BufferReference buffer,
TNC_UInt32 *value_len,
bool value)
{
*value_len = 1;
if (buffer && buffer_len > 0)
{
*buffer = value ? 0x01 : 0x00;
return TNC_RESULT_SUCCESS;
}
else
{
return TNC_RESULT_INVALID_PARAMETER;
}
}
/**
* Write the value of an u_int32_t attribute into the buffer
*/
static TNC_Result uint_attribute(TNC_UInt32 buffer_len,
TNC_BufferReference buffer,
TNC_UInt32 *value_len,
u_int32_t value)
{
*value_len = sizeof(u_int32_t);
if (buffer && buffer_len >= *value_len)
{
htoun32(buffer, value);
return TNC_RESULT_SUCCESS;
}
else
{
return TNC_RESULT_INVALID_PARAMETER;
}
}
/**
* Write the value of string attribute into the buffer
*/
static TNC_Result str_attribute(TNC_UInt32 buffer_len,
TNC_BufferReference buffer,
TNC_UInt32 *value_len,
char *value)
{
*value_len = 1 + strlen(value);
if (buffer && buffer_len >= *value_len)
{
snprintf(buffer, buffer_len, "%s", value);
return TNC_RESULT_SUCCESS;
}
else
{
return TNC_RESULT_INVALID_PARAMETER;
}
}
METHOD(tnccs_manager_t, get_attribute, TNC_Result,
private_tnc_tnccs_manager_t *this, bool is_imc,
TNC_UInt32 imcv_id,
TNC_ConnectionID id,
TNC_AttributeID attribute_id,
TNC_UInt32 buffer_len,
TNC_BufferReference buffer,
TNC_UInt32 *value_len)
{
enumerator_t *enumerator;
tnccs_connection_entry_t *entry;
bool attribute_match = FALSE, entry_found = FALSE;
if (is_imc)
{
switch (attribute_id)
{
/* these attributes are unsupported */
case TNC_ATTRIBUTEID_SOHR:
case TNC_ATTRIBUTEID_SSOHR:
return TNC_RESULT_INVALID_PARAMETER;
/* these attributes are supported */
case TNC_ATTRIBUTEID_PRIMARY_IMC_ID:
attribute_match = TRUE;
break;
/* these attributes are yet to be matched */
default:
break;
}
}
else
{
switch (attribute_id)
{
/* these attributes are unsupported or invalid */
case TNC_ATTRIBUTEID_REASON_STRING:
case TNC_ATTRIBUTEID_REASON_LANGUAGE:
case TNC_ATTRIBUTEID_SOH:
case TNC_ATTRIBUTEID_SSOH:
return TNC_RESULT_INVALID_PARAMETER;
/* these attributes are supported */
case TNC_ATTRIBUTEID_PRIMARY_IMV_ID:
attribute_match = TRUE;
break;
/* these attributes are yet to be matched */
default:
break;
}
}
if (!attribute_match)
{
switch (attribute_id)
{
/* these attributes are supported */
case TNC_ATTRIBUTEID_PREFERRED_LANGUAGE:
case TNC_ATTRIBUTEID_MAX_ROUND_TRIPS:
case TNC_ATTRIBUTEID_MAX_MESSAGE_SIZE:
case TNC_ATTRIBUTEID_HAS_LONG_TYPES:
case TNC_ATTRIBUTEID_HAS_EXCLUSIVE:
case TNC_ATTRIBUTEID_HAS_SOH:
case TNC_ATTRIBUTEID_IFTNCCS_PROTOCOL:
case TNC_ATTRIBUTEID_IFTNCCS_VERSION:
case TNC_ATTRIBUTEID_IFT_PROTOCOL:
case TNC_ATTRIBUTEID_IFT_VERSION:
break;
/* these attributes are unsupported or unknown */
case TNC_ATTRIBUTEID_DHPN:
case TNC_ATTRIBUTEID_TLS_UNIQUE:
default:
return TNC_RESULT_INVALID_PARAMETER;
}
}
/* attributes specific to the TNCC or TNCS are unsupported */
if (id == TNC_CONNECTIONID_ANY)
{
return TNC_RESULT_INVALID_PARAMETER;
}
this->connection_lock->read_lock(this->connection_lock);
enumerator = this->connections->create_enumerator(this->connections);
while (enumerator->enumerate(enumerator, &entry))
{
if (id == entry->id)
{
entry_found = TRUE;
break;
}
}
enumerator->destroy(enumerator);
this->connection_lock->unlock(this->connection_lock);
if (!entry_found)
{
return TNC_RESULT_INVALID_PARAMETER;
}
switch (attribute_id)
{
case TNC_ATTRIBUTEID_PREFERRED_LANGUAGE:
{
recommendations_t *recs;
chunk_t pref_lang;
recs = entry->recs;
if (!recs)
{
return TNC_RESULT_INVALID_PARAMETER;
}
pref_lang = recs->get_preferred_language(recs);
if (pref_lang.len == 0)
{
return TNC_RESULT_INVALID_PARAMETER;
}
*value_len = pref_lang.len;
if (buffer && buffer_len >= pref_lang.len)
{
memcpy(buffer, pref_lang.ptr, pref_lang.len);
}
return TNC_RESULT_SUCCESS;
}
case TNC_ATTRIBUTEID_MAX_ROUND_TRIPS:
return uint_attribute(buffer_len, buffer, value_len,
0xffffffff);
case TNC_ATTRIBUTEID_MAX_MESSAGE_SIZE:
return uint_attribute(buffer_len, buffer, value_len,
entry->max_msg_len);
case TNC_ATTRIBUTEID_HAS_LONG_TYPES:
case TNC_ATTRIBUTEID_HAS_EXCLUSIVE:
return bool_attribute(buffer_len, buffer, value_len,
entry->type == TNCCS_2_0);
case TNC_ATTRIBUTEID_HAS_SOH:
return bool_attribute(buffer_len, buffer, value_len,
entry->type == TNCCS_SOH);
case TNC_ATTRIBUTEID_IFTNCCS_PROTOCOL:
{
char *protocol;
switch (entry->type)
{
case TNCCS_1_1:
case TNCCS_2_0:
protocol = "IF-TNCCS";
break;
case TNCCS_SOH:
protocol = "IF-TNCCS-SOH";
break;
default:
return TNC_RESULT_INVALID_PARAMETER;
}
return str_attribute(buffer_len, buffer, value_len, protocol);
}
case TNC_ATTRIBUTEID_IFTNCCS_VERSION:
{
char *version;
switch (entry->type)
{
case TNCCS_1_1:
version = "1.1";
break;
case TNCCS_2_0:
version = "2.0";
break;
case TNCCS_SOH:
version = "1.0";
break;
default:
return TNC_RESULT_INVALID_PARAMETER;
}
return str_attribute(buffer_len, buffer, value_len, version);
}
case TNC_ATTRIBUTEID_IFT_PROTOCOL:
return str_attribute(buffer_len, buffer, value_len,
"IF-T for Tunneled EAP");
case TNC_ATTRIBUTEID_IFT_VERSION:
return str_attribute(buffer_len, buffer, value_len, "1.1");
default:
return TNC_RESULT_INVALID_PARAMETER;
}
}
METHOD(tnccs_manager_t, set_attribute, TNC_Result,
private_tnc_tnccs_manager_t *this, bool is_imc,
TNC_UInt32 imcv_id,
TNC_ConnectionID id,
TNC_AttributeID attribute_id,
TNC_UInt32 buffer_len,
TNC_BufferReference buffer)
{
enumerator_t *enumerator;
tnccs_connection_entry_t *entry;
recommendations_t *recs = NULL;
if (is_imc || id == TNC_CONNECTIONID_ANY ||
(attribute_id != TNC_ATTRIBUTEID_REASON_STRING &&
attribute_id != TNC_ATTRIBUTEID_REASON_LANGUAGE))
{
return TNC_RESULT_INVALID_PARAMETER;
}
this->connection_lock->read_lock(this->connection_lock);
enumerator = this->connections->create_enumerator(this->connections);
while (enumerator->enumerate(enumerator, &entry))
{
if (id == entry->id)
{
recs = entry->recs;
break;
}
}
enumerator->destroy(enumerator);
this->connection_lock->unlock(this->connection_lock);
if (recs)
{
chunk_t attribute = { buffer, buffer_len };
if (attribute_id == TNC_ATTRIBUTEID_REASON_STRING)
{
return recs->set_reason_string(recs, imcv_id, attribute);
}
else
{
return recs->set_reason_language(recs, imcv_id, attribute);
}
}
return TNC_RESULT_INVALID_PARAMETER;
}
METHOD(tnccs_manager_t, destroy, void,
private_tnc_tnccs_manager_t *this)
{
this->protocols->destroy_function(this->protocols, free);
this->protocol_lock->destroy(this->protocol_lock);
this->connections->destroy_function(this->connections, free);
this->connection_lock->destroy(this->connection_lock);
free(this);
}
/*
* See header
*/
tnccs_manager_t *tnc_tnccs_manager_create()
{
private_tnc_tnccs_manager_t *this;
INIT(this,
.public = {
.add_method = _add_method,
.remove_method = _remove_method,
.create_instance = _create_instance,
.create_connection = _create_connection,
.remove_connection = _remove_connection,
.request_handshake_retry = _request_handshake_retry,
.send_message = _send_message,
.provide_recommendation = _provide_recommendation,
.get_attribute = _get_attribute,
.set_attribute = _set_attribute,
.destroy = _destroy,
},
.protocols = linked_list_create(),
.connections = linked_list_create(),
.protocol_lock = rwlock_create(RWLOCK_TYPE_DEFAULT),
.connection_lock = rwlock_create(RWLOCK_TYPE_DEFAULT),
);
return &this->public;
}
|