ff3fbb77c3f3f6b1b3b8799e9527c8e43ddbc640
[strongswan.git] / src / charon / sa / tasks / ike_natd.c
1 /**
2 * @file ike_natd.c
3 *
4 * @brief Implementation of the ike_natd task.
5 *
6 */
7
8 /*
9 * Copyright (C) 2006-2007 Martin Willi
10 * Copyright (C) 2006 Tobias Brunner, Daniel Roethlisberger
11 * Hochschule fuer Technik Rapperswil
12 *
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
17 *
18 * This program is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 * for more details.
22 */
23
24 #include "ike_natd.h"
25
26 #include <string.h>
27
28 #include <daemon.h>
29 #include <config/peer_cfg.h>
30 #include <crypto/hashers/hasher.h>
31 #include <encoding/payloads/notify_payload.h>
32
33
34 typedef struct private_ike_natd_t private_ike_natd_t;
35
36 /**
37 * Private members of a ike_natd_t task.
38 */
39 struct private_ike_natd_t {
40
41 /**
42 * Public methods and task_t interface.
43 */
44 ike_natd_t public;
45
46 /**
47 * Assigned IKE_SA.
48 */
49 ike_sa_t *ike_sa;
50
51 /**
52 * Are we the initiator?
53 */
54 bool initiator;
55
56 /**
57 * Hasher used to build NAT detection hashes
58 */
59 hasher_t *hasher;
60
61 /**
62 * Did we process any NAT detection notifys for a source address?
63 */
64 bool src_seen;
65
66 /**
67 * Did we process any NAT detection notifys for a destination address?
68 */
69 bool dst_seen;
70
71 /**
72 * Have we found a matching source address NAT hash?
73 */
74 bool src_matched;
75
76 /**
77 * Have we found a matching destination address NAT hash?
78 */
79 bool dst_matched;
80 };
81
82
83 /**
84 * Build NAT detection hash for a host
85 */
86 static chunk_t generate_natd_hash(private_ike_natd_t *this,
87 ike_sa_id_t *ike_sa_id, host_t *host)
88 {
89 chunk_t natd_chunk, spi_i_chunk, spi_r_chunk, addr_chunk, port_chunk;
90 chunk_t natd_hash;
91 u_int64_t spi_i, spi_r;
92 u_int16_t port;
93
94 /* prepare all requred chunks */
95 spi_i = ike_sa_id->get_initiator_spi(ike_sa_id);
96 spi_r = ike_sa_id->get_responder_spi(ike_sa_id);
97 spi_i_chunk.ptr = (void*)&spi_i;
98 spi_i_chunk.len = sizeof(spi_i);
99 spi_r_chunk.ptr = (void*)&spi_r;
100 spi_r_chunk.len = sizeof(spi_r);
101 port = htons(host->get_port(host));
102 port_chunk.ptr = (void*)&port;
103 port_chunk.len = sizeof(port);
104 addr_chunk = host->get_address(host);
105
106 /* natd_hash = SHA1( spi_i | spi_r | address | port ) */
107 natd_chunk = chunk_cat("cccc", spi_i_chunk, spi_r_chunk, addr_chunk, port_chunk);
108 this->hasher->allocate_hash(this->hasher, natd_chunk, &natd_hash);
109 DBG3(DBG_IKE, "natd_chunk %B", &natd_chunk);
110 DBG3(DBG_IKE, "natd_hash %B", &natd_hash);
111
112 chunk_free(&natd_chunk);
113 return natd_hash;
114 }
115
116 /**
117 * build a faked NATD payload to enforce UDP encap
118 */
119 static chunk_t generate_natd_hash_faked(private_ike_natd_t *this)
120 {
121 randomizer_t *randomizer;
122 chunk_t chunk;
123
124 randomizer = randomizer_create();
125 if (randomizer->allocate_pseudo_random_bytes(randomizer, HASH_SIZE_SHA1,
126 &chunk) != SUCCESS)
127 {
128 DBG1(DBG_IKE, "unable to get random bytes for NATD fake");
129 chunk = chunk_empty;
130 }
131 randomizer->destroy(randomizer);
132 return chunk;
133 }
134
135 /**
136 * Build a NAT detection notify payload.
137 */
138 static notify_payload_t *build_natd_payload(private_ike_natd_t *this,
139 notify_type_t type, host_t *host)
140 {
141 chunk_t hash;
142 notify_payload_t *notify;
143 ike_sa_id_t *ike_sa_id;
144 peer_cfg_t *config;
145
146 ike_sa_id = this->ike_sa->get_id(this->ike_sa);
147 config = this->ike_sa->get_peer_cfg(this->ike_sa);
148 notify = notify_payload_create();
149 notify->set_notify_type(notify, type);
150 if (config->force_encap(config) && type == NAT_DETECTION_SOURCE_IP)
151 {
152 hash = generate_natd_hash_faked(this);
153 }
154 else
155 {
156 hash = generate_natd_hash(this, ike_sa_id, host);
157 }
158 notify->set_notification_data(notify, hash);
159 chunk_free(&hash);
160
161 return notify;
162 }
163
164 /**
165 * read notifys from message and evaluate them
166 */
167 static void process_payloads(private_ike_natd_t *this, message_t *message)
168 {
169 iterator_t *iterator;
170 payload_t *payload;
171 notify_payload_t *notify;
172 chunk_t hash, src_hash, dst_hash;
173 ike_sa_id_t *ike_sa_id;
174 host_t *me, *other;
175 peer_cfg_t *config;
176
177 /* Precompute NAT-D hashes for incoming NAT notify comparison */
178 ike_sa_id = message->get_ike_sa_id(message);
179 me = message->get_destination(message);
180 other = message->get_source(message);
181 dst_hash = generate_natd_hash(this, ike_sa_id, me);
182 src_hash = generate_natd_hash(this, ike_sa_id, other);
183
184 DBG3(DBG_IKE, "precalculated src_hash %B", &src_hash);
185 DBG3(DBG_IKE, "precalculated dst_hash %B", &dst_hash);
186
187 iterator = message->get_payload_iterator(message);
188 while (iterator->iterate(iterator, (void**)&payload))
189 {
190 if (payload->get_type(payload) != NOTIFY)
191 {
192 continue;
193 }
194 notify = (notify_payload_t*)payload;
195 switch (notify->get_notify_type(notify))
196 {
197 case NAT_DETECTION_DESTINATION_IP:
198 {
199 this->dst_seen = TRUE;
200 if (!this->dst_matched)
201 {
202 hash = notify->get_notification_data(notify);
203 DBG3(DBG_IKE, "received dst_hash %B", &hash);
204 if (chunk_equals(hash, dst_hash))
205 {
206 this->dst_matched = TRUE;
207 }
208 }
209 break;
210 }
211 case NAT_DETECTION_SOURCE_IP:
212 {
213 this->src_seen = TRUE;
214 if (!this->src_matched)
215 {
216 hash = notify->get_notification_data(notify);
217 DBG3(DBG_IKE, "received src_hash %B", &hash);
218 if (chunk_equals(hash, src_hash))
219 {
220 this->src_matched = TRUE;
221 }
222 }
223 break;
224 }
225 default:
226 break;
227 }
228 }
229 iterator->destroy(iterator);
230
231 chunk_free(&src_hash);
232 chunk_free(&dst_hash);
233
234 if (this->src_seen && this->dst_seen)
235 {
236 this->ike_sa->enable_extension(this->ike_sa, EXT_NATT);
237
238 this->ike_sa->set_condition(this->ike_sa, COND_NAT_HERE,
239 !this->dst_matched);
240 this->ike_sa->set_condition(this->ike_sa, COND_NAT_THERE,
241 !this->src_matched);
242 config = this->ike_sa->get_peer_cfg(this->ike_sa);
243 if (config->force_encap(config))
244 {
245 this->ike_sa->set_condition(this->ike_sa, COND_NAT_FAKE, TRUE);
246 }
247 }
248 }
249
250 /**
251 * Implementation of task_t.process for initiator
252 */
253 static status_t process_i(private_ike_natd_t *this, message_t *message)
254 {
255 process_payloads(this, message);
256
257 if (message->get_exchange_type(message) == IKE_SA_INIT)
258 {
259 peer_cfg_t *peer_cfg = this->ike_sa->get_peer_cfg(this->ike_sa);
260
261 if (this->ike_sa->has_condition(this->ike_sa, COND_NAT_ANY) ||
262 /* if peer supports NAT-T, we switch to port 4500 even
263 * if no NAT is detected. MOBIKE requires this. */
264 (peer_cfg->use_mobike(peer_cfg) &&
265 this->ike_sa->supports_extension(this->ike_sa, EXT_NATT)))
266 {
267 host_t *me, *other;
268
269 /* do not switch if we have a custom port from mobike/NAT */
270 me = this->ike_sa->get_my_host(this->ike_sa);
271 if (me->get_port(me) == IKEV2_UDP_PORT)
272 {
273 me->set_port(me, IKEV2_NATT_PORT);
274 }
275 other = this->ike_sa->get_other_host(this->ike_sa);
276 if (other->get_port(other) == IKEV2_UDP_PORT)
277 {
278 other->set_port(other, IKEV2_NATT_PORT);
279 }
280 }
281 }
282
283 return SUCCESS;
284 }
285
286 /**
287 * Implementation of task_t.process for initiator
288 */
289 static status_t build_i(private_ike_natd_t *this, message_t *message)
290 {
291 notify_payload_t *notify;
292 iterator_t *iterator;
293 host_t *host;
294
295 /* destination is always set */
296 host = message->get_destination(message);
297 notify = build_natd_payload(this, NAT_DETECTION_DESTINATION_IP, host);
298 message->add_payload(message, (payload_t*)notify);
299
300 /* source may be any, we have 3 possibilities to get our source address:
301 * 1. It is defined in the config => use the one of the IKE_SA
302 * 2. We do a routing lookup in the kernel interface
303 * 3. Include all possbile addresses
304 */
305 host = message->get_source(message);
306 if (!host->is_anyaddr(host))
307 { /* 1. */
308 notify = build_natd_payload(this, NAT_DETECTION_SOURCE_IP, host);
309 message->add_payload(message, (payload_t*)notify);
310 }
311 else
312 {
313 host = charon->kernel_interface->get_source_addr(
314 charon->kernel_interface,
315 this->ike_sa->get_other_host(this->ike_sa));
316 if (host)
317 { /* 2. */
318 host->set_port(host, IKEV2_UDP_PORT);
319 notify = build_natd_payload(this, NAT_DETECTION_SOURCE_IP, host);
320 message->add_payload(message, (payload_t*)notify);
321 host->destroy(host);
322 }
323 else
324 { /* 3. */
325 iterator = charon->kernel_interface->create_address_iterator(
326 charon->kernel_interface);
327 while (iterator->iterate(iterator, (void**)&host))
328 {
329 notify = build_natd_payload(this, NAT_DETECTION_SOURCE_IP, host);
330 message->add_payload(message, (payload_t*)notify);
331 }
332 iterator->destroy(iterator);
333 }
334 }
335 return NEED_MORE;
336 }
337
338 /**
339 * Implementation of task_t.build for responder
340 */
341 static status_t build_r(private_ike_natd_t *this, message_t *message)
342 {
343 notify_payload_t *notify;
344 host_t *me, *other;
345
346 /* only add notifies on successfull responses. */
347 if (message->get_exchange_type(message) == IKE_SA_INIT &&
348 message->get_payload(message, SECURITY_ASSOCIATION) == NULL)
349 {
350 return SUCCESS;
351 }
352
353 if (this->src_seen && this->dst_seen)
354 {
355 /* initiator seems to support NAT detection, add response */
356 me = message->get_source(message);
357 notify = build_natd_payload(this, NAT_DETECTION_SOURCE_IP, me);
358 message->add_payload(message, (payload_t*)notify);
359
360 other = message->get_destination(message);
361 notify = build_natd_payload(this, NAT_DETECTION_DESTINATION_IP, other);
362 message->add_payload(message, (payload_t*)notify);
363 }
364 return SUCCESS;
365 }
366
367 /**
368 * Implementation of task_t.process for responder
369 */
370 static status_t process_r(private_ike_natd_t *this, message_t *message)
371 {
372 process_payloads(this, message);
373
374 return NEED_MORE;
375 }
376
377 /**
378 * Implementation of task_t.get_type
379 */
380 static task_type_t get_type(private_ike_natd_t *this)
381 {
382 return IKE_NATD;
383 }
384
385 /**
386 * Implementation of task_t.migrate
387 */
388 static void migrate(private_ike_natd_t *this, ike_sa_t *ike_sa)
389 {
390 this->ike_sa = ike_sa;
391 this->src_seen = FALSE;
392 this->dst_seen = FALSE;
393 this->src_matched = FALSE;
394 this->dst_matched = FALSE;
395 }
396
397 /**
398 * Implementation of task_t.destroy
399 */
400 static void destroy(private_ike_natd_t *this)
401 {
402 this->hasher->destroy(this->hasher);
403 free(this);
404 }
405
406 /*
407 * Described in header.
408 */
409 ike_natd_t *ike_natd_create(ike_sa_t *ike_sa, bool initiator)
410 {
411 private_ike_natd_t *this = malloc_thing(private_ike_natd_t);
412
413 this->public.task.get_type = (task_type_t(*)(task_t*))get_type;
414 this->public.task.migrate = (void(*)(task_t*,ike_sa_t*))migrate;
415 this->public.task.destroy = (void(*)(task_t*))destroy;
416
417 if (initiator)
418 {
419 this->public.task.build = (status_t(*)(task_t*,message_t*))build_i;
420 this->public.task.process = (status_t(*)(task_t*,message_t*))process_i;
421 }
422 else
423 {
424 this->public.task.build = (status_t(*)(task_t*,message_t*))build_r;
425 this->public.task.process = (status_t(*)(task_t*,message_t*))process_r;
426 }
427
428 this->ike_sa = ike_sa;
429 this->initiator = initiator;
430 this->hasher = hasher_create(HASH_SHA1);
431 this->src_seen = FALSE;
432 this->dst_seen = FALSE;
433 this->src_matched = FALSE;
434 this->dst_matched = FALSE;
435
436 return &this->public;
437 }