fc7df498d91e6359e44e1f75d9fe27ee582a7486
[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 #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 /**
78 * flush the IKE_SAs list of additional addresses
79 */
80 static void flush_additional_addresses(private_ike_mobike_t *this)
81 {
82 iterator_t *iterator;
83 host_t *host;
84
85 iterator = this->ike_sa->create_additional_address_iterator(this->ike_sa);
86 while (iterator->iterate(iterator, (void**)&host))
87 {
88 iterator->remove(iterator);
89 host->destroy(host);
90 }
91 iterator->destroy(iterator);
92 }
93
94
95 /**
96 * read notifys from message and evaluate them
97 */
98 static void process_payloads(private_ike_mobike_t *this, message_t *message)
99 {
100 iterator_t *iterator;
101 payload_t *payload;
102 bool first = TRUE;
103
104 iterator = message->get_payload_iterator(message);
105 while (iterator->iterate(iterator, (void**)&payload))
106 {
107 int family = AF_INET;
108 notify_payload_t *notify;
109 chunk_t data;
110 host_t *host;
111
112 if (payload->get_type(payload) != NOTIFY)
113 {
114 continue;
115 }
116 notify = (notify_payload_t*)payload;
117 switch (notify->get_notify_type(notify))
118 {
119 case MOBIKE_SUPPORTED:
120 {
121 peer_cfg_t *peer_cfg;
122
123 peer_cfg = this->ike_sa->get_peer_cfg(this->ike_sa);
124 if (!this->initiator &&
125 peer_cfg && !peer_cfg->use_mobike(peer_cfg))
126 {
127 DBG1(DBG_IKE, "peer supports MOBIKE, but disabled in config");
128 }
129 else
130 {
131 DBG1(DBG_IKE, "peer supports MOBIKE");
132 this->ike_sa->enable_extension(this->ike_sa, EXT_MOBIKE);
133 }
134 break;
135 }
136 case COOKIE2:
137 {
138 chunk_free(&this->cookie2);
139 this->cookie2 = chunk_clone(notify->get_notification_data(notify));
140 break;
141 }
142 case ADDITIONAL_IP6_ADDRESS:
143 {
144 family = AF_INET6;
145 /* fall through */
146 }
147 case ADDITIONAL_IP4_ADDRESS:
148 {
149 if (first)
150 { /* an ADDITIONAL_*_ADDRESS means replace, so flush once */
151 flush_additional_addresses(this);
152 first = FALSE;
153 }
154 data = notify->get_notification_data(notify);
155 host = host_create_from_chunk(family, data, 0);
156 DBG2(DBG_IKE, "got additional MOBIKE peer address: %H", host);
157 this->ike_sa->add_additional_address(this->ike_sa, host);
158 break;
159 }
160 case UPDATE_SA_ADDRESSES:
161 {
162 this->update = TRUE;
163 break;
164 }
165 case NO_ADDITIONAL_ADDRESSES:
166 {
167 flush_additional_addresses(this);
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 iterator->destroy(iterator);
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_interface->create_address_enumerator(
199 charon->kernel_interface, FALSE, FALSE);
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 void 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)
241 {
242 rng->allocate_bytes(rng, COOKIE2_SIZE, &this->cookie2);
243 rng->destroy(rng);
244 message->add_notify(message, FALSE, COOKIE2, this->cookie2);
245 }
246 }
247
248 /**
249 * update addresses of associated CHILD_SAs
250 */
251 static void update_children(private_ike_mobike_t *this)
252 {
253 iterator_t *iterator;
254 child_sa_t *child_sa;
255
256 iterator = this->ike_sa->create_child_sa_iterator(this->ike_sa);
257 while (iterator->iterate(iterator, (void**)&child_sa))
258 {
259 if (child_sa->update(child_sa,
260 this->ike_sa->get_my_host(this->ike_sa),
261 this->ike_sa->get_other_host(this->ike_sa),
262 this->ike_sa->get_virtual_ip(this->ike_sa, TRUE),
263 this->ike_sa->has_condition(this->ike_sa, COND_NAT_ANY)) == NOT_SUPPORTED)
264 {
265 this->ike_sa->rekey_child_sa(this->ike_sa,
266 child_sa->get_protocol(child_sa),
267 child_sa->get_spi(child_sa, TRUE));
268 }
269 }
270 iterator->destroy(iterator);
271 }
272
273 /**
274 * Implementation of ike_mobike_t.transmit
275 */
276 static void transmit(private_ike_mobike_t *this, packet_t *packet)
277 {
278 host_t *me, *other, *me_old, *other_old;
279 iterator_t *iterator;
280 packet_t *copy;
281
282 if (!this->check)
283 {
284 return;
285 }
286
287 me_old = this->ike_sa->get_my_host(this->ike_sa);
288 other_old = this->ike_sa->get_other_host(this->ike_sa);
289
290 me = charon->kernel_interface->get_source_addr(
291 charon->kernel_interface, other_old, NULL);
292 if (me)
293 {
294 me->set_port(me, me->ip_equals(me, me_old) ?
295 me_old->get_port(me_old) : IKEV2_NATT_PORT);
296 packet->set_source(packet, me);
297 }
298
299 iterator = this->ike_sa->create_additional_address_iterator(this->ike_sa);
300 while (iterator->iterate(iterator, (void**)&other))
301 {
302 me = charon->kernel_interface->get_source_addr(
303 charon->kernel_interface, other, NULL);
304 if (me)
305 {
306 if (me->get_family(me) != other->get_family(other))
307 {
308 me->destroy(me);
309 continue;
310 }
311 /* reuse port for an active address, 4500 otherwise */
312 me->set_port(me, me->ip_equals(me, me_old) ?
313 me_old->get_port(me_old) : IKEV2_NATT_PORT);
314 other = other->clone(other);
315 other->set_port(other, other->ip_equals(other, other_old) ?
316 other_old->get_port(other_old) : IKEV2_NATT_PORT);
317 DBG1(DBG_IKE, "checking path %#H - %#H", me, other);
318 copy = packet->clone(packet);
319 copy->set_source(copy, me);
320 copy->set_destination(copy, other);
321 charon->sender->send(charon->sender, copy);
322 }
323 }
324 iterator->destroy(iterator);
325 me = packet->get_source(packet);
326 other = packet->get_destination(packet);
327 DBG1(DBG_IKE, "checking path %#H - %#H", me, other);
328 }
329
330 /**
331 * Implementation of task_t.process for initiator
332 */
333 static status_t build_i(private_ike_mobike_t *this, message_t *message)
334 {
335 if (message->get_exchange_type(message) == IKE_AUTH &&
336 message->get_payload(message, ID_INITIATOR))
337 {
338 message->add_notify(message, FALSE, MOBIKE_SUPPORTED, chunk_empty);
339 build_address_list(this, message);
340 }
341 else if (message->get_exchange_type(message) == INFORMATIONAL)
342 {
343 host_t *old, *new;
344
345 /* we check if the existing address is still valid */
346 old = message->get_source(message);
347 new = charon->kernel_interface->get_source_addr(charon->kernel_interface,
348 message->get_destination(message), old);
349 if (new)
350 {
351 if (!new->ip_equals(new, old))
352 {
353 new->set_port(new, old->get_port(old));
354 message->set_source(message, new);
355 }
356 else
357 {
358 new->destroy(new);
359 }
360 }
361 if (this->update)
362 {
363 message->add_notify(message, FALSE, UPDATE_SA_ADDRESSES, chunk_empty);
364 build_cookie(this, message);
365 update_children(this);
366 }
367 if (this->address)
368 {
369 build_address_list(this, message);
370 }
371 if (this->natd)
372 {
373 this->natd->task.build(&this->natd->task, message);
374 }
375 }
376 return NEED_MORE;
377 }
378
379 /**
380 * Implementation of task_t.process for responder
381 */
382 static status_t process_r(private_ike_mobike_t *this, message_t *message)
383 {
384 if (message->get_exchange_type(message) == IKE_AUTH &&
385 message->get_payload(message, ID_INITIATOR))
386 {
387 process_payloads(this, message);
388 }
389 else if (message->get_exchange_type(message) == INFORMATIONAL)
390 {
391 process_payloads(this, message);
392 if (this->update)
393 {
394 host_t *me, *other;
395
396 me = message->get_destination(message);
397 other = message->get_source(message);
398 this->ike_sa->set_my_host(this->ike_sa, me->clone(me));
399 this->ike_sa->set_other_host(this->ike_sa, other->clone(other));
400 }
401
402 if (this->natd)
403 {
404 this->natd->task.process(&this->natd->task, message);
405 }
406 }
407 return NEED_MORE;
408 }
409
410 /**
411 * Implementation of task_t.build for responder
412 */
413 static status_t build_r(private_ike_mobike_t *this, message_t *message)
414 {
415 if (message->get_exchange_type(message) == IKE_AUTH &&
416 this->ike_sa->get_state(this->ike_sa) == IKE_ESTABLISHED)
417 {
418 if (this->ike_sa->supports_extension(this->ike_sa, EXT_MOBIKE))
419 {
420 message->add_notify(message, FALSE, MOBIKE_SUPPORTED, chunk_empty);
421 build_address_list(this, message);
422 }
423 return SUCCESS;
424 }
425 else if (message->get_exchange_type(message) == INFORMATIONAL)
426 {
427 if (this->natd)
428 {
429 this->natd->task.build(&this->natd->task, message);
430 }
431 if (this->cookie2.ptr)
432 {
433 message->add_notify(message, FALSE, COOKIE2, this->cookie2);
434 chunk_free(&this->cookie2);
435 }
436 if (this->update)
437 {
438 update_children(this);
439 }
440 return SUCCESS;
441 }
442 return NEED_MORE;
443 }
444
445 /**
446 * Implementation of task_t.process for initiator
447 */
448 static status_t process_i(private_ike_mobike_t *this, message_t *message)
449 {
450 if (message->get_exchange_type(message) == IKE_AUTH &&
451 this->ike_sa->get_state(this->ike_sa) == IKE_ESTABLISHED)
452 {
453 process_payloads(this, message);
454 return SUCCESS;
455 }
456 else if (message->get_exchange_type(message) == INFORMATIONAL)
457 {
458 u_int32_t updates = this->ike_sa->get_pending_updates(this->ike_sa) - 1;
459 this->ike_sa->set_pending_updates(this->ike_sa, updates);
460 if (updates > 0)
461 {
462 /* newer update queued, ignore this one */
463 return SUCCESS;
464 }
465 if (this->cookie2.ptr)
466 { /* check cookie if we included one */
467 chunk_t cookie2;
468
469 cookie2 = this->cookie2;
470 this->cookie2 = chunk_empty;
471 process_payloads(this, message);
472 if (!chunk_equals(cookie2, this->cookie2))
473 {
474 chunk_free(&cookie2);
475 DBG1(DBG_IKE, "COOKIE2 mismatch, closing IKE_SA");
476 return FAILED;
477 }
478 chunk_free(&cookie2);
479 }
480 else
481 {
482 process_payloads(this, message);
483 }
484 if (this->natd)
485 {
486 this->natd->task.process(&this->natd->task, message);
487 if (this->natd->has_mapping_changed(this->natd))
488 {
489 /* force an update if mappings have changed */
490 this->update = this->check = TRUE;
491 DBG1(DBG_IKE, "detected changes in NAT mappings, "
492 "initiating MOBIKE update");
493 }
494 }
495 if (this->update)
496 {
497 /* update again, as NAT state may have changed */
498 update_children(this);
499 }
500 if (this->check)
501 {
502 host_t *me_new, *me_old, *other_new, *other_old;
503
504 me_new = message->get_destination(message);
505 other_new = message->get_source(message);
506 me_old = this->ike_sa->get_my_host(this->ike_sa);
507 other_old = this->ike_sa->get_other_host(this->ike_sa);
508
509 if (!me_new->equals(me_new, me_old))
510 {
511 this->update = TRUE;
512 this->ike_sa->set_my_host(this->ike_sa, me_new->clone(me_new));
513 }
514 if (!other_new->equals(other_new, other_old))
515 {
516 this->update = TRUE;
517 this->ike_sa->set_other_host(this->ike_sa, other_new->clone(other_new));
518 }
519 if (this->update)
520 {
521 /* start the update with the same task */
522 this->check = FALSE;
523 this->address = FALSE;
524 this->natd = ike_natd_create(this->ike_sa, this->initiator);
525 this->ike_sa->set_pending_updates(this->ike_sa, 1);
526 return NEED_MORE;
527 }
528 }
529 return SUCCESS;
530 }
531 return NEED_MORE;
532 }
533
534 /**
535 * Implementation of ike_mobike_t.roam.
536 */
537 static void roam(private_ike_mobike_t *this, bool address)
538 {
539 this->check = TRUE;
540 this->address = address;
541 this->ike_sa->set_pending_updates(this->ike_sa,
542 this->ike_sa->get_pending_updates(this->ike_sa) + 1);
543 }
544
545 /**
546 * Implementation of ike_mobike_t.dpd
547 */
548 static void dpd(private_ike_mobike_t *this)
549 {
550 if (!this->natd)
551 {
552 this->natd = ike_natd_create(this->ike_sa, this->initiator);
553 }
554 this->address = FALSE;
555 this->ike_sa->set_pending_updates(this->ike_sa,
556 this->ike_sa->get_pending_updates(this->ike_sa) + 1);
557 }
558
559 /**
560 * Implementation of ike_mobike_t.is_probing.
561 */
562 static bool is_probing(private_ike_mobike_t *this)
563 {
564 return this->check;
565 }
566
567 /**
568 * Implementation of task_t.get_type
569 */
570 static task_type_t get_type(private_ike_mobike_t *this)
571 {
572 return IKE_MOBIKE;
573 }
574
575 /**
576 * Implementation of task_t.migrate
577 */
578 static void migrate(private_ike_mobike_t *this, ike_sa_t *ike_sa)
579 {
580 chunk_free(&this->cookie2);
581 this->ike_sa = ike_sa;
582 if (this->natd)
583 {
584 this->natd->task.migrate(&this->natd->task, ike_sa);
585 }
586 }
587
588 /**
589 * Implementation of task_t.destroy
590 */
591 static void destroy(private_ike_mobike_t *this)
592 {
593 chunk_free(&this->cookie2);
594 if (this->natd)
595 {
596 this->natd->task.destroy(&this->natd->task);
597 }
598 free(this);
599 }
600
601 /*
602 * Described in header.
603 */
604 ike_mobike_t *ike_mobike_create(ike_sa_t *ike_sa, bool initiator)
605 {
606 private_ike_mobike_t *this = malloc_thing(private_ike_mobike_t);
607
608 this->public.roam = (void(*)(ike_mobike_t*,bool))roam;
609 this->public.dpd = (void(*)(ike_mobike_t*))dpd;
610 this->public.transmit = (void(*)(ike_mobike_t*,packet_t*))transmit;
611 this->public.is_probing = (bool(*)(ike_mobike_t*))is_probing;
612 this->public.task.get_type = (task_type_t(*)(task_t*))get_type;
613 this->public.task.migrate = (void(*)(task_t*,ike_sa_t*))migrate;
614 this->public.task.destroy = (void(*)(task_t*))destroy;
615
616 if (initiator)
617 {
618 this->public.task.build = (status_t(*)(task_t*,message_t*))build_i;
619 this->public.task.process = (status_t(*)(task_t*,message_t*))process_i;
620 }
621 else
622 {
623 this->public.task.build = (status_t(*)(task_t*,message_t*))build_r;
624 this->public.task.process = (status_t(*)(task_t*,message_t*))process_r;
625 }
626
627 this->ike_sa = ike_sa;
628 this->initiator = initiator;
629 this->update = FALSE;
630 this->check = FALSE;
631 this->address = TRUE;
632 this->cookie2 = chunk_empty;
633 this->natd = NULL;
634
635 return &this->public;
636 }
637