fixed MOBIKE roaming if clients address changes
[strongswan.git] / src / charon / sa / tasks / ike_mobike.c
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 * $Id$
16 */
17
18 #include "ike_mobike.h"
19
20 #include <string.h>
21
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
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 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 /**
77 * flush the IKE_SAs list of additional addresses
78 */
79 static void flush_additional_addresses(private_ike_mobike_t *this)
80 {
81 iterator_t *iterator;
82 host_t *host;
83
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 */
97 static void process_payloads(private_ike_mobike_t *this, message_t *message)
98 {
99 iterator_t *iterator;
100 payload_t *payload;
101 bool first = TRUE;
102
103 iterator = message->get_payload_iterator(message);
104 while (iterator->iterate(iterator, (void**)&payload))
105 {
106 int family = AF_INET;
107 notify_payload_t *notify;
108 chunk_t data;
109 host_t *host;
110
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 {
120 peer_cfg_t *peer_cfg;
121
122 peer_cfg = this->ike_sa->get_peer_cfg(this->ike_sa);
123 if (!this->initiator &&
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 }
133 break;
134 }
135 case COOKIE2:
136 {
137 chunk_free(&this->cookie2);
138 this->cookie2 = chunk_clone(notify->get_notification_data(notify));
139 break;
140 }
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);
151 first = FALSE;
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 }
159 case UPDATE_SA_ADDRESSES:
160 {
161 this->update = TRUE;
162 break;
163 }
164 case NO_ADDITIONAL_ADDRESSES:
165 {
166 flush_additional_addresses(this);
167 break;
168 }
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 }
179 default:
180 break;
181 }
182 }
183 iterator->destroy(iterator);
184 }
185
186 /**
187 * Add ADDITIONAL_*_ADDRESS notifys depending on our address list
188 */
189 static void build_address_list(private_ike_mobike_t *this, message_t *message)
190 {
191 enumerator_t *enumerator;
192 host_t *host, *me;
193 notify_type_t type;
194 bool additional = FALSE;
195
196 me = this->ike_sa->get_my_host(this->ike_sa);
197 enumerator = charon->kernel_interface->create_address_enumerator(
198 charon->kernel_interface, FALSE, FALSE);
199 while (enumerator->enumerate(enumerator, (void**)&host))
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));
217 additional = TRUE;
218 }
219 if (!additional)
220 {
221 message->add_notify(message, FALSE, NO_ADDITIONAL_ADDRESSES, chunk_empty);
222 }
223 enumerator->destroy(enumerator);
224 }
225
226 /**
227 * build a cookie and add it to the message
228 */
229 static void build_cookie(private_ike_mobike_t *this, message_t *message)
230 {
231 rng_t *rng;
232
233 chunk_free(&this->cookie2);
234 rng = lib->crypto->create_rng(lib->crypto, RNG_STRONG);
235 if (rng)
236 {
237 rng->allocate_bytes(rng, COOKIE2_SIZE, &this->cookie2);
238 rng->destroy(rng);
239 message->add_notify(message, FALSE, COOKIE2, this->cookie2);
240 }
241 }
242
243 /**
244 * update addresses of associated CHILD_SAs
245 */
246 static void update_children(private_ike_mobike_t *this)
247 {
248 iterator_t *iterator;
249 child_sa_t *child_sa;
250
251 iterator = this->ike_sa->create_child_sa_iterator(this->ike_sa);
252 while (iterator->iterate(iterator, (void**)&child_sa))
253 {
254 child_sa->update_hosts(child_sa,
255 this->ike_sa->get_my_host(this->ike_sa),
256 this->ike_sa->get_other_host(this->ike_sa),
257 this->ike_sa->has_condition(this->ike_sa, COND_NAT_ANY));
258 }
259 iterator->destroy(iterator);
260 }
261
262 /**
263 * Implementation of ike_mobike_t.transmit
264 */
265 static void transmit(private_ike_mobike_t *this, packet_t *packet)
266 {
267 host_t *me, *other, *me_old, *other_old;
268 iterator_t *iterator;
269 packet_t *copy;
270
271 if (!this->check)
272 {
273 return;
274 }
275
276 me_old = this->ike_sa->get_my_host(this->ike_sa);
277 other_old = this->ike_sa->get_other_host(this->ike_sa);
278
279 me = charon->kernel_interface->get_source_addr(
280 charon->kernel_interface, other_old, NULL);
281 if (me)
282 {
283 me->set_port(me, me->ip_equals(me, me_old) ?
284 me_old->get_port(me_old) : IKEV2_NATT_PORT);
285 packet->set_source(packet, me);
286 }
287
288 iterator = this->ike_sa->create_additional_address_iterator(this->ike_sa);
289 while (iterator->iterate(iterator, (void**)&other))
290 {
291 me = charon->kernel_interface->get_source_addr(
292 charon->kernel_interface, other, NULL);
293 if (me)
294 {
295 if (me->get_family(me) != other->get_family(other))
296 {
297 me->destroy(me);
298 continue;
299 }
300 /* reuse port for an active address, 4500 otherwise */
301 me->set_port(me, me->ip_equals(me, me_old) ?
302 me_old->get_port(me_old) : IKEV2_NATT_PORT);
303 other = other->clone(other);
304 other->set_port(other, other->ip_equals(other, other_old) ?
305 other_old->get_port(other_old) : IKEV2_NATT_PORT);
306 DBG1(DBG_IKE, "checking path %#H - %#H", me, other);
307 copy = packet->clone(packet);
308 copy->set_source(copy, me);
309 copy->set_destination(copy, other);
310 charon->sender->send(charon->sender, copy);
311 }
312 }
313 iterator->destroy(iterator);
314 me = packet->get_source(packet);
315 other = packet->get_destination(packet);
316 DBG1(DBG_IKE, "checking path %#H - %#H", me, other);
317 }
318
319 /**
320 * Implementation of task_t.process for initiator
321 */
322 static status_t build_i(private_ike_mobike_t *this, message_t *message)
323 {
324 if (message->get_exchange_type(message) == IKE_AUTH &&
325 message->get_payload(message, ID_INITIATOR))
326 {
327 message->add_notify(message, FALSE, MOBIKE_SUPPORTED, chunk_empty);
328 build_address_list(this, message);
329 }
330 else if (message->get_exchange_type(message) == INFORMATIONAL)
331 {
332 host_t *old, *new;
333
334 /* we check if the existing address is still valid */
335 old = message->get_source(message);
336 new = charon->kernel_interface->get_source_addr(charon->kernel_interface,
337 message->get_destination(message), old);
338 if (new)
339 {
340 if (!new->ip_equals(new, old))
341 {
342 new->set_port(new, old->get_port(old));
343 message->set_source(message, new);
344 }
345 else
346 {
347 new->destroy(new);
348 }
349 }
350 if (this->update)
351 {
352 message->add_notify(message, FALSE, UPDATE_SA_ADDRESSES, chunk_empty);
353 build_cookie(this, message);
354 update_children(this);
355 }
356 if (this->address)
357 {
358 build_address_list(this, message);
359 }
360 if (this->natd)
361 {
362 this->natd->task.build(&this->natd->task, message);
363 }
364 }
365 return NEED_MORE;
366 }
367
368 /**
369 * Implementation of task_t.process for responder
370 */
371 static status_t process_r(private_ike_mobike_t *this, message_t *message)
372 {
373 if (message->get_exchange_type(message) == IKE_AUTH &&
374 message->get_payload(message, ID_INITIATOR))
375 {
376 process_payloads(this, message);
377 }
378 else if (message->get_exchange_type(message) == INFORMATIONAL)
379 {
380 process_payloads(this, message);
381 if (this->update)
382 {
383 host_t *me, *other;
384
385 me = message->get_destination(message);
386 other = message->get_source(message);
387 this->ike_sa->set_my_host(this->ike_sa, me->clone(me));
388 this->ike_sa->set_other_host(this->ike_sa, other->clone(other));
389 }
390
391 if (this->natd)
392 {
393 this->natd->task.process(&this->natd->task, message);
394 }
395 }
396 return NEED_MORE;
397 }
398
399 /**
400 * Implementation of task_t.build for responder
401 */
402 static status_t build_r(private_ike_mobike_t *this, message_t *message)
403 {
404 if (message->get_exchange_type(message) == IKE_AUTH &&
405 this->ike_sa->get_state(this->ike_sa) == IKE_ESTABLISHED)
406 {
407 if (this->ike_sa->supports_extension(this->ike_sa, EXT_MOBIKE))
408 {
409 message->add_notify(message, FALSE, MOBIKE_SUPPORTED, chunk_empty);
410 build_address_list(this, message);
411 }
412 return SUCCESS;
413 }
414 else if (message->get_exchange_type(message) == INFORMATIONAL)
415 {
416 if (this->natd)
417 {
418 this->natd->task.build(&this->natd->task, message);
419 }
420 if (this->cookie2.ptr)
421 {
422 message->add_notify(message, FALSE, COOKIE2, this->cookie2);
423 chunk_free(&this->cookie2);
424 }
425 if (this->update)
426 {
427 update_children(this);
428 }
429 return SUCCESS;
430 }
431 return NEED_MORE;
432 }
433
434 /**
435 * Implementation of task_t.process for initiator
436 */
437 static status_t process_i(private_ike_mobike_t *this, message_t *message)
438 {
439 if (message->get_exchange_type(message) == IKE_AUTH &&
440 this->ike_sa->get_state(this->ike_sa) == IKE_ESTABLISHED)
441 {
442 process_payloads(this, message);
443 return SUCCESS;
444 }
445 else if (message->get_exchange_type(message) == INFORMATIONAL)
446 {
447 u_int32_t updates = this->ike_sa->get_pending_updates(this->ike_sa) - 1;
448 this->ike_sa->set_pending_updates(this->ike_sa, updates);
449 if (updates > 0)
450 {
451 /* newer update queued, ignore this one */
452 return SUCCESS;
453 }
454 if (this->cookie2.ptr)
455 { /* check cookie if we included one */
456 chunk_t cookie2;
457
458 cookie2 = this->cookie2;
459 this->cookie2 = chunk_empty;
460 process_payloads(this, message);
461 if (!chunk_equals(cookie2, this->cookie2))
462 {
463 chunk_free(&cookie2);
464 DBG1(DBG_IKE, "COOKIE2 mismatch, closing IKE_SA");
465 return FAILED;
466 }
467 chunk_free(&cookie2);
468 }
469 else
470 {
471 process_payloads(this, message);
472 }
473 if (this->natd)
474 {
475 this->natd->task.process(&this->natd->task, message);
476 if (this->natd->has_mapping_changed(this->natd))
477 {
478 /* force an update if mappings have changed */
479 this->update = this->check = TRUE;
480 DBG1(DBG_IKE, "detected changes in NAT mappings, "
481 "initiating MOBIKE update");
482 }
483 }
484 if (this->update)
485 {
486 /* update again, as NAT state may have changed */
487 update_children(this);
488 }
489 if (this->check)
490 {
491 host_t *me_new, *me_old, *other_new, *other_old;
492
493 me_new = message->get_destination(message);
494 other_new = message->get_source(message);
495 me_old = this->ike_sa->get_my_host(this->ike_sa);
496 other_old = this->ike_sa->get_other_host(this->ike_sa);
497
498 if (!me_new->equals(me_new, me_old))
499 {
500 this->update = TRUE;
501 this->ike_sa->set_my_host(this->ike_sa, me_new->clone(me_new));
502 }
503 if (!other_new->equals(other_new, other_old))
504 {
505 this->update = TRUE;
506 this->ike_sa->set_other_host(this->ike_sa, other_new->clone(other_new));
507 }
508 if (this->update)
509 {
510 /* start the update with the same task */
511 this->check = FALSE;
512 this->address = FALSE;
513 this->natd = ike_natd_create(this->ike_sa, this->initiator);
514 this->ike_sa->set_pending_updates(this->ike_sa, 1);
515 return NEED_MORE;
516 }
517 }
518 return SUCCESS;
519 }
520 return NEED_MORE;
521 }
522
523 /**
524 * Implementation of ike_mobike_t.roam.
525 */
526 static void roam(private_ike_mobike_t *this, bool address)
527 {
528 this->check = TRUE;
529 this->address = address;
530 this->ike_sa->set_pending_updates(this->ike_sa,
531 this->ike_sa->get_pending_updates(this->ike_sa) + 1);
532 }
533
534 /**
535 * Implementation of ike_mobike_t.dpd
536 */
537 static void dpd(private_ike_mobike_t *this)
538 {
539 if (!this->natd)
540 {
541 this->natd = ike_natd_create(this->ike_sa, this->initiator);
542 }
543 this->address = FALSE;
544 this->ike_sa->set_pending_updates(this->ike_sa,
545 this->ike_sa->get_pending_updates(this->ike_sa) + 1);
546 }
547
548 /**
549 * Implementation of ike_mobike_t.is_probing.
550 */
551 static bool is_probing(private_ike_mobike_t *this)
552 {
553 return this->check;
554 }
555
556 /**
557 * Implementation of task_t.get_type
558 */
559 static task_type_t get_type(private_ike_mobike_t *this)
560 {
561 return IKE_MOBIKE;
562 }
563
564 /**
565 * Implementation of task_t.migrate
566 */
567 static void migrate(private_ike_mobike_t *this, ike_sa_t *ike_sa)
568 {
569 chunk_free(&this->cookie2);
570 this->ike_sa = ike_sa;
571 if (this->natd)
572 {
573 this->natd->task.migrate(&this->natd->task, ike_sa);
574 }
575 }
576
577 /**
578 * Implementation of task_t.destroy
579 */
580 static void destroy(private_ike_mobike_t *this)
581 {
582 chunk_free(&this->cookie2);
583 if (this->natd)
584 {
585 this->natd->task.destroy(&this->natd->task);
586 }
587 free(this);
588 }
589
590 /*
591 * Described in header.
592 */
593 ike_mobike_t *ike_mobike_create(ike_sa_t *ike_sa, bool initiator)
594 {
595 private_ike_mobike_t *this = malloc_thing(private_ike_mobike_t);
596
597 this->public.roam = (void(*)(ike_mobike_t*,bool))roam;
598 this->public.dpd = (void(*)(ike_mobike_t*))dpd;
599 this->public.transmit = (void(*)(ike_mobike_t*,packet_t*))transmit;
600 this->public.is_probing = (bool(*)(ike_mobike_t*))is_probing;
601 this->public.task.get_type = (task_type_t(*)(task_t*))get_type;
602 this->public.task.migrate = (void(*)(task_t*,ike_sa_t*))migrate;
603 this->public.task.destroy = (void(*)(task_t*))destroy;
604
605 if (initiator)
606 {
607 this->public.task.build = (status_t(*)(task_t*,message_t*))build_i;
608 this->public.task.process = (status_t(*)(task_t*,message_t*))process_i;
609 }
610 else
611 {
612 this->public.task.build = (status_t(*)(task_t*,message_t*))build_r;
613 this->public.task.process = (status_t(*)(task_t*,message_t*))process_r;
614 }
615
616 this->ike_sa = ike_sa;
617 this->initiator = initiator;
618 this->update = FALSE;
619 this->check = FALSE;
620 this->address = TRUE;
621 this->cookie2 = chunk_empty;
622 this->natd = NULL;
623
624 return &this->public;
625 }
626