added support for transport mode and (experimental!) BEET mode
[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", this->name,
784 child_sa_state_names, this->state, this->reqid);
785
786 if (this->state == CHILD_INSTALLED)
787 {
788 written += fprintf(stream, ", %N SPIs: 0x%0x_i 0x%0x_o",
789 protocol_id_names, this->protocol,
790 htonl(this->me.spi), htonl(this->other.spi));
791
792 if (info->alt)
793 {
794 written += fprintf(stream, "\n%12s: ", this->name);
795
796 if (this->protocol == PROTO_ESP)
797 {
798 written += fprintf(stream, "%N", encryption_algorithm_names,
799 this->encryption.algorithm);
800
801 if (this->encryption.key_size)
802 {
803 written += fprintf(stream, "-%d", this->encryption.key_size);
804 }
805 written += fprintf(stream, "/");
806 }
807
808 written += fprintf(stream, "%N", integrity_algorithm_names,
809 this->integrity.algorithm);
810 if (this->integrity.key_size)
811 {
812 written += fprintf(stream, "-%d", this->integrity.key_size);
813 }
814 written += fprintf(stream, ", rekeying ");
815
816 /* calculate rekey times */
817 if (this->soft_lifetime)
818 {
819 rekeying = this->soft_lifetime - (now - this->install_time);
820 written += fprintf(stream, "in %ds", rekeying);
821 }
822 else
823 {
824 written += fprintf(stream, "disabled");
825 }
826 }
827 }
828 iterator = this->policies->create_iterator(this->policies, TRUE);
829 while (iterator->iterate(iterator, (void**)&policy))
830 {
831 written += fprintf(stream, "\n%12s: %R===%R, last use: ",
832 this->name, policy->my_ts, policy->other_ts);
833
834 /* query policy times */
835 status = charon->kernel_interface->query_policy(charon->kernel_interface,
836 policy->other_ts, policy->my_ts, POLICY_IN, &use);
837 if (status == SUCCESS && use)
838 {
839 written += fprintf(stream, "%ds_in ", now - use);
840 }
841 else
842 {
843 written += fprintf(stream, "no_in ");
844 }
845 status = charon->kernel_interface->query_policy(charon->kernel_interface,
846 policy->my_ts, policy->other_ts, POLICY_OUT, &use);
847 if (status == SUCCESS && use)
848 {
849 written += fprintf(stream, "%ds_out ", now - use);
850 }
851 else
852 {
853 written += fprintf(stream, "no_out ");
854 }
855 status = charon->kernel_interface->query_policy(charon->kernel_interface,
856 policy->other_ts, policy->my_ts, POLICY_FWD, &use);
857 if (status == SUCCESS && use)
858 {
859 written += fprintf(stream, "%ds_fwd", now - use);
860 }
861 else
862 {
863 written += fprintf(stream, "no_fwd");
864 }
865 }
866 iterator->destroy(iterator);
867 return written;
868 }
869
870 /**
871 * register printf() handlers
872 */
873 static void __attribute__ ((constructor))print_register()
874 {
875 register_printf_function(PRINTF_CHILD_SA, print, arginfo_ptr);
876 }
877
878 /**
879 * Update the host adress/port of a SA
880 */
881 static status_t update_sa_hosts(private_child_sa_t *this, host_t *new_me, host_t *new_other,
882 int my_changes, int other_changes, bool mine)
883 {
884 host_t *src, *dst, *new_src, *new_dst;
885 int src_changes, dst_changes;
886 status_t status;
887 u_int32_t spi;
888
889 if (mine)
890 {
891 src = this->other.addr;
892 dst = this->me.addr;
893 new_src = new_other;
894 new_dst = new_me;
895 src_changes = other_changes;
896 dst_changes = my_changes;
897 spi = this->other.spi;
898 }
899 else
900 {
901 src = this->me.addr;
902 dst = this->other.addr;
903 new_src = new_me;
904 new_dst = new_other;
905 src_changes = my_changes;
906 dst_changes = other_changes;
907 spi = this->me.spi;
908 }
909
910 DBG2(DBG_CHD, "updating %N SA 0x%x, from %#H..#H to %#H..%#H",
911 protocol_id_names, this->protocol, ntohl(spi), src, dst, new_src, new_dst);
912
913 status = charon->kernel_interface->update_sa(charon->kernel_interface,
914 dst, spi, this->protocol,
915 new_src, new_dst,
916 src_changes, dst_changes);
917
918 if (status != SUCCESS)
919 {
920 return FAILED;
921 }
922 return SUCCESS;
923 }
924
925 /**
926 * Update the host adress/port of a policy
927 */
928 static status_t update_policy_hosts(private_child_sa_t *this, host_t *new_me, host_t *new_other)
929 {
930 iterator_t *iterator;
931 sa_policy_t *policy;
932 status_t status;
933 /* we always use high priorities, as hosts getting updated are INSTALLED */
934
935 iterator = this->policies->create_iterator(this->policies, TRUE);
936 while (iterator->iterate(iterator, (void**)&policy))
937 {
938 status = charon->kernel_interface->add_policy(
939 charon->kernel_interface,
940 new_me, new_other,
941 policy->my_ts, policy->other_ts,
942 POLICY_OUT, this->protocol, this->reqid, TRUE, this->mode, TRUE);
943
944 status |= charon->kernel_interface->add_policy(
945 charon->kernel_interface,
946 new_other, new_me,
947 policy->other_ts, policy->my_ts,
948 POLICY_IN, this->protocol, this->reqid, TRUE, this->mode, TRUE);
949
950 status |= charon->kernel_interface->add_policy(
951 charon->kernel_interface,
952 new_other, new_me,
953 policy->other_ts, policy->my_ts,
954 POLICY_FWD, this->protocol, this->reqid, TRUE, this->mode, TRUE);
955
956 if (status != SUCCESS)
957 {
958 iterator->destroy(iterator);
959 return FAILED;
960 }
961 }
962 iterator->destroy(iterator);
963
964 return SUCCESS;
965 }
966
967 /**
968 * Implementation of child_sa_t.update_hosts.
969 */
970 static status_t update_hosts(private_child_sa_t *this, host_t *new_me, host_t *new_other,
971 host_diff_t my_changes, host_diff_t other_changes)
972 {
973 if (!my_changes && !other_changes)
974 {
975 return SUCCESS;
976 }
977
978 /* update our (initator) SAs */
979 if (update_sa_hosts(this, new_me, new_other, my_changes, other_changes, TRUE) != SUCCESS)
980 {
981 return FAILED;
982 }
983
984 /* update his (responder) SAs */
985 if (update_sa_hosts(this, new_me, new_other, my_changes, other_changes, FALSE) != SUCCESS)
986 {
987 return FAILED;
988 }
989
990 /* update policies */
991 if (my_changes & HOST_DIFF_ADDR || other_changes & HOST_DIFF_ADDR)
992 {
993 if (update_policy_hosts(this, new_me, new_other) != SUCCESS)
994 {
995 return FAILED;
996 }
997 }
998
999 /* update hosts */
1000 if (my_changes)
1001 {
1002 this->me.addr->destroy(this->me.addr);
1003 this->me.addr = new_me->clone(new_me);
1004 }
1005
1006 if (other_changes)
1007 {
1008 this->other.addr->destroy(this->other.addr);
1009 this->other.addr = new_other->clone(new_other);
1010 }
1011
1012 return SUCCESS;
1013 }
1014
1015 /**
1016 * Implementation of child_sa_t.destroy.
1017 */
1018 static void destroy(private_child_sa_t *this)
1019 {
1020 sa_policy_t *policy;
1021
1022 if (this->state == CHILD_DELETING || this->state == CHILD_INSTALLED)
1023 {
1024 updown(this, FALSE);
1025 }
1026
1027 /* delete SAs in the kernel, if they are set up */
1028 if (this->me.spi)
1029 {
1030 charon->kernel_interface->del_sa(charon->kernel_interface,
1031 this->me.addr, this->me.spi, this->protocol);
1032 }
1033 if (this->alloc_esp_spi && this->alloc_esp_spi != this->me.spi)
1034 {
1035 charon->kernel_interface->del_sa(charon->kernel_interface,
1036 this->me.addr, this->alloc_esp_spi, PROTO_ESP);
1037 }
1038 if (this->alloc_ah_spi && this->alloc_ah_spi != this->me.spi)
1039 {
1040 charon->kernel_interface->del_sa(charon->kernel_interface,
1041 this->me.addr, this->alloc_ah_spi, PROTO_AH);
1042 }
1043 if (this->other.spi)
1044 {
1045 charon->kernel_interface->del_sa(charon->kernel_interface,
1046 this->other.addr, this->other.spi, this->protocol);
1047 }
1048
1049 /* delete all policies in the kernel */
1050 while (this->policies->remove_last(this->policies, (void**)&policy) == SUCCESS)
1051 {
1052 /* let rekeyed policies, as they are used by another child_sa */
1053 charon->kernel_interface->del_policy(charon->kernel_interface,
1054 policy->my_ts, policy->other_ts,
1055 POLICY_OUT);
1056
1057 charon->kernel_interface->del_policy(charon->kernel_interface,
1058 policy->other_ts, policy->my_ts,
1059 POLICY_IN);
1060
1061 charon->kernel_interface->del_policy(charon->kernel_interface,
1062 policy->other_ts, policy->my_ts,
1063 POLICY_FWD);
1064 policy->my_ts->destroy(policy->my_ts);
1065 policy->other_ts->destroy(policy->other_ts);
1066 free(policy);
1067 }
1068 this->policies->destroy(this->policies);
1069
1070 this->my_ts->destroy(this->my_ts);
1071 this->other_ts->destroy(this->other_ts);
1072 this->me.addr->destroy(this->me.addr);
1073 this->other.addr->destroy(this->other.addr);
1074 this->me.id->destroy(this->me.id);
1075 this->other.id->destroy(this->other.id);
1076 free(this->name);
1077 free(this->script);
1078 free(this);
1079 }
1080
1081 /*
1082 * Described in header.
1083 */
1084 child_sa_t * child_sa_create(u_int32_t rekey, host_t *me, host_t* other,
1085 identification_t *my_id, identification_t *other_id,
1086 u_int32_t soft_lifetime, u_int32_t hard_lifetime,
1087 char *script, bool hostaccess, bool use_natt)
1088 {
1089 static u_int32_t reqid = REQID_START;
1090 private_child_sa_t *this = malloc_thing(private_child_sa_t);
1091
1092 /* public functions */
1093 this->public.get_name = (char*(*)(child_sa_t*))get_name;
1094 this->public.set_name = (void(*)(child_sa_t*,char*))set_name;
1095 this->public.get_reqid = (u_int32_t(*)(child_sa_t*))get_reqid;
1096 this->public.get_spi = (u_int32_t(*)(child_sa_t*, bool))get_spi;
1097 this->public.get_protocol = (protocol_id_t(*)(child_sa_t*))get_protocol;
1098 this->public.alloc = (status_t(*)(child_sa_t*,linked_list_t*))alloc;
1099 this->public.add = (status_t(*)(child_sa_t*,proposal_t*,mode_t,prf_plus_t*))add;
1100 this->public.update = (status_t(*)(child_sa_t*,proposal_t*,mode_t,prf_plus_t*))update;
1101 this->public.update_hosts = (status_t (*)(child_sa_t*,host_t*,host_t*,host_diff_t,host_diff_t))update_hosts;
1102 this->public.add_policies = (status_t (*)(child_sa_t*, linked_list_t*,linked_list_t*,mode_t))add_policies;
1103 this->public.get_my_traffic_selectors = (linked_list_t*(*)(child_sa_t*))get_my_traffic_selectors;
1104 this->public.get_other_traffic_selectors = (linked_list_t*(*)(child_sa_t*))get_other_traffic_selectors;
1105 this->public.get_use_time = (status_t (*)(child_sa_t*,bool,time_t*))get_use_time;
1106 this->public.set_rekeying_transaction = (void (*)(child_sa_t*,transaction_t*))set_rekeying_transaction;
1107 this->public.get_rekeying_transaction = (transaction_t* (*)(child_sa_t*))get_rekeying_transaction;
1108 this->public.set_state = (void(*)(child_sa_t*,child_sa_state_t))set_state;
1109 this->public.get_state = (child_sa_state_t(*)(child_sa_t*))get_state;
1110 this->public.destroy = (void(*)(child_sa_t*))destroy;
1111
1112 /* private data */
1113 this->name = strdup("(uninitialized)");
1114 this->me.addr = me->clone(me);
1115 this->other.addr = other->clone(other);
1116 this->me.id = my_id->clone(my_id);
1117 this->other.id = other_id->clone(other_id);
1118 this->me.spi = 0;
1119 this->other.spi = 0;
1120 this->alloc_ah_spi = 0;
1121 this->alloc_esp_spi = 0;
1122 this->script = script ? strdup(script) : NULL;
1123 this->hostaccess = hostaccess;
1124 this->use_natt = use_natt;
1125 this->soft_lifetime = soft_lifetime;
1126 this->hard_lifetime = hard_lifetime;
1127 this->state = CHILD_CREATED;
1128 /* reuse old reqid if we are rekeying an existing CHILD_SA */
1129 this->reqid = rekey ? rekey : ++reqid;
1130 this->encryption.algorithm = ENCR_UNDEFINED;
1131 this->encryption.key_size = 0;
1132 this->integrity.algorithm = AUTH_UNDEFINED;
1133 this->encryption.key_size = 0;
1134 this->policies = linked_list_create();
1135 this->my_ts = linked_list_create();
1136 this->other_ts = linked_list_create();
1137 this->protocol = PROTO_NONE;
1138 this->mode = MODE_TUNNEL;
1139 this->rekeying_transaction = NULL;
1140
1141 return &this->public;
1142 }