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