implemented ReportMessageTypesLong
[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 vendor ID of IMC
43 */
44 TNC_VendorID vendor_id;
45
46 /**
47 * message subtype of IMC
48 */
49 TNC_MessageSubtype subtype;
50
51 /**
52 * ID of IMC as assigned by TNCC
53 */
54 TNC_IMCID id;
55
56 /**
57 * list of TNCC connection entries
58 */
59 linked_list_t *connections;
60
61 /**
62 * rwlock to lock TNCC connection entries
63 */
64 rwlock_t *connection_lock;
65
66 /**
67 * Inform a TNCC about the set of message types the IMC is able to receive
68 *
69 * @param imc_id IMC ID assigned by TNCC
70 * @param supported_types list of supported message types
71 * @param type_count number of list elements
72 * @return TNC result code
73 */
74 TNC_Result (*report_message_types)(TNC_IMCID imc_id,
75 TNC_MessageTypeList supported_types,
76 TNC_UInt32 type_count);
77
78 /**
79 * Inform a TNCC about the set of message types the IMC is able to receive
80 *
81 * @param imc_id IMC ID assigned by TNCC
82 * @param supported_vids list of supported message vendor IDs
83 * @param supported_subtypes list of supported message subtypes
84 * @param type_count number of list elements
85 * @return TNC result code
86 */
87 TNC_Result (*report_message_types_long)(TNC_IMCID imc_id,
88 TNC_VendorIDList supported_vids,
89 TNC_MessageSubtypeList supported_subtypes,
90 TNC_UInt32 type_count);
91
92 /**
93 * Call when an IMC-IMC message is to be sent
94 *
95 * @param imc_id IMC ID assigned by TNCC
96 * @param connection_id network connection ID assigned by TNCC
97 * @param msg message to send
98 * @param msg_len message length in bytes
99 * @param msg_type message type
100 * @return TNC result code
101 */
102 TNC_Result (*send_message)(TNC_IMCID imc_id,
103 TNC_ConnectionID connection_id,
104 TNC_BufferReference msg,
105 TNC_UInt32 msg_len,
106 TNC_MessageType msg_type);
107 };
108
109 METHOD(imc_agent_t, bind_functions, TNC_Result,
110 private_imc_agent_t *this, TNC_TNCC_BindFunctionPointer bind_function)
111 {
112 if (!bind_function)
113 {
114 DBG1(DBG_IMC, "TNC client failed to provide bind function");
115 return TNC_RESULT_INVALID_PARAMETER;
116 }
117 if (bind_function(this->id, "TNC_TNCC_ReportMessageTypes",
118 (void**)&this->report_message_types) != TNC_RESULT_SUCCESS)
119 {
120 this->report_message_types = NULL;
121 }
122 if (bind_function(this->id, "TNC_TNCC_ReportMessageTypesLong",
123 (void**)&this->report_message_types_long) != TNC_RESULT_SUCCESS)
124 {
125 this->report_message_types_long = NULL;
126 }
127 if (bind_function(this->id, "TNC_TNCC_RequestHandshakeRetry",
128 (void**)&this->public.request_handshake_retry) != TNC_RESULT_SUCCESS)
129 {
130 this->public.request_handshake_retry = NULL;
131 }
132 if (bind_function(this->id, "TNC_TNCC_SendMessage",
133 (void**)&this->send_message) != TNC_RESULT_SUCCESS)
134 {
135 this->send_message = NULL;
136 }
137 DBG2(DBG_IMC, "IMC %u \"%s\" provided with bind function",
138 this->id, this->name);
139
140 if (this->report_message_types_long)
141 {
142 this->report_message_types_long(this->id, &this->vendor_id,
143 &this->subtype, 1);
144 }
145 else if (this->report_message_types &&
146 this->vendor_id <= TNC_VENDORID_ANY &&
147 this->subtype <= TNC_SUBTYPE_ANY)
148 {
149 TNC_MessageType type;
150
151 type = (this->vendor_id << 8) | this->subtype;
152 this->report_message_types(this->id, &type, 1);
153 }
154 return TNC_RESULT_SUCCESS;
155 }
156
157 /**
158 * finds a connection state based on its Connection ID
159 */
160 static imc_state_t* find_connection(private_imc_agent_t *this,
161 TNC_ConnectionID id)
162 {
163 enumerator_t *enumerator;
164 imc_state_t *state, *found = NULL;
165
166 this->connection_lock->read_lock(this->connection_lock);
167 enumerator = this->connections->create_enumerator(this->connections);
168 while (enumerator->enumerate(enumerator, &state))
169 {
170 if (id == state->get_connection_id(state))
171 {
172 found = state;
173 break;
174 }
175 }
176 enumerator->destroy(enumerator);
177 this->connection_lock->unlock(this->connection_lock);
178
179 return found;
180 }
181
182 /**
183 * delete a connection state with a given Connection ID
184 */
185 static bool delete_connection(private_imc_agent_t *this, TNC_ConnectionID id)
186 {
187 enumerator_t *enumerator;
188 imc_state_t *state;
189 bool found = FALSE;
190
191 this->connection_lock->write_lock(this->connection_lock);
192 enumerator = this->connections->create_enumerator(this->connections);
193 while (enumerator->enumerate(enumerator, &state))
194 {
195 if (id == state->get_connection_id(state))
196 {
197 found = TRUE;
198 state->destroy(state);
199 this->connections->remove_at(this->connections, enumerator);
200 break;
201 }
202 }
203 enumerator->destroy(enumerator);
204 this->connection_lock->unlock(this->connection_lock);
205
206 return found;
207 }
208
209 METHOD(imc_agent_t, create_state, TNC_Result,
210 private_imc_agent_t *this, imc_state_t *state)
211 {
212 TNC_ConnectionID connection_id;
213
214 connection_id = state->get_connection_id(state);
215 if (find_connection(this, connection_id))
216 {
217 DBG1(DBG_IMC, "IMC %u \"%s\" already created a state for Connection ID %u",
218 this->id, this->name, connection_id);
219 state->destroy(state);
220 return TNC_RESULT_OTHER;
221 }
222 this->connection_lock->write_lock(this->connection_lock);
223 this->connections->insert_last(this->connections, state);
224 this->connection_lock->unlock(this->connection_lock);
225 DBG2(DBG_IMC, "IMC %u \"%s\" created a state for Connection ID %u",
226 this->id, this->name, connection_id);
227 return TNC_RESULT_SUCCESS;
228 }
229
230 METHOD(imc_agent_t, delete_state, TNC_Result,
231 private_imc_agent_t *this, TNC_ConnectionID connection_id)
232 {
233 if (!delete_connection(this, connection_id))
234 {
235 DBG1(DBG_IMC, "IMC %u \"%s\" has no state for Connection ID %u",
236 this->id, this->name, connection_id);
237 return TNC_RESULT_FATAL;
238 }
239 DBG2(DBG_IMC, "IMC %u \"%s\" deleted the state of Connection ID %u",
240 this->id, this->name, connection_id);
241 return TNC_RESULT_SUCCESS;
242 }
243
244 METHOD(imc_agent_t, change_state, TNC_Result,
245 private_imc_agent_t *this, TNC_ConnectionID connection_id,
246 TNC_ConnectionState new_state,
247 imc_state_t **state_p)
248 {
249 imc_state_t *state;
250
251 switch (new_state)
252 {
253 case TNC_CONNECTION_STATE_HANDSHAKE:
254 case TNC_CONNECTION_STATE_ACCESS_ALLOWED:
255 case TNC_CONNECTION_STATE_ACCESS_ISOLATED:
256 case TNC_CONNECTION_STATE_ACCESS_NONE:
257 state = find_connection(this, connection_id);
258
259 if (!state)
260 {
261 DBG1(DBG_IMC, "IMC %u \"%s\" has no state for Connection ID %u",
262 this->id, this->name, connection_id);
263 return TNC_RESULT_FATAL;
264 }
265 state->change_state(state, new_state);
266 DBG2(DBG_IMC, "IMC %u \"%s\" changed state of Connection ID %u to '%N'",
267 this->id, this->name, connection_id,
268 TNC_Connection_State_names, new_state);
269 if (state_p)
270 {
271 *state_p = state;
272 }
273 break;
274 case TNC_CONNECTION_STATE_CREATE:
275 DBG1(DBG_IMC, "state '%N' should be handled by create_state()",
276 TNC_Connection_State_names, new_state);
277 return TNC_RESULT_FATAL;
278 case TNC_CONNECTION_STATE_DELETE:
279 DBG1(DBG_IMC, "state '%N' should be handled by delete_state()",
280 TNC_Connection_State_names, new_state);
281 return TNC_RESULT_FATAL;
282 default:
283 DBG1(DBG_IMC, "IMC %u \"%s\" was notified of unknown state %u "
284 "for Connection ID %u",
285 this->id, this->name, new_state, connection_id);
286 return TNC_RESULT_INVALID_PARAMETER;
287 }
288 return TNC_RESULT_SUCCESS;
289 }
290
291 METHOD(imc_agent_t, get_state, bool,
292 private_imc_agent_t *this, TNC_ConnectionID connection_id,
293 imc_state_t **state)
294 {
295 *state = find_connection(this, connection_id);
296 if (!*state)
297 {
298 DBG1(DBG_IMC, "IMC %u \"%s\" has no state for Connection ID %u",
299 this->id, this->name, connection_id);
300 return FALSE;
301 }
302 return TRUE;
303 }
304
305 METHOD(imc_agent_t, send_message, TNC_Result,
306 private_imc_agent_t *this, TNC_ConnectionID connection_id, chunk_t msg)
307 {
308 TNC_MessageType type;
309
310 if (!this->send_message)
311 {
312 return TNC_RESULT_FATAL;
313 }
314 type = (this->vendor_id << 8) | this->subtype;
315 return this->send_message(this->id, connection_id, msg.ptr, msg.len, type);
316 }
317
318 METHOD(imc_agent_t, receive_message, TNC_Result,
319 private_imc_agent_t *this, TNC_ConnectionID connection_id, chunk_t msg,
320 TNC_MessageType msg_type, pa_tnc_msg_t **pa_tnc_msg)
321 {
322 pa_tnc_msg_t *pa_msg, *error_msg;
323 pa_tnc_attr_t *error_attr;
324 enumerator_t *enumerator;
325 TNC_Result result;
326
327 DBG2(DBG_IMV, "IMC %u \"%s\" received message type 0x%08x for Connection ID %u",
328 this->id, this->name, msg_type, connection_id);
329
330 *pa_tnc_msg = NULL;
331 pa_msg = pa_tnc_msg_create_from_data(msg);
332
333 switch (pa_msg->process(pa_msg))
334 {
335 case SUCCESS:
336 *pa_tnc_msg = pa_msg;
337 break;
338 case VERIFY_ERROR:
339 if (!this->send_message)
340 {
341 /* TNCC doen't have a SendMessage() function */
342 return TNC_RESULT_FATAL;
343 }
344
345 /* build error message */
346 error_msg = pa_tnc_msg_create();
347 enumerator = pa_msg->create_error_enumerator(pa_msg);
348 while (enumerator->enumerate(enumerator, &error_attr))
349 {
350 error_msg->add_attribute(error_msg,
351 error_attr->get_ref(error_attr));
352 }
353 enumerator->destroy(enumerator);
354 error_msg->build(error_msg);
355
356 /* send error message */
357 msg = error_msg->get_encoding(error_msg);
358 result = this->send_message(this->id, connection_id,
359 msg.ptr, msg.len, msg_type);
360
361 /* clean up */
362 error_msg->destroy(error_msg);
363 pa_msg->destroy(pa_msg);
364 return result;
365 case FAILED:
366 default:
367 pa_msg->destroy(pa_msg);
368 return TNC_RESULT_FATAL;
369 }
370 return TNC_RESULT_SUCCESS;
371 }
372
373 METHOD(imc_agent_t, destroy, void,
374 private_imc_agent_t *this)
375 {
376 DBG1(DBG_IMC, "IMC %u \"%s\" terminated", this->id, this->name);
377 this->connections->destroy_function(this->connections, free);
378 this->connection_lock->destroy(this->connection_lock);
379 free(this);
380
381 /* decrease the reference count or terminate */
382 libimcv_deinit();
383 }
384
385 /**
386 * Described in header.
387 */
388 imc_agent_t *imc_agent_create(const char *name,
389 pen_t vendor_id, u_int32_t subtype,
390 TNC_IMCID id, TNC_Version *actual_version)
391 {
392 private_imc_agent_t *this;
393
394 /* initialize or increase the reference count */
395 if (!libimcv_init())
396 {
397 return NULL;
398 }
399
400 INIT(this,
401 .public = {
402 .bind_functions = _bind_functions,
403 .create_state = _create_state,
404 .delete_state = _delete_state,
405 .change_state = _change_state,
406 .get_state = _get_state,
407 .send_message = _send_message,
408 .receive_message = _receive_message,
409 .destroy = _destroy,
410 },
411 .name = name,
412 .vendor_id = vendor_id,
413 .subtype = subtype,
414 .id = id,
415 .connections = linked_list_create(),
416 .connection_lock = rwlock_create(RWLOCK_TYPE_DEFAULT),
417 );
418
419 *actual_version = TNC_IFIMC_VERSION_1;
420 DBG1(DBG_IMC, "IMC %u \"%s\" initialized", this->id, this->name);
421
422 return &this->public;
423 }
424