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