implemented IMC/IMV ReceiveMessageLong functions
[strongswan.git] / src / libimcv / plugins / imc_scanner / imc_scanner.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 "imc_scanner_state.h"
16
17 #include <imc/imc_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 <ietf/ietf_attr_port_filter.h>
22
23 #include <tncif_names.h>
24 #include <tncif_pa_subtypes.h>
25
26 #include <pen/pen.h>
27 #include <utils/lexparser.h>
28 #include <debug.h>
29
30 #include <stdio.h>
31
32 /* IMC definitions */
33
34 static const char imc_name[] = "Scanner";
35
36 #define IMC_VENDOR_ID PEN_ITA
37 #define IMC_SUBTYPE PA_SUBTYPE_ITA_SCANNER
38
39 static imc_agent_t *imc_scanner;
40
41 /**
42 * see section 3.8.1 of TCG TNC IF-IMC Specification 1.3
43 */
44 TNC_Result TNC_IMC_Initialize(TNC_IMCID imc_id,
45 TNC_Version min_version,
46 TNC_Version max_version,
47 TNC_Version *actual_version)
48 {
49 if (imc_scanner)
50 {
51 DBG1(DBG_IMC, "IMC \"%s\" has already been initialized", imc_name);
52 return TNC_RESULT_ALREADY_INITIALIZED;
53 }
54 imc_scanner = imc_agent_create(imc_name, IMC_VENDOR_ID, IMC_SUBTYPE,
55 imc_id, actual_version);
56 if (!imc_scanner)
57 {
58 return TNC_RESULT_FATAL;
59 }
60 if (min_version > TNC_IFIMC_VERSION_1 || max_version < TNC_IFIMC_VERSION_1)
61 {
62 DBG1(DBG_IMC, "no common IF-IMC version");
63 return TNC_RESULT_NO_COMMON_VERSION;
64 }
65 return TNC_RESULT_SUCCESS;
66 }
67
68 /**
69 * see section 3.8.2 of TCG TNC IF-IMC Specification 1.3
70 */
71 TNC_Result TNC_IMC_NotifyConnectionChange(TNC_IMCID imc_id,
72 TNC_ConnectionID connection_id,
73 TNC_ConnectionState new_state)
74 {
75 imc_state_t *state;
76
77 if (!imc_scanner)
78 {
79 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
80 return TNC_RESULT_NOT_INITIALIZED;
81 }
82 switch (new_state)
83 {
84 case TNC_CONNECTION_STATE_CREATE:
85 state = imc_scanner_state_create(connection_id);
86 return imc_scanner->create_state(imc_scanner, state);
87 case TNC_CONNECTION_STATE_DELETE:
88 return imc_scanner->delete_state(imc_scanner, connection_id);
89 default:
90 return imc_scanner->change_state(imc_scanner, connection_id,
91 new_state, NULL);
92 }
93 }
94
95 /**
96 * Determine all TCP and UDP server sockets listening on physical interfaces
97 */
98 static bool do_netstat(ietf_attr_port_filter_t *attr)
99 {
100 FILE *file;
101 char buf[BUF_LEN];
102 chunk_t line, token;
103 int n = 0;
104 bool success = FALSE;
105 const char loopback_v4[] = "127.0.0.1";
106 const char loopback_v6[] = "::1";
107
108 /* Open a pipe stream for reading the output of the netstat commmand */
109 file = popen("/bin/netstat -n -l -p -4 -6 --inet", "r");
110 if (!file)
111 {
112 DBG1(DBG_IMC, "Failed to run netstat command");
113 return FALSE;
114 }
115
116 /* Read the output a line at a time */
117 while (fgets(buf, BUF_LEN-1, file))
118 {
119 u_char *pos;
120 u_int8_t new_protocol, protocol;
121 u_int16_t new_port, port;
122 int i;
123 enumerator_t *enumerator;
124 bool allowed, found = FALSE;
125
126 DBG2(DBG_IMC, "%.*s", strlen(buf)-1, buf);
127
128 if (n++ < 2)
129 {
130 /* skip the first two header lines */
131 continue;
132 }
133 line = chunk_create(buf, strlen(buf));
134
135 /* Extract the IP protocol type */
136 if (!extract_token(&token, ' ', &line))
137 {
138 DBG1(DBG_IMC, "Protocol field in netstat output not found");
139 goto end;
140 }
141 if (match("tcp", &token) || match("tcp6", &token))
142 {
143 new_protocol = IPPROTO_TCP;
144 }
145 else if (match("udp", &token) || match("udp6", &token))
146 {
147 new_protocol = IPPROTO_UDP;
148 }
149 else
150 {
151 DBG1(DBG_IMC, "Skipped unknown IP protocol in netstat output");
152 continue;
153 }
154
155 /* Skip the Recv-Q and Send-Q fields */
156 for (i = 0; i < 3; i++)
157 {
158 if (!eat_whitespace(&line) || !extract_token(&token, ' ', &line))
159 {
160 token = chunk_empty;
161 break;
162 }
163 }
164 if (token.len == 0)
165 {
166 DBG1(DBG_IMC, "Local Address field in netstat output not found");
167 goto end;
168 }
169
170 /* Find the local port appended to the local address */
171 pos = token.ptr + token.len;
172 while (*--pos != ':' && --token.len);
173 if (*pos != ':')
174 {
175 DBG1(DBG_IMC, "Local port field in netstat output not found");
176 goto end;
177 }
178 token.len--;
179
180 /* ignore ports of IPv4 and IPv6 loopback interfaces */
181 if ((token.len == strlen(loopback_v4) &&
182 memeq(loopback_v4, token.ptr, token.len)) ||
183 (token.len == strlen(loopback_v6) &&
184 memeq(loopback_v6, token.ptr, token.len)))
185 {
186 continue;
187 }
188
189 /* convert the port string to an integer */
190 new_port = atoi(pos+1);
191
192 /* check if the there is already a port entry */
193 enumerator = attr->create_port_enumerator(attr);
194 while (enumerator->enumerate(enumerator, &allowed, &protocol, &port))
195 {
196 if (new_port == port && new_protocol == protocol)
197 {
198 found = TRUE;
199 }
200 }
201 enumerator->destroy(enumerator);
202
203 /* Skip the duplicate port entry */
204 if (found)
205 {
206 continue;
207 }
208
209 /* Add new port entry */
210 attr->add_port(attr, FALSE, new_protocol, new_port);
211 }
212
213 /* Successfully completed the parsing of the netstat output */
214 success = TRUE;
215
216 end:
217 /* Close the pipe stream */
218 pclose(file);
219 return success;
220 }
221
222 static TNC_Result send_message(TNC_ConnectionID connection_id)
223 {
224 pa_tnc_msg_t *msg;
225 pa_tnc_attr_t *attr;
226 ietf_attr_port_filter_t *attr_port_filter;
227 TNC_Result result;
228
229 attr = ietf_attr_port_filter_create();
230 attr->set_noskip_flag(attr, TRUE);
231 attr_port_filter = (ietf_attr_port_filter_t*)attr;
232 if (!do_netstat(attr_port_filter))
233 {
234 attr->destroy(attr);
235 return TNC_RESULT_FATAL;
236 }
237 msg = pa_tnc_msg_create();
238 msg->add_attribute(msg, attr);
239 msg->build(msg);
240 result = imc_scanner->send_message(imc_scanner, connection_id, FALSE, 0,
241 TNC_IMVID_ANY, msg->get_encoding(msg));
242 msg->destroy(msg);
243
244 return result;
245 }
246
247 /**
248 * see section 3.8.3 of TCG TNC IF-IMC Specification 1.3
249 */
250 TNC_Result TNC_IMC_BeginHandshake(TNC_IMCID imc_id,
251 TNC_ConnectionID connection_id)
252 {
253 if (!imc_scanner)
254 {
255 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
256 return TNC_RESULT_NOT_INITIALIZED;
257 }
258 return send_message(connection_id);
259 }
260
261 static TNC_Result receive_message(TNC_IMCID imc_id,
262 TNC_ConnectionID connection_id,
263 TNC_UInt32 msg_flags,
264 chunk_t msg,
265 TNC_VendorID msg_vid,
266 TNC_MessageSubtype msg_subtype,
267 TNC_UInt32 src_imv_id,
268 TNC_UInt32 dst_imc_id)
269 {
270 pa_tnc_msg_t *pa_tnc_msg;
271 pa_tnc_attr_t *attr;
272 imc_state_t *state;
273 enumerator_t *enumerator;
274 TNC_Result result;
275 bool fatal_error = FALSE;
276
277 if (!imc_scanner)
278 {
279 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
280 return TNC_RESULT_NOT_INITIALIZED;
281 }
282
283 /* get current IMC state */
284 if (!imc_scanner->get_state(imc_scanner, connection_id, &state))
285 {
286 return TNC_RESULT_FATAL;
287 }
288
289 /* parse received PA-TNC message and automatically handle any errors */
290 result = imc_scanner->receive_message(imc_scanner, state, msg, msg_vid,
291 msg_subtype, src_imv_id, dst_imc_id, &pa_tnc_msg);
292
293 /* no parsed PA-TNC attributes available if an error occurred */
294 if (!pa_tnc_msg)
295 {
296 return result;
297 }
298
299 /* analyze PA-TNC attributes */
300 enumerator = pa_tnc_msg->create_attribute_enumerator(pa_tnc_msg);
301 while (enumerator->enumerate(enumerator, &attr))
302 {
303 ietf_attr_pa_tnc_error_t *error_attr;
304 pa_tnc_error_code_t error_code;
305 chunk_t msg_info, attr_info;
306 u_int32_t offset;
307
308 if (attr->get_vendor_id(attr) != PEN_IETF &&
309 attr->get_type(attr) != IETF_ATTR_PA_TNC_ERROR)
310 {
311 continue;
312 }
313
314 error_attr = (ietf_attr_pa_tnc_error_t*)attr;
315 error_code = error_attr->get_error_code(error_attr);
316 msg_info = error_attr->get_msg_info(error_attr);
317 DBG1(DBG_IMC, "received PA-TNC error '%N' concerning message %#B",
318 pa_tnc_error_code_names, error_code, &msg_info);
319
320 switch (error_code)
321 {
322 case PA_ERROR_INVALID_PARAMETER:
323 offset = error_attr->get_offset(error_attr);
324 DBG1(DBG_IMC, " occurred at offset of %u bytes", offset);
325 break;
326 case PA_ERROR_ATTR_TYPE_NOT_SUPPORTED:
327 attr_info = error_attr->get_attr_info(error_attr);
328 DBG1(DBG_IMC, " unsupported attribute %#B", &attr_info);
329 break;
330 default:
331 break;
332 }
333 fatal_error = TRUE;
334 }
335 enumerator->destroy(enumerator);
336 pa_tnc_msg->destroy(pa_tnc_msg);
337
338 /* if no error occurred then always return the same response */
339 return fatal_error ? TNC_RESULT_FATAL : send_message(connection_id);
340 }
341
342 /**
343 * see section 3.8.4 of TCG TNC IF-IMC Specification 1.3
344 */
345 TNC_Result TNC_IMC_ReceiveMessage(TNC_IMCID imc_id,
346 TNC_ConnectionID connection_id,
347 TNC_BufferReference msg,
348 TNC_UInt32 msg_len,
349 TNC_MessageType msg_type)
350 {
351 TNC_VendorID msg_vid;
352 TNC_MessageSubtype msg_subtype;
353
354 msg_vid = msg_type >> 8;
355 msg_subtype = msg_type & TNC_SUBTYPE_ANY;
356
357 return receive_message(imc_id, connection_id, 0, chunk_create(msg, msg_len),
358 msg_vid, msg_subtype, 0, TNC_IMCID_ANY);
359 }
360
361 /**
362 * see section 3.8.6 of TCG TNC IF-IMV Specification 1.3
363 */
364 TNC_Result TNC_IMC_ReceiveMessageLong(TNC_IMCID imc_id,
365 TNC_ConnectionID connection_id,
366 TNC_UInt32 msg_flags,
367 TNC_BufferReference msg,
368 TNC_UInt32 msg_len,
369 TNC_VendorID msg_vid,
370 TNC_MessageSubtype msg_subtype,
371 TNC_UInt32 src_imv_id,
372 TNC_UInt32 dst_imc_id)
373 {
374 return receive_message(imc_id, connection_id, msg_flags,
375 chunk_create(msg, msg_len), msg_vid, msg_subtype,
376 src_imv_id, dst_imc_id);
377 }
378
379 /**
380 * see section 3.8.7 of TCG TNC IF-IMC Specification 1.3
381 */
382 TNC_Result TNC_IMC_BatchEnding(TNC_IMCID imc_id,
383 TNC_ConnectionID connection_id)
384 {
385 if (!imc_scanner)
386 {
387 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
388 return TNC_RESULT_NOT_INITIALIZED;
389 }
390 return TNC_RESULT_SUCCESS;
391 }
392
393 /**
394 * see section 3.8.8 of TCG TNC IF-IMC Specification 1.3
395 */
396 TNC_Result TNC_IMC_Terminate(TNC_IMCID imc_id)
397 {
398 if (!imc_scanner)
399 {
400 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
401 return TNC_RESULT_NOT_INITIALIZED;
402 }
403 imc_scanner->destroy(imc_scanner);
404 imc_scanner = NULL;
405
406 return TNC_RESULT_SUCCESS;
407 }
408
409 /**
410 * see section 4.2.8.1 of TCG TNC IF-IMC Specification 1.3
411 */
412 TNC_Result TNC_IMC_ProvideBindFunction(TNC_IMCID imc_id,
413 TNC_TNCC_BindFunctionPointer bind_function)
414 {
415 if (!imc_scanner)
416 {
417 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
418 return TNC_RESULT_NOT_INITIALIZED;
419 }
420 return imc_scanner->bind_functions(imc_scanner, bind_function);
421 }