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 <ietf/ietf_attr.h>
21 #include <ietf/ietf_attr_port_filter.h>
23 #include <tncif_pa_subtypes.h>
26 #include <utils/lexparser.h>
33 static const char imc_name
[] = "Scanner";
35 static pen_type_t msg_types
[] = {
36 { PEN_ITA
, PA_SUBTYPE_ITA_SCANNER
}
39 static imc_agent_t
*imc_scanner
;
42 * see section 3.8.1 of TCG TNC IF-IMC Specification 1.3
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
)
51 DBG1(DBG_IMC
, "IMC \"%s\" has already been initialized", imc_name
);
52 return TNC_RESULT_ALREADY_INITIALIZED
;
54 imc_scanner
= imc_agent_create(imc_name
, msg_types
, 1, imc_id
, actual_version
);
57 return TNC_RESULT_FATAL
;
59 if (min_version
> TNC_IFIMC_VERSION_1
|| max_version
< TNC_IFIMC_VERSION_1
)
61 DBG1(DBG_IMC
, "no common IF-IMC version");
62 return TNC_RESULT_NO_COMMON_VERSION
;
64 return TNC_RESULT_SUCCESS
;
68 * see section 3.8.2 of TCG TNC IF-IMC Specification 1.3
70 TNC_Result
TNC_IMC_NotifyConnectionChange(TNC_IMCID imc_id
,
71 TNC_ConnectionID connection_id
,
72 TNC_ConnectionState new_state
)
78 DBG1(DBG_IMC
, "IMC \"%s\" has not been initialized", imc_name
);
79 return TNC_RESULT_NOT_INITIALIZED
;
83 case TNC_CONNECTION_STATE_CREATE
:
84 state
= imc_scanner_state_create(connection_id
);
85 return imc_scanner
->create_state(imc_scanner
, state
);
86 case TNC_CONNECTION_STATE_HANDSHAKE
:
87 if (imc_scanner
->change_state(imc_scanner
, connection_id
, new_state
,
88 &state
) != TNC_RESULT_SUCCESS
)
90 return TNC_RESULT_FATAL
;
92 state
->set_result(state
, imc_id
,
93 TNC_IMV_EVALUATION_RESULT_DONT_KNOW
);
94 return TNC_RESULT_SUCCESS
;
95 case TNC_CONNECTION_STATE_DELETE
:
96 return imc_scanner
->delete_state(imc_scanner
, connection_id
);
98 return imc_scanner
->change_state(imc_scanner
, connection_id
,
104 * Determine all TCP and UDP server sockets listening on physical interfaces
106 static bool do_netstat(ietf_attr_port_filter_t
*attr
)
112 bool success
= FALSE
;
113 const char loopback_v4
[] = "127.0.0.1";
114 const char loopback_v6
[] = "::1";
116 /* Open a pipe stream for reading the output of the netstat commmand */
117 file
= popen("/bin/netstat -n -l -p -4 -6 --inet", "r");
120 DBG1(DBG_IMC
, "Failed to run netstat command");
124 /* Read the output a line at a time */
125 while (fgets(buf
, BUF_LEN
-1, file
))
128 u_int8_t new_protocol
, protocol
;
129 u_int16_t new_port
, port
;
131 enumerator_t
*enumerator
;
132 bool allowed
, found
= FALSE
;
134 DBG2(DBG_IMC
, "%.*s", (int)(strlen(buf
)-1), buf
);
138 /* skip the first two header lines */
141 line
= chunk_create(buf
, strlen(buf
));
143 /* Extract the IP protocol type */
144 if (!extract_token(&token
, ' ', &line
))
146 DBG1(DBG_IMC
, "Protocol field in netstat output not found");
149 if (match("tcp", &token
) || match("tcp6", &token
))
151 new_protocol
= IPPROTO_TCP
;
153 else if (match("udp", &token
) || match("udp6", &token
))
155 new_protocol
= IPPROTO_UDP
;
159 DBG1(DBG_IMC
, "Skipped unknown IP protocol in netstat output");
163 /* Skip the Recv-Q and Send-Q fields */
164 for (i
= 0; i
< 3; i
++)
166 if (!eat_whitespace(&line
) || !extract_token(&token
, ' ', &line
))
174 DBG1(DBG_IMC
, "Local Address field in netstat output not found");
178 /* Find the local port appended to the local address */
179 pos
= token
.ptr
+ token
.len
;
180 while (*--pos
!= ':' && --token
.len
);
183 DBG1(DBG_IMC
, "Local port field in netstat output not found");
188 /* ignore ports of IPv4 and IPv6 loopback interfaces */
189 if ((token
.len
== strlen(loopback_v4
) &&
190 memeq(loopback_v4
, token
.ptr
, token
.len
)) ||
191 (token
.len
== strlen(loopback_v6
) &&
192 memeq(loopback_v6
, token
.ptr
, token
.len
)))
197 /* convert the port string to an integer */
198 new_port
= atoi(pos
+1);
200 /* check if the there is already a port entry */
201 enumerator
= attr
->create_port_enumerator(attr
);
202 while (enumerator
->enumerate(enumerator
, &allowed
, &protocol
, &port
))
204 if (new_port
== port
&& new_protocol
== protocol
)
209 enumerator
->destroy(enumerator
);
211 /* Skip the duplicate port entry */
217 /* Add new port entry */
218 attr
->add_port(attr
, FALSE
, new_protocol
, new_port
);
221 /* Successfully completed the parsing of the netstat output */
225 /* Close the pipe stream */
230 static TNC_Result
send_message(imc_msg_t
*out_msg
)
233 ietf_attr_port_filter_t
*attr_port_filter
;
235 attr
= ietf_attr_port_filter_create();
236 attr
->set_noskip_flag(attr
, TRUE
);
237 attr_port_filter
= (ietf_attr_port_filter_t
*)attr
;
238 if (!do_netstat(attr_port_filter
))
241 return TNC_RESULT_FATAL
;
243 out_msg
->add_attribute(out_msg
, attr
);
245 /* send PA-TNC message with the excl flag not set */
246 return out_msg
->send(out_msg
, FALSE
);
250 * see section 3.8.3 of TCG TNC IF-IMC Specification 1.3
252 TNC_Result
TNC_IMC_BeginHandshake(TNC_IMCID imc_id
,
253 TNC_ConnectionID connection_id
)
261 DBG1(DBG_IMC
, "IMC \"%s\" has not been initialized", imc_name
);
262 return TNC_RESULT_NOT_INITIALIZED
;
264 if (!imc_scanner
->get_state(imc_scanner
, connection_id
, &state
))
266 return TNC_RESULT_FATAL
;
268 out_msg
= imc_msg_create(imc_scanner
, state
, connection_id
, imc_id
,
269 TNC_IMVID_ANY
, msg_types
[0]);
270 result
= send_message(out_msg
);
271 out_msg
->destroy(out_msg
);
276 static TNC_Result
receive_message(imc_msg_t
*in_msg
)
279 bool fatal_error
= FALSE
;
281 /* parse received PA-TNC message and handle local and remote errors */
282 result
= in_msg
->receive(in_msg
, &fatal_error
);
283 if (result
!= TNC_RESULT_SUCCESS
)
287 return fatal_error ? TNC_RESULT_FATAL
: TNC_RESULT_SUCCESS
;
291 * see section 3.8.4 of TCG TNC IF-IMC Specification 1.3
294 TNC_Result
TNC_IMC_ReceiveMessage(TNC_IMCID imc_id
,
295 TNC_ConnectionID connection_id
,
296 TNC_BufferReference msg
,
298 TNC_MessageType msg_type
)
306 DBG1(DBG_IMC
, "IMC \"%s\" has not been initialized", imc_name
);
307 return TNC_RESULT_NOT_INITIALIZED
;
309 if (!imc_scanner
->get_state(imc_scanner
, connection_id
, &state
))
311 return TNC_RESULT_FATAL
;
314 in_msg
= imc_msg_create_from_data(imc_scanner
, state
, connection_id
,
315 msg_type
, chunk_create(msg
, msg_len
));
316 result
= receive_message(in_msg
);
317 in_msg
->destroy(in_msg
);
323 * see section 3.8.6 of TCG TNC IF-IMV Specification 1.3
325 TNC_Result
TNC_IMC_ReceiveMessageLong(TNC_IMCID imc_id
,
326 TNC_ConnectionID connection_id
,
327 TNC_UInt32 msg_flags
,
328 TNC_BufferReference msg
,
330 TNC_VendorID msg_vid
,
331 TNC_MessageSubtype msg_subtype
,
332 TNC_UInt32 src_imv_id
,
333 TNC_UInt32 dst_imc_id
)
341 DBG1(DBG_IMC
, "IMC \"%s\" has not been initialized", imc_name
);
342 return TNC_RESULT_NOT_INITIALIZED
;
344 if (!imc_scanner
->get_state(imc_scanner
, connection_id
, &state
))
346 return TNC_RESULT_FATAL
;
348 in_msg
= imc_msg_create_from_long_data(imc_scanner
, state
, connection_id
,
349 src_imv_id
, dst_imc_id
, msg_vid
, msg_subtype
,
350 chunk_create(msg
, msg_len
));
351 result
= receive_message(in_msg
);
352 in_msg
->destroy(in_msg
);
358 * see section 3.8.7 of TCG TNC IF-IMC Specification 1.3
360 TNC_Result
TNC_IMC_BatchEnding(TNC_IMCID imc_id
,
361 TNC_ConnectionID connection_id
)
365 DBG1(DBG_IMC
, "IMC \"%s\" has not been initialized", imc_name
);
366 return TNC_RESULT_NOT_INITIALIZED
;
368 return TNC_RESULT_SUCCESS
;
372 * see section 3.8.8 of TCG TNC IF-IMC Specification 1.3
374 TNC_Result
TNC_IMC_Terminate(TNC_IMCID imc_id
)
378 DBG1(DBG_IMC
, "IMC \"%s\" has not been initialized", imc_name
);
379 return TNC_RESULT_NOT_INITIALIZED
;
381 imc_scanner
->destroy(imc_scanner
);
384 return TNC_RESULT_SUCCESS
;
388 * see section 4.2.8.1 of TCG TNC IF-IMC Specification 1.3
390 TNC_Result
TNC_IMC_ProvideBindFunction(TNC_IMCID imc_id
,
391 TNC_TNCC_BindFunctionPointer bind_function
)
395 DBG1(DBG_IMC
, "IMC \"%s\" has not been initialized", imc_name
);
396 return TNC_RESULT_NOT_INITIALIZED
;
398 return imc_scanner
->bind_functions(imc_scanner
, bind_function
);