Implemented IF-M segmentation
[strongswan.git] / src / libimcv / imc / imc_msg.c
1 /*
2 * Copyright (C) 2012-2014 Andreas Steffen
3 * HSR Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include "imc_msg.h"
17
18 #include "ietf/ietf_attr.h"
19 #include "ietf/ietf_attr_assess_result.h"
20 #include "ietf/ietf_attr_remediation_instr.h"
21
22 #include <tncif_names.h>
23 #include <tncif_pa_subtypes.h>
24
25 #include <tcg/seg/tcg_seg_attr_max_size.h>
26 #include <tcg/seg/tcg_seg_attr_seg_env.h>
27 #include <tcg/seg/tcg_seg_attr_next_seg.h>
28
29 #include <pen/pen.h>
30 #include <collections/linked_list.h>
31 #include <utils/debug.h>
32
33 typedef struct private_imc_msg_t private_imc_msg_t;
34
35 /**
36 * Private data of a imc_msg_t object.
37 *
38 */
39 struct private_imc_msg_t {
40
41 /**
42 * Public imc_msg_t interface.
43 */
44 imc_msg_t public;
45
46 /**
47 * Connection ID
48 */
49 TNC_ConnectionID connection_id;
50
51 /**
52 * source ID
53 */
54 TNC_UInt32 src_id;
55
56 /**
57 * destination ID
58 */
59 TNC_UInt32 dst_id;
60
61 /**
62 * PA-TNC message type
63 */
64 pen_type_t msg_type;
65
66 /**
67 * List of PA-TNC attributes to be sent
68 */
69 linked_list_t *attr_list;
70
71 /**
72 * PA-TNC message
73 */
74 pa_tnc_msg_t *pa_msg;
75
76 /**
77 * Assigned IMC agent
78 */
79 imc_agent_t *agent;
80
81 /**
82 * Assigned IMC state
83 */
84 imc_state_t *state;
85 };
86
87 METHOD(imc_msg_t, get_src_id, TNC_UInt32,
88 private_imc_msg_t *this)
89 {
90 return this->src_id;
91 }
92
93 METHOD(imc_msg_t, get_dst_id, TNC_UInt32,
94 private_imc_msg_t *this)
95 {
96 return this->dst_id;
97 }
98
99 METHOD(imc_msg_t, get_msg_type, pen_type_t,
100 private_imc_msg_t *this)
101 {
102 return this->msg_type;
103 }
104
105 METHOD(imc_msg_t, send_, TNC_Result,
106 private_imc_msg_t *this, bool excl)
107 {
108 pa_tnc_msg_t *pa_tnc_msg;
109 pa_tnc_attr_t *attr;
110 TNC_UInt32 msg_flags;
111 TNC_MessageType msg_type;
112 bool attr_added, oversize;
113 chunk_t msg;
114 seg_contract_t *contract;
115 seg_contract_manager_t *contracts;
116 enumerator_t *enumerator;
117 TNC_Result result = TNC_RESULT_SUCCESS;
118
119 /* Get IF-M segmentation contract for this subtype if any */
120 contracts = this->state->get_contracts(this->state);
121 contract = contracts->get_contract(contracts, this->msg_type, FALSE);
122
123 while (this->attr_list->get_count(this->attr_list))
124 {
125 pa_tnc_msg = pa_tnc_msg_create(this->state->get_max_msg_len(this->state));
126 attr_added = FALSE;
127
128 enumerator = this->attr_list->create_enumerator(this->attr_list);
129 while (enumerator->enumerate(enumerator, &attr))
130 {
131 if (contract && contract->check_size(contract, attr, &oversize))
132 {
133 if (oversize)
134 {
135 /* TODO generate SWID error msg */
136 }
137 else
138 {
139 attr = contract->first_segment(contract, attr);
140 }
141 }
142 if (pa_tnc_msg->add_attribute(pa_tnc_msg, attr))
143 {
144 attr_added = TRUE;
145 }
146 else
147 {
148 if (attr_added)
149 {
150 break;
151 }
152 else
153 {
154 DBG1(DBG_IMC, "PA-TNC attribute too large to send, deleted");
155 attr->destroy(attr);
156 }
157 }
158 this->attr_list->remove_at(this->attr_list, enumerator);
159 }
160 enumerator->destroy(enumerator);
161
162 /* build and send the PA-TNC message via the IF-IMC interface */
163 if (!pa_tnc_msg->build(pa_tnc_msg))
164 {
165 pa_tnc_msg->destroy(pa_tnc_msg);
166 return TNC_RESULT_FATAL;
167 }
168 msg = pa_tnc_msg->get_encoding(pa_tnc_msg);
169 DBG3(DBG_IMC, "created PA-TNC message: %B", &msg);
170
171 if (this->state->has_long(this->state) && this->agent->send_message_long)
172 {
173 excl = excl && this->state->has_excl(this->state) &&
174 this->dst_id != TNC_IMVID_ANY;
175 msg_flags = excl ? TNC_MESSAGE_FLAGS_EXCLUSIVE : 0;
176 result = this->agent->send_message_long(this->src_id,
177 this->connection_id, msg_flags, msg.ptr, msg.len,
178 this->msg_type.vendor_id, this->msg_type.type,
179 this->dst_id);
180 }
181 else if (this->agent->send_message)
182 {
183 msg_type = (this->msg_type.vendor_id << 8) |
184 (this->msg_type.type & 0x000000ff);
185 result = this->agent->send_message(this->src_id, this->connection_id,
186 msg.ptr, msg.len, msg_type);
187 }
188
189 pa_tnc_msg->destroy(pa_tnc_msg);
190
191 if (result != TNC_RESULT_SUCCESS)
192 {
193 break;
194 }
195 }
196 return result;
197 }
198
199 /**
200 * Print a clearly visible assessment header to the log
201 */
202 static void print_assessment_header(const char *name, TNC_UInt32 dst_id,
203 TNC_UInt32 src_id, bool *first)
204 {
205 if (*first)
206 {
207 if (src_id == TNC_IMCID_ANY)
208 {
209 DBG1(DBG_IMC, "***** assessment of IMC %u \"%s\" *****",
210 dst_id, name);
211 }
212 else
213 {
214 DBG1(DBG_IMC, "***** assessment of IMC %u \"%s\" from IMV %u *****",
215 dst_id, name, src_id);
216 }
217 *first = FALSE;
218 }
219 }
220
221 /**
222 * Print a clearly visible assessment trailer to the log
223 */
224 static void print_assessment_trailer(bool first)
225 {
226 if (!first)
227 {
228 DBG1(DBG_IMC, "***** end of assessment *****");
229 }
230 }
231
232 METHOD(imc_msg_t, receive, TNC_Result,
233 private_imc_msg_t *this, imc_msg_t *out_msg, bool *fatal_error)
234 {
235 linked_list_t *non_fatal_types;
236 TNC_UInt32 target_imc_id;
237 enumerator_t *enumerator;
238 pa_tnc_attr_t *attr;
239 pen_type_t attr_type;
240 chunk_t msg;
241 bool first = TRUE;
242
243 if (this->state->has_long(this->state))
244 {
245 if (this->dst_id != TNC_IMCID_ANY)
246 {
247 DBG2(DBG_IMC, "IMC %u \"%s\" received message for Connection ID %u "
248 "from IMV %u to IMC %u",
249 this->agent->get_id(this->agent),
250 this->agent->get_name(this->agent),
251 this->connection_id, this->src_id, this->dst_id);
252 }
253 else
254 {
255 DBG2(DBG_IMC, "IMC %u \"%s\" received message for Connection ID %u "
256 "from IMV %u", this->agent->get_id(this->agent),
257 this->agent->get_name(this->agent),
258 this->connection_id, this->src_id);
259 }
260 }
261 else
262 {
263 DBG2(DBG_IMC, "IMC %u \"%s\" received message for Connection ID %u",
264 this->agent->get_id(this->agent),
265 this->agent->get_name(this->agent),
266 this->connection_id);
267 }
268 msg = this->pa_msg->get_encoding(this->pa_msg);
269 DBG3(DBG_IMC, "%B", &msg);
270
271 switch (this->pa_msg->process(this->pa_msg))
272 {
273 case SUCCESS:
274 break;
275 case VERIFY_ERROR:
276 {
277 /* extract and copy by reference all error attributes */
278 enumerator = this->pa_msg->create_error_enumerator(this->pa_msg);
279 while (enumerator->enumerate(enumerator, &attr))
280 {
281 out_msg->add_attribute(out_msg, attr->get_ref(attr));
282 }
283 enumerator->destroy(enumerator);
284 return TNC_RESULT_SUCCESS;
285 }
286 case FAILED:
287 default:
288 return TNC_RESULT_FATAL;
289 }
290
291 /* process any IF-M segmentation contracts */
292 enumerator = this->pa_msg->create_attribute_enumerator(this->pa_msg);
293 while (enumerator->enumerate(enumerator, &attr))
294 {
295 uint32_t max_attr_size, max_seg_size, my_max_attr_size, my_max_seg_size;
296 seg_contract_t *contract;
297 seg_contract_manager_t *contracts;
298 char buf[BUF_LEN];
299 pen_type_t type;
300
301 type = attr->get_type(attr);
302
303 contracts = this->state->get_contracts(this->state);
304
305 if (type.vendor_id != PEN_TCG)
306 {
307 continue;
308 }
309
310 switch (type.type)
311 {
312 case TCG_SEG_MAX_ATTR_SIZE_REQ:
313 {
314 tcg_seg_attr_max_size_t *attr_cast;
315
316 attr_cast = (tcg_seg_attr_max_size_t*)attr;
317 attr_cast->get_attr_size(attr_cast, &max_attr_size,
318 &max_seg_size);
319 contract = contracts->get_contract(contracts, this->msg_type,
320 FALSE);
321 if (contract)
322 {
323 contract->set_max_size(contract, max_attr_size,
324 max_seg_size);
325 }
326 else
327 {
328 contract = seg_contract_create(this->msg_type, max_attr_size,
329 max_seg_size, FALSE, this->src_id, TRUE);
330 contracts->add_contract(contracts, contract);
331 }
332 contract->get_info_string(contract, buf, BUF_LEN, TRUE);
333 DBG2(DBG_IMC, "%s", buf);
334
335 /* Determine maximum PA-TNC attribute segment size */
336 my_max_seg_size = this->state->get_max_msg_len(this->state)
337 - PA_TNC_HEADER_SIZE
338 - PA_TNC_ATTR_HEADER_SIZE
339 - TCG_SEG_ATTR_SEG_ENV_HEADER
340 - PA_TNC_ATTR_HEADER_SIZE
341 - TCG_SEG_ATTR_MAX_SIZE_SIZE;
342
343 /* If segmentation is possible select lower segment size */
344 if (max_seg_size != SEG_CONTRACT_NO_FRAGMENTATION &&
345 max_seg_size > my_max_seg_size)
346 {
347 max_seg_size = my_max_seg_size;
348 contract->set_max_size(contract, max_attr_size,
349 max_seg_size);
350 DBG2(DBG_IMC, " lowered maximum segment size to %u bytes",
351 max_seg_size);
352 }
353
354 /* Add Maximum Attribute Size Response attribute */
355 attr = tcg_seg_attr_max_size_create(max_attr_size,
356 max_seg_size, FALSE);
357 out_msg->add_attribute(out_msg, attr);
358 break;
359 }
360 case TCG_SEG_MAX_ATTR_SIZE_RESP:
361 {
362 tcg_seg_attr_max_size_t *attr_cast;
363
364 attr_cast = (tcg_seg_attr_max_size_t*)attr;
365 attr_cast->get_attr_size(attr_cast, &max_attr_size,
366 &max_seg_size);
367 contract = contracts->get_contract(contracts, this->msg_type,
368 TRUE);
369 if (contract)
370 {
371 contract->get_max_size(contract, &my_max_attr_size,
372 &my_max_seg_size);
373 if (my_max_seg_size != SEG_CONTRACT_NO_FRAGMENTATION &&
374 my_max_seg_size > max_seg_size)
375 {
376 my_max_seg_size = max_seg_size;
377 contract->set_max_size(contract, my_max_attr_size,
378 my_max_seg_size);
379 }
380 contract->get_info_string(contract, buf, BUF_LEN, FALSE);
381 DBG2(DBG_IMC, "%s", buf);
382 }
383 else
384 {
385 /* TODO no request pending */
386 DBG1(DBG_IMC, "no contract for this PA message type found");
387 }
388 break;
389 }
390 case TCG_SEG_ATTR_SEG_ENV:
391 {
392 tcg_seg_attr_seg_env_t *seg_env_attr;
393 pa_tnc_attr_t *error;
394 uint32_t base_attr_id;
395 bool more;
396
397 seg_env_attr = (tcg_seg_attr_seg_env_t*)attr;
398 base_attr_id = seg_env_attr->get_base_attr_id(seg_env_attr);
399
400 contract = contracts->get_contract(contracts, this->msg_type,
401 TRUE);
402 if (!contract)
403 {
404 DBG2(DBG_IMC, "no contract for received attribute segment "
405 "with base attribute ID %u", base_attr_id);
406 continue;
407 }
408 attr = contract->add_segment(contract, attr, &error, &more);
409 if (error)
410 {
411 out_msg->add_attribute(out_msg, error);
412 }
413 if (attr)
414 {
415 this->pa_msg->add_attribute(this->pa_msg, attr);
416 }
417 if (more)
418 {
419 /* Send Next Segment Request */
420 attr = tcg_seg_attr_next_seg_create(base_attr_id, FALSE);
421 out_msg->add_attribute(out_msg, attr);
422 }
423 break;
424 }
425 case TCG_SEG_NEXT_SEG_REQ:
426 {
427 tcg_seg_attr_next_seg_t *attr_cast;
428 uint32_t base_attr_id;
429
430 attr_cast = (tcg_seg_attr_next_seg_t*)attr;
431 base_attr_id = attr_cast->get_base_attr_id(attr_cast);
432
433 contract = contracts->get_contract(contracts, this->msg_type,
434 FALSE);
435 if (!contract)
436 {
437 /* TODO no contract - generate error message */
438 DBG2(DBG_IMC, "no contract for received next segment "
439 "request with base attribute ID %u", base_attr_id);
440 continue;
441 }
442 attr = contract->next_segment(contract, base_attr_id);
443 if (attr)
444 {
445 out_msg->add_attribute(out_msg, attr);
446 }
447 else
448 {
449 /* TODO no more segments - generate error message */
450 DBG1(DBG_IMC, "no more segments found for "
451 "base attribute ID %u", base_attr_id);
452 }
453 break;
454 }
455 default:
456 break;
457 }
458 }
459 enumerator->destroy(enumerator);
460
461 /* determine target IMC ID */
462 target_imc_id = (this->dst_id != TNC_IMCID_ANY) ?
463 this->dst_id : this->agent->get_id(this->agent);
464
465 /* preprocess any received IETF standard error attributes */
466 non_fatal_types = this->agent->get_non_fatal_attr_types(this->agent);
467 *fatal_error = this->pa_msg->process_ietf_std_errors(this->pa_msg,
468 non_fatal_types);
469
470 /* preprocess any received IETF assessment result attribute */
471 enumerator = this->pa_msg->create_attribute_enumerator(this->pa_msg);
472 while (enumerator->enumerate(enumerator, &attr))
473 {
474 attr_type = attr->get_type(attr);
475
476 if (attr_type.vendor_id != PEN_IETF)
477 {
478 continue;
479 }
480 if (attr_type.type == IETF_ATTR_ASSESSMENT_RESULT)
481 {
482 ietf_attr_assess_result_t *attr_cast;
483 TNC_IMV_Evaluation_Result res;
484
485 attr_cast = (ietf_attr_assess_result_t*)attr;
486 res = attr_cast->get_result(attr_cast);
487 this->state->set_result(this->state, target_imc_id, res);
488
489 print_assessment_header(this->agent->get_name(this->agent),
490 target_imc_id, this->src_id, &first);
491 DBG1(DBG_IMC, "assessment result is '%N'",
492 TNC_IMV_Evaluation_Result_names, res);
493 }
494 else if (attr_type.type == IETF_ATTR_REMEDIATION_INSTRUCTIONS)
495 {
496 ietf_attr_remediation_instr_t *attr_cast;
497 pen_type_t parameters_type;
498 chunk_t parameters, string, lang_code;
499
500 attr_cast = (ietf_attr_remediation_instr_t*)attr;
501 parameters_type = attr_cast->get_parameters_type(attr_cast);
502 parameters = attr_cast->get_parameters(attr_cast);
503
504 print_assessment_header(this->agent->get_name(this->agent),
505 target_imc_id, this->src_id, &first);
506 if (parameters_type.vendor_id == PEN_IETF)
507 {
508 switch (parameters_type.type)
509 {
510 case IETF_REMEDIATION_PARAMETERS_URI:
511 DBG1(DBG_IMC, "remediation uri: %.*s",
512 parameters.len, parameters.ptr);
513 break;
514 case IETF_REMEDIATION_PARAMETERS_STRING:
515 string = attr_cast->get_string(attr_cast, &lang_code);
516 DBG1(DBG_IMC, "remediation string: [%.*s]\n%.*s",
517 lang_code.len, lang_code.ptr,
518 string.len, string.ptr);
519 break;
520 default:
521 DBG1(DBG_IMC, "remediation parameters: %B", &parameters);
522 }
523 }
524 else
525 {
526 DBG1(DBG_IMC, "remediation parameters: %B", &parameters);
527 }
528 }
529 }
530 enumerator->destroy(enumerator);
531
532 print_assessment_trailer(first);
533
534 return TNC_RESULT_SUCCESS;
535 }
536
537 METHOD(imc_msg_t, add_attribute, void,
538 private_imc_msg_t *this, pa_tnc_attr_t *attr)
539 {
540 this->attr_list->insert_last(this->attr_list, attr);
541 }
542
543 METHOD(imc_msg_t, create_attribute_enumerator, enumerator_t*,
544 private_imc_msg_t *this)
545 {
546 return this->pa_msg->create_attribute_enumerator(this->pa_msg);
547 }
548
549 METHOD(imc_msg_t, get_encoding, chunk_t,
550 private_imc_msg_t *this)
551 {
552 if (this->pa_msg)
553 {
554 return this->pa_msg->get_encoding(this->pa_msg);
555 }
556 return chunk_empty;
557 }
558
559 METHOD(imc_msg_t, destroy, void,
560 private_imc_msg_t *this)
561 {
562 this->attr_list->destroy_offset(this->attr_list,
563 offsetof(pa_tnc_attr_t, destroy));
564 DESTROY_IF(this->pa_msg);
565 free(this);
566 }
567
568 /**
569 * See header
570 */
571 imc_msg_t *imc_msg_create(imc_agent_t *agent, imc_state_t *state,
572 TNC_ConnectionID connection_id,
573 TNC_UInt32 src_id, TNC_UInt32 dst_id,
574 pen_type_t msg_type)
575 {
576 private_imc_msg_t *this;
577
578 INIT(this,
579 .public = {
580 .get_src_id = _get_src_id,
581 .get_dst_id = _get_dst_id,
582 .get_msg_type = _get_msg_type,
583 .send = _send_,
584 .receive = _receive,
585 .add_attribute = _add_attribute,
586 .create_attribute_enumerator = _create_attribute_enumerator,
587 .get_encoding = _get_encoding,
588 .destroy = _destroy,
589 },
590 .connection_id = connection_id,
591 .src_id = src_id,
592 .dst_id = dst_id,
593 .msg_type = msg_type,
594 .attr_list = linked_list_create(),
595 .agent = agent,
596 .state = state,
597 );
598
599 return &this->public;
600 }
601
602 /**
603 * See header
604 */
605 imc_msg_t* imc_msg_create_as_reply(imc_msg_t *msg)
606 {
607 private_imc_msg_t *in;
608 TNC_UInt32 src_id;
609
610 in = (private_imc_msg_t*)msg;
611 src_id = (in->dst_id != TNC_IMCID_ANY) ?
612 in->dst_id : in->agent->get_id(in->agent);
613
614 return imc_msg_create(in->agent, in->state, in->connection_id, src_id,
615 in->src_id, in->msg_type);
616 }
617
618 /**
619 * See header
620 */
621 imc_msg_t *imc_msg_create_from_data(imc_agent_t *agent, imc_state_t *state,
622 TNC_ConnectionID connection_id,
623 TNC_MessageType msg_type,
624 chunk_t msg)
625 {
626 TNC_VendorID msg_vid;
627 TNC_MessageSubtype msg_subtype;
628
629 msg_vid = msg_type >> 8;
630 msg_subtype = msg_type & TNC_SUBTYPE_ANY;
631
632 return imc_msg_create_from_long_data(agent, state, connection_id,
633 TNC_IMVID_ANY, agent->get_id(agent),
634 msg_vid, msg_subtype, msg);
635 }
636
637 /**
638 * See header
639 */
640 imc_msg_t *imc_msg_create_from_long_data(imc_agent_t *agent, imc_state_t *state,
641 TNC_ConnectionID connection_id,
642 TNC_UInt32 src_id,
643 TNC_UInt32 dst_id,
644 TNC_VendorID msg_vid,
645 TNC_MessageSubtype msg_subtype,
646 chunk_t msg)
647 {
648 private_imc_msg_t *this;
649
650 this = (private_imc_msg_t*)imc_msg_create(agent, state,
651 connection_id, src_id, dst_id,
652 pen_type_create(msg_vid, msg_subtype));
653 this->pa_msg = pa_tnc_msg_create_from_data(msg);
654
655 return &this->public;
656 }