Improved flow category guess

This commit is contained in:
Luca Deri
2019-04-02 23:01:03 +02:00
parent 153c77c2cd
commit a455e0d8d4
2 changed files with 212 additions and 205 deletions
+1 -1
View File
@@ -1027,7 +1027,7 @@ struct ndpi_flow_struct {
u_int16_t protocol_stack_info; u_int16_t protocol_stack_info;
/* init parameter, internal used to set up timestamp,... */ /* init parameter, internal used to set up timestamp,... */
u_int16_t guessed_protocol_id, guessed_host_protocol_id, guessed_category; u_int16_t guessed_protocol_id, guessed_host_protocol_id, guessed_category, guessed_header_category;
u_int8_t protocol_id_already_guessed:1, host_already_guessed:1, init_finished:1, setup_packet_direction:1, packet_direction:1, check_extra_packets:1; u_int8_t protocol_id_already_guessed:1, host_already_guessed:1, init_finished:1, setup_packet_direction:1, packet_direction:1, check_extra_packets:1;
/* /*
+19 -12
View File
@@ -601,7 +601,7 @@ void ndpi_set_proto_category(struct ndpi_detection_module_struct *ndpi_mod,
rather it indicates a query for Facebook and not Facebook traffic. rather it indicates a query for Facebook and not Facebook traffic.
- HTTP/SSL are NOT informative as SSL.Facebook (likely) means that this is - HTTP/SSL are NOT informative as SSL.Facebook (likely) means that this is
SSL (HTTPS) traffic containg Facebook traffic. SSL (HTTPS) traffic containg Facebook traffic.
*/ */
u_int8_t ndpi_is_subprotocol_informative(struct ndpi_detection_module_struct *ndpi_mod, u_int8_t ndpi_is_subprotocol_informative(struct ndpi_detection_module_struct *ndpi_mod,
u_int16_t protoId) { u_int16_t protoId) {
if(protoId >= NDPI_MAX_SUPPORTED_PROTOCOLS+NDPI_MAX_NUM_CUSTOM_PROTOCOLS) if(protoId >= NDPI_MAX_SUPPORTED_PROTOCOLS+NDPI_MAX_NUM_CUSTOM_PROTOCOLS)
@@ -2679,7 +2679,7 @@ static ndpi_default_ports_tree_node_t* ndpi_get_guessed_protocol_id(struct ndpi_
These are UDP protocols that must fit a single packet These are UDP protocols that must fit a single packet
and thus that if have NOT been detected they cannot be guessed and thus that if have NOT been detected they cannot be guessed
as they have been excluded as they have been excluded
*/ */
u_int8_t is_udp_guessable_protocol(u_int16_t l7_guessed_proto) { u_int8_t is_udp_guessable_protocol(u_int16_t l7_guessed_proto) {
switch(l7_guessed_proto) { switch(l7_guessed_proto) {
case NDPI_PROTOCOL_QUIC: case NDPI_PROTOCOL_QUIC:
@@ -3744,11 +3744,10 @@ static int ndpi_init_packet_header(struct ndpi_detection_module_struct *ndpi_str
#endif /* NDPI_DETECTION_SUPPORT_IPV6 */ #endif /* NDPI_DETECTION_SUPPORT_IPV6 */
} }
if(flow) { if(flow)
ndpi_apply_flow_protocol_to_packet(flow, &flow->packet); ndpi_apply_flow_protocol_to_packet(flow, &flow->packet);
} else { else
ndpi_int_reset_packet_protocol(&flow->packet); ndpi_int_reset_packet_protocol(&flow->packet);
}
l3len = flow->packet.l3_packet_len; l3len = flow->packet.l3_packet_len;
@@ -3756,7 +3755,7 @@ static int ndpi_init_packet_header(struct ndpi_detection_module_struct *ndpi_str
if(flow->packet.iph != NULL) { if(flow->packet.iph != NULL) {
#endif /* NDPI_DETECTION_SUPPORT_IPV6 */ #endif /* NDPI_DETECTION_SUPPORT_IPV6 */
decaps_iph =flow->packet.iph; decaps_iph = flow->packet.iph;
#ifdef NDPI_DETECTION_SUPPORT_IPV6 #ifdef NDPI_DETECTION_SUPPORT_IPV6
} }
@@ -4186,6 +4185,7 @@ static ndpi_protocol ndpi_process_partial_detection(struct ndpi_detection_module
ret.app_protocol = ret.master_protocol; ret.app_protocol = ret.master_protocol;
ndpi_fill_protocol_category(ndpi_struct, flow, &ret); ndpi_fill_protocol_category(ndpi_struct, flow, &ret);
ndpi_int_change_protocol(ndpi_struct, flow, ret.app_protocol, ret.master_protocol); ndpi_int_change_protocol(ndpi_struct, flow, ret.app_protocol, ret.master_protocol);
return(ret); return(ret);
@@ -4196,7 +4196,7 @@ static ndpi_protocol ndpi_process_partial_detection(struct ndpi_detection_module
/* /*
You can call this function at any time in case of unknown match to see if there is You can call this function at any time in case of unknown match to see if there is
a partial match that has been prevented by the current nDPI preferences configuration a partial match that has been prevented by the current nDPI preferences configuration
*/ */
ndpi_protocol ndpi_get_partial_detection(struct ndpi_detection_module_struct *ndpi_struct, ndpi_protocol ndpi_get_partial_detection(struct ndpi_detection_module_struct *ndpi_struct,
struct ndpi_flow_struct *flow) { struct ndpi_flow_struct *flow) {
if((flow->guessed_protocol_id == NDPI_PROTOCOL_HTTP) if((flow->guessed_protocol_id == NDPI_PROTOCOL_HTTP)
@@ -4588,6 +4588,7 @@ int ndpi_fill_ip_protocol_category(struct ndpi_detection_module_struct *ndpi_str
} }
ret->category = ndpi_get_proto_category(ndpi_struct, *ret); ret->category = ndpi_get_proto_category(ndpi_struct, *ret);
return 0; return 0;
} }
@@ -4597,12 +4598,10 @@ void ndpi_fill_protocol_category(struct ndpi_detection_module_struct *ndpi_struc
struct ndpi_flow_struct *flow, struct ndpi_flow_struct *flow,
ndpi_protocol *ret) { ndpi_protocol *ret) {
if(ndpi_struct->custom_categories.categories_loaded) { if(ndpi_struct->custom_categories.categories_loaded) {
if(flow->packet.iph) { if(flow->guessed_header_category != NDPI_PROTOCOL_CATEGORY_UNSPECIFIED) {
if(ndpi_fill_ip_protocol_category(ndpi_struct, flow->packet.iph->saddr, flow->packet.iph->daddr, ret)) { flow->category = flow->guessed_header_category;
flow->category = ret->category;
return; return;
} }
}
if(flow->host_server_name[0] != '\0') { if(flow->host_server_name[0] != '\0') {
unsigned long id; unsigned long id;
@@ -4731,6 +4730,14 @@ ndpi_protocol ndpi_detection_process_packet(struct ndpi_detection_module_struct
flow->guessed_protocol_id = (int16_t) ndpi_guess_protocol_id(ndpi_struct, flow, protocol, sport, dport, &user_defined_proto); flow->guessed_protocol_id = (int16_t) ndpi_guess_protocol_id(ndpi_struct, flow, protocol, sport, dport, &user_defined_proto);
flow->guessed_host_protocol_id = ndpi_guess_host_protocol_id(ndpi_struct, flow); flow->guessed_host_protocol_id = ndpi_guess_host_protocol_id(ndpi_struct, flow);
if(ndpi_struct->custom_categories.categories_loaded && flow->packet.iph) {
ndpi_protocol ret;
ndpi_fill_ip_protocol_category(ndpi_struct, flow->packet.iph->saddr, flow->packet.iph->daddr, &ret);
flow->guessed_header_category = ret.category;
} else
flow->guessed_header_category = NDPI_PROTOCOL_CATEGORY_UNSPECIFIED;
if(flow->guessed_protocol_id >= (NDPI_MAX_SUPPORTED_PROTOCOLS-1)) { if(flow->guessed_protocol_id >= (NDPI_MAX_SUPPORTED_PROTOCOLS-1)) {
/* This is a custom protocol and it has priority over everything else */ /* This is a custom protocol and it has priority over everything else */
ret.master_protocol = NDPI_PROTOCOL_UNKNOWN, ret.master_protocol = NDPI_PROTOCOL_UNKNOWN,
@@ -6355,7 +6362,7 @@ void ndpi_lru_add_to_cache(struct ndpi_lru_cache *c, u_int32_t key) {
- Leave fields empty/zero when information is missing (e.g. with ICMP ports are zero) - Leave fields empty/zero when information is missing (e.g. with ICMP ports are zero)
- The hash_buf most be 30+1 bits or longer - The hash_buf most be 30+1 bits or longer
- Return code: 0 = OK, -1 otherwise - Return code: 0 = OK, -1 otherwise
*/ */
int ndpi_flowv4_flow_hash(u_int8_t l4_proto, u_int32_t src_ip, int ndpi_flowv4_flow_hash(u_int8_t l4_proto, u_int32_t src_ip,
u_int32_t dst_ip, u_int16_t src_port, u_int16_t dst_port, u_int32_t dst_ip, u_int16_t src_port, u_int16_t dst_port,