4 * @brief Class message_t. Object of this type represents an IKEv2-Message.
9 * Copyright (C) 2005 Jan Hutter, Martin Willi
10 * Hochschule fuer Technik Rapperswil
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
29 #include <ike_sa_id.h>
30 #include <encoding/generator.h>
31 #include <encoding/parser.h>
32 #include <utils/linked_list.h>
33 #include <utils/allocator.h>
34 #include <utils/logger_manager.h>
35 #include <encoding/payloads/encodings.h>
36 #include <encoding/payloads/payload.h>
40 * Supported payload entry used in message_rule_t
43 typedef struct supported_payload_entry_s supported_payload_entry_t
;
45 struct supported_payload_entry_s
{
49 payload_type_t payload_type
;
52 * Minimal occurence of this payload
57 * Max occurence of this payload
63 * Message Rule used to find out which payloads are supported by each message type
66 typedef struct message_rule_s message_rule_t
;
68 struct message_rule_s
{
72 exchange_type_t exchange_type
;
75 * Is message a request or response
79 * Number of supported payloads
81 size_t supported_payloads_count
;
83 * Pointer to first supported payload entry
85 supported_payload_entry_t
*supported_payloads
;
89 * message rule for ike_sa_init from initiator
91 static supported_payload_entry_t supported_ike_sa_init_i_payloads
[] =
93 {SECURITY_ASSOCIATION
,1,1},
99 * message rule for ike_sa_init from responder
101 static supported_payload_entry_t supported_ike_sa_init_r_payloads
[] =
103 {SECURITY_ASSOCIATION
,1,1},
110 * message rules, defines allowed payloads
112 static message_rule_t message_rules
[] = {
113 {IKE_SA_INIT
,TRUE
,(sizeof(supported_ike_sa_init_i_payloads
)/sizeof(supported_payload_entry_t
)),supported_ike_sa_init_i_payloads
},
114 {IKE_SA_INIT
,FALSE
,(sizeof(supported_ike_sa_init_r_payloads
)/sizeof(supported_payload_entry_t
)),supported_ike_sa_init_r_payloads
}
118 * Entry for a payload in the internal used linked list
121 typedef struct payload_entry_s payload_entry_t
;
123 struct payload_entry_s
{
127 payload_type_t payload_type
;
129 * Data struct holding the data of given payload
136 * Private data of an message_t object
138 typedef struct private_message_s private_message_t
;
140 struct private_message_s
{
143 * Public part of a message_t object
149 * Minor version of message
151 u_int8_t major_version
;
154 * Major version of message
156 u_int8_t minor_version
;
159 * First Payload in message
161 payload_type_t first_payload
;
164 * Assigned exchange type
166 exchange_type_t exchange_type
;
170 * TRUE if message is request.
171 * FALSE if message is reply.
176 * Message ID of this message
178 u_int32_t message_id
;
181 * ID of assigned IKE_SA
183 ike_sa_id_t
*ike_sa_id
;
186 * Assigned UDP packet.
188 * Stores incoming packet or last generated one.
193 * Linked List where payload data are stored in
195 linked_list_t
*payloads
;
198 * Assigned parser to parse Header and Body of this message
203 * logger for this message
208 * Gets a list of supported payloads of this message type
210 * @param this calling object
211 * @param[out] supported_payloads first entry of supported payloads
212 * @param[out] supported_payloads_count number of supported payload entries
215 * NOT_FOUND if no supported payload definition could be found
217 status_t (*get_supported_payloads
) (private_message_t
*this, supported_payload_entry_t
**supported_payloads
,size_t *supported_payloads_count
);
222 * Implements private_message_t's get_supported_payloads function.
223 * See #private_message_t.get_supported_payloads.
225 status_t
get_supported_payloads (private_message_t
*this, supported_payload_entry_t
**supported_payloads
,size_t *supported_payloads_count
)
228 exchange_type_t exchange_type
= this->public.get_exchange_type(&(this->public));
229 bool is_request
= this->public.get_request(&(this->public));
232 for (i
= 0; i
< (sizeof(message_rules
) / sizeof(message_rule_t
)); i
++)
234 if ((exchange_type
== message_rules
[i
].exchange_type
) &&
235 (is_request
== message_rules
[i
].is_request
))
237 /* found rule for given exchange_type*/
238 *supported_payloads
= message_rules
[i
].supported_payloads
;
239 *supported_payloads_count
= message_rules
[i
].supported_payloads_count
;
246 *supported_payloads
= NULL
;
247 *supported_payloads_count
= 0;
252 * Implements message_t's set_ike_sa_id function.
253 * See #message_s.set_ike_sa_id.
255 static status_t
set_ike_sa_id (private_message_t
*this,ike_sa_id_t
*ike_sa_id
)
258 status
= ike_sa_id
->clone(ike_sa_id
,&(this->ike_sa_id
));
263 * Implements message_t's get_ike_sa_id function.
264 * See #message_s.get_ike_sa_id.
266 static status_t
get_ike_sa_id (private_message_t
*this,ike_sa_id_t
**ike_sa_id
)
269 if (this->ike_sa_id
== NULL
)
273 status
= this->ike_sa_id
->clone(this->ike_sa_id
,ike_sa_id
);
279 * Implements message_t's set_message_id function.
280 * See #message_s.set_message_id.
282 static status_t
set_message_id (private_message_t
*this,u_int32_t message_id
)
284 this->message_id
= message_id
;
290 * Implements message_t's set_message_id function.
291 * See #message_s.set_message_id.
293 static u_int32_t
get_message_id (private_message_t
*this)
295 return this->message_id
;
299 * Implements message_t's get_responder_spi function.
300 * See #message_s.get_responder_spi.
302 static u_int64_t
get_responder_spi (private_message_t
*this)
304 return (this->ike_sa_id
->get_responder_spi(this->ike_sa_id
));
308 * Implements message_t's set_major_version function.
309 * See #message_s.set_major_version.
311 static status_t
set_major_version (private_message_t
*this,u_int8_t major_version
)
313 this->major_version
= major_version
;
319 * Implements message_t's get_major_version function.
320 * See #message_s.get_major_version.
322 static u_int8_t
get_major_version (private_message_t
*this)
324 return this->major_version
;
328 * Implements message_t's set_minor_version function.
329 * See #message_s.set_minor_version.
331 static status_t
set_minor_version (private_message_t
*this,u_int8_t minor_version
)
333 this->minor_version
= minor_version
;
339 * Implements message_t's get_minor_version function.
340 * See #message_s.get_minor_version.
342 static u_int8_t
get_minor_version (private_message_t
*this)
344 return this->minor_version
;
348 * Implements message_t's set_exchange_type function.
349 * See #message_s.set_exchange_type.
351 static status_t
set_exchange_type (private_message_t
*this,exchange_type_t exchange_type
)
353 this->exchange_type
= exchange_type
;
359 * Implements message_t's get_exchange_type function.
360 * See #message_s.get_exchange_type.
362 static exchange_type_t
get_exchange_type (private_message_t
*this)
364 return this->exchange_type
;
369 * Implements message_t's set_request function.
370 * See #message_s.set_request.
372 static status_t
set_request (private_message_t
*this,bool request
)
374 this->is_request
= request
;
379 * Implements message_t's get_request function.
380 * See #message_s.get_request.
382 static exchange_type_t
get_request (private_message_t
*this)
384 return this->is_request
;
387 static status_t
add_payload(private_message_t
*this, payload_t
*payload
)
389 payload_t
*last_payload
;
390 if ((this->payloads
->get_count(this->payloads
) > 0) &&
391 (this->payloads
->get_last(this->payloads
,(void **) &last_payload
) != SUCCESS
))
396 if (this->payloads
->insert_last(this->payloads
, payload
) != SUCCESS
)
400 if (this->payloads
->get_count(this->payloads
) == 1)
402 this->first_payload
= payload
->get_type(payload
);
406 last_payload
->set_next_type(last_payload
,payload
->get_type(payload
));
409 this->logger
->log(this->logger
, CONTROL
|MORE
, "added payload of type %s to message",
410 mapping_find(payload_type_m
, payload
->get_type(payload
)));
415 static status_t
set_source(private_message_t
*this, host_t
*host
)
417 if (this->packet
->source
!= NULL
)
419 this->packet
->source
->destroy(this->packet
->source
);
421 this->packet
->source
= host
;
425 static status_t
set_destination(private_message_t
*this, host_t
*host
)
427 if (this->packet
->destination
!= NULL
)
429 this->packet
->destination
->destroy(this->packet
->destination
);
431 this->packet
->destination
= host
;
435 static status_t
get_source(private_message_t
*this, host_t
**host
)
437 *host
= this->packet
->source
;
441 static status_t
get_destination(private_message_t
*this, host_t
**host
)
443 *host
= this->packet
->destination
;
448 static status_t
get_payload_iterator(private_message_t
*this, linked_list_iterator_t
**iterator
)
450 return this->payloads
->create_iterator(this->payloads
, iterator
, TRUE
);
455 * Implements message_t's generate function.
456 * See #message_s.generate.
458 static status_t
generate(private_message_t
*this, packet_t
**packet
)
460 generator_t
*generator
;
461 ike_header_t
*ike_header
;
462 payload_t
*payload
, *next_payload
;
463 linked_list_iterator_t
*iterator
;
467 this->logger
->log(this->logger
, CONTROL
, "generating message, contains %d payloads",
468 this->payloads
->get_count(this->payloads
));
470 if (this->exchange_type
== EXCHANGE_TYPE_UNDEFINED
)
472 this->logger
->log(this->logger
, ERROR
, "exchange type is not defined");
473 return INVALID_STATE
;
476 if (this->packet
->source
== NULL
||
477 this->packet
->destination
== NULL
)
479 this->logger
->log(this->logger
, ERROR
, "source/destination not defined");
480 return INVALID_STATE
;
484 ike_header
= ike_header_create();
485 if (ike_header
== NULL
)
491 ike_header
->set_exchange_type(ike_header
, this->exchange_type
);
492 ike_header
->set_message_id(ike_header
, this->message_id
);
493 ike_header
->set_response_flag(ike_header
, !this->is_request
);
494 ike_header
->set_initiator_flag(ike_header
, this->ike_sa_id
->is_initiator(this->ike_sa_id
));
495 ike_header
->set_initiator_spi(ike_header
, this->ike_sa_id
->get_initiator_spi(this->ike_sa_id
));
496 ike_header
->set_responder_spi(ike_header
, this->ike_sa_id
->get_responder_spi(this->ike_sa_id
));
498 generator
= generator_create();
499 if (generator
== NULL
)
504 payload
= (payload_t
*)ike_header
;
506 if (this->payloads
->create_iterator(this->payloads
, &iterator
, TRUE
) != SUCCESS
)
508 generator
->destroy(generator
);
509 ike_header
->destroy(ike_header
);
512 while(iterator
->has_next(iterator
))
514 iterator
->current(iterator
, (void**)&next_payload
);
515 payload
->set_next_type(payload
, next_payload
->get_type(next_payload
));
516 status
= generator
->generate_payload(generator
, payload
);
517 if (status
!= SUCCESS
)
519 generator
->destroy(generator
);
520 ike_header
->destroy(ike_header
);
523 payload
= next_payload
;
525 iterator
->destroy(iterator
);
527 payload
->set_next_type(payload
, NO_PAYLOAD
);
528 status
= generator
->generate_payload(generator
, payload
);
529 if (status
!= SUCCESS
)
531 generator
->destroy(generator
);
532 ike_header
->destroy(ike_header
);
536 ike_header
->destroy(ike_header
);
540 if (this->packet
->data
.ptr
!= NULL
)
542 allocator_free(this->packet
->data
.ptr
);
545 status
= generator
->write_to_chunk(generator
, &(this->packet
->data
));
546 if (status
!= SUCCESS
)
548 generator
->destroy(generator
);
552 this->packet
->clone(this->packet
, packet
);
554 generator
->destroy(generator
);
557 this->logger
->log(this->logger
, CONTROL
, "message generated successfully");
562 * Implements message_t's parse_header function.
563 * See #message_s.parse_header.
565 static status_t
parse_header(private_message_t
*this)
567 ike_header_t
*ike_header
;
571 this->logger
->log(this->logger
, CONTROL
, "parsing header of message");
573 this->parser
->reset_context(this->parser
);
574 status
= this->parser
->parse_payload(this->parser
,HEADER
,(payload_t
**) &ike_header
);
575 if (status
!= SUCCESS
)
577 this->logger
->log(this->logger
, ERROR
, "Header could not be parsed");
583 status
= ike_header
->payload_interface
.verify(&(ike_header
->payload_interface
));
584 if (status
!= SUCCESS
)
586 this->logger
->log(this->logger
, ERROR
, "Header verification failed");
587 ike_header
->destroy(ike_header
);
591 if (this->ike_sa_id
!= NULL
)
593 this->ike_sa_id
->destroy(this->ike_sa_id
);
596 this->ike_sa_id
= ike_sa_id_create(ike_header
->get_initiator_spi(ike_header
),
597 ike_header
->get_responder_spi(ike_header
),
598 ike_header
->get_initiator_flag(ike_header
));
599 if (this->ike_sa_id
== NULL
)
601 this->logger
->log(this->logger
, ERROR
, "could not create ike_sa_id object");
602 ike_header
->destroy(ike_header
);
605 this->exchange_type
= ike_header
->get_exchange_type(ike_header
);
606 this->message_id
= ike_header
->get_message_id(ike_header
);
607 this->is_request
= (!(ike_header
->get_response_flag(ike_header
)));
608 this->major_version
= ike_header
->get_maj_version(ike_header
);
609 this->minor_version
= ike_header
->get_min_version(ike_header
);
610 this->first_payload
= ike_header
->payload_interface
.get_next_type(&(ike_header
->payload_interface
));
613 this->logger
->log(this->logger
, CONTROL
, "parsing header successfully");
615 ike_header
->destroy(ike_header
);
620 * Implements message_t's parse_body function.
621 * See #message_s.parse_body.
623 static status_t
parse_body (private_message_t
*this)
625 status_t status
= SUCCESS
;
627 payload_type_t current_payload_type
= this->first_payload
;
628 supported_payload_entry_t
*supported_payloads
;
629 size_t supported_payloads_count
;
632 this->logger
->log(this->logger
, CONTROL
, "parsing body of message");
634 if (this->get_supported_payloads (this, &supported_payloads
, &supported_payloads_count
) != SUCCESS
)
636 this->logger
->log(this->logger
, ERROR
, "could not get supported payloads");
640 while (current_payload_type
!= NO_PAYLOAD
)
642 payload_t
*current_payload
;
643 bool supported
= FALSE
;
645 this->logger
->log(this->logger
, CONTROL
|MORE
, "start parsing payload of type %s",
646 mapping_find(payload_type_m
, current_payload_type
));
647 for (i
= 0; i
< supported_payloads_count
;i
++)
649 if (supported_payloads
[i
].payload_type
== current_payload_type
)
655 if (!supported
&& (current_payload_type
!= NO_PAYLOAD
))
657 /* type not supported */
658 status
= NOT_SUPPORTED
;
659 this->logger
->log(this->logger
, ERROR
, "payload type %s not supported",mapping_find(payload_type_m
,current_payload_type
));
663 status
= this->parser
->parse_payload(this->parser
,current_payload_type
,(payload_t
**) ¤t_payload
);
664 if (status
!= SUCCESS
)
666 this->logger
->log(this->logger
, ERROR
, "payload type %s could not be parsed",mapping_find(payload_type_m
,current_payload_type
));
670 status
= current_payload
->verify(current_payload
);
671 if (status
!= SUCCESS
)
673 this->logger
->log(this->logger
, ERROR
, "payload type %s could not be verified",mapping_find(payload_type_m
,current_payload_type
));
674 status
= VERIFY_ERROR
;
678 /* get next payload type */
679 current_payload_type
= current_payload
->get_next_type(current_payload
);
681 status
= this->payloads
->insert_last(this->payloads
,current_payload
);
682 if (status
!= SUCCESS
)
684 this->logger
->log(this->logger
, ERROR
, "Could not insert current payload to internal list cause of ressource exhausting");
689 if (status
!= SUCCESS
)
691 /* already parsed payload is destroyed later in destroy call from outside this object */
695 linked_list_iterator_t
*iterator
;
697 status
= this->payloads
->create_iterator(this->payloads
,&iterator
,TRUE
);
698 if (status
!= SUCCESS
)
700 this->logger
->log(this->logger
, ERROR
, "Could not create iterator to check supported payloads");
704 /* check for payloads with wrong count*/
705 for (i
= 0; i
< supported_payloads_count
;i
++)
707 size_t min_occurence
= supported_payloads
[i
].min_occurence
;
708 size_t max_occurence
= supported_payloads
[i
].max_occurence
;
709 payload_type_t payload_type
= supported_payloads
[i
].payload_type
;
710 size_t found_payloads
= 0;
712 iterator
->reset(iterator
);
714 while(iterator
->has_next(iterator
))
716 payload_t
*current_payload
;
717 status
= iterator
->current(iterator
,(void **)¤t_payload
);
718 if (status
!= SUCCESS
)
720 this->logger
->log(this->logger
, ERROR
, "Could not get payload from internal list");
721 iterator
->destroy(iterator
);
724 if (current_payload
->get_type(current_payload
) == payload_type
)
727 if (found_payloads
> max_occurence
)
729 this->logger
->log(this->logger
, ERROR
, "Payload of type %s more than %d times (%d) occured in current message",
730 mapping_find(payload_type_m
,current_payload
->get_type(current_payload
)),max_occurence
,found_payloads
);
731 iterator
->destroy(iterator
);
732 return NOT_SUPPORTED
;
737 if (found_payloads
< min_occurence
)
739 this->logger
->log(this->logger
, ERROR
, "Payload of type %s not occured %d times",
740 mapping_find(payload_type_m
,payload_type
),min_occurence
);
741 iterator
->destroy(iterator
);
742 return NOT_SUPPORTED
;
746 iterator
->destroy(iterator
);
754 * Implements message_t's destroy function.
755 * See #message_s.destroy.
757 static status_t
destroy (private_message_t
*this)
759 linked_list_iterator_t
*iterator
;
761 this->packet
->destroy(this->packet
);
763 if (this->ike_sa_id
!= NULL
)
765 this->ike_sa_id
->destroy(this->ike_sa_id
);
768 this->payloads
->create_iterator(this->payloads
, &iterator
, TRUE
);
769 while (iterator
->has_next(iterator
))
772 iterator
->current(iterator
, (void**)&payload
);
773 this->logger
->log(this->logger
, CONTROL
|MOST
, "Destroying payload of type %s",
774 mapping_find(payload_type_m
, payload
->get_type(payload
)));
775 payload
->destroy(payload
);
777 iterator
->destroy(iterator
);
778 this->payloads
->destroy(this->payloads
);
779 this->parser
->destroy(this->parser
);
780 global_logger_manager
->destroy_logger(global_logger_manager
, this->logger
);
782 allocator_free(this);
787 * Described in Header-File
789 message_t
*message_create_from_packet(packet_t
*packet
)
791 private_message_t
*this = allocator_alloc_thing(private_message_t
);
797 /* public functions */
798 this->public.set_major_version
= (status_t(*)(message_t
*, u_int8_t
))set_major_version
;
799 this->public.get_major_version
= (u_int8_t(*)(message_t
*))get_major_version
;
800 this->public.set_minor_version
= (status_t(*)(message_t
*, u_int8_t
))set_minor_version
;
801 this->public.get_minor_version
= (u_int8_t(*)(message_t
*))get_minor_version
;
802 this->public.set_message_id
= (status_t(*)(message_t
*, u_int32_t
))set_message_id
;
803 this->public.get_message_id
= (u_int32_t(*)(message_t
*))get_message_id
;
804 this->public.get_responder_spi
= (u_int64_t(*)(message_t
*))get_responder_spi
;
805 this->public.set_ike_sa_id
= (status_t(*)(message_t
*, ike_sa_id_t
*))set_ike_sa_id
;
806 this->public.get_ike_sa_id
= (status_t(*)(message_t
*, ike_sa_id_t
**))get_ike_sa_id
;
807 this->public.set_exchange_type
= (status_t(*)(message_t
*, exchange_type_t
))set_exchange_type
;
808 this->public.get_exchange_type
= (exchange_type_t(*)(message_t
*))get_exchange_type
;
809 this->public.set_request
= (status_t(*)(message_t
*, bool))set_request
;
810 this->public.get_request
= (bool(*)(message_t
*))get_request
;
811 this->public.add_payload
= (status_t(*)(message_t
*,payload_t
*))add_payload
;
812 this->public.generate
= (status_t (*) (message_t
*, packet_t
**)) generate
;
813 this->public.set_source
= (status_t (*) (message_t
*,host_t
*)) set_source
;
814 this->public.get_source
= (status_t (*) (message_t
*,host_t
**)) get_source
;
815 this->public.set_destination
= (status_t (*) (message_t
*,host_t
*)) set_destination
;
816 this->public.get_destination
= (status_t (*) (message_t
*,host_t
**)) get_destination
;
817 this->public.get_payload_iterator
= (status_t (*) (message_t
*, linked_list_iterator_t
**)) get_payload_iterator
;
818 this->public.parse_header
= (status_t (*) (message_t
*)) parse_header
;
819 this->public.parse_body
= (status_t (*) (message_t
*)) parse_body
;
820 this->public.destroy
= (status_t(*)(message_t
*))destroy
;
823 this->exchange_type
= EXCHANGE_TYPE_UNDEFINED
;
824 this->is_request
= TRUE
;
825 this->ike_sa_id
= NULL
;
826 this->first_payload
= NO_PAYLOAD
;
827 this->message_id
= 0;
829 /* private functions */
830 this->get_supported_payloads
= get_supported_payloads
;
835 packet
= packet_create();
839 allocator_free(this);
842 this->packet
= packet
;
843 this->payloads
= linked_list_create();
844 if (this->payloads
== NULL
)
846 allocator_free(this);
850 /* parser is created from data of packet */
851 this->parser
= parser_create(this->packet
->data
);
852 if (this->parser
== NULL
)
854 this->payloads
->destroy(this->payloads
);
855 allocator_free(this);
859 this->logger
= global_logger_manager
->create_logger(global_logger_manager
, MESSAGE
, NULL
);
860 if (this->logger
== NULL
)
862 this->parser
->destroy(this->parser
);
863 this->payloads
->destroy(this->payloads
);
864 allocator_free(this);
868 return (&this->public);
872 * Described in Header-File
874 message_t
*message_create()
876 return message_create_from_packet(NULL
);