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