separated tncif_names from standard TCG TNC header files
[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 {
214 imc_state_t *state;
215
216 switch (new_state)
217 {
218 case TNC_CONNECTION_STATE_HANDSHAKE:
219 case TNC_CONNECTION_STATE_ACCESS_ALLOWED:
220 case TNC_CONNECTION_STATE_ACCESS_ISOLATED:
221 case TNC_CONNECTION_STATE_ACCESS_NONE:
222 state = find_connection(this, connection_id);
223 if (!state)
224 {
225 DBG1(DBG_IMC, "IMC %u \"%s\" has no state for Connection ID %u",
226 this->id, this->name, connection_id);
227 return TNC_RESULT_FATAL;
228 }
229 state->change_state(state, new_state);
230 DBG2(DBG_IMC, "IMC %u \"%s\" changed state of Connection ID %u to '%N'",
231 this->id, this->name, connection_id,
232 TNC_Connection_State_names, new_state);
233 break;
234 case TNC_CONNECTION_STATE_CREATE:
235 DBG1(DBG_IMC, "state '%N' should be handled by create_state()",
236 TNC_Connection_State_names, new_state);
237 return TNC_RESULT_FATAL;
238 case TNC_CONNECTION_STATE_DELETE:
239 DBG1(DBG_IMC, "state '%N' should be handled by delete_state()",
240 TNC_Connection_State_names, new_state);
241 return TNC_RESULT_FATAL;
242 default:
243 DBG1(DBG_IMC, "IMC %u \"%s\" was notified of unknown state %u "
244 "for Connection ID %u",
245 this->id, this->name, new_state, connection_id);
246 return TNC_RESULT_INVALID_PARAMETER;
247 }
248 return TNC_RESULT_SUCCESS;
249 }
250
251 METHOD(imc_agent_t, get_state, bool,
252 private_imc_agent_t *this, TNC_ConnectionID connection_id,
253 imc_state_t **state)
254 {
255 *state = find_connection(this, connection_id);
256 if (!*state)
257 {
258 DBG1(DBG_IMC, "IMC %u \"%s\" has no state for Connection ID %u",
259 this->id, this->name, connection_id);
260 return FALSE;
261 }
262 return TRUE;
263 }
264
265 METHOD(imc_agent_t, send_message, TNC_Result,
266 private_imc_agent_t *this, TNC_ConnectionID connection_id, chunk_t msg)
267 {
268 if (!this->send_message)
269 {
270 return TNC_RESULT_FATAL;
271 }
272 return this->send_message(this->id, connection_id, msg.ptr, msg.len,
273 this->type);
274 }
275
276 METHOD(imc_agent_t, receive_message, TNC_Result,
277 private_imc_agent_t *this, TNC_ConnectionID connection_id, chunk_t msg,
278 TNC_MessageType msg_type, pa_tnc_msg_t **pa_tnc_msg)
279 {
280 pa_tnc_msg_t *pa_msg, *error_msg;
281 pa_tnc_attr_t *error_attr;
282 enumerator_t *enumerator;
283 TNC_Result result;
284
285 DBG2(DBG_IMV, "IMC %u \"%s\" received message type 0x%08x for Connection ID %u",
286 this->id, this->name, msg_type, connection_id);
287
288 *pa_tnc_msg = NULL;
289 pa_msg = pa_tnc_msg_create_from_data(msg);
290
291 switch (pa_msg->process(pa_msg))
292 {
293 case SUCCESS:
294 *pa_tnc_msg = pa_msg;
295 break;
296 case VERIFY_ERROR:
297 if (!this->send_message)
298 {
299 /* TNCC doen't have a SendMessage() function */
300 return TNC_RESULT_FATAL;
301 }
302
303 /* build error message */
304 error_msg = pa_tnc_msg_create();
305 enumerator = pa_msg->create_error_enumerator(pa_msg);
306 while (enumerator->enumerate(enumerator, &error_attr))
307 {
308 error_msg->add_attribute(error_msg,
309 error_attr->get_ref(error_attr));
310 }
311 enumerator->destroy(enumerator);
312 error_msg->build(error_msg);
313
314 /* send error message */
315 msg = error_msg->get_encoding(error_msg);
316 result = this->send_message(this->id, connection_id,
317 msg.ptr, msg.len, msg_type);
318
319 /* clean up */
320 error_msg->destroy(error_msg);
321 pa_msg->destroy(pa_msg);
322 return result;
323 case FAILED:
324 default:
325 pa_msg->destroy(pa_msg);
326 return TNC_RESULT_FATAL;
327 }
328 return TNC_RESULT_SUCCESS;
329 }
330
331 METHOD(imc_agent_t, destroy, void,
332 private_imc_agent_t *this)
333 {
334 DBG1(DBG_IMC, "IMC %u \"%s\" terminated", this->id, this->name);
335 this->connections->destroy_function(this->connections, free);
336 this->connection_lock->destroy(this->connection_lock);
337 free(this);
338
339 /* decrease the reference count or terminate */
340 libimcv_deinit();
341 }
342
343 /**
344 * Described in header.
345 */
346 imc_agent_t *imc_agent_create(const char *name,
347 pen_t vendor_id, u_int32_t subtype,
348 TNC_IMCID id, TNC_Version *actual_version)
349 {
350 private_imc_agent_t *this;
351
352 /* initialize or increase the reference count */
353 if (!libimcv_init())
354 {
355 return NULL;
356 }
357
358 INIT(this,
359 .public = {
360 .bind_functions = _bind_functions,
361 .create_state = _create_state,
362 .delete_state = _delete_state,
363 .change_state = _change_state,
364 .get_state = _get_state,
365 .send_message = _send_message,
366 .receive_message = _receive_message,
367 .destroy = _destroy,
368 },
369 .name = name,
370 .type = (vendor_id << 8) | (subtype && 0xff),
371 .id = id,
372 .connections = linked_list_create(),
373 .connection_lock = rwlock_create(RWLOCK_TYPE_DEFAULT),
374 );
375
376 *actual_version = TNC_IFIMC_VERSION_1;
377 DBG1(DBG_IMC, "IMC %u \"%s\" initialized", this->id, this->name);
378
379 return &this->public;
380 }
381