2fb4c6935ab1d9cef9d8a2d470c8f0f24ab06d16
[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 METHOD(task_t, build_i, status_t,
209 private_main_mode_t *this, message_t *message)
210 {
211 switch (this->state)
212 {
213 case MM_INIT:
214 {
215 sa_payload_t *sa_payload;
216 linked_list_t *proposals;
217 packet_t *packet;
218
219 DBG0(DBG_IKE, "initiating Main Mode IKE_SA %s[%d] to %H",
220 this->ike_sa->get_name(this->ike_sa),
221 this->ike_sa->get_unique_id(this->ike_sa),
222 this->ike_sa->get_other_host(this->ike_sa));
223 this->ike_sa->set_state(this->ike_sa, IKE_CONNECTING);
224
225 this->ike_cfg = this->ike_sa->get_ike_cfg(this->ike_sa);
226 this->peer_cfg = this->ike_sa->get_peer_cfg(this->ike_sa);
227 this->peer_cfg->get_ref(this->peer_cfg);
228
229 this->method = this->ph1->get_auth_method(this->ph1, this->peer_cfg);
230 if (this->method == AUTH_NONE)
231 {
232 DBG1(DBG_CFG, "configuration uses unsupported authentication");
233 return FAILED;
234 }
235 this->lifetime = this->peer_cfg->get_reauth_time(this->peer_cfg,
236 FALSE);
237 if (!this->lifetime)
238 { /* fall back to rekey time of no rekey time configured */
239 this->lifetime = this->peer_cfg->get_rekey_time(this->peer_cfg,
240 FALSE);
241 }
242 this->lifetime += this->peer_cfg->get_over_time(this->peer_cfg);
243 proposals = this->ike_cfg->get_proposals(this->ike_cfg);
244 sa_payload = sa_payload_create_from_proposals_v1(proposals,
245 this->lifetime, 0, this->method, MODE_NONE,
246 ENCAP_NONE, 0);
247 proposals->destroy_offset(proposals, offsetof(proposal_t, destroy));
248
249 message->add_payload(message, &sa_payload->payload_interface);
250
251 /* pregenerate message to store SA payload */
252 if (this->ike_sa->generate_message(this->ike_sa, message,
253 &packet) != SUCCESS)
254 {
255 DBG1(DBG_IKE, "pregenerating SA payload failed");
256 return FAILED;
257 }
258 packet->destroy(packet);
259 if (!this->ph1->save_sa_payload(this->ph1, message))
260 {
261 return FAILED;
262 }
263
264 this->state = MM_SA;
265 return NEED_MORE;
266 }
267 case MM_SA:
268 {
269 u_int16_t group;
270
271 if (!this->ph1->create_hasher(this->ph1))
272 {
273 return send_notify(this, NO_PROPOSAL_CHOSEN);
274 }
275 if (!this->proposal->get_algorithm(this->proposal,
276 DIFFIE_HELLMAN_GROUP, &group, NULL))
277 {
278 DBG1(DBG_IKE, "DH group selection failed");
279 return send_notify(this, NO_PROPOSAL_CHOSEN);
280 }
281 if (!this->ph1->create_dh(this->ph1, group))
282 {
283 DBG1(DBG_IKE, "negotiated DH group not supported");
284 return send_notify(this, INVALID_KEY_INFORMATION);
285 }
286 if (!this->ph1->add_nonce_ke(this->ph1, message))
287 {
288 return send_notify(this, INVALID_KEY_INFORMATION);
289 }
290 this->state = MM_KE;
291 return NEED_MORE;
292 }
293 case MM_KE:
294 {
295 id_payload_t *id_payload;
296 identification_t *id;
297
298 id = this->ph1->get_id(this->ph1, this->peer_cfg, TRUE);
299 if (!id)
300 {
301 DBG1(DBG_CFG, "own identity not known");
302 return send_notify(this, INVALID_ID_INFORMATION);
303 }
304 this->ike_sa->set_my_id(this->ike_sa, id->clone(id));
305 id_payload = id_payload_create_from_identification(PLV1_ID, id);
306 message->add_payload(message, &id_payload->payload_interface);
307
308 if (!this->ph1->build_auth(this->ph1, this->method, message,
309 id_payload->get_encoded(id_payload)))
310 {
311 return send_notify(this, AUTHENTICATION_FAILED);
312 }
313
314 this->state = MM_AUTH;
315 return NEED_MORE;
316 }
317 default:
318 return FAILED;
319 }
320 }
321
322 METHOD(task_t, process_r, status_t,
323 private_main_mode_t *this, message_t *message)
324 {
325 switch (this->state)
326 {
327 case MM_INIT:
328 {
329 linked_list_t *list;
330 sa_payload_t *sa_payload;
331 bool private;
332
333 this->ike_cfg = this->ike_sa->get_ike_cfg(this->ike_sa);
334 DBG0(DBG_IKE, "%H is initiating a Main Mode IKE_SA",
335 message->get_source(message));
336 this->ike_sa->set_state(this->ike_sa, IKE_CONNECTING);
337
338 this->ike_sa->update_hosts(this->ike_sa,
339 message->get_destination(message),
340 message->get_source(message), TRUE);
341
342 sa_payload = (sa_payload_t*)message->get_payload(message,
343 PLV1_SECURITY_ASSOCIATION);
344 if (!sa_payload)
345 {
346 DBG1(DBG_IKE, "SA payload missing");
347 return send_notify(this, INVALID_PAYLOAD_TYPE);
348 }
349 if (!this->ph1->save_sa_payload(this->ph1, message))
350 {
351 return send_notify(this, INVALID_PAYLOAD_TYPE);
352 }
353
354 list = sa_payload->get_proposals(sa_payload);
355 private = this->ike_sa->supports_extension(this->ike_sa,
356 EXT_STRONGSWAN);
357 this->proposal = this->ike_cfg->select_proposal(this->ike_cfg,
358 list, private);
359 list->destroy_offset(list, offsetof(proposal_t, destroy));
360 if (!this->proposal)
361 {
362 DBG1(DBG_IKE, "no proposal found");
363 return send_notify(this, NO_PROPOSAL_CHOSEN);
364 }
365 this->ike_sa->set_proposal(this->ike_sa, this->proposal);
366
367 this->method = sa_payload->get_auth_method(sa_payload);
368 this->lifetime = sa_payload->get_lifetime(sa_payload);
369
370 this->state = MM_SA;
371 return NEED_MORE;
372 }
373 case MM_SA:
374 {
375 u_int16_t group;
376
377 if (!this->ph1->create_hasher(this->ph1))
378 {
379 return send_notify(this, INVALID_KEY_INFORMATION);
380 }
381 if (!this->proposal->get_algorithm(this->proposal,
382 DIFFIE_HELLMAN_GROUP, &group, NULL))
383 {
384 DBG1(DBG_IKE, "DH group selection failed");
385 return send_notify(this, INVALID_KEY_INFORMATION);
386 }
387 if (!this->ph1->create_dh(this->ph1, group))
388 {
389 DBG1(DBG_IKE, "negotiated DH group not supported");
390 return send_notify(this, INVALID_KEY_INFORMATION);
391 }
392 if (!this->ph1->get_nonce_ke(this->ph1, message))
393 {
394 return send_notify(this, INVALID_PAYLOAD_TYPE);
395 }
396 this->state = MM_KE;
397 return NEED_MORE;
398 }
399 case MM_KE:
400 {
401 id_payload_t *id_payload;
402 identification_t *id;
403
404 id_payload = (id_payload_t*)message->get_payload(message, PLV1_ID);
405 if (!id_payload)
406 {
407 DBG1(DBG_IKE, "IDii payload missing");
408 return send_notify(this, INVALID_PAYLOAD_TYPE);
409 }
410 id = id_payload->get_identification(id_payload);
411 this->ike_sa->set_other_id(this->ike_sa, id);
412
413 while (TRUE)
414 {
415 DESTROY_IF(this->peer_cfg);
416 this->peer_cfg = this->ph1->select_config(this->ph1,
417 this->method, FALSE, id);
418 if (!this->peer_cfg)
419 {
420 return send_notify(this, AUTHENTICATION_FAILED);
421 }
422 this->ike_sa->set_peer_cfg(this->ike_sa, this->peer_cfg);
423
424 if (this->ph1->verify_auth(this->ph1, this->method, message,
425 id_payload->get_encoded(id_payload)))
426 {
427 break;
428 }
429 }
430
431 if (!charon->bus->authorize(charon->bus, FALSE))
432 {
433 DBG1(DBG_IKE, "Main Mode authorization hook forbids IKE_SA, "
434 "cancelling");
435 return send_notify(this, AUTHENTICATION_FAILED);
436 }
437
438 this->state = MM_AUTH;
439 if (has_notify_errors(this, message))
440 {
441 return FAILED;
442 }
443 return NEED_MORE;
444 }
445 default:
446 return FAILED;
447 }
448 }
449
450 METHOD(task_t, build_r, status_t,
451 private_main_mode_t *this, message_t *message)
452 {
453 switch (this->state)
454 {
455 case MM_SA:
456 {
457 sa_payload_t *sa_payload;
458
459 sa_payload = sa_payload_create_from_proposal_v1(this->proposal,
460 this->lifetime, 0, this->method, MODE_NONE,
461 ENCAP_NONE, 0);
462 message->add_payload(message, &sa_payload->payload_interface);
463
464 return NEED_MORE;
465 }
466 case MM_KE:
467 {
468 if (!this->ph1->add_nonce_ke(this->ph1, message))
469 {
470 return send_notify(this, INVALID_KEY_INFORMATION);
471 }
472 if (!this->ph1->derive_keys(this->ph1, this->peer_cfg, this->method))
473 {
474 return send_notify(this, INVALID_KEY_INFORMATION);
475 }
476 return NEED_MORE;
477 }
478 case MM_AUTH:
479 {
480 id_payload_t *id_payload;
481 identification_t *id;
482 adopt_children_job_t *job = NULL;
483 xauth_t *xauth = NULL;
484
485 id = this->ph1->get_id(this->ph1, this->peer_cfg, TRUE);
486 if (!id)
487 {
488 DBG1(DBG_CFG, "own identity not known");
489 return send_notify(this, INVALID_ID_INFORMATION);
490 }
491 this->ike_sa->set_my_id(this->ike_sa, id->clone(id));
492
493 id_payload = id_payload_create_from_identification(PLV1_ID, id);
494 message->add_payload(message, &id_payload->payload_interface);
495
496 if (!this->ph1->build_auth(this->ph1, this->method, message,
497 id_payload->get_encoded(id_payload)))
498 {
499 return send_notify(this, AUTHENTICATION_FAILED);
500 }
501
502 switch (this->method)
503 {
504 case AUTH_XAUTH_INIT_PSK:
505 case AUTH_XAUTH_INIT_RSA:
506 case AUTH_HYBRID_INIT_RSA:
507 xauth = xauth_create(this->ike_sa, TRUE);
508 this->ike_sa->queue_task(this->ike_sa, (task_t*)xauth);
509 break;
510 case AUTH_XAUTH_RESP_PSK:
511 case AUTH_XAUTH_RESP_RSA:
512 case AUTH_HYBRID_RESP_RSA:
513 /* wait for XAUTH request */
514 break;
515 default:
516 if (charon->ike_sa_manager->check_uniqueness(
517 charon->ike_sa_manager, this->ike_sa, FALSE))
518 {
519 DBG1(DBG_IKE, "cancelling Main Mode due to uniqueness "
520 "policy");
521 return send_notify(this, AUTHENTICATION_FAILED);
522 }
523 if (!establish(this))
524 {
525 return send_notify(this, AUTHENTICATION_FAILED);
526 }
527 job = adopt_children_job_create(
528 this->ike_sa->get_id(this->ike_sa));
529 break;
530 }
531 if (this->ph1->has_virtual_ip(this->ph1, this->peer_cfg))
532 {
533 if (this->peer_cfg->use_pull_mode(this->peer_cfg))
534 {
535 this->ike_sa->queue_task(this->ike_sa,
536 (task_t*)mode_config_create(this->ike_sa, TRUE, TRUE));
537 }
538 }
539 else if (this->ph1->has_pool(this->ph1, this->peer_cfg))
540 {
541 if (!this->peer_cfg->use_pull_mode(this->peer_cfg))
542 {
543 if (job)
544 {
545 job->queue_task(job, (task_t*)
546 mode_config_create(this->ike_sa, TRUE, FALSE));
547 }
548 else if (xauth)
549 {
550 xauth->queue_mode_config_push(xauth);
551 }
552 else
553 {
554 this->ike_sa->queue_task(this->ike_sa, (task_t*)
555 mode_config_create(this->ike_sa, TRUE, FALSE));
556 }
557 }
558 }
559 if (job)
560 {
561 lib->processor->queue_job(lib->processor, (job_t*)job);
562 }
563 return SUCCESS;
564 }
565 default:
566 return FAILED;
567 }
568 }
569
570 /**
571 * Schedule a timeout for the IKE_SA should it not establish
572 */
573 static void schedule_timeout(ike_sa_t *ike_sa)
574 {
575 job_t *job;
576
577 job = (job_t*)delete_ike_sa_job_create(ike_sa->get_id(ike_sa), FALSE);
578 lib->scheduler->schedule_job(lib->scheduler, job, HALF_OPEN_IKE_SA_TIMEOUT);
579 }
580
581 METHOD(task_t, process_i, status_t,
582 private_main_mode_t *this, message_t *message)
583 {
584 switch (this->state)
585 {
586 case MM_SA:
587 {
588 linked_list_t *list;
589 sa_payload_t *sa_payload;
590 auth_method_t method;
591 u_int32_t lifetime;
592 bool private;
593
594 sa_payload = (sa_payload_t*)message->get_payload(message,
595 PLV1_SECURITY_ASSOCIATION);
596 if (!sa_payload)
597 {
598 DBG1(DBG_IKE, "SA payload missing");
599 return send_notify(this, INVALID_PAYLOAD_TYPE);
600 }
601 list = sa_payload->get_proposals(sa_payload);
602 private = this->ike_sa->supports_extension(this->ike_sa,
603 EXT_STRONGSWAN);
604 this->proposal = this->ike_cfg->select_proposal(this->ike_cfg,
605 list, private);
606 list->destroy_offset(list, offsetof(proposal_t, destroy));
607 if (!this->proposal)
608 {
609 DBG1(DBG_IKE, "no proposal found");
610 return send_notify(this, NO_PROPOSAL_CHOSEN);
611 }
612 this->ike_sa->set_proposal(this->ike_sa, this->proposal);
613
614 lifetime = sa_payload->get_lifetime(sa_payload);
615 if (lifetime != this->lifetime)
616 {
617 DBG1(DBG_IKE, "received lifetime %us does not match configured "
618 "lifetime %us", lifetime, this->lifetime);
619 }
620 this->lifetime = lifetime;
621 method = sa_payload->get_auth_method(sa_payload);
622 if (method != this->method)
623 {
624 DBG1(DBG_IKE, "received %N authentication, but configured %N, "
625 "continue with configured", auth_method_names, method,
626 auth_method_names, this->method);
627 }
628 return NEED_MORE;
629 }
630 case MM_KE:
631 {
632 if (!this->ph1->get_nonce_ke(this->ph1, message))
633 {
634 return send_notify(this, INVALID_PAYLOAD_TYPE);
635 }
636 if (!this->ph1->derive_keys(this->ph1, this->peer_cfg, this->method))
637 {
638 return send_notify(this, INVALID_KEY_INFORMATION);
639 }
640 return NEED_MORE;
641 }
642 case MM_AUTH:
643 {
644 id_payload_t *id_payload;
645 identification_t *id, *cid;
646
647 id_payload = (id_payload_t*)message->get_payload(message, PLV1_ID);
648 if (!id_payload)
649 {
650 DBG1(DBG_IKE, "IDir payload missing");
651 return send_delete(this);
652 }
653 id = id_payload->get_identification(id_payload);
654 cid = this->ph1->get_id(this->ph1, this->peer_cfg, FALSE);
655 if (cid && !id->matches(id, cid))
656 {
657 DBG1(DBG_IKE, "IDir '%Y' does not match to '%Y'", id, cid);
658 id->destroy(id);
659 return send_delete(this);
660 }
661 this->ike_sa->set_other_id(this->ike_sa, id);
662
663 if (!this->ph1->verify_auth(this->ph1, this->method, message,
664 id_payload->get_encoded(id_payload)))
665 {
666 return send_delete(this);
667 }
668 if (!charon->bus->authorize(charon->bus, FALSE))
669 {
670 DBG1(DBG_IKE, "Main Mode authorization hook forbids IKE_SA, "
671 "cancelling");
672 return send_delete(this);
673 }
674
675 switch (this->method)
676 {
677 case AUTH_XAUTH_INIT_PSK:
678 case AUTH_XAUTH_INIT_RSA:
679 case AUTH_HYBRID_INIT_RSA:
680 /* wait for XAUTH request */
681 schedule_timeout(this->ike_sa);
682 break;
683 case AUTH_XAUTH_RESP_PSK:
684 case AUTH_XAUTH_RESP_RSA:
685 case AUTH_HYBRID_RESP_RSA:
686 this->ike_sa->queue_task(this->ike_sa,
687 (task_t*)xauth_create(this->ike_sa, TRUE));
688 break;
689 default:
690 if (charon->ike_sa_manager->check_uniqueness(
691 charon->ike_sa_manager, this->ike_sa, FALSE))
692 {
693 DBG1(DBG_IKE, "cancelling Main Mode due to uniqueness "
694 "policy");
695 return send_delete(this);
696 }
697 if (!establish(this))
698 {
699 return send_delete(this);
700 }
701 break;
702 }
703 /* check for and prepare mode config push/pull */
704 if (this->ph1->has_virtual_ip(this->ph1, this->peer_cfg))
705 {
706 if (this->peer_cfg->use_pull_mode(this->peer_cfg))
707 {
708 this->ike_sa->queue_task(this->ike_sa,
709 (task_t*)mode_config_create(this->ike_sa, TRUE, TRUE));
710 }
711 else
712 {
713 schedule_timeout(this->ike_sa);
714 }
715 }
716 else if (this->ph1->has_pool(this->ph1, this->peer_cfg))
717 {
718 if (this->peer_cfg->use_pull_mode(this->peer_cfg))
719 {
720 schedule_timeout(this->ike_sa);
721 }
722 else
723 {
724 this->ike_sa->queue_task(this->ike_sa,
725 (task_t*)mode_config_create(this->ike_sa, TRUE, FALSE));
726 }
727 }
728 return SUCCESS;
729 }
730 default:
731 return FAILED;
732 }
733 }
734
735 METHOD(task_t, get_type, task_type_t,
736 private_main_mode_t *this)
737 {
738 return TASK_MAIN_MODE;
739 }
740
741 METHOD(task_t, migrate, void,
742 private_main_mode_t *this, ike_sa_t *ike_sa)
743 {
744 DESTROY_IF(this->peer_cfg);
745 DESTROY_IF(this->proposal);
746 this->ph1->destroy(this->ph1);
747
748 this->ike_sa = ike_sa;
749 this->state = MM_INIT;
750 this->peer_cfg = NULL;
751 this->proposal = NULL;
752 this->ph1 = phase1_create(ike_sa, this->initiator);
753 }
754
755 METHOD(task_t, destroy, void,
756 private_main_mode_t *this)
757 {
758 DESTROY_IF(this->peer_cfg);
759 DESTROY_IF(this->proposal);
760 this->ph1->destroy(this->ph1);
761 free(this);
762 }
763
764 /*
765 * Described in header.
766 */
767 main_mode_t *main_mode_create(ike_sa_t *ike_sa, bool initiator)
768 {
769 private_main_mode_t *this;
770
771 INIT(this,
772 .public = {
773 .task = {
774 .get_type = _get_type,
775 .migrate = _migrate,
776 .destroy = _destroy,
777 },
778 },
779 .ike_sa = ike_sa,
780 .ph1 = phase1_create(ike_sa, initiator),
781 .initiator = initiator,
782 .state = MM_INIT,
783 );
784
785 if (initiator)
786 {
787 this->public.task.build = _build_i;
788 this->public.task.process = _process_i;
789 }
790 else
791 {
792 this->public.task.build = _build_r;
793 this->public.task.process = _process_r;
794 }
795
796 return &this->public;
797 }