3c530d218ec38234f2f378c88e474788bcf5cf27
[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 me = this->ike_sa->get_my_host(this->ike_sa);
236 me->set_port(me, IKEV2_NATT_PORT);
237 other = this->ike_sa->get_other_host(this->ike_sa);
238 other->set_port(other, IKEV2_NATT_PORT);
239 }
240 }
241
242 return SUCCESS;
243 }
244
245 /**
246 * Implementation of task_t.process for initiator
247 */
248 static status_t build_i(private_ike_natd_t *this, message_t *message)
249 {
250 notify_payload_t *notify;
251 iterator_t *iterator;
252 host_t *host;
253
254 /* destination is always set */
255 host = message->get_destination(message);
256 notify = build_natd_payload(this, NAT_DETECTION_DESTINATION_IP, host);
257 message->add_payload(message, (payload_t*)notify);
258
259 /* source may be any, we have 3 possibilities to get our source address:
260 * 1. It is defined in the config => use the one of the IKE_SA
261 * 2. We do a routing lookup in the kernel interface
262 * 3. Include all possbile addresses
263 */
264 host = message->get_source(message);
265 if (!host->is_anyaddr(host))
266 { /* 1. */
267 notify = build_natd_payload(this, NAT_DETECTION_SOURCE_IP, host);
268 message->add_payload(message, (payload_t*)notify);
269 }
270 else
271 {
272 host = charon->kernel_interface->get_source_addr(
273 charon->kernel_interface,
274 this->ike_sa->get_other_host(this->ike_sa));
275 if (host)
276 { /* 2. */
277 host->set_port(host, IKEV2_UDP_PORT);
278 notify = build_natd_payload(this, NAT_DETECTION_SOURCE_IP, host);
279 message->add_payload(message, (payload_t*)notify);
280 host->destroy(host);
281 }
282 else
283 { /* 3. */
284 iterator = charon->kernel_interface->create_address_iterator(
285 charon->kernel_interface);
286 while (iterator->iterate(iterator, (void**)&host))
287 {
288 notify = build_natd_payload(this, NAT_DETECTION_SOURCE_IP, host);
289 message->add_payload(message, (payload_t*)notify);
290 }
291 iterator->destroy(iterator);
292 }
293 }
294 return NEED_MORE;
295 }
296
297 /**
298 * Implementation of task_t.build for responder
299 */
300 static status_t build_r(private_ike_natd_t *this, message_t *message)
301 {
302 notify_payload_t *notify;
303 host_t *me, *other;
304
305 /* only add notifies on successfull responses. */
306 if (message->get_exchange_type(message) == IKE_SA_INIT &&
307 message->get_payload(message, SECURITY_ASSOCIATION) == NULL)
308 {
309 return SUCCESS;
310 }
311
312 if (this->src_seen && this->dst_seen)
313 {
314 /* initiator seems to support NAT detection, add response */
315 me = message->get_source(message);
316 notify = build_natd_payload(this, NAT_DETECTION_SOURCE_IP, me);
317 message->add_payload(message, (payload_t*)notify);
318
319 other = message->get_destination(message);
320 notify = build_natd_payload(this, NAT_DETECTION_DESTINATION_IP, other);
321 message->add_payload(message, (payload_t*)notify);
322 }
323 return SUCCESS;
324 }
325
326 /**
327 * Implementation of task_t.process for responder
328 */
329 static status_t process_r(private_ike_natd_t *this, message_t *message)
330 {
331 process_payloads(this, message);
332
333 return NEED_MORE;
334 }
335
336 /**
337 * Implementation of task_t.get_type
338 */
339 static task_type_t get_type(private_ike_natd_t *this)
340 {
341 return IKE_NATD;
342 }
343
344 /**
345 * Implementation of task_t.migrate
346 */
347 static void migrate(private_ike_natd_t *this, ike_sa_t *ike_sa)
348 {
349 this->ike_sa = ike_sa;
350 this->src_seen = FALSE;
351 this->dst_seen = FALSE;
352 this->src_matched = FALSE;
353 this->dst_matched = FALSE;
354 }
355
356 /**
357 * Implementation of task_t.destroy
358 */
359 static void destroy(private_ike_natd_t *this)
360 {
361 this->hasher->destroy(this->hasher);
362 free(this);
363 }
364
365 /*
366 * Described in header.
367 */
368 ike_natd_t *ike_natd_create(ike_sa_t *ike_sa, bool initiator)
369 {
370 private_ike_natd_t *this = malloc_thing(private_ike_natd_t);
371
372 this->public.task.get_type = (task_type_t(*)(task_t*))get_type;
373 this->public.task.migrate = (void(*)(task_t*,ike_sa_t*))migrate;
374 this->public.task.destroy = (void(*)(task_t*))destroy;
375
376 if (initiator)
377 {
378 this->public.task.build = (status_t(*)(task_t*,message_t*))build_i;
379 this->public.task.process = (status_t(*)(task_t*,message_t*))process_i;
380 }
381 else
382 {
383 this->public.task.build = (status_t(*)(task_t*,message_t*))build_r;
384 this->public.task.process = (status_t(*)(task_t*,message_t*))process_r;
385 }
386
387 this->ike_sa = ike_sa;
388 this->initiator = initiator;
389 this->hasher = hasher_create(HASH_SHA1);
390 this->src_seen = FALSE;
391 this->dst_seen = FALSE;
392 this->src_matched = FALSE;
393 this->dst_matched = FALSE;
394
395 return &this->public;
396 }