moved force_encap to ike_config, enables responder to enforce udp encapsulation
[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 ike_cfg_t *config;
145
146 ike_sa_id = this->ike_sa->get_id(this->ike_sa);
147 config = this->ike_sa->get_ike_cfg(this->ike_sa);
148 if (config->force_encap(config) && type == NAT_DETECTION_SOURCE_IP)
149 {
150 hash = generate_natd_hash_faked(this);
151 }
152 else
153 {
154 hash = generate_natd_hash(this, ike_sa_id, host);
155 }
156 notify = notify_payload_create();
157 notify->set_notify_type(notify, type);
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 ike_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_ike_cfg(this->ike_sa);
243 if (this->dst_matched && this->src_matched &&
244 config->force_encap(config))
245 {
246 this->ike_sa->set_condition(this->ike_sa, COND_NAT_FAKE, TRUE);
247 }
248 }
249 }
250
251 /**
252 * Implementation of task_t.process for initiator
253 */
254 static status_t process_i(private_ike_natd_t *this, message_t *message)
255 {
256 process_payloads(this, message);
257
258 if (message->get_exchange_type(message) == IKE_SA_INIT)
259 {
260 peer_cfg_t *peer_cfg = this->ike_sa->get_peer_cfg(this->ike_sa);
261
262 if (this->ike_sa->has_condition(this->ike_sa, COND_NAT_ANY) ||
263 /* if peer supports NAT-T, we switch to port 4500 even
264 * if no NAT is detected. MOBIKE requires this. */
265 (peer_cfg->use_mobike(peer_cfg) &&
266 this->ike_sa->supports_extension(this->ike_sa, EXT_NATT)))
267 {
268 host_t *me, *other;
269
270 /* do not switch if we have a custom port from mobike/NAT */
271 me = this->ike_sa->get_my_host(this->ike_sa);
272 if (me->get_port(me) == IKEV2_UDP_PORT)
273 {
274 me->set_port(me, IKEV2_NATT_PORT);
275 }
276 other = this->ike_sa->get_other_host(this->ike_sa);
277 if (other->get_port(other) == IKEV2_UDP_PORT)
278 {
279 other->set_port(other, IKEV2_NATT_PORT);
280 }
281 }
282 }
283
284 return SUCCESS;
285 }
286
287 /**
288 * Implementation of task_t.process for initiator
289 */
290 static status_t build_i(private_ike_natd_t *this, message_t *message)
291 {
292 notify_payload_t *notify;
293 iterator_t *iterator;
294 host_t *host;
295
296 /* destination is always set */
297 host = message->get_destination(message);
298 notify = build_natd_payload(this, NAT_DETECTION_DESTINATION_IP, host);
299 message->add_payload(message, (payload_t*)notify);
300
301 /* source may be any, we have 3 possibilities to get our source address:
302 * 1. It is defined in the config => use the one of the IKE_SA
303 * 2. We do a routing lookup in the kernel interface
304 * 3. Include all possbile addresses
305 */
306 host = message->get_source(message);
307 if (!host->is_anyaddr(host))
308 { /* 1. */
309 notify = build_natd_payload(this, NAT_DETECTION_SOURCE_IP, host);
310 message->add_payload(message, (payload_t*)notify);
311 }
312 else
313 {
314 host = charon->kernel_interface->get_source_addr(
315 charon->kernel_interface,
316 this->ike_sa->get_other_host(this->ike_sa));
317 if (host)
318 { /* 2. */
319 host->set_port(host, IKEV2_UDP_PORT);
320 notify = build_natd_payload(this, NAT_DETECTION_SOURCE_IP, host);
321 message->add_payload(message, (payload_t*)notify);
322 host->destroy(host);
323 }
324 else
325 { /* 3. */
326 iterator = charon->kernel_interface->create_address_iterator(
327 charon->kernel_interface);
328 while (iterator->iterate(iterator, (void**)&host))
329 {
330 notify = build_natd_payload(this, NAT_DETECTION_SOURCE_IP, host);
331 message->add_payload(message, (payload_t*)notify);
332 }
333 iterator->destroy(iterator);
334 }
335 }
336 return NEED_MORE;
337 }
338
339 /**
340 * Implementation of task_t.build for responder
341 */
342 static status_t build_r(private_ike_natd_t *this, message_t *message)
343 {
344 notify_payload_t *notify;
345 host_t *me, *other;
346
347 /* only add notifies on successfull responses. */
348 if (message->get_exchange_type(message) == IKE_SA_INIT &&
349 message->get_payload(message, SECURITY_ASSOCIATION) == NULL)
350 {
351 return SUCCESS;
352 }
353
354 if (this->src_seen && this->dst_seen)
355 {
356 /* initiator seems to support NAT detection, add response */
357 me = message->get_source(message);
358 notify = build_natd_payload(this, NAT_DETECTION_SOURCE_IP, me);
359 message->add_payload(message, (payload_t*)notify);
360
361 other = message->get_destination(message);
362 notify = build_natd_payload(this, NAT_DETECTION_DESTINATION_IP, other);
363 message->add_payload(message, (payload_t*)notify);
364 }
365 return SUCCESS;
366 }
367
368 /**
369 * Implementation of task_t.process for responder
370 */
371 static status_t process_r(private_ike_natd_t *this, message_t *message)
372 {
373 process_payloads(this, message);
374
375 return NEED_MORE;
376 }
377
378 /**
379 * Implementation of task_t.get_type
380 */
381 static task_type_t get_type(private_ike_natd_t *this)
382 {
383 return IKE_NATD;
384 }
385
386 /**
387 * Implementation of task_t.migrate
388 */
389 static void migrate(private_ike_natd_t *this, ike_sa_t *ike_sa)
390 {
391 this->ike_sa = ike_sa;
392 this->src_seen = FALSE;
393 this->dst_seen = FALSE;
394 this->src_matched = FALSE;
395 this->dst_matched = FALSE;
396 }
397
398 /**
399 * Implementation of task_t.destroy
400 */
401 static void destroy(private_ike_natd_t *this)
402 {
403 this->hasher->destroy(this->hasher);
404 free(this);
405 }
406
407 /*
408 * Described in header.
409 */
410 ike_natd_t *ike_natd_create(ike_sa_t *ike_sa, bool initiator)
411 {
412 private_ike_natd_t *this = malloc_thing(private_ike_natd_t);
413
414 this->public.task.get_type = (task_type_t(*)(task_t*))get_type;
415 this->public.task.migrate = (void(*)(task_t*,ike_sa_t*))migrate;
416 this->public.task.destroy = (void(*)(task_t*))destroy;
417
418 if (initiator)
419 {
420 this->public.task.build = (status_t(*)(task_t*,message_t*))build_i;
421 this->public.task.process = (status_t(*)(task_t*,message_t*))process_i;
422 }
423 else
424 {
425 this->public.task.build = (status_t(*)(task_t*,message_t*))build_r;
426 this->public.task.process = (status_t(*)(task_t*,message_t*))process_r;
427 }
428
429 this->ike_sa = ike_sa;
430 this->initiator = initiator;
431 this->hasher = hasher_create(HASH_SHA1);
432 this->src_seen = FALSE;
433 this->dst_seen = FALSE;
434 this->src_matched = FALSE;
435 this->dst_matched = FALSE;
436
437 return &this->public;
438 }