ikev2: Fix spelling of routability
[strongswan.git] / src / libcharon / sa / task_manager.h
1 /*
2 * Copyright (C) 2013-2018 Tobias Brunner
3 * Copyright (C) 2006 Martin Willi
4 * HSR Hochschule fuer Technik Rapperswil
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 */
16
17 /**
18 * @defgroup task_manager task_manager
19 * @{ @ingroup sa
20 */
21
22 #ifndef TASK_MANAGER_H_
23 #define TASK_MANAGER_H_
24
25 typedef struct task_manager_t task_manager_t;
26 typedef enum task_queue_t task_queue_t;
27
28 #include <limits.h>
29
30 #include <library.h>
31 #include <encoding/message.h>
32 #include <sa/ike_sa.h>
33 #include <sa/task.h>
34
35 /**
36 * First retransmit timeout in seconds.
37 */
38 #define RETRANSMIT_TIMEOUT 4.0
39
40 /**
41 * Base which is raised to the power of the retransmission try.
42 */
43 #define RETRANSMIT_BASE 1.8
44
45 /**
46 * Number of retransmits done before giving up.
47 */
48 #define RETRANSMIT_TRIES 5
49
50 /**
51 * Maximum jitter in percent.
52 */
53 #define RETRANSMIT_JITTER_MAX 20
54
55 /**
56 * Interval for mobike routability checks in ms.
57 */
58 #define ROUTABILITY_CHECK_INTERVAL 2500
59
60 /**
61 * Number of routability checks before giving up
62 */
63 #define ROUTABILITY_CHECK_TRIES 10
64
65 /**
66 * Type of task queues the task manager uses to handle tasks
67 */
68 enum task_queue_t {
69 /** tasks currently active, initiated by us */
70 TASK_QUEUE_ACTIVE,
71 /** passive tasks initiated by the remote peer */
72 TASK_QUEUE_PASSIVE,
73 /** tasks queued for initiated, but not yet activated */
74 TASK_QUEUE_QUEUED,
75 };
76
77 /**
78 * The task manager, juggles task and handles message exchanges.
79 *
80 * On incoming requests, the task manager creates new tasks on demand and
81 * juggles the request through all available tasks. Each task inspects the
82 * request and adds payloads as necessary to the response.
83 * On outgoing requests, the task manager delivers the request through the tasks
84 * to build it, the response gets processed by each task to complete.
85 * The task manager has an internal Queue to store task which should get
86 * completed.
87 * For the initial IKE_SA setup, several tasks are queued: One for the
88 * unauthenticated IKE_SA setup, one for authentication, one for CHILD_SA setup
89 * and maybe one for virtual IP assignment.
90 * The task manager is also responsible for retransmission. It uses a backoff
91 * algorithm. The timeout is calculated using
92 * RETRANSMIT_TIMEOUT * (RETRANSMIT_BASE ** try).
93 * When try reaches RETRANSMIT_TRIES, retransmission is given up.
94 *
95 * Using an initial TIMEOUT of 4s, a BASE of 1.8, and 5 TRIES gives us:
96 * @verbatim
97 | relative | absolute
98 ---------------------------------------------------------
99 4s * (1.8 ** 0) = 4s 4s
100 4s * (1.8 ** 1) = 7s 11s
101 4s * (1.8 ** 2) = 13s 24s
102 4s * (1.8 ** 3) = 23s 47s
103 4s * (1.8 ** 4) = 42s 89s
104 4s * (1.8 ** 5) = 76s 165s
105
106 @endverbatim
107 * The peer is considered dead after 2min 45s when no reply comes in.
108 */
109 struct task_manager_t {
110
111 /**
112 * Process an incoming message.
113 *
114 * @param message message to add payloads to
115 * @return
116 * - DESTROY_ME if IKE_SA must be closed
117 * - SUCCESS otherwise
118 */
119 status_t (*process_message) (task_manager_t *this, message_t *message);
120
121 /**
122 * Initiate an exchange with the currently queued tasks.
123 */
124 status_t (*initiate) (task_manager_t *this);
125
126 /**
127 * Queue a task in the manager.
128 *
129 * @param task task to queue
130 */
131 void (*queue_task)(task_manager_t *this, task_t *task);
132
133 /**
134 * Queue a task in the manager, but delay its initiation for at least the
135 * given number of seconds.
136 *
137 * @param task task to queue
138 * @param delay minimum delay in s before initiating the task
139 */
140 void (*queue_task_delayed)(task_manager_t *this, task_t *task,
141 uint32_t delay);
142
143 /**
144 * Queue IKE_SA establishing tasks.
145 */
146 void (*queue_ike)(task_manager_t *this);
147
148 /**
149 * Queue IKE_SA rekey tasks.
150 */
151 void (*queue_ike_rekey)(task_manager_t *this);
152
153 /**
154 * Queue IKE_SA reauth tasks.
155 */
156 void (*queue_ike_reauth)(task_manager_t *this);
157
158 /**
159 * Queue MOBIKE task
160 *
161 * @param roam TRUE to switch to new address
162 * @param address TRUE to include address list update
163 */
164 void (*queue_mobike)(task_manager_t *this, bool roam, bool address);
165
166 /**
167 * Queue IKE_SA delete tasks.
168 */
169 void (*queue_ike_delete)(task_manager_t *this);
170
171 /**
172 * Queue CHILD_SA establishing tasks.
173 *
174 * @param cfg CHILD_SA config to establish
175 * @param reqid reqid to use for CHILD_SA
176 * @param tsi initiator traffic selector, if packet-triggered
177 * @param tsr responder traffic selector, if packet-triggered
178 */
179 void (*queue_child)(task_manager_t *this, child_cfg_t *cfg, uint32_t reqid,
180 traffic_selector_t *tsi, traffic_selector_t *tsr);
181
182 /**
183 * Queue CHILD_SA rekeying tasks.
184 *
185 * @param protocol CHILD_SA protocol, AH|ESP
186 * @param spi CHILD_SA SPI to rekey
187 */
188 void (*queue_child_rekey)(task_manager_t *this, protocol_id_t protocol,
189 uint32_t spi);
190
191 /**
192 * Queue CHILD_SA delete tasks.
193 *
194 * @param protocol CHILD_SA protocol, AH|ESP
195 * @param spi CHILD_SA SPI to rekey
196 * @param expired TRUE if SA already expired
197 */
198 void (*queue_child_delete)(task_manager_t *this, protocol_id_t protocol,
199 uint32_t spi, bool expired);
200
201 /**
202 * Queue liveness checking tasks.
203 */
204 void (*queue_dpd)(task_manager_t *this);
205
206 /**
207 * Retransmit a request if it hasn't been acknowledged yet.
208 *
209 * A return value of INVALID_STATE means that the message was already
210 * acknowledged and has not to be retransmitted. A return value of SUCCESS
211 * means retransmission was required and the message has been resent.
212 *
213 * @param message_id ID of the message to retransmit
214 * @return
215 * - INVALID_STATE if retransmission not required
216 * - SUCCESS if retransmission sent
217 */
218 status_t (*retransmit) (task_manager_t *this, uint32_t message_id);
219
220 /**
221 * Migrate all queued tasks from other to this.
222 *
223 * To rekey or reestablish an IKE_SA completely, all queued or active
224 * tasks should get migrated to the new IKE_SA.
225 *
226 * @param other manager which gives away its tasks
227 */
228 void (*adopt_tasks) (task_manager_t *this, task_manager_t *other);
229
230 /**
231 * Increment a message ID counter, in- or outbound.
232 *
233 * If a message is processed outside of the manager, this call increments
234 * the message ID counters of the task manager.
235 *
236 * @param initiate TRUE to increment the initiating ID
237 */
238 void (*incr_mid)(task_manager_t *this, bool initiate);
239
240 /**
241 * Get the current message ID counter, in- or outbound.
242 *
243 * @param initiate TRUE to get the initiating ID
244 * @return current message ID
245 */
246 uint32_t (*get_mid)(task_manager_t *this, bool initiate);
247
248 /**
249 * Reset message ID counters of the task manager.
250 *
251 * The IKEv2 protocol requires to restart exchanges with message IDs
252 * reset to zero (INVALID_KE_PAYLOAD, COOKIES, ...). The reset() method
253 * resets the message IDs and resets all active tasks using the migrate()
254 * method.
255 * Use a value of UINT_MAX to keep the current message ID.
256 * For IKEv1, the arguments do not set the message ID, but the DPD sequence
257 * number counters.
258 *
259 * @param initiate message ID / DPD seq to initiate exchanges (send)
260 * @param respond message ID / DPD seq to respond to exchanges (expect)
261 */
262 void (*reset)(task_manager_t *this, uint32_t initiate, uint32_t respond);
263
264 /**
265 * Check if we are currently waiting for a reply.
266 *
267 * @return TRUE if we are waiting, FALSE otherwise
268 */
269 bool (*busy) (task_manager_t *this);
270
271 /**
272 * Create an enumerator over tasks in a specific queue.
273 *
274 * @param queue queue to create an enumerator over
275 * @return enumerator over task_t
276 */
277 enumerator_t* (*create_task_enumerator)(task_manager_t *this,
278 task_queue_t queue);
279
280 /**
281 * Remove the task the given enumerator points to.
282 *
283 * @note This should be used with caution, in particular, for tasks in the
284 * active and passive queues.
285 *
286 * @param enumerator enumerator created with the method above
287 */
288 void (*remove_task)(task_manager_t *this, enumerator_t *enumerator);
289
290 /**
291 * Flush all tasks, regardless of the queue.
292 */
293 void (*flush)(task_manager_t *this);
294
295 /**
296 * Flush a queue, cancelling all tasks.
297 *
298 * @param queue queue to flush
299 */
300 void (*flush_queue)(task_manager_t *this, task_queue_t queue);
301
302 /**
303 * Destroy the task_manager_t.
304 */
305 void (*destroy) (task_manager_t *this);
306 };
307
308 /**
309 * Calculate total timeout of the retransmission mechanism.
310 *
311 * This is affected by modifications of retransmit_base, retransmit_timeout,
312 * retransmit_limit or retransmit_tries. The resulting value can then be used
313 * e.g. in kernel plugins to set the system's acquire timeout properly.
314 *
315 * @return calculated total retransmission timeout in seconds
316 */
317 u_int task_manager_total_retransmit_timeout();
318
319 /**
320 * Create a task manager instance for the correct IKE version.
321 *
322 * @param ike_sa IKE_SA to create a task manager for
323 * @return task manager implementation for IKE version
324 */
325 task_manager_t *task_manager_create(ike_sa_t *ike_sa);
326
327 #endif /** TASK_MANAGER_H_ @}*/