Initial work towards custom BPF protocol definition

This commit is contained in:
Luca
2022-09-16 19:48:12 +02:00
parent a966d37a21
commit 0f07ad35be
4 changed files with 98 additions and 18 deletions
+15 -1
View File
@@ -72,6 +72,7 @@ LT_INIT
LT_LIB_M LT_LIB_M
PKG_PROG_PKG_CONFIG PKG_PROG_PKG_CONFIG
PWD=`pwd`
AC_PROG_CC AC_PROG_CC
SYSTEM=`uname -s` SYSTEM=`uname -s`
@@ -172,6 +173,7 @@ if test "x$ac_cv_lib_rrd_rrd_fetch_r" = xyes; then :
EXTRA_TARGETS="$EXTRA_TARGETS rrdtool" EXTRA_TARGETS="$EXTRA_TARGETS rrdtool"
fi fi
ADDITIONAL_INCS=
ADDITIONAL_LIBS="$LIBM" ADDITIONAL_LIBS="$LIBM"
PCAP_HOME=$HOME/PF_RING/userland PCAP_HOME=$HOME/PF_RING/userland
@@ -196,8 +198,19 @@ fi
MACHINE=`uname -m` MACHINE=`uname -m`
CUSTOM_NDPI= AC_MSG_CHECKING([PF_RING nBPF])
NBPF_HOME=${PWD}/../PF_RING/userland/nbpf
LIBNBPF=${NBPF_HOME}/libnbpf.a
if test -f ${LIBNBPF}; then :
ADDITIONAL_LIBS="${ADDITIONAL_LIBS} ${LIBNBPF}"
ADDITIONAL_INCS="${ADDITIONAL_INCS} -I${NBPF_HOME}"
AC_DEFINE_UNQUOTED(HAVE_NBPF, "1", [PF_RING nBPF is present])
AC_MSG_RESULT(yes)
else
AC_MSG_RESULT(no)
fi
CUSTOM_NDPI=
if test -d ../nDPI-custom; then : if test -d ../nDPI-custom; then :
CUSTOM_NDPI="-DCUSTOM_NDPI_PROTOCOLS" CUSTOM_NDPI="-DCUSTOM_NDPI_PROTOCOLS"
AC_MSG_RESULT([Compiling with custom nDPI protocols]) AC_MSG_RESULT([Compiling with custom nDPI protocols])
@@ -357,6 +370,7 @@ AC_SUBST(NDPI_VERSION_SHORT)
AC_SUBST(PCAP_INC) AC_SUBST(PCAP_INC)
AC_SUBST(PCAP_LIB) AC_SUBST(PCAP_LIB)
AC_SUBST(LIBRRD) AC_SUBST(LIBRRD)
AC_SUBST(ADDITIONAL_INCS)
AC_SUBST(ADDITIONAL_LIBS) AC_SUBST(ADDITIONAL_LIBS)
AC_SUBST(DL_LIB) AC_SUBST(DL_LIB)
AC_SUBST(DPDK_TARGET) AC_SUBST(DPDK_TARGET)
+12
View File
@@ -923,6 +923,7 @@ typedef enum {
NDPI_CONFIDENCE_DPI_PARTIAL_CACHE, /* Classification results based on some LRU cache with partial/incomplete DPI information */ NDPI_CONFIDENCE_DPI_PARTIAL_CACHE, /* Classification results based on some LRU cache with partial/incomplete DPI information */
NDPI_CONFIDENCE_DPI_CACHE, /* Classification results based on some LRU cache (i.e. correlation among sessions) */ NDPI_CONFIDENCE_DPI_CACHE, /* Classification results based on some LRU cache (i.e. correlation among sessions) */
NDPI_CONFIDENCE_DPI, /* Deep packet inspection */ NDPI_CONFIDENCE_DPI, /* Deep packet inspection */
NDPI_CONFIDENCE_NBPF, /* PF_RING nBPF (custom protocol) */
/* /*
IMPORTANT IMPORTANT
@@ -1105,6 +1106,13 @@ typedef struct ndpi_list_struct {
struct ndpi_list_struct *next; struct ndpi_list_struct *next;
} ndpi_list; } ndpi_list;
#ifdef HAVE_NBPF
typedef struct {
void *tree; /* cast to nbpf_filter* */
u_int16_t l7_protocol;
} nbpf_filter;
#endif
struct ndpi_detection_module_struct { struct ndpi_detection_module_struct {
NDPI_PROTOCOL_BITMASK detection_bitmask; NDPI_PROTOCOL_BITMASK detection_bitmask;
@@ -1230,6 +1238,10 @@ struct ndpi_detection_module_struct {
/* Current packet */ /* Current packet */
struct ndpi_packet_struct packet; struct ndpi_packet_struct packet;
const struct ndpi_flow_input_info *input_info; const struct ndpi_flow_input_info *input_info;
#ifdef HAVE_NBPF
nbpf_filter nbpf_custom_proto[MAX_NBPF_CUSTOM_PROTO];
#endif
}; };
#endif /* NDPI_LIB_COMPILATION */ #endif /* NDPI_LIB_COMPILATION */
+1 -1
View File
@@ -17,7 +17,7 @@ includedir = @includedir@/ndpi
ifneq ($(OS),Windows_NT) ifneq ($(OS),Windows_NT)
CFLAGS += -fPIC -DPIC CFLAGS += -fPIC -DPIC
endif endif
CFLAGS += -I../include -Ithird_party/include -DNDPI_LIB_COMPILATION @NDPI_CFLAGS@ @GPROF_CFLAGS@ @CUSTOM_NDPI@ CFLAGS += -I../include -Ithird_party/include -DNDPI_LIB_COMPILATION @NDPI_CFLAGS@ @GPROF_CFLAGS@ @CUSTOM_NDPI@ @ADDITIONAL_INCS@
LDFLAGS += @NDPI_LDFLAGS@ LDFLAGS += @NDPI_LDFLAGS@
LIBS = @ADDITIONAL_LIBS@ @LIBS@ @GPROF_LIBS@ LIBS = @ADDITIONAL_LIBS@ @LIBS@ @GPROF_LIBS@
+57 -3
View File
@@ -94,6 +94,10 @@
#include "third_party/include/ndpi_patricia.h" #include "third_party/include/ndpi_patricia.h"
#include "third_party/include/ndpi_md5.h" #include "third_party/include/ndpi_md5.h"
#ifdef HAVE_NBPF
#include "nbpf.h"
#endif
static int _ndpi_debug_callbacks = 0; static int _ndpi_debug_callbacks = 0;
/* #define DGA_DEBUG 1 */ /* #define DGA_DEBUG 1 */
@@ -6272,16 +6276,63 @@ ndpi_protocol ndpi_detection_process_packet(struct ndpi_detection_module_struct
ret.master_protocol = flow->detected_protocol_stack[1], ret.master_protocol = flow->detected_protocol_stack[1],
ret.app_protocol = flow->detected_protocol_stack[0], ret.app_protocol = flow->detected_protocol_stack[0],
ret.category = flow->category; ret.category = flow->category;
return ret; return ret;
} else if(flow->detected_protocol_stack[0] != NDPI_PROTOCOL_UNKNOWN) { } else if(flow->detected_protocol_stack[0] != NDPI_PROTOCOL_UNKNOWN) {
if(ndpi_init_packet(ndpi_str, flow, current_time_ms, packet_data, packetlen, input_info) != 0) if(ndpi_init_packet(ndpi_str, flow, current_time_ms, packet_data, packetlen, input_info) != 0)
return ret; return ret;
goto ret_protocols; goto ret_protocols;
} }
if(ndpi_init_packet(ndpi_str, flow, current_time_ms, packet_data, packetlen, input_info) != 0) if(ndpi_init_packet(ndpi_str, flow, current_time_ms, packet_data, packetlen, input_info) != 0)
return ret; return ret;
#ifdef HAVE_NBPF
if((flow->num_processed_pkts == 1) /* first packet of this flow to be analyzed */
&& (ndpi_str->nbpf_custom_proto[0].tree != NULL)) {
#if 0
const char *filter = "tcp and port 80";
nbpf_tree_t *tree = nbpf_parse(filter, NULL);
nbpf_free(tree);
#endif
u_int8_t i;
nbpf_pkt_info_t t;
memset(&t, 0, sizeof(t));
if(packet->iphv6 != NULL) {
t.tuple.eth_type = 0x86DD;
t.tuple.ip_version = 6;
memcpy(&t.tuple.ip_src.v6, &packet->iphv6->ip6_src, 16);
memcpy(&t.tuple.ip_dst.v6, &packet->iphv6->ip6_dst, 16);
} else {
t.tuple.eth_type = 0x0800;
t.tuple.ip_version = 4;
t.tuple.ip_src.v4 = packet->iph->saddr;
t.tuple.ip_dst.v4 = packet->iph->daddr;
}
t.tuple.l3_proto = flow->l4_proto;
t.tuple.l4_src_port = packet->tcp ? packet->tcp->source : packet->udp->source;
t.tuple.l4_dst_port = packet->tcp ? packet->tcp->dest : packet->udp->dest;
for(i=0; (i<MAX_NBPF_CUSTOM_PROTO) && (ndpi_str->nbpf_custom_proto[i].tree != NULL); i++) {
if(nbpf_match(ndpi_str->nbpf_custom_proto[i].tree, &t)) {
/* match found */
ret.master_protocol = ret.app_protocol = ndpi_str->nbpf_custom_proto[i].l7_protocol;
ndpi_fill_protocol_category(ndpi_str, flow, &ret);
ndpi_reconcile_protocols(ndpi_str, flow, &ret);
flow->confidence = NDPI_CONFIDENCE_NBPF;
return(ret);
}
}
}
#endif
ndpi_connection_tracking(ndpi_str, flow); ndpi_connection_tracking(ndpi_str, flow);
/* build ndpi_selection packet bitmask */ /* build ndpi_selection packet bitmask */
@@ -6330,9 +6381,10 @@ ndpi_protocol ndpi_detection_process_packet(struct ndpi_detection_module_struct
else else
ret.category = flow->category; ret.category = flow->category;
if((flow->num_processed_pkts == 1) && (ret.master_protocol == NDPI_PROTOCOL_UNKNOWN) && if((flow->num_processed_pkts == 1) /* first packet of this flow to be analyzed */
(ret.app_protocol == NDPI_PROTOCOL_UNKNOWN) && packet->tcp && (packet->tcp->syn == 0) && && (ret.master_protocol == NDPI_PROTOCOL_UNKNOWN)
(flow->guessed_protocol_id == 0)) { && (ret.app_protocol == NDPI_PROTOCOL_UNKNOWN) && packet->tcp && (packet->tcp->syn == 0)
&& (flow->guessed_protocol_id == 0)) {
u_int8_t protocol_was_guessed; u_int8_t protocol_was_guessed;
/* /*
@@ -7550,6 +7602,8 @@ const char *ndpi_confidence_get_name(ndpi_confidence_t confidence)
return "DPI (cache)"; return "DPI (cache)";
case NDPI_CONFIDENCE_DPI: case NDPI_CONFIDENCE_DPI:
return "DPI"; return "DPI";
case NDPI_CONFIDENCE_NBPF:
return "nBPF";
default: default:
return NULL; return NULL;
} }