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