d91fa5862013bce796120bea627f6f3f08c3ee02
[strongswan.git] / src / libcharon / sa / ikev2 / tasks / ike_mobike.c
1 /*
2 * Copyright (C) 2010-2014 Tobias Brunner
3 * Copyright (C) 2007 Martin Willi
4 * Hochschule fuer Technik Rapperswil
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 */
16
17 #include "ike_mobike.h"
18
19 #include <string.h>
20
21 #include <hydra.h>
22 #include <daemon.h>
23 #include <sa/ikev2/tasks/ike_natd.h>
24 #include <encoding/payloads/notify_payload.h>
25
26 #define COOKIE2_SIZE 16
27 #define MAX_ADDITIONAL_ADDRS 8
28
29 typedef struct private_ike_mobike_t private_ike_mobike_t;
30
31 /**
32 * Private members of a ike_mobike_t task.
33 */
34 struct private_ike_mobike_t {
35
36 /**
37 * Public methods and task_t interface.
38 */
39 ike_mobike_t public;
40
41 /**
42 * Assigned IKE_SA.
43 */
44 ike_sa_t *ike_sa;
45
46 /**
47 * Are we the initiator?
48 */
49 bool initiator;
50
51 /**
52 * cookie2 value to verify new addresses
53 */
54 chunk_t cookie2;
55
56 /**
57 * NAT discovery reusing the TASK_IKE_NATD task
58 */
59 ike_natd_t *natd;
60
61 /**
62 * use task to update addresses
63 */
64 bool update;
65
66 /**
67 * do routability check
68 */
69 bool check;
70
71 /**
72 * include address list update
73 */
74 bool address;
75
76 /**
77 * additional addresses got updated
78 */
79 bool addresses_updated;
80
81 /**
82 * whether the pending updates counter was increased
83 */
84 bool pending_update;
85 };
86
87 /**
88 * read notifys from message and evaluate them
89 */
90 static void process_payloads(private_ike_mobike_t *this, message_t *message)
91 {
92 enumerator_t *enumerator;
93 payload_t *payload;
94 bool first = TRUE;
95
96 enumerator = message->create_payload_enumerator(message);
97 while (enumerator->enumerate(enumerator, &payload))
98 {
99 int family = AF_INET;
100 notify_payload_t *notify;
101 chunk_t data;
102 host_t *host;
103
104 if (payload->get_type(payload) != PLV2_NOTIFY)
105 {
106 continue;
107 }
108 notify = (notify_payload_t*)payload;
109 switch (notify->get_notify_type(notify))
110 {
111 case MOBIKE_SUPPORTED:
112 {
113 peer_cfg_t *peer_cfg;
114
115 peer_cfg = this->ike_sa->get_peer_cfg(this->ike_sa);
116 if (!this->initiator &&
117 peer_cfg && !peer_cfg->use_mobike(peer_cfg))
118 {
119 DBG1(DBG_IKE, "peer supports MOBIKE, but disabled in config");
120 }
121 else
122 {
123 DBG1(DBG_IKE, "peer supports MOBIKE");
124 this->ike_sa->enable_extension(this->ike_sa, EXT_MOBIKE);
125 }
126 break;
127 }
128 case COOKIE2:
129 {
130 chunk_free(&this->cookie2);
131 this->cookie2 = chunk_clone(notify->get_notification_data(notify));
132 break;
133 }
134 case ADDITIONAL_IP6_ADDRESS:
135 {
136 family = AF_INET6;
137 /* fall through */
138 }
139 case ADDITIONAL_IP4_ADDRESS:
140 {
141 if (first)
142 { /* an ADDITIONAL_*_ADDRESS means replace, so flush once */
143 this->ike_sa->clear_peer_addresses(this->ike_sa);
144 first = FALSE;
145 /* add the peer's current address to the list */
146 host = message->get_source(message);
147 this->ike_sa->add_peer_address(this->ike_sa,
148 host->clone(host));
149 }
150 data = notify->get_notification_data(notify);
151 host = host_create_from_chunk(family, data, 0);
152 DBG2(DBG_IKE, "got additional MOBIKE peer address: %H", host);
153 this->ike_sa->add_peer_address(this->ike_sa, host);
154 this->addresses_updated = TRUE;
155 break;
156 }
157 case UPDATE_SA_ADDRESSES:
158 {
159 this->update = TRUE;
160 break;
161 }
162 case NO_ADDITIONAL_ADDRESSES:
163 {
164 this->ike_sa->clear_peer_addresses(this->ike_sa);
165 /* add the peer's current address to the list */
166 host = message->get_source(message);
167 this->ike_sa->add_peer_address(this->ike_sa, host->clone(host));
168 this->addresses_updated = TRUE;
169 break;
170 }
171 case NAT_DETECTION_SOURCE_IP:
172 case NAT_DETECTION_DESTINATION_IP:
173 {
174 /* NAT check in this MOBIKE exchange, create subtask for it */
175 if (this->natd == NULL)
176 {
177 this->natd = ike_natd_create(this->ike_sa, this->initiator);
178 }
179 break;
180 }
181 default:
182 break;
183 }
184 }
185 enumerator->destroy(enumerator);
186 }
187
188 /**
189 * Add ADDITIONAL_*_ADDRESS notifys depending on our address list
190 */
191 static void build_address_list(private_ike_mobike_t *this, message_t *message)
192 {
193 enumerator_t *enumerator;
194 host_t *host, *me;
195 notify_type_t type;
196 int added = 0;
197
198 me = this->ike_sa->get_my_host(this->ike_sa);
199 enumerator = hydra->kernel_interface->create_address_enumerator(
200 hydra->kernel_interface, ADDR_TYPE_REGULAR);
201 while (enumerator->enumerate(enumerator, (void**)&host))
202 {
203 if (me->ip_equals(me, host))
204 { /* "ADDITIONAL" means do not include IKE_SAs host */
205 continue;
206 }
207 switch (host->get_family(host))
208 {
209 case AF_INET:
210 type = ADDITIONAL_IP4_ADDRESS;
211 break;
212 case AF_INET6:
213 type = ADDITIONAL_IP6_ADDRESS;
214 break;
215 default:
216 continue;
217 }
218 message->add_notify(message, FALSE, type, host->get_address(host));
219 if (++added >= MAX_ADDITIONAL_ADDRS)
220 { /* limit number of notifys, some implementations do not like too
221 * many of them (f.e. strongSwan ;-) */
222 break;
223 }
224 }
225 if (!added)
226 {
227 message->add_notify(message, FALSE, NO_ADDITIONAL_ADDRESSES, chunk_empty);
228 }
229 enumerator->destroy(enumerator);
230 }
231
232 /**
233 * build a cookie and add it to the message
234 */
235 static bool build_cookie(private_ike_mobike_t *this, message_t *message)
236 {
237 rng_t *rng;
238
239 chunk_free(&this->cookie2);
240 rng = lib->crypto->create_rng(lib->crypto, RNG_STRONG);
241 if (!rng || !rng->allocate_bytes(rng, COOKIE2_SIZE, &this->cookie2))
242 {
243 DESTROY_IF(rng);
244 return FALSE;
245 }
246 message->add_notify(message, FALSE, COOKIE2, this->cookie2);
247 rng->destroy(rng);
248 return TRUE;
249 }
250
251 /**
252 * update addresses of associated CHILD_SAs
253 */
254 static void update_children(private_ike_mobike_t *this)
255 {
256 enumerator_t *enumerator;
257 child_sa_t *child_sa;
258 linked_list_t *vips;
259 host_t *host;
260
261 vips = linked_list_create();
262
263 enumerator = this->ike_sa->create_virtual_ip_enumerator(this->ike_sa, TRUE);
264 while (enumerator->enumerate(enumerator, &host))
265 {
266 vips->insert_last(vips, host);
267 }
268 enumerator->destroy(enumerator);
269
270 enumerator = this->ike_sa->create_child_sa_enumerator(this->ike_sa);
271 while (enumerator->enumerate(enumerator, (void**)&child_sa))
272 {
273 if (child_sa->update(child_sa,
274 this->ike_sa->get_my_host(this->ike_sa),
275 this->ike_sa->get_other_host(this->ike_sa), vips,
276 this->ike_sa->has_condition(this->ike_sa,
277 COND_NAT_ANY)) == NOT_SUPPORTED)
278 {
279 this->ike_sa->rekey_child_sa(this->ike_sa,
280 child_sa->get_protocol(child_sa),
281 child_sa->get_spi(child_sa, TRUE));
282 }
283 }
284 enumerator->destroy(enumerator);
285
286 vips->destroy(vips);
287 }
288
289 /**
290 * Apply the port of the old host, if its ip equals the new, use port otherwise.
291 */
292 static void apply_port(host_t *host, host_t *old, u_int16_t port, bool local)
293 {
294 if (host->ip_equals(host, old))
295 {
296 port = old->get_port(old);
297 }
298 else if (local && port == charon->socket->get_port(charon->socket, FALSE))
299 {
300 port = charon->socket->get_port(charon->socket, TRUE);
301 }
302 else if (!local && port == IKEV2_UDP_PORT)
303 {
304 port = IKEV2_NATT_PORT;
305 }
306 host->set_port(host, port);
307 }
308
309 METHOD(ike_mobike_t, transmit, bool,
310 private_ike_mobike_t *this, packet_t *packet)
311 {
312 host_t *me, *other, *me_old, *other_old;
313 enumerator_t *enumerator;
314 ike_cfg_t *ike_cfg;
315 packet_t *copy;
316 int family = AF_UNSPEC;
317 bool found = FALSE;
318
319 me_old = this->ike_sa->get_my_host(this->ike_sa);
320 other_old = this->ike_sa->get_other_host(this->ike_sa);
321 ike_cfg = this->ike_sa->get_ike_cfg(this->ike_sa);
322
323 if (!this->check)
324 {
325 me = hydra->kernel_interface->get_source_addr(hydra->kernel_interface,
326 other_old, me_old);
327 if (me)
328 {
329 if (me->ip_equals(me, me_old))
330 {
331 charon->sender->send(charon->sender, packet->clone(packet));
332 me->destroy(me);
333 return TRUE;
334 }
335 me->destroy(me);
336 }
337 this->check = TRUE;
338 }
339
340 switch (charon->socket->supported_families(charon->socket))
341 {
342 case SOCKET_FAMILY_IPV4:
343 family = AF_INET;
344 break;
345 case SOCKET_FAMILY_IPV6:
346 family = AF_INET6;
347 break;
348 case SOCKET_FAMILY_BOTH:
349 case SOCKET_FAMILY_NONE:
350 break;
351 }
352
353 enumerator = this->ike_sa->create_peer_address_enumerator(this->ike_sa);
354 while (enumerator->enumerate(enumerator, (void**)&other))
355 {
356 if (family != AF_UNSPEC && other->get_family(other) != family)
357 {
358 continue;
359 }
360 me = hydra->kernel_interface->get_source_addr(
361 hydra->kernel_interface, other, NULL);
362 if (me)
363 {
364 /* reuse port for an active address, 4500 otherwise */
365 apply_port(me, me_old, ike_cfg->get_my_port(ike_cfg), TRUE);
366 other = other->clone(other);
367 apply_port(other, other_old, ike_cfg->get_other_port(ike_cfg), FALSE);
368 DBG1(DBG_IKE, "checking path %#H - %#H", me, other);
369 copy = packet->clone(packet);
370 copy->set_source(copy, me);
371 copy->set_destination(copy, other);
372 charon->sender->send(charon->sender, copy);
373 found = TRUE;
374 }
375 }
376 enumerator->destroy(enumerator);
377 return found;
378 }
379
380 METHOD(task_t, build_i, status_t,
381 private_ike_mobike_t *this, message_t *message)
382 {
383 if (message->get_exchange_type(message) == IKE_AUTH &&
384 message->get_message_id(message) == 1)
385 { /* only in first IKE_AUTH */
386 message->add_notify(message, FALSE, MOBIKE_SUPPORTED, chunk_empty);
387 build_address_list(this, message);
388 }
389 else if (message->get_exchange_type(message) == INFORMATIONAL)
390 {
391 host_t *old, *new;
392
393 /* we check if the existing address is still valid */
394 old = message->get_source(message);
395 new = hydra->kernel_interface->get_source_addr(hydra->kernel_interface,
396 message->get_destination(message), old);
397 if (new)
398 {
399 if (!new->ip_equals(new, old))
400 {
401 new->set_port(new, old->get_port(old));
402 message->set_source(message, new);
403 }
404 else
405 {
406 new->destroy(new);
407 }
408 }
409 if (this->update)
410 {
411 message->add_notify(message, FALSE, UPDATE_SA_ADDRESSES,
412 chunk_empty);
413 if (!build_cookie(this, message))
414 {
415 return FAILED;
416 }
417 update_children(this);
418 }
419 if (this->address && !this->check)
420 {
421 build_address_list(this, message);
422 }
423 if (this->natd)
424 {
425 this->natd->task.build(&this->natd->task, message);
426 }
427 }
428 return NEED_MORE;
429 }
430
431 METHOD(task_t, process_r, status_t,
432 private_ike_mobike_t *this, message_t *message)
433 {
434 if (message->get_exchange_type(message) == IKE_AUTH &&
435 message->get_message_id(message) == 1)
436 { /* only first IKE_AUTH */
437 process_payloads(this, message);
438 }
439 else if (message->get_exchange_type(message) == INFORMATIONAL)
440 {
441 process_payloads(this, message);
442 if (this->update)
443 {
444 host_t *me, *other;
445
446 me = message->get_destination(message);
447 other = message->get_source(message);
448 this->ike_sa->set_my_host(this->ike_sa, me->clone(me));
449 this->ike_sa->set_other_host(this->ike_sa, other->clone(other));
450 }
451
452 if (this->natd)
453 {
454 this->natd->task.process(&this->natd->task, message);
455 }
456 if (this->addresses_updated && this->ike_sa->has_condition(this->ike_sa,
457 COND_ORIGINAL_INITIATOR))
458 {
459 host_t *other = message->get_source(message);
460 host_t *other_old = this->ike_sa->get_other_host(this->ike_sa);
461 if (!other->equals(other, other_old))
462 {
463 DBG1(DBG_IKE, "remote address changed from %H to %H", other_old,
464 other);
465 this->ike_sa->set_other_host(this->ike_sa, other->clone(other));
466 this->update = TRUE;
467 }
468 }
469 }
470 return NEED_MORE;
471 }
472
473 METHOD(task_t, build_r, status_t,
474 private_ike_mobike_t *this, message_t *message)
475 {
476 if (message->get_exchange_type(message) == IKE_AUTH &&
477 this->ike_sa->get_state(this->ike_sa) == IKE_ESTABLISHED)
478 {
479 if (this->ike_sa->supports_extension(this->ike_sa, EXT_MOBIKE))
480 {
481 message->add_notify(message, FALSE, MOBIKE_SUPPORTED, chunk_empty);
482 build_address_list(this, message);
483 }
484 return SUCCESS;
485 }
486 else if (message->get_exchange_type(message) == INFORMATIONAL)
487 {
488 if (this->natd)
489 {
490 this->natd->task.build(&this->natd->task, message);
491 }
492 if (this->cookie2.ptr)
493 {
494 message->add_notify(message, FALSE, COOKIE2, this->cookie2);
495 chunk_free(&this->cookie2);
496 }
497 if (this->update)
498 {
499 update_children(this);
500 }
501 return SUCCESS;
502 }
503 return NEED_MORE;
504 }
505
506 METHOD(task_t, process_i, status_t,
507 private_ike_mobike_t *this, message_t *message)
508 {
509 if (message->get_exchange_type(message) == IKE_AUTH &&
510 this->ike_sa->get_state(this->ike_sa) == IKE_ESTABLISHED)
511 {
512 process_payloads(this, message);
513 return SUCCESS;
514 }
515 else if (message->get_exchange_type(message) == INFORMATIONAL)
516 {
517 if (this->ike_sa->get_pending_updates(this->ike_sa) > 1)
518 {
519 /* newer update queued, ignore this one */
520 return SUCCESS;
521 }
522 if (this->cookie2.ptr)
523 { /* check cookie if we included one */
524 chunk_t cookie2;
525
526 cookie2 = this->cookie2;
527 this->cookie2 = chunk_empty;
528 process_payloads(this, message);
529 if (!chunk_equals(cookie2, this->cookie2))
530 {
531 chunk_free(&cookie2);
532 DBG1(DBG_IKE, "COOKIE2 mismatch, closing IKE_SA");
533 return FAILED;
534 }
535 chunk_free(&cookie2);
536 }
537 else
538 {
539 process_payloads(this, message);
540 }
541 if (this->natd)
542 {
543 this->natd->task.process(&this->natd->task, message);
544 if (this->natd->has_mapping_changed(this->natd))
545 {
546 /* force an update if mappings have changed */
547 this->update = this->check = TRUE;
548 DBG1(DBG_IKE, "detected changes in NAT mappings, "
549 "initiating MOBIKE update");
550 }
551 }
552 if (this->update)
553 {
554 /* update again, as NAT state may have changed */
555 update_children(this);
556 }
557 if (this->check)
558 {
559 host_t *me_new, *me_old, *other_new, *other_old;
560
561 me_new = message->get_destination(message);
562 other_new = message->get_source(message);
563 me_old = this->ike_sa->get_my_host(this->ike_sa);
564 other_old = this->ike_sa->get_other_host(this->ike_sa);
565
566 if (!me_new->equals(me_new, me_old))
567 {
568 this->update = TRUE;
569 this->ike_sa->set_my_host(this->ike_sa, me_new->clone(me_new));
570 }
571 if (!other_new->equals(other_new, other_old))
572 {
573 this->update = TRUE;
574 this->ike_sa->set_other_host(this->ike_sa, other_new->clone(other_new));
575 }
576 if (this->update)
577 {
578 /* use the same task to ... */
579 if (!this->ike_sa->has_condition(this->ike_sa,
580 COND_ORIGINAL_INITIATOR))
581 { /*... send an updated list of addresses as responder */
582 update_children(this);
583 this->update = FALSE;
584 }
585 else
586 { /* ... send the update as original initiator */
587 if (this->natd)
588 {
589 this->natd->task.destroy(&this->natd->task);
590 }
591 this->natd = ike_natd_create(this->ike_sa, this->initiator);
592 }
593 this->check = FALSE;
594 return NEED_MORE;
595 }
596 }
597 return SUCCESS;
598 }
599 return NEED_MORE;
600 }
601
602 METHOD(ike_mobike_t, addresses, void,
603 private_ike_mobike_t *this)
604 {
605 this->address = TRUE;
606 if (!this->pending_update)
607 {
608 this->pending_update = TRUE;
609 this->ike_sa->set_pending_updates(this->ike_sa,
610 this->ike_sa->get_pending_updates(this->ike_sa) + 1);
611 }
612 }
613
614 METHOD(ike_mobike_t, roam, void,
615 private_ike_mobike_t *this, bool address)
616 {
617 this->check = TRUE;
618 this->address = address;
619 if (!this->pending_update)
620 {
621 this->pending_update = TRUE;
622 this->ike_sa->set_pending_updates(this->ike_sa,
623 this->ike_sa->get_pending_updates(this->ike_sa) + 1);
624 }
625 }
626
627 METHOD(ike_mobike_t, dpd, void,
628 private_ike_mobike_t *this)
629 {
630 if (!this->natd)
631 {
632 this->natd = ike_natd_create(this->ike_sa, this->initiator);
633 }
634 if (!this->pending_update)
635 {
636 this->pending_update = TRUE;
637 this->ike_sa->set_pending_updates(this->ike_sa,
638 this->ike_sa->get_pending_updates(this->ike_sa) + 1);
639 }
640 }
641
642 METHOD(ike_mobike_t, is_probing, bool,
643 private_ike_mobike_t *this)
644 {
645 return this->check;
646 }
647
648 METHOD(ike_mobike_t, enable_probing, void,
649 private_ike_mobike_t *this)
650 {
651 this->check = TRUE;
652 }
653
654 METHOD(task_t, get_type, task_type_t,
655 private_ike_mobike_t *this)
656 {
657 return TASK_IKE_MOBIKE;
658 }
659
660 METHOD(task_t, migrate, void,
661 private_ike_mobike_t *this, ike_sa_t *ike_sa)
662 {
663 chunk_free(&this->cookie2);
664 this->ike_sa = ike_sa;
665 if (this->natd)
666 {
667 this->natd->task.migrate(&this->natd->task, ike_sa);
668 }
669 if (this->pending_update)
670 {
671 this->ike_sa->set_pending_updates(this->ike_sa,
672 this->ike_sa->get_pending_updates(this->ike_sa) + 1);
673 }
674 }
675
676 METHOD(task_t, destroy, void,
677 private_ike_mobike_t *this)
678 {
679 if (this->pending_update)
680 {
681 this->ike_sa->set_pending_updates(this->ike_sa,
682 this->ike_sa->get_pending_updates(this->ike_sa) - 1);
683 }
684 chunk_free(&this->cookie2);
685 if (this->natd)
686 {
687 this->natd->task.destroy(&this->natd->task);
688 }
689 free(this);
690 }
691
692 /*
693 * Described in header.
694 */
695 ike_mobike_t *ike_mobike_create(ike_sa_t *ike_sa, bool initiator)
696 {
697 private_ike_mobike_t *this;
698
699 INIT(this,
700 .public = {
701 .task = {
702 .get_type = _get_type,
703 .migrate = _migrate,
704 .destroy = _destroy,
705 },
706 .addresses = _addresses,
707 .roam = _roam,
708 .dpd = _dpd,
709 .transmit = _transmit,
710 .is_probing = _is_probing,
711 .enable_probing = _enable_probing,
712 },
713 .ike_sa = ike_sa,
714 .initiator = initiator,
715 );
716
717 if (initiator)
718 {
719 this->public.task.build = _build_i;
720 this->public.task.process = _process_i;
721 }
722 else
723 {
724 this->public.task.build = _build_r;
725 this->public.task.process = _process_r;
726 }
727
728 return &this->public;
729 }