182da8651e293bdac98decda1488c7efbb9b7198
[strongswan.git] / src / charon / sa / child_sa.c
1 /**
2 * @file child_sa.c
3 *
4 * @brief Implementation of child_sa_t.
5 *
6 */
7
8 /*
9 * Copyright (C) 2006 Tobias Brunner, Daniel Roethlisberger
10 * Copyright (C) 2005-2006 Martin Willi
11 * Copyright (C) 2005 Jan Hutter
12 * Hochschule fuer Technik Rapperswil
13 *
14 * This program is free software; you can redistribute it and/or modify it
15 * under the terms of the GNU General Public License as published by the
16 * Free Software Foundation; either version 2 of the License, or (at your
17 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
18 *
19 * This program is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
21 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 * for more details.
23 */
24
25 #define _GNU_SOURCE
26 #include "child_sa.h"
27
28 #include <stdio.h>
29 #include <string.h>
30 #include <printf.h>
31
32 #include <daemon.h>
33
34 ENUM(child_sa_state_names, CHILD_CREATED, CHILD_DELETING,
35 "CREATED",
36 "ROUTED",
37 "INSTALLED",
38 "REKEYING",
39 "DELETING",
40 );
41
42 typedef struct sa_policy_t sa_policy_t;
43
44 /**
45 * Struct used to store information for a policy. This
46 * is needed since we must provide all this information
47 * for deleting a policy...
48 */
49 struct sa_policy_t {
50 /**
51 * Traffic selector for us
52 */
53 traffic_selector_t *my_ts;
54
55 /**
56 * Traffic selector for other
57 */
58 traffic_selector_t *other_ts;
59 };
60
61 typedef struct private_child_sa_t private_child_sa_t;
62
63 /**
64 * Private data of a child_sa_t \ 1bject.
65 */
66 struct private_child_sa_t {
67 /**
68 * Public interface of child_sa_t.
69 */
70 child_sa_t public;
71
72 /**
73 * Name of the policy used by this CHILD_SA
74 */
75 char *name;
76
77 struct {
78 /** address of peer */
79 host_t *addr;
80 /** id of peer */
81 identification_t *id;
82 /** actual used SPI, 0 if unused */
83 u_int32_t spi;
84 } me, other;
85
86 /**
87 * Allocated SPI for a ESP proposal candidates
88 */
89 u_int32_t alloc_esp_spi;
90
91 /**
92 * Allocated SPI for a AH proposal candidates
93 */
94 u_int32_t alloc_ah_spi;
95
96 /**
97 * Protocol used to protect this SA, ESP|AH
98 */
99 protocol_id_t protocol;
100
101 /**
102 * List containing sa_policy_t objects
103 */
104 linked_list_t *policies;
105
106 /**
107 * Seperate list for local traffic selectors
108 */
109 linked_list_t *my_ts;
110
111 /**
112 * Seperate list for remote traffic selectors
113 */
114 linked_list_t *other_ts;
115
116 /**
117 * reqid used for this child_sa
118 */
119 u_int32_t reqid;
120
121 /**
122 * encryption algorithm used for this SA
123 */
124 algorithm_t encryption;
125
126 /**
127 * integrity protection algorithm used for this SA
128 */
129 algorithm_t integrity;
130
131 /**
132 * time, on which SA was installed
133 */
134 time_t install_time;
135
136 /**
137 * Lifetime before rekeying
138 */
139 u_int32_t soft_lifetime;
140
141 /**
142 * Lifetime before delete
143 */
144 u_int32_t hard_lifetime;
145
146 /**
147 * state of the CHILD_SA
148 */
149 child_sa_state_t state;
150
151 /**
152 * transaction which is rekeying this CHILD_SA
153 */
154 transaction_t *rekeying_transaction;
155
156 /**
157 * Updown script
158 */
159 char *script;
160
161 /**
162 * Allow host access
163 */
164 bool hostaccess;
165
166 /**
167 * Specifies if NAT traversal is used
168 */
169 bool use_natt;
170
171 /**
172 * mode this SA uses, tunnel/transport
173 */
174 mode_t mode;
175 };
176
177 /**
178 * Implementation of child_sa_t.get_name.
179 */
180 static char *get_name(private_child_sa_t *this)
181 {
182 return this->name;
183 }
184
185 /**
186 * Implementation of child_sa_t.set_name.
187 */
188 static void set_name(private_child_sa_t *this, char* name)
189 {
190 char buffer[64];
191
192 if (snprintf(buffer, sizeof(buffer), "%s[%d]",
193 name, this->reqid - REQID_START) > 0)
194 {
195 free(this->name);
196 this->name = strdup(buffer);
197 }
198 }
199
200 /**
201 * Implements child_sa_t.get_reqid
202 */
203 static u_int32_t get_reqid(private_child_sa_t *this)
204 {
205 return this->reqid;
206 }
207
208 /**
209 * Implements child_sa_t.get_spi
210 */
211 u_int32_t get_spi(private_child_sa_t *this, bool inbound)
212 {
213 if (inbound)
214 {
215 return this->me.spi;
216 }
217 return this->other.spi;
218 }
219
220 /**
221 * Implements child_sa_t.get_protocol
222 */
223 protocol_id_t get_protocol(private_child_sa_t *this)
224 {
225 return this->protocol;
226 }
227
228 /**
229 * Implements child_sa_t.get_state
230 */
231 static child_sa_state_t get_state(private_child_sa_t *this)
232 {
233 return this->state;
234 }
235
236 /**
237 * Run the up/down script
238 */
239 static void updown(private_child_sa_t *this, bool up)
240 {
241 sa_policy_t *policy;
242 iterator_t *iterator;
243
244 if (this->script == NULL)
245 {
246 return;
247 }
248
249 iterator = this->policies->create_iterator(this->policies, TRUE);
250 while (iterator->iterate(iterator, (void**)&policy))
251 {
252 char command[1024];
253 char *ifname = NULL;
254 char *my_client, *other_client, *my_client_mask, *other_client_mask;
255 char *pos;
256 FILE *shell;
257
258 /* get subnet/bits from string */
259 asprintf(&my_client, "%R", policy->my_ts);
260 pos = strchr(my_client, '/');
261 *pos = '\0';
262 my_client_mask = pos + 1;
263 pos = strchr(my_client_mask, '[');
264 if (pos)
265 {
266 *pos = '\0';
267 }
268 asprintf(&other_client, "%R", policy->other_ts);
269 pos = strchr(other_client, '/');
270 *pos = '\0';
271 other_client_mask = pos + 1;
272 pos = strchr(other_client_mask, '[');
273 if (pos)
274 {
275 *pos = '\0';
276 }
277
278 charon->socket->is_local_address(charon->socket, this->me.addr, &ifname);
279
280 /* build the command with all env variables.
281 * TODO: PLUTO_MY_SRCIP, PLUTO_PEER_CA and PLUTO_NEXT_HOP
282 * are currently missing */
283 snprintf(command, sizeof(command),
284 "2>&1 "
285 "PLUTO_VERSION='1.1' "
286 "PLUTO_VERB='%s%s%s' "
287 "PLUTO_CONNECTION='%s' "
288 "PLUTO_INTERFACE='%s' "
289 "PLUTO_REQID='%u' "
290 "PLUTO_ME='%H' "
291 "PLUTO_MY_ID='%D' "
292 "PLUTO_MY_CLIENT='%s/%s' "
293 "PLUTO_MY_CLIENT_NET='%s' "
294 "PLUTO_MY_CLIENT_MASK='%s' "
295 "PLUTO_MY_PORT='%u' "
296 "PLUTO_MY_PROTOCOL='%u' "
297 "PLUTO_PEER='%H' "
298 "PLUTO_PEER_ID='%D' "
299 "PLUTO_PEER_CLIENT='%s/%s' "
300 "PLUTO_PEER_CLIENT_NET='%s' "
301 "PLUTO_PEER_CLIENT_MASK='%s' "
302 "PLUTO_PEER_PORT='%u' "
303 "PLUTO_PEER_PROTOCOL='%u' "
304 "%s"
305 "%s",
306 up ? "up" : "down",
307 policy->my_ts->is_host(policy->my_ts,
308 this->me.addr) ? "-host" : "-client",
309 this->me.addr->get_family(this->me.addr) == AF_INET ? "" : "-ipv6",
310 this->name,
311 ifname,
312 this->reqid,
313 this->me.addr,
314 this->me.id,
315 my_client, my_client_mask,
316 my_client, my_client_mask,
317 policy->my_ts->get_from_port(policy->my_ts),
318 policy->my_ts->get_protocol(policy->my_ts),
319 this->other.addr,
320 this->other.id,
321 other_client, other_client_mask,
322 other_client, other_client_mask,
323 policy->other_ts->get_from_port(policy->other_ts),
324 policy->other_ts->get_protocol(policy->other_ts),
325 this->hostaccess? "PLUTO_HOST_ACCESS='1' " : "",
326 this->script);
327 free(ifname);
328 free(my_client);
329 free(other_client);
330
331 shell = popen(command, "r");
332
333 if (shell == NULL)
334 {
335 DBG1(DBG_CHD, "could not execute updown script '%s'", this->script);
336 return;
337 }
338
339 while (TRUE)
340 {
341 char resp[128];
342
343 if (fgets(resp, sizeof(resp), shell) == NULL)
344 {
345 if (ferror(shell))
346 {
347 DBG1(DBG_CHD, "error reading output from updown script");
348 return;
349 }
350 else
351 {
352 break;
353 }
354 }
355 else
356 {
357 char *e = resp + strlen(resp);
358 if (e > resp && e[-1] == '\n')
359 { /* trim trailing '\n' */
360 e[-1] = '\0';
361 }
362 DBG1(DBG_CHD, "updown: %s", resp);
363 }
364 }
365 pclose(shell);
366 }
367 iterator->destroy(iterator);
368 }
369
370 /**
371 * Implements child_sa_t.set_state
372 */
373 static void set_state(private_child_sa_t *this, child_sa_state_t state)
374 {
375 this->state = state;
376 if (state == CHILD_INSTALLED)
377 {
378 updown(this, TRUE);
379 }
380 }
381
382 /**
383 * Allocate SPI for a single proposal
384 */
385 static status_t alloc_proposal(private_child_sa_t *this, proposal_t *proposal)
386 {
387 protocol_id_t protocol = proposal->get_protocol(proposal);
388
389 if (protocol == PROTO_AH)
390 {
391 /* get a new spi for AH, if not already done */
392 if (this->alloc_ah_spi == 0)
393 {
394 if (charon->kernel_interface->get_spi(
395 charon->kernel_interface,
396 this->other.addr, this->me.addr,
397 PROTO_AH, this->reqid,
398 &this->alloc_ah_spi) != SUCCESS)
399 {
400 return FAILED;
401 }
402 }
403 proposal->set_spi(proposal, this->alloc_ah_spi);
404 }
405 if (protocol == PROTO_ESP)
406 {
407 /* get a new spi for ESP, if not already done */
408 if (this->alloc_esp_spi == 0)
409 {
410 if (charon->kernel_interface->get_spi(
411 charon->kernel_interface,
412 this->other.addr, this->me.addr,
413 PROTO_ESP, this->reqid,
414 &this->alloc_esp_spi) != SUCCESS)
415 {
416 return FAILED;
417 }
418 }
419 proposal->set_spi(proposal, this->alloc_esp_spi);
420 }
421 return SUCCESS;
422 }
423
424
425 /**
426 * Implements child_sa_t.alloc
427 */
428 static status_t alloc(private_child_sa_t *this, linked_list_t *proposals)
429 {
430 iterator_t *iterator;
431 proposal_t *proposal;
432
433 /* iterator through proposals to update spis */
434 iterator = proposals->create_iterator(proposals, TRUE);
435 while(iterator->iterate(iterator, (void**)&proposal))
436 {
437 if (alloc_proposal(this, proposal) != SUCCESS)
438 {
439 iterator->destroy(iterator);
440 return FAILED;
441 }
442 }
443 iterator->destroy(iterator);
444 return SUCCESS;
445 }
446
447 static status_t install(private_child_sa_t *this, proposal_t *proposal,
448 mode_t mode, prf_plus_t *prf_plus, bool mine)
449 {
450 u_int32_t spi;
451 algorithm_t *enc_algo, *int_algo;
452 algorithm_t enc_algo_none = {ENCR_UNDEFINED, 0};
453 algorithm_t int_algo_none = {AUTH_UNDEFINED, 0};
454 host_t *src;
455 host_t *dst;
456 natt_conf_t *natt;
457 status_t status;
458
459 this->protocol = proposal->get_protocol(proposal);
460
461 /* now we have to decide which spi to use. Use self allocated, if "mine",
462 * or the one in the proposal, if not "mine" (others). Additionally,
463 * source and dest host switch depending on the role */
464 if (mine)
465 {
466 /* if we have allocated SPIs for AH and ESP, we must delete the unused
467 * one. */
468 if (this->protocol == PROTO_ESP)
469 {
470 this->me.spi = this->alloc_esp_spi;
471 if (this->alloc_ah_spi)
472 {
473 charon->kernel_interface->del_sa(charon->kernel_interface, this->me.addr,
474 this->alloc_ah_spi, PROTO_AH);
475 }
476 }
477 else
478 {
479 this->me.spi = this->alloc_ah_spi;
480 if (this->alloc_esp_spi)
481 {
482 charon->kernel_interface->del_sa(charon->kernel_interface, this->me.addr,
483 this->alloc_esp_spi, PROTO_ESP);
484 }
485 }
486 spi = this->me.spi;
487 dst = this->me.addr;
488 src = this->other.addr;
489 }
490 else
491 {
492 this->other.spi = proposal->get_spi(proposal);
493 spi = this->other.spi;
494 src = this->me.addr;
495 dst = this->other.addr;
496 }
497
498 DBG2(DBG_CHD, "adding %s %N SA", mine ? "inbound" : "outbound",
499 protocol_id_names, this->protocol);
500
501 /* select encryption algo */
502 if (proposal->get_algorithm(proposal, ENCRYPTION_ALGORITHM, &enc_algo))
503 {
504 DBG2(DBG_CHD, " using %N for encryption",
505 encryption_algorithm_names, enc_algo->algorithm);
506 }
507 else
508 {
509 enc_algo = &enc_algo_none;
510 }
511
512 /* select integrity algo */
513 if (proposal->get_algorithm(proposal, INTEGRITY_ALGORITHM, &int_algo))
514 {
515 DBG2(DBG_CHD, " using %N for integrity",
516 integrity_algorithm_names, int_algo->algorithm);
517 }
518 else
519 {
520 int_algo = &int_algo_none;
521 }
522
523 /* setup nat-t */
524 if (this->use_natt)
525 {
526 natt = alloca(sizeof(natt_conf_t));
527 natt->sport = src->get_port(src);
528 natt->dport = dst->get_port(dst);
529 }
530 else
531 {
532 natt = NULL;
533 }
534
535
536 /* send SA down to the kernel */
537 DBG2(DBG_CHD, " SPI 0x%.8x, src %H dst %H", ntohl(spi), src, dst);
538 status = charon->kernel_interface->add_sa(charon->kernel_interface,
539 src, dst,
540 spi, this->protocol,
541 this->reqid,
542 mine ? this->soft_lifetime : 0,
543 this->hard_lifetime,
544 enc_algo, int_algo,
545 prf_plus, natt, mode, mine);
546
547 this->encryption = *enc_algo;
548 this->integrity = *int_algo;
549 this->install_time = time(NULL);
550
551 return status;
552 }
553
554 static status_t add(private_child_sa_t *this, proposal_t *proposal,
555 mode_t mode, prf_plus_t *prf_plus)
556 {
557 u_int32_t outbound_spi, inbound_spi;
558
559 /* backup outbound spi, as alloc overwrites it */
560 outbound_spi = proposal->get_spi(proposal);
561
562 /* get SPIs inbound SAs */
563 if (alloc_proposal(this, proposal) != SUCCESS)
564 {
565 return FAILED;
566 }
567 inbound_spi = proposal->get_spi(proposal);
568
569 /* install inbound SAs */
570 if (install(this, proposal, mode, prf_plus, TRUE) != SUCCESS)
571 {
572 return FAILED;
573 }
574
575 /* install outbound SAs, restore spi*/
576 proposal->set_spi(proposal, outbound_spi);
577 if (install(this, proposal, mode, prf_plus, FALSE) != SUCCESS)
578 {
579 return FAILED;
580 }
581 proposal->set_spi(proposal, inbound_spi);
582
583 return SUCCESS;
584 }
585
586 static status_t update(private_child_sa_t *this, proposal_t *proposal,
587 mode_t mode, prf_plus_t *prf_plus)
588 {
589 u_int32_t inbound_spi;
590
591 /* backup received spi, as install() overwrites it */
592 inbound_spi = proposal->get_spi(proposal);
593
594 /* install outbound SAs */
595 if (install(this, proposal, mode, prf_plus, FALSE) != SUCCESS)
596 {
597 return FAILED;
598 }
599
600 /* restore spi */
601 proposal->set_spi(proposal, inbound_spi);
602 /* install inbound SAs */
603 if (install(this, proposal, mode, prf_plus, TRUE) != SUCCESS)
604 {
605 return FAILED;
606 }
607
608 return SUCCESS;
609 }
610
611 static status_t add_policies(private_child_sa_t *this,
612 linked_list_t *my_ts_list,
613 linked_list_t *other_ts_list, mode_t mode)
614 {
615 iterator_t *my_iter, *other_iter;
616 traffic_selector_t *my_ts, *other_ts;
617 /* use low prio for ROUTED policies */
618 bool high_prio = (this->state != CHILD_CREATED);
619
620 /* iterate over both lists */
621 my_iter = my_ts_list->create_iterator(my_ts_list, TRUE);
622 other_iter = other_ts_list->create_iterator(other_ts_list, TRUE);
623 while (my_iter->iterate(my_iter, (void**)&my_ts))
624 {
625 other_iter->reset(other_iter);
626 while (other_iter->iterate(other_iter, (void**)&other_ts))
627 {
628 /* set up policies for every entry in my_ts_list to every entry in other_ts_list */
629 status_t status;
630 sa_policy_t *policy;
631
632 if (my_ts->get_type(my_ts) != other_ts->get_type(other_ts))
633 {
634 DBG2(DBG_CHD,
635 "CHILD_SA policy uses two different IP families, ignored");
636 continue;
637 }
638
639 /* only set up policies if protocol matches, or if one is zero (any) */
640 if (my_ts->get_protocol(my_ts) != other_ts->get_protocol(other_ts) &&
641 my_ts->get_protocol(my_ts) && other_ts->get_protocol(other_ts))
642 {
643 DBG2(DBG_CHD,
644 "CHILD_SA policy uses two different protocols, ignored");
645 continue;
646 }
647
648 /* install 3 policies: out, in and forward */
649 status = charon->kernel_interface->add_policy(charon->kernel_interface,
650 this->me.addr, this->other.addr, my_ts, other_ts, POLICY_OUT,
651 this->protocol, this->reqid, high_prio, mode, FALSE);
652
653 status |= charon->kernel_interface->add_policy(charon->kernel_interface,
654 this->other.addr, this->me.addr, other_ts, my_ts, POLICY_IN,
655 this->protocol, this->reqid, high_prio, mode, FALSE);
656
657 status |= charon->kernel_interface->add_policy(charon->kernel_interface,
658 this->other.addr, this->me.addr, other_ts, my_ts, POLICY_FWD,
659 this->protocol, this->reqid, high_prio, mode, FALSE);
660
661 if (status != SUCCESS)
662 {
663 my_iter->destroy(my_iter);
664 other_iter->destroy(other_iter);
665 return status;
666 }
667
668 /* store policy to delete/update them later */
669 policy = malloc_thing(sa_policy_t);
670 policy->my_ts = my_ts->clone(my_ts);
671 policy->other_ts = other_ts->clone(other_ts);
672 this->policies->insert_last(this->policies, (void*)policy);
673 /* add to separate list to query them via get_*_traffic_selectors() */
674 this->my_ts->insert_last(this->my_ts, (void*)policy->my_ts);
675 this->other_ts->insert_last(this->other_ts, (void*)policy->other_ts);
676 }
677 }
678 my_iter->destroy(my_iter);
679 other_iter->destroy(other_iter);
680
681 /* switch to routed state if no SAD entry set up */
682 if (this->state == CHILD_CREATED)
683 {
684 this->state = CHILD_ROUTED;
685 }
686 /* needed to update hosts */
687 this->mode = mode;
688 return SUCCESS;
689 }
690
691 /**
692 * Implementation of child_sa_t.get_my_traffic_selectors.
693 */
694 static linked_list_t *get_my_traffic_selectors(private_child_sa_t *this)
695 {
696 return this->my_ts;
697 }
698
699 /**
700 * Implementation of child_sa_t.get_my_traffic_selectors.
701 */
702 static linked_list_t *get_other_traffic_selectors(private_child_sa_t *this)
703 {
704 return this->other_ts;
705 }
706
707 /**
708 * Implementation of child_sa_t.set_rekeying_transaction.
709 */
710 static void set_rekeying_transaction(private_child_sa_t *this, transaction_t *transaction)
711 {
712 this->rekeying_transaction = transaction;
713 }
714
715 /**
716 * Implementation of child_sa_t.get_rekeying_transaction.
717 */
718 static transaction_t* get_rekeying_transaction(private_child_sa_t *this)
719 {
720 return this->rekeying_transaction;
721 }
722
723 /**
724 * Implementation of child_sa_t.get_use_time
725 */
726 static status_t get_use_time(private_child_sa_t *this, bool inbound, time_t *use_time)
727 {
728 iterator_t *iterator;
729 sa_policy_t *policy;
730 status_t status = FAILED;
731
732 *use_time = UNDEFINED_TIME;
733
734 iterator = this->policies->create_iterator(this->policies, TRUE);
735 while (iterator->iterate(iterator, (void**)&policy))
736 {
737 if (inbound)
738 {
739 time_t in = UNDEFINED_TIME, fwd = UNDEFINED_TIME;
740
741 status = charon->kernel_interface->query_policy(
742 charon->kernel_interface,
743 policy->other_ts, policy->my_ts,
744 POLICY_IN, (u_int32_t*)&in);
745 status |= charon->kernel_interface->query_policy(
746 charon->kernel_interface,
747 policy->other_ts, policy->my_ts,
748 POLICY_FWD, (u_int32_t*)&fwd);
749 *use_time = max(in, fwd);
750 }
751 else
752 {
753 status = charon->kernel_interface->query_policy(
754 charon->kernel_interface,
755 policy->my_ts, policy->other_ts,
756 POLICY_OUT, (u_int32_t*)use_time);
757 }
758 }
759 iterator->destroy(iterator);
760 return status;
761 }
762
763 /**
764 * output handler in printf()
765 */
766 static int print(FILE *stream, const struct printf_info *info,
767 const void *const *args)
768 {
769 private_child_sa_t *this = *((private_child_sa_t**)(args[0]));
770 iterator_t *iterator;
771 sa_policy_t *policy;
772 u_int32_t now, rekeying, use;
773 status_t status;
774 size_t written = 0;
775
776 if (this == NULL)
777 {
778 return fprintf(stream, "(null)");
779 }
780
781 now = (u_int32_t)time(NULL);
782
783 written += fprintf(stream, "%12s: %N, reqid: %d, %N", this->name,
784 child_sa_state_names, this->state, this->reqid,
785 mode_names, this->mode);
786
787 if (this->state == CHILD_INSTALLED)
788 {
789 written += fprintf(stream, ", %N SPIs: 0x%0x_i 0x%0x_o",
790 protocol_id_names, this->protocol,
791 htonl(this->me.spi), htonl(this->other.spi));
792
793 if (info->alt)
794 {
795 written += fprintf(stream, "\n%12s: ", this->name);
796
797 if (this->protocol == PROTO_ESP)
798 {
799 written += fprintf(stream, "%N", encryption_algorithm_names,
800 this->encryption.algorithm);
801
802 if (this->encryption.key_size)
803 {
804 written += fprintf(stream, "-%d", this->encryption.key_size);
805 }
806 written += fprintf(stream, "/");
807 }
808
809 written += fprintf(stream, "%N", integrity_algorithm_names,
810 this->integrity.algorithm);
811 if (this->integrity.key_size)
812 {
813 written += fprintf(stream, "-%d", this->integrity.key_size);
814 }
815 written += fprintf(stream, ", rekeying ");
816
817 /* calculate rekey times */
818 if (this->soft_lifetime)
819 {
820 rekeying = this->soft_lifetime - (now - this->install_time);
821 written += fprintf(stream, "in %ds", rekeying);
822 }
823 else
824 {
825 written += fprintf(stream, "disabled");
826 }
827 }
828 }
829 iterator = this->policies->create_iterator(this->policies, TRUE);
830 while (iterator->iterate(iterator, (void**)&policy))
831 {
832 written += fprintf(stream, "\n%12s: %R===%R, last use: ",
833 this->name, policy->my_ts, policy->other_ts);
834
835 /* query policy times */
836 status = charon->kernel_interface->query_policy(charon->kernel_interface,
837 policy->other_ts, policy->my_ts, POLICY_IN, &use);
838 if (status == SUCCESS && use)
839 {
840 written += fprintf(stream, "%ds_in ", now - use);
841 }
842 else
843 {
844 written += fprintf(stream, "no_in ");
845 }
846 status = charon->kernel_interface->query_policy(charon->kernel_interface,
847 policy->my_ts, policy->other_ts, POLICY_OUT, &use);
848 if (status == SUCCESS && use)
849 {
850 written += fprintf(stream, "%ds_out ", now - use);
851 }
852 else
853 {
854 written += fprintf(stream, "no_out ");
855 }
856 status = charon->kernel_interface->query_policy(charon->kernel_interface,
857 policy->other_ts, policy->my_ts, POLICY_FWD, &use);
858 if (status == SUCCESS && use)
859 {
860 written += fprintf(stream, "%ds_fwd", now - use);
861 }
862 else
863 {
864 written += fprintf(stream, "no_fwd");
865 }
866 }
867 iterator->destroy(iterator);
868 return written;
869 }
870
871 /**
872 * register printf() handlers
873 */
874 static void __attribute__ ((constructor))print_register()
875 {
876 register_printf_function(PRINTF_CHILD_SA, print, arginfo_ptr);
877 }
878
879 /**
880 * Update the host adress/port of a SA
881 */
882 static status_t update_sa_hosts(private_child_sa_t *this, host_t *new_me, host_t *new_other,
883 int my_changes, int other_changes, bool mine)
884 {
885 host_t *src, *dst, *new_src, *new_dst;
886 int src_changes, dst_changes;
887 status_t status;
888 u_int32_t spi;
889
890 if (mine)
891 {
892 src = this->other.addr;
893 dst = this->me.addr;
894 new_src = new_other;
895 new_dst = new_me;
896 src_changes = other_changes;
897 dst_changes = my_changes;
898 spi = this->other.spi;
899 }
900 else
901 {
902 src = this->me.addr;
903 dst = this->other.addr;
904 new_src = new_me;
905 new_dst = new_other;
906 src_changes = my_changes;
907 dst_changes = other_changes;
908 spi = this->me.spi;
909 }
910
911 DBG2(DBG_CHD, "updating %N SA 0x%x, from %#H..#H to %#H..%#H",
912 protocol_id_names, this->protocol, ntohl(spi), src, dst, new_src, new_dst);
913
914 status = charon->kernel_interface->update_sa(charon->kernel_interface,
915 dst, spi, this->protocol,
916 new_src, new_dst,
917 src_changes, dst_changes);
918
919 if (status != SUCCESS)
920 {
921 return FAILED;
922 }
923 return SUCCESS;
924 }
925
926 /**
927 * Update the host adress/port of a policy
928 */
929 static status_t update_policy_hosts(private_child_sa_t *this, host_t *new_me, host_t *new_other)
930 {
931 iterator_t *iterator;
932 sa_policy_t *policy;
933 status_t status;
934 /* we always use high priorities, as hosts getting updated are INSTALLED */
935
936 iterator = this->policies->create_iterator(this->policies, TRUE);
937 while (iterator->iterate(iterator, (void**)&policy))
938 {
939 status = charon->kernel_interface->add_policy(
940 charon->kernel_interface,
941 new_me, new_other,
942 policy->my_ts, policy->other_ts,
943 POLICY_OUT, this->protocol, this->reqid, TRUE, this->mode, TRUE);
944
945 status |= charon->kernel_interface->add_policy(
946 charon->kernel_interface,
947 new_other, new_me,
948 policy->other_ts, policy->my_ts,
949 POLICY_IN, this->protocol, this->reqid, TRUE, this->mode, TRUE);
950
951 status |= charon->kernel_interface->add_policy(
952 charon->kernel_interface,
953 new_other, new_me,
954 policy->other_ts, policy->my_ts,
955 POLICY_FWD, this->protocol, this->reqid, TRUE, this->mode, TRUE);
956
957 if (status != SUCCESS)
958 {
959 iterator->destroy(iterator);
960 return FAILED;
961 }
962 }
963 iterator->destroy(iterator);
964
965 return SUCCESS;
966 }
967
968 /**
969 * Implementation of child_sa_t.update_hosts.
970 */
971 static status_t update_hosts(private_child_sa_t *this, host_t *new_me, host_t *new_other,
972 host_diff_t my_changes, host_diff_t other_changes)
973 {
974 if (!my_changes && !other_changes)
975 {
976 return SUCCESS;
977 }
978
979 /* update our (initator) SAs */
980 if (update_sa_hosts(this, new_me, new_other, my_changes, other_changes, TRUE) != SUCCESS)
981 {
982 return FAILED;
983 }
984
985 /* update his (responder) SAs */
986 if (update_sa_hosts(this, new_me, new_other, my_changes, other_changes, FALSE) != SUCCESS)
987 {
988 return FAILED;
989 }
990
991 /* update policies */
992 if (my_changes & HOST_DIFF_ADDR || other_changes & HOST_DIFF_ADDR)
993 {
994 if (update_policy_hosts(this, new_me, new_other) != SUCCESS)
995 {
996 return FAILED;
997 }
998 }
999
1000 /* update hosts */
1001 if (my_changes)
1002 {
1003 this->me.addr->destroy(this->me.addr);
1004 this->me.addr = new_me->clone(new_me);
1005 }
1006
1007 if (other_changes)
1008 {
1009 this->other.addr->destroy(this->other.addr);
1010 this->other.addr = new_other->clone(new_other);
1011 }
1012
1013 return SUCCESS;
1014 }
1015
1016 /**
1017 * Implementation of child_sa_t.destroy.
1018 */
1019 static void destroy(private_child_sa_t *this)
1020 {
1021 sa_policy_t *policy;
1022
1023 if (this->state == CHILD_DELETING || this->state == CHILD_INSTALLED)
1024 {
1025 updown(this, FALSE);
1026 }
1027
1028 /* delete SAs in the kernel, if they are set up */
1029 if (this->me.spi)
1030 {
1031 charon->kernel_interface->del_sa(charon->kernel_interface,
1032 this->me.addr, this->me.spi, this->protocol);
1033 }
1034 if (this->alloc_esp_spi && this->alloc_esp_spi != this->me.spi)
1035 {
1036 charon->kernel_interface->del_sa(charon->kernel_interface,
1037 this->me.addr, this->alloc_esp_spi, PROTO_ESP);
1038 }
1039 if (this->alloc_ah_spi && this->alloc_ah_spi != this->me.spi)
1040 {
1041 charon->kernel_interface->del_sa(charon->kernel_interface,
1042 this->me.addr, this->alloc_ah_spi, PROTO_AH);
1043 }
1044 if (this->other.spi)
1045 {
1046 charon->kernel_interface->del_sa(charon->kernel_interface,
1047 this->other.addr, this->other.spi, this->protocol);
1048 }
1049
1050 /* delete all policies in the kernel */
1051 while (this->policies->remove_last(this->policies, (void**)&policy) == SUCCESS)
1052 {
1053 /* let rekeyed policies, as they are used by another child_sa */
1054 charon->kernel_interface->del_policy(charon->kernel_interface,
1055 policy->my_ts, policy->other_ts,
1056 POLICY_OUT);
1057
1058 charon->kernel_interface->del_policy(charon->kernel_interface,
1059 policy->other_ts, policy->my_ts,
1060 POLICY_IN);
1061
1062 charon->kernel_interface->del_policy(charon->kernel_interface,
1063 policy->other_ts, policy->my_ts,
1064 POLICY_FWD);
1065 policy->my_ts->destroy(policy->my_ts);
1066 policy->other_ts->destroy(policy->other_ts);
1067 free(policy);
1068 }
1069 this->policies->destroy(this->policies);
1070
1071 this->my_ts->destroy(this->my_ts);
1072 this->other_ts->destroy(this->other_ts);
1073 this->me.addr->destroy(this->me.addr);
1074 this->other.addr->destroy(this->other.addr);
1075 this->me.id->destroy(this->me.id);
1076 this->other.id->destroy(this->other.id);
1077 free(this->name);
1078 free(this->script);
1079 free(this);
1080 }
1081
1082 /*
1083 * Described in header.
1084 */
1085 child_sa_t * child_sa_create(u_int32_t rekey, host_t *me, host_t* other,
1086 identification_t *my_id, identification_t *other_id,
1087 u_int32_t soft_lifetime, u_int32_t hard_lifetime,
1088 char *script, bool hostaccess, bool use_natt)
1089 {
1090 static u_int32_t reqid = REQID_START;
1091 private_child_sa_t *this = malloc_thing(private_child_sa_t);
1092
1093 /* public functions */
1094 this->public.get_name = (char*(*)(child_sa_t*))get_name;
1095 this->public.set_name = (void(*)(child_sa_t*,char*))set_name;
1096 this->public.get_reqid = (u_int32_t(*)(child_sa_t*))get_reqid;
1097 this->public.get_spi = (u_int32_t(*)(child_sa_t*, bool))get_spi;
1098 this->public.get_protocol = (protocol_id_t(*)(child_sa_t*))get_protocol;
1099 this->public.alloc = (status_t(*)(child_sa_t*,linked_list_t*))alloc;
1100 this->public.add = (status_t(*)(child_sa_t*,proposal_t*,mode_t,prf_plus_t*))add;
1101 this->public.update = (status_t(*)(child_sa_t*,proposal_t*,mode_t,prf_plus_t*))update;
1102 this->public.update_hosts = (status_t (*)(child_sa_t*,host_t*,host_t*,host_diff_t,host_diff_t))update_hosts;
1103 this->public.add_policies = (status_t (*)(child_sa_t*, linked_list_t*,linked_list_t*,mode_t))add_policies;
1104 this->public.get_my_traffic_selectors = (linked_list_t*(*)(child_sa_t*))get_my_traffic_selectors;
1105 this->public.get_other_traffic_selectors = (linked_list_t*(*)(child_sa_t*))get_other_traffic_selectors;
1106 this->public.get_use_time = (status_t (*)(child_sa_t*,bool,time_t*))get_use_time;
1107 this->public.set_rekeying_transaction = (void (*)(child_sa_t*,transaction_t*))set_rekeying_transaction;
1108 this->public.get_rekeying_transaction = (transaction_t* (*)(child_sa_t*))get_rekeying_transaction;
1109 this->public.set_state = (void(*)(child_sa_t*,child_sa_state_t))set_state;
1110 this->public.get_state = (child_sa_state_t(*)(child_sa_t*))get_state;
1111 this->public.destroy = (void(*)(child_sa_t*))destroy;
1112
1113 /* private data */
1114 this->name = strdup("(uninitialized)");
1115 this->me.addr = me->clone(me);
1116 this->other.addr = other->clone(other);
1117 this->me.id = my_id->clone(my_id);
1118 this->other.id = other_id->clone(other_id);
1119 this->me.spi = 0;
1120 this->other.spi = 0;
1121 this->alloc_ah_spi = 0;
1122 this->alloc_esp_spi = 0;
1123 this->script = script ? strdup(script) : NULL;
1124 this->hostaccess = hostaccess;
1125 this->use_natt = use_natt;
1126 this->soft_lifetime = soft_lifetime;
1127 this->hard_lifetime = hard_lifetime;
1128 this->state = CHILD_CREATED;
1129 /* reuse old reqid if we are rekeying an existing CHILD_SA */
1130 this->reqid = rekey ? rekey : ++reqid;
1131 this->encryption.algorithm = ENCR_UNDEFINED;
1132 this->encryption.key_size = 0;
1133 this->integrity.algorithm = AUTH_UNDEFINED;
1134 this->encryption.key_size = 0;
1135 this->policies = linked_list_create();
1136 this->my_ts = linked_list_create();
1137 this->other_ts = linked_list_create();
1138 this->protocol = PROTO_NONE;
1139 this->mode = MODE_TUNNEL;
1140 this->rekeying_transaction = NULL;
1141
1142 return &this->public;
1143 }