133bc296e3c755965b01ef1627b22238035c1090
[strongswan.git] / src / libcharon / sa / ikev2 / tasks / ike_mobike.c
1 /*
2 * Copyright (C) 2010-2012 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 /**
83 * read notifys from message and evaluate them
84 */
85 static void process_payloads(private_ike_mobike_t *this, message_t *message)
86 {
87 enumerator_t *enumerator;
88 payload_t *payload;
89 bool first = TRUE;
90
91 enumerator = message->create_payload_enumerator(message);
92 while (enumerator->enumerate(enumerator, &payload))
93 {
94 int family = AF_INET;
95 notify_payload_t *notify;
96 chunk_t data;
97 host_t *host;
98
99 if (payload->get_type(payload) != NOTIFY)
100 {
101 continue;
102 }
103 notify = (notify_payload_t*)payload;
104 switch (notify->get_notify_type(notify))
105 {
106 case MOBIKE_SUPPORTED:
107 {
108 peer_cfg_t *peer_cfg;
109
110 peer_cfg = this->ike_sa->get_peer_cfg(this->ike_sa);
111 if (!this->initiator &&
112 peer_cfg && !peer_cfg->use_mobike(peer_cfg))
113 {
114 DBG1(DBG_IKE, "peer supports MOBIKE, but disabled in config");
115 }
116 else
117 {
118 DBG1(DBG_IKE, "peer supports MOBIKE");
119 this->ike_sa->enable_extension(this->ike_sa, EXT_MOBIKE);
120 }
121 break;
122 }
123 case COOKIE2:
124 {
125 chunk_free(&this->cookie2);
126 this->cookie2 = chunk_clone(notify->get_notification_data(notify));
127 break;
128 }
129 case ADDITIONAL_IP6_ADDRESS:
130 {
131 family = AF_INET6;
132 /* fall through */
133 }
134 case ADDITIONAL_IP4_ADDRESS:
135 {
136 if (first)
137 { /* an ADDITIONAL_*_ADDRESS means replace, so flush once */
138 this->ike_sa->clear_peer_addresses(this->ike_sa);
139 first = FALSE;
140 /* add the peer's current address to the list */
141 host = message->get_source(message);
142 this->ike_sa->add_peer_address(this->ike_sa,
143 host->clone(host));
144 }
145 data = notify->get_notification_data(notify);
146 host = host_create_from_chunk(family, data, 0);
147 DBG2(DBG_IKE, "got additional MOBIKE peer address: %H", host);
148 this->ike_sa->add_peer_address(this->ike_sa, host);
149 this->addresses_updated = TRUE;
150 break;
151 }
152 case UPDATE_SA_ADDRESSES:
153 {
154 this->update = TRUE;
155 break;
156 }
157 case NO_ADDITIONAL_ADDRESSES:
158 {
159 this->ike_sa->clear_peer_addresses(this->ike_sa);
160 /* add the peer's current address to the list */
161 host = message->get_source(message);
162 this->ike_sa->add_peer_address(this->ike_sa, host->clone(host));
163 this->addresses_updated = TRUE;
164 break;
165 }
166 case NAT_DETECTION_SOURCE_IP:
167 case NAT_DETECTION_DESTINATION_IP:
168 {
169 /* NAT check in this MOBIKE exchange, create subtask for it */
170 if (this->natd == NULL)
171 {
172 this->natd = ike_natd_create(this->ike_sa, this->initiator);
173 }
174 break;
175 }
176 default:
177 break;
178 }
179 }
180 enumerator->destroy(enumerator);
181 }
182
183 /**
184 * Add ADDITIONAL_*_ADDRESS notifys depending on our address list
185 */
186 static void build_address_list(private_ike_mobike_t *this, message_t *message)
187 {
188 enumerator_t *enumerator;
189 host_t *host, *me;
190 notify_type_t type;
191 int added = 0;
192
193 me = this->ike_sa->get_my_host(this->ike_sa);
194 enumerator = hydra->kernel_interface->create_address_enumerator(
195 hydra->kernel_interface, FALSE, FALSE);
196 while (enumerator->enumerate(enumerator, (void**)&host))
197 {
198 if (me->ip_equals(me, host))
199 { /* "ADDITIONAL" means do not include IKE_SAs host */
200 continue;
201 }
202 switch (host->get_family(host))
203 {
204 case AF_INET:
205 type = ADDITIONAL_IP4_ADDRESS;
206 break;
207 case AF_INET6:
208 type = ADDITIONAL_IP6_ADDRESS;
209 break;
210 default:
211 continue;
212 }
213 message->add_notify(message, FALSE, type, host->get_address(host));
214 if (++added >= MAX_ADDITIONAL_ADDRS)
215 { /* limit number of notifys, some implementations do not like too
216 * many of them (f.e. strongSwan ;-) */
217 break;
218 }
219 }
220 if (!added)
221 {
222 message->add_notify(message, FALSE, NO_ADDITIONAL_ADDRESSES, chunk_empty);
223 }
224 enumerator->destroy(enumerator);
225 }
226
227 /**
228 * build a cookie and add it to the message
229 */
230 static bool build_cookie(private_ike_mobike_t *this, message_t *message)
231 {
232 rng_t *rng;
233
234 chunk_free(&this->cookie2);
235 rng = lib->crypto->create_rng(lib->crypto, RNG_STRONG);
236 if (!rng || !rng->allocate_bytes(rng, COOKIE2_SIZE, &this->cookie2))
237 {
238 DESTROY_IF(rng);
239 return FALSE;
240 }
241 message->add_notify(message, FALSE, COOKIE2, this->cookie2);
242 rng->destroy(rng);
243 return TRUE;
244 }
245
246 /**
247 * update addresses of associated CHILD_SAs
248 */
249 static void update_children(private_ike_mobike_t *this)
250 {
251 enumerator_t *enumerator;
252 child_sa_t *child_sa;
253
254 enumerator = this->ike_sa->create_child_sa_enumerator(this->ike_sa);
255 while (enumerator->enumerate(enumerator, (void**)&child_sa))
256 {
257 if (child_sa->update(child_sa,
258 this->ike_sa->get_my_host(this->ike_sa),
259 this->ike_sa->get_other_host(this->ike_sa),
260 this->ike_sa->get_virtual_ip(this->ike_sa, TRUE),
261 this->ike_sa->has_condition(this->ike_sa, COND_NAT_ANY)) == NOT_SUPPORTED)
262 {
263 this->ike_sa->rekey_child_sa(this->ike_sa,
264 child_sa->get_protocol(child_sa),
265 child_sa->get_spi(child_sa, TRUE));
266 }
267 }
268 enumerator->destroy(enumerator);
269 }
270
271 /**
272 * Apply the port of the old host, if its ip equals the new, use port otherwise.
273 */
274 static void apply_port(host_t *host, host_t *old, u_int16_t port, bool local)
275 {
276 if (host->ip_equals(host, old))
277 {
278 port = old->get_port(old);
279 }
280 else if (local && port == charon->socket->get_port(charon->socket, FALSE))
281 {
282 port = charon->socket->get_port(charon->socket, TRUE);
283 }
284 else if (!local && port == IKEV2_UDP_PORT)
285 {
286 port = IKEV2_NATT_PORT;
287 }
288 host->set_port(host, port);
289 }
290
291 METHOD(ike_mobike_t, transmit, void,
292 private_ike_mobike_t *this, packet_t *packet)
293 {
294 host_t *me, *other, *me_old, *other_old;
295 enumerator_t *enumerator;
296 ike_cfg_t *ike_cfg;
297 packet_t *copy;
298
299 if (!this->check)
300 {
301 return;
302 }
303
304 me_old = this->ike_sa->get_my_host(this->ike_sa);
305 other_old = this->ike_sa->get_other_host(this->ike_sa);
306 ike_cfg = this->ike_sa->get_ike_cfg(this->ike_sa);
307
308 enumerator = this->ike_sa->create_peer_address_enumerator(this->ike_sa);
309 while (enumerator->enumerate(enumerator, (void**)&other))
310 {
311 me = hydra->kernel_interface->get_source_addr(
312 hydra->kernel_interface, other, NULL);
313 if (me)
314 {
315 if (me->get_family(me) != other->get_family(other))
316 {
317 me->destroy(me);
318 continue;
319 }
320 /* reuse port for an active address, 4500 otherwise */
321 apply_port(me, me_old, ike_cfg->get_my_port(ike_cfg), TRUE);
322 other = other->clone(other);
323 apply_port(other, other_old, ike_cfg->get_other_port(ike_cfg), FALSE);
324 DBG1(DBG_IKE, "checking path %#H - %#H", me, other);
325 copy = packet->clone(packet);
326 copy->set_source(copy, me);
327 copy->set_destination(copy, other);
328 charon->sender->send(charon->sender, copy);
329 }
330 }
331 enumerator->destroy(enumerator);
332 }
333
334 METHOD(task_t, build_i, status_t,
335 private_ike_mobike_t *this, message_t *message)
336 {
337 if (message->get_exchange_type(message) == IKE_AUTH &&
338 message->get_message_id(message) == 1)
339 { /* only in first IKE_AUTH */
340 message->add_notify(message, FALSE, MOBIKE_SUPPORTED, chunk_empty);
341 build_address_list(this, message);
342 }
343 else if (message->get_exchange_type(message) == INFORMATIONAL)
344 {
345 host_t *old, *new;
346
347 /* we check if the existing address is still valid */
348 old = message->get_source(message);
349 new = hydra->kernel_interface->get_source_addr(hydra->kernel_interface,
350 message->get_destination(message), old);
351 if (new)
352 {
353 if (!new->ip_equals(new, old))
354 {
355 new->set_port(new, old->get_port(old));
356 message->set_source(message, new);
357 }
358 else
359 {
360 new->destroy(new);
361 }
362 }
363 if (this->update)
364 {
365 message->add_notify(message, FALSE, UPDATE_SA_ADDRESSES,
366 chunk_empty);
367 if (!build_cookie(this, message))
368 {
369 return FAILED;
370 }
371 update_children(this);
372 }
373 if (this->address && !this->check)
374 {
375 build_address_list(this, message);
376 }
377 if (this->natd)
378 {
379 this->natd->task.build(&this->natd->task, message);
380 }
381 }
382 return NEED_MORE;
383 }
384
385 METHOD(task_t, process_r, status_t,
386 private_ike_mobike_t *this, message_t *message)
387 {
388 if (message->get_exchange_type(message) == IKE_AUTH &&
389 message->get_message_id(message) == 1)
390 { /* only first IKE_AUTH */
391 process_payloads(this, message);
392 }
393 else if (message->get_exchange_type(message) == INFORMATIONAL)
394 {
395 process_payloads(this, message);
396 if (this->update)
397 {
398 host_t *me, *other;
399
400 me = message->get_destination(message);
401 other = message->get_source(message);
402 this->ike_sa->set_my_host(this->ike_sa, me->clone(me));
403 this->ike_sa->set_other_host(this->ike_sa, other->clone(other));
404 }
405
406 if (this->natd)
407 {
408 this->natd->task.process(&this->natd->task, message);
409 }
410 if (this->addresses_updated && this->ike_sa->has_condition(this->ike_sa,
411 COND_ORIGINAL_INITIATOR))
412 {
413 host_t *other = message->get_source(message);
414 host_t *other_old = this->ike_sa->get_other_host(this->ike_sa);
415 if (!other->equals(other, other_old))
416 {
417 DBG1(DBG_IKE, "remote address changed from %H to %H", other_old,
418 other);
419 this->ike_sa->set_other_host(this->ike_sa, other->clone(other));
420 this->update = TRUE;
421 }
422 }
423 }
424 return NEED_MORE;
425 }
426
427 METHOD(task_t, build_r, status_t,
428 private_ike_mobike_t *this, message_t *message)
429 {
430 if (message->get_exchange_type(message) == IKE_AUTH &&
431 this->ike_sa->get_state(this->ike_sa) == IKE_ESTABLISHED)
432 {
433 if (this->ike_sa->supports_extension(this->ike_sa, EXT_MOBIKE))
434 {
435 message->add_notify(message, FALSE, MOBIKE_SUPPORTED, chunk_empty);
436 build_address_list(this, message);
437 }
438 return SUCCESS;
439 }
440 else if (message->get_exchange_type(message) == INFORMATIONAL)
441 {
442 if (this->natd)
443 {
444 this->natd->task.build(&this->natd->task, message);
445 }
446 if (this->cookie2.ptr)
447 {
448 message->add_notify(message, FALSE, COOKIE2, this->cookie2);
449 chunk_free(&this->cookie2);
450 }
451 if (this->update)
452 {
453 update_children(this);
454 }
455 return SUCCESS;
456 }
457 return NEED_MORE;
458 }
459
460 METHOD(task_t, process_i, status_t,
461 private_ike_mobike_t *this, message_t *message)
462 {
463 if (message->get_exchange_type(message) == IKE_AUTH &&
464 this->ike_sa->get_state(this->ike_sa) == IKE_ESTABLISHED)
465 {
466 process_payloads(this, message);
467 return SUCCESS;
468 }
469 else if (message->get_exchange_type(message) == INFORMATIONAL)
470 {
471 u_int32_t updates = this->ike_sa->get_pending_updates(this->ike_sa) - 1;
472 this->ike_sa->set_pending_updates(this->ike_sa, updates);
473 if (updates > 0)
474 {
475 /* newer update queued, ignore this one */
476 return SUCCESS;
477 }
478 if (this->cookie2.ptr)
479 { /* check cookie if we included one */
480 chunk_t cookie2;
481
482 cookie2 = this->cookie2;
483 this->cookie2 = chunk_empty;
484 process_payloads(this, message);
485 if (!chunk_equals(cookie2, this->cookie2))
486 {
487 chunk_free(&cookie2);
488 DBG1(DBG_IKE, "COOKIE2 mismatch, closing IKE_SA");
489 return FAILED;
490 }
491 chunk_free(&cookie2);
492 }
493 else
494 {
495 process_payloads(this, message);
496 }
497 if (this->natd)
498 {
499 this->natd->task.process(&this->natd->task, message);
500 if (this->natd->has_mapping_changed(this->natd))
501 {
502 /* force an update if mappings have changed */
503 this->update = this->check = TRUE;
504 DBG1(DBG_IKE, "detected changes in NAT mappings, "
505 "initiating MOBIKE update");
506 }
507 }
508 if (this->update)
509 {
510 /* update again, as NAT state may have changed */
511 update_children(this);
512 }
513 if (this->check)
514 {
515 host_t *me_new, *me_old, *other_new, *other_old;
516
517 me_new = message->get_destination(message);
518 other_new = message->get_source(message);
519 me_old = this->ike_sa->get_my_host(this->ike_sa);
520 other_old = this->ike_sa->get_other_host(this->ike_sa);
521
522 if (!me_new->equals(me_new, me_old))
523 {
524 this->update = TRUE;
525 this->ike_sa->set_my_host(this->ike_sa, me_new->clone(me_new));
526 }
527 if (!other_new->equals(other_new, other_old))
528 {
529 this->update = TRUE;
530 this->ike_sa->set_other_host(this->ike_sa, other_new->clone(other_new));
531 }
532 if (this->update)
533 {
534 /* use the same task to ... */
535 if (!this->ike_sa->has_condition(this->ike_sa,
536 COND_ORIGINAL_INITIATOR))
537 { /*... send an updated list of addresses as responder */
538 update_children(this);
539 this->update = FALSE;
540 }
541 else
542 { /* ... send the update as original initiator */
543 if (this->natd)
544 {
545 this->natd->task.destroy(&this->natd->task);
546 }
547 this->natd = ike_natd_create(this->ike_sa, this->initiator);
548 }
549 this->check = FALSE;
550 this->ike_sa->set_pending_updates(this->ike_sa, 1);
551 return NEED_MORE;
552 }
553 }
554 return SUCCESS;
555 }
556 return NEED_MORE;
557 }
558
559 METHOD(ike_mobike_t, addresses, void,
560 private_ike_mobike_t *this)
561 {
562 this->address = TRUE;
563 this->ike_sa->set_pending_updates(this->ike_sa,
564 this->ike_sa->get_pending_updates(this->ike_sa) + 1);
565 }
566
567 METHOD(ike_mobike_t, roam, void,
568 private_ike_mobike_t *this, bool address)
569 {
570 this->check = TRUE;
571 this->address = address;
572 this->ike_sa->set_pending_updates(this->ike_sa,
573 this->ike_sa->get_pending_updates(this->ike_sa) + 1);
574 }
575
576 METHOD(ike_mobike_t, dpd, void,
577 private_ike_mobike_t *this)
578 {
579 if (!this->natd)
580 {
581 this->natd = ike_natd_create(this->ike_sa, this->initiator);
582 }
583 this->ike_sa->set_pending_updates(this->ike_sa,
584 this->ike_sa->get_pending_updates(this->ike_sa) + 1);
585 }
586
587 METHOD(ike_mobike_t, is_probing, bool,
588 private_ike_mobike_t *this)
589 {
590 return this->check;
591 }
592
593 METHOD(task_t, get_type, task_type_t,
594 private_ike_mobike_t *this)
595 {
596 return TASK_IKE_MOBIKE;
597 }
598
599 METHOD(task_t, migrate, void,
600 private_ike_mobike_t *this, ike_sa_t *ike_sa)
601 {
602 chunk_free(&this->cookie2);
603 this->ike_sa = ike_sa;
604 if (this->natd)
605 {
606 this->natd->task.migrate(&this->natd->task, ike_sa);
607 }
608 }
609
610 METHOD(task_t, destroy, void,
611 private_ike_mobike_t *this)
612 {
613 chunk_free(&this->cookie2);
614 if (this->natd)
615 {
616 this->natd->task.destroy(&this->natd->task);
617 }
618 free(this);
619 }
620
621 /*
622 * Described in header.
623 */
624 ike_mobike_t *ike_mobike_create(ike_sa_t *ike_sa, bool initiator)
625 {
626 private_ike_mobike_t *this;
627
628 INIT(this,
629 .public = {
630 .task = {
631 .get_type = _get_type,
632 .migrate = _migrate,
633 .destroy = _destroy,
634 },
635 .addresses = _addresses,
636 .roam = _roam,
637 .dpd = _dpd,
638 .transmit = _transmit,
639 .is_probing = _is_probing,
640 },
641 .ike_sa = ike_sa,
642 .initiator = initiator,
643 );
644
645 if (initiator)
646 {
647 this->public.task.build = _build_i;
648 this->public.task.process = _process_i;
649 }
650 else
651 {
652 this->public.task.build = _build_r;
653 this->public.task.process = _process_r;
654 }
655
656 return &this->public;
657 }