some improvements in signaling code
[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 void *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 /**
173 * Implementation of child_sa_t.get_name.
174 */
175 static char *get_name(private_child_sa_t *this)
176 {
177 return this->name;
178 }
179
180 /**
181 * Implementation of child_sa_t.set_name.
182 */
183 static void set_name(private_child_sa_t *this, char* name)
184 {
185 char buffer[64];
186
187 if (snprintf(buffer, sizeof(buffer), "%s[%d]",
188 name, this->reqid - REQID_START) > 0)
189 {
190 free(this->name);
191 this->name = strdup(buffer);
192 }
193 }
194
195 /**
196 * Implements child_sa_t.get_reqid
197 */
198 static u_int32_t get_reqid(private_child_sa_t *this)
199 {
200 return this->reqid;
201 }
202
203 /**
204 * Implements child_sa_t.get_spi
205 */
206 u_int32_t get_spi(private_child_sa_t *this, bool inbound)
207 {
208 if (inbound)
209 {
210 return this->me.spi;
211 }
212 return this->other.spi;
213 }
214
215 /**
216 * Implements child_sa_t.get_protocol
217 */
218 protocol_id_t get_protocol(private_child_sa_t *this)
219 {
220 return this->protocol;
221 }
222
223 /**
224 * Implements child_sa_t.get_state
225 */
226 static child_sa_state_t get_state(private_child_sa_t *this)
227 {
228 return this->state;
229 }
230
231 /**
232 * Run the up/down script
233 */
234 static void updown(private_child_sa_t *this, bool up)
235 {
236 sa_policy_t *policy;
237 iterator_t *iterator;
238
239 if (this->script == NULL)
240 {
241 return;
242 }
243
244 iterator = this->policies->create_iterator(this->policies, TRUE);
245 while (iterator->iterate(iterator, (void**)&policy))
246 {
247 char command[1024];
248 char *ifname = NULL;
249 char *my_client, *other_client, *my_client_mask, *other_client_mask;
250 char *pos;
251 FILE *shell;
252
253 /* get subnet/bits from string */
254 asprintf(&my_client, "%R", policy->my_ts);
255 pos = strchr(my_client, '/');
256 *pos = '\0';
257 my_client_mask = pos + 1;
258 pos = strchr(my_client_mask, '[');
259 if (pos)
260 {
261 *pos = '\0';
262 }
263 asprintf(&other_client, "%R", policy->other_ts);
264 pos = strchr(other_client, '/');
265 *pos = '\0';
266 other_client_mask = pos + 1;
267 pos = strchr(other_client_mask, '[');
268 if (pos)
269 {
270 *pos = '\0';
271 }
272
273 charon->socket->is_local_address(charon->socket, this->me.addr, &ifname);
274
275 /* build the command with all env variables.
276 * TODO: PLUTO_MY_SRCIP, PLUTO_PEER_CA and PLUTO_NEXT_HOP
277 * are currently missing */
278 snprintf(command, sizeof(command),
279 "2>&1 "
280 "PLUTO_VERSION='1.1' "
281 "PLUTO_VERB='%s%s%s' "
282 "PLUTO_CONNECTION='%s' "
283 "PLUTO_INTERFACE='%s' "
284 "PLUTO_REQID='%u' "
285 "PLUTO_ME='%H' "
286 "PLUTO_MY_ID='%D' "
287 "PLUTO_MY_CLIENT='%s/%s' "
288 "PLUTO_MY_CLIENT_NET='%s' "
289 "PLUTO_MY_CLIENT_MASK='%s' "
290 "PLUTO_MY_PORT='%u' "
291 "PLUTO_MY_PROTOCOL='%u' "
292 "PLUTO_PEER='%H' "
293 "PLUTO_PEER_ID='%D' "
294 "PLUTO_PEER_CLIENT='%s/%s' "
295 "PLUTO_PEER_CLIENT_NET='%s' "
296 "PLUTO_PEER_CLIENT_MASK='%s' "
297 "PLUTO_PEER_PORT='%u' "
298 "PLUTO_PEER_PROTOCOL='%u' "
299 "%s"
300 "%s",
301 up ? "up" : "down",
302 policy->my_ts->is_host(policy->my_ts,
303 this->me.addr) ? "-host" : "-client",
304 this->me.addr->get_family(this->me.addr) == AF_INET ? "" : "-ipv6",
305 this->name,
306 ifname,
307 this->reqid,
308 this->me.addr,
309 this->me.id,
310 my_client, my_client_mask,
311 my_client, my_client_mask,
312 policy->my_ts->get_from_port(policy->my_ts),
313 policy->my_ts->get_protocol(policy->my_ts),
314 this->other.addr,
315 this->other.id,
316 other_client, other_client_mask,
317 other_client, other_client_mask,
318 policy->other_ts->get_from_port(policy->other_ts),
319 policy->other_ts->get_protocol(policy->other_ts),
320 this->hostaccess? "PLUTO_HOST_ACCESS='1' " : "",
321 this->script);
322 free(ifname);
323 free(my_client);
324 free(other_client);
325
326 shell = popen(command, "r");
327
328 if (shell == NULL)
329 {
330 DBG1(DBG_CHD, "could not execute updown script '%s'", this->script);
331 return;
332 }
333
334 while (TRUE)
335 {
336 char resp[128];
337
338 if (fgets(resp, sizeof(resp), shell) == NULL)
339 {
340 if (ferror(shell))
341 {
342 DBG1(DBG_CHD, "error reading output from updown script");
343 return;
344 }
345 else
346 {
347 break;
348 }
349 }
350 else
351 {
352 char *e = resp + strlen(resp);
353 if (e > resp && e[-1] == '\n')
354 { /* trim trailing '\n' */
355 e[-1] = '\0';
356 }
357 DBG1(DBG_CHD, "updown: %s", resp);
358 }
359 }
360 pclose(shell);
361 }
362 iterator->destroy(iterator);
363 }
364
365 /**
366 * Implements child_sa_t.set_state
367 */
368 static void set_state(private_child_sa_t *this, child_sa_state_t state)
369 {
370 this->state = state;
371 if (state == CHILD_INSTALLED)
372 {
373 updown(this, TRUE);
374 }
375 }
376
377 /**
378 * Allocate SPI for a single proposal
379 */
380 static status_t alloc_proposal(private_child_sa_t *this, proposal_t *proposal)
381 {
382 protocol_id_t protocol = proposal->get_protocol(proposal);
383
384 if (protocol == PROTO_AH)
385 {
386 /* get a new spi for AH, if not already done */
387 if (this->alloc_ah_spi == 0)
388 {
389 if (charon->kernel_interface->get_spi(
390 charon->kernel_interface,
391 this->other.addr, this->me.addr,
392 PROTO_AH, this->reqid,
393 &this->alloc_ah_spi) != SUCCESS)
394 {
395 return FAILED;
396 }
397 }
398 proposal->set_spi(proposal, this->alloc_ah_spi);
399 }
400 if (protocol == PROTO_ESP)
401 {
402 /* get a new spi for ESP, if not already done */
403 if (this->alloc_esp_spi == 0)
404 {
405 if (charon->kernel_interface->get_spi(
406 charon->kernel_interface,
407 this->other.addr, this->me.addr,
408 PROTO_ESP, this->reqid,
409 &this->alloc_esp_spi) != SUCCESS)
410 {
411 return FAILED;
412 }
413 }
414 proposal->set_spi(proposal, this->alloc_esp_spi);
415 }
416 return SUCCESS;
417 }
418
419
420 /**
421 * Implements child_sa_t.alloc
422 */
423 static status_t alloc(private_child_sa_t *this, linked_list_t *proposals)
424 {
425 iterator_t *iterator;
426 proposal_t *proposal;
427
428 /* iterator through proposals to update spis */
429 iterator = proposals->create_iterator(proposals, TRUE);
430 while(iterator->iterate(iterator, (void**)&proposal))
431 {
432 if (alloc_proposal(this, proposal) != SUCCESS)
433 {
434 iterator->destroy(iterator);
435 return FAILED;
436 }
437 }
438 iterator->destroy(iterator);
439 return SUCCESS;
440 }
441
442 static status_t install(private_child_sa_t *this, proposal_t *proposal, prf_plus_t *prf_plus, bool mine)
443 {
444 u_int32_t spi;
445 algorithm_t *enc_algo, *int_algo;
446 algorithm_t enc_algo_none = {ENCR_UNDEFINED, 0};
447 algorithm_t int_algo_none = {AUTH_UNDEFINED, 0};
448 host_t *src;
449 host_t *dst;
450 natt_conf_t *natt;
451 status_t status;
452
453 this->protocol = proposal->get_protocol(proposal);
454
455 /* now we have to decide which spi to use. Use self allocated, if "mine",
456 * or the one in the proposal, if not "mine" (others). Additionally,
457 * source and dest host switch depending on the role */
458 if (mine)
459 {
460 /* if we have allocated SPIs for AH and ESP, we must delete the unused
461 * one. */
462 if (this->protocol == PROTO_ESP)
463 {
464 this->me.spi = this->alloc_esp_spi;
465 if (this->alloc_ah_spi)
466 {
467 charon->kernel_interface->del_sa(charon->kernel_interface, this->me.addr,
468 this->alloc_ah_spi, PROTO_AH);
469 }
470 }
471 else
472 {
473 this->me.spi = this->alloc_ah_spi;
474 if (this->alloc_esp_spi)
475 {
476 charon->kernel_interface->del_sa(charon->kernel_interface, this->me.addr,
477 this->alloc_esp_spi, PROTO_ESP);
478 }
479 }
480 spi = this->me.spi;
481 dst = this->me.addr;
482 src = this->other.addr;
483 }
484 else
485 {
486 this->other.spi = proposal->get_spi(proposal);
487 spi = this->other.spi;
488 src = this->me.addr;
489 dst = this->other.addr;
490 }
491
492 DBG2(DBG_CHD, "adding %s %N SA", mine ? "inbound" : "outbound",
493 protocol_id_names, this->protocol);
494
495 /* select encryption algo */
496 if (proposal->get_algorithm(proposal, ENCRYPTION_ALGORITHM, &enc_algo))
497 {
498 DBG2(DBG_CHD, " using %N for encryption",
499 encryption_algorithm_names, enc_algo->algorithm);
500 }
501 else
502 {
503 enc_algo = &enc_algo_none;
504 }
505
506 /* select integrity algo */
507 if (proposal->get_algorithm(proposal, INTEGRITY_ALGORITHM, &int_algo))
508 {
509 DBG2(DBG_CHD, " using %N for integrity",
510 integrity_algorithm_names, int_algo->algorithm);
511 }
512 else
513 {
514 int_algo = &int_algo_none;
515 }
516
517 /* setup nat-t */
518 if (this->use_natt)
519 {
520 natt = alloca(sizeof(natt_conf_t));
521 natt->sport = src->get_port(src);
522 natt->dport = dst->get_port(dst);
523 }
524 else
525 {
526 natt = NULL;
527 }
528
529
530 /* send SA down to the kernel */
531 DBG2(DBG_CHD, " SPI 0x%.8x, src %H dst %H", ntohl(spi), src, dst);
532 status = charon->kernel_interface->add_sa(charon->kernel_interface,
533 src, dst,
534 spi, this->protocol,
535 this->reqid,
536 mine ? this->soft_lifetime : 0,
537 this->hard_lifetime,
538 enc_algo, int_algo,
539 prf_plus, natt, mine);
540
541 this->encryption = *enc_algo;
542 this->integrity = *int_algo;
543 this->install_time = time(NULL);
544
545 return status;
546 }
547
548 static status_t add(private_child_sa_t *this, proposal_t *proposal, prf_plus_t *prf_plus)
549 {
550 u_int32_t outbound_spi, inbound_spi;
551
552 /* backup outbound spi, as alloc overwrites it */
553 outbound_spi = proposal->get_spi(proposal);
554
555 /* get SPIs inbound SAs */
556 if (alloc_proposal(this, proposal) != SUCCESS)
557 {
558 return FAILED;
559 }
560 inbound_spi = proposal->get_spi(proposal);
561
562 /* install inbound SAs */
563 if (install(this, proposal, prf_plus, TRUE) != SUCCESS)
564 {
565 return FAILED;
566 }
567
568 /* install outbound SAs, restore spi*/
569 proposal->set_spi(proposal, outbound_spi);
570 if (install(this, proposal, prf_plus, FALSE) != SUCCESS)
571 {
572 return FAILED;
573 }
574 proposal->set_spi(proposal, inbound_spi);
575
576 return SUCCESS;
577 }
578
579 static status_t update(private_child_sa_t *this, proposal_t *proposal, prf_plus_t *prf_plus)
580 {
581 u_int32_t inbound_spi;
582
583 /* backup received spi, as install() overwrites it */
584 inbound_spi = proposal->get_spi(proposal);
585
586 /* install outbound SAs */
587 if (install(this, proposal, prf_plus, FALSE) != SUCCESS)
588 {
589 return FAILED;
590 }
591
592 /* restore spi */
593 proposal->set_spi(proposal, inbound_spi);
594 /* install inbound SAs */
595 if (install(this, proposal, prf_plus, TRUE) != SUCCESS)
596 {
597 return FAILED;
598 }
599
600 return SUCCESS;
601 }
602
603 static status_t add_policies(private_child_sa_t *this, linked_list_t *my_ts_list, linked_list_t *other_ts_list)
604 {
605 iterator_t *my_iter, *other_iter;
606 traffic_selector_t *my_ts, *other_ts;
607 /* use low prio for ROUTED policies */
608 bool high_prio = (this->state != CHILD_CREATED);
609
610 /* iterate over both lists */
611 my_iter = my_ts_list->create_iterator(my_ts_list, TRUE);
612 other_iter = other_ts_list->create_iterator(other_ts_list, TRUE);
613 while (my_iter->iterate(my_iter, (void**)&my_ts))
614 {
615 other_iter->reset(other_iter);
616 while (other_iter->iterate(other_iter, (void**)&other_ts))
617 {
618 /* set up policies for every entry in my_ts_list to every entry in other_ts_list */
619 status_t status;
620 sa_policy_t *policy;
621
622 if (my_ts->get_type(my_ts) != other_ts->get_type(other_ts))
623 {
624 DBG2(DBG_CHD,
625 "CHILD_SA policy uses two different IP families, ignored");
626 continue;
627 }
628
629 /* only set up policies if protocol matches, or if one is zero (any) */
630 if (my_ts->get_protocol(my_ts) != other_ts->get_protocol(other_ts) &&
631 my_ts->get_protocol(my_ts) && other_ts->get_protocol(other_ts))
632 {
633 DBG2(DBG_CHD,
634 "CHILD_SA policy uses two different protocols, ignored");
635 continue;
636 }
637
638 /* install 3 policies: out, in and forward */
639 status = charon->kernel_interface->add_policy(charon->kernel_interface,
640 this->me.addr, this->other.addr, my_ts, other_ts,
641 POLICY_OUT, this->protocol, this->reqid, high_prio, FALSE);
642
643 status |= charon->kernel_interface->add_policy(charon->kernel_interface,
644 this->other.addr, this->me.addr, other_ts, my_ts,
645 POLICY_IN, this->protocol, this->reqid, high_prio, FALSE);
646
647 status |= charon->kernel_interface->add_policy(charon->kernel_interface,
648 this->other.addr, this->me.addr, other_ts, my_ts,
649 POLICY_FWD, this->protocol, this->reqid, high_prio, FALSE);
650
651 if (status != SUCCESS)
652 {
653 my_iter->destroy(my_iter);
654 other_iter->destroy(other_iter);
655 return status;
656 }
657
658 /* store policy to delete/update them later */
659 policy = malloc_thing(sa_policy_t);
660 policy->my_ts = my_ts->clone(my_ts);
661 policy->other_ts = other_ts->clone(other_ts);
662 this->policies->insert_last(this->policies, (void*)policy);
663 /* add to separate list to query them via get_*_traffic_selectors() */
664 this->my_ts->insert_last(this->my_ts, (void*)policy->my_ts);
665 this->other_ts->insert_last(this->other_ts, (void*)policy->other_ts);
666 }
667 }
668 my_iter->destroy(my_iter);
669 other_iter->destroy(other_iter);
670
671 /* switch to routed state if no SAD entry set up */
672 if (this->state == CHILD_CREATED)
673 {
674 this->state = CHILD_ROUTED;
675 }
676
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.set_rekeying_transaction.
698 */
699 static void set_rekeying_transaction(private_child_sa_t *this, void *transaction)
700 {
701 this->rekeying_transaction = transaction;
702 }
703
704 /**
705 * Implementation of child_sa_t.get_rekeying_transaction.
706 */
707 static void* get_rekeying_transaction(private_child_sa_t *this)
708 {
709 return this->rekeying_transaction;
710 }
711
712 /**
713 * Implementation of child_sa_t.get_use_time
714 */
715 static status_t get_use_time(private_child_sa_t *this, bool inbound, time_t *use_time)
716 {
717 iterator_t *iterator;
718 sa_policy_t *policy;
719 status_t status = FAILED;
720
721 *use_time = UNDEFINED_TIME;
722
723 iterator = this->policies->create_iterator(this->policies, TRUE);
724 while (iterator->iterate(iterator, (void**)&policy))
725 {
726 if (inbound)
727 {
728 time_t in = UNDEFINED_TIME, fwd = UNDEFINED_TIME;
729
730 status = charon->kernel_interface->query_policy(
731 charon->kernel_interface,
732 policy->other_ts, policy->my_ts,
733 POLICY_IN, (u_int32_t*)&in);
734 status |= charon->kernel_interface->query_policy(
735 charon->kernel_interface,
736 policy->other_ts, policy->my_ts,
737 POLICY_FWD, (u_int32_t*)&fwd);
738 *use_time = max(in, fwd);
739 }
740 else
741 {
742 status = charon->kernel_interface->query_policy(
743 charon->kernel_interface,
744 policy->my_ts, policy->other_ts,
745 POLICY_OUT, (u_int32_t*)use_time);
746 }
747 }
748 iterator->destroy(iterator);
749 return status;
750 }
751
752 /**
753 * output handler in printf()
754 */
755 static int print(FILE *stream, const struct printf_info *info,
756 const void *const *args)
757 {
758 private_child_sa_t *this = *((private_child_sa_t**)(args[0]));
759 iterator_t *iterator;
760 sa_policy_t *policy;
761 u_int32_t now, rekeying, use;
762 status_t status;
763 size_t written = 0;
764
765 if (this == NULL)
766 {
767 return fprintf(stream, "(null)");
768 }
769
770 now = (u_int32_t)time(NULL);
771
772 written += fprintf(stream, "%10s: %N, reqid: %d", this->name,
773 child_sa_state_names, this->state, this->reqid);
774
775 if (this->state == CHILD_INSTALLED)
776 {
777 written += fprintf(stream, ", %N, SPIs (in/out): 0x%x/0x%x",
778 protocol_id_names, this->protocol,
779 htonl(this->me.spi), htonl(this->other.spi));
780
781 if (info->alt)
782 {
783 written += fprintf(stream, "\n%10s: ", this->name);
784
785 if (this->protocol == PROTO_ESP)
786 {
787 written += fprintf(stream, "%N", encryption_algorithm_names,
788 this->encryption.algorithm);
789
790 if (this->encryption.key_size)
791 {
792 written += fprintf(stream, "-%d", this->encryption.key_size);
793 }
794 written += fprintf(stream, "/");
795 }
796
797 written += fprintf(stream, "%N", integrity_algorithm_names,
798 this->integrity.algorithm);
799 if (this->integrity.key_size)
800 {
801 written += fprintf(stream, "-%d", this->integrity.key_size);
802 }
803 written += fprintf(stream, ", rekeying: ");
804
805 /* calculate rekey times */
806 if (this->soft_lifetime)
807 {
808 rekeying = this->soft_lifetime - (now - this->install_time);
809 written += fprintf(stream, "%ds", rekeying);
810 }
811 else
812 {
813 written += fprintf(stream, "disabled");
814 }
815 }
816 }
817 iterator = this->policies->create_iterator(this->policies, TRUE);
818 while (iterator->iterate(iterator, (void**)&policy))
819 {
820 written += fprintf(stream, "\n%10s: %R===%R, last use (in/out/fwd): ",
821 this->name, policy->my_ts, policy->other_ts);
822
823 /* query policy times */
824 status = charon->kernel_interface->query_policy(charon->kernel_interface,
825 policy->other_ts, policy->my_ts, POLICY_IN, &use);
826 if (status == SUCCESS && use)
827 {
828 written += fprintf(stream, "%ds/", now - use);
829 }
830 else
831 {
832 written += fprintf(stream, "unused/");
833 }
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/", now - use);
839 }
840 else
841 {
842 written += fprintf(stream, "unused/");
843 }
844 status = charon->kernel_interface->query_policy(charon->kernel_interface,
845 policy->other_ts, policy->my_ts, POLICY_FWD, &use);
846 if (status == SUCCESS && use)
847 {
848 written += fprintf(stream, "%ds", now - use);
849 }
850 else
851 {
852 written += fprintf(stream, "unused");
853 }
854 }
855 iterator->destroy(iterator);
856 return written;
857 }
858
859 /**
860 * arginfo handler in printf()
861 */
862 static int print_arginfo(const struct printf_info *info, size_t n, int *argtypes)
863 {
864 if (n > 0)
865 {
866 argtypes[0] = PA_POINTER;
867 }
868 return 1;
869 }
870
871 /**
872 * register printf() handlers
873 */
874 static void __attribute__ ((constructor))print_register()
875 {
876 register_printf_function(CHILD_SA_PRINTF_SPEC, print, print_arginfo);
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, 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, 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, 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*,prf_plus_t*))add;
1101 this->public.update = (status_t(*)(child_sa_t*,proposal_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*))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*,void*))set_rekeying_transaction;
1108 this->public.get_rekeying_transaction = (void* (*)(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->rekeying_transaction = NULL;
1140
1141 return &this->public;
1142 }