2 * Copyright (C) 2011-2012 Andreas Steffen
3 * HSR Hochschule fuer Technik Rapperswil
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>.
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
16 #include "imc_scanner_state.h"
18 #include <imc/imc_agent.h>
19 #include <imc/imc_msg.h>
20 #include <pa_tnc/pa_tnc_msg.h>
21 #include <ietf/ietf_attr.h>
22 #include <ietf/ietf_attr_port_filter.h>
24 #include <tncif_pa_subtypes.h>
27 #include <utils/lexparser.h>
34 static const char imc_name
[] = "Scanner";
36 static pen_type_t msg_types
[] = {
37 { PEN_ITA
, PA_SUBTYPE_ITA_SCANNER
}
40 static imc_agent_t
*imc_scanner
;
43 * see section 3.8.1 of TCG TNC IF-IMC Specification 1.3
45 TNC_Result
TNC_IMC_Initialize(TNC_IMCID imc_id
,
46 TNC_Version min_version
,
47 TNC_Version max_version
,
48 TNC_Version
*actual_version
)
52 DBG1(DBG_IMC
, "IMC \"%s\" has already been initialized", imc_name
);
53 return TNC_RESULT_ALREADY_INITIALIZED
;
55 imc_scanner
= imc_agent_create(imc_name
, msg_types
, 1, imc_id
, actual_version
);
58 return TNC_RESULT_FATAL
;
60 if (min_version
> TNC_IFIMC_VERSION_1
|| max_version
< TNC_IFIMC_VERSION_1
)
62 DBG1(DBG_IMC
, "no common IF-IMC version");
63 return TNC_RESULT_NO_COMMON_VERSION
;
65 return TNC_RESULT_SUCCESS
;
69 * see section 3.8.2 of TCG TNC IF-IMC Specification 1.3
71 TNC_Result
TNC_IMC_NotifyConnectionChange(TNC_IMCID imc_id
,
72 TNC_ConnectionID connection_id
,
73 TNC_ConnectionState new_state
)
79 DBG1(DBG_IMC
, "IMC \"%s\" has not been initialized", imc_name
);
80 return TNC_RESULT_NOT_INITIALIZED
;
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_HANDSHAKE
:
88 if (imc_scanner
->change_state(imc_scanner
, connection_id
, new_state
,
89 &state
) != TNC_RESULT_SUCCESS
)
91 return TNC_RESULT_FATAL
;
93 state
->set_result(state
, imc_id
,
94 TNC_IMV_EVALUATION_RESULT_DONT_KNOW
);
95 return TNC_RESULT_SUCCESS
;
96 case TNC_CONNECTION_STATE_DELETE
:
97 return imc_scanner
->delete_state(imc_scanner
, connection_id
);
99 return imc_scanner
->change_state(imc_scanner
, connection_id
,
105 * Determine all TCP and UDP server sockets listening on physical interfaces
107 static bool do_netstat(ietf_attr_port_filter_t
*attr
)
113 bool success
= FALSE
;
114 const char loopback_v4
[] = "127.0.0.1";
115 const char loopback_v6
[] = "::1";
117 /* Open a pipe stream for reading the output of the netstat commmand */
118 file
= popen("/bin/netstat -n -l -p -4 -6 --inet", "r");
121 DBG1(DBG_IMC
, "Failed to run netstat command");
125 /* Read the output a line at a time */
126 while (fgets(buf
, BUF_LEN
-1, file
))
129 u_int8_t new_protocol
, protocol
;
130 u_int16_t new_port
, port
;
132 enumerator_t
*enumerator
;
133 bool allowed
, found
= FALSE
;
135 DBG2(DBG_IMC
, "%.*s", (int)(strlen(buf
)-1), buf
);
139 /* skip the first two header lines */
142 line
= chunk_create(buf
, strlen(buf
));
144 /* Extract the IP protocol type */
145 if (!extract_token(&token
, ' ', &line
))
147 DBG1(DBG_IMC
, "Protocol field in netstat output not found");
150 if (match("tcp", &token
) || match("tcp6", &token
))
152 new_protocol
= IPPROTO_TCP
;
154 else if (match("udp", &token
) || match("udp6", &token
))
156 new_protocol
= IPPROTO_UDP
;
160 DBG1(DBG_IMC
, "Skipped unknown IP protocol in netstat output");
164 /* Skip the Recv-Q and Send-Q fields */
165 for (i
= 0; i
< 3; i
++)
167 if (!eat_whitespace(&line
) || !extract_token(&token
, ' ', &line
))
175 DBG1(DBG_IMC
, "Local Address field in netstat output not found");
179 /* Find the local port appended to the local address */
180 pos
= token
.ptr
+ token
.len
;
181 while (*--pos
!= ':' && --token
.len
);
184 DBG1(DBG_IMC
, "Local port field in netstat output not found");
189 /* ignore ports of IPv4 and IPv6 loopback interfaces */
190 if ((token
.len
== strlen(loopback_v4
) &&
191 memeq(loopback_v4
, token
.ptr
, token
.len
)) ||
192 (token
.len
== strlen(loopback_v6
) &&
193 memeq(loopback_v6
, token
.ptr
, token
.len
)))
198 /* convert the port string to an integer */
199 new_port
= atoi(pos
+1);
201 /* check if the there is already a port entry */
202 enumerator
= attr
->create_port_enumerator(attr
);
203 while (enumerator
->enumerate(enumerator
, &allowed
, &protocol
, &port
))
205 if (new_port
== port
&& new_protocol
== protocol
)
210 enumerator
->destroy(enumerator
);
212 /* Skip the duplicate port entry */
218 /* Add new port entry */
219 attr
->add_port(attr
, FALSE
, new_protocol
, new_port
);
222 /* Successfully completed the parsing of the netstat output */
226 /* Close the pipe stream */
231 static TNC_Result
send_message(imc_msg_t
*out_msg
)
234 ietf_attr_port_filter_t
*attr_port_filter
;
236 attr
= ietf_attr_port_filter_create();
237 attr
->set_noskip_flag(attr
, TRUE
);
238 attr_port_filter
= (ietf_attr_port_filter_t
*)attr
;
239 if (!do_netstat(attr_port_filter
))
242 return TNC_RESULT_FATAL
;
244 out_msg
->add_attribute(out_msg
, attr
);
246 /* send PA-TNC message with the excl flag not set */
247 return out_msg
->send(out_msg
, FALSE
);
251 * see section 3.8.3 of TCG TNC IF-IMC Specification 1.3
253 TNC_Result
TNC_IMC_BeginHandshake(TNC_IMCID imc_id
,
254 TNC_ConnectionID connection_id
)
262 DBG1(DBG_IMC
, "IMC \"%s\" has not been initialized", imc_name
);
263 return TNC_RESULT_NOT_INITIALIZED
;
265 if (!imc_scanner
->get_state(imc_scanner
, connection_id
, &state
))
267 return TNC_RESULT_FATAL
;
269 out_msg
= imc_msg_create(imc_scanner
, state
, connection_id
, imc_id
,
270 TNC_IMVID_ANY
, msg_types
[0]);
271 result
= send_message(out_msg
);
272 out_msg
->destroy(out_msg
);
277 static TNC_Result
receive_message(imc_msg_t
*in_msg
)
280 bool fatal_error
= FALSE
;
282 /* parse received PA-TNC message and handle local and remote errors */
283 result
= in_msg
->receive(in_msg
, &fatal_error
);
284 if (result
!= TNC_RESULT_SUCCESS
)
288 return fatal_error ? TNC_RESULT_FATAL
: TNC_RESULT_SUCCESS
;
292 * see section 3.8.4 of TCG TNC IF-IMC Specification 1.3
295 TNC_Result
TNC_IMC_ReceiveMessage(TNC_IMCID imc_id
,
296 TNC_ConnectionID connection_id
,
297 TNC_BufferReference msg
,
299 TNC_MessageType msg_type
)
307 DBG1(DBG_IMC
, "IMC \"%s\" has not been initialized", imc_name
);
308 return TNC_RESULT_NOT_INITIALIZED
;
310 if (!imc_scanner
->get_state(imc_scanner
, connection_id
, &state
))
312 return TNC_RESULT_FATAL
;
315 in_msg
= imc_msg_create_from_data(imc_scanner
, state
, connection_id
,
316 msg_type
, chunk_create(msg
, msg_len
));
317 result
= receive_message(in_msg
);
318 in_msg
->destroy(in_msg
);
324 * see section 3.8.6 of TCG TNC IF-IMV Specification 1.3
326 TNC_Result
TNC_IMC_ReceiveMessageLong(TNC_IMCID imc_id
,
327 TNC_ConnectionID connection_id
,
328 TNC_UInt32 msg_flags
,
329 TNC_BufferReference msg
,
331 TNC_VendorID msg_vid
,
332 TNC_MessageSubtype msg_subtype
,
333 TNC_UInt32 src_imv_id
,
334 TNC_UInt32 dst_imc_id
)
342 DBG1(DBG_IMC
, "IMC \"%s\" has not been initialized", imc_name
);
343 return TNC_RESULT_NOT_INITIALIZED
;
345 if (!imc_scanner
->get_state(imc_scanner
, connection_id
, &state
))
347 return TNC_RESULT_FATAL
;
349 in_msg
= imc_msg_create_from_long_data(imc_scanner
, state
, connection_id
,
350 src_imv_id
, dst_imc_id
, msg_vid
, msg_subtype
,
351 chunk_create(msg
, msg_len
));
352 result
=receive_message(in_msg
);
353 in_msg
->destroy(in_msg
);
359 * see section 3.8.7 of TCG TNC IF-IMC Specification 1.3
361 TNC_Result
TNC_IMC_BatchEnding(TNC_IMCID imc_id
,
362 TNC_ConnectionID connection_id
)
366 DBG1(DBG_IMC
, "IMC \"%s\" has not been initialized", imc_name
);
367 return TNC_RESULT_NOT_INITIALIZED
;
369 return TNC_RESULT_SUCCESS
;
373 * see section 3.8.8 of TCG TNC IF-IMC Specification 1.3
375 TNC_Result
TNC_IMC_Terminate(TNC_IMCID imc_id
)
379 DBG1(DBG_IMC
, "IMC \"%s\" has not been initialized", imc_name
);
380 return TNC_RESULT_NOT_INITIALIZED
;
382 imc_scanner
->destroy(imc_scanner
);
385 return TNC_RESULT_SUCCESS
;
389 * see section 4.2.8.1 of TCG TNC IF-IMC Specification 1.3
391 TNC_Result
TNC_IMC_ProvideBindFunction(TNC_IMCID imc_id
,
392 TNC_TNCC_BindFunctionPointer bind_function
)
396 DBG1(DBG_IMC
, "IMC \"%s\" has not been initialized", imc_name
);
397 return TNC_RESULT_NOT_INITIALIZED
;
399 return imc_scanner
->bind_functions(imc_scanner
, bind_function
);