imc/imv->send_message() uses attr_list
[strongswan.git] / src / libimcv / plugins / imv_test / imv_test.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 "imv_test_state.h"
16
17 #include <imv/imv_agent.h>
18 #include <pa_tnc/pa_tnc_msg.h>
19 #include <ietf/ietf_attr.h>
20 #include <ietf/ietf_attr_pa_tnc_error.h>
21 #include <ita/ita_attr.h>
22 #include <ita/ita_attr_command.h>
23 #include <ita/ita_attr_dummy.h>
24
25 #include <tncif_names.h>
26 #include <tncif_pa_subtypes.h>
27
28 #include <pen/pen.h>
29 #include <debug.h>
30
31 /* IMV definitions */
32
33 static const char imv_name[] = "Test";
34
35 #define IMV_VENDOR_ID PEN_ITA
36 #define IMV_SUBTYPE PA_SUBTYPE_ITA_TEST
37
38 static imv_agent_t *imv_test;
39
40 /**
41 * see section 3.8.1 of TCG TNC IF-IMV Specification 1.3
42 */
43 TNC_Result TNC_IMV_Initialize(TNC_IMVID imv_id,
44 TNC_Version min_version,
45 TNC_Version max_version,
46 TNC_Version *actual_version)
47 {
48 if (imv_test)
49 {
50 DBG1(DBG_IMV, "IMV \"%s\" has already been initialized", imv_name);
51 return TNC_RESULT_ALREADY_INITIALIZED;
52 }
53 imv_test = imv_agent_create(imv_name, IMV_VENDOR_ID, IMV_SUBTYPE,
54 imv_id, actual_version);
55 if (!imv_test)
56 {
57 return TNC_RESULT_FATAL;
58 }
59 if (min_version > TNC_IFIMV_VERSION_1 || max_version < TNC_IFIMV_VERSION_1)
60 {
61 DBG1(DBG_IMV, "no common IF-IMV version");
62 return TNC_RESULT_NO_COMMON_VERSION;
63 }
64 return TNC_RESULT_SUCCESS;
65 }
66
67 /**
68 * see section 3.8.2 of TCG TNC IF-IMV Specification 1.3
69 */
70 TNC_Result TNC_IMV_NotifyConnectionChange(TNC_IMVID imv_id,
71 TNC_ConnectionID connection_id,
72 TNC_ConnectionState new_state)
73 {
74 imv_state_t *state;
75
76 if (!imv_test)
77 {
78 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
79 return TNC_RESULT_NOT_INITIALIZED;
80 }
81 switch (new_state)
82 {
83 case TNC_CONNECTION_STATE_CREATE:
84 state = imv_test_state_create(connection_id);
85 return imv_test->create_state(imv_test, state);
86 case TNC_CONNECTION_STATE_DELETE:
87 return imv_test->delete_state(imv_test, connection_id);
88 default:
89 return imv_test->change_state(imv_test, connection_id,
90 new_state, NULL);
91 }
92 }
93
94 static TNC_Result receive_message(TNC_IMVID imv_id,
95 TNC_ConnectionID connection_id,
96 TNC_UInt32 msg_flags,
97 chunk_t msg,
98 TNC_VendorID msg_vid,
99 TNC_MessageSubtype msg_subtype,
100 TNC_UInt32 src_imc_id,
101 TNC_UInt32 dst_imv_id)
102 {
103 pa_tnc_msg_t *pa_tnc_msg;
104 pa_tnc_attr_t *attr;
105 linked_list_t *attr_list;
106 imv_state_t *state;
107 imv_test_state_t *test_state;
108 enumerator_t *enumerator;
109 TNC_Result result;
110 int rounds;
111 bool fatal_error, retry = FALSE;
112
113 if (!imv_test)
114 {
115 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
116 return TNC_RESULT_NOT_INITIALIZED;
117 }
118
119 /* get current IMV state */
120 if (!imv_test->get_state(imv_test, connection_id, &state))
121 {
122 return TNC_RESULT_FATAL;
123 }
124 test_state = (imv_test_state_t*)state;
125
126 /* parse received PA-TNC message and automatically handle any errors */
127 result = imv_test->receive_message(imv_test, state, msg, msg_vid,
128 msg_subtype, src_imc_id, dst_imv_id, &pa_tnc_msg);
129
130 /* no parsed PA-TNC attributes available if an error occurred */
131 if (!pa_tnc_msg)
132 {
133 return result;
134 }
135
136 /* preprocess any IETF standard error attributes */
137 fatal_error = pa_tnc_msg->process_ietf_std_errors(pa_tnc_msg);
138
139 /* add any new IMC and set its number of rounds */
140 rounds = lib->settings->get_int(lib->settings,
141 "libimcv.plugins.imv-test.rounds", 0);
142 test_state->add_imc(test_state, src_imc_id, rounds);
143
144 /* analyze PA-TNC attributes */
145 enumerator = pa_tnc_msg->create_attribute_enumerator(pa_tnc_msg);
146 while (enumerator->enumerate(enumerator, &attr))
147 {
148 if (attr->get_vendor_id(attr) != PEN_ITA)
149 {
150 continue;
151 }
152 if (attr->get_type(attr) == ITA_ATTR_COMMAND)
153 {
154 ita_attr_command_t *ita_attr;
155 char *command;
156
157 ita_attr = (ita_attr_command_t*)attr;
158 command = ita_attr->get_command(ita_attr);
159
160 if (streq(command, "allow"))
161 {
162 state->set_recommendation(state,
163 TNC_IMV_ACTION_RECOMMENDATION_ALLOW,
164 TNC_IMV_EVALUATION_RESULT_COMPLIANT);
165 }
166 else if (streq(command, "isolate"))
167 {
168 state->set_recommendation(state,
169 TNC_IMV_ACTION_RECOMMENDATION_ISOLATE,
170 TNC_IMV_EVALUATION_RESULT_NONCOMPLIANT_MINOR);
171 }
172 else if (streq(command, "block") || streq(command, "none"))
173 {
174 state->set_recommendation(state,
175 TNC_IMV_ACTION_RECOMMENDATION_NO_ACCESS,
176 TNC_IMV_EVALUATION_RESULT_NONCOMPLIANT_MAJOR);
177 }
178 else if (streq(command, "retry"))
179 {
180 retry = TRUE;
181 }
182 else
183 {
184 DBG1(DBG_IMV, "unsupported ITA Command '%s'", command);
185 state->set_recommendation(state,
186 TNC_IMV_ACTION_RECOMMENDATION_NO_RECOMMENDATION,
187 TNC_IMV_EVALUATION_RESULT_ERROR);
188 }
189 }
190 else if (attr->get_type(attr) == ITA_ATTR_DUMMY)
191 {
192 ita_attr_dummy_t *ita_attr;
193
194 ita_attr = (ita_attr_dummy_t*)attr;
195 DBG1(DBG_IMV, "received dummy attribute value (%d bytes)",
196 ita_attr->get_size(ita_attr));
197 }
198 }
199 enumerator->destroy(enumerator);
200 pa_tnc_msg->destroy(pa_tnc_msg);
201
202 if (fatal_error)
203 {
204 state->set_recommendation(state,
205 TNC_IMV_ACTION_RECOMMENDATION_NO_RECOMMENDATION,
206 TNC_IMV_EVALUATION_RESULT_ERROR);
207 return imv_test->provide_recommendation(imv_test, connection_id);
208 }
209
210 /* request a handshake retry ? */
211 if (retry)
212 {
213 test_state->set_rounds(test_state, rounds);
214 return imv_test->request_handshake_retry(imv_id, connection_id,
215 TNC_RETRY_REASON_IMV_SERIOUS_EVENT);
216 }
217
218 /* repeat the measurement ? */
219 if (test_state->another_round(test_state, src_imc_id))
220 {
221 attr_list = linked_list_create();
222 attr = ita_attr_command_create("repeat");
223 attr_list->insert_last(attr_list, attr);
224 result = imv_test->send_message(imv_test, connection_id, TRUE, imv_id,
225 src_imc_id, attr_list);
226 attr_list->destroy(attr_list);
227
228 return result;
229 }
230
231 return imv_test->provide_recommendation(imv_test, connection_id);
232 }
233
234 /**
235 * see section 3.8.4 of TCG TNC IF-IMV Specification 1.3
236 */
237 TNC_Result TNC_IMV_ReceiveMessage(TNC_IMVID imv_id,
238 TNC_ConnectionID connection_id,
239 TNC_BufferReference msg,
240 TNC_UInt32 msg_len,
241 TNC_MessageType msg_type)
242 {
243 TNC_VendorID msg_vid;
244 TNC_MessageSubtype msg_subtype;
245
246 msg_vid = msg_type >> 8;
247 msg_subtype = msg_type & TNC_SUBTYPE_ANY;
248
249 return receive_message(imv_id, connection_id, 0, chunk_create(msg, msg_len),
250 msg_vid, msg_subtype, 0, TNC_IMVID_ANY);
251 }
252
253 /**
254 * see section 3.8.6 of TCG TNC IF-IMV Specification 1.3
255 */
256 TNC_Result TNC_IMV_ReceiveMessageLong(TNC_IMVID imv_id,
257 TNC_ConnectionID connection_id,
258 TNC_UInt32 msg_flags,
259 TNC_BufferReference msg,
260 TNC_UInt32 msg_len,
261 TNC_VendorID msg_vid,
262 TNC_MessageSubtype msg_subtype,
263 TNC_UInt32 src_imc_id,
264 TNC_UInt32 dst_imv_id)
265 {
266 return receive_message(imv_id, connection_id, msg_flags,
267 chunk_create(msg, msg_len), msg_vid, msg_subtype,
268 src_imc_id, dst_imv_id);
269 }
270
271 /**
272 * see section 3.8.7 of TCG TNC IF-IMV Specification 1.3
273 */
274 TNC_Result TNC_IMV_SolicitRecommendation(TNC_IMVID imv_id,
275 TNC_ConnectionID connection_id)
276 {
277 if (!imv_test)
278 {
279 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
280 return TNC_RESULT_NOT_INITIALIZED;
281 }
282 return imv_test->provide_recommendation(imv_test, connection_id);
283 }
284
285 /**
286 * see section 3.8.8 of TCG TNC IF-IMV Specification 1.3
287 */
288 TNC_Result TNC_IMV_BatchEnding(TNC_IMVID imv_id,
289 TNC_ConnectionID connection_id)
290 {
291 if (!imv_test)
292 {
293 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
294 return TNC_RESULT_NOT_INITIALIZED;
295 }
296 return TNC_RESULT_SUCCESS;
297 }
298
299 /**
300 * see section 3.8.9 of TCG TNC IF-IMV Specification 1.3
301 */
302 TNC_Result TNC_IMV_Terminate(TNC_IMVID imv_id)
303 {
304 if (!imv_test)
305 {
306 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
307 return TNC_RESULT_NOT_INITIALIZED;
308 }
309 imv_test->destroy(imv_test);
310 imv_test = NULL;
311
312 return TNC_RESULT_SUCCESS;
313 }
314
315 /**
316 * see section 4.2.8.1 of TCG TNC IF-IMV Specification 1.3
317 */
318 TNC_Result TNC_IMV_ProvideBindFunction(TNC_IMVID imv_id,
319 TNC_TNCS_BindFunctionPointer bind_function)
320 {
321 if (!imv_test)
322 {
323 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
324 return TNC_RESULT_NOT_INITIALIZED;
325 }
326 return imv_test->bind_functions(imv_test, bind_function);
327 }