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 "generator.h"
31 #include "utils/linked_list.h"
32 #include "utils/allocator.h"
33 #include "utils/logger_manager.h"
34 #include "payloads/encodings.h"
35 #include "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 static supported_payload_entry_t supported_ike_sa_init_i_payloads
[] =
91 {SECURITY_ASSOCIATION
,1,1},
96 static supported_payload_entry_t supported_ike_sa_init_r_payloads
[] =
98 {SECURITY_ASSOCIATION
,1,1},
103 static message_rule_t message_rules
[] = {
104 {IKE_SA_INIT
,TRUE
,(sizeof(supported_ike_sa_init_i_payloads
)/sizeof(supported_payload_entry_t
)),supported_ike_sa_init_i_payloads
},
105 {IKE_SA_INIT
,FALSE
,(sizeof(supported_ike_sa_init_r_payloads
)/sizeof(supported_payload_entry_t
)),supported_ike_sa_init_r_payloads
}
109 * Entry for a payload in the internal used linked list
112 typedef struct payload_entry_s payload_entry_t
;
114 struct payload_entry_s
{
118 payload_type_t payload_type
;
120 * Data struct holding the data of given payload
127 * Private data of an message_t object
129 typedef struct private_message_s private_message_t
;
131 struct private_message_s
{
134 * Public part of a message_t object
140 * Minor version of message
142 u_int8_t major_version
;
145 * Major version of message
147 u_int8_t minor_version
;
150 * First Payload in message
152 payload_type_t first_payload
;
155 * Assigned exchange type
157 exchange_type_t exchange_type
;
161 * TRUE if message is request.
162 * FALSE if message is reply.
167 * Message ID of this message
169 u_int32_t message_id
;
172 * ID of assigned IKE_SA
174 ike_sa_id_t
*ike_sa_id
;
177 * Assigned UDP packet.
179 * Stores incoming packet or last generated one.
184 * Linked List where payload data are stored in
186 linked_list_t
*payloads
;
189 * Assigned parser to parse Header and Body of this message
194 * logger for this message
199 * Gets a list of supported payloads of this message type
201 * @param this calling object
202 * @param[out] supported_payloads first entry of supported payloads
203 * @param[out] supported_payloads_count number of supported payload entries
206 * NOT_FOUND if no supported payload definition could be found
208 status_t (*get_supported_payloads
) (private_message_t
*this, supported_payload_entry_t
**supported_payloads
,size_t *supported_payloads_count
);
213 * Implements private_message_t's get_supported_payloads function.
214 * See #private_message_t.get_supported_payloads.
216 status_t
get_supported_payloads (private_message_t
*this, supported_payload_entry_t
**supported_payloads
,size_t *supported_payloads_count
)
219 exchange_type_t exchange_type
= this->public.get_exchange_type(&(this->public));
220 bool is_request
= this->public.get_request(&(this->public));
223 for (i
= 0; i
< (sizeof(message_rules
) / sizeof(message_rule_t
)); i
++)
225 if ((exchange_type
== message_rules
[i
].exchange_type
) &&
226 (is_request
== message_rules
[i
].is_request
))
228 /* found rule for given exchange_type*/
229 *supported_payloads
= message_rules
[i
].supported_payloads
;
230 *supported_payloads_count
= message_rules
[i
].supported_payloads_count
;
237 *supported_payloads
= NULL
;
238 *supported_payloads_count
= 0;
243 * Implements message_t's set_ike_sa_id function.
244 * See #message_s.set_ike_sa_id.
246 static status_t
set_ike_sa_id (private_message_t
*this,ike_sa_id_t
*ike_sa_id
)
249 status
= ike_sa_id
->clone(ike_sa_id
,&(this->ike_sa_id
));
254 * Implements message_t's get_ike_sa_id function.
255 * See #message_s.get_ike_sa_id.
257 static status_t
get_ike_sa_id (private_message_t
*this,ike_sa_id_t
**ike_sa_id
)
260 if (this->ike_sa_id
== NULL
)
264 status
= this->ike_sa_id
->clone(this->ike_sa_id
,ike_sa_id
);
270 * Implements message_t's set_message_id function.
271 * See #message_s.set_message_id.
273 static status_t
set_message_id (private_message_t
*this,u_int32_t message_id
)
275 this->message_id
= message_id
;
281 * Implements message_t's set_message_id function.
282 * See #message_s.set_message_id.
284 static u_int32_t
get_message_id (private_message_t
*this)
286 return this->message_id
;
290 * Implements message_t's set_major_version function.
291 * See #message_s.set_major_version.
293 static status_t
set_major_version (private_message_t
*this,u_int8_t major_version
)
295 this->major_version
= major_version
;
301 * Implements message_t's get_major_version function.
302 * See #message_s.get_major_version.
304 static u_int8_t
get_major_version (private_message_t
*this)
306 return this->major_version
;
310 * Implements message_t's set_minor_version function.
311 * See #message_s.set_minor_version.
313 static status_t
set_minor_version (private_message_t
*this,u_int8_t minor_version
)
315 this->minor_version
= minor_version
;
321 * Implements message_t's get_minor_version function.
322 * See #message_s.get_minor_version.
324 static u_int8_t
get_minor_version (private_message_t
*this)
326 return this->minor_version
;
330 * Implements message_t's set_exchange_type function.
331 * See #message_s.set_exchange_type.
333 static status_t
set_exchange_type (private_message_t
*this,exchange_type_t exchange_type
)
335 this->exchange_type
= exchange_type
;
341 * Implements message_t's get_exchange_type function.
342 * See #message_s.get_exchange_type.
344 static exchange_type_t
get_exchange_type (private_message_t
*this)
346 return this->exchange_type
;
351 * Implements message_t's set_request function.
352 * See #message_s.set_request.
354 static status_t
set_request (private_message_t
*this,bool request
)
356 this->is_request
= request
;
361 * Implements message_t's get_request function.
362 * See #message_s.get_request.
364 static exchange_type_t
get_request (private_message_t
*this)
366 return this->is_request
;
369 static status_t
add_payload(private_message_t
*this, payload_t
*payload
)
371 payload_t
*last_payload
;
372 if ((this->payloads
->get_count(this->payloads
) > 0) &&
373 (this->payloads
->get_last(this->payloads
,(void **) &last_payload
) != SUCCESS
))
378 if (this->payloads
->insert_last(this->payloads
, payload
) != SUCCESS
)
382 if (this->payloads
->get_count(this->payloads
) == 1)
384 this->first_payload
= payload
->get_type(payload
);
388 last_payload
->set_next_type(last_payload
,payload
->get_type(payload
));
394 static status_t
set_source(private_message_t
*this, host_t
*host
)
396 if (this->packet
->source
!= NULL
)
398 this->packet
->source
->destroy(this->packet
->source
);
400 this->packet
->source
= host
;
404 static status_t
set_destination(private_message_t
*this, host_t
*host
)
406 if (this->packet
->destination
!= NULL
)
408 this->packet
->destination
->destroy(this->packet
->destination
);
410 this->packet
->destination
= host
;
414 static status_t
get_source(private_message_t
*this, host_t
**host
)
416 *host
= this->packet
->source
;
420 static status_t
get_destination(private_message_t
*this, host_t
**host
)
422 *host
= this->packet
->destination
;
427 static status_t
get_payload_iterator(private_message_t
*this, linked_list_iterator_t
**iterator
)
429 return this->payloads
->create_iterator(this->payloads
, iterator
, TRUE
);
434 * Implements message_t's generate function.
435 * See #message_s.generate.
437 static status_t
generate(private_message_t
*this, packet_t
**packet
)
439 generator_t
*generator
;
440 ike_header_t
*ike_header
;
441 payload_t
*payload
, *next_payload
;
442 linked_list_iterator_t
*iterator
;
445 if (this->exchange_type
== EXCHANGE_TYPE_UNDEFINED
)
447 return INVALID_STATE
;
450 if (this->packet
->source
== NULL
||
451 this->packet
->destination
== NULL
)
453 return INVALID_STATE
;
456 ike_header
= ike_header_create();
457 if (ike_header
== NULL
)
463 ike_header
->set_exchange_type(ike_header
, this->exchange_type
);
464 ike_header
->set_message_id(ike_header
, this->message_id
);
465 ike_header
->set_response_flag(ike_header
, !this->is_request
);
466 ike_header
->set_initiator_flag(ike_header
, this->ike_sa_id
->is_initiator(this->ike_sa_id
));
467 ike_header
->set_initiator_spi(ike_header
, this->ike_sa_id
->get_initiator_spi(this->ike_sa_id
));
468 ike_header
->set_responder_spi(ike_header
, this->ike_sa_id
->get_responder_spi(this->ike_sa_id
));
470 generator
= generator_create();
471 if (generator
== NULL
)
476 payload
= (payload_t
*)ike_header
;
478 if (this->payloads
->create_iterator(this->payloads
, &iterator
, TRUE
) != SUCCESS
)
480 generator
->destroy(generator
);
481 ike_header
->destroy(ike_header
);
484 while(iterator
->has_next(iterator
))
486 iterator
->current(iterator
, (void**)&next_payload
);
487 payload
->set_next_type(payload
, next_payload
->get_type(next_payload
));
488 status
= generator
->generate_payload(generator
, payload
);
489 if (status
!= SUCCESS
)
491 generator
->destroy(generator
);
492 ike_header
->destroy(ike_header
);
495 payload
= next_payload
;
497 iterator
->destroy(iterator
);
499 payload
->set_next_type(payload
, NO_PAYLOAD
);
500 status
= generator
->generate_payload(generator
, payload
);
501 if (status
!= SUCCESS
)
503 generator
->destroy(generator
);
504 ike_header
->destroy(ike_header
);
508 ike_header
->destroy(ike_header
);
512 if (this->packet
->data
.ptr
!= NULL
)
514 allocator_free(this->packet
->data
.ptr
);
517 status
= generator
->write_to_chunk(generator
, &(this->packet
->data
));
518 if (status
!= SUCCESS
)
520 generator
->destroy(generator
);
524 this->packet
->clone(this->packet
, packet
);
526 generator
->destroy(generator
);
531 * Implements message_t's parse_header function.
532 * See #message_s.parse_header.
534 static status_t
parse_header (private_message_t
*this)
536 ike_header_t
*ike_header
;
539 this->parser
->reset_context(this->parser
);
540 status
= this->parser
->parse_payload(this->parser
,HEADER
,(payload_t
**) &ike_header
);
541 if (status
!= SUCCESS
)
543 this->logger
->log(this->logger
, ERROR
, "Header could not be parsed");
549 status
= ike_header
->payload_interface
.verify(&(ike_header
->payload_interface
));
550 if (status
!= SUCCESS
)
552 this->logger
->log(this->logger
, ERROR
, "Header could not be verified");
556 if (this->ike_sa_id
!= NULL
)
558 this->ike_sa_id
->destroy(this->ike_sa_id
);
561 this->ike_sa_id
= ike_sa_id_create(ike_header
->get_initiator_spi(ike_header
),
562 ike_header
->get_responder_spi(ike_header
),
563 !ike_header
->get_initiator_flag(ike_header
));
564 if (this->ike_sa_id
== NULL
)
566 this->logger
->log(this->logger
, ERROR
, "Could not creaee ike_sa_id object");
567 ike_header
->destroy(ike_header
);
570 this->exchange_type
= ike_header
->get_exchange_type(ike_header
);
571 this->message_id
= ike_header
->get_message_id(ike_header
);
572 this->is_request
= (!(ike_header
->get_response_flag(ike_header
)));
573 this->major_version
= ike_header
->get_maj_version(ike_header
);
574 this->minor_version
= ike_header
->get_min_version(ike_header
);
575 this->first_payload
= ike_header
->payload_interface
.get_next_type(&(ike_header
->payload_interface
));
577 ike_header
->destroy(ike_header
);
582 * Implements message_t's parse_body function.
583 * See #message_s.parse_body.
585 static status_t
parse_body (private_message_t
*this)
587 status_t status
= SUCCESS
;
589 payload_type_t current_payload_type
= this->first_payload
;
590 supported_payload_entry_t
*supported_payloads
;
591 size_t supported_payloads_count
;
594 if (this->get_supported_payloads (this,&supported_payloads
,&supported_payloads_count
) != SUCCESS
)
596 this->logger
->log(this->logger
, ERROR
, "could not get supported payloads");
597 /* message type is not supported */
602 this->logger
->log(this->logger
, ERROR
, "first payload %s", mapping_find(payload_type_m
, current_payload_type
));
604 while (current_payload_type
!= NO_PAYLOAD
)
606 payload_t
*current_payload
;
608 bool supported
= FALSE
;
609 for (i
= 0; i
< supported_payloads_count
;i
++)
611 if (supported_payloads
[i
].payload_type
== current_payload_type
)
617 if (!supported
&& (current_payload_type
!= NO_PAYLOAD
))
619 /* type not supported */
620 status
= NOT_SUPPORTED
;
621 this->logger
->log(this->logger
, ERROR
, "Payload type %s not supported",mapping_find(payload_type_m
,current_payload_type
));
625 status
= this->parser
->parse_payload(this->parser
,current_payload_type
,(payload_t
**) ¤t_payload
);
626 if (status
!= SUCCESS
)
628 this->logger
->log(this->logger
, ERROR
, "Payload type %s could not be parsed",mapping_find(payload_type_m
,current_payload_type
));
632 current_payload_type
= current_payload
->get_next_type(current_payload
);
634 status
= current_payload
->verify(current_payload
);
635 if (status
!= SUCCESS
)
637 this->logger
->log(this->logger
, ERROR
, "Payload type %s could not be verified",mapping_find(payload_type_m
,current_payload_type
));
638 status
= VERIFY_ERROR
;
642 status
= this->payloads
->insert_last(this->payloads
,current_payload
);
643 if (status
!= SUCCESS
)
645 this->logger
->log(this->logger
, ERROR
, "Could not insert current payload to internal list cause of ressource exhausting");
650 if (status
!= SUCCESS
)
652 /* already parsed payload is destroyed later in destroy call from outside this object */
656 linked_list_iterator_t
*iterator
;
658 status
= this->payloads
->create_iterator(this->payloads
,&iterator
,TRUE
);
659 if (status
!= SUCCESS
)
661 this->logger
->log(this->logger
, ERROR
, "Could not create iterator to check supported payloads");
666 /* check for payloads with wrong count*/
667 for (i
= 0; i
< supported_payloads_count
;i
++)
669 size_t min_occurence
= supported_payloads
[i
].min_occurence
;
670 size_t max_occurence
= supported_payloads
[i
].max_occurence
;
671 payload_type_t payload_type
= supported_payloads
[i
].payload_type
;
672 size_t found_payloads
= 0;
674 iterator
->reset(iterator
);
676 while(iterator
->has_next(iterator
))
678 payload_t
*current_payload
;
679 status
= iterator
->current(iterator
,(void **)¤t_payload
);
680 if (status
!= SUCCESS
)
682 this->logger
->log(this->logger
, CONTROL
|MORE
, "Could not get payload from internal list");
683 iterator
->destroy(iterator
);
686 if (current_payload
->get_type(current_payload
) == payload_type
)
689 if (found_payloads
> max_occurence
)
691 this->logger
->log(this->logger
, CONTROL
|MORE
, "Payload of type %s more than %d times (%d) occured in current message",
692 mapping_find(payload_type_m
,current_payload
->get_type(current_payload
)),max_occurence
,found_payloads
);
693 iterator
->destroy(iterator
);
694 return NOT_SUPPORTED
;
699 if (found_payloads
< min_occurence
)
701 this->logger
->log(this->logger
, CONTROL
|MORE
, "Payload of type %s not occured %d times",
702 mapping_find(payload_type_m
,payload_type
),min_occurence
);
703 iterator
->destroy(iterator
);
704 return NOT_SUPPORTED
;
708 iterator
->destroy(iterator
);
716 * Implements message_t's destroy function.
717 * See #message_s.destroy.
719 static status_t
destroy (private_message_t
*this)
721 linked_list_iterator_t
*iterator
;
723 if (this->packet
!= NULL
)
725 this->packet
->destroy(this->packet
);
727 if (this->ike_sa_id
!= NULL
)
729 this->ike_sa_id
->destroy(this->ike_sa_id
);
732 this->payloads
->create_iterator(this->payloads
, &iterator
, TRUE
);
733 while (iterator
->has_next(iterator
))
736 iterator
->current(iterator
, (void**)&payload
);
737 this->logger
->log(this->logger
, CONTROL
|MORE
, "Destroying payload of type %s",
738 mapping_find(payload_type_m
, payload
->get_type(payload
)));
739 payload
->destroy(payload
);
741 iterator
->destroy(iterator
);
742 this->payloads
->destroy(this->payloads
);
743 this->parser
->destroy(this->parser
);
745 allocator_free(this);
750 * Described in Header-File
752 message_t
*message_create_from_packet(packet_t
*packet
)
754 private_message_t
*this = allocator_alloc_thing(private_message_t
);
760 /* public functions */
761 this->public.set_major_version
= (status_t(*)(message_t
*, u_int8_t
))set_major_version
;
762 this->public.get_major_version
= (u_int8_t(*)(message_t
*))get_major_version
;
763 this->public.set_minor_version
= (status_t(*)(message_t
*, u_int8_t
))set_minor_version
;
764 this->public.get_minor_version
= (u_int8_t(*)(message_t
*))get_minor_version
;
765 this->public.set_message_id
= (status_t(*)(message_t
*, u_int32_t
))set_message_id
;
766 this->public.get_message_id
= (u_int32_t(*)(message_t
*))get_message_id
;
767 this->public.set_ike_sa_id
= (status_t(*)(message_t
*, ike_sa_id_t
*))set_ike_sa_id
;
768 this->public.get_ike_sa_id
= (status_t(*)(message_t
*, ike_sa_id_t
**))get_ike_sa_id
;
769 this->public.set_exchange_type
= (status_t(*)(message_t
*, exchange_type_t
))set_exchange_type
;
770 this->public.get_exchange_type
= (exchange_type_t(*)(message_t
*))get_exchange_type
;
771 this->public.set_request
= (status_t(*)(message_t
*, bool))set_request
;
772 this->public.get_request
= (bool(*)(message_t
*))get_request
;
773 this->public.add_payload
= (status_t(*)(message_t
*,payload_t
*))add_payload
;
774 this->public.generate
= (status_t (*) (message_t
*, packet_t
**)) generate
;
775 this->public.set_source
= (status_t (*) (message_t
*,host_t
*)) set_source
;
776 this->public.get_source
= (status_t (*) (message_t
*,host_t
**)) get_source
;
777 this->public.set_destination
= (status_t (*) (message_t
*,host_t
*)) set_destination
;
778 this->public.get_destination
= (status_t (*) (message_t
*,host_t
**)) get_destination
;
779 this->public.get_payload_iterator
= (status_t (*) (message_t
*, linked_list_iterator_t
**)) get_payload_iterator
;
780 this->public.parse_header
= (status_t (*) (message_t
*)) parse_header
;
781 this->public.parse_body
= (status_t (*) (message_t
*)) parse_body
;
782 this->public.destroy
= (status_t(*)(message_t
*))destroy
;
785 this->exchange_type
= EXCHANGE_TYPE_UNDEFINED
;
786 this->is_request
= TRUE
;
787 this->ike_sa_id
= NULL
;
788 this->first_payload
= NO_PAYLOAD
;
789 this->message_id
= 0;
791 /* private functions */
792 this->get_supported_payloads
= get_supported_payloads
;
797 packet
= packet_create();
801 allocator_free(this);
804 this->packet
= packet
;
805 this->payloads
= linked_list_create();
806 if (this->payloads
== NULL
)
808 allocator_free(this);
812 /* parser is created from data of packet */
813 this->parser
= parser_create(this->packet
->data
);
814 if (this->parser
== NULL
)
816 this->payloads
->destroy(this->payloads
);
817 allocator_free(this);
821 this->logger
= global_logger_manager
->create_logger(global_logger_manager
, MESSAGE
, NULL
);
822 if (this->logger
== NULL
)
824 this->parser
->destroy(this->parser
);
825 this->payloads
->destroy(this->payloads
);
826 allocator_free(this);
830 return (&this->public);
834 * Described in Header-File
836 message_t
*message_create()
838 return message_create_from_packet(NULL
);