67d1b45f10cf1298f7af52f013fd98c6aef09628
[strongswan.git] / src / libcharon / sa / ikev1 / tasks / quick_mode.c
1 /*
2 * Copyright (C) 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 "quick_mode.h"
20
21 #include <string.h>
22
23 #include <daemon.h>
24 #include <sa/ikev1/keymat_v1.h>
25 #include <encoding/payloads/sa_payload.h>
26 #include <encoding/payloads/nonce_payload.h>
27 #include <encoding/payloads/ke_payload.h>
28 #include <encoding/payloads/id_payload.h>
29 #include <encoding/payloads/payload.h>
30 #include <sa/ikev1/tasks/informational.h>
31 #include <sa/ikev1/tasks/quick_delete.h>
32 #include <processing/jobs/inactivity_job.h>
33
34 typedef struct private_quick_mode_t private_quick_mode_t;
35
36 /**
37 * Private members of a quick_mode_t task.
38 */
39 struct private_quick_mode_t {
40
41 /**
42 * Public methods and task_t interface.
43 */
44 quick_mode_t public;
45
46 /**
47 * Assigned IKE_SA.
48 */
49 ike_sa_t *ike_sa;
50
51 /**
52 * TRUE if we are initiating quick mode
53 */
54 bool initiator;
55
56 /**
57 * Traffic selector of initiator
58 */
59 traffic_selector_t *tsi;
60
61 /**
62 * Traffic selector of responder
63 */
64 traffic_selector_t *tsr;
65
66 /**
67 * Initiators nonce
68 */
69 chunk_t nonce_i;
70
71 /**
72 * Responder nonce
73 */
74 chunk_t nonce_r;
75
76 /**
77 * Initiators ESP SPI
78 */
79 u_int32_t spi_i;
80
81 /**
82 * Responder ESP SPI
83 */
84 u_int32_t spi_r;
85
86 /**
87 * Initiators IPComp CPI
88 */
89 u_int16_t cpi_i;
90
91 /**
92 * Responders IPComp CPI
93 */
94 u_int16_t cpi_r;
95
96 /**
97 * selected CHILD_SA proposal
98 */
99 proposal_t *proposal;
100
101 /**
102 * Config of CHILD_SA to establish
103 */
104 child_cfg_t *config;
105
106 /**
107 * CHILD_SA we are about to establish
108 */
109 child_sa_t *child_sa;
110
111 /**
112 * IKEv1 keymat
113 */
114 keymat_v1_t *keymat;
115
116 /**
117 * DH exchange, when PFS is in use
118 */
119 diffie_hellman_t *dh;
120
121 /**
122 * Negotiated lifetime of new SA
123 */
124 u_int32_t lifetime;
125
126 /**
127 * Negotaited lifebytes of new SA
128 */
129 u_int64_t lifebytes;
130
131 /**
132 * Reqid to use, 0 for auto-allocate
133 */
134 u_int32_t reqid;
135
136 /**
137 * SPI of SA we rekey
138 */
139 u_int32_t rekey;
140
141 /**
142 * Negotiated mode, tunnel or transport
143 */
144 ipsec_mode_t mode;
145
146 /**
147 * Use UDP encapsulation
148 */
149 bool udp;
150
151 /** states of quick mode */
152 enum {
153 QM_INIT,
154 QM_NEGOTIATED,
155 } state;
156 };
157
158 /**
159 * Schedule inactivity timeout for CHILD_SA with reqid, if enabled
160 */
161 static void schedule_inactivity_timeout(private_quick_mode_t *this)
162 {
163 u_int32_t timeout;
164 bool close_ike;
165
166 timeout = this->config->get_inactivity(this->config);
167 if (timeout)
168 {
169 close_ike = lib->settings->get_bool(lib->settings,
170 "%s.inactivity_close_ike", FALSE, charon->name);
171 lib->scheduler->schedule_job(lib->scheduler, (job_t*)
172 inactivity_job_create(this->child_sa->get_reqid(this->child_sa),
173 timeout, close_ike), timeout);
174 }
175 }
176
177 /**
178 * Install negotiated CHILD_SA
179 */
180 static bool install(private_quick_mode_t *this)
181 {
182 status_t status, status_i, status_o;
183 chunk_t encr_i, encr_r, integ_i, integ_r;
184 linked_list_t *tsi, *tsr;
185 child_sa_t *old = NULL;
186
187 this->child_sa->set_proposal(this->child_sa, this->proposal);
188 this->child_sa->set_state(this->child_sa, CHILD_INSTALLING);
189 this->child_sa->set_mode(this->child_sa, this->mode);
190
191 if (this->cpi_i && this->cpi_r)
192 { /* DEFLATE is the only transform we currently support */
193 this->child_sa->set_ipcomp(this->child_sa, IPCOMP_DEFLATE);
194 }
195 else
196 {
197 this->cpi_i = this->cpi_r = 0;
198 }
199
200 this->child_sa->set_protocol(this->child_sa,
201 this->proposal->get_protocol(this->proposal));
202
203 status_i = status_o = FAILED;
204 encr_i = encr_r = integ_i = integ_r = chunk_empty;
205 tsi = linked_list_create();
206 tsr = linked_list_create();
207 tsi->insert_last(tsi, this->tsi->clone(this->tsi));
208 tsr->insert_last(tsr, this->tsr->clone(this->tsr));
209 if (this->initiator)
210 {
211 charon->bus->narrow(charon->bus, this->child_sa,
212 NARROW_INITIATOR_POST_AUTH, tsi, tsr);
213 }
214 else
215 {
216 charon->bus->narrow(charon->bus, this->child_sa,
217 NARROW_RESPONDER, tsr, tsi);
218 }
219 if (tsi->get_count(tsi) == 0 || tsr->get_count(tsr) == 0)
220 {
221 tsi->destroy_offset(tsi, offsetof(traffic_selector_t, destroy));
222 tsr->destroy_offset(tsr, offsetof(traffic_selector_t, destroy));
223 DBG1(DBG_IKE, "no acceptable traffic selectors found");
224 return FALSE;
225 }
226
227 if (this->keymat->derive_child_keys(this->keymat, this->proposal, this->dh,
228 this->spi_i, this->spi_r, this->nonce_i, this->nonce_r,
229 &encr_i, &integ_i, &encr_r, &integ_r))
230 {
231 if (this->initiator)
232 {
233 status_i = this->child_sa->install(this->child_sa, encr_r, integ_r,
234 this->spi_i, this->cpi_i, TRUE, FALSE, tsi, tsr);
235 status_o = this->child_sa->install(this->child_sa, encr_i, integ_i,
236 this->spi_r, this->cpi_r, FALSE, FALSE, tsi, tsr);
237 }
238 else
239 {
240 status_i = this->child_sa->install(this->child_sa, encr_i, integ_i,
241 this->spi_r, this->cpi_r, TRUE, FALSE, tsr, tsi);
242 status_o = this->child_sa->install(this->child_sa, encr_r, integ_r,
243 this->spi_i, this->cpi_i, FALSE, FALSE, tsr, tsi);
244 }
245 }
246 chunk_clear(&integ_i);
247 chunk_clear(&integ_r);
248 chunk_clear(&encr_i);
249 chunk_clear(&encr_r);
250
251 if (status_i != SUCCESS || status_o != SUCCESS)
252 {
253 DBG1(DBG_IKE, "unable to install %s%s%sIPsec SA (SAD) in kernel",
254 (status_i != SUCCESS) ? "inbound " : "",
255 (status_i != SUCCESS && status_o != SUCCESS) ? "and ": "",
256 (status_o != SUCCESS) ? "outbound " : "");
257 tsi->destroy_offset(tsi, offsetof(traffic_selector_t, destroy));
258 tsr->destroy_offset(tsr, offsetof(traffic_selector_t, destroy));
259 return FALSE;
260 }
261
262 if (this->initiator)
263 {
264 status = this->child_sa->add_policies(this->child_sa, tsi, tsr);
265 }
266 else
267 {
268 status = this->child_sa->add_policies(this->child_sa, tsr, tsi);
269 }
270 tsi->destroy_offset(tsi, offsetof(traffic_selector_t, destroy));
271 tsr->destroy_offset(tsr, offsetof(traffic_selector_t, destroy));
272 if (status != SUCCESS)
273 {
274 DBG1(DBG_IKE, "unable to install IPsec policies (SPD) in kernel");
275 return FALSE;
276 }
277
278 charon->bus->child_keys(charon->bus, this->child_sa, this->initiator,
279 this->dh, this->nonce_i, this->nonce_r);
280
281 /* add to IKE_SA, and remove from task */
282 this->child_sa->set_state(this->child_sa, CHILD_INSTALLED);
283 this->ike_sa->add_child_sa(this->ike_sa, this->child_sa);
284
285 DBG0(DBG_IKE, "CHILD_SA %s{%d} established "
286 "with SPIs %.8x_i %.8x_o and TS %#R=== %#R",
287 this->child_sa->get_name(this->child_sa),
288 this->child_sa->get_reqid(this->child_sa),
289 ntohl(this->child_sa->get_spi(this->child_sa, TRUE)),
290 ntohl(this->child_sa->get_spi(this->child_sa, FALSE)),
291 this->child_sa->get_traffic_selectors(this->child_sa, TRUE),
292 this->child_sa->get_traffic_selectors(this->child_sa, FALSE));
293
294 if (this->rekey)
295 {
296 old = this->ike_sa->get_child_sa(this->ike_sa,
297 this->proposal->get_protocol(this->proposal),
298 this->rekey, TRUE);
299 }
300 if (old)
301 {
302 charon->bus->child_rekey(charon->bus, old, this->child_sa);
303 }
304 else
305 {
306 charon->bus->child_updown(charon->bus, this->child_sa, TRUE);
307 }
308 if (!this->rekey)
309 {
310 schedule_inactivity_timeout(this);
311 }
312 this->child_sa = NULL;
313 return TRUE;
314 }
315
316 /**
317 * Generate and add NONCE
318 */
319 static bool add_nonce(private_quick_mode_t *this, chunk_t *nonce,
320 message_t *message)
321 {
322 nonce_payload_t *nonce_payload;
323 nonce_gen_t *nonceg;
324
325 nonceg = this->keymat->keymat.create_nonce_gen(&this->keymat->keymat);
326 if (!nonceg)
327 {
328 DBG1(DBG_IKE, "no nonce generator found to create nonce");
329 return FALSE;
330 }
331 if (!nonceg->allocate_nonce(nonceg, NONCE_SIZE, nonce))
332 {
333 DBG1(DBG_IKE, "nonce allocation failed");
334 nonceg->destroy(nonceg);
335 return FALSE;
336 }
337 nonceg->destroy(nonceg);
338
339 nonce_payload = nonce_payload_create(NONCE_V1);
340 nonce_payload->set_nonce(nonce_payload, *nonce);
341 message->add_payload(message, &nonce_payload->payload_interface);
342
343 return TRUE;
344 }
345
346 /**
347 * Extract nonce from NONCE payload
348 */
349 static bool get_nonce(private_quick_mode_t *this, chunk_t *nonce,
350 message_t *message)
351 {
352 nonce_payload_t *nonce_payload;
353
354 nonce_payload = (nonce_payload_t*)message->get_payload(message, NONCE_V1);
355 if (!nonce_payload)
356 {
357 DBG1(DBG_IKE, "NONCE payload missing in message");
358 return FALSE;
359 }
360 *nonce = nonce_payload->get_nonce(nonce_payload);
361
362 return TRUE;
363 }
364
365 /**
366 * Add KE payload to message
367 */
368 static void add_ke(private_quick_mode_t *this, message_t *message)
369 {
370 ke_payload_t *ke_payload;
371
372 ke_payload = ke_payload_create_from_diffie_hellman(KEY_EXCHANGE_V1, this->dh);
373 message->add_payload(message, &ke_payload->payload_interface);
374 }
375
376 /**
377 * Get DH value from a KE payload
378 */
379 static bool get_ke(private_quick_mode_t *this, message_t *message)
380 {
381 ke_payload_t *ke_payload;
382
383 ke_payload = (ke_payload_t*)message->get_payload(message, KEY_EXCHANGE_V1);
384 if (!ke_payload)
385 {
386 DBG1(DBG_IKE, "KE payload missing");
387 return FALSE;
388 }
389 this->dh->set_other_public_value(this->dh,
390 ke_payload->get_key_exchange_data(ke_payload));
391 return TRUE;
392 }
393
394 /**
395 * Select a traffic selector from configuration
396 */
397 static traffic_selector_t* select_ts(private_quick_mode_t *this, bool local,
398 linked_list_t *supplied)
399 {
400 traffic_selector_t *ts;
401 linked_list_t *list;
402 host_t *host;
403
404 host = this->ike_sa->get_virtual_ip(this->ike_sa, local);
405 if (!host)
406 {
407 if (local)
408 {
409 host = this->ike_sa->get_my_host(this->ike_sa);
410 }
411 else
412 {
413 host = this->ike_sa->get_other_host(this->ike_sa);
414 }
415 }
416 list = this->config->get_traffic_selectors(this->config, local,
417 supplied, host);
418 if (list->get_first(list, (void**)&ts) == SUCCESS)
419 {
420 if (list->get_count(list) > 1)
421 {
422 DBG1(DBG_IKE, "configuration has more than one %s traffic selector,"
423 " using first only", local ? "local" : "remote");
424 }
425 ts = ts->clone(ts);
426 }
427 else
428 {
429 DBG1(DBG_IKE, "%s traffic selector missing in configuration",
430 local ? "local" : "local");
431 ts = NULL;
432 }
433 list->destroy_offset(list, offsetof(traffic_selector_t, destroy));
434 return ts;
435 }
436
437 /**
438 * Add selected traffic selectors to message
439 */
440 static void add_ts(private_quick_mode_t *this, message_t *message)
441 {
442 id_payload_t *id_payload;
443 host_t *hsi, *hsr;
444
445 if (this->initiator)
446 {
447 hsi = this->ike_sa->get_my_host(this->ike_sa);
448 hsr = this->ike_sa->get_other_host(this->ike_sa);
449 }
450 else
451 {
452 hsr = this->ike_sa->get_my_host(this->ike_sa);
453 hsi = this->ike_sa->get_other_host(this->ike_sa);
454 }
455 /* add ID payload only if negotiating non host2host tunnels */
456 if (!this->tsi->is_host(this->tsi, hsi) ||
457 !this->tsr->is_host(this->tsr, hsr) ||
458 this->tsi->get_protocol(this->tsi) ||
459 this->tsr->get_protocol(this->tsr) ||
460 this->tsi->get_from_port(this->tsi) ||
461 this->tsr->get_from_port(this->tsr) ||
462 this->tsi->get_to_port(this->tsi) != 65535 ||
463 this->tsr->get_to_port(this->tsr) != 65535)
464 {
465 id_payload = id_payload_create_from_ts(this->tsi);
466 message->add_payload(message, &id_payload->payload_interface);
467 id_payload = id_payload_create_from_ts(this->tsr);
468 message->add_payload(message, &id_payload->payload_interface);
469 }
470 }
471
472 /**
473 * Get traffic selectors from received message
474 */
475 static bool get_ts(private_quick_mode_t *this, message_t *message)
476 {
477 traffic_selector_t *tsi = NULL, *tsr = NULL;
478 enumerator_t *enumerator;
479 id_payload_t *id_payload;
480 payload_t *payload;
481 host_t *hsi, *hsr;
482 bool first = TRUE;
483
484 enumerator = message->create_payload_enumerator(message);
485 while (enumerator->enumerate(enumerator, &payload))
486 {
487 if (payload->get_type(payload) == ID_V1)
488 {
489 id_payload = (id_payload_t*)payload;
490
491 if (first)
492 {
493 tsi = id_payload->get_ts(id_payload);
494 first = FALSE;
495 }
496 else
497 {
498 tsr = id_payload->get_ts(id_payload);
499 break;
500 }
501 }
502 }
503 enumerator->destroy(enumerator);
504
505 /* create host2host selectors if ID payloads missing */
506 if (this->initiator)
507 {
508 hsi = this->ike_sa->get_my_host(this->ike_sa);
509 hsr = this->ike_sa->get_other_host(this->ike_sa);
510 }
511 else
512 {
513 hsr = this->ike_sa->get_my_host(this->ike_sa);
514 hsi = this->ike_sa->get_other_host(this->ike_sa);
515 }
516 if (!tsi)
517 {
518 tsi = traffic_selector_create_from_subnet(hsi->clone(hsi),
519 hsi->get_family(hsi) == AF_INET ? 32 : 128, 0, 0);
520 }
521 if (!tsr)
522 {
523 tsr = traffic_selector_create_from_subnet(hsr->clone(hsr),
524 hsr->get_family(hsr) == AF_INET ? 32 : 128, 0, 0);
525 }
526 if (this->initiator)
527 {
528 /* check if peer selection valid */
529 if (!tsr->is_contained_in(tsr, this->tsr) ||
530 !tsi->is_contained_in(tsi, this->tsi))
531 {
532 DBG1(DBG_IKE, "peer selected invalid traffic selectors: ",
533 "%R for %R, %R for %R", tsi, this->tsi, tsr, this->tsr);
534 tsi->destroy(tsi);
535 tsr->destroy(tsr);
536 return FALSE;
537 }
538 this->tsi->destroy(this->tsi);
539 this->tsr->destroy(this->tsr);
540 this->tsi = tsi;
541 this->tsr = tsr;
542 }
543 else
544 {
545 this->tsi = tsi;
546 this->tsr = tsr;
547 }
548 return TRUE;
549 }
550
551 /**
552 * Add NAT-OA payloads
553 */
554 static void add_nat_oa_payloads(private_quick_mode_t *this, message_t *message)
555 {
556 identification_t *id;
557 id_payload_t *nat_oa;
558 host_t *src, *dst;
559
560 src = message->get_source(message);
561 dst = message->get_destination(message);
562
563 src = this->initiator ? src : dst;
564 dst = this->initiator ? dst : src;
565
566 /* first NAT-OA is the initiator's address */
567 id = identification_create_from_sockaddr(src->get_sockaddr(src));
568 nat_oa = id_payload_create_from_identification(NAT_OA_V1, id);
569 message->add_payload(message, (payload_t*)nat_oa);
570 id->destroy(id);
571
572 /* second NAT-OA is that of the responder */
573 id = identification_create_from_sockaddr(dst->get_sockaddr(dst));
574 nat_oa = id_payload_create_from_identification(NAT_OA_V1, id);
575 message->add_payload(message, (payload_t*)nat_oa);
576 id->destroy(id);
577 }
578
579 /**
580 * Look up lifetimes
581 */
582 static void get_lifetimes(private_quick_mode_t *this)
583 {
584 lifetime_cfg_t *lft;
585
586 lft = this->config->get_lifetime(this->config);
587 if (lft->time.life)
588 {
589 this->lifetime = lft->time.life;
590 }
591 else if (lft->bytes.life)
592 {
593 this->lifebytes = lft->bytes.life;
594 }
595 free(lft);
596 }
597
598 /**
599 * Check and apply lifetimes
600 */
601 static void apply_lifetimes(private_quick_mode_t *this, sa_payload_t *sa_payload)
602 {
603 u_int32_t lifetime;
604 u_int64_t lifebytes;
605
606 lifetime = sa_payload->get_lifetime(sa_payload);
607 lifebytes = sa_payload->get_lifebytes(sa_payload);
608 if (this->lifetime != lifetime)
609 {
610 DBG1(DBG_IKE, "received %us lifetime, configured %us",
611 lifetime, this->lifetime);
612 this->lifetime = lifetime;
613 }
614 if (this->lifebytes != lifebytes)
615 {
616 DBG1(DBG_IKE, "received %llu lifebytes, configured %llu",
617 lifebytes, this->lifebytes);
618 this->lifebytes = lifebytes;
619 }
620 }
621
622 /**
623 * Set the task ready to build notify error message
624 */
625 static status_t send_notify(private_quick_mode_t *this, notify_type_t type)
626 {
627 notify_payload_t *notify;
628
629 notify = notify_payload_create_from_protocol_and_type(NOTIFY_V1,
630 PROTO_ESP, type);
631 notify->set_spi(notify, this->spi_i);
632
633 this->ike_sa->queue_task(this->ike_sa,
634 (task_t*)informational_create(this->ike_sa, notify));
635 /* cancel all active/passive tasks in favour of informational */
636 this->ike_sa->flush_queue(this->ike_sa,
637 this->initiator ? TASK_QUEUE_ACTIVE : TASK_QUEUE_PASSIVE);
638 return ALREADY_DONE;
639 }
640
641 METHOD(task_t, build_i, status_t,
642 private_quick_mode_t *this, message_t *message)
643 {
644 switch (this->state)
645 {
646 case QM_INIT:
647 {
648 enumerator_t *enumerator;
649 sa_payload_t *sa_payload;
650 linked_list_t *list, *tsi, *tsr;
651 proposal_t *proposal;
652 diffie_hellman_group_t group;
653
654 this->udp = this->ike_sa->has_condition(this->ike_sa, COND_NAT_ANY);
655 this->mode = this->config->get_mode(this->config);
656 this->child_sa = child_sa_create(
657 this->ike_sa->get_my_host(this->ike_sa),
658 this->ike_sa->get_other_host(this->ike_sa),
659 this->config, this->reqid, this->udp);
660
661 if (this->udp && this->mode == MODE_TRANSPORT)
662 {
663 /* TODO-IKEv1: disable NAT-T for TRANSPORT mode by default? */
664 add_nat_oa_payloads(this, message);
665 }
666
667 if (this->config->use_ipcomp(this->config))
668 {
669 if (this->udp)
670 {
671 DBG1(DBG_IKE, "IPComp is not supported if either peer is "
672 "natted, IPComp disabled");
673 }
674 else
675 {
676 this->cpi_i = this->child_sa->alloc_cpi(this->child_sa);
677 if (!this->cpi_i)
678 {
679 DBG1(DBG_IKE, "unable to allocate a CPI from kernel, "
680 "IPComp disabled");
681 }
682 }
683 }
684
685 this->spi_i = this->child_sa->alloc_spi(this->child_sa, PROTO_ESP);
686 if (!this->spi_i)
687 {
688 DBG1(DBG_IKE, "allocating SPI from kernel failed");
689 return FAILED;
690 }
691
692 list = this->config->get_proposals(this->config, FALSE);
693 enumerator = list->create_enumerator(list);
694 while (enumerator->enumerate(enumerator, &proposal))
695 {
696 proposal->set_spi(proposal, this->spi_i);
697 }
698 enumerator->destroy(enumerator);
699
700 get_lifetimes(this);
701 sa_payload = sa_payload_create_from_proposals_v1(list,
702 this->lifetime, this->lifebytes, AUTH_NONE,
703 this->mode, this->udp, this->cpi_i);
704 list->destroy_offset(list, offsetof(proposal_t, destroy));
705 message->add_payload(message, &sa_payload->payload_interface);
706
707 if (!add_nonce(this, &this->nonce_i, message))
708 {
709 return FAILED;
710 }
711
712 group = this->config->get_dh_group(this->config);
713 if (group != MODP_NONE)
714 {
715 this->dh = this->keymat->keymat.create_dh(&this->keymat->keymat,
716 group);
717 if (!this->dh)
718 {
719 DBG1(DBG_IKE, "configured DH group %N not supported",
720 diffie_hellman_group_names, group);
721 return FAILED;
722 }
723 add_ke(this, message);
724 }
725 if (!this->tsi)
726 {
727 this->tsi = select_ts(this, TRUE, NULL);
728 }
729 if (!this->tsr)
730 {
731 this->tsr = select_ts(this, FALSE, NULL);
732 }
733 tsi = linked_list_create();
734 tsr = linked_list_create();
735 tsi->insert_last(tsi, this->tsi);
736 tsr->insert_last(tsr, this->tsr);
737 this->tsi = this->tsr = NULL;
738 charon->bus->narrow(charon->bus, this->child_sa,
739 NARROW_INITIATOR_PRE_AUTH, tsi, tsr);
740 tsi->remove_first(tsi, (void**)&this->tsi);
741 tsr->remove_first(tsr, (void**)&this->tsr);
742 tsi->destroy_offset(tsi, offsetof(traffic_selector_t, destroy));
743 tsr->destroy_offset(tsr, offsetof(traffic_selector_t, destroy));
744 if (!this->tsi || !this->tsr)
745 {
746 return FAILED;
747 }
748 add_ts(this, message);
749 return NEED_MORE;
750 }
751 case QM_NEGOTIATED:
752 {
753 return SUCCESS;
754 }
755 default:
756 return FAILED;
757 }
758 }
759
760 /**
761 * Check for notify errors, return TRUE if error found
762 */
763 static bool has_notify_errors(private_quick_mode_t *this, message_t *message)
764 {
765 enumerator_t *enumerator;
766 payload_t *payload;
767 bool err = FALSE;
768
769 enumerator = message->create_payload_enumerator(message);
770 while (enumerator->enumerate(enumerator, &payload))
771 {
772 if (payload->get_type(payload) == NOTIFY_V1)
773 {
774 notify_payload_t *notify;
775 notify_type_t type;
776
777 notify = (notify_payload_t*)payload;
778 type = notify->get_notify_type(notify);
779 if (type < 16384)
780 {
781
782 DBG1(DBG_IKE, "received %N error notify",
783 notify_type_names, type);
784 err = TRUE;
785 }
786 else
787 {
788 DBG1(DBG_IKE, "received %N notify", notify_type_names, type);
789 }
790 }
791 }
792 enumerator->destroy(enumerator);
793
794 return err;
795 }
796
797 /**
798 * Check if this is a rekey for an existing CHILD_SA, reuse reqid if so
799 */
800 static void check_for_rekeyed_child(private_quick_mode_t *this)
801 {
802 enumerator_t *enumerator, *policies;
803 traffic_selector_t *local, *remote;
804 child_sa_t *child_sa;
805
806 enumerator = this->ike_sa->create_child_sa_enumerator(this->ike_sa);
807 while (this->reqid == 0 && enumerator->enumerate(enumerator, &child_sa))
808 {
809 if (child_sa->get_state(child_sa) == CHILD_INSTALLED &&
810 streq(child_sa->get_name(child_sa),
811 this->config->get_name(this->config)))
812 {
813 policies = child_sa->create_policy_enumerator(child_sa);
814 if (policies->enumerate(policies, &local, &remote))
815 {
816 if (local->equals(local, this->tsr) &&
817 remote->equals(remote, this->tsi) &&
818 this->proposal->equals(this->proposal,
819 child_sa->get_proposal(child_sa)))
820 {
821 this->reqid = child_sa->get_reqid(child_sa);
822 this->rekey = child_sa->get_spi(child_sa, TRUE);
823 child_sa->set_state(child_sa, CHILD_REKEYING);
824 DBG1(DBG_IKE, "detected rekeying of CHILD_SA %s{%u}",
825 child_sa->get_name(child_sa), this->reqid);
826 }
827 }
828 policies->destroy(policies);
829 }
830 }
831 enumerator->destroy(enumerator);
832 }
833
834 METHOD(task_t, process_r, status_t,
835 private_quick_mode_t *this, message_t *message)
836 {
837 switch (this->state)
838 {
839 case QM_INIT:
840 {
841 sa_payload_t *sa_payload;
842 linked_list_t *tsi, *tsr, *list = NULL;
843 peer_cfg_t *peer_cfg;
844 host_t *me, *other;
845 u_int16_t group;
846 bool private;
847
848 if (!get_ts(this, message))
849 {
850 return FAILED;
851 }
852 me = this->ike_sa->get_virtual_ip(this->ike_sa, TRUE);
853 if (!me)
854 {
855 me = this->ike_sa->get_my_host(this->ike_sa);
856 }
857 other = this->ike_sa->get_virtual_ip(this->ike_sa, FALSE);
858 if (!other)
859 {
860 other = this->ike_sa->get_other_host(this->ike_sa);
861 }
862 peer_cfg = this->ike_sa->get_peer_cfg(this->ike_sa);
863 tsi = linked_list_create();
864 tsr = linked_list_create();
865 tsi->insert_last(tsi, this->tsi);
866 tsr->insert_last(tsr, this->tsr);
867 this->tsi = this->tsr = NULL;
868 this->config = peer_cfg->select_child_cfg(peer_cfg, tsr, tsi,
869 me, other);
870 if (this->config)
871 {
872 this->tsi = select_ts(this, FALSE, tsi);
873 this->tsr = select_ts(this, TRUE, tsr);
874 }
875 tsi->destroy_offset(tsi, offsetof(traffic_selector_t, destroy));
876 tsr->destroy_offset(tsr, offsetof(traffic_selector_t, destroy));
877 if (!this->config || !this->tsi || !this->tsr)
878 {
879 DBG1(DBG_IKE, "no matching CHILD_SA config found");
880 return send_notify(this, INVALID_ID_INFORMATION);
881 }
882
883 sa_payload = (sa_payload_t*)message->get_payload(message,
884 SECURITY_ASSOCIATION_V1);
885 if (!sa_payload)
886 {
887 DBG1(DBG_IKE, "sa payload missing");
888 return send_notify(this, INVALID_PAYLOAD_TYPE);
889 }
890 if (this->config->use_ipcomp(this->config))
891 {
892 if (this->ike_sa->has_condition(this->ike_sa, COND_NAT_ANY))
893 {
894 DBG1(DBG_IKE, "IPComp is not supported if either peer is "
895 "natted, IPComp disabled");
896 }
897 else
898 {
899 list = sa_payload->get_ipcomp_proposals(sa_payload,
900 &this->cpi_i);
901 if (!list->get_count(list))
902 {
903 DBG1(DBG_IKE, "expected IPComp proposal but peer did "
904 "not send one, IPComp disabled");
905 this->cpi_i = 0;
906 }
907 }
908 }
909 if (!list || !list->get_count(list))
910 {
911 DESTROY_IF(list);
912 list = sa_payload->get_proposals(sa_payload);
913 }
914 private = this->ike_sa->supports_extension(this->ike_sa,
915 EXT_STRONGSWAN);
916 this->proposal = this->config->select_proposal(this->config,
917 list, FALSE, private);
918 list->destroy_offset(list, offsetof(proposal_t, destroy));
919
920 this->mode = sa_payload->get_encap_mode(sa_payload, &this->udp);
921
922 get_lifetimes(this);
923 apply_lifetimes(this, sa_payload);
924
925 if (!this->proposal)
926 {
927 DBG1(DBG_IKE, "no matching proposal found, sending %N",
928 notify_type_names, NO_PROPOSAL_CHOSEN);
929 return send_notify(this, NO_PROPOSAL_CHOSEN);
930 }
931 this->spi_i = this->proposal->get_spi(this->proposal);
932
933 if (!get_nonce(this, &this->nonce_i, message))
934 {
935 return send_notify(this, INVALID_PAYLOAD_TYPE);
936 }
937
938 if (this->proposal->get_algorithm(this->proposal,
939 DIFFIE_HELLMAN_GROUP, &group, NULL))
940 {
941 this->dh = this->keymat->keymat.create_dh(&this->keymat->keymat,
942 group);
943 if (!this->dh)
944 {
945 DBG1(DBG_IKE, "negotiated DH group %N not supported",
946 diffie_hellman_group_names, group);
947 return send_notify(this, INVALID_KEY_INFORMATION);
948 }
949 if (!get_ke(this, message))
950 {
951 return send_notify(this, INVALID_PAYLOAD_TYPE);
952 }
953 }
954
955 check_for_rekeyed_child(this);
956
957 this->child_sa = child_sa_create(
958 this->ike_sa->get_my_host(this->ike_sa),
959 this->ike_sa->get_other_host(this->ike_sa),
960 this->config, this->reqid, this->udp);
961 return NEED_MORE;
962 }
963 case QM_NEGOTIATED:
964 {
965 if (message->get_exchange_type(message) == INFORMATIONAL_V1 ||
966 has_notify_errors(this, message))
967 {
968 return SUCCESS;
969 }
970 if (!install(this))
971 {
972 ike_sa_t *ike_sa = this->ike_sa;
973 task_t *task;
974
975 task = (task_t*)quick_delete_create(this->ike_sa,
976 this->proposal->get_protocol(this->proposal),
977 this->spi_i, TRUE, TRUE);
978 /* flush_queue() destroys the current task */
979 ike_sa->flush_queue(ike_sa, TASK_QUEUE_PASSIVE);
980 ike_sa->queue_task(ike_sa, task);
981 return ALREADY_DONE;
982 }
983 return SUCCESS;
984 }
985 default:
986 return FAILED;
987 }
988 }
989
990 METHOD(task_t, build_r, status_t,
991 private_quick_mode_t *this, message_t *message)
992 {
993 switch (this->state)
994 {
995 case QM_INIT:
996 {
997 sa_payload_t *sa_payload;
998
999 this->spi_r = this->child_sa->alloc_spi(this->child_sa, PROTO_ESP);
1000 if (!this->spi_r)
1001 {
1002 DBG1(DBG_IKE, "allocating SPI from kernel failed");
1003 return send_notify(this, NO_PROPOSAL_CHOSEN);
1004 }
1005 this->proposal->set_spi(this->proposal, this->spi_r);
1006
1007 if (this->cpi_i)
1008 {
1009 this->cpi_r = this->child_sa->alloc_cpi(this->child_sa);
1010 if (!this->cpi_r)
1011 {
1012 DBG1(DBG_IKE, "unable to allocate a CPI from "
1013 "kernel, IPComp disabled");
1014 return send_notify(this, NO_PROPOSAL_CHOSEN);
1015 }
1016 }
1017
1018 if (this->udp && this->mode == MODE_TRANSPORT)
1019 {
1020 /* TODO-IKEv1: disable NAT-T for TRANSPORT mode by default? */
1021 add_nat_oa_payloads(this, message);
1022 }
1023
1024 sa_payload = sa_payload_create_from_proposal_v1(this->proposal,
1025 this->lifetime, this->lifebytes, AUTH_NONE,
1026 this->mode, this->udp, this->cpi_r);
1027 message->add_payload(message, &sa_payload->payload_interface);
1028
1029 if (!add_nonce(this, &this->nonce_r, message))
1030 {
1031 return FAILED;
1032 }
1033 if (this->dh)
1034 {
1035 add_ke(this, message);
1036 }
1037
1038 add_ts(this, message);
1039
1040 this->state = QM_NEGOTIATED;
1041 return NEED_MORE;
1042 }
1043 default:
1044 return FAILED;
1045 }
1046 }
1047
1048 METHOD(task_t, process_i, status_t,
1049 private_quick_mode_t *this, message_t *message)
1050 {
1051 switch (this->state)
1052 {
1053 case QM_INIT:
1054 {
1055 sa_payload_t *sa_payload;
1056 linked_list_t *list = NULL;
1057 bool private;
1058
1059 sa_payload = (sa_payload_t*)message->get_payload(message,
1060 SECURITY_ASSOCIATION_V1);
1061 if (!sa_payload)
1062 {
1063 DBG1(DBG_IKE, "sa payload missing");
1064 return send_notify(this, NO_PROPOSAL_CHOSEN);
1065 }
1066 if (this->cpi_i)
1067 {
1068 list = sa_payload->get_ipcomp_proposals(sa_payload,
1069 &this->cpi_r);
1070 if (!list->get_count(list))
1071 {
1072 DBG1(DBG_IKE, "peer did not acccept our IPComp proposal, "
1073 "IPComp disabled");
1074 this->cpi_i = 0;
1075 }
1076 }
1077 if (!list || !list->get_count(list))
1078 {
1079 DESTROY_IF(list);
1080 list = sa_payload->get_proposals(sa_payload);
1081 }
1082 private = this->ike_sa->supports_extension(this->ike_sa,
1083 EXT_STRONGSWAN);
1084 this->proposal = this->config->select_proposal(this->config,
1085 list, FALSE, private);
1086 list->destroy_offset(list, offsetof(proposal_t, destroy));
1087 if (!this->proposal)
1088 {
1089 DBG1(DBG_IKE, "no matching proposal found");
1090 return send_notify(this, NO_PROPOSAL_CHOSEN);
1091 }
1092 this->spi_r = this->proposal->get_spi(this->proposal);
1093
1094 apply_lifetimes(this, sa_payload);
1095
1096 if (!get_nonce(this, &this->nonce_r, message))
1097 {
1098 return send_notify(this, INVALID_PAYLOAD_TYPE);
1099 }
1100 if (this->dh && !get_ke(this, message))
1101 {
1102 return send_notify(this, INVALID_KEY_INFORMATION);
1103 }
1104 if (!get_ts(this, message))
1105 {
1106 return send_notify(this, INVALID_PAYLOAD_TYPE);
1107 }
1108 if (!install(this))
1109 {
1110 return send_notify(this, NO_PROPOSAL_CHOSEN);
1111 }
1112 this->state = QM_NEGOTIATED;
1113 return NEED_MORE;
1114 }
1115 default:
1116 return FAILED;
1117 }
1118 }
1119
1120 METHOD(task_t, get_type, task_type_t,
1121 private_quick_mode_t *this)
1122 {
1123 return TASK_QUICK_MODE;
1124 }
1125
1126 METHOD(quick_mode_t, use_reqid, void,
1127 private_quick_mode_t *this, u_int32_t reqid)
1128 {
1129 this->reqid = reqid;
1130 }
1131
1132 METHOD(quick_mode_t, rekey, void,
1133 private_quick_mode_t *this, u_int32_t spi)
1134 {
1135 this->rekey = spi;
1136 }
1137
1138 METHOD(task_t, migrate, void,
1139 private_quick_mode_t *this, ike_sa_t *ike_sa)
1140 {
1141 chunk_free(&this->nonce_i);
1142 chunk_free(&this->nonce_r);
1143 DESTROY_IF(this->tsi);
1144 DESTROY_IF(this->tsr);
1145 DESTROY_IF(this->proposal);
1146 DESTROY_IF(this->child_sa);
1147 DESTROY_IF(this->dh);
1148
1149 this->ike_sa = ike_sa;
1150 this->keymat = (keymat_v1_t*)ike_sa->get_keymat(ike_sa);
1151 this->state = QM_INIT;
1152 this->tsi = NULL;
1153 this->tsr = NULL;
1154 this->proposal = NULL;
1155 this->child_sa = NULL;
1156 this->dh = NULL;
1157 this->spi_i = 0;
1158 this->spi_r = 0;
1159
1160 if (!this->initiator)
1161 {
1162 DESTROY_IF(this->config);
1163 this->config = NULL;
1164 }
1165 }
1166
1167 METHOD(task_t, destroy, void,
1168 private_quick_mode_t *this)
1169 {
1170 chunk_free(&this->nonce_i);
1171 chunk_free(&this->nonce_r);
1172 DESTROY_IF(this->tsi);
1173 DESTROY_IF(this->tsr);
1174 DESTROY_IF(this->proposal);
1175 DESTROY_IF(this->child_sa);
1176 DESTROY_IF(this->config);
1177 DESTROY_IF(this->dh);
1178 free(this);
1179 }
1180
1181 /*
1182 * Described in header.
1183 */
1184 quick_mode_t *quick_mode_create(ike_sa_t *ike_sa, child_cfg_t *config,
1185 traffic_selector_t *tsi, traffic_selector_t *tsr)
1186 {
1187 private_quick_mode_t *this;
1188
1189 INIT(this,
1190 .public = {
1191 .task = {
1192 .get_type = _get_type,
1193 .migrate = _migrate,
1194 .destroy = _destroy,
1195 },
1196 .use_reqid = _use_reqid,
1197 .rekey = _rekey,
1198 },
1199 .ike_sa = ike_sa,
1200 .initiator = config != NULL,
1201 .config = config,
1202 .keymat = (keymat_v1_t*)ike_sa->get_keymat(ike_sa),
1203 .state = QM_INIT,
1204 .tsi = tsi ? tsi->clone(tsi) : NULL,
1205 .tsr = tsr ? tsr->clone(tsr) : NULL,
1206 );
1207
1208 if (config)
1209 {
1210 this->public.task.build = _build_i;
1211 this->public.task.process = _process_i;
1212 }
1213 else
1214 {
1215 this->public.task.build = _build_r;
1216 this->public.task.process = _process_r;
1217 }
1218
1219 return &this->public;
1220 }