ikev1: keep vendor ID task alive during full Main/Aggressive Mode
[strongswan.git] / src / libcharon / sa / ikev1 / tasks / isakmp_vendor.c
1 /*
2 * Copyright (C) 2012-2013 Tobias Brunner
3 * Copyright (C) 2009 Martin Willi
4 * Hochschule fuer Technik Rapperswil
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 */
16
17 /*
18 * Copyright (C) 2012 Volker RĂ¼melin
19 *
20 * Permission is hereby granted, free of charge, to any person obtaining a copy
21 * of this software and associated documentation files (the "Software"), to deal
22 * in the Software without restriction, including without limitation the rights
23 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
24 * copies of the Software, and to permit persons to whom the Software is
25 * furnished to do so, subject to the following conditions:
26 *
27 * The above copyright notice and this permission notice shall be included in
28 * all copies or substantial portions of the Software.
29 *
30 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
31 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
32 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
33 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
34 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
35 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
36 * THE SOFTWARE.
37 */
38
39 #include "isakmp_vendor.h"
40
41 #include <daemon.h>
42 #include <encoding/payloads/vendor_id_payload.h>
43
44 typedef struct private_isakmp_vendor_t private_isakmp_vendor_t;
45
46 /**
47 * Private data of an isakmp_vendor_t object.
48 */
49 struct private_isakmp_vendor_t {
50
51 /**
52 * Public isakmp_vendor_t interface.
53 */
54 isakmp_vendor_t public;
55
56 /**
57 * Associated IKE_SA
58 */
59 ike_sa_t *ike_sa;
60
61 /**
62 * Are we the inititator of this task
63 */
64 bool initiator;
65
66 /**
67 * Index of best nat traversal VID found
68 */
69 int best_natt_ext;
70
71 /**
72 * Number of times we have been invoked
73 */
74 int count;
75 };
76
77 /**
78 * IKEv1 Vendor ID database
79 */
80 static struct {
81 /* Description */
82 char *desc;
83 /* extension flag negotiated with vendor ID, if any */
84 ike_extension_t extension;
85 /* send yourself? */
86 bool send;
87 /* length of vendor ID string */
88 int len;
89 /* vendor ID string */
90 char *id;
91 } vendor_ids[] = {
92
93 /* strongSwan MD5("strongSwan") */
94 { "strongSwan", EXT_STRONGSWAN, FALSE, 16,
95 "\x88\x2f\xe5\x6d\x6f\xd2\x0d\xbc\x22\x51\x61\x3b\x2e\xbe\x5b\xeb"},
96
97 /* XAuth, MD5("draft-ietf-ipsra-isakmp-xauth-06.txt") */
98 { "XAuth", EXT_XAUTH, TRUE, 8,
99 "\x09\x00\x26\x89\xdf\xd6\xb7\x12"},
100
101 /* Dead peer detection, RFC 3706 */
102 { "DPD", EXT_DPD, TRUE, 16,
103 "\xaf\xca\xd7\x13\x68\xa1\xf1\xc9\x6b\x86\x96\xfc\x77\x57\x01\x00"},
104
105 { "Cisco Unity", EXT_CISCO_UNITY, FALSE, 16,
106 "\x12\xf5\xf2\x8c\x45\x71\x68\xa9\x70\x2d\x9f\xe2\x74\xcc\x01\x00"},
107
108 /* Proprietary IKE fragmentation extension. Capabilities are handled
109 * specially on receipt of this VID. */
110 { "FRAGMENTATION", EXT_IKE_FRAGMENTATION, FALSE, 20,
111 "\x40\x48\xb7\xd5\x6e\xbc\xe8\x85\x25\xe7\xde\x7f\x00\xd6\xc2\xd3\x80\x00\x00\x00"},
112
113 }, vendor_natt_ids[] = {
114
115 /* NAT-Traversal VIDs ordered by preference */
116
117 /* NAT-Traversal, MD5("RFC 3947") */
118 { "NAT-T (RFC 3947)", EXT_NATT, TRUE, 16,
119 "\x4a\x13\x1c\x81\x07\x03\x58\x45\x5c\x57\x28\xf2\x0e\x95\x45\x2f"},
120
121 { "draft-ietf-ipsec-nat-t-ike-03", EXT_NATT | EXT_NATT_DRAFT_02_03,
122 FALSE, 16,
123 "\x7d\x94\x19\xa6\x53\x10\xca\x6f\x2c\x17\x9d\x92\x15\x52\x9d\x56"},
124
125 { "draft-ietf-ipsec-nat-t-ike-02", EXT_NATT | EXT_NATT_DRAFT_02_03,
126 FALSE, 16,
127 "\xcd\x60\x46\x43\x35\xdf\x21\xf8\x7c\xfd\xb2\xfc\x68\xb6\xa4\x48"},
128
129 { "draft-ietf-ipsec-nat-t-ike-02\\n", EXT_NATT | EXT_NATT_DRAFT_02_03,
130 TRUE, 16,
131 "\x90\xcb\x80\x91\x3e\xbb\x69\x6e\x08\x63\x81\xb5\xec\x42\x7b\x1f"},
132
133 { "draft-ietf-ipsec-nat-t-ike-08", 0, FALSE, 16,
134 "\x8f\x8d\x83\x82\x6d\x24\x6b\x6f\xc7\xa8\xa6\xa4\x28\xc1\x1d\xe8"},
135
136 { "draft-ietf-ipsec-nat-t-ike-07", 0, FALSE, 16,
137 "\x43\x9b\x59\xf8\xba\x67\x6c\x4c\x77\x37\xae\x22\xea\xb8\xf5\x82"},
138
139 { "draft-ietf-ipsec-nat-t-ike-06", 0, FALSE, 16,
140 "\x4d\x1e\x0e\x13\x6d\xea\xfa\x34\xc4\xf3\xea\x9f\x02\xec\x72\x85"},
141
142 { "draft-ietf-ipsec-nat-t-ike-05", 0, FALSE, 16,
143 "\x80\xd0\xbb\x3d\xef\x54\x56\x5e\xe8\x46\x45\xd4\xc8\x5c\xe3\xee"},
144
145 { "draft-ietf-ipsec-nat-t-ike-04", 0, FALSE, 16,
146 "\x99\x09\xb6\x4e\xed\x93\x7c\x65\x73\xde\x52\xac\xe9\x52\xfa\x6b"},
147
148 { "draft-ietf-ipsec-nat-t-ike-00", 0, FALSE, 16,
149 "\x44\x85\x15\x2d\x18\xb6\xbb\xcd\x0b\xe8\xa8\x46\x95\x79\xdd\xcc"},
150
151 { "draft-ietf-ipsec-nat-t-ike", 0, FALSE, 16,
152 "\x4d\xf3\x79\x28\xe9\xfc\x4f\xd1\xb3\x26\x21\x70\xd5\x15\xc6\x62"},
153
154 { "draft-stenberg-ipsec-nat-traversal-02", 0, FALSE, 16,
155 "\x61\x05\xc4\x22\xe7\x68\x47\xe4\x3f\x96\x84\x80\x12\x92\xae\xcd"},
156
157 { "draft-stenberg-ipsec-nat-traversal-01", 0, FALSE, 16,
158 "\x27\xba\xb5\xdc\x01\xea\x07\x60\xea\x4e\x31\x90\xac\x27\xc0\xd0"},
159
160 };
161
162 /**
163 * According to racoon 0x80000000 seems to indicate support for fragmentation
164 * of Aggressive and Main mode messages. 0x40000000 seems to indicate support
165 * for fragmentation of base ISAKMP messages (Cisco adds that and thus sends
166 * 0xc0000000)
167 */
168 static const u_int32_t fragmentation_ike = 0x80000000;
169
170 /**
171 * Check if the given vendor ID indicate support for fragmentation
172 */
173 static bool fragmentation_supported(chunk_t data, int i)
174 {
175 if (vendor_ids[i].extension == EXT_IKE_FRAGMENTATION &&
176 data.len == 20 && memeq(data.ptr, vendor_ids[i].id, 16))
177 {
178 return untoh32(&data.ptr[16]) & fragmentation_ike;
179 }
180 return FALSE;
181 }
182
183 /**
184 * Add supported vendor ID payloads
185 */
186 static void build(private_isakmp_vendor_t *this, message_t *message)
187 {
188 vendor_id_payload_t *vid_payload;
189 bool strongswan, cisco_unity, fragmentation;
190 ike_cfg_t *ike_cfg;
191 int i;
192
193 strongswan = lib->settings->get_bool(lib->settings,
194 "%s.send_vendor_id", FALSE, charon->name);
195 cisco_unity = lib->settings->get_bool(lib->settings,
196 "%s.cisco_unity", FALSE, charon->name);
197 ike_cfg = this->ike_sa->get_ike_cfg(this->ike_sa);
198 fragmentation = ike_cfg->fragmentation(ike_cfg) != FRAGMENTATION_NO;
199 if (!this->initiator && fragmentation)
200 {
201 fragmentation = this->ike_sa->supports_extension(this->ike_sa,
202 EXT_IKE_FRAGMENTATION);
203 }
204 for (i = 0; i < countof(vendor_ids); i++)
205 {
206 if (vendor_ids[i].send ||
207 (vendor_ids[i].extension == EXT_STRONGSWAN && strongswan) ||
208 (vendor_ids[i].extension == EXT_CISCO_UNITY && cisco_unity) ||
209 (vendor_ids[i].extension == EXT_IKE_FRAGMENTATION && fragmentation))
210 {
211 DBG2(DBG_IKE, "sending %s vendor ID", vendor_ids[i].desc);
212 vid_payload = vendor_id_payload_create_data(VENDOR_ID_V1,
213 chunk_clone(chunk_create(vendor_ids[i].id, vendor_ids[i].len)));
214 message->add_payload(message, &vid_payload->payload_interface);
215 }
216 }
217 for (i = 0; i < countof(vendor_natt_ids); i++)
218 {
219 if ((this->initiator && vendor_natt_ids[i].send) ||
220 this->best_natt_ext == i)
221 {
222 DBG2(DBG_IKE, "sending %s vendor ID", vendor_natt_ids[i].desc);
223 vid_payload = vendor_id_payload_create_data(VENDOR_ID_V1,
224 chunk_clone(chunk_create(vendor_natt_ids[i].id,
225 vendor_natt_ids[i].len)));
226 message->add_payload(message, &vid_payload->payload_interface);
227 }
228 }
229 }
230
231 /**
232 * Process vendor ID payloads
233 */
234 static void process(private_isakmp_vendor_t *this, message_t *message)
235 {
236 enumerator_t *enumerator;
237 payload_t *payload;
238 int i;
239
240 enumerator = message->create_payload_enumerator(message);
241 while (enumerator->enumerate(enumerator, &payload))
242 {
243 if (payload->get_type(payload) == VENDOR_ID_V1)
244 {
245 vendor_id_payload_t *vid;
246 bool found = FALSE;
247 chunk_t data;
248
249 vid = (vendor_id_payload_t*)payload;
250 data = vid->get_data(vid);
251
252 for (i = 0; i < countof(vendor_ids); i++)
253 {
254 if (chunk_equals(data, chunk_create(vendor_ids[i].id,
255 vendor_ids[i].len)) ||
256 fragmentation_supported(data, i))
257 {
258 DBG1(DBG_IKE, "received %s vendor ID", vendor_ids[i].desc);
259 if (vendor_ids[i].extension)
260 {
261 this->ike_sa->enable_extension(this->ike_sa,
262 vendor_ids[i].extension);
263 }
264 found = TRUE;
265 break;
266 }
267 }
268 if (!found)
269 {
270 for (i = 0; i < countof(vendor_natt_ids); i++)
271 {
272 if (chunk_equals(data, chunk_create(vendor_natt_ids[i].id,
273 vendor_natt_ids[i].len)))
274 {
275 DBG1(DBG_IKE, "received %s vendor ID",
276 vendor_natt_ids[i].desc);
277 if (vendor_natt_ids[i].extension &&
278 (i < this->best_natt_ext || this->best_natt_ext < 0))
279 {
280 this->best_natt_ext = i;
281 }
282 found = TRUE;
283 break;
284 }
285 }
286 }
287 if (!found)
288 {
289 DBG1(DBG_ENC, "received unknown vendor ID: %#B", &data);
290 }
291 }
292 }
293 enumerator->destroy(enumerator);
294
295 if (this->best_natt_ext >= 0)
296 {
297 this->ike_sa->enable_extension(this->ike_sa,
298 vendor_natt_ids[this->best_natt_ext].extension);
299 }
300 }
301
302 METHOD(task_t, build_i, status_t,
303 private_isakmp_vendor_t *this, message_t *message)
304 {
305 if (this->count++ == 0)
306 {
307 build(this, message);
308 }
309 if (message->get_exchange_type(message) == AGGRESSIVE && this->count > 1)
310 {
311 return SUCCESS;
312 }
313 return NEED_MORE;
314 }
315
316 METHOD(task_t, process_r, status_t,
317 private_isakmp_vendor_t *this, message_t *message)
318 {
319 this->count++;
320 process(this, message);
321 if (message->get_exchange_type(message) == AGGRESSIVE && this->count > 1)
322 {
323 return SUCCESS;
324 }
325 return NEED_MORE;
326 }
327
328 METHOD(task_t, build_r, status_t,
329 private_isakmp_vendor_t *this, message_t *message)
330 {
331 if (this->count == 1)
332 {
333 build(this, message);
334 }
335 if (message->get_exchange_type(message) == ID_PROT && this->count > 2)
336 {
337 return SUCCESS;
338 }
339 return NEED_MORE;
340 }
341
342 METHOD(task_t, process_i, status_t,
343 private_isakmp_vendor_t *this, message_t *message)
344 {
345 process(this, message);
346 if (message->get_exchange_type(message) == ID_PROT && this->count > 2)
347 {
348 return SUCCESS;
349 }
350 return NEED_MORE;
351 }
352
353 METHOD(task_t, migrate, void,
354 private_isakmp_vendor_t *this, ike_sa_t *ike_sa)
355 {
356 this->ike_sa = ike_sa;
357 this->count = 0;
358 }
359
360 METHOD(task_t, get_type, task_type_t,
361 private_isakmp_vendor_t *this)
362 {
363 return TASK_ISAKMP_VENDOR;
364 }
365
366 METHOD(task_t, destroy, void,
367 private_isakmp_vendor_t *this)
368 {
369 free(this);
370 }
371
372 /**
373 * See header
374 */
375 isakmp_vendor_t *isakmp_vendor_create(ike_sa_t *ike_sa, bool initiator)
376 {
377 private_isakmp_vendor_t *this;
378
379 INIT(this,
380 .public = {
381 .task = {
382 .migrate = _migrate,
383 .get_type = _get_type,
384 .destroy = _destroy,
385 },
386 },
387 .initiator = initiator,
388 .ike_sa = ike_sa,
389 .best_natt_ext = -1,
390 );
391
392 if (initiator)
393 {
394 this->public.task.build = _build_i;
395 this->public.task.process = _process_i;
396 }
397 else
398 {
399 this->public.task.build = _build_r;
400 this->public.task.process = _process_r;
401 }
402
403 return &this->public;
404 }