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
;
160 * TRUE if message is from original initiator, FALSE otherwise.
162 bool original_initiator
;
165 * TRUE if message is request.
166 * FALSE if message is reply.
171 * Message ID of this message
173 u_int32_t message_id
;
176 * ID of assigned IKE_SA
178 ike_sa_id_t
*ike_sa_id
;
181 * Assigned UDP packet.
183 * Stores incoming packet or last generated one.
188 * Linked List where payload data are stored in
190 linked_list_t
*payloads
;
193 * Assigned parser to parse Header and Body of this message
198 * logger for this message
203 * Gets a list of supported payloads of this message type
205 * @param this calling object
206 * @param[out] supported_payloads first entry of supported payloads
207 * @param[out] supported_payloads_count number of supported payload entries
210 * NOT_FOUND if no supported payload definition could be found
212 status_t (*get_supported_payloads
) (private_message_t
*this, supported_payload_entry_t
**supported_payloads
,size_t *supported_payloads_count
);
217 * Implements private_message_t's get_supported_payloads function.
218 * See #private_message_t.get_supported_payloads.
220 status_t
get_supported_payloads (private_message_t
*this, supported_payload_entry_t
**supported_payloads
,size_t *supported_payloads_count
)
223 exchange_type_t exchange_type
= this->public.get_exchange_type(&(this->public));
224 bool is_request
= this->public.get_request(&(this->public));
227 for (i
= 0; i
< (sizeof(message_rules
) / sizeof(message_rule_t
)); i
++)
229 if ((exchange_type
== message_rules
[i
].exchange_type
) &&
230 (is_request
== message_rules
[i
].is_request
))
232 /* found rule for given exchange_type*/
233 *supported_payloads
= message_rules
[i
].supported_payloads
;
234 *supported_payloads_count
= message_rules
[i
].supported_payloads_count
;
241 *supported_payloads
= NULL
;
242 *supported_payloads_count
= 0;
247 * Implements message_t's set_ike_sa_id function.
248 * See #message_s.set_ike_sa_id.
250 static status_t
set_ike_sa_id (private_message_t
*this,ike_sa_id_t
*ike_sa_id
)
253 status
= ike_sa_id
->clone(ike_sa_id
,&(this->ike_sa_id
));
258 * Implements message_t's get_ike_sa_id function.
259 * See #message_s.get_ike_sa_id.
261 static status_t
get_ike_sa_id (private_message_t
*this,ike_sa_id_t
**ike_sa_id
)
264 if (this->ike_sa_id
== NULL
)
268 status
= this->ike_sa_id
->clone(this->ike_sa_id
,ike_sa_id
);
274 * Implements message_t's set_message_id function.
275 * See #message_s.set_message_id.
277 static status_t
set_message_id (private_message_t
*this,u_int32_t message_id
)
279 this->message_id
= message_id
;
285 * Implements message_t's set_message_id function.
286 * See #message_s.set_message_id.
288 static u_int32_t
get_message_id (private_message_t
*this)
290 return this->message_id
;
294 * Implements message_t's set_major_version function.
295 * See #message_s.set_major_version.
297 static status_t
set_major_version (private_message_t
*this,u_int8_t major_version
)
299 this->major_version
= major_version
;
305 * Implements message_t's get_major_version function.
306 * See #message_s.get_major_version.
308 static u_int8_t
get_major_version (private_message_t
*this)
310 return this->major_version
;
314 * Implements message_t's set_minor_version function.
315 * See #message_s.set_minor_version.
317 static status_t
set_minor_version (private_message_t
*this,u_int8_t minor_version
)
319 this->minor_version
= minor_version
;
325 * Implements message_t's get_minor_version function.
326 * See #message_s.get_minor_version.
328 static u_int8_t
get_minor_version (private_message_t
*this)
330 return this->minor_version
;
334 * Implements message_t's set_exchange_type function.
335 * See #message_s.set_exchange_type.
337 static status_t
set_exchange_type (private_message_t
*this,exchange_type_t exchange_type
)
339 this->exchange_type
= exchange_type
;
345 * Implements message_t's get_exchange_type function.
346 * See #message_s.get_exchange_type.
348 static exchange_type_t
get_exchange_type (private_message_t
*this)
350 return this->exchange_type
;
354 * Implements message_t's set_original_initiator function.
355 * See #message_s.set_original_initiator.
357 static status_t
set_original_initiator (private_message_t
*this,bool original_initiator
)
359 this->original_initiator
= original_initiator
;
364 * Implements message_t's get_original_initiator function.
365 * See #message_s.get_original_initiator.
367 static exchange_type_t
get_original_initiator (private_message_t
*this)
369 return this->original_initiator
;
373 * Implements message_t's set_request function.
374 * See #message_s.set_request.
376 static status_t
set_request (private_message_t
*this,bool request
)
378 this->is_request
= request
;
383 * Implements message_t's get_request function.
384 * See #message_s.get_request.
386 static exchange_type_t
get_request (private_message_t
*this)
388 return this->is_request
;
391 static status_t
add_payload(private_message_t
*this, payload_t
*payload
)
393 if (this->payloads
->insert_last(this->payloads
, payload
) != SUCCESS
)
400 static status_t
set_source(private_message_t
*this, host_t
*host
)
402 if (this->packet
->source
!= NULL
)
404 this->packet
->source
->destroy(this->packet
->source
);
406 this->packet
->source
= host
;
410 static status_t
set_destination(private_message_t
*this, host_t
*host
)
412 if (this->packet
->destination
!= NULL
)
414 this->packet
->destination
->destroy(this->packet
->destination
);
416 this->packet
->destination
= host
;
420 static status_t
get_source(private_message_t
*this, host_t
**host
)
422 *host
= this->packet
->source
;
426 static status_t
get_destination(private_message_t
*this, host_t
**host
)
428 *host
= this->packet
->destination
;
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
;
443 u_int64_t initiator_spi
, responder_spi
;
447 if (this->exchange_type
== EXCHANGE_TYPE_UNDEFINED
)
449 return INVALID_STATE
;
452 if (this->packet
->source
== NULL
||
453 this->packet
->destination
== NULL
)
455 return INVALID_STATE
;
458 ike_header
= ike_header_create();
459 if (ike_header
== NULL
)
464 this->ike_sa_id
->get_values(this->ike_sa_id
, &initiator_spi
, &responder_spi
, &is_initiator
);
466 ike_header
->set_exchange_type(ike_header
, this->exchange_type
);
467 ike_header
->set_initiator_flag(ike_header
, this->original_initiator
);
468 ike_header
->set_message_id(ike_header
, this->message_id
);
469 ike_header
->set_response_flag(ike_header
, !this->is_request
);
470 ike_header
->set_initiator_flag(ike_header
, is_initiator
);
471 ike_header
->set_initiator_spi(ike_header
, initiator_spi
);
472 ike_header
->set_responder_spi(ike_header
, responder_spi
);
474 generator
= generator_create();
475 if (generator
== NULL
)
480 payload
= (payload_t
*)ike_header
;
482 if (this->payloads
->create_iterator(this->payloads
, &iterator
, TRUE
) != SUCCESS
)
484 generator
->destroy(generator
);
485 ike_header
->destroy(ike_header
);
488 while(iterator
->has_next(iterator
))
490 iterator
->current(iterator
, (void**)&next_payload
);
491 payload
->set_next_type(payload
, next_payload
->get_type(next_payload
));
492 status
= generator
->generate_payload(generator
, payload
);
493 if (status
!= SUCCESS
)
495 generator
->destroy(generator
);
496 ike_header
->destroy(ike_header
);
499 payload
= next_payload
;
501 iterator
->destroy(iterator
);
503 payload
->set_next_type(payload
, NO_PAYLOAD
);
504 status
= generator
->generate_payload(generator
, payload
);
505 if (status
!= SUCCESS
)
507 generator
->destroy(generator
);
508 ike_header
->destroy(ike_header
);
512 ike_header
->destroy(ike_header
);
516 if (this->packet
->data
.ptr
!= NULL
)
518 allocator_free(this->packet
->data
.ptr
);
521 status
= generator
->write_to_chunk(generator
, &(this->packet
->data
));
522 if (status
!= SUCCESS
)
524 generator
->destroy(generator
);
528 this->packet
->clone(this->packet
, packet
);
530 generator
->destroy(generator
);
535 * Implements message_t's parse_header function.
536 * See #message_s.parse_header.
538 static status_t
parse_header (private_message_t
*this)
540 ike_header_t
*ike_header
;
543 this->parser
->reset_context(this->parser
);
544 status
= this->parser
->parse_payload(this->parser
,HEADER
,(payload_t
**) &ike_header
);
545 if (status
!= SUCCESS
)
547 this->logger
->log(this->logger
, ERROR
, "Header could not be parsed");
553 status
= ike_header
->payload_interface
.verify(&(ike_header
->payload_interface
));
554 if (status
!= SUCCESS
)
556 this->logger
->log(this->logger
, ERROR
, "Header could not be verified");
560 if (this->ike_sa_id
!= NULL
)
562 this->ike_sa_id
->destroy(this->ike_sa_id
);
564 this->original_initiator
= (!ike_header
->get_initiator_flag(ike_header
));
566 this->ike_sa_id
= ike_sa_id_create(ike_header
->get_initiator_spi(ike_header
),ike_header
->get_responder_spi(ike_header
),this->original_initiator
);
567 if (this->ike_sa_id
== NULL
)
569 this->logger
->log(this->logger
, ERROR
, "Could not creaee ike_sa_id object");
570 ike_header
->destroy(ike_header
);
573 this->exchange_type
= ike_header
->get_exchange_type(ike_header
);
574 this->message_id
= ike_header
->get_message_id(ike_header
);
575 this->is_request
= (!(ike_header
->get_response_flag(ike_header
)));
576 this->major_version
= ike_header
->get_maj_version(ike_header
);
577 this->minor_version
= ike_header
->get_min_version(ike_header
);
578 this->first_payload
= ike_header
->payload_interface
.get_next_type(&(ike_header
->payload_interface
));
580 ike_header
->destroy(ike_header
);
585 * Implements message_t's parse_body function.
586 * See #message_s.parse_body.
588 static status_t
parse_body (private_message_t
*this)
592 payload_type_t current_payload_type
= this->first_payload
;
593 supported_payload_entry_t
*supported_payloads
;
594 size_t supported_payloads_count
;
596 if (this->get_supported_payloads (this,&supported_payloads
,&supported_payloads_count
) != SUCCESS
)
598 /* message type is not supported */
602 while (current_payload_type
!= NO_PAYLOAD
)
604 payload_t
*current_payload
;
606 bool supported
= FALSE
;
607 for (i
= 0; i
< supported_payloads_count
;i
++)
609 if (supported_payloads
[i
].payload_type
== current_payload_type
)
615 if (!supported
&& (current_payload_type
!= NO_PAYLOAD
))
617 /* type not supported */
618 status
= NOT_SUPPORTED
;
619 this->logger
->log(this->logger
, ERROR
, "Payload type %s not supported",mapping_find(payload_type_m
,current_payload_type
));
623 status
= this->parser
->parse_payload(this->parser
,current_payload_type
,(payload_t
**) ¤t_payload
);
624 if (status
!= SUCCESS
)
626 this->logger
->log(this->logger
, ERROR
, "Payload type %s could not be parsed",mapping_find(payload_type_m
,current_payload_type
));
630 current_payload_type
= current_payload
->get_next_type(current_payload
);
632 status
= current_payload
->verify(current_payload
);
633 if (status
!= SUCCESS
)
635 this->logger
->log(this->logger
, ERROR
, "Payload type %s could not be verified",mapping_find(payload_type_m
,current_payload_type
));
636 status
= VERIFY_ERROR
;
640 status
= this->payloads
->insert_last(this->payloads
,current_payload
);
641 if (status
!= SUCCESS
)
643 this->logger
->log(this->logger
, ERROR
, "Could not insert current payload to internal list cause of ressource exhausting");
648 if (status
!= SUCCESS
)
650 /* already parsed payload is destroyed later in destroy call from outside this object */
654 linked_list_iterator_t
*iterator
;
656 status
= this->payloads
->create_iterator(this->payloads
,&iterator
,TRUE
);
657 if (status
!= SUCCESS
)
659 this->logger
->log(this->logger
, ERROR
, "Could not create iterator to check supported payloads");
664 /* check for payloads with wrong count*/
665 for (i
= 0; i
< supported_payloads_count
;i
++)
667 size_t min_occurence
= supported_payloads
[i
].min_occurence
;
668 size_t max_occurence
= supported_payloads
[i
].max_occurence
;
669 payload_type_t payload_type
= supported_payloads
[i
].payload_type
;
670 size_t found_payloads
= 0;
672 iterator
->reset(iterator
);
674 while(iterator
->has_next(iterator
))
676 payload_t
*current_payload
;
677 status
= iterator
->current(iterator
,(void **)¤t_payload
);
678 if (status
!= SUCCESS
)
680 this->logger
->log(this->logger
, CONTROL_MORE
, "Could not get payload from internal list");
681 iterator
->destroy(iterator
);
684 if (current_payload
->get_type(current_payload
) == payload_type
)
687 if (found_payloads
> max_occurence
)
689 this->logger
->log(this->logger
, CONTROL_MORE
, "Payload of type %s more than %d times (%d) occured in current message",
690 mapping_find(payload_type_m
,current_payload
->get_type(current_payload
)),max_occurence
,found_payloads
);
691 iterator
->destroy(iterator
);
692 return NOT_SUPPORTED
;
697 if (found_payloads
< min_occurence
)
699 this->logger
->log(this->logger
, CONTROL_MORE
, "Payload of type %s not occured %d times",
700 mapping_find(payload_type_m
,payload_type
),min_occurence
);
701 iterator
->destroy(iterator
);
702 return NOT_SUPPORTED
;
706 iterator
->destroy(iterator
);
714 * Implements message_t's destroy function.
715 * See #message_s.destroy.
717 static status_t
destroy (private_message_t
*this)
719 linked_list_iterator_t
*iterator
;
721 if (this->packet
!= NULL
)
723 this->packet
->destroy(this->packet
);
725 if (this->ike_sa_id
!= NULL
)
727 this->ike_sa_id
->destroy(this->ike_sa_id
);
730 this->payloads
->create_iterator(this->payloads
, &iterator
, TRUE
);
731 while (iterator
->has_next(iterator
))
734 iterator
->current(iterator
, (void**)&payload
);
735 this->logger
->log(this->logger
, CONTROL_MORE
, "Destroying payload of type %s",
736 mapping_find(payload_type_m
, payload
->get_type(payload
)));
737 payload
->destroy(payload
);
739 iterator
->destroy(iterator
);
740 this->payloads
->destroy(this->payloads
);
741 this->parser
->destroy(this->parser
);
743 allocator_free(this);
748 * Described in Header-File
750 message_t
*message_create_from_packet(packet_t
*packet
)
752 private_message_t
*this = allocator_alloc_thing(private_message_t
);
758 /* public functions */
759 this->public.set_major_version
= (status_t(*)(message_t
*, u_int8_t
))set_major_version
;
760 this->public.get_major_version
= (u_int8_t(*)(message_t
*))get_major_version
;
761 this->public.set_minor_version
= (status_t(*)(message_t
*, u_int8_t
))set_minor_version
;
762 this->public.get_minor_version
= (u_int8_t(*)(message_t
*))get_minor_version
;
763 this->public.set_message_id
= (status_t(*)(message_t
*, u_int32_t
))set_message_id
;
764 this->public.get_message_id
= (u_int32_t(*)(message_t
*))get_message_id
;
765 this->public.set_ike_sa_id
= (status_t(*)(message_t
*, ike_sa_id_t
*))set_ike_sa_id
;
766 this->public.get_ike_sa_id
= (status_t(*)(message_t
*, ike_sa_id_t
**))get_ike_sa_id
;
767 this->public.set_exchange_type
= (status_t(*)(message_t
*, exchange_type_t
))set_exchange_type
;
768 this->public.get_exchange_type
= (exchange_type_t(*)(message_t
*))get_exchange_type
;
769 this->public.set_original_initiator
= (status_t(*)(message_t
*, bool))set_original_initiator
;
770 this->public.get_original_initiator
= (bool(*)(message_t
*))get_original_initiator
;
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.parse_header
= (status_t (*) (message_t
*)) parse_header
;
780 this->public.parse_body
= (status_t (*) (message_t
*)) parse_body
;
781 this->public.destroy
= (status_t(*)(message_t
*))destroy
;
784 this->exchange_type
= EXCHANGE_TYPE_UNDEFINED
;
785 this->original_initiator
= TRUE
;
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
);