task: Add optional pre_process() method
[strongswan.git] / src / libcharon / sa / task.h
1 /*
2 * Copyright (C) 2007-2015 Tobias Brunner
3 * Copyright (C) 2006 Martin Willi
4 * 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 task
19 * @{ @ingroup sa
20 */
21
22 #ifndef TASK_H_
23 #define TASK_H_
24
25 #include <utils/utils.h>
26
27 typedef enum task_type_t task_type_t;
28 typedef struct task_t task_t;
29
30 #include <library.h>
31 #include <sa/ike_sa.h>
32 #include <encoding/message.h>
33
34 /**
35 * Different kinds of tasks.
36 */
37 enum task_type_t {
38 /** establish an unauthenticated IKE_SA */
39 TASK_IKE_INIT,
40 /** detect NAT situation */
41 TASK_IKE_NATD,
42 /** handle MOBIKE stuff */
43 TASK_IKE_MOBIKE,
44 /** authenticate the initiated IKE_SA */
45 TASK_IKE_AUTH,
46 /** AUTH_LIFETIME negotiation, RFC4478 */
47 TASK_IKE_AUTH_LIFETIME,
48 /** certificate processing before authentication (certreqs, cert parsing) */
49 TASK_IKE_CERT_PRE,
50 /** certificate processing after authentication (certs payload generation) */
51 TASK_IKE_CERT_POST,
52 /** Configuration payloads, virtual IP and such */
53 TASK_IKE_CONFIG,
54 /** rekey an IKE_SA */
55 TASK_IKE_REKEY,
56 /** reestablish a complete IKE_SA, break-before-make */
57 TASK_IKE_REAUTH,
58 /** completion task for make-before-break IKE_SA re-authentication */
59 TASK_IKE_REAUTH_COMPLETE,
60 /** redirect an active IKE_SA */
61 TASK_IKE_REDIRECT,
62 /** delete an IKE_SA */
63 TASK_IKE_DELETE,
64 /** liveness check */
65 TASK_IKE_DPD,
66 /** Vendor ID processing */
67 TASK_IKE_VENDOR,
68 #ifdef ME
69 /** handle ME stuff */
70 TASK_IKE_ME,
71 #endif /* ME */
72 /** establish a CHILD_SA within an IKE_SA */
73 TASK_CHILD_CREATE,
74 /** delete an established CHILD_SA */
75 TASK_CHILD_DELETE,
76 /** rekey a CHILD_SA */
77 TASK_CHILD_REKEY,
78 /** IKEv1 main mode */
79 TASK_MAIN_MODE,
80 /** IKEv1 aggressive mode */
81 TASK_AGGRESSIVE_MODE,
82 /** IKEv1 informational exchange */
83 TASK_INFORMATIONAL,
84 /** IKEv1 delete using an informational */
85 TASK_ISAKMP_DELETE,
86 /** IKEv1 XAUTH authentication */
87 TASK_XAUTH,
88 /** IKEv1 Mode Config */
89 TASK_MODE_CONFIG,
90 /** IKEv1 quick mode */
91 TASK_QUICK_MODE,
92 /** IKEv1 delete of a quick mode SA */
93 TASK_QUICK_DELETE,
94 /** IKEv1 vendor ID payload handling */
95 TASK_ISAKMP_VENDOR,
96 /** IKEv1 NAT detection */
97 TASK_ISAKMP_NATD,
98 /** IKEv1 DPD */
99 TASK_ISAKMP_DPD,
100 /** IKEv1 pre-authentication certificate handling */
101 TASK_ISAKMP_CERT_PRE,
102 /** IKEv1 post-authentication certificate handling */
103 TASK_ISAKMP_CERT_POST,
104 };
105
106 /**
107 * enum names for task_type_t.
108 */
109 extern enum_name_t *task_type_names;
110
111 /**
112 * Interface for a task, an operation handled within exchanges.
113 *
114 * A task is an elemantary operation. It may be handled by a single or by
115 * multiple exchanges. An exchange may even complete multiple tasks.
116 * A task has a build() and an process() operation. The build() operation
117 * creates payloads and adds it to the message. The process() operation
118 * inspects a message and handles its payloads. An initiator of an exchange
119 * first calls build() to build the request, and processes the response message
120 * with the process() method.
121 * A responder does the opposite; it calls process() first to handle an incoming
122 * request and secondly calls build() to build an appropriate response.
123 * Both methods return either SUCCESS, NEED_MORE or FAILED. A SUCCESS indicates
124 * that the task completed, even when the task completed unsuccessfully. The
125 * manager then removes the task from the list. A NEED_MORE is returned when
126 * the task needs further build()/process() calls to complete, the manager
127 * leaves the taks in the queue. A returned FAILED indicates a critical failure.
128 * The manager closes the IKE_SA whenever a task returns FAILED.
129 */
130 struct task_t {
131
132 /**
133 * Build a request or response message for this task.
134 *
135 * @param message message to add payloads to
136 * @return
137 * - FAILED if a critical error occurred
138 * - DESTROY_ME if IKE_SA has been properly deleted
139 * - NEED_MORE if another call to build/process needed
140 * - ALREADY_DONE to cancel task processing
141 * - SUCCESS if task completed
142 */
143 status_t (*build) (task_t *this, message_t *message);
144
145 /**
146 * Process a request or response message for this task.
147 *
148 * @param message message to read payloads from
149 * @return
150 * - FAILED if a critical error occurred
151 * - DESTROY_ME if IKE_SA has been properly deleted
152 * - NEED_MORE if another call to build/process needed
153 * - ALREADY_DONE to cancel task processing
154 * - SUCCESS if task completed
155 */
156 status_t (*process) (task_t *this, message_t *message);
157
158 /**
159 * Verify a message before processing it (optional to implement by tasks).
160 *
161 * @param message message to verify
162 * @return
163 * - FAILED if verification is not successful, the
164 * message will be silently discarded
165 * - DESTROY_ME if IKE_SA has to be destroyed
166 * - SUCCESS if verification is successful
167 */
168 status_t (*pre_process) (task_t *this, message_t *message);
169
170 /**
171 * Get the type of the task implementation.
172 */
173 task_type_t (*get_type) (task_t *this);
174
175 /**
176 * Migrate a task to a new IKE_SA.
177 *
178 * After migrating a task, it goes back to a state where it can be
179 * used again to initate an exchange. This is useful when a task
180 * has to get migrated to a new IKE_SA.
181 * A special usage is when a INVALID_KE_PAYLOAD is received. A call
182 * to reset resets the task, but uses another DH group for the next
183 * try.
184 * The ike_sa is the new IKE_SA this task belongs to and operates on.
185 *
186 * @param ike_sa new IKE_SA this task works for
187 */
188 void (*migrate) (task_t *this, ike_sa_t *ike_sa);
189
190 /**
191 * Destroys a task_t object.
192 */
193 void (*destroy) (task_t *this);
194 };
195
196 #endif /** TASK_H_ @}*/