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