Added add_segment() method to IETF attributes
[strongswan.git] / src / libimcv / plugins / imv_test / imv_test_agent.c
1 /*
2 * Copyright (C) 2013-2014 Andreas Steffen
3 * HSR Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include "imv_test_agent.h"
17 #include "imv_test_state.h"
18
19 #include <imv/imv_agent.h>
20 #include <imv/imv_msg.h>
21 #include <ietf/ietf_attr.h>
22 #include <ietf/ietf_attr_pa_tnc_error.h>
23 #include <ita/ita_attr.h>
24 #include <ita/ita_attr_get_settings.h>
25 #include <ita/ita_attr_command.h>
26 #include <ita/ita_attr_dummy.h>
27
28 #include <tncif_names.h>
29 #include <tncif_pa_subtypes.h>
30
31 #include <pen/pen.h>
32 #include <utils/debug.h>
33
34 typedef struct private_imv_test_agent_t private_imv_test_agent_t;
35
36 /* Subscribed PA-TNC message subtypes */
37 static pen_type_t msg_types[] = {
38 { PEN_ITA, PA_SUBTYPE_ITA_TEST }
39 };
40
41 /**
42 * Private data of an imv_test_agent_t object.
43 */
44 struct private_imv_test_agent_t {
45
46 /**
47 * Public members of imv_test_agent_t
48 */
49 imv_agent_if_t public;
50
51 /**
52 * IMV agent responsible for generic functions
53 */
54 imv_agent_t *agent;
55
56 };
57
58 METHOD(imv_agent_if_t, bind_functions, TNC_Result,
59 private_imv_test_agent_t *this, TNC_TNCS_BindFunctionPointer bind_function)
60 {
61 return this->agent->bind_functions(this->agent, bind_function);
62 }
63
64 METHOD(imv_agent_if_t, notify_connection_change, TNC_Result,
65 private_imv_test_agent_t *this, TNC_ConnectionID id,
66 TNC_ConnectionState new_state)
67 {
68 imv_state_t *state;
69
70 switch (new_state)
71 {
72 case TNC_CONNECTION_STATE_CREATE:
73 state = imv_test_state_create(id);
74 return this->agent->create_state(this->agent, state);
75 case TNC_CONNECTION_STATE_DELETE:
76 return this->agent->delete_state(this->agent, id);
77 default:
78 return this->agent->change_state(this->agent, id, new_state, NULL);
79 }
80 }
81
82 /**
83 * Process a received message
84 */
85 static TNC_Result receive_msg(private_imv_test_agent_t *this, imv_state_t *state,
86 imv_msg_t *in_msg)
87 {
88 imv_msg_t *out_msg;
89 imv_test_state_t *test_state;
90 enumerator_t *enumerator;
91 pa_tnc_attr_t *attr;
92 pen_type_t attr_type;
93 TNC_Result result;
94 int rounds;
95 bool fatal_error = FALSE, received_command = FALSE, retry = FALSE;
96
97 /* generate an outgoing PA-TNC message - we might need it */
98 out_msg = imv_msg_create_as_reply(in_msg);
99
100 /* parse received PA-TNC message and handle local and remote errors */
101 result = in_msg->receive(in_msg, out_msg, &fatal_error);
102 if (result != TNC_RESULT_SUCCESS)
103 {
104 out_msg->destroy(out_msg);
105 return result;
106 }
107
108 /* add any new IMC and set its number of rounds */
109 rounds = lib->settings->get_int(lib->settings,
110 "%s.plugins.imv-test.rounds", 0, lib->ns);
111 test_state = (imv_test_state_t*)state;
112 test_state->add_imc(test_state, in_msg->get_src_id(in_msg), rounds);
113
114 /* analyze PA-TNC attributes */
115 enumerator = in_msg->create_attribute_enumerator(in_msg);
116 while (enumerator->enumerate(enumerator, &attr))
117 {
118 attr_type = attr->get_type(attr);
119
120 if (attr_type.vendor_id != PEN_ITA)
121 {
122 continue;
123 }
124 if (attr_type.type == ITA_ATTR_COMMAND)
125 {
126 ita_attr_command_t *ita_attr;
127 char *command;
128
129 received_command = TRUE;
130 ita_attr = (ita_attr_command_t*)attr;
131 command = ita_attr->get_command(ita_attr);
132
133 if (streq(command, "allow"))
134 {
135 state->set_recommendation(state,
136 TNC_IMV_ACTION_RECOMMENDATION_ALLOW,
137 TNC_IMV_EVALUATION_RESULT_COMPLIANT);
138 }
139 else if (streq(command, "isolate"))
140 {
141 state->set_recommendation(state,
142 TNC_IMV_ACTION_RECOMMENDATION_ISOLATE,
143 TNC_IMV_EVALUATION_RESULT_NONCOMPLIANT_MINOR);
144 }
145 else if (streq(command, "block") || streq(command, "none"))
146 {
147 state->set_recommendation(state,
148 TNC_IMV_ACTION_RECOMMENDATION_NO_ACCESS,
149 TNC_IMV_EVALUATION_RESULT_NONCOMPLIANT_MAJOR);
150 }
151 else if (streq(command, "retry"))
152 {
153 retry = TRUE;
154 }
155 else
156 {
157 DBG1(DBG_IMV, "unsupported ITA Command '%s'", command);
158 state->set_recommendation(state,
159 TNC_IMV_ACTION_RECOMMENDATION_NO_RECOMMENDATION,
160 TNC_IMV_EVALUATION_RESULT_ERROR);
161 }
162 }
163 else if (attr_type.type == ITA_ATTR_DUMMY)
164 {
165 ita_attr_dummy_t *ita_attr;
166
167 ita_attr = (ita_attr_dummy_t*)attr;
168 DBG1(DBG_IMV, "received dummy attribute value (%d bytes)",
169 ita_attr->get_size(ita_attr));
170 }
171 }
172 enumerator->destroy(enumerator);
173
174 if (fatal_error)
175 {
176 state->set_recommendation(state,
177 TNC_IMV_ACTION_RECOMMENDATION_NO_RECOMMENDATION,
178 TNC_IMV_EVALUATION_RESULT_ERROR);
179 result = out_msg->send_assessment(out_msg);
180 if (result == TNC_RESULT_SUCCESS)
181 {
182 result = this->agent->provide_recommendation(this->agent, state);
183 }
184 return result;
185 }
186
187 /* request a handshake retry ? */
188 if (retry)
189 {
190 test_state->set_rounds(test_state, rounds);
191 return this->agent->request_handshake_retry(
192 this->agent->get_id(this->agent),
193 state->get_connection_id(state),
194 TNC_RETRY_REASON_IMV_SERIOUS_EVENT);
195 }
196
197 /* repeat the measurement ? */
198 if (test_state->another_round(test_state, in_msg->get_src_id(in_msg)))
199 {
200 attr = ita_attr_command_create("repeat");
201 out_msg->add_attribute(out_msg, attr);
202
203 /* send PA-TNC message with excl flag set */
204 result = out_msg->send(out_msg, TRUE);
205 out_msg->destroy(out_msg);
206
207 return result;
208 }
209
210 if (received_command)
211 {
212 result = out_msg->send_assessment(out_msg);
213 if (result == TNC_RESULT_SUCCESS)
214 {
215 result = this->agent->provide_recommendation(this->agent, state);
216 }
217 }
218 else
219 {
220 /* send PA-TNC message with the EXCL flag set */
221 result = out_msg->send(out_msg, TRUE);
222 }
223 out_msg->destroy(out_msg);
224
225 return result;
226 }
227
228 METHOD(imv_agent_if_t, receive_message, TNC_Result,
229 private_imv_test_agent_t *this, TNC_ConnectionID id,
230 TNC_MessageType msg_type, chunk_t msg)
231 {
232 imv_state_t *state;
233 imv_msg_t *in_msg;
234 TNC_Result result;
235
236 if (!this->agent->get_state(this->agent, id, &state))
237 {
238 return TNC_RESULT_FATAL;
239 }
240 in_msg = imv_msg_create_from_data(this->agent, state, id, msg_type, msg);
241 result = receive_msg(this, state, in_msg);
242 in_msg->destroy(in_msg);
243
244 return result;
245 }
246
247 METHOD(imv_agent_if_t, receive_message_long, TNC_Result,
248 private_imv_test_agent_t *this, TNC_ConnectionID id,
249 TNC_UInt32 src_imc_id, TNC_UInt32 dst_imv_id,
250 TNC_VendorID msg_vid, TNC_MessageSubtype msg_subtype, chunk_t msg)
251 {
252 imv_state_t *state;
253 imv_msg_t *in_msg;
254 TNC_Result result;
255
256 if (!this->agent->get_state(this->agent, id, &state))
257 {
258 return TNC_RESULT_FATAL;
259 }
260 in_msg = imv_msg_create_from_long_data(this->agent, state, id,
261 src_imc_id, dst_imv_id, msg_vid, msg_subtype, msg);
262 result = receive_msg(this, state, in_msg);
263 in_msg->destroy(in_msg);
264
265 return result;
266
267 }
268
269 METHOD(imv_agent_if_t, batch_ending, TNC_Result,
270 private_imv_test_agent_t *this, TNC_ConnectionID id)
271 {
272 return TNC_RESULT_SUCCESS;
273 }
274
275 METHOD(imv_agent_if_t, solicit_recommendation, TNC_Result,
276 private_imv_test_agent_t *this, TNC_ConnectionID id)
277 {
278 imv_state_t *state;
279
280 if (!this->agent->get_state(this->agent, id, &state))
281 {
282 return TNC_RESULT_FATAL;
283 }
284 return this->agent->provide_recommendation(this->agent, state);
285 }
286
287 METHOD(imv_agent_if_t, destroy, void,
288 private_imv_test_agent_t *this)
289 {
290 DESTROY_IF(this->agent);
291 free(this);
292 }
293
294 /**
295 * Described in header.
296 */
297 imv_agent_if_t *imv_test_agent_create(const char *name, TNC_IMVID id,
298 TNC_Version *actual_version)
299 {
300 private_imv_test_agent_t *this;
301 imv_agent_t *agent;
302
303 agent = imv_agent_create(name, msg_types, countof(msg_types), id,
304 actual_version);
305 if (!agent)
306 {
307 return NULL;
308 }
309
310 INIT(this,
311 .public = {
312 .bind_functions = _bind_functions,
313 .notify_connection_change = _notify_connection_change,
314 .receive_message = _receive_message,
315 .receive_message_long = _receive_message_long,
316 .batch_ending = _batch_ending,
317 .solicit_recommendation = _solicit_recommendation,
318 .destroy = _destroy,
319 },
320 .agent = agent,
321 );
322
323 return &this->public;
324 }
325