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