0f823769a20ba7c8051506bb3cb4adc8cfef5ac9
[strongswan.git] / src / libcharon / processing / jobs / dpd_timeout_job.c
1 /*
2 * Copyright (C) 2012 Martin Willi
3 * Copyright (C) 2012 revosec AG
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include <stdlib.h>
17
18 #include "dpd_timeout_job.h"
19
20 #include <sa/ike_sa.h>
21 #include <daemon.h>
22
23
24 typedef struct private_dpd_timeout_job_t private_dpd_timeout_job_t;
25
26 /**
27 * Private data
28 */
29 struct private_dpd_timeout_job_t {
30
31 /**
32 * public dpd_timeout_job_t interface
33 */
34 dpd_timeout_job_t public;
35
36 /**
37 * IKE_SA identifier
38 */
39 ike_sa_id_t *ike_sa_id;
40
41 /**
42 * Timestamp of first DPD check
43 */
44 time_t check;
45 };
46
47 METHOD(job_t, destroy, void,
48 private_dpd_timeout_job_t *this)
49 {
50 this->ike_sa_id->destroy(this->ike_sa_id);
51 free(this);
52 }
53
54 METHOD(job_t, execute, job_requeue_t,
55 private_dpd_timeout_job_t *this)
56 {
57 time_t use_time, current;
58 enumerator_t *enumerator;
59 child_sa_t *child_sa;
60 ike_sa_t *ike_sa;
61
62 ike_sa = charon->ike_sa_manager->checkout(charon->ike_sa_manager,
63 this->ike_sa_id);
64 if (ike_sa)
65 {
66 use_time = ike_sa->get_statistic(ike_sa, STAT_INBOUND);
67
68 enumerator = ike_sa->create_child_sa_enumerator(ike_sa);
69 while (enumerator->enumerate(enumerator, &child_sa))
70 {
71 child_sa->get_usestats(child_sa, TRUE, &current, NULL, NULL);
72 use_time = max(use_time, current);
73 }
74 enumerator->destroy(enumerator);
75
76 /* check if no incoming packet during timeout, reestablish SA */
77 if (use_time < this->check)
78 {
79 DBG1(DBG_JOB, "DPD check timed out, enforcing DPD action");
80 charon->bus->ike_updown(charon->bus, ike_sa, FALSE);
81 ike_sa->reestablish(ike_sa);
82 charon->ike_sa_manager->checkin_and_destroy(charon->ike_sa_manager,
83 ike_sa);
84 }
85 else
86 {
87 charon->ike_sa_manager->checkin(charon->ike_sa_manager, ike_sa);
88 }
89 }
90 return JOB_REQUEUE_NONE;
91 }
92
93 METHOD(job_t, get_priority, job_priority_t,
94 private_dpd_timeout_job_t *this)
95 {
96 return JOB_PRIO_HIGH;
97 }
98
99 /*
100 * Described in header
101 */
102 dpd_timeout_job_t *dpd_timeout_job_create(ike_sa_id_t *ike_sa_id)
103 {
104 private_dpd_timeout_job_t *this;
105
106 INIT(this,
107 .public = {
108 .job_interface = {
109 .execute = _execute,
110 .get_priority = _get_priority,
111 .destroy = _destroy,
112 },
113 },
114 .ike_sa_id = ike_sa_id->clone(ike_sa_id),
115 .check = time_monotonic(NULL),
116 );
117
118 return &this->public;
119 }