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