9de5c2e71e9c03c5058e766e1631fffac0d1c054
[strongswan.git] / src / libcharon / sa / ikev1 / tasks / informational.c
1 /*
2 * Copyright (C) 2011 Martin Willi
3 * Copyright (C) 2011 revosec AG
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 "informational.h"
17
18 #include <daemon.h>
19 #include <sa/ikev1/tasks/isakmp_delete.h>
20 #include <sa/ikev1/tasks/quick_delete.h>
21 #include <encoding/payloads/delete_payload.h>
22
23 typedef struct private_informational_t private_informational_t;
24
25 /**
26 * Private members of a informational_t task.
27 */
28 struct private_informational_t {
29
30 /**
31 * Public methods and task_t interface.
32 */
33 informational_t public;
34
35 /**
36 * Assigned IKE_SA.
37 */
38 ike_sa_t *ike_sa;
39
40 /**
41 * Notify payload to send
42 */
43 notify_payload_t *notify;
44
45 /**
46 * Delete subtask
47 */
48 task_t *del;
49 };
50
51 METHOD(task_t, build_i, status_t,
52 private_informational_t *this, message_t *message)
53 {
54 message->add_payload(message, &this->notify->payload_interface);
55 this->notify = NULL;
56 return SUCCESS;
57 }
58
59 METHOD(task_t, process_r, status_t,
60 private_informational_t *this, message_t *message)
61 {
62 enumerator_t *enumerator;
63 delete_payload_t *delete;
64 notify_payload_t *notify;
65 notify_type_t type;
66 payload_t *payload;
67 status_t status = SUCCESS;
68
69 enumerator = message->create_payload_enumerator(message);
70 while (enumerator->enumerate(enumerator, &payload))
71 {
72 switch (payload->get_type(payload))
73 {
74 case NOTIFY_V1:
75 notify = (notify_payload_t*)payload;
76 type = notify->get_notify_type(notify);
77
78 if (type == INITIAL_CONTACT_IKEV1)
79 {
80 this->ike_sa->set_condition(this->ike_sa,
81 COND_INIT_CONTACT_SEEN, TRUE);
82 }
83 else if (type < 16384)
84 {
85 DBG1(DBG_IKE, "received %N error notify",
86 notify_type_names, notify->get_notify_type(notify));
87 if (this->ike_sa->get_state(this->ike_sa) == IKE_CONNECTING)
88 { /* only critical during main mode */
89 status = FAILED;
90 }
91 break;
92 }
93 else
94 {
95 DBG1(DBG_IKE, "received %N notify",
96 notify_type_names, notify->get_notify_type(notify));
97 }
98 continue;
99 case DELETE_V1:
100 if (!this->del)
101 {
102 delete = (delete_payload_t*)payload;
103 if (delete->get_protocol_id(delete) == PROTO_IKE)
104 {
105 this->del = (task_t*)isakmp_delete_create(this->ike_sa,
106 FALSE);
107 }
108 else
109 {
110 this->del = (task_t*)quick_delete_create(this->ike_sa,
111 PROTO_NONE, 0, FALSE);
112 }
113 }
114 break;
115 default:
116 continue;
117 }
118 break;
119 }
120 enumerator->destroy(enumerator);
121
122 if (this->del && status == SUCCESS)
123 {
124 return this->del->process(this->del, message);
125 }
126 return status;
127 }
128
129 METHOD(task_t, build_r, status_t,
130 private_informational_t *this, message_t *message)
131 {
132 if (this->del)
133 {
134 return this->del->build(this->del, message);
135 }
136 return FAILED;
137 }
138
139 METHOD(task_t, process_i, status_t,
140 private_informational_t *this, message_t *message)
141 {
142 return FAILED;
143 }
144
145 METHOD(task_t, get_type, task_type_t,
146 private_informational_t *this)
147 {
148 return TASK_INFORMATIONAL;
149 }
150
151 METHOD(task_t, migrate, void,
152 private_informational_t *this, ike_sa_t *ike_sa)
153 {
154 this->ike_sa = ike_sa;
155 }
156
157 METHOD(task_t, destroy, void,
158 private_informational_t *this)
159 {
160 DESTROY_IF(this->notify);
161 DESTROY_IF(this->del);
162 free(this);
163 }
164
165 /*
166 * Described in header.
167 */
168 informational_t *informational_create(ike_sa_t *ike_sa, notify_payload_t *notify)
169 {
170 private_informational_t *this;
171
172 INIT(this,
173 .public = {
174 .task = {
175 .get_type = _get_type,
176 .migrate = _migrate,
177 .destroy = _destroy,
178 },
179 },
180 .ike_sa = ike_sa,
181 .notify = notify,
182 );
183
184 if (notify)
185 {
186 this->public.task.build = _build_i;
187 this->public.task.process = _process_i;
188 }
189 else
190 {
191 this->public.task.build = _build_r;
192 this->public.task.process = _process_r;
193 }
194
195 return &this->public;
196 }