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 "imv_scanner_state.h"
18 #include <imv/imv_agent.h>
19 #include <imv/imv_msg.h>
20 #include <ietf/ietf_attr.h>
21 #include <ietf/ietf_attr_attr_request.h>
22 #include <ietf/ietf_attr_pa_tnc_error.h>
23 #include <ietf/ietf_attr_port_filter.h>
25 #include <tncif_names.h>
26 #include <tncif_pa_subtypes.h>
29 #include <collections/linked_list.h>
30 #include <utils/lexparser.h>
31 #include <utils/debug.h>
35 static const char imv_name
[] = "Scanner";
37 static pen_type_t msg_types
[] = {
38 { PEN_IETF
, PA_SUBTYPE_IETF_VPN
}
41 static imv_agent_t
*imv_scanner
;
43 typedef struct port_range_t port_range_t
;
46 u_int16_t start
, stop
;
53 * TRUE: all server ports on the TNC client must be closed
54 * FALSE: any server port on the TNC client is allowed to be open
56 static bool closed_port_policy
= TRUE
;
59 * List of TCP and UDP port ranges
61 * TRUE: server ports on the TNC client that are allowed to be open
62 * FALSE: server ports on the TNC client that must be closed
64 static linked_list_t
*tcp_ports
, *udp_ports
;
67 * Get a TCP or UDP port list from strongswan.conf
69 static linked_list_t
* get_port_list(char *label
)
73 chunk_t port_list
, port_item
, port_start
;
74 port_range_t
*port_range
;
76 list
= linked_list_create();
78 snprintf(key
, sizeof(key
), "libimcv.plugins.imv-scanner.%s_ports", label
);
79 value
= lib
->settings
->get_str(lib
->settings
, key
, NULL
);
82 DBG1(DBG_IMV
, "%s not defined", key
);
85 port_list
= chunk_create(value
, strlen(value
));
86 DBG2(DBG_IMV
, "list of %s ports that %s:", label
,
87 closed_port_policy ?
"are allowed to be open" : "must be closed");
89 while (eat_whitespace(&port_list
))
91 if (!extract_token(&port_item
, ' ', &port_list
))
93 /* reached last port item */
94 port_item
= port_list
;
95 port_list
= chunk_empty
;
97 port_range
= malloc_thing(port_range_t
);
98 port_range
->start
= atoi(port_item
.ptr
);
100 if (extract_token(&port_start
, '-', &port_item
) && port_item
.len
)
102 port_range
->stop
= atoi(port_item
.ptr
);
106 port_range
->stop
= port_range
->start
;
108 DBG2(DBG_IMV
, "%5u - %5u", port_range
->start
, port_range
->stop
);
109 list
->insert_last(list
, port_range
);
117 * see section 3.8.1 of TCG TNC IF-IMV Specification 1.3
119 TNC_Result
TNC_IMV_Initialize(TNC_IMVID imv_id
,
120 TNC_Version min_version
,
121 TNC_Version max_version
,
122 TNC_Version
*actual_version
)
126 DBG1(DBG_IMV
, "IMV \"%s\" has already been initialized", imv_name
);
127 return TNC_RESULT_ALREADY_INITIALIZED
;
129 imv_scanner
= imv_agent_create(imv_name
, msg_types
, countof(msg_types
),
130 imv_id
, actual_version
);
133 return TNC_RESULT_FATAL
;
135 if (min_version
> TNC_IFIMV_VERSION_1
|| max_version
< TNC_IFIMV_VERSION_1
)
137 DBG1(DBG_IMV
, "no common IF-IMV version");
138 return TNC_RESULT_NO_COMMON_VERSION
;
141 /* set the default port policy to closed (TRUE) or open (FALSE) */
142 closed_port_policy
= lib
->settings
->get_bool(lib
->settings
,
143 "libimcv.plugins.imv-scanner.closed_port_policy", TRUE
);
144 DBG2(DBG_IMV
, "default port policy is %s ports",
145 closed_port_policy ?
"closed" : "open");
147 /* get the list of open|closed ports */
148 tcp_ports
= get_port_list("tcp");
149 udp_ports
= get_port_list("udp");
151 return TNC_RESULT_SUCCESS
;
155 * see section 3.8.2 of TCG TNC IF-IMV Specification 1.3
157 TNC_Result
TNC_IMV_NotifyConnectionChange(TNC_IMVID imv_id
,
158 TNC_ConnectionID connection_id
,
159 TNC_ConnectionState new_state
)
165 DBG1(DBG_IMV
, "IMV \"%s\" has not been initialized", imv_name
);
166 return TNC_RESULT_NOT_INITIALIZED
;
170 case TNC_CONNECTION_STATE_CREATE
:
171 state
= imv_scanner_state_create(connection_id
);
172 return imv_scanner
->create_state(imv_scanner
, state
);
173 case TNC_CONNECTION_STATE_DELETE
:
174 return imv_scanner
->delete_state(imv_scanner
, connection_id
);
176 return imv_scanner
->change_state(imv_scanner
, connection_id
,
181 static TNC_Result
receive_message(imv_state_t
*state
, imv_msg_t
*in_msg
)
184 enumerator_t
*enumerator
;
188 bool fatal_error
= FALSE
;
190 /* parse received PA-TNC message and handle local and remote errors */
191 result
= in_msg
->receive(in_msg
, &fatal_error
);
192 if (result
!= TNC_RESULT_SUCCESS
)
197 /* analyze PA-TNC attributes */
198 enumerator
= in_msg
->create_attribute_enumerator(in_msg
);
199 while (enumerator
->enumerate(enumerator
, &attr
))
201 type
= attr
->get_type(attr
);
203 if (type
.vendor_id
== PEN_IETF
&& type
.type
== IETF_ATTR_PORT_FILTER
)
205 imv_scanner_state_t
*imv_scanner_state
;
206 ietf_attr_port_filter_t
*attr_port_filter
;
207 enumerator_t
*enumerator
;
210 bool blocked
, compliant
= TRUE
;
213 imv_scanner_state
= (imv_scanner_state_t
*)state
;
214 attr_port_filter
= (ietf_attr_port_filter_t
*)attr
;
215 enumerator
= attr_port_filter
->create_port_enumerator(attr_port_filter
);
216 while (enumerator
->enumerate(enumerator
, &blocked
, &protocol
, &port
))
219 port_range_t
*port_range
;
220 bool passed
, found
= FALSE
;
225 /* ignore closed ports */
229 e
= (protocol
== IPPROTO_TCP
) ?
230 tcp_ports
->create_enumerator(tcp_ports
) :
231 udp_ports
->create_enumerator(udp_ports
);
232 while (e
->enumerate(e
, &port_range
))
234 if (port
>= port_range
->start
&& port
<= port_range
->stop
)
242 passed
= (closed_port_policy
== found
);
243 DBG2(DBG_IMV
, "%s port %5u %s: %s",
244 (protocol
== IPPROTO_TCP
) ?
"tcp" : "udp", port
,
245 blocked ?
"closed" : "open", passed ?
"ok" : "fatal");
249 snprintf(buf
, sizeof(buf
), "%s/%u",
250 (protocol
== IPPROTO_TCP
) ?
"tcp" : "udp", port
);
251 imv_scanner_state
->add_violating_port(imv_scanner_state
,
255 enumerator
->destroy(enumerator
);
259 state
->set_recommendation(state
,
260 TNC_IMV_ACTION_RECOMMENDATION_ALLOW
,
261 TNC_IMV_EVALUATION_RESULT_COMPLIANT
);
265 state
->set_recommendation(state
,
266 TNC_IMV_ACTION_RECOMMENDATION_NO_ACCESS
,
267 TNC_IMV_EVALUATION_RESULT_NONCOMPLIANT_MAJOR
);
271 enumerator
->destroy(enumerator
);
275 state
->set_recommendation(state
,
276 TNC_IMV_ACTION_RECOMMENDATION_NO_RECOMMENDATION
,
277 TNC_IMV_EVALUATION_RESULT_ERROR
);
280 out_msg
= imv_msg_create_as_reply(in_msg
);
281 result
= out_msg
->send_assessment(out_msg
);
282 out_msg
->destroy(out_msg
);
283 if (result
!= TNC_RESULT_SUCCESS
)
287 return imv_scanner
->provide_recommendation(imv_scanner
, state
);
291 * see section 3.8.4 of TCG TNC IF-IMV Specification 1.3
293 TNC_Result
TNC_IMV_ReceiveMessage(TNC_IMVID imv_id
,
294 TNC_ConnectionID connection_id
,
295 TNC_BufferReference msg
,
297 TNC_MessageType msg_type
)
305 DBG1(DBG_IMV
, "IMV \"%s\" has not been initialized", imv_name
);
306 return TNC_RESULT_NOT_INITIALIZED
;
308 if (!imv_scanner
->get_state(imv_scanner
, connection_id
, &state
))
310 return TNC_RESULT_FATAL
;
313 in_msg
= imv_msg_create_from_data(imv_scanner
, state
, connection_id
, msg_type
,
314 chunk_create(msg
, msg_len
));
315 result
= receive_message(state
, in_msg
);
316 in_msg
->destroy(in_msg
);
322 * see section 3.8.6 of TCG TNC IF-IMV Specification 1.3
324 TNC_Result
TNC_IMV_ReceiveMessageLong(TNC_IMVID imv_id
,
325 TNC_ConnectionID connection_id
,
326 TNC_UInt32 msg_flags
,
327 TNC_BufferReference msg
,
329 TNC_VendorID msg_vid
,
330 TNC_MessageSubtype msg_subtype
,
331 TNC_UInt32 src_imc_id
,
332 TNC_UInt32 dst_imv_id
)
340 DBG1(DBG_IMV
, "IMV \"%s\" has not been initialized", imv_name
);
341 return TNC_RESULT_NOT_INITIALIZED
;
343 if (!imv_scanner
->get_state(imv_scanner
, connection_id
, &state
))
345 return TNC_RESULT_FATAL
;
347 in_msg
= imv_msg_create_from_long_data(imv_scanner
, state
, connection_id
,
348 src_imc_id
, dst_imv_id
, msg_vid
, msg_subtype
,
349 chunk_create(msg
, msg_len
));
350 result
=receive_message(state
, in_msg
);
351 in_msg
->destroy(in_msg
);
357 * see section 3.8.7 of TCG TNC IF-IMV Specification 1.3
359 TNC_Result
TNC_IMV_SolicitRecommendation(TNC_IMVID imv_id
,
360 TNC_ConnectionID connection_id
)
366 DBG1(DBG_IMV
, "IMV \"%s\" has not been initialized", imv_name
);
367 return TNC_RESULT_NOT_INITIALIZED
;
369 if (!imv_scanner
->get_state(imv_scanner
, connection_id
, &state
))
371 return TNC_RESULT_FATAL
;
373 return imv_scanner
->provide_recommendation(imv_scanner
, state
);
377 * see section 3.8.8 of TCG TNC IF-IMV Specification 1.3
379 TNC_Result
TNC_IMV_BatchEnding(TNC_IMVID imv_id
,
380 TNC_ConnectionID connection_id
)
385 TNC_IMV_Action_Recommendation rec
;
386 TNC_IMV_Evaluation_Result eval
;
387 TNC_Result result
= TNC_RESULT_SUCCESS
;
391 DBG1(DBG_IMV
, "IMV \"%s\" has not been initialized", imv_name
);
392 return TNC_RESULT_NOT_INITIALIZED
;
394 if (!imv_scanner
->get_state(imv_scanner
, connection_id
, &state
))
396 return TNC_RESULT_FATAL
;
398 state
->get_recommendation(state
, &rec
, &eval
);
399 if (rec
== TNC_IMV_ACTION_RECOMMENDATION_NO_RECOMMENDATION
)
401 out_msg
= imv_msg_create(imv_scanner
, state
, connection_id
, imv_id
,
402 TNC_IMCID_ANY
, msg_types
[0]);
403 attr
= ietf_attr_attr_request_create(PEN_IETF
, IETF_ATTR_PORT_FILTER
);
404 out_msg
->add_attribute(out_msg
, attr
);
406 /* send PA-TNC message with excl flag not set */
407 result
= out_msg
->send(out_msg
, FALSE
);
408 out_msg
->destroy(out_msg
);
415 * see section 3.8.9 of TCG TNC IF-IMV Specification 1.3
417 TNC_Result
TNC_IMV_Terminate(TNC_IMVID imv_id
)
421 DBG1(DBG_IMV
, "IMV \"%s\" has not been initialized", imv_name
);
422 return TNC_RESULT_NOT_INITIALIZED
;
424 tcp_ports
->destroy_function(tcp_ports
, free
);
425 udp_ports
->destroy_function(udp_ports
, free
);
426 imv_scanner
->destroy(imv_scanner
);
429 return TNC_RESULT_SUCCESS
;
433 * see section 4.2.8.1 of TCG TNC IF-IMV Specification 1.3
435 TNC_Result
TNC_IMV_ProvideBindFunction(TNC_IMVID imv_id
,
436 TNC_TNCS_BindFunctionPointer bind_function
)
440 DBG1(DBG_IMV
, "IMV \"%s\" has not been initialized", imv_name
);
441 return TNC_RESULT_NOT_INITIALIZED
;
443 return imv_scanner
->bind_functions(imv_scanner
, bind_function
);