Migrated ike_mobike task to INIT/METHOD macros.
[strongswan.git] / src / libcharon / 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
16 #include "ike_mobike.h"
17
18 #include <string.h>
19
20 #include <hydra.h>
21 #include <daemon.h>
22 #include <sa/tasks/ike_natd.h>
23 #include <encoding/payloads/notify_payload.h>
24
25 #define COOKIE2_SIZE 16
26 #define MAX_ADDITIONAL_ADDRS 8
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 enumerator_t *enumerator;
100 payload_t *payload;
101 bool first = TRUE;
102
103 enumerator = message->create_payload_enumerator(message);
104 while (enumerator->enumerate(enumerator, &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 enumerator->destroy(enumerator);
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 int added = 0;
195
196 me = this->ike_sa->get_my_host(this->ike_sa);
197 enumerator = hydra->kernel_interface->create_address_enumerator(
198 hydra->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 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 }
222 }
223 if (!added)
224 {
225 message->add_notify(message, FALSE, NO_ADDITIONAL_ADDRESSES, chunk_empty);
226 }
227 enumerator->destroy(enumerator);
228 }
229
230 /**
231 * build a cookie and add it to the message
232 */
233 static 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
247 /**
248 * update addresses of associated CHILD_SAs
249 */
250 static void update_children(private_ike_mobike_t *this)
251 {
252 iterator_t *iterator;
253 child_sa_t *child_sa;
254
255 iterator = this->ike_sa->create_child_sa_iterator(this->ike_sa);
256 while (iterator->iterate(iterator, (void**)&child_sa))
257 {
258 if (child_sa->update(child_sa,
259 this->ike_sa->get_my_host(this->ike_sa),
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 }
268 }
269 iterator->destroy(iterator);
270 }
271
272 /**
273 * Apply the port of the old host, if its ip equals the new, use port otherwise.
274 */
275 static void apply_port(host_t *host, host_t *old, u_int16_t port)
276 {
277 if (host->ip_equals(host, old))
278 {
279 port = old->get_port(old);
280 }
281 else if (port == IKEV2_UDP_PORT)
282 {
283 port = IKEV2_NATT_PORT;
284 }
285 host->set_port(host, port);
286 }
287
288 METHOD(ike_mobike_t, transmit, void,
289 private_ike_mobike_t *this, packet_t *packet)
290 {
291 host_t *me, *other, *me_old, *other_old;
292 iterator_t *iterator;
293 ike_cfg_t *ike_cfg;
294 packet_t *copy;
295
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);
303 ike_cfg = this->ike_sa->get_ike_cfg(this->ike_sa);
304
305 me = hydra->kernel_interface->get_source_addr(
306 hydra->kernel_interface, other_old, NULL);
307 if (me)
308 {
309 apply_port(me, me_old, ike_cfg->get_my_port(ike_cfg));
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);
314 }
315
316 iterator = this->ike_sa->create_additional_address_iterator(this->ike_sa);
317 while (iterator->iterate(iterator, (void**)&other))
318 {
319 me = hydra->kernel_interface->get_source_addr(
320 hydra->kernel_interface, other, NULL);
321 if (me)
322 {
323 if (me->get_family(me) != other->get_family(other))
324 {
325 me->destroy(me);
326 continue;
327 }
328 /* reuse port for an active address, 4500 otherwise */
329 apply_port(me, me_old, ike_cfg->get_my_port(ike_cfg));
330 other = other->clone(other);
331 apply_port(other, other_old, ike_cfg->get_other_port(ike_cfg));
332 DBG1(DBG_IKE, "checking path %#H - %#H", me, other);
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
342 METHOD(task_t, build_i, status_t,
343 private_ike_mobike_t *this, message_t *message)
344 {
345 if (message->get_message_id(message) == 1)
346 { /* only in first IKE_AUTH */
347 message->add_notify(message, FALSE, MOBIKE_SUPPORTED, chunk_empty);
348 build_address_list(this, message);
349 }
350 else if (message->get_exchange_type(message) == INFORMATIONAL)
351 {
352 host_t *old, *new;
353
354 /* we check if the existing address is still valid */
355 old = message->get_source(message);
356 new = hydra->kernel_interface->get_source_addr(hydra->kernel_interface,
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 }
370 if (this->update)
371 {
372 message->add_notify(message, FALSE, UPDATE_SA_ADDRESSES, chunk_empty);
373 build_cookie(this, message);
374 update_children(this);
375 }
376 if (this->address)
377 {
378 build_address_list(this, message);
379 }
380 if (this->natd)
381 {
382 this->natd->task.build(&this->natd->task, message);
383 }
384 }
385 return NEED_MORE;
386 }
387
388 METHOD(task_t, process_r, status_t,
389 private_ike_mobike_t *this, message_t *message)
390 {
391 if (message->get_message_id(message) == 1)
392 { /* only first IKE_AUTH */
393 process_payloads(this, message);
394 }
395 else if (message->get_exchange_type(message) == INFORMATIONAL)
396 {
397 process_payloads(this, message);
398 if (this->update)
399 {
400 host_t *me, *other;
401
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 }
407
408 if (this->natd)
409 {
410 this->natd->task.process(&this->natd->task, message);
411 }
412 }
413 return NEED_MORE;
414 }
415
416 METHOD(task_t, build_r, status_t,
417 private_ike_mobike_t *this, message_t *message)
418 {
419 if (message->get_exchange_type(message) == IKE_AUTH &&
420 this->ike_sa->get_state(this->ike_sa) == IKE_ESTABLISHED)
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 }
429 else if (message->get_exchange_type(message) == INFORMATIONAL)
430 {
431 if (this->natd)
432 {
433 this->natd->task.build(&this->natd->task, message);
434 }
435 if (this->cookie2.ptr)
436 {
437 message->add_notify(message, FALSE, COOKIE2, this->cookie2);
438 chunk_free(&this->cookie2);
439 }
440 if (this->update)
441 {
442 update_children(this);
443 }
444 return SUCCESS;
445 }
446 return NEED_MORE;
447 }
448
449 METHOD(task_t, process_i, status_t,
450 private_ike_mobike_t *this, message_t *message)
451 {
452 if (message->get_exchange_type(message) == IKE_AUTH &&
453 this->ike_sa->get_state(this->ike_sa) == IKE_ESTABLISHED)
454 {
455 process_payloads(this, message);
456 return SUCCESS;
457 }
458 else if (message->get_exchange_type(message) == INFORMATIONAL)
459 {
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 }
467 if (this->cookie2.ptr)
468 { /* check cookie if we included one */
469 chunk_t cookie2;
470
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 }
486 if (this->natd)
487 {
488 this->natd->task.process(&this->natd->task, message);
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 }
496 }
497 if (this->update)
498 {
499 /* update again, as NAT state may have changed */
500 update_children(this);
501 }
502 if (this->check)
503 {
504 host_t *me_new, *me_old, *other_new, *other_old;
505
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);
510
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));
515 }
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;
526 if (this->natd)
527 {
528 this->natd->task.destroy(&this->natd->task);
529 }
530 this->natd = ike_natd_create(this->ike_sa, this->initiator);
531 this->ike_sa->set_pending_updates(this->ike_sa, 1);
532 return NEED_MORE;
533 }
534 }
535 return SUCCESS;
536 }
537 return NEED_MORE;
538 }
539
540 METHOD(ike_mobike_t, roam, void,
541 private_ike_mobike_t *this, bool address)
542 {
543 this->check = TRUE;
544 this->address = address;
545 this->ike_sa->set_pending_updates(this->ike_sa,
546 this->ike_sa->get_pending_updates(this->ike_sa) + 1);
547 }
548
549 METHOD(ike_mobike_t, dpd, void,
550 private_ike_mobike_t *this)
551 {
552 if (!this->natd)
553 {
554 this->natd = ike_natd_create(this->ike_sa, this->initiator);
555 }
556 this->address = FALSE;
557 this->ike_sa->set_pending_updates(this->ike_sa,
558 this->ike_sa->get_pending_updates(this->ike_sa) + 1);
559 }
560
561 METHOD(ike_mobike_t, is_probing, bool,
562 private_ike_mobike_t *this)
563 {
564 return this->check;
565 }
566
567 METHOD(task_t, get_type, task_type_t,
568 private_ike_mobike_t *this)
569 {
570 return IKE_MOBIKE;
571 }
572
573 METHOD(task_t, migrate, void,
574 private_ike_mobike_t *this, ike_sa_t *ike_sa)
575 {
576 chunk_free(&this->cookie2);
577 this->ike_sa = ike_sa;
578 if (this->natd)
579 {
580 this->natd->task.migrate(&this->natd->task, ike_sa);
581 }
582 }
583
584 METHOD(task_t, destroy, void,
585 private_ike_mobike_t *this)
586 {
587 chunk_free(&this->cookie2);
588 if (this->natd)
589 {
590 this->natd->task.destroy(&this->natd->task);
591 }
592 free(this);
593 }
594
595 /*
596 * Described in header.
597 */
598 ike_mobike_t *ike_mobike_create(ike_sa_t *ike_sa, bool initiator)
599 {
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 );
618
619 if (initiator)
620 {
621 this->public.task.build = _build_i;
622 this->public.task.process = _process_i;
623 }
624 else
625 {
626 this->public.task.build = _build_r;
627 this->public.task.process = _process_r;
628 }
629
630 return &this->public;
631 }
632