da693f2b9c23de488ad8925b696195e60b3824c1
[strongswan.git] / src / libcharon / sa / 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/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 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 = this->ike_sa->get_other_host(this->ike_sa);
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 = this->ike_sa->get_other_host(this->ike_sa);
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 void 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)
237 {
238 rng->allocate_bytes(rng, COOKIE2_SIZE, &this->cookie2);
239 rng->destroy(rng);
240 message->add_notify(message, FALSE, COOKIE2, this->cookie2);
241 }
242 }
243
244 /**
245 * update addresses of associated CHILD_SAs
246 */
247 static void update_children(private_ike_mobike_t *this)
248 {
249 enumerator_t *enumerator;
250 child_sa_t *child_sa;
251
252 enumerator = this->ike_sa->create_child_sa_enumerator(this->ike_sa);
253 while (enumerator->enumerate(enumerator, (void**)&child_sa))
254 {
255 if (child_sa->update(child_sa,
256 this->ike_sa->get_my_host(this->ike_sa),
257 this->ike_sa->get_other_host(this->ike_sa),
258 this->ike_sa->get_virtual_ip(this->ike_sa, TRUE),
259 this->ike_sa->has_condition(this->ike_sa, COND_NAT_ANY)) == NOT_SUPPORTED)
260 {
261 this->ike_sa->rekey_child_sa(this->ike_sa,
262 child_sa->get_protocol(child_sa),
263 child_sa->get_spi(child_sa, TRUE));
264 }
265 }
266 enumerator->destroy(enumerator);
267 }
268
269 /**
270 * Apply the port of the old host, if its ip equals the new, use port otherwise.
271 */
272 static void apply_port(host_t *host, host_t *old, u_int16_t port)
273 {
274 if (host->ip_equals(host, old))
275 {
276 port = old->get_port(old);
277 }
278 else if (port == IKEV2_UDP_PORT)
279 {
280 port = IKEV2_NATT_PORT;
281 }
282 host->set_port(host, port);
283 }
284
285 METHOD(ike_mobike_t, transmit, void,
286 private_ike_mobike_t *this, packet_t *packet)
287 {
288 host_t *me, *other, *me_old, *other_old;
289 enumerator_t *enumerator;
290 ike_cfg_t *ike_cfg;
291 packet_t *copy;
292
293 if (!this->check)
294 {
295 return;
296 }
297
298 me_old = this->ike_sa->get_my_host(this->ike_sa);
299 other_old = this->ike_sa->get_other_host(this->ike_sa);
300 ike_cfg = this->ike_sa->get_ike_cfg(this->ike_sa);
301
302 me = hydra->kernel_interface->get_source_addr(
303 hydra->kernel_interface, other_old, NULL);
304 if (me)
305 {
306 apply_port(me, me_old, ike_cfg->get_my_port(ike_cfg));
307 DBG1(DBG_IKE, "checking original path %#H - %#H", me, other_old);
308 copy = packet->clone(packet);
309 copy->set_source(copy, me);
310 charon->sender->send(charon->sender, copy);
311 }
312
313 enumerator = this->ike_sa->create_peer_address_enumerator(this->ike_sa);
314 while (enumerator->enumerate(enumerator, (void**)&other))
315 {
316 me = hydra->kernel_interface->get_source_addr(
317 hydra->kernel_interface, other, NULL);
318 if (me)
319 {
320 if (me->get_family(me) != other->get_family(other))
321 {
322 me->destroy(me);
323 continue;
324 }
325 /* reuse port for an active address, 4500 otherwise */
326 apply_port(me, me_old, ike_cfg->get_my_port(ike_cfg));
327 other = other->clone(other);
328 apply_port(other, other_old, ike_cfg->get_other_port(ike_cfg));
329 DBG1(DBG_IKE, "checking path %#H - %#H", me, other);
330 copy = packet->clone(packet);
331 copy->set_source(copy, me);
332 copy->set_destination(copy, other);
333 charon->sender->send(charon->sender, copy);
334 }
335 }
336 enumerator->destroy(enumerator);
337 }
338
339 METHOD(task_t, build_i, status_t,
340 private_ike_mobike_t *this, message_t *message)
341 {
342 if (message->get_exchange_type(message) == IKE_AUTH &&
343 message->get_message_id(message) == 1)
344 { /* only in first IKE_AUTH */
345 message->add_notify(message, FALSE, MOBIKE_SUPPORTED, chunk_empty);
346 build_address_list(this, message);
347 }
348 else if (message->get_exchange_type(message) == INFORMATIONAL)
349 {
350 host_t *old, *new;
351
352 /* we check if the existing address is still valid */
353 old = message->get_source(message);
354 new = hydra->kernel_interface->get_source_addr(hydra->kernel_interface,
355 message->get_destination(message), old);
356 if (new)
357 {
358 if (!new->ip_equals(new, old))
359 {
360 new->set_port(new, old->get_port(old));
361 message->set_source(message, new);
362 }
363 else
364 {
365 new->destroy(new);
366 }
367 }
368 if (this->update)
369 {
370 message->add_notify(message, FALSE, UPDATE_SA_ADDRESSES,
371 chunk_empty);
372 build_cookie(this, message);
373 update_children(this);
374 }
375 if (this->address && !this->check)
376 {
377 build_address_list(this, message);
378 }
379 if (this->natd)
380 {
381 this->natd->task.build(&this->natd->task, message);
382 }
383 }
384 return NEED_MORE;
385 }
386
387 METHOD(task_t, process_r, status_t,
388 private_ike_mobike_t *this, message_t *message)
389 {
390 if (message->get_exchange_type(message) == IKE_AUTH &&
391 message->get_message_id(message) == 1)
392 { /* only first IKE_AUTH */
393 process_payloads(this, message);
394 }
395 else if (message->get_exchange_type(message) == INFORMATIONAL)
396 {
397 process_payloads(this, message);
398 if (this->update)
399 {
400 host_t *me, *other;
401
402 me = message->get_destination(message);
403 other = message->get_source(message);
404 this->ike_sa->set_my_host(this->ike_sa, me->clone(me));
405 this->ike_sa->set_other_host(this->ike_sa, other->clone(other));
406 }
407
408 if (this->natd)
409 {
410 this->natd->task.process(&this->natd->task, message);
411 }
412 if (this->addresses_updated && this->ike_sa->has_condition(this->ike_sa,
413 COND_ORIGINAL_INITIATOR))
414 {
415 host_t *other = message->get_source(message);
416 host_t *other_old = this->ike_sa->get_other_host(this->ike_sa);
417 if (!other->equals(other, other_old))
418 {
419 DBG1(DBG_IKE, "remote address changed from %H to %H", other_old,
420 other);
421 this->ike_sa->set_other_host(this->ike_sa, other->clone(other));
422 this->update = TRUE;
423 }
424 }
425 }
426 return NEED_MORE;
427 }
428
429 METHOD(task_t, build_r, status_t,
430 private_ike_mobike_t *this, message_t *message)
431 {
432 if (message->get_exchange_type(message) == IKE_AUTH &&
433 this->ike_sa->get_state(this->ike_sa) == IKE_ESTABLISHED)
434 {
435 if (this->ike_sa->supports_extension(this->ike_sa, EXT_MOBIKE))
436 {
437 message->add_notify(message, FALSE, MOBIKE_SUPPORTED, chunk_empty);
438 build_address_list(this, message);
439 }
440 return SUCCESS;
441 }
442 else if (message->get_exchange_type(message) == INFORMATIONAL)
443 {
444 if (this->natd)
445 {
446 this->natd->task.build(&this->natd->task, message);
447 }
448 if (this->cookie2.ptr)
449 {
450 message->add_notify(message, FALSE, COOKIE2, this->cookie2);
451 chunk_free(&this->cookie2);
452 }
453 if (this->update)
454 {
455 update_children(this);
456 }
457 return SUCCESS;
458 }
459 return NEED_MORE;
460 }
461
462 METHOD(task_t, process_i, status_t,
463 private_ike_mobike_t *this, message_t *message)
464 {
465 if (message->get_exchange_type(message) == IKE_AUTH &&
466 this->ike_sa->get_state(this->ike_sa) == IKE_ESTABLISHED)
467 {
468 process_payloads(this, message);
469 return SUCCESS;
470 }
471 else if (message->get_exchange_type(message) == INFORMATIONAL)
472 {
473 u_int32_t updates = this->ike_sa->get_pending_updates(this->ike_sa) - 1;
474 this->ike_sa->set_pending_updates(this->ike_sa, updates);
475 if (updates > 0)
476 {
477 /* newer update queued, ignore this one */
478 return SUCCESS;
479 }
480 if (this->cookie2.ptr)
481 { /* check cookie if we included one */
482 chunk_t cookie2;
483
484 cookie2 = this->cookie2;
485 this->cookie2 = chunk_empty;
486 process_payloads(this, message);
487 if (!chunk_equals(cookie2, this->cookie2))
488 {
489 chunk_free(&cookie2);
490 DBG1(DBG_IKE, "COOKIE2 mismatch, closing IKE_SA");
491 return FAILED;
492 }
493 chunk_free(&cookie2);
494 }
495 else
496 {
497 process_payloads(this, message);
498 }
499 if (this->natd)
500 {
501 this->natd->task.process(&this->natd->task, message);
502 if (this->natd->has_mapping_changed(this->natd))
503 {
504 /* force an update if mappings have changed */
505 this->update = this->check = TRUE;
506 DBG1(DBG_IKE, "detected changes in NAT mappings, "
507 "initiating MOBIKE update");
508 }
509 }
510 if (this->update)
511 {
512 /* update again, as NAT state may have changed */
513 update_children(this);
514 }
515 if (this->check)
516 {
517 host_t *me_new, *me_old, *other_new, *other_old;
518
519 me_new = message->get_destination(message);
520 other_new = message->get_source(message);
521 me_old = this->ike_sa->get_my_host(this->ike_sa);
522 other_old = this->ike_sa->get_other_host(this->ike_sa);
523
524 if (!me_new->equals(me_new, me_old))
525 {
526 this->update = TRUE;
527 this->ike_sa->set_my_host(this->ike_sa, me_new->clone(me_new));
528 }
529 if (!other_new->equals(other_new, other_old))
530 {
531 this->update = TRUE;
532 this->ike_sa->set_other_host(this->ike_sa, other_new->clone(other_new));
533 }
534 if (this->update)
535 {
536 /* use the same task to ... */
537 if (!this->ike_sa->has_condition(this->ike_sa,
538 COND_ORIGINAL_INITIATOR))
539 { /*... send an updated list of addresses as responder */
540 update_children(this);
541 this->update = FALSE;
542 }
543 else
544 { /* ... send the update as original initiator */
545 if (this->natd)
546 {
547 this->natd->task.destroy(&this->natd->task);
548 }
549 this->natd = ike_natd_create(this->ike_sa, this->initiator);
550 }
551 this->check = FALSE;
552 this->ike_sa->set_pending_updates(this->ike_sa, 1);
553 return NEED_MORE;
554 }
555 }
556 return SUCCESS;
557 }
558 return NEED_MORE;
559 }
560
561 METHOD(ike_mobike_t, addresses, void,
562 private_ike_mobike_t *this)
563 {
564 this->address = TRUE;
565 this->ike_sa->set_pending_updates(this->ike_sa,
566 this->ike_sa->get_pending_updates(this->ike_sa) + 1);
567 }
568
569 METHOD(ike_mobike_t, roam, void,
570 private_ike_mobike_t *this, bool address)
571 {
572 this->check = TRUE;
573 this->address = address;
574 this->ike_sa->set_pending_updates(this->ike_sa,
575 this->ike_sa->get_pending_updates(this->ike_sa) + 1);
576 }
577
578 METHOD(ike_mobike_t, dpd, void,
579 private_ike_mobike_t *this)
580 {
581 if (!this->natd)
582 {
583 this->natd = ike_natd_create(this->ike_sa, this->initiator);
584 }
585 this->ike_sa->set_pending_updates(this->ike_sa,
586 this->ike_sa->get_pending_updates(this->ike_sa) + 1);
587 }
588
589 METHOD(ike_mobike_t, is_probing, bool,
590 private_ike_mobike_t *this)
591 {
592 return this->check;
593 }
594
595 METHOD(task_t, get_type, task_type_t,
596 private_ike_mobike_t *this)
597 {
598 return IKE_MOBIKE;
599 }
600
601 METHOD(task_t, migrate, void,
602 private_ike_mobike_t *this, ike_sa_t *ike_sa)
603 {
604 chunk_free(&this->cookie2);
605 this->ike_sa = ike_sa;
606 if (this->natd)
607 {
608 this->natd->task.migrate(&this->natd->task, ike_sa);
609 }
610 }
611
612 METHOD(task_t, destroy, void,
613 private_ike_mobike_t *this)
614 {
615 chunk_free(&this->cookie2);
616 if (this->natd)
617 {
618 this->natd->task.destroy(&this->natd->task);
619 }
620 free(this);
621 }
622
623 /*
624 * Described in header.
625 */
626 ike_mobike_t *ike_mobike_create(ike_sa_t *ike_sa, bool initiator)
627 {
628 private_ike_mobike_t *this;
629
630 INIT(this,
631 .public = {
632 .task = {
633 .get_type = _get_type,
634 .migrate = _migrate,
635 .destroy = _destroy,
636 },
637 .addresses = _addresses,
638 .roam = _roam,
639 .dpd = _dpd,
640 .transmit = _transmit,
641 .is_probing = _is_probing,
642 },
643 .ike_sa = ike_sa,
644 .initiator = initiator,
645 );
646
647 if (initiator)
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 }
660