applied new changes from NATT team
[strongswan.git] / src / charon / sa / states / ike_sa_established.c
1 /**
2 * @file ike_sa_established.c
3 *
4 * @brief Implementation of ike_sa_established_t.
5 *
6 */
7
8 /*
9 * Copyright (C) 2006 Tobias Brunner, Daniel Roethlisberger
10 * Copyright (C) 2005 Jan Hutter, Martin Willi
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 <string.h>
25
26 #include "ike_sa_established.h"
27
28 #include <daemon.h>
29 #include <encoding/payloads/delete_payload.h>
30 #include <encoding/payloads/sa_payload.h>
31 #include <encoding/payloads/ts_payload.h>
32 #include <encoding/payloads/nonce_payload.h>
33 #include <sa/child_sa.h>
34 #include <sa/states/delete_ike_sa_requested.h>
35 #include <queues/jobs/send_dpd_job.h>
36
37 typedef struct private_ike_sa_established_t private_ike_sa_established_t;
38
39 /**
40 * Private data of a ike_sa_established_t object.
41 */
42 struct private_ike_sa_established_t {
43 /**
44 * methods of the state_t interface
45 */
46 ike_sa_established_t public;
47
48 /**
49 * Assigned IKE_SA.
50 */
51 protected_ike_sa_t *ike_sa;
52
53 /**
54 * Nonce for a new child SA, chosen by initiator
55 */
56 chunk_t nonce_i;
57
58 /**
59 * Nonce for a new child SA, chosen by responder
60 */
61 chunk_t nonce_r;
62
63 /**
64 * Traffic selectors for a new child SA, responder side
65 */
66 linked_list_t *my_ts;
67
68 /**
69 * Traffic selectors for a new child SA, initiator side
70 */
71 linked_list_t *other_ts;
72
73 /**
74 * Newly set up child sa
75 */
76 child_sa_t *child_sa;
77
78 /**
79 * Old child sa, if we are rekeying
80 */
81 child_sa_t *old_child_sa;
82
83 /**
84 * Assigned logger. Use logger of IKE_SA.
85 */
86 logger_t *logger;
87 };
88
89 /**
90 * Schedule send dpd job
91 */
92 static void schedule_dpd_job(private_ike_sa_established_t *this)
93 {
94 u_int32_t interval = charon->configuration->get_dpd_interval(charon->configuration);
95
96 if (interval)
97 {
98 charon->event_queue->add_relative(charon->event_queue,
99 (job_t*)send_dpd_job_create(this->ike_sa->public.get_id(&this->ike_sa->public)),
100 interval);
101 }
102 }
103
104 /**
105 * Implementation of private_ike_sa_established_t.build_sa_payload.
106 */
107 static status_t build_sa_payload(private_ike_sa_established_t *this, sa_payload_t *request, message_t *response)
108 {
109 proposal_t *proposal, *proposal_tmp;
110 linked_list_t *proposal_list;
111 sa_payload_t *sa_response;
112 chunk_t seed;
113 prf_plus_t *prf_plus;
114 status_t status;
115 connection_t *connection;
116 policy_t *policy;
117 u_int32_t reqid = 0;
118 bool use_natt;
119
120 /* prepare reply */
121 sa_response = sa_payload_create();
122
123 /* get proposals from request, and select one with ours */
124 policy = this->ike_sa->get_policy(this->ike_sa);
125 proposal_list = request->get_proposals(request);
126 this->logger->log(this->logger, CONTROL|LEVEL1, "Selecting proposals:");
127 proposal = policy->select_proposal(policy, proposal_list);
128 /* list is not needed anymore */
129 while (proposal_list->remove_last(proposal_list, (void**)&proposal_tmp) == SUCCESS)
130 {
131 proposal_tmp->destroy(proposal_tmp);
132 }
133 proposal_list->destroy(proposal_list);
134 /* do we have a proposal? */
135 if (proposal == NULL)
136 {
137 notify_payload_t *notify;
138 this->logger->log(this->logger, AUDIT, "CREATE_CHILD_SA request did not contain any proposals we accept. "
139 "Adding NO_PROPOSAL_CHOSEN notify");
140 /* add NO_PROPOSAL_CHOSEN and an empty SA payload */
141 notify = notify_payload_create_from_protocol_and_type(PROTO_IKE, NO_PROPOSAL_CHOSEN);
142 response->add_payload(response, (payload_t*)notify);
143 }
144 else
145 {
146 /* set up child sa */
147 seed = chunk_alloc(this->nonce_i.len + this->nonce_r.len);
148 memcpy(seed.ptr, this->nonce_i.ptr, this->nonce_i.len);
149 memcpy(seed.ptr + this->nonce_i.len, this->nonce_r.ptr, this->nonce_r.len);
150 prf_plus = prf_plus_create(this->ike_sa->get_child_prf(this->ike_sa), seed);
151 this->logger->log_chunk(this->logger, RAW|LEVEL2, "Rekey seed", seed);
152 chunk_free(&seed);
153 chunk_free(&this->nonce_i);
154 chunk_free(&this->nonce_r);
155
156 policy = this->ike_sa->get_policy(this->ike_sa);
157 connection = this->ike_sa->get_connection(this->ike_sa);
158 if (this->old_child_sa)
159 { /* reuse old reqid if we are rekeying */
160 reqid = this->old_child_sa->get_reqid(this->old_child_sa);
161 }
162 use_natt = this->ike_sa->public.is_any_host_behind_nat(&this->ike_sa->public);
163 this->child_sa = child_sa_create(reqid,
164 connection->get_my_host(connection),
165 connection->get_other_host(connection),
166 policy->get_soft_lifetime(policy),
167 policy->get_hard_lifetime(policy),
168 use_natt);
169
170 status = this->child_sa->add(this->child_sa, proposal, prf_plus);
171 prf_plus->destroy(prf_plus);
172 if (status != SUCCESS)
173 {
174 this->logger->log(this->logger, AUDIT, "Could not install CHILD_SA!");
175 sa_response->destroy(sa_response);
176 proposal->destroy(proposal);
177 return DESTROY_ME;
178 }
179
180 /* add proposal to sa payload */
181 sa_response->add_proposal(sa_response, proposal);
182 proposal->destroy(proposal);
183 }
184 response->add_payload(response, (payload_t*)sa_response);
185 return SUCCESS;
186 }
187
188 /**
189 * Implementation of private_ike_sa_established_t.build_ts_payload.
190 */
191 static status_t build_ts_payload(private_ike_sa_established_t *this, bool ts_initiator, ts_payload_t *request, message_t* response)
192 {
193 linked_list_t *ts_received, *ts_selected;
194 traffic_selector_t *ts;
195 status_t status = SUCCESS;
196 ts_payload_t *ts_response;
197 policy_t *policy;
198
199 policy = this->ike_sa->get_policy(this->ike_sa);
200
201 /* build a reply payload with selected traffic selectors */
202 ts_received = request->get_traffic_selectors(request);
203 /* select ts depending on payload type */
204 if (ts_initiator)
205 {
206 ts_selected = policy->select_other_traffic_selectors(policy, ts_received);
207 this->other_ts = ts_selected;
208 }
209 else
210 {
211 ts_selected = policy->select_my_traffic_selectors(policy, ts_received);
212 this->my_ts = ts_selected;
213 }
214
215 ts_response = ts_payload_create_from_traffic_selectors(ts_initiator, ts_selected);
216 response->add_payload(response, (payload_t*)ts_response);
217
218 /* add notify if traffic selectors do not match */
219 if (!ts_initiator &&
220 (ts_selected->get_count(ts_selected) == 0 || this->other_ts->get_count(this->other_ts) == 0))
221 {
222 notify_payload_t *notify;
223
224 this->logger->log(this->logger, AUDIT, "IKE_AUTH request did not contain any traffic selectors we accept. "
225 "Adding TS_UNACCEPTABLE notify");
226
227 notify = notify_payload_create_from_protocol_and_type(0, TS_UNACCEPTABLE);
228 response->add_payload(response, (payload_t*)notify);
229 }
230
231 /* cleanup */
232 while (ts_received->remove_last(ts_received, (void**)&ts) == SUCCESS)
233 {
234 ts->destroy(ts);
235 }
236 ts_received->destroy(ts_received);
237
238 return status;
239 }
240
241 /**
242 * Implementation of private_ike_sa_established_t.build_nonce_payload.
243 */
244 static status_t build_nonce_payload(private_ike_sa_established_t *this, nonce_payload_t *nonce_request, message_t *response)
245 {
246 nonce_payload_t *nonce_payload;
247 randomizer_t *randomizer;
248 status_t status;
249
250 randomizer = this->ike_sa->get_randomizer(this->ike_sa);
251 status = randomizer->allocate_pseudo_random_bytes(randomizer, NONCE_SIZE, &this->nonce_r);
252 if (status != SUCCESS)
253 {
254 return status;
255 }
256 nonce_payload = nonce_payload_create();
257 nonce_payload->set_nonce(nonce_payload, this->nonce_r);
258
259 response->add_payload(response,(payload_t *) nonce_payload);
260
261 this->nonce_i = nonce_request->get_nonce(nonce_request);
262
263 return SUCCESS;
264 }
265
266 /**
267 * Process a CREATE_CHILD_SA request
268 */
269 static status_t process_create_child_sa(private_ike_sa_established_t *this, message_t *request, message_t *response)
270 {
271 ts_payload_t *tsi_request = NULL, *tsr_request = NULL;
272 sa_payload_t *sa_request = NULL;
273 nonce_payload_t *nonce_request = NULL;
274 notify_payload_t *notify = NULL;
275 iterator_t *payloads;
276 status_t status;
277
278 /* iterate over incoming payloads. Message is verified, we can be sure there are the required payloads */
279 payloads = request->get_payload_iterator(request);
280 while (payloads->has_next(payloads))
281 {
282 payload_t *payload;
283 payloads->current(payloads, (void**)&payload);
284
285 switch (payload->get_type(payload))
286 {
287 case SECURITY_ASSOCIATION:
288 {
289 sa_request = (sa_payload_t*)payload;
290 break;
291 }
292 case TRAFFIC_SELECTOR_INITIATOR:
293 {
294 tsi_request = (ts_payload_t*)payload;
295 break;
296 }
297 case TRAFFIC_SELECTOR_RESPONDER:
298 {
299 tsr_request = (ts_payload_t*)payload;
300 break;
301 }
302 case NONCE:
303 {
304 nonce_request = (nonce_payload_t*)payload;
305 break;
306 }
307 case KEY_EXCHANGE:
308 {
309 /* we currently do not support a diffie hellman exchange
310 * for CHILD_SAs. */
311 u_int16_t no_group[1];
312 no_group[0] = htons(MODP_NONE);
313 chunk_t no_group_chunk = chunk_from_buf((u_int8_t*)no_group);
314 this->ike_sa->send_notify(this->ike_sa, CREATE_CHILD_SA, INVALID_KE_PAYLOAD, no_group_chunk);
315 payloads->destroy(payloads);
316 return FAILED;
317 }
318 case NOTIFY:
319 {
320 notify = (notify_payload_t*)payload;
321 break;
322 }
323 default:
324 {
325 this->logger->log(this->logger, ERROR|LEVEL1, "Ignoring payload %s (%d)",
326 mapping_find(payload_type_m, payload->get_type(payload)), payload->get_type(payload));
327 break;
328 }
329 }
330 }
331 /* iterator can be destroyed */
332 payloads->destroy(payloads);
333
334 /* check if we have all payloads */
335 if (!(sa_request && nonce_request && tsi_request && tsr_request))
336 {
337 this->logger->log(this->logger, AUDIT, "CREATE_CHILD_SA request did not contain all required payloads. Ignored");
338 return FAILED;
339 }
340
341 if (notify && notify->get_notify_message_type(notify) == REKEY_SA)
342 {
343 u_int32_t spi = notify->get_spi(notify);
344 this->old_child_sa = this->ike_sa->get_child_sa(this->ike_sa, spi);
345 this->logger->log(this->logger, CONTROL, "Rekeying CHILD_SA with SPI 0x%x", spi);
346 }
347 else
348 {
349 this->logger->log(this->logger, CONTROL, "Create new CHILD_SA");
350 }
351
352 /* build response */
353 this->ike_sa->build_message(this->ike_sa, CREATE_CHILD_SA, FALSE, &response);
354
355 /* add payloads to it */
356 status = build_nonce_payload(this, nonce_request, response);
357 if (status != SUCCESS)
358 {
359 response->destroy(response);
360 return status;
361 }
362 status = build_sa_payload(this, sa_request, response);
363 if (status != SUCCESS)
364 {
365 response->destroy(response);
366 return status;
367 }
368 status = build_ts_payload(this, TRUE, tsi_request, response);
369 if (status != SUCCESS)
370 {
371 response->destroy(response);
372 return status;
373 }
374 status = build_ts_payload(this, FALSE, tsr_request, response);
375 if (status != SUCCESS)
376 {
377 response->destroy(response);
378 return status;
379 }
380
381 status = this->ike_sa->send_response(this->ike_sa, response);
382 /* message can now be sent (must not be destroyed) */
383 if (status != SUCCESS)
384 {
385 this->logger->log(this->logger, AUDIT, "Unable to send CREATE_CHILD_SA reply. Ignored");
386 response->destroy(response);
387 return FAILED;
388 }
389
390 /* install child SA policies */
391 if (!this->child_sa)
392 {
393 this->logger->log(this->logger, ERROR, "Proposal negotiation failed, no CHILD_SA built");
394 }
395 else if (this->my_ts->get_count(this->my_ts) == 0 || this->other_ts->get_count(this->other_ts) == 0)
396 {
397 this->logger->log(this->logger, ERROR, "Traffic selector negotiation failed, no CHILD_SA built");
398 this->child_sa->destroy(this->child_sa);
399 this->child_sa = NULL;
400 }
401 else
402 {
403 status = this->child_sa->add_policies(this->child_sa, this->my_ts, this->other_ts);
404 if (status != SUCCESS)
405 {
406 this->logger->log(this->logger, AUDIT, "Could not install CHILD_SA policy!");
407 }
408 if (this->old_child_sa)
409 { /* mark old child sa as rekeyed */
410 this->old_child_sa->set_rekeyed(this->old_child_sa);
411 }
412 this->ike_sa->add_child_sa(this->ike_sa, this->child_sa);
413 }
414
415 return SUCCESS;
416 }
417
418
419 /**
420 * Process an informational request
421 */
422 static status_t process_informational(private_ike_sa_established_t *this, message_t *request, message_t *response)
423 {
424 delete_payload_t *delete_request = NULL;
425 iterator_t *payloads = request->get_payload_iterator(request);
426
427 if (!payloads->get_count(payloads))
428 {
429 this->logger->log(this->logger, CONTROL, "DPD request received.");
430 }
431
432 while (payloads->has_next(payloads))
433 {
434 payload_t *payload;
435 payloads->current(payloads, (void**)&payload);
436
437 switch (payload->get_type(payload))
438 {
439 case DELETE:
440 {
441 delete_request = (delete_payload_t *) payload;
442 break;
443 }
444 default:
445 {
446 this->logger->log(this->logger, ERROR|LEVEL1, "Ignoring Payload %s (%d)",
447 mapping_find(payload_type_m, payload->get_type(payload)),
448 payload->get_type(payload));
449 break;
450 }
451 }
452 }
453 /* iterator can be destroyed */
454 payloads->destroy(payloads);
455
456 if (delete_request)
457 {
458 if (delete_request->get_protocol_id(delete_request) == PROTO_IKE)
459 {
460 this->logger->log(this->logger, CONTROL, "DELETE request for IKE_SA received");
461 /* switch to delete_ike_sa_requested. This is not absolutely correct, but we
462 * allow the clean destruction of an SA only in this state. */
463 this->ike_sa->set_new_state(this->ike_sa, (state_t*)delete_ike_sa_requested_create(this->ike_sa));
464 this->public.state_interface.destroy(&(this->public.state_interface));
465 this->ike_sa->send_response(this->ike_sa, response);
466 return DESTROY_ME;
467 }
468 else
469 {
470 iterator_t *iterator;
471 delete_payload_t *delete_response = delete_payload_create(delete_request->get_protocol_id(delete_request));
472 iterator = delete_request->create_spi_iterator(delete_request);
473 while (iterator->has_next(iterator))
474 {
475 u_int32_t spi;
476 iterator->current(iterator, (void**)&spi);
477 this->logger->log(this->logger, CONTROL, "DELETE request for CHILD_SA with SPI 0x%x received", spi);
478 spi = this->ike_sa->destroy_child_sa(this->ike_sa, spi);
479 if (spi)
480 {
481 delete_response->add_spi(delete_response, spi);
482 }
483 }
484 iterator->destroy(iterator);
485 response->add_payload(response, (payload_t*)delete_response);
486 }
487 }
488
489 if (this->ike_sa->send_response(this->ike_sa, response) != SUCCESS)
490 {
491 /* something is seriously wrong, kill connection */
492 this->logger->log(this->logger, AUDIT, "Unable to send reply. Deleting IKE_SA");
493 response->destroy(response);
494 return DESTROY_ME;
495 }
496 return SUCCESS;
497 }
498
499 /**
500 * Process an informational response
501 */
502 static status_t process_informational_response(private_ike_sa_established_t *this, message_t *message)
503 {
504 iterator_t *payloads = message->get_payload_iterator(message);
505
506 if (!payloads->get_count(payloads))
507 {
508 if (message->get_message_id(message)
509 != this->ike_sa->get_last_dpd_message_id(this->ike_sa))
510 {
511 this->logger->log(this->logger, ERROR|LEVEL1, "DPD response received that does not match our last sent dpd message.");
512 payloads->destroy(payloads);
513 return FAILED;
514 }
515
516 this->logger->log(this->logger, CONTROL, "DPD response received. Schedule job.");
517 schedule_dpd_job(this);
518
519 payloads->destroy(payloads);
520 return SUCCESS;
521 }
522
523 while (payloads->has_next(payloads))
524 {
525 payload_t *payload;
526 payloads->current(payloads, (void**)&payload);
527
528 switch (payload->get_type(payload))
529 {
530 default:
531 {
532 this->logger->log(this->logger, ERROR|LEVEL1, "Ignoring Payload %s (%d)",
533 mapping_find(payload_type_m, payload->get_type(payload)),
534 payload->get_type(payload));
535 break;
536 }
537 }
538 }
539 /* iterator can be destroyed */
540 payloads->destroy(payloads);
541
542 return SUCCESS;
543 }
544
545 /**
546 * Implements state_t.get_state
547 * Implements state_t.process_message
548 */
549 static status_t process_message(private_ike_sa_established_t *this, message_t *message)
550 {
551 ike_sa_id_t *ike_sa_id;
552 message_t *response;
553 crypter_t *crypter;
554 signer_t *signer;
555 status_t status;
556
557 /* get signer for verification and crypter for decryption */
558 ike_sa_id = this->ike_sa->public.get_id(&this->ike_sa->public);
559 if (!ike_sa_id->is_initiator(ike_sa_id))
560 {
561 crypter = this->ike_sa->get_crypter_initiator(this->ike_sa);
562 signer = this->ike_sa->get_signer_initiator(this->ike_sa);
563 }
564 else
565 {
566 crypter = this->ike_sa->get_crypter_responder(this->ike_sa);
567 signer = this->ike_sa->get_signer_responder(this->ike_sa);
568 }
569
570 /* parse incoming message */
571 status = message->parse_body(message, crypter, signer);
572 if (status != SUCCESS)
573 {
574 this->logger->log(this->logger, AUDIT, "%s request decryption failed. Ignoring message",
575 mapping_find(exchange_type_m, message->get_exchange_type(message)));
576 return status;
577 }
578
579 status = this->ike_sa->update_connection_hosts(this->ike_sa,
580 message->get_destination(message), message->get_source(message));
581 if (status != SUCCESS)
582 {
583 return status;
584 }
585
586 /* process responses */
587 if (!message->get_request(message))
588 {
589 switch (message->get_exchange_type(message))
590 {
591 case INFORMATIONAL:
592 status = process_informational_response(this, message);
593 break;
594 default:
595 this->logger->log(this->logger, ERROR | LEVEL1,
596 "Only INFORMATIONAL responses are handled in state ike_sa_established");
597 status = FAILED;
598 break;
599 }
600
601 /* we don't really reply to this message but the retransmit mechanism relies on this */
602 this->ike_sa->set_last_replied_message_id(this->ike_sa, message->get_message_id(message));
603
604 /* return here */
605 return status;
606 }
607
608 /* prepare a reply of the same type */
609 this->ike_sa->build_message(this->ike_sa, message->get_exchange_type(message), FALSE, &response);
610
611 /* handle the different message types in their functions */
612 switch (message->get_exchange_type(message))
613 {
614 case INFORMATIONAL:
615 status = process_informational(this, message, response);
616 break;
617 case CREATE_CHILD_SA:
618 status = process_create_child_sa(this, message, response);
619 break;
620 default:
621 this->logger->log(this->logger, ERROR | LEVEL1,
622 "Message of type %s not supported in state ike_sa_established",
623 mapping_find(exchange_type_m, message->get_exchange_type(message)));
624 status = NOT_SUPPORTED;
625 }
626
627 return status;
628 }
629
630 /**
631 * Implementation of state_t.get_state.
632 */
633 static ike_sa_state_t get_state(private_ike_sa_established_t *this)
634 {
635 return IKE_SA_ESTABLISHED;
636 }
637
638 /**
639 * Implementation of state_t.get_state
640 */
641 static void destroy(private_ike_sa_established_t *this)
642 {
643 chunk_free(&this->nonce_i);
644 chunk_free(&this->nonce_r);
645 free(this);
646 }
647
648 /*
649 * Described in header.
650 */
651 ike_sa_established_t *ike_sa_established_create(protected_ike_sa_t *ike_sa)
652 {
653 private_ike_sa_established_t *this = malloc_thing(private_ike_sa_established_t);
654
655 /* interface functions */
656 this->public.state_interface.process_message = (status_t (*) (state_t *,message_t *)) process_message;
657 this->public.state_interface.get_state = (ike_sa_state_t (*) (state_t *)) get_state;
658 this->public.state_interface.destroy = (void (*) (state_t *)) destroy;
659
660 /* private data */
661 this->ike_sa = ike_sa;
662 this->logger = logger_manager->get_logger(logger_manager, IKE_SA);
663 this->nonce_i = CHUNK_INITIALIZER;
664 this->nonce_r = CHUNK_INITIALIZER;
665 this->old_child_sa = NULL;
666
667 /* schedule initial dpd job */
668 schedule_dpd_job(this);
669
670 return &(this->public);
671 }