refactoring of change_state()
[strongswan.git] / src / libimcv / imc / imc_agent.c
1 /*
2 * Copyright (C) 2011 Andreas Steffen, HSR Hochschule fuer Technik Rapperswil
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * for more details.
13 */
14
15 #include "imcv.h"
16 #include "imc_agent.h"
17
18 #include <tncif_names.h>
19
20 #include <debug.h>
21 #include <utils/linked_list.h>
22 #include <threading/rwlock.h>
23
24 typedef struct private_imc_agent_t private_imc_agent_t;
25
26 /**
27 * Private data of an imc_agent_t object.
28 */
29 struct private_imc_agent_t {
30
31 /**
32 * Public members of imc_agent_t
33 */
34 imc_agent_t public;
35
36 /**
37 * name of IMC
38 */
39 const char *name;
40
41 /**
42 * message type of IMC
43 */
44 TNC_MessageType type;
45
46 /**
47 * ID of IMC as assigned by TNCC
48 */
49 TNC_IMCID id;
50
51 /**
52 * list of TNCC connection entries
53 */
54 linked_list_t *connections;
55
56 /**
57 * rwlock to lock TNCS connection entries
58 */
59 rwlock_t *connection_lock;
60
61 /**
62 * Inform a TNCS about the set of message types the IMC is able to receive
63 *
64 * @param imc_id IMC ID assigned by TNCC
65 * @param supported_types list of supported message types
66 * @param type_count number of list elements
67 * @return TNC result code
68 */
69 TNC_Result (*report_message_types)(TNC_IMCID imc_id,
70 TNC_MessageTypeList supported_types,
71 TNC_UInt32 type_count);
72
73 /**
74 * Call when an IMC-IMC message is to be sent
75 *
76 * @param imc_id IMC ID assigned by TNCC
77 * @param connection_id network connection ID assigned by TNCC
78 * @param msg message to send
79 * @param msg_len message length in bytes
80 * @param msg_type message type
81 * @return TNC result code
82 */
83 TNC_Result (*send_message)(TNC_IMCID imc_id,
84 TNC_ConnectionID connection_id,
85 TNC_BufferReference msg,
86 TNC_UInt32 msg_len,
87 TNC_MessageType msg_type);
88 };
89
90 METHOD(imc_agent_t, bind_functions, TNC_Result,
91 private_imc_agent_t *this, TNC_TNCC_BindFunctionPointer bind_function)
92 {
93 if (!bind_function)
94 {
95 DBG1(DBG_IMC, "TNC client failed to provide bind function");
96 return TNC_RESULT_INVALID_PARAMETER;
97 }
98 if (bind_function(this->id, "TNC_TNCC_ReportMessageTypes",
99 (void**)&this->report_message_types) != TNC_RESULT_SUCCESS)
100 {
101 this->report_message_types = NULL;
102 }
103 if (bind_function(this->id, "TNC_TNCC_RequestHandshakeRetry",
104 (void**)&this->public.request_handshake_retry) != TNC_RESULT_SUCCESS)
105 {
106 this->public.request_handshake_retry = NULL;
107 }
108 if (bind_function(this->id, "TNC_TNCC_SendMessage",
109 (void**)&this->send_message) != TNC_RESULT_SUCCESS)
110 {
111 this->send_message = NULL;
112 }
113 DBG2(DBG_IMC, "IMC %u \"%s\" provided with bind function",
114 this->id, this->name);
115
116 if (this->report_message_types)
117 {
118 this->report_message_types(this->id, &this->type, 1);
119 }
120 return TNC_RESULT_SUCCESS;
121 }
122
123 /**
124 * finds a connection state based on its Connection ID
125 */
126 static imc_state_t* find_connection(private_imc_agent_t *this,
127 TNC_ConnectionID id)
128 {
129 enumerator_t *enumerator;
130 imc_state_t *state, *found = NULL;
131
132 this->connection_lock->read_lock(this->connection_lock);
133 enumerator = this->connections->create_enumerator(this->connections);
134 while (enumerator->enumerate(enumerator, &state))
135 {
136 if (id == state->get_connection_id(state))
137 {
138 found = state;
139 break;
140 }
141 }
142 enumerator->destroy(enumerator);
143 this->connection_lock->unlock(this->connection_lock);
144
145 return found;
146 }
147
148 /**
149 * delete a connection state with a given Connection ID
150 */
151 static bool delete_connection(private_imc_agent_t *this, TNC_ConnectionID id)
152 {
153 enumerator_t *enumerator;
154 imc_state_t *state;
155 bool found = FALSE;
156
157 this->connection_lock->write_lock(this->connection_lock);
158 enumerator = this->connections->create_enumerator(this->connections);
159 while (enumerator->enumerate(enumerator, &state))
160 {
161 if (id == state->get_connection_id(state))
162 {
163 found = TRUE;
164 state->destroy(state);
165 this->connections->remove_at(this->connections, enumerator);
166 break;
167 }
168 }
169 enumerator->destroy(enumerator);
170 this->connection_lock->unlock(this->connection_lock);
171
172 return found;
173 }
174
175 METHOD(imc_agent_t, create_state, TNC_Result,
176 private_imc_agent_t *this, imc_state_t *state)
177 {
178 TNC_ConnectionID connection_id;
179
180 connection_id = state->get_connection_id(state);
181 if (find_connection(this, connection_id))
182 {
183 DBG1(DBG_IMC, "IMC %u \"%s\" already created a state for Connection ID %u",
184 this->id, this->name, connection_id);
185 state->destroy(state);
186 return TNC_RESULT_OTHER;
187 }
188 this->connection_lock->write_lock(this->connection_lock);
189 this->connections->insert_last(this->connections, state);
190 this->connection_lock->unlock(this->connection_lock);
191 DBG2(DBG_IMC, "IMC %u \"%s\" created a state for Connection ID %u",
192 this->id, this->name, connection_id);
193 return TNC_RESULT_SUCCESS;
194 }
195
196 METHOD(imc_agent_t, delete_state, TNC_Result,
197 private_imc_agent_t *this, TNC_ConnectionID connection_id)
198 {
199 if (!delete_connection(this, connection_id))
200 {
201 DBG1(DBG_IMC, "IMC %u \"%s\" has no state for Connection ID %u",
202 this->id, this->name, connection_id);
203 return TNC_RESULT_FATAL;
204 }
205 DBG2(DBG_IMC, "IMC %u \"%s\" deleted the state of Connection ID %u",
206 this->id, this->name, connection_id);
207 return TNC_RESULT_SUCCESS;
208 }
209
210 METHOD(imc_agent_t, change_state, TNC_Result,
211 private_imc_agent_t *this, TNC_ConnectionID connection_id,
212 TNC_ConnectionState new_state,
213 imc_state_t **state_p)
214 {
215 imc_state_t *state;
216
217 switch (new_state)
218 {
219 case TNC_CONNECTION_STATE_HANDSHAKE:
220 case TNC_CONNECTION_STATE_ACCESS_ALLOWED:
221 case TNC_CONNECTION_STATE_ACCESS_ISOLATED:
222 case TNC_CONNECTION_STATE_ACCESS_NONE:
223 state = find_connection(this, connection_id);
224
225 if (!state)
226 {
227 DBG1(DBG_IMC, "IMC %u \"%s\" has no state for Connection ID %u",
228 this->id, this->name, connection_id);
229 return TNC_RESULT_FATAL;
230 }
231 state->change_state(state, new_state);
232 DBG2(DBG_IMC, "IMC %u \"%s\" changed state of Connection ID %u to '%N'",
233 this->id, this->name, connection_id,
234 TNC_Connection_State_names, new_state);
235 if (state_p)
236 {
237 *state_p = state;
238 }
239 break;
240 case TNC_CONNECTION_STATE_CREATE:
241 DBG1(DBG_IMC, "state '%N' should be handled by create_state()",
242 TNC_Connection_State_names, new_state);
243 return TNC_RESULT_FATAL;
244 case TNC_CONNECTION_STATE_DELETE:
245 DBG1(DBG_IMC, "state '%N' should be handled by delete_state()",
246 TNC_Connection_State_names, new_state);
247 return TNC_RESULT_FATAL;
248 default:
249 DBG1(DBG_IMC, "IMC %u \"%s\" was notified of unknown state %u "
250 "for Connection ID %u",
251 this->id, this->name, new_state, connection_id);
252 return TNC_RESULT_INVALID_PARAMETER;
253 }
254 return TNC_RESULT_SUCCESS;
255 }
256
257 METHOD(imc_agent_t, get_state, bool,
258 private_imc_agent_t *this, TNC_ConnectionID connection_id,
259 imc_state_t **state)
260 {
261 *state = find_connection(this, connection_id);
262 if (!*state)
263 {
264 DBG1(DBG_IMC, "IMC %u \"%s\" has no state for Connection ID %u",
265 this->id, this->name, connection_id);
266 return FALSE;
267 }
268 return TRUE;
269 }
270
271 METHOD(imc_agent_t, send_message, TNC_Result,
272 private_imc_agent_t *this, TNC_ConnectionID connection_id, chunk_t msg)
273 {
274 if (!this->send_message)
275 {
276 return TNC_RESULT_FATAL;
277 }
278 return this->send_message(this->id, connection_id, msg.ptr, msg.len,
279 this->type);
280 }
281
282 METHOD(imc_agent_t, receive_message, TNC_Result,
283 private_imc_agent_t *this, TNC_ConnectionID connection_id, chunk_t msg,
284 TNC_MessageType msg_type, pa_tnc_msg_t **pa_tnc_msg)
285 {
286 pa_tnc_msg_t *pa_msg, *error_msg;
287 pa_tnc_attr_t *error_attr;
288 enumerator_t *enumerator;
289 TNC_Result result;
290
291 DBG2(DBG_IMV, "IMC %u \"%s\" received message type 0x%08x for Connection ID %u",
292 this->id, this->name, msg_type, connection_id);
293
294 *pa_tnc_msg = NULL;
295 pa_msg = pa_tnc_msg_create_from_data(msg);
296
297 switch (pa_msg->process(pa_msg))
298 {
299 case SUCCESS:
300 *pa_tnc_msg = pa_msg;
301 break;
302 case VERIFY_ERROR:
303 if (!this->send_message)
304 {
305 /* TNCC doen't have a SendMessage() function */
306 return TNC_RESULT_FATAL;
307 }
308
309 /* build error message */
310 error_msg = pa_tnc_msg_create();
311 enumerator = pa_msg->create_error_enumerator(pa_msg);
312 while (enumerator->enumerate(enumerator, &error_attr))
313 {
314 error_msg->add_attribute(error_msg,
315 error_attr->get_ref(error_attr));
316 }
317 enumerator->destroy(enumerator);
318 error_msg->build(error_msg);
319
320 /* send error message */
321 msg = error_msg->get_encoding(error_msg);
322 result = this->send_message(this->id, connection_id,
323 msg.ptr, msg.len, msg_type);
324
325 /* clean up */
326 error_msg->destroy(error_msg);
327 pa_msg->destroy(pa_msg);
328 return result;
329 case FAILED:
330 default:
331 pa_msg->destroy(pa_msg);
332 return TNC_RESULT_FATAL;
333 }
334 return TNC_RESULT_SUCCESS;
335 }
336
337 METHOD(imc_agent_t, destroy, void,
338 private_imc_agent_t *this)
339 {
340 DBG1(DBG_IMC, "IMC %u \"%s\" terminated", this->id, this->name);
341 this->connections->destroy_function(this->connections, free);
342 this->connection_lock->destroy(this->connection_lock);
343 free(this);
344
345 /* decrease the reference count or terminate */
346 libimcv_deinit();
347 }
348
349 /**
350 * Described in header.
351 */
352 imc_agent_t *imc_agent_create(const char *name,
353 pen_t vendor_id, u_int32_t subtype,
354 TNC_IMCID id, TNC_Version *actual_version)
355 {
356 private_imc_agent_t *this;
357
358 /* initialize or increase the reference count */
359 if (!libimcv_init())
360 {
361 return NULL;
362 }
363
364 INIT(this,
365 .public = {
366 .bind_functions = _bind_functions,
367 .create_state = _create_state,
368 .delete_state = _delete_state,
369 .change_state = _change_state,
370 .get_state = _get_state,
371 .send_message = _send_message,
372 .receive_message = _receive_message,
373 .destroy = _destroy,
374 },
375 .name = name,
376 .type = (vendor_id << 8) | (subtype && 0xff),
377 .id = id,
378 .connections = linked_list_create(),
379 .connection_lock = rwlock_create(RWLOCK_TYPE_DEFAULT),
380 );
381
382 *actual_version = TNC_IFIMC_VERSION_1;
383 DBG1(DBG_IMC, "IMC %u \"%s\" initialized", this->id, this->name);
384
385 return &this->public;
386 }
387