f0ddad02109e7482a0a43a8626ed10cd2ac84448
[strongswan.git] / src / libcharon / sa / 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/tasks/isakmp_delete.h>
20 #include <sa/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 status = FAILED;
88 break;
89 }
90 else
91 {
92 DBG1(DBG_IKE, "received %N notify",
93 notify_type_names, notify->get_notify_type(notify));
94 }
95 continue;
96 case DELETE_V1:
97 if (!this->del)
98 {
99 delete = (delete_payload_t*)payload;
100 if (delete->get_protocol_id(delete) == PROTO_IKE)
101 {
102 this->del = (task_t*)isakmp_delete_create(this->ike_sa,
103 FALSE);
104 }
105 else
106 {
107 this->del = (task_t*)quick_delete_create(this->ike_sa,
108 PROTO_NONE, 0);
109 }
110 }
111 break;
112 default:
113 continue;
114 }
115 break;
116 }
117 enumerator->destroy(enumerator);
118
119 if (this->del && status == SUCCESS)
120 {
121 return this->del->process(this->del, message);
122 }
123 return status;
124 }
125
126 METHOD(task_t, build_r, status_t,
127 private_informational_t *this, message_t *message)
128 {
129 if (this->del)
130 {
131 return this->del->build(this->del, message);
132 }
133 return FAILED;
134 }
135
136 METHOD(task_t, process_i, status_t,
137 private_informational_t *this, message_t *message)
138 {
139 return FAILED;
140 }
141
142 METHOD(task_t, get_type, task_type_t,
143 private_informational_t *this)
144 {
145 return TASK_INFORMATIONAL;
146 }
147
148 METHOD(task_t, migrate, void,
149 private_informational_t *this, ike_sa_t *ike_sa)
150 {
151 this->ike_sa = ike_sa;
152 }
153
154 METHOD(task_t, destroy, void,
155 private_informational_t *this)
156 {
157 DESTROY_IF(this->notify);
158 DESTROY_IF(this->del);
159 free(this);
160 }
161
162 /*
163 * Described in header.
164 */
165 informational_t *informational_create(ike_sa_t *ike_sa, notify_payload_t *notify)
166 {
167 private_informational_t *this;
168
169 INIT(this,
170 .public = {
171 .task = {
172 .get_type = _get_type,
173 .migrate = _migrate,
174 .destroy = _destroy,
175 },
176 },
177 .ike_sa = ike_sa,
178 .notify = notify,
179 );
180
181 if (notify)
182 {
183 this->public.task.build = _build_i;
184 this->public.task.process = _process_i;
185 }
186 else
187 {
188 this->public.task.build = _build_r;
189 this->public.task.process = _process_r;
190 }
191
192 return &this->public;
193 }