From e4fd4bc9f9b283c70301e87b66cdb8fa255b1c40 Mon Sep 17 00:00:00 2001 From: Luca Deri Date: Mon, 30 Nov 2015 18:46:52 +0100 Subject: [PATCH] Typedef refresh --- example/ndpiReader.c | 548 +++++++++++++++++++--------- src/include/Makefile.am | 13 +- src/include/linux_compat.h | 188 ---------- src/include/ndpi_api.h | 39 +- src/include/ndpi_define.h | 48 +-- src/include/ndpi_includes.h | 68 ++++ src/include/ndpi_main.h | 88 +---- src/include/ndpi_protocol_ids.h | 9 +- src/include/ndpi_protocols.h | 10 +- src/include/ndpi_typedefs.h | 375 ++++++++++++++----- src/include/ndpi_unix.h | 9 - src/include/ndpi_win32.h | 38 +- src/lib/Makefile.am | 5 +- src/lib/ndpi_main.c | 627 +++++++++++++------------------- src/lib/protocols/mdns.c | 146 ++++---- src/lib/protocols/tcp_udp.c | 5 +- 16 files changed, 1136 insertions(+), 1080 deletions(-) delete mode 100644 src/include/linux_compat.h create mode 100644 src/include/ndpi_includes.h diff --git a/example/ndpiReader.c b/example/ndpiReader.c index c11c0c50d..0d473f58a 100644 --- a/example/ndpiReader.c +++ b/example/ndpiReader.c @@ -42,18 +42,50 @@ #include #include #include +#include #include "../config.h" +#include "ndpi_api.h" #ifdef HAVE_JSON_C #include #endif -#include "ndpi_api.h" - -#include - #define MAX_NUM_READER_THREADS 16 +#define IDLE_SCAN_PERIOD 10 /* msec (use detection_tick_resolution = 1000) */ +#define MAX_IDLE_TIME 30000 +#define IDLE_SCAN_BUDGET 1024 +#define NUM_ROOTS 512 +#define GTP_U_V1_PORT 2152 +#define TZSP_PORT 37008 +#define MAX_NDPI_FLOWS 200000000 + +#ifndef ETH_P_IP +#define ETH_P_IP 0x0800 /* IPv4 */ +#endif + +#ifndef ETH_P_IPv6 +#define ETH_P_IPV6 0x86dd /* IPv6 */ +#endif + +#define SLARP 0x8035 /* Cisco Slarp */ +#define CISCO_D_PROTO 0x2000 /* Cisco Discovery Protocol */ + +#define VLAN 0x8100 +#define MPLS_UNI 0x8847 +#define MPLS_MULTI 0x8848 +#define PPPoE 0x8864 +#define SNAP 0xaa + +/* mask for FCF */ +#define WIFI_DATA 0x2 /* 0000 0010 */ +#define FCF_TYPE(fc) (((fc) >> 2) & 0x3) /* 0000 0011 = 0x3 */ +#define FCF_SUBTYPE(fc) (((fc) >> 4) & 0xF) /* 0000 1111 = 0xF */ +#define FCF_TO_DS(fc) ((fc) & 0x0100) +#define FCF_FROM_DS(fc) ((fc) & 0x0200) + +/* mask for Bad FCF presence */ +#define BAD_FCS 0x50 /* 0101 0000 */ /** * @brief Set main components necessary to the detection @@ -100,12 +132,6 @@ static u_int32_t detection_tick_resolution = 1000; static time_t capture_for = 0; static time_t capture_until = 0; -#define IDLE_SCAN_PERIOD 10 /* msec (use detection_tick_resolution = 1000) */ -#define MAX_IDLE_TIME 30000 -#define IDLE_SCAN_BUDGET 1024 - -#define NUM_ROOTS 512 - static u_int32_t num_flows; struct thread_stats { @@ -143,21 +169,15 @@ struct reader_thread { static struct reader_thread ndpi_thread_info[MAX_NUM_READER_THREADS]; -#define GTP_U_V1_PORT 2152 -#define MAX_NDPI_FLOWS 200000000 /** * @brief ID tracking */ typedef struct ndpi_id { - u_int8_t ip[4]; //< Ip address - struct ndpi_id_struct *ndpi_id; //< nDpi worker structure + u_int8_t ip[4]; // Ip address + struct ndpi_id_struct *ndpi_id; // nDpi worker structure } ndpi_id_t; -static u_int32_t size_id_struct = 0; //< ID tracking structure size - -#ifndef ETH_P_IP -#define ETH_P_IP 0x0800 -#endif +static u_int32_t size_id_struct = 0; // ID tracking structure size // flow tracking typedef struct ndpi_flow { @@ -168,10 +188,9 @@ typedef struct ndpi_flow { u_int8_t detection_completed, protocol; u_int16_t vlan_id; struct ndpi_flow_struct *ndpi_flow; - char lower_name[32], upper_name[32]; - + char lower_name[48], upper_name[48]; + u_int8_t ip_version; u_int64_t last_seen; - u_int64_t bytes; u_int32_t packets; @@ -207,7 +226,7 @@ static void help(u_int long_help) { #endif " -d | Disable protocol guess and use only DPI\n" " -q | Quiet mode\n" - " -t | Dissect GTP tunnels\n" + " -t | Dissect GTP/TZSP tunnels\n" " -r | Print nDPI version and git revision\n" " -w | Write test output on the specified file. This is useful for\n" " | testing purposes in order to compare results across runs\n" @@ -476,19 +495,18 @@ static void printFlow(u_int16_t thread_id, struct ndpi_flow *flow) { FILE *out = results_file ? results_file : stdout; if(!json_flag) { -#if 0 - fprintf(out, "\t%s [VLAN: %u] %s:%u <-> %s:%u\n", - ipProto2Name(flow->protocol), flow->vlan_id, - flow->lower_name, ntohs(flow->lower_port), - flow->upper_name, ntohs(flow->upper_port)); - -#else fprintf(out, "\t%u", ++num_flows); - fprintf(out, "\t%s %s:%u <-> %s:%u ", - ipProto2Name(flow->protocol), - flow->lower_name, ntohs(flow->lower_port), - flow->upper_name, ntohs(flow->upper_port)); + fprintf(out, "\t%s %s%s%s:%u <-> %s%s%s:%u ", + ipProto2Name(flow->protocol), + (flow->ip_version == 6) ? "[" : "", + flow->lower_name, + (flow->ip_version == 6) ? "]" : "", + ntohs(flow->lower_port), + (flow->ip_version == 6) ? "[" : "", + flow->upper_name, + (flow->ip_version == 6) ? "]" : "", + ntohs(flow->upper_port)); if(flow->vlan_id > 0) fprintf(out, "[VLAN: %u]", flow->vlan_id); @@ -505,14 +523,13 @@ static void printFlow(u_int16_t thread_id, struct ndpi_flow *flow) { ndpi_get_proto_name(ndpi_thread_info[thread_id].ndpi_struct, flow->detected_protocol.protocol)); fprintf(out, "[%u pkts/%llu bytes]", - flow->packets, (long long unsigned int)flow->bytes); + flow->packets, (long long unsigned int)flow->bytes); if(flow->host_server_name[0] != '\0') fprintf(out, "[Host: %s]", flow->host_server_name); if(flow->ssl.client_certificate[0] != '\0') fprintf(out, "[SSL client: %s]", flow->ssl.client_certificate); if(flow->ssl.server_certificate[0] != '\0') fprintf(out, "[SSL server: %s]", flow->ssl.server_certificate); fprintf(out, "\n"); -#endif } else { #ifdef HAVE_JSON_C jObj = json_object_new_object(); @@ -633,16 +650,6 @@ static void node_proto_guess_walker(const void *node, ndpi_VISIT which, int dept struct ndpi_flow *flow = *(struct ndpi_flow **) node; u_int16_t thread_id = *((u_int16_t *) user_data); -#if 0 - printf("<%d>Walk on node %s (%p)\n", - depth, - which == preorder?"preorder": - which == postorder?"postorder": - which == endorder?"endorder": - which == leaf?"leaf": "unknown", - flow); -#endif - if((which == ndpi_preorder) || (which == ndpi_leaf)) { /* Avoid walking the same node multiple times */ if(enable_protocol_guess) { if(flow->detected_protocol.protocol == NDPI_PROTOCOL_UNKNOWN) { @@ -706,23 +713,27 @@ static struct ndpi_flow *get_ndpi_flow(u_int16_t thread_id, const u_int8_t version, u_int16_t vlan_id, const struct ndpi_iphdr *iph, + const struct ndpi_ipv6hdr *iph6, u_int16_t ip_offset, u_int16_t ipsize, u_int16_t l4_packet_len, + struct ndpi_tcphdr **tcph, + struct ndpi_udphdr **udph, + u_int16_t *sport, u_int16_t *dport, struct ndpi_id_struct **src, struct ndpi_id_struct **dst, u_int8_t *proto, - const struct ndpi_ip6_hdr *iph6) { + u_int8_t **payload, + u_int16_t *payload_len, + u_int8_t *src_to_dst_direction) { u_int32_t idx, l4_offset; - struct ndpi_tcphdr *tcph = NULL; - struct ndpi_udphdr *udph = NULL; u_int32_t lower_ip; u_int32_t upper_ip; u_int16_t lower_port; u_int16_t upper_port; struct ndpi_flow flow; void *ret; - u_int8_t *l3; + u_int8_t *l3, *l4; /* Note: to keep things simple (ndpiReader is just a demo app) @@ -739,7 +750,7 @@ static struct ndpi_flow *get_ndpi_flow(u_int16_t thread_id, l4_offset = iph->ihl * 4; l3 = (u_int8_t*)iph; } else { - l4_offset = sizeof(struct ndpi_ip6_hdr); + l4_offset = sizeof(struct ndpi_ipv6hdr); l3 = (u_int8_t*)iph6; } @@ -768,19 +779,25 @@ static struct ndpi_flow *get_ndpi_flow(u_int16_t thread_id, } *proto = iph->protocol; + l4 = ((u_int8_t *) l3 + l4_offset); if(iph->protocol == 6 && l4_packet_len >= 20) { + u_int tcp_len; + ndpi_thread_info[thread_id].stats.tcp_count++; // tcp - tcph = (struct ndpi_tcphdr *) ((u_int8_t *) l3 + l4_offset); + *tcph = (struct ndpi_tcphdr *)l4; + *sport = ntohs((*tcph)->source), *dport = ntohs((*tcph)->dest); + if(iph->saddr < iph->daddr) { - lower_port = tcph->source; - upper_port = tcph->dest; + lower_port = (*tcph)->source, upper_port = (*tcph)->dest; + *src_to_dst_direction = 1; } else { - lower_port = tcph->dest; - upper_port = tcph->source; + lower_port = (*tcph)->dest; + upper_port = (*tcph)->source; + *src_to_dst_direction = 0; if(iph->saddr == iph->daddr) { if(lower_port > upper_port) { u_int16_t p = lower_port; @@ -790,18 +807,38 @@ static struct ndpi_flow *get_ndpi_flow(u_int16_t thread_id, } } } + + tcp_len = ndpi_min(4*(*tcph)->doff, l4_packet_len); + *payload = &l4[tcp_len]; + *payload_len = ndpi_max(0, l4_packet_len-4*(*tcph)->doff); } else if(iph->protocol == 17 && l4_packet_len >= 8) { // udp ndpi_thread_info[thread_id].stats.udp_count++; - udph = (struct ndpi_udphdr *) ((u_int8_t *) l3 + l4_offset); + *udph = (struct ndpi_udphdr *)l4; + *sport = ntohs((*udph)->source), *dport = ntohs((*udph)->dest); + *payload = &l4[sizeof(struct ndpi_udphdr)]; + *payload_len = ndpi_max(0, l4_packet_len-sizeof(struct ndpi_udphdr)); + if(iph->saddr < iph->daddr) { - lower_port = udph->source; - upper_port = udph->dest; + lower_port = (*udph)->source, upper_port = (*udph)->dest; + *src_to_dst_direction = 1; } else { - lower_port = udph->dest; - upper_port = udph->source; + lower_port = (*udph)->dest, upper_port = (*udph)->source; + + *src_to_dst_direction = 0; + + if(iph->saddr == iph->daddr) { + if(lower_port > upper_port) { + u_int16_t p = lower_port; + + lower_port = upper_port; + upper_port = p; + } + } } + + *sport = ntohs(lower_port), *dport = ntohs(upper_port); } else { // non tcp/udp protocols lower_port = 0; @@ -835,6 +872,7 @@ static struct ndpi_flow *get_ndpi_flow(u_int16_t thread_id, newflow->protocol = iph->protocol, newflow->vlan_id = vlan_id; newflow->lower_ip = lower_ip, newflow->upper_ip = upper_ip; newflow->lower_port = lower_port, newflow->upper_port = upper_port; + newflow->ip_version = version; if(version == 4) { inet_ntop(AF_INET, &lower_ip, newflow->lower_name, sizeof(newflow->lower_name)); @@ -871,8 +909,7 @@ static struct ndpi_flow *get_ndpi_flow(u_int16_t thread_id, *src = newflow->src_id, *dst = newflow->dst_id; // printFlow(thread_id, newflow); - - return newflow ; + return newflow; } } else { struct ndpi_flow *flow = *(struct ndpi_flow**)ret; @@ -891,29 +928,36 @@ static struct ndpi_flow *get_ndpi_flow(u_int16_t thread_id, static struct ndpi_flow *get_ndpi_flow6(u_int16_t thread_id, u_int16_t vlan_id, - const struct ndpi_ip6_hdr *iph6, + const struct ndpi_ipv6hdr *iph6, u_int16_t ip_offset, + struct ndpi_tcphdr **tcph, + struct ndpi_udphdr **udph, + u_int16_t *sport, u_int16_t *dport, struct ndpi_id_struct **src, struct ndpi_id_struct **dst, - u_int8_t *proto) { + u_int8_t *proto, + u_int8_t **payload, + u_int16_t *payload_len, + u_int8_t *src_to_dst_direction) { struct ndpi_iphdr iph; memset(&iph, 0, sizeof(iph)); iph.version = 4; - iph.saddr = iph6->ip6_src.__u6_addr.__u6_addr32[2] + iph6->ip6_src.__u6_addr.__u6_addr32[3]; - iph.daddr = iph6->ip6_dst.__u6_addr.__u6_addr32[2] + iph6->ip6_dst.__u6_addr.__u6_addr32[3]; + iph.saddr = iph6->ip6_src.u6_addr.u6_addr32[2] + iph6->ip6_src.u6_addr.u6_addr32[3]; + iph.daddr = iph6->ip6_dst.u6_addr.u6_addr32[2] + iph6->ip6_dst.u6_addr.u6_addr32[3]; iph.protocol = iph6->ip6_ctlun.ip6_un1.ip6_un1_nxt; if(iph.protocol == 0x3C /* IPv6 destination option */) { - u_int8_t *options = (u_int8_t*)iph6 + sizeof(const struct ndpi_ip6_hdr); + u_int8_t *options = (u_int8_t*)iph6 + sizeof(const struct ndpi_ipv6hdr); iph.protocol = options[0]; } - return(get_ndpi_flow(thread_id, 6, vlan_id, &iph, ip_offset, - sizeof(struct ndpi_ip6_hdr), + return(get_ndpi_flow(thread_id, 6, vlan_id, &iph, iph6, ip_offset, + sizeof(struct ndpi_ipv6hdr), ntohs(iph6->ip6_ctlun.ip6_un1.ip6_un1_plen), - src, dst, proto, iph6)); + tcph, udph, sport, dport, + src, dst, proto, payload, payload_len, src_to_dst_direction)); } /* ***************************************************** */ @@ -938,8 +982,8 @@ static void setupDetection(u_int16_t thread_id) { ndpi_set_protocol_detection_bitmask2(ndpi_thread_info[thread_id].ndpi_struct, &all); // allocate memory for id and flow tracking - size_id_struct = ndpi_detection_get_sizeof_ndpi_id_struct(); - size_flow_struct = ndpi_detection_get_sizeof_ndpi_flow_struct(); + size_id_struct = sizeof(struct ndpi_id_struct); + size_flow_struct = sizeof(struct ndpi_flow_struct); // clear memory for results memset(ndpi_thread_info[thread_id].stats.protocol_counter, 0, sizeof(ndpi_thread_info[thread_id].stats.protocol_counter)); @@ -970,20 +1014,31 @@ static unsigned int packet_processing(u_int16_t thread_id, const u_int64_t time, u_int16_t vlan_id, const struct ndpi_iphdr *iph, - struct ndpi_ip6_hdr *iph6, + struct ndpi_ipv6hdr *iph6, u_int16_t ip_offset, u_int16_t ipsize, u_int16_t rawsize) { struct ndpi_id_struct *src, *dst; struct ndpi_flow *flow; struct ndpi_flow_struct *ndpi_flow = NULL; u_int8_t proto; - + struct ndpi_tcphdr *tcph = NULL; + struct ndpi_udphdr *udph = NULL; + u_int16_t sport, dport, payload_len; + u_int8_t *payload; + u_int8_t src_to_dst_direction= 1; + if(iph) - flow = get_ndpi_flow(thread_id, 4, vlan_id, iph, ip_offset, ipsize, + flow = get_ndpi_flow(thread_id, 4, vlan_id, iph, NULL, + ip_offset, ipsize, ntohs(iph->tot_len) - (iph->ihl * 4), - &src, &dst, &proto, NULL); + &tcph, &udph, &sport, &dport, + &src, &dst, &proto, + &payload, &payload_len, &src_to_dst_direction); else - flow = get_ndpi_flow6(thread_id, vlan_id, iph6, ip_offset, &src, &dst, &proto); + flow = get_ndpi_flow6(thread_id, vlan_id, iph6, ip_offset, + &tcph, &udph, &sport, &dport, + &src, &dst, &proto, + &payload, &payload_len, &src_to_dst_direction); if(flow != NULL) { ndpi_thread_info[thread_id].stats.ip_packet_count++; @@ -1000,7 +1055,7 @@ static unsigned int packet_processing(u_int16_t thread_id, flow->detected_protocol = ndpi_detection_process_packet(ndpi_thread_info[thread_id].ndpi_struct, ndpi_flow, iph ? (uint8_t *)iph : (uint8_t *)iph6, ipsize, time, src, dst); - + if((flow->detected_protocol.protocol != NDPI_PROTOCOL_UNKNOWN) || ((proto == IPPROTO_UDP) && (flow->packets > 8)) || ((proto == IPPROTO_TCP) && (flow->packets > 10))) { @@ -1016,31 +1071,6 @@ static unsigned int packet_processing(u_int16_t thread_id, snprintf(flow->ssl.server_certificate, sizeof(flow->ssl.server_certificate), "%s", flow->ndpi_flow->protos.ssl.server_certificate); } -#if 0 - if(verbose > 1) { - if(ndpi_is_proto(flow->detected_protocol, NDPI_PROTOCOL_HTTP)) { - char *method; - - printf("[URL] %s\n", ndpi_get_http_url(ndpi_thread_info[thread_id].ndpi_struct, ndpi_flow)); - printf("[Content-Type] %s\n", ndpi_get_http_content_type(ndpi_thread_info[thread_id].ndpi_struct, ndpi_flow)); - - switch(ndpi_get_http_method(ndpi_thread_info[thread_id].ndpi_struct, ndpi_flow)) { - case HTTP_METHOD_OPTIONS: method = "HTTP_METHOD_OPTIONS"; break; - case HTTP_METHOD_GET: method = "HTTP_METHOD_GET"; break; - case HTTP_METHOD_HEAD: method = "HTTP_METHOD_HEAD"; break; - case HTTP_METHOD_POST: method = "HTTP_METHOD_POST"; break; - case HTTP_METHOD_PUT: method = "HTTP_METHOD_PUT"; break; - case HTTP_METHOD_DELETE: method = "HTTP_METHOD_DELETE"; break; - case HTTP_METHOD_TRACE: method = "HTTP_METHOD_TRACE"; break; - case HTTP_METHOD_CONNECT: method = "HTTP_METHOD_CONNECT"; break; - default: method = "HTTP_METHOD_UNKNOWN"; break; - } - - printf("[Method] %s\n", method); - } - } -#endif - free_ndpi_flow(flow); if(verbose > 1) { @@ -1055,11 +1085,6 @@ static unsigned int packet_processing(u_int16_t thread_id, } } -#if 0 - if(ndpi_flow->l4.tcp.host_server_name[0] != '\0') - printf("%s\n", ndpi_flow->l4.tcp.host_server_name); -#endif - if(live_capture) { if(ndpi_thread_info[thread_id].last_idle_scan_time + IDLE_SCAN_PERIOD < ndpi_thread_info[thread_id].last_time) { /* scan for idle flows */ @@ -1525,86 +1550,229 @@ static void openPcapFileOrDevice(u_int16_t thread_id) { /* ***************************************************** */ -static void pcap_packet_callback(u_char *args, const struct pcap_pkthdr *header, const u_char *packet) { +static void pcap_packet_callback(u_char *args, + const struct pcap_pkthdr *header, + const u_char *packet) { + + /* + * Declare pointers to packet headers + */ + + /* --- Ethernet header --- */ const struct ndpi_ethhdr *ethernet; + /* --- Ethernet II header --- */ + const struct ndpi_ethhdr *ethernet_2; + /* --- LLC header --- */ + const struct ndpi_llc_header *llc; + + /* --- Cisco HDLC header --- */ + const struct ndpi_chdlc *chdlc; + /* --- SLARP frame --- */ + struct ndpi_slarp *slarp; + /* --- CDP --- */ + struct ndpi_cdp *cdp; + + /* --- Radio Tap header --- */ + const struct ndpi_radiotap_header *radiotap; + /* --- Wifi header --- */ + const struct ndpi_wifi_header *wifi; + + /* --- MPLS header --- */ + struct ndpi_mpls_header *mpls; + + /** --- IP header --- **/ struct ndpi_iphdr *iph; - struct ndpi_ip6_hdr *iph6; + /** --- IPv6 header --- **/ + struct ndpi_ipv6hdr *iph6; + + /* lengths and offsets */ + u_int16_t eth_offset = 0; + u_int16_t radio_len; + u_int16_t fc; + u_int16_t type; + int wifi_len; + int llc_off; + int pyld_eth_len = 0; + int check; + u_int32_t fcs; + u_int64_t time; - u_int16_t type, ip_offset, ip_len; + u_int16_t ip_offset, ip_len, ip6_offset; u_int16_t frag_off = 0, vlan_id = 0; - u_int8_t proto = 0, vlan_packet = 0; + u_int8_t proto = 0; + u_int32_t label; + u_int16_t thread_id = *((u_int16_t*)args); - // printf("[ndpiReader] pcap_packet_callback : [%u.%u.%u.%u.%u -> %u.%u.%u.%u.%u]\n", ethernet->h_dest[1],ethernet->h_dest[2],ethernet->h_dest[3],ethernet->h_dest[4],ethernet->h_dest[5],ethernet->h_source[1],ethernet->h_source[2],ethernet->h_source[3],ethernet->h_source[4],ethernet->h_source[5]); + /* counters */ + u_int8_t malformed_pkts = 0, vlan_packet = 0; + u_int8_t slarp_pkts = 0, cdp_pkts = 0; + + /* Increment raw packet counter */ ndpi_thread_info[thread_id].stats.raw_packet_count++; if((capture_until != 0) && (header->ts.tv_sec >= capture_until)) { if(ndpi_thread_info[thread_id]._pcap_handle != NULL) pcap_breakloop(ndpi_thread_info[thread_id]._pcap_handle); - return; } + /* Check if capture is live or not */ if (!live_capture) { if (!pcap_start.tv_sec) pcap_start.tv_sec = header->ts.tv_sec, pcap_start.tv_usec = header->ts.tv_usec; pcap_end.tv_sec = header->ts.tv_sec, pcap_end.tv_usec = header->ts.tv_usec; } + /* setting time */ time = ((uint64_t) header->ts.tv_sec) * detection_tick_resolution + header->ts.tv_usec / (1000000 / detection_tick_resolution); - if(ndpi_thread_info[thread_id].last_time > time) { /* safety check */ - // printf("\nWARNING: timestamp bug in the pcap file (ts delta: %llu, repairing)\n", ndpi_thread_info[thread_id].last_time - time); + /* safety check */ + if(ndpi_thread_info[thread_id].last_time > time) { + /* printf("\nWARNING: timestamp bug in the pcap file (ts delta: %llu, repairing)\n", ndpi_thread_info[thread_id].last_time - time); */ time = ndpi_thread_info[thread_id].last_time; } + /* update last time value */ ndpi_thread_info[thread_id].last_time = time; - if(ndpi_thread_info[thread_id]._pcap_datalink_type == DLT_NULL) { - if(ntohl(*((u_int32_t*)packet)) == 2) - type = ETH_P_IP; - else - type = 0x86DD; /* IPv6 */ + /*** check Data Link type ***/ + int datalink_type = ndpi_thread_info[thread_id]._pcap_datalink_type; - ip_offset = 4; - } else if(ndpi_thread_info[thread_id]._pcap_datalink_type == DLT_EN10MB) { - ethernet = (struct ndpi_ethhdr *) packet; - ip_offset = sizeof(struct ndpi_ethhdr); - type = ntohs(ethernet->h_proto); - } else if(ndpi_thread_info[thread_id]._pcap_datalink_type == 113 /* Linux Cooked Capture */) { - type = (packet[14] << 8) + packet[15]; - ip_offset = 16; - } else - return; + datalink_check: + switch(datalink_type) { - while(1) { - if(type == 0x8100 /* VLAN */) { - vlan_id = ((packet[ip_offset] << 8) + packet[ip_offset+1]) & 0xFFF; - type = (packet[ip_offset+2] << 8) + packet[ip_offset+3]; - ip_offset += 4; - vlan_packet = 1; - } else if(type == 0x8847 /* MPLS */) { - u_int32_t label = ntohl(*((u_int32_t*)&packet[ip_offset])); + case DLT_NULL : + if(ntohl(*((u_int32_t*)&packet[eth_offset])) == 2) + type = ETH_P_IP; + else + type = ETH_P_IPV6; - ndpi_thread_info[thread_id].stats.mpls_count++; - type = 0x800, ip_offset += 4; + ip_offset = 4 + eth_offset; - while((label & 0x100) != 0x100) { - ip_offset += 4; - label = ntohl(*((u_int32_t*)&packet[ip_offset])); - } - } else if(type == 0x8864 /* PPPoE */) { - ndpi_thread_info[thread_id].stats.pppoe_count++; - type = 0x0800; - ip_offset += 8; - } else + /* Cisco PPP in HDLC-like framing - 50 */ + case DLT_PPP_SERIAL: + chdlc = (struct ndpi_chdlc *) &packet[eth_offset]; + ip_offset = sizeof(struct ndpi_chdlc); /* CHDLC_OFF = 4 */ + type = ntohs(chdlc->proto_code); break; - } + /* Cisco PPP with HDLC framing - 104 */ + case DLT_C_HDLC: + chdlc = (struct ndpi_chdlc *) &packet[eth_offset]; + ip_offset = sizeof(struct ndpi_chdlc); /* CHDLC_OFF = 4 */ + type = ntohs(chdlc->proto_code); + break; + + /* IEEE 802.3 Ethernet - 1 */ + case DLT_EN10MB : + ethernet = (struct ndpi_ethhdr *) &packet[eth_offset]; + ip_offset = sizeof(struct ndpi_ethhdr) + eth_offset; + check = ntohs(ethernet->h_proto); + + if(check <= 1500) + pyld_eth_len = check; + else if (check >= 1536) + type = check; + + if(pyld_eth_len != 0) { + /* check for LLC layer with SNAP extension */ + if(packet[ip_offset] == SNAP) { + llc = (struct ndpi_llc_header *)(&packet[ip_offset]); + type = llc->snap.proto_ID; + ip_offset += + 8; + } + } + break; + + /* Linux Cooked Capture - 113 */ + case DLT_LINUX_SLL : + type = (packet[eth_offset+14] << 8) + packet[eth_offset+15]; + ip_offset = 16 + eth_offset; + break; + + /* Radiotap link-layer - 127 */ + case DLT_IEEE802_11_RADIO : + radiotap = (struct ndpi_radiotap_header *) &packet[eth_offset]; + radio_len = radiotap->len; + + /* Check Bad FCS presence */ + if((radiotap->flags & BAD_FCS) == BAD_FCS) { + malformed_pkts += 1; + ndpi_thread_info[thread_id].stats.total_discarded_bytes += header->len; + return; + } + + fcs = header->len - 4; + + /* Calculate 802.11 header length (variable) */ + wifi = (struct ndpi_wifi_header*)( packet + eth_offset + radio_len); + fc = wifi->fc; + + /* check wifi data presence */ + if(FCF_TYPE(fc) == WIFI_DATA) { + if((FCF_TO_DS(fc) && FCF_FROM_DS(fc) == 0x0) || + (FCF_TO_DS(fc) == 0x0 && FCF_FROM_DS(fc))) + wifi_len = 26; /* + 4 byte fcs */ + } else /* no data frames */ + break; + + /* Check ether_type from LLC */ + llc = (struct ndpi_llc_header*)(packet + eth_offset + wifi_len + radio_len); + if(llc->dsap == SNAP) + type = ntohs(llc->snap.proto_ID); + + /* Set IP header offset */ + ip_offset = wifi_len + radio_len + sizeof(struct ndpi_llc_header) + eth_offset; + break; + + default: + return; + } + + /* check ether type */ + if(type == VLAN) { + vlan_id = ((packet[ip_offset] << 8) + packet[ip_offset+1]) & 0xFFF; + type = (packet[ip_offset+2] << 8) + packet[ip_offset+3]; + ip_offset += 4; + vlan_packet = 1; + } + else if(type == MPLS_UNI || type == MPLS_MULTI) { + + mpls = (struct ndpi_mpls_header *) &packet[ip_offset]; + label = ntohl(mpls->label); + /* label = ntohl(*((u_int32_t*)&packet[ip_offset])); */ + ndpi_thread_info[thread_id].stats.mpls_count++; + type = ETH_P_IP, ip_offset += 4; + + while((label & 0x100) != 0x100) { + ip_offset += 4; + label = ntohl(mpls->label); + } + } + else if(type == SLARP) { + slarp = (struct ndpi_slarp *) &packet[ip_offset]; + if(slarp->slarp_type == 0x02 || slarp->slarp_type == 0x00 || slarp->slarp_type == 0x01) { + /* TODO if info are needed */ + } + slarp_pkts++; + } + else if(type == CISCO_D_PROTO) { + cdp = (struct ndpi_cdp *) &packet[ip_offset]; + cdp_pkts++; + } + else if(type == PPPoE) { + ndpi_thread_info[thread_id].stats.pppoe_count++; + type = ETH_P_IP; + ip_offset += 8; + } + ndpi_thread_info[thread_id].stats.vlan_count += vlan_packet; + iph_check: + /* Check and set IP header size and total packet length */ iph = (struct ndpi_iphdr *) &packet[ip_offset]; - // just work on Ethernet packets that contain IP + /* just work on Ethernet packets that contain IP */ if(type == ETH_P_IP && header->caplen >= ip_offset) { frag_off = ntohs(iph->frag_off); @@ -1623,10 +1791,15 @@ static void pcap_packet_callback(u_char *args, const struct pcap_pkthdr *header, ip_len = ((u_short)iph->ihl * 4); iph6 = NULL; + if(iph->protocol == 41) { + ip_offset += ip_len; + goto iph_check; + } + if((frag_off & 0x3FFF) != 0) { static u_int8_t ipv4_frags_warning_used = 0; - ndpi_thread_info[thread_id].stats.fragmented_count++; + if(ipv4_frags_warning_used == 0) { if((!json_flag) && (!quiet_mode)) printf("\n\nWARNING: IPv4 fragments are not handled by this demo (nDPI supports them)\n"); ipv4_frags_warning_used = 1; @@ -1636,27 +1809,27 @@ static void pcap_packet_callback(u_char *args, const struct pcap_pkthdr *header, return; } } else if(iph->version == 6) { - iph6 = (struct ndpi_ip6_hdr *)&packet[ip_offset]; + iph6 = (struct ndpi_ipv6hdr *)&packet[ip_offset]; proto = iph6->ip6_ctlun.ip6_un1.ip6_un1_nxt; - ip_len = sizeof(struct ndpi_ip6_hdr); + ip_len = sizeof(struct ndpi_ipv6hdr); if(proto == 0x3C /* IPv6 destination option */) { - u_int8_t *options = (u_int8_t*)&packet[ip_offset+ip_len]; + u_int8_t *options = (u_int8_t*)&packet[ip_offset+ip_len]; proto = options[0]; ip_len += 8 * (options[1] + 1); } - iph = NULL; + } else { static u_int8_t ipv4_warning_used = 0; v4_warning: if(ipv4_warning_used == 0) { - if((!json_flag) && (!quiet_mode)) printf("\n\nWARNING: only IPv4/IPv6 packets are supported in this demo (nDPI supports both IPv4 and IPv6), all other packets will be discarded\n\n"); + if((!json_flag) && (!quiet_mode)) + printf("\n\nWARNING: only IPv4/IPv6 packets are supported in this demo (nDPI supports both IPv4 and IPv6), all other packets will be discarded\n\n"); ipv4_warning_used = 1; } - ndpi_thread_info[thread_id].stats.total_discarded_bytes += header->len; return; } @@ -1671,9 +1844,10 @@ static void pcap_packet_callback(u_char *args, const struct pcap_pkthdr *header, u_int8_t flags = packet[offset]; u_int8_t message_type = packet[offset+1]; - if((((flags & 0xE0) >> 5) == 1 /* GTPv1 */) && (message_type == 0xFF /* T-PDU */)) { - ip_offset = ip_offset+ip_len+sizeof(struct ndpi_udphdr)+8 /* GTPv1 header len */; + if((((flags & 0xE0) >> 5) == 1 /* GTPv1 */) && + (message_type == 0xFF /* T-PDU */)) { + ip_offset = ip_offset+ip_len+sizeof(struct ndpi_udphdr)+8; /* GTPv1 header len */ if(flags & 0x04) ip_offset += 1; /* next_ext_header is present */ if(flags & 0x02) ip_offset += 4; /* sequence_number is present (it also includes next_ext_header and pdu_number) */ if(flags & 0x01) ip_offset += 1; /* pdu_number is present */ @@ -1685,10 +1859,48 @@ static void pcap_packet_callback(u_char *args, const struct pcap_pkthdr *header, goto v4_warning; } } + } else if((sport == TZSP_PORT) || (dport == TZSP_PORT)) { + /* https://en.wikipedia.org/wiki/TZSP */ + u_int offset = ip_offset+ip_len+sizeof(struct ndpi_udphdr); + u_int8_t version = packet[offset]; + u_int8_t type = packet[offset+1]; + u_int16_t encapsulates = ntohs(*((u_int16_t*)&packet[offset+2])); + + if((version == 1) && (type == 0) && (encapsulates == 1)) { + u_int8_t stop = 0; + + offset += 4; + + while((!stop) && (offset < header->caplen)) { + u_int8_t tag_type = packet[offset]; + u_int8_t tag_len; + + switch(tag_type) { + case 0: /* PADDING Tag */ + tag_len = 1; + break; + case 1: /* END Tag */ + tag_len = 1, stop = 1; + break; + default: + tag_len = packet[offset+1]; + break; + } + + offset += tag_len; + + if(offset >= header->caplen) + return; /* Invalid packet */ + else { + eth_offset = offset; + goto datalink_check; + } + } + } } } - // process the packet + /* process the packet */ packet_processing(thread_id, time, vlan_id, iph, iph6, ip_offset, header->len - ip_offset, header->len); } @@ -1823,16 +2035,6 @@ struct timezone { /* ***************************************************** */ -#if 0 -int gettimeofday(struct timeval *tv, void *notUsed) { - tv->tv_sec = time(NULL); - tv->tv_usec = 0; - return(0); -} -#endif - -/* ***************************************************** */ - int gettimeofday(struct timeval *tv, struct timezone *tz) { FILETIME ft; LARGE_INTEGER li; diff --git a/src/include/Makefile.am b/src/include/Makefile.am index 71a507ae2..cd901f58b 100644 --- a/src/include/Makefile.am +++ b/src/include/Makefile.am @@ -1,9 +1,10 @@ - library_includedir=$(includedir)/libndpi-1.4/libndpi library_include_HEADERS = ndpi_api.h \ - ndpi_debug_functions.h \ - ndpi_define.h \ - ndpi_macros.h \ - ndpi_protocols_osdpi.h \ - ndpi_public_functions.h + ndpi_define.h \ + ndpi_typedefs.h \ + ndpi_main.h \ + ndpi_protocol_ids.h \ + ndpi_protocols.h \ + ndpi_win32.h \ + ndpi_includes.h diff --git a/src/include/linux_compat.h b/src/include/linux_compat.h deleted file mode 100644 index 38601f180..000000000 --- a/src/include/linux_compat.h +++ /dev/null @@ -1,188 +0,0 @@ -/* - * linux_compat.h - * - * Copyright (C) 2009-2011 by ipoque GmbH - * Copyright (C) 2011-15 - ntop.org - * - * This file is part of nDPI, an open source deep packet inspection - * library based on the OpenDPI and PACE technology by ipoque GmbH - * - * nDPI is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * nDPI is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with nDPI. If not, see . - * - */ - - -#ifndef __NDPI_LINUX_COMPAT_H__ -#define __NDPI_LINUX_COMPAT_H__ - -#include "ndpi_define.h" - -#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) -#include - -#if _BYTE_ORDER == _LITTLE_ENDIAN -#ifndef __LITTLE_ENDIAN__ -#define __LITTLE_ENDIAN__ 1 -#endif -#else -#ifndef __BIG_ENDIAN__ -#define __BIG_ENDIAN__ 1 -#endif -#endif -#endif - -#pragma pack(push, 1) /* push current alignment to stack */ -#pragma pack(1) /* set alignment to 1 byte boundary */ - -#pragma pack(pop) /* restore original alignment from stack */ - -struct ndpi_ethhdr { - u_char h_dest[6]; /* destination eth addr */ - u_char h_source[6]; /* source ether addr */ - u_int16_t h_proto; /* packet type ID field */ -}; - -struct ndpi_80211q { - u_int16_t vlanId; - u_int16_t protoType; -}; - -struct ndpi_iphdr { -#if defined(__LITTLE_ENDIAN__) - u_int8_t ihl:4, version:4; -#elif defined(__BIG_ENDIAN__) - u_int8_t version:4, ihl:4; -#else -# error "Byte order must be defined" -#endif - u_int8_t tos; - u_int16_t tot_len; - u_int16_t id; - u_int16_t frag_off; - u_int8_t ttl; - u_int8_t protocol; - u_int16_t check; - u_int32_t saddr; - u_int32_t daddr; -}; - - -#ifdef WIN32 - -typedef unsigned char u_char; -typedef unsigned short u_short; -typedef unsigned int uint; -typedef unsigned long u_long; -typedef u_char u_int8_t; -typedef u_short u_int16_t; -typedef uint u_int32_t; - -#define _WS2TCPIP_H_ /* Avoid compilation problems */ -#define HAVE_SIN6_LEN - - -/* IPv6 address */ -/* Already defined in WS2tcpip.h */ -struct ndpi_win_in6_addr -{ - union - { - u_int8_t u6_addr8[16]; - u_int16_t u6_addr16[8]; - u_int32_t u6_addr32[4]; - } in6_u; -#ifdef s6_addr -#undef s6_addr -#endif - -#ifdef s6_addr16 -#undef s6_addr16 -#endif - -#ifdef s6_addr32 -#undef s6_addr32 -#endif - -#define s6_addr in6_u.u6_addr8 - // #define s6_addr16 in6_u.u6_addr16 - // #define s6_addr32 in6_u.u6_addr32 - -}; - -#define in6_addr win_in6_addr - -/* Generic extension header. */ -struct ndpi_ip6_ext -{ - u_int8_t ip6e_nxt; /* next header. */ - u_int8_t ip6e_len; /* length in units of 8 octets. */ -}; - -#define s6_addr16 __u6_addr.__u6_addr16 -#define s6_addr32 __u6_addr.__u6_addr32 -#else -#ifndef __KERNEL__ -#include -#endif -#endif - - - -struct ndpi_in6_addr { - union { - u_int8_t __u6_addr8[16]; - u_int16_t __u6_addr16[8]; - u_int32_t __u6_addr32[4]; - } __u6_addr; /* 128-bit IP6 address */ -}; - -struct ndpi_ip6_hdr { - union { - struct ndpi_ip6_hdrctl { - u_int32_t ip6_un1_flow; - u_int16_t ip6_un1_plen; - u_int8_t ip6_un1_nxt; - u_int8_t ip6_un1_hlim; - } ip6_un1; - u_int8_t ip6_un2_vfc; - } ip6_ctlun; - struct ndpi_in6_addr ip6_src; - struct ndpi_in6_addr ip6_dst; -}; - -struct ndpi_tcphdr { - u_int16_t source; - u_int16_t dest; - u_int32_t seq; - u_int32_t ack_seq; -#if defined(__LITTLE_ENDIAN__) - u_int16_t res1:4, doff:4, fin:1, syn:1, rst:1, psh:1, ack:1, urg:1, ece:1, cwr:1; -#elif defined(__BIG_ENDIAN__) - u_int16_t doff:4, res1:4, cwr:1, ece:1, urg:1, ack:1, psh:1, rst:1, syn:1, fin:1; -#else -# error "Byte order must be defined" -#endif - u_int16_t window; - u_int16_t check; - u_int16_t urg_ptr; -}; - -struct ndpi_udphdr { - u_int16_t source; - u_int16_t dest; - u_int16_t len; - u_int16_t check; -}; - -#endif diff --git a/src/include/ndpi_api.h b/src/include/ndpi_api.h index 30948706b..c788dcb29 100644 --- a/src/include/ndpi_api.h +++ b/src/include/ndpi_api.h @@ -23,8 +23,8 @@ */ -#ifndef __NDPI_PUBLIC_FUNCTIONS_H__ -#define __NDPI_PUBLIC_FUNCTIONS_H__ +#ifndef __NDPI_API_H__ +#define __NDPI_API_H__ #include "ndpi_main.h" @@ -32,26 +32,26 @@ extern "C" { #endif - /** - * This function returns the size of the flow struct - * @return the size of the flow struct + /** + * This function returns the size of the flow struct + * @return the size of the flow struct */ u_int32_t ndpi_detection_get_sizeof_ndpi_flow_struct(void); - - /** - * This function returns the size of the id struct - * @return the size of the id struct + + /** + * This function returns the size of the id struct + * @return the size of the id struct */ u_int32_t ndpi_detection_get_sizeof_ndpi_id_struct(void); - /* Public malloc/free */ void* ndpi_malloc(unsigned long size); void* ndpi_calloc(unsigned long count, unsigned long size); void ndpi_free(void *ptr); void *ndpi_realloc(void *ptr, size_t old_size, size_t new_size); char *ndpi_strdup(const char *s); - /* + + /** * Find the first occurrence of find in s, where the search is limited to the * first slen characters of s. */ @@ -70,7 +70,7 @@ extern "C" { /** * This function returns a new initialized detection module. * @param ticks_per_second the timestamp resolution per second (like 1000 for millisecond resolution) - * @param ndpi_malloc function pointer to a memory allocator + * @param __ndpi_malloc function pointer to a memory allocator * @param ndpi_debug_printf a function pointer to a debug output function, use NULL in productive envionments * @return the initialized detection module */ @@ -88,8 +88,8 @@ extern "C" { /** * This function enables cache support in nDPI used for some protocol such as Skype - * @param cache host name - * @param cache port + * @param host host name + * @param port port number */ void ndpi_enable_cache(struct ndpi_detection_module_struct *ndpi_mod, char* host, u_int port); @@ -150,14 +150,6 @@ extern "C" { */ u_int8_t ndpi_detection_get_l4(const u_int8_t * l3, u_int16_t l3_len, const u_int8_t ** l4_return, u_int16_t * l4_len_return, u_int8_t * l4_protocol_return, u_int32_t flags); - /** - * returns the real protocol for the flow of the last packet given to the detection. - * if no real protocol could be found, the unknown protocol will be returned. - * - * @param ndpi_struct the detection module - * @return the protocol id of the last real protocol found in the protocol history of the flow - */ - /* u_int16_t ndpi_detection_get_real_protocol_of_flow(struct ndpi_detection_module_struct *ndpi_struct, struct ndpi_flow_struct *flow); */ /** * returns true if the protocol history of the flow of the last packet given to the detection @@ -244,4 +236,5 @@ extern "C" { #ifdef __cplusplus } #endif -#endif + +#endif /* __NDPI_API_H__ */ diff --git a/src/include/ndpi_define.h b/src/include/ndpi_define.h index 4fcdc617f..3a4102255 100644 --- a/src/include/ndpi_define.h +++ b/src/include/ndpi_define.h @@ -42,15 +42,22 @@ #endif/* BYTE_ORDER */ #endif/* __OPENBSD__ */ -#if 0 -#ifndef NDPI_ENABLE_DEBUG_MESSAGES -#define NDPI_ENABLE_DEBUG_MESSAGES + +#if __BYTE_ORDER == __LITTLE_ENDIAN +#ifndef __LITTLE_ENDIAN__ +#define __LITTLE_ENDIAN__ +#endif +#else +#ifndef __BIG_ENDIAN__ +#define __BIG_ENDIAN__ #endif #endif #ifdef WIN32 +#ifndef __LITTLE_ENDIAN__ #define __LITTLE_ENDIAN__ 1 #endif +#endif #if !(defined(__LITTLE_ENDIAN__) || defined(__BIG_ENDIAN__)) #if defined(__mips__) @@ -59,13 +66,6 @@ #define __BIG_ENDIAN__ #endif -/* Kernel modules */ -#if defined(__LITTLE_ENDIAN) -#define __LITTLE_ENDIAN__ -#endif -#if defined(__BIG_ENDIAN) -#define __BIG_ENDIAN__ -#endif /* Everything else */ #if (defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__)) #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ @@ -165,11 +165,6 @@ #define MAX_PACKET_COUNTER 65000 #define MAX_DEFAULT_PORTS 5 -/********************** - * detection features * - **********************/ -/* #define NDPI_SELECT_DETECTION_WITH_REAL_PROTOCOL ( 1 << 0 ) */ - #define NDPI_DIRECTCONNECT_CONNECTION_IP_TICK_TIMEOUT 600 #define NDPI_IRC_CONNECTION_TIMEOUT 120 #define NDPI_GNUTELLA_CONNECTION_TIMEOUT 60 @@ -199,7 +194,7 @@ #else /* NDPI_ENABLE_DEBUG_MESSAGES */ -#if defined(WIN32) +#ifdef WIN32 #define NDPI_LOG(...) {} #else #define NDPI_LOG(proto, mod, log_level, args...) {} @@ -218,12 +213,7 @@ #define NDPI_COMPARE_IPV6_ADDRESS_STRUCTS(x,y) \ ((((u_int64_t *)(x))[0]) < (((u_int64_t *)(y))[0]) || ( (((u_int64_t *)(x))[0]) == (((u_int64_t *)(y))[0]) && (((u_int64_t *)(x))[1]) < (((u_int64_t *)(y))[1])) ) -#if !defined(__KERNEL__) && !defined(NDPI_IPTABLES_EXT) #define NDPI_NUM_BITS 256 -#else -/* custom protocols not supported */ -#define NDPI_NUM_BITS 192 -#endif #define NDPI_BITS /* 32 */ (sizeof(ndpi_ndpi_mask) * 8 /* number of bits in a byte */) /* bits per mask */ #define howmanybits(x, y) (((x)+((y)-1))/(y)) @@ -238,7 +228,7 @@ #define NDPI_NUM_FDS_BITS howmanybits(NDPI_NUM_BITS, NDPI_BITS) #define NDPI_PROTOCOL_BITMASK ndpi_protocol_bitmask_struct_t - + #define NDPI_BITMASK_ADD(a,b) NDPI_SET(&a,b) #define NDPI_BITMASK_DEL(a,b) NDPI_CLR(&a,b) #define NDPI_BITMASK_RESET(a) NDPI_ZERO(&a) @@ -246,7 +236,7 @@ #define NDPI_BITMASK_SET(a, b) { memcpy(&a, &b, sizeof(NDPI_PROTOCOL_BITMASK)); } /* this is a very very tricky macro *g*, - * the compiler will remove all shifts here if the protocol is static... + * the compiler will remove all shifts here if the protocol is static... */ #define NDPI_ADD_PROTOCOL_TO_BITMASK(bmask,value) NDPI_SET(&bmask,value) #define NDPI_DEL_PROTOCOL_FROM_BITMASK(bmask,value) NDPI_CLR(&bmask,value) @@ -289,21 +279,17 @@ #define get_l32(X,O) get_u_int32_t(X,O) #elif defined(__BIG_ENDIAN__) || defined(__BIG_ENDIAN) /* convert the bytes from big to little endian */ -#ifndef __KERNEL__ # define get_l16(X,O) bswap_16(get_u_int16_t(X,O)) # define get_l32(X,O) bswap_32(get_u_int32_t(X,O)) #else -# define get_l16(X,O) __cpu_to_le16(get_u_int16_t(X,O)) -# define get_l32(X,O) __cpu_to_le32(get_u_int32_t(X,O)) -#endif - -#else - #error "__BYTE_ORDER MUST BE DEFINED !" - #endif /* __BYTE_ORDER */ /* define memory callback function */ #define match_first_bytes(payload,st) (memcmp((payload),(st),(sizeof(st)-1))==0) +#if defined(WIN32) && !defined(snprintf) +#define snprintf _snprintf +#endif + #endif /* __NDPI_DEFINE_INCLUDE_FILE__ */ diff --git a/src/include/ndpi_includes.h b/src/include/ndpi_includes.h new file mode 100644 index 000000000..ce36a25f9 --- /dev/null +++ b/src/include/ndpi_includes.h @@ -0,0 +1,68 @@ +/* + * ndpi_includes.h + * + * Copyright (C) 2011-15 - ntop.org + * + * This file is part of nDPI, an open source deep packet inspection + * library based on the OpenDPI and PACE technology by ipoque GmbH + * + * nDPI is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * nDPI is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with nDPI. If not, see . + * + */ + +#ifndef __NDPI_INCLUDES_H__ +#define __NDPI_INCLUDES_H__ + +#include +#include +#include +#include +#include +#include +#include + +#ifdef WIN32 +#include "ndpi_win32.h" +#else +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#if !defined __APPLE__ && !defined __FreeBSD__ && !defined __NetBSD__ && !defined __OpenBSD__ +#include +#include + +#if defined __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__ +#include + +#if defined __NetBSD__ || defined __OpenBSD__ +#include + +#ifdef __OpenBSD__ +#include + +#endif +#endif +#endif +#endif + +#endif /* Win32 */ + +#endif /* __NDPI_INCLUDES_H__ */ diff --git a/src/include/ndpi_main.h b/src/include/ndpi_main.h index adec3edf1..a70f35c8d 100644 --- a/src/include/ndpi_main.h +++ b/src/include/ndpi_main.h @@ -22,53 +22,15 @@ * */ -#ifndef __NDPI_MAIN_INCLUDE_FILE__ -#define __NDPI_MAIN_INCLUDE_FILE__ +#ifndef __NDPI_MAIN_H__ +#define __NDPI_MAIN_H__ -#ifndef __KERNEL__ -#include -#include -#include -#include -#ifndef WIN32 -#include -#endif -#include -#include -#endif - - -#ifndef WIN32 -#ifndef __KERNEL__ -#include -#endif - -#if !defined __APPLE__ && !defined __FreeBSD__ && !defined __NetBSD__ && !defined __OpenBSD__ - -#ifndef __KERNEL__ -#include -#include -#else -#include -#include -#endif - -#endif - -/* default includes */ - -#ifndef __KERNEL__ -#include -#include -#endif -#endif - -#include "ndpi_win32.h" -#include "ndpi_unix.h" +#include "ndpi_includes.h" #include "ndpi_define.h" #include "ndpi_protocol_ids.h" #include "ndpi_typedefs.h" #include "ndpi_protocols.h" +#include "ndpi_api.h" void *ndpi_tdelete(const void * __restrict, void ** __restrict, @@ -103,58 +65,46 @@ void ndpi_set_detected_protocol(struct ndpi_detection_module_struct *ndpi_struct u_int16_t upper_detected_protocol, u_int16_t lower_detected_protocol); - -/* function to parse a packet which has line based information into a line based structure - * this function will also set some well known line pointers like: - * - host, user agent, empty line,.... - */ extern void ndpi_parse_packet_line_info(struct ndpi_detection_module_struct *ndpi_struct, struct ndpi_flow_struct *flow); extern void ndpi_parse_packet_line_info_any(struct ndpi_detection_module_struct *ndpi_struct, struct ndpi_flow_struct *flow); + extern u_int16_t ndpi_check_for_email_address(struct ndpi_detection_module_struct *ndpi_struct, struct ndpi_flow_struct *flow, u_int16_t counter); -extern void ndpi_int_change_packet_protocol(struct ndpi_detection_module_struct *ndpi_struct, - struct ndpi_flow_struct *flow, + +extern void ndpi_int_change_packet_protocol(struct ndpi_detection_module_struct *ndpi_struct, struct ndpi_flow_struct *flow, u_int16_t upper_detected_protocol, u_int16_t lower_detected_protocol); -extern void ndpi_int_change_protocol(struct ndpi_detection_module_struct *ndpi_struct, - struct ndpi_flow_struct *flow, +extern void ndpi_int_change_protocol(struct ndpi_detection_module_struct *ndpi_struct, struct ndpi_flow_struct *flow, u_int16_t upper_detected_protocol, u_int16_t lower_detected_protocol); -extern void ndpi_set_proto_defaults(struct ndpi_detection_module_struct *ndpi_mod, - ndpi_protocol_breed_t protoBreed, u_int16_t protoId, - u_int16_t tcp_alias_protoId[2], u_int16_t udp_alias_protoId[2], - char *protoName, +extern void ndpi_set_proto_defaults(struct ndpi_detection_module_struct *ndpi_mod, ndpi_protocol_breed_t protoBreed, u_int16_t protoId, + u_int16_t tcp_alias_protoId[2], u_int16_t udp_alias_protoId[2], char *protoName, ndpi_port_range *tcpDefPorts, ndpi_port_range *udpDefPorts); + extern void ndpi_int_reset_packet_protocol(struct ndpi_packet_struct *packet); extern void ndpi_int_reset_protocol(struct ndpi_flow_struct *flow); + extern int ndpi_packet_src_ip_eql(const struct ndpi_packet_struct *packet, const ndpi_ip_addr_t * ip); extern int ndpi_packet_dst_ip_eql(const struct ndpi_packet_struct *packet, const ndpi_ip_addr_t * ip); extern void ndpi_packet_src_ip_get(const struct ndpi_packet_struct *packet, ndpi_ip_addr_t * ip); extern void ndpi_packet_dst_ip_get(const struct ndpi_packet_struct *packet, ndpi_ip_addr_t * ip); + extern char *ndpi_get_ip_string(struct ndpi_detection_module_struct *ndpi_struct, const ndpi_ip_addr_t * ip); -extern char *ndpi_get_packet_src_ip_string(struct ndpi_detection_module_struct *ndpi_struct, - const struct ndpi_packet_struct *packet); +extern char *ndpi_get_packet_src_ip_string(struct ndpi_detection_module_struct *ndpi_struct, const struct ndpi_packet_struct *packet); extern char* ndpi_get_proto_by_id(struct ndpi_detection_module_struct *ndpi_mod, u_int id); + extern u_int16_t ndpi_guess_protocol_id(struct ndpi_detection_module_struct *ndpi_struct, u_int8_t proto, u_int16_t sport, u_int16_t dport); + extern u_int8_t ndpi_is_proto(ndpi_protocol p, u_int16_t proto); + extern u_int16_t ndpi_get_lower_proto(ndpi_protocol p); -extern int ndpi_get_protocol_id_master_proto(struct ndpi_detection_module_struct *ndpi_struct, - u_int16_t protocol_id, +extern int ndpi_get_protocol_id_master_proto(struct ndpi_detection_module_struct *ndpi_struct, u_int16_t protocol_id, u_int16_t** tcp_master_proto, u_int16_t** udp_master_proto); -extern u_int8_t ndpi_net_match(u_int32_t ip_to_check, - u_int32_t net, - u_int32_t num_bits); - -extern u_int8_t ndpi_ips_match(u_int32_t src, u_int32_t dst, - u_int32_t net, u_int32_t num_bits); - #ifdef NDPI_ENABLE_DEBUG_MESSAGES void ndpi_debug_get_last_log_function_line(struct ndpi_detection_module_struct *ndpi_struct, const char **file, const char **func, u_int32_t * line); #endif -#include "ndpi_api.h" - -#endif /* __NDPI_MAIN_INCLUDE_FILE__ */ +#endif /* __NDPI_MAIN_H__ */ diff --git a/src/include/ndpi_protocol_ids.h b/src/include/ndpi_protocol_ids.h index cfb5897ba..2bfa42d68 100644 --- a/src/include/ndpi_protocol_ids.h +++ b/src/include/ndpi_protocol_ids.h @@ -23,7 +23,7 @@ */ -#ifndef __NDPI_API_INCLUDE_FILE__ +#ifndef __NDPI_API_H__ #endif @@ -201,6 +201,11 @@ #define NDPI_PROTOCOL_WHATSAPP_VOICE 189 #define NDPI_PROTOCOL_STARCRAFT 213 /* Matteo Bracci */ #define NDPI_PROTOCOL_TEREDO 214 +#define NDPI_PROTOCOL_HEP 216 /* Sipcapture.org QXIP BV */ +#define NDPI_PROTOCOL_UBNTAC2 217 /* Ubiquity UBNT AirControl 2 - Thomas Fjellstrom */ +#define NDPI_PROTOCOL_MS_LYNC 218 + + #define NDPI_CONTENT_AVI 39 #define NDPI_CONTENT_FLASH 40 @@ -263,7 +268,7 @@ #define NDPI_SERVICE_HOTSPOT_SHIELD 215 /* UPDATE UPDATE UPDATE UPDATE UPDATE UPDATE UPDATE UPDATE UPDATE */ -#define NDPI_LAST_IMPLEMENTED_PROTOCOL NDPI_SERVICE_HOTSPOT_SHIELD +#define NDPI_LAST_IMPLEMENTED_PROTOCOL NDPI_PROTOCOL_MS_LYNC #define NDPI_MAX_SUPPORTED_PROTOCOLS (NDPI_LAST_IMPLEMENTED_PROTOCOL + 1) #define NDPI_MAX_NUM_CUSTOM_PROTOCOLS (NDPI_NUM_BITS-NDPI_LAST_IMPLEMENTED_PROTOCOL) diff --git a/src/include/ndpi_protocols.h b/src/include/ndpi_protocols.h index 6171f00bc..240f7b42e 100644 --- a/src/include/ndpi_protocols.h +++ b/src/include/ndpi_protocols.h @@ -23,8 +23,8 @@ */ -#ifndef __NDPI_PROTOCOLS_INCLUDE_FILE__ -#define __NDPI_PROTOCOLS_INCLUDE_FILE__ +#ifndef __NDPI_PROTOCOLS_H__ +#define __NDPI_PROTOCOLS_H__ #include "ndpi_main.h" @@ -72,6 +72,7 @@ void ndpi_search_oscar(struct ndpi_detection_module_struct *ndpi_struct, struct void ndpi_search_jabber_tcp(struct ndpi_detection_module_struct *ndpi_struct, struct ndpi_flow_struct *flow); void ndpi_search_irc_tcp(struct ndpi_detection_module_struct *ndpi_struct, struct ndpi_flow_struct *flow); void ndpi_search_sip(struct ndpi_detection_module_struct *ndpi_struct, struct ndpi_flow_struct *flow); +void ndpi_search_hep(struct ndpi_detection_module_struct *ndpi_struct, struct ndpi_flow_struct *flow); void ndpi_search_direct_download_link_tcp(struct ndpi_detection_module_struct *ndpi_struct, struct ndpi_flow_struct *flow); void ndpi_search_mail_pop_tcp(struct ndpi_detection_module_struct *ndpi_struct, struct ndpi_flow_struct *flow); void ndpi_search_mail_imap_tcp(struct ndpi_detection_module_struct *ndpi_struct, struct ndpi_flow_struct *flow); @@ -195,6 +196,7 @@ void ndpi_search_eaq(struct ndpi_detection_module_struct *ndpi_struct, struct nd void ndpi_search_kakaotalk_voice(struct ndpi_detection_module_struct *ndpi_struct, struct ndpi_flow_struct *flow); void ndpi_search_mpegts(struct ndpi_detection_module_struct *ndpi_struct, struct ndpi_flow_struct *flow); void ndpi_search_starcraft(struct ndpi_detection_module_struct *ndpi_struct, struct ndpi_flow_struct *flow); +void ndpi_search_ubntac2(struct ndpi_detection_module_struct *ndpi_struct, struct ndpi_flow_struct *flow); /* --- INIT FUNCTIONS --- */ @@ -288,6 +290,7 @@ void init_rtsp_dissector(struct ndpi_detection_module_struct *ndpi_struct, u_int void init_sflow_dissector(struct ndpi_detection_module_struct *ndpi_struct, u_int32_t *id, NDPI_PROTOCOL_BITMASK *detection_bitmask); void init_shoutcast_dissector(struct ndpi_detection_module_struct *ndpi_struct, u_int32_t *id, NDPI_PROTOCOL_BITMASK *detection_bitmask); void init_sip_dissector(struct ndpi_detection_module_struct *ndpi_struct, u_int32_t *id, NDPI_PROTOCOL_BITMASK *detection_bitmask); +void init_hep_dissector(struct ndpi_detection_module_struct *ndpi_struct, u_int32_t *id, NDPI_PROTOCOL_BITMASK *detection_bitmask); void init_skinny_dissector(struct ndpi_detection_module_struct *ndpi_struct, u_int32_t *id, NDPI_PROTOCOL_BITMASK *detection_bitmask); void init_skype_dissector(struct ndpi_detection_module_struct *ndpi_struct, u_int32_t *id, NDPI_PROTOCOL_BITMASK *detection_bitmask); void init_smb_dissector(struct ndpi_detection_module_struct *ndpi_struct, u_int32_t *id, NDPI_PROTOCOL_BITMASK *detection_bitmask); @@ -332,5 +335,6 @@ void init_yahoo_dissector(struct ndpi_detection_module_struct *ndpi_struct, u_in void init_zattoo_dissector(struct ndpi_detection_module_struct *ndpi_struct, u_int32_t *id, NDPI_PROTOCOL_BITMASK *detection_bitmask); void init_zmq_dissector(struct ndpi_detection_module_struct *ndpi_struct, u_int32_t *id, NDPI_PROTOCOL_BITMASK *detection_bitmask); void init_stracraft_dissector(struct ndpi_detection_module_struct *ndpi_struct, u_int32_t *id, NDPI_PROTOCOL_BITMASK *detection_bitmask); +void init_ubntac2_dissector(struct ndpi_detection_module_struct *ndpi_struct, u_int32_t *id, NDPI_PROTOCOL_BITMASK *detection_bitmask); -#endif /* __NDPI_PROTOCOLS_INCLUDE_FILE__ */ +#endif /* __NDPI_PROTOCOLS_H__ */ diff --git a/src/include/ndpi_typedefs.h b/src/include/ndpi_typedefs.h index 7f82d0a28..d73768f52 100644 --- a/src/include/ndpi_typedefs.h +++ b/src/include/ndpi_typedefs.h @@ -22,94 +22,280 @@ * */ -#ifndef __NDPI_TYPEDEFS_FILE__ -#define __NDPI_TYPEDEFS_FILE__ +#ifndef __NDPI_TYPEDEFS_H__ +#define __NDPI_TYPEDEFS_H__ -typedef enum { - NDPI_LOG_ERROR, - NDPI_LOG_TRACE, - NDPI_LOG_DEBUG -} ndpi_log_level_t; +#include "ndpi_define.h" -typedef void (*ndpi_debug_function_ptr) (u_int32_t protocol, - void *module_struct, ndpi_log_level_t log_level, - const char *format, ...); #define BT_ANNOUNCE +#define SNAP_EXT -typedef enum { - ndpi_preorder, - ndpi_postorder, - ndpi_endorder, - ndpi_leaf -} ndpi_VISIT; -typedef struct node_t { +/* NDPI_LOG_LEVEL */ +typedef enum + { + NDPI_LOG_ERROR, + NDPI_LOG_TRACE, + NDPI_LOG_DEBUG + } ndpi_log_level_t; + +/* NDPI_VISIT */ +typedef enum + { + ndpi_preorder, + ndpi_postorder, + ndpi_endorder, + ndpi_leaf + } ndpi_VISIT; + +/* NDPI_NODE */ +typedef struct node_t +{ char *key; struct node_t *left, *right; } ndpi_node; +/* NDPI_MASK_SIZE */ typedef u_int32_t ndpi_ndpi_mask; -typedef struct ndpi_protocol_bitmask_struct { - ndpi_ndpi_mask fds_bits[NDPI_NUM_FDS_BITS]; +/* NDPI_PROTO_BITMASK_STRUCT */ +typedef struct ndpi_protocol_bitmask_struct +{ + ndpi_ndpi_mask fds_bits[NDPI_NUM_FDS_BITS]; } ndpi_protocol_bitmask_struct_t; -#ifdef NDPI_DETECTION_SUPPORT_IPV6 -struct ndpi_ip6_addr { - union { - u_int8_t u6_addr8[16]; - u_int16_t u6_addr16[8]; - u_int32_t u6_addr32[4]; - u_int64_t u6_addr64[2]; - } ndpi_v6_u; +/* NDPI_DEBUG_FUNCTION_PTR (cast) */ +typedef void (*ndpi_debug_function_ptr) (u_int32_t protocol, void *module_struct, + ndpi_log_level_t log_level, const char *format, ...); -#define ndpi_v6_addr ndpi_v6_u.u6_addr8 -#define ndpi_v6_addr16 ndpi_v6_u.u6_addr16 -#define ndpi_v6_addr32 ndpi_v6_u.u6_addr32 -#define ndpi_v6_addr64 ndpi_v6_u.u6_addr64 -}; -struct ndpi_ipv6hdr { - /* use userspace and kernelspace compatible compile parameters */ +/* ************************************************************ */ +/* ******************* NDPI NETWORKS HEADERS ****************** */ +/* ************************************************************ */ + +/* ++++++++++++++++++++++++ Cisco headers +++++++++++++++++++++ */ + +/* Cisco HDLC */ +#ifdef _MSC_VER +/* Windows */ +#define PACK_ON __pragma(pack(push, 1)) +#define PACK_OFF __pragma(pack(pop)) +#elif defined(__GNUC__) +/* GNU C */ +#define PACK_ON +#define PACK_OFF __attribute__((packed)) +#endif + +PACK_ON +struct ndpi_chdlc +{ + u_int8_t addr; /* 0x0F (Unicast) - 0x8F (Broadcast) */ + u_int8_t ctrl; /* always 0x00 */ + u_int16_t proto_code; /* protocol type (e.g. 0x0800 IP) */ +} PACK_OFF; + +/* SLARP - Serial Line ARP http://tinyurl.com/qa54e95 */ +PACK_ON +struct ndpi_slarp +{ + /* address requests (0x00) + address replies (0x01) + keep-alive (0x02) + */ + u_int32_t slarp_type; + u_int32_t addr_1; + u_int32_t addr_2; +} PACK_OFF; + +/* Cisco Discovery Protocol http://tinyurl.com/qa6yw9l */ +PACK_ON +struct ndpi_cdp +{ + u_int8_t version; + u_int8_t ttl; + u_int16_t checksum; + u_int16_t type; + u_int16_t length; +} PACK_OFF; + +/* +++++++++++++++ Ethernet header (IEEE 802.3) +++++++++++++++ */ + +PACK_ON +struct ndpi_ethhdr +{ + u_char h_dest[6]; /* destination eth addr */ + u_char h_source[6]; /* source ether addr */ + u_int16_t h_proto; /* data length (<= 1500) or type ID proto (>=1536) */ +} PACK_OFF; + +/* +++++++++++++++++++ LLC header (IEEE 802.2) ++++++++++++++++ */ + +PACK_ON +struct ndpi_snap_extension +{ + u_int16_t oui; + u_int8_t oui2; + u_int16_t proto_ID; +} PACK_OFF; + +PACK_ON +struct ndpi_llc_header +{ + u_int8_t dsap; + u_int8_t ssap; + u_int8_t ctrl; +#ifdef SNAP_EXT + struct ndpi_snap_extension snap; +#endif +} PACK_OFF; + +/* ++++++++++ RADIO TAP header (for IEEE 802.11) +++++++++++++ */ +PACK_ON +struct ndpi_radiotap_header +{ + u_int8_t version; /* set to 0 */ + u_int8_t pad; + u_int16_t len; + u_int32_t present; + u_int64_t MAC_timestamp; + u_int8_t flags; +} PACK_OFF; + +/* ++++++++++++ Wireless header (IEEE 802.11) ++++++++++++++++ */ +PACK_ON +struct ndpi_wifi_header +{ + u_int16_t fc; + u_int16_t duration; + u_char rcvr[6]; + u_char trsm[6]; + u_char dest[6]; + u_int16_t seq_ctrl; + /* u_int64_t ccmp - for data encription only - check fc.flag */ +} PACK_OFF; + +/* +++++++++++++++++++++++ MPLS header +++++++++++++++++++++++ */ + +PACK_ON +struct ndpi_mpls_header +{ + u_int32_t label:20, exp:3, s:1, ttl:8; +} PACK_OFF; + +/* ++++++++++++++++++++++++ IP header ++++++++++++++++++++++++ */ + +PACK_ON +struct ndpi_iphdr { #if defined(__LITTLE_ENDIAN__) - u_int8_t priority:4, version:4; + u_int8_t ihl:4, version:4; #elif defined(__BIG_ENDIAN__) - u_int8_t version:4, priority:4; + u_int8_t version:4, ihl:4; #else # error "Byte order must be defined" #endif + u_int8_t tos; + u_int16_t tot_len; + u_int16_t id; + u_int16_t frag_off; + u_int8_t ttl; + u_int8_t protocol; + u_int16_t check; + u_int32_t saddr; + u_int32_t daddr; +} PACK_OFF; - u_int8_t flow_lbl[3]; +/* +++++++++++++++++++++++ IPv6 header +++++++++++++++++++++++ */ +/* rfc3542 */ - u_int16_t payload_len; - u_int8_t nexthdr; - u_int8_t hop_limit; - - struct ndpi_ip6_addr saddr; - struct ndpi_ip6_addr daddr; +struct ndpi_in6_addr +{ + union + { + u_int8_t u6_addr8[16]; + u_int16_t u6_addr16[8]; + u_int32_t u6_addr32[4]; + } u6_addr; /* 128-bit IP6 address */ }; -#endif -typedef union { +PACK_ON +struct ndpi_ipv6hdr +{ + union + { + struct ndpi_ip6_hdrctl + { + u_int32_t ip6_un1_flow; + u_int16_t ip6_un1_plen; + u_int8_t ip6_un1_nxt; + u_int8_t ip6_un1_hlim; + } ip6_un1; + u_int8_t ip6_un2_vfc; + } ip6_ctlun; + + struct ndpi_in6_addr ip6_src; + struct ndpi_in6_addr ip6_dst; +} PACK_OFF; + +/* +++++++++++++++++++++++ TCP header +++++++++++++++++++++++ */ + +PACK_ON +struct ndpi_tcphdr +{ + u_int16_t source; + u_int16_t dest; + u_int32_t seq; + u_int32_t ack_seq; +#if defined(__LITTLE_ENDIAN__) + u_int16_t res1:4, doff:4, fin:1, syn:1, rst:1, psh:1, ack:1, urg:1, ece:1, cwr:1; +#elif defined(__BIG_ENDIAN__) + u_int16_t doff:4, res1:4, cwr:1, ece:1, urg:1, ack:1, psh:1, rst:1, syn:1, fin:1; +#else +# error "Byte order must be defined" +#endif + u_int16_t window; + u_int16_t check; + u_int16_t urg_ptr; +} PACK_OFF; + +/* +++++++++++++++++++++++ UDP header +++++++++++++++++++++++ */ + +PACK_ON +struct ndpi_udphdr +{ + u_int16_t source; + u_int16_t dest; + u_int16_t len; + u_int16_t check; +} PACK_OFF; + +PACK_ON +struct ndpi_dns_packet_header { + u_int16_t transaction_id, flags, num_queries, answer_rrs, authority_rrs, additional_rrs; +} PACK_OFF; + +typedef union +{ u_int32_t ipv4; u_int8_t ipv4_u_int8_t[4]; #ifdef NDPI_DETECTION_SUPPORT_IPV6 - struct ndpi_ip6_addr ipv6; + struct ndpi_in6_addr ipv6; #endif } ndpi_ip_addr_t; +/* ************************************************************ */ +/* ******************* ********************* ****************** */ +/* ************************************************************ */ #ifdef NDPI_PROTOCOL_BITTORRENT -#ifndef __KERNEL__ + typedef struct spinlock { volatile int val; } spinlock_t; + typedef struct atomic { volatile int counter; } atomic_t; -#endif - struct hash_ip4p_node { struct hash_ip4p_node *next,*prev; time_t lchg; @@ -155,16 +341,12 @@ typedef enum { } ndpi_http_method; typedef struct ndpi_id_struct { - /* detected_protocol_bitmask: - * access this bitmask to find out whether an id has used skype or not - * if a flag is set here, it will not be resetted - * to compare this, use: - * if (NDPI_BITMASK_COMPARE(id->detected_protocol_bitmask, - * NDPI_PROTOCOL_BITMASK_XXX) != 0) - * { - * // protocol XXX detected on this id - * } - */ + /** + detected_protocol_bitmask: + access this bitmask to find out whether an id has used skype or not + if a flag is set here, it will not be resetted + to compare this, use: + **/ NDPI_PROTOCOL_BITMASK detected_protocol_bitmask; #ifdef NDPI_PROTOCOL_RTSP ndpi_ip_addr_t rtsp_ip_address; @@ -310,7 +492,7 @@ struct ndpi_flow_tcp_struct { u_int32_t http_stage:2; u_int32_t http_empty_line_seen:1; u_int32_t http_wait_for_retransmission:1; -#endif +#endif #ifdef NDPI_PROTOCOL_GNUTELLA u_int32_t gnutella_stage:2; // 0 - 2 #endif @@ -390,7 +572,7 @@ struct ndpi_flow_tcp_struct { u_char prev_zmq_pkt[10]; #endif } -#if !defined(WIN32) +#ifndef WIN32 __attribute__ ((__packed__)) #endif ; @@ -439,7 +621,7 @@ struct ndpi_flow_udp_struct { u_int32_t eaq_sequence; #endif } -#if !defined(WIN32) +#ifndef WIN32 __attribute__ ((__packed__)) #endif ; @@ -468,10 +650,10 @@ typedef struct ndpi_packet_struct { u_int8_t detected_subprotocol_stack[NDPI_PROTOCOL_HISTORY_SIZE]; -#if !defined(WIN32) - __attribute__ ((__packed__)) +#ifndef WIN32 + __attribute__ ((__packed__)) #endif - u_int16_t protocol_stack_info; + u_int16_t protocol_stack_info; struct ndpi_int_one_line_struct line[NDPI_MAX_PARSE_LINES_PER_PACKET]; struct ndpi_int_one_line_struct host_line; @@ -574,7 +756,7 @@ typedef struct ndpi_detection_module_struct { #ifdef NDPI_ENABLE_DEBUG_MESSAGES void *user_data; #endif - + /* callback function buffer */ struct ndpi_call_function_struct callback_buffer[NDPI_MAX_SUPPORTED_PROTOCOLS + 1]; u_int32_t callback_buffer_size; @@ -666,41 +848,45 @@ typedef struct ndpi_detection_module_struct { typedef struct ndpi_flow_struct { u_int16_t detected_protocol_stack[NDPI_PROTOCOL_HISTORY_SIZE]; -#if !defined(WIN32) - __attribute__ ((__packed__)) +#ifndef WIN32 + __attribute__ ((__packed__)) #endif - u_int16_t protocol_stack_info; + u_int16_t protocol_stack_info; /* init parameter, internal used to set up timestamp,... */ u_int16_t guessed_protocol_id, guessed_host_proto_id; - u_int8_t protocol_id_already_guessed:1, host_already_guessed:1, init_finished:1, setup_packet_direction:1, packet_direction:1; - /* if ndpi_struct->direction_detect_disable == 1 */ - /* tcp sequence number connection tracking */ + u_int8_t protocol_id_already_guessed:1, host_already_guessed:1, init_finished:1, setup_packet_direction:1, packet_direction:1; + + /* + if ndpi_struct->direction_detect_disable == 1 + tcp sequence number connection tracking + */ u_int32_t next_tcp_seq_nr[2]; - /* the tcp / udp / other l4 value union - * this is used to reduce the number of bytes for tcp or udp protocol states - * */ + /* + the tcp / udp / other l4 value union + used to reduce the number of bytes for tcp or udp protocol states + */ union { struct ndpi_flow_tcp_struct tcp; struct ndpi_flow_udp_struct udp; } l4; - struct ndpi_id_struct *server_id; /* - Pointer to src or dst - that identifies the - server of this connection - */ -#ifndef __KERNEL__ - u_char host_server_name[256]; /* HTTP host or DNS query */ -#else - u_char host_server_name[160]; -#endif - u_char detected_os[32]; /* Via HTTP User-Agent */ - u_char nat_ip[24]; /* Via HTTP X-Forwarded-For */ + /* + Pointer to src or dst + that identifies the + server of this connection + */ + struct ndpi_id_struct *server_id; + /* HTTP host or DNS query */ + u_char host_server_name[256]; + /* Via HTTP User-Agent */ + u_char detected_os[32]; + /* Via HTTP X-Forwarded-For */ + u_char nat_ip[24]; - /* + /* This structure below will not not stay inside the protos structure below as HTTP is used by many subprotocols such as FaceBook, Google... so it is hard to know @@ -708,7 +894,7 @@ typedef struct ndpi_flow_struct { time being. */ struct { - ndpi_http_method method; + ndpi_http_method method; char *url, *content_type; } http; @@ -718,17 +904,18 @@ typedef struct ndpi_flow_struct { u_int8_t bad_packet /* the received packet looks bad */; u_int16_t query_type, query_class, rsp_type; } dns; - + struct { u_int8_t request_code; u_int8_t version; } ntp; - + struct { char client_certificate[48], server_certificate[48]; } ssl; } protos; - /* ALL protocol specific 64 bit variables here */ + + /*** ALL protocol specific 64 bit variables here ***/ /* protocols which have marked a connection as this connection cannot be protocol XXX, multiple u_int64_t */ NDPI_PROTOCOL_BITMASK excluded_protocol_bitmask; @@ -754,7 +941,7 @@ typedef struct ndpi_flow_struct { #endif #ifdef NDPI_PROTOCOL_HTTP u_int32_t http_detected:1; -#endif +#endif #ifdef NDPI_PROTOCOL_RTSP u_int32_t rtsprdt_stage:2; u_int32_t rtsp_control_flow:1; @@ -818,4 +1005,4 @@ typedef struct ndpi_flow_struct { struct ndpi_id_struct *dst; } ndpi_flow_struct_t; -#endif/* __NDPI_TYPEDEFS_FILE__ */ +#endif/* __NDPI_TYPEDEFS_H__ */ diff --git a/src/include/ndpi_unix.h b/src/include/ndpi_unix.h index 79de7b43c..b680d3c30 100644 --- a/src/include/ndpi_unix.h +++ b/src/include/ndpi_unix.h @@ -25,8 +25,6 @@ #ifndef __NDPI_UNIX_INCLUDE_FILE__ #define __NDPI_UNIX_INCLUDE_FILE__ -#include "linux_compat.h" - #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) #include #if defined(__NetBSD__) || defined(__OpenBSD__) @@ -38,16 +36,9 @@ #endif #ifndef WIN32 -#ifndef __KERNEL__ - #include #include #include -#else -#include -#include -#include -#endif #endif #endif /* __NDPI_UNIX_INCLUDE_FILE__ */ diff --git a/src/include/ndpi_win32.h b/src/include/ndpi_win32.h index 3b568beb9..645c022e5 100644 --- a/src/include/ndpi_win32.h +++ b/src/include/ndpi_win32.h @@ -22,36 +22,34 @@ * */ -#ifndef __NDPI_WIN32_INCLUDE_FILE__ -#define __NDPI_WIN32_INCLUDE_FILE__ +#ifndef __NDPI_WIN32_H__ +#define __NDPI_WIN32_H__ -#ifdef WIN32 -#include /* winsock.h is included automatically */ +#include +#include #include #include -#include /* getopt from: http://www.pwilson.net/sample.html. */ -#include /* for getpid() and the exec..() family */ +#include /* getopt from: http://www.pwilson.net/sample.html. */ +#include /* for getpid() and the exec..() family */ +#include #ifndef _CRT_SECURE_NO_WARNINGS #define _CRT_SECURE_NO_WARNINGS #endif -#define snprintf _snprintf -extern char* strsep(char **stringp, const char *delim); +#define _WS2TCPIP_H_ /* Avoid compilation problems */ + +extern char* strsep(char **sp, const char *sep); -#define __attribute__(x) -#include -#ifndef __GNUC__ typedef unsigned char u_char; typedef unsigned short u_short; typedef unsigned int uint; typedef unsigned long u_long; -#endif -typedef u_char u_int8_t; -typedef u_short u_int16_t; -typedef unsigned int u_int32_t; -typedef unsigned __int64 u_int64_t; - +typedef u_char u_int8_t; +typedef u_short u_int16_t; +typedef uint u_int32_t; +typedef uint u_int; +typedef unsigned __int64 u_int64_t; #define pthread_t HANDLE #define pthread_mutex_t HANDLE @@ -62,12 +60,10 @@ typedef unsigned __int64 u_int64_t; #define pthread_rwlock_unlock pthread_mutex_unlock #define pthread_rwlock_destroy pthread_mutex_destroy -#define gmtime_r(a, b) gmtime(a) /* Already thread safe on windows */ +#define gmtime_r(a, b) memcpy(b, gmtime(a), sizeof(struct tm)) extern unsigned long waitForNextEvent(unsigned long ulDelay /* ms */); #define sleep(a /* sec */) waitForNextEvent(1000*a /* ms */) -#endif /* Win32 */ - -#endif /* __NDPI_WIN32_INCLUDE_FILE__ */ +#endif /* __NDPI_WIN32_H__ */ diff --git a/src/lib/Makefile.am b/src/lib/Makefile.am index 4e8b1f6c4..bfbb1fb74 100644 --- a/src/lib/Makefile.am +++ b/src/lib/Makefile.am @@ -7,14 +7,11 @@ libndpi_la_LDFLAGS = -version-info 1:0:0 -export-symbols $(top_srcdir)/libndpi.s libndpi_la_includedir = $(includedir)/libndpi-@VERSION@/libndpi libndpi_la_include_HEADERS = ../include/ndpi_api.h \ - ../include/linux_compat.h \ ../include/ndpi_define.h \ ../include/ndpi_main.h \ ../include/ndpi_protocol_ids.h \ ../include/ndpi_protocols.h \ - ../include/ndpi_typedefs.h \ - ../include/ndpi_unix.h \ - ../include/ndpi_win32.h + ../include/ndpi_typedefs.h libndpi_la_SOURCES = ndpi_content_match.c.inc \ ndpi_main.c \ diff --git a/src/lib/ndpi_main.c b/src/lib/ndpi_main.c index fc5042b0f..c8958465c 100644 --- a/src/lib/ndpi_main.c +++ b/src/lib/ndpi_main.c @@ -23,151 +23,21 @@ */ -#ifndef __KERNEL__ #include #include -#endif - #include "ahocorasick.h" #include "ndpi_api.h" - - -#ifndef __KERNEL__ #include "../../config.h" -#endif -// #define DEBUG - -#ifdef __KERNEL__ -#include -#define printf printk -#else #include #ifndef WIN32 #include #endif -#endif #include "ndpi_content_match.c.inc" #include "third_party/include/ndpi_patricia.h" #include "third_party/src/ndpi_patricia.c" -#ifdef WIN32 -/* http://social.msdn.microsoft.com/Forums/uk/vcgeneral/thread/963aac07-da1a-4612-be4a-faac3f1d65ca */ -#ifndef strtok_r -#define strtok_r(a,b,c) strtok(a,b) -#endif -#endif - -#ifdef __KERNEL__ -#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,39) -static inline char _tolower(const char c) -{ - return c | 0x20; -} - -static int _kstrtoull(const char *s, unsigned int base, unsigned long long *res) -{ - unsigned long long acc; - int ok; - - if(base == 0) { - if(s[0] == '0') { - if(_tolower(s[1]) == 'x' && isxdigit(s[2])) - base = 16; - else - base = 8; - } else - base = 10; - } - if(base == 16 && s[0] == '0' && _tolower(s[1]) == 'x') - s += 2; - - acc = 0; - ok = 0; - while (*s) { - unsigned int val; - - if('0' <= *s && *s <= '9') - val = *s - '0'; - else if('a' <= _tolower(*s) && _tolower(*s) <= 'f') - val = _tolower(*s) - 'a' + 10; - else if(*s == '\n') { - if(*(s + 1) == '\0') - break; - else - return -EINVAL; - } else - return -EINVAL; - - if(val >= base) - return -EINVAL; - if(acc > div_u64(ULLONG_MAX - val, base)) - return -ERANGE; - acc = acc * base + val; - ok = 1; - - s++; - } - if(!ok) - return -EINVAL; - *res = acc; - return 0; -} - -int kstrtoull(const char *s, unsigned int base, unsigned long long *res) -{ - if(s[0] == '+') - s++; - return _kstrtoull(s, base, res); -} -int kstrtoll(const char *s, unsigned int base, long long *res) -{ - unsigned long long tmp; - int rv; - - if(s[0] == '-') { - rv = _kstrtoull(s + 1, base, &tmp); - if(rv < 0) - return rv; - if((long long)(-tmp) >= 0) - return -ERANGE; - *res = -tmp; - } else { - rv = kstrtoull(s, base, &tmp); - if(rv < 0) - return rv; - if((long long)tmp < 0) - return -ERANGE; - *res = tmp; - } - return 0; -} -int kstrtoint(const char *s, unsigned int base, int *res) -{ - long long tmp; - int rv; - - rv = kstrtoll(s, base, &tmp); - if(rv < 0) - return rv; - if(tmp != (long long)(int)tmp) - return -ERANGE; - *res = tmp; - return 0; -} -#endif - -int atoi(const char *str) { - int rc; - - if(kstrtoint(str, 0, &rc) == 0 /* Success */) - return(rc); - else - return(0); -} -#endif - /* ftp://ftp.cc.uoc.gr/mirrors/OpenBSD/src/lib/libc/stdlib/tsearch.c */ /* find or insert datum into search tree */ void * @@ -560,15 +430,6 @@ void ndpi_set_proto_defaults(struct ndpi_detection_module_struct *ndpi_mod, if(udpDefPorts[j].port_low != 0) addDefaultPort(&udpDefPorts[j], &ndpi_mod->proto_defaults[protoId], &ndpi_mod->udpRoot); if(tcpDefPorts[j].port_low != 0) addDefaultPort(&tcpDefPorts[j], &ndpi_mod->proto_defaults[protoId], &ndpi_mod->tcpRoot); } - -#if 0 - printf("%s(%d, %s, %p) [%s]\n", - __FUNCTION__, - protoId, - ndpi_mod->proto_defaults[protoId].protoName, - ndpi_mod, - ndpi_mod->proto_defaults[1].protoName); -#endif } /* ******************************************************************** */ @@ -769,7 +630,7 @@ static void init_string_based_protocols(struct ndpi_detection_module_struct *ndp Do NOT add web services (NDPI_SERVICE_xxx) here. */ static void ndpi_init_protocol_defaults(struct ndpi_detection_module_struct *ndpi_mod) { - + int i; ndpi_port_range ports_a[MAX_DEFAULT_PORTS], ports_b[MAX_DEFAULT_PORTS]; u_int16_t no_master[2] = { NDPI_PROTOCOL_NO_MASTER_PROTO, NDPI_PROTOCOL_NO_MASTER_PROTO }, @@ -1618,11 +1479,21 @@ static void ndpi_init_protocol_defaults(struct ndpi_detection_module_struct *ndp ndpi_build_default_ports(ports_a, 0, 0, 0, 0, 0) /* TCP */, ndpi_build_default_ports(ports_b, 58267, 0, 0, 0, 0) /* UDP */); ndpi_set_proto_defaults(ndpi_mod, NDPI_PROTOCOL_FUN, NDPI_PROTOCOL_STARCRAFT, - no_master, + no_master, no_master, "Starcraft", ndpi_build_default_ports(ports_a, 1119, 0, 0, 0, 0), /* TCP */ ndpi_build_default_ports(ports_b, 1119, 0, 0, 0, 0)); /* UDP */ - + ndpi_set_proto_defaults(ndpi_mod, NDPI_PROTOCOL_SAFE, NDPI_PROTOCOL_UBNTAC2, + no_master, + no_master, "UBNTAC2", + ndpi_build_default_ports(ports_a, 0, 0, 0, 0, 0), /* TCP */ + ndpi_build_default_ports(ports_b, 10001, 0, 0, 0, 0)); /* UDP */ + ndpi_set_proto_defaults(ndpi_mod, NDPI_PROTOCOL_SAFE, NDPI_PROTOCOL_MS_LYNC, + no_master, + no_master, "Lync", + ndpi_build_default_ports(ports_a, 0, 0, 0, 0, 0), /* TCP */ + ndpi_build_default_ports(ports_b, 0, 0, 0, 0, 0)); /* UDP */ + /* calling function for host and content matched protocols */ init_string_based_protocols(ndpi_mod); @@ -1666,20 +1537,20 @@ static int fill_prefix_v4(prefix_t *p, struct in_addr *a, int b, int mb) { /* ******************************************* */ -u_int16_t ndpi_network_ptree_match(struct ndpi_detection_module_struct *ndpi_struct, struct in_addr *pin) { +u_int16_t ndpi_network_ptree_match(struct ndpi_detection_module_struct *ndpi_struct, struct in_addr *pin /* network byte order */) { prefix_t prefix; patricia_node_t *node; /* Make sure all in network byte order otherwise compares wont work */ fill_prefix_v4(&prefix, pin, 32, ((patricia_tree_t*)ndpi_struct->protocols_ptree)->maxbits); node = ndpi_patricia_search_best(ndpi_struct->protocols_ptree, &prefix); - + return(node ? node->value.user_value : NDPI_PROTOCOL_UNKNOWN); } /* ******************************************* */ -u_int16_t ndpi_host_ptree_match(struct ndpi_detection_module_struct *ndpi_struct, u_int32_t host) { +u_int16_t ndpi_host_ptree_match(struct ndpi_detection_module_struct *ndpi_struct, u_int32_t host /* network byte order */) { struct in_addr pin; pin.s_addr = host; @@ -1700,11 +1571,9 @@ u_int8_t ndpi_is_tor_flow(struct ndpi_detection_module_struct *ndpi_struct, struct ndpi_packet_struct *packet = &flow->packet; if(packet->tcp != NULL) { - if(flow->packet.iph) { - struct in_addr saddr = { packet->iph->saddr }; - struct in_addr daddr = { packet->iph->daddr }; - if(tor_ptree_match(ndpi_struct, &saddr) - || tor_ptree_match(ndpi_struct, &daddr)) { + if(packet->iph) { + if(tor_ptree_match(ndpi_struct, (struct in_addr *)&packet->iph->saddr) + || tor_ptree_match(ndpi_struct, (struct in_addr *)&packet->iph->daddr)) { return(1); } } @@ -1749,10 +1618,9 @@ static int ndpi_add_host_ip_subprotocol(struct ndpi_detection_module_struct *ndp patricia_node_t *node; struct in_addr pin; - + inet_pton(AF_INET, value, &pin); - pin.s_addr = ntohl(pin.s_addr); - + if((node = add_to_ptree(ndpi_struct->protocols_ptree, AF_INET, &pin, 32)) != NULL) { node->value.user_value = protocol_id; } @@ -1954,60 +1822,28 @@ u_int16_t ndpi_guess_protocol_id(struct ndpi_detection_module_struct *ndpi_struc /* ******************************************************************** */ -#if 0 -#ifndef __KERNEL__ -static int add_proto_default_port(u_int16_t **ports, u_int16_t new_port, - ndpi_proto_defaults_t *def, - ndpi_default_ports_tree_node_t *root) { - u_int num_ports, i; - - if(*ports == NULL) { - ndpi_port_range range = { new_port, new_port }; - - addDefaultPort(&range, def, &root); - return(0); - } - - for(num_ports=0; (*ports)[num_ports] != 0; num_ports++) - ; - - if(num_ports >= MAX_DEFAULT_PORTS) { - printf("Too many ports defined: ignored port %d\n", new_port); - return(-1); - } else { - u_int16_t *new_ports = (u_int16_t*)ndpi_malloc(num_ports+1); - ndpi_port_range range; - - if(new_ports == NULL) { - printf("Not enough memory\n"); - return(-2); - } - - for(i=0; indpi_num_supported_protocols); } /* ******************************************************************** */ +#ifdef WIN32 +char * strsep(char **sp, char *sep) +{ + char *p, *s; + if (sp == NULL || *sp == NULL || **sp == '\0') return(NULL); + s = *sp; + p = s + strcspn(s, sep); + if (*p != '\0') *p++ = '\0'; + *sp = p; + return(s); +} +#endif + +/* ******************************************************************** */ + + int ndpi_handle_rule(struct ndpi_detection_module_struct *ndpi_mod, char* rule, u_int8_t do_add) { char *at, *proto, *elem; ndpi_proto_defaults_t *def; @@ -2123,9 +1959,6 @@ int ndpi_handle_rule(struct ndpi_detection_module_struct *ndpi_mod, char* rule, */ int ndpi_load_protocols_file(struct ndpi_detection_module_struct *ndpi_mod, char* path) { -#ifdef __KERNEL__ - return(0); -#else FILE *fd = fopen(path, "r"); int i; @@ -2150,14 +1983,6 @@ int ndpi_load_protocols_file(struct ndpi_detection_module_struct *ndpi_mod, char fclose(fd); -#if 0 - printf("\nTCP:\n"); - ndpi_twalk(ndpi_mod->tcpRoot, ndpi_default_ports_tree_node_t_walker, NULL); - printf("\nUDP:\n"); - ndpi_twalk(ndpi_mod->udpRoot, ndpi_default_ports_tree_node_t_walker, NULL); -#endif -#endif - return(0); } @@ -2179,7 +2004,7 @@ void ndpi_set_bitmask_protocol_detection( char * label, #ifdef DEBUG NDPI_LOG(0, ndpi_struct, NDPI_LOG_DEBUG,"[NDPI] ndpi_set_bitmask_protocol_detection: %s : [callback_buffer] idx= %u, [proto_defaults] protocol_id=%u\n", label, idx, ndpi_protocol_id); #endif - + if(ndpi_struct->proto_defaults[ndpi_protocol_id].protoIdx != 0) printf("[NDPI] Internal error: protocol %s/%u has been already registered\n", label, ndpi_protocol_id); else { @@ -2189,7 +2014,7 @@ void ndpi_set_bitmask_protocol_detection( char * label, } /* - Set funcition and index protocol within proto_default strcuture for port protocol detection + Set function and index protocol within proto_default strcuture for port protocol detection and callback_buffer function for DPI protocol detection */ ndpi_struct->proto_defaults[ndpi_protocol_id].protoIdx = idx; @@ -2229,9 +2054,6 @@ void ndpi_set_protocol_detection_bitmask2(struct ndpi_detection_module_struct *n /* HTTP */ init_http_dissector(ndpi_struct, &a, detection_bitmask); - /* SKYPE */ - init_skype_dissector(ndpi_struct, &a, detection_bitmask); - /* Stracraft */ init_starcraft_dissector(ndpi_struct, &a, detection_bitmask); @@ -2253,9 +2075,6 @@ void ndpi_set_protocol_detection_bitmask2(struct ndpi_detection_module_struct *n /* SIP */ init_sip_dissector(ndpi_struct, &a, detection_bitmask); - /* BITTORRENT */ - init_bittorrent_dissector(ndpi_struct, &a, detection_bitmask); - /* Teredo */ init_teredo_dissector(ndpi_struct, &a, detection_bitmask); @@ -2326,10 +2145,10 @@ void ndpi_set_protocol_detection_bitmask2(struct ndpi_detection_module_struct *n init_non_tcp_udp_dissector(ndpi_struct, &a, detection_bitmask); /* TVANTS */ - init_tvants_dissector(ndpi_struct, &a, detection_bitmask); + init_tvants_dissector(ndpi_struct, &a, detection_bitmask); /* SOPCAST */ - init_sopcast_dissector(ndpi_struct, &a, detection_bitmask); + init_sopcast_dissector(ndpi_struct, &a, detection_bitmask); /* TVUPLAYER */ init_tvuplayer_dissector(ndpi_struct, &a, detection_bitmask); @@ -2356,13 +2175,13 @@ void ndpi_set_protocol_detection_bitmask2(struct ndpi_detection_module_struct *n init_ssh_dissector(ndpi_struct, &a, detection_bitmask); /* AYIYA */ - init_ayiya_dissector(ndpi_struct, &a, detection_bitmask); + init_ayiya_dissector(ndpi_struct, &a, detection_bitmask); /* THUNDER */ - init_thunder_dissector(ndpi_struct, &a, detection_bitmask); + init_thunder_dissector(ndpi_struct, &a, detection_bitmask); /* VNC */ - init_vnc_dissector(ndpi_struct, &a, detection_bitmask); + init_vnc_dissector(ndpi_struct, &a, detection_bitmask); /* TEAMVIEWER */ init_teamviewer_dissector(ndpi_struct, &a, detection_bitmask); @@ -2423,7 +2242,7 @@ void ndpi_set_protocol_detection_bitmask2(struct ndpi_detection_module_struct *n /* SNMP */ init_snmp_dissector(ndpi_struct, &a, detection_bitmask); - + /* KONTIKI */ init_kontiki_dissector(ndpi_struct, &a, detection_bitmask); @@ -2492,13 +2311,13 @@ void ndpi_set_protocol_detection_bitmask2(struct ndpi_detection_module_struct *n /* AIMINI */ init_aimini_dissector(ndpi_struct, &a, detection_bitmask); - + /* FLORENSIA */ init_florensia_dissector(ndpi_struct, &a, detection_bitmask); /* MAPLESTORY */ init_maplestory_dissector(ndpi_struct, &a, detection_bitmask); - + /* DOFUS */ init_dofus_dissector(ndpi_struct, &a, detection_bitmask); @@ -2510,10 +2329,10 @@ void ndpi_set_protocol_detection_bitmask2(struct ndpi_detection_module_struct *n /* CROSSIFIRE */ init_crossfire_dissector(ndpi_struct, &a, detection_bitmask); - + /* GUILDWARS */ init_guildwars_dissector(ndpi_struct, &a, detection_bitmask); - + /* ARMAGETRON */ init_armagetron_dissector(ndpi_struct, &a, detection_bitmask); @@ -2552,7 +2371,7 @@ void ndpi_set_protocol_detection_bitmask2(struct ndpi_detection_module_struct *n /* NOE */ init_noe_dissector(ndpi_struct, &a, detection_bitmask); - + /* CISCOVPN */ init_ciscovpn_dissector(ndpi_struct, &a, detection_bitmask); @@ -2621,10 +2440,17 @@ void ndpi_set_protocol_detection_bitmask2(struct ndpi_detection_module_struct *n /* KAKAOTALK_VOICE */ init_kakaotalk_voice_dissector(ndpi_struct, &a, detection_bitmask); - + /* MPEGTS */ init_mpegts_dissector(ndpi_struct, &a, detection_bitmask); - + + /* Put false-positive sensitive protocols at the end */ + + /* SKYPE */ + init_skype_dissector(ndpi_struct, &a, detection_bitmask); + + /* BITTORRENT */ + init_bittorrent_dissector(ndpi_struct, &a, detection_bitmask); /* ----------------------------------------------------------------- */ @@ -2708,8 +2534,7 @@ void ndpi_set_protocol_detection_bitmask2(struct ndpi_detection_module_struct *n * nxt_hdr: protocol of the actual payload * returns 0 upon success and 1 upon failure */ -static int ndpi_handle_ipv6_extension_headers(struct ndpi_detection_module_struct *ndpi_struct, - const u_int8_t ** l4ptr, u_int16_t * l4len, u_int8_t * nxt_hdr) +static int ndpi_handle_ipv6_extension_headers(struct ndpi_detection_module_struct *ndpi_struct, const u_int8_t ** l4ptr, u_int16_t * l4len, u_int8_t * nxt_hdr) { while ((*nxt_hdr == 0 || *nxt_hdr == 43 || *nxt_hdr == 44 || *nxt_hdr == 60 || *nxt_hdr == 135 || *nxt_hdr == 59)) { u_int16_t ehdr_len; @@ -2759,7 +2584,8 @@ static u_int8_t ndpi_iph_is_valid_and_not_fragmented(const struct ndpi_iphdr *ip } static u_int8_t ndpi_detection_get_l4_internal(struct ndpi_detection_module_struct *ndpi_struct, - const u_int8_t * l3, u_int16_t l3_len, const u_int8_t ** l4_return, u_int16_t * l4_len_return, + const u_int8_t * l3, u_int16_t l3_len, + const u_int8_t ** l4_return, u_int16_t * l4_len_return, u_int8_t * l4_protocol_return, u_int32_t flags) { const struct ndpi_iphdr *iph = NULL; @@ -2812,10 +2638,10 @@ static u_int8_t ndpi_detection_get_l4_internal(struct ndpi_detection_module_stru l4protocol = iph->protocol; } #ifdef NDPI_DETECTION_SUPPORT_IPV6 - else if(iph_v6 != NULL && (l3_len - sizeof(struct ndpi_ipv6hdr)) >= ntohs(iph_v6->payload_len)) { + else if(iph_v6 != NULL && (l3_len - sizeof(struct ndpi_ipv6hdr)) >= ntohs(iph_v6->ip6_ctlun.ip6_un1.ip6_un1_plen)) { l4ptr = (((const u_int8_t *) iph_v6) + sizeof(struct ndpi_ipv6hdr)); - l4len = ntohs(iph_v6->payload_len); - l4protocol = iph_v6->nexthdr; + l4len = ntohs(iph_v6->ip6_ctlun.ip6_un1.ip6_un1_plen); + l4protocol = iph_v6->ip6_ctlun.ip6_un1.ip6_un1_nxt; // we need to handle IPv6 extension headers if present if(ndpi_handle_ipv6_extension_headers(ndpi_struct, &l4ptr, &l4len, &l4protocol) != 0) { @@ -2843,11 +2669,6 @@ static u_int8_t ndpi_detection_get_l4_internal(struct ndpi_detection_module_stru return 0; } -#if !defined(WIN32) -#define ATTRIBUTE_ALWAYS_INLINE static inline -#else -__forceinline static -#endif void ndpi_apply_flow_protocol_to_packet(struct ndpi_flow_struct *flow, struct ndpi_packet_struct *packet) { @@ -2973,12 +2794,6 @@ static int ndpi_init_packet_header(struct ndpi_detection_module_struct *ndpi_str return 0; } - -#if !defined(WIN32) -static inline -#else -__forceinline static -#endif void ndpi_connection_tracking(struct ndpi_detection_module_struct *ndpi_struct, struct ndpi_flow_struct *flow) { @@ -2991,9 +2806,6 @@ void ndpi_connection_tracking(struct ndpi_detection_module_struct *ndpi_struct, const struct ndpi_tcphdr *tcph = packet->tcp; const struct ndpi_udphdr *udph = flow->packet.udp; - //struct ndpi_unique_flow_struct unique_flow; - //uint8_t new_connection; - u_int8_t proxy_enabled = 0; packet->tcp_retransmission = 0, packet->packet_direction = 0; @@ -3005,7 +2817,7 @@ void ndpi_connection_tracking(struct ndpi_detection_module_struct *ndpi_struct, packet->packet_direction = 1; #ifdef NDPI_DETECTION_SUPPORT_IPV6 - if(iphv6 != NULL && NDPI_COMPARE_IPV6_ADDRESS_STRUCTS(&iphv6->saddr, &iphv6->daddr) != 0) + if(iphv6 != NULL && NDPI_COMPARE_IPV6_ADDRESS_STRUCTS(&iphv6->ip6_src, &iphv6->ip6_dst) != 0) packet->packet_direction = 1; #endif } @@ -3211,8 +3023,7 @@ void check_ndpi_tcp_flow_func(struct ndpi_detection_module_struct *ndpi_struct, ndpi_struct->callback_buffer[proto_index].excluded_protocol_bitmask) == 0 && NDPI_BITMASK_COMPARE(ndpi_struct->callback_buffer[proto_index].detection_bitmask, detection_bitmask) != 0 - && (ndpi_struct->callback_buffer[proto_index].ndpi_selection_bitmask - & *ndpi_selection_packet) == ndpi_struct->callback_buffer[proto_index].ndpi_selection_bitmask) { + && (ndpi_struct->callback_buffer[proto_index].ndpi_selection_bitmask & *ndpi_selection_packet) == ndpi_struct->callback_buffer[proto_index].ndpi_selection_bitmask) { if((flow->guessed_protocol_id != NDPI_PROTOCOL_UNKNOWN) && (ndpi_struct->proto_defaults[flow->guessed_protocol_id].func != NULL)) ndpi_struct->proto_defaults[flow->guessed_protocol_id].func(ndpi_struct, flow), @@ -3222,8 +3033,7 @@ void check_ndpi_tcp_flow_func(struct ndpi_detection_module_struct *ndpi_struct, if(flow->detected_protocol_stack[0] == NDPI_PROTOCOL_UNKNOWN) { for(a = 0; a < ndpi_struct->callback_buffer_size_tcp_payload; a++) { if((func != ndpi_struct->callback_buffer_tcp_payload[a].func) - && (ndpi_struct->callback_buffer_tcp_payload[a].ndpi_selection_bitmask - & *ndpi_selection_packet) == ndpi_struct->callback_buffer_tcp_payload[a].ndpi_selection_bitmask + && (ndpi_struct->callback_buffer_tcp_payload[a].ndpi_selection_bitmask & *ndpi_selection_packet) == ndpi_struct->callback_buffer_tcp_payload[a].ndpi_selection_bitmask && NDPI_BITMASK_COMPARE(flow->excluded_protocol_bitmask, ndpi_struct->callback_buffer_tcp_payload[a].excluded_protocol_bitmask) == 0 && NDPI_BITMASK_COMPARE(ndpi_struct->callback_buffer_tcp_payload[a].detection_bitmask, @@ -3280,6 +3090,109 @@ void check_ndpi_flow_func(struct ndpi_detection_module_struct *ndpi_struct, check_ndpi_other_flow_func(ndpi_struct, flow, ndpi_selection_packet); } +/* ********************************************************************************* */ + +ndpi_protocol ndpi_l4_detection_process_packet(struct ndpi_detection_module_struct *ndpi_struct, + struct ndpi_flow_struct *flow, + const struct ndpi_iphdr *iph, + struct ndpi_ipv6hdr *iph6, + struct ndpi_tcphdr *tcp, + struct ndpi_udphdr *udp, + u_int8_t src_to_dst_direction, + u_int8_t l4_proto, + struct ndpi_id_struct *src, + u_int16_t sport, + struct ndpi_id_struct *dst, + u_int16_t dport, + u_int8_t *payload, u_int16_t payload_len) { + NDPI_SELECTION_BITMASK_PROTOCOL_SIZE ndpi_selection_packet; + u_int32_t a; + ndpi_protocol ret = { NDPI_PROTOCOL_UNKNOWN, NDPI_PROTOCOL_UNKNOWN }; + + if(payload_len == 0) return(ret); + + flow->packet.tcp = tcp, flow->packet.udp = udp; + flow->packet.payload = payload, flow->packet.payload_packet_len = payload_len; + + if(src_to_dst_direction) + flow->src = src, flow->dst = dst; + else + flow->src = dst, flow->dst = src; + + ndpi_selection_packet = NDPI_SELECTION_BITMASK_PROTOCOL_COMPLETE_TRAFFIC; + if((flow->packet.iph = iph) != NULL) + ndpi_selection_packet |= NDPI_SELECTION_BITMASK_PROTOCOL_IP | NDPI_SELECTION_BITMASK_PROTOCOL_IPV4_OR_IPV6; +#ifdef NDPI_DETECTION_SUPPORT_IPV6 + else if((flow->packet.iphv6 = iph6) != NULL) + ndpi_selection_packet |= NDPI_SELECTION_BITMASK_PROTOCOL_IPV6 | NDPI_SELECTION_BITMASK_PROTOCOL_IPV4_OR_IPV6; +#endif /* NDPI_DETECTION_SUPPORT_IPV6 */ + + if(flow->packet.tcp != NULL) + ndpi_selection_packet |= + (NDPI_SELECTION_BITMASK_PROTOCOL_INT_TCP | NDPI_SELECTION_BITMASK_PROTOCOL_INT_TCP_OR_UDP); + + if(flow->packet.udp != NULL) + ndpi_selection_packet |= + (NDPI_SELECTION_BITMASK_PROTOCOL_INT_UDP | NDPI_SELECTION_BITMASK_PROTOCOL_INT_TCP_OR_UDP); + + if(flow->packet.payload_packet_len != 0) { + ndpi_selection_packet |= NDPI_SELECTION_BITMASK_PROTOCOL_HAS_PAYLOAD; + + if(!flow->protocol_id_already_guessed) { + flow->guessed_protocol_id = (int16_t)ndpi_guess_protocol_id(ndpi_struct, l4_proto, sport, dport); + flow->protocol_id_already_guessed = 1; + } + } + + if(flow->packet.tcp_retransmission == 0) + ndpi_selection_packet |= NDPI_SELECTION_BITMASK_PROTOCOL_NO_TCP_RETRANSMISSION; + + flow->packet.l4_protocol = l4_proto, flow->packet.packet_direction = src_to_dst_direction; + + check_ndpi_flow_func(ndpi_struct, flow, &ndpi_selection_packet); + + a = flow->packet.detected_protocol_stack[0]; + if(NDPI_COMPARE_PROTOCOL_TO_BITMASK(ndpi_struct->detection_bitmask, a) == 0) + a = NDPI_PROTOCOL_UNKNOWN; + + if(a != NDPI_PROTOCOL_UNKNOWN) { + int i; + + for(i=0; (ihost_server_name)) && (flow->host_server_name[i] != '\0'); i++) + flow->host_server_name[i] = tolower(flow->host_server_name[i]); + + flow->host_server_name[i] ='\0'; + } + + ret_protocols: + if(flow->detected_protocol_stack[1] != NDPI_PROTOCOL_UNKNOWN) { + ret.master_protocol = flow->detected_protocol_stack[1], ret.protocol = flow->detected_protocol_stack[0]; + + if(ret.protocol == ret.master_protocol) + ret.master_protocol = NDPI_PROTOCOL_UNKNOWN; + } else + ret.protocol = flow->detected_protocol_stack[0]; + + if((ret.protocol == NDPI_PROTOCOL_UNKNOWN) + && flow->packet.iph + && (!flow->host_already_guessed)) { + + if((flow->guessed_host_proto_id = ndpi_network_ptree_match(ndpi_struct, + (struct in_addr *)&flow->packet.iph->saddr)) == NDPI_PROTOCOL_UNKNOWN) { + flow->guessed_host_proto_id = ndpi_network_ptree_match(ndpi_struct, (struct in_addr *)&flow->packet.iph->daddr); + } + + flow->host_already_guessed = 1; + } + + if((ret.protocol == NDPI_PROTOCOL_UNKNOWN) && (ret.master_protocol != NDPI_PROTOCOL_UNKNOWN)) + ret.protocol = flow->guessed_host_proto_id; + + return(ret); +} + +/* ********************************************************************************* */ + ndpi_protocol ndpi_detection_process_packet(struct ndpi_detection_module_struct *ndpi_struct, struct ndpi_flow_struct *flow, const unsigned char *packet, @@ -3307,15 +3220,7 @@ ndpi_protocol ndpi_detection_process_packet(struct ndpi_detection_module_struct } flow->packet.tick_timestamp_l = current_tick_l; -#ifdef __KERNEL__ - { - u_int64_t d = current_tick_l; - do_div(d,1000); - flow->packet.tick_timestamp = d; - } -#else flow->packet.tick_timestamp = (u_int32_t)current_tick_l/1000; -#endif /* parse packet */ flow->packet.iph = (struct ndpi_iphdr *)packet; @@ -3366,7 +3271,7 @@ ndpi_protocol ndpi_detection_process_packet(struct ndpi_detection_module_struct #ifdef NDPI_DETECTION_SUPPORT_IPV6 if(flow->packet.iphv6 != NULL) { - protocol = flow->packet.iphv6->nexthdr, saddr = 0, daddr = 0; + protocol = flow->packet.iphv6->ip6_ctlun.ip6_un1.ip6_un1_nxt, saddr = 0, daddr = 0; } else #endif { @@ -3388,15 +3293,6 @@ ndpi_protocol ndpi_detection_process_packet(struct ndpi_detection_module_struct } } -#if 0 - a = flow->detected_protocol_stack[0]; - if(a != NDPI_PROTOCOL_UNKNOWN) { - NDPI_LOG(NDPI_PROTOCOL_UNKNOWN, ndpi_struct, NDPI_LOG_TRACE, "PROCESS KNOWN PROTOCOL\n"); - ndpi_struct->proto_defaults[a].func(ndpi_struct, flow); - return a; - } -#endif - check_ndpi_flow_func(ndpi_struct, flow, &ndpi_selection_packet); a = flow->packet.detected_protocol_stack[0]; @@ -3415,7 +3311,7 @@ ndpi_protocol ndpi_detection_process_packet(struct ndpi_detection_module_struct ret_protocols: if(flow->detected_protocol_stack[1] != NDPI_PROTOCOL_UNKNOWN) { ret.master_protocol = flow->detected_protocol_stack[1], ret.protocol = flow->detected_protocol_stack[0]; - + if(ret.protocol == ret.master_protocol) ret.master_protocol = NDPI_PROTOCOL_UNKNOWN; } else @@ -3424,30 +3320,16 @@ ndpi_protocol ndpi_detection_process_packet(struct ndpi_detection_module_struct if((ret.protocol == NDPI_PROTOCOL_UNKNOWN) && flow->packet.iph && (!flow->host_already_guessed)) { - struct in_addr pin; - - pin.s_addr = flow->packet.iph->saddr; - if((flow->guessed_host_proto_id = ndpi_network_ptree_match(ndpi_struct, &pin)) == NDPI_PROTOCOL_UNKNOWN) { - pin.s_addr = flow->packet.iph->daddr; - flow->guessed_host_proto_id = ndpi_network_ptree_match(ndpi_struct, &pin); + + if((flow->guessed_host_proto_id = ndpi_network_ptree_match(ndpi_struct, (struct in_addr *)&flow->packet.iph->saddr)) == NDPI_PROTOCOL_UNKNOWN) { + flow->guessed_host_proto_id = ndpi_network_ptree_match(ndpi_struct, (struct in_addr *)&flow->packet.iph->daddr); } - + flow->host_already_guessed = 1; } -#if 0 - - /* Swap protocols in case of success */ - if(ret.master_protocol != NDPI_PROTOCOL_UNKNOWN) { - u_int16_t t = ret.master_protocol; - - ret.master_protocol = ret.protocol; - ret.protocol = t; - } -#endif - - if((ret.protocol == NDPI_PROTOCOL_UNKNOWN) && (ret.master_protocol != NDPI_PROTOCOL_UNKNOWN)) - ret.protocol = flow->guessed_host_proto_id; + if((ret.protocol == NDPI_PROTOCOL_UNKNOWN) && (ret.master_protocol != NDPI_PROTOCOL_UNKNOWN)) + ret.protocol = flow->guessed_host_proto_id; return(ret); } @@ -4059,39 +3941,50 @@ int NDPI_PROTOCOL_IP_is_set(const ndpi_ip_addr_t * ip) /* NTOP */ int ndpi_packet_src_ip_eql(const struct ndpi_packet_struct *packet, const ndpi_ip_addr_t * ip) { -#ifdef NDPI_DETECTION_SUPPORT_IPV6 - if(packet->iphv6 != NULL) { - if(packet->iphv6->saddr.ndpi_v6_u.u6_addr64[0] == ip->ipv6.ndpi_v6_u.u6_addr64[0] && - packet->iphv6->saddr.ndpi_v6_u.u6_addr64[1] == ip->ipv6.ndpi_v6_u.u6_addr64[1]) { +#ifdef NDPI_DETECTION_SUPPORT_IPV6 + + /* IPv6 */ + if(packet->iphv6 != NULL) { + + if(packet->iphv6->ip6_src.u6_addr.u6_addr32[0] == ip->ipv6.u6_addr.u6_addr32[0] && + packet->iphv6->ip6_src.u6_addr.u6_addr32[1] == ip->ipv6.u6_addr.u6_addr32[1] && + packet->iphv6->ip6_src.u6_addr.u6_addr32[2] == ip->ipv6.u6_addr.u6_addr32[2] && + packet->iphv6->ip6_src.u6_addr.u6_addr32[3] == ip->ipv6.u6_addr.u6_addr32[3]) return 1; - } else { - return 0; - } + //else + return 0; } #endif - if(packet->iph->saddr == ip->ipv4) { + + /* IPv4 */ + if(packet->iph->saddr == ip->ipv4) return 1; - } return 0; } /* check if the destination ip address in packet and ip are equal */ int ndpi_packet_dst_ip_eql(const struct ndpi_packet_struct *packet, const ndpi_ip_addr_t * ip) { + #ifdef NDPI_DETECTION_SUPPORT_IPV6 + + /* IPv6 */ if(packet->iphv6 != NULL) { - if(packet->iphv6->daddr.ndpi_v6_u.u6_addr64[0] == ip->ipv6.ndpi_v6_u.u6_addr64[0] && - packet->iphv6->daddr.ndpi_v6_u.u6_addr64[1] == ip->ipv6.ndpi_v6_u.u6_addr64[1]) { + + if(packet->iphv6->ip6_dst.u6_addr.u6_addr32[0] == ip->ipv6.u6_addr.u6_addr32[0] && + packet->iphv6->ip6_dst.u6_addr.u6_addr32[1] == ip->ipv6.u6_addr.u6_addr32[1] && + packet->iphv6->ip6_dst.u6_addr.u6_addr32[2] == ip->ipv6.u6_addr.u6_addr32[2] && + packet->iphv6->ip6_dst.u6_addr.u6_addr32[3] == ip->ipv6.u6_addr.u6_addr32[3]) return 1; - } else { - return 0; - } + //else + return 0; } #endif - if(packet->iph->daddr == ip->ipv4) { + + /* IPv4 */ + if(packet->iph->saddr == ip->ipv4) return 1; - } return 0; } @@ -4100,12 +3993,21 @@ int ndpi_packet_dst_ip_eql(const struct ndpi_packet_struct *packet, const ndpi_i void ndpi_packet_src_ip_get(const struct ndpi_packet_struct *packet, ndpi_ip_addr_t * ip) { NDPI_PROTOCOL_IP_clear(ip); + #ifdef NDPI_DETECTION_SUPPORT_IPV6 + + /* IPv6 */ if(packet->iphv6 != NULL) { - ip->ipv6.ndpi_v6_u.u6_addr64[0] = packet->iphv6->saddr.ndpi_v6_u.u6_addr64[0]; - ip->ipv6.ndpi_v6_u.u6_addr64[1] = packet->iphv6->saddr.ndpi_v6_u.u6_addr64[1]; + + ip->ipv6.u6_addr.u6_addr32[0] = packet->iphv6->ip6_src.u6_addr.u6_addr32[0]; + ip->ipv6.u6_addr.u6_addr32[1] = packet->iphv6->ip6_src.u6_addr.u6_addr32[1]; + ip->ipv6.u6_addr.u6_addr32[2] = packet->iphv6->ip6_src.u6_addr.u6_addr32[2]; + ip->ipv6.u6_addr.u6_addr32[3] = packet->iphv6->ip6_src.u6_addr.u6_addr32[3]; + } else #endif + + /* IPv4 */ ip->ipv4 = packet->iph->saddr; } @@ -4114,12 +4016,20 @@ void ndpi_packet_src_ip_get(const struct ndpi_packet_struct *packet, ndpi_ip_add void ndpi_packet_dst_ip_get(const struct ndpi_packet_struct *packet, ndpi_ip_addr_t * ip) { NDPI_PROTOCOL_IP_clear(ip); + #ifdef NDPI_DETECTION_SUPPORT_IPV6 + if(packet->iphv6 != NULL) { - ip->ipv6.ndpi_v6_u.u6_addr64[0] = packet->iphv6->daddr.ndpi_v6_u.u6_addr64[0]; - ip->ipv6.ndpi_v6_u.u6_addr64[1] = packet->iphv6->daddr.ndpi_v6_u.u6_addr64[1]; + + ip->ipv6.u6_addr.u6_addr32[0] = packet->iphv6->ip6_dst.u6_addr.u6_addr32[0]; + ip->ipv6.u6_addr.u6_addr32[1] = packet->iphv6->ip6_dst.u6_addr.u6_addr32[1]; + ip->ipv6.u6_addr.u6_addr32[2] = packet->iphv6->ip6_dst.u6_addr.u6_addr32[2]; + ip->ipv6.u6_addr.u6_addr32[3] = packet->iphv6->ip6_dst.u6_addr.u6_addr32[3]; + } else + #endif + ip->ipv4 = packet->iph->daddr; } @@ -4133,15 +4043,22 @@ char *ndpi_get_ip_string(struct ndpi_detection_module_struct *ndpi_struct, const u_int8_t *a = (const u_int8_t *) &ip->ipv4; #ifdef NDPI_DETECTION_SUPPORT_IPV6 - if(ip->ipv6.ndpi_v6_u.u6_addr32[1] != 0 || ip->ipv6.ndpi_v6_u.u6_addr64[1] != 0) { - const u_int16_t *b = ip->ipv6.ndpi_v6_u.u6_addr16; + if(ip->ipv6.u6_addr.u6_addr32[0] != 0 || + ip->ipv6.u6_addr.u6_addr32[1] != 0 || + ip->ipv6.u6_addr.u6_addr32[1] != 0 || + ip->ipv6.u6_addr.u6_addr32[1] != 0) { + + const u_int16_t *b = ip->ipv6.u6_addr.u6_addr16; snprintf(ndpi_struct->ip_string, 32, "%x:%x:%x:%x:%x:%x:%x:%x", ntohs(b[0]), ntohs(b[1]), ntohs(b[2]), ntohs(b[3]), ntohs(b[4]), ntohs(b[5]), ntohs(b[6]), ntohs(b[7])); + return ndpi_struct->ip_string; } #endif + snprintf(ndpi_struct->ip_string, 32, "%u.%u.%u.%u", a[0], a[1], a[2], a[3]); + return ndpi_struct->ip_string; } @@ -4176,22 +4093,12 @@ u_int16_t ntohs_ndpi_bytestream_to_number(const u_int8_t * str, u_int16_t max_ch /* ****************************************************** */ -#if 0 -#ifndef __KERNEL__ -static u_int is_port(u_int16_t sport, u_int16_t dport, u_int16_t match_port) { - return(((match_port == sport) || (match_port == dport)) ? 1 : 0); -} -#endif -#endif - -/* ****************************************************** */ - ndpi_protocol ndpi_find_port_based_protocol(struct ndpi_detection_module_struct *ndpi_struct /* NOTUSED */, u_int8_t proto, u_int32_t shost, u_int16_t sport, u_int32_t dhost, u_int16_t dport) { ndpi_protocol p = NDPI_PROTOCOL_NULL; - + /* Skyfile (host 193.252.234.246 or host 10.10.102.80) */ if((shost == 0xC1FCEAF6) || (dhost == 0xC1FCEAF6) || (shost == 0x0A0A6650) || (dhost == 0x0A0A6650)) { @@ -4233,7 +4140,7 @@ ndpi_protocol ndpi_guess_undetected_protocol(struct ndpi_detection_module_struct if(ret.protocol == ret.master_protocol) ret.master_protocol = NDPI_PROTOCOL_UNKNOWN; - + return(ret); } @@ -4249,14 +4156,14 @@ ndpi_protocol ndpi_guess_undetected_protocol(struct ndpi_detection_module_struct ret = ndpi_find_port_based_protocol(ndpi_struct, proto, shost, sport, dhost, dport); if(ret.protocol != NDPI_PROTOCOL_UNKNOWN) - return(ret); + return(ret); check_guessed_skype: - addr.s_addr = shost; + addr.s_addr = htonl(shost); if(ndpi_network_ptree_match(ndpi_struct, &addr) == NDPI_PROTOCOL_SKYPE) { ret.protocol = NDPI_PROTOCOL_SKYPE; } else { - addr.s_addr = dhost; + addr.s_addr = htonl(dhost); if(ndpi_network_ptree_match(ndpi_struct, &addr) == NDPI_PROTOCOL_SKYPE) ret.protocol = NDPI_PROTOCOL_SKYPE; } @@ -4268,14 +4175,14 @@ ndpi_protocol ndpi_guess_undetected_protocol(struct ndpi_detection_module_struct /* ****************************************************** */ -char* ndpi_protocol2name(struct ndpi_detection_module_struct *ndpi_mod, +char* ndpi_protocol2name(struct ndpi_detection_module_struct *ndpi_mod, ndpi_protocol proto, char *buf, u_int buf_len) { if(proto.master_protocol != NDPI_PROTOCOL_UNKNOWN) { snprintf(buf, buf_len, "%s.%s", ndpi_get_proto_name(ndpi_mod, proto.master_protocol), ndpi_get_proto_name(ndpi_mod, proto.protocol)); } else - snprintf(buf, buf_len, "%s", + snprintf(buf, buf_len, "%s", ndpi_get_proto_name(ndpi_mod, proto.protocol)); return(buf); @@ -4490,11 +4397,7 @@ void ndpi_free_flow(struct ndpi_flow_struct *flow) { /* ****************************************************** */ -#ifndef __KERNEL__ -char* ndpi_revision() { - return(NDPI_GIT_RELEASE); -} -#endif +char* ndpi_revision() { return(NDPI_GIT_RELEASE); } /* ****************************************************** */ @@ -4570,45 +4473,3 @@ void NDPI_DUMP_BITMASK(NDPI_PROTOCOL_BITMASK a) { printf("\n"); } - -#ifdef WIN32 -/* http://www.opensource.apple.com/source/xnu/xnu-1456.1.26/bsd/libkern/strsep.c */ - -/* - * Get next token from string *stringp, where tokens are possibly-empty - * strings separated by characters from delim. - * - * Writes NULs into the string at *stringp to end tokens. - * delim need not remain constant from call to call. - * On return, *stringp points past the last NUL written (if there might - * be further tokens), or is NULL (if there are definitely no more tokens). - * - * If *stringp is NULL, strsep returns NULL. - */ -char* strsep(char **stringp, const char *delim) { - char *s; - const char *spanp; - int c, sc; - char *tok; - - if((s = *stringp) == NULL) - return (NULL); - for(tok = s;;) { - c = *s++; - spanp = delim; - do { - if((sc = *spanp++) == c) { - if(c == 0) - s = NULL; - else - s[-1] = 0; - *stringp = s; - return (tok); - } - } while (sc != 0); - } - /* NOTREACHED */ -} -#endif - - diff --git a/src/lib/protocols/mdns.c b/src/lib/protocols/mdns.c index 8226ab93c..d4426e7f3 100644 --- a/src/lib/protocols/mdns.c +++ b/src/lib/protocols/mdns.c @@ -37,108 +37,108 @@ static void ndpi_int_mdns_add_connection(struct ndpi_detection_module_struct } static int ndpi_int_check_mdns_payload(struct ndpi_detection_module_struct - *ndpi_struct, struct ndpi_flow_struct *flow) + *ndpi_struct, struct ndpi_flow_struct *flow) { - struct ndpi_packet_struct *packet = &flow->packet; + struct ndpi_packet_struct *packet = &flow->packet; - if ((packet->payload[2] & 0x80) == 0 && - ntohs(get_u_int16_t(packet->payload, 4)) <= NDPI_MAX_MDNS_REQUESTS && - ntohs(get_u_int16_t(packet->payload, 6)) <= NDPI_MAX_MDNS_REQUESTS) { + if ((packet->payload[2] & 0x80) == 0 && + ntohs(get_u_int16_t(packet->payload, 4)) <= NDPI_MAX_MDNS_REQUESTS && + ntohs(get_u_int16_t(packet->payload, 6)) <= NDPI_MAX_MDNS_REQUESTS) { - NDPI_LOG(NDPI_PROTOCOL_MDNS, ndpi_struct, NDPI_LOG_DEBUG, "found MDNS with question query.\n"); + NDPI_LOG(NDPI_PROTOCOL_MDNS, ndpi_struct, NDPI_LOG_DEBUG, "found MDNS with question query.\n"); - return 1; - } else if ((packet->payload[2] & 0x80) != 0 && - ntohs(get_u_int16_t(packet->payload, 4)) == 0 && - ntohs(get_u_int16_t(packet->payload, 6)) <= NDPI_MAX_MDNS_REQUESTS && - ntohs(get_u_int16_t(packet->payload, 6)) != 0) { - NDPI_LOG(NDPI_PROTOCOL_MDNS, ndpi_struct, NDPI_LOG_DEBUG, "found MDNS with answer query.\n"); + return 1; + } else if ((packet->payload[2] & 0x80) != 0 && + ntohs(get_u_int16_t(packet->payload, 4)) == 0 && + ntohs(get_u_int16_t(packet->payload, 6)) <= NDPI_MAX_MDNS_REQUESTS && + ntohs(get_u_int16_t(packet->payload, 6)) != 0) { + NDPI_LOG(NDPI_PROTOCOL_MDNS, ndpi_struct, NDPI_LOG_DEBUG, "found MDNS with answer query.\n"); - return 1; - } + return 1; + } - return 0; + return 0; } void ndpi_search_mdns(struct ndpi_detection_module_struct *ndpi_struct, struct ndpi_flow_struct *flow) { - struct ndpi_packet_struct *packet = &flow->packet; + struct ndpi_packet_struct *packet = &flow->packet; -// struct ndpi_id_struct *src=ndpi_struct->src; -// struct ndpi_id_struct *dst=ndpi_struct->dst; + // struct ndpi_id_struct *src=ndpi_struct->src; + // struct ndpi_id_struct *dst=ndpi_struct->dst; - u_int16_t dport; -// const u_int16_t sport=ntohs(packet->udp->source); + u_int16_t dport; + // const u_int16_t sport=ntohs(packet->udp->source); - /* check if UDP and */ - if (packet->udp != NULL) { - /*read destination port */ - dport = ntohs(packet->udp->dest); + /* check if UDP and */ + if (packet->udp != NULL) { + /*read destination port */ + dport = ntohs(packet->udp->dest); - NDPI_LOG(NDPI_PROTOCOL_MDNS, ndpi_struct, NDPI_LOG_DEBUG, "MDNS udp start \n"); + NDPI_LOG(NDPI_PROTOCOL_MDNS, ndpi_struct, NDPI_LOG_DEBUG, "MDNS udp start \n"); - /*check standard MDNS to port 5353 */ - /*took this information from http://www.it-administrator.de/lexikon/multicast-dns.html */ + /*check standard MDNS to port 5353 */ + /*took this information from http://www.it-administrator.de/lexikon/multicast-dns.html */ - if (dport == 5353 && packet->payload_packet_len >= 12) { + if (dport == 5353 && packet->payload_packet_len >= 12) { - NDPI_LOG(NDPI_PROTOCOL_MDNS, ndpi_struct, NDPI_LOG_DEBUG, "found MDNS with destination port 5353\n"); + NDPI_LOG(NDPI_PROTOCOL_MDNS, ndpi_struct, NDPI_LOG_DEBUG, "found MDNS with destination port 5353\n"); - /* MDNS header is similar to dns header */ - /* dns header - 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 - +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ - | ID | - +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ - |QR| Opcode |AA|TC|RD|RA| Z | RCODE | - +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ - | QDCOUNT | - +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ - | ANCOUNT | - +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ - | NSCOUNT | - +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ - | ARCOUNT | - +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ - * - * dns query check: query: QR set, ancount = 0, nscount = 0, QDCOUNT < MAX_MDNS, ARCOUNT < MAX_MDNS - * - */ + /* MDNS header is similar to dns header */ + /* dns header + 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 + +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ + | ID | + +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ + |QR| Opcode |AA|TC|RD|RA| Z | RCODE | + +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ + | QDCOUNT | + +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ + | ANCOUNT | + +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ + | NSCOUNT | + +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ + | ARCOUNT | + +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ + * + * dns query check: query: QR set, ancount = 0, nscount = 0, QDCOUNT < MAX_MDNS, ARCOUNT < MAX_MDNS + * + */ - /* mdns protocol must have destination address 224.0.0.251 */ - /* took this information from http://www.it-administrator.de/lexikon/multicast-dns.html */ + /* mdns protocol must have destination address 224.0.0.251 */ + /* took this information from http://www.it-administrator.de/lexikon/multicast-dns.html */ - if (packet->iph != NULL && ntohl(packet->iph->daddr) == 0xe00000fb) { + if (packet->iph != NULL && ntohl(packet->iph->daddr) == 0xe00000fb) { - NDPI_LOG(NDPI_PROTOCOL_MDNS, ndpi_struct, - NDPI_LOG_DEBUG, "found MDNS with destination address 224.0.0.251 (=0xe00000fb)\n"); + NDPI_LOG(NDPI_PROTOCOL_MDNS, ndpi_struct, + NDPI_LOG_DEBUG, "found MDNS with destination address 224.0.0.251 (=0xe00000fb)\n"); - if (ndpi_int_check_mdns_payload(ndpi_struct, flow) == 1) { - ndpi_int_mdns_add_connection(ndpi_struct, flow); - return; - } - } + if (ndpi_int_check_mdns_payload(ndpi_struct, flow) == 1) { + ndpi_int_mdns_add_connection(ndpi_struct, flow); + return; + } + } #ifdef NDPI_DETECTION_SUPPORT_IPV6 - if (packet->iphv6 != NULL) { - const u_int32_t *daddr = packet->iphv6->daddr.ndpi_v6_u.u6_addr32; - if (daddr[0] == htonl(0xff020000) && daddr[1] == 0 && daddr[2] == 0 && daddr[3] == htonl(0xfb)) { + if (packet->iphv6 != NULL) { + const u_int32_t *daddr = packet->iphv6->ip6_dst.u6_addr.u6_addr32; + if (daddr[0] == htonl(0xff020000) && daddr[1] == 0 && daddr[2] == 0 && daddr[3] == htonl(0xfb)) { - NDPI_LOG(NDPI_PROTOCOL_MDNS, ndpi_struct, - NDPI_LOG_DEBUG, "found MDNS with destination address ff02::fb\n"); + NDPI_LOG(NDPI_PROTOCOL_MDNS, ndpi_struct, + NDPI_LOG_DEBUG, "found MDNS with destination address ff02::fb\n"); - if (ndpi_int_check_mdns_payload(ndpi_struct, flow) == 1) { - ndpi_int_mdns_add_connection(ndpi_struct, flow); - return; - } - } - } + if (ndpi_int_check_mdns_payload(ndpi_struct, flow) == 1) { + ndpi_int_mdns_add_connection(ndpi_struct, flow); + return; + } + } + } #endif - } - } - NDPI_ADD_PROTOCOL_TO_BITMASK(flow->excluded_protocol_bitmask, NDPI_PROTOCOL_MDNS); + } + } + NDPI_ADD_PROTOCOL_TO_BITMASK(flow->excluded_protocol_bitmask, NDPI_PROTOCOL_MDNS); } diff --git a/src/lib/protocols/tcp_udp.c b/src/lib/protocols/tcp_udp.c index 7ca276dca..9f9febfeb 100644 --- a/src/lib/protocols/tcp_udp.c +++ b/src/lib/protocols/tcp_udp.c @@ -48,6 +48,9 @@ void ndpi_search_tcp_or_udp(struct ndpi_detection_module_struct *ndpi_struct, st u_int proto; struct ndpi_packet_struct *packet = &flow->packet; + if(flow->host_server_name[0] != '\0') + return; + if(ndpi_is_tor_flow(ndpi_struct, flow)) { ndpi_set_detected_protocol(ndpi_struct, flow, NDPI_PROTOCOL_TOR, NDPI_PROTOCOL_UNKNOWN); return; @@ -61,7 +64,7 @@ void ndpi_search_tcp_or_udp(struct ndpi_detection_module_struct *ndpi_struct, st proto = ndpi_search_tcp_or_udp_raw(ndpi_struct, flow->packet.iph ? flow->packet.iph->protocol : #ifdef NDPI_DETECTION_SUPPORT_IPV6 - flow->packet.iphv6->nexthdr, + flow->packet.iphv6->ip6_ctlun.ip6_un1.ip6_un1_nxt, #else 0, #endif