ikev1: Send INITIAL_CONTACT notify in Main Mode
[strongswan.git] / src / libcharon / sa / ikev1 / tasks / main_mode.c
1 /*
2 * Copyright (C) 2011-2012 Tobias Brunner
3 * Hochschule fuer Technik Rapperswil
4 *
5 * Copyright (C) 2011 Martin Willi
6 * Copyright (C) 2011 revosec AG
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 * for more details.
17 */
18
19 #include "main_mode.h"
20
21 #include <string.h>
22
23 #include <daemon.h>
24 #include <sa/ikev1/phase1.h>
25 #include <encoding/payloads/sa_payload.h>
26 #include <encoding/payloads/id_payload.h>
27 #include <encoding/payloads/hash_payload.h>
28 #include <sa/ikev1/tasks/xauth.h>
29 #include <sa/ikev1/tasks/mode_config.h>
30 #include <sa/ikev1/tasks/informational.h>
31 #include <sa/ikev1/tasks/isakmp_delete.h>
32 #include <processing/jobs/adopt_children_job.h>
33 #include <processing/jobs/delete_ike_sa_job.h>
34
35 typedef struct private_main_mode_t private_main_mode_t;
36
37 /**
38 * Private members of a main_mode_t task.
39 */
40 struct private_main_mode_t {
41
42 /**
43 * Public methods and task_t interface.
44 */
45 main_mode_t public;
46
47 /**
48 * Assigned IKE_SA.
49 */
50 ike_sa_t *ike_sa;
51
52 /**
53 * Are we the initiator?
54 */
55 bool initiator;
56
57 /**
58 * Common phase 1 helper class
59 */
60 phase1_t *ph1;
61
62 /**
63 * IKE config to establish
64 */
65 ike_cfg_t *ike_cfg;
66
67 /**
68 * Peer config to use
69 */
70 peer_cfg_t *peer_cfg;
71
72 /**
73 * selected IKE proposal
74 */
75 proposal_t *proposal;
76
77 /**
78 * Negotiated SA lifetime
79 */
80 u_int32_t lifetime;
81
82 /**
83 * Negotiated authentication method
84 */
85 auth_method_t method;
86
87 /** states of main mode */
88 enum {
89 MM_INIT,
90 MM_SA,
91 MM_KE,
92 MM_AUTH,
93 } state;
94 };
95
96 /**
97 * Set IKE_SA to established state
98 */
99 static bool establish(private_main_mode_t *this)
100 {
101 if (!charon->bus->authorize(charon->bus, TRUE))
102 {
103 DBG1(DBG_IKE, "final authorization hook forbids IKE_SA, cancelling");
104 return FALSE;
105 }
106
107 DBG0(DBG_IKE, "IKE_SA %s[%d] established between %H[%Y]...%H[%Y]",
108 this->ike_sa->get_name(this->ike_sa),
109 this->ike_sa->get_unique_id(this->ike_sa),
110 this->ike_sa->get_my_host(this->ike_sa),
111 this->ike_sa->get_my_id(this->ike_sa),
112 this->ike_sa->get_other_host(this->ike_sa),
113 this->ike_sa->get_other_id(this->ike_sa));
114
115 this->ike_sa->set_state(this->ike_sa, IKE_ESTABLISHED);
116 charon->bus->ike_updown(charon->bus, this->ike_sa, TRUE);
117
118 return TRUE;
119 }
120
121 /**
122 * Check for notify errors, return TRUE if error found
123 */
124 static bool has_notify_errors(private_main_mode_t *this, message_t *message)
125 {
126 enumerator_t *enumerator;
127 payload_t *payload;
128 bool err = FALSE;
129
130 enumerator = message->create_payload_enumerator(message);
131 while (enumerator->enumerate(enumerator, &payload))
132 {
133 if (payload->get_type(payload) == PLV1_NOTIFY)
134 {
135 notify_payload_t *notify;
136 notify_type_t type;
137
138 notify = (notify_payload_t*)payload;
139 type = notify->get_notify_type(notify);
140 if (type < 16384)
141 {
142 DBG1(DBG_IKE, "received %N error notify",
143 notify_type_names, type);
144 err = TRUE;
145 }
146 else if (type == INITIAL_CONTACT_IKEV1)
147 {
148 if (!this->initiator && this->state == MM_AUTH)
149 {
150 /* If authenticated and received INITIAL_CONTACT,
151 * delete any existing IKE_SAs with that peer.
152 * The delete takes place when the SA is checked in due
153 * to other id not known until the 3rd message.*/
154 this->ike_sa->set_condition(this->ike_sa,
155 COND_INIT_CONTACT_SEEN, TRUE);
156 }
157 }
158 else
159 {
160 DBG1(DBG_IKE, "received %N notify", notify_type_names, type);
161 }
162 }
163 }
164 enumerator->destroy(enumerator);
165
166 return err;
167 }
168
169 /**
170 * Queue a task sending a notify in an INFORMATIONAL exchange
171 */
172 static status_t send_notify(private_main_mode_t *this, notify_type_t type)
173 {
174 notify_payload_t *notify;
175 ike_sa_id_t *ike_sa_id;
176 u_int64_t spi_i, spi_r;
177 chunk_t spi;
178
179 notify = notify_payload_create_from_protocol_and_type(PLV1_NOTIFY,
180 PROTO_IKE, type);
181 ike_sa_id = this->ike_sa->get_id(this->ike_sa);
182 spi_i = ike_sa_id->get_initiator_spi(ike_sa_id);
183 spi_r = ike_sa_id->get_responder_spi(ike_sa_id);
184 spi = chunk_cata("cc", chunk_from_thing(spi_i), chunk_from_thing(spi_r));
185 notify->set_spi_data(notify, spi);
186
187 this->ike_sa->queue_task(this->ike_sa,
188 (task_t*)informational_create(this->ike_sa, notify));
189 /* cancel all active/passive tasks in favour of informational */
190 this->ike_sa->flush_queue(this->ike_sa,
191 this->initiator ? TASK_QUEUE_ACTIVE : TASK_QUEUE_PASSIVE);
192 return ALREADY_DONE;
193 }
194
195 /**
196 * Queue a delete task if authentication failed as initiator
197 */
198 static status_t send_delete(private_main_mode_t *this)
199 {
200 this->ike_sa->queue_task(this->ike_sa,
201 (task_t*)isakmp_delete_create(this->ike_sa, TRUE));
202 /* cancel all active tasks in favour of informational */
203 this->ike_sa->flush_queue(this->ike_sa,
204 this->initiator ? TASK_QUEUE_ACTIVE : TASK_QUEUE_PASSIVE);
205 return ALREADY_DONE;
206 }
207
208 /**
209 * Add an INITIAL_CONTACT notify if first contact with peer
210 */
211 static void add_initial_contact(private_main_mode_t *this, message_t *message,
212 identification_t *idi)
213 {
214 identification_t *idr;
215 host_t *host;
216
217 idr = this->ph1->get_id(this->ph1, this->peer_cfg, FALSE);
218 if (idr && !idr->contains_wildcards(idr))
219 {
220 if (this->peer_cfg->get_unique_policy(this->peer_cfg) != UNIQUE_NO &&
221 this->peer_cfg->get_unique_policy(this->peer_cfg) != UNIQUE_NEVER)
222 {
223 host = this->ike_sa->get_other_host(this->ike_sa);
224 if (!charon->ike_sa_manager->has_contact(charon->ike_sa_manager,
225 idi, idr, host->get_family(host)))
226 {
227 message->add_notify(message, FALSE, INITIAL_CONTACT_IKEV1,
228 chunk_empty);
229 }
230 }
231 }
232 }
233
234 METHOD(task_t, build_i, status_t,
235 private_main_mode_t *this, message_t *message)
236 {
237 switch (this->state)
238 {
239 case MM_INIT:
240 {
241 sa_payload_t *sa_payload;
242 linked_list_t *proposals;
243 packet_t *packet;
244
245 DBG0(DBG_IKE, "initiating Main Mode IKE_SA %s[%d] to %H",
246 this->ike_sa->get_name(this->ike_sa),
247 this->ike_sa->get_unique_id(this->ike_sa),
248 this->ike_sa->get_other_host(this->ike_sa));
249 this->ike_sa->set_state(this->ike_sa, IKE_CONNECTING);
250
251 this->ike_cfg = this->ike_sa->get_ike_cfg(this->ike_sa);
252 this->peer_cfg = this->ike_sa->get_peer_cfg(this->ike_sa);
253 this->peer_cfg->get_ref(this->peer_cfg);
254
255 this->method = this->ph1->get_auth_method(this->ph1, this->peer_cfg);
256 if (this->method == AUTH_NONE)
257 {
258 DBG1(DBG_CFG, "configuration uses unsupported authentication");
259 return FAILED;
260 }
261 this->lifetime = this->peer_cfg->get_reauth_time(this->peer_cfg,
262 FALSE);
263 if (!this->lifetime)
264 { /* fall back to rekey time of no rekey time configured */
265 this->lifetime = this->peer_cfg->get_rekey_time(this->peer_cfg,
266 FALSE);
267 }
268 this->lifetime += this->peer_cfg->get_over_time(this->peer_cfg);
269 proposals = this->ike_cfg->get_proposals(this->ike_cfg);
270 sa_payload = sa_payload_create_from_proposals_v1(proposals,
271 this->lifetime, 0, this->method, MODE_NONE,
272 ENCAP_NONE, 0);
273 proposals->destroy_offset(proposals, offsetof(proposal_t, destroy));
274
275 message->add_payload(message, &sa_payload->payload_interface);
276
277 /* pregenerate message to store SA payload */
278 if (this->ike_sa->generate_message(this->ike_sa, message,
279 &packet) != SUCCESS)
280 {
281 DBG1(DBG_IKE, "pregenerating SA payload failed");
282 return FAILED;
283 }
284 packet->destroy(packet);
285 if (!this->ph1->save_sa_payload(this->ph1, message))
286 {
287 return FAILED;
288 }
289
290 this->state = MM_SA;
291 return NEED_MORE;
292 }
293 case MM_SA:
294 {
295 u_int16_t group;
296
297 if (!this->ph1->create_hasher(this->ph1))
298 {
299 return send_notify(this, NO_PROPOSAL_CHOSEN);
300 }
301 if (!this->proposal->get_algorithm(this->proposal,
302 DIFFIE_HELLMAN_GROUP, &group, NULL))
303 {
304 DBG1(DBG_IKE, "DH group selection failed");
305 return send_notify(this, NO_PROPOSAL_CHOSEN);
306 }
307 if (!this->ph1->create_dh(this->ph1, group))
308 {
309 DBG1(DBG_IKE, "negotiated DH group not supported");
310 return send_notify(this, INVALID_KEY_INFORMATION);
311 }
312 if (!this->ph1->add_nonce_ke(this->ph1, message))
313 {
314 return send_notify(this, INVALID_KEY_INFORMATION);
315 }
316 this->state = MM_KE;
317 return NEED_MORE;
318 }
319 case MM_KE:
320 {
321 id_payload_t *id_payload;
322 identification_t *id;
323
324 id = this->ph1->get_id(this->ph1, this->peer_cfg, TRUE);
325 if (!id)
326 {
327 DBG1(DBG_CFG, "own identity not known");
328 return send_notify(this, INVALID_ID_INFORMATION);
329 }
330 this->ike_sa->set_my_id(this->ike_sa, id->clone(id));
331 id_payload = id_payload_create_from_identification(PLV1_ID, id);
332 message->add_payload(message, &id_payload->payload_interface);
333
334 if (!this->ph1->build_auth(this->ph1, this->method, message,
335 id_payload->get_encoded(id_payload)))
336 {
337 return send_notify(this, AUTHENTICATION_FAILED);
338 }
339
340 add_initial_contact(this, message, id);
341
342 this->state = MM_AUTH;
343 return NEED_MORE;
344 }
345 default:
346 return FAILED;
347 }
348 }
349
350 METHOD(task_t, process_r, status_t,
351 private_main_mode_t *this, message_t *message)
352 {
353 switch (this->state)
354 {
355 case MM_INIT:
356 {
357 linked_list_t *list;
358 sa_payload_t *sa_payload;
359 bool private;
360
361 this->ike_cfg = this->ike_sa->get_ike_cfg(this->ike_sa);
362 DBG0(DBG_IKE, "%H is initiating a Main Mode IKE_SA",
363 message->get_source(message));
364 this->ike_sa->set_state(this->ike_sa, IKE_CONNECTING);
365
366 this->ike_sa->update_hosts(this->ike_sa,
367 message->get_destination(message),
368 message->get_source(message), TRUE);
369
370 sa_payload = (sa_payload_t*)message->get_payload(message,
371 PLV1_SECURITY_ASSOCIATION);
372 if (!sa_payload)
373 {
374 DBG1(DBG_IKE, "SA payload missing");
375 return send_notify(this, INVALID_PAYLOAD_TYPE);
376 }
377 if (!this->ph1->save_sa_payload(this->ph1, message))
378 {
379 return send_notify(this, INVALID_PAYLOAD_TYPE);
380 }
381
382 list = sa_payload->get_proposals(sa_payload);
383 private = this->ike_sa->supports_extension(this->ike_sa,
384 EXT_STRONGSWAN);
385 this->proposal = this->ike_cfg->select_proposal(this->ike_cfg,
386 list, private);
387 list->destroy_offset(list, offsetof(proposal_t, destroy));
388 if (!this->proposal)
389 {
390 DBG1(DBG_IKE, "no proposal found");
391 return send_notify(this, NO_PROPOSAL_CHOSEN);
392 }
393 this->ike_sa->set_proposal(this->ike_sa, this->proposal);
394
395 this->method = sa_payload->get_auth_method(sa_payload);
396 this->lifetime = sa_payload->get_lifetime(sa_payload);
397
398 this->state = MM_SA;
399 return NEED_MORE;
400 }
401 case MM_SA:
402 {
403 u_int16_t group;
404
405 if (!this->ph1->create_hasher(this->ph1))
406 {
407 return send_notify(this, INVALID_KEY_INFORMATION);
408 }
409 if (!this->proposal->get_algorithm(this->proposal,
410 DIFFIE_HELLMAN_GROUP, &group, NULL))
411 {
412 DBG1(DBG_IKE, "DH group selection failed");
413 return send_notify(this, INVALID_KEY_INFORMATION);
414 }
415 if (!this->ph1->create_dh(this->ph1, group))
416 {
417 DBG1(DBG_IKE, "negotiated DH group not supported");
418 return send_notify(this, INVALID_KEY_INFORMATION);
419 }
420 if (!this->ph1->get_nonce_ke(this->ph1, message))
421 {
422 return send_notify(this, INVALID_PAYLOAD_TYPE);
423 }
424 this->state = MM_KE;
425 return NEED_MORE;
426 }
427 case MM_KE:
428 {
429 id_payload_t *id_payload;
430 identification_t *id;
431
432 id_payload = (id_payload_t*)message->get_payload(message, PLV1_ID);
433 if (!id_payload)
434 {
435 DBG1(DBG_IKE, "IDii payload missing");
436 return send_notify(this, INVALID_PAYLOAD_TYPE);
437 }
438 id = id_payload->get_identification(id_payload);
439 this->ike_sa->set_other_id(this->ike_sa, id);
440
441 while (TRUE)
442 {
443 DESTROY_IF(this->peer_cfg);
444 this->peer_cfg = this->ph1->select_config(this->ph1,
445 this->method, FALSE, id);
446 if (!this->peer_cfg)
447 {
448 return send_notify(this, AUTHENTICATION_FAILED);
449 }
450 this->ike_sa->set_peer_cfg(this->ike_sa, this->peer_cfg);
451
452 if (this->ph1->verify_auth(this->ph1, this->method, message,
453 id_payload->get_encoded(id_payload)))
454 {
455 break;
456 }
457 }
458
459 if (!charon->bus->authorize(charon->bus, FALSE))
460 {
461 DBG1(DBG_IKE, "Main Mode authorization hook forbids IKE_SA, "
462 "cancelling");
463 return send_notify(this, AUTHENTICATION_FAILED);
464 }
465
466 this->state = MM_AUTH;
467 if (has_notify_errors(this, message))
468 {
469 return FAILED;
470 }
471 return NEED_MORE;
472 }
473 default:
474 return FAILED;
475 }
476 }
477
478 METHOD(task_t, build_r, status_t,
479 private_main_mode_t *this, message_t *message)
480 {
481 switch (this->state)
482 {
483 case MM_SA:
484 {
485 sa_payload_t *sa_payload;
486
487 sa_payload = sa_payload_create_from_proposal_v1(this->proposal,
488 this->lifetime, 0, this->method, MODE_NONE,
489 ENCAP_NONE, 0);
490 message->add_payload(message, &sa_payload->payload_interface);
491
492 return NEED_MORE;
493 }
494 case MM_KE:
495 {
496 if (!this->ph1->add_nonce_ke(this->ph1, message))
497 {
498 return send_notify(this, INVALID_KEY_INFORMATION);
499 }
500 if (!this->ph1->derive_keys(this->ph1, this->peer_cfg, this->method))
501 {
502 return send_notify(this, INVALID_KEY_INFORMATION);
503 }
504 return NEED_MORE;
505 }
506 case MM_AUTH:
507 {
508 id_payload_t *id_payload;
509 identification_t *id;
510 adopt_children_job_t *job = NULL;
511 xauth_t *xauth = NULL;
512
513 id = this->ph1->get_id(this->ph1, this->peer_cfg, TRUE);
514 if (!id)
515 {
516 DBG1(DBG_CFG, "own identity not known");
517 return send_notify(this, INVALID_ID_INFORMATION);
518 }
519 this->ike_sa->set_my_id(this->ike_sa, id->clone(id));
520
521 id_payload = id_payload_create_from_identification(PLV1_ID, id);
522 message->add_payload(message, &id_payload->payload_interface);
523
524 if (!this->ph1->build_auth(this->ph1, this->method, message,
525 id_payload->get_encoded(id_payload)))
526 {
527 return send_notify(this, AUTHENTICATION_FAILED);
528 }
529
530 switch (this->method)
531 {
532 case AUTH_XAUTH_INIT_PSK:
533 case AUTH_XAUTH_INIT_RSA:
534 case AUTH_HYBRID_INIT_RSA:
535 xauth = xauth_create(this->ike_sa, TRUE);
536 this->ike_sa->queue_task(this->ike_sa, (task_t*)xauth);
537 break;
538 case AUTH_XAUTH_RESP_PSK:
539 case AUTH_XAUTH_RESP_RSA:
540 case AUTH_HYBRID_RESP_RSA:
541 /* wait for XAUTH request */
542 break;
543 default:
544 if (charon->ike_sa_manager->check_uniqueness(
545 charon->ike_sa_manager, this->ike_sa, FALSE))
546 {
547 DBG1(DBG_IKE, "cancelling Main Mode due to uniqueness "
548 "policy");
549 return send_notify(this, AUTHENTICATION_FAILED);
550 }
551 if (!establish(this))
552 {
553 return send_notify(this, AUTHENTICATION_FAILED);
554 }
555 job = adopt_children_job_create(
556 this->ike_sa->get_id(this->ike_sa));
557 break;
558 }
559 if (this->ph1->has_virtual_ip(this->ph1, this->peer_cfg))
560 {
561 if (this->peer_cfg->use_pull_mode(this->peer_cfg))
562 {
563 this->ike_sa->queue_task(this->ike_sa,
564 (task_t*)mode_config_create(this->ike_sa, TRUE, TRUE));
565 }
566 }
567 else if (this->ph1->has_pool(this->ph1, this->peer_cfg))
568 {
569 if (!this->peer_cfg->use_pull_mode(this->peer_cfg))
570 {
571 if (job)
572 {
573 job->queue_task(job, (task_t*)
574 mode_config_create(this->ike_sa, TRUE, FALSE));
575 }
576 else if (xauth)
577 {
578 xauth->queue_mode_config_push(xauth);
579 }
580 else
581 {
582 this->ike_sa->queue_task(this->ike_sa, (task_t*)
583 mode_config_create(this->ike_sa, TRUE, FALSE));
584 }
585 }
586 }
587 if (job)
588 {
589 lib->processor->queue_job(lib->processor, (job_t*)job);
590 }
591 return SUCCESS;
592 }
593 default:
594 return FAILED;
595 }
596 }
597
598 /**
599 * Schedule a timeout for the IKE_SA should it not establish
600 */
601 static void schedule_timeout(ike_sa_t *ike_sa)
602 {
603 job_t *job;
604
605 job = (job_t*)delete_ike_sa_job_create(ike_sa->get_id(ike_sa), FALSE);
606 lib->scheduler->schedule_job(lib->scheduler, job, HALF_OPEN_IKE_SA_TIMEOUT);
607 }
608
609 METHOD(task_t, process_i, status_t,
610 private_main_mode_t *this, message_t *message)
611 {
612 switch (this->state)
613 {
614 case MM_SA:
615 {
616 linked_list_t *list;
617 sa_payload_t *sa_payload;
618 auth_method_t method;
619 u_int32_t lifetime;
620 bool private;
621
622 sa_payload = (sa_payload_t*)message->get_payload(message,
623 PLV1_SECURITY_ASSOCIATION);
624 if (!sa_payload)
625 {
626 DBG1(DBG_IKE, "SA payload missing");
627 return send_notify(this, INVALID_PAYLOAD_TYPE);
628 }
629 list = sa_payload->get_proposals(sa_payload);
630 private = this->ike_sa->supports_extension(this->ike_sa,
631 EXT_STRONGSWAN);
632 this->proposal = this->ike_cfg->select_proposal(this->ike_cfg,
633 list, private);
634 list->destroy_offset(list, offsetof(proposal_t, destroy));
635 if (!this->proposal)
636 {
637 DBG1(DBG_IKE, "no proposal found");
638 return send_notify(this, NO_PROPOSAL_CHOSEN);
639 }
640 this->ike_sa->set_proposal(this->ike_sa, this->proposal);
641
642 lifetime = sa_payload->get_lifetime(sa_payload);
643 if (lifetime != this->lifetime)
644 {
645 DBG1(DBG_IKE, "received lifetime %us does not match configured "
646 "lifetime %us", lifetime, this->lifetime);
647 }
648 this->lifetime = lifetime;
649 method = sa_payload->get_auth_method(sa_payload);
650 if (method != this->method)
651 {
652 DBG1(DBG_IKE, "received %N authentication, but configured %N, "
653 "continue with configured", auth_method_names, method,
654 auth_method_names, this->method);
655 }
656 return NEED_MORE;
657 }
658 case MM_KE:
659 {
660 if (!this->ph1->get_nonce_ke(this->ph1, message))
661 {
662 return send_notify(this, INVALID_PAYLOAD_TYPE);
663 }
664 if (!this->ph1->derive_keys(this->ph1, this->peer_cfg, this->method))
665 {
666 return send_notify(this, INVALID_KEY_INFORMATION);
667 }
668 return NEED_MORE;
669 }
670 case MM_AUTH:
671 {
672 id_payload_t *id_payload;
673 identification_t *id, *cid;
674
675 id_payload = (id_payload_t*)message->get_payload(message, PLV1_ID);
676 if (!id_payload)
677 {
678 DBG1(DBG_IKE, "IDir payload missing");
679 return send_delete(this);
680 }
681 id = id_payload->get_identification(id_payload);
682 cid = this->ph1->get_id(this->ph1, this->peer_cfg, FALSE);
683 if (cid && !id->matches(id, cid))
684 {
685 DBG1(DBG_IKE, "IDir '%Y' does not match to '%Y'", id, cid);
686 id->destroy(id);
687 return send_delete(this);
688 }
689 this->ike_sa->set_other_id(this->ike_sa, id);
690
691 if (!this->ph1->verify_auth(this->ph1, this->method, message,
692 id_payload->get_encoded(id_payload)))
693 {
694 return send_delete(this);
695 }
696 if (!charon->bus->authorize(charon->bus, FALSE))
697 {
698 DBG1(DBG_IKE, "Main Mode authorization hook forbids IKE_SA, "
699 "cancelling");
700 return send_delete(this);
701 }
702
703 switch (this->method)
704 {
705 case AUTH_XAUTH_INIT_PSK:
706 case AUTH_XAUTH_INIT_RSA:
707 case AUTH_HYBRID_INIT_RSA:
708 /* wait for XAUTH request */
709 schedule_timeout(this->ike_sa);
710 break;
711 case AUTH_XAUTH_RESP_PSK:
712 case AUTH_XAUTH_RESP_RSA:
713 case AUTH_HYBRID_RESP_RSA:
714 this->ike_sa->queue_task(this->ike_sa,
715 (task_t*)xauth_create(this->ike_sa, TRUE));
716 break;
717 default:
718 if (charon->ike_sa_manager->check_uniqueness(
719 charon->ike_sa_manager, this->ike_sa, FALSE))
720 {
721 DBG1(DBG_IKE, "cancelling Main Mode due to uniqueness "
722 "policy");
723 return send_delete(this);
724 }
725 if (!establish(this))
726 {
727 return send_delete(this);
728 }
729 break;
730 }
731 /* check for and prepare mode config push/pull */
732 if (this->ph1->has_virtual_ip(this->ph1, this->peer_cfg))
733 {
734 if (this->peer_cfg->use_pull_mode(this->peer_cfg))
735 {
736 this->ike_sa->queue_task(this->ike_sa,
737 (task_t*)mode_config_create(this->ike_sa, TRUE, TRUE));
738 }
739 else
740 {
741 schedule_timeout(this->ike_sa);
742 }
743 }
744 else if (this->ph1->has_pool(this->ph1, this->peer_cfg))
745 {
746 if (this->peer_cfg->use_pull_mode(this->peer_cfg))
747 {
748 schedule_timeout(this->ike_sa);
749 }
750 else
751 {
752 this->ike_sa->queue_task(this->ike_sa,
753 (task_t*)mode_config_create(this->ike_sa, TRUE, FALSE));
754 }
755 }
756 return SUCCESS;
757 }
758 default:
759 return FAILED;
760 }
761 }
762
763 METHOD(task_t, get_type, task_type_t,
764 private_main_mode_t *this)
765 {
766 return TASK_MAIN_MODE;
767 }
768
769 METHOD(task_t, migrate, void,
770 private_main_mode_t *this, ike_sa_t *ike_sa)
771 {
772 DESTROY_IF(this->peer_cfg);
773 DESTROY_IF(this->proposal);
774 this->ph1->destroy(this->ph1);
775
776 this->ike_sa = ike_sa;
777 this->state = MM_INIT;
778 this->peer_cfg = NULL;
779 this->proposal = NULL;
780 this->ph1 = phase1_create(ike_sa, this->initiator);
781 }
782
783 METHOD(task_t, destroy, void,
784 private_main_mode_t *this)
785 {
786 DESTROY_IF(this->peer_cfg);
787 DESTROY_IF(this->proposal);
788 this->ph1->destroy(this->ph1);
789 free(this);
790 }
791
792 /*
793 * Described in header.
794 */
795 main_mode_t *main_mode_create(ike_sa_t *ike_sa, bool initiator)
796 {
797 private_main_mode_t *this;
798
799 INIT(this,
800 .public = {
801 .task = {
802 .get_type = _get_type,
803 .migrate = _migrate,
804 .destroy = _destroy,
805 },
806 },
807 .ike_sa = ike_sa,
808 .ph1 = phase1_create(ike_sa, initiator),
809 .initiator = initiator,
810 .state = MM_INIT,
811 );
812
813 if (initiator)
814 {
815 this->public.task.build = _build_i;
816 this->public.task.process = _process_i;
817 }
818 else
819 {
820 this->public.task.build = _build_r;
821 this->public.task.process = _process_r;
822 }
823
824 return &this->public;
825 }