Moving charon to libcharon.
[strongswan.git] / src / libcharon / sa / tasks / ike_natd.c
1 /*
2 * Copyright (C) 2006-2007 Martin Willi
3 * Copyright (C) 2006 Tobias Brunner, Daniel Roethlisberger
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_natd.h"
18
19 #include <string.h>
20
21 #include <daemon.h>
22 #include <config/peer_cfg.h>
23 #include <crypto/hashers/hasher.h>
24 #include <encoding/payloads/notify_payload.h>
25
26
27 typedef struct private_ike_natd_t private_ike_natd_t;
28
29 /**
30 * Private members of a ike_natd_t task.
31 */
32 struct private_ike_natd_t {
33
34 /**
35 * Public methods and task_t interface.
36 */
37 ike_natd_t public;
38
39 /**
40 * Assigned IKE_SA.
41 */
42 ike_sa_t *ike_sa;
43
44 /**
45 * Are we the initiator?
46 */
47 bool initiator;
48
49 /**
50 * Hasher used to build NAT detection hashes
51 */
52 hasher_t *hasher;
53
54 /**
55 * Did we process any NAT detection notifys for a source address?
56 */
57 bool src_seen;
58
59 /**
60 * Did we process any NAT detection notifys for a destination address?
61 */
62 bool dst_seen;
63
64 /**
65 * Have we found a matching source address NAT hash?
66 */
67 bool src_matched;
68
69 /**
70 * Have we found a matching destination address NAT hash?
71 */
72 bool dst_matched;
73
74 /**
75 * whether NAT mappings for our NATed address has changed
76 */
77 bool mapping_changed;
78 };
79
80
81 /**
82 * Build NAT detection hash for a host
83 */
84 static chunk_t generate_natd_hash(private_ike_natd_t *this,
85 ike_sa_id_t *ike_sa_id, host_t *host)
86 {
87 chunk_t natd_chunk, spi_i_chunk, spi_r_chunk, addr_chunk, port_chunk;
88 chunk_t natd_hash;
89 u_int64_t spi_i, spi_r;
90 u_int16_t port;
91
92 /* prepare all required chunks */
93 spi_i = ike_sa_id->get_initiator_spi(ike_sa_id);
94 spi_r = ike_sa_id->get_responder_spi(ike_sa_id);
95 spi_i_chunk.ptr = (void*)&spi_i;
96 spi_i_chunk.len = sizeof(spi_i);
97 spi_r_chunk.ptr = (void*)&spi_r;
98 spi_r_chunk.len = sizeof(spi_r);
99 port = htons(host->get_port(host));
100 port_chunk.ptr = (void*)&port;
101 port_chunk.len = sizeof(port);
102 addr_chunk = host->get_address(host);
103
104 /* natd_hash = SHA1( spi_i | spi_r | address | port ) */
105 natd_chunk = chunk_cat("cccc", spi_i_chunk, spi_r_chunk, addr_chunk, port_chunk);
106 this->hasher->allocate_hash(this->hasher, natd_chunk, &natd_hash);
107 DBG3(DBG_IKE, "natd_chunk %B", &natd_chunk);
108 DBG3(DBG_IKE, "natd_hash %B", &natd_hash);
109
110 chunk_free(&natd_chunk);
111 return natd_hash;
112 }
113
114 /**
115 * build a faked NATD payload to enforce UDP encap
116 */
117 static chunk_t generate_natd_hash_faked(private_ike_natd_t *this)
118 {
119 rng_t *rng;
120 chunk_t chunk;
121
122 rng = lib->crypto->create_rng(lib->crypto, RNG_WEAK);
123 if (!rng)
124 {
125 DBG1(DBG_IKE, "unable to get random bytes for NATD fake");
126 return chunk_empty;
127 }
128 rng->allocate_bytes(rng, HASH_SIZE_SHA1, &chunk);
129 rng->destroy(rng);
130 return chunk;
131 }
132
133 /**
134 * Build a NAT detection notify payload.
135 */
136 static notify_payload_t *build_natd_payload(private_ike_natd_t *this,
137 notify_type_t type, host_t *host)
138 {
139 chunk_t hash;
140 notify_payload_t *notify;
141 ike_sa_id_t *ike_sa_id;
142 ike_cfg_t *config;
143
144 ike_sa_id = this->ike_sa->get_id(this->ike_sa);
145 config = this->ike_sa->get_ike_cfg(this->ike_sa);
146 if (config->force_encap(config) && type == NAT_DETECTION_SOURCE_IP)
147 {
148 hash = generate_natd_hash_faked(this);
149 }
150 else
151 {
152 hash = generate_natd_hash(this, ike_sa_id, host);
153 }
154 notify = notify_payload_create();
155 notify->set_notify_type(notify, type);
156 notify->set_notification_data(notify, hash);
157 chunk_free(&hash);
158
159 return notify;
160 }
161
162 /**
163 * read notifys from message and evaluate them
164 */
165 static void process_payloads(private_ike_natd_t *this, message_t *message)
166 {
167 enumerator_t *enumerator;
168 payload_t *payload;
169 notify_payload_t *notify;
170 chunk_t hash, src_hash, dst_hash;
171 ike_sa_id_t *ike_sa_id;
172 host_t *me, *other;
173 ike_cfg_t *config;
174
175 /* Precompute NAT-D hashes for incoming NAT notify comparison */
176 ike_sa_id = message->get_ike_sa_id(message);
177 me = message->get_destination(message);
178 other = message->get_source(message);
179 dst_hash = generate_natd_hash(this, ike_sa_id, me);
180 src_hash = generate_natd_hash(this, ike_sa_id, other);
181
182 DBG3(DBG_IKE, "precalculated src_hash %B", &src_hash);
183 DBG3(DBG_IKE, "precalculated dst_hash %B", &dst_hash);
184
185 enumerator = message->create_payload_enumerator(message);
186 while (enumerator->enumerate(enumerator, &payload))
187 {
188 if (payload->get_type(payload) != NOTIFY)
189 {
190 continue;
191 }
192 notify = (notify_payload_t*)payload;
193 switch (notify->get_notify_type(notify))
194 {
195 case NAT_DETECTION_DESTINATION_IP:
196 {
197 this->dst_seen = TRUE;
198 hash = notify->get_notification_data(notify);
199 if (!this->dst_matched)
200 {
201 DBG3(DBG_IKE, "received dst_hash %B", &hash);
202 if (chunk_equals(hash, dst_hash))
203 {
204 this->dst_matched = TRUE;
205 }
206 }
207 /* RFC4555 says we should also compare against IKE_SA_INIT
208 * NATD payloads, but this does not work: We are running
209 * there at port 500, but use 4500 afterwards... */
210 if (message->get_exchange_type(message) == INFORMATIONAL &&
211 this->initiator && !this->dst_matched)
212 {
213 this->mapping_changed = this->ike_sa->has_mapping_changed(
214 this->ike_sa, hash);
215 }
216 break;
217 }
218 case NAT_DETECTION_SOURCE_IP:
219 {
220 this->src_seen = TRUE;
221 if (!this->src_matched)
222 {
223 hash = notify->get_notification_data(notify);
224 DBG3(DBG_IKE, "received src_hash %B", &hash);
225 if (chunk_equals(hash, src_hash))
226 {
227 this->src_matched = TRUE;
228 }
229 }
230 break;
231 }
232 default:
233 break;
234 }
235 }
236 enumerator->destroy(enumerator);
237
238 chunk_free(&src_hash);
239 chunk_free(&dst_hash);
240
241 if (this->src_seen && this->dst_seen)
242 {
243 this->ike_sa->enable_extension(this->ike_sa, EXT_NATT);
244
245 this->ike_sa->set_condition(this->ike_sa, COND_NAT_HERE,
246 !this->dst_matched);
247 this->ike_sa->set_condition(this->ike_sa, COND_NAT_THERE,
248 !this->src_matched);
249 config = this->ike_sa->get_ike_cfg(this->ike_sa);
250 if (this->dst_matched && this->src_matched &&
251 config->force_encap(config))
252 {
253 this->ike_sa->set_condition(this->ike_sa, COND_NAT_FAKE, TRUE);
254 }
255 }
256 }
257
258 /**
259 * Implementation of task_t.process for initiator
260 */
261 static status_t process_i(private_ike_natd_t *this, message_t *message)
262 {
263 process_payloads(this, message);
264
265 if (message->get_exchange_type(message) == IKE_SA_INIT)
266 {
267 peer_cfg_t *peer_cfg = this->ike_sa->get_peer_cfg(this->ike_sa);
268
269 #ifdef ME
270 /* if we are on a mediated connection we have already switched to
271 * port 4500 and the correct destination port is already configured,
272 * therefore we must not switch again */
273 if (peer_cfg->get_mediated_by(peer_cfg))
274 {
275 return SUCCESS;
276 }
277 #endif /* ME */
278
279 if (this->ike_sa->has_condition(this->ike_sa, COND_NAT_ANY) ||
280 #ifdef ME
281 /* if we are on a mediation connection we switch to port 4500 even
282 * if no NAT is detected. */
283 peer_cfg->is_mediation(peer_cfg) ||
284 #endif /* ME */
285 /* if peer supports NAT-T, we switch to port 4500 even
286 * if no NAT is detected. MOBIKE requires this. */
287 (peer_cfg->use_mobike(peer_cfg) &&
288 this->ike_sa->supports_extension(this->ike_sa, EXT_NATT)))
289 {
290 host_t *me, *other;
291
292 /* do not switch if we have a custom port from mobike/NAT */
293 me = this->ike_sa->get_my_host(this->ike_sa);
294 if (me->get_port(me) == IKEV2_UDP_PORT)
295 {
296 me->set_port(me, IKEV2_NATT_PORT);
297 }
298 other = this->ike_sa->get_other_host(this->ike_sa);
299 if (other->get_port(other) == IKEV2_UDP_PORT)
300 {
301 other->set_port(other, IKEV2_NATT_PORT);
302 }
303 }
304 }
305
306 return SUCCESS;
307 }
308
309 /**
310 * Implementation of task_t.process for initiator
311 */
312 static status_t build_i(private_ike_natd_t *this, message_t *message)
313 {
314 notify_payload_t *notify;
315 enumerator_t *enumerator;
316 ike_cfg_t *ike_cfg;
317 host_t *host;
318
319 if (this->hasher == NULL)
320 {
321 DBG1(DBG_IKE, "unable to build NATD payloads, SHA1 not supported");
322 return NEED_MORE;
323 }
324
325 ike_cfg = this->ike_sa->get_ike_cfg(this->ike_sa);
326
327 /* destination is always set */
328 host = message->get_destination(message);
329 notify = build_natd_payload(this, NAT_DETECTION_DESTINATION_IP, host);
330 message->add_payload(message, (payload_t*)notify);
331
332 /* source may be any, we have 3 possibilities to get our source address:
333 * 1. It is defined in the config => use the one of the IKE_SA
334 * 2. We do a routing lookup in the kernel interface
335 * 3. Include all possbile addresses
336 */
337 host = message->get_source(message);
338 if (!host->is_anyaddr(host))
339 { /* 1. */
340 notify = build_natd_payload(this, NAT_DETECTION_SOURCE_IP, host);
341 message->add_payload(message, (payload_t*)notify);
342 }
343 else
344 {
345 host = charon->kernel_interface->get_source_addr(charon->kernel_interface,
346 this->ike_sa->get_other_host(this->ike_sa), NULL);
347 if (host)
348 { /* 2. */
349 host->set_port(host, ike_cfg->get_my_port(ike_cfg));
350 notify = build_natd_payload(this, NAT_DETECTION_SOURCE_IP, host);
351 message->add_payload(message, (payload_t*)notify);
352 host->destroy(host);
353 }
354 else
355 { /* 3. */
356 enumerator = charon->kernel_interface->create_address_enumerator(
357 charon->kernel_interface, FALSE, FALSE);
358 while (enumerator->enumerate(enumerator, (void**)&host))
359 {
360 /* apply port 500 to host, but work on a copy */
361 host = host->clone(host);
362 host->set_port(host, ike_cfg->get_my_port(ike_cfg));
363 notify = build_natd_payload(this, NAT_DETECTION_SOURCE_IP, host);
364 host->destroy(host);
365 message->add_payload(message, (payload_t*)notify);
366 }
367 enumerator->destroy(enumerator);
368 }
369 }
370 return NEED_MORE;
371 }
372
373 /**
374 * Implementation of task_t.build for responder
375 */
376 static status_t build_r(private_ike_natd_t *this, message_t *message)
377 {
378 notify_payload_t *notify;
379 host_t *me, *other;
380
381 /* only add notifies on successfull responses. */
382 if (message->get_exchange_type(message) == IKE_SA_INIT &&
383 message->get_payload(message, SECURITY_ASSOCIATION) == NULL)
384 {
385 return SUCCESS;
386 }
387
388 if (this->src_seen && this->dst_seen)
389 {
390 if (this->hasher == NULL)
391 {
392 DBG1(DBG_IKE, "unable to build NATD payloads, SHA1 not supported");
393 return SUCCESS;
394 }
395
396 /* initiator seems to support NAT detection, add response */
397 me = message->get_source(message);
398 notify = build_natd_payload(this, NAT_DETECTION_SOURCE_IP, me);
399 message->add_payload(message, (payload_t*)notify);
400
401 other = message->get_destination(message);
402 notify = build_natd_payload(this, NAT_DETECTION_DESTINATION_IP, other);
403 message->add_payload(message, (payload_t*)notify);
404 }
405 return SUCCESS;
406 }
407
408 /**
409 * Implementation of task_t.process for responder
410 */
411 static status_t process_r(private_ike_natd_t *this, message_t *message)
412 {
413 process_payloads(this, message);
414
415 return NEED_MORE;
416 }
417
418 /**
419 * Implementation of task_t.get_type
420 */
421 static task_type_t get_type(private_ike_natd_t *this)
422 {
423 return IKE_NATD;
424 }
425
426 /**
427 * Implementation of task_t.migrate
428 */
429 static void migrate(private_ike_natd_t *this, ike_sa_t *ike_sa)
430 {
431 this->ike_sa = ike_sa;
432 this->src_seen = FALSE;
433 this->dst_seen = FALSE;
434 this->src_matched = FALSE;
435 this->dst_matched = FALSE;
436 this->mapping_changed = FALSE;
437 }
438
439 /**
440 * Implementation of ike_natd_t.has_mapping_changed
441 */
442 static bool has_mapping_changed(private_ike_natd_t *this)
443 {
444 return this->mapping_changed;
445 }
446
447 /**
448 * Implementation of task_t.destroy
449 */
450 static void destroy(private_ike_natd_t *this)
451 {
452 DESTROY_IF(this->hasher);
453 free(this);
454 }
455
456 /*
457 * Described in header.
458 */
459 ike_natd_t *ike_natd_create(ike_sa_t *ike_sa, bool initiator)
460 {
461 private_ike_natd_t *this = malloc_thing(private_ike_natd_t);
462
463 this->public.task.get_type = (task_type_t(*)(task_t*))get_type;
464 this->public.task.migrate = (void(*)(task_t*,ike_sa_t*))migrate;
465 this->public.task.destroy = (void(*)(task_t*))destroy;
466
467 if (initiator)
468 {
469 this->public.task.build = (status_t(*)(task_t*,message_t*))build_i;
470 this->public.task.process = (status_t(*)(task_t*,message_t*))process_i;
471 }
472 else
473 {
474 this->public.task.build = (status_t(*)(task_t*,message_t*))build_r;
475 this->public.task.process = (status_t(*)(task_t*,message_t*))process_r;
476 }
477
478 this->public.has_mapping_changed = (bool(*)(ike_natd_t*))has_mapping_changed;
479
480 this->ike_sa = ike_sa;
481 this->initiator = initiator;
482 this->hasher = lib->crypto->create_hasher(lib->crypto, HASH_SHA1);
483 this->src_seen = FALSE;
484 this->dst_seen = FALSE;
485 this->src_matched = FALSE;
486 this->dst_matched = FALSE;
487 this->mapping_changed = FALSE;
488
489 return &this->public;
490 }