Refactored traffic selector handling in quick mode
[strongswan.git] / src / libcharon / sa / tasks / quick_mode.c
1 /*
2 * Copyright (C) 2011 Martin Willi
3 * Copyright (C) 2011 revosec AG
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include "quick_mode.h"
17
18 #include <string.h>
19
20 #include <daemon.h>
21 #include <sa/keymat_v1.h>
22 #include <encoding/payloads/sa_payload.h>
23 #include <encoding/payloads/nonce_payload.h>
24 #include <encoding/payloads/id_payload.h>
25
26 typedef struct private_quick_mode_t private_quick_mode_t;
27
28 /**
29 * Private members of a quick_mode_t task.
30 */
31 struct private_quick_mode_t {
32
33 /**
34 * Public methods and task_t interface.
35 */
36 quick_mode_t public;
37
38 /**
39 * Assigned IKE_SA.
40 */
41 ike_sa_t *ike_sa;
42
43 /**
44 * TRUE if we are initiating quick mode
45 */
46 bool initiator;
47
48 /**
49 * Traffic selector of initiator
50 */
51 traffic_selector_t *tsi;
52
53 /**
54 * Traffic selector of responder
55 */
56 traffic_selector_t *tsr;
57
58 /**
59 * Initiators nonce
60 */
61 chunk_t nonce_i;
62
63 /**
64 * Responder nonce
65 */
66 chunk_t nonce_r;
67
68 /**
69 * Initiators ESP SPI
70 */
71 u_int32_t spi_i;
72
73 /**
74 * Responder ESP SPI
75 */
76 u_int32_t spi_r;
77
78 /**
79 * selected CHILD_SA proposal
80 */
81 proposal_t *proposal;
82
83 /**
84 * Config of CHILD_SA to establish
85 */
86 child_cfg_t *config;
87
88 /**
89 * CHILD_SA we are about to establish
90 */
91 child_sa_t *child_sa;
92
93 /**
94 * IKEv1 keymat
95 */
96 keymat_v1_t *keymat;
97
98 /** states of quick mode */
99 enum {
100 QM_INIT,
101 QM_NEGOTIATED,
102 } state;
103 };
104
105 /**
106 * Install negotiated CHILD_SA
107 */
108 static bool install(private_quick_mode_t *this)
109 {
110 status_t status, status_i, status_o;
111 chunk_t encr_i, encr_r, integ_i, integ_r;
112 linked_list_t *tsi, *tsr;
113
114 this->child_sa->set_proposal(this->child_sa, this->proposal);
115 this->child_sa->set_state(this->child_sa, CHILD_INSTALLING);
116 this->child_sa->set_mode(this->child_sa, MODE_TUNNEL);
117 this->child_sa->set_protocol(this->child_sa,
118 this->proposal->get_protocol(this->proposal));
119
120 status_i = status_o = FAILED;
121 encr_i = encr_r = integ_i = integ_r = chunk_empty;
122 tsi = linked_list_create();
123 tsr = linked_list_create();
124 tsi->insert_last(tsi, this->tsi);
125 tsr->insert_last(tsr, this->tsr);
126 if (this->keymat->derive_child_keys(this->keymat, this->proposal, NULL,
127 this->spi_i, this->spi_r, this->nonce_i, this->nonce_r,
128 &encr_i, &integ_i, &encr_r, &integ_r))
129 {
130 if (this->initiator)
131 {
132 status_i = this->child_sa->install(this->child_sa, encr_r, integ_r,
133 this->spi_i, 0, TRUE, FALSE, tsi, tsr);
134 status_o = this->child_sa->install(this->child_sa, encr_i, integ_i,
135 this->spi_r, 0, FALSE, FALSE, tsi, tsr);
136 }
137 else
138 {
139 status_i = this->child_sa->install(this->child_sa, encr_i, integ_i,
140 this->spi_r, 0, TRUE, FALSE, tsr, tsi);
141 status_o = this->child_sa->install(this->child_sa, encr_r, integ_r,
142 this->spi_i, 0, FALSE, FALSE, tsr, tsi);
143 }
144 }
145 chunk_clear(&integ_i);
146 chunk_clear(&integ_r);
147 chunk_clear(&encr_i);
148 chunk_clear(&encr_r);
149
150 if (status_i != SUCCESS || status_o != SUCCESS)
151 {
152 DBG1(DBG_IKE, "unable to install %s%s%sIPsec SA (SAD) in kernel",
153 (status_i != SUCCESS) ? "inbound " : "",
154 (status_i != SUCCESS && status_o != SUCCESS) ? "and ": "",
155 (status_o != SUCCESS) ? "outbound " : "");
156 tsi->destroy(tsi);
157 tsr->destroy(tsr);
158 return FALSE;
159 }
160
161 if (this->initiator)
162 {
163 status = this->child_sa->add_policies(this->child_sa, tsi, tsr);
164 }
165 else
166 {
167 status = this->child_sa->add_policies(this->child_sa, tsr, tsi);
168 }
169 tsi->destroy(tsi);
170 tsr->destroy(tsr);
171 if (status != SUCCESS)
172 {
173 DBG1(DBG_IKE, "unable to install IPsec policies (SPD) in kernel");
174 return FALSE;
175 }
176
177 charon->bus->child_keys(charon->bus, this->child_sa, this->initiator,
178 NULL, this->nonce_i, this->nonce_r);
179
180 /* add to IKE_SA, and remove from task */
181 this->child_sa->set_state(this->child_sa, CHILD_INSTALLED);
182 this->ike_sa->add_child_sa(this->ike_sa, this->child_sa);
183
184 DBG0(DBG_IKE, "CHILD_SA %s{%d} established "
185 "with SPIs %.8x_i %.8x_o and TS %#R=== %#R",
186 this->child_sa->get_name(this->child_sa),
187 this->child_sa->get_reqid(this->child_sa),
188 ntohl(this->child_sa->get_spi(this->child_sa, TRUE)),
189 ntohl(this->child_sa->get_spi(this->child_sa, FALSE)),
190 this->child_sa->get_traffic_selectors(this->child_sa, TRUE),
191 this->child_sa->get_traffic_selectors(this->child_sa, FALSE));
192
193 charon->bus->child_updown(charon->bus, this->child_sa, TRUE);
194
195 this->child_sa = NULL;
196
197 return TRUE;
198 }
199
200 /**
201 * Generate and add NONCE
202 */
203 static bool add_nonce(private_quick_mode_t *this, chunk_t *nonce,
204 message_t *message)
205 {
206 nonce_payload_t *nonce_payload;
207 rng_t *rng;
208
209 rng = lib->crypto->create_rng(lib->crypto, RNG_WEAK);
210 if (!rng)
211 {
212 DBG1(DBG_IKE, "no RNG found to create nonce");
213 return FALSE;
214 }
215 rng->allocate_bytes(rng, NONCE_SIZE, nonce);
216 rng->destroy(rng);
217
218 nonce_payload = nonce_payload_create(NONCE_V1);
219 nonce_payload->set_nonce(nonce_payload, *nonce);
220 message->add_payload(message, &nonce_payload->payload_interface);
221
222 return TRUE;
223 }
224
225 /**
226 * Extract nonce from NONCE payload
227 */
228 static bool get_nonce(private_quick_mode_t *this, chunk_t *nonce,
229 message_t *message)
230 {
231 nonce_payload_t *nonce_payload;
232
233 nonce_payload = (nonce_payload_t*)message->get_payload(message, NONCE_V1);
234 if (!nonce_payload)
235 {
236 DBG1(DBG_IKE, "NONCE payload missing in message");
237 return FALSE;
238 }
239 *nonce = nonce_payload->get_nonce(nonce_payload);
240
241 return TRUE;
242 }
243
244 /**
245 * Select a traffic selector from configuration
246 */
247 static traffic_selector_t* select_ts(private_quick_mode_t *this, bool initiator)
248 {
249 traffic_selector_t *ts;
250 linked_list_t *list;
251 host_t *host;
252
253 if (initiator)
254 {
255 host = this->ike_sa->get_my_host(this->ike_sa);
256 }
257 else
258 {
259 host = this->ike_sa->get_other_host(this->ike_sa);
260 }
261 list = this->config->get_traffic_selectors(this->config, initiator,
262 NULL, host);
263 if (list->get_first(list, (void**)&ts) == SUCCESS)
264 {
265 if (list->get_count(list) > 1)
266 {
267 DBG1(DBG_IKE, "configuration has more than one %s traffic selector,"
268 " using first IKEv1", initiator ? "initiator" : "responder");
269 }
270 ts = ts->clone(ts);
271 }
272 else
273 {
274 DBG1(DBG_IKE, "%s traffic selector missing in configuration",
275 initiator ? "initiator" : "responder");
276 ts = NULL;
277 }
278 list->destroy_offset(list, offsetof(traffic_selector_t, destroy));
279 return ts;
280 }
281
282 /**
283 * Add selected traffic selectors to message
284 */
285 static void add_ts(private_quick_mode_t *this, message_t *message)
286 {
287 id_payload_t *id_payload;
288
289 id_payload = id_payload_create_from_ts(this->tsi);
290 message->add_payload(message, &id_payload->payload_interface);
291 id_payload = id_payload_create_from_ts(this->tsr);
292 message->add_payload(message, &id_payload->payload_interface);
293 }
294
295 /**
296 * Get traffic selectors from received message
297 */
298 static bool get_ts(private_quick_mode_t *this, message_t *message,
299 bool initiator)
300 {
301 traffic_selector_t *tsi = NULL, *tsr = NULL;
302 enumerator_t *enumerator;
303 id_payload_t *id_payload;
304 payload_t *payload;
305 host_t *hsi, *hsr;
306 bool first = TRUE;
307
308 enumerator = message->create_payload_enumerator(message);
309 while (enumerator->enumerate(enumerator, &payload))
310 {
311 if (payload->get_type(payload) == ID_V1)
312 {
313 id_payload = (id_payload_t*)payload;
314
315 if (first)
316 {
317 tsi = id_payload->get_ts(id_payload);
318 first = FALSE;
319 }
320 else
321 {
322 tsr = id_payload->get_ts(id_payload);
323 break;
324 }
325 }
326 }
327 enumerator->destroy(enumerator);
328
329 /* create host2host selectors if ID payloads missing */
330 if (initiator)
331 {
332 hsi = this->ike_sa->get_my_host(this->ike_sa);
333 hsr = this->ike_sa->get_other_host(this->ike_sa);
334 }
335 else
336 {
337 hsr = this->ike_sa->get_my_host(this->ike_sa);
338 hsi = this->ike_sa->get_other_host(this->ike_sa);
339 }
340 if (!tsi)
341 {
342 tsi = traffic_selector_create_from_subnet(hsi->clone(hsi),
343 hsi->get_family(hsi) == AF_INET ? 32 : 128, 0, 0);
344 }
345 if (!tsr)
346 {
347 tsr = traffic_selector_create_from_subnet(hsr->clone(hsr),
348 hsr->get_family(hsr) == AF_INET ? 32 : 128, 0, 0);
349 }
350 if (initiator)
351 {
352 /* check if peer selection valid */
353 if (!tsr->is_contained_in(tsr, this->tsr) ||
354 !tsi->is_contained_in(tsi, this->tsi))
355 {
356 DBG1(DBG_IKE, "peer selected invalid traffic selectors: ",
357 "%R for %R, %R for %R", tsi, this->tsi, tsr, this->tsr);
358 tsi->destroy(tsi);
359 tsr->destroy(tsr);
360 return FALSE;
361 }
362 this->tsi->destroy(this->tsi);
363 this->tsr->destroy(this->tsr);
364 this->tsi = tsi;
365 this->tsr = tsr;
366 }
367 else
368 {
369 this->tsi = tsi;
370 this->tsr = tsr;
371 }
372 return TRUE;
373 }
374
375 METHOD(task_t, build_i, status_t,
376 private_quick_mode_t *this, message_t *message)
377 {
378 switch (this->state)
379 {
380 case QM_INIT:
381 {
382 enumerator_t *enumerator;
383 sa_payload_t *sa_payload;
384 linked_list_t *list;
385 proposal_t *proposal;
386
387 this->child_sa = child_sa_create(
388 this->ike_sa->get_my_host(this->ike_sa),
389 this->ike_sa->get_other_host(this->ike_sa),
390 this->config, 0, FALSE);
391
392 list = this->config->get_proposals(this->config, TRUE);
393
394 this->spi_i = this->child_sa->alloc_spi(this->child_sa, PROTO_ESP);
395 if (!this->spi_i)
396 {
397 DBG1(DBG_IKE, "allocating SPI from kernel failed");
398 return FAILED;
399 }
400 enumerator = list->create_enumerator(list);
401 while (enumerator->enumerate(enumerator, &proposal))
402 {
403 proposal->set_spi(proposal, this->spi_i);
404 }
405 enumerator->destroy(enumerator);
406
407 sa_payload = sa_payload_create_from_proposal_list(
408 SECURITY_ASSOCIATION_V1, list);
409 list->destroy_offset(list, offsetof(proposal_t, destroy));
410 message->add_payload(message, &sa_payload->payload_interface);
411
412 if (!add_nonce(this, &this->nonce_i, message))
413 {
414 return FAILED;
415 }
416 this->tsi = select_ts(this, TRUE);
417 this->tsr = select_ts(this, FALSE);
418 if (!this->tsi || !this->tsr)
419 {
420 return FAILED;
421 }
422 add_ts(this, message);
423 return NEED_MORE;
424 }
425 case QM_NEGOTIATED:
426 {
427 return SUCCESS;
428 }
429 default:
430 return FAILED;
431 }
432 }
433
434 METHOD(task_t, process_r, status_t,
435 private_quick_mode_t *this, message_t *message)
436 {
437 switch (this->state)
438 {
439 case QM_INIT:
440 {
441 sa_payload_t *sa_payload;
442 linked_list_t *tsi, *tsr, *list;
443 peer_cfg_t *peer_cfg;
444 host_t *me, *other;
445
446 if (!get_ts(this, message, FALSE))
447 {
448 return FAILED;
449 }
450 me = this->ike_sa->get_virtual_ip(this->ike_sa, TRUE);
451 if (!me)
452 {
453 me = this->ike_sa->get_my_host(this->ike_sa);
454 }
455 other = this->ike_sa->get_virtual_ip(this->ike_sa, FALSE);
456 if (!other)
457 {
458 other = this->ike_sa->get_other_host(this->ike_sa);
459 }
460 peer_cfg = this->ike_sa->get_peer_cfg(this->ike_sa);
461 tsi = linked_list_create();
462 tsr = linked_list_create();
463 tsi->insert_last(tsi, this->tsi);
464 tsr->insert_last(tsr, this->tsr);
465 this->config = peer_cfg->select_child_cfg(peer_cfg, tsr, tsi,
466 me, other);
467 tsi->destroy(tsi);
468 tsr->destroy(tsr);
469 if (!this->config)
470 {
471 DBG1(DBG_IKE, "no child config found");
472 return FAILED;
473 }
474
475 sa_payload = (sa_payload_t*)message->get_payload(message,
476 SECURITY_ASSOCIATION_V1);
477 if (!sa_payload)
478 {
479 DBG1(DBG_IKE, "sa payload missing");
480 return FAILED;
481 }
482 list = sa_payload->get_proposals(sa_payload);
483 this->proposal = this->config->select_proposal(this->config,
484 list, TRUE, FALSE);
485 list->destroy_offset(list, offsetof(proposal_t, destroy));
486 if (!this->proposal)
487 {
488 DBG1(DBG_IKE, "no matching proposal found");
489 return FAILED;
490 }
491 this->spi_i = this->proposal->get_spi(this->proposal);
492
493 if (!get_nonce(this, &this->nonce_i, message))
494 {
495 return FAILED;
496 }
497 this->child_sa = child_sa_create(
498 this->ike_sa->get_my_host(this->ike_sa),
499 this->ike_sa->get_other_host(this->ike_sa),
500 this->config, 0, FALSE);
501 return NEED_MORE;
502 }
503 case QM_NEGOTIATED:
504 {
505 if (!install(this))
506 {
507 return FAILED;
508 }
509 return SUCCESS;
510 }
511 default:
512 return FAILED;
513 }
514 }
515
516 METHOD(task_t, build_r, status_t,
517 private_quick_mode_t *this, message_t *message)
518 {
519 switch (this->state)
520 {
521 case QM_INIT:
522 {
523 sa_payload_t *sa_payload;
524
525 this->spi_r = this->child_sa->alloc_spi(this->child_sa, PROTO_ESP);
526 if (!this->spi_r)
527 {
528 DBG1(DBG_IKE, "allocating SPI from kernel failed");
529 return FAILED;
530 }
531 this->proposal->set_spi(this->proposal, this->spi_r);
532
533 sa_payload = sa_payload_create_from_proposal(
534 SECURITY_ASSOCIATION_V1, this->proposal);
535 message->add_payload(message, &sa_payload->payload_interface);
536
537 if (!add_nonce(this, &this->nonce_r, message))
538 {
539 return FAILED;
540 }
541 add_ts(this, message);
542
543 this->state = QM_NEGOTIATED;
544 return NEED_MORE;
545 }
546 default:
547 return FAILED;
548 }
549 }
550
551 METHOD(task_t, process_i, status_t,
552 private_quick_mode_t *this, message_t *message)
553 {
554 switch (this->state)
555 {
556 case QM_INIT:
557 {
558 sa_payload_t *sa_payload;
559 linked_list_t *list;
560
561 sa_payload = (sa_payload_t*)message->get_payload(message,
562 SECURITY_ASSOCIATION_V1);
563 if (!sa_payload)
564 {
565 DBG1(DBG_IKE, "sa payload missing");
566 return FAILED;
567 }
568 list = sa_payload->get_proposals(sa_payload);
569 this->proposal = this->config->select_proposal(this->config,
570 list, TRUE, FALSE);
571 list->destroy_offset(list, offsetof(proposal_t, destroy));
572 if (!this->proposal)
573 {
574 DBG1(DBG_IKE, "no matching proposal found");
575 return FAILED;
576 }
577 this->spi_r = this->proposal->get_spi(this->proposal);
578
579 if (!get_nonce(this, &this->nonce_r, message))
580 {
581 return FAILED;
582 }
583 if (!get_ts(this, message, TRUE))
584 {
585 return FAILED;
586 }
587 if (!install(this))
588 {
589 return FAILED;
590 }
591 this->state = QM_NEGOTIATED;
592 return NEED_MORE;
593 }
594 default:
595 return FAILED;
596 }
597 }
598
599 METHOD(task_t, get_type, task_type_t,
600 private_quick_mode_t *this)
601 {
602 return TASK_QUICK_MODE;
603 }
604
605 METHOD(task_t, migrate, void,
606 private_quick_mode_t *this, ike_sa_t *ike_sa)
607 {
608 this->ike_sa = ike_sa;
609 }
610
611 METHOD(task_t, destroy, void,
612 private_quick_mode_t *this)
613 {
614 chunk_free(&this->nonce_i);
615 chunk_free(&this->nonce_r);
616 DESTROY_IF(this->tsi);
617 DESTROY_IF(this->tsr);
618 DESTROY_IF(this->proposal);
619 DESTROY_IF(this->child_sa);
620 DESTROY_IF(this->config);
621 free(this);
622 }
623
624 /*
625 * Described in header.
626 */
627 quick_mode_t *quick_mode_create(ike_sa_t *ike_sa, child_cfg_t *config,
628 traffic_selector_t *tsi, traffic_selector_t *tsr)
629 {
630 private_quick_mode_t *this;
631
632 INIT(this,
633 .public = {
634 .task = {
635 .get_type = _get_type,
636 .migrate = _migrate,
637 .destroy = _destroy,
638 },
639 },
640 .ike_sa = ike_sa,
641 .initiator = config != NULL,
642 .config = config,
643 .keymat = (keymat_v1_t*)ike_sa->get_keymat(ike_sa),
644 .state = QM_INIT,
645 );
646
647 if (config)
648 {
649 this->public.task.build = _build_i;
650 this->public.task.process = _process_i;
651 }
652 else
653 {
654 this->public.task.build = _build_r;
655 this->public.task.process = _process_r;
656 }
657
658 return &this->public;
659 }