only switch to port 4500 if we are on 500: fixed reauthentication in NAT
[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 NAT detection notify payload.
118 */
119 static notify_payload_t *build_natd_payload(private_ike_natd_t *this,
120 notify_type_t type, host_t *host)
121 {
122 chunk_t hash;
123 notify_payload_t *notify;
124 ike_sa_id_t *ike_sa_id;
125
126 ike_sa_id = this->ike_sa->get_id(this->ike_sa);
127 notify = notify_payload_create();
128 notify->set_notify_type(notify, type);
129 hash = generate_natd_hash(this, ike_sa_id, host);
130 notify->set_notification_data(notify, hash);
131 chunk_free(&hash);
132
133 return notify;
134 }
135
136 /**
137 * read notifys from message and evaluate them
138 */
139 static void process_payloads(private_ike_natd_t *this, message_t *message)
140 {
141 iterator_t *iterator;
142 payload_t *payload;
143 notify_payload_t *notify;
144 chunk_t hash, src_hash, dst_hash;
145 ike_sa_id_t *ike_sa_id;
146 host_t *me, *other;
147
148 /* Precompute NAT-D hashes for incoming NAT notify comparison */
149 ike_sa_id = message->get_ike_sa_id(message);
150 me = message->get_destination(message);
151 other = message->get_source(message);
152 dst_hash = generate_natd_hash(this, ike_sa_id, me);
153 src_hash = generate_natd_hash(this, ike_sa_id, other);
154
155 DBG3(DBG_IKE, "precalculated src_hash %B", &src_hash);
156 DBG3(DBG_IKE, "precalculated dst_hash %B", &dst_hash);
157
158 iterator = message->get_payload_iterator(message);
159 while (iterator->iterate(iterator, (void**)&payload))
160 {
161 if (payload->get_type(payload) != NOTIFY)
162 {
163 continue;
164 }
165 notify = (notify_payload_t*)payload;
166 switch (notify->get_notify_type(notify))
167 {
168 case NAT_DETECTION_DESTINATION_IP:
169 {
170 this->dst_seen = TRUE;
171 if (!this->dst_matched)
172 {
173 hash = notify->get_notification_data(notify);
174 DBG3(DBG_IKE, "received dst_hash %B", &hash);
175 if (chunk_equals(hash, dst_hash))
176 {
177 this->dst_matched = TRUE;
178 }
179 }
180 break;
181 }
182 case NAT_DETECTION_SOURCE_IP:
183 {
184 this->src_seen = TRUE;
185 if (!this->src_matched)
186 {
187 hash = notify->get_notification_data(notify);
188 DBG3(DBG_IKE, "received src_hash %B", &hash);
189 if (chunk_equals(hash, src_hash))
190 {
191 this->src_matched = TRUE;
192 }
193 }
194 break;
195 }
196 default:
197 break;
198 }
199 }
200 iterator->destroy(iterator);
201
202 chunk_free(&src_hash);
203 chunk_free(&dst_hash);
204
205 if (this->src_seen && this->dst_seen)
206 {
207 this->ike_sa->enable_extension(this->ike_sa, EXT_NATT);
208
209 this->ike_sa->set_condition(this->ike_sa, COND_NAT_HERE,
210 !this->dst_matched);
211 this->ike_sa->set_condition(this->ike_sa, COND_NAT_THERE,
212 !this->src_matched);
213 }
214 }
215
216 /**
217 * Implementation of task_t.process for initiator
218 */
219 static status_t process_i(private_ike_natd_t *this, message_t *message)
220 {
221 process_payloads(this, message);
222
223 if (message->get_exchange_type(message) == IKE_SA_INIT)
224 {
225 peer_cfg_t *peer_cfg = this->ike_sa->get_peer_cfg(this->ike_sa);
226
227 if (this->ike_sa->has_condition(this->ike_sa, COND_NAT_ANY) ||
228 /* if peer supports NAT-T, we switch to port 4500 even
229 * if no NAT is detected. MOBIKE requires this. */
230 (peer_cfg->use_mobike(peer_cfg) &&
231 this->ike_sa->supports_extension(this->ike_sa, EXT_NATT)))
232 {
233 host_t *me, *other;
234
235 /* do not switch if we have a custom port from mobike/NAT */
236 me = this->ike_sa->get_my_host(this->ike_sa);
237 if (me->get_port(me) == IKEV2_UDP_PORT)
238 {
239 me->set_port(me, IKEV2_NATT_PORT);
240 }
241 other = this->ike_sa->get_other_host(this->ike_sa);
242 if (other->get_port(other) == IKEV2_UDP_PORT)
243 {
244 other->set_port(other, IKEV2_NATT_PORT);
245 }
246 }
247 }
248
249 return SUCCESS;
250 }
251
252 /**
253 * Implementation of task_t.process for initiator
254 */
255 static status_t build_i(private_ike_natd_t *this, message_t *message)
256 {
257 notify_payload_t *notify;
258 iterator_t *iterator;
259 host_t *host;
260
261 /* destination is always set */
262 host = message->get_destination(message);
263 notify = build_natd_payload(this, NAT_DETECTION_DESTINATION_IP, host);
264 message->add_payload(message, (payload_t*)notify);
265
266 /* source may be any, we have 3 possibilities to get our source address:
267 * 1. It is defined in the config => use the one of the IKE_SA
268 * 2. We do a routing lookup in the kernel interface
269 * 3. Include all possbile addresses
270 */
271 host = message->get_source(message);
272 if (!host->is_anyaddr(host))
273 { /* 1. */
274 notify = build_natd_payload(this, NAT_DETECTION_SOURCE_IP, host);
275 message->add_payload(message, (payload_t*)notify);
276 }
277 else
278 {
279 host = charon->kernel_interface->get_source_addr(
280 charon->kernel_interface,
281 this->ike_sa->get_other_host(this->ike_sa));
282 if (host)
283 { /* 2. */
284 host->set_port(host, IKEV2_UDP_PORT);
285 notify = build_natd_payload(this, NAT_DETECTION_SOURCE_IP, host);
286 message->add_payload(message, (payload_t*)notify);
287 host->destroy(host);
288 }
289 else
290 { /* 3. */
291 iterator = charon->kernel_interface->create_address_iterator(
292 charon->kernel_interface);
293 while (iterator->iterate(iterator, (void**)&host))
294 {
295 notify = build_natd_payload(this, NAT_DETECTION_SOURCE_IP, host);
296 message->add_payload(message, (payload_t*)notify);
297 }
298 iterator->destroy(iterator);
299 }
300 }
301 return NEED_MORE;
302 }
303
304 /**
305 * Implementation of task_t.build for responder
306 */
307 static status_t build_r(private_ike_natd_t *this, message_t *message)
308 {
309 notify_payload_t *notify;
310 host_t *me, *other;
311
312 /* only add notifies on successfull responses. */
313 if (message->get_exchange_type(message) == IKE_SA_INIT &&
314 message->get_payload(message, SECURITY_ASSOCIATION) == NULL)
315 {
316 return SUCCESS;
317 }
318
319 if (this->src_seen && this->dst_seen)
320 {
321 /* initiator seems to support NAT detection, add response */
322 me = message->get_source(message);
323 notify = build_natd_payload(this, NAT_DETECTION_SOURCE_IP, me);
324 message->add_payload(message, (payload_t*)notify);
325
326 other = message->get_destination(message);
327 notify = build_natd_payload(this, NAT_DETECTION_DESTINATION_IP, other);
328 message->add_payload(message, (payload_t*)notify);
329 }
330 return SUCCESS;
331 }
332
333 /**
334 * Implementation of task_t.process for responder
335 */
336 static status_t process_r(private_ike_natd_t *this, message_t *message)
337 {
338 process_payloads(this, message);
339
340 return NEED_MORE;
341 }
342
343 /**
344 * Implementation of task_t.get_type
345 */
346 static task_type_t get_type(private_ike_natd_t *this)
347 {
348 return IKE_NATD;
349 }
350
351 /**
352 * Implementation of task_t.migrate
353 */
354 static void migrate(private_ike_natd_t *this, ike_sa_t *ike_sa)
355 {
356 this->ike_sa = ike_sa;
357 this->src_seen = FALSE;
358 this->dst_seen = FALSE;
359 this->src_matched = FALSE;
360 this->dst_matched = FALSE;
361 }
362
363 /**
364 * Implementation of task_t.destroy
365 */
366 static void destroy(private_ike_natd_t *this)
367 {
368 this->hasher->destroy(this->hasher);
369 free(this);
370 }
371
372 /*
373 * Described in header.
374 */
375 ike_natd_t *ike_natd_create(ike_sa_t *ike_sa, bool initiator)
376 {
377 private_ike_natd_t *this = malloc_thing(private_ike_natd_t);
378
379 this->public.task.get_type = (task_type_t(*)(task_t*))get_type;
380 this->public.task.migrate = (void(*)(task_t*,ike_sa_t*))migrate;
381 this->public.task.destroy = (void(*)(task_t*))destroy;
382
383 if (initiator)
384 {
385 this->public.task.build = (status_t(*)(task_t*,message_t*))build_i;
386 this->public.task.process = (status_t(*)(task_t*,message_t*))process_i;
387 }
388 else
389 {
390 this->public.task.build = (status_t(*)(task_t*,message_t*))build_r;
391 this->public.task.process = (status_t(*)(task_t*,message_t*))process_r;
392 }
393
394 this->ike_sa = ike_sa;
395 this->initiator = initiator;
396 this->hasher = hasher_create(HASH_SHA1);
397 this->src_seen = FALSE;
398 this->dst_seen = FALSE;
399 this->src_matched = FALSE;
400 this->dst_matched = FALSE;
401
402 return &this->public;
403 }