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