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_attr_request.h>
22 #include <ietf/ietf_attr_port_filter.h>
24 #include <tncif_pa_subtypes.h>
27 #include <utils/lexparser.h>
28 #include <utils/debug.h>
34 static const char imc_name
[] = "Scanner";
36 static pen_type_t msg_types
[] = {
37 { PEN_IETF
, PA_SUBTYPE_IETF_VPN
}
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
, countof(msg_types
),
56 imc_id
, actual_version
);
59 return TNC_RESULT_FATAL
;
61 if (min_version
> TNC_IFIMC_VERSION_1
|| max_version
< TNC_IFIMC_VERSION_1
)
63 DBG1(DBG_IMC
, "no common IF-IMC version");
64 return TNC_RESULT_NO_COMMON_VERSION
;
66 return TNC_RESULT_SUCCESS
;
70 * see section 3.8.2 of TCG TNC IF-IMC Specification 1.3
72 TNC_Result
TNC_IMC_NotifyConnectionChange(TNC_IMCID imc_id
,
73 TNC_ConnectionID connection_id
,
74 TNC_ConnectionState new_state
)
80 DBG1(DBG_IMC
, "IMC \"%s\" has not been initialized", imc_name
);
81 return TNC_RESULT_NOT_INITIALIZED
;
85 case TNC_CONNECTION_STATE_CREATE
:
86 state
= imc_scanner_state_create(connection_id
);
87 return imc_scanner
->create_state(imc_scanner
, state
);
88 case TNC_CONNECTION_STATE_HANDSHAKE
:
89 if (imc_scanner
->change_state(imc_scanner
, connection_id
, new_state
,
90 &state
) != TNC_RESULT_SUCCESS
)
92 return TNC_RESULT_FATAL
;
94 state
->set_result(state
, imc_id
,
95 TNC_IMV_EVALUATION_RESULT_DONT_KNOW
);
96 return TNC_RESULT_SUCCESS
;
97 case TNC_CONNECTION_STATE_DELETE
:
98 return imc_scanner
->delete_state(imc_scanner
, connection_id
);
100 return imc_scanner
->change_state(imc_scanner
, connection_id
,
106 * Determine all TCP and UDP server sockets listening on physical interfaces
108 static bool do_netstat(ietf_attr_port_filter_t
*attr
)
114 bool success
= FALSE
;
115 const char system_v4
[] = "127.0.1.1";
116 const char loopback_v4
[] = "127.0.0.1";
117 const char loopback_v6
[] = "::1";
119 /* Open a pipe stream for reading the output of the netstat commmand */
120 file
= popen("/bin/netstat -n -l -p -4 -6 --inet", "r");
123 DBG1(DBG_IMC
, "failed to run netstat command");
127 /* Read the output a line at a time */
128 while (fgets(buf
, sizeof(buf
), file
))
131 u_int8_t new_protocol
, protocol
;
132 u_int16_t new_port
, port
;
134 enumerator_t
*enumerator
;
135 bool allowed
, found
= FALSE
;
137 DBG2(DBG_IMC
, "%.*s", (int)(strlen(buf
)-1), buf
);
141 /* skip the first two header lines */
144 line
= chunk_create(buf
, strlen(buf
));
146 /* Extract the IP protocol type */
147 if (!extract_token(&token
, ' ', &line
))
149 DBG1(DBG_IMC
, "protocol field in netstat output not found");
152 if (match("tcp", &token
) || match("tcp6", &token
))
154 new_protocol
= IPPROTO_TCP
;
156 else if (match("udp", &token
) || match("udp6", &token
))
158 new_protocol
= IPPROTO_UDP
;
162 DBG1(DBG_IMC
, "skipped unknown IP protocol in netstat output");
166 /* Skip the Recv-Q and Send-Q fields */
167 for (i
= 0; i
< 3; i
++)
169 if (!eat_whitespace(&line
) || !extract_token(&token
, ' ', &line
))
177 DBG1(DBG_IMC
, "local address field in netstat output not found");
181 /* Find the local port appended to the local address */
182 pos
= token
.ptr
+ token
.len
;
183 while (*--pos
!= ':' && --token
.len
);
186 DBG1(DBG_IMC
, "local port field in netstat output not found");
191 /* ignore ports of IPv4 and IPv6 loopback interfaces
192 and the internal system IPv4 address */
193 if ((token
.len
== strlen(system_v4
) &&
194 memeq(system_v4
, token
.ptr
, token
.len
)) ||
195 (token
.len
== strlen(loopback_v4
) &&
196 memeq(loopback_v4
, token
.ptr
, token
.len
)) ||
197 (token
.len
== strlen(loopback_v6
) &&
198 memeq(loopback_v6
, token
.ptr
, token
.len
)))
203 /* convert the port string to an integer */
204 new_port
= atoi(pos
+1);
206 /* check if the there is already a port entry */
207 enumerator
= attr
->create_port_enumerator(attr
);
208 while (enumerator
->enumerate(enumerator
, &allowed
, &protocol
, &port
))
210 if (new_port
== port
&& new_protocol
== protocol
)
215 enumerator
->destroy(enumerator
);
217 /* Skip the duplicate port entry */
223 /* Add new port entry */
224 attr
->add_port(attr
, FALSE
, new_protocol
, new_port
);
227 /* Successfully completed the parsing of the netstat output */
231 /* Close the pipe stream */
237 * Add IETF Port Filter attribute to the send queue
239 static TNC_Result
add_port_filter(imc_msg_t
*msg
)
242 ietf_attr_port_filter_t
*attr_port_filter
;
244 attr
= ietf_attr_port_filter_create();
245 attr
->set_noskip_flag(attr
, TRUE
);
246 attr_port_filter
= (ietf_attr_port_filter_t
*)attr
;
247 if (!do_netstat(attr_port_filter
))
250 return TNC_RESULT_FATAL
;
252 msg
->add_attribute(msg
, attr
);
254 return TNC_RESULT_SUCCESS
;
258 * see section 3.8.3 of TCG TNC IF-IMC Specification 1.3
260 TNC_Result
TNC_IMC_BeginHandshake(TNC_IMCID imc_id
,
261 TNC_ConnectionID connection_id
)
265 TNC_Result result
= TNC_RESULT_SUCCESS
;
269 DBG1(DBG_IMC
, "IMC \"%s\" has not been initialized", imc_name
);
270 return TNC_RESULT_NOT_INITIALIZED
;
272 if (!imc_scanner
->get_state(imc_scanner
, connection_id
, &state
))
274 return TNC_RESULT_FATAL
;
276 if (lib
->settings
->get_bool(lib
->settings
,
277 "libimcv.plugins.imc-scanner.push_info", TRUE
))
279 out_msg
= imc_msg_create(imc_scanner
, state
, connection_id
, imc_id
,
280 TNC_IMVID_ANY
, msg_types
[0]);
281 result
= add_port_filter(out_msg
);
282 if (result
== TNC_RESULT_SUCCESS
)
284 /* send PA-TNC message with the excl flag not set */
285 result
= out_msg
->send(out_msg
, FALSE
);
287 out_msg
->destroy(out_msg
);
293 static TNC_Result
receive_message(imc_msg_t
*in_msg
)
296 enumerator_t
*enumerator
;
298 pen_type_t attr_type
;
299 TNC_Result result
= TNC_RESULT_SUCCESS
;
300 bool fatal_error
= FALSE
;
302 /* parse received PA-TNC message and handle local and remote errors */
303 result
= in_msg
->receive(in_msg
, &fatal_error
);
304 if (result
!= TNC_RESULT_SUCCESS
)
308 out_msg
= imc_msg_create_as_reply(in_msg
);
310 /* analyze PA-TNC attributes */
311 enumerator
= in_msg
->create_attribute_enumerator(in_msg
);
312 while (enumerator
->enumerate(enumerator
, &attr
))
314 attr_type
= attr
->get_type(attr
);
316 if (attr_type
.vendor_id
!= PEN_IETF
)
320 if (attr_type
.type
== IETF_ATTR_ATTRIBUTE_REQUEST
)
322 ietf_attr_attr_request_t
*attr_cast
;
326 attr_cast
= (ietf_attr_attr_request_t
*)attr
;
328 e
= attr_cast
->create_enumerator(attr_cast
);
329 while (e
->enumerate(e
, &entry
))
331 if (entry
->vendor_id
!= PEN_IETF
)
337 case IETF_ATTR_PORT_FILTER
:
338 result
= add_port_filter(out_msg
);
347 enumerator
->destroy(enumerator
);
351 result
= TNC_RESULT_FATAL
;
353 else if (result
== TNC_RESULT_SUCCESS
)
355 result
= out_msg
->send(out_msg
, TRUE
);
357 out_msg
->destroy(out_msg
);
363 * see section 3.8.4 of TCG TNC IF-IMC Specification 1.3
366 TNC_Result
TNC_IMC_ReceiveMessage(TNC_IMCID imc_id
,
367 TNC_ConnectionID connection_id
,
368 TNC_BufferReference msg
,
370 TNC_MessageType msg_type
)
378 DBG1(DBG_IMC
, "IMC \"%s\" has not been initialized", imc_name
);
379 return TNC_RESULT_NOT_INITIALIZED
;
381 if (!imc_scanner
->get_state(imc_scanner
, connection_id
, &state
))
383 return TNC_RESULT_FATAL
;
386 in_msg
= imc_msg_create_from_data(imc_scanner
, state
, connection_id
,
387 msg_type
, chunk_create(msg
, msg_len
));
388 result
= receive_message(in_msg
);
389 in_msg
->destroy(in_msg
);
395 * see section 3.8.6 of TCG TNC IF-IMV Specification 1.3
397 TNC_Result
TNC_IMC_ReceiveMessageLong(TNC_IMCID imc_id
,
398 TNC_ConnectionID connection_id
,
399 TNC_UInt32 msg_flags
,
400 TNC_BufferReference msg
,
402 TNC_VendorID msg_vid
,
403 TNC_MessageSubtype msg_subtype
,
404 TNC_UInt32 src_imv_id
,
405 TNC_UInt32 dst_imc_id
)
413 DBG1(DBG_IMC
, "IMC \"%s\" has not been initialized", imc_name
);
414 return TNC_RESULT_NOT_INITIALIZED
;
416 if (!imc_scanner
->get_state(imc_scanner
, connection_id
, &state
))
418 return TNC_RESULT_FATAL
;
420 in_msg
= imc_msg_create_from_long_data(imc_scanner
, state
, connection_id
,
421 src_imv_id
, dst_imc_id
, msg_vid
, msg_subtype
,
422 chunk_create(msg
, msg_len
));
423 result
= receive_message(in_msg
);
424 in_msg
->destroy(in_msg
);
430 * see section 3.8.7 of TCG TNC IF-IMC Specification 1.3
432 TNC_Result
TNC_IMC_BatchEnding(TNC_IMCID imc_id
,
433 TNC_ConnectionID connection_id
)
437 DBG1(DBG_IMC
, "IMC \"%s\" has not been initialized", imc_name
);
438 return TNC_RESULT_NOT_INITIALIZED
;
440 return TNC_RESULT_SUCCESS
;
444 * see section 3.8.8 of TCG TNC IF-IMC Specification 1.3
446 TNC_Result
TNC_IMC_Terminate(TNC_IMCID imc_id
)
450 DBG1(DBG_IMC
, "IMC \"%s\" has not been initialized", imc_name
);
451 return TNC_RESULT_NOT_INITIALIZED
;
453 imc_scanner
->destroy(imc_scanner
);
456 return TNC_RESULT_SUCCESS
;
460 * see section 4.2.8.1 of TCG TNC IF-IMC Specification 1.3
462 TNC_Result
TNC_IMC_ProvideBindFunction(TNC_IMCID imc_id
,
463 TNC_TNCC_BindFunctionPointer bind_function
)
467 DBG1(DBG_IMC
, "IMC \"%s\" has not been initialized", imc_name
);
468 return TNC_RESULT_NOT_INITIALIZED
;
470 return imc_scanner
->bind_functions(imc_scanner
, bind_function
);