Added TZSP encapsulation support
This commit is contained in:
+59
-24
@@ -59,6 +59,7 @@
|
|||||||
#define IDLE_SCAN_BUDGET 1024
|
#define IDLE_SCAN_BUDGET 1024
|
||||||
#define NUM_ROOTS 512
|
#define NUM_ROOTS 512
|
||||||
#define GTP_U_V1_PORT 2152
|
#define GTP_U_V1_PORT 2152
|
||||||
|
#define TZSP_PORT 37008
|
||||||
#define MAX_NDPI_FLOWS 200000000
|
#define MAX_NDPI_FLOWS 200000000
|
||||||
|
|
||||||
#ifndef ETH_P_IP
|
#ifndef ETH_P_IP
|
||||||
@@ -223,7 +224,7 @@ static void help(u_int long_help) {
|
|||||||
#endif
|
#endif
|
||||||
" -d | Disable protocol guess and use only DPI\n"
|
" -d | Disable protocol guess and use only DPI\n"
|
||||||
" -q | Quiet mode\n"
|
" -q | Quiet mode\n"
|
||||||
" -t | Dissect GTP tunnels\n"
|
" -t | Dissect GTP/TZSP tunnels\n"
|
||||||
" -r | Print nDPI version and git revision\n"
|
" -r | Print nDPI version and git revision\n"
|
||||||
" -w <path> | Write test output on the specified file. This is useful for\n"
|
" -w <path> | Write test output on the specified file. This is useful for\n"
|
||||||
" | testing purposes in order to compare results across runs\n"
|
" | testing purposes in order to compare results across runs\n"
|
||||||
@@ -1541,12 +1542,13 @@ 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
|
* Declare pointers to packet headers
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/** --- Ethernet header --- **/
|
/** --- Ethernet header --- **/
|
||||||
const struct ndpi_ethhdr *ethernet;
|
const struct ndpi_ethhdr *ethernet;
|
||||||
|
|
||||||
@@ -1564,6 +1566,7 @@ static void pcap_packet_callback(u_char *args, const struct pcap_pkthdr *header,
|
|||||||
struct ndpi_ip6_hdr *iph6;
|
struct ndpi_ip6_hdr *iph6;
|
||||||
|
|
||||||
/* lengths and offsets */
|
/* lengths and offsets */
|
||||||
|
u_int16_t eth_offset = 0;
|
||||||
u_int16_t radio_len;
|
u_int16_t radio_len;
|
||||||
u_int16_t fc;
|
u_int16_t fc;
|
||||||
int wifi_data_len;
|
int wifi_data_len;
|
||||||
@@ -1592,7 +1595,6 @@ static void pcap_packet_callback(u_char *args, const struct pcap_pkthdr *header,
|
|||||||
|
|
||||||
/* Check if capture is live or not */
|
/* Check if capture is live or not */
|
||||||
if (!live_capture) {
|
if (!live_capture) {
|
||||||
|
|
||||||
if (!pcap_start.tv_sec) pcap_start.tv_sec = header->ts.tv_sec, pcap_start.tv_usec = header->ts.tv_usec;
|
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;
|
pcap_end.tv_sec = header->ts.tv_sec, pcap_end.tv_usec = header->ts.tv_usec;
|
||||||
}
|
}
|
||||||
@@ -1612,31 +1614,31 @@ static void pcap_packet_callback(u_char *args, const struct pcap_pkthdr *header,
|
|||||||
/*** check Data Link type ***/
|
/*** check Data Link type ***/
|
||||||
int datalink_type = ndpi_thread_info[thread_id]._pcap_datalink_type;
|
int datalink_type = ndpi_thread_info[thread_id]._pcap_datalink_type;
|
||||||
|
|
||||||
switch(datalink_type)
|
datalink_check:
|
||||||
{
|
switch(datalink_type) {
|
||||||
case DLT_NULL :
|
case DLT_NULL :
|
||||||
if(ntohl(*((u_int32_t*)packet)) == 2)
|
if(ntohl(*((u_int32_t*)&packet[eth_offset])) == 2)
|
||||||
type = ETH_P_IP;
|
type = ETH_P_IP;
|
||||||
else
|
else
|
||||||
type = ETH_P_IPV6;
|
type = ETH_P_IPV6;
|
||||||
|
|
||||||
ip_offset = 4;
|
ip_offset = 4 + eth_offset;
|
||||||
|
|
||||||
case DLT_EN10MB :
|
case DLT_EN10MB :
|
||||||
ethernet = (struct ndpi_ethhdr *) packet;
|
ethernet = (struct ndpi_ethhdr *) &packet[eth_offset];
|
||||||
ip_offset = sizeof(struct ndpi_ethhdr);
|
ip_offset = sizeof(struct ndpi_ethhdr) + eth_offset;
|
||||||
type = ntohs(ethernet->h_proto);
|
type = ntohs(ethernet->h_proto);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
/* Linux Cooked Capture - 113 */
|
/* Linux Cooked Capture - 113 */
|
||||||
case DLT_LINUX_SLL :
|
case DLT_LINUX_SLL :
|
||||||
type = (packet[14] << 8) + packet[15];
|
type = (packet[eth_offset+14] << 8) + packet[eth_offset+15];
|
||||||
ip_offset = 16;
|
ip_offset = 16 + eth_offset;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
/* Radiotap link-layer - 127 */
|
/* Radiotap link-layer - 127 */
|
||||||
case DLT_IEEE802_11_RADIO :
|
case DLT_IEEE802_11_RADIO :
|
||||||
radiotap = (struct ndpi_radiotap_header *) packet;
|
radiotap = (struct ndpi_radiotap_header *) &packet[eth_offset];
|
||||||
radio_len = radiotap->len;
|
radio_len = radiotap->len;
|
||||||
|
|
||||||
/* Check Bad FCS presence */
|
/* Check Bad FCS presence */
|
||||||
@@ -1649,28 +1651,25 @@ static void pcap_packet_callback(u_char *args, const struct pcap_pkthdr *header,
|
|||||||
fcs = header->len - 4;
|
fcs = header->len - 4;
|
||||||
|
|
||||||
/* Calculate 802.11 header length (variable) */
|
/* Calculate 802.11 header length (variable) */
|
||||||
wifi_data = (struct ndpi_wifi_data_frame*)( packet + radio_len);
|
wifi_data = (struct ndpi_wifi_data_frame*)( packet + eth_offset + radio_len);
|
||||||
fc = wifi_data->fc;
|
fc = wifi_data->fc;
|
||||||
|
|
||||||
/* check wifi data presence */
|
/* check wifi data presence */
|
||||||
if(FCF_TYPE(fc) == WIFI_DATA) {
|
if(FCF_TYPE(fc) == WIFI_DATA) {
|
||||||
|
|
||||||
if((FCF_TO_DS(fc) && FCF_FROM_DS(fc) == 0x0) ||
|
if((FCF_TO_DS(fc) && FCF_FROM_DS(fc) == 0x0) ||
|
||||||
(FCF_TO_DS(fc) == 0x0 && FCF_FROM_DS(fc)))
|
(FCF_TO_DS(fc) == 0x0 && FCF_FROM_DS(fc)))
|
||||||
wifi_data_len = 26; /* + 4 byte fcs */
|
wifi_data_len = 26; /* + 4 byte fcs */
|
||||||
|
|
||||||
/* TODO: check QoS Control for aggregated MSDU */
|
/* TODO: check QoS Control for aggregated MSDU */
|
||||||
|
|
||||||
|
|
||||||
} else /* no data frames */
|
} else /* no data frames */
|
||||||
break;
|
break;
|
||||||
|
|
||||||
/* Check ether_type from LLC */
|
/* Check ether_type from LLC */
|
||||||
llc = (struct ndpi_llc_header_proto*)(packet + wifi_data_len + radio_len);
|
llc = (struct ndpi_llc_header_proto*)(packet + eth_offset + wifi_data_len + radio_len);
|
||||||
llc_ether_type = ntohs(llc->ether_IP_type);
|
llc_ether_type = ntohs(llc->ether_IP_type);
|
||||||
|
|
||||||
/* Set IP header offset */
|
/* Set IP header offset */
|
||||||
ip_offset = wifi_data_len + radio_len + sizeof(struct ndpi_llc_header_proto);
|
ip_offset = wifi_data_len + radio_len + sizeof(struct ndpi_llc_header_proto) + eth_offset;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
@@ -1728,7 +1727,6 @@ static void pcap_packet_callback(u_char *args, const struct pcap_pkthdr *header,
|
|||||||
|
|
||||||
/* Check IP version */
|
/* Check IP version */
|
||||||
if(iph->version == 4) {
|
if(iph->version == 4) {
|
||||||
|
|
||||||
ip_len = ((u_short)iph->ihl * 4);
|
ip_len = ((u_short)iph->ihl * 4);
|
||||||
iph6 = NULL;
|
iph6 = NULL;
|
||||||
|
|
||||||
@@ -1758,24 +1756,23 @@ static void pcap_packet_callback(u_char *args, const struct pcap_pkthdr *header,
|
|||||||
}
|
}
|
||||||
iph = NULL;
|
iph = NULL;
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
static u_int8_t ipv4_warning_used = 0;
|
static u_int8_t ipv4_warning_used = 0;
|
||||||
|
|
||||||
v4_warning:
|
v4_warning:
|
||||||
if(ipv4_warning_used == 0) {
|
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;
|
ipv4_warning_used = 1;
|
||||||
}
|
}
|
||||||
ndpi_thread_info[thread_id].stats.total_discarded_bytes += header->len;
|
ndpi_thread_info[thread_id].stats.total_discarded_bytes += header->len;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if(decode_tunnels && (proto == IPPROTO_UDP)) {
|
|
||||||
|
|
||||||
|
if(decode_tunnels && (proto == IPPROTO_UDP)) {
|
||||||
struct ndpi_udphdr *udp = (struct ndpi_udphdr *)&packet[ip_offset+ip_len];
|
struct ndpi_udphdr *udp = (struct ndpi_udphdr *)&packet[ip_offset+ip_len];
|
||||||
u_int16_t sport = ntohs(udp->source), dport = ntohs(udp->dest);
|
u_int16_t sport = ntohs(udp->source), dport = ntohs(udp->dest);
|
||||||
|
|
||||||
if((sport == GTP_U_V1_PORT) || (dport == GTP_U_V1_PORT)) {
|
if((sport == GTP_U_V1_PORT) || (dport == GTP_U_V1_PORT)) {
|
||||||
|
|
||||||
/* Check if it's GTPv1 */
|
/* Check if it's GTPv1 */
|
||||||
u_int offset = ip_offset+ip_len+sizeof(struct ndpi_udphdr);
|
u_int offset = ip_offset+ip_len+sizeof(struct ndpi_udphdr);
|
||||||
u_int8_t flags = packet[offset];
|
u_int8_t flags = packet[offset];
|
||||||
@@ -1796,6 +1793,44 @@ static void pcap_packet_callback(u_char *args, const struct pcap_pkthdr *header,
|
|||||||
goto v4_warning;
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user