Moved liveness checking task creation to protocol specific task manager
[strongswan.git] / src / libcharon / sa / task_manager.h
1 /*
2 * Copyright (C) 2006 Martin Willi
3 * Hochschule fuer Technik Rapperswil
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 /**
17 * @defgroup task_manager task_manager
18 * @{ @ingroup sa
19 */
20
21 #ifndef TASK_MANAGER_H_
22 #define TASK_MANAGER_H_
23
24 typedef struct task_manager_t task_manager_t;
25 typedef enum task_queue_t task_queue_t;
26
27 #include <limits.h>
28
29 #include <library.h>
30 #include <encoding/message.h>
31 #include <sa/ike_sa.h>
32 #include <sa/task.h>
33
34 /**
35 * First retransmit timeout in seconds.
36 */
37 #define RETRANSMIT_TIMEOUT 4.0
38
39 /**
40 * Base which is raised to the power of the retransmission try.
41 */
42 #define RETRANSMIT_BASE 1.8
43
44 /**
45 * Number of retransmits done before giving up.
46 */
47 #define RETRANSMIT_TRIES 5
48
49 /**
50 * Interval for mobike routability checks in ms.
51 */
52 #define ROUTEABILITY_CHECK_INTERVAL 2500
53
54 /**
55 * Number of routability checks before giving up
56 */
57 #define ROUTEABILITY_CHECK_TRIES 10
58
59 /**
60 * Type of task queues the task manager uses to handle tasks
61 */
62 enum task_queue_t {
63 /** tasks currently active, initiated by us */
64 TASK_QUEUE_ACTIVE,
65 /** passive tasks initiated by the remote peer */
66 TASK_QUEUE_PASSIVE,
67 /** tasks queued for initiated, but not yet activated */
68 TASK_QUEUE_QUEUED,
69 };
70
71 /**
72 * The task manager, juggles task and handles message exchanges.
73 *
74 * On incoming requests, the task manager creates new tasks on demand and
75 * juggles the request through all available tasks. Each task inspects the
76 * request and adds payloads as necessary to the response.
77 * On outgoing requests, the task manager delivers the request through the tasks
78 * to build it, the response gets processed by each task to complete.
79 * The task manager has an internal Queue to store task which should get
80 * completed.
81 * For the initial IKE_SA setup, several tasks are queued: One for the
82 * unauthenticated IKE_SA setup, one for authentication, one for CHILD_SA setup
83 * and maybe one for virtual IP assignement.
84 * The task manager is also responsible for retransmission. It uses a backoff
85 * algorithm. The timeout is calculated using
86 * RETRANSMIT_TIMEOUT * (RETRANSMIT_BASE ** try).
87 * When try reaches RETRANSMIT_TRIES, retransmission is given up.
88 *
89 * Using an initial TIMEOUT of 4s, a BASE of 1.8, and 5 TRIES gives us:
90 * @verbatim
91 | relative | absolute
92 ---------------------------------------------------------
93 4s * (1.8 ** 0) = 4s 4s
94 4s * (1.8 ** 1) = 7s 11s
95 4s * (1.8 ** 2) = 13s 24s
96 4s * (1.8 ** 3) = 23s 47s
97 4s * (1.8 ** 4) = 42s 89s
98 4s * (1.8 ** 5) = 76s 165s
99
100 @endverbatim
101 * The peer is considered dead after 2min 45s when no reply comes in.
102 */
103 struct task_manager_t {
104
105 /**
106 * Process an incoming message.
107 *
108 * @param message message to add payloads to
109 * @return
110 * - DESTROY_ME if IKE_SA must be closed
111 * - SUCCESS otherwise
112 */
113 status_t (*process_message) (task_manager_t *this, message_t *message);
114
115 /**
116 * Initiate an exchange with the currently queued tasks.
117 */
118 status_t (*initiate) (task_manager_t *this);
119
120 /**
121 * Queue a task in the manager.
122 *
123 * @param task task to queue
124 */
125 void (*queue_task) (task_manager_t *this, task_t *task);
126
127 /**
128 * Queue liveness checking tasks.
129 */
130 void (*queue_dpd)(task_manager_t *this);
131
132 /**
133 * Retransmit a request if it hasn't been acknowledged yet.
134 *
135 * A return value of INVALID_STATE means that the message was already
136 * acknowledged and has not to be retransmitted. A return value of SUCCESS
137 * means retransmission was required and the message has been resent.
138 *
139 * @param message_id ID of the message to retransmit
140 * @return
141 * - INVALID_STATE if retransmission not required
142 * - SUCCESS if retransmission sent
143 */
144 status_t (*retransmit) (task_manager_t *this, u_int32_t message_id);
145
146 /**
147 * Migrate all tasks from other to this.
148 *
149 * To rekey or reestablish an IKE_SA completely, all queued or active
150 * tasks should get migrated to the new IKE_SA.
151 *
152 * @param other manager which gives away its tasks
153 */
154 void (*adopt_tasks) (task_manager_t *this, task_manager_t *other);
155
156 /**
157 * Increment a message ID counter, in- or outbound.
158 *
159 * If a message is processed outside of the manager, this call increments
160 * the message ID counters of the task manager.
161 *
162 * @param inititate TRUE to increment the initiating ID
163 */
164 void (*incr_mid)(task_manager_t *this, bool initiate);
165
166 /**
167 * Reset message ID counters of the task manager.
168 *
169 * The IKEv2 protocol requires to restart exchanges with message IDs
170 * reset to zero (INVALID_KE_PAYLOAD, COOKIES, ...). The reset() method
171 * resets the message IDs and resets all active tasks using the migrate()
172 * method.
173 * Use a value of UINT_MAX to keep the current message ID.
174 *
175 * @param initiate message ID to initiate exchanges (send)
176 * @param respond message ID to respond to exchanges (expect)
177 */
178 void (*reset) (task_manager_t *this, u_int32_t initiate, u_int32_t respond);
179
180 /**
181 * Check if we are currently waiting for a reply.
182 *
183 * @return TRUE if we are waiting, FALSE otherwise
184 */
185 bool (*busy) (task_manager_t *this);
186
187 /**
188 * Create an enumerator over tasks in a specific queue.
189 *
190 * @param queue queue to create an enumerator over
191 * @return enumerator over task_t
192 */
193 enumerator_t* (*create_task_enumerator)(task_manager_t *this,
194 task_queue_t queue);
195
196 /**
197 * Destroy the task_manager_t.
198 */
199 void (*destroy) (task_manager_t *this);
200 };
201
202 /**
203 * Create a task manager instance for the correct IKE version.
204 *
205 * @param ike_sa IKE_SA to create a task manager for
206 * @return task manager implementation for IKE version
207 */
208 task_manager_t *task_manager_create(ike_sa_t *ike_sa);
209
210 #endif /** TASK_MANAGER_H_ @}*/